Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2020 Facebook */
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 /* will set before prog run */
0036 bool in_test_mode = 0;
0037 
0038 /* will collect results during prog run */
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         /* test mode is used by selftests to
0056          * test functionality of bpf_hash_map iter.
0057          *
0058          * the above hashmap1 will have correct size
0059          * and will be accepted, hashmap2 and hashmap3
0060          * should be rejected due to smaller key/value
0061          * size.
0062          */
0063         if (key == (void *)0 || val == (void *)0)
0064             return 0;
0065 
0066         /* update the value and then delete the <key, value> pair.
0067          * it should not impact the existing 'val' which is still
0068          * accessible under rcu.
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     /* non-test mode, the map is prepared with the
0086      * below bpftool command sequence:
0087      *   bpftool map create /sys/fs/bpf/m1 type hash \
0088      *      key 12 value 8 entries 3 name map1
0089      *   bpftool map update id 77 key 0 0 0 1 0 0 0 0 0 0 0 1 \
0090      *      value 0 0 0 1 0 0 0 1
0091      *   bpftool map update id 77 key 0 0 0 1 0 0 0 0 0 0 0 2 \
0092      *      value 0 0 0 1 0 0 0 2
0093      * The bpftool iter command line:
0094      *   bpftool iter pin ./bpf_iter_bpf_hash_map.o /sys/fs/bpf/p1 \
0095      *      map id 77
0096      * The below output will be:
0097      *   map dump starts
0098      *   77: (1000000 0 2000000) (200000001000000)
0099      *   77: (1000000 0 1000000) (100000001000000)
0100      *   map dump ends
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 }