All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2] bpftool: Add command to list BPF types, helpers
@ 2022-06-29 14:40 Quentin Monnet
  2022-06-29 14:40 ` [PATCH bpf-next 1/2] bpftool: Add feature list (prog/map/link/attach types, helpers) Quentin Monnet
  2022-06-29 14:40 ` [PATCH bpf-next 2/2] bpftool: Use feature list in bash completion Quentin Monnet
  0 siblings, 2 replies; 6+ messages in thread
From: Quentin Monnet @ 2022-06-29 14:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, bpf, netdev, Quentin Monnet

Now that bpftool relies on libbpf to get a "standard" textual
representation for program, map, link, and attach types, we can make it
list all these types (plus BPF helpers) that it knows from compilation
time.

The first use case for this feature is to help with bash completion. It
also provides a simple way for scripts to iterate over existing BPF types,
using the canonical names known to libbpf.

The first patch adds a new subcommand "bpftool feature list" to do this,
and the second one updates the bash completion to drop the hardcoded lists
of map types or cgroup attach types.

Quentin Monnet (2):
  bpftool: Add feature list (prog/map/link/attach types, helpers)
  bpftool: Use feature list in bash completion

 .../bpftool/Documentation/bpftool-feature.rst | 12 ++++
 tools/bpf/bpftool/bash-completion/bpftool     | 28 ++++------
 tools/bpf/bpftool/feature.c                   | 55 +++++++++++++++++++
 .../selftests/bpf/test_bpftool_synctypes.py   | 20 +------
 4 files changed, 80 insertions(+), 35 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 1/2] bpftool: Add feature list (prog/map/link/attach types, helpers)
  2022-06-29 14:40 [PATCH bpf-next 0/2] bpftool: Add command to list BPF types, helpers Quentin Monnet
@ 2022-06-29 14:40 ` Quentin Monnet
  2022-06-29 17:32   ` Daniel Müller
  2022-06-29 14:40 ` [PATCH bpf-next 2/2] bpftool: Use feature list in bash completion Quentin Monnet
  1 sibling, 1 reply; 6+ messages in thread
From: Quentin Monnet @ 2022-06-29 14:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, bpf, netdev, Quentin Monnet

Add a "bpftool feature list" subcommand to list BPF "features".
Contrarily to "bpftool feature probe", this is not about the features
available on the system. Instead, it lists all features known to bpftool
from compilation time; in other words, all program, map, attach, link
types known to the libbpf version in use, and all helpers found in the
UAPI BPF header.

The first use case for this feature is bash completion: running the
command provides a list of types that can be used to produce the list of
candidate map types, for example.

Now that bpftool uses "standard" names provided by libbpf for the
program, map, link, and attach types, having the ability to list these
types and helpers could also be useful in scripts to loop over existing
items.

Sample output:

    # bpftool feature list prog_types | grep -vw unspec | head -n 6
    socket_filter
    kprobe
    sched_cls
    sched_act
    tracepoint
    xdp

    # bpftool -p feature list map_types | jq '.[1]'
    "hash"

    # bpftool feature list attach_types | grep '^cgroup_'
    cgroup_inet_ingress
    cgroup_inet_egress
    [...]
    cgroup_inet_sock_release

    # bpftool feature list helpers | grep -vw bpf_unspec | wc -l
    207

The "unspec" types and helpers are not filtered out by bpftool, so as to
remain closer to the enums, and to preserve the indices in the JSON
arrays (e.g. "hash" at index 1 == BPF_MAP_TYPE_HASH in map types list).

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
---
 .../bpftool/Documentation/bpftool-feature.rst | 12 ++++
 tools/bpf/bpftool/bash-completion/bpftool     |  7 ++-
 tools/bpf/bpftool/feature.c                   | 55 +++++++++++++++++++
 3 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/Documentation/bpftool-feature.rst b/tools/bpf/bpftool/Documentation/bpftool-feature.rst
index 4ce9a77bc1e0..4bf1724d0e8c 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-feature.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-feature.rst
@@ -24,9 +24,11 @@ FEATURE COMMANDS
 ================
 
 |	**bpftool** **feature probe** [*COMPONENT*] [**full**] [**unprivileged**] [**macros** [**prefix** *PREFIX*]]
+|	**bpftool** **feature list** *GROUP*
 |	**bpftool** **feature help**
 |
 |	*COMPONENT* := { **kernel** | **dev** *NAME* }
+|	*GROUP* := { **prog_types** | **map_types** | **attach_types** | **helpers** }
 
 DESCRIPTION
 ===========
@@ -70,6 +72,16 @@ DESCRIPTION
 		  The keywords **full**, **macros** and **prefix** have the
 		  same role as when probing the kernel.
 
+	**bpftool feature list** *GROUP*
+		  List items known to bpftool. These can be BPF program types
+		  (**prog_types**), BPF map types (**map_types**), attach types
+		  (**attach_types**), link types (**link_types**), or BPF helper
+		  functions (**helpers**). The command does not probe the system, but
+		  simply lists the elements that bpftool knows from compilation time,
+		  as provided from libbpf (for all object types) or from the BPF UAPI
+		  header (list of helpers). This can be used in scripts to iterate over
+		  BPF types or helpers.
+
 	**bpftool feature help**
 		  Print short help message.
 
diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index 91f89a9a5b36..9cef6516320b 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool
@@ -1175,9 +1175,14 @@ _bpftool()
                     _bpftool_once_attr 'full unprivileged'
                     return 0
                     ;;
+                list)
+                    [[ $prev != "$command" ]] && return 0
+                    COMPREPLY=( $( compgen -W 'prog_types map_types \
+                        attach_types link_types helpers' -- "$cur" ) )
+                    ;;
                 *)
                     [[ $prev == $object ]] && \
-                        COMPREPLY=( $( compgen -W 'help probe' -- "$cur" ) )
+                        COMPREPLY=( $( compgen -W 'help list probe' -- "$cur" ) )
                     ;;
             esac
             ;;
diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c
index bac4ef428a02..576cc6b90c6a 100644
--- a/tools/bpf/bpftool/feature.c
+++ b/tools/bpf/bpftool/feature.c
@@ -1258,6 +1258,58 @@ static int do_probe(int argc, char **argv)
 	return 0;
 }
 
+static const char *get_helper_name(unsigned int id)
+{
+	if (id >= ARRAY_SIZE(helper_name))
+		return NULL;
+
+	return helper_name[id];
+}
+
+static int do_list(int argc, char **argv)
+{
+	const char *(*get_name)(unsigned int id);
+	unsigned int id = 0;
+
+	if (argc < 1)
+		usage();
+
+	if (is_prefix(*argv, "prog_types")) {
+		get_name = (const char *(*)(unsigned int))libbpf_bpf_prog_type_str;
+	} else if (is_prefix(*argv, "map_types")) {
+		get_name = (const char *(*)(unsigned int))libbpf_bpf_map_type_str;
+	} else if (is_prefix(*argv, "attach_types")) {
+		get_name = (const char *(*)(unsigned int))libbpf_bpf_attach_type_str;
+	} else if (is_prefix(*argv, "link_types")) {
+		get_name = (const char *(*)(unsigned int))libbpf_bpf_link_type_str;
+	} else if (is_prefix(*argv, "helpers")) {
+		get_name = get_helper_name;
+	} else {
+		p_err("expected 'prog_types', 'map_types', 'attach_types', 'link_types' or 'helpers', got: %s", *argv);
+		return -1;
+	}
+
+	if (json_output)
+		jsonw_start_array(json_wtr);	/* root array */
+
+	while (true) {
+		const char *name;
+
+		name = get_name(id++);
+		if (!name)
+			break;
+		if (json_output)
+			jsonw_string(json_wtr, name);
+		else
+			printf("%s\n", name);
+	}
+
+	if (json_output)
+		jsonw_end_array(json_wtr);	/* root array */
+
+	return 0;
+}
+
 static int do_help(int argc, char **argv)
 {
 	if (json_output) {
@@ -1267,9 +1319,11 @@ static int do_help(int argc, char **argv)
 
 	fprintf(stderr,
 		"Usage: %1$s %2$s probe [COMPONENT] [full] [unprivileged] [macros [prefix PREFIX]]\n"
+		"       %1$s %2$s list GROUP\n"
 		"       %1$s %2$s help\n"
 		"\n"
 		"       COMPONENT := { kernel | dev NAME }\n"
+		"       GROUP := { prog_types | map_types | attach_types | link_types | helpers }\n"
 		"       " HELP_SPEC_OPTIONS " }\n"
 		"",
 		bin_name, argv[-2]);
@@ -1279,6 +1333,7 @@ static int do_help(int argc, char **argv)
 
 static const struct cmd cmds[] = {
 	{ "probe",	do_probe },
+	{ "list",	do_list },
 	{ "help",	do_help },
 	{ 0 }
 };
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 2/2] bpftool: Use feature list in bash completion
  2022-06-29 14:40 [PATCH bpf-next 0/2] bpftool: Add command to list BPF types, helpers Quentin Monnet
  2022-06-29 14:40 ` [PATCH bpf-next 1/2] bpftool: Add feature list (prog/map/link/attach types, helpers) Quentin Monnet
@ 2022-06-29 14:40 ` Quentin Monnet
  2022-06-29 16:56   ` Daniel Müller
  1 sibling, 1 reply; 6+ messages in thread
From: Quentin Monnet @ 2022-06-29 14:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, bpf, netdev, Quentin Monnet

Now that bpftool is able to produce a list of known program, map, attach
types, let's use as much of this as we can in the bash completion file,
so that we don't have to expand the list each time a new type is added
to the kernel.

Also update the relevant test script to remove some checks that are no
longer needed.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
---
 tools/bpf/bpftool/bash-completion/bpftool     | 21 ++++---------------
 .../selftests/bpf/test_bpftool_synctypes.py   | 20 +++---------------
 2 files changed, 7 insertions(+), 34 deletions(-)

diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index 9cef6516320b..ee177f83b179 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool
@@ -703,15 +703,8 @@ _bpftool()
                             return 0
                             ;;
                         type)
-                            local BPFTOOL_MAP_CREATE_TYPES='hash array \
-                                prog_array perf_event_array percpu_hash \
-                                percpu_array stack_trace cgroup_array lru_hash \
-                                lru_percpu_hash lpm_trie array_of_maps \
-                                hash_of_maps devmap devmap_hash sockmap cpumap \
-                                xskmap sockhash cgroup_storage reuseport_sockarray \
-                                percpu_cgroup_storage queue stack sk_storage \
-                                struct_ops ringbuf inode_storage task_storage \
-                                bloom_filter'
+                            local BPFTOOL_MAP_CREATE_TYPES="$(bpftool feature list map_types | \
+                                grep -v '^unspec$')"
                             COMPREPLY=( $( compgen -W "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) )
                             return 0
                             ;;
@@ -1039,14 +1032,8 @@ _bpftool()
                     return 0
                     ;;
                 attach|detach)
-                    local BPFTOOL_CGROUP_ATTACH_TYPES='cgroup_inet_ingress cgroup_inet_egress \
-                        cgroup_inet_sock_create cgroup_sock_ops cgroup_device cgroup_inet4_bind \
-                        cgroup_inet6_bind cgroup_inet4_post_bind cgroup_inet6_post_bind \
-                        cgroup_inet4_connect cgroup_inet6_connect cgroup_inet4_getpeername \
-                        cgroup_inet6_getpeername cgroup_inet4_getsockname cgroup_inet6_getsockname \
-                        cgroup_udp4_sendmsg cgroup_udp6_sendmsg cgroup_udp4_recvmsg \
-                        cgroup_udp6_recvmsg cgroup_sysctl cgroup_getsockopt cgroup_setsockopt \
-                        cgroup_inet_sock_release'
+                    local BPFTOOL_CGROUP_ATTACH_TYPES="$(bpftool feature list attach_types | \
+                        grep '^cgroup_')"
                     local ATTACH_FLAGS='multi override'
                     local PROG_TYPE='id pinned tag name'
                     # Check for $prev = $command first
diff --git a/tools/testing/selftests/bpf/test_bpftool_synctypes.py b/tools/testing/selftests/bpf/test_bpftool_synctypes.py
index e443e6542cb9..a6410bebe603 100755
--- a/tools/testing/selftests/bpf/test_bpftool_synctypes.py
+++ b/tools/testing/selftests/bpf/test_bpftool_synctypes.py
@@ -471,12 +471,6 @@ class BashcompExtractor(FileExtractor):
     def get_prog_attach_types(self):
         return self.get_bashcomp_list('BPFTOOL_PROG_ATTACH_TYPES')
 
-    def get_map_types(self):
-        return self.get_bashcomp_list('BPFTOOL_MAP_CREATE_TYPES')
-
-    def get_cgroup_attach_types(self):
-        return self.get_bashcomp_list('BPFTOOL_CGROUP_ATTACH_TYPES')
-
 def verify(first_set, second_set, message):
     """
     Print all values that differ between two sets.
@@ -516,17 +510,12 @@ def main():
     man_map_types = man_map_info.get_map_types()
     man_map_info.close()
 
-    bashcomp_info = BashcompExtractor()
-    bashcomp_map_types = bashcomp_info.get_map_types()
-
     verify(source_map_types, help_map_types,
             f'Comparing {BpfHeaderExtractor.filename} (bpf_map_type) and {MapFileExtractor.filename} (do_help() TYPE):')
     verify(source_map_types, man_map_types,
             f'Comparing {BpfHeaderExtractor.filename} (bpf_map_type) and {ManMapExtractor.filename} (TYPE):')
     verify(help_map_options, man_map_options,
             f'Comparing {MapFileExtractor.filename} (do_help() OPTIONS) and {ManMapExtractor.filename} (OPTIONS):')
-    verify(source_map_types, bashcomp_map_types,
-            f'Comparing {BpfHeaderExtractor.filename} (bpf_map_type) and {BashcompExtractor.filename} (BPFTOOL_MAP_CREATE_TYPES):')
 
     # Attach types (names)
 
@@ -542,8 +531,10 @@ def main():
     man_prog_attach_types = man_prog_info.get_attach_types()
     man_prog_info.close()
 
-    bashcomp_info.reset_read() # We stopped at map types, rewind
+
+    bashcomp_info = BashcompExtractor()
     bashcomp_prog_attach_types = bashcomp_info.get_prog_attach_types()
+    bashcomp_info.close()
 
     verify(source_prog_attach_types, help_prog_attach_types,
             f'Comparing {ProgFileExtractor.filename} (bpf_attach_type) and {ProgFileExtractor.filename} (do_help() ATTACH_TYPE):')
@@ -568,17 +559,12 @@ def main():
     man_cgroup_attach_types = man_cgroup_info.get_attach_types()
     man_cgroup_info.close()
 
-    bashcomp_cgroup_attach_types = bashcomp_info.get_cgroup_attach_types()
-    bashcomp_info.close()
-
     verify(source_cgroup_attach_types, help_cgroup_attach_types,
             f'Comparing {BpfHeaderExtractor.filename} (bpf_attach_type) and {CgroupFileExtractor.filename} (do_help() ATTACH_TYPE):')
     verify(source_cgroup_attach_types, man_cgroup_attach_types,
             f'Comparing {BpfHeaderExtractor.filename} (bpf_attach_type) and {ManCgroupExtractor.filename} (ATTACH_TYPE):')
     verify(help_cgroup_options, man_cgroup_options,
             f'Comparing {CgroupFileExtractor.filename} (do_help() OPTIONS) and {ManCgroupExtractor.filename} (OPTIONS):')
-    verify(source_cgroup_attach_types, bashcomp_cgroup_attach_types,
-            f'Comparing {BpfHeaderExtractor.filename} (bpf_attach_type) and {BashcompExtractor.filename} (BPFTOOL_CGROUP_ATTACH_TYPES):')
 
     # Options for remaining commands
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 2/2] bpftool: Use feature list in bash completion
  2022-06-29 14:40 ` [PATCH bpf-next 2/2] bpftool: Use feature list in bash completion Quentin Monnet
@ 2022-06-29 16:56   ` Daniel Müller
  0 siblings, 0 replies; 6+ messages in thread
From: Daniel Müller @ 2022-06-29 16:56 UTC (permalink / raw)
  To: Quentin Monnet
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, bpf, netdev

On Wed, Jun 29, 2022 at 03:40:19PM +0100, Quentin Monnet wrote:
> Now that bpftool is able to produce a list of known program, map, attach
> types, let's use as much of this as we can in the bash completion file,
> so that we don't have to expand the list each time a new type is added
> to the kernel.
> 
> Also update the relevant test script to remove some checks that are no
> longer needed.
> 
> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> ---
>  tools/bpf/bpftool/bash-completion/bpftool     | 21 ++++---------------
>  .../selftests/bpf/test_bpftool_synctypes.py   | 20 +++---------------
>  2 files changed, 7 insertions(+), 34 deletions(-)
> 
> diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
> index 9cef6516320b..ee177f83b179 100644
> --- a/tools/bpf/bpftool/bash-completion/bpftool
> +++ b/tools/bpf/bpftool/bash-completion/bpftool
> @@ -703,15 +703,8 @@ _bpftool()
>                              return 0
>                              ;;
>                          type)
> -                            local BPFTOOL_MAP_CREATE_TYPES='hash array \
> -                                prog_array perf_event_array percpu_hash \
> -                                percpu_array stack_trace cgroup_array lru_hash \
> -                                lru_percpu_hash lpm_trie array_of_maps \
> -                                hash_of_maps devmap devmap_hash sockmap cpumap \
> -                                xskmap sockhash cgroup_storage reuseport_sockarray \
> -                                percpu_cgroup_storage queue stack sk_storage \
> -                                struct_ops ringbuf inode_storage task_storage \
> -                                bloom_filter'
> +                            local BPFTOOL_MAP_CREATE_TYPES="$(bpftool feature list map_types | \
> +                                grep -v '^unspec$')"
>                              COMPREPLY=( $( compgen -W "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) )
>                              return 0
>                              ;;
> @@ -1039,14 +1032,8 @@ _bpftool()
>                      return 0
>                      ;;
>                  attach|detach)
> -                    local BPFTOOL_CGROUP_ATTACH_TYPES='cgroup_inet_ingress cgroup_inet_egress \
> -                        cgroup_inet_sock_create cgroup_sock_ops cgroup_device cgroup_inet4_bind \
> -                        cgroup_inet6_bind cgroup_inet4_post_bind cgroup_inet6_post_bind \
> -                        cgroup_inet4_connect cgroup_inet6_connect cgroup_inet4_getpeername \
> -                        cgroup_inet6_getpeername cgroup_inet4_getsockname cgroup_inet6_getsockname \
> -                        cgroup_udp4_sendmsg cgroup_udp6_sendmsg cgroup_udp4_recvmsg \
> -                        cgroup_udp6_recvmsg cgroup_sysctl cgroup_getsockopt cgroup_setsockopt \
> -                        cgroup_inet_sock_release'
> +                    local BPFTOOL_CGROUP_ATTACH_TYPES="$(bpftool feature list attach_types | \
> +                        grep '^cgroup_')"
>                      local ATTACH_FLAGS='multi override'
>                      local PROG_TYPE='id pinned tag name'
>                      # Check for $prev = $command first
> diff --git a/tools/testing/selftests/bpf/test_bpftool_synctypes.py b/tools/testing/selftests/bpf/test_bpftool_synctypes.py
> index e443e6542cb9..a6410bebe603 100755
> --- a/tools/testing/selftests/bpf/test_bpftool_synctypes.py
> +++ b/tools/testing/selftests/bpf/test_bpftool_synctypes.py
> @@ -471,12 +471,6 @@ class BashcompExtractor(FileExtractor):
>      def get_prog_attach_types(self):
>          return self.get_bashcomp_list('BPFTOOL_PROG_ATTACH_TYPES')
>  
> -    def get_map_types(self):
> -        return self.get_bashcomp_list('BPFTOOL_MAP_CREATE_TYPES')
> -
> -    def get_cgroup_attach_types(self):
> -        return self.get_bashcomp_list('BPFTOOL_CGROUP_ATTACH_TYPES')
> -
>  def verify(first_set, second_set, message):
>      """
>      Print all values that differ between two sets.
> @@ -516,17 +510,12 @@ def main():
>      man_map_types = man_map_info.get_map_types()
>      man_map_info.close()
>  
> -    bashcomp_info = BashcompExtractor()
> -    bashcomp_map_types = bashcomp_info.get_map_types()
> -
>      verify(source_map_types, help_map_types,
>              f'Comparing {BpfHeaderExtractor.filename} (bpf_map_type) and {MapFileExtractor.filename} (do_help() TYPE):')
>      verify(source_map_types, man_map_types,
>              f'Comparing {BpfHeaderExtractor.filename} (bpf_map_type) and {ManMapExtractor.filename} (TYPE):')
>      verify(help_map_options, man_map_options,
>              f'Comparing {MapFileExtractor.filename} (do_help() OPTIONS) and {ManMapExtractor.filename} (OPTIONS):')
> -    verify(source_map_types, bashcomp_map_types,
> -            f'Comparing {BpfHeaderExtractor.filename} (bpf_map_type) and {BashcompExtractor.filename} (BPFTOOL_MAP_CREATE_TYPES):')
>  
>      # Attach types (names)
>  
> @@ -542,8 +531,10 @@ def main():
>      man_prog_attach_types = man_prog_info.get_attach_types()
>      man_prog_info.close()
>  
> -    bashcomp_info.reset_read() # We stopped at map types, rewind
> +
> +    bashcomp_info = BashcompExtractor()
>      bashcomp_prog_attach_types = bashcomp_info.get_prog_attach_types()
> +    bashcomp_info.close()
>  
>      verify(source_prog_attach_types, help_prog_attach_types,
>              f'Comparing {ProgFileExtractor.filename} (bpf_attach_type) and {ProgFileExtractor.filename} (do_help() ATTACH_TYPE):')
> @@ -568,17 +559,12 @@ def main():
>      man_cgroup_attach_types = man_cgroup_info.get_attach_types()
>      man_cgroup_info.close()
>  
> -    bashcomp_cgroup_attach_types = bashcomp_info.get_cgroup_attach_types()
> -    bashcomp_info.close()
> -
>      verify(source_cgroup_attach_types, help_cgroup_attach_types,
>              f'Comparing {BpfHeaderExtractor.filename} (bpf_attach_type) and {CgroupFileExtractor.filename} (do_help() ATTACH_TYPE):')
>      verify(source_cgroup_attach_types, man_cgroup_attach_types,
>              f'Comparing {BpfHeaderExtractor.filename} (bpf_attach_type) and {ManCgroupExtractor.filename} (ATTACH_TYPE):')
>      verify(help_cgroup_options, man_cgroup_options,
>              f'Comparing {CgroupFileExtractor.filename} (do_help() OPTIONS) and {ManCgroupExtractor.filename} (OPTIONS):')
> -    verify(source_cgroup_attach_types, bashcomp_cgroup_attach_types,
> -            f'Comparing {BpfHeaderExtractor.filename} (bpf_attach_type) and {BashcompExtractor.filename} (BPFTOOL_CGROUP_ATTACH_TYPES):')
>  
>      # Options for remaining commands
>  

That is a nice simplification. Looks good to me.

Acked-by: Daniel Müller <deso@posteo.net>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 1/2] bpftool: Add feature list (prog/map/link/attach types, helpers)
  2022-06-29 14:40 ` [PATCH bpf-next 1/2] bpftool: Add feature list (prog/map/link/attach types, helpers) Quentin Monnet
@ 2022-06-29 17:32   ` Daniel Müller
  2022-06-29 20:35     ` Quentin Monnet
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Müller @ 2022-06-29 17:32 UTC (permalink / raw)
  To: Quentin Monnet
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, bpf, netdev

On Wed, Jun 29, 2022 at 03:40:18PM +0100, Quentin Monnet wrote:
> Add a "bpftool feature list" subcommand to list BPF "features".
> Contrarily to "bpftool feature probe", this is not about the features
> available on the system. Instead, it lists all features known to bpftool
> from compilation time; in other words, all program, map, attach, link
> types known to the libbpf version in use, and all helpers found in the
> UAPI BPF header.
> 
> The first use case for this feature is bash completion: running the
> command provides a list of types that can be used to produce the list of
> candidate map types, for example.
> 
> Now that bpftool uses "standard" names provided by libbpf for the
> program, map, link, and attach types, having the ability to list these
> types and helpers could also be useful in scripts to loop over existing
> items.
> 
> Sample output:
> 
>     # bpftool feature list prog_types | grep -vw unspec | head -n 6
>     socket_filter
>     kprobe
>     sched_cls
>     sched_act
>     tracepoint
>     xdp
> 
>     # bpftool -p feature list map_types | jq '.[1]'
>     "hash"
> 
>     # bpftool feature list attach_types | grep '^cgroup_'
>     cgroup_inet_ingress
>     cgroup_inet_egress
>     [...]
>     cgroup_inet_sock_release
> 
>     # bpftool feature list helpers | grep -vw bpf_unspec | wc -l
>     207
> 
> The "unspec" types and helpers are not filtered out by bpftool, so as to
> remain closer to the enums, and to preserve the indices in the JSON
> arrays (e.g. "hash" at index 1 == BPF_MAP_TYPE_HASH in map types list).
> 
> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> ---
>  .../bpftool/Documentation/bpftool-feature.rst | 12 ++++
>  tools/bpf/bpftool/bash-completion/bpftool     |  7 ++-
>  tools/bpf/bpftool/feature.c                   | 55 +++++++++++++++++++
>  3 files changed, 73 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/bpf/bpftool/Documentation/bpftool-feature.rst b/tools/bpf/bpftool/Documentation/bpftool-feature.rst
> index 4ce9a77bc1e0..4bf1724d0e8c 100644
> --- a/tools/bpf/bpftool/Documentation/bpftool-feature.rst
> +++ b/tools/bpf/bpftool/Documentation/bpftool-feature.rst
> @@ -24,9 +24,11 @@ FEATURE COMMANDS
>  ================
>  
>  |	**bpftool** **feature probe** [*COMPONENT*] [**full**] [**unprivileged**] [**macros** [**prefix** *PREFIX*]]
> +|	**bpftool** **feature list** *GROUP*
>  |	**bpftool** **feature help**
>  |
>  |	*COMPONENT* := { **kernel** | **dev** *NAME* }
> +|	*GROUP* := { **prog_types** | **map_types** | **attach_types** | **helpers** }

Is **link_types** missing from this enumeration?


>  DESCRIPTION
>  ===========
> @@ -70,6 +72,16 @@ DESCRIPTION
>  		  The keywords **full**, **macros** and **prefix** have the
>  		  same role as when probing the kernel.
>  
> +	**bpftool feature list** *GROUP*
> +		  List items known to bpftool. These can be BPF program types
> +		  (**prog_types**), BPF map types (**map_types**), attach types
> +		  (**attach_types**), link types (**link_types**), or BPF helper
> +		  functions (**helpers**). The command does not probe the system, but
> +		  simply lists the elements that bpftool knows from compilation time,
> +		  as provided from libbpf (for all object types) or from the BPF UAPI
> +		  header (list of helpers). This can be used in scripts to iterate over
> +		  BPF types or helpers.
> +
>  	**bpftool feature help**
>  		  Print short help message.
>  
> diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
> index 91f89a9a5b36..9cef6516320b 100644
> --- a/tools/bpf/bpftool/bash-completion/bpftool
> +++ b/tools/bpf/bpftool/bash-completion/bpftool
> @@ -1175,9 +1175,14 @@ _bpftool()
>                      _bpftool_once_attr 'full unprivileged'
>                      return 0
>                      ;;
> +                list)
> +                    [[ $prev != "$command" ]] && return 0
> +                    COMPREPLY=( $( compgen -W 'prog_types map_types \
> +                        attach_types link_types helpers' -- "$cur" ) )
> +                    ;;
>                  *)
>                      [[ $prev == $object ]] && \
> -                        COMPREPLY=( $( compgen -W 'help probe' -- "$cur" ) )
> +                        COMPREPLY=( $( compgen -W 'help list probe' -- "$cur" ) )
>                      ;;
>              esac
>              ;;
> diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c
> index bac4ef428a02..576cc6b90c6a 100644
> --- a/tools/bpf/bpftool/feature.c
> +++ b/tools/bpf/bpftool/feature.c
> @@ -1258,6 +1258,58 @@ static int do_probe(int argc, char **argv)
>  	return 0;
>  }
>  
> +static const char *get_helper_name(unsigned int id)
> +{
> +	if (id >= ARRAY_SIZE(helper_name))
> +		return NULL;
> +
> +	return helper_name[id];
> +}
> +
> +static int do_list(int argc, char **argv)
> +{
> +	const char *(*get_name)(unsigned int id);
> +	unsigned int id = 0;
> +
> +	if (argc < 1)
> +		usage();
> +
> +	if (is_prefix(*argv, "prog_types")) {
> +		get_name = (const char *(*)(unsigned int))libbpf_bpf_prog_type_str;
> +	} else if (is_prefix(*argv, "map_types")) {
> +		get_name = (const char *(*)(unsigned int))libbpf_bpf_map_type_str;
> +	} else if (is_prefix(*argv, "attach_types")) {
> +		get_name = (const char *(*)(unsigned int))libbpf_bpf_attach_type_str;
> +	} else if (is_prefix(*argv, "link_types")) {
> +		get_name = (const char *(*)(unsigned int))libbpf_bpf_link_type_str;
> +	} else if (is_prefix(*argv, "helpers")) {
> +		get_name = get_helper_name;
> +	} else {
> +		p_err("expected 'prog_types', 'map_types', 'attach_types', 'link_types' or 'helpers', got: %s", *argv);
> +		return -1;
> +	}
> +
> +	if (json_output)
> +		jsonw_start_array(json_wtr);	/* root array */
> +
> +	while (true) {
> +		const char *name;
> +
> +		name = get_name(id++);
> +		if (!name)
> +			break;
> +		if (json_output)
> +			jsonw_string(json_wtr, name);
> +		else
> +			printf("%s\n", name);
> +	}
> +
> +	if (json_output)
> +		jsonw_end_array(json_wtr);	/* root array */
> +
> +	return 0;
> +}
> +
>  static int do_help(int argc, char **argv)
>  {
>  	if (json_output) {
> @@ -1267,9 +1319,11 @@ static int do_help(int argc, char **argv)
>  
>  	fprintf(stderr,
>  		"Usage: %1$s %2$s probe [COMPONENT] [full] [unprivileged] [macros [prefix PREFIX]]\n"
> +		"       %1$s %2$s list GROUP\n"
>  		"       %1$s %2$s help\n"
>  		"\n"
>  		"       COMPONENT := { kernel | dev NAME }\n"
> +		"       GROUP := { prog_types | map_types | attach_types | link_types | helpers }\n"
>  		"       " HELP_SPEC_OPTIONS " }\n"
>  		"",
>  		bin_name, argv[-2]);
> @@ -1279,6 +1333,7 @@ static int do_help(int argc, char **argv)
>  
>  static const struct cmd cmds[] = {
>  	{ "probe",	do_probe },
> +	{ "list",	do_list },
>  	{ "help",	do_help },
>  	{ 0 }
>  };
> -- 
> 2.34.1
> 

The rest looks good to me. Thanks!

Acked-by: Daniel Müller <deso@posteo.net>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 1/2] bpftool: Add feature list (prog/map/link/attach types, helpers)
  2022-06-29 17:32   ` Daniel Müller
@ 2022-06-29 20:35     ` Quentin Monnet
  0 siblings, 0 replies; 6+ messages in thread
From: Quentin Monnet @ 2022-06-29 20:35 UTC (permalink / raw)
  To: Daniel Müller
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, bpf, Networking

On Wed, 29 Jun 2022 at 18:33, Daniel Müller <deso@posteo.net> wrote:
>
> On Wed, Jun 29, 2022 at 03:40:18PM +0100, Quentin Monnet wrote:
> > Add a "bpftool feature list" subcommand to list BPF "features".
> > Contrarily to "bpftool feature probe", this is not about the features
> > available on the system. Instead, it lists all features known to bpftool
> > from compilation time; in other words, all program, map, attach, link
> > types known to the libbpf version in use, and all helpers found in the
> > UAPI BPF header.
> >
> > The first use case for this feature is bash completion: running the
> > command provides a list of types that can be used to produce the list of
> > candidate map types, for example.
> >
> > Now that bpftool uses "standard" names provided by libbpf for the
> > program, map, link, and attach types, having the ability to list these
> > types and helpers could also be useful in scripts to loop over existing
> > items.
> >
> > Sample output:
> >
> >     # bpftool feature list prog_types | grep -vw unspec | head -n 6
> >     socket_filter
> >     kprobe
> >     sched_cls
> >     sched_act
> >     tracepoint
> >     xdp
> >
> >     # bpftool -p feature list map_types | jq '.[1]'
> >     "hash"
> >
> >     # bpftool feature list attach_types | grep '^cgroup_'
> >     cgroup_inet_ingress
> >     cgroup_inet_egress
> >     [...]
> >     cgroup_inet_sock_release
> >
> >     # bpftool feature list helpers | grep -vw bpf_unspec | wc -l
> >     207
> >
> > The "unspec" types and helpers are not filtered out by bpftool, so as to
> > remain closer to the enums, and to preserve the indices in the JSON
> > arrays (e.g. "hash" at index 1 == BPF_MAP_TYPE_HASH in map types list).
> >
> > Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> > ---
> >  .../bpftool/Documentation/bpftool-feature.rst | 12 ++++
> >  tools/bpf/bpftool/bash-completion/bpftool     |  7 ++-
> >  tools/bpf/bpftool/feature.c                   | 55 +++++++++++++++++++
> >  3 files changed, 73 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/bpf/bpftool/Documentation/bpftool-feature.rst b/tools/bpf/bpftool/Documentation/bpftool-feature.rst
> > index 4ce9a77bc1e0..4bf1724d0e8c 100644
> > --- a/tools/bpf/bpftool/Documentation/bpftool-feature.rst
> > +++ b/tools/bpf/bpftool/Documentation/bpftool-feature.rst
> > @@ -24,9 +24,11 @@ FEATURE COMMANDS
> >  ================
> >
> >  |    **bpftool** **feature probe** [*COMPONENT*] [**full**] [**unprivileged**] [**macros** [**prefix** *PREFIX*]]
> > +|    **bpftool** **feature list** *GROUP*
> >  |    **bpftool** **feature help**
> >  |
> >  |    *COMPONENT* := { **kernel** | **dev** *NAME* }
> > +|    *GROUP* := { **prog_types** | **map_types** | **attach_types** | **helpers** }
>
> Is **link_types** missing from this enumeration?

Yes of course, thanks for catching this. And for the review! v2 is on its way.
Quentin

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-06-29 20:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-29 14:40 [PATCH bpf-next 0/2] bpftool: Add command to list BPF types, helpers Quentin Monnet
2022-06-29 14:40 ` [PATCH bpf-next 1/2] bpftool: Add feature list (prog/map/link/attach types, helpers) Quentin Monnet
2022-06-29 17:32   ` Daniel Müller
2022-06-29 20:35     ` Quentin Monnet
2022-06-29 14:40 ` [PATCH bpf-next 2/2] bpftool: Use feature list in bash completion Quentin Monnet
2022-06-29 16:56   ` Daniel Müller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.