1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include "bpf_iter.h"
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6
7 char _license[] SEC("license") = "GPL";
8
9 struct key_t {
10 int a;
11 int b;
12 int c;
13 };
14
15 struct {
16 __uint(type, BPF_MAP_TYPE_ARRAY);
17 __uint(max_entries, 3);
18 __type(key, __u32);
19 __type(value, __u64);
20 } arraymap1 SEC(".maps");
21
22 struct {
23 __uint(type, BPF_MAP_TYPE_HASH);
24 __uint(max_entries, 10);
25 __type(key, __u64);
26 __type(value, __u32);
27 } hashmap1 SEC(".maps");
28
29 __u32 key_sum = 0;
30 __u64 val_sum = 0;
31
32 SEC("iter/bpf_map_elem")
dump_bpf_array_map(struct bpf_iter__bpf_map_elem * ctx)33 int dump_bpf_array_map(struct bpf_iter__bpf_map_elem *ctx)
34 {
35 __u32 *hmap_val, *key = ctx->key;
36 __u64 *val = ctx->value;
37
38 if (key == (void *)0 || val == (void *)0)
39 return 0;
40
41 bpf_seq_write(ctx->meta->seq, key, sizeof(__u32));
42 bpf_seq_write(ctx->meta->seq, val, sizeof(__u64));
43 key_sum += *key;
44 val_sum += *val;
45
46 /* workaround - It's necessary to do this convoluted (val, key)
47 * write into hashmap1, instead of simply doing
48 * bpf_map_update_elem(&hashmap1, val, key, BPF_ANY);
49 * because key has MEM_RDONLY flag and bpf_map_update elem expects
50 * types without this flag
51 */
52 bpf_map_update_elem(&hashmap1, val, val, BPF_ANY);
53 hmap_val = bpf_map_lookup_elem(&hashmap1, val);
54 if (hmap_val)
55 *hmap_val = *key;
56
57 *val = *key;
58 return 0;
59 }
60