All of lore.kernel.org
 help / color / mirror / Atom feed
From: Olivier Matz <olivier.matz@6wind.com>
To: dev@dpdk.org
Cc: Olivier Matz <olivier.matz@6wind.com>,
	Bruce Richardson <bruce.richardson@intel.com>,
	Xueming Li <xuemingl@nvidia.com>, Gaetan Rivet <grive@u256.net>,
	Ray Kinsella <mdr@ashroe.eu>
Subject: [dpdk-dev] [PATCH v2 3/5] kvargs: new function to get from key and value
Date: Wed, 29 Sep 2021 23:39:41 +0200	[thread overview]
Message-ID: <20210929213943.17817-4-olivier.matz@6wind.com> (raw)
In-Reply-To: <20210929213943.17817-1-olivier.matz@6wind.com>

A quite common scenario with kvargs is to lookup for a <key>=<value> in
a kvlist. For instance, check if name=foo is present in
name=toto,name=foo,name=bar. This is currently done in drivers/bus with
rte_kvargs_process() + the rte_kvargs_strcmp() handler.

This approach is not straightforward, and can be replaced by this new
function.

Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
Reviewed-by: Xueming Li <xuemingl@nvidia.com>
---
 drivers/bus/auxiliary/auxiliary_params.c |  9 ++++----
 drivers/bus/vdev/vdev_params.c           | 13 ++---------
 lib/kvargs/rte_kvargs.c                  | 23 +++++++++++++++----
 lib/kvargs/rte_kvargs.h                  | 29 +++++++++++++++++++++++-
 lib/kvargs/version.map                   |  2 ++
 5 files changed, 54 insertions(+), 22 deletions(-)

diff --git a/drivers/bus/auxiliary/auxiliary_params.c b/drivers/bus/auxiliary/auxiliary_params.c
index cd3fa56cb4..a9c7853ed1 100644
--- a/drivers/bus/auxiliary/auxiliary_params.c
+++ b/drivers/bus/auxiliary/auxiliary_params.c
@@ -25,13 +25,12 @@ auxiliary_dev_match(const struct rte_device *dev,
 	      const void *_kvlist)
 {
 	const struct rte_kvargs *kvlist = _kvlist;
-	int ret;
+	const char *key = auxiliary_params_keys[RTE_AUXILIARY_PARAM_NAME];
 
-	ret = rte_kvargs_process(kvlist,
-			auxiliary_params_keys[RTE_AUXILIARY_PARAM_NAME],
-			rte_kvargs_strcmp, (void *)(uintptr_t)dev->name);
+	if (rte_kvargs_get_with_value(kvlist, key, dev->name) == NULL)
+		return -1;
 
-	return ret != 0 ? -1 : 0;
+	return 0;
 }
 
 void *
diff --git a/drivers/bus/vdev/vdev_params.c b/drivers/bus/vdev/vdev_params.c
index 6f74704d1c..37d95395e7 100644
--- a/drivers/bus/vdev/vdev_params.c
+++ b/drivers/bus/vdev/vdev_params.c
@@ -26,19 +26,10 @@ static int
 vdev_dev_match(const struct rte_device *dev,
 	       const void *_kvlist)
 {
-	int ret;
 	const struct rte_kvargs *kvlist = _kvlist;
-	char *name;
+	const char *key = vdev_params_keys[RTE_VDEV_PARAM_NAME];
 
-	/* cannot pass const dev->name to rte_kvargs_process() */
-	name = strdup(dev->name);
-	if (name == NULL)
-		return -1;
-	ret = rte_kvargs_process(kvlist,
-		vdev_params_keys[RTE_VDEV_PARAM_NAME],
-		rte_kvargs_strcmp, name);
-	free(name);
-	if (ret != 0)
+	if (rte_kvargs_get_with_value(kvlist, key, dev->name) == NULL)
 		return -1;
 
 	return 0;
diff --git a/lib/kvargs/rte_kvargs.c b/lib/kvargs/rte_kvargs.c
index 38e9d5c1ca..20abb23183 100644
--- a/lib/kvargs/rte_kvargs.c
+++ b/lib/kvargs/rte_kvargs.c
@@ -204,21 +204,34 @@ rte_kvargs_free(struct rte_kvargs *kvlist)
 	free(kvlist);
 }
 
-/* Lookup a value in an rte_kvargs list by its key. */
+/* Lookup a value in an rte_kvargs list by its key and value. */
 const char *
-rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key)
+rte_kvargs_get_with_value(const struct rte_kvargs *kvlist, const char *key,
+			  const char *value)
 {
 	unsigned int i;
 
-	if (kvlist == NULL || key == NULL)
+	if (kvlist == NULL)
 		return NULL;
 	for (i = 0; i < kvlist->count; ++i) {
-		if (strcmp(kvlist->pairs[i].key, key) == 0)
-			return kvlist->pairs[i].value;
+		if (key != NULL && strcmp(kvlist->pairs[i].key, key) != 0)
+			continue;
+		if (value != NULL && strcmp(kvlist->pairs[i].value, value) != 0)
+			continue;
+		return kvlist->pairs[i].value;
 	}
 	return NULL;
 }
 
+/* Lookup a value in an rte_kvargs list by its key. */
+const char *
+rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key)
+{
+	if (kvlist == NULL || key == NULL)
+		return NULL;
+	return rte_kvargs_get_with_value(kvlist, key, NULL);
+}
+
 /*
  * Parse the arguments "key=value,key=value,..." string and return
  * an allocated structure that contains a key/value list. Also
diff --git a/lib/kvargs/rte_kvargs.h b/lib/kvargs/rte_kvargs.h
index 328f0d3cc6..e40df1bd2a 100644
--- a/lib/kvargs/rte_kvargs.h
+++ b/lib/kvargs/rte_kvargs.h
@@ -116,7 +116,7 @@ void rte_kvargs_free(struct rte_kvargs *kvlist);
 /**
  * Get the value associated with a given key.
  *
- * If multiple key matches, the value of the first one is returned.
+ * If multiple keys match, the value of the first one is returned.
  *
  * The memory returned is allocated as part of the rte_kvargs structure,
  * it must never be modified.
@@ -132,6 +132,33 @@ void rte_kvargs_free(struct rte_kvargs *kvlist);
  */
 const char *rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Get the value associated with a given key and value.
+ *
+ * Find the first entry in the kvlist whose key and value match the
+ * ones passed as argument.
+ *
+ * The memory returned is allocated as part of the rte_kvargs structure,
+ * it must never be modified.
+ *
+ * @param kvlist
+ *   A list of rte_kvargs pair of 'key=value'.
+ * @param key
+ *   The matching key. If NULL, any key will match.
+ * @param value
+ *   The matching value. If NULL, any value will match.
+ *
+ * @return
+ *   NULL if no key matches the input,
+ *   a value associated with a matching key otherwise.
+ */
+__rte_experimental
+const char *rte_kvargs_get_with_value(const struct rte_kvargs *kvlist,
+				      const char *key, const char *value);
+
 /**
  * Call a handler function for each key/value matching the key
  *
diff --git a/lib/kvargs/version.map b/lib/kvargs/version.map
index 236f35c02b..82879b7140 100644
--- a/lib/kvargs/version.map
+++ b/lib/kvargs/version.map
@@ -16,4 +16,6 @@ EXPERIMENTAL {
 
 	rte_kvargs_strcmp;
 
+	# added in 21.11
+	rte_kvargs_get_with_value;
 };
-- 
2.30.2


  parent reply	other threads:[~2021-09-29 21:40 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-24 15:57 [dpdk-dev] [PATCH 0/5] kvargs: promote or remove experimental api Olivier Matz
2021-09-24 15:57 ` [dpdk-dev] [PATCH 1/5] kvargs: promote delimited parsing as stable Olivier Matz
2021-09-24 15:57 ` [dpdk-dev] [PATCH 2/5] kvargs: promote get from key " Olivier Matz
2021-09-24 15:57 ` [dpdk-dev] [PATCH 3/5] kvargs: new function to get from key and value Olivier Matz
2021-09-24 15:57 ` [dpdk-dev] [PATCH 4/5] kvargs: remove experimental function to compare string Olivier Matz
2021-09-26 16:30   ` Olivier Matz
2021-09-24 15:57 ` [dpdk-dev] [PATCH 5/5] kvargs: fix comments style Olivier Matz
2021-09-25  7:02 ` [dpdk-dev] [PATCH 0/5] kvargs: promote or remove experimental api Xueming(Steven) Li
2021-09-29 21:39 ` [dpdk-dev] [PATCH v2 " Olivier Matz
2021-09-29 21:39   ` [dpdk-dev] [PATCH v2 1/5] kvargs: promote delimited parsing as stable Olivier Matz
2021-09-29 21:39   ` [dpdk-dev] [PATCH v2 2/5] kvargs: promote get from key " Olivier Matz
2021-09-29 21:39   ` Olivier Matz [this message]
2021-09-29 21:39   ` [dpdk-dev] [PATCH v2 4/5] kvargs: remove experimental function to compare string Olivier Matz
2021-09-30 14:00     ` Olivier Matz
2021-09-30 15:19       ` David Marchand
2021-09-29 21:39   ` [dpdk-dev] [PATCH v2 5/5] kvargs: fix comments style Olivier Matz
2021-09-30  8:25   ` [dpdk-dev] [PATCH v2 0/5] kvargs: promote or remove experimental api David Marchand
2021-09-30  9:57     ` Kinsella, Ray
2021-09-30 16:19       ` David Marchand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210929213943.17817-4-olivier.matz@6wind.com \
    --to=olivier.matz@6wind.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=grive@u256.net \
    --cc=mdr@ashroe.eu \
    --cc=xuemingl@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.