tools: bpftool: add "prog run" subcommand to test-run programs
Subsystems (4)
PATCH v2 1/1 tools: bpftool: add "prog run" subcommand to test-run programs
Add a new "bpftool prog run" subcommand to run a loaded program on input data (and possibly with input context) passed by the user.
Print output data (and output context if relevant) into a file or into the console. Print return value and duration for the test run into the console.
A "repeat" argument can be passed to run the program several times in a row.
The command does not perform any kind of verification based on program type (Is this program type allowed to use an input context?) or on data consistency (Can I work with empty input data?), this is left to the kernel.
Example invocation:
# perl -e 'print "\x0" x 14' | ./bpftool prog run \
pinned /sys/fs/bpf/sample_ret0 \
data_in - data_out - repeat 5
0000000 0000 0000 0000 0000 0000 0000 0000 | ........ ......
Return value: 0, duration (average): 260nsWhen one of data_in or ctx_in is "-", bpftool reads from standard input, in binary format. Other formats (JSON, hexdump) might be supported (via an optional command line keyword like "data_fmt_in") in the future if relevant, but this would require doing more parsing in bpftool.
v2:
- Fix argument names for function check_single_stdin(). (Yonghong)
--- .../bpftool/Documentation/bpftool-prog.rst | 34 ++ tools/bpf/bpftool/bash-completion/bpftool | 28 +- tools/bpf/bpftool/main.c | 29 ++ tools/bpf/bpftool/main.h | 1 + tools/bpf/bpftool/prog.c | 348 +++++++++++++++++- tools/include/linux/sizes.h | 48 +++ 6 files changed, 485 insertions(+), 3 deletions(-) create mode 100644 tools/include/linux/sizes.h
tools/bpf/bpftool/Documentation/bpftool-prog.rst +34 -0
@@ -29,6 +29,7 @@ PROG COMMANDS | **bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*] | **bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*] | **bpftool** **prog tracelog** +| **bpftool** **prog run** *PROG* **data_in** *FILE* [**data_out** *FILE* [**data_size_out** *L*]] [**ctx_in** *FILE* [**ctx_out** *FILE* [**ctx_size_out** *M*]]] [**repeat** *N*] | **bpftool** **prog help** | | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
@@ -146,6 +147,39 @@ DESCRIPTION streaming data from BPF programs to user space, one can use perf events (see also **bpftool-map**\ (8)). + **bpftool prog run** *PROG* **data_in** *FILE* [**data_out** *FILE* [**data_size_out** *L*]] [**ctx_in** *FILE* [**ctx_out** *FILE* [**ctx_size_out** *M*]]] [**repeat** *N*] + Run BPF program *PROG* in the kernel testing infrastructure + for BPF, meaning that the program works on the data and + context provided by the user, and not on actual packets or + monitored functions etc. Return value and duration for the + test run are printed out to the console. + + Input data is read from the *FILE* passed with **data_in**. + If this *FILE* is "**-**", input data is read from standard + input. Input context, if any, is read from *FILE* passed with + **ctx_in**. Again, "**-**" can be used to read from standard + input, but only if standard input is not already in use for + input data. If a *FILE* is passed with **data_out**, output + data is written to that file. Similarly, output context is + written to the *FILE* passed with **ctx_out**. For both + output flows, "**-**" can be used to print to the standard + output (as plain text, or JSON if relevant option was + passed). If output keywords are omitted, output data and + context are discarded. Keywords **data_size_out** and + **ctx_size_out** are used to pass the size (in bytes) for the + output buffers to the kernel, although the default of 32 kB + should be more than enough for most cases. + + Keyword **repeat** is used to indicate the number of + consecutive runs to perform. Note that output data and + context printed to files correspond to the last of those + runs. The duration printed out at the end of the runs is an + average over all runs performed by the command. + + Not all program types support test run. Among those which do, + not all of them can take the **ctx_in**/**ctx_out** + arguments. bpftool does not perform checks on program types. + **bpftool prog help** Print short help message.
tools/bpf/bpftool/bash-completion/bpftool +26 -2
@@ -408,10 +408,34 @@ _bpftool() tracelog) return 0 ;; + run) + if [[ ${#words[@]} -lt 5 ]]; then + _filedir + return 0 + fi + case $prev in + id) + _bpftool_get_prog_ids + return 0 + ;; + data_in|data_out|ctx_in|ctx_out) + _filedir + return 0 + ;; + repeat|data_size_out|ctx_size_out) + return 0 + ;; + *) + _bpftool_once_attr 'data_in data_out data_size_out \ + ctx_in ctx_out ctx_size_out repeat' + return 0 + ;; + esac + ;; *) [[ $prev == $object ]] && \ - COMPREPLY=( $( compgen -W 'dump help pin attach detach load \ - show list tracelog' -- "$cur" ) ) + COMPREPLY=( $( compgen -W 'dump help pin attach detach \ + load show list tracelog run' -- "$cur" ) ) ;; esac ;;
tools/bpf/bpftool/main.c +29 -0
@@ -117,6 +117,35 @@ bool is_prefix(const char *pfx, const char *str)return !memcmp (str, pfx,strlen (pfx)); } +/* Last argument MUST be NULL pointer */ +int detect_common_prefix (const char *arg, ...) +{ +unsigned int count =0 ; +const char *ref; +char msg[256 ]; +va_list ap; + +snprintf (msg,sizeof (msg),"ambiguous prefix: '%s' could be '" , arg); +va_start (ap, arg); + while ((ref = va_arg(ap, const char *))) { +if (!is_prefix (arg, ref)) +continue ; + count++; +if (count >1 ) +strncat (msg,"' or '" ,sizeof (msg) -strlen (msg) -1 ); +strncat (msg, ref,sizeof (msg) -strlen (msg) -1 ); + } +va_end (ap); +strncat (msg,"'" ,sizeof (msg) -strlen (msg) -1 ); + +if (count >=2 ) { +p_err (msg); +return -1 ; + } + +return 0 ; +} +void fprint_hex (FILE *f,void *arg,unsigned int n,const char *sep) {unsigned char *data = arg;
tools/bpf/bpftool/main.h +1 -0
@@ -101,6 +101,7 @@ void p_err(const char *fmt, ...);void p_info (const char *fmt, ...);bool is_prefix (const char *pfx,const char *str); +int detect_common_prefix (const char *arg, ...);void fprint_hex (FILE *f,void *arg,unsigned int n,const char *sep);void usage (void )__noreturn ;
tools/bpf/bpftool/prog.c +347 -1
378 lines not on this page · raw message
tools/include/linux/sizes.h new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 29 lines · Quentin Monnet
Add a new "bpftool prog run" subcommand to run a loaded program on input data (and possibly with input context) passed by the user.
Print output data (and output context if relevant) into a file or into the console. Print return value and duration for the test run into the console.
A "repeat" argument can be passed to run the program several times in a row.
The command does not perform any kind of verification based on program type (Is this program type allowed to use an input context?) or on data consistency (Can I work with empty input data?), this is left to the kernel.
Example invocation:
# perl -e 'print "\x0" x 14' | ./bpftool prog run \ pinned /sys/fs/bpf/sample_ret0 \ data_in - data_out - repeat 5 0000000 0000 0000 0000 0000 0000 0000 0000 | ........ ...... Return value: 0, duration (average): 260nsWhen one of
data_inorctx_inis "-", bpftool reads from standard input, in binary format. Other formats (JSON, hexdump) might be supported (via an optional command line keyword like "data_fmt_in") in the future if relevant, but this would require doing more parsing in bpftool.
Quoted 5 lines
v2:
- Fix argument names for functioncheck_single_stdin(). (Yonghong)Signed-off-by Quentin Monnet <[email protected]>Reviewed-by Jakub Kicinski <[email protected]>
Daniel Borkmann
maintainerQuoted 28 lines · Quentin Monnet
Add a new "bpftool prog run" subcommand to run a loaded program on input data (and possibly with input context) passed by the user.
Print output data (and output context if relevant) into a file or into the console. Print return value and duration for the test run into the console.
A "repeat" argument can be passed to run the program several times in a row.
The command does not perform any kind of verification based on program type (Is this program type allowed to use an input context?) or on data consistency (Can I work with empty input data?), this is left to the kernel.
Example invocation:
# perl -e 'print "\x0" x 14' | ./bpftool prog run \ pinned /sys/fs/bpf/sample_ret0 \ data_in - data_out - repeat 5 0000000 0000 0000 0000 0000 0000 0000 0000 | ........ ...... Return value: 0, duration (average): 260nsWhen one of
data_inorctx_inis "-", bpftool reads from standard input, in binary format. Other formats (JSON, hexdump) might be supported (via an optional command line keyword like "data_fmt_in") in the future if relevant, but this would require doing more parsing in bpftool.
Quoted 5 lines
v2:
- Fix argument names for functioncheck_single_stdin(). (Yonghong)Signed-off-by Quentin Monnet <[email protected]>Reviewed-by Jakub Kicinski <[email protected]>
Looks great, thanks for adding, applied!