xref: /linux/tools/testing/selftests/bpf/progs/csum_diff_test.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright Amazon.com Inc. or its affiliates */
3 #include <linux/types.h>
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 #define BUFF_SZ 512
9 
10 /* Will be updated by benchmark before program loading */
11 char to_buff[BUFF_SZ];
12 const volatile unsigned int to_buff_len = 0;
13 char from_buff[BUFF_SZ];
14 const volatile unsigned int from_buff_len = 0;
15 unsigned short seed = 0;
16 
17 short result;
18 
19 char _license[] SEC("license") = "GPL";
20 
21 SEC("tc")
22 int compute_checksum(void *ctx)
23 {
24 	int to_len_half = to_buff_len / 2;
25 	int from_len_half = from_buff_len / 2;
26 	short result2;
27 
28 	/* Calculate checksum in one go */
29 	result2 = bpf_csum_diff((void *)from_buff, from_buff_len,
30 				(void *)to_buff, to_buff_len, seed);
31 
32 	/* Calculate checksum by concatenating bpf_csum_diff()*/
33 	result = bpf_csum_diff((void *)from_buff, from_buff_len - from_len_half,
34 			       (void *)to_buff, to_buff_len - to_len_half, seed);
35 
36 	result = bpf_csum_diff((void *)from_buff + (from_buff_len - from_len_half), from_len_half,
37 			       (void *)to_buff + (to_buff_len - to_len_half), to_len_half, result);
38 
39 	result = (result == result2) ? result : 0;
40 
41 	return 0;
42 }
43