1 // SPDX-License-Identifier: GPL-2.0
2 #include "vmlinux.h"
3 #include <bpf/bpf_helpers.h>
4
5 char _license[] SEC("license") = "GPL";
6
7 struct {
8 __uint(type, BPF_MAP_TYPE_ARRAY);
9 __uint(max_entries, 3);
10 __type(key, __u32);
11 __type(value, __u64);
12 } arraymap SEC(".maps");
13
14 struct {
15 __uint(type, BPF_MAP_TYPE_HASH);
16 __uint(max_entries, 5);
17 __type(key, __u32);
18 __type(value, __u64);
19 } hashmap SEC(".maps");
20
21 struct callback_ctx {
22 int output;
23 };
24
25 u32 data_output = 0;
26 int use_array = 0;
27
28 static __u64
check_map_elem(struct bpf_map * map,__u32 * key,__u64 * val,struct callback_ctx * data)29 check_map_elem(struct bpf_map *map, __u32 *key, __u64 *val,
30 struct callback_ctx *data)
31 {
32 data->output += *val;
33 return 0;
34 }
35
36 SEC("tc")
test_pkt_access(struct __sk_buff * skb)37 int test_pkt_access(struct __sk_buff *skb)
38 {
39 struct callback_ctx data;
40
41 data.output = 0;
42 if (use_array)
43 bpf_for_each_map_elem(&arraymap, check_map_elem, &data, 0);
44 else
45 bpf_for_each_map_elem(&hashmap, check_map_elem, &data, 0);
46 data_output = data.output;
47
48 return 0;
49 }
50