DPDK patches and discussions
 help / color / mirror / Atom feed
From: Olivier Matz <olivier.matz@6wind.com>
To: dev@dpdk.org
Cc: xuemingl@nvidia.com, Lior Margalit <lmargalit@nvidia.com>,
	Parav Pandit <parav@nvidia.com>,
	David Marchand <david.marchand@redhat.com>,
	Ray Kinsella <mdr@ashroe.eu>
Subject: [PATCH v4] bus: fix device iterator match from arguments
Date: Wed, 24 Nov 2021 13:45:24 +0100	[thread overview]
Message-ID: <20211124124524.25754-1-olivier.matz@6wind.com> (raw)
In-Reply-To: <20211122061250.3220823-1-xuemingl@nvidia.com>

From: Xueming Li <xuemingl@nvidia.com>

Device iterator RTE_DEV_FOREACH() failed to return devices from
classifier like "class=vdpa", because matching name from empty kvargs
returns no result. If device name not specified in kvargs, the function
should iterate all devices.

This patch allows empty devargs or devargs without name specified.

Fixes: 6aebb942907d ("kvargs: add function to get from key and value")

Signed-off-by: Xueming Li <xuemingl@nvidia.com>
Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
Reviewed-by: Xueming Li <xuemingl@nvidia.com>
---
bug is specific to 21.11, no need to cc stable@dpdk.org

v4:
- disable unit test when net/null is not enabled
v3:
- add unit test
v2:
- use rte_kvargs_get() + strcmp instead of rte_kvargs_get_with_value()
---
 app/test/meson.build                     |   5 +
 app/test/test_vdev.c                     | 168 +++++++++++++++++++++++
 drivers/bus/auxiliary/auxiliary_params.c |   9 +-
 drivers/bus/vdev/vdev_params.c           |   9 +-
 4 files changed, 189 insertions(+), 2 deletions(-)
 create mode 100644 app/test/test_vdev.c

diff --git a/app/test/meson.build b/app/test/meson.build
index 961bebc5cb..2b480adfba 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -426,6 +426,11 @@ if dpdk_conf.has('RTE_NET_RING')
         fast_tests += [['pdump_autotest', true]]
     endif
 endif
+if dpdk_conf.has('RTE_NET_NULL')
+    test_deps += 'net_null'
+    test_sources += 'test_vdev.c'
+    fast_tests += [['vdev_autotest', true]]
+endif
 
 if dpdk_conf.has('RTE_HAS_LIBPCAP')
     ext_deps += pcap_dep
diff --git a/app/test/test_vdev.c b/app/test/test_vdev.c
new file mode 100644
index 0000000000..720722c363
--- /dev/null
+++ b/app/test/test_vdev.c
@@ -0,0 +1,168 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2021 6WIND S.A.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <rte_common.h>
+#include <rte_kvargs.h>
+#include <rte_bus_vdev.h>
+
+#include "test.h"
+
+#define TEST_VDEV_KEY_NAME "name"
+
+static const char * const valid_keys[] = {
+	TEST_VDEV_KEY_NAME,
+	NULL,
+};
+
+static int
+cmp_dev_name(const struct rte_device *dev, const void *name)
+{
+	return strcmp(dev->name, name);
+}
+
+static int
+cmp_dev_match(const struct rte_device *dev, const void *_kvlist)
+{
+	const struct rte_kvargs *kvlist = _kvlist;
+	const char *key = TEST_VDEV_KEY_NAME;
+	const char *name;
+
+	/* no kvlist arg, all devices match */
+	if (kvlist == NULL)
+		return 0;
+
+	/* if key is present in kvlist and does not match, filter device */
+	name = rte_kvargs_get(kvlist, key);
+	if (name != NULL && strcmp(name, dev->name))
+		return -1;
+
+	return 0;
+}
+
+static struct rte_device *
+get_matching_vdev(const char *match_str)
+{
+	struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
+	struct rte_kvargs *kvargs = NULL;
+	struct rte_device *dev;
+
+	if (match_str != NULL) {
+		kvargs = rte_kvargs_parse(match_str, valid_keys);
+		if (kvargs == NULL) {
+			printf("Failed to parse match string\n");
+			return NULL;
+		}
+	}
+
+	dev = vdev_bus->find_device(NULL, cmp_dev_match, kvargs);
+	rte_kvargs_free(kvargs);
+
+	return dev;
+}
+
+static int
+test_vdev_bus(void)
+{
+	struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
+	struct rte_dev_iterator dev_iter = { 0 };
+	struct rte_device *dev, *dev0, *dev1;
+
+	/* not supported */
+	if (vdev_bus == NULL)
+		return 0;
+
+	/* create first vdev */
+	if (rte_vdev_init("net_null_test0", "") < 0) {
+		printf("Failed to create vdev net_null_test0\n");
+		goto fail;
+	}
+	dev0 = vdev_bus->find_device(NULL, cmp_dev_name, "net_null_test0");
+	if (dev0 == NULL) {
+		printf("Cannot find net_null_test0 vdev\n");
+		goto fail;
+	}
+
+	/* create second vdev */
+	if (rte_vdev_init("net_null_test1", "") < 0) {
+		printf("Failed to create vdev net_null_test1\n");
+		goto fail;
+	}
+	dev1 = vdev_bus->find_device(NULL, cmp_dev_name, "net_null_test1");
+	if (dev1 == NULL) {
+		printf("Cannot find net_null_test1 vdev\n");
+		goto fail;
+	}
+
+	/* try to match vdevs */
+	dev = get_matching_vdev("name=net_null_test0");
+	if (dev != dev0) {
+		printf("Cannot match net_null_test0 vdev\n");
+		goto fail;
+	}
+
+	dev = get_matching_vdev("name=net_null_test1");
+	if (dev != dev1) {
+		printf("Cannot match net_null_test1 vdev\n");
+		goto fail;
+	}
+
+	dev = get_matching_vdev("name=unexistant");
+	if (dev != NULL) {
+		printf("Unexistant vdev should not match\n");
+		goto fail;
+	}
+
+	dev = get_matching_vdev("");
+	if (dev == NULL || dev == dev1) {
+		printf("Cannot match any vdev with empty match string\n");
+		goto fail;
+	}
+
+	dev = get_matching_vdev(NULL);
+	if (dev == NULL || dev == dev1) {
+		printf("Cannot match any vdev with NULL match string\n");
+		goto fail;
+	}
+
+	/* iterate all vdevs, and ensure we find vdev0 and vdev1 */
+	RTE_DEV_FOREACH(dev, "bus=vdev", &dev_iter) {
+		if (dev == dev0)
+			dev0 = NULL;
+		else if (dev == dev1)
+			dev1 = NULL;
+	}
+	if (dev0 != NULL) {
+		printf("dev0 was not iterated\n");
+		goto fail;
+	}
+	if (dev1 != NULL) {
+		printf("dev1 was not iterated\n");
+		goto fail;
+	}
+
+	rte_vdev_uninit("net_null_test0");
+	rte_vdev_uninit("net_null_test1");
+
+	return 0;
+
+fail:
+	rte_vdev_uninit("net_null_test0");
+	rte_vdev_uninit("net_null_test1");
+	return -1;
+}
+
+static int
+test_vdev(void)
+{
+	printf("== test vdev bus ==\n");
+	if (test_vdev_bus() < 0)
+		return -1;
+	return 0;
+}
+
+REGISTER_TEST_COMMAND(vdev_autotest, test_vdev);
diff --git a/drivers/bus/auxiliary/auxiliary_params.c b/drivers/bus/auxiliary/auxiliary_params.c
index 8dd8813611..9c08ccdd1b 100644
--- a/drivers/bus/auxiliary/auxiliary_params.c
+++ b/drivers/bus/auxiliary/auxiliary_params.c
@@ -28,8 +28,15 @@ auxiliary_dev_match(const struct rte_device *dev,
 {
 	const struct rte_kvargs *kvlist = _kvlist;
 	const char *key = auxiliary_params_keys[RTE_AUXILIARY_PARAM_NAME];
+	const char *name;
 
-	if (rte_kvargs_get_with_value(kvlist, key, dev->name) == NULL)
+	/* no kvlist arg, all devices match */
+	if (kvlist == NULL)
+		return 0;
+
+	/* if key is present in kvlist and does not match, filter device */
+	name = rte_kvargs_get(kvlist, key);
+	if (name != NULL && strcmp(name, dev->name))
 		return -1;
 
 	return 0;
diff --git a/drivers/bus/vdev/vdev_params.c b/drivers/bus/vdev/vdev_params.c
index 37d95395e7..3969faf16d 100644
--- a/drivers/bus/vdev/vdev_params.c
+++ b/drivers/bus/vdev/vdev_params.c
@@ -28,8 +28,15 @@ vdev_dev_match(const struct rte_device *dev,
 {
 	const struct rte_kvargs *kvlist = _kvlist;
 	const char *key = vdev_params_keys[RTE_VDEV_PARAM_NAME];
+	const char *name;
 
-	if (rte_kvargs_get_with_value(kvlist, key, dev->name) == NULL)
+	/* no kvlist arg, all devices match */
+	if (kvlist == NULL)
+		return 0;
+
+	/* if key is present in kvlist and does not match, filter device */
+	name = rte_kvargs_get(kvlist, key);
+	if (name != NULL && strcmp(name, dev->name))
 		return -1;
 
 	return 0;
-- 
2.30.2


  parent reply	other threads:[~2021-11-24 12:45 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-22  6:12 [PATCH] kvargs: " Xueming Li
2021-11-23 10:25 ` Olivier Matz
2021-11-23 11:25   ` Xueming(Steven) Li
2021-11-23 12:31     ` Olivier Matz
2021-11-23 12:49       ` Xueming(Steven) Li
2021-11-23 20:02         ` Olivier Matz
2021-11-24 10:21           ` Xueming(Steven) Li
2021-11-24 10:17 ` [PATCH v2] " Xueming Li
2021-11-24 11:07   ` Olivier Matz
2021-11-24 11:19     ` Xueming(Steven) Li
2021-11-24 11:02 ` [PATCH v3] bus: " Olivier Matz
2021-11-24 11:31   ` Xueming(Steven) Li
2021-11-24 11:43     ` Xueming(Steven) Li
2021-11-24 12:14       ` Olivier Matz
2021-11-24 12:45 ` Olivier Matz [this message]
2021-11-24 13:00   ` [PATCH v4] " Xueming(Steven) Li
2021-11-24 13:44     ` 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=20211124124524.25754-1-olivier.matz@6wind.com \
    --to=olivier.matz@6wind.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=lmargalit@nvidia.com \
    --cc=mdr@ashroe.eu \
    --cc=parav@nvidia.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).