Two tests are added. One is from John Fastabend ({1]) which tests tracing style access for xdp program from the kernel ctx. Another is a tc test to test both kernel ctx tracing style access and explicit non-ctx type cast. [1] https://lore.kernel.org/bpf/20221109215242.1279993-1-john.fastabend@xxxxxxxxx/ Signed-off-by: Yonghong Song <yhs@xxxxxx> --- .../bpf/prog_tests/get_kern_btf_id.c | 81 +++++++++++++++++++ .../selftests/bpf/progs/get_kern_btf_id.c | 44 ++++++++++ 2 files changed, 125 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c create mode 100644 tools/testing/selftests/bpf/progs/get_kern_btf_id.c diff --git a/tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c b/tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c new file mode 100644 index 000000000000..205631916564 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <network_helpers.h> +#include "get_kern_btf_id.skel.h" + +static void test_xdp(void) +{ + struct get_kern_btf_id *skel; + int err, prog_fd; + char buf[128]; + + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .data_out = buf, + .data_size_out = sizeof(buf), + .repeat = 1, + ); + + skel = get_kern_btf_id__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + bpf_program__set_autoload(skel->progs.md_xdp, true); + err = get_kern_btf_id__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto out; + + prog_fd = bpf_program__fd(skel->progs.md_xdp); + err = bpf_prog_test_run_opts(prog_fd, &topts); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(topts.retval, XDP_PASS, "xdp test_run retval"); + + ASSERT_EQ(skel->bss->ifindex, 1, "xdp_md ifindex"); + ASSERT_EQ(skel->bss->ifindex, skel->bss->ingress_ifindex, "xdp_md ingress_ifindex"); + ASSERT_STREQ(skel->bss->name, "lo", "xdp_md name"); + ASSERT_NEQ(skel->bss->inum, 0, "xdp_md inum"); + +out: + get_kern_btf_id__destroy(skel); +} + +static void test_tc(void) +{ + struct get_kern_btf_id *skel; + int err, prog_fd; + + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + + skel = get_kern_btf_id__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + bpf_program__set_autoload(skel->progs.md_skb, true); + err = get_kern_btf_id__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto out; + + prog_fd = bpf_program__fd(skel->progs.md_skb); + err = bpf_prog_test_run_opts(prog_fd, &topts); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(topts.retval, 0, "tc test_run retval"); + + ASSERT_EQ(skel->bss->meta_len, 0, "skb meta_len"); + ASSERT_EQ(skel->bss->frag0_len, 0, "skb frag0_len"); + +out: + get_kern_btf_id__destroy(skel); +} + +void test_get_kern_btf_id(void) +{ + if (test__start_subtest("xdp")) + test_xdp(); + if (test__start_subtest("tc")) + test_tc(); +} diff --git a/tools/testing/selftests/bpf/progs/get_kern_btf_id.c b/tools/testing/selftests/bpf/progs/get_kern_btf_id.c new file mode 100644 index 000000000000..b530c7c52ff3 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/get_kern_btf_id.c @@ -0,0 +1,44 @@ +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_core_read.h> + +#define IFNAMSIZ 16 + +int ifindex, ingress_ifindex; +char name[IFNAMSIZ]; +unsigned int inum; +int meta_len, frag0_len; + +extern void *bpf_get_kern_btf_id(void *, __u32) __ksym; + +SEC("?xdp") +int md_xdp(struct xdp_md *ctx) +{ + struct xdp_buff *kctx = bpf_get_kern_btf_id(ctx, 0); + struct net_device *dev; + + dev = kctx->rxq->dev; + ifindex = dev->ifindex; + inum = dev->nd_net.net->ns.inum; + __builtin_memcpy(name, dev->name, IFNAMSIZ); + ingress_ifindex = ctx->ingress_ifindex; + return XDP_PASS; +} + +SEC("?tc") +int md_skb(struct __sk_buff *skb) +{ + struct sk_buff *kskb = bpf_get_kern_btf_id(skb, 0); + struct skb_shared_info *shared_info; + + /* Simulate the following kernel macro: + * #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB))) + */ + shared_info = bpf_get_kern_btf_id(kskb->head + kskb->end, + bpf_core_type_id_kernel(struct skb_shared_info)); + meta_len = shared_info->meta_len; + frag0_len = shared_info->frag_list->len; + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.30.2