xref: /linux/tools/testing/selftests/bpf/progs/inner_array_lookup.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/bpf.h>
4 #include <bpf/bpf_helpers.h>
5 
6 struct inner_map {
7 	__uint(type, BPF_MAP_TYPE_ARRAY);
8 	__uint(max_entries, 5);
9 	__type(key, int);
10 	__type(value, int);
11 } inner_map1 SEC(".maps");
12 
13 struct outer_map {
14 	__uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
15 	__uint(max_entries, 3);
16 	__type(key, int);
17 	__array(values, struct inner_map);
18 } outer_map1 SEC(".maps") = {
19 	.values = {
20 		[2] = &inner_map1,
21 	},
22 };
23 
24 SEC("raw_tp/sys_enter")
25 int handle__sys_enter(void *ctx)
26 {
27 	int outer_key = 2, inner_key = 3;
28 	int *val;
29 	void *map;
30 
31 	map = bpf_map_lookup_elem(&outer_map1, &outer_key);
32 	if (!map)
33 		return 1;
34 
35 	val = bpf_map_lookup_elem(map, &inner_key);
36 	if (!val)
37 		return 1;
38 
39 	if (*val == 1)
40 		*val = 2;
41 
42 	return 0;
43 }
44 
45 char _license[] SEC("license") = "GPL";
46