Skip to content

Commit 4e1ea33

Browse files
iamkafaiAlexei Starovoitov
authored andcommitted
bpftool: Support dumping a map with btf_vmlinux_value_type_id
This patch makes bpftool support dumping a map's value properly when the map's value type is a type of the running kernel's btf. (i.e. map_info.btf_vmlinux_value_type_id is set instead of map_info.btf_value_type_id). The first usecase is for the BPF_MAP_TYPE_STRUCT_OPS. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200115230044.1103008-1-kafai@fb.com
1 parent 84c72ce commit 4e1ea33

1 file changed

Lines changed: 50 additions & 11 deletions

File tree

‎tools/bpf/bpftool/map.c‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ static int do_dump_btf(const struct btf_dumper *d,
252252
struct bpf_map_info *map_info, void *key,
253253
void *value)
254254
{
255+
__u32 value_id;
255256
int ret;
256257

257258
/* start of key-value pair */
@@ -265,9 +266,12 @@ static int do_dump_btf(const struct btf_dumper *d,
265266
goto err_end_obj;
266267
}
267268

269+
value_id = map_info->btf_vmlinux_value_type_id ?
270+
: map_info->btf_value_type_id;
271+
268272
if (!map_is_per_cpu(map_info->type)) {
269273
jsonw_name(d->jw, "value");
270-
ret = btf_dumper_type(d, map_info->btf_value_type_id, value);
274+
ret = btf_dumper_type(d, value_id, value);
271275
} else {
272276
unsigned int i, n, step;
273277

@@ -279,8 +283,7 @@ static int do_dump_btf(const struct btf_dumper *d,
279283
jsonw_start_object(d->jw);
280284
jsonw_int_field(d->jw, "cpu", i);
281285
jsonw_name(d->jw, "value");
282-
ret = btf_dumper_type(d, map_info->btf_value_type_id,
283-
value + i * step);
286+
ret = btf_dumper_type(d, value_id, value + i * step);
284287
jsonw_end_object(d->jw);
285288
if (ret)
286289
break;
@@ -932,6 +935,44 @@ static int maps_have_btf(int *fds, int nb_fds)
932935
return 1;
933936
}
934937

938+
static struct btf *btf_vmlinux;
939+
940+
static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
941+
{
942+
struct btf *btf = NULL;
943+
944+
if (info->btf_vmlinux_value_type_id) {
945+
if (!btf_vmlinux) {
946+
btf_vmlinux = libbpf_find_kernel_btf();
947+
if (IS_ERR(btf_vmlinux))
948+
p_err("failed to get kernel btf");
949+
}
950+
return btf_vmlinux;
951+
} else if (info->btf_value_type_id) {
952+
int err;
953+
954+
err = btf__get_from_id(info->btf_id, &btf);
955+
if (err || !btf) {
956+
p_err("failed to get btf");
957+
btf = err ? ERR_PTR(err) : ERR_PTR(-ESRCH);
958+
}
959+
}
960+
961+
return btf;
962+
}
963+
964+
static void free_map_kv_btf(struct btf *btf)
965+
{
966+
if (!IS_ERR(btf) && btf != btf_vmlinux)
967+
btf__free(btf);
968+
}
969+
970+
static void free_btf_vmlinux(void)
971+
{
972+
if (!IS_ERR(btf_vmlinux))
973+
btf__free(btf_vmlinux);
974+
}
975+
935976
static int
936977
map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
937978
bool show_header)
@@ -952,13 +993,10 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
952993
prev_key = NULL;
953994

954995
if (wtr) {
955-
if (info->btf_id) {
956-
err = btf__get_from_id(info->btf_id, &btf);
957-
if (err || !btf) {
958-
err = err ? : -ESRCH;
959-
p_err("failed to get btf");
960-
goto exit_free;
961-
}
996+
btf = get_map_kv_btf(info);
997+
if (IS_ERR(btf)) {
998+
err = PTR_ERR(btf);
999+
goto exit_free;
9621000
}
9631001

9641002
if (show_header) {
@@ -999,7 +1037,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
9991037
free(key);
10001038
free(value);
10011039
close(fd);
1002-
btf__free(btf);
1040+
free_map_kv_btf(btf);
10031041

10041042
return err;
10051043
}
@@ -1067,6 +1105,7 @@ static int do_dump(int argc, char **argv)
10671105
close(fds[i]);
10681106
exit_free:
10691107
free(fds);
1108+
free_btf_vmlinux();
10701109
return err;
10711110
}
10721111

0 commit comments

Comments
 (0)