linux/tools/testing/selftests/bpf/progs/cgroup_ancestor.c
Alexis Lothoré (eBPF Foundation) f957c230e1 selftests/bpf: convert test_skb_cgroup_id_user to test_progs
test_skb_cgroup_id_user allows testing skb cgroup id retrieval at different
levels, but is not integrated in test_progs, so it is not run
automatically in CI. The test overlaps a bit with
cgroup_skb_sk_lookup_kern, which is integrated in test_progs and test
extensively skb cgroup helpers, but there is still one major difference
between the two tests which justifies the conversion:
cgroup_skb_sk_lookup_kern deals with a BPF_PROG_TYPE_CGROUP_SKB (attached
on a cgroup), while test_skb_cgroup_id_user deals with a
BPF_PROG_TYPE_SCHED_CLS (attached on a qdisc)

Convert test_skb_cgroup_id_user into test_progs framework in order to run
it automatically in CI. The main differences with the original test are the
following:
- rename the test to make it shorter and more straightforward regarding
  tested feature
- the wrapping shell script has been dropped since every setup step is now
  handled in the main C test file
- the test has been renamed for a shorter name and reflecting the tested
  API
- add dedicated assert log per level to ease test failure debugging
- use global variables instead of maps to access bpf prog data

Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
Link: https://lore.kernel.org/r/20240813-convert_cgroup_tests-v4-4-a33c03458cf6@bootlin.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
2024-08-14 18:49:57 -07:00

40 lines
903 B
C

// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2018 Facebook
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_tracing_net.h"
#define NUM_CGROUP_LEVELS 4
__u64 cgroup_ids[NUM_CGROUP_LEVELS];
__u16 dport;
static __always_inline void log_nth_level(struct __sk_buff *skb, __u32 level)
{
/* [1] &level passed to external function that may change it, it's
* incompatible with loop unroll.
*/
cgroup_ids[level] = bpf_skb_ancestor_cgroup_id(skb, level);
}
SEC("tc")
int log_cgroup_id(struct __sk_buff *skb)
{
struct sock *sk = (void *)skb->sk;
if (!sk)
return TC_ACT_OK;
sk = bpf_core_cast(sk, struct sock);
if (sk->sk_protocol == IPPROTO_UDP && sk->sk_dport == dport) {
log_nth_level(skb, 0);
log_nth_level(skb, 1);
log_nth_level(skb, 2);
log_nth_level(skb, 3);
}
return TC_ACT_OK;
}
char _license[] SEC("license") = "GPL";