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_PERCPU_HASH);
17 	__uint(max_entries, 3);
18 	__type(key, struct key_t);
19 	__type(value, __u32);
20 } hashmap1 SEC(".maps");
21 
22 /* will set before prog run */
23 volatile const __u32 num_cpus = 0;
24 
25 /* will collect results during prog run */
26 __u32 key_sum_a = 0, key_sum_b = 0, key_sum_c = 0;
27 __u32 val_sum = 0;
28 
29 SEC("iter/bpf_map_elem")
dump_bpf_percpu_hash_map(struct bpf_iter__bpf_map_elem * ctx)30 int dump_bpf_percpu_hash_map(struct bpf_iter__bpf_map_elem *ctx)
31 {
32 	struct key_t *key = ctx->key;
33 	void *pptr = ctx->value;
34 	__u32 step;
35 	int i;
36 
37 	if (key == (void *)0 || pptr == (void *)0)
38 		return 0;
39 
40 	key_sum_a += key->a;
41 	key_sum_b += key->b;
42 	key_sum_c += key->c;
43 
44 	step = 8;
45 	for (i = 0; i < num_cpus; i++) {
46 		val_sum += *(__u32 *)pptr;
47 		pptr += step;
48 	}
49 	return 0;
50 }
51