[dpdk-dev] [PATCH v2 04/18] eal: add lightweight kvarg parsing utility

Gaetan Rivet gaetan.rivet at 6wind.com
Wed Mar 21 18:15:25 CET 2018


This library offers a quick way to parse parameters passed with a
key=value syntax.

A single function is needed and finds the relevant element within the
text. No dynamic allocation is performed. It is possible to chain the
parsing of each pairs for quickly scanning a list.

This utility is private to the EAL and should allow avoiding having to
move around the more complete librte_kvargs.

Signed-off-by: Gaetan Rivet <gaetan.rivet at 6wind.com>
---
 lib/librte_eal/common/eal_common_dev.c | 38 ++++++++++++++++++++++++++++++++++
 lib/librte_eal/common/eal_private.h    | 34 ++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
index cd071442f..4032f1bd8 100644
--- a/lib/librte_eal/common/eal_common_dev.c
+++ b/lib/librte_eal/common/eal_common_dev.c
@@ -13,10 +13,48 @@
 #include <rte_dev.h>
 #include <rte_devargs.h>
 #include <rte_debug.h>
+#include <rte_errno.h>
 #include <rte_log.h>
 
 #include "eal_private.h"
 
+/* EAL-private function. */
+int
+rte_parse_kv(const char *str, struct rte_kvarg *kv)
+{
+	const char *equal;
+	const char *end;
+
+	if (str == NULL || str[0] == '\0')
+		return 1;
+	equal = strchr(str, '=');
+	if (equal == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+	end = strchr(equal + 1, ',');
+	end = end ? end : strchr(equal + 1, '/');
+	end = end ? end : strchr(equal + 1, '\0');
+	if (end == NULL) {
+		rte_errno = ENODEV;
+		return -1;
+	}
+	if (kv == NULL)
+		return 0;
+	snprintf(kv->data, sizeof(kv->data), "%s", str);
+	kv->key = &kv->data[0];
+	strchr(kv->data, end[0])[0] = '\0';
+	if (strchr(kv->data, '=')) {
+		kv->value = strchr(kv->data, '=') + 1;
+		strchr(kv->data, '=')[0] = '\0';
+	}
+	if (end[0] == '\0')
+		kv->next = NULL;
+	else
+		kv->next = end + 1;
+	return 0;
+}
+
 static int cmp_detached_dev_name(const struct rte_device *dev,
 	const void *_name)
 {
diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h
index 0b2877000..d2774a3ad 100644
--- a/lib/librte_eal/common/eal_private.h
+++ b/lib/librte_eal/common/eal_private.h
@@ -205,4 +205,38 @@ struct rte_bus *rte_bus_find_by_device_name(const char *str);
 
 int rte_mp_channel_init(void);
 
+/*
+ * Lightweight kvarg parsing library.
+ */
+
+#define RTE_MAX_KVARG_LEN 64
+
+/**
+ * Kvarg representation.
+ */
+struct rte_kvarg {
+	char *key; /**< points the key in the data. */
+	char *value; /**< points the value in the data. */
+	const char *next; /**< next token to parse, if any. */
+	char data[RTE_MAX_KVARG_LEN + 1]; /**< local copy of key and value. */
+};
+
+/**
+ * Parse one kvarg.
+ *
+ * The key-value pair must be shorter than the rte_kvarg data size.
+ *
+ * @param[in] str
+ *   text to parse.
+ *
+ * @param[out] kv
+ *   kvarg structure to fill.
+ *
+ * @return
+ *   0 if parsing succeeded.
+ *   >0 if there was nothing to parse.
+ *   <0 on error, rte_errno is set.
+ */
+int rte_parse_kv(const char *str, struct rte_kvarg *kv);
+
 #endif /* _EAL_PRIVATE_H_ */
-- 
2.11.0



More information about the dev mailing list