The VSI timeout and delay arguments are parsed with strtoul() without
checking the end pointer, so a value such as "10abc" is accepted. They
are also assigned into a uint32_t before being validated, so a value
above UINT32_MAX is truncated and only rejected if it happens to
truncate to zero.

Report the offending string rather than the truncated value, which was
not meaningful once the conversion had failed.

The nc argument used atoi(), so any unparsable value was taken as zero
and silently ignored. The vf_link_legacy handler did validate, but only
to store a value in 0..1, which is what rte_kvargs_handle_bool() does.
Use that for both, and update the parameter strings to say 0|1 rather
than int.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 drivers/net/enetc/enetc4_ethdev.c | 13 ++++--
 drivers/net/enetc/enetc4_vf.c     | 70 +++++++++++--------------------
 2 files changed, 33 insertions(+), 50 deletions(-)

diff --git a/drivers/net/enetc/enetc4_ethdev.c 
b/drivers/net/enetc/enetc4_ethdev.c
index 199cd45ee5..efa7d9abb2 100644
--- a/drivers/net/enetc/enetc4_ethdev.c
+++ b/drivers/net/enetc/enetc4_ethdev.c
@@ -151,14 +151,19 @@ static int parse_txq_wrr(const char *key __rte_unused, 
const char *value,
 }
 
 static int
-parse_nc(const char *key __rte_unused, const char *value, void *extra_args)
+parse_nc(const char *key, const char *value, void *extra_args)
 {
        struct rte_eth_dev *dev = extra_args;
        struct enetc_eth_hw *hw =
                ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+       bool nc;
+       int ret;
+
+       ret = rte_kvargs_handle_bool(key, value, &nc);
+       if (ret < 0)
+               return ret;
 
-       if (value && atoi(value) == 1)
-               hw->nc_mode = 1;
+       hw->nc_mode = nc;
 
        return 0;
 }
@@ -1636,5 +1641,5 @@ RTE_PMD_REGISTER_KMOD_DEP(net_enetc4, "* vfio-pci");
 RTE_PMD_REGISTER_PARAM_STRING(net_enetc4,
                              ENETC4_TXQ_PRIORITIES "=<string> "
                              ENETC4_TXQ_WRR "=<string> "
-                             ENETC4_NC_MEMORY "=<int>");
+                             ENETC4_NC_MEMORY "=<0|1>");
 RTE_LOG_REGISTER_DEFAULT(enetc4_logtype_pmd, NOTICE);
diff --git a/drivers/net/enetc/enetc4_vf.c b/drivers/net/enetc/enetc4_vf.c
index 6eb1e5d760..421b9ceab1 100644
--- a/drivers/net/enetc/enetc4_vf.c
+++ b/drivers/net/enetc/enetc4_vf.c
@@ -22,7 +22,7 @@ enetc4_vf_get_devarg_nc(struct rte_eth_dev *dev)
                ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
        struct rte_devargs *devargs = dev->device->devargs;
        struct rte_kvargs *kvlist;
-       const char *val;
+       bool nc = false;
 
        if (!devargs)
                return;
@@ -31,9 +31,9 @@ enetc4_vf_get_devarg_nc(struct rte_eth_dev *dev)
        if (!kvlist)
                return;
 
-       val = rte_kvargs_get(kvlist, ENETC4_NC_MEMORY);
-       if (val && atoi(val) == 1)
-               hw->nc_mode = 1;
+       if (rte_kvargs_process_opt(kvlist, ENETC4_NC_MEMORY,
+                                  rte_kvargs_handle_bool, &nc) == 0)
+               hw->nc_mode = nc;
 
        rte_kvargs_free(kvlist);
 }
@@ -108,40 +108,14 @@ enetc_crc_calc(uint16_t crc, const uint8_t *buffer, 
size_t len)
        return crc;
 }
 
-static int
-parse_vf_link_legacy(const char *key __rte_unused, const char *value,
-                    void *opaque)
-{
-       struct rte_eth_dev *dev = (struct rte_eth_dev *)opaque;
-       struct enetc_eth_hw *hw =
-                       ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
-       char *endptr;
-       unsigned long val;
-
-       if (!value || *value == '\0') {
-               ENETC_PMD_WARN("Empty value for devarg %s, ignoring",
-                              ENETC_VF_LINK_LEGACY);
-               return -EINVAL;
-       }
-
-       errno = 0;
-       val = strtoul(value, &endptr, 0);
-       if (errno != 0 || *endptr != '\0' || val > 1) {
-               ENETC_PMD_WARN("Invalid value '%s' for devarg %s, expected 0 or 
1",
-                              value, ENETC_VF_LINK_LEGACY);
-               return -EINVAL;
-       }
-
-       hw->vf_link_legacy = (uint8_t)val;
-
-       return 0;
-}
-
 static void
 enetc4_vf_get_devargs(struct rte_eth_dev *dev)
 {
+       struct enetc_eth_hw *hw =
+               ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
        struct rte_devargs *devargs;
        struct rte_kvargs *kvlist;
+       bool legacy = false;
 
        devargs = dev->device->devargs;
        if (!devargs)
@@ -152,10 +126,12 @@ enetc4_vf_get_devargs(struct rte_eth_dev *dev)
                return;
 
        if (rte_kvargs_count(kvlist, ENETC_VF_LINK_LEGACY)) {
-               if (rte_kvargs_process(kvlist, ENETC_VF_LINK_LEGACY,
-                                      parse_vf_link_legacy, (void *)dev) < 0)
+               if (rte_kvargs_process_opt(kvlist, ENETC_VF_LINK_LEGACY,
+                                          rte_kvargs_handle_bool, &legacy) < 0)
                        ENETC_PMD_WARN("Failed to parse devarg %s",
                                       ENETC_VF_LINK_LEGACY);
+               else
+                       hw->vf_link_legacy = legacy;
        }
 
        rte_kvargs_free(kvlist);
@@ -2012,28 +1988,30 @@ enetc4_vf_dev_init(struct rte_eth_dev *eth_dev)
                        /* parse optional VSI-PSI timeout devarg */
                        val = rte_kvargs_get(kvlist, ENETC4_VSI_TIMEOUT);
                        if (val) {
-                               errno = 0;
-                               hw->vsi_timeout = (uint32_t)strtoul(val, NULL, 
0);
-                               if (errno != 0 || hw->vsi_timeout == 0) {
-                                       ENETC_PMD_ERR("Invalid VSI Timeout 
value = %u",
-                                                       hw->vsi_timeout);
+                               uint64_t num;
+
+                               if (rte_kvargs_to_uint(val, 1, UINT32_MAX, 
&num) < 0) {
+                                       ENETC_PMD_ERR("Invalid VSI Timeout 
value = %s",
+                                                       val);
                                        rte_kvargs_free(kvlist);
                                        return -1;
                                }
+                               hw->vsi_timeout = num;
                                ENETC_PMD_NOTICE("VSI timeout set to %u", 
hw->vsi_timeout);
                        }
 
                        /* parse optional VSI-PSI delay devarg */
                        val = rte_kvargs_get(kvlist, ENETC4_VSI_DELAY);
                        if (val) {
-                               errno = 0;
-                               hw->vsi_delay = (uint32_t)strtoul(val, NULL, 0);
-                               if (errno != 0 || hw->vsi_delay == 0) {
-                                       ENETC_PMD_ERR("Invalid VSI Delay value 
= %u",
-                                                       hw->vsi_delay);
+                               uint64_t num;
+
+                               if (rte_kvargs_to_uint(val, 1, UINT32_MAX, 
&num) < 0) {
+                                       ENETC_PMD_ERR("Invalid VSI Delay value 
= %s",
+                                                       val);
                                        rte_kvargs_free(kvlist);
                                        return -1;
                                }
+                               hw->vsi_delay = num;
                                ENETC_PMD_NOTICE("VSI delay set to %u us", 
hw->vsi_delay);
                        }
 
@@ -2207,7 +2185,7 @@ RTE_PMD_REGISTER_PARAM_STRING(net_enetc4_vf,
                              ENETC4_VSI_DISABLE "=<any> "
                              ENETC4_VSI_TIMEOUT "=<uint> "
                              ENETC4_VSI_DELAY "=<uint> "
-                             ENETC4_NC_MEMORY "=<int> "
+                             ENETC4_NC_MEMORY "=<0|1> "
                              ENETC4_TXQ_PRIORITIES "=<string> "
                              ENETC4_TXQ_WRR "=<string> "
                              ENETC_VF_LINK_LEGACY "=<0|1>");
-- 
2.53.0

Reply via email to