tools: bpftool: support creating and dumping outer maps
Subsystems (4)
cover tools: bpftool: support creating and dumping outer maps
This series makes bpftool able to create and dump the content for outer maps (maps of types array-of-maps and hash-of-maps). The modifications are rather succinct: dumping works if we remove the related restriction in bpftool's code, and creation is just a matter of passing the relevant inner_map_fd, which we do through a new command-line keyword.
Quentin Monnet (2):
tools: bpftool: dump outer maps content
tools: bpftool: add "inner_map" to "bpftool map create" outer maps
.../bpf/bpftool/Documentation/bpftool-map.rst | 10 +++- tools/bpf/bpftool/bash-completion/bpftool | 22 +++++++- tools/bpf/bpftool/map.c | 52 ++++++++++++------- 3 files changed, 62 insertions(+), 22 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 tools: bpftool: dump outer maps content
Although user space can lookup and dump the content of an outer map (hash-of-maps or array-of-maps), bpftool does not allow to do so.
It seems that the only reason for that is historical. Lookups for outer maps was added in commit 14dc6f04f49d ("bpf: Add syscall lookup support for fd array and htab"), and although the relevant code in bpftool had not been merged yet, I suspect it had already been written with the assumption that user space could not read outer maps.
Let's remove the restriction, dump for outer maps works with no further change.
--- tools/bpf/bpftool/map.c | 4 ---- 1 file changed, 4 deletions(-)
tools/bpf/bpftool/map.c +0 -4
@@ -739,10 +739,6 @@ static int dump_map_elem(int fd, void *key, void *value,/* lookup error handling */ lookup_errno = errno; -if (map_is_map_of_maps (map_info->type) || -map_is_map_of_progs (map_info->type)) -return 0 ; -if (json_output) {jsonw_start_object (json_wtr);jsonw_name (json_wtr,"key" );
Signature · 1 line
2.25.1
Re: [PATCH bpf-next 1/2] tools: bpftool: dump outer maps content
Quoted 24 lines · Quentin Monnet
Although user space can lookup and dump the content of an outer map (hash-of-maps or array-of-maps), bpftool does not allow to do so.
It seems that the only reason for that is historical. Lookups for outer maps was added in commit
14dc6f04f49d("bpf: Add syscall lookup support for fd array and htab"), and although the relevant code in bpftool had not been merged yet, I suspect it had already been written with the assumption that user space could not read outer maps.Let's remove the restriction, dump for outer maps works with no further change.
Reported-by Martynas Pumputis <[email protected]>Signed-off-by Quentin Monnet <[email protected]>--- tools/bpf/bpftool/map.c | 4 ---- 1 file changed, 4 deletions(-)tools/bpf/bpftool/map.c +0
-0
tools/bpf/bpftool/map.c static int dump_map_elem(int fd, void *key, void *value, · line 745
Quoted 7 lines
tools/bpf/bpftool/map.c +0
-4@@ -739,10 +739,6 @@ static int dump_map_elem(int fd, void *key, void *value,/* lookup error handling */ lookup_errno = errno; -if (map_is_map_of_maps (map_info->type) || -map_is_map_of_progs (map_info->type)) -return 0 ; -
this code path handles the error case when lookup fails, or am I misreading it? It's fine to remove this restriction, but the commit message is completely misleading. That whole dump_map_elem() code is a bit confusing. E.g., what's the purpose of num_elems there?..
Also, can you please update the commit message with how the output looks like for map-of-maps with your change?
Quoted 6 lines
tools/bpf/bpftool/map.c +0
-1if (json_output) {jsonw_start_object (json_wtr);jsonw_name (json_wtr,"key" ); --2.25.1
Re: [PATCH bpf-next 1/2] tools: bpftool: dump outer maps content
Quoted 38 lines · Andrii Nakryiko
Quoted 24 lines · Quentin Monnet
Although user space can lookup and dump the content of an outer map (hash-of-maps or array-of-maps), bpftool does not allow to do so.
It seems that the only reason for that is historical. Lookups for outer maps was added in commit
14dc6f04f49d("bpf: Add syscall lookup support for fd array and htab"), and although the relevant code in bpftool had not been merged yet, I suspect it had already been written with the assumption that user space could not read outer maps.Let's remove the restriction, dump for outer maps works with no further change.
Reported-by Martynas Pumputis <[email protected]>Signed-off-by Quentin Monnet <[email protected]>--- tools/bpf/bpftool/map.c | 4 ---- 1 file changed, 4 deletions(-)tools/bpf/bpftool/map.c +0
-0tools/bpf/bpftool/map.c static int dump_map_elem(int fd, void *key, void *value, · line 745
Quoted 7 lines
tools/bpf/bpftool/map.c +0
-4@@ -739,10 +739,6 @@ static int dump_map_elem(int fd, void *key, void *value,/* lookup error handling */ lookup_errno = errno; -if (map_is_map_of_maps (map_info->type) || -map_is_map_of_progs (map_info->type)) -return 0 ; -this code path handles the error case when lookup fails, or am I misreading it? It's fine to remove this restriction, but the commit message is completely misleading. That whole
dump_map_elem()code is a bit confusing. E.g., what's the purpose ofnum_elemsthere?..
Quoted 2 lines
Also, can you please update the commit message with how the output looks like for map-of-maps with your change?
Ok, the function _is_ confusing and _I_ totally got confused. Dumping outer maps is already supported and this patch is not needed (and as you mentioned, it does not do what I expected and wrote). Sorry about that.
I'll send a clean-up for that function instead in v2, to avoid confusion in the future. Thanks for the review!
Quentin
PATCH 2/2 tools: bpftool: add "inner_map" to "bpftool map create" outer maps
In reply to Quentin MonnetThere is no support for creating maps of types array-of-map or hash-of-map in bpftool. This is because the kernel needs an inner_map_fd to collect metadata on the inner maps to be supported by the new map, but bpftool does not provide a way to pass this file descriptor.
Add a new optional "inner_map" keyword that can be used to pass a reference to a map, retrieve a fd to that map, and pass it as the inner_map_fd.
Add related documentation and bash completion. Note that we can reference the inner map by its name, meaning we can have several times the keyword "name" with different meanings (mandatory outer map name, and possibly a name to use to find the inner_map_fd). The bash completion will offer it just once, and will not suggest "name" on the following command:
# bpftool map create /sys/fs/bpf/my_outer_map type hash_of_maps \
inner_map name my_inner_map [TAB]Fixing that specific case seems too convoluted. Completion will work as expected, however, if the outer map name comes first and the "inner_map name ..." is passed second.
--- .../bpf/bpftool/Documentation/bpftool-map.rst | 10 +++- tools/bpf/bpftool/bash-completion/bpftool | 22 ++++++++- tools/bpf/bpftool/map.c | 48 +++++++++++++------ 3 files changed, 62 insertions(+), 18 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +8 -2
@@ -23,7 +23,8 @@ MAP COMMANDS | **bpftool** **map** { **show** | **list** } [*MAP*] | **bpftool** **map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* \ -| **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*] +| **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**inner_map** *MAP*] \ +| [**dev** *NAME*] | **bpftool** **map dump** *MAP* | **bpftool** **map update** *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*] | **bpftool** **map lookup** *MAP* [**key** *DATA*]
@@ -67,7 +68,7 @@ DESCRIPTION maps. On such kernels bpftool will automatically emit this information as well. - **bpftool map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*] + **bpftool map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**inner_map** *MAP*] [**dev** *NAME*] Create a new map with given parameters and pin it to *bpffs* as *FILE*.
@@ -75,6 +76,11 @@ DESCRIPTION desired flags, e.g. 1024 for **BPF_F_MMAPABLE** (see bpf.h UAPI header for existing flags). + To create maps of type array-of-maps or hash-of-maps, the + **inner_map** keyword must be used to pass an inner map. The + kernel needs it to collect metadata related to the inner maps + that the new map will work with. + Keyword **dev** expects a network interface name, and is used to request hardware offload for the map.
tools/bpf/bpftool/bash-completion/bpftool +21 -1
@@ -709,9 +709,26 @@ _bpftool() "$cur" ) ) return 0 ;; - key|value|flags|name|entries) + key|value|flags|entries) return 0 ;; + inner_map) + COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) ) + return 0 + ;; + id) + _bpftool_get_map_ids + ;; + name) + case $pprev in + inner_map) + _bpftool_get_map_names + ;; + *) + return 0 + ;; + esac + ;; *) _bpftool_once_attr 'type' _bpftool_once_attr 'key'
@@ -719,6 +736,9 @@ _bpftool() _bpftool_once_attr 'entries' _bpftool_once_attr 'name' _bpftool_once_attr 'flags' + if _bpftool_search_list 'array_of_maps' 'hash_of_maps'; then + _bpftool_once_attr 'inner_map' + fi _bpftool_once_attr 'dev' return 0 ;;
tools/bpf/bpftool/map.c +33 -15
120 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH bpf-next 2/2] tools: bpftool: add "inner_map" to "bpftool map create" outer maps
Quoted 20 lines · Quentin Monnet
There is no support for creating maps of types array-of-map or hash-of-map in bpftool. This is because the kernel needs an
inner_map_fdto collect metadata on the inner maps to be supported by the new map, but bpftool does not provide a way to pass this file descriptor.Add a new optional "
inner_map" keyword that can be used to pass a reference to a map, retrieve a fd to that map, and pass it as theinner_map_fd.Add related documentation and bash completion. Note that we can reference the inner map by its name, meaning we can have several times the keyword "name" with different meanings (mandatory outer map name, and possibly a name to use to find the
inner_map_fd). The bash completion will offer it just once, and will not suggest "name" on the following command:# bpftool map create /sys/fs/bpf/my_outer_map type hash_of_maps \ inner_map name my_inner_map [TAB]
Quoted 6 lines
Fixing that specific case seems too convoluted. Completion will work as expected, however, if the outer map name comes first and the "
inner_mapname ..." is passed second.Signed-off-by Quentin Monnet <[email protected]>---
Quoted 5 lines
.../bpf/bpftool/Documentation/bpftool-map.rst | 10 +++- tools/bpf/bpftool/bash-completion/bpftool | 22 ++++++++- tools/bpf/bpftool/map.c | 48 +++++++++++++------ 3 files changed, 62 insertions(+), 18 deletions(-)
[...]