Skip to content

Commit d5fbcf4

Browse files
TropicaoMartin KaFai Lau
authored andcommitted
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) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent ac8d16b commit d5fbcf4

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

tools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,41 @@
22
#include <uapi/linux/bpf.h>
33
#include <linux/if_link.h>
44
#include <test_progs.h>
5+
#include <network_helpers.h>
56

67
#include "test_xdp_with_cpumap_frags_helpers.skel.h"
78
#include "test_xdp_with_cpumap_helpers.skel.h"
89

910
#define IFINDEX_LO 1
11+
#define TEST_NS "cpu_attach_ns"
1012

1113
static void test_xdp_with_cpumap_helpers(void)
1214
{
13-
struct test_xdp_with_cpumap_helpers *skel;
15+
struct test_xdp_with_cpumap_helpers *skel = NULL;
1416
struct bpf_prog_info info = {};
1517
__u32 len = sizeof(info);
1618
struct bpf_cpumap_val val = {
1719
.qsize = 192,
1820
};
19-
int err, prog_fd, map_fd;
21+
int err, prog_fd, prog_redir_fd, map_fd;
22+
struct nstoken *nstoken = NULL;
2023
__u32 idx = 0;
2124

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");
30+
2231
skel = test_xdp_with_cpumap_helpers__open_and_load();
2332
if (!ASSERT_OK_PTR(skel, "test_xdp_with_cpumap_helpers__open_and_load"))
2433
return;
2534

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);
2837
if (!ASSERT_OK(err, "Generic attach of program with 8-byte CPUMAP"))
2938
goto out_close;
3039

31-
err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
32-
ASSERT_OK(err, "XDP program detach");
33-
3440
prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm);
3541
map_fd = bpf_map__fd(skel->maps.cpu_map);
3642
err = bpf_prog_get_info_by_fd(prog_fd, &info, &len);
@@ -45,6 +51,23 @@ static void test_xdp_with_cpumap_helpers(void)
4551
ASSERT_OK(err, "Read cpumap entry");
4652
ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to cpumap entry prog_id");
4753

54+
/* send a packet to trigger any potential bugs in there */
55+
char data[10] = {};
56+
DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
57+
.data_in = &data,
58+
.data_size_in = 10,
59+
.flags = BPF_F_TEST_XDP_LIVE_FRAMES,
60+
.repeat = 1,
61+
);
62+
err = bpf_prog_test_run_opts(prog_redir_fd, &opts);
63+
ASSERT_OK(err, "XDP test run");
64+
65+
/* wait for the packets to be flushed */
66+
kern_sync_rcu();
67+
68+
err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
69+
ASSERT_OK(err, "XDP program detach");
70+
4871
/* can not attach BPF_XDP_CPUMAP program to a device */
4972
err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
5073
if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_CPUMAP program"))
@@ -65,6 +88,8 @@ static void test_xdp_with_cpumap_helpers(void)
6588
ASSERT_NEQ(err, 0, "Add BPF_XDP program with frags to cpumap entry");
6689

6790
out_close:
91+
close_netns(nstoken);
92+
SYS_NOFAIL("ip netns del %s", TEST_NS);
6893
test_xdp_with_cpumap_helpers__destroy(skel);
6994
}
7095

@@ -111,7 +136,7 @@ static void test_xdp_with_cpumap_frags_helpers(void)
111136
test_xdp_with_cpumap_frags_helpers__destroy(skel);
112137
}
113138

114-
void serial_test_xdp_cpumap_attach(void)
139+
void test_xdp_cpumap_attach(void)
115140
{
116141
if (test__start_subtest("CPUMAP with programs in entries"))
117142
test_xdp_with_cpumap_helpers();

0 commit comments

Comments
 (0)