Skip to content

Commit 323302f

Browse files
ameryhungMartin KaFai Lau
authored andcommitted
selftests/bpf: Test bpf_xdp_pull_data
Test bpf_xdp_pull_data() with xdp packets with different layouts. The xdp bpf program first checks if the layout is as expected. Then, it calls bpf_xdp_pull_data(). Finally, it checks the 0xbb marker at offset 1024 using directly packet access. Signed-off-by: Amery Hung <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent fe9544e commit 323302f

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include <network_helpers.h>
5+
#include "test_xdp_pull_data.skel.h"
6+
7+
#define PULL_MAX (1 << 31)
8+
#define PULL_PLUS_ONE (1 << 30)
9+
10+
#define XDP_PACKET_HEADROOM 256
11+
12+
/* Find headroom and tailroom occupied by struct xdp_frame and struct
13+
* skb_shared_info so that we can calculate the maximum pull lengths for
14+
* test cases. They might not be the real size of the structures due to
15+
* cache alignment.
16+
*/
17+
static int find_xdp_sizes(struct test_xdp_pull_data *skel, int frame_sz)
18+
{
19+
LIBBPF_OPTS(bpf_test_run_opts, topts);
20+
struct xdp_md ctx = {};
21+
int prog_fd, err;
22+
__u8 *buf;
23+
24+
buf = calloc(frame_sz, sizeof(__u8));
25+
if (!ASSERT_OK_PTR(buf, "calloc buf"))
26+
return -ENOMEM;
27+
28+
topts.data_in = buf;
29+
topts.data_out = buf;
30+
topts.data_size_in = frame_sz;
31+
topts.data_size_out = frame_sz;
32+
/* Pass a data_end larger than the linear space available to make sure
33+
* bpf_prog_test_run_xdp() will fill the linear data area so that
34+
* xdp_find_sizes can infer the size of struct skb_shared_info
35+
*/
36+
ctx.data_end = frame_sz;
37+
topts.ctx_in = &ctx;
38+
topts.ctx_out = &ctx;
39+
topts.ctx_size_in = sizeof(ctx);
40+
topts.ctx_size_out = sizeof(ctx);
41+
42+
prog_fd = bpf_program__fd(skel->progs.xdp_find_sizes);
43+
err = bpf_prog_test_run_opts(prog_fd, &topts);
44+
ASSERT_OK(err, "bpf_prog_test_run_opts");
45+
46+
free(buf);
47+
48+
return err;
49+
}
50+
51+
/* xdp_pull_data_prog will directly read a marker 0xbb stored at buf[1024]
52+
* so caller expecting XDP_PASS should always pass pull_len no less than 1024
53+
*/
54+
static void run_test(struct test_xdp_pull_data *skel, int retval,
55+
int frame_sz, int buff_len, int meta_len, int data_len,
56+
int pull_len)
57+
{
58+
LIBBPF_OPTS(bpf_test_run_opts, topts);
59+
struct xdp_md ctx = {};
60+
int prog_fd, err;
61+
__u8 *buf;
62+
63+
buf = calloc(buff_len, sizeof(__u8));
64+
if (!ASSERT_OK_PTR(buf, "calloc buf"))
65+
return;
66+
67+
buf[meta_len + 1023] = 0xaa;
68+
buf[meta_len + 1024] = 0xbb;
69+
buf[meta_len + 1025] = 0xcc;
70+
71+
topts.data_in = buf;
72+
topts.data_out = buf;
73+
topts.data_size_in = buff_len;
74+
topts.data_size_out = buff_len;
75+
ctx.data = meta_len;
76+
ctx.data_end = meta_len + data_len;
77+
topts.ctx_in = &ctx;
78+
topts.ctx_out = &ctx;
79+
topts.ctx_size_in = sizeof(ctx);
80+
topts.ctx_size_out = sizeof(ctx);
81+
82+
skel->bss->data_len = data_len;
83+
if (pull_len & PULL_MAX) {
84+
int headroom = XDP_PACKET_HEADROOM - meta_len - skel->bss->xdpf_sz;
85+
int tailroom = frame_sz - XDP_PACKET_HEADROOM -
86+
data_len - skel->bss->sinfo_sz;
87+
88+
pull_len = pull_len & PULL_PLUS_ONE ? 1 : 0;
89+
pull_len += headroom + tailroom + data_len;
90+
}
91+
skel->bss->pull_len = pull_len;
92+
93+
prog_fd = bpf_program__fd(skel->progs.xdp_pull_data_prog);
94+
err = bpf_prog_test_run_opts(prog_fd, &topts);
95+
ASSERT_OK(err, "bpf_prog_test_run_opts");
96+
ASSERT_EQ(topts.retval, retval, "xdp_pull_data_prog retval");
97+
98+
if (retval == XDP_DROP)
99+
goto out;
100+
101+
ASSERT_EQ(ctx.data_end, meta_len + pull_len, "linear data size");
102+
ASSERT_EQ(topts.data_size_out, buff_len, "linear + non-linear data size");
103+
/* Make sure data around xdp->data_end was not messed up by
104+
* bpf_xdp_pull_data()
105+
*/
106+
ASSERT_EQ(buf[meta_len + 1023], 0xaa, "data[1023]");
107+
ASSERT_EQ(buf[meta_len + 1024], 0xbb, "data[1024]");
108+
ASSERT_EQ(buf[meta_len + 1025], 0xcc, "data[1025]");
109+
out:
110+
free(buf);
111+
}
112+
113+
static void test_xdp_pull_data_basic(void)
114+
{
115+
u32 pg_sz, max_meta_len, max_data_len;
116+
struct test_xdp_pull_data *skel;
117+
118+
skel = test_xdp_pull_data__open_and_load();
119+
if (!ASSERT_OK_PTR(skel, "test_xdp_pull_data__open_and_load"))
120+
return;
121+
122+
pg_sz = sysconf(_SC_PAGE_SIZE);
123+
124+
if (find_xdp_sizes(skel, pg_sz))
125+
goto out;
126+
127+
max_meta_len = XDP_PACKET_HEADROOM - skel->bss->xdpf_sz;
128+
max_data_len = pg_sz - XDP_PACKET_HEADROOM - skel->bss->sinfo_sz;
129+
130+
/* linear xdp pkt, pull 0 byte */
131+
run_test(skel, XDP_PASS, pg_sz, 2048, 0, 2048, 2048);
132+
133+
/* multi-buf pkt, pull results in linear xdp pkt */
134+
run_test(skel, XDP_PASS, pg_sz, 2048, 0, 1024, 2048);
135+
136+
/* multi-buf pkt, pull 1 byte to linear data area */
137+
run_test(skel, XDP_PASS, pg_sz, 9000, 0, 1024, 1025);
138+
139+
/* multi-buf pkt, pull 0 byte to linear data area */
140+
run_test(skel, XDP_PASS, pg_sz, 9000, 0, 1025, 1025);
141+
142+
/* multi-buf pkt, empty linear data area, pull requires memmove */
143+
run_test(skel, XDP_PASS, pg_sz, 9000, 0, 0, PULL_MAX);
144+
145+
/* multi-buf pkt, no headroom */
146+
run_test(skel, XDP_PASS, pg_sz, 9000, max_meta_len, 1024, PULL_MAX);
147+
148+
/* multi-buf pkt, no tailroom, pull requires memmove */
149+
run_test(skel, XDP_PASS, pg_sz, 9000, 0, max_data_len, PULL_MAX);
150+
151+
/* Test cases with invalid pull length */
152+
153+
/* linear xdp pkt, pull more than total data len */
154+
run_test(skel, XDP_DROP, pg_sz, 2048, 0, 2048, 2049);
155+
156+
/* multi-buf pkt with no space left in linear data area */
157+
run_test(skel, XDP_DROP, pg_sz, 9000, max_meta_len, max_data_len,
158+
PULL_MAX | PULL_PLUS_ONE);
159+
160+
/* multi-buf pkt, empty linear data area */
161+
run_test(skel, XDP_DROP, pg_sz, 9000, 0, 0, PULL_MAX | PULL_PLUS_ONE);
162+
163+
/* multi-buf pkt, no headroom */
164+
run_test(skel, XDP_DROP, pg_sz, 9000, max_meta_len, 1024,
165+
PULL_MAX | PULL_PLUS_ONE);
166+
167+
/* multi-buf pkt, no tailroom */
168+
run_test(skel, XDP_DROP, pg_sz, 9000, 0, max_data_len,
169+
PULL_MAX | PULL_PLUS_ONE);
170+
171+
out:
172+
test_xdp_pull_data__destroy(skel);
173+
}
174+
175+
void test_xdp_pull_data(void)
176+
{
177+
if (test__start_subtest("xdp_pull_data"))
178+
test_xdp_pull_data_basic();
179+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
int xdpf_sz;
7+
int sinfo_sz;
8+
int data_len;
9+
int pull_len;
10+
11+
#define XDP_PACKET_HEADROOM 256
12+
13+
SEC("xdp.frags")
14+
int xdp_find_sizes(struct xdp_md *ctx)
15+
{
16+
xdpf_sz = sizeof(struct xdp_frame);
17+
sinfo_sz = __PAGE_SIZE - XDP_PACKET_HEADROOM -
18+
(ctx->data_end - ctx->data);
19+
20+
return XDP_PASS;
21+
}
22+
23+
SEC("xdp.frags")
24+
int xdp_pull_data_prog(struct xdp_md *ctx)
25+
{
26+
__u8 *data_end = (void *)(long)ctx->data_end;
27+
__u8 *data = (void *)(long)ctx->data;
28+
__u8 *val_p;
29+
int err;
30+
31+
if (data_len != data_end - data)
32+
return XDP_DROP;
33+
34+
err = bpf_xdp_pull_data(ctx, pull_len);
35+
if (err)
36+
return XDP_DROP;
37+
38+
val_p = (void *)(long)ctx->data + 1024;
39+
if (val_p + 1 > (void *)(long)ctx->data_end)
40+
return XDP_DROP;
41+
42+
if (*val_p != 0xbb)
43+
return XDP_DROP;
44+
45+
return XDP_PASS;
46+
}
47+
48+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)