Skip to content

Commit 699c86d

Browse files
yonghong-songborkmann
authored andcommitted
bpf: btf: add pretty print for hash/lru_hash maps
Commit a26ca7c ("bpf: btf: Add pretty print support to the basic arraymap") added pretty print support to array map. This patch adds pretty print for hash and lru_hash maps. The following example shows the pretty-print result of a pinned hashmap: struct map_value { int count_a; int count_b; }; cat /sys/fs/bpf/pinned_hash_map: 87907: {87907,87908} 57354: {37354,57355} 76625: {76625,76626} ... Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent dc1508a commit 699c86d

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎kernel/bpf/hashtab.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
* General Public License for more details.
1212
*/
1313
#include <linux/bpf.h>
14+
#include <linux/btf.h>
1415
#include <linux/jhash.h>
1516
#include <linux/filter.h>
1617
#include <linux/rculist_nulls.h>
18+
#include <uapi/linux/btf.h>
1719
#include "percpu_freelist.h"
1820
#include "bpf_lru_list.h"
1921
#include "map_in_map.h"
@@ -1162,6 +1164,44 @@ static void htab_map_free(struct bpf_map *map)
11621164
kfree(htab);
11631165
}
11641166

1167+
static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1168+
struct seq_file *m)
1169+
{
1170+
void *value;
1171+
1172+
rcu_read_lock();
1173+
1174+
value = htab_map_lookup_elem(map, key);
1175+
if (!value) {
1176+
rcu_read_unlock();
1177+
return;
1178+
}
1179+
1180+
btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1181+
seq_puts(m, ": ");
1182+
btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1183+
seq_puts(m, "\n");
1184+
1185+
rcu_read_unlock();
1186+
}
1187+
1188+
static int htab_map_check_btf(const struct bpf_map *map, const struct btf *btf,
1189+
u32 btf_key_id, u32 btf_value_id)
1190+
{
1191+
const struct btf_type *key_type, *value_type;
1192+
u32 key_size, value_size;
1193+
1194+
key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
1195+
if (!key_type || key_size != map->key_size)
1196+
return -EINVAL;
1197+
1198+
value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1199+
if (!value_type || value_size != map->value_size)
1200+
return -EINVAL;
1201+
1202+
return 0;
1203+
}
1204+
11651205
const struct bpf_map_ops htab_map_ops = {
11661206
.map_alloc_check = htab_map_alloc_check,
11671207
.map_alloc = htab_map_alloc,
@@ -1171,6 +1211,8 @@ const struct bpf_map_ops htab_map_ops = {
11711211
.map_update_elem = htab_map_update_elem,
11721212
.map_delete_elem = htab_map_delete_elem,
11731213
.map_gen_lookup = htab_map_gen_lookup,
1214+
.map_seq_show_elem = htab_map_seq_show_elem,
1215+
.map_check_btf = htab_map_check_btf,
11741216
};
11751217

11761218
const struct bpf_map_ops htab_lru_map_ops = {
@@ -1182,6 +1224,8 @@ const struct bpf_map_ops htab_lru_map_ops = {
11821224
.map_update_elem = htab_lru_map_update_elem,
11831225
.map_delete_elem = htab_lru_map_delete_elem,
11841226
.map_gen_lookup = htab_lru_map_gen_lookup,
1227+
.map_seq_show_elem = htab_map_seq_show_elem,
1228+
.map_check_btf = htab_map_check_btf,
11851229
};
11861230

11871231
/* Called from eBPF program */

0 commit comments

Comments
 (0)