[dpdk-dev] [RFC PATCH v2 3/7] hcdev: add child device representing a device context

Thomas Monjalon thomas at monjalon.net
Fri Jul 30 15:55:29 CEST 2021


The computing device may operate in some isolated contexts.
Memory and processing are isolated in a silo represented by
a child device.
The context is provided as an opaque by the caller of
rte_hcdev_add_child().

Signed-off-by: Thomas Monjalon <thomas at monjalon.net>
---
 lib/hcdev/hcdev.c        | 45 ++++++++++++++++++++++++--
 lib/hcdev/hcdev_driver.h |  2 +-
 lib/hcdev/rte_hcdev.h    | 69 +++++++++++++++++++++++++++++++++++++---
 lib/hcdev/version.map    |  1 +
 4 files changed, 110 insertions(+), 7 deletions(-)

diff --git a/lib/hcdev/hcdev.c b/lib/hcdev/hcdev.c
index 2a7ce1ccd8..d40010749a 100644
--- a/lib/hcdev/hcdev.c
+++ b/lib/hcdev/hcdev.c
@@ -79,13 +79,22 @@ rte_hcdev_is_valid(int16_t dev_id)
 	return false;
 }
 
+static bool
+hcdev_match_parent(int16_t dev_id, int16_t parent)
+{
+	if (parent == RTE_HCDEV_ID_ANY)
+		return true;
+	return hcdevs[dev_id].info.parent == parent;
+}
+
 int16_t
-rte_hcdev_find_next(int16_t dev_id)
+rte_hcdev_find_next(int16_t dev_id, int16_t parent)
 {
 	if (dev_id < 0)
 		dev_id = 0;
 	while (dev_id < hcdev_max &&
-			hcdevs[dev_id].state == RTE_HCDEV_STATE_UNUSED)
+			(hcdevs[dev_id].state == RTE_HCDEV_STATE_UNUSED ||
+			!hcdev_match_parent(dev_id, parent)))
 		dev_id++;
 
 	if (dev_id >= hcdev_max)
@@ -176,6 +185,7 @@ rte_hcdev_allocate(const char *name)
 	dev->info.name = dev->name;
 	dev->info.dev_id = dev_id;
 	dev->info.numa_node = -1;
+	dev->info.parent = RTE_HCDEV_ID_NONE;
 	TAILQ_INIT(&dev->callbacks);
 
 	hcdev_count++;
@@ -184,6 +194,28 @@ rte_hcdev_allocate(const char *name)
 	return dev;
 }
 
+int16_t
+rte_hcdev_add_child(const char *name, int16_t parent, uint64_t child_context)
+{
+	struct rte_hcdev *dev;
+
+	if (!rte_hcdev_is_valid(parent)) {
+		HCDEV_LOG(ERR, "add child to invalid parent ID %d", parent);
+		rte_errno = ENODEV;
+		return -rte_errno;
+	}
+
+	dev = rte_hcdev_allocate(name);
+	if (dev == NULL)
+		return -rte_errno;
+
+	dev->info.parent = parent;
+	dev->info.context = child_context;
+
+	rte_hcdev_complete_new(dev);
+	return dev->info.dev_id;
+}
+
 void
 rte_hcdev_complete_new(struct rte_hcdev *dev)
 {
@@ -197,10 +229,19 @@ rte_hcdev_complete_new(struct rte_hcdev *dev)
 int
 rte_hcdev_release(struct rte_hcdev *dev)
 {
+	int16_t dev_id, child;
+
 	if (dev == NULL) {
 		rte_errno = ENODEV;
 		return -rte_errno;
 	}
+	dev_id = dev->info.dev_id;
+	RTE_HCDEV_FOREACH_CHILD(child, dev_id) {
+		HCDEV_LOG(ERR, "cannot release device %d with child %d",
+				dev_id, child);
+		rte_errno = EBUSY;
+		return -rte_errno;
+	}
 
 	HCDEV_LOG(DEBUG, "free device %s (id %d)",
 			dev->info.name, dev->info.dev_id);
diff --git a/lib/hcdev/hcdev_driver.h b/lib/hcdev/hcdev_driver.h
index 80d11bd612..39f6fc57ab 100644
--- a/lib/hcdev/hcdev_driver.h
+++ b/lib/hcdev/hcdev_driver.h
@@ -31,7 +31,7 @@ typedef int (rte_hcdev_info_get_t)(struct rte_hcdev *dev, struct rte_hcdev_info
 struct rte_hcdev_ops {
 	/* Get device info. If NULL, info is just copied. */
 	rte_hcdev_info_get_t *dev_info_get;
-	/* Close device. */
+	/* Close device or child context. */
 	rte_hcdev_close_t *dev_close;
 };
 
diff --git a/lib/hcdev/rte_hcdev.h b/lib/hcdev/rte_hcdev.h
index 8131e4045a..518020fd2f 100644
--- a/lib/hcdev/rte_hcdev.h
+++ b/lib/hcdev/rte_hcdev.h
@@ -42,8 +42,12 @@ extern "C" {
 struct rte_hcdev_info {
 	/** Unique identifier name. */
 	const char *name;
+	/** Opaque handler of the device context. */
+	uint64_t context;
 	/** Device ID. */
 	int16_t dev_id;
+	/** ID of the parent device, RTE_HCDEV_ID_NONE if no parent */
+	int16_t parent;
 	/** Total processors available on device. */
 	uint32_t processor_count;
 	/** Total memory available on device. */
@@ -112,6 +116,33 @@ uint16_t rte_hcdev_count_avail(void);
 __rte_experimental
 bool rte_hcdev_is_valid(int16_t dev_id);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Create a virtual device representing a context in the parent device.
+ *
+ * @param name
+ *   Unique string to identify the device.
+ * @param parent
+ *   Device ID of the parent.
+ * @param child_context
+ *   Opaque context handler.
+ *
+ * @return
+ *   Device ID of the new created child, -rte_errno otherwise:
+ *   - EINVAL if empty name
+ *   - ENAMETOOLONG if long name
+ *   - EEXIST if existing device name
+ *   - ENODEV if invalid parent
+ *   - EPERM if secondary process
+ *   - ENOENT if too many devices
+ *   - ENOMEM if out of space
+ */
+__rte_experimental
+int16_t rte_hcdev_add_child(const char *name,
+		int16_t parent, uint64_t child_context);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice.
@@ -120,13 +151,17 @@ bool rte_hcdev_is_valid(int16_t dev_id);
  *
  * @param dev_id
  *   The initial device ID to start the research.
+ * @param parent
+ *   The device ID of the parent.
+ *   RTE_HCDEV_ID_NONE means no parent.
+ *   RTE_HCDEV_ID_ANY means no or any parent.
  *
  * @return
  *   Next device ID corresponding to a valid and initialized computing device,
  *   RTE_HCDEV_ID_NONE if there is none.
  */
 __rte_experimental
-int16_t rte_hcdev_find_next(int16_t dev_id);
+int16_t rte_hcdev_find_next(int16_t dev_id, int16_t parent);
 
 /**
  * @warning
@@ -138,15 +173,41 @@ int16_t rte_hcdev_find_next(int16_t dev_id);
  *   The ID of the next possible valid device, usually 0 to iterate all.
  */
 #define RTE_HCDEV_FOREACH(dev_id) \
-	for (dev_id = rte_hcdev_find_next(0); \
+	RTE_HCDEV_FOREACH_CHILD(dev_id, RTE_HCDEV_ID_ANY)
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Macro to iterate over all valid computing devices having no parent.
+ *
+ * @param dev_id
+ *   The ID of the next possible valid device, usually 0 to iterate all.
+ */
+#define RTE_HCDEV_FOREACH_PARENT(dev_id) \
+	RTE_HCDEV_FOREACH_CHILD(dev_id, RTE_HCDEV_ID_NONE)
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Macro to iterate over all valid children of a computing device parent.
+ *
+ * @param dev_id
+ *   The ID of the next possible valid device, usually 0 to iterate all.
+ * @param parent
+ *   The device ID of the parent.
+ */
+#define RTE_HCDEV_FOREACH_CHILD(dev_id, parent) \
+	for (dev_id = rte_hcdev_find_next(0, parent); \
 	     dev_id > 0; \
-	     dev_id = rte_hcdev_find_next(dev_id + 1))
+	     dev_id = rte_hcdev_find_next(dev_id + 1, parent))
 
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice.
  *
- * Close device.
+ * Close device or child context.
  * All resources are released.
  *
  * @param dev_id
diff --git a/lib/hcdev/version.map b/lib/hcdev/version.map
index 24a5a5a7c4..6d1a1ab1c9 100644
--- a/lib/hcdev/version.map
+++ b/lib/hcdev/version.map
@@ -2,6 +2,7 @@ EXPERIMENTAL {
 	global:
 
 	# added in 21.11
+	rte_hcdev_add_child;
 	rte_hcdev_callback_register;
 	rte_hcdev_callback_unregister;
 	rte_hcdev_close;
-- 
2.31.1



More information about the dev mailing list