mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Quentin Monnet <qmo@kernel.org>
To: Daniel Xu <dxu@dxuuu.xyz>,
	hawk@kernel.org, kuba@kernel.org, andrii@kernel.org,
	john.fastabend@gmail.com, ast@kernel.org, daniel@iogearbox.net,
	davem@davemloft.net
Cc: martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org,
	yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me,
	haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	andrii.nakryiko@gmail.com, antony@phenome.org, toke@kernel.org
Subject: Re: [PATCH bpf-next v4 3/4] bpftool: btf: Support dumping a specific types from file
Date: Fri, 13 Dec 2024 15:17:36 +0000	[thread overview]
Message-ID: <7fa902e5-0916-4bc9-b1e0-2729903d3de0@kernel.org> (raw)
In-Reply-To: <5ec7617fd9c28ff721947aceb80937dc10fca770.1734052995.git.dxu@dxuuu.xyz>

2024-12-12 18:24 UTC-0700 ~ Daniel Xu <dxu@dxuuu.xyz>
> 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 specific types by
> providing a optional root_id argument(s).
> 
> 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 <dxu@dxuuu.xyz>
> ---
>  .../bpf/bpftool/Documentation/bpftool-btf.rst |  8 +++-
>  tools/bpf/bpftool/btf.c                       | 39 ++++++++++++++++++-
>  2 files changed, 43 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/bpf/bpftool/Documentation/bpftool-btf.rst b/tools/bpf/bpftool/Documentation/bpftool-btf.rst
> index 245569f43035..dbe6d6d94e4c 100644
> --- a/tools/bpf/bpftool/Documentation/bpftool-btf.rst
> +++ b/tools/bpf/bpftool/Documentation/bpftool-btf.rst
> @@ -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,10 @@ 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.
> +    It can be passed multiple times to dump multiple types.
> +
>  bpftool btf help
>      Print short help message.
>  
> diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
> index 3e995faf9efa..2636655ac180 100644
> --- a/tools/bpf/bpftool/btf.c
> +++ b/tools/bpf/bpftool/btf.c
> @@ -27,6 +27,8 @@
>  #define KFUNC_DECL_TAG		"bpf_kfunc"
>  #define FASTCALL_DECL_TAG	"bpf_fastcall"
>  
> +#define MAX_ROOT_IDS		16
> +
>  static const char * const btf_kind_str[NR_BTF_KINDS] = {
>  	[BTF_KIND_UNKN]		= "UNKNOWN",
>  	[BTF_KIND_INT]		= "INT",
> @@ -880,7 +882,8 @@ static int do_dump(int argc, char **argv)
>  {
>  	bool dump_c = false, sort_dump_c = true;
>  	struct btf *btf = NULL, *base = NULL;
> -	__u32 root_type_ids[2];
> +	__u32 root_type_ids[MAX_ROOT_IDS];
> +	bool have_id_filtering;
>  	int root_type_cnt = 0;
>  	__u32 btf_id = -1;
>  	const char *src;
> @@ -974,6 +977,8 @@ static int do_dump(int argc, char **argv)
>  		goto done;
>  	}
>  
> +	have_id_filtering = !!root_type_cnt;
> +
>  	while (argc) {
>  		if (is_prefix(*argv, "format")) {
>  			NEXT_ARG();
> @@ -993,6 +998,36 @@ 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 (have_id_filtering) {
> +				p_err("cannot use root_id with other type filtering");
> +				err = -EINVAL;
> +				goto done;
> +			} else if (root_type_cnt == MAX_ROOT_IDS) {
> +				p_err("only %d root_id are supported", MAX_ROOT_IDS);


I doubt users will often reach this limit, but if they do, the message
can be confusing, because MAX_ROOT_IDS also accounts for root_type_ids[]
cells used when we pass map arguments ("key" or "value" or "kv"), so you
could pass 15 "root_id" on the command line and get a message telling
only 16 are supported.

Maybe add a counter to tell how many were defined from the rest of the
command line, and adjust the value in the error message?


> +				err = -E2BIG;
> +				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;
> +			}
> +			for (i = 0; i < root_type_cnt; i++) {
> +				if (root_type_ids[i] == root_id) {
> +					err = -EINVAL;
> +					p_err("duplicate root_id %d supplied", root_id);
> +					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 +1438,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"


  reply	other threads:[~2024-12-13 15:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-13  1:24 [PATCH bpf-next v4 0/4] bpftool: btf: Support dumping a single type " Daniel Xu
2024-12-13  1:24 ` [PATCH bpf-next v4 1/4] bpftool: man: Add missing format argument to command description Daniel Xu
2024-12-13 15:15   ` Quentin Monnet
2024-12-13  1:24 ` [PATCH bpf-next v4 2/4] bpftool: btf: Validate root_type_ids early Daniel Xu
2024-12-13 15:15   ` Quentin Monnet
2024-12-13  1:24 ` [PATCH bpf-next v4 3/4] bpftool: btf: Support dumping a specific types from file Daniel Xu
2024-12-13 15:17   ` Quentin Monnet [this message]
2024-12-13 16:45     ` Daniel Xu
2024-12-13 16:55       ` Quentin Monnet
2024-12-13 18:01         ` Daniel Xu
2024-12-13  1:24 ` [PATCH bpf-next v4 4/4] bpftool: bash: Add bash completion for root_id argument Daniel Xu
2024-12-13 15:17   ` Quentin Monnet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7fa902e5-0916-4bc9-b1e0-2729903d3de0@kernel.org \
    --to=qmo@kernel.org \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=antony@phenome.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dxu@dxuuu.xyz \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=toke@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®