[dpdk-dev] [PATCH v1 09/18] eal/dev: implement device iteration
Gaetan Rivet
gaetan.rivet at 6wind.com
Thu Mar 15 18:49:39 CET 2018
Use the iteration hooks in the abstraction layers to perform the
requested filtering on the internal device lists.
Signed-off-by: Gaetan Rivet <gaetan.rivet at 6wind.com>
---
lib/librte_eal/common/eal_common_dev.c | 81 +++++++++++++++++++++++++++++++++
lib/librte_eal/common/include/rte_dev.h | 25 ++++++++++
lib/librte_eal/rte_eal_version.map | 1 +
3 files changed, 107 insertions(+)
diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
index bdab1423a..159fdcf73 100644
--- a/lib/librte_eal/common/eal_common_dev.c
+++ b/lib/librte_eal/common/eal_common_dev.c
@@ -303,3 +303,84 @@ rte_dev_iterator_init(struct rte_dev_iterator *it, const char *str)
it->cls = cls;
return 0;
}
+
+static int
+class_next_dev_cmp(const struct rte_class *cls,
+ const void *_it)
+{
+ const struct rte_dev_iterator *cit = _it;
+ struct rte_dev_iterator *it;
+
+ if (cls->dev_iterate == NULL)
+ return 1;
+ /* We need a mutable iterator. */
+ it = (void *)(intptr_t)cit;
+ /* it->clsstr != NULL means a class
+ * was specified in the devstr.
+ */
+ if (it->clsstr != NULL && cls != it->cls)
+ return 1;
+ return cls->dev_iterate(it) == NULL;
+}
+
+static int
+bus_next_dev_cmp(const struct rte_bus *bus,
+ const void *_it)
+{
+ const struct rte_dev_iterator *cit = _it;
+ struct rte_class *cls = NULL;
+ struct rte_dev_iterator *it;
+
+ if (bus->dev_iterate == NULL)
+ return 1;
+ /* We need a mutable iterator. */
+ it = (void *)(intptr_t)cit;
+ /* it->busstr != NULL means a bus
+ * was specified in the devstr.
+ */
+ if (it->busstr != NULL && bus != it->bus)
+ return 1;
+ if (it->clsstr == NULL)
+ return bus->dev_iterate(it) == NULL;
+ /* clsstr != NULL */
+ if (it->device == NULL)
+next_dev_on_bus:
+ bus->dev_iterate(it);
+ if (it->device == NULL)
+ return 1;
+ if (it->cls != NULL)
+ cls = TAILQ_PREV(it->cls, rte_class_list, next);
+ cls = rte_class_find(cls, class_next_dev_cmp, it);
+ if (cls != NULL) {
+ it->cls = cls;
+ return 0;
+ }
+ goto next_dev_on_bus;
+}
+__rte_experimental struct rte_device *
+rte_dev_iterator_next(struct rte_dev_iterator *it)
+{
+ struct rte_bus *bus = NULL;
+ int old_errno = rte_errno;
+
+ rte_errno = 0;
+ if (it->busstr == NULL && it->clsstr == NULL) {
+ /* Invalid iterator. */
+ rte_errno = EINVAL;
+ return NULL;
+ }
+ if (it->bus != NULL)
+ bus = TAILQ_PREV(it->bus, rte_bus_list, next);
+ while ((bus = rte_bus_find(bus, bus_next_dev_cmp, it))) {
+ if (it->device != NULL) {
+ it->bus = bus;
+ return it->device;
+ }
+ if (it->busstr != NULL ||
+ rte_errno != 0)
+ break;
+ }
+ if (rte_errno == 0)
+ rte_errno = old_errno;
+ return NULL;
+}
diff --git a/lib/librte_eal/common/include/rte_dev.h b/lib/librte_eal/common/include/rte_dev.h
index 9b53f0ad3..11a6d786c 100644
--- a/lib/librte_eal/common/include/rte_dev.h
+++ b/lib/librte_eal/common/include/rte_dev.h
@@ -322,6 +322,31 @@ typedef struct rte_device *(*rte_dev_iterate_t)(struct rte_dev_iterator *it);
int __rte_experimental
rte_dev_iterator_init(struct rte_dev_iterator *it, const char *str);
+/**
+ * Iterates on a device iterator.
+ *
+ * Generates a new rte_device handle corresponding to the next element
+ * in the list described in comprehension by the iterator.
+ *
+ * The next object is returned, and the iterator is updated.
+ *
+ * @param it
+ * Device iterator handle.
+ *
+ * @return
+ * An rte_device handle if found.
+ * NULL if an error occurred (rte_errno is set).
+ * NULL if no device could be found (rte_errno is not set).
+ */
+struct rte_device * __rte_experimental
+rte_dev_iterator_next(struct rte_dev_iterator *it);
+
+#define RTE_DEV_FOREACH(dev, devstr, it) \
+ for (rte_dev_iterator_init(it, devstr), \
+ dev = rte_dev_iterator_next(it); \
+ dev != NULL; \
+ dev = rte_dev_iterator_next(it))
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map
index 921da3075..925efcb6d 100644
--- a/lib/librte_eal/rte_eal_version.map
+++ b/lib/librte_eal/rte_eal_version.map
@@ -229,6 +229,7 @@ EXPERIMENTAL {
rte_mp_request;
rte_mp_reply;
rte_dev_iterator_init;
+ rte_dev_iterator_next;
rte_service_attr_get;
rte_service_attr_reset_all;
rte_service_component_register;
--
2.11.0
More information about the dev
mailing list