Skip to content

Commit 1c67c5c

Browse files
mrpreKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: add perf test for adjust_{head,meta}
We added a memset operation during the adjust operation, which may cause performance issues. Therefore, we added perf testing, and testing found that for common header length operations, memset() operation increased the performance overhead by 2ns, which is negligible for the net stack. Before memset ./test_progs -a xdp_adjust_head_perf -v run adjust head with size 6 cost 56 ns run adjust head with size 20 cost 56 ns run adjust head with size 40 cost 56 ns run adjust head with size 200 cost 56 ns After memset ./test_progs -a xdp_adjust_head_perf -v run adjust head with size 6 cost 58 ns run adjust head with size 20 cost 58 ns run adjust head with size 40 cost 58 ns run adjust head with size 200 cost 66 ns Signed-off-by: Jiayuan Chen <[email protected]>
1 parent 15b7044 commit 1c67c5c

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

Diff for: tools/testing/selftests/bpf/prog_tests/xdp_perf.c

+45-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <test_progs.h>
3+
#include <network_helpers.h>
4+
#include "xdp_dummy.skel.h"
35

46
void test_xdp_perf(void)
57
{
6-
const char *file = "./xdp_dummy.bpf.o";
7-
struct bpf_object *obj;
8+
struct xdp_dummy *skel;
89
char in[128], out[128];
910
int err, prog_fd;
1011
LIBBPF_OPTS(bpf_test_run_opts, topts,
@@ -15,14 +16,51 @@ void test_xdp_perf(void)
1516
.repeat = 1000000,
1617
);
1718

18-
err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
19-
if (CHECK_FAIL(err))
20-
return;
21-
19+
skel = xdp_dummy__open_and_load();
20+
prog_fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
2221
err = bpf_prog_test_run_opts(prog_fd, &topts);
2322
ASSERT_OK(err, "test_run");
2423
ASSERT_EQ(topts.retval, XDP_PASS, "test_run retval");
2524
ASSERT_EQ(topts.data_size_out, 128, "test_run data_size_out");
2625

27-
bpf_object__close(obj);
26+
xdp_dummy__destroy(skel);
27+
}
28+
29+
void test_xdp_adjust_head_perf(void)
30+
{
31+
struct xdp_dummy *skel;
32+
int repeat = 9000000;
33+
struct xdp_md ctx_in;
34+
char data[100];
35+
int err, prog_fd;
36+
size_t test_header_size[] = {
37+
ETH_ALEN,
38+
sizeof(struct iphdr),
39+
sizeof(struct ipv6hdr),
40+
200,
41+
};
42+
DECLARE_LIBBPF_OPTS(bpf_test_run_opts, topts,
43+
.data_in = &data,
44+
.data_size_in = sizeof(data),
45+
.repeat = repeat,
46+
);
47+
48+
topts.ctx_in = &ctx_in;
49+
topts.ctx_size_in = sizeof(ctx_in);
50+
memset(&ctx_in, 0, sizeof(ctx_in));
51+
ctx_in.data_meta = 0;
52+
ctx_in.data_end = ctx_in.data + sizeof(data);
53+
54+
skel = xdp_dummy__open_and_load();
55+
prog_fd = bpf_program__fd(skel->progs.xdp_dummy_adjust_head);
56+
57+
for (int i = 0; i < ARRAY_SIZE(test_header_size); i++) {
58+
skel->bss->head_size = test_header_size[i];
59+
err = bpf_prog_test_run_opts(prog_fd, &topts);
60+
ASSERT_OK(err, "test_run");
61+
ASSERT_EQ(topts.retval, XDP_PASS, "test_run retval");
62+
fprintf(stdout, "run adjust head with size %zd cost %d ns\n",
63+
test_header_size[i], topts.duration);
64+
}
65+
xdp_dummy__destroy(skel);
2866
}

Diff for: tools/testing/selftests/bpf/progs/xdp_dummy.c

+14
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,24 @@
44
#include <linux/bpf.h>
55
#include <bpf/bpf_helpers.h>
66

7+
int head_size;
8+
79
SEC("xdp")
810
int xdp_dummy_prog(struct xdp_md *ctx)
911
{
1012
return XDP_PASS;
1113
}
1214

15+
SEC("xdp")
16+
int xdp_dummy_adjust_head(struct xdp_md *ctx)
17+
{
18+
if (bpf_xdp_adjust_head(ctx, -head_size))
19+
return XDP_DROP;
20+
21+
if (bpf_xdp_adjust_head(ctx, head_size))
22+
return XDP_DROP;
23+
24+
return XDP_PASS;
25+
}
26+
1327
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)