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