5ecd8c2273
Add selftests for bpf_skb_ancestor_cgroup_id helper. test_skb_cgroup_id.sh prepares testing interface and adds tc qdisc and filter for it using BPF object compiled from test_skb_cgroup_id_kern.c program. BPF program in test_skb_cgroup_id_kern.c gets ancestor cgroup id using the new helper at different levels of cgroup hierarchy that skb belongs to, including root level and non-existing level, and saves it to the map where the key is the level of corresponding cgroup and the value is its id. To trigger BPF program, user space program test_skb_cgroup_id_user is run. It adds itself into testing cgroup and sends UDP datagram to link-local multicast address of testing interface. Then it reads cgroup ids saved in kernel for different levels from the BPF map and compares them with those in user space. They must be equal for every level of ancestry. Example of run: # ./test_skb_cgroup_id.sh Wait for testing link-local IP to become available ... OK Note: 8 bytes struct bpf_elf_map fixup performed due to size mismatch! [PASS] Signed-off-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
48 lines
1.0 KiB
C
48 lines
1.0 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
// Copyright (c) 2018 Facebook
|
|
|
|
#include <linux/bpf.h>
|
|
#include <linux/pkt_cls.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "bpf_helpers.h"
|
|
|
|
#define NUM_CGROUP_LEVELS 4
|
|
|
|
struct bpf_map_def SEC("maps") cgroup_ids = {
|
|
.type = BPF_MAP_TYPE_ARRAY,
|
|
.key_size = sizeof(__u32),
|
|
.value_size = sizeof(__u64),
|
|
.max_entries = NUM_CGROUP_LEVELS,
|
|
};
|
|
|
|
static __always_inline void log_nth_level(struct __sk_buff *skb, __u32 level)
|
|
{
|
|
__u64 id;
|
|
|
|
/* [1] &level passed to external function that may change it, it's
|
|
* incompatible with loop unroll.
|
|
*/
|
|
id = bpf_skb_ancestor_cgroup_id(skb, level);
|
|
bpf_map_update_elem(&cgroup_ids, &level, &id, 0);
|
|
}
|
|
|
|
SEC("cgroup_id_logger")
|
|
int log_cgroup_id(struct __sk_buff *skb)
|
|
{
|
|
/* Loop unroll can't be used here due to [1]. Unrolling manually.
|
|
* Number of calls should be in sync with NUM_CGROUP_LEVELS.
|
|
*/
|
|
log_nth_level(skb, 0);
|
|
log_nth_level(skb, 1);
|
|
log_nth_level(skb, 2);
|
|
log_nth_level(skb, 3);
|
|
|
|
return TC_ACT_OK;
|
|
}
|
|
|
|
int _version SEC("version") = 1;
|
|
|
|
char _license[] SEC("license") = "GPL";
|