Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: make xdp_cpumap_attach keep redirect prog attached

Current test only checks attach/detach on cpu map type program, and so
does not check that it can be properly executed, neither that it
redirects correctly.

Update the existing test to extend its coverage:
- keep the redirected program loaded
- try to execute it through bpf_prog_test_run_opts with some dummy
context

While at it, bring the following minor improvements:
- isolate test interface in its own namespace

Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
Link: https://lore.kernel.org/r/20241009-convert_xdp_tests-v3-2-51cea913710c@bootlin.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

authored by

Alexis Lothoré (eBPF Foundation) and committed by
Martin KaFai Lau
d5fbcf46 ac8d16b2

+33 -8
+33 -8
tools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c
··· 2 2 #include <uapi/linux/bpf.h> 3 3 #include <linux/if_link.h> 4 4 #include <test_progs.h> 5 + #include <network_helpers.h> 5 6 6 7 #include "test_xdp_with_cpumap_frags_helpers.skel.h" 7 8 #include "test_xdp_with_cpumap_helpers.skel.h" 8 9 9 10 #define IFINDEX_LO 1 11 + #define TEST_NS "cpu_attach_ns" 10 12 11 13 static void test_xdp_with_cpumap_helpers(void) 12 14 { 13 - struct test_xdp_with_cpumap_helpers *skel; 15 + struct test_xdp_with_cpumap_helpers *skel = NULL; 14 16 struct bpf_prog_info info = {}; 15 17 __u32 len = sizeof(info); 16 18 struct bpf_cpumap_val val = { 17 19 .qsize = 192, 18 20 }; 19 - int err, prog_fd, map_fd; 21 + int err, prog_fd, prog_redir_fd, map_fd; 22 + struct nstoken *nstoken = NULL; 20 23 __u32 idx = 0; 24 + 25 + SYS(out_close, "ip netns add %s", TEST_NS); 26 + nstoken = open_netns(TEST_NS); 27 + if (!ASSERT_OK_PTR(nstoken, "open_netns")) 28 + goto out_close; 29 + SYS(out_close, "ip link set dev lo up"); 21 30 22 31 skel = test_xdp_with_cpumap_helpers__open_and_load(); 23 32 if (!ASSERT_OK_PTR(skel, "test_xdp_with_cpumap_helpers__open_and_load")) 24 33 return; 25 34 26 - prog_fd = bpf_program__fd(skel->progs.xdp_redir_prog); 27 - err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL); 35 + prog_redir_fd = bpf_program__fd(skel->progs.xdp_redir_prog); 36 + err = bpf_xdp_attach(IFINDEX_LO, prog_redir_fd, XDP_FLAGS_SKB_MODE, NULL); 28 37 if (!ASSERT_OK(err, "Generic attach of program with 8-byte CPUMAP")) 29 38 goto out_close; 30 - 31 - err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL); 32 - ASSERT_OK(err, "XDP program detach"); 33 39 34 40 prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm); 35 41 map_fd = bpf_map__fd(skel->maps.cpu_map); ··· 50 44 err = bpf_map_lookup_elem(map_fd, &idx, &val); 51 45 ASSERT_OK(err, "Read cpumap entry"); 52 46 ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to cpumap entry prog_id"); 47 + 48 + /* send a packet to trigger any potential bugs in there */ 49 + char data[10] = {}; 50 + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, 51 + .data_in = &data, 52 + .data_size_in = 10, 53 + .flags = BPF_F_TEST_XDP_LIVE_FRAMES, 54 + .repeat = 1, 55 + ); 56 + err = bpf_prog_test_run_opts(prog_redir_fd, &opts); 57 + ASSERT_OK(err, "XDP test run"); 58 + 59 + /* wait for the packets to be flushed */ 60 + kern_sync_rcu(); 61 + 62 + err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL); 63 + ASSERT_OK(err, "XDP program detach"); 53 64 54 65 /* can not attach BPF_XDP_CPUMAP program to a device */ 55 66 err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL); ··· 88 65 ASSERT_NEQ(err, 0, "Add BPF_XDP program with frags to cpumap entry"); 89 66 90 67 out_close: 68 + close_netns(nstoken); 69 + SYS_NOFAIL("ip netns del %s", TEST_NS); 91 70 test_xdp_with_cpumap_helpers__destroy(skel); 92 71 } 93 72 ··· 136 111 test_xdp_with_cpumap_frags_helpers__destroy(skel); 137 112 } 138 113 139 - void serial_test_xdp_cpumap_attach(void) 114 + void test_xdp_cpumap_attach(void) 140 115 { 141 116 if (test__start_subtest("CPUMAP with programs in entries")) 142 117 test_xdp_with_cpumap_helpers();