diff options
author | Yonghong Song <yhs@fb.com> | 2018-08-29 17:43:13 -0400 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-08-30 08:03:53 -0400 |
commit | c7b27c37af3da5a63f32b0bc99569e3069e4d9c1 (patch) | |
tree | 91ae3f4139905d41b937717f5e3de4522363f0dc | |
parent | 234dbe3dc1db002076bb593c1f6dc5f3ebc46b21 (diff) |
bpf: add bpffs pretty print for percpu arraymap/hash/lru_hash
Added bpffs pretty print for percpu arraymap, percpu hashmap
and percpu lru hashmap.
For each map <key, value> pair, the format is:
<key_value>: {
cpu0: <value_on_cpu0>
cpu1: <value_on_cpu1>
...
cpun: <value_on_cpun>
}
For example, on my VM, there are 4 cpus, and
for test_btf test in the next patch:
cat /sys/fs/bpf/pprint_test_percpu_hash
You may get:
...
43602: {
cpu0: {43602,0,-43602,0x3,0xaa52,0x3,{43602|[82,170,0,0,0,0,0,0]},ENUM_TWO}
cpu1: {43602,0,-43602,0x3,0xaa52,0x3,{43602|[82,170,0,0,0,0,0,0]},ENUM_TWO}
cpu2: {43602,0,-43602,0x3,0xaa52,0x3,{43602|[82,170,0,0,0,0,0,0]},ENUM_TWO}
cpu3: {43602,0,-43602,0x3,0xaa52,0x3,{43602|[82,170,0,0,0,0,0,0]},ENUM_TWO}
}
72847: {
cpu0: {72847,0,-72847,0x3,0x11c8f,0x3,{72847|[143,28,1,0,0,0,0,0]},ENUM_THREE}
cpu1: {72847,0,-72847,0x3,0x11c8f,0x3,{72847|[143,28,1,0,0,0,0,0]},ENUM_THREE}
cpu2: {72847,0,-72847,0x3,0x11c8f,0x3,{72847|[143,28,1,0,0,0,0,0]},ENUM_THREE}
cpu3: {72847,0,-72847,0x3,0x11c8f,0x3,{72847|[143,28,1,0,0,0,0,0]},ENUM_THREE}
}
...
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
-rw-r--r-- | kernel/bpf/arraymap.c | 24 | ||||
-rw-r--r-- | kernel/bpf/hashtab.c | 31 |
2 files changed, 55 insertions, 0 deletions
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 0c17aab3ce5f..f9d24121be99 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c | |||
@@ -358,6 +358,29 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key, | |||
358 | rcu_read_unlock(); | 358 | rcu_read_unlock(); |
359 | } | 359 | } |
360 | 360 | ||
361 | static void percpu_array_map_seq_show_elem(struct bpf_map *map, void *key, | ||
362 | struct seq_file *m) | ||
363 | { | ||
364 | struct bpf_array *array = container_of(map, struct bpf_array, map); | ||
365 | u32 index = *(u32 *)key; | ||
366 | void __percpu *pptr; | ||
367 | int cpu; | ||
368 | |||
369 | rcu_read_lock(); | ||
370 | |||
371 | seq_printf(m, "%u: {\n", *(u32 *)key); | ||
372 | pptr = array->pptrs[index & array->index_mask]; | ||
373 | for_each_possible_cpu(cpu) { | ||
374 | seq_printf(m, "\tcpu%d: ", cpu); | ||
375 | btf_type_seq_show(map->btf, map->btf_value_type_id, | ||
376 | per_cpu_ptr(pptr, cpu), m); | ||
377 | seq_puts(m, "\n"); | ||
378 | } | ||
379 | seq_puts(m, "}\n"); | ||
380 | |||
381 | rcu_read_unlock(); | ||
382 | } | ||
383 | |||
361 | static int array_map_check_btf(const struct bpf_map *map, | 384 | static int array_map_check_btf(const struct bpf_map *map, |
362 | const struct btf_type *key_type, | 385 | const struct btf_type *key_type, |
363 | const struct btf_type *value_type) | 386 | const struct btf_type *value_type) |
@@ -398,6 +421,7 @@ const struct bpf_map_ops percpu_array_map_ops = { | |||
398 | .map_lookup_elem = percpu_array_map_lookup_elem, | 421 | .map_lookup_elem = percpu_array_map_lookup_elem, |
399 | .map_update_elem = array_map_update_elem, | 422 | .map_update_elem = array_map_update_elem, |
400 | .map_delete_elem = array_map_delete_elem, | 423 | .map_delete_elem = array_map_delete_elem, |
424 | .map_seq_show_elem = percpu_array_map_seq_show_elem, | ||
401 | .map_check_btf = array_map_check_btf, | 425 | .map_check_btf = array_map_check_btf, |
402 | }; | 426 | }; |
403 | 427 | ||
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 03cc59ee9c95..2c1790288138 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c | |||
@@ -1285,6 +1285,35 @@ int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, | |||
1285 | return ret; | 1285 | return ret; |
1286 | } | 1286 | } |
1287 | 1287 | ||
1288 | static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key, | ||
1289 | struct seq_file *m) | ||
1290 | { | ||
1291 | struct htab_elem *l; | ||
1292 | void __percpu *pptr; | ||
1293 | int cpu; | ||
1294 | |||
1295 | rcu_read_lock(); | ||
1296 | |||
1297 | l = __htab_map_lookup_elem(map, key); | ||
1298 | if (!l) { | ||
1299 | rcu_read_unlock(); | ||
1300 | return; | ||
1301 | } | ||
1302 | |||
1303 | btf_type_seq_show(map->btf, map->btf_key_type_id, key, m); | ||
1304 | seq_puts(m, ": {\n"); | ||
1305 | pptr = htab_elem_get_ptr(l, map->key_size); | ||
1306 | for_each_possible_cpu(cpu) { | ||
1307 | seq_printf(m, "\tcpu%d: ", cpu); | ||
1308 | btf_type_seq_show(map->btf, map->btf_value_type_id, | ||
1309 | per_cpu_ptr(pptr, cpu), m); | ||
1310 | seq_puts(m, "\n"); | ||
1311 | } | ||
1312 | seq_puts(m, "}\n"); | ||
1313 | |||
1314 | rcu_read_unlock(); | ||
1315 | } | ||
1316 | |||
1288 | const struct bpf_map_ops htab_percpu_map_ops = { | 1317 | const struct bpf_map_ops htab_percpu_map_ops = { |
1289 | .map_alloc_check = htab_map_alloc_check, | 1318 | .map_alloc_check = htab_map_alloc_check, |
1290 | .map_alloc = htab_map_alloc, | 1319 | .map_alloc = htab_map_alloc, |
@@ -1293,6 +1322,7 @@ const struct bpf_map_ops htab_percpu_map_ops = { | |||
1293 | .map_lookup_elem = htab_percpu_map_lookup_elem, | 1322 | .map_lookup_elem = htab_percpu_map_lookup_elem, |
1294 | .map_update_elem = htab_percpu_map_update_elem, | 1323 | .map_update_elem = htab_percpu_map_update_elem, |
1295 | .map_delete_elem = htab_map_delete_elem, | 1324 | .map_delete_elem = htab_map_delete_elem, |
1325 | .map_seq_show_elem = htab_percpu_map_seq_show_elem, | ||
1296 | }; | 1326 | }; |
1297 | 1327 | ||
1298 | const struct bpf_map_ops htab_lru_percpu_map_ops = { | 1328 | const struct bpf_map_ops htab_lru_percpu_map_ops = { |
@@ -1303,6 +1333,7 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = { | |||
1303 | .map_lookup_elem = htab_lru_percpu_map_lookup_elem, | 1333 | .map_lookup_elem = htab_lru_percpu_map_lookup_elem, |
1304 | .map_update_elem = htab_lru_percpu_map_update_elem, | 1334 | .map_update_elem = htab_lru_percpu_map_update_elem, |
1305 | .map_delete_elem = htab_lru_map_delete_elem, | 1335 | .map_delete_elem = htab_lru_map_delete_elem, |
1336 | .map_seq_show_elem = htab_percpu_map_seq_show_elem, | ||
1306 | }; | 1337 | }; |
1307 | 1338 | ||
1308 | static int fd_htab_map_alloc_check(union bpf_attr *attr) | 1339 | static int fd_htab_map_alloc_check(union bpf_attr *attr) |