bpftool: match programs and maps by names
Subsystems (4)
cover bpftool: match programs and maps by names
When working with frequently modified BPF programs, both the ID and the tag may change. bpftool currently doesn't provide a "stable" way to match such programs. This patchset allows bpftool to match programs and maps by name.
When given a tag that matches several programs, bpftool currently only considers the first match. The first patch changes that behavior to either process all matching programs (for the show and dump commands) or error out. The second patch implements program lookup by name, with the same behavior as for tags in case of ambiguity. The last patch implements map lookup by name.
Paul Chaignon (3):
bpftool: match several programs with same tag
bpftool: match programs by name
bpftool: match maps by name
.../bpf/bpftool/Documentation/bpftool-map.rst | 12 +- .../bpftool/Documentation/bpftool-prog.rst | 18 +- tools/bpf/bpftool/bash-completion/bpftool | 145 ++++++- tools/bpf/bpftool/main.h | 4 +- tools/bpf/bpftool/map.c | 366 +++++++++++++--- tools/bpf/bpftool/prog.c | 389 +++++++++++++----- 6 files changed, 735 insertions(+), 199 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/3 bpftool: match several programs with same tag
When several BPF programs have the same tag, bpftool matches only the first (in ID order). This patch changes that behavior such that dump and show commands return all matched programs. Commands that require a single program (e.g., pin and attach) will error out if given a tag that matches several. bpftool prog dump will also error out if file or visual are given and several programs have the given tag.
In the case of the dump command, a program header is added before each dump only if the tag matches several programs; this patch doesn't change the output if a single program matches.
--- .../bpftool/Documentation/bpftool-prog.rst | 16 +- tools/bpf/bpftool/prog.c | 371 ++++++++++++------ 2 files changed, 272 insertions(+), 115 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-prog.rst +12 -4
@@ -53,8 +53,10 @@ DESCRIPTION =========== **bpftool prog { show | list }** [*PROG*] Show information about loaded programs. If *PROG* is - specified show information only about given program, otherwise - list all programs currently loaded on the system. + specified show information only about given programs, + otherwise list all programs currently loaded on the system. + In case of **tag**, *PROG* may match several programs which + will all be shown. Output will start with program ID followed by program type and zero or more named attributes (depending on kernel version).
@@ -68,11 +70,15 @@ DESCRIPTION performed via the **kernel.bpf_stats_enabled** sysctl knob. **bpftool prog dump xlated** *PROG* [{ **file** *FILE* | **opcodes** | **visual** | **linum** }] - Dump eBPF instructions of the program from the kernel. By + Dump eBPF instructions of the programs from the kernel. By default, eBPF will be disassembled and printed to standard output in human-readable format. In this case, **opcodes** controls if raw opcodes should be printed as well. + In case of **tag**, *PROG* may match several programs which + will all be dumped. However, if **file** or **visual** is + specified, *PROG* must match a single program. + If **file** is specified, the binary image will instead be written to *FILE*.
@@ -80,15 +86,17 @@ DESCRIPTION built instead, and eBPF instructions will be presented with CFG in DOT format, on standard output. - If the prog has line_info available, the source line will + If the programs have line_info available, the source line will be displayed by default. If **linum** is specified, the filename, line number and line column will also be displayed on top of the source line. **bpftool prog dump jited** *PROG* [{ **file** *FILE* | **opcodes** | **linum** }] Dump jited image (host machine code) of the program. + If *FILE* is specified image will be written to a file, otherwise it will be disassembled and printed to stdout. + *PROG* must match a single program when **file** is specified. **opcodes** controls if raw opcodes will be printed.
tools/bpf/bpftool/prog.c +260 -111
538 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 2/3 bpftool: match programs by name
In reply to Paul ChaignonWhen working with frequently modified BPF programs, both the ID and the tag may change. bpftool currently doesn't provide a "stable" way to match such programs.
This patch implements lookup by name for programs. The show and dump commands will return all programs with the given name, whereas other commands will error out if several programs have the same name.
--- .../bpf/bpftool/Documentation/bpftool-map.rst | 2 +- .../bpftool/Documentation/bpftool-prog.rst | 12 +++++----- tools/bpf/bpftool/bash-completion/bpftool | 22 ++++++++++++----- tools/bpf/bpftool/main.h | 2 +- tools/bpf/bpftool/prog.c | 24 +++++++++++++++---- 5 files changed, 43 insertions(+), 19 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +1 -1
@@ -41,7 +41,7 @@ MAP COMMANDS | | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* } | *DATA* := { [**hex**] *BYTES* } -| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* } +| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* } | *VALUE* := { *DATA* | *MAP* | *PROG* } | *UPDATE_FLAGS* := { **any** | **exist** | **noexist** } | *TYPE* := { **hash** | **array** | **prog_array** | **perf_event_array** | **percpu_hash**
tools/bpf/bpftool/Documentation/bpftool-prog.rst +6 -6
@@ -33,7 +33,7 @@ PROG COMMANDS | **bpftool** **prog help** | | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* } -| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* } +| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* } | *TYPE* := { | **socket** | **kprobe** | **kretprobe** | **classifier** | **action** | | **tracepoint** | **raw_tracepoint** | **xdp** | **perf_event** | **cgroup/skb** |
@@ -55,8 +55,8 @@ DESCRIPTION Show information about loaded programs. If *PROG* is specified show information only about given programs, otherwise list all programs currently loaded on the system. - In case of **tag**, *PROG* may match several programs which - will all be shown. + In case of **tag** or **name**, *PROG* may match several + programs which will all be shown. Output will start with program ID followed by program type and zero or more named attributes (depending on kernel version).
@@ -75,9 +75,9 @@ DESCRIPTION output in human-readable format. In this case, **opcodes** controls if raw opcodes should be printed as well. - In case of **tag**, *PROG* may match several programs which - will all be dumped. However, if **file** or **visual** is - specified, *PROG* must match a single program. + In case of **tag** or **name**, *PROG* may match several + programs which will all be dumped. However, if **file** or + **visual** is specified, *PROG* must match a single program. If **file** is specified, the binary image will instead be written to *FILE*.
tools/bpf/bpftool/bash-completion/bpftool +16 -6
@@ -71,6 +71,12 @@ _bpftool_get_prog_tags() command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) ) } +_bpftool_get_prog_names() +{ + COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \ + command sed -n 's/.*"name": "\(.*\)",$/\1/p' )" -- "$cur" ) ) +} + _bpftool_get_btf_ids() { COMPREPLY+=( $( compgen -W "$( bpftool -jp btf 2>&1 | \
@@ -201,6 +207,10 @@ _bpftool() _bpftool_get_prog_tags return 0 ;; + name) + _bpftool_get_prog_names + return 0 + ;; dev) _sysfs_get_netdevs return 0
@@ -263,7 +273,7 @@ _bpftool() ;; esac - local PROG_TYPE='id pinned tag' + local PROG_TYPE='id pinned tag name' local MAP_TYPE='id pinned' case $command in show|list)
@@ -559,7 +569,7 @@ _bpftool() return 0 ;; prog_array) - local PROG_TYPE='id pinned tag' + local PROG_TYPE='id pinned tag name' COMPREPLY+=( $( compgen -W "$PROG_TYPE" \ -- "$cur" ) ) return 0
@@ -644,7 +654,7 @@ _bpftool() esac ;; btf) - local PROG_TYPE='id pinned tag' + local PROG_TYPE='id pinned tag name' local MAP_TYPE='id pinned' case $command in dump)
@@ -735,7 +745,7 @@ _bpftool() connect6 sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl \ getsockopt setsockopt' local ATTACH_FLAGS='multi override' - local PROG_TYPE='id pinned tag' + local PROG_TYPE='id pinned tag name' case $prev in $command) _filedir
@@ -760,7 +770,7 @@ _bpftool() elif [[ "$command" == "attach" ]]; then # We have an attach type on the command line, # but it is not the previous word, or - # "id|pinned|tag" (we already checked for + # "id|pinned|tag|name" (we already checked for # that). This should only leave the case when # we need attach flags for "attach" commamnd. _bpftool_one_of_list "$ATTACH_FLAGS"
@@ -786,7 +796,7 @@ _bpftool() esac ;; net) - local PROG_TYPE='id pinned tag' + local PROG_TYPE='id pinned tag name' local ATTACH_TYPES='xdp xdpgeneric xdpdrv xdpoffload' case $command in show|list)
tools/bpf/bpftool/main.h +1 -1
@@ -42,7 +42,7 @@#define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"#define HELP_SPEC_PROGRAM \ - "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }" + "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG | name PROG_NAME }"#define HELP_SPEC_OPTIONS \ "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} |\n" \ "\t {-m|--mapcompat} | {-n|--nomount} }"
tools/bpf/bpftool/prog.c +19 -5
@@ -83,7 +83,7 @@ static void print_boot_time(__u64 nsecs, char *buf, unsigned int size) }static int -prog_fd_by_tag (unsigned char *tag,int *fds) +prog_fd_by_nametag (char *nametag,int *fds,bool tag) {unsigned int id =0 ;int fd, nb_fds =0 ;
@@ -117,7 +117,8 @@ prog_fd_by_tag(unsigned char *tag, int *fds) goto err_close_fd; } -if (memcmp (tag, info.tag,BPF_TAG_SIZE )) { +if ((tag &&memcmp (nametag, info.tag,BPF_TAG_SIZE )) || + (!tag &&strncmp (nametag, info.name,BPF_OBJ_NAME_LEN ))) {close (fd);continue ; }
@@ -164,7 +165,7 @@ prog_parse_fds(int *argc, char ***argv, int *fds) }return 1 ; }else if (is_prefix (**argv,"tag" )) { -unsigned char tag[BPF_TAG_SIZE ]; +char tag[BPF_TAG_SIZE ];NEXT_ARGP ();
@@ -176,7 +177,20 @@ prog_parse_fds(int *argc, char ***argv, int *fds) }NEXT_ARGP (); -return prog_fd_by_tag (tag, fds); +return prog_fd_by_nametag (tag, fds, true); + }else if (is_prefix (**argv,"name" )) { +char *name; + +NEXT_ARGP (); + + name = **argv; +if (strlen (name) >BPF_OBJ_NAME_LEN -1 ) { +p_err ("can't parse name" ); +return -1 ; + } +NEXT_ARGP (); + +return prog_fd_by_nametag (name, fds, false); }else if (is_prefix (**argv,"pinned" )) {char *path;
@@ -191,7 +205,7 @@ prog_parse_fds(int *argc, char ***argv, int *fds)return 1 ; } -p_err ("expected 'id', 'tag' or 'pinned', got: '%s'?" , **argv); +p_err ("expected 'id', 'tag', 'name' or 'pinned', got: '%s'?" , **argv);return -1 ; }
Signature · 1 line
2.17.1
PATCH 3/3 bpftool: match maps by name
In reply to Paul ChaignonThis patch implements lookup by name for maps and changes the behavior of lookups by tag to be consistent with prog subcommands. Similarly to program subcommands, the show and dump commands will return all maps with the given name (or tag), whereas other commands will error out if several maps have the same name (resp. tag).
When a map has BTF info, it is dumped in JSON with available BTF info. This patch requires that all matched maps have BTF info before switching the output format to JSON.
--- .../bpf/bpftool/Documentation/bpftool-map.rst | 10 +- tools/bpf/bpftool/bash-completion/bpftool | 131 ++++++- tools/bpf/bpftool/main.h | 2 +- tools/bpf/bpftool/map.c | 366 +++++++++++++++--- 4 files changed, 432 insertions(+), 77 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +6 -4
@@ -39,7 +39,7 @@ MAP COMMANDS | **bpftool** **map freeze** *MAP* | **bpftool** **map help** | -| *MAP* := { **id** *MAP_ID* | **pinned** *FILE* } +| *MAP* := { **id** *MAP_ID* | **pinned** *FILE* | **name** *MAP_NAME* } | *DATA* := { [**hex**] *BYTES* } | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* } | *VALUE* := { *DATA* | *MAP* | *PROG* }
@@ -55,8 +55,9 @@ DESCRIPTION =========== **bpftool map { show | list }** [*MAP*] Show information about loaded maps. If *MAP* is specified - show information only about given map, otherwise list all - maps currently loaded on the system. + show information only about given maps, otherwise list all + maps currently loaded on the system. In case of **name**, + *MAP* may match several maps which will all be shown. Output will start with map ID followed by map type and zero or more named attributes (depending on kernel version).
@@ -66,7 +67,8 @@ DESCRIPTION as *FILE*. **bpftool map dump** *MAP* - Dump all entries in a given *MAP*. + Dump all entries in a given *MAP*. In case of **name**, + *MAP* may match several maps which will all be dumped. **bpftool map update** *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*] Update map entry for a given *KEY*.
tools/bpf/bpftool/bash-completion/bpftool +122 -9
232 lines not on this page · raw message
tools/bpf/bpftool/main.h +1 -1
@@ -47,7 +47,7 @@"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} |\n" \ "\t {-m|--mapcompat} | {-n|--nomount} }"#define HELP_SPEC_MAP \ - "MAP := { id MAP_ID | pinned FILE }" + "MAP := { id MAP_ID | pinned FILE | name MAP_NAME }"static const char *const prog_type_name[] = { [BPF_PROG_TYPE_UNSPEC ] ="unspec" ,
tools/bpf/bpftool/map.c +303 -63
469 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH bpf-next 1/3] bpftool: match several programs with same tag
In reply to Paul ChaignonHi Paul,
2019-12-10 17:06 UTC+0100 ~ Paul Chaignon <[email protected]>
Quoted 35 lines
When several BPF programs have the same tag, bpftool matches only the first (in ID order). This patch changes that behavior such that dump and show commands return all matched programs. Commands that require a single program (e.g., pin and attach) will error out if given a tag that matches several. bpftool prog dump will also error out if file or visual are given and several programs have the given tag.
In the case of the dump command, a program header is added before each dump only if the tag matches several programs; this patch doesn't change the output if a single program matches.
Signed-off-by Paul Chaignon <[email protected]>--- .../bpftool/Documentation/bpftool-prog.rst | 16 +- tools/bpf/bpftool/prog.c | 371 ++++++++++++------ 2 files changed, 272 insertions(+), 115 deletions(-)tools/bpf/bpftool/prog.c +5
-0@@ -25,6 +25,11 @@#include "main.h" #include "xlated_dumper.h" +enum dump_mode { +DUMP_JITED , +DUMP_XLATED , +}; +static const char *const attach_type_strings[] = { [BPF_SK_SKB_STREAM_PARSER ] ="stream_parser" , [BPF_SK_SKB_STREAM_VERDICT ] ="stream_verdict" ,
tools/bpf/bpftool/prog.c static void print_boot_time(__u64 nsecs, char *buf, unsigned int size) · line 86
Quoted 6 lines
tools/bpf/bpftool/prog.c +2
-1@@ -77,11 +82,13 @@ static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)strftime (buf, size,"%FT%T%z" , &load_tm); } -static int prog_fd_by_tag (unsigned char *tag) +static int +prog_fd_by_tag (unsigned char *tag,int *fds)
Nit: No line break necessary if it fits on one line. (Sorry for misleading you on that in an earlier discussion :/)
Quoted 9 lines
tools/bpf/bpftool/prog.c +2
-1{unsigned int id =0 ; +int fd, nb_fds =0 ; +void *tmp;int err; -int fd;while (true) {struct bpf_prog_info info = {};
[...]
Quoted 27 lines
tools/bpf/bpftool/prog.c +9
-5@@ -351,21 +421,43 @@ static int show_prog(int fd)static int do_show (int argc,char **argv) { +int fd, nb_fds, i; +int *fds =NULL ;__u32 id =0 ;int err; -int fd;if (show_pinned)build_pinned_obj_table (&prog_table,BPF_OBJ_PROG );if (argc ==2 ) { - fd =prog_parse_fd (&argc, &argv); -if (fd <0 ) + fds =malloc (sizeof (int )); +if (!fds) { +p_err ("mem alloc failed" );return -1 ; + } + nb_fds =prog_parse_fds (&argc, &argv, fds); +if (nb_fds <1 ) + goto err_free; - err =show_prog (fd); -close (fd);
tools/bpf/bpftool/prog.c static int show_prog(int fd) · line 450
Quoted 10 lines
tools/bpf/bpftool/prog.c +9
-1-return err; +if (json_output && nb_fds >1 ) +jsonw_start_array (json_wtr);/* root array */ +for (i =0 ; i < nb_fds; i++) { + err =show_prog (fds[i]); +close (fds[i]); +if (err) { +for (i++; i < nb_fds; i++) +close (fds[i]); + goto err_free;
Alternatively, we could keep trying to list the remaining programs. For example, if the system has a long list of BPF programs running and one of them is removed while printing the list, we would still have the rest of the list.
If we went this way, maybe just set err to non-zero if no program at all could be printed?
Quoted 13 lines
tools/bpf/bpftool/prog.c +10
-0+ } + } +if (json_output && nb_fds >1 ) +jsonw_end_array (json_wtr);/* root array */ + +return 0 ; + +err_free: +free (fds); +return -1 ; }if (argc)
Re: [PATCH bpf-next 2/3] bpftool: match programs by name
In reply to Paul Chaignon2019-12-10 17:06 UTC+0100 ~ Paul Chaignon <[email protected]>
Quoted 9 lines
When working with frequently modified BPF programs, both the ID and the tag may change. bpftool currently doesn't provide a "stable" way to match such programs.
This patch implements lookup by name for programs. The show and dump commands will return all programs with the given name, whereas other commands will error out if several programs have the same name.
Signed-off-by Paul Chaignon <[email protected]>
Thanks!
Quentin
Re: [PATCH bpf-next 3/3] bpftool: match maps by name
In reply to Paul Chaignon2019-12-10 17:06 UTC+0100 ~ Paul Chaignon <[email protected]>
tools/bpf/bpftool/bash-completion/bpftool
Quoted 23 lines
This patch implements lookup by name for maps and changes the behavior of lookups by tag to be consistent with prog subcommands. Similarly to program subcommands, the show and dump commands will return all maps with the given name (or tag), whereas other commands will error out if several maps have the same name (resp. tag).
When a map has BTF info, it is dumped in JSON with available BTF info. This patch requires that all matched maps have BTF info before switching the output format to JSON.
Signed-off-by Paul Chaignon <[email protected]>--- .../bpf/bpftool/Documentation/bpftool-map.rst | 10 +- tools/bpf/bpftool/bash-completion/bpftool | 131 ++++++- tools/bpf/bpftool/main.h | 2 +- tools/bpf/bpftool/map.c | 366 +++++++++++++++--- 4 files changed, 432 insertions(+), 77 deletions(-)tools/bpf/bpftool/bash-completion/bpftool +0
-0
Nice work on the completion, thanks!
Quoted 4 lines
tools/bpf/bpftool/map.c +0
-0
[...]
Quoted 15 lines
tools/bpf/bpftool/map.c +8
-2@@ -654,14 +771,42 @@ static int do_show(int argc, char **argv)build_pinned_obj_table (&map_table,BPF_OBJ_MAP );if (argc ==2 ) { - fd =map_parse_fd_and_info (&argc, &argv, &info, &len); -if (fd <0 ) + fds =malloc (sizeof (int )); +if (!fds) { +p_err ("mem alloc failed" );return -1 ; + } + nb_fds =map_parse_fds (&argc, &argv, fds); +if (nb_fds <1 ) + goto err_free; +
tools/bpf/bpftool/map.c static int do_show(int argc, char **argv) · line 792
Quoted 10 lines
tools/bpf/bpftool/map.c +10
-0+if (json_output && nb_fds >1 ) +jsonw_start_array (json_wtr);/* root array */ +for (i =0 ; i < nb_fds; i++) { + err =bpf_obj_get_info_by_fd (fds[i], &info, &len); +if (err) { +p_err ("can't get map info: %s" , +strerror (errno)); +for (; i < nb_fds; i++) +close (fds[i]); + goto err_free;
Same remarks as on patch 1, we may want to keep listing the maps even if we get a failure for one of them?
Quoted 14 lines
tools/bpf/bpftool/map.c +9
-4+ } -if (json_output) -return show_map_close_json (fd, &info); -else -return show_map_close_plain (fd, &info); +if (json_output) +show_map_close_json (fds[i], &info); +else +show_map_close_plain (fds[i], &info); + +close (fds[i]); + } +if (json_output && nb_fds >1 )
tools/bpf/bpftool/map.c static int do_show(int argc, char **argv) · line 812
Quoted 10 lines
tools/bpf/bpftool/map.c +7
-0+jsonw_end_array (json_wtr);/* root array */ + +return 0 ; + +err_free: +free (fds); +return -1 ; }if (argc)
The rest of the code looks good to me, thanks a lot for working on this! Quentin
Re: [PATCH bpf-next 1/3] bpftool: match several programs with same tag
In reply to Paul ChaignonQuoted 10 lines · Paul Chaignon
When several BPF programs have the same tag, bpftool matches only the first (in ID order). This patch changes that behavior such that dump and show commands return all matched programs. Commands that require a single program (e.g., pin and attach) will error out if given a tag that matches several. bpftool prog dump will also error out if file or visual are given and several programs have the given tag.
In the case of the dump command, a program header is added before each dump only if the tag matches several programs; this patch doesn't change the output if a single program matches.
How does this work? Could you add examples to the commit message?
This header idea doesn't seem correct, aren't id and other per-instance fields only printed once?
tools/bpf/bpftool/prog.c static int prog_fd_by_tag(unsigned char *tag) · line 131
Quoted 10 lines
Signed-off-by Paul Chaignon <[email protected]>tools/bpf/bpftool/prog.c +7
-1-close (fd); +if (nb_fds >0 ) { + tmp =realloc (fds, (nb_fds +1 ) *sizeof (int )); +if (!tmp) { +p_err ("failed to realloc" ); + goto err_close_fd; + } + fds = tmp;
How does this work? the new array is never returned to the caller, and the caller will most likely access freed memory, no?
Quoted 17 lines
tools/bpf/bpftool/prog.c +14
-0+ } + fds[nb_fds++] = fd; } + +err_close_fd: +close (fd); +err_close_fds: +for (nb_fds--; nb_fds >=0 ; nb_fds--) +close (fds[nb_fds]); +return -1 ; } +int prog_parse_fd (int *argc,char ***argv) +{ +int *fds =NULL ; +int nb_fds, fd; +
tools/bpf/bpftool/prog.c int prog_parse_fd(int *argc, char ***argv) · line 212
Quoted 10 lines
tools/bpf/bpftool/prog.c +10
-0+ fds =malloc (sizeof (int )); +if (!fds) { +p_err ("mem alloc failed" ); +return -1 ; + } + nb_fds =prog_parse_fds (argc, argv, fds); +if (nb_fds !=1 ) { +if (nb_fds >1 ) { +p_err ("several programs match this handle" ); +for (nb_fds--; nb_fds >=0 ; nb_fds--)
nit: since you checked nb_fds is positive, while (nb_fds--) ?
tools/bpf/bpftool/prog.c int prog_parse_fd(int *argc, char ***argv) · line 220
Quoted 8 lines
tools/bpf/bpftool/prog.c +8
-0+close (fds[nb_fds]); + } + fd =-1 ; + goto err_free; + } + + fd = fds[0 ]; +err_free:
nit: we tried to call the labels exit_xyz if the code is used on both
error and success path, but maybe that pattern got lost over time.
Quoted 33 lines
tools/bpf/bpftool/prog.c +13
-5+free (fds); +return fd; +} +static void show_prog_maps (int fd,u32 num_maps) {struct bpf_prog_info info = {};static int do_show (int argc,char **argv) { +int fd, nb_fds, i; +int *fds =NULL ;__u32 id =0 ;int err; -int fd;if (show_pinned)build_pinned_obj_table (&prog_table,BPF_OBJ_PROG );if (argc ==2 ) { - fd =prog_parse_fd (&argc, &argv); -if (fd <0 ) + fds =malloc (sizeof (int )); +if (!fds) { +p_err ("mem alloc failed" );return -1 ; + } + nb_fds =prog_parse_fds (&argc, &argv, fds); +if (nb_fds <1 ) + goto err_free; - err =show_prog (fd); -close (fd);
tools/bpf/bpftool/prog.c static int show_prog(int fd) · line 450
Quoted 10 lines
tools/bpf/bpftool/prog.c +9
-1-return err; +if (json_output && nb_fds >1 ) +jsonw_start_array (json_wtr);/* root array */ +for (i =0 ; i < nb_fds; i++) { + err =show_prog (fds[i]); +close (fds[i]); +if (err) { +for (i++; i < nb_fds; i++) +close (fds[i]); + goto err_free;
I'm 90% sure JSON arrays close/end themselves on exit 🤔
tools/bpf/bpftool/prog.c static int show_prog(int fd) · line 460
Quoted 10 lines
tools/bpf/bpftool/prog.c +10
-0+ } + } +if (json_output && nb_fds >1 ) +jsonw_end_array (json_wtr);/* root array */ + +return 0 ; + +err_free: +free (fds); +return -1 ;
Perhaps move the argc == 2 code to a separate function?
Quoted 6 lines
tools/bpf/bpftool/prog.c +0
-0}if (argc)@@ -408,101 +500,32 @@ static int do_show(int argc, char **argv)return err; }
Re: [PATCH bpf-next 2/3] bpftool: match programs by name
In reply to Paul ChaignonQuoted 11 lines · Paul Chaignon
When working with frequently modified BPF programs, both the ID and the tag may change. bpftool currently doesn't provide a "stable" way to match such programs.
This patch implements lookup by name for programs. The show and dump commands will return all programs with the given name, whereas other commands will error out if several programs have the same name.
Signed-off-by Paul Chaignon <[email protected]>
tools/bpf/bpftool/prog.c prog_parse_fds(int *argc, char ***argv, int *fds) · line 168
Quoted 5 lines
tools/bpf/bpftool/prog.c +1
-1@@ -164,7 +165,7 @@ prog_parse_fds(int *argc, char ***argv, int *fds) }return 1 ; }else if (is_prefix (**argv,"tag" )) { -unsigned char tag[BPF_TAG_SIZE ]; +char tag[BPF_TAG_SIZE ];
Perhaps better to change the argument to prog_fd_by_nametag() to void *?
Quoted 6 lines
tools/bpf/bpftool/prog.c +0
-0NEXT_ARGP ();@@ -176,7 +177,20 @@ prog_parse_fds(int *argc, char ***argv, int *fds) }NEXT_ARGP ();
tools/bpf/bpftool/prog.c prog_parse_fds(int *argc, char ***argv, int *fds) · line 187
Quoted 10 lines
tools/bpf/bpftool/prog.c +8
-1-return prog_fd_by_tag (tag, fds); +return prog_fd_by_nametag (tag, fds, true); + }else if (is_prefix (**argv,"name" )) { +char *name; + +NEXT_ARGP (); + + name = **argv; +if (strlen (name) >BPF_OBJ_NAME_LEN -1 ) {
Is this needed? strncmp will simply never match, is it preferred to hard error?
Quoted 18 lines
tools/bpf/bpftool/prog.c +7
-1+p_err ("can't parse name" ); +return -1 ; + } +NEXT_ARGP (); + +return prog_fd_by_nametag (name, fds, false); }else if (is_prefix (**argv,"pinned" )) {char *path;@@ -191,7 +205,7 @@ prog_parse_fds(int *argc, char ***argv, int *fds)return 1 ; } -p_err ("expected 'id', 'tag' or 'pinned', got: '%s'?" , **argv); +p_err ("expected 'id', 'tag', 'name' or 'pinned', got: '%s'?" , **argv);return -1 ; }
Re: [PATCH bpf-next 1/3] bpftool: match several programs with same tag
In reply to Jakub KicinskiQuoted 8 lines · Jakub Kicinski
Quoted 7 lines · Paul Chaignon
When several BPF programs have the same tag, bpftool matches only the first (in ID order). This patch changes that behavior such that dump and show commands return all matched programs. Commands that require a single program (e.g., pin and attach) will error out if given a tag that matches several. bpftool prog dump will also error out if file or visual are given and several programs have the given tag.
Quoted 8 lines
Quoted 3 lines
In the case of the dump command, a program header is added before each dump only if the tag matches several programs; this patch doesn't change the output if a single program matches.
How does this work? Could you add examples to the commit message?
This header idea doesn't seem correct, aren't id and other per-instance fields only printed once?
Sorry, that was unclear. What I call the header here is the first line from the prog show output (in the case of plain output). So the output when multiple programs match looks as follows. When a single program matches, the first line (with the ID, type, name, tag and license) is omitted.
$ ./bpftool prog dump xlated tag 6deef7357e7b4530 3: cgroup_skb tag 6deef7357e7b4530 gpl 0: (bf) r6 = r1 [...] 7: (95) exit
4: cgroup_skb tag 6deef7357e7b4530 gpl
0: (bf) r6 = r1
[...]
7: (95) exit
Quoted 14 lines
tools/bpf/bpftool/prog.c static int prog_fd_by_tag(unsigned char *tag) · line 131
Quoted 11 lines
Signed-off-by Paul Chaignon <[email protected]>tools/bpf/bpftool/prog.c +7
-1-close (fd); +if (nb_fds >0 ) { + tmp =realloc (fds, (nb_fds +1 ) *sizeof (int )); +if (!tmp) { +p_err ("failed to realloc" ); + goto err_close_fd; + } + fds = tmp;How does this work? the new array is never returned to the caller, and the caller will most likely access freed memory, no?
Oh, this is bad. Yes, fds should actually be "int **" and this line should be "*fds = tmp;". I'll fix it in v2.
[...]
Quoted 11 lines
tools/bpf/bpftool/prog.c int prog_parse_fd(int *argc, char ***argv) · line 220
Quoted 8 lines
tools/bpf/bpftool/prog.c +8
-0+close (fds[nb_fds]); + } + fd =-1 ; + goto err_free; + } + + fd = fds[0 ]; +err_free:nit: we tried to call the labels
exit_xyzif the code is used on both
error and success path, but maybe that pattern got lost over time.
Seems lost in prog.c but still valid across bpftool. I'll make the change.
[...]
Paul
Quoted 1 line
Re: [PATCH bpf-next 2/3] bpftool: match programs by name
In reply to Jakub KicinskiQuoted 27 lines · Jakub Kicinski
Quoted 11 lines · Paul Chaignon
When working with frequently modified BPF programs, both the ID and the tag may change. bpftool currently doesn't provide a "stable" way to match such programs.
This patch implements lookup by name for programs. The show and dump commands will return all programs with the given name, whereas other commands will error out if several programs have the same name.
Signed-off-by Paul Chaignon <[email protected]>tools/bpf/bpftool/prog.c prog_parse_fds(int *argc, char ***argv, int *fds) · line 168
Quoted 5 lines
tools/bpf/bpftool/prog.c +1
-1@@ -164,7 +165,7 @@ prog_parse_fds(int *argc, char ***argv, int *fds) }return 1 ; }else if (is_prefix (**argv,"tag" )) { -unsigned char tag[BPF_TAG_SIZE ]; +char tag[BPF_TAG_SIZE ];Perhaps better to change the argument to
prog_fd_by_nametag()to void *?Quoted 7 lines
tools/bpf/bpftool/prog.c +0
-0NEXT_ARGP ();@@ -176,7 +177,20 @@ prog_parse_fds(int *argc, char ***argv, int *fds) }NEXT_ARGP ();
Quoted 12 lines
tools/bpf/bpftool/prog.c prog_parse_fds(int *argc, char ***argv, int *fds) · line 187
Quoted 9 lines
tools/bpf/bpftool/prog.c +8
-1-return prog_fd_by_tag (tag, fds); +return prog_fd_by_nametag (tag, fds, true); + }else if (is_prefix (**argv,"name" )) { +char *name; + +NEXT_ARGP (); + + name = **argv; +if (strlen (name) >BPF_OBJ_NAME_LEN -1 ) {Is this needed? strncmp will simply never match, is it preferred to hard error?
I tried to follow the fail-early pattern of lookups by tag above. I do like that there's a different error message for a longer than expected name. Since libbpf silently truncates names, typing a longer name is not uncommon.
[...]
Paul
Re: [PATCH bpf-next 2/3] bpftool: match programs by name
Quoted 17 lines · Paul Chaignon
Quoted 4 lines
Quoted 4 lines
tools/bpf/bpftool/prog.c +0
-0@@ -176,7 +177,20 @@ prog_parse_fds(int *argc, char ***argv, int *fds) }NEXT_ARGP ();Quoted 12 lines
tools/bpf/bpftool/prog.c prog_parse_fds(int *argc, char ***argv, int *fds) · line 187
Quoted 9 lines
tools/bpf/bpftool/prog.c +8
-1-return prog_fd_by_tag (tag, fds); +return prog_fd_by_nametag (tag, fds, true); + }else if (is_prefix (**argv,"name" )) { +char *name; + +NEXT_ARGP (); + + name = **argv; +if (strlen (name) >BPF_OBJ_NAME_LEN -1 ) {Is this needed? strncmp will simply never match, is it preferred to hard error?
Quoted 1 line
I tried to follow the fail-early pattern of lookups by tag above.
Right although tag does a scanf and if we didn't scan all letters we'd use uninit memory.
Quoted 3 lines
I do like that there's a different error message for a longer than expected name. Since libbpf silently truncates names, typing a longer name is not uncommon.
Ugh, I didn't realize libbpf truncates names. Okay, let's keep the error for now so we can switch to truncation if users complain.
Re: [PATCH bpf-next 1/3] bpftool: match several programs with same tag
In reply to Quentin MonnetQuoted 20 lines · Quentin Monnet
Hi Paul,
2019-12-10 17:06 UTC+0100 ~ Paul Chaignon <[email protected]>
Quoted 17 lines
When several BPF programs have the same tag, bpftool matches only the first (in ID order). This patch changes that behavior such that dump and show commands return all matched programs. Commands that require a single program (e.g., pin and attach) will error out if given a tag that matches several. bpftool prog dump will also error out if file or visual are given and several programs have the given tag.
In the case of the dump command, a program header is added before each dump only if the tag matches several programs; this patch doesn't change the output if a single program matches.
Signed-off-by Paul Chaignon <[email protected]>--- .../bpftool/Documentation/bpftool-prog.rst | 16 +- tools/bpf/bpftool/prog.c | 371 ++++++++++++------ 2 files changed, 272 insertions(+), 115 deletions(-)
[...]
Quoted 34 lines
Quoted 34 lines
tools/bpf/bpftool/prog.c +15
-6@@ -351,21 +421,43 @@ static int show_prog(int fd)static int do_show (int argc,char **argv) { +int fd, nb_fds, i; +int *fds =NULL ;__u32 id =0 ;int err; -int fd;if (show_pinned)build_pinned_obj_table (&prog_table,BPF_OBJ_PROG );if (argc ==2 ) { - fd =prog_parse_fd (&argc, &argv); -if (fd <0 ) + fds =malloc (sizeof (int )); +if (!fds) { +p_err ("mem alloc failed" );return -1 ; + } + nb_fds =prog_parse_fds (&argc, &argv, fds); +if (nb_fds <1 ) + goto err_free; - err =show_prog (fd); -close (fd); -return err; +if (json_output && nb_fds >1 ) +jsonw_start_array (json_wtr);/* root array */ +for (i =0 ; i < nb_fds; i++) { + err =show_prog (fds[i]); +close (fds[i]); +if (err) {
Quoted 8 lines
tools/bpf/bpftool/prog.c static int show_prog(int fd) · line 450
Quoted 3 lines
tools/bpf/bpftool/prog.c +3
-0+for (i++; i < nb_fds; i++) +close (fds[i]); + goto err_free;Alternatively, we could keep trying to list the remaining programs. For example, if the system has a long list of BPF programs running and one of them is removed while printing the list, we would still have the rest of the list.
As discussed off the list, since the kernel keeps a refcount for programs, a program won't be removed while printing the list, not as long as we hold an fd to it. Unless there's another common case of failure, I'm going to keep the current behavior in the v2 and break out of the loop on errors (though, with the appropriate JSON array closing).
Quoted 17 lines
If we went this way, maybe just set err to non-zero if no program at all could be printed?
Quoted 13 lines
tools/bpf/bpftool/prog.c +10
-0+ } + } +if (json_output && nb_fds >1 ) +jsonw_end_array (json_wtr);/* root array */ + +return 0 ; + +err_free: +free (fds); +return -1 ; }if (argc)