diff options
author | Yonghong Song <yhs@fb.com> | 2018-08-09 11:55:20 -0400 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-08-10 14:54:07 -0400 |
commit | 699c86d6ec21d0f885d12800249d138659de8489 (patch) | |
tree | bb2bd8aa6de8cc79a8112954356e241b58dfb8b2 | |
parent | dc1508a579e682a1e5f1ed0753390e0aa7c23a97 (diff) |
bpf: btf: add pretty print for hash/lru_hash maps
Commit a26ca7c982cb ("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>
-rw-r--r-- | kernel/bpf/hashtab.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 513d9dfcf4ee..d6110042e0d9 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c | |||
@@ -11,9 +11,11 @@ | |||
11 | * General Public License for more details. | 11 | * General Public License for more details. |
12 | */ | 12 | */ |
13 | #include <linux/bpf.h> | 13 | #include <linux/bpf.h> |
14 | #include <linux/btf.h> | ||
14 | #include <linux/jhash.h> | 15 | #include <linux/jhash.h> |
15 | #include <linux/filter.h> | 16 | #include <linux/filter.h> |
16 | #include <linux/rculist_nulls.h> | 17 | #include <linux/rculist_nulls.h> |
18 | #include <uapi/linux/btf.h> | ||
17 | #include "percpu_freelist.h" | 19 | #include "percpu_freelist.h" |
18 | #include "bpf_lru_list.h" | 20 | #include "bpf_lru_list.h" |
19 | #include "map_in_map.h" | 21 | #include "map_in_map.h" |
@@ -1162,6 +1164,44 @@ static void htab_map_free(struct bpf_map *map) | |||
1162 | kfree(htab); | 1164 | kfree(htab); |
1163 | } | 1165 | } |
1164 | 1166 | ||
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 | |||
1165 | const struct bpf_map_ops htab_map_ops = { | 1205 | const struct bpf_map_ops htab_map_ops = { |
1166 | .map_alloc_check = htab_map_alloc_check, | 1206 | .map_alloc_check = htab_map_alloc_check, |
1167 | .map_alloc = htab_map_alloc, | 1207 | .map_alloc = htab_map_alloc, |
@@ -1171,6 +1211,8 @@ const struct bpf_map_ops htab_map_ops = { | |||
1171 | .map_update_elem = htab_map_update_elem, | 1211 | .map_update_elem = htab_map_update_elem, |
1172 | .map_delete_elem = htab_map_delete_elem, | 1212 | .map_delete_elem = htab_map_delete_elem, |
1173 | .map_gen_lookup = htab_map_gen_lookup, | 1213 | .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, | ||
1174 | }; | 1216 | }; |
1175 | 1217 | ||
1176 | const struct bpf_map_ops htab_lru_map_ops = { | 1218 | const struct bpf_map_ops htab_lru_map_ops = { |
@@ -1182,6 +1224,8 @@ const struct bpf_map_ops htab_lru_map_ops = { | |||
1182 | .map_update_elem = htab_lru_map_update_elem, | 1224 | .map_update_elem = htab_lru_map_update_elem, |
1183 | .map_delete_elem = htab_lru_map_delete_elem, | 1225 | .map_delete_elem = htab_lru_map_delete_elem, |
1184 | .map_gen_lookup = htab_lru_map_gen_lookup, | 1226 | .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, | ||
1185 | }; | 1229 | }; |
1186 | 1230 | ||
1187 | /* Called from eBPF program */ | 1231 | /* Called from eBPF program */ |