1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
7 char _license
[] SEC("license") = "GPL";
16 __uint(type
, BPF_MAP_TYPE_HASH
);
17 __uint(max_entries
, 3);
18 __type(key
, struct key_t
);
20 } hashmap1
SEC(".maps");
23 __uint(type
, BPF_MAP_TYPE_HASH
);
24 __uint(max_entries
, 3);
27 } hashmap2
SEC(".maps");
30 __uint(type
, BPF_MAP_TYPE_HASH
);
31 __uint(max_entries
, 3);
32 __type(key
, struct key_t
);
34 } hashmap3
SEC(".maps");
36 /* will set before prog run */
37 bool in_test_mode
= 0;
39 /* will collect results during prog run */
40 __u32 key_sum_a
= 0, key_sum_b
= 0, key_sum_c
= 0;
43 SEC("iter/bpf_map_elem")
44 int dump_bpf_hash_map(struct bpf_iter__bpf_map_elem
*ctx
)
46 struct seq_file
*seq
= ctx
->meta
->seq
;
47 __u32 seq_num
= ctx
->meta
->seq_num
;
48 struct bpf_map
*map
= ctx
->map
;
49 struct key_t
*key
= ctx
->key
;
51 __u64
*val
= ctx
->value
;
56 /* test mode is used by selftests to
57 * test functionality of bpf_hash_map iter.
59 * the above hashmap1 will have correct size
60 * and will be accepted, hashmap2 and hashmap3
61 * should be rejected due to smaller key/value
64 if (key
== (void *)0 || val
== (void *)0)
67 /* update the value and then delete the <key, value> pair.
68 * it should not impact the existing 'val' which is still
69 * accessible under rcu.
71 __builtin_memcpy(&tmp_key
, key
, sizeof(struct key_t
));
72 ret
= bpf_map_update_elem(&hashmap1
, &tmp_key
, &tmp_val
, 0);
75 ret
= bpf_map_delete_elem(&hashmap1
, &tmp_key
);
86 /* non-test mode, the map is prepared with the
87 * below bpftool command sequence:
88 * bpftool map create /sys/fs/bpf/m1 type hash \
89 * key 12 value 8 entries 3 name map1
90 * bpftool map update id 77 key 0 0 0 1 0 0 0 0 0 0 0 1 \
91 * value 0 0 0 1 0 0 0 1
92 * bpftool map update id 77 key 0 0 0 1 0 0 0 0 0 0 0 2 \
93 * value 0 0 0 1 0 0 0 2
94 * The bpftool iter command line:
95 * bpftool iter pin ./bpf_iter_bpf_hash_map.o /sys/fs/bpf/p1 \
97 * The below output will be:
99 * 77: (1000000 0 2000000) (200000001000000)
100 * 77: (1000000 0 1000000) (100000001000000)
104 BPF_SEQ_PRINTF(seq
, "map dump starts\n");
106 if (key
== (void *)0 || val
== (void *)0) {
107 BPF_SEQ_PRINTF(seq
, "map dump ends\n");
111 BPF_SEQ_PRINTF(seq
, "%d: (%x %d %x) (%llx)\n", map
->id
,
112 key
->a
, key
->b
, key
->c
, *val
);