This adds a selftest to read the xdp_md net_device and extract common fields we use. This will also compare xdp_md->ifindex with the value extracted through the dev pointer. Signed-off-by: John Fastabend <john.fastabend@xxxxxxxxx> --- .../testing/selftests/bpf/prog_tests/xdp_md.c | 35 +++++++++++++++++++ .../testing/selftests/bpf/progs/test_xdp_md.c | 25 +++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_md.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_md.c diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_md.c b/tools/testing/selftests/bpf/prog_tests/xdp_md.c new file mode 100644 index 000000000000..facf3f3ab86f --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/xdp_md.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <network_helpers.h> +#include "test_xdp_md.skel.h" + +void test_xdp_md(void) +{ + struct test_xdp_md *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 = test_xdp_md__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + 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_md 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"); + + test_xdp_md__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_xdp_md.c b/tools/testing/selftests/bpf/progs/test_xdp_md.c new file mode 100644 index 000000000000..66ad4a7c80cd --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_xdp_md.c @@ -0,0 +1,25 @@ +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <string.h> + +#define IFNAMSIZ 16 + +int ifindex, ingress_ifindex; +char name[IFNAMSIZ]; +unsigned int inum; + +SEC("xdp") +int md_xdp(struct xdp_md *ctx) +{ + struct net_device *dev; + + dev = ctx->rx_dev; + + ifindex = dev->ifindex; + inum = dev->nd_net.net->ns.inum; + memcpy(name, dev->name, IFNAMSIZ); + ingress_ifindex = ctx->ingress_ifindex; + return XDP_PASS; +} + +char _license[] SEC("license") = "GPL"; -- 2.33.0