xref: /linux/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
test_xdp_adjust_tail_shrink(void)5 static void test_xdp_adjust_tail_shrink(void)
6 {
7 	const char *file = "./test_xdp_adjust_tail_shrink.bpf.o";
8 	__u32 expect_sz;
9 	struct bpf_object *obj;
10 	int err, prog_fd;
11 	char buf[128];
12 	LIBBPF_OPTS(bpf_test_run_opts, topts,
13 		.data_in = &pkt_v4,
14 		.data_size_in = sizeof(pkt_v4),
15 		.data_out = buf,
16 		.data_size_out = sizeof(buf),
17 		.repeat = 1,
18 	);
19 
20 	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
21 	if (!ASSERT_OK(err, "test_xdp_adjust_tail_shrink"))
22 		return;
23 
24 	err = bpf_prog_test_run_opts(prog_fd, &topts);
25 	ASSERT_OK(err, "ipv4");
26 	ASSERT_EQ(topts.retval, XDP_DROP, "ipv4 retval");
27 
28 	expect_sz = sizeof(pkt_v6) - 20;  /* Test shrink with 20 bytes */
29 	topts.data_in = &pkt_v6;
30 	topts.data_size_in = sizeof(pkt_v6);
31 	topts.data_size_out = sizeof(buf);
32 	err = bpf_prog_test_run_opts(prog_fd, &topts);
33 	ASSERT_OK(err, "ipv6");
34 	ASSERT_EQ(topts.retval, XDP_TX, "ipv6 retval");
35 	ASSERT_EQ(topts.data_size_out, expect_sz, "ipv6 size");
36 
37 	bpf_object__close(obj);
38 }
39 
test_xdp_adjust_tail_grow(void)40 static void test_xdp_adjust_tail_grow(void)
41 {
42 	const char *file = "./test_xdp_adjust_tail_grow.bpf.o";
43 	struct bpf_object *obj;
44 	char buf[4096]; /* avoid segfault: large buf to hold grow results */
45 	__u32 expect_sz;
46 	int err, prog_fd;
47 	LIBBPF_OPTS(bpf_test_run_opts, topts,
48 		.data_in = &pkt_v4,
49 		.data_size_in = sizeof(pkt_v4),
50 		.data_out = buf,
51 		.data_size_out = sizeof(buf),
52 		.repeat = 1,
53 	);
54 
55 	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
56 	if (!ASSERT_OK(err, "test_xdp_adjust_tail_grow"))
57 		return;
58 
59 	err = bpf_prog_test_run_opts(prog_fd, &topts);
60 	ASSERT_OK(err, "ipv4");
61 	ASSERT_EQ(topts.retval, XDP_DROP, "ipv4 retval");
62 
63 	expect_sz = sizeof(pkt_v6) + 40; /* Test grow with 40 bytes */
64 	topts.data_in = &pkt_v6;
65 	topts.data_size_in = sizeof(pkt_v6);
66 	topts.data_size_out = sizeof(buf);
67 	err = bpf_prog_test_run_opts(prog_fd, &topts);
68 	ASSERT_OK(err, "ipv6");
69 	ASSERT_EQ(topts.retval, XDP_TX, "ipv6 retval");
70 	ASSERT_EQ(topts.data_size_out, expect_sz, "ipv6 size");
71 
72 	bpf_object__close(obj);
73 }
74 
test_xdp_adjust_tail_grow2(void)75 static void test_xdp_adjust_tail_grow2(void)
76 {
77 	const char *file = "./test_xdp_adjust_tail_grow.bpf.o";
78 	char buf[4096]; /* avoid segfault: large buf to hold grow results */
79 	struct bpf_object *obj;
80 	int err, cnt, i;
81 	int max_grow, prog_fd;
82 	/* SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) */
83 #if defined(__s390x__)
84 	int tailroom = 512;
85 #else
86 	int tailroom = 320;
87 #endif
88 
89 	LIBBPF_OPTS(bpf_test_run_opts, tattr,
90 		.repeat		= 1,
91 		.data_in	= &buf,
92 		.data_out	= &buf,
93 		.data_size_in	= 0, /* Per test */
94 		.data_size_out	= 0, /* Per test */
95 	);
96 
97 	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
98 	if (!ASSERT_OK(err, "test_xdp_adjust_tail_grow"))
99 		return;
100 
101 	/* Test case-64 */
102 	memset(buf, 1, sizeof(buf));
103 	tattr.data_size_in  =  64; /* Determine test case via pkt size */
104 	tattr.data_size_out = 128; /* Limit copy_size */
105 	/* Kernel side alloc packet memory area that is zero init */
106 	err = bpf_prog_test_run_opts(prog_fd, &tattr);
107 
108 	ASSERT_EQ(errno, ENOSPC, "case-64 errno"); /* Due limit copy_size in bpf_test_finish */
109 	ASSERT_EQ(tattr.retval, XDP_TX, "case-64 retval");
110 	ASSERT_EQ(tattr.data_size_out, 192, "case-64 data_size_out"); /* Expected grow size */
111 
112 	/* Extra checks for data contents */
113 	ASSERT_EQ(buf[0], 1, "case-64-data buf[0]"); /*  0-63  memset to 1 */
114 	ASSERT_EQ(buf[63], 1, "case-64-data buf[63]");
115 	ASSERT_EQ(buf[64], 0, "case-64-data buf[64]"); /* 64-127 memset to 0 */
116 	ASSERT_EQ(buf[127], 0, "case-64-data buf[127]");
117 	ASSERT_EQ(buf[128], 1, "case-64-data buf[128]"); /* 128-191 memset to 1 */
118 	ASSERT_EQ(buf[191], 1, "case-64-data buf[191]");
119 
120 	/* Test case-128 */
121 	memset(buf, 2, sizeof(buf));
122 	tattr.data_size_in  = 128; /* Determine test case via pkt size */
123 	tattr.data_size_out = sizeof(buf);   /* Copy everything */
124 	err = bpf_prog_test_run_opts(prog_fd, &tattr);
125 
126 	max_grow = 4096 - XDP_PACKET_HEADROOM -	tailroom; /* 3520 */
127 	ASSERT_OK(err, "case-128");
128 	ASSERT_EQ(tattr.retval, XDP_TX, "case-128 retval");
129 	ASSERT_EQ(tattr.data_size_out, max_grow, "case-128 data_size_out"); /* Expect max grow */
130 
131 	/* Extra checks for data content: Count grow size, will contain zeros */
132 	for (i = 0, cnt = 0; i < sizeof(buf); i++) {
133 		if (buf[i] == 0)
134 			cnt++;
135 	}
136 	ASSERT_EQ(cnt, max_grow - tattr.data_size_in, "case-128-data cnt"); /* Grow increase */
137 	ASSERT_EQ(tattr.data_size_out, max_grow, "case-128-data data_size_out"); /* Total grow */
138 
139 	bpf_object__close(obj);
140 }
141 
test_xdp_adjust_frags_tail_shrink(void)142 static void test_xdp_adjust_frags_tail_shrink(void)
143 {
144 	const char *file = "./test_xdp_adjust_tail_shrink.bpf.o";
145 	__u32 exp_size;
146 	struct bpf_program *prog;
147 	struct bpf_object *obj;
148 	int err, prog_fd;
149 	__u8 *buf;
150 	LIBBPF_OPTS(bpf_test_run_opts, topts);
151 
152 	/* For the individual test cases, the first byte in the packet
153 	 * indicates which test will be run.
154 	 */
155 	obj = bpf_object__open(file);
156 	if (libbpf_get_error(obj))
157 		return;
158 
159 	prog = bpf_object__next_program(obj, NULL);
160 	if (bpf_object__load(obj))
161 		return;
162 
163 	prog_fd = bpf_program__fd(prog);
164 
165 	buf = malloc(9000);
166 	if (!ASSERT_OK_PTR(buf, "alloc buf 9Kb"))
167 		goto out;
168 
169 	memset(buf, 0, 9000);
170 
171 	/* Test case removing 10 bytes from last frag, NOT freeing it */
172 	exp_size = 8990; /* 9000 - 10 */
173 	topts.data_in = buf;
174 	topts.data_out = buf;
175 	topts.data_size_in = 9000;
176 	topts.data_size_out = 9000;
177 	err = bpf_prog_test_run_opts(prog_fd, &topts);
178 
179 	ASSERT_OK(err, "9Kb-10b");
180 	ASSERT_EQ(topts.retval, XDP_TX, "9Kb-10b retval");
181 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb-10b size");
182 
183 	/* Test case removing one of two pages, assuming 4K pages */
184 	buf[0] = 1;
185 	exp_size = 4900; /* 9000 - 4100 */
186 
187 	topts.data_size_out = 9000; /* reset from previous invocation */
188 	err = bpf_prog_test_run_opts(prog_fd, &topts);
189 
190 	ASSERT_OK(err, "9Kb-4Kb");
191 	ASSERT_EQ(topts.retval, XDP_TX, "9Kb-4Kb retval");
192 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb-4Kb size");
193 
194 	/* Test case removing two pages resulting in a linear xdp_buff */
195 	buf[0] = 2;
196 	exp_size = 800; /* 9000 - 8200 */
197 	topts.data_size_out = 9000; /* reset from previous invocation */
198 	err = bpf_prog_test_run_opts(prog_fd, &topts);
199 
200 	ASSERT_OK(err, "9Kb-9Kb");
201 	ASSERT_EQ(topts.retval, XDP_TX, "9Kb-9Kb retval");
202 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb-9Kb size");
203 
204 	free(buf);
205 out:
206 	bpf_object__close(obj);
207 }
208 
test_xdp_adjust_frags_tail_grow(void)209 static void test_xdp_adjust_frags_tail_grow(void)
210 {
211 	const char *file = "./test_xdp_adjust_tail_grow.bpf.o";
212 	__u32 exp_size;
213 	struct bpf_program *prog;
214 	struct bpf_object *obj;
215 	int err, i, prog_fd;
216 	__u8 *buf;
217 	LIBBPF_OPTS(bpf_test_run_opts, topts);
218 
219 	obj = bpf_object__open(file);
220 	if (libbpf_get_error(obj))
221 		return;
222 
223 	prog = bpf_object__next_program(obj, NULL);
224 	if (bpf_object__load(obj))
225 		goto out;
226 
227 	prog_fd = bpf_program__fd(prog);
228 
229 	buf = malloc(16384);
230 	if (!ASSERT_OK_PTR(buf, "alloc buf 16Kb"))
231 		goto out;
232 
233 	/* Test case add 10 bytes to last frag */
234 	memset(buf, 1, 16384);
235 	exp_size = 9000 + 10;
236 
237 	topts.data_in = buf;
238 	topts.data_out = buf;
239 	topts.data_size_in = 9000;
240 	topts.data_size_out = 16384;
241 	err = bpf_prog_test_run_opts(prog_fd, &topts);
242 
243 	ASSERT_OK(err, "9Kb+10b");
244 	ASSERT_EQ(topts.retval, XDP_TX, "9Kb+10b retval");
245 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb+10b size");
246 
247 	for (i = 0; i < 9000; i++)
248 		ASSERT_EQ(buf[i], 1, "9Kb+10b-old");
249 
250 	for (i = 9000; i < 9010; i++)
251 		ASSERT_EQ(buf[i], 0, "9Kb+10b-new");
252 
253 	for (i = 9010; i < 16384; i++)
254 		ASSERT_EQ(buf[i], 1, "9Kb+10b-untouched");
255 
256 	/* Test a too large grow */
257 	memset(buf, 1, 16384);
258 	exp_size = 9001;
259 
260 	topts.data_in = topts.data_out = buf;
261 	topts.data_size_in = 9001;
262 	topts.data_size_out = 16384;
263 	err = bpf_prog_test_run_opts(prog_fd, &topts);
264 
265 	ASSERT_OK(err, "9Kb+10b");
266 	ASSERT_EQ(topts.retval, XDP_DROP, "9Kb+10b retval");
267 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb+10b size");
268 
269 	free(buf);
270 out:
271 	bpf_object__close(obj);
272 }
273 
test_xdp_adjust_tail(void)274 void test_xdp_adjust_tail(void)
275 {
276 	if (test__start_subtest("xdp_adjust_tail_shrink"))
277 		test_xdp_adjust_tail_shrink();
278 	if (test__start_subtest("xdp_adjust_tail_grow"))
279 		test_xdp_adjust_tail_grow();
280 	if (test__start_subtest("xdp_adjust_tail_grow2"))
281 		test_xdp_adjust_tail_grow2();
282 	if (test__start_subtest("xdp_adjust_frags_tail_shrink"))
283 		test_xdp_adjust_frags_tail_shrink();
284 	if (test__start_subtest("xdp_adjust_frags_tail_grow"))
285 		test_xdp_adjust_frags_tail_grow();
286 }
287