Skip to contentSkip to conversation

bpftool: Add bpf_token show

PATCH Superseded Tao Chen 2 participants 7 messages Jul 23, 2025

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tao Chen

author
#0 lore raw

PATCH v3 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 | 226 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 229 insertions(+), 1 deletion(-)
 create mode 100644 tools/bpf/bpftool/token.c

Change list:
v2 -> v3:
Quentin suggested:
- patch1
- remove print when token not found.
- patch2
- refactor description message.
- patch3
- update commit message.
v2: https://lore.kernel.org/bpf/[email protected]

     https://lore.kernel.org/bpf/[email protected]

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 +226 -0

226 lines not on this page · raw message

Signature · 1 line
2.48.1
author
#1 lore raw

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

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

---
 .../bpftool/Documentation/bpftool-token.rst   | 64 +++++++++++++++++++
 1 file changed, 64 insertions(+)
 create mode 100644 tools/bpf/bpftool/Documentation/bpftool-token.rst
tools/bpf/bpftool/Documentation/bpftool-token.rst new file +64 -0
new file mode 100644
index 00000000000..d082c499cfe
@@ -0,0 +1,64 @@
+.. 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 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.
+
+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
author
#3 lore raw

PATCH v3 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 token
help  list  show
---
 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 v3 1/3
#4 lore raw
In reply to Tao Chen

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

Quoted 38 lines

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"]
}]

---

[...]

Quoted 5 lines
tools/bpf/bpftool/token.c new file +0 -0
new file mode 100644
index 00000000000..06b56ea40b8

[...]

tools/bpf/bpftool/token.c line 209

Quoted 10 lines
tools/bpf/bpftool/token.c new file +10 -0
+static int do_help(int argc, char **argv)
+{
+	if (json_output) {
+		jsonw_null(json_wtr);
+		return 0;
+	}
+
+	fprintf(stderr,
+		"Usage: %1$s %2$s { show | list }\n"
+		"	%1$s %2$s help\n"

One more nit: applying and testing the help message locally, I noticed that the alignement is not correct:

    $ ./bpftool token help
    Usage: bpftool token { show | list }
            bpftool token help

The two "bpftool" should be aligned. This is because you use a tab for indent on the "help" line. Can you please replace it with spaces to fix indentation, and remain consistent with what other files do?

After that change, you can add:

maintainer
#5 lore raw

2025-07-23 11:40 UTC+0100 ~ Quentin Monnet <[email protected]>

Quoted 69 lines

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

Quoted 38 lines

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"]
}]

---

[...]

Quoted 5 lines
tools/bpf/bpftool/token.c new file +0 -0
new file mode 100644
index 00000000000..06b56ea40b8

[...]

tools/bpf/bpftool/token.c line 209

Quoted 10 lines
tools/bpf/bpftool/token.c new file +10 -0
+static int do_help(int argc, char **argv)
+{
+	if (json_output) {
+		jsonw_null(json_wtr);
+		return 0;
+	}
+
+	fprintf(stderr,
+		"Usage: %1$s %2$s { show | list }\n"
+		"	%1$s %2$s help\n"

One more nit: applying and testing the help message locally, I noticed that the alignement is not correct:

    $ ./bpftool token help
    Usage: bpftool token { show | list }
            bpftool token help
Quoted 8 lines

The two "bpftool" should be aligned. This is because you use a tab for indent on the "help" line. Can you please replace it with spaces to fix indentation, and remain consistent with what other files do?

After that change, you can add:

Please also take a look at the "CHECK" reports from checkpatch, some of them are worth addressing:

https://netdev.bots.linux.dev/static/nipa/984952/14165722/checkpatch/stdout

(accessed via
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/)

Thanks,
Quentin

author
#6 lore raw
In reply to Quentin Monnet
Quoted 65 lines · Quentin Monnet

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

Quoted 38 lines

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"]
}]

---

[...]

Quoted 5 lines
tools/bpf/bpftool/token.c new file +0 -0
new file mode 100644
index 00000000000..06b56ea40b8

[...]

tools/bpf/bpftool/token.c line 209

Quoted 10 lines
tools/bpf/bpftool/token.c new file +10 -0
+static int do_help(int argc, char **argv)
+{
+	if (json_output) {
+		jsonw_null(json_wtr);
+		return 0;
+	}
+
+	fprintf(stderr,
+		"Usage: %1$s %2$s { show | list }\n"
+		"	%1$s %2$s help\n"

One more nit: applying and testing the help message locally, I noticed that the alignement is not correct:

Quoted 8 lines
     $ ./bpftool token help
     Usage: bpftool token { show | list }
             bpftool token help

The two "bpftool" should be aligned. This is because you use a tab for indent on the "help" line. Can you please replace it with spaces to fix indentation, and remain consistent with what other files do?

Thanks for your testing, i will fix it, thanks again.

Quoted 3 lines

After that change, you can add:

Signature · 2 lines
Best Regards
Tao Chen