From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id EB83743922; Mon, 22 Jan 2024 05:03:37 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D3F9F427E0; Mon, 22 Jan 2024 05:03:37 +0100 (CET) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by mails.dpdk.org (Postfix) with ESMTP id 073A0406A2 for ; Mon, 22 Jan 2024 05:03:36 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4TJGlS6CY1z1xmY7; Mon, 22 Jan 2024 12:02:44 +0800 (CST) Received: from dggpeml500024.china.huawei.com (unknown [7.185.36.10]) by mail.maildlp.com (Postfix) with ESMTPS id D72D91A0174; Mon, 22 Jan 2024 12:03:19 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 22 Jan 2024 12:01:50 +0800 From: Chengwen Feng To: , , , CC: Subject: [PATCH 12/12] examples/dma: replace getopt with argparse Date: Mon, 22 Jan 2024 03:58:02 +0000 Message-ID: <20240122035802.31491-13-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240122035802.31491-1-fengchengwen@huawei.com> References: <20231121122651.7078-1-fengchengwen@huawei.com> <20240122035802.31491-1-fengchengwen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500024.china.huawei.com (7.185.36.10) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Replace getopt with argparse. Signed-off-by: Chengwen Feng --- examples/dma/dmafwd.c | 279 ++++++++++++++++++--------------------- examples/dma/meson.build | 2 +- 2 files changed, 127 insertions(+), 154 deletions(-) diff --git a/examples/dma/dmafwd.c b/examples/dma/dmafwd.c index f27317a622..4cc0913240 100644 --- a/examples/dma/dmafwd.c +++ b/examples/dma/dmafwd.c @@ -4,11 +4,11 @@ #include #include -#include #include #include #include +#include #include #include #include @@ -18,16 +18,18 @@ #define MAX_PKT_BURST 32 #define MEMPOOL_CACHE_SIZE 512 #define MIN_POOL_SIZE 65536U -#define CMD_LINE_OPT_MAC_UPDATING "mac-updating" -#define CMD_LINE_OPT_NO_MAC_UPDATING "no-mac-updating" -#define CMD_LINE_OPT_PORTMASK "portmask" -#define CMD_LINE_OPT_NB_QUEUE "nb-queue" -#define CMD_LINE_OPT_COPY_TYPE "copy-type" -#define CMD_LINE_OPT_RING_SIZE "ring-size" -#define CMD_LINE_OPT_BATCH_SIZE "dma-batch-size" -#define CMD_LINE_OPT_FRAME_SIZE "max-frame-size" -#define CMD_LINE_OPT_FORCE_COPY_SIZE "force-min-copy-size" -#define CMD_LINE_OPT_STATS_INTERVAL "stats-interval" +#define CMD_LINE_OPT_MAC_UPDATING "--mac-updating" +#define CMD_LINE_OPT_NO_MAC_UPDATING "--no-mac-updating" +#define CMD_LINE_OPT_PORTMASK "--portmask" +#define CMD_LINE_OPT_PORTMASK_INDEX 1 +#define CMD_LINE_OPT_NB_QUEUE "--nb-queue" +#define CMD_LINE_OPT_COPY_TYPE "--copy-type" +#define CMD_LINE_OPT_COPY_TYPE_INDEX 2 +#define CMD_LINE_OPT_RING_SIZE "--ring-size" +#define CMD_LINE_OPT_BATCH_SIZE "--dma-batch-size" +#define CMD_LINE_OPT_FRAME_SIZE "--max-frame-size" +#define CMD_LINE_OPT_FORCE_COPY_SIZE "--force-min-copy-size" +#define CMD_LINE_OPT_STATS_INTERVAL "--stats-interval" /* configurable number of RX/TX ring descriptors */ #define RX_DEFAULT_RINGSIZE 1024 @@ -95,10 +97,10 @@ static copy_mode_t copy_mode = COPY_MODE_DMA_NUM; /* size of descriptor ring for hardware copy mode or * rte_ring for software copy mode */ -static unsigned short ring_size = 2048; +static uint16_t ring_size = 2048; /* interval, in seconds, between stats prints */ -static unsigned short stats_interval = 1; +static uint16_t stats_interval = 1; /* global mbuf arrays for tracking DMA bufs */ #define MBUF_RING_SIZE 2048 #define MBUF_RING_MASK (MBUF_RING_SIZE - 1) @@ -583,26 +585,6 @@ static void start_forwarding_cores(void) } /* >8 End of starting to process for each lcore. */ -/* Display usage */ -static void -dma_usage(const char *prgname) -{ - printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n" - " -b --dma-batch-size: number of requests per DMA batch\n" - " -f --max-frame-size: max frame size\n" - " -m --force-min-copy-size: force a minimum copy length, even for smaller packets\n" - " -p --portmask: hexadecimal bitmask of ports to configure\n" - " -q NQ: number of RX queues per port (default is 1)\n" - " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n" - " When enabled:\n" - " - The source MAC address is replaced by the TX port MAC address\n" - " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n" - " -c --copy-type CT: type of copy: sw|hw\n" - " -s --ring-size RS: size of dmadev descriptor ring for hardware copy mode or rte_ring for software copy mode\n" - " -i --stats-interval SI: interval, in seconds, between stats prints (default is 1)\n", - prgname); -} - static int dma_parse_portmask(const char *portmask) { @@ -628,142 +610,133 @@ dma_parse_copy_mode(const char *copy_mode) return COPY_MODE_INVALID_NUM; } +static int +dma_parse_args_cb(uint32_t index, const char *value, void *opaque) +{ + int port_mask; + + RTE_SET_USED(opaque); + + if (index == CMD_LINE_OPT_PORTMASK_INDEX) { + port_mask = dma_parse_portmask(value); + if (port_mask & ~dma_enabled_port_mask || port_mask <= 0) { + printf("Invalid portmask, %s, suggest 0x%x\n", + value, dma_enabled_port_mask); + return -1; + } + dma_enabled_port_mask = port_mask; + } else if (index == CMD_LINE_OPT_COPY_TYPE_INDEX) { + copy_mode = dma_parse_copy_mode(value); + if (copy_mode == COPY_MODE_INVALID_NUM) { + printf("Invalid copy type. Use: sw, hw\n"); + return -1; + } + } else { + printf("Invalid index %u\n", index); + return -1; + } + + return 0; +} + /* Parse the argument given in the command line of the application */ static int dma_parse_args(int argc, char **argv, unsigned int nb_ports) { - static const char short_options[] = - "b:" /* dma batch size */ - "c:" /* copy type (sw|hw) */ - "f:" /* max frame size */ - "m:" /* force min copy size */ - "p:" /* portmask */ - "q:" /* number of RX queues per port */ - "s:" /* ring size */ - "i:" /* interval, in seconds, between stats prints */ - ; - - static const struct option lgopts[] = { - {CMD_LINE_OPT_MAC_UPDATING, no_argument, &mac_updating, 1}, - {CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, &mac_updating, 0}, - {CMD_LINE_OPT_PORTMASK, required_argument, NULL, 'p'}, - {CMD_LINE_OPT_NB_QUEUE, required_argument, NULL, 'q'}, - {CMD_LINE_OPT_COPY_TYPE, required_argument, NULL, 'c'}, - {CMD_LINE_OPT_RING_SIZE, required_argument, NULL, 's'}, - {CMD_LINE_OPT_BATCH_SIZE, required_argument, NULL, 'b'}, - {CMD_LINE_OPT_FRAME_SIZE, required_argument, NULL, 'f'}, - {CMD_LINE_OPT_FORCE_COPY_SIZE, required_argument, NULL, 'm'}, - {CMD_LINE_OPT_STATS_INTERVAL, required_argument, NULL, 'i'}, - {NULL, 0, 0, 0} + static struct rte_argparse obj = { + .prog_name = "dma", + .usage = "[EAL options] -- [optional parameters]", + .descriptor = NULL, + .epilog = NULL, + .exit_on_error = false, + .callback = dma_parse_args_cb, + .opaque = NULL, + .args = { + { CMD_LINE_OPT_MAC_UPDATING, NULL, "Enable MAC addresses updating", + &mac_updating, (void *)1, + RTE_ARGPARSE_ARG_NO_VALUE | RTE_ARGPARSE_ARG_VALUE_INT, + }, + { CMD_LINE_OPT_NO_MAC_UPDATING, NULL, "Disable MAC addresses updating", + &mac_updating, (void *)0, + RTE_ARGPARSE_ARG_NO_VALUE | RTE_ARGPARSE_ARG_VALUE_INT, + }, + { CMD_LINE_OPT_PORTMASK, "-p", "hexadecimal bitmask of ports to configure", + NULL, (void *)CMD_LINE_OPT_PORTMASK_INDEX, + RTE_ARGPARSE_ARG_REQUIRED_VALUE, + }, + { CMD_LINE_OPT_NB_QUEUE, "-q", "number of RX queues per port (default is 1)", + &nb_queues, NULL, + RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_U16, + }, + { CMD_LINE_OPT_COPY_TYPE, "-c", "type of copy: sw|hw", + NULL, (void *)CMD_LINE_OPT_COPY_TYPE_INDEX, + RTE_ARGPARSE_ARG_REQUIRED_VALUE, + }, + { CMD_LINE_OPT_RING_SIZE, "-s", "size of dmadev descriptor ring for hardware copy mode or rte_ring for software copy mode", + &ring_size, NULL, + RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_U16, + }, + { CMD_LINE_OPT_BATCH_SIZE, "-b", "number of requests per DMA batch", + &dma_batch_sz, NULL, + RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_U32, + }, + { CMD_LINE_OPT_FRAME_SIZE, "-f", "max frame size", + &max_frame_size, NULL, + RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_U32, + }, + { CMD_LINE_OPT_FORCE_COPY_SIZE, "-m", "force a minimum copy length, even for smaller packets", + &force_min_copy_size, NULL, + RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_U32, + }, + { CMD_LINE_OPT_STATS_INTERVAL, "-i", "interval, in seconds, between stats prints (default is 1)", + &stats_interval, NULL, + RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_U16, + }, + ARGPARSE_ARG_END(), + }, }; const unsigned int default_port_mask = (1 << nb_ports) - 1; - int opt, ret; - char **argvopt; - int option_index; - char *prgname = argv[0]; + int ret; dma_enabled_port_mask = default_port_mask; - argvopt = argv; - - while ((opt = getopt_long(argc, argvopt, short_options, - lgopts, &option_index)) != EOF) { - - switch (opt) { - case 'b': - dma_batch_sz = atoi(optarg); - if (dma_batch_sz > MAX_PKT_BURST) { - printf("Invalid dma batch size, %s.\n", optarg); - dma_usage(prgname); - return -1; - } - break; - case 'f': - max_frame_size = atoi(optarg); - if (max_frame_size > RTE_ETHER_MAX_JUMBO_FRAME_LEN) { - printf("Invalid max frame size, %s.\n", optarg); - dma_usage(prgname); - return -1; - } - break; - - case 'm': - force_min_copy_size = atoi(optarg); - break; - - /* portmask */ - case 'p': - dma_enabled_port_mask = dma_parse_portmask(optarg); - if (dma_enabled_port_mask & ~default_port_mask || - dma_enabled_port_mask <= 0) { - printf("Invalid portmask, %s, suggest 0x%x\n", - optarg, default_port_mask); - dma_usage(prgname); - return -1; - } - break; - - case 'q': - nb_queues = atoi(optarg); - if (nb_queues == 0 || nb_queues > MAX_RX_QUEUES_COUNT) { - printf("Invalid RX queues number %s. Max %u\n", - optarg, MAX_RX_QUEUES_COUNT); - dma_usage(prgname); - return -1; - } - break; - - case 'c': - copy_mode = dma_parse_copy_mode(optarg); - if (copy_mode == COPY_MODE_INVALID_NUM) { - printf("Invalid copy type. Use: sw, hw\n"); - dma_usage(prgname); - return -1; - } - break; + ret = rte_argparse_parse(&obj, argc, argv); + if (ret != 0) + return ret; - case 's': - ring_size = atoi(optarg); - if (ring_size == 0) { - printf("Invalid ring size, %s.\n", optarg); - dma_usage(prgname); - return -1; - } - /* ring_size must be less-than or equal to MBUF_RING_SIZE - * to avoid overwriting bufs - */ - if (ring_size > MBUF_RING_SIZE) { - printf("Max ring_size is %d, setting ring_size to max", - MBUF_RING_SIZE); - ring_size = MBUF_RING_SIZE; - } - break; + /* check argument's value which parsing by autosave. */ + if (dma_batch_sz <= 0 || dma_batch_sz > MAX_PKT_BURST) { + printf("Invalid dma batch size, %d.\n", dma_batch_sz); + return -1; + } - case 'i': - stats_interval = atoi(optarg); - if (stats_interval == 0) { - printf("Invalid stats interval, setting to 1\n"); - stats_interval = 1; /* set to default */ - } - break; + if (max_frame_size <= 0 || max_frame_size > RTE_ETHER_MAX_JUMBO_FRAME_LEN) { + printf("Invalid max frame size, %d.\n", max_frame_size); + return -1; + } - /* long options */ - case 0: - break; + if (nb_queues <= 0 || nb_queues > MAX_RX_QUEUES_COUNT) { + printf("Invalid RX queues number %d. Max %u\n", + nb_queues, MAX_RX_QUEUES_COUNT); + return -1; + } - default: - dma_usage(prgname); - return -1; - } + if (ring_size <= 0) { + printf("Invalid ring size, %d.\n", ring_size); + return -1; + } + if (ring_size > MBUF_RING_SIZE) { + printf("Max ring_size is %d, setting ring_size to max", + MBUF_RING_SIZE); + ring_size = MBUF_RING_SIZE; } - printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled"); - if (optind >= 0) - argv[optind - 1] = prgname; + if (stats_interval <= 0) { + printf("Invalid stats interval, setting to 1\n"); + stats_interval = 1; /* set to default */ + } - ret = optind - 1; - optind = 1; /* reset getopt lib */ - return ret; + return 0; } /* check link status, return true if at least one port is up */ diff --git a/examples/dma/meson.build b/examples/dma/meson.build index 9fdcad660e..124f9476fc 100644 --- a/examples/dma/meson.build +++ b/examples/dma/meson.build @@ -8,7 +8,7 @@ allow_experimental_apis = true -deps += ['dmadev'] +deps += ['argparse', 'dmadev'] sources = files( 'dmafwd.c', -- 2.17.1