[PATCH 4/4] app/testpmd: enhance getopt_long usage
Ferruh Yigit
ferruh.yigit at amd.com
Tue Mar 12 18:03:00 CET 2024
On 3/8/2024 2:48 PM, David Marchand wrote:
> This is a cleanup similar to previous ones in EAL and examples.
> Instead of using strcmp for every long options while getopt_long already
> did such parsing, rely on getopt_long return value.
>
> Note for reviewers: this patch is best reviewed once applied locally and
> displayed with git show -w.
>
Thanks for the cleanup, it was needed.
> Signed-off-by: David Marchand <david.marchand at redhat.com>
> ---
> app/test-pmd/parameters.c | 1928 +++++++++++++++++++++----------------
> 1 file changed, 1084 insertions(+), 844 deletions(-)
>
> diff --git a/app/test-pmd/parameters.c b/app/test-pmd/parameters.c
> index 271f0c995a..58f43cb5a8 100644
> --- a/app/test-pmd/parameters.c
> +++ b/app/test-pmd/parameters.c
> @@ -40,6 +40,346 @@
>
> #include "testpmd.h"
>
> +enum {
> + /* long options mapped to a short option */
> +#define TESTPMD_OPT_AUTO_START "auto-start"
> + TESTPMD_OPT_AUTO_START_NUM = 'a',
> +#define TESTPMD_OPT_HELP "help"
> + TESTPMD_OPT_HELP_NUM = 'h',
> +#define TESTPMD_OPT_INTERACTIVE "interactive"
> + TESTPMD_OPT_INTERACTIVE_NUM = 'i',
> +
> + /* first long only option value must be >= 256 */
> + TESTPMD_OPT_LONG_MIN_NUM = 256,
>
This is to be able to use all short options, not sure if this requires a
comment.
<...>
> +};
> +
> +static struct option long_options[] = {
>
Can it be constant?
> + { TESTPMD_OPT_AUTO_START, 0, NULL, TESTPMD_OPT_AUTO_START_NUM },
>
In original version "auto-start" long version is enabled only if
'RTE_LIB_CMDLINE' enabled, not sure why, but not for short version, I
think there is a confusion.
OK to remove RTE_LIB_CMDLINE requirement for 'auto-start' but perhaps
better to have it in separate patch, what do you think?
> + { TESTPMD_OPT_HELP, 0, NULL, TESTPMD_OPT_HELP_NUM },
> +#ifdef RTE_LIB_CMDLINE
> + { TESTPMD_OPT_INTERACTIVE, 0, NULL, TESTPMD_OPT_INTERACTIVE_NUM },
> + { TESTPMD_OPT_CMDLINE_FILE, 1, NULL, TESTPMD_OPT_CMDLINE_FILE},
>
What about using 'no_argument' & 'required_argument' instead of '0' &
'1', but I guess it will make lines too big,
as flag will be always NULL in this approach, what about defining macros
for arg and no_arg, like:
```
#define ARG(name) (name), required_argument, NULL, name##_NUM
#define NOARG(name) (name), no_argument, NULL, name##_NUM
#define OPTARG(name) (name), optional_argument, NULL, name##_NUM
{ NOARG(TESTPMD_OPT_INTERACTIVE) },
{ ARG(TESTPMD_OPT_CMDLINE_FILE) },
```
> + { TESTPMD_OPT_ETH_PEERS_CONFIGFILE, 1, NULL, TESTPMD_OPT_ETH_PEERS_CONFIGFILE_NUM },
> + { TESTPMD_OPT_ETH_PEER, 1, NULL, TESTPMD_OPT_ETH_PEER_NUM },
>
These long options are within "#ifdef RTE_LIB_CMDLINE" block, I don't
know why, I guess above two can work without cmdline library.
<...>
> -
> argvopt = argv;
>
Why 'argvopt' is required, why not use 'argv' directly?
<...>
> #ifdef RTE_LIB_CMDLINE
> ...
> + case TESTPMD_OPT_ETH_PEER_NUM: {
> + char *port_end;
>
> - if (rte_ether_unformat_addr(port_end,
> - &peer_eth_addrs[n]) < 0)
> - rte_exit(EXIT_FAILURE,
> - "Invalid ethernet address: %s\n",
> - port_end);
> - nb_peer_eth_addrs++;
> - }
> + errno = 0;
> + n = strtoul(optarg, &port_end, 10);
> + if (errno != 0 || port_end == optarg || *port_end++ != ',')
> + rte_exit(EXIT_FAILURE,
> + "Invalid eth-peer: %s", optarg);
> + if (n >= RTE_MAX_ETHPORTS)
> + rte_exit(EXIT_FAILURE,
> + "eth-peer: port %d >= RTE_MAX_ETHPORTS(%d)\n",
> + n, RTE_MAX_ETHPORTS);
> +
> + if (rte_ether_unformat_addr(port_end,
> + &peer_eth_addrs[n]) < 0)
> + rte_exit(EXIT_FAILURE,
> + "Invalid ethernet address: %s\n",
> + port_end);
> + nb_peer_eth_addrs++;
> + break;
> + }
> #endif
>
Similar to the 'struct option long_options' comment, I think expect from
"interactive" & "cmdline-file", rest can be moved out of "#ifdef
RTE_LIB_CMDLINE" block, but not this patch in different patch.
<...>
> default:
> usage(argv[0]);
>
It is not shown in the patch, to print the error case it uses:
`fprintf(stderr, "Invalid option: %s\n", argv[optind]);`
But as far as I can see 'optind' is the next arg to call, so instead it
should be 'optind - 1'.
And just after while loop, there is "if (optind != argc)" check, I don't
see when this check will be hit, so it looks useless.
More information about the dev
mailing list