diff mbox series

[bpf-next,v2,07/11] selftests/xsk: add option to run single test

Message ID 20230824122853.3494-8-magnus.karlsson@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series seltests/xsk: various improvements to xskxceiver | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-28 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers warning 5 maintainers not CCed: shuah@kernel.org yonghong.song@linux.dev mykolal@fb.com linux-kselftest@vger.kernel.org jonathan.lemon@gmail.com
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success net selftest script(s) already in Makefile
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns WARNING: line length of 88 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain_full }}
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for veristat

Commit Message

Magnus Karlsson Aug. 24, 2023, 12:28 p.m. UTC
From: Magnus Karlsson <magnus.karlsson@intel.com>

Add a command line option to be able to run a single test. This option
(-t) takes a number from the list of tests available with the "-l"
option. Here are two examples:

Run test number 2, the "receive single packet" test in all available modes:

./test_xsk.sh -t 2

Run test number 21, the metadata copy test in skb mode only

./test_xsh.sh -t 21 -m skb

Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
---
 tools/testing/selftests/bpf/test_xsk.sh  | 10 +++-
 tools/testing/selftests/bpf/xskxceiver.c | 58 ++++++++++++++++--------
 tools/testing/selftests/bpf/xskxceiver.h |  3 ++
 3 files changed, 52 insertions(+), 19 deletions(-)

Comments

Fijalkowski, Maciej Aug. 25, 2023, 12:44 p.m. UTC | #1
On Thu, Aug 24, 2023 at 02:28:49PM +0200, Magnus Karlsson wrote:
> From: Magnus Karlsson <magnus.karlsson@intel.com>
> 
> Add a command line option to be able to run a single test. This option
> (-t) takes a number from the list of tests available with the "-l"
> option. Here are two examples:
> 
> Run test number 2, the "receive single packet" test in all available modes:
> 
> ./test_xsk.sh -t 2
> 
> Run test number 21, the metadata copy test in skb mode only
> 
> ./test_xsh.sh -t 21 -m skb
> 
> Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
> ---
>  tools/testing/selftests/bpf/test_xsk.sh  | 10 +++-
>  tools/testing/selftests/bpf/xskxceiver.c | 58 ++++++++++++++++--------
>  tools/testing/selftests/bpf/xskxceiver.h |  3 ++
>  3 files changed, 52 insertions(+), 19 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/test_xsk.sh b/tools/testing/selftests/bpf/test_xsk.sh
> index 489101922984..b7186ae48497 100755
> --- a/tools/testing/selftests/bpf/test_xsk.sh
> +++ b/tools/testing/selftests/bpf/test_xsk.sh
> @@ -79,12 +79,15 @@
>  #
>  # List available tests
>  #   ./test_xsk.sh -l
> +#
> +# Run a specific test from the test suite
> +#   sudo ./test_xsk.sh -t TEST_NAME
>  
>  . xsk_prereqs.sh
>  
>  ETH=""
>  
> -while getopts "vi:dm:l" flag
> +while getopts "vi:dm:lt:" flag
>  do
>  	case "${flag}" in
>  		v) verbose=1;;
> @@ -92,6 +95,7 @@ do
>  		i) ETH=${OPTARG};;
>  		m) XSKTEST_MODE=${OPTARG};;
>  		l) list=1;;
> +		t) XSKTEST_TEST=${OPTARG};;
>  	esac
>  done
>  
> @@ -170,6 +174,10 @@ if [ -n "$XSKTEST_MODE" ]; then
>  	ARGS+="-m ${XSKTEST_MODE} "
>  fi
>  
> +if [ -n "$XSKTEST_TEST" ]; then
> +	ARGS+="-t ${XSKTEST_TEST} "
> +fi
> +
>  retval=$?
>  test_status $retval "${TEST_NAME}"
>  
> diff --git a/tools/testing/selftests/bpf/xskxceiver.c b/tools/testing/selftests/bpf/xskxceiver.c
> index a063b9af7fff..6eca5f95a3e0 100644
> --- a/tools/testing/selftests/bpf/xskxceiver.c
> +++ b/tools/testing/selftests/bpf/xskxceiver.c
> @@ -110,6 +110,7 @@ static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61";
>  static bool opt_verbose;
>  static bool opt_print_tests;
>  static enum test_mode opt_mode = TEST_MODE_ALL;
> +static u32 opt_run_test = RUN_ALL_TESTS;
>  
>  static void __exit_with_error(int error, const char *file, const char *func, int line)
>  {
> @@ -316,10 +317,11 @@ static struct option long_options[] = {
>  	{"verbose", no_argument, 0, 'v'},
>  	{"mode", required_argument, 0, 'm'},
>  	{"list", no_argument, 0, 'l'},
> +	{"test", required_argument, 0, 't'},
>  	{0, 0, 0, 0}
>  };
>  
> -static void usage(const char *prog)
> +static void print_usage(char **argv)
>  {
>  	const char *str =
>  		"  Usage: xskxceiver [OPTIONS]\n"
> @@ -328,9 +330,11 @@ static void usage(const char *prog)
>  		"  -v, --verbose        Verbose output\n"
>  		"  -b, --busy-poll      Enable busy poll\n"
>  		"  -m, --mode           Run only mode skb, drv, or zc\n"
> -		"  -l, --list           List all available tests\n";
> +		"  -l, --list           List all available tests\n"
> +		"  -t, --test           Run a specific test. Enter number from -l option.\n";
>  
> -	ksft_print_msg(str, prog);
> +	ksft_print_msg(str, basename(argv[0]));
> +	ksft_exit_xfail();
>  }
>  
>  static bool validate_interface(struct ifobject *ifobj)
> @@ -350,7 +354,7 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
>  	opterr = 0;
>  
>  	for (;;) {
> -		c = getopt_long(argc, argv, "i:vbm:l", long_options, &option_index);
> +		c = getopt_long(argc, argv, "i:vbm:lt:", long_options, &option_index);
>  		if (c == -1)
>  			break;
>  
> @@ -390,16 +394,20 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
>  								strlen("zc")))) {
>  				opt_mode = TEST_MODE_ZC;
>  			} else {
> -				usage(basename(argv[0]));
> -				ksft_exit_xfail();
> +				print_usage(argv);
>  			}
>  			break;
>  		case 'l':
>  			opt_print_tests = true;
>  			break;
> +		case 't':
> +			errno = 0;
> +			opt_run_test = strtol(optarg, NULL, 0);
> +			if (errno)
> +				print_usage(argv);

aren't you missing ksft_exit_xfail(); here?

> +			break;
>  		default:
> -			usage(basename(argv[0]));
> -			ksft_exit_xfail();
> +			print_usage(argv);
>  		}
>  	}
>  }
> @@ -2330,8 +2338,8 @@ int main(int argc, char **argv)
>  	struct pkt_stream *rx_pkt_stream_default;
>  	struct pkt_stream *tx_pkt_stream_default;
>  	struct ifobject *ifobj_tx, *ifobj_rx;
> +	u32 i, j, failed_tests = 0, nb_tests;
>  	int modes = TEST_MODE_SKB + 1;
> -	u32 i, j, failed_tests = 0;
>  	struct test_spec test;
>  	bool shared_netdev;
>  
> @@ -2353,15 +2361,17 @@ int main(int argc, char **argv)
>  		print_tests();
>  		ksft_exit_xpass();
>  	}
> +	if (opt_run_test != RUN_ALL_TESTS && opt_run_test >= ARRAY_SIZE(tests)) {
> +		ksft_print_msg("Error: test %u does not exist.\n", opt_run_test);
> +		ksft_exit_xfail();
> +	}
>  
>  	shared_netdev = (ifobj_tx->ifindex == ifobj_rx->ifindex);
>  	ifobj_tx->shared_umem = shared_netdev;
>  	ifobj_rx->shared_umem = shared_netdev;
>  
> -	if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx)) {
> -		usage(basename(argv[0]));
> -		ksft_exit_xfail();
> -	}
> +	if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx))
> +		print_usage(argv);
>  
>  	if (is_xdp_supported(ifobj_tx->ifindex)) {
>  		modes++;
> @@ -2380,19 +2390,31 @@ int main(int argc, char **argv)
>  	test.tx_pkt_stream_default = tx_pkt_stream_default;
>  	test.rx_pkt_stream_default = rx_pkt_stream_default;
>  
> +	if (opt_run_test == RUN_ALL_TESTS)
> +		nb_tests = ARRAY_SIZE(tests);
> +	else
> +		nb_tests = 1;
>  	if (opt_mode == TEST_MODE_ALL)
> -		ksft_set_plan(modes * ARRAY_SIZE(tests));
> +		ksft_set_plan(modes * nb_tests);
>  	else
> -		ksft_set_plan(ARRAY_SIZE(tests));
> +		ksft_set_plan(nb_tests);
>  
>  	for (i = 0; i < modes; i++) {
>  		if (opt_mode != TEST_MODE_ALL && i != opt_mode)
>  			continue;
>  
> -		for (j = 0; j < ARRAY_SIZE(tests); j++) {
> -			test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]);
> +		if (opt_run_test == RUN_ALL_TESTS) {
> +			for (j = 0; j < ARRAY_SIZE(tests); j++) {
> +				test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]);
> +				run_pkt_test(&test);
> +				usleep(USLEEP_MAX);
> +
> +				if (test.fail)
> +					failed_tests++;
> +			}
> +		} else {
> +			test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[opt_run_test]);
>  			run_pkt_test(&test);
> -			usleep(USLEEP_MAX);

Could we simplify this and avoid branching just for getting the index of
test to run with something like:

		for (curr_test = opt_run_test; j = 0; j < nb_tests; j++, curr_test++) {

branch vs additional variable, up to you but repeated code caught my eye

>  
>  			if (test.fail)
>  				failed_tests++;
> diff --git a/tools/testing/selftests/bpf/xskxceiver.h b/tools/testing/selftests/bpf/xskxceiver.h
> index 3a71d490db3e..8015aeea839d 100644
> --- a/tools/testing/selftests/bpf/xskxceiver.h
> +++ b/tools/testing/selftests/bpf/xskxceiver.h
> @@ -5,6 +5,8 @@
>  #ifndef XSKXCEIVER_H_
>  #define XSKXCEIVER_H_
>  
> +#include <limits.h>
> +
>  #include "xsk_xdp_progs.skel.h"
>  
>  #ifndef SOL_XDP
> @@ -56,6 +58,7 @@
>  #define XSK_DESC__MAX_SKB_FRAGS 18
>  #define HUGEPAGE_SIZE (2 * 1024 * 1024)
>  #define PKT_DUMP_NB_TO_PRINT 16
> +#define RUN_ALL_TESTS UINT_MAX
>  
>  #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
>  
> -- 
> 2.34.1
>
Magnus Karlsson Aug. 25, 2023, 12:50 p.m. UTC | #2
On Fri, 25 Aug 2023 at 14:45, Maciej Fijalkowski
<maciej.fijalkowski@intel.com> wrote:
>
> On Thu, Aug 24, 2023 at 02:28:49PM +0200, Magnus Karlsson wrote:
> > From: Magnus Karlsson <magnus.karlsson@intel.com>
> >
> > Add a command line option to be able to run a single test. This option
> > (-t) takes a number from the list of tests available with the "-l"
> > option. Here are two examples:
> >
> > Run test number 2, the "receive single packet" test in all available modes:
> >
> > ./test_xsk.sh -t 2
> >
> > Run test number 21, the metadata copy test in skb mode only
> >
> > ./test_xsh.sh -t 21 -m skb
> >
> > Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
> > ---
> >  tools/testing/selftests/bpf/test_xsk.sh  | 10 +++-
> >  tools/testing/selftests/bpf/xskxceiver.c | 58 ++++++++++++++++--------
> >  tools/testing/selftests/bpf/xskxceiver.h |  3 ++
> >  3 files changed, 52 insertions(+), 19 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/test_xsk.sh b/tools/testing/selftests/bpf/test_xsk.sh
> > index 489101922984..b7186ae48497 100755
> > --- a/tools/testing/selftests/bpf/test_xsk.sh
> > +++ b/tools/testing/selftests/bpf/test_xsk.sh
> > @@ -79,12 +79,15 @@
> >  #
> >  # List available tests
> >  #   ./test_xsk.sh -l
> > +#
> > +# Run a specific test from the test suite
> > +#   sudo ./test_xsk.sh -t TEST_NAME
> >
> >  . xsk_prereqs.sh
> >
> >  ETH=""
> >
> > -while getopts "vi:dm:l" flag
> > +while getopts "vi:dm:lt:" flag
> >  do
> >       case "${flag}" in
> >               v) verbose=1;;
> > @@ -92,6 +95,7 @@ do
> >               i) ETH=${OPTARG};;
> >               m) XSKTEST_MODE=${OPTARG};;
> >               l) list=1;;
> > +             t) XSKTEST_TEST=${OPTARG};;
> >       esac
> >  done
> >
> > @@ -170,6 +174,10 @@ if [ -n "$XSKTEST_MODE" ]; then
> >       ARGS+="-m ${XSKTEST_MODE} "
> >  fi
> >
> > +if [ -n "$XSKTEST_TEST" ]; then
> > +     ARGS+="-t ${XSKTEST_TEST} "
> > +fi
> > +
> >  retval=$?
> >  test_status $retval "${TEST_NAME}"
> >
> > diff --git a/tools/testing/selftests/bpf/xskxceiver.c b/tools/testing/selftests/bpf/xskxceiver.c
> > index a063b9af7fff..6eca5f95a3e0 100644
> > --- a/tools/testing/selftests/bpf/xskxceiver.c
> > +++ b/tools/testing/selftests/bpf/xskxceiver.c
> > @@ -110,6 +110,7 @@ static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61";
> >  static bool opt_verbose;
> >  static bool opt_print_tests;
> >  static enum test_mode opt_mode = TEST_MODE_ALL;
> > +static u32 opt_run_test = RUN_ALL_TESTS;
> >
> >  static void __exit_with_error(int error, const char *file, const char *func, int line)
> >  {
> > @@ -316,10 +317,11 @@ static struct option long_options[] = {
> >       {"verbose", no_argument, 0, 'v'},
> >       {"mode", required_argument, 0, 'm'},
> >       {"list", no_argument, 0, 'l'},
> > +     {"test", required_argument, 0, 't'},
> >       {0, 0, 0, 0}
> >  };
> >
> > -static void usage(const char *prog)
> > +static void print_usage(char **argv)
> >  {
> >       const char *str =
> >               "  Usage: xskxceiver [OPTIONS]\n"
> > @@ -328,9 +330,11 @@ static void usage(const char *prog)
> >               "  -v, --verbose        Verbose output\n"
> >               "  -b, --busy-poll      Enable busy poll\n"
> >               "  -m, --mode           Run only mode skb, drv, or zc\n"
> > -             "  -l, --list           List all available tests\n";
> > +             "  -l, --list           List all available tests\n"
> > +             "  -t, --test           Run a specific test. Enter number from -l option.\n";
> >
> > -     ksft_print_msg(str, prog);
> > +     ksft_print_msg(str, basename(argv[0]));
> > +     ksft_exit_xfail();
> >  }
> >
> >  static bool validate_interface(struct ifobject *ifobj)
> > @@ -350,7 +354,7 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
> >       opterr = 0;
> >
> >       for (;;) {
> > -             c = getopt_long(argc, argv, "i:vbm:l", long_options, &option_index);
> > +             c = getopt_long(argc, argv, "i:vbm:lt:", long_options, &option_index);
> >               if (c == -1)
> >                       break;
> >
> > @@ -390,16 +394,20 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
> >                                                               strlen("zc")))) {
> >                               opt_mode = TEST_MODE_ZC;
> >                       } else {
> > -                             usage(basename(argv[0]));
> > -                             ksft_exit_xfail();
> > +                             print_usage(argv);
> >                       }
> >                       break;
> >               case 'l':
> >                       opt_print_tests = true;
> >                       break;
> > +             case 't':
> > +                     errno = 0;
> > +                     opt_run_test = strtol(optarg, NULL, 0);
> > +                     if (errno)
> > +                             print_usage(argv);
>
> aren't you missing ksft_exit_xfail(); here?

It is in print_usage() instead since all calls to print_usage() ends that way.

> > +                     break;
> >               default:
> > -                     usage(basename(argv[0]));
> > -                     ksft_exit_xfail();
> > +                     print_usage(argv);
> >               }
> >       }
> >  }
> > @@ -2330,8 +2338,8 @@ int main(int argc, char **argv)
> >       struct pkt_stream *rx_pkt_stream_default;
> >       struct pkt_stream *tx_pkt_stream_default;
> >       struct ifobject *ifobj_tx, *ifobj_rx;
> > +     u32 i, j, failed_tests = 0, nb_tests;
> >       int modes = TEST_MODE_SKB + 1;
> > -     u32 i, j, failed_tests = 0;
> >       struct test_spec test;
> >       bool shared_netdev;
> >
> > @@ -2353,15 +2361,17 @@ int main(int argc, char **argv)
> >               print_tests();
> >               ksft_exit_xpass();
> >       }
> > +     if (opt_run_test != RUN_ALL_TESTS && opt_run_test >= ARRAY_SIZE(tests)) {
> > +             ksft_print_msg("Error: test %u does not exist.\n", opt_run_test);
> > +             ksft_exit_xfail();
> > +     }
> >
> >       shared_netdev = (ifobj_tx->ifindex == ifobj_rx->ifindex);
> >       ifobj_tx->shared_umem = shared_netdev;
> >       ifobj_rx->shared_umem = shared_netdev;
> >
> > -     if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx)) {
> > -             usage(basename(argv[0]));
> > -             ksft_exit_xfail();
> > -     }
> > +     if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx))
> > +             print_usage(argv);
> >
> >       if (is_xdp_supported(ifobj_tx->ifindex)) {
> >               modes++;
> > @@ -2380,19 +2390,31 @@ int main(int argc, char **argv)
> >       test.tx_pkt_stream_default = tx_pkt_stream_default;
> >       test.rx_pkt_stream_default = rx_pkt_stream_default;
> >
> > +     if (opt_run_test == RUN_ALL_TESTS)
> > +             nb_tests = ARRAY_SIZE(tests);
> > +     else
> > +             nb_tests = 1;
> >       if (opt_mode == TEST_MODE_ALL)
> > -             ksft_set_plan(modes * ARRAY_SIZE(tests));
> > +             ksft_set_plan(modes * nb_tests);
> >       else
> > -             ksft_set_plan(ARRAY_SIZE(tests));
> > +             ksft_set_plan(nb_tests);
> >
> >       for (i = 0; i < modes; i++) {
> >               if (opt_mode != TEST_MODE_ALL && i != opt_mode)
> >                       continue;
> >
> > -             for (j = 0; j < ARRAY_SIZE(tests); j++) {
> > -                     test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]);
> > +             if (opt_run_test == RUN_ALL_TESTS) {
> > +                     for (j = 0; j < ARRAY_SIZE(tests); j++) {
> > +                             test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]);
> > +                             run_pkt_test(&test);
> > +                             usleep(USLEEP_MAX);
> > +
> > +                             if (test.fail)
> > +                                     failed_tests++;
> > +                     }
> > +             } else {
> > +                     test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[opt_run_test]);
> >                       run_pkt_test(&test);
> > -                     usleep(USLEEP_MAX);
>
> Could we simplify this and avoid branching just for getting the index of
> test to run with something like:
>
>                 for (curr_test = opt_run_test; j = 0; j < nb_tests; j++, curr_test++) {
>
> branch vs additional variable, up to you but repeated code caught my eye

Would be nicer without repeated code for sure. I will try to simplify it.

> >
> >                       if (test.fail)
> >                               failed_tests++;
> > diff --git a/tools/testing/selftests/bpf/xskxceiver.h b/tools/testing/selftests/bpf/xskxceiver.h
> > index 3a71d490db3e..8015aeea839d 100644
> > --- a/tools/testing/selftests/bpf/xskxceiver.h
> > +++ b/tools/testing/selftests/bpf/xskxceiver.h
> > @@ -5,6 +5,8 @@
> >  #ifndef XSKXCEIVER_H_
> >  #define XSKXCEIVER_H_
> >
> > +#include <limits.h>
> > +
> >  #include "xsk_xdp_progs.skel.h"
> >
> >  #ifndef SOL_XDP
> > @@ -56,6 +58,7 @@
> >  #define XSK_DESC__MAX_SKB_FRAGS 18
> >  #define HUGEPAGE_SIZE (2 * 1024 * 1024)
> >  #define PKT_DUMP_NB_TO_PRINT 16
> > +#define RUN_ALL_TESTS UINT_MAX
> >
> >  #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
> >
> > --
> > 2.34.1
> >
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/test_xsk.sh b/tools/testing/selftests/bpf/test_xsk.sh
index 489101922984..b7186ae48497 100755
--- a/tools/testing/selftests/bpf/test_xsk.sh
+++ b/tools/testing/selftests/bpf/test_xsk.sh
@@ -79,12 +79,15 @@ 
 #
 # List available tests
 #   ./test_xsk.sh -l
+#
+# Run a specific test from the test suite
+#   sudo ./test_xsk.sh -t TEST_NAME
 
 . xsk_prereqs.sh
 
 ETH=""
 
-while getopts "vi:dm:l" flag
+while getopts "vi:dm:lt:" flag
 do
 	case "${flag}" in
 		v) verbose=1;;
@@ -92,6 +95,7 @@  do
 		i) ETH=${OPTARG};;
 		m) XSKTEST_MODE=${OPTARG};;
 		l) list=1;;
+		t) XSKTEST_TEST=${OPTARG};;
 	esac
 done
 
@@ -170,6 +174,10 @@  if [ -n "$XSKTEST_MODE" ]; then
 	ARGS+="-m ${XSKTEST_MODE} "
 fi
 
+if [ -n "$XSKTEST_TEST" ]; then
+	ARGS+="-t ${XSKTEST_TEST} "
+fi
+
 retval=$?
 test_status $retval "${TEST_NAME}"
 
diff --git a/tools/testing/selftests/bpf/xskxceiver.c b/tools/testing/selftests/bpf/xskxceiver.c
index a063b9af7fff..6eca5f95a3e0 100644
--- a/tools/testing/selftests/bpf/xskxceiver.c
+++ b/tools/testing/selftests/bpf/xskxceiver.c
@@ -110,6 +110,7 @@  static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61";
 static bool opt_verbose;
 static bool opt_print_tests;
 static enum test_mode opt_mode = TEST_MODE_ALL;
+static u32 opt_run_test = RUN_ALL_TESTS;
 
 static void __exit_with_error(int error, const char *file, const char *func, int line)
 {
@@ -316,10 +317,11 @@  static struct option long_options[] = {
 	{"verbose", no_argument, 0, 'v'},
 	{"mode", required_argument, 0, 'm'},
 	{"list", no_argument, 0, 'l'},
+	{"test", required_argument, 0, 't'},
 	{0, 0, 0, 0}
 };
 
-static void usage(const char *prog)
+static void print_usage(char **argv)
 {
 	const char *str =
 		"  Usage: xskxceiver [OPTIONS]\n"
@@ -328,9 +330,11 @@  static void usage(const char *prog)
 		"  -v, --verbose        Verbose output\n"
 		"  -b, --busy-poll      Enable busy poll\n"
 		"  -m, --mode           Run only mode skb, drv, or zc\n"
-		"  -l, --list           List all available tests\n";
+		"  -l, --list           List all available tests\n"
+		"  -t, --test           Run a specific test. Enter number from -l option.\n";
 
-	ksft_print_msg(str, prog);
+	ksft_print_msg(str, basename(argv[0]));
+	ksft_exit_xfail();
 }
 
 static bool validate_interface(struct ifobject *ifobj)
@@ -350,7 +354,7 @@  static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
 	opterr = 0;
 
 	for (;;) {
-		c = getopt_long(argc, argv, "i:vbm:l", long_options, &option_index);
+		c = getopt_long(argc, argv, "i:vbm:lt:", long_options, &option_index);
 		if (c == -1)
 			break;
 
@@ -390,16 +394,20 @@  static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
 								strlen("zc")))) {
 				opt_mode = TEST_MODE_ZC;
 			} else {
-				usage(basename(argv[0]));
-				ksft_exit_xfail();
+				print_usage(argv);
 			}
 			break;
 		case 'l':
 			opt_print_tests = true;
 			break;
+		case 't':
+			errno = 0;
+			opt_run_test = strtol(optarg, NULL, 0);
+			if (errno)
+				print_usage(argv);
+			break;
 		default:
-			usage(basename(argv[0]));
-			ksft_exit_xfail();
+			print_usage(argv);
 		}
 	}
 }
@@ -2330,8 +2338,8 @@  int main(int argc, char **argv)
 	struct pkt_stream *rx_pkt_stream_default;
 	struct pkt_stream *tx_pkt_stream_default;
 	struct ifobject *ifobj_tx, *ifobj_rx;
+	u32 i, j, failed_tests = 0, nb_tests;
 	int modes = TEST_MODE_SKB + 1;
-	u32 i, j, failed_tests = 0;
 	struct test_spec test;
 	bool shared_netdev;
 
@@ -2353,15 +2361,17 @@  int main(int argc, char **argv)
 		print_tests();
 		ksft_exit_xpass();
 	}
+	if (opt_run_test != RUN_ALL_TESTS && opt_run_test >= ARRAY_SIZE(tests)) {
+		ksft_print_msg("Error: test %u does not exist.\n", opt_run_test);
+		ksft_exit_xfail();
+	}
 
 	shared_netdev = (ifobj_tx->ifindex == ifobj_rx->ifindex);
 	ifobj_tx->shared_umem = shared_netdev;
 	ifobj_rx->shared_umem = shared_netdev;
 
-	if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx)) {
-		usage(basename(argv[0]));
-		ksft_exit_xfail();
-	}
+	if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx))
+		print_usage(argv);
 
 	if (is_xdp_supported(ifobj_tx->ifindex)) {
 		modes++;
@@ -2380,19 +2390,31 @@  int main(int argc, char **argv)
 	test.tx_pkt_stream_default = tx_pkt_stream_default;
 	test.rx_pkt_stream_default = rx_pkt_stream_default;
 
+	if (opt_run_test == RUN_ALL_TESTS)
+		nb_tests = ARRAY_SIZE(tests);
+	else
+		nb_tests = 1;
 	if (opt_mode == TEST_MODE_ALL)
-		ksft_set_plan(modes * ARRAY_SIZE(tests));
+		ksft_set_plan(modes * nb_tests);
 	else
-		ksft_set_plan(ARRAY_SIZE(tests));
+		ksft_set_plan(nb_tests);
 
 	for (i = 0; i < modes; i++) {
 		if (opt_mode != TEST_MODE_ALL && i != opt_mode)
 			continue;
 
-		for (j = 0; j < ARRAY_SIZE(tests); j++) {
-			test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]);
+		if (opt_run_test == RUN_ALL_TESTS) {
+			for (j = 0; j < ARRAY_SIZE(tests); j++) {
+				test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]);
+				run_pkt_test(&test);
+				usleep(USLEEP_MAX);
+
+				if (test.fail)
+					failed_tests++;
+			}
+		} else {
+			test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[opt_run_test]);
 			run_pkt_test(&test);
-			usleep(USLEEP_MAX);
 
 			if (test.fail)
 				failed_tests++;
diff --git a/tools/testing/selftests/bpf/xskxceiver.h b/tools/testing/selftests/bpf/xskxceiver.h
index 3a71d490db3e..8015aeea839d 100644
--- a/tools/testing/selftests/bpf/xskxceiver.h
+++ b/tools/testing/selftests/bpf/xskxceiver.h
@@ -5,6 +5,8 @@ 
 #ifndef XSKXCEIVER_H_
 #define XSKXCEIVER_H_
 
+#include <limits.h>
+
 #include "xsk_xdp_progs.skel.h"
 
 #ifndef SOL_XDP
@@ -56,6 +58,7 @@ 
 #define XSK_DESC__MAX_SKB_FRAGS 18
 #define HUGEPAGE_SIZE (2 * 1024 * 1024)
 #define PKT_DUMP_NB_TO_PRINT 16
+#define RUN_ALL_TESTS UINT_MAX
 
 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)