Skip to content

Commit 9b1f62f

Browse files
committed
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]>
1 parent a3b6243 commit 9b1f62f

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
/* xdp_pull_data_prog will directly read a marker 0xbb stored at buf[1024]
8+
* so caller expecting XDP_PASS should always pass pull_len no less than 1024
9+
*/
10+
void test_xdp_pull_data_common(struct test_xdp_pull_data *skel,
11+
int buf_len, int linear_len,
12+
int pull_len, int retval)
13+
{
14+
LIBBPF_OPTS(bpf_test_run_opts, topts);
15+
struct xdp_md ctx = {};
16+
int prog_fd, err;
17+
__u8 *buf;
18+
19+
buf = calloc(buf_len, sizeof(__u8));
20+
if (!ASSERT_OK_PTR(buf, "calloc buf"))
21+
return;
22+
23+
buf[1023] = 0xaa;
24+
buf[1024] = 0xbb;
25+
buf[1025] = 0xcc;
26+
27+
topts.data_in = buf;
28+
topts.data_out = buf;
29+
topts.data_size_in = buf_len;
30+
topts.data_size_out = buf_len;
31+
ctx.data_end = linear_len;
32+
topts.ctx_in = &ctx;
33+
topts.ctx_out = &ctx;
34+
topts.ctx_size_in = sizeof(ctx);
35+
topts.ctx_size_out = sizeof(ctx);
36+
37+
skel->bss->linear_len = linear_len;
38+
skel->bss->pull_len = pull_len;
39+
40+
prog_fd = bpf_program__fd(skel->progs.xdp_pull_data_prog);
41+
err = bpf_prog_test_run_opts(prog_fd, &topts);
42+
ASSERT_OK(err, "bpf_prog_test_run_opts");
43+
ASSERT_EQ(topts.retval, retval, "xdp_pull_data_prog retval");
44+
45+
if (retval == XDP_DROP)
46+
goto out;
47+
48+
ASSERT_EQ(ctx.data_end, pull_len, "linear data size");
49+
ASSERT_EQ(topts.data_size_out, buf_len, "linear + non-linear data size");
50+
/* Make sure data around xdp->data_end was not messed up by
51+
* bpf_xdp_pull_data()
52+
*/
53+
ASSERT_EQ(buf[1023], 0xaa, "buf[1023]");
54+
ASSERT_EQ(buf[1024], 0xbb, "buf[1024]");
55+
ASSERT_EQ(buf[1025], 0xcc, "buf[1025]");
56+
out:
57+
free(buf);
58+
}
59+
60+
static void test_xdp_pull_data_basic(void)
61+
{
62+
struct test_xdp_pull_data *skel;
63+
u32 page_size;
64+
65+
skel = test_xdp_pull_data__open_and_load();
66+
if (!ASSERT_OK_PTR(skel, "test_xdp_pull_data__open_and_load"))
67+
return;
68+
69+
page_size = sysconf(_SC_PAGE_SIZE);
70+
71+
/* linear xdp pkt, pull 0 byte */
72+
test_xdp_pull_data_common(skel, 2048, 2048, 2048, XDP_PASS);
73+
/* multi-buf pkt, pull results in linear xdp pkt */
74+
test_xdp_pull_data_common(skel, 2048, 1024, 2048, XDP_PASS);
75+
/* multi-buf pkt, pull 1 byte to linear data area */
76+
test_xdp_pull_data_common(skel, 9000, 1024, 1025, XDP_PASS);
77+
/* multi-buf pkt, pull 0 byte to linear data area */
78+
test_xdp_pull_data_common(skel, 9000, 1025, 1025, XDP_PASS);
79+
80+
/* linear xdp pkt, pull more than total data len */
81+
test_xdp_pull_data_common(skel, 2048, 2048, 2049, XDP_DROP);
82+
/* multi-buf pkt with no space left in linear data area.
83+
* Since ctx.data_end (4096) > max_data_sz, bpf_prog_test_run_xdp()
84+
* will fill the whole linear data area and put the reset into a
85+
* fragment.
86+
*/
87+
test_xdp_pull_data_common(skel, page_size, page_size, page_size, XDP_DROP);
88+
89+
test_xdp_pull_data__destroy(skel);
90+
}
91+
92+
void test_xdp_pull_data(void)
93+
{
94+
if (test__start_subtest("xdp_pull_data"))
95+
test_xdp_pull_data_basic();
96+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
int _version SEC("version") = 1;
7+
8+
int linear_len;
9+
int pull_len;
10+
11+
SEC("xdp.frags")
12+
int xdp_pull_data_prog(struct xdp_md *xdp)
13+
{
14+
__u8 *data_end = (void *)(long)xdp->data_end;
15+
__u8 *data = (void *)(long)xdp->data;
16+
__u8 *val_p;
17+
int err;
18+
19+
if (linear_len != data_end - data)
20+
return XDP_DROP;
21+
22+
err = bpf_xdp_pull_data(xdp, pull_len, 0);
23+
if (err)
24+
return XDP_DROP;
25+
26+
val_p = (void *)(long)xdp->data + 1024;
27+
if (val_p + 1 > (void *)(long)xdp->data_end)
28+
return XDP_DROP;
29+
30+
if (*val_p != 0xbb)
31+
return XDP_DROP;
32+
33+
return XDP_PASS;
34+
}
35+
36+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)