1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
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 
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 
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 	int tailroom = 320; /* SKB_DATA_ALIGN(sizeof(struct skb_shared_info))*/;
80 	struct bpf_object *obj;
81 	int err, cnt, i;
82 	int max_grow, prog_fd;
83 
84 	LIBBPF_OPTS(bpf_test_run_opts, tattr,
85 		.repeat		= 1,
86 		.data_in	= &buf,
87 		.data_out	= &buf,
88 		.data_size_in	= 0, /* Per test */
89 		.data_size_out	= 0, /* Per test */
90 	);
91 
92 	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
93 	if (!ASSERT_OK(err, "test_xdp_adjust_tail_grow"))
94 		return;
95 
96 	/* Test case-64 */
97 	memset(buf, 1, sizeof(buf));
98 	tattr.data_size_in  =  64; /* Determine test case via pkt size */
99 	tattr.data_size_out = 128; /* Limit copy_size */
100 	/* Kernel side alloc packet memory area that is zero init */
101 	err = bpf_prog_test_run_opts(prog_fd, &tattr);
102 
103 	ASSERT_EQ(errno, ENOSPC, "case-64 errno"); /* Due limit copy_size in bpf_test_finish */
104 	ASSERT_EQ(tattr.retval, XDP_TX, "case-64 retval");
105 	ASSERT_EQ(tattr.data_size_out, 192, "case-64 data_size_out"); /* Expected grow size */
106 
107 	/* Extra checks for data contents */
108 	ASSERT_EQ(buf[0], 1, "case-64-data buf[0]"); /*  0-63  memset to 1 */
109 	ASSERT_EQ(buf[63], 1, "case-64-data buf[63]");
110 	ASSERT_EQ(buf[64], 0, "case-64-data buf[64]"); /* 64-127 memset to 0 */
111 	ASSERT_EQ(buf[127], 0, "case-64-data buf[127]");
112 	ASSERT_EQ(buf[128], 1, "case-64-data buf[128]"); /* 128-191 memset to 1 */
113 	ASSERT_EQ(buf[191], 1, "case-64-data buf[191]");
114 
115 	/* Test case-128 */
116 	memset(buf, 2, sizeof(buf));
117 	tattr.data_size_in  = 128; /* Determine test case via pkt size */
118 	tattr.data_size_out = sizeof(buf);   /* Copy everything */
119 	err = bpf_prog_test_run_opts(prog_fd, &tattr);
120 
121 	max_grow = 4096 - XDP_PACKET_HEADROOM -	tailroom; /* 3520 */
122 	ASSERT_OK(err, "case-128");
123 	ASSERT_EQ(tattr.retval, XDP_TX, "case-128 retval");
124 	ASSERT_EQ(tattr.data_size_out, max_grow, "case-128 data_size_out"); /* Expect max grow */
125 
126 	/* Extra checks for data content: Count grow size, will contain zeros */
127 	for (i = 0, cnt = 0; i < sizeof(buf); i++) {
128 		if (buf[i] == 0)
129 			cnt++;
130 	}
131 	ASSERT_EQ(cnt, max_grow - tattr.data_size_in, "case-128-data cnt"); /* Grow increase */
132 	ASSERT_EQ(tattr.data_size_out, max_grow, "case-128-data data_size_out"); /* Total grow */
133 
134 	bpf_object__close(obj);
135 }
136 
137 static void test_xdp_adjust_frags_tail_shrink(void)
138 {
139 	const char *file = "./test_xdp_adjust_tail_shrink.bpf.o";
140 	__u32 exp_size;
141 	struct bpf_program *prog;
142 	struct bpf_object *obj;
143 	int err, prog_fd;
144 	__u8 *buf;
145 	LIBBPF_OPTS(bpf_test_run_opts, topts);
146 
147 	/* For the individual test cases, the first byte in the packet
148 	 * indicates which test will be run.
149 	 */
150 	obj = bpf_object__open(file);
151 	if (libbpf_get_error(obj))
152 		return;
153 
154 	prog = bpf_object__next_program(obj, NULL);
155 	if (bpf_object__load(obj))
156 		return;
157 
158 	prog_fd = bpf_program__fd(prog);
159 
160 	buf = malloc(9000);
161 	if (!ASSERT_OK_PTR(buf, "alloc buf 9Kb"))
162 		goto out;
163 
164 	memset(buf, 0, 9000);
165 
166 	/* Test case removing 10 bytes from last frag, NOT freeing it */
167 	exp_size = 8990; /* 9000 - 10 */
168 	topts.data_in = buf;
169 	topts.data_out = buf;
170 	topts.data_size_in = 9000;
171 	topts.data_size_out = 9000;
172 	err = bpf_prog_test_run_opts(prog_fd, &topts);
173 
174 	ASSERT_OK(err, "9Kb-10b");
175 	ASSERT_EQ(topts.retval, XDP_TX, "9Kb-10b retval");
176 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb-10b size");
177 
178 	/* Test case removing one of two pages, assuming 4K pages */
179 	buf[0] = 1;
180 	exp_size = 4900; /* 9000 - 4100 */
181 
182 	topts.data_size_out = 9000; /* reset from previous invocation */
183 	err = bpf_prog_test_run_opts(prog_fd, &topts);
184 
185 	ASSERT_OK(err, "9Kb-4Kb");
186 	ASSERT_EQ(topts.retval, XDP_TX, "9Kb-4Kb retval");
187 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb-4Kb size");
188 
189 	/* Test case removing two pages resulting in a linear xdp_buff */
190 	buf[0] = 2;
191 	exp_size = 800; /* 9000 - 8200 */
192 	topts.data_size_out = 9000; /* reset from previous invocation */
193 	err = bpf_prog_test_run_opts(prog_fd, &topts);
194 
195 	ASSERT_OK(err, "9Kb-9Kb");
196 	ASSERT_EQ(topts.retval, XDP_TX, "9Kb-9Kb retval");
197 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb-9Kb size");
198 
199 	free(buf);
200 out:
201 	bpf_object__close(obj);
202 }
203 
204 static void test_xdp_adjust_frags_tail_grow(void)
205 {
206 	const char *file = "./test_xdp_adjust_tail_grow.bpf.o";
207 	__u32 exp_size;
208 	struct bpf_program *prog;
209 	struct bpf_object *obj;
210 	int err, i, prog_fd;
211 	__u8 *buf;
212 	LIBBPF_OPTS(bpf_test_run_opts, topts);
213 
214 	obj = bpf_object__open(file);
215 	if (libbpf_get_error(obj))
216 		return;
217 
218 	prog = bpf_object__next_program(obj, NULL);
219 	if (bpf_object__load(obj))
220 		return;
221 
222 	prog_fd = bpf_program__fd(prog);
223 
224 	buf = malloc(16384);
225 	if (!ASSERT_OK_PTR(buf, "alloc buf 16Kb"))
226 		goto out;
227 
228 	/* Test case add 10 bytes to last frag */
229 	memset(buf, 1, 16384);
230 	exp_size = 9000 + 10;
231 
232 	topts.data_in = buf;
233 	topts.data_out = buf;
234 	topts.data_size_in = 9000;
235 	topts.data_size_out = 16384;
236 	err = bpf_prog_test_run_opts(prog_fd, &topts);
237 
238 	ASSERT_OK(err, "9Kb+10b");
239 	ASSERT_EQ(topts.retval, XDP_TX, "9Kb+10b retval");
240 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb+10b size");
241 
242 	for (i = 0; i < 9000; i++)
243 		ASSERT_EQ(buf[i], 1, "9Kb+10b-old");
244 
245 	for (i = 9000; i < 9010; i++)
246 		ASSERT_EQ(buf[i], 0, "9Kb+10b-new");
247 
248 	for (i = 9010; i < 16384; i++)
249 		ASSERT_EQ(buf[i], 1, "9Kb+10b-untouched");
250 
251 	/* Test a too large grow */
252 	memset(buf, 1, 16384);
253 	exp_size = 9001;
254 
255 	topts.data_in = topts.data_out = buf;
256 	topts.data_size_in = 9001;
257 	topts.data_size_out = 16384;
258 	err = bpf_prog_test_run_opts(prog_fd, &topts);
259 
260 	ASSERT_OK(err, "9Kb+10b");
261 	ASSERT_EQ(topts.retval, XDP_DROP, "9Kb+10b retval");
262 	ASSERT_EQ(topts.data_size_out, exp_size, "9Kb+10b size");
263 
264 	free(buf);
265 out:
266 	bpf_object__close(obj);
267 }
268 
269 void test_xdp_adjust_tail(void)
270 {
271 	if (test__start_subtest("xdp_adjust_tail_shrink"))
272 		test_xdp_adjust_tail_shrink();
273 	if (test__start_subtest("xdp_adjust_tail_grow"))
274 		test_xdp_adjust_tail_grow();
275 	if (test__start_subtest("xdp_adjust_tail_grow2"))
276 		test_xdp_adjust_tail_grow2();
277 	if (test__start_subtest("xdp_adjust_frags_tail_shrink"))
278 		test_xdp_adjust_frags_tail_shrink();
279 	if (test__start_subtest("xdp_adjust_frags_tail_grow"))
280 		test_xdp_adjust_frags_tail_grow();
281 }
282