Skip to contentSkip to conversation

bpftool: Add bpf_token show

PATCH RESEND Superseded Tao Chen 2 participants 8 messages Jul 22, 2025

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tao Chen

author
#0 lore raw

PATCH RESEND v2 1/3 bpftool: Add bpf_token show

Add bpftool token show command to get token info from bpffs in /proc/mounts.

Example plain output for token show:
token_info /sys/fs/bpf/token
allowed_cmds:
map_create prog_load
allowed_maps:
allowed_progs:
kprobe
allowed_attachs:
xdp
token_info /sys/fs/bpf/token2
allowed_cmds:
map_create prog_load
allowed_maps:
allowed_progs:
kprobe
allowed_attachs:
xdp

Example json output for token show:

[{
	"token_info": "/sys/fs/bpf/token",
	"allowed_cmds": ["map_create", "prog_load"],
	"allowed_maps": [],
	"allowed_progs": ["kprobe"],
	"allowed_attachs": ["xdp"]
}, {
	"token_info": "/sys/fs/bpf/token2",
	"allowed_cmds": ["map_create", "prog_load"],
	"allowed_maps": [],
	"allowed_progs": ["kprobe"],
	"allowed_attachs": ["xdp"]
}]
---
 tools/bpf/bpftool/main.c  |   3 +-
 tools/bpf/bpftool/main.h  |   1 +
 tools/bpf/bpftool/token.c | 232 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 235 insertions(+), 1 deletion(-)
 create mode 100644 tools/bpf/bpftool/token.c

Change list:
v1 -> v2:
Quentin suggested:
- patch1
- remove zclose macro.
- rename __json_array_str to split_json_array_str
- print empty array when value is null for json format.
- show all tokens info and format plain output for readable.
- add info when token not found.
- add copyright in token.c
- patch2
- update 'eBPF progs' to 'eBPF tokens'.
- update description.
- patch3
- add bash-completion.
v1: https://lore.kernel.org/bpf/[email protected]

tools/bpf/bpftool/main.c +2 -1
index 2b7f2bd3a7d..0f1183b2ed0 100644
@@ -61,7 +61,7 @@ static int do_help(int argc, char **argv)
 		"       %s batch file FILE\n"
 		"       %s version\n"
 		"\n"
-		"       OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
+		"       OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter | token }\n"
 		"       " HELP_SPEC_OPTIONS " |\n"
 		"                    {-V|--version} }\n"
 		"",
@@ -87,6 +87,7 @@ static const struct cmd commands[] = {
 	{ "gen",	do_gen },
 	{ "struct_ops",	do_struct_ops },
 	{ "iter",	do_iter },
+	{ "token",	do_token },
 	{ "version",	do_version },
 	{ 0 }
 };
tools/bpf/bpftool/main.h +1 -0
index 6db704fda5c..a2bb0714b3d 100644
@@ -166,6 +166,7 @@ int do_tracelog(int argc, char **arg) __weak;
 int do_feature(int argc, char **argv) __weak;
 int do_struct_ops(int argc, char **argv) __weak;
 int do_iter(int argc, char **argv) __weak;
+int do_token(int argc, char **argv) __weak;
 
 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
 int prog_parse_fd(int *argc, char ***argv);
tools/bpf/bpftool/token.c new file +232 -0

232 lines not on this page · raw message

Signature · 1 line
2.48.1
author
#1 lore raw

PATCH v2 2/3 bpftool: Add bpftool-token manpage

Add bpftool-token manpage with information and examples of token-related commands.

---
 .../bpftool/Documentation/bpftool-token.rst   | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 tools/bpf/bpftool/Documentation/bpftool-token.rst
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +63 -0
new file mode 100644
index 00000000000..c5fe9292258
@@ -0,0 +1,63 @@
+.. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+================
+bpftool-token
+================
+-------------------------------------------------------------------------------
+tool for inspection and simple manipulation of eBPF tokens
+-------------------------------------------------------------------------------
+
+:Manual section: 8
+
+.. include:: substitutions.rst
+
+SYNOPSIS
+========
+
+**bpftool** [*OPTIONS*] **token** *COMMAND*
+
+*OPTIONS* := { |COMMON_OPTIONS| }
+
+*COMMANDS* := { **show** | **list** | **help** }
+
+TOKEN COMMANDS
+===============
+
+| **bpftool** **token** { **show** | **list** }
+| **bpftool** **token help**
+|
+
+DESCRIPTION
+===========
+bpftool token { show | list }
+    List all the speciafic allowed types for **bpf**\ () system call
+    commands, maps, programs, and attach types, as well as the
+    *bpffs* mount point used to set the token information.
+
+bpftool prog help
+    Print short help message.
+
+OPTIONS
+========
+.. include:: common_options.rst
+
+EXAMPLES
+========
+|
+| **# mkdir -p /sys/fs/bpf/token**
+| **# mount -t bpf bpffs /sys/fs/bpf/token** \
+|         **-o delegate_cmds=prog_load:map_create** \
+|         **-o delegate_progs=kprobe** \
+|         **-o delegate_attachs=xdp**
+| **# bpftool token list**
+
+::
+
+    token_info  /sys/fs/bpf/token
+            allowed_cmds:
+              map_create          prog_load
+            allowed_maps:
+            allowed_progs:
+              kprobe
+            allowed_attachs:
+              xdp
Signature · 1 line
2.48.1
maintainer
#2 lore raw

Re: [PATCH bpf-next v2 2/3] bpftool: Add bpftool-token manpage

2025-07-22 20:09 UTC+0800 ~ Tao Chen <[email protected]>

Quoted 38 lines

Add bpftool-token manpage with information and examples of token-related commands.

---
 .../bpftool/Documentation/bpftool-token.rst   | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 tools/bpf/bpftool/Documentation/bpftool-token.rst
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +23 -0
new file mode 100644
index 00000000000..c5fe9292258
@@ -0,0 +1,63 @@
+.. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+================
+bpftool-token
+================
+-------------------------------------------------------------------------------
+tool for inspection and simple manipulation of eBPF tokens
+-------------------------------------------------------------------------------
+
+:Manual section: 8
+
+.. include:: substitutions.rst
+
+SYNOPSIS
+========
+
+**bpftool** [*OPTIONS*] **token** *COMMAND*
+
+*OPTIONS* := { |COMMON_OPTIONS| }
+
+*COMMANDS* := { **show** | **list** | **help** }
+
+TOKEN COMMANDS

tools/bpf/bpftool/Documentation/bpftool-token.rst line 33

Quoted 10 lines
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +10 -0
+===============
+
+| **bpftool** **token** { **show** | **list** }
+| **bpftool** **token help**
+|
+
+DESCRIPTION
+===========
+bpftool token { show | list }
+    List all the speciafic allowed types for **bpf**\ () system call

Typo: "speciafic".

tools/bpf/bpftool/Documentation/bpftool-token.rst line 35

Quoted 2 lines
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +2 -0
+    commands, maps, programs, and attach types, as well as the
+    *bpffs* mount point used to set the token information.

This sentence needs to be adjusted now that you can print info for several mountpoints.

How about:

List BPF token information for each *bpffs* mount point containing token information on the system. Information include mount point path, allowed **bpf**\ () system call commands, maps, programs, and attach types for the token.

Quoted 28 lines
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +28 -0
+
+bpftool prog help
+    Print short help message.
+
+OPTIONS
+========
+.. include:: common_options.rst
+
+EXAMPLES
+========
+|
+| **# mkdir -p /sys/fs/bpf/token**
+| **# mount -t bpf bpffs /sys/fs/bpf/token** \
+|         **-o delegate_cmds=prog_load:map_create** \
+|         **-o delegate_progs=kprobe** \
+|         **-o delegate_attachs=xdp**
+| **# bpftool token list**
+
+::
+
+    token_info  /sys/fs/bpf/token
+            allowed_cmds:
+              map_create          prog_load
+            allowed_maps:
+            allowed_progs:
+              kprobe
+            allowed_attachs:
+              xdp
author
#3 lore raw

Re: [PATCH bpf-next v2 2/3] bpftool: Add bpftool-token manpage

Quoted 44 lines · Quentin Monnet

2025-07-22 20:09 UTC+0800 ~ Tao Chen <[email protected]>

Quoted 43 lines

Add bpftool-token manpage with information and examples of token-related commands.

---
  .../bpftool/Documentation/bpftool-token.rst   | 63 +++++++++++++++++++
  1 file changed, 63 insertions(+)

create mode 100644 tools/bpf/bpftool/Documentation/bpftool-token.rst

tools/bpf/bpftool/Documentation/bpftool-token.rst new file +28 -0
new file mode 100644
index 00000000000..c5fe9292258
@@ -0,0 +1,63 @@
+.. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+================
+bpftool-token
+================
+-------------------------------------------------------------------------------
+tool for inspection and simple manipulation of eBPF tokens
+-------------------------------------------------------------------------------
+
+:Manual section: 8
+
+.. include:: substitutions.rst
+
+SYNOPSIS
+========
+
+**bpftool** [*OPTIONS*] **token** *COMMAND*
+
+*OPTIONS* := { |COMMON_OPTIONS| }
+
+*COMMANDS* := { **show** | **list** | **help** }
+
+TOKEN COMMANDS
+===============
+
+| **bpftool** **token** { **show** | **list** }
+| **bpftool** **token help**
+|
Quoted 9 lines

tools/bpf/bpftool/Documentation/bpftool-token.rst line 33

Quoted 5 lines
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +5 -0
+
+DESCRIPTION
+===========
+bpftool token { show | list }
+    List all the speciafic allowed types for **bpf**\ () system call

Typo: "speciafic".

will fix it, thanks.

Quoted 8 lines

tools/bpf/bpftool/Documentation/bpftool-token.rst line 35

Quoted 3 lines
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +2 -0
+    commands, maps, programs, and attach types, as well as the
+    *bpffs* mount point used to set the token information.

This sentence needs to be adjusted now that you can print info for several mountpoints.

Quoted 7 lines

How about:

List BPF token information for each *bpffs* mount point containing token information on the system. Information include mount point path, allowed **bpf**\ () system call commands, maps, programs, and attach types for the token.

This really looks better, thank you for your help.

Quoted 30 lines
Quoted 29 lines
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +28 -0
+
+bpftool prog help
+    Print short help message.
+
+OPTIONS
+========
+.. include:: common_options.rst
+
+EXAMPLES
+========
+|
+| **# mkdir -p /sys/fs/bpf/token**
+| **# mount -t bpf bpffs /sys/fs/bpf/token** \
+|         **-o delegate_cmds=prog_load:map_create** \
+|         **-o delegate_progs=kprobe** \
+|         **-o delegate_attachs=xdp**
+| **# bpftool token list**
+
+::
+
+    token_info  /sys/fs/bpf/token
+            allowed_cmds:
+              map_create          prog_load
+            allowed_maps:
+            allowed_progs:
+              kprobe
+            allowed_attachs:
+              xdp
Signature · 2 lines
Best Regards
Tao Chen
author
#4 lore raw

PATCH v2 3/3 bpftool: Add bash completion for token argument

In reply to Tao Chen

This commit updates the bash completion script with the new token argument.

$ bpftool
batch       cgroup      gen         iter        map         perf        struct_ops
btf         feature     help        link        net         prog        token
---
 tools/bpf/bpftool/bash-completion/bpftool | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/bpf/bpftool/bash-completion/bpftool +11 -0
index a759ba24471..527bb47ac46 100644
@@ -1215,6 +1215,17 @@ _bpftool()
                     ;;
             esac
             ;;
+        token)
+            case $command in
+               show|list)
+                   return 0
+                   ;;
+               *)
+                   [[ $prev == $object ]] && \
+                       COMPREPLY=( $( compgen -W 'help show list' -- "$cur" ) )
+                   ;;
+            esac
+            ;;
     esac
 } &&
 complete -F _bpftool bpftool
Signature · 1 line
2.48.1
maintainer Reviewed-by on v2 3/3
#5 lore raw

Re: [PATCH bpf-next v2 3/3] bpftool: Add bash completion for token argument

2025-07-22 20:09 UTC+0800 ~ Tao Chen <[email protected]>

Quoted 5 lines

This commit updates the bash completion script with the new token argument.

$ bpftool
batch       cgroup      gen         iter        map         perf        struct_ops
btf         feature     help        link        net         prog        token

This is a terrible example, offering "token" as completion for just "bpftool [tab]" works without this patch :) The main commands are parsed from the output of "bpftool help" so it should work after your first patch. In this one, we add "list", "show" and "help" for completing "bpftool token [tab]".

Quoted 18 lines
---
 tools/bpf/bpftool/bash-completion/bpftool | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/bpf/bpftool/bash-completion/bpftool +4 -0
index a759ba24471..527bb47ac46 100644
@@ -1215,6 +1215,17 @@ _bpftool()
                     ;;
             esac
             ;;
+        token)
+            case $command in
+               show|list)
+                   return 0

tools/bpf/bpftool/bash-completion/bpftool _bpftool() · line 1231

Quoted 10 lines
tools/bpf/bpftool/bash-completion/bpftool +7 -0
+                   ;;
+               *)
+                   [[ $prev == $object ]] && \
+                       COMPREPLY=( $( compgen -W 'help show list' -- "$cur" ) )
+                   ;;
+            esac
+            ;;
     esac
 } &&
 complete -F _bpftool bpftool

Other than the example in the description, this looks good.

Thanks

author
#6 lore raw

Re: [PATCH bpf-next v2 3/3] bpftool: Add bash completion for token argument

Quoted 8 lines · Quentin Monnet

2025-07-22 20:09 UTC+0800 ~ Tao Chen <[email protected]>

Quoted 5 lines

This commit updates the bash completion script with the new token argument.

$ bpftool
batch       cgroup      gen         iter        map         perf        struct_ops
btf         feature     help        link        net         prog        token
Quoted 6 lines

This is a terrible example, offering "token" as completion for just "bpftool [tab]" works without this patch :) The main commands are parsed from the output of "bpftool help" so it should work after your first patch. In this one, we add "list", "show" and "help" for completing "bpftool token [tab]".

As you said, how about this one? I will change it in v3, thanks.

    $ bpftool token
    help  list  show
Quoted 36 lines
Quoted 19 lines
---
  tools/bpf/bpftool/bash-completion/bpftool | 11 +++++++++++
  1 file changed, 11 insertions(+)
tools/bpf/bpftool/bash-completion/bpftool +4 -0
index a759ba24471..527bb47ac46 100644
@@ -1215,6 +1215,17 @@ _bpftool()
                      ;;
              esac
              ;;
+        token)
+            case $command in
+               show|list)
+                   return 0

tools/bpf/bpftool/bash-completion/bpftool _bpftool() · line 1231

Quoted 10 lines
tools/bpf/bpftool/bash-completion/bpftool +7 -0
+                   ;;
+               *)
+                   [[ $prev == $object ]] && \
+                       COMPREPLY=( $( compgen -W 'help show list' -- "$cur" ) )
+                   ;;
+            esac
+            ;;
      esac
  } &&
  complete -F _bpftool bpftool

Other than the example in the description, this looks good.

Thanks

Signature · 2 lines
Best Regards
Tao Chen
maintainer
#7 lore raw

Re: [PATCH bpf-next v2 3/3] bpftool: Add bash completion for token argument

2025-07-23 00:35 UTC+0800 ~ Tao Chen <[email protected]>

Quoted 18 lines · Quentin Monnet
Quoted 10 lines

2025-07-22 20:09 UTC+0800 ~ Tao Chen <[email protected]>

Quoted 7 lines

This commit updates the bash completion script with the new token argument.

$ bpftool
batch       cgroup      gen         iter        map        
perf        struct_ops
btf         feature     help        link        net        
prog        token
Quoted 6 lines

This is a terrible example, offering "token" as completion for just "bpftool [tab]" works without this patch :) The main commands are parsed from the output of "bpftool help" so it should work after your first patch. In this one, we add "list", "show" and "help" for completing "bpftool token [tab]".

Quoted 3 lines

As you said, how about this one? I will change it in v3, thanks.

    $ bpftool token
    help  list  show

Yes, perfect