1*9c42652fSHou Tao // SPDX-License-Identifier: GPL-2.0
2*9c42652fSHou Tao /* Copyright (C) 2021. Huawei Technologies Co., Ltd */
3*9c42652fSHou Tao #include <linux/types.h>
4*9c42652fSHou Tao #include <linux/bpf.h>
5*9c42652fSHou Tao #include <bpf/bpf_helpers.h>
6*9c42652fSHou Tao #include <bpf/bpf_tracing.h>
7*9c42652fSHou Tao 
8*9c42652fSHou Tao #define STRNCMP_STR_SZ 4096
9*9c42652fSHou Tao 
10*9c42652fSHou Tao /* Will be updated by benchmark before program loading */
11*9c42652fSHou Tao const volatile unsigned int cmp_str_len = 1;
12*9c42652fSHou Tao const char target[STRNCMP_STR_SZ];
13*9c42652fSHou Tao 
14*9c42652fSHou Tao long hits = 0;
15*9c42652fSHou Tao char str[STRNCMP_STR_SZ];
16*9c42652fSHou Tao 
17*9c42652fSHou Tao char _license[] SEC("license") = "GPL";
18*9c42652fSHou Tao 
local_strncmp(const char * s1,unsigned int sz,const char * s2)19*9c42652fSHou Tao static __always_inline int local_strncmp(const char *s1, unsigned int sz,
20*9c42652fSHou Tao 					 const char *s2)
21*9c42652fSHou Tao {
22*9c42652fSHou Tao 	int ret = 0;
23*9c42652fSHou Tao 	unsigned int i;
24*9c42652fSHou Tao 
25*9c42652fSHou Tao 	for (i = 0; i < sz; i++) {
26*9c42652fSHou Tao 		/* E.g. 0xff > 0x31 */
27*9c42652fSHou Tao 		ret = (unsigned char)s1[i] - (unsigned char)s2[i];
28*9c42652fSHou Tao 		if (ret || !s1[i])
29*9c42652fSHou Tao 			break;
30*9c42652fSHou Tao 	}
31*9c42652fSHou Tao 
32*9c42652fSHou Tao 	return ret;
33*9c42652fSHou Tao }
34*9c42652fSHou Tao 
35*9c42652fSHou Tao SEC("tp/syscalls/sys_enter_getpgid")
strncmp_no_helper(void * ctx)36*9c42652fSHou Tao int strncmp_no_helper(void *ctx)
37*9c42652fSHou Tao {
38*9c42652fSHou Tao 	if (local_strncmp(str, cmp_str_len + 1, target) < 0)
39*9c42652fSHou Tao 		__sync_add_and_fetch(&hits, 1);
40*9c42652fSHou Tao 	return 0;
41*9c42652fSHou Tao }
42*9c42652fSHou Tao 
43*9c42652fSHou Tao SEC("tp/syscalls/sys_enter_getpgid")
strncmp_helper(void * ctx)44*9c42652fSHou Tao int strncmp_helper(void *ctx)
45*9c42652fSHou Tao {
46*9c42652fSHou Tao 	if (bpf_strncmp(str, cmp_str_len + 1, target) < 0)
47*9c42652fSHou Tao 		__sync_add_and_fetch(&hits, 1);
48*9c42652fSHou Tao 	return 0;
49*9c42652fSHou Tao }
50*9c42652fSHou Tao 
51