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 { 10 __uint(type, BPF_MAP_TYPE_ARRAY); 11 __uint(max_entries, 3); 12 __type(key, __u32); 13 __type(value, __u64); 14 } arraymap1 SEC(".maps"); 15 16 struct { 17 __uint(type, BPF_MAP_TYPE_HASH); 18 __uint(max_entries, 10); 19 __type(key, __u64); 20 __type(value, __u32); 21 } hashmap1 SEC(".maps"); 22 23 __u32 key_sum = 0; 24 __u64 val_sum = 0; 25 26 SEC("iter/bpf_map_elem") 27 int dump_bpf_array_map(struct bpf_iter__bpf_map_elem *ctx) 28 { 29 __u32 *hmap_val, *key = ctx->key; 30 __u64 *val = ctx->value; 31 32 if (key == (void *)0 || val == (void *)0) 33 return 0; 34 35 bpf_seq_write(ctx->meta->seq, key, sizeof(__u32)); 36 bpf_seq_write(ctx->meta->seq, val, sizeof(__u64)); 37 key_sum += *key; 38 val_sum += *val; 39 40 /* workaround - It's necessary to do this convoluted (val, key) 41 * write into hashmap1, instead of simply doing 42 * bpf_map_update_elem(&hashmap1, val, key, BPF_ANY); 43 * because key has MEM_RDONLY flag and bpf_map_update elem expects 44 * types without this flag 45 */ 46 bpf_map_update_elem(&hashmap1, val, val, BPF_ANY); 47 hmap_val = bpf_map_lookup_elem(&hashmap1, val); 48 if (hmap_val) 49 *hmap_val = *key; 50 51 *val = *key; 52 return 0; 53 } 54