1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5
6 char _license[] SEC("license") = "GPL";
7
8 struct {
9 __uint(type, BPF_MAP_TYPE_ARRAY);
10 __uint(max_entries, 3);
11 __type(key, __u32);
12 __type(value, __u64);
13 } arraymap SEC(".maps");
14
15 struct {
16 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
17 __uint(max_entries, 1);
18 __type(key, __u32);
19 __type(value, __u64);
20 } percpu_map SEC(".maps");
21
22 struct callback_ctx {
23 int output;
24 };
25
26 static __u64
check_array_elem(struct bpf_map * map,__u32 * key,__u64 * val,struct callback_ctx * data)27 check_array_elem(struct bpf_map *map, __u32 *key, __u64 *val,
28 struct callback_ctx *data)
29 {
30 data->output += *val;
31 if (*key == 1)
32 return 1; /* stop the iteration */
33 return 0;
34 }
35
36 __u32 cpu = 0;
37 __u64 percpu_val = 0;
38
39 static __u64
check_percpu_elem(struct bpf_map * map,__u32 * key,__u64 * val,struct callback_ctx * data)40 check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val,
41 struct callback_ctx *data)
42 {
43 cpu = bpf_get_smp_processor_id();
44 percpu_val = *val;
45 return 0;
46 }
47
48 u32 arraymap_output = 0;
49
50 SEC("classifier")
test_pkt_access(struct __sk_buff * skb)51 int test_pkt_access(struct __sk_buff *skb)
52 {
53 struct callback_ctx data;
54
55 data.output = 0;
56 bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0);
57 arraymap_output = data.output;
58
59 bpf_for_each_map_elem(&percpu_map, check_percpu_elem, (void *)0, 0);
60 return 0;
61 }
62