parse_u16() ignores the end pointer, so "8junk" is accepted, and assigns
a u64 into a u16 without a range check, so "65536" wraps to zero. Zero
is still rejected, as it is the unset marker for quanta_size.

parse_bool() checks neither errno nor the end pointer.

iavf_parse_watchdog_period() uses atoi(), which cannot report an error
at all, so a malformed value becomes zero and the negative check never
fires.

no-poll-on-link-down, auto_reset, auto_reconfig and enable_ptype_lldp
are booleans, so make the fields bool and use rte_kvargs_handle_bool().
That is every caller of the local parse_bool(), so it goes away too. A
bare key now enables the option, and the usual spellings are accepted.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 drivers/net/intel/iavf/iavf.h        |  8 ++--
 drivers/net/intel/iavf/iavf_ethdev.c | 61 +++++++++-------------------
 2 files changed, 24 insertions(+), 45 deletions(-)

diff --git a/drivers/net/intel/iavf/iavf.h b/drivers/net/intel/iavf/iavf.h
index 293adaf6c9..6bfe07e1b0 100644
--- a/drivers/net/intel/iavf/iavf.h
+++ b/drivers/net/intel/iavf/iavf.h
@@ -322,11 +322,11 @@ struct iavf_devargs {
        uint8_t proto_xtr[IAVF_MAX_QUEUE_NUM];
        uint16_t quanta_size;
        uint32_t watchdog_period;
-       int auto_reset;
-       int auto_reconfig;
-       int no_poll_on_link_down;
+       bool auto_reset;
+       bool auto_reconfig;
+       bool no_poll_on_link_down;
        uint64_t mbuf_check;
-       int enable_ptype_lldp;
+       bool enable_ptype_lldp;
 };
 
 struct iavf_security_ctx;
diff --git a/drivers/net/intel/iavf/iavf_ethdev.c 
b/drivers/net/intel/iavf/iavf_ethdev.c
index d601ec3b6a..273040d8a5 100644
--- a/drivers/net/intel/iavf/iavf_ethdev.c
+++ b/drivers/net/intel/iavf/iavf_ethdev.c
@@ -3,6 +3,7 @@
  */
 
 #include <ctype.h>
+#include <limits.h>
 #include <sys/queue.h>
 #include <stdalign.h>
 #include <stdio.h>
@@ -13,6 +14,7 @@
 #include <stdarg.h>
 #include <inttypes.h>
 #include <rte_byteorder.h>
+#include <rte_kvargs.h>
 #include <rte_common.h>
 #include <rte_os_shim.h>
 
@@ -2352,11 +2354,10 @@ static int
 parse_u16(__rte_unused const char *key, const char *value, void *args)
 {
        u16 *num = (u16 *)args;
-       u16 tmp;
+       uint64_t tmp;
 
-       errno = 0;
-       tmp = strtoull(value, NULL, 10);
-       if (errno || !tmp) {
+       /* zero is not accepted, it is the "unset" marker for this argument */
+       if (rte_kvargs_to_uint(value, 1, UINT16_MAX, &tmp) < 0) {
                PMD_DRV_LOG(WARNING, "%s: \"%s\" is not a valid u16",
                            key, value);
                return -1;
@@ -2367,35 +2368,13 @@ parse_u16(__rte_unused const char *key, const char 
*value, void *args)
        return 0;
 }
 
-static int
-parse_bool(const char *key, const char *value, void *args)
-{
-       int *i = (int *)args;
-       char *end;
-       int num;
-
-       num = strtoul(value, &end, 10);
-
-       if (num != 0 && num != 1) {
-               PMD_DRV_LOG(WARNING, "invalid value:\"%s\" for key:\"%s\", "
-                       "value must be 0 or 1",
-                       value, key);
-               return -1;
-       }
-
-       *i = num;
-       return 0;
-}
-
 static int
 iavf_parse_watchdog_period(__rte_unused const char *key, const char *value, 
void *args)
 {
        int *num = (int *)args;
-       int tmp;
+       uint64_t tmp;
 
-       errno = 0;
-       tmp = atoi(value);
-       if (tmp < 0) {
+       if (rte_kvargs_to_uint(value, 0, INT_MAX, &tmp) < 0) {
                PMD_DRV_LOG(WARNING, "%s: \"%s\" is not greater than or equal 
to zero",
                                key, value);
                return -1;
@@ -2466,9 +2445,9 @@ static int iavf_parse_devargs(struct rte_eth_dev *dev)
        int ret;
        int watchdog_period = -1;
 
-       ad->devargs.auto_reset = 1;
-       ad->devargs.no_poll_on_link_down = 1;
-       ad->devargs.auto_reconfig = 1;
+       ad->devargs.auto_reset = true;
+       ad->devargs.no_poll_on_link_down = true;
+       ad->devargs.auto_reconfig = true;
 
        if (!devargs)
                return 0;
@@ -2502,8 +2481,8 @@ static int iavf_parse_devargs(struct rte_eth_dev *dev)
        else
                ad->devargs.watchdog_period = watchdog_period;
 
-       ret = rte_kvargs_process(kvlist, IAVF_NO_POLL_ON_LINK_DOWN_ARG,
-                                &parse_bool, 
&ad->devargs.no_poll_on_link_down);
+       ret = rte_kvargs_process_opt(kvlist, IAVF_NO_POLL_ON_LINK_DOWN_ARG,
+                                rte_kvargs_handle_bool, 
&ad->devargs.no_poll_on_link_down);
        if (ret)
                goto bail;
 
@@ -2520,24 +2499,24 @@ static int iavf_parse_devargs(struct rte_eth_dev *dev)
        if (ret)
                goto bail;
 
-       ret = rte_kvargs_process(kvlist, IAVF_ENABLE_AUTO_RESET_ARG,
-                                &parse_bool, &ad->devargs.auto_reset);
+       ret = rte_kvargs_process_opt(kvlist, IAVF_ENABLE_AUTO_RESET_ARG,
+                                rte_kvargs_handle_bool, 
&ad->devargs.auto_reset);
        if (ret)
                goto bail;
 
-       if (ad->devargs.auto_reset != 0 && ad->devargs.no_poll_on_link_down == 
0) {
+       if (ad->devargs.auto_reset && !ad->devargs.no_poll_on_link_down) {
                PMD_INIT_LOG(WARNING,
                        "no-poll-on-link-down=0 is incompatible with 
auto_reset=1, ignoring");
-               ad->devargs.no_poll_on_link_down = 1;
+               ad->devargs.no_poll_on_link_down = true;
        }
 
-       ret = rte_kvargs_process(kvlist, IAVF_ENABLE_AUTO_RECONFIG_ARG,
-                                &parse_bool, &ad->devargs.auto_reconfig);
+       ret = rte_kvargs_process_opt(kvlist, IAVF_ENABLE_AUTO_RECONFIG_ARG,
+                                rte_kvargs_handle_bool, 
&ad->devargs.auto_reconfig);
        if (ret)
                goto bail;
 
-       ret = rte_kvargs_process(kvlist, IAVF_ENABLE_PTYPE_LLDP_ARG,
-                                &parse_bool, &ad->devargs.enable_ptype_lldp);
+       ret = rte_kvargs_process_opt(kvlist, IAVF_ENABLE_PTYPE_LLDP_ARG,
+                                rte_kvargs_handle_bool, 
&ad->devargs.enable_ptype_lldp);
        if (ret)
                goto bail;
 
-- 
2.53.0

Reply via email to