The open coded parse_uint() is correct, but the same checks are now
available from kvargs. Use them, and express the lower bound as part of
the range rather than as a separate test after each call.

qdisc_bypass and ignore_outgoing are booleans, so parse them with
rte_kvargs_handle_bool() into a bool. A bare "qdisc_bypass" with no
value now enables it, which the manual pair loop here supports without
any further change.

Note that both parameters of rte_pmd_init_internals() stay an unsigned
int: the address of each is passed to setsockopt(), which expects a
four byte int.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 drivers/net/af_packet/rte_eth_af_packet.c | 81 ++++++-----------------
 1 file changed, 20 insertions(+), 61 deletions(-)

diff --git a/drivers/net/af_packet/rte_eth_af_packet.c 
b/drivers/net/af_packet/rte_eth_af_packet.c
index fbaea57615..1c2da2d6da 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -15,7 +15,6 @@
 #include <rte_kvargs.h>
 #include <bus_vdev_driver.h>
 
-#include <ctype.h>
 #include <errno.h>
 #include <limits.h>
 #include <linux/if_ether.h>
@@ -70,7 +69,7 @@ struct __rte_cache_aligned pkt_rx_queue {
        uint8_t vlan_strip;
        uint8_t timestamp_offloading;
        uint8_t scatter_enabled;
-       uint8_t ignore_outgoing;
+       bool ignore_outgoing;
 
        volatile unsigned long rx_pkts;
        volatile unsigned long rx_bytes;
@@ -204,7 +203,7 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, 
uint16_t nb_pkts)
                        break;
 
                /* skip outgoing frames if ignore_outgoing is set */
-               if (pkt_q->ignore_outgoing != 0) {
+               if (pkt_q->ignore_outgoing) {
                        sll = (struct sockaddr_ll *)((uint8_t *)ppd +
                                        TPACKET_ALIGN(sizeof(*ppd)));
                        if (sll->sll_pkttype == PACKET_OUTGOING)
@@ -1209,36 +1208,17 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
 /* Parse an unsigned integer device argument. */
 static int
 parse_uint(const char *key, const char *value,
-          unsigned int *out, unsigned long limit)
+          unsigned int *out, unsigned long min, unsigned long max)
 {
-       unsigned long val;
-       char *endptr;
+       uint64_t val;
 
-       if (value == NULL) {
-               PMD_LOG(ERR, "no value for argument \"%s\"", key);
-               return -1;
-       }
-
-       /* Skip leading whitespace so a leading sign can be detected. */
-       while (isspace((unsigned char)*value))
-               value++;
-
-       /* strtoul() silently accepts and negates a leading '-'. */
-       if (*value == '\0' || *value == '-') {
-               PMD_LOG(ERR, "invalid value \"%s\" for argument \"%s\"",
-                       value, key);
-               return -1;
-       }
-
-       errno = 0;
-       val = strtoul(value, &endptr, 10);
-       if (errno != 0 || *endptr != '\0' || val > limit) {
+       if (rte_kvargs_to_uint(value, min, max, &val) < 0) {
                PMD_LOG(ERR, "invalid value \"%s\" for argument \"%s\"",
-                       value, key);
+                       value == NULL ? "" : value, key);
                return -1;
        }
 
-       *out = (unsigned int)val;
+       *out = val;
        return 0;
 }
 
@@ -1257,9 +1237,9 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
        unsigned int framesize = DFLT_FRAME_SIZE;
        unsigned int framecount = DFLT_FRAME_COUNT;
        unsigned int qpairs = 1;
-       unsigned int qdisc_bypass = 1;
+       bool qdisc_bypass = true;
        const char *fanout_mode = NULL;
-       unsigned int ignore_outgoing = 0;
+       bool ignore_outgoing = false;
 
        /* do some parameter checking */
        if (*sockfd < 0)
@@ -1273,52 +1253,31 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
        for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
                pair = &kvlist->pairs[k_idx];
                if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
-                       if (parse_uint(pair->key, pair->value,
-                                      &qpairs, RTE_MAX_QUEUES_PER_PORT) < 0)
-                               return -1;
-                       if (qpairs < 1) {
-                               PMD_LOG(ERR,
-                                       "%s: invalid qpairs value",
-                                       name);
+                       if (parse_uint(pair->key, pair->value, &qpairs,
+                                      1, RTE_MAX_QUEUES_PER_PORT) < 0)
                                return -1;
-                       }
                        continue;
                }
                if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
-                       if (parse_uint(pair->key, pair->value, &blocksize, 
UINT_MAX) < 0)
-                               return -1;
-                       if (!blocksize) {
-                               PMD_LOG(ERR,
-                                       "%s: invalid blocksize value",
-                                       name);
+                       if (parse_uint(pair->key, pair->value, &blocksize,
+                                      1, UINT_MAX) < 0)
                                return -1;
-                       }
                        continue;
                }
                if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
-                       if (parse_uint(pair->key, pair->value, &framesize, 
UINT_MAX) < 0)
-                               return -1;
-                       if (!framesize) {
-                               PMD_LOG(ERR,
-                                       "%s: invalid framesize value",
-                                       name);
+                       if (parse_uint(pair->key, pair->value, &framesize,
+                                      1, UINT_MAX) < 0)
                                return -1;
-                       }
                        continue;
                }
                if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) {
-                       if (parse_uint(pair->key, pair->value, &framecount, 
UINT_MAX) < 0)
+                       if (parse_uint(pair->key, pair->value, &framecount,
+                                      1, UINT_MAX) < 0)
                                return -1;
-                       if (!framecount) {
-                               PMD_LOG(ERR,
-                                       "%s: invalid framecount value",
-                                       name);
-                               return -1;
-                       }
                        continue;
                }
                if (strstr(pair->key, ETH_AF_PACKET_QDISC_BYPASS_ARG) != NULL) {
-                       if (parse_uint(pair->key, pair->value, &qdisc_bypass, 
1) < 0)
+                       if (rte_kvargs_handle_bool(pair->key, pair->value, 
&qdisc_bypass) < 0)
                                return -1;
                        continue;
                }
@@ -1327,7 +1286,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                        continue;
                }
                if (strstr(pair->key, ETH_AF_PACKET_IGNORE_OUTGOING_ARG) != 
NULL) {
-                       if (parse_uint(pair->key, pair->value, 
&ignore_outgoing, 1) < 0)
+                       if (rte_kvargs_handle_bool(pair->key, pair->value, 
&ignore_outgoing) < 0)
                                return -1;
                        continue;
                }
@@ -1357,7 +1316,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                PMD_LOG(DEBUG, "%s:\tfanout mode %s", name, fanout_mode);
        else
                PMD_LOG(DEBUG, "%s:\tfanout mode %s", name, "default 
PACKET_FANOUT_HASH");
-       PMD_LOG(DEBUG, "%s:\tignore outgoing %u", name, ignore_outgoing);
+       PMD_LOG(DEBUG, "%s:\tignore outgoing %d", name, ignore_outgoing);
 
        if (rte_pmd_init_internals(dev, *sockfd, qpairs,
                                   blocksize, blockcount,
-- 
2.53.0

Reply via email to