dlb2_string_to_int() returns -errno on failure, but errno is only
meaningful when strtol() actually failed; on a plain parse failure it
returns -0, that is success, with an uninitialised result. The end
pointer is also only checked after the errno test, so "abc" reaches the
range check with a value of zero.

All thirteen dlb2 devargs handlers go through this one function, so
converting it covers them all.

set_numa_node() is replaced by rte_kvargs_handle_socket_id(). It used
"> RTE_MAX_NUMA_NODES", which is off by one and accepted a socket id one
past the end of the range, so that value is now rejected. The helper
also rejects a negative socket id other than -1, which is SOCKET_ID_ANY.

The four boolean arguments use rte_kvargs_handle_bool() and their local
handlers go away: vector_opts_enable, default_ldb_port_allocation,
enable_cq_weight and use_default_hl.

Beware that this changes what the first three accept. They only ever
tested for a leading 'y' or 'Y', so "=1" meant false, and any typo
silently meant false as well. They now take the usual boolean
spellings, "=1" means true, and anything else is rejected. The
documented "=<y/Y>" form still works; the examples are updated to the
more usual "=<0|1>". use_default_hl already treated '0' and 'n' as
false and everything else as true, so only the rejection of garbage
is new there.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 doc/guides/eventdevs/dlb2.rst |   4 +-
 drivers/event/dlb2/dlb2.c     | 127 ++++------------------------------
 2 files changed, 14 insertions(+), 117 deletions(-)

diff --git a/doc/guides/eventdevs/dlb2.rst b/doc/guides/eventdevs/dlb2.rst
index 06841ba312..0966746f4e 100644
--- a/doc/guides/eventdevs/dlb2.rst
+++ b/doc/guides/eventdevs/dlb2.rst
@@ -380,7 +380,7 @@ follows
 
     .. code-block:: console
 
-       --allow ea:00.0,vector_opts_enabled=<y/Y>
+       --allow ea:00.0,vector_opts_enabled=<0|1>
 
 Maximum CQ Depth
 ~~~~~~~~~~~~~~~~
@@ -459,7 +459,7 @@ Example command to enable QE Weight feature:
 
     .. code-block:: console
 
-       --allow ea:00.0,enable_cq_weight=<y/Y>
+       --allow ea:00.0,enable_cq_weight=<0|1>
 
 Credit Handling Scenario Improvements
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/drivers/event/dlb2/dlb2.c b/drivers/event/dlb2/dlb2.c
index 084875f1c8..b3eceab094 100644
--- a/drivers/event/dlb2/dlb2.c
+++ b/drivers/event/dlb2/dlb2.c
@@ -288,24 +288,15 @@ dlb2_hw_query_resources(struct dlb2_eventdev *dlb2)
        return 0;
 }
 
-#define DLB2_BASE_10 10
-
 static int
 dlb2_string_to_int(int *result, const char *str)
 {
-       long ret;
-       char *endptr;
+       int64_t ret;
 
-       if (str == NULL || result == NULL)
+       if (result == NULL)
                return -EINVAL;
 
-       errno = 0;
-       ret = strtol(str, &endptr, DLB2_BASE_10);
-       if (errno)
-               return -errno;
-
-       /* long int and int may be different width for some architectures */
-       if (ret < INT_MIN || ret > INT_MAX || endptr == str)
+       if (rte_kvargs_to_int(str, INT_MIN, INT_MAX, &ret) < 0)
                return -EINVAL;
 
        *result = ret;
@@ -329,22 +320,6 @@ set_producer_coremask(const char *key __rte_unused,
        return 0;
 }
 
-static int
-set_numa_node(const char *key __rte_unused, const char *value, void *opaque)
-{
-       int *socket_id = opaque;
-       int ret;
-
-       ret = dlb2_string_to_int(socket_id, value);
-       if (ret < 0)
-               return ret;
-
-       if (*socket_id > RTE_MAX_NUMA_NODES)
-               return -EINVAL;
-       return 0;
-}
-
-
 static int
 set_max_cq_depth(const char *key __rte_unused,
                 const char *value,
@@ -667,84 +642,6 @@ set_default_depth_thresh(const char *key __rte_unused,
        return 0;
 }
 
-static int
-set_vector_opts_enab(const char *key __rte_unused,
-       const char *value,
-       void *opaque)
-{
-       bool *dlb2_vector_opts_enabled = opaque;
-
-       if (value == NULL || opaque == NULL) {
-               DLB2_LOG_ERR("NULL pointer");
-               return -EINVAL;
-       }
-
-       if ((*value == 'y') || (*value == 'Y'))
-               *dlb2_vector_opts_enabled = true;
-       else
-               *dlb2_vector_opts_enabled = false;
-
-       return 0;
-}
-
-static int
-set_default_ldb_port_allocation(const char *key __rte_unused,
-                     const char *value,
-                     void *opaque)
-{
-       bool *default_ldb_port_allocation = opaque;
-
-       if (value == NULL || opaque == NULL) {
-               DLB2_LOG_ERR("NULL pointer");
-               return -EINVAL;
-       }
-
-       if ((*value == 'y') || (*value == 'Y'))
-               *default_ldb_port_allocation = true;
-       else
-               *default_ldb_port_allocation = false;
-
-       return 0;
-}
-
-static int
-set_enable_cq_weight(const char *key __rte_unused,
-                     const char *value,
-                     void *opaque)
-{
-       bool *enable_cq_weight = opaque;
-
-       if (value == NULL || opaque == NULL) {
-               DLB2_LOG_ERR("NULL pointer");
-               return -EINVAL;
-       }
-
-       if ((*value == 'y') || (*value == 'Y'))
-               *enable_cq_weight = true;
-       else
-               *enable_cq_weight = false;
-
-       return 0;
-}
-
-static int set_hl_override(const char *key __rte_unused, const char *value,
-                          void *opaque)
-{
-       bool *default_hl = opaque;
-
-       if (value == NULL || opaque == NULL) {
-               DLB2_LOG_ERR("NULL pointer");
-               return -EINVAL;
-       }
-
-       if ((*value == 'n') || (*value == 'N') || (*value == '0'))
-               *default_hl = false;
-       else
-               *default_hl = true;
-
-       return 0;
-}
-
 static int set_hl_entries(const char *key __rte_unused, const char *value,
                          void *opaque)
 {
@@ -5223,7 +5120,7 @@ dlb2_parse_params(const char *params,
                                      name);
                } else {
                        int ret = rte_kvargs_process(kvlist, NUMA_NODE_ARG,
-                                                    set_numa_node,
+                                                    
rte_kvargs_handle_socket_id,
                                                     &dlb2_args->socket_id);
                        if (ret != 0) {
                                DLB2_LOG_ERR("%s: Error parsing numa node 
parameter",
@@ -5335,9 +5232,9 @@ dlb2_parse_params(const char *params,
                                return ret;
                        }
 
-                       ret = rte_kvargs_process(kvlist,
+                       ret = rte_kvargs_process_opt(kvlist,
                                        DLB2_VECTOR_OPTS_ENAB_ARG,
-                                       set_vector_opts_enab,
+                                       rte_kvargs_handle_bool,
                                        &dlb2_args->vector_opts_enabled);
                        if (ret != 0) {
                                DLB2_LOG_ERR("%s: Error parsing vector opts 
enabled",
@@ -5403,9 +5300,9 @@ dlb2_parse_params(const char *params,
                                return ret;
                        }
 
-                       ret = rte_kvargs_process(kvlist,
+                       ret = rte_kvargs_process_opt(kvlist,
                                                 
DLB2_DEFAULT_LDB_PORT_ALLOCATION_ARG,
-                                                
set_default_ldb_port_allocation,
+                                                rte_kvargs_handle_bool,
                                                 
&dlb2_args->default_ldb_port_allocation);
                        if (ret != 0) {
                                DLB2_LOG_ERR("%s: Error parsing ldb default 
port allocation arg",
@@ -5414,9 +5311,9 @@ dlb2_parse_params(const char *params,
                                return ret;
                        }
 
-                       ret = rte_kvargs_process(kvlist,
+                       ret = rte_kvargs_process_opt(kvlist,
                                                 DLB2_ENABLE_CQ_WEIGHT_ARG,
-                                                set_enable_cq_weight,
+                                                rte_kvargs_handle_bool,
                                                 &dlb2_args->enable_cq_weight);
                        if (ret != 0) {
                                DLB2_LOG_ERR("%s: Error parsing 
enable_cq_weight arg",
@@ -5427,8 +5324,8 @@ dlb2_parse_params(const char *params,
                        if (version == DLB2_HW_V2 && 
dlb2_args->enable_cq_weight)
                                DLB2_LOG_INFO("Ignoring 'enable_cq_weight=y'. 
Only supported for 2.5 HW onwards");
 
-                       ret = rte_kvargs_process(kvlist, DLB2_USE_DEFAULT_HL,
-                                                set_hl_override,
+                       ret = rte_kvargs_process_opt(kvlist, 
DLB2_USE_DEFAULT_HL,
+                                                rte_kvargs_handle_bool,
                                                 &dlb2_args->use_default_hl);
                        if (ret != 0) {
                                DLB2_LOG_ERR("%s: Error parsing hl_override 
arg",
-- 
2.53.0

Reply via email to