]> git.droids-corp.org - dpdk.git/commitdiff
kvargs: new function to get from key and value
authorOlivier Matz <olivier.matz@6wind.com>
Fri, 24 Sep 2021 12:09:37 +0000 (14:09 +0200)
committerOlivier Matz <olivier.matz@6wind.com>
Fri, 24 Sep 2021 12:59:39 +0000 (14:59 +0200)
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>
drivers/bus/auxiliary/auxiliary_params.c
drivers/bus/vdev/vdev_params.c
lib/kvargs/rte_kvargs.c
lib/kvargs/rte_kvargs.h
lib/kvargs/version.map

index cd3fa56cb4517fde7bda9a23276e68664fa7a50d..a9c7853ed1d1f6c573f2c7e5e8a5ea2d97bdbf53 100644 (file)
@@ -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 *
index 6f74704d1c8755c0bb32a5fdd57b71fd36e5b701..37d95395e7a79230da32bc082bd18f9e5ee5f76a 100644 (file)
@@ -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;
index 38e9d5c1ca0ea7acbe32d51d3ab242de65ff11fe..20abb2318393580f7b61fa75622168a65bcaaf84 100644 (file)
@@ -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
index ba34b1ae702b2349a76a0a7a6d7b80246fcdf89b..135c4b3e40ae905598f8444222c01b6fb1605f29 100644 (file)
@@ -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
  *
index 236f35c02bbcec52f0bc9dd09776fad0649aba4e..82879b714063f2678c1ae7c97783f121f898e4dc 100644 (file)
@@ -16,4 +16,6 @@ EXPERIMENTAL {
 
        rte_kvargs_strcmp;
 
+       # added in 21.11
+       rte_kvargs_get_with_value;
 };