History

bpf: btf: Add pretty print support to the basic arraymap

This patch adds pretty print support to the basic arraymap. Support for other bpf maps can be added later. This patch adds new attrs to the BPF_MAP_CREATE command to allow specifying the btf_fd, btf_key_id and btf_value_id. The BPF_MAP_CREATE can then associate the btf to the map if the creating map supports BTF. A BTF supported map needs to implement two new map ops, map_seq_show_elem() and map_check_btf(). This patch has implemented these new map ops for the basic arraymap. It also adds file_operations, bpffs_map_fops, to the pinned map such that the pinned map can be opened and read. After that, the user has an intuitive way to do "cat bpffs/pathto/a-pinned-map" instead of getting an error. bpffs_map_fops should not be extended further to support other operations. Other operations (e.g. write/key-lookup...) should be realized by the userspace tools (e.g. bpftool) through the BPF_OBJ_GET_INFO_BY_FD, map's lookup/update interface...etc. Follow up patches will allow the userspace to obtain the BTF from a map-fd. Here is a sample output when reading a pinned arraymap with the following map's value: struct map_value { int count_a; int count_b; }; cat /sys/fs/bpf/pinned_array_map: 0: {1,2} 1: {3,4} 2: {5,6} ...

Martin KaFai Lau committed 8 years ago

Commit a26ca7c1 parent 60197cf5 files · +254 −7Browse files

Showing the changes to kernel/bpf/arraymap.c (1 of 5 files). Show all

Unified · Split

kernel/bpf/arraymap.c+50−0
@@ -11,11 +11,13 @@ · 10 unmodified lines
1111 * General Public License for more details.
1212 */
1313 #include <linux/bpf.h>
14+#include <linux/btf.h>
1415 #include <linux/err.h>
1516 #include <linux/slab.h>
1617 #include <linux/mm.h>
1718 #include <linux/filter.h>
1819 #include <linux/perf_event.h>
20+#include <uapi/linux/btf.h>
1921
2022 #include "map_in_map.h"
2123
@@ -336,6 +338,52 @@ static void array_map_free(struct bpf_map *map) · 314 unmodified lines
336338 bpf_map_area_free(array);
337339 }
338340
341+static void array_map_seq_show_elem(struct bpf_map *map, void *key,
342+ struct seq_file *m)
343+{
344+ void *value;
345+
346+ rcu_read_lock();
347+
348+ value = array_map_lookup_elem(map, key);
349+ if (!value) {
350+ rcu_read_unlock();
351+ return;
352+ }
353+
354+ seq_printf(m, "%u: ", *(u32 *)key);
355+ btf_type_seq_show(map->btf, map->btf_value_id, value, m);
356+ seq_puts(m, "\n");
357+
358+ rcu_read_unlock();
359+}
360+
361+static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
362+ u32 btf_key_id, u32 btf_value_id)
363+{
364+ const struct btf_type *key_type, *value_type;
365+ u32 key_size, value_size;
366+ u32 int_data;
367+
368+ key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
369+ if (!key_type || BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
370+ return -EINVAL;
371+
372+ int_data = *(u32 *)(key_type + 1);
373+ /* bpf array can only take a u32 key. This check makes
374+ * sure that the btf matches the attr used during map_create.
375+ */
376+ if (BTF_INT_BITS(int_data) != 32 || key_size != 4 ||
377+ BTF_INT_OFFSET(int_data))
378+ return -EINVAL;
379+
380+ value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
381+ if (!value_type || value_size > map->value_size)
382+ return -EINVAL;
383+
384+ return 0;
385+}
386+
339387 const struct bpf_map_ops array_map_ops = {
340388 .map_alloc_check = array_map_alloc_check,
341389 .map_alloc = array_map_alloc,
@@ -345,6 +393,8 @@ const struct bpf_map_ops array_map_ops = { · 3 unmodified lines
345393 .map_update_elem = array_map_update_elem,
346394 .map_delete_elem = array_map_delete_elem,
347395 .map_gen_lookup = array_map_gen_lookup,
396+ .map_seq_show_elem = array_map_seq_show_elem,
397+ .map_check_btf = array_map_check_btf,
348398 };
349399
350400 const struct bpf_map_ops percpu_array_map_ops = {