Skip to contentSkip to conversation

kvargs: add numeric conversion helpers and use them

PATCH Superseded Stephen Hemminger 4 participants 201 messages Sep 14 – Oct 10 Page 2 of 2

Subsystems (6)
lore
Share & tools
lore
#115 lore raw

PATCH v2 47/61 net/sfc: use kvargs numeric helpers

In reply to Stephen Hemminger

sfc_kvarg_long_handler() only checked that strtol() consumed at least one character, so trailing garbage such as "5abc" was accepted, and errno was never checked, so an out of range value was silently taken as LONG_MAX or LONG_MIN.

It did nothing beyond that conversion, so drop it and use the new rte_kvargs_handle_long(). The callers keep their long variables and their own range checks.

---
 drivers/net/sfc/sfc.c        |  2 +-
 drivers/net/sfc/sfc_kvargs.c | 19 -------------------
 drivers/net/sfc/sfc_kvargs.h |  2 --
 drivers/net/sfc/sfc_port.c   |  2 +-
 4 files changed, 2 insertions(+), 23 deletions(-)
drivers/net/sfc/sfc.c +1 -1
index 39cd8d519a..3d460cc222 100644
@@ -1213,7 +1213,7 @@ sfc_kvarg_rxd_wait_timeout_ns(struct sfc_adapter *sa)
 	value = SFC_RXD_WAIT_TIMEOUT_NS_DEF;
 
 	rc = sfc_kvargs_process(sa, SFC_KVARG_RXD_WAIT_TIMEOUT_NS,
-				sfc_kvarg_long_handler, &value);
+				rte_kvargs_handle_long, &value);
 	if (rc != 0)
 		return rc;
 
drivers/net/sfc/sfc_kvargs.c +0 -19
index eb36fa98ca..d67f822dd2 100644
@@ -107,25 +107,6 @@ sfc_kvarg_bool_handler(__rte_unused const char *key,
 	return 0;
 }
 
-int
-sfc_kvarg_long_handler(__rte_unused const char *key,
-		       const char *value_str, void *opaque)
-{
-	long value;
-	char *endptr;
-
-	if (!opaque)
-		return -EINVAL;
-
-	value = strtol(value_str, &endptr, 0);
-	if (endptr == value_str)
-		return -EINVAL;
-
-	*(long *)opaque = value;
-
-	return 0;
-}
-
 int
 sfc_kvarg_string_handler(__rte_unused const char *key,
 			 const char *value_str, void *opaque)
drivers/net/sfc/sfc_kvargs.h +0 -2
index 4dcc61e973..99de772d2d 100644
@@ -88,8 +88,6 @@ int sfc_kvargs_process_opt(struct sfc_adapter *sa, const char *key_match,
 
 int sfc_kvarg_bool_handler(const char *key, const char *value_str,
 			   void *opaque);
-int sfc_kvarg_long_handler(const char *key, const char *value_str,
-			   void *opaque);
 int sfc_kvarg_string_handler(const char *key, const char *value_str,
 			     void *opaque);
 
drivers/net/sfc/sfc_port.c +1 -1
index 33b53f7ac8..d0652b612b 100644
@@ -488,7 +488,7 @@ sfc_port_attach(struct sfc_adapter *sa)
 	kvarg_stats_update_period_ms = SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF;
 
 	rc = sfc_kvargs_process(sa, SFC_KVARG_STATS_UPDATE_PERIOD_MS,
-				sfc_kvarg_long_handler,
+				rte_kvargs_handle_long,
 				&kvarg_stats_update_period_ms);
 	if ((rc == 0) &&
 	    ((kvarg_stats_update_period_ms < 0) ||
Signature · 1 line
2.53.0
#116 lore raw

PATCH v2 48/61 crypto/mvsam: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_integer_arg() uses atoi(), which cannot report an error, so a malformed value such as "abc" becomes zero and the negative check never fires.

---
 drivers/crypto/mvsam/rte_mrvl_pmd.c | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)
drivers/crypto/mvsam/rte_mrvl_pmd.c +3 -19
index 65a63c9d62..4ac9e209df 100644
@@ -1117,22 +1117,6 @@ cryptodev_mrvl_crypto_create(const char *name,
 	return ret;
 }
 
-/** Parse integer from integer argument */
-static int
-parse_integer_arg(const char *key __rte_unused,
-		const char *value, void *extra_args)
-{
-	int *i = (int *) extra_args;
-
-	*i = atoi(value);
-	if (*i < 0) {
-		MRVL_LOG(ERR, "Argument has to be positive!");
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
 /** Parse name */
 static int
 parse_name_arg(const char *key __rte_unused,
@@ -1170,14 +1154,14 @@ mrvl_pmd_parse_input_args(struct mrvl_pmd_init_params *params,
 		/* Common VDEV parameters */
 		ret = rte_kvargs_process(kvlist,
 					 RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
-					 &parse_integer_arg,
+					 rte_kvargs_handle_uint,
 					 &params->common.max_nb_queue_pairs);
 		if (ret < 0)
 			goto free_kvlist;
 
 		ret = rte_kvargs_process(kvlist,
 					 RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
-					 &parse_integer_arg,
+					 rte_kvargs_handle_socket_id,
 					 &params->common.socket_id);
 		if (ret < 0)
 			goto free_kvlist;
@@ -1191,7 +1175,7 @@ mrvl_pmd_parse_input_args(struct mrvl_pmd_init_params *params,
 
 		ret = rte_kvargs_process(kvlist,
 					 MRVL_PMD_MAX_NB_SESS_ARG,
-					 &parse_integer_arg,
+					 rte_kvargs_handle_u32,
 					 &params->max_nb_sessions);
 		if (ret < 0)
 			goto free_kvlist;
Signature · 1 line
2.53.0
#117 lore raw

PATCH v2 49/61 ml/cnxk: use kvargs numeric helpers in cn10k

In reply to Stephen Hemminger

parse_integer_arg() uses atoi(), which cannot report an error, so a malformed value such as "abc" becomes zero and the negative check never fires.

enable_dpe_warnings, report_dpe_warnings, cache_model_data and hw_queue_lock are booleans, so make the fields bool and use rte_kvargs_handle_bool(). The "< 0 || > 1" checks which followed each of them are then dead and go away; the *_set flags stay, since they still distinguish an unset argument from one set to false. A bare key now enables the option, and the usual spellings are accepted.

---
 drivers/ml/cnxk/cn10k_ml_dev.c | 74 +++++++---------------------------
 drivers/ml/cnxk/cn10k_ml_dev.h |  8 ++--
 drivers/ml/cnxk/cn10k_ml_ops.c |  2 +-
 3 files changed, 20 insertions(+), 64 deletions(-)
drivers/ml/cnxk/cn10k_ml_dev.c +15 -59
index c35895dc14..ce96a408c4 100644
@@ -80,20 +80,6 @@ parse_string_arg(const char *key __rte_unused, const char *value, void *extra_ar
 	return 0;
 }
 
-static int
-parse_integer_arg(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	int *i = (int *)extra_args;
-
-	*i = atoi(value);
-	if (*i < 0) {
-		plt_err("Argument has to be positive.");
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
 static int
 cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10k_mldev)
 {
@@ -131,8 +117,9 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_FW_ENABLE_DPE_WARNINGS) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_FW_ENABLE_DPE_WARNINGS,
-					 &parse_integer_arg, &cn10k_mldev->fw.enable_dpe_warnings);
+		ret = rte_kvargs_process_opt(kvlist, CN10K_ML_FW_ENABLE_DPE_WARNINGS,
+					 rte_kvargs_handle_bool,
+					 &cn10k_mldev->fw.enable_dpe_warnings);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
 				CN10K_ML_FW_ENABLE_DPE_WARNINGS);
@@ -143,8 +130,9 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_FW_REPORT_DPE_WARNINGS) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_FW_REPORT_DPE_WARNINGS,
-					 &parse_integer_arg, &cn10k_mldev->fw.report_dpe_warnings);
+		ret = rte_kvargs_process_opt(kvlist, CN10K_ML_FW_REPORT_DPE_WARNINGS,
+					 rte_kvargs_handle_bool,
+					 &cn10k_mldev->fw.report_dpe_warnings);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
 				CN10K_ML_FW_REPORT_DPE_WARNINGS);
@@ -155,7 +143,8 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_DEV_CACHE_MODEL_DATA) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_DEV_CACHE_MODEL_DATA, &parse_integer_arg,
+		ret = rte_kvargs_process_opt(kvlist, CN10K_ML_DEV_CACHE_MODEL_DATA,
+					 rte_kvargs_handle_bool,
 					 &cn10k_mldev->cache_model_data);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
@@ -178,7 +167,8 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_DEV_HW_QUEUE_LOCK) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_DEV_HW_QUEUE_LOCK, &parse_integer_arg,
+		ret = rte_kvargs_process_opt(kvlist, CN10K_ML_DEV_HW_QUEUE_LOCK,
+					 rte_kvargs_handle_bool,
 					 &cn10k_mldev->hw_queue_lock);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
@@ -190,7 +180,7 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_OCM_PAGE_SIZE) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_OCM_PAGE_SIZE, &parse_integer_arg,
+		ret = rte_kvargs_process(kvlist, CN10K_ML_OCM_PAGE_SIZE, rte_kvargs_handle_int,
 					 &cn10k_mldev->ocm_page_size);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s", CN10K_ML_OCM_PAGE_SIZE);
@@ -207,44 +197,18 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 		cn10k_mldev->fw.path = fw_path;
 	plt_info("ML: %s = %s", CN10K_ML_FW_PATH, cn10k_mldev->fw.path);
 
-	if (!enable_dpe_warnings_set) {
+	if (!enable_dpe_warnings_set)
 		cn10k_mldev->fw.enable_dpe_warnings = CN10K_ML_FW_ENABLE_DPE_WARNINGS_DEFAULT;
-	} else {
-		if ((cn10k_mldev->fw.enable_dpe_warnings < 0) ||
-		    (cn10k_mldev->fw.enable_dpe_warnings > 1)) {
-			plt_err("Invalid argument, %s = %d", CN10K_ML_FW_ENABLE_DPE_WARNINGS,
-				cn10k_mldev->fw.enable_dpe_warnings);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_info("ML: %s = %d", CN10K_ML_FW_ENABLE_DPE_WARNINGS,
 		 cn10k_mldev->fw.enable_dpe_warnings);
 
-	if (!report_dpe_warnings_set) {
+	if (!report_dpe_warnings_set)
 		cn10k_mldev->fw.report_dpe_warnings = CN10K_ML_FW_REPORT_DPE_WARNINGS_DEFAULT;
-	} else {
-		if ((cn10k_mldev->fw.report_dpe_warnings < 0) ||
-		    (cn10k_mldev->fw.report_dpe_warnings > 1)) {
-			plt_err("Invalid argument, %s = %d", CN10K_ML_FW_REPORT_DPE_WARNINGS,
-				cn10k_mldev->fw.report_dpe_warnings);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_info("ML: %s = %d", CN10K_ML_FW_REPORT_DPE_WARNINGS,
 		 cn10k_mldev->fw.report_dpe_warnings);
 
-	if (!cache_model_data_set) {
+	if (!cache_model_data_set)
 		cn10k_mldev->cache_model_data = CN10K_ML_DEV_CACHE_MODEL_DATA_DEFAULT;
-	} else {
-		if ((cn10k_mldev->cache_model_data < 0) || (cn10k_mldev->cache_model_data > 1)) {
-			plt_err("Invalid argument, %s = %d", CN10K_ML_DEV_CACHE_MODEL_DATA,
-				cn10k_mldev->cache_model_data);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_info("ML: %s = %d", CN10K_ML_DEV_CACHE_MODEL_DATA, cn10k_mldev->cache_model_data);
 
 	if (!ocm_alloc_mode_set) {
@@ -261,16 +225,8 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 	plt_info("ML: %s = %s", CN10K_ML_OCM_ALLOC_MODE, cn10k_mldev->ocm.alloc_mode);
 
-	if (!hw_queue_lock_set) {
+	if (!hw_queue_lock_set)
 		cn10k_mldev->hw_queue_lock = CN10K_ML_DEV_HW_QUEUE_LOCK_DEFAULT;
-	} else {
-		if ((cn10k_mldev->hw_queue_lock < 0) || (cn10k_mldev->hw_queue_lock > 1)) {
-			plt_err("Invalid argument, %s = %d", CN10K_ML_DEV_HW_QUEUE_LOCK,
-				cn10k_mldev->hw_queue_lock);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_info("ML: %s = %d", CN10K_ML_DEV_HW_QUEUE_LOCK, cn10k_mldev->hw_queue_lock);
 
 	if (!ocm_page_size_set) {
drivers/ml/cnxk/cn10k_ml_dev.h +4 -4
index dadb3b571b..4ca8dc63f4 100644
@@ -108,10 +108,10 @@ struct cn10k_ml_fw {
 	const char *path;
 
 	/* Enable DPE warnings */
-	int enable_dpe_warnings;
+	bool enable_dpe_warnings;
 
 	/* Report DPE warnings */
-	int report_dpe_warnings;
+	bool report_dpe_warnings;
 
 	/* Data buffer */
 	uint8_t *data;
@@ -132,10 +132,10 @@ struct cn10k_ml_dev {
 	struct cn10k_ml_ocm ocm;
 
 	/* Enable / disable model data caching */
-	int cache_model_data;
+	bool cache_model_data;
 
 	/* Use spinlock version of ROC enqueue */
-	int hw_queue_lock;
+	bool hw_queue_lock;
 
 	/* OCM page size */
 	int ocm_page_size;
drivers/ml/cnxk/cn10k_ml_ops.c +1 -1
index 597806b787..732f9b62ae 100644
@@ -377,7 +377,7 @@ cn10k_ml_dev_configure(struct cnxk_ml_dev *cnxk_mldev, const struct rte_ml_dev_c
 	rte_spinlock_init(&ocm->lock);
 
 	/* Set JCMDQ enqueue function */
-	if (cn10k_mldev->hw_queue_lock == 1)
+	if (cn10k_mldev->hw_queue_lock)
 		cn10k_mldev->ml_jcmdq_enqueue = roc_ml_jcmdq_enqueue_sl;
 	else
 		cn10k_mldev->ml_jcmdq_enqueue = roc_ml_jcmdq_enqueue_lf;
Signature · 1 line
2.53.0
#118 lore raw

PATCH v2 50/61 ml/cnxk: use kvargs numeric helpers in mvtvm

In reply to Stephen Hemminger

parse_integer_arg() used atoi(), which cannot report an error, so a malformed value became zero and the negative check never fired. parse_uint_arg() open coded the errno and end pointer checks.

parse_uint_arg() also stored through a uint32_t pointer, but max_nb_qpairs is a uint16_t followed by an int in the same struct, so parsing that argument wrote past the field.

Neither handler did anything beyond a range checked store, so drop them and pass rte_kvargs_handle_u16() directly for max_qps.

cache_model_data is a boolean, so make the field bool and use rte_kvargs_handle_bool(), which makes the "< 0 || > 1" check that followed it dead code. cache_model_data_set stays, since it still tells an unset argument from one set to false. A bare key now enables the option, and the usual spellings are accepted.

Bugzilla ID: 2043

Fixes 48c6081ab4b3 ("ml/cnxk: enable creation of MVTVM virtual device")
---
 drivers/ml/cnxk/mvtvm_ml_dev.c | 47 ++++------------------------------
 drivers/ml/cnxk/mvtvm_ml_dev.h |  2 +-
 2 files changed, 6 insertions(+), 43 deletions(-)
drivers/ml/cnxk/mvtvm_ml_dev.c +5 -42
index 74959a12fc..cbaf17698e 100644
@@ -20,36 +20,6 @@
 
 static const char *const valid_args[] = {MVTVM_ML_DEV_MAX_QPS, MVTVM_ML_DEV_CACHE_MODEL_DATA, NULL};
 
-static int
-parse_integer_arg(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	int *i = (int *)extra_args;
-
-	*i = atoi(value);
-	if (*i < 0) {
-		plt_err("Argument has to be positive.");
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
-static int
-parse_uint_arg(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	int i;
-	char *end;
-	errno = 0;
-
-	i = strtol(value, &end, 10);
-	if (*end != 0 || errno != 0 || i < 0)
-		return -EINVAL;
-
-	*((uint32_t *)extra_args) = i;
-
-	return 0;
-}
-
 static int
 mvtvm_mldev_parse_devargs(const char *args, struct mvtvm_ml_dev *mvtvm_mldev)
 {
@@ -68,7 +38,7 @@ mvtvm_mldev_parse_devargs(const char *args, struct mvtvm_ml_dev *mvtvm_mldev)
 	}
 
 	if (rte_kvargs_count(kvlist, MVTVM_ML_DEV_MAX_QPS) == 1) {
-		ret = rte_kvargs_process(kvlist, MVTVM_ML_DEV_MAX_QPS, &parse_uint_arg,
+		ret = rte_kvargs_process(kvlist, MVTVM_ML_DEV_MAX_QPS, rte_kvargs_handle_u16,
 					 &mvtvm_mldev->max_nb_qpairs);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s", MVTVM_ML_DEV_MAX_QPS);
@@ -79,8 +49,9 @@ mvtvm_mldev_parse_devargs(const char *args, struct mvtvm_ml_dev *mvtvm_mldev)
 	}
 
 	if (rte_kvargs_count(kvlist, MVTVM_ML_DEV_CACHE_MODEL_DATA) == 1) {
-		ret = rte_kvargs_process(kvlist, MVTVM_ML_DEV_CACHE_MODEL_DATA, &parse_integer_arg,
-					 &mvtvm_mldev->cache_model_data);
+		ret = rte_kvargs_process_opt(kvlist, MVTVM_ML_DEV_CACHE_MODEL_DATA,
+					     rte_kvargs_handle_bool,
+					     &mvtvm_mldev->cache_model_data);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
 				MVTVM_ML_DEV_CACHE_MODEL_DATA);
@@ -95,16 +66,8 @@ mvtvm_mldev_parse_devargs(const char *args, struct mvtvm_ml_dev *mvtvm_mldev)
 		mvtvm_mldev->max_nb_qpairs = MVTVM_ML_DEV_MAX_QPS_DEFAULT;
 	plt_ml_dbg("ML: %s = %u", MVTVM_ML_DEV_MAX_QPS, mvtvm_mldev->max_nb_qpairs);
 
-	if (!cache_model_data_set) {
+	if (!cache_model_data_set)
 		mvtvm_mldev->cache_model_data = CN10K_ML_DEV_CACHE_MODEL_DATA_DEFAULT;
-	} else {
-		if ((mvtvm_mldev->cache_model_data < 0) || (mvtvm_mldev->cache_model_data > 1)) {
-			plt_err("Invalid argument, %s = %d", MVTVM_ML_DEV_CACHE_MODEL_DATA,
-				mvtvm_mldev->cache_model_data);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_ml_dbg("ML: %s = %d", MVTVM_ML_DEV_CACHE_MODEL_DATA, mvtvm_mldev->cache_model_data);
 
 exit:
drivers/ml/cnxk/mvtvm_ml_dev.h +1 -1
index 05e30f094c..f8b34ecd3a 100644
@@ -31,7 +31,7 @@ struct mvtvm_ml_dev {
 	uint16_t max_nb_qpairs;
 
 	/* Enable / disable model data caching */
-	int cache_model_data;
+	bool cache_model_data;
 };
 
 #endif /* _MVTVM_ML_DEV_H_ */
Signature · 1 line
2.53.0
#119 lore raw

PATCH v2 51/61 event/opdl: use kvargs numeric helpers

In reply to Stephen Hemminger

All three handlers use atoi(), which cannot report an error, so a malformed value is silently taken as zero. Zero is within the accepted range for each of these arguments, so a typo was accepted and quietly applied rather than rejected.

The booleans use rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 drivers/event/opdl/opdl_evdev.c | 49 ++++++---------------------------
 1 file changed, 9 insertions(+), 40 deletions(-)
drivers/event/opdl/opdl_evdev.c +9 -40
index ffa65ef930..4650d3eca8 100644
@@ -3,6 +3,7 @@
  */
 
 #include <inttypes.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -580,38 +581,6 @@ opdl_close(struct rte_eventdev *dev)
 	return 0;
 }
 
-static int
-assign_numa_node(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *socket_id = opaque;
-	*socket_id = atoi(value);
-	if (*socket_id >= RTE_MAX_NUMA_NODES)
-		return -1;
-	return 0;
-}
-
-static int
-set_do_validation(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *do_val = opaque;
-	*do_val = atoi(value);
-	if (*do_val != 0)
-		*do_val = 1;
-
-	return 0;
-}
-static int
-set_do_test(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *do_test = opaque;
-
-	*do_test = atoi(value);
-
-	if (*do_test != 0)
-		*do_test = 1;
-	return 0;
-}
-
 static int
 opdl_probe(struct rte_vdev_device *vdev)
 {
@@ -650,8 +619,8 @@ opdl_probe(struct rte_vdev_device *vdev)
 	struct rte_eventdev *dev;
 	struct opdl_evdev *opdl;
 	int socket_id = rte_socket_id();
-	int do_validation = 0;
-	int do_test = 0;
+	bool do_validation = false;
+	bool do_test = false;
 	int str_len;
 	int test_result = 0;
 
@@ -666,7 +635,7 @@ opdl_probe(struct rte_vdev_device *vdev)
 					name);
 		} else {
 			int ret = rte_kvargs_process(kvlist, NUMA_NODE_ARG,
-					assign_numa_node, &socket_id);
+					rte_kvargs_handle_socket_id, &socket_id);
 			if (ret != 0) {
 				PMD_DRV_LOG(ERR,
 						"%s: Error parsing numa node parameter",
@@ -676,8 +645,8 @@ opdl_probe(struct rte_vdev_device *vdev)
 				return ret;
 			}
 
-			ret = rte_kvargs_process(kvlist, DO_VALIDATION_ARG,
-					set_do_validation, &do_validation);
+			ret = rte_kvargs_process_opt(kvlist, DO_VALIDATION_ARG,
+					rte_kvargs_handle_bool, &do_validation);
 			if (ret != 0) {
 				PMD_DRV_LOG(ERR,
 					"%s: Error parsing do validation parameter",
@@ -686,8 +655,8 @@ opdl_probe(struct rte_vdev_device *vdev)
 				return ret;
 			}
 
-			ret = rte_kvargs_process(kvlist, DO_TEST_ARG,
-					set_do_test, &do_test);
+			ret = rte_kvargs_process_opt(kvlist, DO_TEST_ARG,
+					rte_kvargs_handle_bool, &do_test);
 			if (ret != 0) {
 				PMD_DRV_LOG(ERR,
 					"%s: Error parsing do test parameter",
@@ -734,7 +703,7 @@ opdl_probe(struct rte_vdev_device *vdev)
 	str_len = strlen(name);
 	memcpy(opdl->service_name, name, str_len);
 
-	if (do_test == 1)
+	if (do_test)
 		test_result =  opdl_selftest();
 
 done:
Signature · 1 line
2.53.0
#120 lore raw

PATCH v2 52/61 mempool/cnxk: use kvargs numeric helpers

In reply to Stephen Hemminger

Both handlers use atoi(), which cannot report an error, so a malformed value is silently taken as zero. For max_pools that then gets clamped up to the minimum, and for halo_ena zero is a valid setting, so neither was rejected.

Since the handlers can now fail, propagate the rte_kvargs_process() return value instead of discarding it.

halo_ena is a boolean, so drop its handler and use rte_kvargs_handle_bool() on a bool. That also removes the store through a uint8_t pointer into what the caller declared as a wider variable, which only worked because the local was zeroed first and the host is little endian. A bare "halo_ena" now enables it, and the usual spellings are accepted.

Bugzilla ID: 2044

Fixes 45abbfc836b1 ("mempool/cnxk: support HALO in mempool")
---
 drivers/mempool/cnxk/cnxk_mempool.c | 37 +++++++++++++----------------
 1 file changed, 16 insertions(+), 21 deletions(-)
drivers/mempool/cnxk/cnxk_mempool.c +16 -21
index 6939fccff4..e5f1b9491b 100644
@@ -2,6 +2,7 @@
  * Copyright(C) 2021 Marvell.
  */
 
+#include <limits.h>
 #include <rte_atomic.h>
 #include <bus_pci_driver.h>
 #include <rte_common.h>
@@ -34,10 +35,14 @@ npa_aura_size_to_u32(uint8_t val)
 static int
 parse_max_pools_handler(const char *key, const char *value, void *extra_args)
 {
+	uint64_t val;
+
 	RTE_SET_USED(key);
-	uint32_t val;
 
-	val = rte_align32pow2(atoi(value));
+	if (rte_kvargs_to_uint(value, 0, INT_MAX, &val) < 0)
+		return -EINVAL;
+
+	val = rte_align32pow2(val);
 	if (val < npa_aura_size_to_u32(NPA_AURA_SZ_128))
 		val = 128;
 	if (val > npa_aura_size_to_u32(NPA_AURA_SZ_1M))
@@ -47,27 +52,14 @@ parse_max_pools_handler(const char *key, const char *value, void *extra_args)
 	return 0;
 }
 
-static int
-parse_halo_ena_handler(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-	uint8_t val;
-
-	val = atoi(value);
-	if (val != 0 && val != 1)
-		return -EINVAL;
-
-	*(uint8_t *)extra_args = val;
-	return 0;
-}
-
 static int
 cnxk_mempool_plt_parse_devargs(struct rte_pci_device *pci_dev)
 {
 	uint32_t max_pools = npa_aura_size_to_u32(NPA_AURA_SZ_128);
 	struct rte_devargs *devargs = pci_dev->device.devargs;
 	struct rte_kvargs *kvlist;
-	uint32_t halo_ena = 0;
+	bool halo_ena = false;
+	int ret;
 
 	if (devargs == NULL)
 		goto null_devargs;
@@ -75,12 +67,15 @@ cnxk_mempool_plt_parse_devargs(struct rte_pci_device *pci_dev)
 	if (kvlist == NULL)
 		goto exit;
 
-	rte_kvargs_process(kvlist, CNXK_NPA_MAX_POOLS_PARAM,
-			   &parse_max_pools_handler, &max_pools);
-	rte_kvargs_process(kvlist, CNXK_NPA_HALO_ENA_PARAM,
-			   &parse_halo_ena_handler, &halo_ena);
+	ret = rte_kvargs_process(kvlist, CNXK_NPA_MAX_POOLS_PARAM,
+				 &parse_max_pools_handler, &max_pools);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_NPA_HALO_ENA_PARAM,
+				      rte_kvargs_handle_bool, &halo_ena);
 	rte_kvargs_free(kvlist);
 
+	if (ret != 0)
+		goto exit;
+
 null_devargs:
 	roc_idev_npa_maxpools_set(max_pools);
 	roc_idev_npa_halo_ena_set(halo_ena);
Signature · 1 line
2.53.0
#121 lore raw

PATCH v2 53/61 event/octeontx: use kvargs numeric helpers

In reply to Stephen Hemminger

ssovf_parsekv() open coded the errno, end pointer and range checks, and the value it parsed was only ever used as a boolean.

Use rte_kvargs_handle_bool() and make the flag a bool to match. The documented "=1" form still works, and the usual spellings such as "on" and "true" are now accepted as well.

The boolean uses rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 drivers/event/octeontx/ssovf_evdev.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +4 -21
index 957fcab04e..4a56d91bf2 100644
@@ -3,6 +3,7 @@
  */
 
 #include <inttypes.h>
+#include <stdbool.h>
 #include <stdlib.h>
 
 #include <rte_common.h>
@@ -23,7 +24,7 @@
 #include "timvf_evdev.h"
 #include "otx_cryptodev_hw_access.h"
 
-static uint8_t timvf_enable_stats;
+static bool timvf_enable_stats;
 
 RTE_LOG_REGISTER_DEFAULT(otx_logtype_ssovf, NOTICE);
 
@@ -716,24 +717,6 @@ ssovf_close(struct rte_eventdev *dev)
 	return 0;
 }
 
-static int
-ssovf_parsekv(const char *key, const char *value, void *opaque)
-{
-	uint8_t *flag = opaque;
-	uint64_t v;
-	char *end;
-
-	errno = 0;
-	v = strtoul(value, &end, 0);
-	if ((errno != 0) || (value == end) || *end != '\0' || v > 1) {
-		ssovf_log_err("invalid %s value %s", key, value);
-		return -EINVAL;
-	}
-
-	*flag = !!v;
-	return 0;
-}
-
 static int
 ssovf_timvf_caps_get(const struct rte_eventdev *dev, uint64_t flags,
 		     uint32_t *caps, const struct event_timer_adapter_ops **ops)
@@ -879,8 +862,8 @@ ssovf_vdev_probe(struct rte_vdev_device *vdev)
 				"Ignoring unsupported params supplied '%s'",
 				name);
 		} else {
-			ret = rte_kvargs_process(kvlist, TIMVF_ENABLE_STATS_ARG,
-						 ssovf_parsekv,
+			ret = rte_kvargs_process_opt(kvlist, TIMVF_ENABLE_STATS_ARG,
+						     rte_kvargs_handle_bool,
 						 &timvf_enable_stats);
 			if (ret != 0) {
 				ssovf_log_err("%s: Error in timvf stats", name);
Signature · 1 line
2.53.0
#122 lore raw

PATCH v2 54/61 baseband/turbo_sw: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_u16_arg() never checked the end pointer, so a value such as "8junk" was accepted, and it used base 0, so a leading zero silently selected octal.

It was also used for both device arguments, but only queues_num is a uint16_t; socket_id is an int, so parsing it wrote two bytes into a four byte object and left the rest uninitialised.

The handler did nothing beyond a range checked store, so drop it and pass rte_kvargs_handle_u16() and rte_kvargs_handle_socket_id() directly, each matching the width of the field it fills.

The open coded range check only tested the upper bound, so a negative socket id such as -5 was accepted and used. The helper checks both ends, accepting -1, which is SOCKET_ID_ANY, through RTE_MAX_NUMA_NODES - 1, so the check is now redundant and is removed.

---
 .../baseband/turbo_sw/bbdev_turbo_software.c  | 29 ++-----------------
 1 file changed, 2 insertions(+), 27 deletions(-)
drivers/baseband/turbo_sw/bbdev_turbo_software.c +2 -27
index 2cab495294..d6801d9123 100644
@@ -1835,25 +1835,6 @@ dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
 	return nb_dequeued;
 }
 
-/* Parse 16bit integer from string argument */
-static inline int
-parse_u16_arg(const char *key, const char *value, void *extra_args)
-{
-	uint16_t *u16 = extra_args;
-	unsigned int long result;
-
-	if ((value == NULL) || (extra_args == NULL))
-		return -EINVAL;
-	errno = 0;
-	result = strtoul(value, NULL, 0);
-	if ((result >= (1 << 16)) || (errno != 0)) {
-		rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
-		return -ERANGE;
-	}
-	*u16 = (uint16_t)result;
-	return 0;
-}
-
 /* Parse parameters used to create device */
 static int
 parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
@@ -1869,20 +1850,14 @@ parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
 			return -EFAULT;
 
 		ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[0],
-					&parse_u16_arg, &params->queues_num);
+					rte_kvargs_handle_u16, &params->queues_num);
 		if (ret < 0)
 			goto exit;
 
 		ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[1],
-					&parse_u16_arg, &params->socket_id);
+					rte_kvargs_handle_socket_id, &params->socket_id);
 		if (ret < 0)
 			goto exit;
-
-		if (params->socket_id >= RTE_MAX_NUMA_NODES) {
-			rte_bbdev_log(ERR, "Invalid socket, must be < %u",
-					RTE_MAX_NUMA_NODES);
-			goto exit;
-		}
 	}
 
 exit:
Signature · 1 line
2.53.0
#123 lore raw

PATCH v2 55/61 baseband/la12xx: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_u16_arg() stored through a uint16_t pointer and parse_integer_arg() through a uint32_t pointer, but both device arguments are 8 bit fields:
queues_num is a uint8_t and modem_id an int8_t, and they are adjacent in struct bbdev_la12xx_params. Parsing either argument therefore wrote past the field and corrupted its neighbour.

parse_u16_arg() did nothing beyond a range checked store, so drop it and pass rte_kvargs_handle_u8() directly, which also gives queues_num the width it actually has.

The modem id keeps a handler of its own. parse_integer_arg() rejected both ends of the range, and a plain rte_kvargs_handle_i8() would accept anything down to -128 while the caller only checks the upper bound, so "modem=-5" would be stored and only fail later in open_ipc_dev(). The new handler bounds it to -1..LA12XX_MAX_MODEM-1, keeping -1 which la12xx_bbdev_create() uses to mean "derive the modem id from the device id", and replaces the caller's upper bound check.

The handler now rejects an out of range modem id, where the old check did a "goto exit" with ret still zero. The probe also checks the result of parse_bbdev_la12xx_params(), which it used to discard, so a bad modem id or queue count now fails the probe instead of only being logged.

Fixes ee36ba0f3042 ("baseband/la12xx: add devargs option for max queues")
---
 drivers/baseband/la12xx/bbdev_la12xx.c | 60 +++++++++-----------------
 1 file changed, 20 insertions(+), 40 deletions(-)
drivers/baseband/la12xx/bbdev_la12xx.c +20 -40
index 1116dacc4b..b9199189e7 100644
@@ -912,44 +912,25 @@ setup_la12xx_dev(struct rte_bbdev *dev)
 	return ret;
 }
 
-static inline int
-parse_u16_arg(const char *key, const char *value, void *extra_args)
-{
-	uint16_t *u16 = extra_args;
-
-	uint64_t result;
-	if ((value == NULL) || (extra_args == NULL))
-		return -EINVAL;
-	errno = 0;
-	result = strtoul(value, NULL, 0);
-	if ((result >= (1 << 16)) || (errno != 0)) {
-		rte_bbdev_log(ERR, "Invalid value %" PRIu64 " for %s",
-			      result, key);
-		return -ERANGE;
-	}
-	*u16 = (uint16_t)result;
-	return 0;
-}
-
-/* Parse integer from integer argument */
+/*
+ * Parse the modem id.
+ *
+ * A dedicated handler is needed because -1 is meaningful here: it leaves the
+ * modem id unset, and la12xx_bbdev_create() then derives it from the device
+ * id. rte_kvargs_handle_i8() alone would also accept anything down to -128.
+ */
 static int
-parse_integer_arg(const char *key __rte_unused,
-		const char *value, void *extra_args)
+parse_modem_id(const char *key, const char *value, void *extra_args)
 {
-	int i;
-	char *end;
-
-	errno = 0;
+	int64_t modem_id;
 
-	i = strtol(value, &end, 10);
-	if (*end != 0 || errno != 0 || i < 0 || i > LA12XX_MAX_MODEM) {
-		rte_bbdev_log(ERR, "Supported Port IDS are 0 to %d",
-			LA12XX_MAX_MODEM - 1);
+	if (rte_kvargs_to_int(value, -1, LA12XX_MAX_MODEM - 1, &modem_id) < 0) {
+		rte_bbdev_log(ERR, "Invalid %s, must be -1..%u", key,
+				LA12XX_MAX_MODEM - 1);
 		return -EINVAL;
 	}
 
-	*((uint32_t *)extra_args) = i;
-
+	*(int8_t *)extra_args = modem_id;
 	return 0;
 }
 
@@ -970,20 +951,16 @@ parse_bbdev_la12xx_params(struct bbdev_la12xx_params *params,
 			return -EFAULT;
 
 		ret = rte_kvargs_process(kvlist, bbdev_la12xx_valid_params[0],
-					&parse_u16_arg, &params->queues_num);
+					rte_kvargs_handle_u8, &params->queues_num);
 		if (ret < 0)
 			goto exit;
 
 		ret = rte_kvargs_process(kvlist,
 					bbdev_la12xx_valid_params[1],
-					&parse_integer_arg,
+					parse_modem_id,
 					&params->modem_id);
-
-		if (params->modem_id >= LA12XX_MAX_MODEM) {
-			rte_bbdev_log(ERR, "Invalid modem id, must be < %u",
-					LA12XX_MAX_MODEM);
+		if (ret < 0)
 			goto exit;
-		}
 	}
 
 exit:
@@ -1062,6 +1039,7 @@ la12xx_bbdev_probe(struct rte_vdev_device *vdev)
 	};
 	const char *name;
 	const char *input_args;
+	int ret;
 
 	PMD_INIT_FUNC_TRACE();
 
@@ -1073,7 +1051,9 @@ la12xx_bbdev_probe(struct rte_vdev_device *vdev)
 		return -EINVAL;
 
 	input_args = rte_vdev_device_args(vdev);
-	parse_bbdev_la12xx_params(&init_params, input_args);
+	ret = parse_bbdev_la12xx_params(&init_params, input_args);
+	if (ret < 0)
+		return ret;
 
 	return la12xx_bbdev_create(vdev, &init_params);
 }
Signature · 1 line
2.53.0
#124 lore raw

PATCH v2 56/61 dma/hisi_acc: use kvargs numeric helpers

In reply to Stephen Hemminger

hacc_dma_parse_queues() open codes the errno and end pointer checks the helper performs. A queue count of zero is rejected by making one the minimum of the accepted range, as before.

---
 drivers/dma/hisi_acc/hisi_acc_dmadev.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/dma/hisi_acc/hisi_acc_dmadev.c +1 -4
index 196aaadc05..7db93852a3 100644
@@ -660,13 +660,10 @@ hacc_dma_parse_queues(const char *key, const char *value, void *extra_args)
 {
 	struct hacc_dma_config *config = extra_args;
 	uint64_t val;
-	char *end;
 
 	RTE_SET_USED(key);
 
-	errno = 0;
-	val = strtoull(value, &end, 0);
-	if (errno == ERANGE || value == end || *end != '\0' || val == 0) {
+	if (rte_kvargs_to_uint(value, 1, UINT64_MAX, &val) < 0) {
 		HACC_DMA_LOG(ERR, "%s invalid queues! set to default one queue!",
 			    config->dev->name);
 		config->queues = HACC_DMA_DEFAULT_QUEUES;
Signature · 1 line
2.53.0
#125 lore raw

PATCH v2 57/61 crypto/virtio: use kvargs numeric helpers

In reply to Stephen Hemminger

get_integer_arg() never checks the end pointer and returns -errno, so a malformed value such as "abc" leaves errno untouched, converts to zero and is reported as success. It also used base 0, so a leading zero silently selected octal.

---
 drivers/crypto/virtio/virtio_user_cryptodev.c | 21 ++-----------------
 1 file changed, 2 insertions(+), 19 deletions(-)
drivers/crypto/virtio/virtio_user_cryptodev.c +2 -19
index 4daa188e1d..9844debeaf 100644
@@ -345,23 +345,6 @@ get_string_arg(const char *key __rte_unused,
 	return 0;
 }
 
-static int
-get_integer_arg(const char *key __rte_unused,
-		const char *value, void *extra_args)
-{
-	uint64_t integer = 0;
-	if (!value || !extra_args)
-		return -EINVAL;
-	errno = 0;
-	integer = strtoull(value, NULL, 0);
-	/* extra_args keeps default value, it should be replaced
-	 * only in case of successful parsing of the 'value' arg
-	 */
-	if (errno == 0)
-		*(uint64_t *)extra_args = integer;
-	return -errno;
-}
-
 static struct rte_cryptodev *
 virtio_user_cryptodev_alloc(struct rte_vdev_device *vdev)
 {
@@ -432,7 +415,7 @@ virtio_user_pmd_probe(struct rte_vdev_device *vdev)
 
 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
-					&get_integer_arg, &queues) < 0) {
+					rte_kvargs_handle_u64, &queues) < 0) {
 			PMD_INIT_LOG(ERR, "error to parse %s",
 					VIRTIO_USER_ARG_QUEUES_NUM);
 			goto end;
@@ -441,7 +424,7 @@ virtio_user_pmd_probe(struct rte_vdev_device *vdev)
 
 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
-					&get_integer_arg, &queue_size) < 0) {
+					rte_kvargs_handle_u64, &queue_size) < 0) {
 			PMD_INIT_LOG(ERR, "error to parse %s",
 					VIRTIO_USER_ARG_QUEUE_SIZE);
 			goto end;
Signature · 1 line
2.53.0
#126 lore raw

PATCH v2 58/61 net/ice: use kvargs hex helper for debug mask

In reply to Stephen Hemminger

parse_u64() checks errno but not the end pointer, so "hw_debug_mask=junk" is silently accepted as zero, and a trailing-garbage value such as "0x80zz" is taken as 0x80.

The value is documented as a hexadecimal mask, so use rte_kvargs_handle_hex64(), which reads it as hexadecimal with or without a 0x prefix and validates the whole string. The documented form "hw_debug_mask=0x80" is unchanged, and so is a bare "80".

---
 drivers/net/intel/ice/ice_ethdev.c | 21 +--------------------
 1 file changed, 1 insertion(+), 20 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +1 -20
index 4c3db27812..a1ed6b5b6f 100644
@@ -2144,25 +2144,6 @@ ice_base_queue_get(struct ice_pf *pf)
 	}
 }
 
-static int
-parse_u64(const char *key, const char *value, void *args)
-{
-	u64 *num = (u64 *)args;
-	u64 tmp;
-
-	errno = 0;
-	tmp = strtoull(value, NULL, 16);
-	if (errno) {
-		PMD_DRV_LOG(WARNING, "%s: \"%s\" is not a valid u64",
-			    key, value);
-		return -1;
-	}
-
-	*num = tmp;
-
-	return 0;
-}
-
 static int
 parse_tx_sched_levels(const char *key, const char *value, void *args)
 {
@@ -2427,7 +2408,7 @@ static int ice_parse_devargs(struct rte_eth_dev *dev)
 		goto bail;
 
 	ret = rte_kvargs_process(kvlist, ICE_HW_DEBUG_MASK_ARG,
-				 &parse_u64, &ad->hw.debug_mask);
+				 rte_kvargs_handle_hex64, &ad->hw.debug_mask);
 	if (ret)
 		goto bail;
 
Signature · 1 line
2.53.0
#127 lore raw

PATCH v2 59/61 net/hns3: use kvargs hex helper for capability mask

In reply to Stephen Hemminger

hns3_parse_dev_caps_mask() does no validation whatsoever: strtoull() is called with neither errno cleared nor the end pointer checked, so "dev_caps_mask=junk" is silently taken as zero and "dev_caps_mask=-1" wraps around to enable every capability.

The value is documented as a hexadecimal mask, so use rte_kvargs_handle_hex64(), which reads it as hexadecimal with or without a 0x prefix. The documented form "dev_caps_mask=0xF" is unchanged, and so is a bare "F".

Note that hns3_parse_devargs() discards the rte_kvargs_process() return value for every argument it parses, so a malformed mask is now reported but still leaves the default of zero in place rather than failing probe. Propagating those errors is a behaviour change per argument and is left for a separate patch.

HNS3_CONVERT_TO_HEXADECIMAL loses its last user here, and HNS3_CONVERT_TO_DECIMAL lost its own earlier in this series, so remove both.

---
 drivers/net/hns3/hns3_common.c | 18 +-----------------
 drivers/net/hns3/hns3_common.h |  3 ---
 2 files changed, 1 insertion(+), 20 deletions(-)
drivers/net/hns3/hns3_common.c +1 -17
index c21740ba5e..35cc3fca40 100644
@@ -205,22 +205,6 @@ hns3_get_io_hint_func_name(uint32_t hint)
 	}
 }
 
-static int
-hns3_parse_dev_caps_mask(const char *key, const char *value, void *extra_args)
-{
-	uint64_t val;
-
-	RTE_SET_USED(key);
-
-	if (value == NULL || extra_args == NULL)
-		return 0;
-
-	val = strtoull(value, NULL, HNS3_CONVERT_TO_HEXADECIMAL);
-	*(uint64_t *)extra_args = val;
-
-	return 0;
-}
-
 static int
 hns3_parse_mbx_time_limit(const char *key, const char *value, void *extra_args)
 {
@@ -341,7 +325,7 @@ hns3_parse_devargs(struct rte_eth_dev *dev)
 	(void)rte_kvargs_process(kvlist, HNS3_DEVARG_TX_FUNC_HINT,
 			   &hns3_parse_io_hint_func, &tx_func_hint);
 	(void)rte_kvargs_process(kvlist, HNS3_DEVARG_DEV_CAPS_MASK,
-			   &hns3_parse_dev_caps_mask, &dev_caps_mask);
+			   rte_kvargs_handle_hex64, &dev_caps_mask);
 	(void)rte_kvargs_process(kvlist, HNS3_DEVARG_MBX_TIME_LIMIT_MS,
 			   &hns3_parse_mbx_time_limit, &mbx_time_limit_ms);
 	if (!hns->is_vf) {
drivers/net/hns3/hns3_common.h +0 -3
index 7b3f96b01a..967cde9e39 100644
@@ -9,9 +9,6 @@
 
 #include "hns3_ethdev.h"
 
-#define HNS3_CONVERT_TO_DECIMAL 10
-#define HNS3_CONVERT_TO_HEXADECIMAL 16
-
 enum {
 	HNS3_IO_FUNC_HINT_NONE = 0,
 	HNS3_IO_FUNC_HINT_VEC,
Signature · 1 line
2.53.0
#128 lore raw

PATCH v2 60/61 net/cxgbe: use kvargs numeric helpers

In reply to Stephen Hemminger

check_devargs_handler() is a single handler shared by all five device arguments, which dispatches on the key to decide how to parse the value.

The dispatch is done with strncmp(key, NAME, strlen(key)), which compares only as many bytes as the key given by the user is long, so a short key matches a longer name by prefix. That is harmless today because rte_kvargs_process() has already selected the pairs by exact key, but it means the handler cannot tell which argument it was called for except by accident.

The filtermode and filtermask branch checks errno without clearing it first, so a stale ERANGE from unrelated earlier code rejects a valid mask. The boolean branch tests only for "1" and silently ignores anything else, so "keep_ovlan=yes" leaves the default in place without complaint.

Pass the handler to cxgbe_get_devargs() instead of having one handler work out what it was called for: rte_kvargs_handle_hex32() for the two masks, which are documented as hexadecimal, and rte_kvargs_handle_bool() for the three flags.

The masks are still read as hexadecimal with or without a 0x prefix, so "filtermode=44" is unchanged. The flags now also accept "y", "yes", "on" and "true" as well as "1", and a value which is not a boolean at all is rejected rather than ignored.

---
 drivers/net/cxgbe/cxgbe_main.c | 38 ++++++----------------------------
 1 file changed, 6 insertions(+), 32 deletions(-)
drivers/net/cxgbe/cxgbe_main.c +6 -32
index 2ed21f2d66..e0eefae6c1 100644
@@ -744,36 +744,8 @@ void cxgbe_print_port_info(struct adapter *adap)
 	}
 }
 
-static int check_devargs_handler(const char *key, const char *value, void *p)
-{
-	if (!strncmp(key, CXGBE_DEVARG_CMN_KEEP_OVLAN, strlen(key)) ||
-	    !strncmp(key, CXGBE_DEVARG_CMN_TX_MODE_LATENCY, strlen(key)) ||
-	    !strncmp(key, CXGBE_DEVARG_VF_FORCE_LINK_UP, strlen(key))) {
-		if (!strncmp(value, "1", 1)) {
-			bool *dst_val = (bool *)p;
-
-			*dst_val = true;
-		}
-	}
-
-	if (!strncmp(key, CXGBE_DEVARG_PF_FILTER_MODE, strlen(key)) ||
-	    !strncmp(key, CXGBE_DEVARG_PF_FILTER_MASK, strlen(key))) {
-		u32 *dst_val = (u32 *)p;
-		char *endptr = NULL;
-		u32 arg_val;
-
-		arg_val = strtoul(value, &endptr, 16);
-		if (errno || endptr == value)
-			return -EINVAL;
-
-		*dst_val = arg_val;
-	}
-
-	return 0;
-}
-
 static int cxgbe_get_devargs(struct rte_devargs *devargs, const char *key,
-			     void *p)
+			     arg_handler_t handler, void *p)
 {
 	struct rte_kvargs *kvlist;
 	int ret = 0;
@@ -788,7 +760,7 @@ static int cxgbe_get_devargs(struct rte_devargs *devargs, const char *key,
 	if (!rte_kvargs_count(kvlist, key))
 		goto out;
 
-	ret = rte_kvargs_process(kvlist, key, check_devargs_handler, p);
+	ret = rte_kvargs_process(kvlist, key, handler, p);
 
 out:
 	rte_kvargs_free(kvlist);
@@ -807,7 +779,8 @@ static void cxgbe_get_devargs_int(struct adapter *adap, bool *dst,
 	if (!pdev)
 		return;
 
-	ret = cxgbe_get_devargs(pdev->device.devargs, key, &devarg_value);
+	ret = cxgbe_get_devargs(pdev->device.devargs, key,
+				rte_kvargs_handle_bool, &devarg_value);
 	if (ret)
 		return;
 
@@ -825,7 +798,8 @@ static void cxgbe_get_devargs_u32(struct adapter *adap, u32 *dst,
 	if (!pdev)
 		return;
 
-	ret = cxgbe_get_devargs(pdev->device.devargs, key, &devarg_value);
+	ret = cxgbe_get_devargs(pdev->device.devargs, key,
+				rte_kvargs_handle_hex32, &devarg_value);
 	if (ret)
 		return;
 
Signature · 1 line
2.53.0
#129 lore raw

PATCH v2 61/61 doc: note kvargs devargs conversion in release notes

In reply to Stephen Hemminger

Record that the drivers were converted to the new kvargs helpers, and what a user should expect to change: a device argument which is malformed or out of range is now reported and rejected, where before it was commonly truncated, wrapped around, or taken as zero.

This is kept as a separate patch at the end of the series rather than folded into the last driver, so that the note does not depend on any one driver patch surviving review.

The individual drivers are not listed. At 52 entries the list was too long to be useful, and it meant a third of the driver patches touched the release notes only to append a bullet.

---
 doc/guides/rel_notes/release_26_11.rst | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
doc/guides/rel_notes/release_26_11.rst +18 -0
index af19196ba3..e2aa2fd48e 100644
@@ -164,6 +164,24 @@ New Features
   ``rte_kvargs_to_hex`` for the cases where a driver needs a narrower range
   than the target type allows.
 
+  52 drivers were converted to use these helpers. A device argument which
+  is malformed or out of range is now reported and rejected, where before
+  it was commonly truncated, wrapped around, or taken as zero.
+
+  The device arguments which are booleans were converted along with them.
+  Those now take any of the spellings above rather than only ``0`` and
+  ``1``, and ``key`` on its own means true. A value which is not a
+  boolean is rejected, where several drivers previously read it as false.
+  That includes an empty ``key=``, which ``net/pcap`` used to take as
+  true for ``infinite_rx``.
+
+  Two of these change what an existing argument means. ``event/dlb2``
+  only looked for a leading ``y`` in ``vector_opts_enable``,
+  ``default_ldb_port_allocation`` and ``enable_cq_weight``, so ``=1``
+  used to mean false and now means true. ``net/virtio`` compared
+  ``vectorized`` against ``"1"``, so ``vectorized=y`` used to mean false
+  and now means true.
+
 
 Removed Items
 -------------
Signature · 1 line
2.53.0
#130 lore raw

Re: [PATCH v2 00/61] kvargs: add numeric conversion helpers

In reply to Stephen Hemminger
Quoted 18 lines · Stephen Hemminger

Almost every driver parses numeric device arguments with its own code, and most get some part of it wrong: atoi() does not detect invalid input, errno is checked without being cleared, the end pointer is ignored, the value is truncated before it is range checked, or base 0 turns a leading zero into octal.

This series adds range checked converters and ready made rte_kvargs_process() handlers to librte_kvargs, then converts the drivers.

Library (patches 1 to 3)
------------------------

rte_kvargs_to_uint/to_int/to_hex convert with an explicit range

rte_kvargs_handle_u8/u16/u32/u64 store into a typed variable
rte_kvargs_handle_i8/i16/i32/i64
rte_kvargs_handle_uint/int/long/ulong/size

Quoted 5 lines

rte_kvargs_handle_bool 1/y/yes/on/true and negatives,
bare key means true
rte_kvargs_handle_socket_id -1 (SOCKET_ID_ANY) through
RTE_MAX_NUMA_NODES - 1
rte_kvargs_handle_hex32/hex64 bare hex mask, 0x optional

The argparse also provide such convert functions
int rte_argparse_parse_type(const char *str, enum rte_argparse_value_type val_type, void *val);

Can these be merged into one?

Quoted 7 lines

Values are decimal or 0x hexadecimal, no octal, no negative value for an unsigned type, no trailing garbage.
The handlers log key, value, and expected range on failure. Unit tests cover all of them.

The new api's are marked experimental for 26.11.

#131 lore raw

Re: [PATCH v2 00/61] kvargs: add numeric conversion helpers

Quoted 16 lines · On Wed, 30 Sep 2026 09:14:22 +0800 fengchengwen <[email protected]> wrote:
Quoted 10 lines

Library (patches 1 to 3)
------------------------

rte_kvargs_to_uint/to_int/to_hex convert with an explicit range

rte_kvargs_handle_u8/u16/u32/u64 store into a typed variable
rte_kvargs_handle_i8/i16/i32/i64
rte_kvargs_handle_uint/int/long/ulong/size

Quoted 5 lines

rte_kvargs_handle_bool 1/y/yes/on/true and negatives,
bare key means true
rte_kvargs_handle_socket_id -1 (SOCKET_ID_ANY) through
RTE_MAX_NUMA_NODES - 1
rte_kvargs_handle_hex32/hex64 bare hex mask, 0x optional

Quoted 4 lines

The argparse also provide such convert functions
int rte_argparse_parse_type(const char *str, enum rte_argparse_value_type val_type, void *val);

Can these be merged into one?

No because argparse takes different function signature than kvargs. The point of these helpers was to be drop in replacement in drivers. The two overlap, but kvargs covers a few more cases.

#132 lore raw

v3 cover kvargs: add numeric conversion helpers

In reply to Stephen Hemminger

Almost every driver parses numeric device arguments with its own code, and most get some part of it wrong: atoi() does not detect invalid input, errno is checked without being cleared, the end pointer is ignored, the value is truncated before it is range checked, or base 0 turns a leading zero into octal.

This series adds range checked converters and ready made rte_kvargs_process() handlers to librte_kvargs, then converts the drivers.

Patch 1 is a type fix to drivers/net/ring so that it can use helper for socket id.

Library (patches 2 to 4)
------------------------

rte_kvargs_to_uint/to_int/to_hex convert with an explicit range

rte_kvargs_handle_u8/u16/u32/u64 store into a typed variable
rte_kvargs_handle_i8/i16/i32/i64
rte_kvargs_handle_uint/int/long/ulong/size

rte_kvargs_handle_bool 1/y/yes/on/true and negatives,
bare key means true
rte_kvargs_handle_socket_id -1 (SOCKET_ID_ANY) through
RTE_MAX_NUMA_NODES - 1
rte_kvargs_handle_hex32/hex64 bare hex mask, 0x optional
rte_kvargs_handle_urange/irange caller supplied bounds

Values are decimal or 0x hexadecimal, no octal, no negative value for an unsigned type, no trailing garbage.
The handlers log key, value, and expected range on failure. Unit tests cover all of them.

The new api's are marked experimental for 26.11.

Drivers (patches 6 to 61)
-------------------------

52 drivers converted. Local handlers that only did a range checked store are deleted, 40 of them.

The af_xdp conversion (patch 17) goes further than the rest. With some changes to types, like queue and indexes converted to uint16_t, the local handlers can be replaced by helpers. Likewise boolean flags can be bool instead of int.

There are some cases not covered easily by kvargs helpers, where the driver parses a list, or does more with the value than store it.

Range handlers (patches 63 to 65)
---------------------------------

An argument whose range is narrower than its type still needed a local handler, about 25 of them, and 22 of those rejected a bad value without logging anything. The new handlers take the bounds from the caller, so these report like the typed ones do.

event/sw and net/cnxk are converted, 9 more handlers deleted.

Behaviour changes
-----------------

- malformed, out of range and wrapped values now fail probe with a message instead of being silently misread
- a leading zero is decimal, not octal
- boolean arguments accept on/off/yes/no/true/false and reject anything else, including numbers other than 0 and 1
- socket IDs are checked at both ends (dlb2 accepted RTE_MAX_NUMA_NODES, turbo_sw and bbdev_null accepted negatives)
- the four bare hex masks (ice hw_debug_mask, hns3 dev_caps_mask, cxgbe filtermode/filtermask, ark Pkt_dir) keep their syntax

v3 - add range helper
- declutter the AI generated comments

Stephen Hemminger (65):
net/ring: make numa node signed
kvargs: add numeric conversion helpers
kvargs: add a socket ID handler
kvargs: add hexadecimal conversion helpers
eal: validate memory size arguments net/null: use kvargs numeric helpers net/vhost: use kvargs numeric helpers vdpa/ifc: use kvargs numeric helpers net/softnic: use kvargs numeric helpers dma/skeleton: use kvargs numeric helpers raw/skeleton: use kvargs numeric helpers baseband/null: use kvargs numeric helpers net/memif: use kvargs numeric helpers net/af_packet: use kvargs numeric helpers net/pcap: use kvargs numeric helpers net/ring: use kvargs numeric helpers net/af_xdp: use kvargs numeric helpers net/ark: use kvargs numeric helpers net/failsafe: use kvargs numeric helpers net/virtio: use kvargs numeric helpers net/bonding: use kvargs numeric helpers net/ena: use kvargs numeric helpers net/netvsc: use kvargs numeric helpers net/ice: use kvargs numeric helpers net/iavf: use kvargs numeric helpers net/i40e: use kvargs numeric helpers net/idpf: use kvargs numeric helpers net/cpfl: use kvargs numeric helpers net/ixgbe: use kvargs numeric helpers net/txgbe: use kvargs numeric helpers net/octeontx: use kvargs numeric helpers net/octeon_ep: use kvargs numeric helpers net/qede: use kvargs numeric helpers net/nfb: use kvargs numeric helpers net/thunderx: use kvargs numeric helpers net/i40e: propagate VF queue number parse errors net/cnxk: use kvargs numeric helpers net/xsc: use kvargs numeric helpers net/hns3: use kvargs numeric helpers net/enetc: use kvargs numeric helpers event/dlb2: use kvargs numeric helpers net/nfp: use kvargs numeric helpers drivers/crypto: use kvargs numeric helpers event/sw: use kvargs numeric helpers net/bnxt: use kvargs numeric helpers net/bnxt: propagate devargs parsing errors net/mlx4: use kvargs numeric helpers net/sfc: use kvargs numeric helpers crypto/mvsam: use kvargs numeric helpers ml/cnxk: use kvargs numeric helpers in cn10k ml/cnxk: use kvargs numeric helpers in mvtvm event/opdl: use kvargs numeric helpers mempool/cnxk: use kvargs numeric helpers event/octeontx: use kvargs numeric helpers baseband/turbo_sw: use kvargs numeric helpers baseband/la12xx: use kvargs numeric helpers dma/hisi_acc: use kvargs numeric helpers crypto/virtio: use kvargs numeric helpers net/ice: use kvargs hex helper for debug mask net/hns3: use kvargs hex helper for capability mask net/cxgbe: use kvargs numeric helpers
kvargs: add range conversion handlers event/sw: use kvargs range handlers net/cnxk: use kvargs range handlers
doc: note kvargs devargs conversion in release notes

 app/test/test_kvargs.c                        | 340 ++++++++++
 doc/guides/eventdevs/dlb2.rst                 |   4 +-
 doc/guides/nics/null.rst                      |   2 +-
 doc/guides/prog_guide/devargs.rst             |  18 +
 doc/guides/rel_notes/release_26_11.rst        |  57 ++
 drivers/baseband/la12xx/bbdev_la12xx.c        |  60 +-
 drivers/baseband/null/bbdev_null.c            |  29 +-
 .../baseband/turbo_sw/bbdev_turbo_software.c  |  29 +-
 drivers/crypto/dpaa2_sec/dpaa2_sec_dpseci.c   |  15 +-
 drivers/crypto/dpaa_sec/dpaa_sec.c            |  17 +-
 drivers/crypto/mvsam/rte_mrvl_pmd.c           |  22 +-
 drivers/crypto/virtio/virtio_user_cryptodev.c |  21 +-
 drivers/dma/hisi_acc/hisi_acc_dmadev.c        |   5 +-
 drivers/dma/skeleton/skeleton_dmadev.c        |  23 +-
 drivers/event/dlb2/dlb2.c                     | 127 +---
 drivers/event/octeontx/ssovf_evdev.c          |  25 +-
 drivers/event/opdl/opdl_evdev.c               |  49 +-
 drivers/event/sw/sw_evdev.c                   | 107 +---
 drivers/mempool/cnxk/cnxk_mempool.c           |  37 +-
 drivers/ml/cnxk/cn10k_ml_dev.c                |  74 +--
 drivers/ml/cnxk/cn10k_ml_dev.h                |   8 +-
 drivers/ml/cnxk/cn10k_ml_ops.c                |   2 +-
 drivers/ml/cnxk/mvtvm_ml_dev.c                |  47 +-
 drivers/ml/cnxk/mvtvm_ml_dev.h                |   2 +-
 drivers/net/af_packet/rte_eth_af_packet.c     |  72 +--
 drivers/net/af_xdp/rte_eth_af_xdp.c           | 431 ++++++-------
 drivers/net/ark/ark_ethdev.c                  |   4 +-
 drivers/net/bnxt/bnxt_ethdev.c                | 148 ++---
 drivers/net/bonding/eth_bond_private.h        |   8 -
 drivers/net/bonding/rte_eth_bond_args.c       |  71 +-
 drivers/net/bonding/rte_eth_bond_pmd.c        |   8 +-
 drivers/net/cnxk/cnxk_ethdev_devargs.c        | 353 +++-------
 drivers/net/cxgbe/cxgbe_main.c                |  38 +-
 drivers/net/ena/ena_ethdev.c                  |  45 +-
 drivers/net/enetc/enetc4_vf.c                 |  22 +-
 drivers/net/failsafe/failsafe_args.c          |  20 +-
 drivers/net/hns3/hns3_common.c                |  23 +-
 drivers/net/hns3/hns3_common.h                |   3 -
 drivers/net/intel/cpfl/cpfl_ethdev.c          |  26 +-
 drivers/net/intel/i40e/i40e_ethdev.c          |  57 +-
 drivers/net/intel/iavf/iavf.h                 |   6 +-
 drivers/net/intel/iavf/iavf_ethdev.c          |  55 +-
 drivers/net/intel/ice/ice_ethdev.c            | 123 +---
 drivers/net/intel/ice/ice_ethdev.h            |  10 +-
 drivers/net/intel/idpf/idpf_ethdev.c          |  26 +-
 drivers/net/intel/ixgbe/ixgbe_ethdev.c        |  40 +-
 drivers/net/intel/ixgbe/ixgbe_ethdev.h        |   4 +-
 drivers/net/memif/rte_eth_memif.c             |  73 +--
 drivers/net/mlx4/mlx4.c                       |  33 +-
 drivers/net/netvsc/hn_ethdev.c                |   6 +-
 drivers/net/nfb/nfb_ethdev.c                  |  13 +-
 drivers/net/nfp/nfp_ethdev.c                  |  36 +-
 drivers/net/null/rte_eth_null.c               |  78 +--
 drivers/net/octeon_ep/otx_ep_ethdev.c         |  16 +-
 drivers/net/octeontx/octeontx_ethdev.c        |  18 +-
 drivers/net/pcap/pcap_ethdev.c                |  51 +-
 drivers/net/qede/qede_ethdev.c                |  55 +-
 drivers/net/ring/rte_eth_ring.c               |  59 +-
 drivers/net/ring/rte_eth_ring.h               |  11 +-
 drivers/net/sfc/sfc.c                         |   2 +-
 drivers/net/sfc/sfc_kvargs.c                  |  19 -
 drivers/net/sfc/sfc_kvargs.h                  |   2 -
 drivers/net/sfc/sfc_port.c                    |   2 +-
 drivers/net/softnic/rte_eth_softnic.c         |  30 +-
 .../net/softnic/rte_eth_softnic_internals.h   |   3 +-
 drivers/net/thunderx/nicvf_ethdev.c           |  13 +-
 drivers/net/txgbe/txgbe_ethdev.c              |  97 ++-
 drivers/net/vhost/rte_eth_vhost.c             |  69 +-
 drivers/net/virtio/virtio_ethdev.c            |  36 +-
 drivers/net/virtio/virtio_user_ethdev.c       |  59 +-
 drivers/net/xsc/xsc_dev.c                     |  50 +-
 drivers/raw/skeleton/skeleton_rawdev.c        |  20 +-
 drivers/vdpa/ifc/ifcvf_vdpa.c                 |  29 +-
 lib/eal/common/eal_common_options.c           |  27 +-
 lib/kvargs/rte_kvargs.c                       | 604 ++++++++++++++++++
 lib/kvargs/rte_kvargs.h                       | 314 +++++++++
 76 files changed, 2371 insertions(+), 2167 deletions(-)
Signature · 1 line
2.53.0
#133 lore raw

PATCH v3 1/65 net/ring: make numa node signed

rte_eth_from_rings() takes a numa node (socket) but used unsigned where int should have been used to allow for SOCKET_ID_ANY. The only usage is in call rte_malloc routines with socket and those functions expect int.

Change the parameter to int socket_id and carry that through the two internal create functions. The devargs path keeps working as before:
"nodeaction=name:-1:CREATE" was already accepted, since strtol() returned -1 into an unsigned int which became SOCKET_ID_ANY again at the allocation.

---
 doc/guides/rel_notes/release_26_11.rst |  6 ++++
 drivers/net/ring/rte_eth_ring.c        | 38 +++++++++++++-------------
 drivers/net/ring/rte_eth_ring.h        | 11 ++++----
 3 files changed, 31 insertions(+), 24 deletions(-)
doc/guides/rel_notes/release_26_11.rst +6 -0
index e027c7a27f..58b0e588ce 100644
@@ -206,6 +206,12 @@ API Changes
    Also, make sure to start the actual text at the margin.
    =======================================================
 
+* net/ring: The last parameter of ``rte_eth_from_rings`` changed from
+  ``const unsigned numa_node`` to ``int socket_id``, so that
+  ``SOCKET_ID_ANY`` can be passed as it can everywhere else a socket is
+  given. The value was already used as a signed socket id by the memory
+  and ring allocations underneath.
+
 * ethdev: Updated VMDq related API.
 
   * At port configuration time, the number of VMDq pools advertised by a driver
drivers/net/ring/rte_eth_ring.c +19 -19
index 4cbdbe3085..533a4c1b6b 100644
@@ -32,7 +32,7 @@ struct ring_internal_args {
 	const unsigned int nb_rx_queues;
 	struct rte_ring * const *tx_queues;
 	const unsigned int nb_tx_queues;
-	const unsigned int numa_node;
+	int socket_id;
 };
 
 /* rte_eth_from_rings() stashes a pointer to its on-stack args here */
@@ -361,7 +361,7 @@ do_eth_dev_ring_create(const char *name,
 		const unsigned int nb_rx_queues,
 		struct rte_ring *const tx_queues[],
 		const unsigned int nb_tx_queues,
-		const unsigned int numa_node, enum dev_action action,
+		int socket_id, enum dev_action action,
 		struct rte_eth_dev **eth_dev_p)
 {
 	struct rte_eth_dev_data *data = NULL;
@@ -371,24 +371,24 @@ do_eth_dev_ring_create(const char *name,
 	void **tx_queues_local = NULL;
 	unsigned int i;
 
-	PMD_LOG(INFO, "Creating rings-backed ethdev on numa socket %u",
-			numa_node);
+	PMD_LOG(INFO, "Creating rings-backed ethdev on numa socket %d",
+			socket_id);
 
 	rx_queues_local = rte_calloc_socket(name, nb_rx_queues,
-					    sizeof(void *), 0, numa_node);
+					    sizeof(void *), 0, socket_id);
 	if (rx_queues_local == NULL) {
 		rte_errno = ENOMEM;
 		goto error;
 	}
 
 	tx_queues_local = rte_calloc_socket(name, nb_tx_queues,
-					    sizeof(void *), 0, numa_node);
+					    sizeof(void *), 0, socket_id);
 	if (tx_queues_local == NULL) {
 		rte_errno = ENOMEM;
 		goto error;
 	}
 
-	internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
+	internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
 	if (internals == NULL) {
 		rte_errno = ENOMEM;
 		goto error;
@@ -404,7 +404,7 @@ do_eth_dev_ring_create(const char *name,
 	/* now put it all together
 	 * - store EAL device in eth_dev,
 	 * - store queue data in internals,
-	 * - store numa_node info in eth_dev_data
+	 * - store socket id info in eth_dev_data
 	 * - point eth_dev_data to internals
 	 * - and point eth_dev structure to new eth_dev_data structure
 	 */
@@ -439,7 +439,7 @@ do_eth_dev_ring_create(const char *name,
 	data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
 
 	eth_dev->dev_ops = &ops;
-	data->numa_node = numa_node;
+	data->numa_node = socket_id;
 
 	/* finally assign rx and tx ops */
 	eth_dev->rx_pkt_burst = eth_ring_rx;
@@ -464,14 +464,14 @@ rte_eth_from_rings(const char *name, struct rte_ring *const rx_queues[],
 		const unsigned int nb_rx_queues,
 		struct rte_ring *const tx_queues[],
 		const unsigned int nb_tx_queues,
-		const unsigned int numa_node)
+		int socket_id)
 {
 	struct ring_internal_args args = {
 		.rx_queues = rx_queues,
 		.nb_rx_queues = nb_rx_queues,
 		.tx_queues = tx_queues,
 		.nb_tx_queues = nb_tx_queues,
-		.numa_node = numa_node,
+		.socket_id = socket_id,
 	};
 	char ring_name[RTE_RING_NAMESIZE];
 	uint16_t port_id = RTE_MAX_ETHPORTS;
@@ -526,7 +526,7 @@ rte_eth_from_ring(struct rte_ring *r)
 static int
 eth_dev_ring_create(const char *name,
 		struct rte_vdev_device *vdev,
-		const unsigned int numa_node,
+		int socket_id,
 		enum dev_action action, struct rte_eth_dev **eth_dev)
 {
 	/* rx and tx are so-called from point of view of first port.
@@ -549,7 +549,7 @@ eth_dev_ring_create(const char *name,
 		}
 
 		rxtx[i] = (action == DEV_CREATE) ?
-				rte_ring_create(rng_name, 1024, numa_node,
+				rte_ring_create(rng_name, 1024, socket_id,
 						RING_F_SP_ENQ|RING_F_SC_DEQ) :
 				rte_ring_lookup(rng_name);
 		if (rxtx[i] == NULL)
@@ -557,7 +557,7 @@ eth_dev_ring_create(const char *name,
 	}
 
 	if (do_eth_dev_ring_create(name, vdev, rxtx, num_rings, rxtx, num_rings,
-		numa_node, action, eth_dev) < 0)
+		socket_id, action, eth_dev) < 0)
 		return -1;
 
 	return 0;
@@ -565,7 +565,7 @@ eth_dev_ring_create(const char *name,
 
 struct node_action_pair {
 	char name[ETH_RING_ACTION_MAX_LEN];
-	unsigned int node;
+	int socket_id;
 	enum dev_action action;
 };
 
@@ -626,7 +626,7 @@ static int parse_kvlist(const char *key __rte_unused,
 		goto out;
 
 	errno = 0;
-	info->list[info->count].node = strtol(node, &end, 10);
+	info->list[info->count].socket_id = strtol(node, &end, 10);
 
 	if ((errno != 0) || (*end != '\0')) {
 		PMD_LOG(WARNING,
@@ -686,7 +686,7 @@ rte_pmd_ring_probe(struct rte_vdev_device *dev)
 			internal_args->nb_rx_queues,
 			internal_args->tx_queues,
 			internal_args->nb_tx_queues,
-			internal_args->numa_node,
+			internal_args->socket_id,
 			DEV_ATTACH,
 			&eth_dev);
 		return ret >= 0 ? 0 : ret;
@@ -740,7 +740,7 @@ rte_pmd_ring_probe(struct rte_vdev_device *dev)
 		for (info->count = 0; info->count < info->total; info->count++) {
 			ret = eth_dev_ring_create(info->list[info->count].name,
 						  dev,
-						  info->list[info->count].node,
+						  info->list[info->count].socket_id,
 						  info->list[info->count].action,
 						  &eth_dev);
 			if (ret == -1 && info->list[info->count].action == DEV_CREATE) {
@@ -748,7 +748,7 @@ rte_pmd_ring_probe(struct rte_vdev_device *dev)
 					"Attach to pmd_ring for %s",
 					name);
 				ret = eth_dev_ring_create(name, dev,
-						info->list[info->count].node,
+						info->list[info->count].socket_id,
 						DEV_ATTACH,
 						&eth_dev);
 			}
drivers/net/ring/rte_eth_ring.h +6 -5
index 98292c7b33..dfbfce9f2f 100644
@@ -24,17 +24,18 @@ extern "C" {
  *    pointer to array of rte_rings to be used as TX queues
  * @param nb_tx_queues
  *    number of elements in the tx_queues array
- * @param numa_node
- *    the numa node on which the memory for this port is to be allocated
+ * @param socket_id
+ *    the NUMA socket on which the memory for this port is to be allocated,
+ *    or SOCKET_ID_ANY
  * @return
  *    the port number of the newly created the ethdev or -1 on error.
  */
 int rte_eth_from_rings(const char *name,
 		struct rte_ring * const rx_queues[],
-		const unsigned nb_rx_queues,
+		const unsigned int nb_rx_queues,
 		struct rte_ring *const tx_queues[],
-		const unsigned nb_tx_queues,
-		const unsigned numa_node);
+		const unsigned int nb_tx_queues,
+		int socket_id);
 
 /**
  * Create a new ethdev port from a ring
Signature · 1 line
2.53.0
#134 lore raw

PATCH v3 2/65 kvargs: add numeric conversion helpers

In reply to Stephen Hemminger

Drivers which take numeric values in devargs each open code the conversion from string to integer, and often get it wrong.
A survey of the tree finds at least fifteen separate implementations of "parse an unsigned integer devarg", of which two are exported from lib/ and byte for byte identical to each other.

The recurring bugs are:

- atoi() is used, so overflow is undefined and nothing is validated;
- errno is checked without being reset first, so an unrelated earlier

    failure rejects a valid value;
  - errno is checked but endptr is not, so "foo" is silently accepted
    as zero;
  - endptr is checked but errno is not, so an overflowing value is
    accepted as ULLONG_MAX;
  - the result is stored into a narrower type with no range check, so
    nb_desc=65537 silently becomes 1;
  - strtoul() is used for an unsigned target, so a leading '-' is
    accepted and wrapped around, and dev_caps_mask=-1 enables
    everything;
  - the value is dereferenced without checking for NULL, so a key given
    with no value segfaults;

- base 0 is passed, so a leading zero unexpectedly selects octal.

Add a set of helpers matching arg_handler_t, so they can be passed straight to rte_kvargs_process(), covering the integer types drivers actually store into. Each validates the whole string and only writes the target on success, so a caller supplied default survives a bad argument.

Add rte_kvargs_handle_bool for on/off style arguments. It accepts the word forms which only sfc supports today, and treats a key given without a value as true.

Where a driver needs a range narrower than the target type, expose the underlying rte_kvargs_to_uint and rte_kvargs_to_int.

Octal is deliberately not supported: no driver documents it, and reading "010" as eight has been a recurring surprise.

---
 app/test/test_kvargs.c                 | 221 +++++++++++++
 doc/guides/prog_guide/devargs.rst      |  18 +
 doc/guides/rel_notes/release_26_11.rst |  20 ++
 lib/kvargs/rte_kvargs.c                | 441 +++++++++++++++++++++++++
 lib/kvargs/rte_kvargs.h                | 186 +++++++++++
 5 files changed, 886 insertions(+)
app/test/test_kvargs.c +221 -0
index a14b75948a..b74dfd6acb 100644
@@ -2,6 +2,8 @@
  * Copyright 2014 6WIND S.A.
  */
 
+#include <errno.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -328,6 +330,221 @@ static int test_invalid_kvargs(void)
 	return -1;
 }
 
+/* Check the numeric conversion helpers on a value passed through kvargs. */
+static int
+handle_one(arg_handler_t handler, const char *value, void *opaque)
+{
+	struct rte_kvargs *kvlist;
+	char args[128];
+	int ret;
+
+	if (value != NULL)
+		snprintf(args, sizeof(args), "k=%s", value);
+	else
+		snprintf(args, sizeof(args), "k");
+
+	kvlist = rte_kvargs_parse(args, NULL);
+	if (kvlist == NULL)
+		return -1;
+
+	ret = rte_kvargs_process_opt(kvlist, "k", handler, opaque);
+	rte_kvargs_free(kvlist);
+
+	/* rte_kvargs_process_opt() flattens the handler error to -1. */
+	return ret;
+}
+
+/* A handler must accept a good value, and leave the target alone otherwise. */
+#define CHECK_GOOD(handler, type, str, expected) do { \
+	type v = (type)0x5a; \
+	TEST_ASSERT_SUCCESS(handle_one(handler, str, &v), \
+			    "%s rejected \"%s\"", #handler, str); \
+	TEST_ASSERT_EQUAL(v, (type)(expected), \
+			  "%s(\"%s\") gave the wrong value", #handler, str); \
+} while (0)
+
+#define CHECK_BAD(handler, type, str) do { \
+	type v = (type)0x5a; \
+	TEST_ASSERT_FAIL(handle_one(handler, str, &v), \
+			 "%s accepted \"%s\"", #handler, str); \
+	TEST_ASSERT_EQUAL(v, (type)0x5a, \
+			  "%s clobbered the target on \"%s\"", #handler, str); \
+} while (0)
+
+static int
+test_handle_unsigned(void)
+{
+	CHECK_GOOD(rte_kvargs_handle_u8, uint8_t, "0", 0);
+	CHECK_GOOD(rte_kvargs_handle_u8, uint8_t, "255", 255);
+	CHECK_GOOD(rte_kvargs_handle_u8, uint8_t, "0xff", 255);
+	CHECK_GOOD(rte_kvargs_handle_u8, uint8_t, "0XFF", 255);
+	CHECK_GOOD(rte_kvargs_handle_u8, uint8_t, "+7", 7);
+	/* A leading zero must not select octal. */
+	CHECK_GOOD(rte_kvargs_handle_u8, uint8_t, "010", 10);
+	CHECK_BAD(rte_kvargs_handle_u8, uint8_t, "256");
+	CHECK_BAD(rte_kvargs_handle_u8, uint8_t, "-1");
+
+	CHECK_GOOD(rte_kvargs_handle_u16, uint16_t, "65535", 65535);
+	CHECK_BAD(rte_kvargs_handle_u16, uint16_t, "65536");
+
+	CHECK_GOOD(rte_kvargs_handle_u32, uint32_t, "4294967295", UINT32_MAX);
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "4294967296");
+
+	CHECK_GOOD(rte_kvargs_handle_u64, uint64_t, "18446744073709551615",
+		   UINT64_MAX);
+	CHECK_GOOD(rte_kvargs_handle_u64, uint64_t, "0xffffffffffffffff",
+		   UINT64_MAX);
+	CHECK_BAD(rte_kvargs_handle_u64, uint64_t, "18446744073709551616");
+
+	CHECK_GOOD(rte_kvargs_handle_uint, unsigned int, "42", 42);
+	CHECK_GOOD(rte_kvargs_handle_size, size_t, "42", 42);
+
+	/* Malformed values, rejected for every width. */
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "abc");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "12abc");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "12 34");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "0x");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "0x0x10");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "0X0X10");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "--1");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "+-1");
+	/* strtoull() would skip the space and negate what follows. */
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "+ 1");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "- 1");
+	CHECK_BAD(rte_kvargs_handle_u32, uint32_t, "+ -1");
+	/* Trailing white space is fine, though. */
+	CHECK_GOOD(rte_kvargs_handle_u32, uint32_t, " 12 ", 12);
+
+	/* A key with no value at all. */
+	{
+		uint32_t v = 0x5a;
+
+		TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_u32, NULL, &v),
+				 "u32 accepted a key with no value");
+		TEST_ASSERT_EQUAL(v, 0x5aU, "target clobbered");
+	}
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_handle_signed(void)
+{
+	CHECK_GOOD(rte_kvargs_handle_i8, int8_t, "-128", -128);
+	CHECK_GOOD(rte_kvargs_handle_i8, int8_t, "127", 127);
+	CHECK_BAD(rte_kvargs_handle_i8, int8_t, "-129");
+	CHECK_BAD(rte_kvargs_handle_i8, int8_t, "128");
+
+	CHECK_GOOD(rte_kvargs_handle_i16, int16_t, "-32768", -32768);
+	CHECK_BAD(rte_kvargs_handle_i16, int16_t, "32768");
+
+	CHECK_GOOD(rte_kvargs_handle_i32, int32_t, "-2147483648", INT32_MIN);
+	CHECK_BAD(rte_kvargs_handle_i32, int32_t, "2147483648");
+
+	/* INT64_MIN has a magnitude one past INT64_MAX. */
+	CHECK_GOOD(rte_kvargs_handle_i64, int64_t, "-9223372036854775808",
+		   INT64_MIN);
+	CHECK_GOOD(rte_kvargs_handle_i64, int64_t, "9223372036854775807",
+		   INT64_MAX);
+	CHECK_BAD(rte_kvargs_handle_i64, int64_t, "9223372036854775808");
+	CHECK_BAD(rte_kvargs_handle_i64, int64_t, "-9223372036854775809");
+
+	/* A sign in front of a hex value. */
+	CHECK_GOOD(rte_kvargs_handle_i32, int32_t, "-0x10", -16);
+	CHECK_BAD(rte_kvargs_handle_i32, int32_t, "-0x0x10");
+
+	CHECK_GOOD(rte_kvargs_handle_int, int, "-1", -1);
+	CHECK_GOOD(rte_kvargs_handle_int, int, "+1", 1);
+	CHECK_BAD(rte_kvargs_handle_int, int, "");
+	CHECK_BAD(rte_kvargs_handle_int, int, "1x");
+	CHECK_BAD(rte_kvargs_handle_int, int, "-");
+	CHECK_BAD(rte_kvargs_handle_int, int, "- 1");
+	CHECK_BAD(rte_kvargs_handle_int, int, "--1");
+
+	CHECK_GOOD(rte_kvargs_handle_long, long, "-1", -1);
+	CHECK_GOOD(rte_kvargs_handle_long, long, "+1", 1);
+	CHECK_BAD(rte_kvargs_handle_long, long, "");
+	CHECK_BAD(rte_kvargs_handle_long, long, "1x");
+
+	CHECK_GOOD(rte_kvargs_handle_ulong, unsigned long, "1", 1);
+	CHECK_GOOD(rte_kvargs_handle_ulong, unsigned long, "0x10", 16);
+	CHECK_BAD(rte_kvargs_handle_ulong, unsigned long, "-1");
+	CHECK_BAD(rte_kvargs_handle_ulong, unsigned long, "1x");
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_handle_bool(void)
+{
+	static const char * const yes[] = {
+		"1", "y", "Y", "yes", "YES", "on", "On", "true", "TRUE",
+	};
+	static const char * const no[] = {
+		"0", "n", "N", "no", "NO", "off", "Off", "false", "FALSE",
+	};
+	unsigned int i;
+	bool v;
+
+	for (i = 0; i < RTE_DIM(yes); i++) {
+		v = false;
+		TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_bool, yes[i], &v),
+				    "bool rejected \"%s\"", yes[i]);
+		TEST_ASSERT(v, "\"%s\" should be true", yes[i]);
+	}
+
+	for (i = 0; i < RTE_DIM(no); i++) {
+		v = true;
+		TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_bool, no[i], &v),
+				    "bool rejected \"%s\"", no[i]);
+		TEST_ASSERT(!v, "\"%s\" should be false", no[i]);
+	}
+
+	/* A bare key is enough to enable the option. */
+	v = false;
+	TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_bool, NULL, &v),
+			    "bool rejected a key with no value");
+	TEST_ASSERT(v, "a key with no value should be true");
+
+	/* But a blank value is not a missing one. */
+	CHECK_BAD(rte_kvargs_handle_bool, bool, "");
+	CHECK_BAD(rte_kvargs_handle_bool, bool, "2");
+	CHECK_BAD(rte_kvargs_handle_bool, bool, "yep");
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_kvargs_to_range(void)
+{
+	uint64_t u = 0x5a;
+	int64_t s = 0x5a;
+
+	TEST_ASSERT_SUCCESS(rte_kvargs_to_uint("10", 0, 10, &u), "10 in [0,10]");
+	TEST_ASSERT_EQUAL(u, 10U, "wrong value");
+
+	TEST_ASSERT_EQUAL(rte_kvargs_to_uint("11", 0, 10, &u), -ERANGE,
+			  "11 should be out of [0,10]");
+	TEST_ASSERT_EQUAL(u, 10U, "target clobbered on range error");
+
+	TEST_ASSERT_EQUAL(rte_kvargs_to_uint("0", 1, 10, &u), -ERANGE,
+			  "0 should be out of [1,10]");
+	TEST_ASSERT_EQUAL(rte_kvargs_to_uint(NULL, 0, 10, &u), -EINVAL,
+			  "NULL should be rejected");
+	TEST_ASSERT_EQUAL(rte_kvargs_to_uint("x", 0, 10, &u), -EINVAL,
+			  "\"x\" should be rejected");
+	TEST_ASSERT_EQUAL(rte_kvargs_to_uint("5", 0, 10, NULL), -EINVAL,
+			  "a NULL result should be rejected");
+
+	TEST_ASSERT_SUCCESS(rte_kvargs_to_int("-5", -10, 10, &s), "-5 in [-10,10]");
+	TEST_ASSERT_EQUAL(s, -5, "wrong value");
+	TEST_ASSERT_EQUAL(rte_kvargs_to_int("-11", -10, 10, &s), -ERANGE,
+			  "-11 should be out of [-10,10]");
+
+	return TEST_SUCCESS;
+}
+
 static struct unit_test_suite kvargs_test_suite  = {
 	.suite_name = "Kvargs Unit Test Suite",
 	.setup = NULL,
@@ -354,6 +571,10 @@ static struct unit_test_suite kvargs_test_suite  = {
 		TEST_CASE(test_parse_empty_elements),
 		TEST_CASE(test_parse_with_only_key),
 		TEST_CASE(test_invalid_kvargs),
+		TEST_CASE(test_handle_unsigned),
+		TEST_CASE(test_handle_signed),
+		TEST_CASE(test_handle_bool),
+		TEST_CASE(test_kvargs_to_range),
 		TEST_CASES_END() /**< NULL terminate unit test array */
 	}
 };
doc/guides/prog_guide/devargs.rst +18 -0
index c8a7224aa0..b73ad20cf6 100644
@@ -248,6 +248,24 @@ PMD drivers can parse devargs using the kvargs library:
        return 0;
    }
 
+Rather than writing a handler for each numeric argument, use the
+conversion handlers provided by kvargs, which do the range checking:
+
+.. code-block:: c
+
+   uint16_t queues = 1;
+   bool scalar = false;
+
+   rte_kvargs_process(kvlist, "queues", rte_kvargs_handle_u16, &queues);
+   rte_kvargs_process_opt(kvlist, "scalar", rte_kvargs_handle_bool, &scalar);
+
+Boolean arguments accept ``1``, ``y``, ``yes``, ``on`` and ``true``,
+case insensitively, and their negative counterparts. A bare ``scalar``
+with no value means true, but reaches the handler only through
+rte_kvargs_process_opt(); rte_kvargs_process() rejects a missing value
+first. An empty ``scalar=`` is rejected, since that is what an unset
+shell variable expands to.
+
 For Ethernet devices, use ``rte_eth_devargs_parse()``
 to parse standard Ethernet arguments like representors:
 
doc/guides/rel_notes/release_26_11.rst +20 -0
index 58b0e588ce..20271cee52 100644
@@ -138,6 +138,26 @@ New Features
   Added ``rte_bbdev_queue_stats_get()`` function to retrieve statistics
   for a specific queue, complementing the existing device-level statistics API.
 
+* **Added numeric conversion helpers to kvargs.**
+
+  Added a set of ``arg_handler_t`` compatible helpers which convert a device
+  argument value into a numeric variable, so that drivers no longer need to
+  open code the conversion and its validation:
+
+  * ``rte_kvargs_handle_u8``, ``rte_kvargs_handle_u16``,
+    ``rte_kvargs_handle_u32``, ``rte_kvargs_handle_u64``,
+    ``rte_kvargs_handle_uint``, ``rte_kvargs_handle_ulong``
+    and ``rte_kvargs_handle_size``
+  * ``rte_kvargs_handle_i8``, ``rte_kvargs_handle_i16``,
+    ``rte_kvargs_handle_i32``, ``rte_kvargs_handle_i64``,
+    ``rte_kvargs_handle_int`` and ``rte_kvargs_handle_long``
+  * ``rte_kvargs_handle_bool``, accepting ``1``, ``y``, ``yes``, ``on``,
+    ``true`` and their negative counterparts. A bare ``key`` means true;
+    an empty ``key=`` is rejected.
+
+  Added ``rte_kvargs_to_uint`` and ``rte_kvargs_to_int`` for the cases where
+  a driver needs a narrower range than the target type allows.
+
 
 Removed Items
 -------------
lib/kvargs/rte_kvargs.c +441 -0

459 lines not on this page · raw message

lib/kvargs/rte_kvargs.h +186 -0

198 lines not on this page · raw message

Signature · 1 line
2.53.0
#135 lore raw

PATCH v3 3/65 kvargs: add a socket ID handler

In reply to Stephen Hemminger

Device arguments naming a NUMA socket are common, and every driver that takes one open codes the same range check against RTE_MAX_NUMA_NODES. They do not agree on what the range is: dlb2 accepts RTE_MAX_NUMA_NODES itself, and turbo_sw and bbdev_null check only the upper bound, so a negative socket id is taken as valid.

Add a handler for these: rte_kvargs_handle_socket_id() It stores through an int and accepts -1,
which is SOCKET_ID_ANY, through RTE_MAX_NUMA_NODES - 1.

SOCKET_ID_ANY is spelled as a literal -1 rather than included from rte_memory.h for the same reason. The value is part of the ABI.

---
 app/test/test_kvargs.c                 | 31 ++++++++++++++++++++++++++
 doc/guides/rel_notes/release_26_11.rst |  2 ++
 lib/kvargs/rte_kvargs.c                | 18 +++++++++++++++
 lib/kvargs/rte_kvargs.h                | 27 ++++++++++++++++++++++
 4 files changed, 78 insertions(+)
app/test/test_kvargs.c +31 -0
index b74dfd6acb..f05c7918e9 100644
@@ -515,6 +515,36 @@ test_handle_bool(void)
 	return TEST_SUCCESS;
 }
 
+static int
+test_handle_socket_id(void)
+{
+	char buf[32];
+
+	/* SOCKET_ID_ANY is a valid socket id, anything below it is not. */
+	CHECK_GOOD(rte_kvargs_handle_socket_id, int, "-1", -1);
+	CHECK_GOOD(rte_kvargs_handle_socket_id, int, "0", 0);
+	CHECK_BAD(rte_kvargs_handle_socket_id, int, "-2");
+	CHECK_BAD(rte_kvargs_handle_socket_id, int, "");
+	CHECK_BAD(rte_kvargs_handle_socket_id, int, "1x");
+
+	/* The last valid value, and the first one past it. */
+	{
+		int socket = 0x5a;
+
+		snprintf(buf, sizeof(buf), "%d", RTE_MAX_NUMA_NODES - 1);
+		TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_socket_id, buf,
+					       &socket), "socket id %s rejected", buf);
+		TEST_ASSERT_EQUAL(socket, RTE_MAX_NUMA_NODES - 1, "wrong socket id");
+
+		snprintf(buf, sizeof(buf), "%d", RTE_MAX_NUMA_NODES);
+		TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_socket_id, buf,
+					    &socket), "socket id %s accepted", buf);
+		TEST_ASSERT_EQUAL(socket, RTE_MAX_NUMA_NODES - 1, "target clobbered");
+	}
+
+	return TEST_SUCCESS;
+}
+
 static int
 test_kvargs_to_range(void)
 {
@@ -574,6 +604,7 @@ static struct unit_test_suite kvargs_test_suite  = {
 		TEST_CASE(test_handle_unsigned),
 		TEST_CASE(test_handle_signed),
 		TEST_CASE(test_handle_bool),
+		TEST_CASE(test_handle_socket_id),
 		TEST_CASE(test_kvargs_to_range),
 		TEST_CASES_END() /**< NULL terminate unit test array */
 	}
doc/guides/rel_notes/release_26_11.rst +2 -0
index 20271cee52..7225e9e787 100644
@@ -151,6 +151,8 @@ New Features
   * ``rte_kvargs_handle_i8``, ``rte_kvargs_handle_i16``,
     ``rte_kvargs_handle_i32``, ``rte_kvargs_handle_i64``,
     ``rte_kvargs_handle_int`` and ``rte_kvargs_handle_long``
+  * ``rte_kvargs_handle_socket_id``, which accepts ``SOCKET_ID_ANY``
+    through ``RTE_MAX_NUMA_NODES`` - 1
   * ``rte_kvargs_handle_bool``, accepting ``1``, ``y``, ``yes``, ``on``,
     ``true`` and their negative counterparts. A bare ``key`` means true;
     an empty ``key=`` is rejected.
lib/kvargs/rte_kvargs.c +18 -0
index 3b961a19a8..6e5e3fcd7c 100644
@@ -750,3 +750,21 @@ rte_kvargs_handle_bool(const char *key, const char *value, void *opaque)
 
 	return -EINVAL;
 }
+
+RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_kvargs_handle_socket_id, 26.11)
+int
+rte_kvargs_handle_socket_id(const char *key, const char *value, void *opaque)
+{
+	int64_t val;
+	int ret;
+
+	/* SOCKET_ID_ANY, which is -1, is a valid socket id. It is spelled
+	 * out here rather than included from EAL, which kvargs sits below.
+	 */
+	ret = kvargs_store_int(key, value, opaque, -1,
+			       RTE_MAX_NUMA_NODES - 1, &val);
+	if (ret == 0)
+		*(int *)opaque = (int)val;
+
+	return ret;
+}
lib/kvargs/rte_kvargs.h +27 -0
index 118cf3c79b..acc15607bc 100644
@@ -359,6 +359,33 @@ int rte_kvargs_handle_size(const char *key, const char *value, void *opaque);
 __rte_experimental
 int rte_kvargs_handle_bool(const char *key, const char *value, void *opaque);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Convert a key/value pair to a NUMA socket id.
+ *
+ * Accepts -1, which is SOCKET_ID_ANY, through RTE_MAX_NUMA_NODES - 1.
+ * The bound is the compile time maximum rather than the set of sockets
+ * present on the running system, matching what drivers checked before
+ * this helper existed.
+ *
+ * @param key
+ *   The key, used for error reporting only. May be NULL.
+ * @param value
+ *   The value to convert.
+ * @param opaque
+ *   Pointer to an ``int`` to store the result into. On error it is left
+ *   unmodified.
+ *
+ * @return
+ *   - 0 on success.
+ *   - -EINVAL if the value is malformed, or if @p opaque is NULL.
+ *   - -ERANGE if the value is not a valid socket id.
+ */
+__rte_experimental
+int rte_kvargs_handle_socket_id(const char *key, const char *value, void *opaque);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice.
Signature · 1 line
2.53.0
#136 lore raw

PATCH v3 4/65 kvargs: add hexadecimal conversion helpers

In reply to Stephen Hemminger

Several device arguments are documented as a bare hexadecimal mask, with no 0x prefix. Each place the driver open codes strtoull() with base 16, and each gets the validation wrong in a one way or another.

Introduce a new helper to handle these type of args. Add rte_kvargs_handle_hex32() and rte_kvargs_handle_hex64().

A sign is rejected, as it is for the unsigned handlers, so a mask cannot be given as -1.

Only the 32 and 64 bit widths are added, since those are the only ones any caller needs.

---
 app/test/test_kvargs.c                 |  45 +++++++++
 doc/guides/rel_notes/release_26_11.rst |   8 +-
 lib/kvargs/rte_kvargs.c                | 131 ++++++++++++++++++++++---
 lib/kvargs/rte_kvargs.h                |  44 +++++++++
 4 files changed, 213 insertions(+), 15 deletions(-)
app/test/test_kvargs.c +45 -0
index f05c7918e9..6e84ebf35c 100644
@@ -475,6 +475,50 @@ test_handle_signed(void)
 	return TEST_SUCCESS;
 }
 
+static int
+test_handle_hex(void)
+{
+	uint64_t u = 0x5a;
+
+	/* The 0x prefix is optional, and a bare value is still hexadecimal. */
+	CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "0xff", 0xff);
+	CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "0XFF", 0xff);
+	CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "ff", 0xff);
+	CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "FF", 0xff);
+	/* "10" is sixteen here, not ten. */
+	CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "10", 0x10);
+	CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "0", 0);
+	CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "00110F10", 0x00110f10);
+	CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "ffffffff", 0xffffffff);
+	CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "100000000");
+	CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "");
+	CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "-1");
+	CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "0x");
+	CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "0x0x10");
+	CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "1g");
+	CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "0x1p");
+
+	CHECK_GOOD(rte_kvargs_handle_hex64, uint64_t, "0xF", 0xf);
+	CHECK_GOOD(rte_kvargs_handle_hex64, uint64_t, "F", 0xf);
+	CHECK_GOOD(rte_kvargs_handle_hex64, uint64_t, "ffffffffffffffff",
+		   UINT64_MAX);
+	CHECK_BAD(rte_kvargs_handle_hex64, uint64_t, "10000000000000000");
+	CHECK_BAD(rte_kvargs_handle_hex64, uint64_t, "-1");
+
+	/* The underlying conversion, with a mask narrower than the type. */
+	TEST_ASSERT_SUCCESS(rte_kvargs_to_hex("fff", 0xfff, &u), "fff in 0..fff");
+	TEST_ASSERT_EQUAL(u, 0xfffU, "wrong value");
+	TEST_ASSERT_EQUAL(rte_kvargs_to_hex("1000", 0xfff, &u), -ERANGE,
+			  "1000 should be out of 0..fff");
+	TEST_ASSERT_EQUAL(u, 0xfffU, "target clobbered on range error");
+	TEST_ASSERT_EQUAL(rte_kvargs_to_hex(NULL, 0xfff, &u), -EINVAL,
+			  "NULL should be rejected");
+	TEST_ASSERT_EQUAL(rte_kvargs_to_hex("f", 0xfff, NULL), -EINVAL,
+			  "a NULL result should be rejected");
+
+	return TEST_SUCCESS;
+}
+
 static int
 test_handle_bool(void)
 {
@@ -603,6 +647,7 @@ static struct unit_test_suite kvargs_test_suite  = {
 		TEST_CASE(test_invalid_kvargs),
 		TEST_CASE(test_handle_unsigned),
 		TEST_CASE(test_handle_signed),
+		TEST_CASE(test_handle_hex),
 		TEST_CASE(test_handle_bool),
 		TEST_CASE(test_handle_socket_id),
 		TEST_CASE(test_kvargs_to_range),
doc/guides/rel_notes/release_26_11.rst +6 -2
index 7225e9e787..27b3fe4253 100644
@@ -156,9 +156,13 @@ New Features
   * ``rte_kvargs_handle_bool``, accepting ``1``, ``y``, ``yes``, ``on``,
     ``true`` and their negative counterparts. A bare ``key`` means true;
     an empty ``key=`` is rejected.
+  * ``rte_kvargs_handle_hex32`` and ``rte_kvargs_handle_hex64``, for the
+    arguments documented as a bare hexadecimal mask, where the value is
+    always read as hexadecimal whether or not it carries a ``0x`` prefix
 
-  Added ``rte_kvargs_to_uint`` and ``rte_kvargs_to_int`` for the cases where
-  a driver needs a narrower range than the target type allows.
+  Added ``rte_kvargs_to_uint``, ``rte_kvargs_to_int`` and
+  ``rte_kvargs_to_hex`` for the cases where a driver needs a narrower range
+  than the target type allows.
 
 
 Removed Items
lib/kvargs/rte_kvargs.c +118 -13

160 lines not on this page · raw message

lib/kvargs/rte_kvargs.h +44 -0

56 lines not on this page · raw message

Signature · 1 line
2.53.0
#137 lore raw

PATCH v3 5/65 eal: validate memory size arguments

In reply to Stephen Hemminger

The -m, -n and -r options are converted with atoi(), which does not detect any error: it returns zero for a string that is not a number at all, and its behaviour on overflow is undefined.

So "-m foo" is silently taken as zero rather than rejected, and "-m 99999999999999999999" is undefined. The -n and -r cases are partly covered by their existing checks for zero, but only because a garbage value happens to convert to zero; "-n 4x" is accepted as four.

The -m value is also scaled by 1024 * 1024 with no check, so a large enough value wraps around and asks for a small amount of memory.

Use rte_kvargs_to_uint() for the three options, which converts the whole string and checks it against a range. EAL already depends on kvargs, so no new dependency is introduced. Note that this now bounds -m so that the scaled value cannot overflow size_t, which on a 32-bit build limits it to just under 4G; previously such a value was accepted and wrapped.

---
 lib/eal/common/eal_common_options.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
lib/eal/common/eal_common_options.c +20 -7
index 42cdef632f..519307bdc3 100644
@@ -9,6 +9,7 @@
 #include <ctype.h>
 #include <limits.h>
 #include <errno.h>
+#include <stdint.h>
 #include <getopt.h>
 #include <sys/queue.h>
 #ifndef RTE_EXEC_ENV_WINDOWS
@@ -29,6 +30,7 @@
 #include <rte_tailq.h>
 #include <rte_version.h>
 #include <rte_devargs.h>
+#include <rte_kvargs.h>
 #include <rte_memcpy.h>
 #ifndef RTE_EXEC_ENV_WINDOWS
 #include <rte_telemetry.h>
@@ -2210,23 +2212,34 @@ eal_parse_args(void)
 
 	/* memory options */
 	if (args.memory_size != NULL) {
-		int_cfg->memory = atoi(args.memory_size);
-		int_cfg->memory *= 1024ULL;
-		int_cfg->memory *= 1024ULL;
+		uint64_t mem;
+
+		/* value is in megabytes, and must not overflow when scaled */
+		if (rte_kvargs_to_uint(args.memory_size, 0,
+				       SIZE_MAX / (1024 * 1024), &mem) < 0) {
+			EAL_LOG(ERR, "invalid memory size parameter");
+			return -1;
+		}
+		int_cfg->memory = (size_t)mem * 1024 * 1024;
 	}
 	if (args.memory_channels != NULL) {
-		int_cfg->force_nchannel = atoi(args.memory_channels);
-		if (int_cfg->force_nchannel == 0) {
+		uint64_t channels;
+
+		if (rte_kvargs_to_uint(args.memory_channels, 1, UINT_MAX,
+				       &channels) < 0) {
 			EAL_LOG(ERR, "invalid memory channel parameter");
 			return -1;
 		}
+		int_cfg->force_nchannel = channels;
 	}
 	if (args.memory_ranks != NULL) {
-		int_cfg->force_nrank = atoi(args.memory_ranks);
-		if (int_cfg->force_nrank == 0 || int_cfg->force_nrank > 16) {
+		uint64_t ranks;
+
+		if (rte_kvargs_to_uint(args.memory_ranks, 1, 16, &ranks) < 0) {
 			EAL_LOG(ERR, "invalid memory rank parameter");
 			return -1;
 		}
+		int_cfg->force_nrank = ranks;
 	}
 	if (args.no_huge) {
 		int_cfg->no_hugetlbfs = 1;
Signature · 1 line
2.53.0
#138 lore raw

PATCH v3 6/65 net/null: use kvargs numeric helpers

In reply to Stephen Hemminger

The three device argument handlers each convert with strtoul() and check neither errno nor the end pointer, so "size=foo" is silently taken as zero and "size=99999999999999999999" as ULONG_MAX truncated to unsigned int. The checks for UINT_MAX that follow only catch the truncated overflow case on a 32 bit build.

A zero packet size was accepted, and the value is later cast to the 16 bit mbuf data length, so anything above 65535 was silently truncated. Convert with an explicit 1..UINT16_MAX range instead.

The copy and no-rx arguments are booleans, so store them as bool and use rte_kvargs_handle_bool. This also means they now accept the word forms such as "copy=on", not only 0 and 1.

While here, set an error code when copy and no-rx are both given. The error was logged but the zero return from the last successful rte_kvargs_process() was returned, so probe reported success without ever creating the device.

The booleans use rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 doc/guides/nics/null.rst        |  2 +-
 drivers/net/null/rte_eth_null.c | 78 ++++++++++-----------------------
 2 files changed, 24 insertions(+), 56 deletions(-)
doc/guides/nics/null.rst +1 -1
index c0e3199102..fd8d685d83 100644
@@ -30,7 +30,7 @@ Runtime Configuration
 
 - ``size`` [optional, default=64 bytes]
 
- Custom packet length value to use.r
+ Custom packet length value to use, from 1 to 65535.
  If ``copy`` is enabled, this is the length of copy operation.
 
 .. code-block:: console
drivers/net/null/rte_eth_null.c +23 -55

135 lines not on this page · raw message

Signature · 1 line
2.53.0
#139 lore raw

PATCH v3 7/65 net/vhost: use kvargs numeric helpers

In reply to Stephen Hemminger

open_int() converts with strtoul() and checks errno without resetting it first, so an unrelated earlier failure makes a valid value fail, and an overflow which does not happen to produce USHRT_MAX is accepted. The end pointer is not checked at all, so "queues=foo" is silently taken as zero.

More seriously, open_int() stores through a uint16_t pointer, but seven of its eight callers pass the address of an int. Only two bytes of a four byte object are written; this happens to work on little endian because the callers initialize the variable to zero first, but it is wrong on big endian.

The queues argument is the only real number, and it keeps a handler of its own so that the range is checked where it is parsed. The caller tested "queues > RTE_MAX_QUEUES_PER_PORT" after the fact but left ret at zero, so an out of range value skipped the rest of the parsing and still returned success from probe. Bounding it in the handler to 1..RTE_MAX_QUEUES_PER_PORT makes it fail properly, and also rejects "queues=0", which was accepted before and left the device with no queues.

The rest are booleans, so store them as bool and use rte_kvargs_handle_bool, which also gives them the word forms such as "client=on". They are processed with rte_kvargs_process_opt(), so a bare key with no value enables the option.

Bugzilla ID: 2039

Fixes 39cac2adcad0 ("net/vhost: add client option")
---
 drivers/net/vhost/rte_eth_vhost.c | 69 ++++++++++++++++---------------
 1 file changed, 35 insertions(+), 34 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +35 -34
index 56c7a973e9..2117aba741 100644
@@ -1583,17 +1583,18 @@ open_iface(const char *key __rte_unused, const char *value, void *extra_args)
 	return 0;
 }
 
-static inline int
-open_int(const char *key __rte_unused, const char *value, void *extra_args)
+static int
+open_queues(const char *key, const char *value, void *extra_args)
 {
-	uint16_t *n = extra_args;
+	uint64_t queues;
 
-	if (value == NULL || extra_args == NULL)
+	if (rte_kvargs_to_uint(value, 1, RTE_MAX_QUEUES_PER_PORT, &queues) < 0) {
+		VHOST_LOG_LINE(ERR, "invalid %s, must be 1..%u", key,
+			       RTE_MAX_QUEUES_PER_PORT);
 		return -EINVAL;
+	}
 
-	*n = (uint16_t)strtoul(value, NULL, 0);
-	if (*n == USHRT_MAX && errno == ERANGE)
-		return -1;
+	*(uint16_t *)extra_args = queues;
 
 	return 0;
 }
@@ -1607,13 +1608,13 @@ rte_pmd_vhost_probe(struct rte_vdev_device *dev)
 	uint16_t queues;
 	uint64_t flags = RTE_VHOST_USER_NET_STATS_ENABLE;
 	uint64_t disable_flags = 0;
-	int client_mode = 0;
-	int iommu_support = 0;
-	int postcopy_support = 0;
-	int tso = 0;
-	int linear_buf = 0;
-	int ext_buf = 0;
-	int legacy_ol_flags = 0;
+	bool client_mode = false;
+	bool iommu_support = false;
+	bool postcopy_support = false;
+	bool tso = false;
+	bool linear_buf = false;
+	bool ext_buf = false;
+	bool legacy_ol_flags = false;
 	struct rte_eth_dev *eth_dev;
 	const char *name = rte_vdev_device_name(dev);
 
@@ -1651,16 +1652,16 @@ rte_pmd_vhost_probe(struct rte_vdev_device *dev)
 
 	if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
 		ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
-					 &open_int, &queues);
-		if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
+					 &open_queues, &queues);
+		if (ret < 0)
 			goto out_free;
 
 	} else
 		queues = 1;
 
 	if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
-		ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
-					 &open_int, &client_mode);
+		ret = rte_kvargs_process_opt(kvlist, ETH_VHOST_CLIENT_ARG,
+					     rte_kvargs_handle_bool, &client_mode);
 		if (ret < 0)
 			goto out_free;
 
@@ -1669,8 +1670,8 @@ rte_pmd_vhost_probe(struct rte_vdev_device *dev)
 	}
 
 	if (rte_kvargs_count(kvlist, ETH_VHOST_IOMMU_SUPPORT) == 1) {
-		ret = rte_kvargs_process(kvlist, ETH_VHOST_IOMMU_SUPPORT,
-					 &open_int, &iommu_support);
+		ret = rte_kvargs_process_opt(kvlist, ETH_VHOST_IOMMU_SUPPORT,
+					     rte_kvargs_handle_bool, &iommu_support);
 		if (ret < 0)
 			goto out_free;
 
@@ -1679,8 +1680,8 @@ rte_pmd_vhost_probe(struct rte_vdev_device *dev)
 	}
 
 	if (rte_kvargs_count(kvlist, ETH_VHOST_POSTCOPY_SUPPORT) == 1) {
-		ret = rte_kvargs_process(kvlist, ETH_VHOST_POSTCOPY_SUPPORT,
-					 &open_int, &postcopy_support);
+		ret = rte_kvargs_process_opt(kvlist, ETH_VHOST_POSTCOPY_SUPPORT,
+					     rte_kvargs_handle_bool, &postcopy_support);
 		if (ret < 0)
 			goto out_free;
 
@@ -1689,49 +1690,49 @@ rte_pmd_vhost_probe(struct rte_vdev_device *dev)
 	}
 
 	if (rte_kvargs_count(kvlist, ETH_VHOST_VIRTIO_NET_F_HOST_TSO) == 1) {
-		ret = rte_kvargs_process(kvlist,
+		ret = rte_kvargs_process_opt(kvlist,
 				ETH_VHOST_VIRTIO_NET_F_HOST_TSO,
-				&open_int, &tso);
+				rte_kvargs_handle_bool, &tso);
 		if (ret < 0)
 			goto out_free;
 	}
 
-	if (tso == 0) {
+	if (!tso) {
 		disable_flags |= (1ULL << VIRTIO_NET_F_HOST_TSO4);
 		disable_flags |= (1ULL << VIRTIO_NET_F_HOST_TSO6);
 	}
 
 	if (rte_kvargs_count(kvlist, ETH_VHOST_LINEAR_BUF) == 1) {
-		ret = rte_kvargs_process(kvlist,
+		ret = rte_kvargs_process_opt(kvlist,
 				ETH_VHOST_LINEAR_BUF,
-				&open_int, &linear_buf);
+				rte_kvargs_handle_bool, &linear_buf);
 		if (ret < 0)
 			goto out_free;
 
-		if (linear_buf == 1)
+		if (linear_buf)
 			flags |= RTE_VHOST_USER_LINEARBUF_SUPPORT;
 	}
 
 	if (rte_kvargs_count(kvlist, ETH_VHOST_EXT_BUF) == 1) {
-		ret = rte_kvargs_process(kvlist,
+		ret = rte_kvargs_process_opt(kvlist,
 				ETH_VHOST_EXT_BUF,
-				&open_int, &ext_buf);
+				rte_kvargs_handle_bool, &ext_buf);
 		if (ret < 0)
 			goto out_free;
 
-		if (ext_buf == 1)
+		if (ext_buf)
 			flags |= RTE_VHOST_USER_EXTBUF_SUPPORT;
 	}
 
 	if (rte_kvargs_count(kvlist, ETH_VHOST_LEGACY_OL_FLAGS) == 1) {
-		ret = rte_kvargs_process(kvlist,
+		ret = rte_kvargs_process_opt(kvlist,
 				ETH_VHOST_LEGACY_OL_FLAGS,
-				&open_int, &legacy_ol_flags);
+				rte_kvargs_handle_bool, &legacy_ol_flags);
 		if (ret < 0)
 			goto out_free;
 	}
 
-	if (legacy_ol_flags == 0)
+	if (!legacy_ol_flags)
 		flags |= RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
 
 	if (dev->device.numa_node == SOCKET_ID_ANY)
Signature · 1 line
2.53.0
#140 lore raw

PATCH v3 8/65 vdpa/ifc: use kvargs numeric helpers

In reply to Stephen Hemminger

open_int() stores through a uint16_t pointer but both callers pass the address of an int, so only two bytes of a four byte object are written. It also checks errno without resetting it first, and never checks the end pointer, so "vdpa=foo" is silently taken as zero.

Both arguments are used as booleans, so store them as bool and use rte_kvargs_handle_bool.

The booleans use rte_kvargs_process_opt(), so that a bare key with no value enables the option.

Bugzilla ID: 2040

Fixes 40ef35f4a504 ("net/ifc: detect if VDPA mode is specified")
---
 drivers/vdpa/ifc/ifcvf_vdpa.c | 29 +++++++----------------------
 1 file changed, 7 insertions(+), 22 deletions(-)
drivers/vdpa/ifc/ifcvf_vdpa.c +7 -22
index f319d455ba..1381f7cc9a 100644
@@ -1480,21 +1480,6 @@ static struct rte_vdpa_dev_ops ifcvf_net_ops = {
 	.get_dev_type = ifcvf_get_device_type,
 };
 
-static inline int
-open_int(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	uint16_t *n = extra_args;
-
-	if (value == NULL || extra_args == NULL)
-		return -EINVAL;
-
-	*n = (uint16_t)strtoul(value, NULL, 0);
-	if (*n == USHRT_MAX && errno == ERANGE)
-		return -1;
-
-	return 0;
-}
-
 static int16_t
 ifcvf_pci_get_device_type(struct rte_pci_device *pci_dev)
 {
@@ -1635,8 +1620,8 @@ ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
 	uint64_t features;
 	struct ifcvf_internal *internal = NULL;
 	struct internal_list *list = NULL;
-	int vdpa_mode = 0;
-	int sw_fallback_lm = 0;
+	bool vdpa_mode = false;
+	bool sw_fallback_lm = false;
 	struct rte_kvargs *kvlist = NULL;
 	int ret = 0;
 	int16_t device_id;
@@ -1662,9 +1647,9 @@ ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
 		return 1;
 	}
 
-	ret = rte_kvargs_process(kvlist, IFCVF_VDPA_MODE, &open_int,
-			&vdpa_mode);
-	if (ret < 0 || vdpa_mode == 0) {
+	ret = rte_kvargs_process_opt(kvlist, IFCVF_VDPA_MODE,
+			rte_kvargs_handle_bool, &vdpa_mode);
+	if (ret < 0 || !vdpa_mode) {
 		rte_kvargs_free(kvlist);
 		return 1;
 	}
@@ -1756,8 +1741,8 @@ ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
 	list->internal = internal;
 
 	if (rte_kvargs_count(kvlist, IFCVF_SW_FALLBACK_LM)) {
-		ret = rte_kvargs_process(kvlist, IFCVF_SW_FALLBACK_LM,
-				&open_int, &sw_fallback_lm);
+		ret = rte_kvargs_process_opt(kvlist, IFCVF_SW_FALLBACK_LM,
+				rte_kvargs_handle_bool, &sw_fallback_lm);
 		if (ret < 0)
 			goto error;
 	}
Signature · 1 line
2.53.0
#141 lore raw

PATCH v3 9/65 net/softnic: use kvargs numeric helpers

In reply to Stephen Hemminger

get_uint32() and get_uint16() check neither errno nor the end pointer, so a malformed value is silently taken as zero and an out of range one is truncated into the target.

The service cores argument is only ever tested for truth, so make it a bool. It was declared int but written through a uint32_t pointer.

The boolean uses rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 drivers/net/softnic/rte_eth_softnic.c         | 30 +++----------------
 .../net/softnic/rte_eth_softnic_internals.h   |  3 +-
 2 files changed, 6 insertions(+), 27 deletions(-)
drivers/net/softnic/rte_eth_softnic.c +4 -26
index 868e194509..81688d1dcf 100644
@@ -344,28 +344,6 @@ get_string(const char *key __rte_unused, const char *value, void *extra_args)
 	return 0;
 }
 
-static int
-get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	if (!value || !extra_args)
-		return -EINVAL;
-
-	*(uint32_t *)extra_args = strtoull(value, NULL, 0);
-
-	return 0;
-}
-
-static int
-get_uint16(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	if (!value || !extra_args)
-		return -EINVAL;
-
-	*(uint16_t *)extra_args = strtoull(value, NULL, 0);
-
-	return 0;
-}
-
 static int
 pmd_parse_args(struct pmd_params *p, const char *params)
 {
@@ -412,7 +390,7 @@ pmd_parse_args(struct pmd_params *p, const char *params)
 	/* Connection listening port (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) {
 		ret = rte_kvargs_process(kvlist, PMD_PARAM_CONN_PORT,
-			&get_uint16, &p->conn_port);
+			rte_kvargs_handle_u16, &p->conn_port);
 		if (ret < 0)
 			goto out_free;
 	}
@@ -420,15 +398,15 @@ pmd_parse_args(struct pmd_params *p, const char *params)
 	/* CPU ID (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
 		ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
-			&get_uint32, &p->cpu_id);
+			rte_kvargs_handle_u32, &p->cpu_id);
 		if (ret < 0)
 			goto out_free;
 	}
 
 	/* Service cores (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_SC) == 1) {
-		ret = rte_kvargs_process(kvlist, PMD_PARAM_SC,
-			&get_uint32, &p->sc);
+		ret = rte_kvargs_process_opt(kvlist, PMD_PARAM_SC,
+			rte_kvargs_handle_bool, &p->sc);
 		if (ret < 0)
 			goto out_free;
 	}
drivers/net/softnic/rte_eth_softnic_internals.h +2 -1
index d3975dad10..a1f309afb1 100644
@@ -5,6 +5,7 @@
 #ifndef __INCLUDE_RTE_ETH_SOFTNIC_INTERNALS_H__
 #define __INCLUDE_RTE_ETH_SOFTNIC_INTERNALS_H__
 
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <sys/queue.h>
@@ -34,7 +35,7 @@ struct pmd_params {
 	char firmware[SOFTNIC_PATH_MAX];
 	uint16_t conn_port;
 	uint32_t cpu_id;
-	int sc; /**< Service cores. */
+	bool sc; /**< Service cores. */
 };
 
 /**
Signature · 1 line
2.53.0
#142 lore raw

PATCH v3 10/65 dma/skeleton: use kvargs numeric helpers

In reply to Stephen Hemminger

The lcore argument is converted with atoi(), which validates nothing:
a malformed value such as "abc" becomes zero, and the range check that follows only catches a value which happens to land outside it.

Use rte_kvargs_to_int() with the lcore range, and log the key and the range when it does not fit.

The parsing function returns void and probe does not fail on a bad argument, which is left alone here: an unusable lcore still falls back to the default. Only the "Parse lcore_id" message is now skipped when the conversion failed, since it reported a value which had not been parsed at all.

---
 drivers/dma/skeleton/skeleton_dmadev.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
drivers/dma/skeleton/skeleton_dmadev.c +14 -9
index e287802eb9..44158d5a7f 100644
@@ -638,19 +638,24 @@ skeldma_destroy(const char *name)
 }
 
 static int
-skeldma_parse_lcore(const char *key __rte_unused,
+skeldma_parse_lcore(const char *key,
 		    const char *value,
 		    void *opaque)
 {
-	int lcore_id;
+	int64_t lcore_id;
+	int ret;
 
-	if (value == NULL || opaque == NULL)
+	if (opaque == NULL)
 		return -EINVAL;
 
-	lcore_id = atoi(value);
-	if (lcore_id >= 0 && lcore_id < RTE_MAX_LCORE)
-		*(int *)opaque = lcore_id;
+	ret = rte_kvargs_to_int(value, 0, RTE_MAX_LCORE - 1, &lcore_id);
+	if (ret < 0) {
+		SKELDMA_LOG(ERR, "Invalid %s, must be 0..%u", key,
+			    RTE_MAX_LCORE - 1);
+		return ret;
+	}
 
+	*(int *)opaque = lcore_id;
 	return 0;
 }
 
@@ -673,9 +678,9 @@ skeldma_parse_vdev_args(struct rte_vdev_device *vdev, int *lcore_id)
 	if (!kvlist)
 		return;
 
-	(void)rte_kvargs_process(kvlist, SKELDMA_ARG_LCORE,
-				 skeldma_parse_lcore, lcore_id);
-	SKELDMA_LOG(INFO, "Parse lcore_id = %d", *lcore_id);
+	if (rte_kvargs_process(kvlist, SKELDMA_ARG_LCORE,
+			       skeldma_parse_lcore, lcore_id) == 0)
+		SKELDMA_LOG(INFO, "Parse lcore_id = %d", *lcore_id);
 
 	rte_kvargs_free(kvlist);
 }
Signature · 1 line
2.53.0
#143 lore raw

Re: [PATCH v3 10/65] dma/skeleton: use kvargs numeric helpers

Quoted 34 lines · Stephen Hemminger

The lcore argument is converted with atoi(), which validates nothing:
a malformed value such as "abc" becomes zero, and the range check that follows only catches a value which happens to land outside it.

Use rte_kvargs_to_int() with the lcore range, and log the key and the range when it does not fit.

The parsing function returns void and probe does not fail on a bad argument, which is left alone here: an unusable lcore still falls back to the default. Only the "Parse lcore_id" message is now skipped when the conversion failed, since it reported a value which had not been parsed at all.

---
 drivers/dma/skeleton/skeleton_dmadev.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
drivers/dma/skeleton/skeleton_dmadev.c +3 -2
index e287802eb9..44158d5a7f 100644
@@ -638,19 +638,24 @@ skeldma_destroy(const char *name)
 }
 
 static int
-skeldma_parse_lcore(const char *key __rte_unused,
+skeldma_parse_lcore(const char *key,
 		    const char *value,
 		    void *opaque)
 {
-	int lcore_id;
+	int64_t lcore_id;
+	int ret;

drivers/dma/skeleton/skeleton_dmadev.c skeldma_destroy(const char *name) · line 652

Quoted 10 lines
drivers/dma/skeleton/skeleton_dmadev.c +3 -4
 
-	if (value == NULL || opaque == NULL)
+	if (opaque == NULL)
 		return -EINVAL;
 
-	lcore_id = atoi(value);
-	if (lcore_id >= 0 && lcore_id < RTE_MAX_LCORE)
-		*(int *)opaque = lcore_id;
+	ret = rte_kvargs_to_int(value, 0, RTE_MAX_LCORE - 1, &lcore_id);
+	if (ret < 0) {

Why not if (ret != 0) ?

Thanks

Quoted 22 lines
drivers/dma/skeleton/skeleton_dmadev.c +8 -3
+		SKELDMA_LOG(ERR, "Invalid %s, must be 0..%u", key,
+			    RTE_MAX_LCORE - 1);
+		return ret;
+	}
 
+	*(int *)opaque = lcore_id;
 	return 0;
 }
 
@@ -673,9 +678,9 @@ skeldma_parse_vdev_args(struct rte_vdev_device *vdev, int *lcore_id)
 	if (!kvlist)
 		return;
 
-	(void)rte_kvargs_process(kvlist, SKELDMA_ARG_LCORE,
-				 skeldma_parse_lcore, lcore_id);
-	SKELDMA_LOG(INFO, "Parse lcore_id = %d", *lcore_id);
+	if (rte_kvargs_process(kvlist, SKELDMA_ARG_LCORE,
+			       skeldma_parse_lcore, lcore_id) == 0)
+		SKELDMA_LOG(INFO, "Parse lcore_id = %d", *lcore_id);
 
 	rte_kvargs_free(kvlist);
 }
#144 lore raw

PATCH v3 11/65 raw/skeleton: use kvargs numeric helpers

In reply to Stephen Hemminger

The selftest argument is converted with atoi(), so a malformed value is silently taken as zero. Make it a bool and use rte_kvargs_handle_bool, which rejects anything which is not a recognized boolean and removes the need for the separate range check on the result.

The boolean uses rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 drivers/raw/skeleton/skeleton_rawdev.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)
drivers/raw/skeleton/skeleton_rawdev.c +4 -16
index 6e99d35536..4e20952e47 100644
@@ -658,22 +658,10 @@ skeleton_rawdev_destroy(const char *name)
 	return 0;
 }
 
-static int
-skeldev_get_selftest(const char *key __rte_unused,
-		     const char *value,
-		     void *opaque)
-{
-	int *flag = opaque;
-	if (value == NULL || opaque == NULL)
-		return -EINVAL;
-	*flag = atoi(value);
-	return 0;
-}
-
 static int
 skeldev_parse_vdev_args(struct rte_vdev_device *vdev)
 {
-	int selftest = 0;
+	bool selftest = false;
 	const char *name;
 	const char *params;
 
@@ -693,10 +681,10 @@ skeldev_parse_vdev_args(struct rte_vdev_device *vdev)
 				"Ignoring unsupported params supplied '%s'",
 				name);
 		} else {
-			int ret = rte_kvargs_process(kvlist,
+			int ret = rte_kvargs_process_opt(kvlist,
 					SKELETON_SELFTEST_ARG,
-					skeldev_get_selftest, &selftest);
-			if (ret != 0 || (selftest < 0 || selftest > 1)) {
+					rte_kvargs_handle_bool, &selftest);
+			if (ret != 0) {
 				SKELETON_PMD_ERR("%s: Error in parsing args",
 						 name);
 				rte_kvargs_free(kvlist);
Signature · 1 line
2.53.0
#145 lore raw

PATCH v3 12/65 baseband/null: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_u16_arg() does not check the end pointer, so "max_nb_queues=foo" is silently taken as zero.

It was used for socket_id as well, which is an int rather than a uint16_t, so parsing it wrote only two of the four bytes. Use rte_kvargs_handle_socket_id() for that argument.

As in turbo_sw, the open coded range check only tested the upper bound, so a negative socket id was accepted. The helper checks both ends, so the check is removed.

Bugzilla ID: 2041

Fixes 7dc2b1589440 ("bb/null: add null base band device driver")
---
 drivers/baseband/null/bbdev_null.c | 29 ++---------------------------
 1 file changed, 2 insertions(+), 27 deletions(-)
drivers/baseband/null/bbdev_null.c +2 -27
index cfa1790126..fc23a84f32 100644
@@ -202,25 +202,6 @@ dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
 	return nb_dequeued;
 }
 
-/* Parse 16bit integer from string argument */
-static inline int
-parse_u16_arg(const char *key, const char *value, void *extra_args)
-{
-	uint16_t *u16 = extra_args;
-	unsigned int long result;
-
-	if ((value == NULL) || (extra_args == NULL))
-		return -EINVAL;
-	errno = 0;
-	result = strtoul(value, NULL, 0);
-	if ((result >= (1 << 16)) || (errno != 0)) {
-		rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
-		return -ERANGE;
-	}
-	*u16 = (uint16_t)result;
-	return 0;
-}
-
 /* Parse parameters used to create device */
 static int
 parse_bbdev_null_params(struct bbdev_null_params *params,
@@ -237,20 +218,14 @@ parse_bbdev_null_params(struct bbdev_null_params *params,
 			return -EFAULT;
 
 		ret = rte_kvargs_process(kvlist, bbdev_null_valid_params[0],
-					&parse_u16_arg, &params->queues_num);
+					rte_kvargs_handle_u16, &params->queues_num);
 		if (ret < 0)
 			goto exit;
 
 		ret = rte_kvargs_process(kvlist, bbdev_null_valid_params[1],
-					&parse_u16_arg, &params->socket_id);
+					rte_kvargs_handle_socket_id, &params->socket_id);
 		if (ret < 0)
 			goto exit;
-
-		if (params->socket_id >= RTE_MAX_NUMA_NODES) {
-			rte_bbdev_log(ERR, "Invalid socket, must be < %u",
-					RTE_MAX_NUMA_NODES);
-			goto exit;
-		}
 	}
 
 exit:
Signature · 1 line
2.53.0
#146 lore raw

PATCH v3 13/65 net/memif: use kvargs numeric helpers

In reply to Stephen Hemminger

memif_set_id() dereferences the value without checking it for NULL, so "net_memif,id" with no value crashes. It then ignores the result of the conversion entirely, with a comment noting that zero is a valid id, so "id=foo" silently selects interface zero.

memif_set_bs() and memif_set_rs() check neither errno nor the end pointer, so a malformed value is taken as zero and rejected with a misleading message, while an overflowing one wraps into range.

memif_set_owner() is nearly correct but does not reset errno before strtoul(), and rejects UINT32_MAX which is a valid uid.

---
 drivers/net/memif/rte_eth_memif.c | 73 ++++++++++++++-----------------
 1 file changed, 34 insertions(+), 39 deletions(-)
drivers/net/memif/rte_eth_memif.c +34 -39
index 1c8dc1d5ec..212947c833 100644
@@ -1807,45 +1807,42 @@ memif_set_zc(const char *key __rte_unused, const char *value, void *extra_args)
 	return 0;
 }
 
-static int
-memif_set_id(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	memif_interface_id_t *id = (memif_interface_id_t *)extra_args;
-
-	/* even if parsing fails, 0 is a valid id */
-	*id = strtoul(value, NULL, 10);
-	return 0;
-}
-
 static int
 memif_set_bs(const char *key __rte_unused, const char *value, void *extra_args)
 {
-	unsigned long tmp;
-	uint16_t *pkt_buffer_size = (uint16_t *)extra_args;
+	uint64_t tmp;
+	int ret;
 
-	tmp = strtoul(value, NULL, 10);
-	if (tmp == 0 || tmp > 0xFFFF) {
-		MIF_LOG(ERR, "Invalid buffer size: %s.", value);
+	if (extra_args == NULL)
 		return -EINVAL;
+
+	ret = rte_kvargs_to_uint(value, 1, UINT16_MAX, &tmp);
+	if (ret < 0) {
+		MIF_LOG(ERR, "Invalid buffer size: %s.", value);
+		return ret;
 	}
-	*pkt_buffer_size = tmp;
+
+	*(uint16_t *)extra_args = tmp;
 	return 0;
 }
 
 static int
 memif_set_rs(const char *key __rte_unused, const char *value, void *extra_args)
 {
-	unsigned long tmp;
-	memif_log2_ring_size_t *log2_ring_size =
-	    (memif_log2_ring_size_t *)extra_args;
+	uint64_t tmp;
+	int ret;
 
-	tmp = strtoul(value, NULL, 10);
-	if (tmp == 0 || tmp > ETH_MEMIF_MAX_LOG2_RING_SIZE) {
+	if (extra_args == NULL)
+		return -EINVAL;
+
+	ret = rte_kvargs_to_uint(value, 1, ETH_MEMIF_MAX_LOG2_RING_SIZE, &tmp);
+	if (ret < 0) {
 		MIF_LOG(ERR, "Invalid ring size: %s (max %u).",
 			value, ETH_MEMIF_MAX_LOG2_RING_SIZE);
-		return -EINVAL;
+		return ret;
 	}
-	*log2_ring_size = tmp;
+
+	*(memif_log2_ring_size_t *)extra_args = tmp;
 	return 0;
 }
 
@@ -1917,21 +1914,19 @@ memif_set_owner(const char *key, const char *value, void *extra_args)
 	RTE_ASSERT(sizeof(uid_t) == sizeof(uint32_t));
 	RTE_ASSERT(sizeof(gid_t) == sizeof(uint32_t));
 
-	unsigned long val;
-	char *end = NULL;
-	uint32_t *id = (uint32_t *)extra_args;
+	uint64_t val;
+	int ret;
 
-	val = strtoul(value, &end, 10);
-	if (*value == '\0' || *end != '\0') {
-		MIF_LOG(ERR, "Failed to parse %s: %s.", key, value);
+	if (extra_args == NULL)
 		return -EINVAL;
-	}
-	if (val >= UINT32_MAX) {
-		MIF_LOG(ERR, "Invalid %s: %s.", key, value);
-		return -ERANGE;
+
+	ret = rte_kvargs_to_uint(value, 0, UINT32_MAX, &val);
+	if (ret < 0) {
+		MIF_LOG(ERR, "Failed to parse %s: %s.", key, value);
+		return ret;
 	}
 
-	*id = val;
+	*(uint32_t *)extra_args = val;
 	return 0;
 }
 
@@ -2032,15 +2027,15 @@ rte_pmd_memif_probe(struct rte_vdev_device *vdev)
 		if (ret < 0)
 			goto exit;
 		ret = rte_kvargs_process(kvlist, ETH_MEMIF_ID_ARG,
-					 &memif_set_id, &id);
+					 rte_kvargs_handle_u32, &id);
 		if (ret < 0)
 			goto exit;
 		ret = rte_kvargs_process(kvlist, ETH_MEMIF_PKT_BUFFER_SIZE_ARG,
-					 &memif_set_bs, &pkt_buffer_size);
+					 memif_set_bs, &pkt_buffer_size);
 		if (ret < 0)
 			goto exit;
 		ret = rte_kvargs_process(kvlist, ETH_MEMIF_RING_SIZE_ARG,
-					 &memif_set_rs, &log2_ring_size);
+					 memif_set_rs, &log2_ring_size);
 		if (ret < 0)
 			goto exit;
 		ret = rte_kvargs_process(kvlist, ETH_MEMIF_SOCKET_ARG,
@@ -2053,11 +2048,11 @@ rte_pmd_memif_probe(struct rte_vdev_device *vdev)
 		if (ret < 0)
 			goto exit;
 		ret = rte_kvargs_process(kvlist, ETH_MEMIF_OWNER_UID_ARG,
-					 &memif_set_owner, &owner_uid);
+					 memif_set_owner, &owner_uid);
 		if (ret < 0)
 			goto exit;
 		ret = rte_kvargs_process(kvlist, ETH_MEMIF_OWNER_GID_ARG,
-					 &memif_set_owner, &owner_gid);
+					 memif_set_owner, &owner_gid);
 		if (ret < 0)
 			goto exit;
 		ret = rte_kvargs_process(kvlist, ETH_MEMIF_MAC_ARG,
Signature · 1 line
2.53.0
#147 lore raw

PATCH v3 14/65 net/af_packet: use kvargs numeric helpers

In reply to Stephen Hemminger

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 is a boolean, so parse it 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 the qdisc_bypass parameter of rte_pmd_init_internals() stays an unsigned int: its address is passed to setsockopt(), which expects a four byte int.

---
 drivers/net/af_packet/rte_eth_af_packet.c | 72 +++++------------------
 1 file changed, 16 insertions(+), 56 deletions(-)
drivers/net/af_packet/rte_eth_af_packet.c +16 -56
index a93df97023..18153d9e7e 100644
@@ -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>
@@ -1156,36 +1155,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;
 }
 
@@ -1204,7 +1184,7 @@ 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;
 
 	/* do some parameter checking */
@@ -1219,52 +1199,32 @@ 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)
+			if (parse_uint(pair->key, pair->value, &qpairs,
+				       1, RTE_MAX_QUEUES_PER_PORT) < 0)
 				return -1;
-			if (qpairs < 1) {
-				PMD_LOG(ERR,
-					"%s: invalid qpairs value",
-					name);
-				return -1;
-			}
 			continue;
 		}
 		if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
-			if (parse_uint(pair->key, pair->value, &blocksize, UINT_MAX) < 0)
+			if (parse_uint(pair->key, pair->value, &blocksize,
+				       1, UINT_MAX) < 0)
 				return -1;
-			if (!blocksize) {
-				PMD_LOG(ERR,
-					"%s: invalid blocksize value",
-					name);
-				return -1;
-			}
 			continue;
 		}
 		if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
-			if (parse_uint(pair->key, pair->value, &framesize, UINT_MAX) < 0)
+			if (parse_uint(pair->key, pair->value, &framesize,
+				       1, UINT_MAX) < 0)
 				return -1;
-			if (!framesize) {
-				PMD_LOG(ERR,
-					"%s: invalid framesize value",
-					name);
-				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;
 		}
Signature · 1 line
2.53.0
#148 lore raw

PATCH v3 15/65 net/pcap: use kvargs numeric helpers

In reply to Stephen Hemminger

The open coded conversion is correct, but the same checks are now available from kvargs.

process_bool_flag() duplicated what rte_kvargs_handle_bool() does, so drop it and use the helper for phy_mac, infinite_rx and eof. The process_opt() form newly enables the bare "infinite_rx" spelling: the value == NULL branch of process_bool_flag() was dead, since rte_kvargs_process() rejects a missing value before the handler runs. The usual words such as "on" and "true" are now accepted as well, while an empty "infinite_rx=" is rejected rather than taken as true.

---
 drivers/net/pcap/pcap_ethdev.c | 51 ++++++++--------------------------
 1 file changed, 12 insertions(+), 39 deletions(-)
drivers/net/pcap/pcap_ethdev.c +12 -39
index b1f3718c8a..df3a480f97 100644
@@ -1584,46 +1584,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args)
 	return open_iface(key, value, extra_args);
 }
 
-static int
-process_bool_flag(const char *key, const char *value, void *extra_args)
-{
-	bool *flag = extra_args;
-
-	if (value == NULL || *value == '\0') {
-		*flag = true; /* default with no additional argument */
-	} else if (strcmp(value, "0") == 0) {
-		*flag = false;
-	} else if (strcmp(value, "1") == 0) {
-		*flag = true;
-	} else {
-		PMD_LOG(ERR, "Invalid '%s' value '%s'", key, value);
-		return -1;
-	}
-	return 0;
-}
-
 static int
 process_snapshot_len(const char *key, const char *value, void *extra_args)
 {
-	uint32_t *snaplen = extra_args;
-	unsigned long val;
-	char *endptr;
-
-	if (value == NULL || *value == '\0') {
-		PMD_LOG(ERR, "Argument '%s' requires a value", key);
-		return -1;
-	}
+	uint64_t val;
 
-	errno = 0;
-	val = strtoul(value, &endptr, 10);
-	if (errno != 0 || *endptr != '\0' ||
-	    val < RTE_ETHER_HDR_LEN ||
-	    val > ETH_PCAP_MAXIMUM_SNAPLEN) {
-		PMD_LOG(ERR, "Invalid '%s' value '%s'", key, value);
+	if (rte_kvargs_to_uint(value, RTE_ETHER_HDR_LEN,
+			       ETH_PCAP_MAXIMUM_SNAPLEN, &val) < 0) {
+		PMD_LOG(ERR, "Invalid '%s' value '%s'", key,
+			value == NULL ? "" : value);
 		return -1;
 	}
 
-	*snaplen = (uint32_t)val;
+	*(uint32_t *)extra_args = val;
 	return 0;
 }
 
@@ -1932,8 +1905,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)
 
 		dumpers.queue[0] = pcaps.queue[0];
 
-		ret = rte_kvargs_process(kvlist, ETH_PCAP_PHY_MAC_ARG,
-					 &process_bool_flag, &pcaps.phy_mac);
+		ret = rte_kvargs_process_opt(kvlist, ETH_PCAP_PHY_MAC_ARG,
+					 rte_kvargs_handle_bool, &pcaps.phy_mac);
 		if (ret < 0)
 			goto free_kvlist;
 
@@ -1971,9 +1944,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)
 				ETH_PCAP_INFINITE_RX_ARG);
 
 		if (infinite_rx_arg_cnt == 1) {
-			ret = rte_kvargs_process(kvlist,
+			ret = rte_kvargs_process_opt(kvlist,
 					ETH_PCAP_INFINITE_RX_ARG,
-					 &process_bool_flag,
+					 rte_kvargs_handle_bool,
 					 &devargs_all.infinite_rx);
 			if (ret < 0)
 				goto free_kvlist;
@@ -1991,8 +1964,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)
 		 * Check whether to signal EOF via link status change.
 		 */
 		if (rte_kvargs_count(kvlist, ETH_PCAP_EOF_ARG) == 1) {
-			ret = rte_kvargs_process(kvlist, ETH_PCAP_EOF_ARG,
-						 &process_bool_flag,
+			ret = rte_kvargs_process_opt(kvlist, ETH_PCAP_EOF_ARG,
+						 rte_kvargs_handle_bool,
 						 &devargs_all.eof);
 			if (ret < 0)
 				goto free_kvlist;
Signature · 1 line
2.53.0
#149 lore raw

PATCH v3 16/65 net/ring: use kvargs numeric helpers

In reply to Stephen Hemminger

The node number was converted with strtol() and checked only for trailing garbage, so any value in range was taken. A socket id above RTE_MAX_NUMA_NODES was accepted and failed later in the allocation.

Convert with rte_kvargs_to_int() over the same range as rte_kvargs_handle_socket_id(), so -1 keeps meaning SOCKET_ID_ANY and anything above the last socket is rejected where it is parsed. The handler cannot be used directly since the value is one field of "name:node:action" rather than the whole devarg.

Report a strdup() failure as -ENOMEM instead of reusing the empty parameter message.

---
 drivers/net/ring/rte_eth_ring.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
drivers/net/ring/rte_eth_ring.c +13 -10
index 533a4c1b6b..a2ac704a8d 100644
@@ -579,21 +579,25 @@ static int parse_kvlist(const char *key __rte_unused,
 			const char *value, void *data)
 {
 	struct node_action_list *info = data;
+	int64_t socket_val;
 	int ret;
-	char *name;
+	char *name = NULL;
 	char *action;
 	char *node;
-	char *end;
-
-	name = strdup(value);
 
 	ret = -EINVAL;
 
-	if (!name) {
+	if (value == NULL) {
 		PMD_LOG(WARNING, "command line parameter is empty for ring pmd!");
 		goto out;
 	}
 
+	name = strdup(value);
+	if (!name) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
 	node = strchr(name, ':');
 	if (!node) {
 		PMD_LOG(WARNING, "could not parse node value from %s",
@@ -625,14 +629,13 @@ static int parse_kvlist(const char *key __rte_unused,
 	else
 		goto out;
 
-	errno = 0;
-	info->list[info->count].socket_id = strtol(node, &end, 10);
-
-	if ((errno != 0) || (*end != '\0')) {
+	/* -1 is SOCKET_ID_ANY, the range rte_kvargs_handle_socket_id() takes. */
+	if (rte_kvargs_to_int(node, -1, RTE_MAX_NUMA_NODES - 1, &socket_val) < 0) {
 		PMD_LOG(WARNING,
-			"node value %s is unparseable as a number", node);
+			"node value %s is not a valid socket id", node);
 		goto out;
 	}
+	info->list[info->count].socket_id = socket_val;
 
 	strlcpy(info->list[info->count].name, name,
 		sizeof(info->list[info->count].name));
Signature · 1 line
2.53.0
#150 lore raw

PATCH v3 17/65 net/af_xdp: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_integer_arg(), parse_budget_arg() and parse_hex_arg() open coded the string to number conversion. parse_budget_arg() checked neither the end pointer nor errno, so "busy_budget=foo" was silently taken as zero, and it stored the unvalidated result before testing it, so a rejected argument still overwrote the caller's default. Replace all three with the kvargs helpers, keeping only the wrappers which apply a range or a width that no handler provides.

The shared_umem, force_copy, use_cni and use_pinned_map arguments are booleans, and are already stored as bool in the internals, so parse them as bool rather than as int. They use rte_kvargs_process_opt(), so that a bare key with no value enables the option.

Queue counts and indexes are counts, not signed quantities, so use uint16_t for them; the ethdev layer cannot address more queues than that anyway. The ethtool channel counts are 32 bit, so clamp them to RTE_MAX_QUEUES_PER_PORT rather than truncating.

Collect the parsed arguments into a struct af_xdp_devargs and drive the parsing from a table, which replaces the fifteen argument parameter list of parse_parameters() and the thirteen of init_internals() with a single pointer. The kvargs list is now freed by the function which parsed it, rather than by the one which did not allocate it, which also fixes the leak on the paths where probe returned early after parse_parameters() succeeded.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 431 ++++++++++++++--------------
 1 file changed, 217 insertions(+), 214 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +217 -214
index 9c99dcec20..6d346a736f 100644
@@ -178,10 +178,10 @@ struct pkt_tx_queue {
 struct pmd_internals {
 	int if_index;
 	char if_name[IFNAMSIZ];
-	int start_queue_idx;
-	int queue_cnt;
-	int max_queue_cnt;
-	int configured_queue_cnt;
+	uint16_t start_queue_idx;
+	uint16_t queue_cnt;
+	uint16_t max_queue_cnt;
+	uint16_t configured_queue_cnt;
 	uint32_t mode_flag;
 	bool shared_umem;
 	char prog_path[PATH_MAX];
@@ -258,6 +258,24 @@ static const char * const valid_arguments[] = {
 	NULL
 };
 
+/* Device arguments, as parsed from the vdev argument string. */
+struct af_xdp_devargs {
+	char if_name[IFNAMSIZ];
+	uint16_t start_queue_idx;
+	uint16_t queue_cnt;
+	bool shared_umem;
+	char prog_path[PATH_MAX];
+	int busy_budget;		/**< -1 until the default is applied */
+	bool force_copy;
+	bool use_cni;
+	bool use_pinned_map;
+	char dp_path[PATH_MAX];
+	uint32_t xdp_mode;
+	int rx_timestamp_offset;	/**< -1 when not specified */
+	int rx_timestamp_valid_offset;	/**< -1 when not specified */
+	uint8_t rx_timestamp_valid_mask;
+};
+
 static const struct rte_eth_link pmd_link = {
 	.link_speed = RTE_ETH_SPEED_NUM_10G,
 	.link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
@@ -2270,67 +2288,85 @@ static const struct eth_dev_ops ops_afxdp_dp = {
 	.read_clock = eth_af_xdp_read_clock,
 };
 
-/** parse busy_budget argument */
+/**
+ * Parse busy_budget, which is a uint16_t stored in an int so that -1 can
+ * mean "not specified" until the default is applied.
+ */
 static int
 parse_budget_arg(const char *key __rte_unused,
 		  const char *value, void *extra_args)
 {
-	int *i = (int *)extra_args;
-	char *end;
+	int64_t budget;
+	int ret;
 
-	*i = strtol(value, &end, 10);
-	if (*i < 0 || *i > UINT16_MAX) {
-		AF_XDP_LOG_LINE(ERR, "Invalid busy_budget %i, must be >= 0 and <= %u",
-				*i, UINT16_MAX);
-		return -EINVAL;
+	ret = rte_kvargs_to_int(value, 0, UINT16_MAX, &budget);
+	if (ret < 0) {
+		AF_XDP_LOG_LINE(ERR, "Invalid busy_budget, must be >= 0 and <= %u",
+				UINT16_MAX);
+		return ret;
 	}
 
+	*(int *)extra_args = budget;
 	return 0;
 }
 
-/** parse integer from integer argument */
+/** parse queue_count, which must leave at least one queue */
 static int
-parse_integer_arg(const char *key __rte_unused,
-		  const char *value, void *extra_args)
+parse_queue_count_arg(const char *key __rte_unused,
+		      const char *value, void *extra_args)
 {
-	int *i = (int *)extra_args;
-	char *end;
-	long val;
-
-	if (value == NULL || extra_args == NULL)
-		return -EINVAL;
+	uint64_t queue_cnt;
+	int ret;
 
-	errno = 0;
-	val = strtol(value, &end, 10);
-	if (errno != 0 || end == value || *end != '\0' || val < 0 || val > INT_MAX) {
-		AF_XDP_LOG_LINE(ERR, "Argument has to be a valid non-negative integer.");
-		return -EINVAL;
+	ret = rte_kvargs_to_uint(value, 1, RTE_MAX_QUEUES_PER_PORT, &queue_cnt);
+	if (ret < 0) {
+		AF_XDP_LOG_LINE(ERR, "Invalid queue count, must be >= 1 and <= %u",
+				RTE_MAX_QUEUES_PER_PORT);
+		return ret;
 	}
 
-	*i = (int)val;
+	*(uint16_t *)extra_args = queue_cnt;
 	return 0;
 }
 
-/** parse hex from argument */
+/**
+ * Parse a metadata offset into the packet headroom. Stored in an int so
+ * that -1 can mean "not specified"; a negative value from the user would
+ * be indistinguishable from that, so only zero and above are accepted.
+ */
 static int
-parse_hex_arg(const char *key __rte_unused,
-	      const char *value, void *extra_args)
+parse_offset_arg(const char *key __rte_unused,
+		 const char *value, void *extra_args)
 {
-	int *i = (int *)extra_args;
-	char *end;
-	unsigned long val;
+	int64_t offset;
+	int ret;
 
-	if (value == NULL || extra_args == NULL)
-		return -EINVAL;
+	ret = rte_kvargs_to_int(value, 0, XDP_PACKET_HEADROOM, &offset);
+	if (ret < 0) {
+		AF_XDP_LOG_LINE(ERR, "Invalid metadata offset, must be >= 0 and <= %u",
+				XDP_PACKET_HEADROOM);
+		return ret;
+	}
 
-	errno = 0;
-	val = strtoul(value, &end, 16);
-	if (errno != 0 || end == value || *end != '\0' || val > UINT8_MAX) {
+	*(int *)extra_args = offset;
+	return 0;
+}
+
+/** parse the validity hint mask, a bare hexadecimal byte */
+static int
+parse_mask_arg(const char *key __rte_unused,
+	       const char *value, void *extra_args)
+{
+	uint64_t mask;
+	int ret;
+
+	ret = rte_kvargs_to_hex(value, UINT8_MAX, &mask);
+	if (ret < 0) {
 		AF_XDP_LOG_LINE(ERR, "Validity mask must be a valid hex byte (0-0xFF).");
-		return -EINVAL;
+		return ret;
 	}
 
-	*i = (int)val;
+	*(uint8_t *)extra_args = mask;
 	return 0;
 }
 
@@ -2396,12 +2432,14 @@ parse_prog_arg(const char *key __rte_unused,
 }
 
 static int
-xdp_get_channels_info(const char *if_name, int *max_queues,
-				int *configured_queues)
+xdp_get_channels_info(const char *if_name, uint16_t *max_queues,
+				uint16_t *configured_queues)
 {
+	const uint32_t max_q = RTE_MAX_QUEUES_PER_PORT;
 	struct ethtool_channels channels;
 	struct ifreq ifr;
-	int fd, ret, rxtx_q_count;
+	uint32_t rxtx_q_count;
+	int fd, ret;
 
 	fd = socket(AF_INET, SOCK_DGRAM, 0);
 	if (fd < 0)
@@ -2423,6 +2461,9 @@ xdp_get_channels_info(const char *if_name, int *max_queues,
 	/* For drivers with rx/tx queue configured */
 	rxtx_q_count = RTE_MIN(channels.rx_count, channels.tx_count);
 
+	/* The ethtool counts are 32 bit, but a port cannot have more
+	 * queues than the ethdev layer allows.
+	 */
 	if ((channels.max_combined == 0 && rxtx_q_count == 0) || errno == EOPNOTSUPP) {
 		/* If the device says it has no channels, then all traffic
 		 * is sent to a single stream, so max queues = 1.
@@ -2430,12 +2471,14 @@ xdp_get_channels_info(const char *if_name, int *max_queues,
 		*max_queues = 1;
 		*configured_queues = 1;
 	} else if (channels.max_combined > 0) {
-		*max_queues = channels.max_combined;
-		*configured_queues = channels.combined_count;
+		*max_queues = RTE_MIN(channels.max_combined, max_q);
+		*configured_queues = RTE_MIN(channels.combined_count, max_q);
 		AF_XDP_LOG_LINE(INFO, "Using Combined queues configuration");
 	} else {
-		*max_queues = RTE_MIN(channels.max_rx, channels.max_tx);
-		*configured_queues = rxtx_q_count;
+		uint32_t max_rxtx = RTE_MIN(channels.max_rx, channels.max_tx);
+
+		*max_queues = RTE_MIN(max_rxtx, max_q);
+		*configured_queues = RTE_MIN(rxtx_q_count, max_q);
 		AF_XDP_LOG_LINE(INFO, "Using Rx/Tx queues configuration");
 	}
 
@@ -2445,92 +2488,64 @@ xdp_get_channels_info(const char *if_name, int *max_queues,
 }
 
 static int
-parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
-		 int *queue_cnt, int *shared_umem, char *prog_path,
-		 int *busy_budget, int *force_copy, int *use_cni,
-		 int *use_pinned_map, char *dp_path, uint32_t *xdp_mode,
-		 int *rx_timestamp_offset, int *rx_timestamp_valid_offset,
-		 int *rx_timestamp_valid_mask)
+parse_parameters(struct rte_kvargs *kvlist, struct af_xdp_devargs *devargs)
 {
-	int ret;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
-				 &parse_name_arg, if_name);
-	if (ret < 0)
-		goto free_kvlist;
+	/*
+	 * The boolean arguments are processed with rte_kvargs_process_opt()
+	 * so that a bare key, with no value, enables the option.
+	 */
+	static const struct {
+		const char *key;
+		arg_handler_t handler;
+		size_t offset;
+		bool optional_value;
+	} args[] = {
+		{ ETH_AF_XDP_IFACE_ARG, parse_name_arg,
+		  offsetof(struct af_xdp_devargs, if_name) },
+		{ ETH_AF_XDP_START_QUEUE_ARG, rte_kvargs_handle_u16,
+		  offsetof(struct af_xdp_devargs, start_queue_idx) },
+		{ ETH_AF_XDP_QUEUE_COUNT_ARG, parse_queue_count_arg,
+		  offsetof(struct af_xdp_devargs, queue_cnt) },
+		{ ETH_AF_XDP_SHARED_UMEM_ARG, rte_kvargs_handle_bool,
+		  offsetof(struct af_xdp_devargs, shared_umem), true },
+		{ ETH_AF_XDP_PROG_ARG, parse_prog_arg,
+		  offsetof(struct af_xdp_devargs, prog_path) },
+		{ ETH_AF_XDP_BUDGET_ARG, parse_budget_arg,
+		  offsetof(struct af_xdp_devargs, busy_budget) },
+		{ ETH_AF_XDP_FORCE_COPY_ARG, rte_kvargs_handle_bool,
+		  offsetof(struct af_xdp_devargs, force_copy), true },
+		{ ETH_AF_XDP_USE_CNI_ARG, rte_kvargs_handle_bool,
+		  offsetof(struct af_xdp_devargs, use_cni), true },
+		{ ETH_AF_XDP_USE_PINNED_MAP_ARG, rte_kvargs_handle_bool,
+		  offsetof(struct af_xdp_devargs, use_pinned_map), true },
+		{ ETH_AF_XDP_DP_PATH_ARG, parse_prog_arg,
+		  offsetof(struct af_xdp_devargs, dp_path) },
+		{ ETH_AF_XDP_MODE_ARG, parse_mode_arg,
+		  offsetof(struct af_xdp_devargs, xdp_mode) },
+		{ ETH_AF_XDP_RX_TIMESTAMP_OFFSET_ARG, parse_offset_arg,
+		  offsetof(struct af_xdp_devargs, rx_timestamp_offset) },
+		{ ETH_AF_XDP_RX_TIMESTAMP_VALID_OFFSET_ARG, parse_offset_arg,
+		  offsetof(struct af_xdp_devargs, rx_timestamp_valid_offset) },
+		{ ETH_AF_XDP_RX_TIMESTAMP_VALID_MASK_ARG, parse_mask_arg,
+		  offsetof(struct af_xdp_devargs, rx_timestamp_valid_mask) },
+	};
+	unsigned int i;
 
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
-				 &parse_integer_arg, start_queue);
-	if (ret < 0)
-		goto free_kvlist;
+	for (i = 0; i < RTE_DIM(args); i++) {
+		void *target = RTE_PTR_ADD(devargs, args[i].offset);
+		int ret;
 
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
-				 &parse_integer_arg, queue_cnt);
-	if (ret < 0 || *queue_cnt <= 0) {
-		ret = -EINVAL;
-		goto free_kvlist;
+		if (args[i].optional_value)
+			ret = rte_kvargs_process_opt(kvlist, args[i].key,
+						     args[i].handler, target);
+		else
+			ret = rte_kvargs_process(kvlist, args[i].key,
+						 args[i].handler, target);
+		if (ret < 0)
+			return ret;
 	}
 
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_SHARED_UMEM_ARG,
-				&parse_integer_arg, shared_umem);
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PROG_ARG,
-				 &parse_prog_arg, prog_path);
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_BUDGET_ARG,
-				&parse_budget_arg, busy_budget);
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_FORCE_COPY_ARG,
-				&parse_integer_arg, force_copy);
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_USE_CNI_ARG,
-				 &parse_integer_arg, use_cni);
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_USE_PINNED_MAP_ARG,
-				 &parse_integer_arg, use_pinned_map);
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_DP_PATH_ARG,
-				 &parse_prog_arg, dp_path);
-
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_MODE_ARG,
-				 &parse_mode_arg, xdp_mode);
-
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_RX_TIMESTAMP_OFFSET_ARG,
-				 &parse_integer_arg, rx_timestamp_offset);
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_RX_TIMESTAMP_VALID_OFFSET_ARG,
-				 &parse_integer_arg, rx_timestamp_valid_offset);
-	if (ret < 0)
-		goto free_kvlist;
-
-	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_RX_TIMESTAMP_VALID_MASK_ARG,
-				 &parse_hex_arg, rx_timestamp_valid_mask);
-	if (ret < 0)
-		goto free_kvlist;
-
-free_kvlist:
-	rte_kvargs_free(kvlist);
-	return ret;
+	return 0;
 }
 
 static int
@@ -2564,57 +2579,54 @@ get_iface_info(const char *if_name,
 }
 
 static struct rte_eth_dev *
-init_internals(struct rte_vdev_device *dev, const char *if_name,
-	       int start_queue_idx, int queue_cnt, int shared_umem,
-	       const char *prog_path, int busy_budget, int force_copy,
-	       int use_cni, int use_pinned_map, const char *dp_path, uint32_t xdp_mode,
-	       int rx_timestamp_offset, int rx_timestamp_valid_offset,
-	       int rx_timestamp_valid_mask)
+init_internals(struct rte_vdev_device *dev,
+	       const struct af_xdp_devargs *devargs)
 {
 	const char *name = rte_vdev_device_name(dev);
 	const unsigned int numa_node = dev->device.numa_node;
+	const uint16_t queue_cnt = devargs->queue_cnt;
 	struct pmd_process_private *process_private;
 	struct pmd_internals *internals;
 	struct rte_eth_dev *eth_dev;
+	uint16_t i;
 	int ret;
-	int i;
 
 	internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
 	if (internals == NULL)
 		return NULL;
 
-	internals->start_queue_idx = start_queue_idx;
+	internals->start_queue_idx = devargs->start_queue_idx;
 	internals->queue_cnt = queue_cnt;
-	strlcpy(internals->if_name, if_name, IFNAMSIZ);
-	strlcpy(internals->prog_path, prog_path, PATH_MAX);
+	strlcpy(internals->if_name, devargs->if_name, IFNAMSIZ);
+	strlcpy(internals->prog_path, devargs->prog_path, PATH_MAX);
 	internals->custom_prog_configured = 0;
 
 #ifndef ETH_AF_XDP_SHARED_UMEM
-	if (shared_umem) {
+	if (devargs->shared_umem) {
 		AF_XDP_LOG_LINE(ERR, "Shared UMEM feature not available. "
 				"Check kernel and libbpf version");
 		goto err_free_internals;
 	}
 #endif
-	internals->shared_umem = shared_umem;
-	internals->force_copy = force_copy;
-	internals->use_cni = use_cni;
-	internals->use_pinned_map = use_pinned_map;
-	internals->mode_flag = XDP_FLAGS_UPDATE_IF_NOEXIST | xdp_mode;
-	strlcpy(internals->dp_path, dp_path, PATH_MAX);
-	internals->rx_timestamp_offset = rx_timestamp_offset;
-	internals->rx_timestamp_valid_offset = rx_timestamp_valid_offset;
-	internals->rx_timestamp_valid_mask = (uint8_t)rx_timestamp_valid_mask;
-
-	if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
+	internals->shared_umem = devargs->shared_umem;
+	internals->force_copy = devargs->force_copy;
+	internals->use_cni = devargs->use_cni;
+	internals->use_pinned_map = devargs->use_pinned_map;
+	internals->mode_flag = XDP_FLAGS_UPDATE_IF_NOEXIST | devargs->xdp_mode;
+	strlcpy(internals->dp_path, devargs->dp_path, PATH_MAX);
+	internals->rx_timestamp_offset = devargs->rx_timestamp_offset;
+	internals->rx_timestamp_valid_offset = devargs->rx_timestamp_valid_offset;
+	internals->rx_timestamp_valid_mask = devargs->rx_timestamp_valid_mask;
+
+	if (xdp_get_channels_info(internals->if_name, &internals->max_queue_cnt,
 				  &internals->configured_queue_cnt)) {
 		AF_XDP_LOG_LINE(ERR, "Failed to get channel info of interface: %s",
-				if_name);
+				internals->if_name);
 		goto err_free_internals;
 	}
 
 	if (queue_cnt > internals->configured_queue_cnt) {
-		AF_XDP_LOG_LINE(ERR, "Specified queue count %d is larger than configured queue count %d.",
+		AF_XDP_LOG_LINE(ERR, "Specified queue count %u is larger than configured queue count %u.",
 				queue_cnt, internals->configured_queue_cnt);
 		goto err_free_internals;
 	}
@@ -2637,12 +2649,12 @@ init_internals(struct rte_vdev_device *dev, const char *if_name,
 	for (i = 0; i < queue_cnt; i++) {
 		internals->tx_queues[i].pair = &internals->rx_queues[i];
 		internals->rx_queues[i].pair = &internals->tx_queues[i];
-		internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
-		internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
-		internals->rx_queues[i].busy_budget = busy_budget;
+		internals->rx_queues[i].xsk_queue_idx = internals->start_queue_idx + i;
+		internals->tx_queues[i].xsk_queue_idx = internals->start_queue_idx + i;
+		internals->rx_queues[i].busy_budget = devargs->busy_budget;
 	}
 
-	ret = get_iface_info(if_name, &internals->eth_addr,
+	ret = get_iface_info(internals->if_name, &internals->eth_addr,
 			     &internals->if_index);
 	if (ret)
 		goto err_free_tx;
@@ -2828,23 +2840,17 @@ get_dflt_dp_path(char *dp_path, size_t size, const char *if_name,
 static int
 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 {
-	struct rte_kvargs *kvlist;
-	char if_name[IFNAMSIZ] = {'\0'};
-	int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
-	int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
-	int shared_umem = 0;
-	char prog_path[PATH_MAX] = {'\0'};
-	int busy_budget = -1, ret;
-	int force_copy = 0;
-	int use_cni = 0;
-	int use_pinned_map = 0;
-	uint32_t xdp_mode = 0;
-	char dp_path[PATH_MAX] = {'\0'};
-	int rx_timestamp_offset = -1;
-	int rx_timestamp_valid_offset = -1;
-	int rx_timestamp_valid_mask = 0;
+	struct af_xdp_devargs devargs = {
+		.start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX,
+		.queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT,
+		.busy_budget = -1,
+		.rx_timestamp_offset = -1,
+		.rx_timestamp_valid_offset = -1,
+	};
 	struct rte_eth_dev *eth_dev = NULL;
 	const char *name = rte_vdev_device_name(dev);
+	struct rte_kvargs *kvlist;
+	int ret;
 
 	AF_XDP_LOG_LINE(INFO, "Initializing pmd_af_xdp for %s", name);
 
@@ -2885,97 +2891,98 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 		return -EINVAL;
 	}
 
-	if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
-			     &xsk_queue_cnt, &shared_umem, prog_path,
-			     &busy_budget, &force_copy, &use_cni, &use_pinned_map,
-			     dp_path, &xdp_mode, &rx_timestamp_offset,
-			     &rx_timestamp_valid_offset, &rx_timestamp_valid_mask) < 0) {
+	ret = parse_parameters(kvlist, &devargs);
+	rte_kvargs_free(kvlist);
+	if (ret < 0) {
 		AF_XDP_LOG_LINE(ERR, "Invalid kvargs value");
 		return -EINVAL;
 	}
 
-	if (strlen(if_name) == 0) {
+	if (strlen(devargs.if_name) == 0) {
 		AF_XDP_LOG_LINE(ERR, "Network interface must be specified");
 		return -EINVAL;
 	}
 
-	if (rx_timestamp_offset >= 0) {
-		if (rx_timestamp_offset < (int)sizeof(uint64_t) ||
-		    rx_timestamp_offset > XDP_PACKET_HEADROOM) {
-			AF_XDP_LOG_LINE(ERR,
-				"Timestamp offset must be between %zu and %u bytes",
-				sizeof(uint64_t), XDP_PACKET_HEADROOM);
-			return -EINVAL;
-		}
+	/* The offsets are already bounded by XDP_PACKET_HEADROOM when parsed. */
+	if (devargs.rx_timestamp_offset >= 0 &&
+	    devargs.rx_timestamp_offset < (int)sizeof(uint64_t)) {
+		AF_XDP_LOG_LINE(ERR,
+			"Timestamp offset must be between %zu and %u bytes",
+			sizeof(uint64_t), XDP_PACKET_HEADROOM);
+		return -EINVAL;
 	}
 
-	if (rx_timestamp_valid_offset >= 0) {
-		if (rx_timestamp_offset < 0) {
+	if (devargs.rx_timestamp_valid_offset >= 0) {
+		if (devargs.rx_timestamp_offset < 0) {
 			AF_XDP_LOG_LINE(ERR,
 				"Timestamp offset must be configured when validity offset is configured");
 			return -EINVAL;
 		}
-		if (rx_timestamp_valid_offset < 1 ||
-		    rx_timestamp_valid_offset > XDP_PACKET_HEADROOM) {
+		if (devargs.rx_timestamp_valid_offset < 1) {
 			AF_XDP_LOG_LINE(ERR,
 				"Validity hint offset must be between 1 and %u bytes",
 				XDP_PACKET_HEADROOM);
 			return -EINVAL;
 		}
-		if (rx_timestamp_valid_offset > rx_timestamp_offset - (int)sizeof(uint64_t) &&
-		    rx_timestamp_valid_offset <= rx_timestamp_offset) {
+		if (devargs.rx_timestamp_valid_offset >
+				devargs.rx_timestamp_offset - (int)sizeof(uint64_t) &&
+		    devargs.rx_timestamp_valid_offset <= devargs.rx_timestamp_offset) {
 			AF_XDP_LOG_LINE(ERR,
 				"Validity hint offset %d overlaps with 8-byte timestamp at offset %d",
-				rx_timestamp_valid_offset, rx_timestamp_offset);
+				devargs.rx_timestamp_valid_offset,
+				devargs.rx_timestamp_offset);
 			return -EINVAL;
 		}
-		if (rx_timestamp_valid_mask == 0) {
+		if (devargs.rx_timestamp_valid_mask == 0) {
 			AF_XDP_LOG_LINE(ERR,
 				"Validity mask cannot be zero when validity offset is configured");
 			return -EINVAL;
 		}
 	}
 
-	if (use_cni && use_pinned_map) {
+	if (devargs.use_cni && devargs.use_pinned_map) {
 		AF_XDP_LOG_LINE(ERR, "When '%s' parameter is used, '%s' parameter is not valid",
 			ETH_AF_XDP_USE_CNI_ARG, ETH_AF_XDP_USE_PINNED_MAP_ARG);
 		return -EINVAL;
 	}
 
-	if ((use_cni || use_pinned_map) && busy_budget > 0) {
+	if ((devargs.use_cni || devargs.use_pinned_map) && devargs.busy_budget > 0) {
 		AF_XDP_LOG_LINE(ERR, "When '%s' or '%s' parameter is used, '%s' parameter is not valid",
 			ETH_AF_XDP_USE_CNI_ARG, ETH_AF_XDP_USE_PINNED_MAP_ARG,
 			ETH_AF_XDP_BUDGET_ARG);
 		return -EINVAL;
 	}
 
-	if ((use_cni || use_pinned_map) && strnlen(prog_path, PATH_MAX)) {
+	if ((devargs.use_cni || devargs.use_pinned_map) &&
+	    strnlen(devargs.prog_path, PATH_MAX)) {
 		AF_XDP_LOG_LINE(ERR, "When '%s' or '%s' parameter is used, '%s' parameter is not valid",
 			ETH_AF_XDP_USE_CNI_ARG, ETH_AF_XDP_USE_PINNED_MAP_ARG,
 			ETH_AF_XDP_PROG_ARG);
 		return -EINVAL;
 	}
 
-	if (use_cni && !strnlen(dp_path, PATH_MAX)) {
+	if (devargs.use_cni && !strnlen(devargs.dp_path, PATH_MAX)) {
 		/* The UDS path is bounded by sun_path, not by PATH_MAX. */
-		ret = get_dflt_dp_path(dp_path,
+		ret = get_dflt_dp_path(devargs.dp_path,
 				RTE_SIZEOF_FIELD(struct sockaddr_un, sun_path),
-				if_name, DP_UDS_SOCK);
+				devargs.if_name, DP_UDS_SOCK);
 		if (ret < 0)
 			return ret;
 		AF_XDP_LOG_LINE(INFO, "'%s' parameter not provided, setting value to '%s'",
-			ETH_AF_XDP_DP_PATH_ARG, dp_path);
+			ETH_AF_XDP_DP_PATH_ARG, devargs.dp_path);
 	}
 
-	if (use_pinned_map && !strnlen(dp_path, PATH_MAX)) {
-		ret = get_dflt_dp_path(dp_path, sizeof(dp_path), if_name, DP_XSK_MAP);
+	if (devargs.use_pinned_map && !strnlen(devargs.dp_path, PATH_MAX)) {
+		ret = get_dflt_dp_path(devargs.dp_path, sizeof(devargs.dp_path),
+				devargs.if_name, DP_XSK_MAP);
 		if (ret < 0)
 			return ret;
 		AF_XDP_LOG_LINE(INFO, "'%s' parameter not provided, setting value to '%s'",
-			ETH_AF_XDP_DP_PATH_ARG, dp_path);
+			ETH_AF_XDP_DP_PATH_ARG, devargs.dp_path);
 	}
 
-	if ((!use_cni && !use_pinned_map) && strnlen(dp_path, PATH_MAX)) {
+	if ((!devargs.use_cni && !devargs.use_pinned_map) &&
+	    strnlen(devargs.dp_path, PATH_MAX)) {
 		AF_XDP_LOG_LINE(ERR, "'%s' parameter is set, but '%s' or '%s' were not enabled",
 			ETH_AF_XDP_DP_PATH_ARG, ETH_AF_XDP_USE_CNI_ARG,
 			ETH_AF_XDP_USE_PINNED_MAP_ARG);
@@ -2986,10 +2993,10 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	 * The socket address is copied into sun_path which is much shorter than
 	 * PATH_MAX, reject an oversized path instead of silently truncating it.
 	 */
-	if (use_cni && strnlen(dp_path, PATH_MAX) >=
+	if (devargs.use_cni && strnlen(devargs.dp_path, PATH_MAX) >=
 		       RTE_SIZEOF_FIELD(struct sockaddr_un, sun_path)) {
 		AF_XDP_LOG_LINE(ERR, "'%s' value '%s' is too long for a unix socket address",
-				ETH_AF_XDP_DP_PATH_ARG, dp_path);
+				ETH_AF_XDP_DP_PATH_ARG, devargs.dp_path);
 		return -ENAMETOOLONG;
 	}
 
@@ -2999,21 +3006,17 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 		char numa_path[PATH_MAX];
 
 		snprintf(numa_path, sizeof(numa_path), "/sys/class/net/%s/device/numa_node",
-			 if_name);
+			 devargs.if_name);
 		if (access(numa_path, R_OK) != 0 || eal_parse_sysfs_value(numa_path, &numa) != 0)
 			dev->device.numa_node = rte_socket_id();
 		else
 			dev->device.numa_node = numa;
 	}
 
-	busy_budget = busy_budget == -1 ? ETH_AF_XDP_DFLT_BUSY_BUDGET :
-					busy_budget;
+	if (devargs.busy_budget == -1)
+		devargs.busy_budget = ETH_AF_XDP_DFLT_BUSY_BUDGET;
 
-	eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
-				 xsk_queue_cnt, shared_umem, prog_path,
-				 busy_budget, force_copy, use_cni, use_pinned_map,
-				 dp_path, xdp_mode, rx_timestamp_offset,
-				 rx_timestamp_valid_offset, rx_timestamp_valid_mask);
+	eth_dev = init_internals(dev, &devargs);
 	if (eth_dev == NULL) {
 		AF_XDP_LOG_LINE(ERR, "Failed to init internals");
 		return -1;
Signature · 1 line
2.53.0
#151 lore raw

PATCH v3 18/65 net/ark: use kvargs numeric helpers

In reply to Stephen Hemminger

The packet director argument is stored in a uint32_t but converted with strtol() with no validation, so a malformed value is silently taken as zero and a negative one wraps around. It is also converted through a signed long, so on a 32-bit build a value above INT32_MAX saturates at LONG_MAX rather than being stored.

It is documented as "Pkt_dir=0x00110F10" and read as base 16, so use rte_kvargs_handle_hex32(), which keeps hexadecimal with or without the 0x prefix and validates the whole string. Both the documented form and a bare "00110F10" are unchanged.

---
 drivers/net/ark/ark_ethdev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ark/ark_ethdev.c +3 -1
index d6b1abf383..c6784b64a3 100644
@@ -896,7 +896,9 @@ process_pktdir_arg(const char *key, const char *value,
 	struct ark_adapter *ark =
 		(struct ark_adapter *)extra_args;
 
-	ark->pkt_dir_v = strtol(value, NULL, 16);
+	if (rte_kvargs_handle_hex32(key, value, &ark->pkt_dir_v) < 0)
+		return -EINVAL;
+
 	ARK_PMD_LOG(DEBUG, "pkt_dir_v = 0x%x\n", ark->pkt_dir_v);
 	return 0;
 }
Signature · 1 line
2.53.0
#152 lore raw

PATCH v3 19/65 net/failsafe: use kvargs numeric helpers

In reply to Stephen Hemminger

fs_get_u64_arg() returns -errno on failure, but errno is not reset before strtoull(), so an unrelated earlier failure is reported. It also accepts a value with trailing garbage, and base 0 means a leading zero selects octal.

---
 drivers/net/failsafe/failsafe_args.c | 20 +-------------------
 1 file changed, 1 insertion(+), 19 deletions(-)
drivers/net/failsafe/failsafe_args.c +1 -19
index 1b8f1d3050..f256a9f6a0 100644
@@ -345,24 +345,6 @@ fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
 	return 0;
 }
 
-static int
-fs_get_u64_arg(const char *key __rte_unused,
-		const char *value, void *out)
-{
-	uint64_t *u64 = out;
-	char *endptr = NULL;
-
-	if ((value == NULL) || (out == NULL))
-		return -EINVAL;
-	errno = 0;
-	*u64 = strtoull(value, &endptr, 0);
-	if (errno != 0)
-		return -errno;
-	if (endptr == value)
-		return -1;
-	return 0;
-}
-
 static int
 fs_get_mac_addr_arg(const char *key __rte_unused,
 		const char *value, void *out)
@@ -416,7 +398,7 @@ failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
 		if (arg_count == 1) {
 			ret = rte_kvargs_process(kvlist,
 					PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
-					&fs_get_u64_arg, &failsafe_hotplug_poll);
+					rte_kvargs_handle_u64, &failsafe_hotplug_poll);
 			if (ret < 0)
 				goto free_kvlist;
 		}
Signature · 1 line
2.53.0
#153 lore raw

PATCH v3 20/65 net/virtio: use kvargs numeric helpers

In reply to Stephen Hemminger

get_integer_arg() checks only errno, so "queues=foo" was silently taken as zero, and since the store is skipped on failure but zero is returned, the argument was accepted while leaving the default in place. It also uses base 0, so a leading zero selects octal.

link_speed_handler() checks neither errno nor the end pointer before validating the result against the speed capabilities.

The boolean arguments become bool and use rte_kvargs_handle_bool():
server, cq, packed_vq, vectorized, mrg_rxbuf and in_order for virtio_user, and vectorized for virtio. They are passed by value into virtio_user_dev_init(), whose parameters stay int.

vectorized_check_handler() treated anything other than "1" as false, so "vectorized=y" silently disabled it and a typo did the same. It is now a boolean like the rest: the usual spellings work, a bare key enables the option, and anything else is rejected instead of being read as false.

---
 drivers/net/virtio/virtio_ethdev.c      | 36 ++++++---------
 drivers/net/virtio/virtio_user_ethdev.c | 59 +++++++++----------------
 2 files changed, 34 insertions(+), 61 deletions(-)
drivers/net/virtio/virtio_ethdev.c +13 -23
index a6fe4f3023..556e4bfede 100644
@@ -47,7 +47,7 @@ static int virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
 static uint32_t virtio_dev_speed_capa_get(uint32_t speed);
 static int virtio_dev_devargs_parse(struct rte_devargs *devargs,
 	uint32_t *speed,
-	int *vectorized);
+	bool *vectorized);
 static int virtio_dev_info_get(struct rte_eth_dev *dev,
 				struct rte_eth_dev_info *dev_info);
 static int virtio_dev_link_update(struct rte_eth_dev *dev,
@@ -2029,7 +2029,7 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 {
 	struct virtio_hw *hw = eth_dev->data->dev_private;
 	uint32_t speed = RTE_ETH_SPEED_NUM_UNKNOWN;
-	int vectorized = 0;
+	bool vectorized = false;
 	int ret;
 
 	if (sizeof(struct virtio_net_hdr_hash_report) > RTE_PKTMBUF_HEADROOM) {
@@ -2128,20 +2128,6 @@ virtio_dev_speed_capa_get(uint32_t speed)
 	}
 }
 
-static int vectorized_check_handler(__rte_unused const char *key,
-		const char *value, void *ret_val)
-{
-	if (value == NULL || ret_val == NULL)
-		return -EINVAL;
-
-	if (strcmp(value, "1") == 0)
-		*(int *)ret_val = 1;
-	else
-		*(int *)ret_val = 0;
-
-	return 0;
-}
-
 #define VIRTIO_ARG_SPEED      "speed"
 #define VIRTIO_ARG_VECTORIZED "vectorized"
 
@@ -2149,21 +2135,25 @@ static int
 link_speed_handler(const char *key __rte_unused,
 		const char *value, void *ret_val)
 {
-	uint32_t val;
-	if (!value || !ret_val)
+	uint64_t val;
+
+	if (ret_val == NULL)
+		return -EINVAL;
+
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &val) < 0)
 		return -EINVAL;
-	val = strtoul(value, NULL, 0);
+
 	/* validate input */
 	if (virtio_dev_speed_capa_get(val) == 0)
 		return -EINVAL;
-	*(uint32_t *)ret_val = val;
 
+	*(uint32_t *)ret_val = val;
 	return 0;
 }
 
 
 static int
-virtio_dev_devargs_parse(struct rte_devargs *devargs, uint32_t *speed, int *vectorized)
+virtio_dev_devargs_parse(struct rte_devargs *devargs, uint32_t *speed, bool *vectorized)
 {
 	struct rte_kvargs *kvlist;
 	int ret = 0;
@@ -2190,9 +2180,9 @@ virtio_dev_devargs_parse(struct rte_devargs *devargs, uint32_t *speed, int *vect
 
 	if (vectorized &&
 		rte_kvargs_count(kvlist, VIRTIO_ARG_VECTORIZED) == 1) {
-		ret = rte_kvargs_process(kvlist,
+		ret = rte_kvargs_process_opt(kvlist,
 				VIRTIO_ARG_VECTORIZED,
-				vectorized_check_handler, vectorized);
+				rte_kvargs_handle_bool, vectorized);
 		if (ret < 0) {
 			PMD_INIT_LOG(ERR, "Failed to parse %s",
 					VIRTIO_ARG_VECTORIZED);
drivers/net/virtio/virtio_user_ethdev.c +21 -38

126 lines not on this page · raw message

Signature · 1 line
2.53.0
#154 lore raw

PATCH v3 21/65 net/bonding: use kvargs numeric helpers

In reply to Stephen Hemminger

The mode, socket id and time_ms parsers all assign the strtol() result before checking the end pointer and errno, and the mode parser truncates into a uint8_t so "mode=257" aliases onto mode 1.

parse_port_id() has the same unchecked pattern, and did not bound the result before using it, relying on rte_eth_dev_is_valid_port() to catch out of range values.

The socket id parser cast to int8_t when testing for SOCKET_ID_ANY, so any value whose low byte is 0xff, such as 255 or -257, was accepted as SOCKET_ID_ANY.

Drop the stray errno reset in the agg mode parser, which only does string comparisons.

---
 drivers/net/bonding/eth_bond_private.h  |  8 ---
 drivers/net/bonding/rte_eth_bond_args.c | 71 ++++---------------------
 drivers/net/bonding/rte_eth_bond_pmd.c  |  8 +--
 3 files changed, 14 insertions(+), 73 deletions(-)
drivers/net/bonding/eth_bond_private.h +0 -8
index 378bbba4e6..9a2fb7167e 100644
@@ -298,10 +298,6 @@ int
 bond_ethdev_parse_member_agg_mode_kvarg(const char *key __rte_unused,
 		const char *value, void *extra_args);
 
-int
-bond_ethdev_parse_socket_id_kvarg(const char *key,
-		const char *value, void *extra_args);
-
 int
 bond_ethdev_parse_primary_member_port_id_kvarg(const char *key,
 		const char *value, void *extra_args);
@@ -314,10 +310,6 @@ int
 bond_ethdev_parse_bond_mac_addr_kvarg(const char *key,
 		const char *value, void *extra_args);
 
-int
-bond_ethdev_parse_time_ms_kvarg(const char *key,
-		const char *value, void *extra_args);
-
 void
 bond_tlb_disable(struct bond_dev_private *internals);
 
drivers/net/bonding/rte_eth_bond_args.c +10 -61

106 lines not on this page · raw message

drivers/net/bonding/rte_eth_bond_pmd.c +4 -4
index 6a4f997b5a..766dfd9459 100644
@@ -3869,7 +3869,7 @@ bond_probe(struct rte_vdev_device *dev)
 	arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
 	if (arg_count == 1) {
 		if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
-				&bond_ethdev_parse_socket_id_kvarg, &socket_id)
+				rte_kvargs_handle_socket_id, &socket_id)
 				!= 0) {
 			RTE_BOND_LOG(ERR, "Invalid socket Id specified for "
 					"bonding device %s", name);
@@ -4207,7 +4207,7 @@ bond_ethdev_configure(struct rte_eth_dev *dev)
 
 		if (rte_kvargs_process(kvlist,
 				       PMD_BOND_LSC_POLL_PERIOD_KVARG,
-				       &bond_ethdev_parse_time_ms_kvarg,
+				       rte_kvargs_handle_u32,
 				       &lsc_poll_interval_ms) < 0) {
 			RTE_BOND_LOG(INFO,
 				     "Invalid lsc polling interval value specified for bonding"
@@ -4236,7 +4236,7 @@ bond_ethdev_configure(struct rte_eth_dev *dev)
 
 		if (rte_kvargs_process(kvlist,
 				       PMD_BOND_LINK_UP_PROP_DELAY_KVARG,
-				       &bond_ethdev_parse_time_ms_kvarg,
+				       rte_kvargs_handle_u32,
 				       &link_up_delay_ms) < 0) {
 			RTE_BOND_LOG(INFO,
 				     "Invalid link up propagation delay value specified for"
@@ -4266,7 +4266,7 @@ bond_ethdev_configure(struct rte_eth_dev *dev)
 
 		if (rte_kvargs_process(kvlist,
 				       PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG,
-				       &bond_ethdev_parse_time_ms_kvarg,
+				       rte_kvargs_handle_u32,
 				       &link_down_delay_ms) < 0) {
 			RTE_BOND_LOG(INFO,
 				     "Invalid link down propagation delay value specified for"
Signature · 1 line
2.53.0
#155 lore raw

PATCH v3 22/65 net/ena: use kvargs numeric helpers

In reply to Stephen Hemminger

ena_process_uint_devarg() only checks that strtoull() consumed at least one character, so trailing garbage such as "8junk" is accepted, and a negative value is silently wrapped to a huge unsigned value.

ena_process_llq_policy_devarg() ignores the end pointer entirely, so any unparsable value becomes zero and disables the LLQ policy rather than being rejected.

ena_process_bool_devarg() only served enable_frag_bypass, and did nothing beyond accepting "0" or "1" and storing into a bool, so drop it and point rte_kvargs_handle_bool() at the field. A bare "enable_frag_bypass" now enables it, along with the usual spellings.

---
 drivers/net/ena/ena_ethdev.c | 45 +++++-------------------------------
 1 file changed, 6 insertions(+), 39 deletions(-)
drivers/net/ena/ena_ethdev.c +6 -39
index ae48afcfdc..732f5f6f34 100644
@@ -48,8 +48,6 @@
 
 #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
 
-#define DECIMAL_BASE 10
-
 #define MAX_WIDE_LLQ_DEPTH_UNSUPPORTED 0
 
 #define ENA_TS_OFFSET_UNSET -1
@@ -321,9 +319,6 @@ static int ena_xstats_get_by_id(struct rte_eth_dev *dev,
 static int ena_process_llq_policy_devarg(const char *key,
 			const char *value,
 			void *opaque);
-static int ena_process_bool_devarg(const char *key,
-				   const char *value,
-				   void *opaque);
 static int ena_parse_devargs(struct ena_adapter *adapter,
 			     struct rte_devargs *devargs);
 static void ena_copy_customer_metrics(struct ena_adapter *adapter,
@@ -3832,11 +3827,9 @@ static int ena_process_uint_devarg(const char *key,
 				  void *opaque)
 {
 	struct ena_adapter *adapter = opaque;
-	char *str_end;
 	uint64_t uint64_value;
 
-	uint64_value = strtoull(value, &str_end, DECIMAL_BASE);
-	if (value == str_end) {
+	if (rte_kvargs_to_uint(value, 0, UINT64_MAX, &uint64_value) < 0) {
 		PMD_INIT_LOG_LINE(ERR,
 			"Invalid value for key '%s'. Only uint values are accepted.",
 			key);
@@ -3881,17 +3874,15 @@ static int ena_process_uint_devarg(const char *key,
 static int ena_process_llq_policy_devarg(const char *key, const char *value, void *opaque)
 {
 	struct ena_adapter *adapter = opaque;
-	uint32_t policy;
+	uint64_t policy;
 
-	policy = strtoul(value, NULL, DECIMAL_BASE);
-	if (policy < ENA_LLQ_POLICY_LAST) {
-		adapter->llq_header_policy = policy;
-	} else {
+	if (rte_kvargs_to_uint(value, 0, ENA_LLQ_POLICY_LAST - 1, &policy) < 0) {
 		PMD_INIT_LOG_LINE(ERR,
 			"Invalid value: '%s' for key '%s'. valid [0-3]",
 			value, key);
 		return -EINVAL;
 	}
+	adapter->llq_header_policy = policy;
 	PMD_INIT_LOG_LINE(INFO,
 		"LLQ policy is %u [0 - disabled, 1 - device recommended, 2 - normal, 3 - large]",
 		adapter->llq_header_policy);
@@ -3899,30 +3890,6 @@ static int ena_process_llq_policy_devarg(const char *key, const char *value, voi
 	return 0;
 }
 
-static int ena_process_bool_devarg(const char *key, const char *value, void *opaque)
-{
-	struct ena_adapter *adapter = opaque;
-	bool bool_value;
-
-	/* Parse the value. */
-	if (strcmp(value, "1") == 0) {
-		bool_value = true;
-	} else if (strcmp(value, "0") == 0) {
-		bool_value = false;
-	} else {
-		PMD_INIT_LOG_LINE(ERR,
-			"Invalid value: '%s' for key '%s'. Accepted: '0' or '1'",
-			value, key);
-		return -EINVAL;
-	}
-
-	/* Now, assign it to the proper adapter field. */
-	if (strcmp(key, ENA_DEVARG_ENABLE_FRAG_BYPASS) == 0)
-		adapter->enable_frag_bypass = bool_value;
-
-	return 0;
-}
-
 static int ena_parse_devargs(struct ena_adapter *adapter, struct rte_devargs *devargs)
 {
 	static const char * const allowed_args[] = {
@@ -3956,8 +3923,8 @@ static int ena_parse_devargs(struct ena_adapter *adapter, struct rte_devargs *de
 		ena_process_uint_devarg, adapter);
 	if (rc != 0)
 		goto exit;
-	rc = rte_kvargs_process(kvlist, ENA_DEVARG_ENABLE_FRAG_BYPASS,
-		ena_process_bool_devarg, adapter);
+	rc = rte_kvargs_process_opt(kvlist, ENA_DEVARG_ENABLE_FRAG_BYPASS,
+		rte_kvargs_handle_bool, &adapter->enable_frag_bypass);
 	if (rc != 0)
 		goto exit;
 
Signature · 1 line
2.53.0
#156 lore raw

PATCH v3 23/65 net/netvsc: use kvargs numeric helpers

In reply to Stephen Hemminger

hn_set_parameter() does not check errno, so an out of range value is accepted as ULONG_MAX, and the result is stored into 32-bit fields without any range check.

This also drops support for octal, which base 0 accepted; hexadecimal still works.

---
 drivers/net/netvsc/hn_ethdev.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/net/netvsc/hn_ethdev.c +2 -4
index e4fdf89873..fef532b605 100644
@@ -202,11 +202,9 @@ eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
 static int hn_set_parameter(const char *key, const char *value, void *opaque)
 {
 	struct hn_data *hv = opaque;
-	char *endp = NULL;
-	unsigned long v;
+	uint64_t v;
 
-	v = strtoul(value, &endp, 0);
-	if (*value == '\0' || *endp != '\0') {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &v) < 0) {
 		PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
 		return -EINVAL;
 	}
Signature · 1 line
2.53.0
#157 lore raw

PATCH v3 24/65 net/ice: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_u32() open codes the errno, end pointer and range checks that the helper does.

parse_tx_sched_levels() assigns before validating and uses base 0, so a leading zero silently selects octal.

handle_field_offs_arg() converts with atoi(), which reports no error, and stores the result through a uint8_t pointer while the caller passes &xtr_field_offs, which is an int. Only one byte of a four byte field was written. Convert with rte_kvargs_to_int() and store through an int pointer instead. The range starts at zero, which keeps the old isdigit() rejection of a negative value: -1 is used internally to mean that no extraction field is configured.

The debug_mask parser is converted separately, later in this series, since it needs the hexadecimal handler rather than a decimal one.

parse_bool() stored an int through the pointer it was given, but three of its five callers pass the address of a uint8_t field in struct
ice_devargs: default_mac_disable, ddp_load_sched and source_prune. Each of those parsed four bytes over a one byte field and corrupted whatever followed it. default_mac_disable is the worst of the three, since it is immediately followed by proto_xtr[ICE_MAX_QUEUE_NUM], which ice_parse_devargs() has just initialized when the argument is parsed.

All five arguments are booleans, so make the fields bool and use rte_kvargs_handle_bool(), which removes the mismatch along with the hand written parser. A bare key such as "safe-mode-support" now enables the option, and the usual spellings are accepted.

Bugzilla ID: 2037
Bugzilla ID: 2038

Fixes 66214b547c65 ("net/ice: add option to disable default MAC")
Fixes df539aaf35cb ("net/ice: refactor flex descriptor protocol field extraction")
---
 drivers/net/intel/ice/ice_ethdev.c | 102 +++++++++--------------------
 drivers/net/intel/ice/ice_ethdev.h |  10 +--
 2 files changed, 36 insertions(+), 76 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +31 -71
index dfee07c457..4c3db27812 100644
@@ -8,6 +8,7 @@
 
 #include <ctype.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -15,6 +16,7 @@
 #include <math.h>
 
 #include <rte_tailq.h>
+#include <rte_kvargs.h>
 #include <rte_os_shim.h>
 
 #include "eal_firmware.h"
@@ -738,18 +740,20 @@ handle_proto_xtr_arg(__rte_unused const char *key, const char *value,
 }
 
 static int
-handle_field_offs_arg(__rte_unused const char *key, const char *value,
-				void *offs_args)
+handle_field_offs_arg(const char *key, const char *value, void *offs_args)
 {
-	uint8_t *offset = offs_args;
+	int64_t offset;
 
-	if (value == NULL || offs_args == NULL)
+	if (offs_args == NULL)
 		return -EINVAL;
 
-	if (!isdigit(*value))
-		return -1;
+	/* A negative offset is reserved: -1 means no field is configured. */
+	if (rte_kvargs_to_int(value, 0, INT_MAX, &offset) < 0) {
+		PMD_DRV_LOG(ERR, "Invalid %s, must be a positive offset", key);
+		return -EINVAL;
+	}
 
-	*offset = atoi(value);
+	*(int *)offs_args = offset;
 
 	return 0;
 }
@@ -1089,7 +1093,7 @@ ice_init_mac_address(struct rte_eth_dev *dev)
 		return -ENOMEM;
 	}
 	/* store it to dev data */
-	if (ad->devargs.default_mac_disable != 1)
+	if (!ad->devargs.default_mac_disable)
 		rte_ether_addr_copy((struct rte_ether_addr *)hw->port_info[0].mac.perm_addr,
 			&dev->data->mac_addrs[0]);
 	return 0;
@@ -1119,7 +1123,7 @@ ice_add_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
 	struct ice_adapter *ad = (struct ice_adapter *)hw->back;
 	int ret = 0;
 
-	if (ad->devargs.default_mac_disable == 1 && rte_is_same_ether_addr(mac_addr,
+	if (ad->devargs.default_mac_disable && rte_is_same_ether_addr(mac_addr,
 			(struct rte_ether_addr *)hw->port_info[0].mac.perm_addr)) {
 		PMD_DRV_LOG(ERR, "This Default MAC filter is disabled.");
 		return 0;
@@ -1769,7 +1773,7 @@ ice_setup_vsi(struct ice_pf *pf, enum ice_vsi_type type)
 		 */
 		vsi_ctx.info.sw_id = hw->port_info->sw_id;
 		/* Source Prune */
-		if (ad->devargs.source_prune != 1) {
+		if (!ad->devargs.source_prune) {
 			/* Disable source prune to support VRRP
 			 * when source-prune devarg is not set
 			 */
@@ -2140,25 +2144,6 @@ ice_base_queue_get(struct ice_pf *pf)
 	}
 }
 
-static int
-parse_bool(const char *key, const char *value, void *args)
-{
-	int *i = args;
-
-	if (value == NULL || value[0] == '\0') {
-		PMD_DRV_LOG(WARNING, "key:\"%s\", requires a value, which must be 0 or 1", key);
-		return -1;
-	}
-	if (value[1] != '\0' || (value[0] != '0' && value[0] != '1')) {
-		PMD_DRV_LOG(WARNING, "invalid value:\"%s\" for key:\"%s\", value must be 0 or 1",
-			value, key);
-		return -1;
-	}
-
-	*i = (value[0] == '1');
-	return 0;
-}
-
 static int
 parse_u64(const char *key, const char *value, void *args)
 {
@@ -2178,45 +2163,19 @@ parse_u64(const char *key, const char *value, void *args)
 	return 0;
 }
 
-static int
-parse_u32(const char *key, const char *value, void *args)
-{
-	uint32_t *num = args;
-	unsigned long tmp;
-	char *endptr;
-
-	errno = 0;
-	tmp = strtoul(value, &endptr, 0);
-	if (errno != 0 || endptr == value || *endptr != '\0') {
-		PMD_DRV_LOG(WARNING, "%s: \"%s\" is not a valid u32", key, value);
-		return -1;
-	}
-	if (tmp > UINT32_MAX) {
-		PMD_DRV_LOG(WARNING, "%s: value \"%s\" is out of range", key, value);
-		return -1;
-	}
-
-	*num = (uint32_t)tmp;
-
-	return 0;
-}
-
 static int
 parse_tx_sched_levels(const char *key, const char *value, void *args)
 {
 	uint8_t *num = args;
-	long tmp;
-	char *endptr;
+	uint64_t tmp;
 
-	errno = 0;
-	tmp = strtol(value, &endptr, 0);
 	/* the value needs two stage validation, since the actual number of available
 	 * levels is not known at this point. Initially just validate that it is in
 	 * the correct range, between 3 and 8. Later validation will check that the
 	 * available layers on a particular port is higher than the value specified here.
 	 */
-	if (errno || *endptr != '\0' ||
-			tmp < (ICE_VSI_LAYER_OFFSET - 1) || tmp >= ICE_TM_MAX_LAYERS) {
+	if (rte_kvargs_to_uint(value, ICE_VSI_LAYER_OFFSET - 1,
+			       ICE_TM_MAX_LAYERS - 1, &tmp) < 0) {
 		PMD_DRV_LOG(WARNING, "%s: Invalid value \"%s\", should be in range [%d, %d]",
 			    key, value, ICE_VSI_LAYER_OFFSET - 1, ICE_TM_MAX_LAYERS - 1);
 		return -1;
@@ -2457,13 +2416,13 @@ static int ice_parse_devargs(struct rte_eth_dev *dev)
 	if (ret)
 		goto bail;
 
-	ret = rte_kvargs_process(kvlist, ICE_SAFE_MODE_SUPPORT_ARG,
-				 &parse_bool, &ad->devargs.safe_mode_support);
+	ret = rte_kvargs_process_opt(kvlist, ICE_SAFE_MODE_SUPPORT_ARG,
+				 rte_kvargs_handle_bool, &ad->devargs.safe_mode_support);
 	if (ret)
 		goto bail;
 
-	ret = rte_kvargs_process(kvlist, ICE_DEFAULT_MAC_DISABLE,
-				&parse_bool, &ad->devargs.default_mac_disable);
+	ret = rte_kvargs_process_opt(kvlist, ICE_DEFAULT_MAC_DISABLE,
+				rte_kvargs_handle_bool, &ad->devargs.default_mac_disable);
 	if (ret)
 		goto bail;
 
@@ -2482,8 +2441,8 @@ static int ice_parse_devargs(struct rte_eth_dev *dev)
 	if (ret)
 		goto bail;
 
-	ret = rte_kvargs_process(kvlist, ICE_RX_LOW_LATENCY_ARG,
-				 &parse_bool, &ad->devargs.rx_low_latency);
+	ret = rte_kvargs_process_opt(kvlist, ICE_RX_LOW_LATENCY_ARG,
+				 rte_kvargs_handle_bool, &ad->devargs.rx_low_latency);
 	if (ret)
 		goto bail;
 
@@ -2492,8 +2451,8 @@ static int ice_parse_devargs(struct rte_eth_dev *dev)
 	if (ret)
 		goto bail;
 
-	ret = rte_kvargs_process(kvlist, ICE_DDP_LOAD_SCHED_ARG,
-				 &parse_bool, &ad->devargs.ddp_load_sched);
+	ret = rte_kvargs_process_opt(kvlist, ICE_DDP_LOAD_SCHED_ARG,
+				 rte_kvargs_handle_bool, &ad->devargs.ddp_load_sched);
 	if (ret)
 		goto bail;
 
@@ -2503,12 +2462,12 @@ static int ice_parse_devargs(struct rte_eth_dev *dev)
 		goto bail;
 
 	ret = rte_kvargs_process(kvlist, ICE_RL_BURST_SIZE_ARG,
-				 &parse_u32, &ad->devargs.rl_burst_size);
+				 rte_kvargs_handle_u32, &ad->devargs.rl_burst_size);
 	if (ret)
 		goto bail;
 
-	ret = rte_kvargs_process(kvlist, ICE_SOURCE_PRUNE_ARG,
-				 &parse_bool, &ad->devargs.source_prune);
+	ret = rte_kvargs_process_opt(kvlist, ICE_SOURCE_PRUNE_ARG,
+				 rte_kvargs_handle_bool, &ad->devargs.source_prune);
 	if (ret)
 		goto bail;
 
@@ -2762,7 +2721,7 @@ ice_dev_init(struct rte_eth_dev *dev)
 	}
 
 	if (ret) {
-		if (ad->devargs.safe_mode_support == 0) {
+		if (!ad->devargs.safe_mode_support) {
 			PMD_INIT_LOG(ERR, "Failed to load the DDP package,"
 					"Use safe-mode-support=1 to enter Safe Mode");
 			goto err_init_fw;
@@ -4313,7 +4272,8 @@ __vsi_queues_bind_intr(struct ice_vsi *vsi, uint16_t msix_vect,
 {
 	struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
 	uint32_t val, val_tx;
-	int rx_low_latency, i;
+	bool rx_low_latency;
+	int i;
 
 	rx_low_latency = vsi->adapter->devargs.rx_low_latency;
 	for (i = 0; i < nb_queue; i++) {
drivers/net/intel/ice/ice_ethdev.h +5 -5
index e39757e842..39583cff38 100644
@@ -627,16 +627,16 @@ struct ice_pf {
  * Cache devargs parse result.
  */
 struct ice_devargs {
-	int rx_low_latency;
-	int safe_mode_support;
+	bool rx_low_latency;
+	bool safe_mode_support;
 	uint8_t proto_xtr_dflt;
-	uint8_t default_mac_disable;
+	bool default_mac_disable;
 	uint8_t proto_xtr[ICE_MAX_QUEUE_NUM];
 	uint8_t pin_idx;
 	uint8_t pps_out_ena;
-	uint8_t ddp_load_sched;
+	bool ddp_load_sched;
 	uint8_t tm_exposed_levels;
-	uint8_t source_prune;
+	bool source_prune;
 	uint32_t rl_burst_size;
 	int link_state_on_close;
 	int xtr_field_offs;
Signature · 1 line
2.53.0
#158 lore raw

PATCH v3 25/65 net/iavf: use kvargs numeric helpers

In reply to Stephen Hemminger

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 and enable_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.

---
 drivers/net/intel/iavf/iavf.h        |  6 +--
 drivers/net/intel/iavf/iavf_ethdev.c | 55 +++++++++-------------------
 2 files changed, 20 insertions(+), 41 deletions(-)
drivers/net/intel/iavf/iavf.h +3 -3
index eb7cf2b58d..8f31fbabbf 100644
@@ -324,10 +324,10 @@ struct iavf_devargs {
 	uint8_t proto_xtr[IAVF_MAX_QUEUE_NUM];
 	uint16_t quanta_size;
 	uint32_t watchdog_period;
-	int auto_reset;
-	int no_poll_on_link_down;
+	bool auto_reset;
+	bool no_poll_on_link_down;
 	uint64_t mbuf_check;
-	int enable_lldp;
+	bool enable_lldp;
 };
 
 struct iavf_security_ctx;
drivers/net/intel/iavf/iavf_ethdev.c +17 -38

114 lines not on this page · raw message

Signature · 1 line
2.53.0
#159 lore raw

PATCH v3 26/65 net/i40e: use kvargs numeric helpers

In reply to Stephen Hemminger

Replace the open coded errno and end pointer checks in the multi driver and VF queue number handlers.

The VF queue number handler also used base 0, so a leading zero silently selected octal. It is now parsed as a uint16_t, which is the width of the field it feeds, so a value above 65535 is rejected outright rather than warned about; anything in range still warns and returns success, so that a later valid instance of the same argument takes effect.

support-multi-driver is a boolean and the field behind it is already a bool, so drop i40e_parse_multi_drv_handler() and point rte_kvargs_handle_bool() at the field.

Note that a value other than 0 or 1 used to log a warning and carry on with the default. It now fails the probe, like every other malformed devarg in this series.

---
 drivers/net/intel/i40e/i40e_ethdev.c | 45 ++++++----------------------
 1 file changed, 9 insertions(+), 36 deletions(-)
drivers/net/intel/i40e/i40e_ethdev.c +9 -36
index defdff0378..ad826467de 100644
@@ -14,6 +14,7 @@
 #include <assert.h>
 
 #include <rte_common.h>
+#include <rte_kvargs.h>
 #include <rte_eal.h>
 #include <rte_string_fns.h>
 #include <rte_pci.h>
@@ -1267,33 +1268,6 @@ i40e_init_queue_region_conf(struct rte_eth_dev *dev)
 	memset(info, 0, sizeof(struct i40e_queue_regions));
 }
 
-static int
-i40e_parse_multi_drv_handler(__rte_unused const char *key,
-			       const char *value,
-			       void *opaque)
-{
-	struct i40e_pf *pf;
-	unsigned long support_multi_driver;
-	char *end;
-
-	pf = (struct i40e_pf *)opaque;
-
-	errno = 0;
-	support_multi_driver = strtoul(value, &end, 10);
-	if (errno != 0 || end == value || *end != 0) {
-		PMD_DRV_LOG(WARNING, "Wrong global configuration");
-		return -(EINVAL);
-	}
-
-	if (support_multi_driver == 1 || support_multi_driver == 0)
-		pf->support_multi_driver = (bool)support_multi_driver;
-	else
-		PMD_DRV_LOG(WARNING, "%s must be 1 or 0,",
-			    "enable global configuration by default."
-			    ETH_I40E_SUPPORT_MULTI_DRIVER);
-	return 0;
-}
-
 static int
 i40e_support_multi_driver(struct rte_eth_dev *dev)
 {
@@ -1322,8 +1296,9 @@ i40e_support_multi_driver(struct rte_eth_dev *dev)
 			    "the first invalid or last valid one is used !",
 			    ETH_I40E_SUPPORT_MULTI_DRIVER);
 
-	if (rte_kvargs_process(kvlist, ETH_I40E_SUPPORT_MULTI_DRIVER,
-			       i40e_parse_multi_drv_handler, pf) < 0) {
+	if (rte_kvargs_process_opt(kvlist, ETH_I40E_SUPPORT_MULTI_DRIVER,
+			       rte_kvargs_handle_bool,
+			       &pf->support_multi_driver) < 0) {
 		rte_kvargs_free(kvlist);
 		return -EINVAL;
 	}
@@ -4847,15 +4822,12 @@ static int i40e_pf_parse_vf_queue_number_handler(const char *key,
 		void *opaque)
 {
 	struct i40e_pf *pf;
-	unsigned long num;
-	char *end;
+	uint64_t num;
 
 	pf = (struct i40e_pf *)opaque;
 	RTE_SET_USED(key);
 
-	errno = 0;
-	num = strtoul(value, &end, 0);
-	if (errno != 0 || end == value || *end != 0) {
+	if (rte_kvargs_to_uint(value, 0, UINT16_MAX, &num) < 0) {
 		PMD_DRV_LOG(WARNING, "Wrong VF queue number = %s, Now it is "
 			    "kept the value = %hu", value, pf->vf_nb_qp_max);
 		return -(EINVAL);
@@ -4865,9 +4837,10 @@ static int i40e_pf_parse_vf_queue_number_handler(const char *key,
 		pf->vf_nb_qp_max = (uint16_t)num;
 	else
 		/* here return 0 to make next valid same argument work */
-		PMD_DRV_LOG(WARNING, "Wrong VF queue number = %lu, it must be "
+		PMD_DRV_LOG(WARNING, "Wrong VF queue number = %hu, it must be "
 			    "power of 2 and equal or less than 16 !, Now it is "
-			    "kept the value = %hu", num, pf->vf_nb_qp_max);
+			    "kept the value = %hu", (uint16_t)num,
+			    pf->vf_nb_qp_max);
 
 	return 0;
 }
Signature · 1 line
2.53.0
#160 lore raw

PATCH v3 27/65 net/idpf: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_bool() only tested errno for ERANGE and ignored the end pointer, so "1junk" was accepted.

It also stored through an int pointer, but both device arguments are bool fields, so parsing either wrote four bytes into a one byte object.

The handler did nothing beyond a range checked store of a boolean, so drop it and pass rte_kvargs_handle_bool() directly, which stores a bool and also accepts the usual spellings such as "on" and "true".

The booleans use rte_kvargs_process_opt(), so that a bare key with no value enables the option.

Bugzilla ID: 2042

Fixes 549343c25db8 ("net/idpf: support device initialization")
---
 drivers/net/intel/idpf/idpf_ethdev.c | 26 +++-----------------------
 1 file changed, 3 insertions(+), 23 deletions(-)
drivers/net/intel/idpf/idpf_ethdev.c +3 -23
index e9e9781fdd..b3020fcd7c 100644
@@ -3,6 +3,7 @@
  */
 
 #include <rte_atomic.h>
+#include <rte_kvargs.h>
 #include <rte_eal.h>
 #include <rte_ether.h>
 #include <rte_malloc.h>
@@ -1244,27 +1245,6 @@ parse_vport(const char *key, const char *value, void *args)
 	return 0;
 }
 
-static int
-parse_bool(const char *key, const char *value, void *args)
-{
-	bool *i = args;
-	char *end;
-	int num;
-
-	errno = 0;
-
-	num = strtoul(value, &end, 10);
-
-	if (errno == ERANGE || (num != 0 && num != 1)) {
-		PMD_INIT_LOG(ERR, "invalid value:\"%s\" for key:\"%s\", value must be 0 or 1",
-			value, key);
-		return -EINVAL;
-	}
-
-	*i = (num == 1);
-	return 0;
-}
-
 static int
 idpf_parse_devargs(struct rte_pci_device *pci_dev, struct idpf_adapter_ext *adapter,
 		   struct idpf_devargs *idpf_args)
@@ -1307,12 +1287,12 @@ idpf_parse_devargs(struct rte_pci_device *pci_dev, struct idpf_adapter_ext *adap
 	if (ret != 0)
 		goto bail;
 
-	ret = rte_kvargs_process(kvlist, IDPF_TX_SINGLE_Q, &parse_bool,
+	ret = rte_kvargs_process_opt(kvlist, IDPF_TX_SINGLE_Q, rte_kvargs_handle_bool,
 				 &adapter->base.is_tx_singleq);
 	if (ret != 0)
 		goto bail;
 
-	ret = rte_kvargs_process(kvlist, IDPF_RX_SINGLE_Q, &parse_bool,
+	ret = rte_kvargs_process_opt(kvlist, IDPF_RX_SINGLE_Q, rte_kvargs_handle_bool,
 				 &adapter->base.is_rx_singleq);
 	if (ret != 0)
 		goto bail;
Signature · 1 line
2.53.0
#161 lore raw

PATCH v3 28/65 net/cpfl: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_bool() only tested errno for ERANGE and ignored the end pointer, so "1junk" was accepted.

It also stored through an int pointer, but both device arguments are bool fields, so parsing either wrote four bytes into a one byte object.

The handler did nothing beyond a range checked store of a boolean, so drop it and pass rte_kvargs_handle_bool() directly, which stores a bool and also accepts the usual spellings such as "on" and "true".

The booleans use rte_kvargs_process_opt(), so that a bare key with no value enables the option.

Bugzilla ID: 2042

Fixes 2d823ecd671c ("net/cpfl: support device initialization")
---
 drivers/net/intel/cpfl/cpfl_ethdev.c | 26 +++-----------------------
 1 file changed, 3 insertions(+), 23 deletions(-)
drivers/net/intel/cpfl/cpfl_ethdev.c +3 -23
index 32e0f81d99..60ec5af31b 100644
@@ -3,6 +3,7 @@
  */
 
 #include <rte_atomic.h>
+#include <rte_kvargs.h>
 #include <rte_eal.h>
 #include <rte_ether.h>
 #include <rte_malloc.h>
@@ -1558,27 +1559,6 @@ parse_vport(const char *key, const char *value, void *args)
 	return 0;
 }
 
-static int
-parse_bool(const char *key, const char *value, void *args)
-{
-	bool *i = args;
-	char *end;
-	int num;
-
-	errno = 0;
-
-	num = strtoul(value, &end, 10);
-
-	if (errno == ERANGE || (num != 0 && num != 1)) {
-		PMD_INIT_LOG(ERR, "invalid value:\"%s\" for key:\"%s\", value must be 0 or 1",
-			value, key);
-		return -EINVAL;
-	}
-
-	*i = (num == 1);
-	return 0;
-}
-
 static int
 enlist(uint16_t *list, uint16_t *len_list, const uint16_t max_list, uint16_t val)
 {
@@ -1756,12 +1736,12 @@ cpfl_parse_devargs(struct rte_pci_device *pci_dev, struct cpfl_adapter_ext *adap
 	if (ret != 0)
 		goto fail;
 
-	ret = rte_kvargs_process(kvlist, CPFL_TX_SINGLE_Q, &parse_bool,
+	ret = rte_kvargs_process_opt(kvlist, CPFL_TX_SINGLE_Q, rte_kvargs_handle_bool,
 				 &adapter->base.is_tx_singleq);
 	if (ret != 0)
 		goto fail;
 
-	ret = rte_kvargs_process(kvlist, CPFL_RX_SINGLE_Q, &parse_bool,
+	ret = rte_kvargs_process_opt(kvlist, CPFL_RX_SINGLE_Q, rte_kvargs_handle_bool,
 				 &adapter->base.is_rx_singleq);
 	if (ret != 0)
 		goto fail;
Signature · 1 line
2.53.0
#162 lore raw

PATCH v3 29/65 net/ixgbe: use kvargs numeric helpers

In reply to Stephen Hemminger

devarg_handle_int() truncates the strtoul() result into a uint16_t and then infers overflow from the truncated value being USHRT_MAX, so a value such as "65536" is silently accepted as zero. It also never checked the end pointer, and read errno without clearing it first.

fiber_sdp3_no_tx_disable and pflink_fullchk are booleans, so make the fields bool and parse them with rte_kvargs_handle_bool() straight into the adapter, which also removes the temporary and the "== 1" dance. A bare key now enables the option, and the usual spellings are accepted.

---
 drivers/net/intel/ixgbe/ixgbe_ethdev.c | 40 ++++++--------------------
 drivers/net/intel/ixgbe/ixgbe_ethdev.h |  4 +--
 2 files changed, 11 insertions(+), 33 deletions(-)
drivers/net/intel/ixgbe/ixgbe_ethdev.c +9 -31
index 284df77023..e05c9a8d1f 100644
@@ -353,8 +353,6 @@ static int ixgbe_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
 static int ixgbe_filter_restore(struct rte_eth_dev *dev);
 static void ixgbe_l2_tunnel_conf(struct rte_eth_dev *dev);
 static int ixgbe_wait_for_link_up(struct ixgbe_hw *hw);
-static int devarg_handle_int(__rte_unused const char *key, const char *value,
-			     void *extra_args);
 
 /*
  * Define VF Stats MACRO for Non "cleared on read" register
@@ -989,7 +987,6 @@ ixgbe_parse_devargs(struct ixgbe_adapter *adapter,
 		      struct rte_devargs *devargs)
 {
 	struct rte_kvargs *kvlist;
-	uint16_t sdp3_no_tx_disable;
 	enum rte_eth_fdir_pballoc_type pballoc;
 	int ret = 0;
 
@@ -1000,11 +997,10 @@ ixgbe_parse_devargs(struct ixgbe_adapter *adapter,
 	if (kvlist == NULL)
 		return 0;
 
-	if (rte_kvargs_count(kvlist, IXGBE_DEVARG_FIBER_SDP3_NOT_TX_DISABLE) == 1 &&
-	    rte_kvargs_process(kvlist, IXGBE_DEVARG_FIBER_SDP3_NOT_TX_DISABLE,
-			       devarg_handle_int, &sdp3_no_tx_disable) == 0 &&
-	    sdp3_no_tx_disable == 1)
-		adapter->sdp3_no_tx_disable = 1;
+	if (rte_kvargs_count(kvlist, IXGBE_DEVARG_FIBER_SDP3_NOT_TX_DISABLE) == 1)
+		(void)rte_kvargs_process_opt(kvlist, IXGBE_DEVARG_FIBER_SDP3_NOT_TX_DISABLE,
+					     rte_kvargs_handle_bool,
+					     &adapter->sdp3_no_tx_disable);
 
 	if (rte_kvargs_count(kvlist, IXGBE_DEVARG_FDIR_BUFFER_SIZE) != 0) {
 		if (rte_kvargs_process(kvlist, IXGBE_DEVARG_FDIR_BUFFER_SIZE,
@@ -1499,28 +1495,11 @@ generate_random_mac_addr(struct rte_ether_addr *mac_addr)
 	memcpy(&mac_addr->addr_bytes[3], &random, 3);
 }
 
-static int
-devarg_handle_int(__rte_unused const char *key, const char *value,
-		  void *extra_args)
-{
-	uint16_t *n = extra_args;
-
-	if (value == NULL || extra_args == NULL)
-		return -EINVAL;
-
-	*n = (uint16_t)strtoul(value, NULL, 0);
-	if (*n == USHRT_MAX && errno == ERANGE)
-		return -1;
-
-	return 0;
-}
-
 static void
 ixgbevf_parse_devargs(struct ixgbe_adapter *adapter,
 		      struct rte_devargs *devargs)
 {
 	struct rte_kvargs *kvlist;
-	uint16_t pflink_fullchk;
 
 	if (devargs == NULL)
 		return;
@@ -1529,11 +1508,10 @@ ixgbevf_parse_devargs(struct ixgbe_adapter *adapter,
 	if (kvlist == NULL)
 		return;
 
-	if (rte_kvargs_count(kvlist, IXGBEVF_DEVARG_PFLINK_FULLCHK) == 1 &&
-	    rte_kvargs_process(kvlist, IXGBEVF_DEVARG_PFLINK_FULLCHK,
-			       devarg_handle_int, &pflink_fullchk) == 0 &&
-	    pflink_fullchk == 1)
-		adapter->pflink_fullchk = 1;
+	if (rte_kvargs_count(kvlist, IXGBEVF_DEVARG_PFLINK_FULLCHK) == 1)
+		(void)rte_kvargs_process_opt(kvlist, IXGBEVF_DEVARG_PFLINK_FULLCHK,
+					     rte_kvargs_handle_bool,
+					     &adapter->pflink_fullchk);
 
 	rte_kvargs_free(kvlist);
 }
@@ -4181,7 +4159,7 @@ ixgbevf_check_link(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
 		*speed = IXGBE_LINK_SPEED_UNKNOWN;
 	}
 
-	if (wait_to_complete == 0 && adapter->pflink_fullchk == 0) {
+	if (wait_to_complete == 0 && !adapter->pflink_fullchk) {
 		if (*speed == IXGBE_LINK_SPEED_UNKNOWN)
 			mac->get_link_status = true;
 		else
drivers/net/intel/ixgbe/ixgbe_ethdev.h +2 -2
index 0efef29a5d..fff9a07943 100644
@@ -490,13 +490,13 @@ struct ixgbe_adapter {
 	uint8_t rss_reta_updated;
 
 	/* Used for limiting SDP3 TX_DISABLE checks */
-	uint8_t sdp3_no_tx_disable;
+	bool sdp3_no_tx_disable;
 	uint16_t max_vfs;
 
 	/* Used for VF link sync with PF's physical and logical (by checking
 	 * mailbox status) link status.
 	 */
-	uint8_t pflink_fullchk;
+	bool pflink_fullchk;
 	uint8_t mac_ctrl_frame_fwd;
 	RTE_ATOMIC(bool) link_thread_running;
 	rte_thread_t link_thread_tid;
Signature · 1 line
2.53.0
#163 lore raw

PATCH v3 30/65 net/txgbe: use kvargs numeric helpers

In reply to Stephen Hemminger

txgbe_handle_devarg() truncates the strtoul() result into a uint16_t and then infers overflow from the truncated value being USHRT_MAX, so a value such as "65536" is silently accepted as zero. It also never checked the end pointer, and read errno without clearing it first.

All thirteen rte_kvargs_process() results were discarded, so a bad value was ignored and the default used instead. Collect them and fail the probe, which makes a malformed devarg visible rather than silently changing the configuration the user asked for.

auto_neg, poll, present, sgmii, tx_headwb and rx_desc_merge are booleans, so parse them with rte_kvargs_handle_bool() into a bool. The struct txgbe_devargs fields stay u16, since they live in base code shared with the vendor tree; a bool assigned into them still yields 0 or 1. A bare key now enables the option, and the usual spellings are accepted.

---
 drivers/net/txgbe/txgbe_ethdev.c | 97 ++++++++++++++------------------
 1 file changed, 42 insertions(+), 55 deletions(-)
drivers/net/txgbe/txgbe_ethdev.c +42 -55
index 0734a1c63c..815bb3b486 100644
@@ -447,28 +447,6 @@ txgbe_swfw_lock_reset(struct txgbe_hw *hw)
 }
 
 static int
-txgbe_handle_devarg(__rte_unused const char *key, const char *value,
-		  void *extra_args)
-{
-	uint16_t *arg = extra_args;
-	char *endp;
-	unsigned long num;
-
-	if (value == NULL || extra_args == NULL)
-		return -EINVAL;
-
-	errno = 0;
-	num = strtoul(value, &endp, 10);
-	if (*value == '\0' || *endp != '\0')
-		return -EINVAL;
-
-	if (num > UINT16_MAX || errno == ERANGE)
-		return -ERANGE;
-	*arg = (uint16_t)num;
-	return 0;
-}
-
-static void
 txgbe_parse_devargs(struct rte_eth_dev *dev)
 {
 	struct rte_eth_fdir_conf *fdir_conf = TXGBE_DEV_FDIR_CONF(dev);
@@ -476,10 +454,10 @@ txgbe_parse_devargs(struct rte_eth_dev *dev)
 	struct rte_devargs *devargs = pci_dev->device.devargs;
 	struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
 	struct rte_kvargs *kvlist;
-	u16 auto_neg = 1;
-	u16 poll = 0;
-	u16 present = 0;
-	u16 sgmii = 0;
+	bool auto_neg = true;
+	bool poll = false;
+	bool present = false;
+	bool sgmii = false;
 	u16 ffe_set = 0;
 	u16 ffe_main = 27;
 	u16 ffe_pre = 8;
@@ -488,9 +466,10 @@ txgbe_parse_devargs(struct rte_eth_dev *dev)
 	u16 pballoc = 0;
 	u16 drop_queue = 127;
 	/* New devargs for amberlite config */
-	u16 tx_headwb = 1;
+	bool tx_headwb = true;
 	u16 tx_headwb_size = 16;
-	u16 rx_desc_merge = 1;
+	bool rx_desc_merge = true;
+	int ret;
 
 	if (devargs == NULL)
 		goto null;
@@ -499,34 +478,37 @@ txgbe_parse_devargs(struct rte_eth_dev *dev)
 	if (kvlist == NULL)
 		goto null;
 
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_BP_AUTO,
-			   &txgbe_handle_devarg, &auto_neg);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_KR_POLL,
-			   &txgbe_handle_devarg, &poll);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_KR_PRESENT,
-			   &txgbe_handle_devarg, &present);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_KX_SGMII,
-			   &txgbe_handle_devarg, &sgmii);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_FFE_SET,
-			   &txgbe_handle_devarg, &ffe_set);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_FFE_MAIN,
-			   &txgbe_handle_devarg, &ffe_main);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_FFE_PRE,
-			   &txgbe_handle_devarg, &ffe_pre);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_FFE_POST,
-			   &txgbe_handle_devarg, &ffe_post);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_FDIR_PBALLOC,
-			   &txgbe_handle_devarg, &pballoc);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_FDIR_DROP_QUEUE,
-			   &txgbe_handle_devarg, &drop_queue);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_TX_HEAD_WB,
-			   &txgbe_handle_devarg, &tx_headwb);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_TX_HEAD_WB_SIZE,
-			   &txgbe_handle_devarg, &tx_headwb_size);
-	rte_kvargs_process(kvlist, TXGBE_DEVARG_RX_DESC_MERGE,
-			   &txgbe_handle_devarg, &rx_desc_merge);
+	ret = rte_kvargs_process_opt(kvlist, TXGBE_DEVARG_BP_AUTO,
+				     rte_kvargs_handle_bool, &auto_neg);
+	ret |= rte_kvargs_process_opt(kvlist, TXGBE_DEVARG_KR_POLL,
+				      rte_kvargs_handle_bool, &poll);
+	ret |= rte_kvargs_process_opt(kvlist, TXGBE_DEVARG_KR_PRESENT,
+				      rte_kvargs_handle_bool, &present);
+	ret |= rte_kvargs_process_opt(kvlist, TXGBE_DEVARG_KX_SGMII,
+				      rte_kvargs_handle_bool, &sgmii);
+	ret |= rte_kvargs_process(kvlist, TXGBE_DEVARG_FFE_SET,
+				  rte_kvargs_handle_u16, &ffe_set);
+	ret |= rte_kvargs_process(kvlist, TXGBE_DEVARG_FFE_MAIN,
+				  rte_kvargs_handle_u16, &ffe_main);
+	ret |= rte_kvargs_process(kvlist, TXGBE_DEVARG_FFE_PRE,
+				  rte_kvargs_handle_u16, &ffe_pre);
+	ret |= rte_kvargs_process(kvlist, TXGBE_DEVARG_FFE_POST,
+				  rte_kvargs_handle_u16, &ffe_post);
+	ret |= rte_kvargs_process(kvlist, TXGBE_DEVARG_FDIR_PBALLOC,
+				  rte_kvargs_handle_u16, &pballoc);
+	ret |= rte_kvargs_process(kvlist, TXGBE_DEVARG_FDIR_DROP_QUEUE,
+				  rte_kvargs_handle_u16, &drop_queue);
+	ret |= rte_kvargs_process_opt(kvlist, TXGBE_DEVARG_TX_HEAD_WB,
+				      rte_kvargs_handle_bool, &tx_headwb);
+	ret |= rte_kvargs_process(kvlist, TXGBE_DEVARG_TX_HEAD_WB_SIZE,
+				  rte_kvargs_handle_u16, &tx_headwb_size);
+	ret |= rte_kvargs_process_opt(kvlist, TXGBE_DEVARG_RX_DESC_MERGE,
+				      rte_kvargs_handle_bool, &rx_desc_merge);
 	rte_kvargs_free(kvlist);
 
+	if (ret != 0)
+		return -EINVAL;
+
 null:
 	hw->devarg.auto_neg = auto_neg;
 	hw->devarg.poll = poll;
@@ -542,6 +524,8 @@ txgbe_parse_devargs(struct rte_eth_dev *dev)
 
 	fdir_conf->pballoc = pballoc;
 	fdir_conf->drop_queue = drop_queue;
+
+	return 0;
 }
 
 static void
@@ -642,7 +626,10 @@ eth_txgbe_dev_init(struct rte_eth_dev *eth_dev, void *init_params __rte_unused)
 	hw->isb_dma = TMZ_PADDR(mz);
 	hw->isb_mem = TMZ_VADDR(mz);
 
-	txgbe_parse_devargs(eth_dev);
+	err = txgbe_parse_devargs(eth_dev);
+	if (err != 0)
+		return err;
+
 	/* Initialize the shared code (base driver) */
 	err = txgbe_init_shared_code(hw);
 	if (err != 0) {
Signature · 1 line
2.53.0
#164 lore raw

PATCH v3 31/65 net/octeontx: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_integer_arg() uses atoi(), which cannot report an error, so a malformed value such as "abc" becomes zero and the negative check never fires.

It also stores through an int pointer, while the caller passes &params->nr_port, which is a uint8_t and the only member of a struct octeontx_vdev_init_params on the stack. Parsing nr_port therefore wrote three bytes past the field.

Use rte_kvargs_handle_u8(), which both validates the value and matches the width of the field.

Bugzilla ID: 2036

Fixes f7be70e5130e ("net/octeontx: add net device probe and remove")
---
 drivers/net/octeontx/octeontx_ethdev.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)
drivers/net/octeontx/octeontx_ethdev.c +1 -17
index e4dca30d9d..d2ad9a7d04 100644
@@ -65,22 +65,6 @@ RTE_LOG_REGISTER_SUFFIX(otx_net_logtype_mbox, mbox, NOTICE);
 RTE_LOG_REGISTER_SUFFIX(otx_net_logtype_init, init, NOTICE);
 RTE_LOG_REGISTER_SUFFIX(otx_net_logtype_driver, driver, NOTICE);
 
-/* Parse integer from integer argument */
-static int
-parse_integer_arg(const char *key __rte_unused,
-		const char *value, void *extra_args)
-{
-	int *i = (int *)extra_args;
-
-	*i = atoi(value);
-	if (*i < 0) {
-		octeontx_log_err("argument has to be positive.");
-		return -1;
-	}
-
-	return 0;
-}
-
 static int
 octeontx_parse_vdev_init_params(struct octeontx_vdev_init_params *params,
 				struct rte_vdev_device *dev)
@@ -106,7 +90,7 @@ octeontx_parse_vdev_init_params(struct octeontx_vdev_init_params *params,
 
 		ret = rte_kvargs_process(kvlist,
 					OCTEONTX_VDEV_NR_PORT_ARG,
-					&parse_integer_arg,
+					rte_kvargs_handle_u8,
 					&params->nr_port);
 		if (ret < 0)
 			goto free_kvlist;
Signature · 1 line
2.53.0
#165 lore raw

PATCH v3 32/65 net/octeon_ep: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_flag() uses atoi(), which cannot report an error, so a malformed value is silently taken as zero. The result was only ever used as a boolean, so use rte_kvargs_handle_bool() and drop the local handler.

This also accepts the usual spellings such as "on" and "true" in addition to 0 and 1.

The boolean uses rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 drivers/net/octeon_ep/otx_ep_ethdev.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/net/octeon_ep/otx_ep_ethdev.c +3 -13
index 85b3c09155..3d6521d084 100644
@@ -29,21 +29,11 @@ static const struct rte_eth_desc_lim otx_ep_tx_desc_lim = {
 	.nb_align	= OTX_EP_TXD_ALIGN,
 };
 
-static int
-parse_flag(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-
-	*(uint8_t *)extra_args = atoi(value);
-
-	return 0;
-}
-
 static int
 otx_ethdev_parse_devargs(struct rte_devargs *devargs, struct otx_ep_device *otx_epvf)
 {
 	struct rte_kvargs *kvlist;
-	uint8_t ism_enable = 0;
+	bool ism_enable = false;
 
 	if (devargs == NULL)
 		goto null_devargs;
@@ -52,11 +42,11 @@ otx_ethdev_parse_devargs(struct rte_devargs *devargs, struct otx_ep_device *otx_
 	if (kvlist == NULL)
 		goto exit;
 
-	rte_kvargs_process(kvlist, OTX_ISM_ENABLE, &parse_flag, &ism_enable);
+	rte_kvargs_process_opt(kvlist, OTX_ISM_ENABLE, rte_kvargs_handle_bool, &ism_enable);
 	rte_kvargs_free(kvlist);
 
 null_devargs:
-	otx_epvf->ism_ena = !!ism_enable;
+	otx_epvf->ism_ena = ism_enable;
 
 	return 0;
 
Signature · 1 line
2.53.0
#166 lore raw

PATCH v3 33/65 net/qede: use kvargs numeric helpers

In reply to Stephen Hemminger

qede_args_check() ignores the end pointer, so "1junk" is accepted, and uses base 0, so a leading zero silently selects octal.

On failure it returned errno, a positive value, where the kvargs handler contract expects a negative error code; return -EINVAL.

Both arguments are booleans, so drop qede_args_check() and use rte_kvargs_handle_bool() on qdev->enable_tx_switching directly. The handler needed the device only to reach that field and to apply the VF argument on a VF alone, so the IS_VF() test moves to the caller and the loop over valid_args[] becomes two explicit calls.

The old handler accepted any integer and reduced it with "!!", so "vf_txswitch=2" meant true. It is now rejected, along with the other values that are not booleans. A bare key enables tx-switching.

---
 drivers/net/qede/qede_ethdev.c | 55 ++++++++++------------------------
 1 file changed, 16 insertions(+), 39 deletions(-)
drivers/net/qede/qede_ethdev.c +16 -39
index c41177b86a..fa5f24b023 100644
@@ -1201,39 +1201,14 @@ static const char * const valid_args[] = {
 	NULL,
 };
 
-static int qede_args_check(const char *key, const char *val, void *opaque)
-{
-	unsigned long tmp;
-	int ret = 0;
-	struct rte_eth_dev *eth_dev = opaque;
-	struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
-	struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
-
-	errno = 0;
-	tmp = strtoul(val, NULL, 0);
-	if (errno) {
-		DP_INFO(edev, "%s: \"%s\" is not a valid integer", key, val);
-		return errno;
-	}
-
-	if ((strcmp(QEDE_NPAR_TX_SWITCHING, key) == 0) ||
-	    ((strcmp(QEDE_VF_TX_SWITCHING, key) == 0) && IS_VF(edev))) {
-		qdev->enable_tx_switching = !!tmp;
-		DP_INFO(edev, "Disabling %s tx-switching\n",
-			strcmp(QEDE_NPAR_TX_SWITCHING, key) ?
-			"VF" : "NPAR");
-	}
-
-	return ret;
-}
-
 static int qede_args(struct rte_eth_dev *eth_dev)
 {
 	struct rte_pci_device *pci_dev = RTE_CLASS_TO_BUS_DEVICE(eth_dev, *pci_dev);
+	struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
+	struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
 	struct rte_kvargs *kvlist;
 	struct rte_devargs *devargs;
 	int ret;
-	int i;
 
 	devargs = pci_dev->device.devargs;
 	if (!devargs)
@@ -1243,20 +1218,22 @@ static int qede_args(struct rte_eth_dev *eth_dev)
 	if (kvlist == NULL)
 		return -EINVAL;
 
-	 /* Process parameters. */
-	for (i = 0; (valid_args[i] != NULL); ++i) {
-		if (rte_kvargs_count(kvlist, valid_args[i])) {
-			ret = rte_kvargs_process(kvlist, valid_args[i],
-						 qede_args_check, eth_dev);
-			if (ret != ECORE_SUCCESS) {
-				rte_kvargs_free(kvlist);
-				return ret;
-			}
-		}
-	}
+	/*
+	 * Both arguments select the same thing. The VF one is only honoured
+	 * on a VF, which is why the two are handled separately rather than
+	 * in a loop over valid_args[].
+	 */
+	ret = rte_kvargs_process_opt(kvlist, QEDE_NPAR_TX_SWITCHING,
+				     rte_kvargs_handle_bool,
+				     &qdev->enable_tx_switching);
+	if (ret == 0 && IS_VF(edev))
+		ret = rte_kvargs_process_opt(kvlist, QEDE_VF_TX_SWITCHING,
+					     rte_kvargs_handle_bool,
+					     &qdev->enable_tx_switching);
+
 	rte_kvargs_free(kvlist);
 
-	return 0;
+	return ret;
 }
 
 static int qede_dev_configure(struct rte_eth_dev *eth_dev)
Signature · 1 line
2.53.0
#167 lore raw

PATCH v3 34/65 net/nfb: use kvargs numeric helpers

In reply to Stephen Hemminger

Replace the open coded isdigit(), errno and end pointer checks in nfb_eth_dev_create_for_ifc_by_port() with a range checked conversion.

The "port >= LONG_MAX" test was redundant with the interface count check that follows it, since ifc_cnt is far below LONG_MAX.

---
 drivers/net/nfb/nfb_ethdev.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/nfb/nfb_ethdev.c +4 -9
index ba7c849a4b..f2ad831c08 100644
@@ -965,19 +965,14 @@ static int nfb_eth_dev_create_for_ifc_by_port(const char *key __rte_unused,
 		const char *value, void *opaque)
 {
 	int ret = -EINVAL;
-	char *end;
-	unsigned long port;
+	uint64_t port;
 	struct nfb_ifc_create_params *ifc_params = opaque;
 
-	if (value == NULL || strlen(value) == 0 || !isdigit(*value))
+	if (ifc_params->map_info.ifc_cnt == 0)
 		goto out;
 
-	errno = 0;
-	port = strtoul(value, &end, 10);
-	if (errno != 0 || *end != '\0')
-		goto out;
-
-	if (port >= LONG_MAX || port >= (unsigned long)ifc_params->map_info.ifc_cnt)
+	if (rte_kvargs_to_uint(value, 0, ifc_params->map_info.ifc_cnt - 1,
+			       &port) < 0)
 		goto out;
 
 	ifc_params->ifc_info = &ifc_params->map_info.ifc[port];
Signature · 1 line
2.53.0
#168 lore raw

PATCH v3 35/65 net/thunderx: use kvargs numeric helpers

In reply to Stephen Hemminger

nicvf_parse_devargs() uses atoi() on the skip_data_bytes value, which cannot report an error, so a malformed value is silently taken as zero and the argument is quietly ignored rather than rejected.

---
 drivers/net/thunderx/nicvf_ethdev.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/net/thunderx/nicvf_ethdev.c +11 -2
index 30d03f4db9..aa45bd96b1 100644
@@ -3,6 +3,7 @@
  */
 
 #include <assert.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <errno.h>
@@ -2161,9 +2162,17 @@ nicvf_set_first_skip(struct rte_eth_dev *dev)
 
 	for (i = 0; i != kvlist->count; ++i) {
 		const struct rte_kvargs_pair *pair = &kvlist->pairs[i];
+		uint64_t val;
 
-		if (!strcmp(pair->key, SKIP_DATA_BYTES))
-			bytes_to_skip = atoi(pair->value);
+		if (strcmp(pair->key, SKIP_DATA_BYTES))
+			continue;
+
+		if (rte_kvargs_to_uint(pair->value, 0, INT_MAX, &val) < 0) {
+			PMD_INIT_LOG(ERR, "skip_data_bytes is not a valid number");
+			ret = -EINVAL;
+			goto exit;
+		}
+		bytes_to_skip = val;
 	}
 
 	/*128 bytes amounts to one cache line*/
Signature · 1 line
2.53.0
#169 lore raw

PATCH v3 36/65 net/i40e: propagate VF queue number parse errors

In reply to Stephen Hemminger

i40e_pf_config_vf_rxq_number() discarded the rte_kvargs_process() return value and unconditionally returned success, and its own return value was then discarded by i40e_pf_parameter_init(). A malformed or out of range queue-num-per-vf value was therefore silently ignored, leaving the default in place with no indication to the user.

Every other rte_kvargs_process() call site in this driver already checks its return value. Do the same here, so that a bad value fails probe rather than being masked.

rte_kvargs_process() stops at the first failing pair, which preserves the "first invalid or last valid one is used" behaviour already documented in the warning above the call.

---
 drivers/net/intel/i40e/i40e_ethdev.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/net/intel/i40e/i40e_ethdev.c +8 -4
index ad826467de..cdbb4d461e 100644
@@ -4850,6 +4850,7 @@ static int i40e_pf_config_vf_rxq_number(struct rte_eth_dev *dev)
 	struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
 	struct rte_kvargs *kvlist;
 	int kvargs_count;
+	int ret;
 
 	/* set default queue number per VF as 4 */
 	pf->vf_nb_qp_max = RTE_LIBRTE_I40E_QUEUE_NUM_PER_VF;
@@ -4872,12 +4873,12 @@ static int i40e_pf_config_vf_rxq_number(struct rte_eth_dev *dev)
 			    "the first invalid or last valid one is used !",
 			    ETH_I40E_QUEUE_NUM_PER_VF_ARG);
 
-	rte_kvargs_process(kvlist, ETH_I40E_QUEUE_NUM_PER_VF_ARG,
-			   i40e_pf_parse_vf_queue_number_handler, pf);
+	ret = rte_kvargs_process(kvlist, ETH_I40E_QUEUE_NUM_PER_VF_ARG,
+				 i40e_pf_parse_vf_queue_number_handler, pf);
 
 	rte_kvargs_free(kvlist);
 
-	return 0;
+	return ret;
 }
 
 static int
@@ -4887,13 +4888,16 @@ i40e_pf_parameter_init(struct rte_eth_dev *dev)
 	struct i40e_hw *hw = I40E_PF_TO_HW(pf);
 	struct rte_pci_device *pci_dev = RTE_CLASS_TO_BUS_DEVICE(dev, *pci_dev);
 	uint16_t qp_count = 0, vsi_count = 0;
+	int ret;
 
 	if (pci_dev->max_vfs && !hw->func_caps.sr_iov_1_1) {
 		PMD_INIT_LOG(ERR, "HW configuration doesn't support SRIOV");
 		return -EINVAL;
 	}
 
-	i40e_pf_config_vf_rxq_number(dev);
+	ret = i40e_pf_config_vf_rxq_number(dev);
+	if (ret != 0)
+		return ret;
 
 	/* Add the parameter init for LFC */
 	pf->fc_conf.pause_time = I40E_DEFAULT_PAUSE_TIME;
Signature · 1 line
2.53.0
#170 lore raw

PATCH v3 37/65 net/cnxk: use kvargs numeric helpers

In reply to Stephen Hemminger

Most handlers in this file used atoi(), which cannot report an error, so a malformed value was silently taken as zero. Several of them also assigned a uint32_t into a uint16_t without a range check, so a value such as "65536" wrapped to zero.

parse_ipsec_in_spi_range(), parse_ipsec_out_max_sa() and parse_meta_bufsize() went further and deliberately swallowed the error, setting the value to zero and returning success.

Seven of the handlers were left doing nothing beyond a plain range checked store, so drop them and pass rte_kvargs_handle_u16() or rte_kvargs_handle_u32() to rte_kvargs_process() directly. The rest keep a local handler because they have a narrower range or non-numeric syntax.

Since the handlers can now fail, propagate the rte_kvargs_process() return value instead of discarding it, so that a bad argument fails probe rather than being ignored.

The pre_l2 header and SDP channel mask parsers are left alone: each packs several fields into one value separated by punctuation, as in flow_pre_l2_info=0x2/0x7e/0x1, which no single arg_handler_t can parse.

The eleven boolean arguments become bool and use rte_kvargs_handle_bool(): scalar_enable, tx_compl_ena, tag_as_xor, lock_rx_ctx, no_inl_dev, custom_sa_act, rx_inj_ena, custom_meta_aura_dis, custom_inb_sa, force_tail_drop and disable_xqe_drop. Most of the fields behind them are already bool, so the "!!" coercions on the way in are no longer needed. A bare key now enables the option, and the usual spellings are accepted.

---
 drivers/net/cnxk/cnxk_ethdev_devargs.c | 283 +++++++++----------------
 1 file changed, 97 insertions(+), 186 deletions(-)
drivers/net/cnxk/cnxk_ethdev_devargs.c +97 -186
index ea18090919..887349fe24 100644
@@ -5,6 +5,8 @@
 #include <inttypes.h>
 #include <math.h>
 
+#include <rte_kvargs.h>
+
 #include "cnxk_ethdev.h"
 
 struct sdp_channel {
@@ -19,28 +21,14 @@ struct flow_pre_l2_size_info {
 	uint8_t pre_l2_size_shift_dir;
 };
 
-static int
-parse_outb_nb_desc(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-	uint32_t val;
-
-	val = atoi(value);
-
-	*(uint16_t *)extra_args = val;
-
-	return 0;
-}
-
 static int
 parse_outb_nb_crypto_qs(const char *key, const char *value, void *extra_args)
 {
-	RTE_SET_USED(key);
-	uint32_t val;
+	uint64_t val;
 
-	val = atoi(value);
+	RTE_SET_USED(key);
 
-	if (val < 1 || val > 64)
+	if (rte_kvargs_to_uint(value, 1, 64, &val) < 0)
 		return -EINVAL;
 
 	*(uint16_t *)extra_args = val;
@@ -48,51 +36,15 @@ parse_outb_nb_crypto_qs(const char *key, const char *value, void *extra_args)
 	return 0;
 }
 
-static int
-parse_ipsec_in_spi_range(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-	uint32_t val;
-
-	errno = 0;
-	val = strtoul(value, NULL, 0);
-	if (errno)
-		val = 0;
-
-	*(uint32_t *)extra_args = val;
-
-	return 0;
-}
-
 static int
 parse_rxc_step(const char *key, const char *value, void *extra_args)
 {
-	RTE_SET_USED(key);
-	uint32_t val;
+	uint64_t val;
 
-	errno = 0;
-	val = strtoul(value, NULL, 0);
-	if (errno)
-		return -EINVAL;
-
-	if (val > ROC_NIX_INL_REAS_STEP_MAX)
-		return -EINVAL;
-
-	*(uint32_t *)extra_args = val;
-
-	return 0;
-}
-
-static int
-parse_ipsec_out_max_sa(const char *key, const char *value, void *extra_args)
-{
 	RTE_SET_USED(key);
-	uint32_t val;
 
-	errno = 0;
-	val = strtoul(value, NULL, 0);
-	if (errno)
-		val = 0;
+	if (rte_kvargs_to_uint(value, 0, ROC_NIX_INL_REAS_STEP_MAX, &val) < 0)
+		return -EINVAL;
 
 	*(uint32_t *)extra_args = val;
 
@@ -102,12 +54,11 @@ parse_ipsec_out_max_sa(const char *key, const char *value, void *extra_args)
 static int
 parse_flow_max_priority(const char *key, const char *value, void *extra_args)
 {
-	RTE_SET_USED(key);
-	uint16_t val;
+	uint64_t val;
 
-	val = atoi(value);
+	RTE_SET_USED(key);
 
-	if (val < 1 || val > ROC_NPC_MAX_MCAM_PRIORITY)
+	if (rte_kvargs_to_uint(value, 1, ROC_NPC_MAX_MCAM_PRIORITY, &val) < 0)
 		return -EINVAL;
 
 	*(uint16_t *)extra_args = val;
@@ -118,13 +69,12 @@ parse_flow_max_priority(const char *key, const char *value, void *extra_args)
 static int
 parse_flow_prealloc_size(const char *key, const char *value, void *extra_args)
 {
-	RTE_SET_USED(key);
-	uint16_t val;
+	uint64_t val;
 
-	val = atoi(value);
+	RTE_SET_USED(key);
 
 	/* Limit the prealloc size to 32 */
-	if (val < 1 || val > 32)
+	if (rte_kvargs_to_uint(value, 1, 32, &val) < 0)
 		return -EINVAL;
 
 	*(uint16_t *)extra_args = val;
@@ -135,10 +85,12 @@ parse_flow_prealloc_size(const char *key, const char *value, void *extra_args)
 static int
 parse_reta_size(const char *key, const char *value, void *extra_args)
 {
+	uint64_t val;
+
 	RTE_SET_USED(key);
-	uint32_t val;
 
-	val = atoi(value);
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &val) < 0)
+		return -EINVAL;
 
 	if (val <= RTE_ETH_RSS_RETA_SIZE_64)
 		val = ROC_NIX_RSS_RETA_SZ_64;
@@ -177,45 +129,6 @@ parse_pre_l2_hdr_info(const char *key, const char *value, void *extra_args)
 	return 0;
 }
 
-static int
-parse_flag(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-
-	*(uint16_t *)extra_args = atoi(value);
-
-	return 0;
-}
-
-static int
-parse_sqb_count(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-	uint32_t val;
-
-	val = atoi(value);
-
-	*(uint16_t *)extra_args = val;
-
-	return 0;
-}
-
-static int
-parse_meta_bufsize(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-	uint32_t val;
-
-	errno = 0;
-	val = strtoul(value, NULL, 0);
-	if (errno)
-		val = 0;
-
-	*(uint32_t *)extra_args = val;
-
-	return 0;
-}
-
 static int
 parse_switch_header_type(const char *key, const char *value, void *extra_args)
 {
@@ -248,12 +161,11 @@ parse_switch_header_type(const char *key, const char *value, void *extra_args)
 static int
 parse_skip_size_info(const char *key, const char *value, void *extra_args)
 {
+	uint64_t val;
+
 	RTE_SET_USED(key);
-	uint32_t val;
 
-	errno = 0;
-	val = strtoul(value, NULL, 0);
-	if (errno || val > 255)
+	if (rte_kvargs_to_uint(value, 0, 255, &val) < 0)
 		return -EINVAL;
 
 	*(uint16_t *)extra_args = val;
@@ -282,19 +194,6 @@ parse_sdp_channel_mask(const char *key, const char *value, void *extra_args)
 	return 0;
 }
 
-static int
-parse_val_u16(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-	uint16_t val;
-
-	val = atoi(value);
-
-	*(uint16_t *)extra_args = val;
-
-	return 0;
-}
-
 #define CNXK_RSS_RETA_SIZE	"reta_size"
 #define CNXK_SCL_ENABLE		"scalar_enable"
 #define CNXK_TX_COMPL_ENA       "tx_compl_ena"
@@ -334,7 +233,7 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	uint32_t ipsec_in_max_spi = BIT(8) - 1;
 	uint16_t sqb_slack = ROC_NIX_SQB_SLACK;
 	uint32_t ipsec_out_max_sa = BIT(12);
-	uint16_t custom_meta_aura_dis = 0;
+	bool custom_meta_aura_dis = false;
 	uint16_t flow_prealloc_size = 1;
 	uint16_t switch_header_type = 0;
 	uint16_t skip_size_info = 0;
@@ -343,19 +242,20 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	uint32_t ipsec_in_min_spi = 0;
 	uint16_t outb_nb_desc = 8200;
 	struct sdp_channel sdp_chan;
-	uint16_t rss_tag_as_xor = 0;
-	uint16_t force_tail_drop = 0;
-	uint16_t scalar_enable = 0;
-	uint16_t tx_compl_ena = 0;
-	uint16_t custom_sa_act = 0;
-	uint16_t custom_inb_sa = 0;
+	bool rss_tag_as_xor = false;
+	bool force_tail_drop = false;
+	bool scalar_enable = false;
+	bool tx_compl_ena = false;
+	bool custom_sa_act = false;
+	bool custom_inb_sa = false;
 	struct rte_kvargs *kvlist;
-	uint16_t dis_xqe_drop = 0;
+	bool dis_xqe_drop = false;
 	uint32_t meta_buf_sz = 0;
-	uint16_t lock_rx_ctx = 0;
-	uint16_t rx_inj_ena = 0;
-	uint16_t no_inl_dev = 0;
+	bool lock_rx_ctx = false;
+	bool rx_inj_ena = false;
+	bool no_inl_dev = false;
 	uint32_t rxc_step = 0;
+	int ret;
 
 	memset(&sdp_chan, 0, sizeof(sdp_chan));
 	memset(&pre_l2_info, 0, sizeof(struct flow_pre_l2_size_info));
@@ -367,60 +267,71 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	if (kvlist == NULL)
 		goto exit;
 
-	rte_kvargs_process(kvlist, CNXK_RSS_RETA_SIZE, &parse_reta_size,
-			   &reta_sz);
-	rte_kvargs_process(kvlist, CNXK_SCL_ENABLE, &parse_flag,
-			   &scalar_enable);
-	rte_kvargs_process(kvlist, CNXK_TX_COMPL_ENA, &parse_flag,
-			   &tx_compl_ena);
-	rte_kvargs_process(kvlist, CNXK_MAX_SQB_COUNT, &parse_sqb_count,
-			   &sqb_count);
-	rte_kvargs_process(kvlist, CNXK_FLOW_PREALLOC_SIZE,
-			   &parse_flow_prealloc_size, &flow_prealloc_size);
-	rte_kvargs_process(kvlist, CNXK_FLOW_MAX_PRIORITY,
-			   &parse_flow_max_priority, &flow_max_priority);
-	rte_kvargs_process(kvlist, CNXK_SWITCH_HEADER_TYPE,
-			   &parse_switch_header_type, &switch_header_type);
-	rte_kvargs_process(kvlist, CNXK_RSS_TAG_AS_XOR, &parse_flag,
-			   &rss_tag_as_xor);
-	rte_kvargs_process(kvlist, CNXK_LOCK_RX_CTX, &parse_flag, &lock_rx_ctx);
-	rte_kvargs_process(kvlist, CNXK_IPSEC_IN_MIN_SPI,
-			   &parse_ipsec_in_spi_range, &ipsec_in_min_spi);
-	rte_kvargs_process(kvlist, CNXK_IPSEC_IN_MAX_SPI,
-			   &parse_ipsec_in_spi_range, &ipsec_in_max_spi);
-	rte_kvargs_process(kvlist, CNXK_IPSEC_OUT_MAX_SA,
-			   &parse_ipsec_out_max_sa, &ipsec_out_max_sa);
-	rte_kvargs_process(kvlist, CNXK_OUTB_NB_DESC, &parse_outb_nb_desc,
-			   &outb_nb_desc);
-	rte_kvargs_process(kvlist, CNXK_OUTB_NB_CRYPTO_QS,
-			   &parse_outb_nb_crypto_qs, &outb_nb_crypto_qs);
-	rte_kvargs_process(kvlist, CNXK_NO_INL_DEV, &parse_flag, &no_inl_dev);
-	rte_kvargs_process(kvlist, CNXK_SDP_CHANNEL_MASK,
-			   &parse_sdp_channel_mask, &sdp_chan);
-	rte_kvargs_process(kvlist, CNXK_FLOW_PRE_L2_INFO,
-			   &parse_pre_l2_hdr_info, &pre_l2_info);
-	rte_kvargs_process(kvlist, CNXK_CUSTOM_SA_ACT, &parse_flag,
-			   &custom_sa_act);
-	rte_kvargs_process(kvlist, CNXK_SQB_SLACK, &parse_sqb_count,
-			   &sqb_slack);
-	rte_kvargs_process(kvlist, CNXK_NIX_META_BUF_SZ, &parse_meta_bufsize, &meta_buf_sz);
-	rte_kvargs_process(kvlist, CNXK_FLOW_AGING_POLL_FREQ, &parse_val_u16,
-			   &aging_thread_poll_freq);
-	rte_kvargs_process(kvlist, CNXK_NIX_RX_INJ_ENABLE, &parse_flag, &rx_inj_ena);
-	rte_kvargs_process(kvlist, CNXK_CUSTOM_META_AURA_DIS, &parse_flag,
-			   &custom_meta_aura_dis);
-	rte_kvargs_process(kvlist, CNXK_CUSTOM_INB_SA, &parse_flag, &custom_inb_sa);
-	rte_kvargs_process(kvlist, CNXK_FORCE_TAIL_DROP, &parse_flag, &force_tail_drop);
-	rte_kvargs_process(kvlist, CNXK_DIS_XQE_DROP, &parse_flag, &dis_xqe_drop);
-	rte_kvargs_process(kvlist, CNXK_RXC_STEP, &parse_rxc_step, &rxc_step);
-	rte_kvargs_process(kvlist, CNXK_SKIP_SIZE_INFO, &parse_skip_size_info,
-			   &skip_size_info);
+	ret = 0;
+	ret |= rte_kvargs_process(kvlist, CNXK_RSS_RETA_SIZE, &parse_reta_size,
+				  &reta_sz);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_SCL_ENABLE, rte_kvargs_handle_bool,
+				  &scalar_enable);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_TX_COMPL_ENA, rte_kvargs_handle_bool,
+				  &tx_compl_ena);
+	ret |= rte_kvargs_process(kvlist, CNXK_MAX_SQB_COUNT, rte_kvargs_handle_u16,
+				  &sqb_count);
+	ret |= rte_kvargs_process(kvlist, CNXK_FLOW_PREALLOC_SIZE,
+				  &parse_flow_prealloc_size, &flow_prealloc_size);
+	ret |= rte_kvargs_process(kvlist, CNXK_FLOW_MAX_PRIORITY,
+				  &parse_flow_max_priority, &flow_max_priority);
+	ret |= rte_kvargs_process(kvlist, CNXK_SWITCH_HEADER_TYPE,
+				  &parse_switch_header_type, &switch_header_type);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_RSS_TAG_AS_XOR, rte_kvargs_handle_bool,
+				  &rss_tag_as_xor);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_LOCK_RX_CTX, rte_kvargs_handle_bool,
+				  &lock_rx_ctx);
+	ret |= rte_kvargs_process(kvlist, CNXK_IPSEC_IN_MIN_SPI,
+				  rte_kvargs_handle_u32, &ipsec_in_min_spi);
+	ret |= rte_kvargs_process(kvlist, CNXK_IPSEC_IN_MAX_SPI,
+				  rte_kvargs_handle_u32, &ipsec_in_max_spi);
+	ret |= rte_kvargs_process(kvlist, CNXK_IPSEC_OUT_MAX_SA,
+				  rte_kvargs_handle_u32, &ipsec_out_max_sa);
+	ret |= rte_kvargs_process(kvlist, CNXK_OUTB_NB_DESC, rte_kvargs_handle_u16,
+				  &outb_nb_desc);
+	ret |= rte_kvargs_process(kvlist, CNXK_OUTB_NB_CRYPTO_QS,
+				  &parse_outb_nb_crypto_qs, &outb_nb_crypto_qs);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_NO_INL_DEV, rte_kvargs_handle_bool,
+				  &no_inl_dev);
+	ret |= rte_kvargs_process(kvlist, CNXK_SDP_CHANNEL_MASK,
+				  &parse_sdp_channel_mask, &sdp_chan);
+	ret |= rte_kvargs_process(kvlist, CNXK_FLOW_PRE_L2_INFO,
+				  &parse_pre_l2_hdr_info, &pre_l2_info);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_CUSTOM_SA_ACT, rte_kvargs_handle_bool,
+				  &custom_sa_act);
+	ret |= rte_kvargs_process(kvlist, CNXK_SQB_SLACK, rte_kvargs_handle_u16,
+				  &sqb_slack);
+	ret |= rte_kvargs_process(kvlist, CNXK_NIX_META_BUF_SZ, rte_kvargs_handle_u32,
+				  &meta_buf_sz);
+	ret |= rte_kvargs_process(kvlist, CNXK_FLOW_AGING_POLL_FREQ, rte_kvargs_handle_u16,
+				  &aging_thread_poll_freq);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_NIX_RX_INJ_ENABLE, rte_kvargs_handle_bool,
+				  &rx_inj_ena);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_CUSTOM_META_AURA_DIS, rte_kvargs_handle_bool,
+				  &custom_meta_aura_dis);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_CUSTOM_INB_SA, rte_kvargs_handle_bool,
+				  &custom_inb_sa);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_FORCE_TAIL_DROP, rte_kvargs_handle_bool,
+				  &force_tail_drop);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_DIS_XQE_DROP, rte_kvargs_handle_bool,
+				  &dis_xqe_drop);
+	ret |= rte_kvargs_process(kvlist, CNXK_RXC_STEP, &parse_rxc_step, &rxc_step);
+	ret |= rte_kvargs_process(kvlist, CNXK_SKIP_SIZE_INFO, &parse_skip_size_info,
+				  &skip_size_info);
 	rte_kvargs_free(kvlist);
 
+	if (ret != 0)
+		goto exit;
+
 null_devargs:
-	dev->scalar_ena = !!scalar_enable;
-	dev->tx_compl_ena = !!tx_compl_ena;
-	dev->inb.no_inl_dev = !!no_inl_dev;
+	dev->scalar_ena = scalar_enable;
+	dev->tx_compl_ena = tx_compl_ena;
+	dev->inb.no_inl_dev = no_inl_dev;
 	dev->inb.min_spi = ipsec_in_min_spi;
 	dev->inb.max_spi = ipsec_in_max_spi;
 	dev->inb.custom_meta_aura_dis = custom_meta_aura_dis;
@@ -428,7 +339,7 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	dev->outb.nb_desc = outb_nb_desc;
 	dev->outb.nb_crypto_qs = outb_nb_crypto_qs;
 	dev->nix.ipsec_out_max_sa = ipsec_out_max_sa;
-	dev->nix.rss_tag_as_xor = !!rss_tag_as_xor;
+	dev->nix.rss_tag_as_xor = rss_tag_as_xor;
 	dev->nix.max_sqb_count = sqb_count;
 	dev->nix.reta_sz = reta_sz;
 	dev->nix.lock_rx_ctx = lock_rx_ctx;
@@ -458,7 +369,7 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	if (roc_feature_nix_has_rx_inject())
 		dev->nix.rx_inj_ena = rx_inj_ena;
 	dev->nix.force_tail_drop = force_tail_drop;
-	dev->nix.dis_xqe_drop = !!dis_xqe_drop;
+	dev->nix.dis_xqe_drop = dis_xqe_drop;
 	dev->nix.rxc_step = rxc_step;
 	return 0;
 exit:
Signature · 1 line
2.53.0
#171 lore raw

PATCH v3 38/65 net/xsc: use kvargs numeric helpers

In reply to Stephen Hemminger

xsc_dev_args_parse() uses atoi() on each value, which cannot report an error, so a malformed argument is silently taken as zero. For pph_mode and nic_mode zero is a meaningful setting, so a typo quietly selects a mode rather than being rejected.

Fold the three copies of the lookup into a helper that range checks the value and leaves the default in place when it is rejected. The defaults are now assigned up front so they also apply on error.

---
 drivers/net/xsc/xsc_dev.c | 50 +++++++++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 18 deletions(-)
drivers/net/xsc/xsc_dev.c +32 -18
index 478f489516..78a0984295 100644
@@ -199,34 +199,48 @@ xsc_dev_alloc_vfos_info(struct xsc_dev *xdev)
 	return 0;
 }
 
+/* Parse one optional numeric devarg, leaving the default in place if unset. */
+static void
+xsc_dev_arg_get(struct rte_kvargs *kvlist, const char *key, uint64_t max,
+		int *result)
+{
+	const char *value;
+	uint64_t val;
+
+	value = rte_kvargs_get(kvlist, key);
+	if (value == NULL)
+		return;
+
+	if (rte_kvargs_to_uint(value, 0, max, &val) < 0) {
+		PMD_DRV_LOG(ERR, "Invalid %s \"%s\", using default %d",
+			    key, value, *result);
+		return;
+	}
+
+	*result = val;
+}
+
 static void
 xsc_dev_args_parse(struct xsc_dev *xdev, struct rte_devargs *devargs)
 {
 	struct rte_kvargs *kvlist;
 	struct xsc_devargs *xdevargs = &xdev->devargs;
-	const char *tmp;
+
+	xdevargs->pph_mode = XSC_PPH_NONE;
+	xdevargs->nic_mode = XSC_NIC_MODE_LEGACY;
+	xdevargs->flow_mode = XSC_DEV_DEF_FLOW_MODE;
 
 	kvlist = rte_kvargs_parse(devargs->args, NULL);
 	if (kvlist == NULL)
 		return;
 
-	tmp = rte_kvargs_get(kvlist, XSC_PPH_MODE_ARG);
-	if (tmp != NULL)
-		xdevargs->pph_mode = atoi(tmp);
-	else
-		xdevargs->pph_mode = XSC_PPH_NONE;
-
-	tmp = rte_kvargs_get(kvlist, XSC_NIC_MODE_ARG);
-	if (tmp != NULL)
-		xdevargs->nic_mode = atoi(tmp);
-	else
-		xdevargs->nic_mode = XSC_NIC_MODE_LEGACY;
-
-	tmp = rte_kvargs_get(kvlist, XSC_FLOW_MODE_ARG);
-	if (tmp != NULL)
-		xdevargs->flow_mode = atoi(tmp);
-	else
-		xdevargs->flow_mode = XSC_DEV_DEF_FLOW_MODE;
+	xsc_dev_arg_get(kvlist, XSC_PPH_MODE_ARG,
+			XSC_RX_PPH | XSC_TX_PPH | XSC_VFREP_PPH | XSC_UPLINK_PPH,
+			&xdevargs->pph_mode);
+	xsc_dev_arg_get(kvlist, XSC_NIC_MODE_ARG, XSC_NIC_MODE_SOC,
+			&xdevargs->nic_mode);
+	xsc_dev_arg_get(kvlist, XSC_FLOW_MODE_ARG, INT_MAX,
+			&xdevargs->flow_mode);
 
 	rte_kvargs_free(kvlist);
 }
Signature · 1 line
2.53.0
#172 lore raw

PATCH v3 39/65 net/hns3: use kvargs numeric helpers

In reply to Stephen Hemminger

hns3_parse_mbx_time_limit() checks neither errno nor the end pointer, so a malformed value such as "abc" is taken as zero and then silently ignored by the range test that follows.

The range test is now part of the conversion, which keeps the existing behaviour of leaving the default in place when the value is out of range.

The device capability mask parser is converted separately, later in this series, since it needs the hexadecimal handler rather than a decimal one.

---
 drivers/net/hns3/hns3_common.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/net/hns3/hns3_common.c +2 -3
index 29b51856d9..c21740ba5e 100644
@@ -231,14 +231,13 @@ hns3_parse_mbx_time_limit(const char *key, const char *value, void *extra_args)
 	if (value == NULL || extra_args == NULL)
 		return 0;
 
-	val = strtoul(value, NULL, HNS3_CONVERT_TO_DECIMAL);
-
 	/*
 	 * 500ms is empirical value in process of mailbox communication. If
 	 * the delay value is set to one lower than the empirical value, mailbox
 	 * communication may fail.
 	 */
-	if (val > HNS3_MBX_DEF_TIME_LIMIT_MS && val <= UINT16_MAX)
+	if (rte_kvargs_to_uint(value, HNS3_MBX_DEF_TIME_LIMIT_MS + 1,
+			       UINT16_MAX, &val) == 0)
 		*(uint16_t *)extra_args = val;
 
 	return 0;
Signature · 1 line
2.53.0
#173 lore raw

Re: [PATCH v3 39/65] net/hns3: use kvargs numeric helpers

Quoted 24 lines · Stephen Hemminger

hns3_parse_mbx_time_limit() checks neither errno nor the end pointer, so a malformed value such as "abc" is taken as zero and then silently ignored by the range test that follows.

The range test is now part of the conversion, which keeps the existing behaviour of leaving the default in place when the value is out of range.

The device capability mask parser is converted separately, later in this series, since it needs the hexadecimal handler rather than a decimal one.

---
 drivers/net/hns3/hns3_common.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/net/hns3/hns3_common.c +0 -0
index 29b51856d9..c21740ba5e 100644
@@ -231,14 +231,13 @@ hns3_parse_mbx_time_limit(const char *key, const char *value, void *extra_args)
 	if (value == NULL || extra_args == NULL)
 		return 0;
 

drivers/net/hns3/hns3_common.c hns3_parse_mbx_time_limit(const char *key, const char *value, void *extra_args) · line 240

Quoted 10 lines
drivers/net/hns3/hns3_common.c +2 -3
-	val = strtoul(value, NULL, HNS3_CONVERT_TO_DECIMAL);
-
 	/*
 	 * 500ms is empirical value in process of mailbox communication. If
 	 * the delay value is set to one lower than the empirical value, mailbox
 	 * communication may fail.
 	 */
-	if (val > HNS3_MBX_DEF_TIME_LIMIT_MS && val <= UINT16_MAX)
+	if (rte_kvargs_to_uint(value, HNS3_MBX_DEF_TIME_LIMIT_MS + 1,
+			       UINT16_MAX, &val) == 0)

It should be:
if (rte_kvargs_to_uint(value, HNS3_MBX_DEF_TIME_LIMIT_MS,

			       UINT16_MAX, &val) == 0)

Thanks

Quoted 3 lines
drivers/net/hns3/hns3_common.c +0 -0
 		*(uint16_t *)extra_args = val;
 
 	return 0;
#174 lore raw

PATCH v3 40/65 net/enetc: use kvargs numeric helpers

In reply to Stephen Hemminger

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.

---
 drivers/net/enetc/enetc4_vf.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
drivers/net/enetc/enetc4_vf.c +12 -10
index 6eb1e5d760..fd985c85c9 100644
@@ -2012,28 +2012,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);
 			}
 
Signature · 1 line
2.53.0
#175 lore raw

PATCH v3 41/65 event/dlb2: use kvargs numeric helpers

In reply to Stephen Hemminger

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.

---
 doc/guides/eventdevs/dlb2.rst |   4 +-
 drivers/event/dlb2/dlb2.c     | 127 ++++------------------------------
 2 files changed, 14 insertions(+), 117 deletions(-)
doc/guides/eventdevs/dlb2.rst +2 -2
index 06841ba312..0966746f4e 100644
@@ -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
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/event/dlb2/dlb2.c +12 -115

184 lines not on this page · raw message

Signature · 1 line
2.53.0
#176 lore raw

PATCH v3 42/65 net/nfp: use kvargs numeric helpers

In reply to Stephen Hemminger

nfp_devarg_handle_int() infers overflow from the result being ULONG_MAX rather than checking errno, so the literal value 18446744073709551615 is rejected while a genuine overflow of any other value is not detected.

Both users of it parse a boolean, so use rte_kvargs_handle_bool() and drop the local handler along with the open coded 0/1 check. The documented "=0" and "=1" forms still work, and the usual spellings such as "on" and "true" are now accepted as well.

The boolean uses rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 drivers/net/nfp/nfp_ethdev.c | 36 ++----------------------------------
 1 file changed, 2 insertions(+), 34 deletions(-)
drivers/net/nfp/nfp_ethdev.c +2 -34
index 2f7ce812e4..2ad1f12e5b 100644
@@ -48,28 +48,6 @@ struct nfp_net_init {
 	struct nfp_net_hw_priv *hw_priv;
 };
 
-static int
-nfp_devarg_handle_int(const char *key,
-		const char *value,
-		void *extra_args)
-{
-	char *end_ptr;
-	uint64_t *num = extra_args;
-
-	if (value == NULL)
-		return -EPERM;
-
-	*num = strtoul(value, &end_ptr, 10);
-	if (*num == ULONG_MAX) {
-		PMD_DRV_LOG(ERR, "%s: '%s' is not a valid param.", key, value);
-		return -ERANGE;
-	} else if (value == end_ptr) {
-		return -EPERM;
-	}
-
-	return 0;
-}
-
 static int
 nfp_devarg_parse_bool_para(struct rte_kvargs *kvlist,
 		const char *key_match,
@@ -77,7 +55,6 @@ nfp_devarg_parse_bool_para(struct rte_kvargs *kvlist,
 {
 	int ret;
 	uint32_t count;
-	uint64_t value;
 
 	count = rte_kvargs_count(kvlist, key_match);
 	if (count == 0)
@@ -88,20 +65,11 @@ nfp_devarg_parse_bool_para(struct rte_kvargs *kvlist,
 		return -EINVAL;
 	}
 
-	ret = rte_kvargs_process(kvlist, key_match, &nfp_devarg_handle_int, &value);
+	ret = rte_kvargs_process_opt(kvlist, key_match, rte_kvargs_handle_bool,
+			value_ret);
 	if (ret != 0)
 		return -EINVAL;
 
-	if (value == 1) {
-		*value_ret = true;
-	} else if (value == 0) {
-		*value_ret = false;
-	} else {
-		PMD_DRV_LOG(ERR, "The param does not work, the format is %s=0/1.",
-				key_match);
-		return -EINVAL;
-	}
-
 	return 0;
 }
 
Signature · 1 line
2.53.0
#177 lore raw

PATCH v3 43/65 drivers/crypto: use kvargs numeric helpers

In reply to Stephen Hemminger

The dump mode handlers use atoi(), which cannot report an error, so a malformed value is silently taken as zero, which disables dumping entirely rather than being rejected.

Out of range values keep being clamped to the full dump level, as before.

---
 drivers/crypto/dpaa2_sec/dpaa2_sec_dpseci.c | 15 ++++++++++++---
 drivers/crypto/dpaa_sec/dpaa_sec.c          | 17 +++++++++++++----
 2 files changed, 25 insertions(+), 7 deletions(-)
drivers/crypto/dpaa2_sec/dpaa2_sec_dpseci.c +12 -3
index 3d980d096f..93e92af6a7 100644
@@ -4342,13 +4342,22 @@ check_devargs_handler(const char *key, const char *value,
 	if (!strcmp(key, "drv_strict_order")) {
 		priv->en_loose_ordered = false;
 	} else if (!strcmp(key, "drv_dump_mode")) {
-		dpaa2_sec_dp_dump = atoi(value);
-		if (dpaa2_sec_dp_dump > DPAA2_SEC_DP_FULL_DUMP) {
+		uint64_t val;
+
+		if (rte_kvargs_to_uint(value, 0, UINT8_MAX, &val) < 0) {
+			DPAA2_SEC_WARN("WARN: invalid value \"%s\" for \"%s\"",
+				       value, key);
+			return -EINVAL;
+		}
+
+		if (val > DPAA2_SEC_DP_FULL_DUMP) {
 			DPAA2_SEC_WARN("WARN: DPAA2_SEC_DP_DUMP_LEVEL is not "
 				      "supported, changing to FULL error"
 				      " prints");
-			dpaa2_sec_dp_dump = DPAA2_SEC_DP_FULL_DUMP;
+			val = DPAA2_SEC_DP_FULL_DUMP;
 		}
+
+		dpaa2_sec_dp_dump = val;
 	} else
 		return -1;
 
drivers/crypto/dpaa_sec/dpaa_sec.c +13 -4
index c53ee70853..3feb752bcd 100644
@@ -3625,16 +3625,25 @@ dpaa_sec_uninit(struct rte_cryptodev *dev)
 }
 
 static int
-check_devargs_handler(__rte_unused const char *key, const char *value,
+check_devargs_handler(const char *key, const char *value,
 		      __rte_unused void *opaque)
 {
-	dpaa_sec_dp_dump = atoi(value);
-	if (dpaa_sec_dp_dump > DPAA_SEC_DP_FULL_DUMP) {
+	uint64_t val;
+
+	if (rte_kvargs_to_uint(value, 0, UINT8_MAX, &val) < 0) {
+		DPAA_SEC_WARN("WARN: invalid value \"%s\" for \"%s\"",
+			      value, key);
+		return -EINVAL;
+	}
+
+	if (val > DPAA_SEC_DP_FULL_DUMP) {
 		DPAA_SEC_WARN("WARN: DPAA_SEC_DP_DUMP_LEVEL is not "
 			      "supported, changing to FULL error prints");
-		dpaa_sec_dp_dump = DPAA_SEC_DP_FULL_DUMP;
+		val = DPAA_SEC_DP_FULL_DUMP;
 	}
 
+	dpaa_sec_dp_dump = val;
+
 	return 0;
 }
 
Signature · 1 line
2.53.0
#178 lore raw

PATCH v3 44/65 event/sw: use kvargs numeric helpers

In reply to Stephen Hemminger

All six handlers use atoi(), which cannot report an error, so a malformed value is silently taken as zero. Since zero is within the accepted range for every one of these arguments, a typo such as "sched_quanta=abc" was accepted and quietly applied as zero.

The lower bound is now part of the conversion, so the separate negative checks are no longer needed.

refill_once is a boolean, so drop set_refill_once() and use rte_kvargs_handle_bool(). The registered parameter string said "=<int>", which was never true, and now says "=<0|1>". A bare "refill_once" enables it, and the usual spellings are accepted.

---
 drivers/event/sw/sw_evdev.c | 56 ++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 32 deletions(-)
drivers/event/sw/sw_evdev.c +24 -32
index 3ad82e94ac..fa9fe3e4c2 100644
@@ -872,23 +872,16 @@ sw_close(struct rte_eventdev *dev)
 	return 0;
 }
 
-static int
-assign_numa_node(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *socket_id = opaque;
-	*socket_id = atoi(value);
-	if (*socket_id >= RTE_MAX_NUMA_NODES)
-		return -1;
-	return 0;
-}
-
 static int
 set_sched_quanta(const char *key __rte_unused, const char *value, void *opaque)
 {
 	int *quanta = opaque;
-	*quanta = atoi(value);
-	if (*quanta < 0 || *quanta >= 4096)
+	uint64_t val;
+
+	if (rte_kvargs_to_uint(value, 0, 4095, &val) < 0)
 		return -1;
+
+	*quanta = val;
 	return 0;
 }
 
@@ -896,9 +889,12 @@ static int
 set_credit_quanta(const char *key __rte_unused, const char *value, void *opaque)
 {
 	int *credit = opaque;
-	*credit = atoi(value);
-	if (*credit < 0 || *credit >= 128)
+	uint64_t val;
+
+	if (rte_kvargs_to_uint(value, 0, 127, &val) < 0)
 		return -1;
+
+	*credit = val;
 	return 0;
 }
 
@@ -906,9 +902,12 @@ static int
 set_deq_burst_sz(const char *key __rte_unused, const char *value, void *opaque)
 {
 	int *deq_burst_sz = opaque;
-	*deq_burst_sz = atoi(value);
-	if (*deq_burst_sz < 0 || *deq_burst_sz > SCHED_DEQUEUE_MAX_BURST_SIZE)
+	uint64_t val;
+
+	if (rte_kvargs_to_uint(value, 0, SCHED_DEQUEUE_MAX_BURST_SIZE, &val) < 0)
 		return -1;
+
+	*deq_burst_sz = val;
 	return 0;
 }
 
@@ -916,19 +915,12 @@ static int
 set_min_burst_sz(const char *key __rte_unused, const char *value, void *opaque)
 {
 	int *min_burst_sz = opaque;
-	*min_burst_sz = atoi(value);
-	if (*min_burst_sz < 0 || *min_burst_sz > SCHED_DEQUEUE_MAX_BURST_SIZE)
-		return -1;
-	return 0;
-}
+	uint64_t val;
 
-static int
-set_refill_once(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *refill_once_per_call = opaque;
-	*refill_once_per_call = atoi(value);
-	if (*refill_once_per_call < 0 || *refill_once_per_call > 1)
+	if (rte_kvargs_to_uint(value, 0, SCHED_DEQUEUE_MAX_BURST_SIZE, &val) < 0)
 		return -1;
+
+	*min_burst_sz = val;
 	return 0;
 }
 
@@ -991,7 +983,7 @@ sw_probe(struct rte_vdev_device *vdev)
 	int credit_quanta = SW_DEFAULT_CREDIT_QUANTA;
 	int min_burst_size = 1;
 	int deq_burst_size = SCHED_DEQUEUE_DEFAULT_BURST_SIZE;
-	int refill_once = 0;
+	bool refill_once = false;
 
 	name = rte_vdev_device_name(vdev);
 	params = rte_vdev_device_args(vdev);
@@ -1004,7 +996,7 @@ sw_probe(struct rte_vdev_device *vdev)
 				name);
 		} else {
 			int ret = rte_kvargs_process(kvlist, NUMA_NODE_ARG,
-					assign_numa_node, &socket_id);
+					rte_kvargs_handle_socket_id, &socket_id);
 			if (ret != 0) {
 				SW_LOG_ERR(
 					"%s: Error parsing numa node parameter",
@@ -1053,8 +1045,8 @@ sw_probe(struct rte_vdev_device *vdev)
 				return ret;
 			}
 
-			ret = rte_kvargs_process(kvlist, REFIL_ONCE_ARG,
-					set_refill_once, &refill_once);
+			ret = rte_kvargs_process_opt(kvlist, REFIL_ONCE_ARG,
+					rte_kvargs_handle_bool, &refill_once);
 			if (ret != 0) {
 				SW_LOG_ERR(
 					"%s: Error parsing refill once per call switch",
@@ -1146,5 +1138,5 @@ RTE_PMD_REGISTER_VDEV(EVENTDEV_NAME_SW_PMD, evdev_sw_pmd_drv);
 RTE_PMD_REGISTER_PARAM_STRING(event_sw, NUMA_NODE_ARG "=<int> "
 		SCHED_QUANTA_ARG "=<int>" CREDIT_QUANTA_ARG "=<int>"
 		MIN_BURST_SIZE_ARG "=<int>" DEQ_BURST_SIZE_ARG "=<int>"
-		REFIL_ONCE_ARG "=<int>");
+		REFIL_ONCE_ARG "=<0|1>");
 RTE_LOG_REGISTER_DEFAULT(eventdev_sw_log_level, NOTICE);
Signature · 1 line
2.53.0
#179 lore raw

PATCH v3 45/65 net/bnxt: use kvargs numeric helpers

In reply to Stephen Hemminger

All fourteen handlers repeat the same open coded conversion, testing errno for ERANGE without ever clearing it first. A stale ERANGE left by earlier code therefore rejects a valid value, and the test only fires at all when the result happens to be ULONG_MAX.

The per-argument range checks are left as they are, since they carry the documented limits for each devarg.

---
 drivers/net/bnxt/bnxt_ethdev.c | 106 ++++++++++-----------------------
 1 file changed, 32 insertions(+), 74 deletions(-)
drivers/net/bnxt/bnxt_ethdev.c +32 -74
index 4d4349457c..2942482579 100644
@@ -5872,8 +5872,7 @@ bnxt_parse_devarg_flow_xstat(__rte_unused const char *key,
 			     const char *value, void *opaque_arg)
 {
 	struct bnxt *bp = opaque_arg;
-	unsigned long flow_xstat;
-	char *end = NULL;
+	uint64_t flow_xstat;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -5881,9 +5880,7 @@ bnxt_parse_devarg_flow_xstat(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	flow_xstat = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (flow_xstat == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &flow_xstat) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to flow_xstat devarg.");
 		return -EINVAL;
@@ -5907,8 +5904,7 @@ bnxt_parse_devarg_max_num_kflows(__rte_unused const char *key,
 					const char *value, void *opaque_arg)
 {
 	struct bnxt *bp = opaque_arg;
-	unsigned long max_num_kflows;
-	char *end = NULL;
+	uint64_t max_num_kflows;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -5916,9 +5912,7 @@ bnxt_parse_devarg_max_num_kflows(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	max_num_kflows = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-		(max_num_kflows == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &max_num_kflows) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			"Invalid parameter passed to max_num_kflows devarg.");
 		return -EINVAL;
@@ -5932,8 +5926,8 @@ bnxt_parse_devarg_max_num_kflows(__rte_unused const char *key,
 
 	bp->max_num_kflows = max_num_kflows;
 	if (bp->max_num_kflows)
-		PMD_DRV_LOG_LINE(INFO, "max_num_kflows set as %ldK.",
-				max_num_kflows);
+		PMD_DRV_LOG_LINE(INFO, "max_num_kflows set as %uK.",
+				(unsigned int)max_num_kflows);
 
 	return 0;
 }
@@ -5943,8 +5937,7 @@ bnxt_parse_devarg_cqe_mode(__rte_unused const char *key,
 			   const char *value, void *opaque_arg)
 {
 	struct bnxt *bp = opaque_arg;
-	unsigned long cqe_mode;
-	char *end = NULL;
+	uint64_t cqe_mode;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -5953,9 +5946,7 @@ bnxt_parse_devarg_cqe_mode(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	cqe_mode = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (cqe_mode == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &cqe_mode) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to cqe-mode "
 			    "devargs.");
@@ -5980,8 +5971,7 @@ bnxt_parse_devarg_app_instance_id(__rte_unused const char *key,
 				  const char *value, void *opaque_arg)
 {
 	struct bnxt *bp = opaque_arg;
-	unsigned long app_instance_id;
-	char *end = NULL;
+	uint64_t app_instance_id;
 
 	if (!opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -5989,9 +5979,7 @@ bnxt_parse_devarg_app_instance_id(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	app_instance_id = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (app_instance_id == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &app_instance_id) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 				 "Invalid parameter passed to instance devargs");
 		return -EINVAL;
@@ -6014,8 +6002,7 @@ bnxt_parse_devarg_app_id(__rte_unused const char *key,
 				 const char *value, void *opaque_arg)
 {
 	struct bnxt *bp = opaque_arg;
-	unsigned long app_id;
-	char *end = NULL;
+	uint64_t app_id;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6024,9 +6011,7 @@ bnxt_parse_devarg_app_id(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	app_id = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (app_id == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &app_id) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to app_id "
 			    "devargs.");
@@ -6050,8 +6035,7 @@ bnxt_parse_devarg_ieee_1588(__rte_unused const char *key,
 			    const char *value, void *opaque_arg)
 {
 	struct bnxt *bp = opaque_arg;
-	unsigned long ieee_1588;
-	char *end = NULL;
+	uint64_t ieee_1588;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6060,9 +6044,7 @@ bnxt_parse_devarg_ieee_1588(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	ieee_1588 = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (ieee_1588 == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &ieee_1588) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to ieee_1588 "
 			    "devargs.");
@@ -6094,8 +6076,7 @@ bnxt_parse_devarg_rep_is_pf(__rte_unused const char *key,
 			    const char *value, void *opaque_arg)
 {
 	struct bnxt_representor *vfr_bp = opaque_arg;
-	unsigned long rep_is_pf;
-	char *end = NULL;
+	uint64_t rep_is_pf;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6103,9 +6084,7 @@ bnxt_parse_devarg_rep_is_pf(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	rep_is_pf = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (rep_is_pf == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &rep_is_pf) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to rep_is_pf devargs.");
 		return -EINVAL;
@@ -6131,8 +6110,7 @@ bnxt_parse_devarg_rep_based_pf(__rte_unused const char *key,
 			       const char *value, void *opaque_arg)
 {
 	struct bnxt_representor *vfr_bp = opaque_arg;
-	unsigned long rep_based_pf;
-	char *end = NULL;
+	uint64_t rep_based_pf;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6141,9 +6119,7 @@ bnxt_parse_devarg_rep_based_pf(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	rep_based_pf = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (rep_based_pf == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &rep_based_pf) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to rep_based_pf "
 			    "devargs.");
@@ -6169,8 +6145,7 @@ bnxt_parse_devarg_rep_q_r2f(__rte_unused const char *key,
 			    const char *value, void *opaque_arg)
 {
 	struct bnxt_representor *vfr_bp = opaque_arg;
-	unsigned long rep_q_r2f;
-	char *end = NULL;
+	uint64_t rep_q_r2f;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6179,9 +6154,7 @@ bnxt_parse_devarg_rep_q_r2f(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	rep_q_r2f = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (rep_q_r2f == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &rep_q_r2f) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to rep_q_r2f "
 			    "devargs.");
@@ -6206,8 +6179,7 @@ bnxt_parse_devarg_rep_q_f2r(__rte_unused const char *key,
 			    const char *value, void *opaque_arg)
 {
 	struct bnxt_representor *vfr_bp = opaque_arg;
-	unsigned long rep_q_f2r;
-	char *end = NULL;
+	uint64_t rep_q_f2r;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6216,9 +6188,7 @@ bnxt_parse_devarg_rep_q_f2r(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	rep_q_f2r = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (rep_q_f2r == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &rep_q_f2r) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to rep_q_f2r "
 			    "devargs.");
@@ -6243,8 +6213,7 @@ bnxt_parse_devarg_rep_fc_r2f(__rte_unused const char *key,
 			     const char *value, void *opaque_arg)
 {
 	struct bnxt_representor *vfr_bp = opaque_arg;
-	unsigned long rep_fc_r2f;
-	char *end = NULL;
+	uint64_t rep_fc_r2f;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6253,9 +6222,7 @@ bnxt_parse_devarg_rep_fc_r2f(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	rep_fc_r2f = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (rep_fc_r2f == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &rep_fc_r2f) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to rep_fc_r2f "
 			    "devargs.");
@@ -6270,7 +6237,7 @@ bnxt_parse_devarg_rep_fc_r2f(__rte_unused const char *key,
 
 	vfr_bp->flags |= BNXT_REP_FC_R2F_VALID;
 	vfr_bp->rep_fc_r2f = rep_fc_r2f;
-	PMD_DRV_LOG_LINE(INFO, "rep-fc-r2f = %lu", rep_fc_r2f);
+	PMD_DRV_LOG_LINE(INFO, "rep-fc-r2f = %u", (unsigned int)rep_fc_r2f);
 
 	return 0;
 }
@@ -6280,8 +6247,7 @@ bnxt_parse_devarg_rep_fc_f2r(__rte_unused const char *key,
 			     const char *value, void *opaque_arg)
 {
 	struct bnxt_representor *vfr_bp = opaque_arg;
-	unsigned long rep_fc_f2r;
-	char *end = NULL;
+	uint64_t rep_fc_f2r;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6290,9 +6256,7 @@ bnxt_parse_devarg_rep_fc_f2r(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	rep_fc_f2r = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (rep_fc_f2r == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &rep_fc_f2r) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 			    "Invalid parameter passed to rep_fc_f2r "
 			    "devargs.");
@@ -6307,7 +6271,7 @@ bnxt_parse_devarg_rep_fc_f2r(__rte_unused const char *key,
 
 	vfr_bp->flags |= BNXT_REP_FC_F2R_VALID;
 	vfr_bp->rep_fc_f2r = rep_fc_f2r;
-	PMD_DRV_LOG_LINE(INFO, "rep-fc-f2r = %lu", rep_fc_f2r);
+	PMD_DRV_LOG_LINE(INFO, "rep-fc-f2r = %u", (unsigned int)rep_fc_f2r);
 
 	return 0;
 }
@@ -6317,8 +6281,7 @@ bnxt_parse_devarg_representor_mode(__rte_unused const char *key,
 				   const char *value, void *opaque_arg)
 {
 	struct bnxt *bp = opaque_arg;
-	unsigned long rep;
-	char *end = NULL;
+	uint64_t rep;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6326,9 +6289,7 @@ bnxt_parse_devarg_representor_mode(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	rep = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (rep == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &rep) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 				 "Invalid param passed to rep mode in devargs");
 		return -EINVAL;
@@ -6346,8 +6307,7 @@ bnxt_parse_devarg_scalar_mode(__rte_unused const char *key,
 			      const char *value, void *opaque_arg)
 {
 	struct bnxt *bp = opaque_arg;
-	unsigned long rep;
-	char *end = NULL;
+	uint64_t rep;
 
 	if (!value || !opaque_arg) {
 		PMD_DRV_LOG_LINE(ERR,
@@ -6355,9 +6315,7 @@ bnxt_parse_devarg_scalar_mode(__rte_unused const char *key,
 		return -EINVAL;
 	}
 
-	rep = strtoul(value, &end, 10);
-	if (end == NULL || *end != '\0' ||
-	    (rep == ULONG_MAX && errno == ERANGE)) {
+	if (rte_kvargs_to_uint(value, 0, UINT32_MAX, &rep) < 0) {
 		PMD_DRV_LOG_LINE(ERR,
 				 "Invalid param passed to scalar mode in devargs");
 		return -EINVAL;
Signature · 1 line
2.53.0
#180 lore raw

PATCH v3 46/65 net/bnxt: propagate devargs parsing errors

In reply to Stephen Hemminger

bnxt_parse_dev_args() placed the "err" label immediately after the last "goto err", in the middle of the fall-through path, so the jump skipped nothing. The seven handlers that follow it ran even when an earlier argument had already failed to parse, and their return values were discarded, so a bad value in any of them was ignored entirely.

Move the label to just before the kvargs list is freed and check every handler, so that parsing stops at the first bad argument.

Fixes 29ce7059e8e7 ("net/bnxt: check kvargs parsing")
---
 drivers/net/bnxt/bnxt_ethdev.c | 42 ++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 15 deletions(-)
drivers/net/bnxt/bnxt_ethdev.c +27 -15
index 2942482579..f09a1df44a 100644
@@ -6359,57 +6359,69 @@ bnxt_parse_dev_args(struct bnxt *bp, struct rte_devargs *devargs)
 	if (ret)
 		goto err;
 
-err:
 	/*
 	 * Handler for "mpc" devarg.
 	 * Invoked as for ex: "-a 000:00:0d.0,mpc=1"
 	 */
-	rte_kvargs_process(kvlist, BNXT_DEVARG_MPC,
-			   bnxt_parse_devarg_mpc, bp);
+	ret = rte_kvargs_process(kvlist, BNXT_DEVARG_MPC,
+				 bnxt_parse_devarg_mpc, bp);
+	if (ret)
+		goto err;
 
 	/*
 	 * Handler for "app-id" devarg.
 	 * Invoked as for ex: "-a 000:00:0d.0,app-id=1"
 	 */
-	rte_kvargs_process(kvlist, BNXT_DEVARG_APP_ID,
-			   bnxt_parse_devarg_app_id, bp);
+	ret = rte_kvargs_process(kvlist, BNXT_DEVARG_APP_ID,
+				 bnxt_parse_devarg_app_id, bp);
+	if (ret)
+		goto err;
 
 	/*
 	 * Handler for "ieee-1588" devarg.
 	 * Invoked as for ex: "-a 000:00:0d.0,ieee-1588=1"
 	 */
-	rte_kvargs_process(kvlist, BNXT_DEVARG_IEEE_1588,
-			   bnxt_parse_devarg_ieee_1588, bp);
+	ret = rte_kvargs_process(kvlist, BNXT_DEVARG_IEEE_1588,
+				 bnxt_parse_devarg_ieee_1588, bp);
+	if (ret)
+		goto err;
 
 	/*
 	 * Handler for "cqe-mode" devarg.
 	 * Invoked as for ex: "-a 000:00:0d.0,cqe-mode=1"
 	 */
-	rte_kvargs_process(kvlist, BNXT_DEVARG_CQE_MODE,
-			   bnxt_parse_devarg_cqe_mode, bp);
+	ret = rte_kvargs_process(kvlist, BNXT_DEVARG_CQE_MODE,
+				 bnxt_parse_devarg_cqe_mode, bp);
+	if (ret)
+		goto err;
 
 	/*
 	 * Handler for "representor" devarg.
 	 * Invoked as for ex: "-a 000:00:0d.0,representor=1"
 	 */
-	rte_kvargs_process(kvlist, BNXT_DEVARG_REPRESENTOR,
-			   bnxt_parse_devarg_representor_mode, bp);
+	ret = rte_kvargs_process(kvlist, BNXT_DEVARG_REPRESENTOR,
+				 bnxt_parse_devarg_representor_mode, bp);
+	if (ret)
+		goto err;
 
 	/*
 	 * Handler for "scalar-mode" devarg.
 	 * Invoked as for ex: "-a 000:00:0d.0,scalar-mode=1"
 	 */
-	rte_kvargs_process(kvlist, BNXT_DEVARG_SCALAR_MODE,
-			   bnxt_parse_devarg_scalar_mode, bp);
+	ret = rte_kvargs_process(kvlist, BNXT_DEVARG_SCALAR_MODE,
+				 bnxt_parse_devarg_scalar_mode, bp);
+	if (ret)
+		goto err;
 
 	/*
 	 * Handler for "app-instance-id" devarg.
 	 * Invoked as for ex: "-a 000:00:0d.0,app-instance-id=1"
 	 * This argument is required for enabling truflow hot upgrade feature.
 	 */
-	rte_kvargs_process(kvlist, BNXT_DEVARD_APP_INST_ID,
-			   bnxt_parse_devarg_app_instance_id, bp);
+	ret = rte_kvargs_process(kvlist, BNXT_DEVARD_APP_INST_ID,
+				 bnxt_parse_devarg_app_instance_id, bp);
 
+err:
 	rte_kvargs_free(kvlist);
 	return ret;
 }
Signature · 1 line
2.53.0
#181 lore raw

PATCH v3 47/65 net/mlx4: use kvargs numeric helpers

In reply to Stephen Hemminger

mlx4_arg_parse() checks errno without clearing it first, so a stale value left by earlier code rejects a valid argument, while a malformed value such as "abc" sets no errno at all and is silently taken as zero. The end pointer is never checked, so "1junk" is accepted.

Base 0 was used, so a leading zero silently selected octal.

The PCI_SLOT_NAME parser is left alone: it reads sysfs rather than a device argument.

mr_ext_memseg_en is a boolean, so make the mlx4_conf field bool and parse it with rte_kvargs_handle_bool(). mlx4_arg_parse() serves several keys through a cast function pointer, so the boolean gets a call of its own and the generic loop skips it. The one bit field it is later copied into stays as it is.

---
 drivers/net/mlx4/mlx4.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)
drivers/net/mlx4/mlx4.c +21 -12
index 28f1116891..1e64230e35 100644
@@ -70,7 +70,7 @@ struct mlx4_conf {
 		uint32_t present; /**< Bit-field for existing ports. */
 		uint32_t enabled; /**< Bit-field for user-enabled ports. */
 	} ports;
-	int mr_ext_memseg_en;
+	bool mr_ext_memseg_en;
 	/** Whether memseg should be extended for MR creation. */
 };
 
@@ -532,12 +532,10 @@ mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
 static int
 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
 {
-	unsigned long tmp;
+	uint64_t tmp;
 
-	errno = 0;
-	tmp = strtoul(val, NULL, 0);
-	if (errno) {
-		rte_errno = errno;
+	if (rte_kvargs_to_uint(val, 0, UINT32_MAX, &tmp) < 0) {
+		rte_errno = EINVAL;
 		WARN("%s: \"%s\" is not a valid integer", key, val);
 		return -rte_errno;
 	}
@@ -545,18 +543,16 @@ mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
 		uint32_t ports = rte_log2_u32(conf->ports.present + 1);
 
 		if (tmp >= ports) {
-			ERROR("port index %lu outside range [0,%" PRIu32 ")",
-			      tmp, ports);
+			ERROR("port index %u outside range [0,%u)",
+			      (unsigned int)tmp, ports);
 			return -EINVAL;
 		}
 		if (!(conf->ports.present & (1 << tmp))) {
 			rte_errno = EINVAL;
-			ERROR("invalid port index %lu", tmp);
+			ERROR("invalid port index %u", (unsigned int)tmp);
 			return -rte_errno;
 		}
 		conf->ports.enabled |= 1 << tmp;
-	} else if (strcmp(MLX4_MR_EXT_MEMSEG_EN_KVARG, key) == 0) {
-		conf->mr_ext_memseg_en = !!tmp;
 	} else {
 		rte_errno = EINVAL;
 		WARN("%s: unknown parameter", key);
@@ -590,8 +586,21 @@ mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
 		ERROR("failed to parse kvargs");
 		return -rte_errno;
 	}
+	/*
+	 * mr_ext_memseg_en is a boolean and has a handler of its own; the
+	 * loop below covers the arguments which mlx4_arg_parse() handles.
+	 */
+	ret = rte_kvargs_process_opt(kvlist, MLX4_MR_EXT_MEMSEG_EN_KVARG,
+				     rte_kvargs_handle_bool,
+				     &conf->mr_ext_memseg_en);
+	if (ret != 0)
+		goto free_kvlist;
+
 	/* Process parameters. */
 	for (i = 0; pmd_mlx4_init_params[i]; ++i) {
+		if (strcmp(pmd_mlx4_init_params[i],
+			   MLX4_MR_EXT_MEMSEG_EN_KVARG) == 0)
+			continue;
 		arg_count = rte_kvargs_count(kvlist, pmd_mlx4_init_params[i]);
 		while (arg_count-- > 0) {
 			ret = rte_kvargs_process(kvlist,
@@ -783,7 +792,7 @@ mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
 	struct rte_eth_dev *prev_dev = NULL;
 	struct mlx4_conf conf = {
 		.ports.present = 0,
-		.mr_ext_memseg_en = 1,
+		.mr_ext_memseg_en = true,
 	};
 	unsigned int vf;
 	int i;
Signature · 1 line
2.53.0
#182 lore raw

PATCH v3 48/65 net/sfc: use kvargs numeric helpers

In reply to Stephen Hemminger

sfc_kvarg_long_handler() only checked that strtol() consumed at least one character, so trailing garbage such as "5abc" was accepted, and errno was never checked, so an out of range value was silently taken as LONG_MAX or LONG_MIN.

It did nothing beyond that conversion, so drop it and use the new rte_kvargs_handle_long(). The callers keep their long variables and their own range checks.

---
 drivers/net/sfc/sfc.c        |  2 +-
 drivers/net/sfc/sfc_kvargs.c | 19 -------------------
 drivers/net/sfc/sfc_kvargs.h |  2 --
 drivers/net/sfc/sfc_port.c   |  2 +-
 4 files changed, 2 insertions(+), 23 deletions(-)
drivers/net/sfc/sfc.c +1 -1
index 39cd8d519a..3d460cc222 100644
@@ -1213,7 +1213,7 @@ sfc_kvarg_rxd_wait_timeout_ns(struct sfc_adapter *sa)
 	value = SFC_RXD_WAIT_TIMEOUT_NS_DEF;
 
 	rc = sfc_kvargs_process(sa, SFC_KVARG_RXD_WAIT_TIMEOUT_NS,
-				sfc_kvarg_long_handler, &value);
+				rte_kvargs_handle_long, &value);
 	if (rc != 0)
 		return rc;
 
drivers/net/sfc/sfc_kvargs.c +0 -19
index eb36fa98ca..d67f822dd2 100644
@@ -107,25 +107,6 @@ sfc_kvarg_bool_handler(__rte_unused const char *key,
 	return 0;
 }
 
-int
-sfc_kvarg_long_handler(__rte_unused const char *key,
-		       const char *value_str, void *opaque)
-{
-	long value;
-	char *endptr;
-
-	if (!opaque)
-		return -EINVAL;
-
-	value = strtol(value_str, &endptr, 0);
-	if (endptr == value_str)
-		return -EINVAL;
-
-	*(long *)opaque = value;
-
-	return 0;
-}
-
 int
 sfc_kvarg_string_handler(__rte_unused const char *key,
 			 const char *value_str, void *opaque)
drivers/net/sfc/sfc_kvargs.h +0 -2
index 4dcc61e973..99de772d2d 100644
@@ -88,8 +88,6 @@ int sfc_kvargs_process_opt(struct sfc_adapter *sa, const char *key_match,
 
 int sfc_kvarg_bool_handler(const char *key, const char *value_str,
 			   void *opaque);
-int sfc_kvarg_long_handler(const char *key, const char *value_str,
-			   void *opaque);
 int sfc_kvarg_string_handler(const char *key, const char *value_str,
 			     void *opaque);
 
drivers/net/sfc/sfc_port.c +1 -1
index 33b53f7ac8..d0652b612b 100644
@@ -488,7 +488,7 @@ sfc_port_attach(struct sfc_adapter *sa)
 	kvarg_stats_update_period_ms = SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF;
 
 	rc = sfc_kvargs_process(sa, SFC_KVARG_STATS_UPDATE_PERIOD_MS,
-				sfc_kvarg_long_handler,
+				rte_kvargs_handle_long,
 				&kvarg_stats_update_period_ms);
 	if ((rc == 0) &&
 	    ((kvarg_stats_update_period_ms < 0) ||
Signature · 1 line
2.53.0
#183 lore raw

PATCH v3 49/65 crypto/mvsam: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_integer_arg() uses atoi(), which cannot report an error, so a malformed value such as "abc" becomes zero and the negative check never fires.

---
 drivers/crypto/mvsam/rte_mrvl_pmd.c | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)
drivers/crypto/mvsam/rte_mrvl_pmd.c +3 -19
index 65a63c9d62..4ac9e209df 100644
@@ -1117,22 +1117,6 @@ cryptodev_mrvl_crypto_create(const char *name,
 	return ret;
 }
 
-/** Parse integer from integer argument */
-static int
-parse_integer_arg(const char *key __rte_unused,
-		const char *value, void *extra_args)
-{
-	int *i = (int *) extra_args;
-
-	*i = atoi(value);
-	if (*i < 0) {
-		MRVL_LOG(ERR, "Argument has to be positive!");
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
 /** Parse name */
 static int
 parse_name_arg(const char *key __rte_unused,
@@ -1170,14 +1154,14 @@ mrvl_pmd_parse_input_args(struct mrvl_pmd_init_params *params,
 		/* Common VDEV parameters */
 		ret = rte_kvargs_process(kvlist,
 					 RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
-					 &parse_integer_arg,
+					 rte_kvargs_handle_uint,
 					 &params->common.max_nb_queue_pairs);
 		if (ret < 0)
 			goto free_kvlist;
 
 		ret = rte_kvargs_process(kvlist,
 					 RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
-					 &parse_integer_arg,
+					 rte_kvargs_handle_socket_id,
 					 &params->common.socket_id);
 		if (ret < 0)
 			goto free_kvlist;
@@ -1191,7 +1175,7 @@ mrvl_pmd_parse_input_args(struct mrvl_pmd_init_params *params,
 
 		ret = rte_kvargs_process(kvlist,
 					 MRVL_PMD_MAX_NB_SESS_ARG,
-					 &parse_integer_arg,
+					 rte_kvargs_handle_u32,
 					 &params->max_nb_sessions);
 		if (ret < 0)
 			goto free_kvlist;
Signature · 1 line
2.53.0
#184 lore raw

PATCH v3 50/65 ml/cnxk: use kvargs numeric helpers in cn10k

In reply to Stephen Hemminger

parse_integer_arg() uses atoi(), which cannot report an error, so a malformed value such as "abc" becomes zero and the negative check never fires.

enable_dpe_warnings, report_dpe_warnings, cache_model_data and hw_queue_lock are booleans, so make the fields bool and use rte_kvargs_handle_bool(). The "< 0 || > 1" checks which followed each of them are then dead and go away; the *_set flags stay, since they still distinguish an unset argument from one set to false. A bare key now enables the option, and the usual spellings are accepted.

---
 drivers/ml/cnxk/cn10k_ml_dev.c | 74 +++++++---------------------------
 drivers/ml/cnxk/cn10k_ml_dev.h |  8 ++--
 drivers/ml/cnxk/cn10k_ml_ops.c |  2 +-
 3 files changed, 20 insertions(+), 64 deletions(-)
drivers/ml/cnxk/cn10k_ml_dev.c +15 -59
index c35895dc14..ce96a408c4 100644
@@ -80,20 +80,6 @@ parse_string_arg(const char *key __rte_unused, const char *value, void *extra_ar
 	return 0;
 }
 
-static int
-parse_integer_arg(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	int *i = (int *)extra_args;
-
-	*i = atoi(value);
-	if (*i < 0) {
-		plt_err("Argument has to be positive.");
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
 static int
 cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10k_mldev)
 {
@@ -131,8 +117,9 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_FW_ENABLE_DPE_WARNINGS) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_FW_ENABLE_DPE_WARNINGS,
-					 &parse_integer_arg, &cn10k_mldev->fw.enable_dpe_warnings);
+		ret = rte_kvargs_process_opt(kvlist, CN10K_ML_FW_ENABLE_DPE_WARNINGS,
+					 rte_kvargs_handle_bool,
+					 &cn10k_mldev->fw.enable_dpe_warnings);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
 				CN10K_ML_FW_ENABLE_DPE_WARNINGS);
@@ -143,8 +130,9 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_FW_REPORT_DPE_WARNINGS) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_FW_REPORT_DPE_WARNINGS,
-					 &parse_integer_arg, &cn10k_mldev->fw.report_dpe_warnings);
+		ret = rte_kvargs_process_opt(kvlist, CN10K_ML_FW_REPORT_DPE_WARNINGS,
+					 rte_kvargs_handle_bool,
+					 &cn10k_mldev->fw.report_dpe_warnings);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
 				CN10K_ML_FW_REPORT_DPE_WARNINGS);
@@ -155,7 +143,8 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_DEV_CACHE_MODEL_DATA) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_DEV_CACHE_MODEL_DATA, &parse_integer_arg,
+		ret = rte_kvargs_process_opt(kvlist, CN10K_ML_DEV_CACHE_MODEL_DATA,
+					 rte_kvargs_handle_bool,
 					 &cn10k_mldev->cache_model_data);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
@@ -178,7 +167,8 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_DEV_HW_QUEUE_LOCK) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_DEV_HW_QUEUE_LOCK, &parse_integer_arg,
+		ret = rte_kvargs_process_opt(kvlist, CN10K_ML_DEV_HW_QUEUE_LOCK,
+					 rte_kvargs_handle_bool,
 					 &cn10k_mldev->hw_queue_lock);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
@@ -190,7 +180,7 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 
 	if (rte_kvargs_count(kvlist, CN10K_ML_OCM_PAGE_SIZE) == 1) {
-		ret = rte_kvargs_process(kvlist, CN10K_ML_OCM_PAGE_SIZE, &parse_integer_arg,
+		ret = rte_kvargs_process(kvlist, CN10K_ML_OCM_PAGE_SIZE, rte_kvargs_handle_int,
 					 &cn10k_mldev->ocm_page_size);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s", CN10K_ML_OCM_PAGE_SIZE);
@@ -207,44 +197,18 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 		cn10k_mldev->fw.path = fw_path;
 	plt_info("ML: %s = %s", CN10K_ML_FW_PATH, cn10k_mldev->fw.path);
 
-	if (!enable_dpe_warnings_set) {
+	if (!enable_dpe_warnings_set)
 		cn10k_mldev->fw.enable_dpe_warnings = CN10K_ML_FW_ENABLE_DPE_WARNINGS_DEFAULT;
-	} else {
-		if ((cn10k_mldev->fw.enable_dpe_warnings < 0) ||
-		    (cn10k_mldev->fw.enable_dpe_warnings > 1)) {
-			plt_err("Invalid argument, %s = %d", CN10K_ML_FW_ENABLE_DPE_WARNINGS,
-				cn10k_mldev->fw.enable_dpe_warnings);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_info("ML: %s = %d", CN10K_ML_FW_ENABLE_DPE_WARNINGS,
 		 cn10k_mldev->fw.enable_dpe_warnings);
 
-	if (!report_dpe_warnings_set) {
+	if (!report_dpe_warnings_set)
 		cn10k_mldev->fw.report_dpe_warnings = CN10K_ML_FW_REPORT_DPE_WARNINGS_DEFAULT;
-	} else {
-		if ((cn10k_mldev->fw.report_dpe_warnings < 0) ||
-		    (cn10k_mldev->fw.report_dpe_warnings > 1)) {
-			plt_err("Invalid argument, %s = %d", CN10K_ML_FW_REPORT_DPE_WARNINGS,
-				cn10k_mldev->fw.report_dpe_warnings);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_info("ML: %s = %d", CN10K_ML_FW_REPORT_DPE_WARNINGS,
 		 cn10k_mldev->fw.report_dpe_warnings);
 
-	if (!cache_model_data_set) {
+	if (!cache_model_data_set)
 		cn10k_mldev->cache_model_data = CN10K_ML_DEV_CACHE_MODEL_DATA_DEFAULT;
-	} else {
-		if ((cn10k_mldev->cache_model_data < 0) || (cn10k_mldev->cache_model_data > 1)) {
-			plt_err("Invalid argument, %s = %d", CN10K_ML_DEV_CACHE_MODEL_DATA,
-				cn10k_mldev->cache_model_data);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_info("ML: %s = %d", CN10K_ML_DEV_CACHE_MODEL_DATA, cn10k_mldev->cache_model_data);
 
 	if (!ocm_alloc_mode_set) {
@@ -261,16 +225,8 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *cn10
 	}
 	plt_info("ML: %s = %s", CN10K_ML_OCM_ALLOC_MODE, cn10k_mldev->ocm.alloc_mode);
 
-	if (!hw_queue_lock_set) {
+	if (!hw_queue_lock_set)
 		cn10k_mldev->hw_queue_lock = CN10K_ML_DEV_HW_QUEUE_LOCK_DEFAULT;
-	} else {
-		if ((cn10k_mldev->hw_queue_lock < 0) || (cn10k_mldev->hw_queue_lock > 1)) {
-			plt_err("Invalid argument, %s = %d", CN10K_ML_DEV_HW_QUEUE_LOCK,
-				cn10k_mldev->hw_queue_lock);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_info("ML: %s = %d", CN10K_ML_DEV_HW_QUEUE_LOCK, cn10k_mldev->hw_queue_lock);
 
 	if (!ocm_page_size_set) {
drivers/ml/cnxk/cn10k_ml_dev.h +4 -4
index dadb3b571b..4ca8dc63f4 100644
@@ -108,10 +108,10 @@ struct cn10k_ml_fw {
 	const char *path;
 
 	/* Enable DPE warnings */
-	int enable_dpe_warnings;
+	bool enable_dpe_warnings;
 
 	/* Report DPE warnings */
-	int report_dpe_warnings;
+	bool report_dpe_warnings;
 
 	/* Data buffer */
 	uint8_t *data;
@@ -132,10 +132,10 @@ struct cn10k_ml_dev {
 	struct cn10k_ml_ocm ocm;
 
 	/* Enable / disable model data caching */
-	int cache_model_data;
+	bool cache_model_data;
 
 	/* Use spinlock version of ROC enqueue */
-	int hw_queue_lock;
+	bool hw_queue_lock;
 
 	/* OCM page size */
 	int ocm_page_size;
drivers/ml/cnxk/cn10k_ml_ops.c +1 -1
index 597806b787..732f9b62ae 100644
@@ -377,7 +377,7 @@ cn10k_ml_dev_configure(struct cnxk_ml_dev *cnxk_mldev, const struct rte_ml_dev_c
 	rte_spinlock_init(&ocm->lock);
 
 	/* Set JCMDQ enqueue function */
-	if (cn10k_mldev->hw_queue_lock == 1)
+	if (cn10k_mldev->hw_queue_lock)
 		cn10k_mldev->ml_jcmdq_enqueue = roc_ml_jcmdq_enqueue_sl;
 	else
 		cn10k_mldev->ml_jcmdq_enqueue = roc_ml_jcmdq_enqueue_lf;
Signature · 1 line
2.53.0
#185 lore raw

PATCH v3 51/65 ml/cnxk: use kvargs numeric helpers in mvtvm

In reply to Stephen Hemminger

parse_integer_arg() used atoi(), which cannot report an error, so a malformed value became zero and the negative check never fired. parse_uint_arg() open coded the errno and end pointer checks.

parse_uint_arg() also stored through a uint32_t pointer, but max_nb_qpairs is a uint16_t followed by an int in the same struct, so parsing that argument wrote past the field.

Neither handler did anything beyond a range checked store, so drop them and pass rte_kvargs_handle_u16() directly for max_qps.

cache_model_data is a boolean, so make the field bool and use rte_kvargs_handle_bool(), which makes the "< 0 || > 1" check that followed it dead code. cache_model_data_set stays, since it still tells an unset argument from one set to false. A bare key now enables the option, and the usual spellings are accepted.

Bugzilla ID: 2043

Fixes 48c6081ab4b3 ("ml/cnxk: enable creation of MVTVM virtual device")
---
 drivers/ml/cnxk/mvtvm_ml_dev.c | 47 ++++------------------------------
 drivers/ml/cnxk/mvtvm_ml_dev.h |  2 +-
 2 files changed, 6 insertions(+), 43 deletions(-)
drivers/ml/cnxk/mvtvm_ml_dev.c +5 -42
index 74959a12fc..cbaf17698e 100644
@@ -20,36 +20,6 @@
 
 static const char *const valid_args[] = {MVTVM_ML_DEV_MAX_QPS, MVTVM_ML_DEV_CACHE_MODEL_DATA, NULL};
 
-static int
-parse_integer_arg(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	int *i = (int *)extra_args;
-
-	*i = atoi(value);
-	if (*i < 0) {
-		plt_err("Argument has to be positive.");
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
-static int
-parse_uint_arg(const char *key __rte_unused, const char *value, void *extra_args)
-{
-	int i;
-	char *end;
-	errno = 0;
-
-	i = strtol(value, &end, 10);
-	if (*end != 0 || errno != 0 || i < 0)
-		return -EINVAL;
-
-	*((uint32_t *)extra_args) = i;
-
-	return 0;
-}
-
 static int
 mvtvm_mldev_parse_devargs(const char *args, struct mvtvm_ml_dev *mvtvm_mldev)
 {
@@ -68,7 +38,7 @@ mvtvm_mldev_parse_devargs(const char *args, struct mvtvm_ml_dev *mvtvm_mldev)
 	}
 
 	if (rte_kvargs_count(kvlist, MVTVM_ML_DEV_MAX_QPS) == 1) {
-		ret = rte_kvargs_process(kvlist, MVTVM_ML_DEV_MAX_QPS, &parse_uint_arg,
+		ret = rte_kvargs_process(kvlist, MVTVM_ML_DEV_MAX_QPS, rte_kvargs_handle_u16,
 					 &mvtvm_mldev->max_nb_qpairs);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s", MVTVM_ML_DEV_MAX_QPS);
@@ -79,8 +49,9 @@ mvtvm_mldev_parse_devargs(const char *args, struct mvtvm_ml_dev *mvtvm_mldev)
 	}
 
 	if (rte_kvargs_count(kvlist, MVTVM_ML_DEV_CACHE_MODEL_DATA) == 1) {
-		ret = rte_kvargs_process(kvlist, MVTVM_ML_DEV_CACHE_MODEL_DATA, &parse_integer_arg,
-					 &mvtvm_mldev->cache_model_data);
+		ret = rte_kvargs_process_opt(kvlist, MVTVM_ML_DEV_CACHE_MODEL_DATA,
+					     rte_kvargs_handle_bool,
+					     &mvtvm_mldev->cache_model_data);
 		if (ret < 0) {
 			plt_err("Error processing arguments, key = %s",
 				MVTVM_ML_DEV_CACHE_MODEL_DATA);
@@ -95,16 +66,8 @@ mvtvm_mldev_parse_devargs(const char *args, struct mvtvm_ml_dev *mvtvm_mldev)
 		mvtvm_mldev->max_nb_qpairs = MVTVM_ML_DEV_MAX_QPS_DEFAULT;
 	plt_ml_dbg("ML: %s = %u", MVTVM_ML_DEV_MAX_QPS, mvtvm_mldev->max_nb_qpairs);
 
-	if (!cache_model_data_set) {
+	if (!cache_model_data_set)
 		mvtvm_mldev->cache_model_data = CN10K_ML_DEV_CACHE_MODEL_DATA_DEFAULT;
-	} else {
-		if ((mvtvm_mldev->cache_model_data < 0) || (mvtvm_mldev->cache_model_data > 1)) {
-			plt_err("Invalid argument, %s = %d", MVTVM_ML_DEV_CACHE_MODEL_DATA,
-				mvtvm_mldev->cache_model_data);
-			ret = -EINVAL;
-			goto exit;
-		}
-	}
 	plt_ml_dbg("ML: %s = %d", MVTVM_ML_DEV_CACHE_MODEL_DATA, mvtvm_mldev->cache_model_data);
 
 exit:
drivers/ml/cnxk/mvtvm_ml_dev.h +1 -1
index 05e30f094c..f8b34ecd3a 100644
@@ -31,7 +31,7 @@ struct mvtvm_ml_dev {
 	uint16_t max_nb_qpairs;
 
 	/* Enable / disable model data caching */
-	int cache_model_data;
+	bool cache_model_data;
 };
 
 #endif /* _MVTVM_ML_DEV_H_ */
Signature · 1 line
2.53.0
#186 lore raw

PATCH v3 52/65 event/opdl: use kvargs numeric helpers

In reply to Stephen Hemminger

All three handlers use atoi(), which cannot report an error, so a malformed value is silently taken as zero. Zero is within the accepted range for each of these arguments, so a typo was accepted and quietly applied rather than rejected.

The booleans use rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 drivers/event/opdl/opdl_evdev.c | 49 ++++++---------------------------
 1 file changed, 9 insertions(+), 40 deletions(-)
drivers/event/opdl/opdl_evdev.c +9 -40
index ffa65ef930..4650d3eca8 100644
@@ -3,6 +3,7 @@
  */
 
 #include <inttypes.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -580,38 +581,6 @@ opdl_close(struct rte_eventdev *dev)
 	return 0;
 }
 
-static int
-assign_numa_node(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *socket_id = opaque;
-	*socket_id = atoi(value);
-	if (*socket_id >= RTE_MAX_NUMA_NODES)
-		return -1;
-	return 0;
-}
-
-static int
-set_do_validation(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *do_val = opaque;
-	*do_val = atoi(value);
-	if (*do_val != 0)
-		*do_val = 1;
-
-	return 0;
-}
-static int
-set_do_test(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *do_test = opaque;
-
-	*do_test = atoi(value);
-
-	if (*do_test != 0)
-		*do_test = 1;
-	return 0;
-}
-
 static int
 opdl_probe(struct rte_vdev_device *vdev)
 {
@@ -650,8 +619,8 @@ opdl_probe(struct rte_vdev_device *vdev)
 	struct rte_eventdev *dev;
 	struct opdl_evdev *opdl;
 	int socket_id = rte_socket_id();
-	int do_validation = 0;
-	int do_test = 0;
+	bool do_validation = false;
+	bool do_test = false;
 	int str_len;
 	int test_result = 0;
 
@@ -666,7 +635,7 @@ opdl_probe(struct rte_vdev_device *vdev)
 					name);
 		} else {
 			int ret = rte_kvargs_process(kvlist, NUMA_NODE_ARG,
-					assign_numa_node, &socket_id);
+					rte_kvargs_handle_socket_id, &socket_id);
 			if (ret != 0) {
 				PMD_DRV_LOG(ERR,
 						"%s: Error parsing numa node parameter",
@@ -676,8 +645,8 @@ opdl_probe(struct rte_vdev_device *vdev)
 				return ret;
 			}
 
-			ret = rte_kvargs_process(kvlist, DO_VALIDATION_ARG,
-					set_do_validation, &do_validation);
+			ret = rte_kvargs_process_opt(kvlist, DO_VALIDATION_ARG,
+					rte_kvargs_handle_bool, &do_validation);
 			if (ret != 0) {
 				PMD_DRV_LOG(ERR,
 					"%s: Error parsing do validation parameter",
@@ -686,8 +655,8 @@ opdl_probe(struct rte_vdev_device *vdev)
 				return ret;
 			}
 
-			ret = rte_kvargs_process(kvlist, DO_TEST_ARG,
-					set_do_test, &do_test);
+			ret = rte_kvargs_process_opt(kvlist, DO_TEST_ARG,
+					rte_kvargs_handle_bool, &do_test);
 			if (ret != 0) {
 				PMD_DRV_LOG(ERR,
 					"%s: Error parsing do test parameter",
@@ -734,7 +703,7 @@ opdl_probe(struct rte_vdev_device *vdev)
 	str_len = strlen(name);
 	memcpy(opdl->service_name, name, str_len);
 
-	if (do_test == 1)
+	if (do_test)
 		test_result =  opdl_selftest();
 
 done:
Signature · 1 line
2.53.0
#187 lore raw

PATCH v3 53/65 mempool/cnxk: use kvargs numeric helpers

In reply to Stephen Hemminger

Both handlers use atoi(), which cannot report an error, so a malformed value is silently taken as zero. For max_pools that then gets clamped up to the minimum, and for halo_ena zero is a valid setting, so neither was rejected.

Since the handlers can now fail, propagate the rte_kvargs_process() return value instead of discarding it.

halo_ena is a boolean, so drop its handler and use rte_kvargs_handle_bool() on a bool. That also removes the store through a uint8_t pointer into what the caller declared as a wider variable, which only worked because the local was zeroed first and the host is little endian. A bare "halo_ena" now enables it, and the usual spellings are accepted.

Bugzilla ID: 2044

Fixes 45abbfc836b1 ("mempool/cnxk: support HALO in mempool")
---
 drivers/mempool/cnxk/cnxk_mempool.c | 37 +++++++++++++----------------
 1 file changed, 16 insertions(+), 21 deletions(-)
drivers/mempool/cnxk/cnxk_mempool.c +16 -21
index 6939fccff4..e5f1b9491b 100644
@@ -2,6 +2,7 @@
  * Copyright(C) 2021 Marvell.
  */
 
+#include <limits.h>
 #include <rte_atomic.h>
 #include <bus_pci_driver.h>
 #include <rte_common.h>
@@ -34,10 +35,14 @@ npa_aura_size_to_u32(uint8_t val)
 static int
 parse_max_pools_handler(const char *key, const char *value, void *extra_args)
 {
+	uint64_t val;
+
 	RTE_SET_USED(key);
-	uint32_t val;
 
-	val = rte_align32pow2(atoi(value));
+	if (rte_kvargs_to_uint(value, 0, INT_MAX, &val) < 0)
+		return -EINVAL;
+
+	val = rte_align32pow2(val);
 	if (val < npa_aura_size_to_u32(NPA_AURA_SZ_128))
 		val = 128;
 	if (val > npa_aura_size_to_u32(NPA_AURA_SZ_1M))
@@ -47,27 +52,14 @@ parse_max_pools_handler(const char *key, const char *value, void *extra_args)
 	return 0;
 }
 
-static int
-parse_halo_ena_handler(const char *key, const char *value, void *extra_args)
-{
-	RTE_SET_USED(key);
-	uint8_t val;
-
-	val = atoi(value);
-	if (val != 0 && val != 1)
-		return -EINVAL;
-
-	*(uint8_t *)extra_args = val;
-	return 0;
-}
-
 static int
 cnxk_mempool_plt_parse_devargs(struct rte_pci_device *pci_dev)
 {
 	uint32_t max_pools = npa_aura_size_to_u32(NPA_AURA_SZ_128);
 	struct rte_devargs *devargs = pci_dev->device.devargs;
 	struct rte_kvargs *kvlist;
-	uint32_t halo_ena = 0;
+	bool halo_ena = false;
+	int ret;
 
 	if (devargs == NULL)
 		goto null_devargs;
@@ -75,12 +67,15 @@ cnxk_mempool_plt_parse_devargs(struct rte_pci_device *pci_dev)
 	if (kvlist == NULL)
 		goto exit;
 
-	rte_kvargs_process(kvlist, CNXK_NPA_MAX_POOLS_PARAM,
-			   &parse_max_pools_handler, &max_pools);
-	rte_kvargs_process(kvlist, CNXK_NPA_HALO_ENA_PARAM,
-			   &parse_halo_ena_handler, &halo_ena);
+	ret = rte_kvargs_process(kvlist, CNXK_NPA_MAX_POOLS_PARAM,
+				 &parse_max_pools_handler, &max_pools);
+	ret |= rte_kvargs_process_opt(kvlist, CNXK_NPA_HALO_ENA_PARAM,
+				      rte_kvargs_handle_bool, &halo_ena);
 	rte_kvargs_free(kvlist);
 
+	if (ret != 0)
+		goto exit;
+
 null_devargs:
 	roc_idev_npa_maxpools_set(max_pools);
 	roc_idev_npa_halo_ena_set(halo_ena);
Signature · 1 line
2.53.0
#188 lore raw

PATCH v3 54/65 event/octeontx: use kvargs numeric helpers

In reply to Stephen Hemminger

ssovf_parsekv() open coded the errno, end pointer and range checks, and the value it parsed was only ever used as a boolean.

Use rte_kvargs_handle_bool() and make the flag a bool to match. The documented "=1" form still works, and the usual spellings such as "on" and "true" are now accepted as well.

The boolean uses rte_kvargs_process_opt(), so that a bare key with no value enables the option.

---
 drivers/event/octeontx/ssovf_evdev.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +4 -21
index 957fcab04e..4a56d91bf2 100644
@@ -3,6 +3,7 @@
  */
 
 #include <inttypes.h>
+#include <stdbool.h>
 #include <stdlib.h>
 
 #include <rte_common.h>
@@ -23,7 +24,7 @@
 #include "timvf_evdev.h"
 #include "otx_cryptodev_hw_access.h"
 
-static uint8_t timvf_enable_stats;
+static bool timvf_enable_stats;
 
 RTE_LOG_REGISTER_DEFAULT(otx_logtype_ssovf, NOTICE);
 
@@ -716,24 +717,6 @@ ssovf_close(struct rte_eventdev *dev)
 	return 0;
 }
 
-static int
-ssovf_parsekv(const char *key, const char *value, void *opaque)
-{
-	uint8_t *flag = opaque;
-	uint64_t v;
-	char *end;
-
-	errno = 0;
-	v = strtoul(value, &end, 0);
-	if ((errno != 0) || (value == end) || *end != '\0' || v > 1) {
-		ssovf_log_err("invalid %s value %s", key, value);
-		return -EINVAL;
-	}
-
-	*flag = !!v;
-	return 0;
-}
-
 static int
 ssovf_timvf_caps_get(const struct rte_eventdev *dev, uint64_t flags,
 		     uint32_t *caps, const struct event_timer_adapter_ops **ops)
@@ -879,8 +862,8 @@ ssovf_vdev_probe(struct rte_vdev_device *vdev)
 				"Ignoring unsupported params supplied '%s'",
 				name);
 		} else {
-			ret = rte_kvargs_process(kvlist, TIMVF_ENABLE_STATS_ARG,
-						 ssovf_parsekv,
+			ret = rte_kvargs_process_opt(kvlist, TIMVF_ENABLE_STATS_ARG,
+						     rte_kvargs_handle_bool,
 						 &timvf_enable_stats);
 			if (ret != 0) {
 				ssovf_log_err("%s: Error in timvf stats", name);
Signature · 1 line
2.53.0
#189 lore raw

PATCH v3 55/65 baseband/turbo_sw: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_u16_arg() never checked the end pointer, so a value such as "8junk" was accepted, and it used base 0, so a leading zero silently selected octal.

It was also used for both device arguments, but only queues_num is a uint16_t; socket_id is an int, so parsing it wrote two bytes into a four byte object and left the rest uninitialised.

The handler did nothing beyond a range checked store, so drop it and pass rte_kvargs_handle_u16() and rte_kvargs_handle_socket_id() directly, each matching the width of the field it fills.

The open coded range check only tested the upper bound, so a negative socket id such as -5 was accepted and used. The helper checks both ends, accepting -1, which is SOCKET_ID_ANY, through RTE_MAX_NUMA_NODES - 1, so the check is now redundant and is removed.

---
 .../baseband/turbo_sw/bbdev_turbo_software.c  | 29 ++-----------------
 1 file changed, 2 insertions(+), 27 deletions(-)
drivers/baseband/turbo_sw/bbdev_turbo_software.c +2 -27
index 2cab495294..d6801d9123 100644
@@ -1835,25 +1835,6 @@ dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
 	return nb_dequeued;
 }
 
-/* Parse 16bit integer from string argument */
-static inline int
-parse_u16_arg(const char *key, const char *value, void *extra_args)
-{
-	uint16_t *u16 = extra_args;
-	unsigned int long result;
-
-	if ((value == NULL) || (extra_args == NULL))
-		return -EINVAL;
-	errno = 0;
-	result = strtoul(value, NULL, 0);
-	if ((result >= (1 << 16)) || (errno != 0)) {
-		rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
-		return -ERANGE;
-	}
-	*u16 = (uint16_t)result;
-	return 0;
-}
-
 /* Parse parameters used to create device */
 static int
 parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
@@ -1869,20 +1850,14 @@ parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
 			return -EFAULT;
 
 		ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[0],
-					&parse_u16_arg, &params->queues_num);
+					rte_kvargs_handle_u16, &params->queues_num);
 		if (ret < 0)
 			goto exit;
 
 		ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[1],
-					&parse_u16_arg, &params->socket_id);
+					rte_kvargs_handle_socket_id, &params->socket_id);
 		if (ret < 0)
 			goto exit;
-
-		if (params->socket_id >= RTE_MAX_NUMA_NODES) {
-			rte_bbdev_log(ERR, "Invalid socket, must be < %u",
-					RTE_MAX_NUMA_NODES);
-			goto exit;
-		}
 	}
 
 exit:
Signature · 1 line
2.53.0
#190 lore raw

PATCH v3 56/65 baseband/la12xx: use kvargs numeric helpers

In reply to Stephen Hemminger

parse_u16_arg() stored through a uint16_t pointer and parse_integer_arg() through a uint32_t pointer, but both device arguments are 8 bit fields:
queues_num is a uint8_t and modem_id an int8_t, and they are adjacent in struct bbdev_la12xx_params. Parsing either argument therefore wrote past the field and corrupted its neighbour.

parse_u16_arg() did nothing beyond a range checked store, so drop it and pass rte_kvargs_handle_u8() directly, which also gives queues_num the width it actually has.

The modem id keeps a handler of its own. parse_integer_arg() rejected both ends of the range, and a plain rte_kvargs_handle_i8() would accept anything down to -128 while the caller only checks the upper bound, so "modem=-5" would be stored and only fail later in open_ipc_dev(). The new handler bounds it to -1..LA12XX_MAX_MODEM-1, keeping -1 which la12xx_bbdev_create() uses to mean "derive the modem id from the device id", and replaces the caller's upper bound check.

The handler now rejects an out of range modem id, where the old check did a "goto exit" with ret still zero. The probe also checks the result of parse_bbdev_la12xx_params(), which it used to discard, so a bad modem id or queue count now fails the probe instead of only being logged.

Fixes ee36ba0f3042 ("baseband/la12xx: add devargs option for max queues")
---
 drivers/baseband/la12xx/bbdev_la12xx.c | 60 +++++++++-----------------
 1 file changed, 20 insertions(+), 40 deletions(-)
drivers/baseband/la12xx/bbdev_la12xx.c +20 -40
index 1116dacc4b..b9199189e7 100644
@@ -912,44 +912,25 @@ setup_la12xx_dev(struct rte_bbdev *dev)
 	return ret;
 }
 
-static inline int
-parse_u16_arg(const char *key, const char *value, void *extra_args)
-{
-	uint16_t *u16 = extra_args;
-
-	uint64_t result;
-	if ((value == NULL) || (extra_args == NULL))
-		return -EINVAL;
-	errno = 0;
-	result = strtoul(value, NULL, 0);
-	if ((result >= (1 << 16)) || (errno != 0)) {
-		rte_bbdev_log(ERR, "Invalid value %" PRIu64 " for %s",
-			      result, key);
-		return -ERANGE;
-	}
-	*u16 = (uint16_t)result;
-	return 0;
-}
-
-/* Parse integer from integer argument */
+/*
+ * Parse the modem id.
+ *
+ * A dedicated handler is needed because -1 is meaningful here: it leaves the
+ * modem id unset, and la12xx_bbdev_create() then derives it from the device
+ * id. rte_kvargs_handle_i8() alone would also accept anything down to -128.
+ */
 static int
-parse_integer_arg(const char *key __rte_unused,
-		const char *value, void *extra_args)
+parse_modem_id(const char *key, const char *value, void *extra_args)
 {
-	int i;
-	char *end;
-
-	errno = 0;
+	int64_t modem_id;
 
-	i = strtol(value, &end, 10);
-	if (*end != 0 || errno != 0 || i < 0 || i > LA12XX_MAX_MODEM) {
-		rte_bbdev_log(ERR, "Supported Port IDS are 0 to %d",
-			LA12XX_MAX_MODEM - 1);
+	if (rte_kvargs_to_int(value, -1, LA12XX_MAX_MODEM - 1, &modem_id) < 0) {
+		rte_bbdev_log(ERR, "Invalid %s, must be -1..%u", key,
+				LA12XX_MAX_MODEM - 1);
 		return -EINVAL;
 	}
 
-	*((uint32_t *)extra_args) = i;
-
+	*(int8_t *)extra_args = modem_id;
 	return 0;
 }
 
@@ -970,20 +951,16 @@ parse_bbdev_la12xx_params(struct bbdev_la12xx_params *params,
 			return -EFAULT;
 
 		ret = rte_kvargs_process(kvlist, bbdev_la12xx_valid_params[0],
-					&parse_u16_arg, &params->queues_num);
+					rte_kvargs_handle_u8, &params->queues_num);
 		if (ret < 0)
 			goto exit;
 
 		ret = rte_kvargs_process(kvlist,
 					bbdev_la12xx_valid_params[1],
-					&parse_integer_arg,
+					parse_modem_id,
 					&params->modem_id);
-
-		if (params->modem_id >= LA12XX_MAX_MODEM) {
-			rte_bbdev_log(ERR, "Invalid modem id, must be < %u",
-					LA12XX_MAX_MODEM);
+		if (ret < 0)
 			goto exit;
-		}
 	}
 
 exit:
@@ -1062,6 +1039,7 @@ la12xx_bbdev_probe(struct rte_vdev_device *vdev)
 	};
 	const char *name;
 	const char *input_args;
+	int ret;
 
 	PMD_INIT_FUNC_TRACE();
 
@@ -1073,7 +1051,9 @@ la12xx_bbdev_probe(struct rte_vdev_device *vdev)
 		return -EINVAL;
 
 	input_args = rte_vdev_device_args(vdev);
-	parse_bbdev_la12xx_params(&init_params, input_args);
+	ret = parse_bbdev_la12xx_params(&init_params, input_args);
+	if (ret < 0)
+		return ret;
 
 	return la12xx_bbdev_create(vdev, &init_params);
 }
Signature · 1 line
2.53.0
#191 lore raw

PATCH v3 57/65 dma/hisi_acc: use kvargs numeric helpers

In reply to Stephen Hemminger

hacc_dma_parse_queues() open codes the errno and end pointer checks the helper performs. A queue count of zero is rejected by making one the minimum of the accepted range, as before.

---
 drivers/dma/hisi_acc/hisi_acc_dmadev.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/dma/hisi_acc/hisi_acc_dmadev.c +1 -4
index 196aaadc05..7db93852a3 100644
@@ -660,13 +660,10 @@ hacc_dma_parse_queues(const char *key, const char *value, void *extra_args)
 {
 	struct hacc_dma_config *config = extra_args;
 	uint64_t val;
-	char *end;
 
 	RTE_SET_USED(key);
 
-	errno = 0;
-	val = strtoull(value, &end, 0);
-	if (errno == ERANGE || value == end || *end != '\0' || val == 0) {
+	if (rte_kvargs_to_uint(value, 1, UINT64_MAX, &val) < 0) {
 		HACC_DMA_LOG(ERR, "%s invalid queues! set to default one queue!",
 			    config->dev->name);
 		config->queues = HACC_DMA_DEFAULT_QUEUES;
Signature · 1 line
2.53.0
#192 lore raw

PATCH v3 58/65 crypto/virtio: use kvargs numeric helpers

In reply to Stephen Hemminger

get_integer_arg() never checks the end pointer and returns -errno, so a malformed value such as "abc" leaves errno untouched, converts to zero and is reported as success. It also used base 0, so a leading zero silently selected octal.

---
 drivers/crypto/virtio/virtio_user_cryptodev.c | 21 ++-----------------
 1 file changed, 2 insertions(+), 19 deletions(-)
drivers/crypto/virtio/virtio_user_cryptodev.c +2 -19
index 4daa188e1d..9844debeaf 100644
@@ -345,23 +345,6 @@ get_string_arg(const char *key __rte_unused,
 	return 0;
 }
 
-static int
-get_integer_arg(const char *key __rte_unused,
-		const char *value, void *extra_args)
-{
-	uint64_t integer = 0;
-	if (!value || !extra_args)
-		return -EINVAL;
-	errno = 0;
-	integer = strtoull(value, NULL, 0);
-	/* extra_args keeps default value, it should be replaced
-	 * only in case of successful parsing of the 'value' arg
-	 */
-	if (errno == 0)
-		*(uint64_t *)extra_args = integer;
-	return -errno;
-}
-
 static struct rte_cryptodev *
 virtio_user_cryptodev_alloc(struct rte_vdev_device *vdev)
 {
@@ -432,7 +415,7 @@ virtio_user_pmd_probe(struct rte_vdev_device *vdev)
 
 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
-					&get_integer_arg, &queues) < 0) {
+					rte_kvargs_handle_u64, &queues) < 0) {
 			PMD_INIT_LOG(ERR, "error to parse %s",
 					VIRTIO_USER_ARG_QUEUES_NUM);
 			goto end;
@@ -441,7 +424,7 @@ virtio_user_pmd_probe(struct rte_vdev_device *vdev)
 
 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
-					&get_integer_arg, &queue_size) < 0) {
+					rte_kvargs_handle_u64, &queue_size) < 0) {
 			PMD_INIT_LOG(ERR, "error to parse %s",
 					VIRTIO_USER_ARG_QUEUE_SIZE);
 			goto end;
Signature · 1 line
2.53.0
#193 lore raw

PATCH v3 59/65 net/ice: use kvargs hex helper for debug mask

In reply to Stephen Hemminger

parse_u64() checks errno but not the end pointer, so "hw_debug_mask=junk" is silently accepted as zero, and a trailing-garbage value such as "0x80zz" is taken as 0x80.

The value is documented as a hexadecimal mask, so use rte_kvargs_handle_hex64(), which reads it as hexadecimal with or without a 0x prefix and validates the whole string. The documented form "hw_debug_mask=0x80" is unchanged, and so is a bare "80".

---
 drivers/net/intel/ice/ice_ethdev.c | 21 +--------------------
 1 file changed, 1 insertion(+), 20 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +1 -20
index 4c3db27812..a1ed6b5b6f 100644
@@ -2144,25 +2144,6 @@ ice_base_queue_get(struct ice_pf *pf)
 	}
 }
 
-static int
-parse_u64(const char *key, const char *value, void *args)
-{
-	u64 *num = (u64 *)args;
-	u64 tmp;
-
-	errno = 0;
-	tmp = strtoull(value, NULL, 16);
-	if (errno) {
-		PMD_DRV_LOG(WARNING, "%s: \"%s\" is not a valid u64",
-			    key, value);
-		return -1;
-	}
-
-	*num = tmp;
-
-	return 0;
-}
-
 static int
 parse_tx_sched_levels(const char *key, const char *value, void *args)
 {
@@ -2427,7 +2408,7 @@ static int ice_parse_devargs(struct rte_eth_dev *dev)
 		goto bail;
 
 	ret = rte_kvargs_process(kvlist, ICE_HW_DEBUG_MASK_ARG,
-				 &parse_u64, &ad->hw.debug_mask);
+				 rte_kvargs_handle_hex64, &ad->hw.debug_mask);
 	if (ret)
 		goto bail;
 
Signature · 1 line
2.53.0
#194 lore raw

PATCH v3 60/65 net/hns3: use kvargs hex helper for capability mask

In reply to Stephen Hemminger

hns3_parse_dev_caps_mask() does no validation whatsoever: strtoull() is called with neither errno cleared nor the end pointer checked, so "dev_caps_mask=junk" is silently taken as zero and "dev_caps_mask=-1" wraps around to enable every capability.

The value is documented as a hexadecimal mask, so use rte_kvargs_handle_hex64(), which reads it as hexadecimal with or without a 0x prefix. The documented form "dev_caps_mask=0xF" is unchanged, and so is a bare "F".

Note that hns3_parse_devargs() discards the rte_kvargs_process() return value for every argument it parses, so a malformed mask is now reported but still leaves the default of zero in place rather than failing probe. Propagating those errors is a behaviour change per argument and is left for a separate patch.

HNS3_CONVERT_TO_HEXADECIMAL loses its last user here, and HNS3_CONVERT_TO_DECIMAL lost its own earlier in this series, so remove both.

---
 drivers/net/hns3/hns3_common.c | 18 +-----------------
 drivers/net/hns3/hns3_common.h |  3 ---
 2 files changed, 1 insertion(+), 20 deletions(-)
drivers/net/hns3/hns3_common.c +1 -17
index c21740ba5e..35cc3fca40 100644
@@ -205,22 +205,6 @@ hns3_get_io_hint_func_name(uint32_t hint)
 	}
 }
 
-static int
-hns3_parse_dev_caps_mask(const char *key, const char *value, void *extra_args)
-{
-	uint64_t val;
-
-	RTE_SET_USED(key);
-
-	if (value == NULL || extra_args == NULL)
-		return 0;
-
-	val = strtoull(value, NULL, HNS3_CONVERT_TO_HEXADECIMAL);
-	*(uint64_t *)extra_args = val;
-
-	return 0;
-}
-
 static int
 hns3_parse_mbx_time_limit(const char *key, const char *value, void *extra_args)
 {
@@ -341,7 +325,7 @@ hns3_parse_devargs(struct rte_eth_dev *dev)
 	(void)rte_kvargs_process(kvlist, HNS3_DEVARG_TX_FUNC_HINT,
 			   &hns3_parse_io_hint_func, &tx_func_hint);
 	(void)rte_kvargs_process(kvlist, HNS3_DEVARG_DEV_CAPS_MASK,
-			   &hns3_parse_dev_caps_mask, &dev_caps_mask);
+			   rte_kvargs_handle_hex64, &dev_caps_mask);
 	(void)rte_kvargs_process(kvlist, HNS3_DEVARG_MBX_TIME_LIMIT_MS,
 			   &hns3_parse_mbx_time_limit, &mbx_time_limit_ms);
 	if (!hns->is_vf) {
drivers/net/hns3/hns3_common.h +0 -3
index 7b3f96b01a..967cde9e39 100644
@@ -9,9 +9,6 @@
 
 #include "hns3_ethdev.h"
 
-#define HNS3_CONVERT_TO_DECIMAL 10
-#define HNS3_CONVERT_TO_HEXADECIMAL 16
-
 enum {
 	HNS3_IO_FUNC_HINT_NONE = 0,
 	HNS3_IO_FUNC_HINT_VEC,
Signature · 1 line
2.53.0
Acked-by on v3 60/65
#195 lore raw

Re: [PATCH v3 60/65] net/hns3: use kvargs hex helper for capability mask

Quoted 21 lines · Stephen Hemminger

hns3_parse_dev_caps_mask() does no validation whatsoever: strtoull() is called with neither errno cleared nor the end pointer checked, so "dev_caps_mask=junk" is silently taken as zero and "dev_caps_mask=-1" wraps around to enable every capability.

The value is documented as a hexadecimal mask, so use rte_kvargs_handle_hex64(), which reads it as hexadecimal with or without a 0x prefix. The documented form "dev_caps_mask=0xF" is unchanged, and so is a bare "F".

Note that hns3_parse_devargs() discards the rte_kvargs_process() return value for every argument it parses, so a malformed mask is now reported but still leaves the default of zero in place rather than failing probe. Propagating those errors is a behaviour change per argument and is left for a separate patch.

HNS3_CONVERT_TO_HEXADECIMAL loses its last user here, and HNS3_CONVERT_TO_DECIMAL lost its own earlier in this series, so remove both.

#196 lore raw

PATCH v3 61/65 net/cxgbe: use kvargs numeric helpers

In reply to Stephen Hemminger

check_devargs_handler() is a single handler shared by all five device arguments, which dispatches on the key to decide how to parse the value.

The dispatch is done with strncmp(key, NAME, strlen(key)), which compares only as many bytes as the key given by the user is long, so a short key matches a longer name by prefix. That is harmless today because rte_kvargs_process() has already selected the pairs by exact key, but it means the handler cannot tell which argument it was called for except by accident.

The filtermode and filtermask branch checks errno without clearing it first, so a stale ERANGE from unrelated earlier code rejects a valid mask. The boolean branch tests only for "1" and silently ignores anything else, so "keep_ovlan=yes" leaves the default in place without complaint.

Pass the handler to cxgbe_get_devargs() instead of having one handler work out what it was called for: rte_kvargs_handle_hex32() for the two masks, which are documented as hexadecimal, and rte_kvargs_handle_bool() for the three flags.

The masks are still read as hexadecimal with or without a 0x prefix, so "filtermode=44" is unchanged. The flags now also accept "y", "yes", "on" and "true" as well as "1", and a value which is not a boolean at all is rejected rather than ignored.

---
 drivers/net/cxgbe/cxgbe_main.c | 38 ++++++----------------------------
 1 file changed, 6 insertions(+), 32 deletions(-)
drivers/net/cxgbe/cxgbe_main.c +6 -32
index 2ed21f2d66..e0eefae6c1 100644
@@ -744,36 +744,8 @@ void cxgbe_print_port_info(struct adapter *adap)
 	}
 }
 
-static int check_devargs_handler(const char *key, const char *value, void *p)
-{
-	if (!strncmp(key, CXGBE_DEVARG_CMN_KEEP_OVLAN, strlen(key)) ||
-	    !strncmp(key, CXGBE_DEVARG_CMN_TX_MODE_LATENCY, strlen(key)) ||
-	    !strncmp(key, CXGBE_DEVARG_VF_FORCE_LINK_UP, strlen(key))) {
-		if (!strncmp(value, "1", 1)) {
-			bool *dst_val = (bool *)p;
-
-			*dst_val = true;
-		}
-	}
-
-	if (!strncmp(key, CXGBE_DEVARG_PF_FILTER_MODE, strlen(key)) ||
-	    !strncmp(key, CXGBE_DEVARG_PF_FILTER_MASK, strlen(key))) {
-		u32 *dst_val = (u32 *)p;
-		char *endptr = NULL;
-		u32 arg_val;
-
-		arg_val = strtoul(value, &endptr, 16);
-		if (errno || endptr == value)
-			return -EINVAL;
-
-		*dst_val = arg_val;
-	}
-
-	return 0;
-}
-
 static int cxgbe_get_devargs(struct rte_devargs *devargs, const char *key,
-			     void *p)
+			     arg_handler_t handler, void *p)
 {
 	struct rte_kvargs *kvlist;
 	int ret = 0;
@@ -788,7 +760,7 @@ static int cxgbe_get_devargs(struct rte_devargs *devargs, const char *key,
 	if (!rte_kvargs_count(kvlist, key))
 		goto out;
 
-	ret = rte_kvargs_process(kvlist, key, check_devargs_handler, p);
+	ret = rte_kvargs_process(kvlist, key, handler, p);
 
 out:
 	rte_kvargs_free(kvlist);
@@ -807,7 +779,8 @@ static void cxgbe_get_devargs_int(struct adapter *adap, bool *dst,
 	if (!pdev)
 		return;
 
-	ret = cxgbe_get_devargs(pdev->device.devargs, key, &devarg_value);
+	ret = cxgbe_get_devargs(pdev->device.devargs, key,
+				rte_kvargs_handle_bool, &devarg_value);
 	if (ret)
 		return;
 
@@ -825,7 +798,8 @@ static void cxgbe_get_devargs_u32(struct adapter *adap, u32 *dst,
 	if (!pdev)
 		return;
 
-	ret = cxgbe_get_devargs(pdev->device.devargs, key, &devarg_value);
+	ret = cxgbe_get_devargs(pdev->device.devargs, key,
+				rte_kvargs_handle_hex32, &devarg_value);
 	if (ret)
 		return;
 
Signature · 1 line
2.53.0
#197 lore raw

PATCH v3 62/65 kvargs: add range conversion handlers

In reply to Stephen Hemminger

An argument whose valid range is narrower than the type it is stored in cannot use the typed handlers, so drivers write a handler of their own which calls rte_kvargs_to_uint() and stores the result. There are about 25 of these.

They do not report the same way. Of the call sites, 22 reject a bad value with no message at all, so nothing tells the user what the range was. Those which do log one each word it differently:

net/af_xdp: "Invalid busy_budget, must be >= 0 and <= %u"
dma/skeleton: "Invalid %s, must be 0..%u"
net/bnxt: "Invalid parameter passed to rep_q_r2f devargs."

Add handlers which take the bounds from the caller:

rte_kvargs_handle_urange
rte_kvargs_handle_irange

A handler has only one opaque pointer, so the bounds are passed and the result returned through a structure. As the typed handlers, the result is written only on success, so seeding it with the default leaves it alone when the key is absent and when the value is rejected.

A value outside the range is now reported as one which does not fit a whole type already is, naming the key and the bounds.

kvargs_store_uint() grows a minimum to go with the maximum it already had; the typed handlers pass zero.

---
 app/test/test_kvargs.c                 | 43 ++++++++++++++++++
 doc/guides/rel_notes/release_26_11.rst |  6 +++
 lib/kvargs/rte_kvargs.c                | 62 +++++++++++++++++++++-----
 lib/kvargs/rte_kvargs.h                | 57 +++++++++++++++++++++++
 4 files changed, 157 insertions(+), 11 deletions(-)
app/test/test_kvargs.c +43 -0
index 6e84ebf35c..12e1376e86 100644
@@ -619,6 +619,48 @@ test_kvargs_to_range(void)
 	return TEST_SUCCESS;
 }
 
+static int
+test_handle_range(void)
+{
+	struct rte_kvargs_urange u = { .min = 1, .max = 10, .val = 7 };
+	struct rte_kvargs_irange s = { .min = -10, .max = 10, .val = -7 };
+	struct rte_kvargs *kvlist;
+
+	TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_urange, "10", &u),
+			    "10 rejected in [1,10]");
+	TEST_ASSERT_EQUAL(u.val, 10U, "wrong value");
+
+	/* Out of range and malformed values leave the result alone. */
+	TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_urange, "11", &u),
+			 "11 accepted in [1,10]");
+	TEST_ASSERT_EQUAL(u.val, 10U, "target clobbered above the range");
+	TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_urange, "0", &u),
+			 "0 accepted in [1,10]");
+	TEST_ASSERT_EQUAL(u.val, 10U, "target clobbered below the range");
+	TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_urange, "x", &u),
+			 "\"x\" accepted");
+	TEST_ASSERT_EQUAL(u.val, 10U, "target clobbered on a bad value");
+
+	TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_irange, "-10", &s),
+			    "-10 rejected in [-10,10]");
+	TEST_ASSERT_EQUAL(s.val, -10, "wrong value");
+	TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_irange, "-11", &s),
+			 "-11 accepted in [-10,10]");
+	TEST_ASSERT_EQUAL(s.val, -10, "target clobbered below the range");
+
+	/* A key which is not present leaves the seeded default alone. */
+	u.val = 7;
+	kvlist = rte_kvargs_parse("other=1", NULL);
+	TEST_ASSERT_NOT_NULL(kvlist, "parse failed");
+	TEST_ASSERT_SUCCESS(rte_kvargs_process(kvlist, "k",
+					       rte_kvargs_handle_urange, &u),
+			    "processing an absent key failed");
+	rte_kvargs_free(kvlist);
+	TEST_ASSERT_EQUAL(u.val, 7U, "default lost when the key is absent");
+
+	return TEST_SUCCESS;
+}
+
 static struct unit_test_suite kvargs_test_suite  = {
 	.suite_name = "Kvargs Unit Test Suite",
 	.setup = NULL,
@@ -651,6 +693,7 @@ static struct unit_test_suite kvargs_test_suite  = {
 		TEST_CASE(test_handle_bool),
 		TEST_CASE(test_handle_socket_id),
 		TEST_CASE(test_kvargs_to_range),
+		TEST_CASE(test_handle_range),
 		TEST_CASES_END() /**< NULL terminate unit test array */
 	}
 };
doc/guides/rel_notes/release_26_11.rst +6 -0
index 27b3fe4253..e2cab9a484 100644
@@ -164,6 +164,12 @@ New Features
   ``rte_kvargs_to_hex`` for the cases where a driver needs a narrower range
   than the target type allows.
 
+  Added ``rte_kvargs_handle_urange`` and ``rte_kvargs_handle_irange``,
+  which take the bounds and return the result through a
+  ``struct rte_kvargs_urange`` or ``struct rte_kvargs_irange``. An
+  argument whose valid range is narrower than its storage type no longer
+  needs a handler of its own, and a value outside the range is reported
+  as one which does not fit a whole type already is.
 
 Removed Items
 -------------
lib/kvargs/rte_kvargs.c +51 -11

120 lines not on this page · raw message

lib/kvargs/rte_kvargs.h +57 -0

63 lines not on this page · raw message

Signature · 1 line
2.53.0
#198 lore raw

PATCH v3 63/65 event/sw: use kvargs range handlers

In reply to Stephen Hemminger

The handlers for sched_quanta, credit_quanta, min_burst and deq_burst each applied a range and stored the result, and returned -1 without logging, so a value out of range was rejected with nothing to say what the range was.

Use rte_kvargs_handle_urange(), which reports it. The per key SW_LOG_ERR is kept since it names the device, which kvargs cannot.

---
 drivers/event/sw/sw_evdev.c | 89 +++++++++----------------------------
 1 file changed, 21 insertions(+), 68 deletions(-)
drivers/event/sw/sw_evdev.c +21 -68
index fa9fe3e4c2..30c6111868 100644
@@ -872,58 +872,6 @@ sw_close(struct rte_eventdev *dev)
 	return 0;
 }
 
-static int
-set_sched_quanta(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *quanta = opaque;
-	uint64_t val;
-
-	if (rte_kvargs_to_uint(value, 0, 4095, &val) < 0)
-		return -1;
-
-	*quanta = val;
-	return 0;
-}
-
-static int
-set_credit_quanta(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *credit = opaque;
-	uint64_t val;
-
-	if (rte_kvargs_to_uint(value, 0, 127, &val) < 0)
-		return -1;
-
-	*credit = val;
-	return 0;
-}
-
-static int
-set_deq_burst_sz(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *deq_burst_sz = opaque;
-	uint64_t val;
-
-	if (rte_kvargs_to_uint(value, 0, SCHED_DEQUEUE_MAX_BURST_SIZE, &val) < 0)
-		return -1;
-
-	*deq_burst_sz = val;
-	return 0;
-}
-
-static int
-set_min_burst_sz(const char *key __rte_unused, const char *value, void *opaque)
-{
-	int *min_burst_sz = opaque;
-	uint64_t val;
-
-	if (rte_kvargs_to_uint(value, 0, SCHED_DEQUEUE_MAX_BURST_SIZE, &val) < 0)
-		return -1;
-
-	*min_burst_sz = val;
-	return 0;
-}
-
 static int32_t sw_sched_service_func(void *args)
 {
 	struct rte_eventdev *dev = args;
@@ -979,10 +927,15 @@ sw_probe(struct rte_vdev_device *vdev)
 	struct rte_eventdev *dev;
 	struct sw_evdev *sw;
 	int socket_id = rte_socket_id();
-	int sched_quanta  = SW_DEFAULT_SCHED_QUANTA;
-	int credit_quanta = SW_DEFAULT_CREDIT_QUANTA;
-	int min_burst_size = 1;
-	int deq_burst_size = SCHED_DEQUEUE_DEFAULT_BURST_SIZE;
+	struct rte_kvargs_urange sched_quanta = {
+		.min = 0, .max = 4095, .val = SW_DEFAULT_SCHED_QUANTA };
+	struct rte_kvargs_urange credit_quanta = {
+		.min = 0, .max = 127, .val = SW_DEFAULT_CREDIT_QUANTA };
+	struct rte_kvargs_urange min_burst_size = {
+		.min = 0, .max = SCHED_DEQUEUE_MAX_BURST_SIZE, .val = 1 };
+	struct rte_kvargs_urange deq_burst_size = {
+		.min = 0, .max = SCHED_DEQUEUE_MAX_BURST_SIZE,
+		.val = SCHED_DEQUEUE_DEFAULT_BURST_SIZE };
 	bool refill_once = false;
 
 	name = rte_vdev_device_name(vdev);
@@ -1006,7 +959,7 @@ sw_probe(struct rte_vdev_device *vdev)
 			}
 
 			ret = rte_kvargs_process(kvlist, SCHED_QUANTA_ARG,
-					set_sched_quanta, &sched_quanta);
+					rte_kvargs_handle_urange, &sched_quanta);
 			if (ret != 0) {
 				SW_LOG_ERR(
 					"%s: Error parsing sched quanta parameter",
@@ -1016,7 +969,7 @@ sw_probe(struct rte_vdev_device *vdev)
 			}
 
 			ret = rte_kvargs_process(kvlist, CREDIT_QUANTA_ARG,
-					set_credit_quanta, &credit_quanta);
+					rte_kvargs_handle_urange, &credit_quanta);
 			if (ret != 0) {
 				SW_LOG_ERR(
 					"%s: Error parsing credit quanta parameter",
@@ -1026,7 +979,7 @@ sw_probe(struct rte_vdev_device *vdev)
 			}
 
 			ret = rte_kvargs_process(kvlist, MIN_BURST_SIZE_ARG,
-					set_min_burst_sz, &min_burst_size);
+					rte_kvargs_handle_urange, &min_burst_size);
 			if (ret != 0) {
 				SW_LOG_ERR(
 					"%s: Error parsing minimum burst size parameter",
@@ -1036,7 +989,7 @@ sw_probe(struct rte_vdev_device *vdev)
 			}
 
 			ret = rte_kvargs_process(kvlist, DEQ_BURST_SIZE_ARG,
-					set_deq_burst_sz, &deq_burst_size);
+					rte_kvargs_handle_urange, &deq_burst_size);
 			if (ret != 0) {
 				SW_LOG_ERR(
 					"%s: Error parsing dequeue burst size parameter",
@@ -1061,10 +1014,10 @@ sw_probe(struct rte_vdev_device *vdev)
 
 	SW_LOG_INFO(
 			"Creating eventdev sw device %s, numa_node=%d, "
-			"sched_quanta=%d, credit_quanta=%d "
-			"min_burst=%d, deq_burst=%d, refill_once=%d",
-			name, socket_id, sched_quanta, credit_quanta,
-			min_burst_size, deq_burst_size, refill_once);
+			"sched_quanta=%"PRIu64", credit_quanta=%"PRIu64" "
+			"min_burst=%"PRIu64", deq_burst=%"PRIu64", refill_once=%d",
+			name, socket_id, sched_quanta.val, credit_quanta.val,
+			min_burst_size.val, deq_burst_size.val, refill_once);
 
 	dev = rte_event_pmd_vdev_init(name,
 			sizeof(struct sw_evdev), socket_id, vdev);
@@ -1085,10 +1038,10 @@ sw_probe(struct rte_vdev_device *vdev)
 	sw->data = dev->data;
 
 	/* copy values passed from vdev command line to instance */
-	sw->credit_update_quanta = credit_quanta;
-	sw->sched_quanta = sched_quanta;
-	sw->sched_min_burst_size = min_burst_size;
-	sw->sched_deq_burst_size = deq_burst_size;
+	sw->credit_update_quanta = credit_quanta.val;
+	sw->sched_quanta = sched_quanta.val;
+	sw->sched_min_burst_size = min_burst_size.val;
+	sw->sched_deq_burst_size = deq_burst_size.val;
 	sw->refill_once_per_iter = refill_once;
 
 	/* register service with EAL */
Signature · 1 line
2.53.0
#199 lore raw

PATCH v3 64/65 net/cnxk: use kvargs range handlers

In reply to Stephen Hemminger

Five of the six handlers here applied a range and stored the result, and returned -EINVAL without logging, so a value out of range was rejected with nothing to say what the range was.

Use rte_kvargs_handle_urange() for those five. parse_reta_size() keeps its handler since it rounds the value up to a supported table size rather than rejecting it.

---
 drivers/net/cnxk/cnxk_ethdev_devargs.c | 112 +++++--------------------
 1 file changed, 21 insertions(+), 91 deletions(-)
drivers/net/cnxk/cnxk_ethdev_devargs.c +21 -91
index 887349fe24..18472d54b8 100644
@@ -21,67 +21,6 @@ struct flow_pre_l2_size_info {
 	uint8_t pre_l2_size_shift_dir;
 };
 
-static int
-parse_outb_nb_crypto_qs(const char *key, const char *value, void *extra_args)
-{
-	uint64_t val;
-
-	RTE_SET_USED(key);
-
-	if (rte_kvargs_to_uint(value, 1, 64, &val) < 0)
-		return -EINVAL;
-
-	*(uint16_t *)extra_args = val;
-
-	return 0;
-}
-
-static int
-parse_rxc_step(const char *key, const char *value, void *extra_args)
-{
-	uint64_t val;
-
-	RTE_SET_USED(key);
-
-	if (rte_kvargs_to_uint(value, 0, ROC_NIX_INL_REAS_STEP_MAX, &val) < 0)
-		return -EINVAL;
-
-	*(uint32_t *)extra_args = val;
-
-	return 0;
-}
-
-static int
-parse_flow_max_priority(const char *key, const char *value, void *extra_args)
-{
-	uint64_t val;
-
-	RTE_SET_USED(key);
-
-	if (rte_kvargs_to_uint(value, 1, ROC_NPC_MAX_MCAM_PRIORITY, &val) < 0)
-		return -EINVAL;
-
-	*(uint16_t *)extra_args = val;
-
-	return 0;
-}
-
-static int
-parse_flow_prealloc_size(const char *key, const char *value, void *extra_args)
-{
-	uint64_t val;
-
-	RTE_SET_USED(key);
-
-	/* Limit the prealloc size to 32 */
-	if (rte_kvargs_to_uint(value, 1, 32, &val) < 0)
-		return -EINVAL;
-
-	*(uint16_t *)extra_args = val;
-
-	return 0;
-}
-
 static int
 parse_reta_size(const char *key, const char *value, void *extra_args)
 {
@@ -158,21 +97,6 @@ parse_switch_header_type(const char *key, const char *value, void *extra_args)
 	return 0;
 }
 
-static int
-parse_skip_size_info(const char *key, const char *value, void *extra_args)
-{
-	uint64_t val;
-
-	RTE_SET_USED(key);
-
-	if (rte_kvargs_to_uint(value, 0, 255, &val) < 0)
-		return -EINVAL;
-
-	*(uint16_t *)extra_args = val;
-
-	return 0;
-}
-
 static int
 parse_sdp_channel_mask(const char *key, const char *value, void *extra_args)
 {
@@ -234,11 +158,15 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	uint16_t sqb_slack = ROC_NIX_SQB_SLACK;
 	uint32_t ipsec_out_max_sa = BIT(12);
 	bool custom_meta_aura_dis = false;
-	uint16_t flow_prealloc_size = 1;
+	struct rte_kvargs_urange flow_prealloc_size = {
+		.min = 1, .max = 32, .val = 1 };
 	uint16_t switch_header_type = 0;
-	uint16_t skip_size_info = 0;
-	uint16_t flow_max_priority = 3;
-	uint16_t outb_nb_crypto_qs = 1;
+	struct rte_kvargs_urange skip_size_info = {
+		.min = 0, .max = 255, .val = 0 };
+	struct rte_kvargs_urange flow_max_priority = {
+		.min = 1, .max = ROC_NPC_MAX_MCAM_PRIORITY, .val = 3 };
+	struct rte_kvargs_urange outb_nb_crypto_qs = {
+		.min = 1, .max = 64, .val = 1 };
 	uint32_t ipsec_in_min_spi = 0;
 	uint16_t outb_nb_desc = 8200;
 	struct sdp_channel sdp_chan;
@@ -254,7 +182,8 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	bool lock_rx_ctx = false;
 	bool rx_inj_ena = false;
 	bool no_inl_dev = false;
-	uint32_t rxc_step = 0;
+	struct rte_kvargs_urange rxc_step = {
+		.min = 0, .max = ROC_NIX_INL_REAS_STEP_MAX, .val = 0 };
 	int ret;
 
 	memset(&sdp_chan, 0, sizeof(sdp_chan));
@@ -277,9 +206,9 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	ret |= rte_kvargs_process(kvlist, CNXK_MAX_SQB_COUNT, rte_kvargs_handle_u16,
 				  &sqb_count);
 	ret |= rte_kvargs_process(kvlist, CNXK_FLOW_PREALLOC_SIZE,
-				  &parse_flow_prealloc_size, &flow_prealloc_size);
+				  rte_kvargs_handle_urange, &flow_prealloc_size);
 	ret |= rte_kvargs_process(kvlist, CNXK_FLOW_MAX_PRIORITY,
-				  &parse_flow_max_priority, &flow_max_priority);
+				  rte_kvargs_handle_urange, &flow_max_priority);
 	ret |= rte_kvargs_process(kvlist, CNXK_SWITCH_HEADER_TYPE,
 				  &parse_switch_header_type, &switch_header_type);
 	ret |= rte_kvargs_process_opt(kvlist, CNXK_RSS_TAG_AS_XOR, rte_kvargs_handle_bool,
@@ -295,7 +224,7 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	ret |= rte_kvargs_process(kvlist, CNXK_OUTB_NB_DESC, rte_kvargs_handle_u16,
 				  &outb_nb_desc);
 	ret |= rte_kvargs_process(kvlist, CNXK_OUTB_NB_CRYPTO_QS,
-				  &parse_outb_nb_crypto_qs, &outb_nb_crypto_qs);
+				  rte_kvargs_handle_urange, &outb_nb_crypto_qs);
 	ret |= rte_kvargs_process_opt(kvlist, CNXK_NO_INL_DEV, rte_kvargs_handle_bool,
 				  &no_inl_dev);
 	ret |= rte_kvargs_process(kvlist, CNXK_SDP_CHANNEL_MASK,
@@ -320,8 +249,9 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 				  &force_tail_drop);
 	ret |= rte_kvargs_process_opt(kvlist, CNXK_DIS_XQE_DROP, rte_kvargs_handle_bool,
 				  &dis_xqe_drop);
-	ret |= rte_kvargs_process(kvlist, CNXK_RXC_STEP, &parse_rxc_step, &rxc_step);
-	ret |= rte_kvargs_process(kvlist, CNXK_SKIP_SIZE_INFO, &parse_skip_size_info,
+	ret |= rte_kvargs_process(kvlist, CNXK_RXC_STEP, rte_kvargs_handle_urange,
+				  &rxc_step);
+	ret |= rte_kvargs_process(kvlist, CNXK_SKIP_SIZE_INFO, rte_kvargs_handle_urange,
 				  &skip_size_info);
 	rte_kvargs_free(kvlist);
 
@@ -337,7 +267,7 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	dev->inb.custom_meta_aura_dis = custom_meta_aura_dis;
 	dev->outb.max_sa = ipsec_out_max_sa;
 	dev->outb.nb_desc = outb_nb_desc;
-	dev->outb.nb_crypto_qs = outb_nb_crypto_qs;
+	dev->outb.nb_crypto_qs = outb_nb_crypto_qs.val;
 	dev->nix.ipsec_out_max_sa = ipsec_out_max_sa;
 	dev->nix.rss_tag_as_xor = rss_tag_as_xor;
 	dev->nix.max_sqb_count = sqb_count;
@@ -350,15 +280,15 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 	if (roc_feature_nix_has_own_meta_aura())
 		dev->nix.meta_buf_sz = meta_buf_sz;
 
-	dev->npc.flow_prealloc_size = flow_prealloc_size;
+	dev->npc.flow_prealloc_size = flow_prealloc_size.val;
 
 	if (roc_model_is_cn20k())
 		dev->npc.flow_max_priority = ROC_NPC_MAX_MCAM_PRIORITY;
 	else
-		dev->npc.flow_max_priority = flow_max_priority;
+		dev->npc.flow_max_priority = flow_max_priority.val;
 
 	dev->npc.switch_header_type = switch_header_type;
-	dev->npc.skip_size = skip_size_info;
+	dev->npc.skip_size = skip_size_info.val;
 	dev->npc.sdp_channel = sdp_chan.channel;
 	dev->npc.sdp_channel_mask = sdp_chan.mask;
 	dev->npc.is_sdp_mask_set = sdp_chan.is_sdp_mask_set;
@@ -370,7 +300,7 @@ cnxk_ethdev_parse_devargs(struct rte_devargs *devargs, struct cnxk_eth_dev *dev)
 		dev->nix.rx_inj_ena = rx_inj_ena;
 	dev->nix.force_tail_drop = force_tail_drop;
 	dev->nix.dis_xqe_drop = dis_xqe_drop;
-	dev->nix.rxc_step = rxc_step;
+	dev->nix.rxc_step = rxc_step.val;
 	return 0;
 exit:
 	return -EINVAL;
Signature · 1 line
2.53.0
#200 lore raw

PATCH v3 65/65 doc: note kvargs devargs conversion in release notes

In reply to Stephen Hemminger

Record that the drivers were converted to the new kvargs helpers, and what a user should expect to change: a device argument which is malformed or out of range is now reported and rejected, where before it was commonly truncated, wrapped around, or taken as zero.

This is kept as a separate patch at the end of the series rather than folded into the last driver, so that the note does not depend on any one driver patch surviving review.

The individual drivers are not listed. At 52 entries the list was too long to be useful, and it meant a third of the driver patches touched the release notes only to append a bullet.

---
 doc/guides/rel_notes/release_26_11.rst | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
doc/guides/rel_notes/release_26_11.rst +19 -0
index e2cab9a484..3404c476c3 100644
@@ -171,6 +171,25 @@ New Features
   needs a handler of its own, and a value outside the range is reported
   as one which does not fit a whole type already is.
 
+  52 drivers were converted to use these helpers. A device argument which
+  is malformed or out of range is now reported and rejected, where before
+  it was commonly truncated, wrapped around, or taken as zero.
+
+  The device arguments which are booleans were converted along with them.
+  Those now take any of the spellings above rather than only ``0`` and
+  ``1``, and ``key`` on its own means true. A value which is not a
+  boolean is rejected, where several drivers previously read it as false.
+  That includes an empty ``key=``, which ``net/pcap`` used to take as
+  true for ``infinite_rx``.
+
+  Two of these change what an existing argument means. ``event/dlb2``
+  only looked for a leading ``y`` in ``vector_opts_enable``,
+  ``default_ldb_port_allocation`` and ``enable_cq_weight``, so ``=1``
+  used to mean false and now means true. ``net/virtio`` compared
+  ``vectorized`` against ``"1"``, so ``vectorized=y`` used to mean false
+  and now means true.
+
+
 Removed Items
 -------------
 
Signature · 1 line
2.53.0