xref: /linux/tools/testing/selftests/bpf/prog_tests/test_csum_diff.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright Amazon.com Inc. or its affiliates */
3 #include <test_progs.h>
4 #include "csum_diff_test.skel.h"
5 
6 #define BUFF_SZ 512
7 
8 struct testcase {
9 	unsigned long long to_buff[BUFF_SZ / 8];
10 	unsigned int to_buff_len;
11 	unsigned long long from_buff[BUFF_SZ / 8];
12 	unsigned int from_buff_len;
13 	unsigned short seed;
14 	unsigned short result;
15 };
16 
17 #define NUM_PUSH_TESTS 4
18 
19 struct testcase push_tests[NUM_PUSH_TESTS] = {
20 	{
21 		.to_buff = {
22 			0xdeadbeefdeadbeef,
23 		},
24 		.to_buff_len = 8,
25 		.from_buff = {},
26 		.from_buff_len = 0,
27 		.seed = 0,
28 		.result = 0x3b3b
29 	},
30 	{
31 		.to_buff = {
32 			0xdeadbeefdeadbeef,
33 			0xbeefdeadbeefdead,
34 		},
35 		.to_buff_len = 16,
36 		.from_buff = {},
37 		.from_buff_len = 0,
38 		.seed = 0x1234,
39 		.result = 0x88aa
40 	},
41 	{
42 		.to_buff = {
43 			0xdeadbeefdeadbeef,
44 			0xbeefdeadbeefdead,
45 		},
46 		.to_buff_len = 15,
47 		.from_buff = {},
48 		.from_buff_len = 0,
49 		.seed = 0x1234,
50 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
51 		.result = 0xcaa9
52 #else
53 		.result = 0x87fd
54 #endif
55 	},
56 	{
57 		.to_buff = {
58 			0x327b23c66b8b4567,
59 			0x66334873643c9869,
60 			0x19495cff74b0dc51,
61 			0x625558ec2ae8944a,
62 			0x46e87ccd238e1f29,
63 			0x507ed7ab3d1b58ba,
64 			0x41b71efb2eb141f2,
65 			0x7545e14679e2a9e3,
66 			0x5bd062c2515f007c,
67 			0x4db127f812200854,
68 			0x1f16e9e80216231b,
69 			0x66ef438d1190cde7,
70 			0x3352255a140e0f76,
71 			0x0ded7263109cf92e,
72 			0x1befd79f7fdcc233,
73 			0x6b68079a41a7c4c9,
74 			0x25e45d324e6afb66,
75 			0x431bd7b7519b500d,
76 			0x7c83e4583f2dba31,
77 			0x62bbd95a257130a3,
78 			0x628c895d436c6125,
79 			0x721da317333ab105,
80 			0x2d1d5ae92443a858,
81 			0x75a2a8d46763845e,
82 			0x79838cb208edbdab,
83 			0x0b03e0c64353d0cd,
84 			0x54e49eb4189a769b,
85 			0x2ca8861171f32454,
86 			0x02901d820836c40e,
87 			0x081386413a95f874,
88 			0x7c3dbd3d1e7ff521,
89 			0x6ceaf087737b8ddc,
90 			0x4516dde922221a70,
91 			0x614fd4a13006c83e,
92 			0x5577f8e1419ac241,
93 			0x05072367440badfc,
94 			0x77465f013804823e,
95 			0x5c482a977724c67e,
96 			0x5e884adc2463b9ea,
97 			0x2d51779651ead36b,
98 			0x153ea438580bd78f,
99 			0x70a64e2a3855585c,
100 			0x2a487cb06a2342ec,
101 			0x725a06fb1d4ed43b,
102 			0x57e4ccaf2cd89a32,
103 			0x4b588f547a6d8d3c,
104 			0x6de91b18542289ec,
105 			0x7644a45c38437fdb,
106 			0x684a481a32fff902,
107 			0x749abb43579478fe,
108 			0x1ba026fa3dc240fb,
109 			0x75c6c33a79a1deaa,
110 			0x70c6a52912e685fb,
111 			0x374a3fe6520eedd1,
112 			0x23f9c13c4f4ef005,
113 			0x275ac794649bb77c,
114 			0x1cf10fd839386575,
115 			0x235ba861180115be,
116 			0x354fe9f947398c89,
117 			0x741226bb15b5af5c,
118 			0x10233c990d34b6a8,
119 			0x615740953f6ab60f,
120 			0x77ae35eb7e0c57b1,
121 			0x310c50b3579be4f1,
122 		},
123 		.to_buff_len = 512,
124 		.from_buff = {},
125 		.from_buff_len = 0,
126 		.seed = 0xffff,
127 		.result = 0xca45
128 	},
129 };
130 
131 #define NUM_PULL_TESTS 4
132 
133 struct testcase pull_tests[NUM_PULL_TESTS] = {
134 	{
135 		.from_buff = {
136 			0xdeadbeefdeadbeef,
137 		},
138 		.from_buff_len = 8,
139 		.to_buff = {},
140 		.to_buff_len = 0,
141 		.seed = 0,
142 		.result = 0xc4c4
143 	},
144 	{
145 		.from_buff = {
146 			0xdeadbeefdeadbeef,
147 			0xbeefdeadbeefdead,
148 		},
149 		.from_buff_len = 16,
150 		.to_buff = {},
151 		.to_buff_len = 0,
152 		.seed = 0x1234,
153 		.result = 0x9bbd
154 	},
155 	{
156 		.from_buff = {
157 			0xdeadbeefdeadbeef,
158 			0xbeefdeadbeefdead,
159 		},
160 		.from_buff_len = 15,
161 		.to_buff = {},
162 		.to_buff_len = 0,
163 		.seed = 0x1234,
164 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
165 		.result = 0x59be
166 #else
167 		.result = 0x9c6a
168 #endif
169 	},
170 	{
171 		.from_buff = {
172 			0x327b23c66b8b4567,
173 			0x66334873643c9869,
174 			0x19495cff74b0dc51,
175 			0x625558ec2ae8944a,
176 			0x46e87ccd238e1f29,
177 			0x507ed7ab3d1b58ba,
178 			0x41b71efb2eb141f2,
179 			0x7545e14679e2a9e3,
180 			0x5bd062c2515f007c,
181 			0x4db127f812200854,
182 			0x1f16e9e80216231b,
183 			0x66ef438d1190cde7,
184 			0x3352255a140e0f76,
185 			0x0ded7263109cf92e,
186 			0x1befd79f7fdcc233,
187 			0x6b68079a41a7c4c9,
188 			0x25e45d324e6afb66,
189 			0x431bd7b7519b500d,
190 			0x7c83e4583f2dba31,
191 			0x62bbd95a257130a3,
192 			0x628c895d436c6125,
193 			0x721da317333ab105,
194 			0x2d1d5ae92443a858,
195 			0x75a2a8d46763845e,
196 			0x79838cb208edbdab,
197 			0x0b03e0c64353d0cd,
198 			0x54e49eb4189a769b,
199 			0x2ca8861171f32454,
200 			0x02901d820836c40e,
201 			0x081386413a95f874,
202 			0x7c3dbd3d1e7ff521,
203 			0x6ceaf087737b8ddc,
204 			0x4516dde922221a70,
205 			0x614fd4a13006c83e,
206 			0x5577f8e1419ac241,
207 			0x05072367440badfc,
208 			0x77465f013804823e,
209 			0x5c482a977724c67e,
210 			0x5e884adc2463b9ea,
211 			0x2d51779651ead36b,
212 			0x153ea438580bd78f,
213 			0x70a64e2a3855585c,
214 			0x2a487cb06a2342ec,
215 			0x725a06fb1d4ed43b,
216 			0x57e4ccaf2cd89a32,
217 			0x4b588f547a6d8d3c,
218 			0x6de91b18542289ec,
219 			0x7644a45c38437fdb,
220 			0x684a481a32fff902,
221 			0x749abb43579478fe,
222 			0x1ba026fa3dc240fb,
223 			0x75c6c33a79a1deaa,
224 			0x70c6a52912e685fb,
225 			0x374a3fe6520eedd1,
226 			0x23f9c13c4f4ef005,
227 			0x275ac794649bb77c,
228 			0x1cf10fd839386575,
229 			0x235ba861180115be,
230 			0x354fe9f947398c89,
231 			0x741226bb15b5af5c,
232 			0x10233c990d34b6a8,
233 			0x615740953f6ab60f,
234 			0x77ae35eb7e0c57b1,
235 			0x310c50b3579be4f1,
236 		},
237 		.from_buff_len = 512,
238 		.to_buff = {},
239 		.to_buff_len = 0,
240 		.seed = 0xffff,
241 		.result = 0x35ba
242 	},
243 };
244 
245 #define NUM_DIFF_TESTS 4
246 
247 struct testcase diff_tests[NUM_DIFF_TESTS] = {
248 	{
249 		.from_buff = {
250 			0xdeadbeefdeadbeef,
251 		},
252 		.from_buff_len = 8,
253 		.to_buff = {
254 			0xabababababababab,
255 		},
256 		.to_buff_len = 8,
257 		.seed = 0,
258 		.result = 0x7373
259 	},
260 	{
261 		.from_buff = {
262 			0xdeadbeefdeadbeef,
263 		},
264 		.from_buff_len = 7,
265 		.to_buff = {
266 			0xabababababababab,
267 		},
268 		.to_buff_len = 7,
269 		.seed = 0,
270 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
271 		.result = 0xa673
272 #else
273 		.result = 0x73b7
274 #endif
275 	},
276 	{
277 		.from_buff = {
278 			0,
279 		},
280 		.from_buff_len = 8,
281 		.to_buff = {
282 			0xabababababababab,
283 		},
284 		.to_buff_len = 8,
285 		.seed = 0,
286 		.result = 0xaeae
287 	},
288 	{
289 		.from_buff = {
290 			0xdeadbeefdeadbeef
291 		},
292 		.from_buff_len = 8,
293 		.to_buff = {
294 			0,
295 		},
296 		.to_buff_len = 8,
297 		.seed = 0xffff,
298 		.result = 0xc4c4
299 	},
300 };
301 
302 #define NUM_EDGE_TESTS 4
303 
304 struct testcase edge_tests[NUM_EDGE_TESTS] = {
305 	{
306 		.from_buff = {},
307 		.from_buff_len = 0,
308 		.to_buff = {},
309 		.to_buff_len = 0,
310 		.seed = 0,
311 		.result = 0
312 	},
313 	{
314 		.from_buff = {
315 			0x1234
316 		},
317 		.from_buff_len = 0,
318 		.to_buff = {
319 			0x1234
320 		},
321 		.to_buff_len = 0,
322 		.seed = 0,
323 		.result = 0
324 	},
325 	{
326 		.from_buff = {},
327 		.from_buff_len = 0,
328 		.to_buff = {},
329 		.to_buff_len = 0,
330 		.seed = 0x1234,
331 		.result = 0x1234
332 	},
333 	{
334 		.from_buff = {},
335 		.from_buff_len = 512,
336 		.to_buff = {},
337 		.to_buff_len = 0,
338 		.seed = 0xffff,
339 		.result = 0xffff
340 	},
341 };
342 
343 static unsigned short trigger_csum_diff(const struct csum_diff_test *skel)
344 {
345 	u8 tmp_out[64 << 2] = {};
346 	u8 tmp_in[64] = {};
347 	int err;
348 	int pfd;
349 
350 	LIBBPF_OPTS(bpf_test_run_opts, topts,
351 		.data_in = tmp_in,
352 		.data_size_in = sizeof(tmp_in),
353 		.data_out = tmp_out,
354 		.data_size_out = sizeof(tmp_out),
355 		.repeat = 1,
356 	);
357 	pfd = bpf_program__fd(skel->progs.compute_checksum);
358 	err = bpf_prog_test_run_opts(pfd, &topts);
359 	if (err)
360 		return -1;
361 
362 	return skel->bss->result;
363 }
364 
365 static void test_csum_diff(struct testcase *tests, int num_tests)
366 {
367 	struct csum_diff_test *skel;
368 	unsigned short got;
369 	int err;
370 
371 	for (int i = 0; i < num_tests; i++) {
372 		skel = csum_diff_test__open();
373 		if (!ASSERT_OK_PTR(skel, "csum_diff_test open"))
374 			return;
375 
376 		skel->rodata->to_buff_len = tests[i].to_buff_len;
377 		skel->rodata->from_buff_len = tests[i].from_buff_len;
378 
379 		err = csum_diff_test__load(skel);
380 		if (!ASSERT_EQ(err, 0, "csum_diff_test load"))
381 			goto out;
382 
383 		memcpy(skel->bss->to_buff, tests[i].to_buff, tests[i].to_buff_len);
384 		memcpy(skel->bss->from_buff, tests[i].from_buff, tests[i].from_buff_len);
385 		skel->bss->seed = tests[i].seed;
386 
387 		got = trigger_csum_diff(skel);
388 		ASSERT_EQ(got, tests[i].result, "csum_diff result");
389 
390 		csum_diff_test__destroy(skel);
391 	}
392 
393 	return;
394 out:
395 	csum_diff_test__destroy(skel);
396 }
397 
398 void test_test_csum_diff(void)
399 {
400 	if (test__start_subtest("csum_diff_push"))
401 		test_csum_diff(push_tests, NUM_PUSH_TESTS);
402 	if (test__start_subtest("csum_diff_pull"))
403 		test_csum_diff(pull_tests, NUM_PULL_TESTS);
404 	if (test__start_subtest("csum_diff_diff"))
405 		test_csum_diff(diff_tests, NUM_DIFF_TESTS);
406 	if (test__start_subtest("csum_diff_edge"))
407 		test_csum_diff(edge_tests, NUM_EDGE_TESTS);
408 }
409