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

selftests/bpf: check program redirect in xdp_cpumap_attach

xdp_cpumap_attach, in its current form, only checks that an xdp cpumap
program can be executed, but not that it performs correctly the cpu
redirect as configured by userspace (bpf_prog_test_run_opts will return
success even if the redirect program returns an error)

Add a check to ensure that the program performs the configured redirect
as well. The check is based on a global variable incremented by a
chained program executed only if the redirect program properly executes.

Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
Link: https://lore.kernel.org/r/20241009-convert_xdp_tests-v3-3-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
d124d984 d5fbcf46

+9 -1
+4 -1
tools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c
··· 62 62 err = bpf_prog_test_run_opts(prog_redir_fd, &opts); 63 63 ASSERT_OK(err, "XDP test run"); 64 64 65 - /* wait for the packets to be flushed */ 65 + /* wait for the packets to be flushed, then check that redirect has been 66 + * performed 67 + */ 66 68 kern_sync_rcu(); 69 + ASSERT_NEQ(skel->bss->redirect_count, 0, "redirected packets"); 67 70 68 71 err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL); 69 72 ASSERT_OK(err, "XDP program detach");
+5
tools/testing/selftests/bpf/progs/test_xdp_with_cpumap_helpers.c
··· 12 12 __uint(max_entries, 4); 13 13 } cpu_map SEC(".maps"); 14 14 15 + __u32 redirect_count = 0; 16 + 15 17 SEC("xdp") 16 18 int xdp_redir_prog(struct xdp_md *ctx) 17 19 { ··· 29 27 SEC("xdp/cpumap") 30 28 int xdp_dummy_cm(struct xdp_md *ctx) 31 29 { 30 + if (bpf_get_smp_processor_id() == 0) 31 + redirect_count++; 32 + 32 33 if (ctx->ingress_ifindex == IFINDEX_LO) 33 34 return XDP_DROP; 34 35