xref: /linux/tools/testing/selftests/bpf/progs/htab_update.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*1c636b62SHou Tao // SPDX-License-Identifier: GPL-2.0
2*1c636b62SHou Tao /* Copyright (C) 2022. Huawei Technologies Co., Ltd */
3*1c636b62SHou Tao #include <linux/bpf.h>
4*1c636b62SHou Tao #include <bpf/bpf_helpers.h>
5*1c636b62SHou Tao #include <bpf/bpf_tracing.h>
6*1c636b62SHou Tao 
7*1c636b62SHou Tao char _license[] SEC("license") = "GPL";
8*1c636b62SHou Tao 
9*1c636b62SHou Tao struct {
10*1c636b62SHou Tao 	__uint(type, BPF_MAP_TYPE_HASH);
11*1c636b62SHou Tao 	__uint(max_entries, 1);
12*1c636b62SHou Tao 	__uint(key_size, sizeof(__u32));
13*1c636b62SHou Tao 	__uint(value_size, sizeof(__u32));
14*1c636b62SHou Tao } htab SEC(".maps");
15*1c636b62SHou Tao 
16*1c636b62SHou Tao int pid = 0;
17*1c636b62SHou Tao int update_err = 0;
18*1c636b62SHou Tao 
19*1c636b62SHou Tao SEC("?fentry/lookup_elem_raw")
lookup_elem_raw(void * ctx)20*1c636b62SHou Tao int lookup_elem_raw(void *ctx)
21*1c636b62SHou Tao {
22*1c636b62SHou Tao 	__u32 key = 0, value = 1;
23*1c636b62SHou Tao 
24*1c636b62SHou Tao 	if ((bpf_get_current_pid_tgid() >> 32) != pid)
25*1c636b62SHou Tao 		return 0;
26*1c636b62SHou Tao 
27*1c636b62SHou Tao 	update_err = bpf_map_update_elem(&htab, &key, &value, 0);
28*1c636b62SHou Tao 	return 0;
29*1c636b62SHou Tao }
30