Skip to content

Commit a8057ed

Browse files
author
Martin KaFai Lau
committed
Merge branch 'selftests/bpf: add coverage for xdp_features in test_progs'
Alexis Lothoré says: ==================== this small series aims to increase coverage of xdp features in test_progs. The initial versions proposed to rework test_xdp_features.sh to make it fit in test_progs, but some discussions in v1 and v2 showed that the script is still needed as a standalone tool. So this new revision lets test_xdp_features.sh as-is, and rather adds missing coverage in existing test (cpu map). The new revision is now also a follow-up to the update performed by Florian Kauer in [1] for devmap programs testing. [1] https://lore.kernel.org/bpf/20240911-devel-koalo-fix-ingress-ifindex-v4-2-5c643ae10258@linutronix.de/ --- Changes in v3: - Drop xdp_features rework commit - update xdp_cpumap_attach to extend its coverage - Link to v2: https://lore.kernel.org/r/[email protected] Changes in v2: - fix endianness management in userspace packet parsing (call htonl on constant rather than packet part) The new test has been run in a local x86 environment and in CI: torvalds#560/1 xdp_cpumap_attach/CPUMAP with programs in entries:OK torvalds#560/2 xdp_cpumap_attach/CPUMAP with frags programs in entries:OK torvalds#560 xdp_cpumap_attach:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED ==================== Signed-off-by: Martin KaFai Lau <[email protected]>
2 parents 693fe95 + d124d98 commit a8057ed

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

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

Lines changed: 36 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,26 @@ 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, then check that redirect has been
66+
* performed
67+
*/
68+
kern_sync_rcu();
69+
ASSERT_NEQ(skel->bss->redirect_count, 0, "redirected packets");
70+
71+
err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
72+
ASSERT_OK(err, "XDP program detach");
73+
4874
/* can not attach BPF_XDP_CPUMAP program to a device */
4975
err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
5076
if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_CPUMAP program"))
@@ -65,6 +91,8 @@ static void test_xdp_with_cpumap_helpers(void)
6591
ASSERT_NEQ(err, 0, "Add BPF_XDP program with frags to cpumap entry");
6692

6793
out_close:
94+
close_netns(nstoken);
95+
SYS_NOFAIL("ip netns del %s", TEST_NS);
6896
test_xdp_with_cpumap_helpers__destroy(skel);
6997
}
7098

@@ -111,7 +139,7 @@ static void test_xdp_with_cpumap_frags_helpers(void)
111139
test_xdp_with_cpumap_frags_helpers__destroy(skel);
112140
}
113141

114-
void serial_test_xdp_cpumap_attach(void)
142+
void test_xdp_cpumap_attach(void)
115143
{
116144
if (test__start_subtest("CPUMAP with programs in entries"))
117145
test_xdp_with_cpumap_helpers();

tools/testing/selftests/bpf/progs/test_xdp_with_cpumap_helpers.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ struct {
1212
__uint(max_entries, 4);
1313
} cpu_map SEC(".maps");
1414

15+
__u32 redirect_count = 0;
16+
1517
SEC("xdp")
1618
int xdp_redir_prog(struct xdp_md *ctx)
1719
{
18-
return bpf_redirect_map(&cpu_map, 1, 0);
20+
return bpf_redirect_map(&cpu_map, 0, 0);
1921
}
2022

2123
SEC("xdp")
@@ -27,6 +29,9 @@ int xdp_dummy_prog(struct xdp_md *ctx)
2729
SEC("xdp/cpumap")
2830
int xdp_dummy_cm(struct xdp_md *ctx)
2931
{
32+
if (bpf_get_smp_processor_id() == 0)
33+
redirect_count++;
34+
3035
if (ctx->ingress_ifindex == IFINDEX_LO)
3136
return XDP_DROP;
3237

0 commit comments

Comments
 (0)