[dpdk-dev] [PATCH V19 3/4] eal: add driver auto bind for hot insertion

Jeff Guo jia.guo at intel.com
Fri Apr 6 12:56:56 CEST 2018


Normally we use driverctl or dpdk-devbind.py to bind kernel driver before
application running, but lack of an function to automatically bind driver
at runtime. This patch introduce a new API (rte_dev_bind_kernel_driver),
aim to let user call it to bind the specific kernel driver according
their own policy, that would preparing for the next step of attach device,
let app running smoothly when hotplug behavior occur.

Signed-off-by: Jeff Guo <jia.guo at intel.com>
---
v19->v18:
no change
---
 doc/guides/rel_notes/release_18_05.rst  |  8 +++--
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  7 +++++
 lib/librte_eal/common/include/rte_dev.h | 16 ++++++++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 54 +++++++++++++++++++++++++++++++++
 lib/librte_eal/rte_eal_version.map      |  1 +
 5 files changed, 83 insertions(+), 3 deletions(-)

diff --git a/doc/guides/rel_notes/release_18_05.rst b/doc/guides/rel_notes/release_18_05.rst
index 2707e73..f8822d3 100644
--- a/doc/guides/rel_notes/release_18_05.rst
+++ b/doc/guides/rel_notes/release_18_05.rst
@@ -70,11 +70,13 @@ New Features
 
   Linux uevent is supported as backend of this device event notification framework.
 
-* **Added hot plug failure handler.**
+* **Added hot plug failure handler and kernel driver auto-bind func**
 
-  Added a failure handler machenism to handle hot unplug device.
+  Added a failure handler machenism to handle hot unplug device, and added an kernel driver
+  auto bind function for hot plug insertion. The list of new APIs:
 
-  * ``rte_dev_handle_hot_unplug`` for handle hot unplug device failure.
+  * ``rte_dev_handle_hot_unplug`` for handle hot uplug device failure.
+  * ``rte_dev_bind_kernel_driver`` for hot plug insertion.
 
 API Changes
 -----------
diff --git a/lib/librte_eal/bsdapp/eal/eal_dev.c b/lib/librte_eal/bsdapp/eal/eal_dev.c
index 1c6c51b..e953a87 100644
--- a/lib/librte_eal/bsdapp/eal/eal_dev.c
+++ b/lib/librte_eal/bsdapp/eal/eal_dev.c
@@ -19,3 +19,10 @@ rte_dev_event_monitor_stop(void)
 	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
 	return -1;
 }
+
+int __rte_experimental
+rte_dev_bind_driver(const char *dev_name, enum rte_kernel_driver kdrv_type)
+{
+	RTE_LOG(ERR, EAL, "Bind driver is not supported for FreeBSD\n");
+	return -1;
+}
diff --git a/lib/librte_eal/common/include/rte_dev.h b/lib/librte_eal/common/include/rte_dev.h
index 17c446d..35f45d3 100644
--- a/lib/librte_eal/common/include/rte_dev.h
+++ b/lib/librte_eal/common/include/rte_dev.h
@@ -380,4 +380,20 @@ rte_dev_event_monitor_stop(void);
  */
 int __rte_experimental
 rte_dev_handle_hot_unplug(void);
+
+/**
+ * It can be used to bind a device to a specific type of kernel driver.
+ *
+ * @param dev_name
+ *  The device name.
+ * @param kdrv_type
+ *  The specific kernel driver's type.
+ *
+ * @return
+ *  - On success, zero.
+ *  - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_bind_kernel_driver(const char *dev_name,
+			   enum rte_kernel_driver kdrv_type);
 #endif /* _RTE_DEV_H_ */
diff --git a/lib/librte_eal/linuxapp/eal/eal_dev.c b/lib/librte_eal/linuxapp/eal/eal_dev.c
index 84b7efc..2ad7444 100644
--- a/lib/librte_eal/linuxapp/eal/eal_dev.c
+++ b/lib/librte_eal/linuxapp/eal/eal_dev.c
@@ -4,6 +4,7 @@
 
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include <signal.h>
 #include <setjmp.h>
 #include <pthread.h>
@@ -359,3 +360,56 @@ rte_dev_event_monitor_stop(void)
 
 	return 0;
 }
+
+int __rte_experimental
+rte_dev_bind_kernel_driver(const char *dev_name,
+			   enum rte_kernel_driver kdrv_type)
+{
+	const char *kdrv_name = NULL;
+	char drv_override_path[1024];
+	int drv_override_fd = -1;
+
+	if (!dev_name || !kdrv_type)
+		return -1;
+
+	switch (kdrv_type) {
+	case RTE_KDRV_IGB_UIO:
+		kdrv_name = "igb_uio";
+		break;
+	case RTE_KDRV_VFIO:
+		kdrv_name = "vfio-pci";
+		break;
+	case RTE_KDRV_UIO_GENERIC:
+		kdrv_name = "uio_pci_generic";
+		break;
+	case RTE_KDRV_NIC_UIO:
+		RTE_LOG(ERR, EAL, "Don't support to bind nic uio driver.\n");
+		goto err;
+	default:
+		break;
+	}
+
+	snprintf(drv_override_path, sizeof(drv_override_path),
+		"/sys/bus/pci/devices/%s/driver_override", dev_name);
+
+	/* specify the driver for a device by writing to driver_override */
+	drv_override_fd = open(drv_override_path, O_WRONLY);
+	if (drv_override_fd < 0) {
+		RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
+			drv_override_path, strerror(errno));
+		goto err;
+	}
+
+	if (write(drv_override_fd, kdrv_name, sizeof(kdrv_name)) < 0) {
+		RTE_LOG(ERR, EAL,
+			"Error: bind failed - Cannot write "
+			"driver %s to device %s\n", kdrv_name, dev_name);
+		goto err;
+	}
+
+	close(drv_override_fd);
+	return 0;
+err:
+	close(drv_override_fd);
+	return -1;
+}
diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map
index 873ef38..664a837 100644
--- a/lib/librte_eal/rte_eal_version.map
+++ b/lib/librte_eal/rte_eal_version.map
@@ -263,5 +263,6 @@ EXPERIMENTAL {
 	rte_dev_event_callback_register;
 	rte_dev_event_callback_unregister;
 	rte_dev_handle_hot_unplug;
+	rte_dev_bind_kernel_driver;
 
 } DPDK_18.02;
-- 
2.7.4



More information about the dev mailing list