bpftool: btf: Support dumping a single type from file
Subsystems (4)
v3 cover bpftool: btf: Support dumping a single type from file
Some projects, for example xdp-tools [0], prefer to check in a minimized vmlinux.h rather than the complete file which can get rather large.
However, when you try to add a minimized version of a complex struct (eg struct xfrm_state), things can get quite complex if you're trying to manually untangle and deduplicate the dependencies.
This commit teaches bpftool to do a minimized dump of a single type by providing an optional root_id argument.
Example usage:
$ ./bpftool btf dump file ~/dev/linux/vmlinux | rg "STRUCT 'xfrm_state'"
[12643] STRUCT 'xfrm_state' size=912 vlen=58 $ ./bpftool btf dump file ~/dev/linux/vmlinux root_id 12643 format c
#ifndef __VMLINUX_H__
#define __VMLINUX_H__ [..]
struct xfrm_type_offload;
struct xfrm_sec_ctx;
struct xfrm_state {
possible_net_t xs_net;
union {
struct hlist_node gclist;
struct hlist_node bydst;
};
union {
struct hlist_node dev_gclist;
struct hlist_node bysrc;
};
struct hlist_node byspi;
[..][0]: https://github.com/xdp-project/xdp-tools/blob/master/headers/bpf/vmlinux.h
=== Changelog ===
Changes in v3:
* Make root_id a top level btf-dump argument rather than attached to file
* Update bash completion script
* Refactor root_type_ids checking to after btf handle creation
* Update help messages and fix existing man page inconsistency
Changes in v2:
* Add early error check for invalid BTF ID
Daniel Xu (4):
bpftool: man: Add missing format argument to command description
bpftool: btf: Validate root_type_ids early
bpftool: btf: Support dumping a single type from file
bpftool: bash: Add bash completion for root_id argument
.../bpf/bpftool/Documentation/bpftool-btf.rst | 7 ++-- tools/bpf/bpftool/bash-completion/bpftool | 7 ++-- tools/bpf/bpftool/btf.c | 33 ++++++++++++++++++- 3 files changed, 42 insertions(+), 5 deletions(-)
Signature · 1 line
2.46.0
PATCH v3 1/4 bpftool: man: Add missing format argument to command description
The command description was missing the optional argument. Add it there for consistency with the rest of the commands.
--- tools/bpf/bpftool/Documentation/bpftool-btf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/Documentation/bpftool-btf.rst +1 -1
@@ -43,7 +43,7 @@ bpftool btf { show | list } [id *BTF_ID*] that hold open file descriptors (FDs) against BTF objects. On such kernels bpftool will automatically emit this information as well. -bpftool btf dump *BTF_SRC* +bpftool btf dump *BTF_SRC* [format *FORMAT*] Dump BTF entries from a given *BTF_SRC*. When **id** is specified, BTF object with that ID will be loaded and all
Signature · 1 line
2.46.0
PATCH v3 2/4 bpftool: btf: Validate root_type_ids early
In reply to Daniel XuHandle invalid root_type_ids early, as an invalid ID will cause dumpers to half-emit valid boilerplate and then bail with an unclean exit. This is ugly and possibly confusing for users, so preemptively handle the common error case before any dumping begins.
--- tools/bpf/bpftool/btf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/bpf/bpftool/btf.c +12 -0
@@ -886,6 +886,7 @@ static int do_dump(int argc, char **argv)const char *src;int fd =-1 ;int err =0 ; +int i;if (!REQ_ARGS (2 )) {usage ();
@@ -1017,6 +1018,17 @@ static int do_dump(int argc, char **argv) } } +/* Invalid root IDs causes half emitted boilerplate and then unclean +* exit. It's an ugly user experience, so handle common error here. +*/ +for (i =0 ; i < root_type_cnt; i++) { +if (root_type_ids[i] >=btf__type_cnt (btf)) { + err = -EINVAL ; +p_err ("invalid root ID: %u" , root_type_ids[i]); + goto done; + } + } +if (dump_c) {if (json_output) {p_err ("JSON output for C-syntax dump is not supported" );
Signature · 1 line
2.46.0
PATCH v3 3/4 bpftool: btf: Support dumping a single type from file
In reply to Daniel XuSome projects, for example xdp-tools [0], prefer to check in a minimized vmlinux.h rather than the complete file which can get rather large.
However, when you try to add a minimized version of a complex struct (eg struct xfrm_state), things can get quite complex if you're trying to manually untangle and deduplicate the dependencies.
This commit teaches bpftool to do a minimized dump of a single type by providing an optional root_id argument.
Example usage:
$ ./bpftool btf dump file ~/dev/linux/vmlinux | rg "STRUCT 'xfrm_state'"
[12643] STRUCT 'xfrm_state' size=912 vlen=58 $ ./bpftool btf dump file ~/dev/linux/vmlinux root_id 12643 format c
#ifndef __VMLINUX_H__
#define __VMLINUX_H__ [..]
struct xfrm_type_offload;
struct xfrm_sec_ctx;
struct xfrm_state {
possible_net_t xs_net;
union {
struct hlist_node gclist;
struct hlist_node bydst;
};
union {
struct hlist_node dev_gclist;
struct hlist_node bysrc;
};
struct hlist_node byspi;
[..][0]: https://github.com/xdp-project/xdp-tools/blob/master/headers/bpf/vmlinux.h
--- .../bpf/bpftool/Documentation/bpftool-btf.rst | 7 +++++-- tools/bpf/bpftool/btf.c | 21 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-btf.rst +5 -2
@@ -24,7 +24,7 @@ BTF COMMANDS ============= | **bpftool** **btf** { **show** | **list** } [**id** *BTF_ID*] -| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] +| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] [**root_id** *ROOT_ID*] | **bpftool** **btf help** | | *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }
@@ -43,7 +43,7 @@ bpftool btf { show | list } [id *BTF_ID*] that hold open file descriptors (FDs) against BTF objects. On such kernels bpftool will automatically emit this information as well. -bpftool btf dump *BTF_SRC* [format *FORMAT*] +bpftool btf dump *BTF_SRC* [format *FORMAT*] [root_id *ROOT_ID*] Dump BTF entries from a given *BTF_SRC*. When **id** is specified, BTF object with that ID will be loaded and all
@@ -67,6 +67,9 @@ bpftool btf dump *BTF_SRC* [format *FORMAT*] formatting, the output is sorted by default. Use the **unsorted** option to avoid sorting the output. + **root_id** option can be used to filter a dump to a single type and all + its dependent types. It cannot be used with any other types of filtering. + bpftool btf help Print short help message.
tools/bpf/bpftool/btf.c +20 -1
@@ -993,6 +993,25 @@ static int do_dump(int argc, char **argv) goto done; }NEXT_ARG (); + }else if (is_prefix (*argv,"root_id" )) { +__u32 root_id; +char *end; + +if (root_type_cnt) { +p_err ("cannot use root_id with other type filtering" ); + err = -EINVAL ; + goto done; + } + +NEXT_ARG (); + root_id =strtoul (*argv, &end,0 ); +if (*end) { + err =-1 ; +p_err ("can't parse %s as root ID" , *argv); + goto done; + } + root_type_ids[root_type_cnt++] = root_id; +NEXT_ARG (); }else if (is_prefix (*argv,"unsorted" )) { sort_dump_c = false;NEXT_ARG ();
@@ -1403,7 +1422,7 @@ static int do_help(int argc, char **argv)fprintf (stderr,"Usage: %1$s %2$s { show | list } [id BTF_ID]\n" -" %1$s %2$s dump BTF_SRC [format FORMAT]\n" +" %1$s %2$s dump BTF_SRC [format FORMAT] [root_id ROOT_ID]\n" " %1$s %2$s help\n" "\n" " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
Signature · 1 line
2.46.0
Quoted 84 lines · Daniel Xu
Some projects, for example xdp-tools [0], prefer to check in a minimized
vmlinux.hrather than the complete file which can get rather large.However, when you try to add a minimized version of a complex struct (eg struct
xfrm_state), things can get quite complex if you're trying to manually untangle and deduplicate the dependencies.This commit teaches bpftool to do a minimized dump of a single type by providing an optional
root_idargument.Example usage:
$ ./bpftool btf dump file ~/dev/linux/vmlinux | rg "STRUCT 'xfrm_state'" [12643] STRUCT 'xfrm_state' size=912 vlen=58$ ./bpftool btf dump file ~/dev/linux/vmlinux root_id 12643 format c #ifndef __VMLINUX_H__ #define __VMLINUX_H__[..] struct xfrm_type_offload; struct xfrm_sec_ctx; struct xfrm_state { possible_net_t xs_net; union { struct hlist_node gclist; struct hlist_node bydst; }; union { struct hlist_node dev_gclist; struct hlist_node bysrc; }; struct hlist_node byspi; [..][0]: https://github.com/xdp-project/xdp-tools/blob/master/headers/bpf/vmlinux.h
Signed-off-by Daniel Xu <[email protected]>--- .../bpf/bpftool/Documentation/bpftool-btf.rst | 7 +++++-- tools/bpf/bpftool/btf.c | 21 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-)tools/bpf/bpftool/Documentation/bpftool-btf.rst +5
-2@@ -24,7 +24,7 @@ BTF COMMANDS ============= | **bpftool** **btf** { **show** | **list** } [**id** *BTF_ID*] -| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] +| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] [**root_id** *ROOT_ID*] | **bpftool** **btf help** | | *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }@@ -43,7 +43,7 @@ bpftool btf { show | list } [id *BTF_ID*] that hold open file descriptors (FDs) against BTF objects. On such kernels bpftool will automatically emit this information as well. -bpftool btf dump *BTF_SRC* [format *FORMAT*] +bpftool btf dump *BTF_SRC* [format *FORMAT*] [root_id *ROOT_ID*] Dump BTF entries from a given *BTF_SRC*. When **id** is specified, BTF object with that ID will be loaded and all@@ -67,6 +67,9 @@ bpftool btf dump *BTF_SRC* [format *FORMAT*] formatting, the output is sorted by default. Use the **unsorted** option to avoid sorting the output. + **root_id** option can be used to filter a dump to a single type and all + its dependent types. It cannot be used with any other types of filtering. + bpftool btf help Print short help message.tools/bpf/bpftool/btf.c +0
-0
tools/bpf/bpftool/btf.c static int do_dump(int argc, char **argv) · line 1001
Quoted 9 lines
tools/bpf/bpftool/btf.c +6
-0@@ -993,6 +993,25 @@ static int do_dump(int argc, char **argv) goto done; }NEXT_ARG (); + }else if (is_prefix (*argv,"root_id" )) { +__u32 root_id; +char *end; + +if (root_type_cnt) { +p_err ("cannot use root_id with other type filtering" );
this is a confusing error if the user just wanted to provide two root_id arguments... Also, why don't we allow multiple root_ids?
I'd bump root_type_ids[] to have something like 16 elements or something (though we can always do dynamic realloc as well, probably), and allow multiple types to be specified.
Thoughts?
Quoted 28 lines
tools/bpf/bpftool/btf.c +14
-2+ err = -EINVAL ; + goto done; + } + +NEXT_ARG (); + root_id =strtoul (*argv, &end,0 ); +if (*end) { + err =-1 ; +p_err ("can't parse %s as root ID" , *argv); + goto done; + } + root_type_ids[root_type_cnt++] = root_id; +NEXT_ARG (); }else if (is_prefix (*argv,"unsorted" )) { sort_dump_c = false;NEXT_ARG ();@@ -1403,7 +1422,7 @@ static int do_help(int argc, char **argv)fprintf (stderr,"Usage: %1$s %2$s { show | list } [id BTF_ID]\n" - " %1$s %2$s dump BTF_SRC [format FORMAT]\n" +" %1$s %2$s dump BTF_SRC [format FORMAT] [root_id ROOT_ID]\n" " %1$s %2$s help\n" "\n" " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n" --2.46.0
Quoted 98 lines · Andrii Nakryiko
Quoted 84 lines · Daniel Xu
Some projects, for example xdp-tools [0], prefer to check in a minimized
vmlinux.hrather than the complete file which can get rather large.However, when you try to add a minimized version of a complex struct (eg struct
xfrm_state), things can get quite complex if you're trying to manually untangle and deduplicate the dependencies.This commit teaches bpftool to do a minimized dump of a single type by providing an optional
root_idargument.Example usage:
$ ./bpftool btf dump file ~/dev/linux/vmlinux | rg "STRUCT 'xfrm_state'" [12643] STRUCT 'xfrm_state' size=912 vlen=58$ ./bpftool btf dump file ~/dev/linux/vmlinux root_id 12643 format c #ifndef __VMLINUX_H__ #define __VMLINUX_H__[..] struct xfrm_type_offload; struct xfrm_sec_ctx; struct xfrm_state { possible_net_t xs_net; union { struct hlist_node gclist; struct hlist_node bydst; }; union { struct hlist_node dev_gclist; struct hlist_node bysrc; }; struct hlist_node byspi; [..][0]: https://github.com/xdp-project/xdp-tools/blob/master/headers/bpf/vmlinux.h
Signed-off-by Daniel Xu <[email protected]>--- .../bpf/bpftool/Documentation/bpftool-btf.rst | 7 +++++-- tools/bpf/bpftool/btf.c | 21 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-)tools/bpf/bpftool/Documentation/bpftool-btf.rst +5
-2@@ -24,7 +24,7 @@ BTF COMMANDS ============= | **bpftool** **btf** { **show** | **list** } [**id** *BTF_ID*] -| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] +| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] [**root_id** *ROOT_ID*] | **bpftool** **btf help** | | *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }@@ -43,7 +43,7 @@ bpftool btf { show | list } [id *BTF_ID*] that hold open file descriptors (FDs) against BTF objects. On such kernels bpftool will automatically emit this information as well. -bpftool btf dump *BTF_SRC* [format *FORMAT*] +bpftool btf dump *BTF_SRC* [format *FORMAT*] [root_id *ROOT_ID*] Dump BTF entries from a given *BTF_SRC*. When **id** is specified, BTF object with that ID will be loaded and all@@ -67,6 +67,9 @@ bpftool btf dump *BTF_SRC* [format *FORMAT*] formatting, the output is sorted by default. Use the **unsorted** option to avoid sorting the output. + **root_id** option can be used to filter a dump to a single type and all + its dependent types. It cannot be used with any other types of filtering. + bpftool btf help Print short help message.tools/bpf/bpftool/btf.c +0
-0tools/bpf/bpftool/btf.c static int do_dump(int argc, char **argv) · line 1001
Quoted 9 lines
tools/bpf/bpftool/btf.c +6
-0@@ -993,6 +993,25 @@ static int do_dump(int argc, char **argv) goto done; }NEXT_ARG (); + }else if (is_prefix (*argv,"root_id" )) { +__u32 root_id; +char *end; + +if (root_type_cnt) { +p_err ("cannot use root_id with other type filtering" );this is a confusing error if the user just wanted to provide two
root_idarguments... Also, why don't we allow multipleroot_ids?
Quoted 5 lines
I'd bump root_type_ids[] to have something like 16 elements or something (though we can always do dynamic realloc as well, probably), and allow multiple types to be specified.
Thoughts?
That's a good point. I added this check b/c I didn't think it would make sense to allow root_id filtering in combination with map dump filtering (which uses same root_type_ids param):
map MAP [{key | value | kv | all}]
But code can easily be tweaked to still block combination but allow multiple root_ids when used alone. 16 seems sufficient to me.
Do you think it'd be more bpftool-y to require "root_id" each time or to just take a comma separated value?
Quoted 110 lines · Daniel Xu
Quoted 98 lines · Andrii Nakryiko
Quoted 84 lines · Daniel Xu
Some projects, for example xdp-tools [0], prefer to check in a minimized
vmlinux.hrather than the complete file which can get rather large.However, when you try to add a minimized version of a complex struct (eg struct
xfrm_state), things can get quite complex if you're trying to manually untangle and deduplicate the dependencies.This commit teaches bpftool to do a minimized dump of a single type by providing an optional
root_idargument.Example usage:
$ ./bpftool btf dump file ~/dev/linux/vmlinux | rg "STRUCT 'xfrm_state'" [12643] STRUCT 'xfrm_state' size=912 vlen=58$ ./bpftool btf dump file ~/dev/linux/vmlinux root_id 12643 format c #ifndef __VMLINUX_H__ #define __VMLINUX_H__[..] struct xfrm_type_offload; struct xfrm_sec_ctx; struct xfrm_state { possible_net_t xs_net; union { struct hlist_node gclist; struct hlist_node bydst; }; union { struct hlist_node dev_gclist; struct hlist_node bysrc; }; struct hlist_node byspi; [..][0]: https://github.com/xdp-project/xdp-tools/blob/master/headers/bpf/vmlinux.h
Signed-off-by Daniel Xu <[email protected]>--- .../bpf/bpftool/Documentation/bpftool-btf.rst | 7 +++++-- tools/bpf/bpftool/btf.c | 21 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-)tools/bpf/bpftool/Documentation/bpftool-btf.rst +5
-2@@ -24,7 +24,7 @@ BTF COMMANDS ============= | **bpftool** **btf** { **show** | **list** } [**id** *BTF_ID*] -| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] +| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] [**root_id** *ROOT_ID*] | **bpftool** **btf help** | | *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }@@ -43,7 +43,7 @@ bpftool btf { show | list } [id *BTF_ID*] that hold open file descriptors (FDs) against BTF objects. On such kernels bpftool will automatically emit this information as well. -bpftool btf dump *BTF_SRC* [format *FORMAT*] +bpftool btf dump *BTF_SRC* [format *FORMAT*] [root_id *ROOT_ID*] Dump BTF entries from a given *BTF_SRC*. When **id** is specified, BTF object with that ID will be loaded and all@@ -67,6 +67,9 @@ bpftool btf dump *BTF_SRC* [format *FORMAT*] formatting, the output is sorted by default. Use the **unsorted** option to avoid sorting the output. + **root_id** option can be used to filter a dump to a single type and all + its dependent types. It cannot be used with any other types of filtering. + bpftool btf help Print short help message.tools/bpf/bpftool/btf.c +0
-0tools/bpf/bpftool/btf.c static int do_dump(int argc, char **argv) · line 1001
Quoted 9 lines
tools/bpf/bpftool/btf.c +6
-0@@ -993,6 +993,25 @@ static int do_dump(int argc, char **argv) goto done; }NEXT_ARG (); + }else if (is_prefix (*argv,"root_id" )) { +__u32 root_id; +char *end; + +if (root_type_cnt) { +p_err ("cannot use root_id with other type filtering" );this is a confusing error if the user just wanted to provide two
root_idarguments... Also, why don't we allow multipleroot_ids?Quoted 5 lines
I'd bump root_type_ids[] to have something like 16 elements or something (though we can always do dynamic realloc as well, probably), and allow multiple types to be specified.
Thoughts?
That's a good point. I added this check b/c I didn't think it would make sense to allow
root_idfiltering in combination with map dump filtering (which uses sameroot_type_idsparam):
Quoted 7 lines
map MAP [{key | value | kv | all}]
But code can easily be tweaked to still block combination but allow multiple
root_ids when used alone. 16 seems sufficient to me.Do you think it'd be more bpftool-y to require "
root_id" each time or to just take a comma separated value?
root_id 123 root_id 345 root_id 789
definitely more bpftool-y
PATCH v3 4/4 bpftool: bash: Add bash completion for root_id argument
In reply to Daniel XuThis commit updates the bash completion script with the new root_id argument.
--- tools/bpf/bpftool/bash-completion/bpftool | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
tools/bpf/bpftool/bash-completion/bpftool +5 -2
@@ -930,6 +930,9 @@ _bpftool() format) COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) ) ;; + root_id) + return 0; + ;; c) COMPREPLY=( $( compgen -W "unsorted" -- "$cur" ) ) ;;
@@ -937,13 +940,13 @@ _bpftool() # emit extra options case ${words[3]} in id|file) - _bpftool_once_attr 'format' + _bpftool_once_attr 'format root_id' ;; map|prog) if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) ) fi - _bpftool_once_attr 'format' + _bpftool_once_attr 'format root_id' ;; *) ;;
Signature · 1 line
2.46.0