0001
0002
0003 #include "bpf_iter.h"
0004 #include <bpf/bpf_helpers.h>
0005
0006 char _license[] SEC("license") = "GPL";
0007
0008 struct key_t {
0009 int a;
0010 int b;
0011 int c;
0012 };
0013
0014 struct {
0015 __uint(type, BPF_MAP_TYPE_HASH);
0016 __uint(max_entries, 3);
0017 __type(key, struct key_t);
0018 __type(value, __u64);
0019 } hashmap1 SEC(".maps");
0020
0021 struct {
0022 __uint(type, BPF_MAP_TYPE_HASH);
0023 __uint(max_entries, 3);
0024 __type(key, __u64);
0025 __type(value, __u64);
0026 } hashmap2 SEC(".maps");
0027
0028 struct {
0029 __uint(type, BPF_MAP_TYPE_HASH);
0030 __uint(max_entries, 3);
0031 __type(key, struct key_t);
0032 __type(value, __u32);
0033 } hashmap3 SEC(".maps");
0034
0035
0036 bool in_test_mode = 0;
0037
0038
0039 __u32 key_sum_a = 0, key_sum_b = 0, key_sum_c = 0;
0040 __u64 val_sum = 0;
0041
0042 SEC("iter/bpf_map_elem")
0043 int dump_bpf_hash_map(struct bpf_iter__bpf_map_elem *ctx)
0044 {
0045 struct seq_file *seq = ctx->meta->seq;
0046 __u32 seq_num = ctx->meta->seq_num;
0047 struct bpf_map *map = ctx->map;
0048 struct key_t *key = ctx->key;
0049 struct key_t tmp_key;
0050 __u64 *val = ctx->value;
0051 __u64 tmp_val = 0;
0052 int ret;
0053
0054 if (in_test_mode) {
0055
0056
0057
0058
0059
0060
0061
0062
0063 if (key == (void *)0 || val == (void *)0)
0064 return 0;
0065
0066
0067
0068
0069
0070 __builtin_memcpy(&tmp_key, key, sizeof(struct key_t));
0071 ret = bpf_map_update_elem(&hashmap1, &tmp_key, &tmp_val, 0);
0072 if (ret)
0073 return 0;
0074 ret = bpf_map_delete_elem(&hashmap1, &tmp_key);
0075 if (ret)
0076 return 0;
0077
0078 key_sum_a += key->a;
0079 key_sum_b += key->b;
0080 key_sum_c += key->c;
0081 val_sum += *val;
0082 return 0;
0083 }
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 if (seq_num == 0)
0103 BPF_SEQ_PRINTF(seq, "map dump starts\n");
0104
0105 if (key == (void *)0 || val == (void *)0) {
0106 BPF_SEQ_PRINTF(seq, "map dump ends\n");
0107 return 0;
0108 }
0109
0110 BPF_SEQ_PRINTF(seq, "%d: (%x %d %x) (%llx)\n", map->id,
0111 key->a, key->b, key->c, *val);
0112
0113 return 0;
0114 }
0115
0116 SEC("iter.s/bpf_map_elem")
0117 int sleepable_dummy_dump(struct bpf_iter__bpf_map_elem *ctx)
0118 {
0119 if (ctx->meta->seq_num == 0)
0120 BPF_SEQ_PRINTF(ctx->meta->seq, "map dump starts\n");
0121
0122 return 0;
0123 }