diff mbox series

[v2,bpf-next,5/5] bpftool: Support dumping a map with btf_vmlinux_value_type_id

Message ID 20200115222312.948025-1-kafai@fb.com
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series bpftool: Support dumping a map with btf_vmlinux_value_type_id | expand

Commit Message

Martin KaFai Lau Jan. 15, 2020, 10:23 p.m. UTC
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>
---
 tools/bpf/bpftool/map.c | 62 +++++++++++++++++++++++++++++++++--------
 1 file changed, 51 insertions(+), 11 deletions(-)

Comments

Andrii Nakryiko Jan. 15, 2020, 10:46 p.m. UTC | #1
On Wed, Jan 15, 2020 at 2:28 PM Martin KaFai Lau <kafai@fb.com> wrote:
>
> 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>
> ---

LGTM.

Acked-by: Andrii Nakryiko <andriin@fb.com>

>  tools/bpf/bpftool/map.c | 62 +++++++++++++++++++++++++++++++++--------
>  1 file changed, 51 insertions(+), 11 deletions(-)
>

[...]
Martin KaFai Lau Jan. 15, 2020, 10:49 p.m. UTC | #2
On Wed, Jan 15, 2020 at 02:46:10PM -0800, Andrii Nakryiko wrote:
> On Wed, Jan 15, 2020 at 2:28 PM Martin KaFai Lau <kafai@fb.com> wrote:
> >
> > 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>
> > ---
> 
> LGTM.
> 
> Acked-by: Andrii Nakryiko <andriin@fb.com>
Thanks for the review!

I just noticied I forgot to remove the #include "libbpf_internal.h".
I will respin one more.
Andrii Nakryiko Jan. 15, 2020, 10:59 p.m. UTC | #3
On Wed, Jan 15, 2020 at 2:50 PM Martin Lau <kafai@fb.com> wrote:
>
> On Wed, Jan 15, 2020 at 02:46:10PM -0800, Andrii Nakryiko wrote:
> > On Wed, Jan 15, 2020 at 2:28 PM Martin KaFai Lau <kafai@fb.com> wrote:
> > >
> > > 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>
> > > ---
> >
> > LGTM.
> >
> > Acked-by: Andrii Nakryiko <andriin@fb.com>
> Thanks for the review!
>
> I just noticied I forgot to remove the #include "libbpf_internal.h".
> I will respin one more.

didn't notice that. Please also update a subject on patch exposing
libbpf_find_kernel_btf (it still mentions old name).
Martin KaFai Lau Jan. 15, 2020, 11:12 p.m. UTC | #4
On Wed, Jan 15, 2020 at 02:59:59PM -0800, Andrii Nakryiko wrote:
> On Wed, Jan 15, 2020 at 2:50 PM Martin Lau <kafai@fb.com> wrote:
> >
> > On Wed, Jan 15, 2020 at 02:46:10PM -0800, Andrii Nakryiko wrote:
> > > On Wed, Jan 15, 2020 at 2:28 PM Martin KaFai Lau <kafai@fb.com> wrote:
> > > >
> > > > 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>
> > > > ---
> > >
> > > LGTM.
> > >
> > > Acked-by: Andrii Nakryiko <andriin@fb.com>
> > Thanks for the review!
> >
> > I just noticied I forgot to remove the #include "libbpf_internal.h".
> > I will respin one more.
> 
> didn't notice that. Please also update a subject on patch exposing
> libbpf_find_kernel_btf (it still mentions old name).
oops. already went out. :p
The commit message mentioned the renaming from old to new name.
Andrii Nakryiko Jan. 15, 2020, 11:41 p.m. UTC | #5
On Wed, Jan 15, 2020 at 3:12 PM Martin Lau <kafai@fb.com> wrote:
>
> On Wed, Jan 15, 2020 at 02:59:59PM -0800, Andrii Nakryiko wrote:
> > On Wed, Jan 15, 2020 at 2:50 PM Martin Lau <kafai@fb.com> wrote:
> > >
> > > On Wed, Jan 15, 2020 at 02:46:10PM -0800, Andrii Nakryiko wrote:
> > > > On Wed, Jan 15, 2020 at 2:28 PM Martin KaFai Lau <kafai@fb.com> wrote:
> > > > >
> > > > > 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>
> > > > > ---
> > > >
> > > > LGTM.
> > > >
> > > > Acked-by: Andrii Nakryiko <andriin@fb.com>
> > > Thanks for the review!
> > >
> > > I just noticied I forgot to remove the #include "libbpf_internal.h".
> > > I will respin one more.
> >
> > didn't notice that. Please also update a subject on patch exposing
> > libbpf_find_kernel_btf (it still mentions old name).
> oops. already went out. :p
> The commit message mentioned the renaming from old to new name.

no worries
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index 4c5b15d736b6..33636f86bcbb 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -20,6 +20,7 @@ 
 #include "btf.h"
 #include "json_writer.h"
 #include "main.h"
+#include "libbpf_internal.h"
 
 const char * const map_type_name[] = {
 	[BPF_MAP_TYPE_UNSPEC]			= "unspec",
@@ -252,6 +253,7 @@  static int do_dump_btf(const struct btf_dumper *d,
 		       struct bpf_map_info *map_info, void *key,
 		       void *value)
 {
+	__u32 value_id;
 	int ret;
 
 	/* start of key-value pair */
@@ -265,9 +267,12 @@  static int do_dump_btf(const struct btf_dumper *d,
 			goto err_end_obj;
 	}
 
+	value_id = map_info->btf_vmlinux_value_type_id ?
+		: map_info->btf_value_type_id;
+
 	if (!map_is_per_cpu(map_info->type)) {
 		jsonw_name(d->jw, "value");
-		ret = btf_dumper_type(d, map_info->btf_value_type_id, value);
+		ret = btf_dumper_type(d, value_id, value);
 	} else {
 		unsigned int i, n, step;
 
@@ -279,8 +284,7 @@  static int do_dump_btf(const struct btf_dumper *d,
 			jsonw_start_object(d->jw);
 			jsonw_int_field(d->jw, "cpu", i);
 			jsonw_name(d->jw, "value");
-			ret = btf_dumper_type(d, map_info->btf_value_type_id,
-					      value + i * step);
+			ret = btf_dumper_type(d, value_id, value + i * step);
 			jsonw_end_object(d->jw);
 			if (ret)
 				break;
@@ -932,6 +936,44 @@  static int maps_have_btf(int *fds, int nb_fds)
 	return 1;
 }
 
+static struct btf *btf_vmlinux;
+
+static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
+{
+	struct btf *btf = NULL;
+
+	if (info->btf_vmlinux_value_type_id) {
+		if (!btf_vmlinux) {
+			btf_vmlinux = libbpf_find_kernel_btf();
+			if (IS_ERR(btf_vmlinux))
+				p_err("failed to get kernel btf");
+		}
+		return btf_vmlinux;
+	} else if (info->btf_value_type_id) {
+		int err;
+
+		err = btf__get_from_id(info->btf_id, &btf);
+		if (err || !btf) {
+			p_err("failed to get btf");
+			btf = err ? ERR_PTR(err) : ERR_PTR(-ESRCH);
+		}
+	}
+
+	return btf;
+}
+
+static void free_map_kv_btf(struct btf *btf)
+{
+	if (!IS_ERR(btf) && btf != btf_vmlinux)
+		btf__free(btf);
+}
+
+static void free_btf_vmlinux(void)
+{
+	if (!IS_ERR(btf_vmlinux))
+		btf__free(btf_vmlinux);
+}
+
 static int
 map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
 	 bool show_header)
@@ -952,13 +994,10 @@  map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
 	prev_key = NULL;
 
 	if (wtr) {
-		if (info->btf_id) {
-			err = btf__get_from_id(info->btf_id, &btf);
-			if (err || !btf) {
-				err = err ? : -ESRCH;
-				p_err("failed to get btf");
-				goto exit_free;
-			}
+		btf = get_map_kv_btf(info);
+		if (IS_ERR(btf)) {
+			err = PTR_ERR(btf);
+			goto exit_free;
 		}
 
 		if (show_header) {
@@ -999,7 +1038,7 @@  map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
 	free(key);
 	free(value);
 	close(fd);
-	btf__free(btf);
+	free_map_kv_btf(btf);
 
 	return err;
 }
@@ -1067,6 +1106,7 @@  static int do_dump(int argc, char **argv)
 		close(fds[i]);
 exit_free:
 	free(fds);
+	free_btf_vmlinux();
 	return err;
 }