Skip to contentSkip to conversation

Add hot plug event in rte eal interrupt and inplement it in i40e driver.

RFC In review Jeff Guo 19 participants 494 messages May 28, 2017 – Oct 24, 2018 Page 3 of 5

Subsystems (2)
lore
Share & tools
lore
author
#243 lore raw

PATCH v12 6/7 eal: add failure handle mechanism for hot-unplug

In reply to Jeff Guo

The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.

By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable

---
v12->v11:
add and delete some checking about sigbus recover.

---
 doc/guides/rel_notes/release_18_08.rst  |   5 +
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  14 +++
 lib/librte_eal/common/eal_private.h     |  26 +++++
 lib/librte_eal/common/include/rte_dev.h |  26 +++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 164 +++++++++++++++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map      |   2 +
 6 files changed, 236 insertions(+), 1 deletion(-)
doc/guides/rel_notes/release_18_08.rst +5 -0
index 321fa84..fe0e60f 100644
@@ -117,6 +117,11 @@ New Features
 
   Added support for chained mbufs (input and output).
 
+* **Added hot-unplug handle mechanism.**
+
+  ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are
+  for enabling or disabling hotplug handle mechanism.
+
 
 API Changes
 -----------
lib/librte_eal/bsdapp/eal/eal_dev.c +14 -0
index 1c6c51b..255d611 100644
@@ -19,3 +19,17 @@ 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_hotplug_handle_enable(void)
+{
+	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
+	return -1;
+}
+
+int __rte_experimental
+rte_dev_hotplug_handle_disable(void)
+{
+	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
+	return -1;
+}
lib/librte_eal/common/eal_private.h +26 -0
index a2d1528..637f20d 100644
@@ -317,4 +317,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,
  */
 int rte_bus_sigbus_handler(const void *failure_addr);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Register the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_sigbus_handler_register(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Unregister the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_sigbus_handler_unregister(void);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_dev.h +26 -0
index b80a805..ff580a0 100644
@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void);
 int __rte_experimental
 rte_dev_event_monitor_stop(void);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Enable hotplug handling for devices.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_hotplug_handle_enable(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Disable hotplug handling for devices.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_hotplug_handle_disable(void);
+
 #endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/eal_dev.c +163 -1

209 lines not on this page · raw message

lib/librte_eal/rte_eal_version.map +2 -0
index 73282bb..a3255aa 100644
@@ -281,6 +281,8 @@ EXPERIMENTAL {
 	rte_dev_event_callback_unregister;
 	rte_dev_event_monitor_start;
 	rte_dev_event_monitor_stop;
+	rte_dev_hotplug_handle_enable;
+	rte_dev_hotplug_handle_disable;
 	rte_dev_iterator_init;
 	rte_dev_iterator_next;
 	rte_devargs_add;
Signature · 1 line
2.7.4
#244 lore raw

Re: [PATCH v12 6/7] eal: add failure handle mechanism for hot-unplug

Hi Jeff,

Looks ok to me in general, just one thing I missed before:

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +8 -0
+static void sigbus_handler(int signum, siginfo_t *info,
+				void *ctx __rte_unused)
+{
+	int ret;
+
+	RTE_LOG(INFO, EAL, "Thread[%d] catch SIGBUS, fault address:%p\n",
+		(int)pthread_self(), info->si_addr);
+

lib/librte_eal/linuxapp/eal/eal_dev.c enum eal_dev_event_subsystem { · line 81

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +10 -0
+	rte_spinlock_lock(&failure_handle_lock);
+	ret = rte_bus_sigbus_handler(info->si_addr);
+	rte_spinlock_unlock(&failure_handle_lock);
+	if (ret == -1) {
+		rte_exit(EXIT_FAILURE,
+			 "Failed to handle SIGBUS for hot-unplug, "
+			 "(rte_errno: %s)!", strerror(rte_errno));
+	} else if (ret == 1) {
+		if (sigbus_action_old.sa_handler)
+			(*(sigbus_action_old.sa_handler))(signum);

Shouldn't we check sigbus_action_old.sa_flags here,and based on that invoke either sa_handler() or sa_sigaction()?
Konstantin

Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +7 -0
+		else
+			rte_exit(EXIT_FAILURE,
+				 "Failed to handle generic SIGBUS!");
+	}
+
+	RTE_LOG(INFO, EAL, "Success to handle SIGBUS for hot-unplug!\n");
+}
author
#245 lore raw

Re: [PATCH v12 6/7] eal: add failure handle mechanism for hot-unplug

Quoted 17 lines · Konstantin

Hi Jeff,

Looks ok to me in general, just one thing I missed before:

Quoted 13 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +13 -0
+static void sigbus_handler(int signum, siginfo_t *info,
+				void *ctx __rte_unused)
+{
+	int ret;
+
+	RTE_LOG(INFO, EAL, "Thread[%d] catch SIGBUS, fault address:%p\n",
+		(int)pthread_self(), info->si_addr);
+
+	rte_spinlock_lock(&failure_handle_lock);
+	ret = rte_bus_sigbus_handler(info->si_addr);
+	rte_spinlock_unlock(&failure_handle_lock);
+	if (ret == -1) {
+		rte_exit(EXIT_FAILURE,
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c enum eal_dev_event_subsystem { · line 81

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +5 -0
+			 "Failed to handle SIGBUS for hot-unplug, "
+			 "(rte_errno: %s)!", strerror(rte_errno));
+	} else if (ret == 1) {
+		if (sigbus_action_old.sa_handler)
+			(*(sigbus_action_old.sa_handler))(signum);

Shouldn't we check sigbus_action_old.sa_flags here,and based on that invoke either sa_handler() or sa_sigaction()?
Konstantin

you are right here, konstantin.

We should not assume the old action should always be sa_handler. There is a flags check missing here. Thanks.

Quoted 7 lines
Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +7 -0
+		else
+			rte_exit(EXIT_FAILURE,
+				 "Failed to handle generic SIGBUS!");
+	}
+
+	RTE_LOG(INFO, EAL, "Success to handle SIGBUS for hot-unplug!\n");
+}
#246 lore raw

Re: [PATCH v12 6/7] eal: add failure handle mechanism for hot-unplug

In reply to Jeff Guo
Quoted 15 lines · Jeff Guo

The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.

By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable

---

<snip>

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +5 -0
+static void sigbus_handler(int signum, siginfo_t *info,
+				void *ctx __rte_unused)
+{
+	int ret;
+

lib/librte_eal/linuxapp/eal/eal_dev.c enum eal_dev_event_subsystem { · line 78

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +10 -0
+	RTE_LOG(INFO, EAL, "Thread[%d] catch SIGBUS, fault address:%p\n",
+		(int)pthread_self(), info->si_addr);
+
+	rte_spinlock_lock(&failure_handle_lock);
+	ret = rte_bus_sigbus_handler(info->si_addr);
+	rte_spinlock_unlock(&failure_handle_lock);
+	if (ret == -1) {
+		rte_exit(EXIT_FAILURE,
+			 "Failed to handle SIGBUS for hot-unplug, "
+			 "(rte_errno: %s)!", strerror(rte_errno));

Do we really want to exit the application on sigbus handle failure?

lib/librte_eal/linuxapp/eal/eal_dev.c enum eal_dev_event_subsystem { · line 87

Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +9 -0
+	} else if (ret == 1) {
+		if (sigbus_action_old.sa_handler)
+			(*(sigbus_action_old.sa_handler))(signum);
+		else
+			rte_exit(EXIT_FAILURE,
+				 "Failed to handle generic SIGBUS!");
+	}
+
+	RTE_LOG(INFO, EAL, "Success to handle SIGBUS for hot-unplug!\n");

Again, does this all need to be with INFO log level? IMO it should be DEBUG.

Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +10 -0
+}
+
+static int cmp_dev_name(const struct rte_device *dev,
+	const void *_name)
+{
+	const char *name = _name;
+
+	return strcmp(dev->name, name);
+}
+
  static int

<snip>

lib/librte_eal/linuxapp/eal/eal_dev.c rte_dev_event_monitor_stop(void) · line 324

Quoted 8 lines

int __rte_experimental

lib/librte_eal/linuxapp/eal/eal_dev.c +1 -0
@@ -220,5 +320,67 @@ rte_dev_event_monitor_stop(void)
  	close(intr_handle.fd);
  	intr_handle.fd = -1;
  	monitor_started = false;
+
  	return 0;

This looks like unintended change.

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +7 -0
  }
+
+int __rte_experimental
+rte_dev_sigbus_handler_register(void)
+{
+	sigset_t mask;
+	struct sigaction action;
+

<snip>

lib/librte_eal/rte_eal_version.map EXPERIMENTAL { · line 285

Quoted 8 lines
lib/librte_eal/rte_eal_version.map +2 -0
@@ -281,6 +281,8 @@ EXPERIMENTAL {
  	rte_dev_event_callback_unregister;
  	rte_dev_event_monitor_start;
  	rte_dev_event_monitor_stop;
+	rte_dev_hotplug_handle_enable;
+	rte_dev_hotplug_handle_disable;

Nitpicking - disable should be above enable, as E follows D in alphabet :)

Quoted 4 lines
lib/librte_eal/rte_eal_version.map +0 -0
  	rte_dev_iterator_init;
  	rte_dev_iterator_next;
  	rte_devargs_add;
Signature · 2 lines
Thanks,
Anatoly
#247 lore raw

Re: [PATCH v12 6/7] eal: add failure handle mechanism for hot-unplug

Quoted 41 lines

-----Original Message-----
From: Burakov, Anatoly
Sent: Tuesday, October 2, 2018 4:54 PM
To: Guo, Jia <[email protected]>; [email protected]; Richardson, Bruce <[email protected]>; Yigit, Ferruh <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Wu, Jingjing <[email protected]>; [email protected]; [email protected]; [email protected]; Van Haaren, Harry <[email protected]>; Zhang, Qi Z <[email protected]>; He, Shaopeng <[email protected]>; Iremonger, Bernard <[email protected]>; [email protected]; Lu, Wenzhuo <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Zhang, Helin <[email protected]>
Subject: Re: [PATCH v12 6/7] eal: add failure handle mechanism for hot-unplug

Quoted 15 lines · Jeff Guo

The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.

By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable

---

<snip>

Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +9 -0
+static void sigbus_handler(int signum, siginfo_t *info,
+				void *ctx __rte_unused)
+{
+	int ret;
+
+	RTE_LOG(INFO, EAL, "Thread[%d] catch SIGBUS, fault address:%p\n",
+		(int)pthread_self(), info->si_addr);
+
+	rte_spinlock_lock(&failure_handle_lock);
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c enum eal_dev_event_subsystem { · line 78

Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +6 -0
+	ret = rte_bus_sigbus_handler(info->si_addr);
+	rte_spinlock_unlock(&failure_handle_lock);
+	if (ret == -1) {
+		rte_exit(EXIT_FAILURE,
+			 "Failed to handle SIGBUS for hot-unplug, "
+			 "(rte_errno: %s)!", strerror(rte_errno));

Do we really want to exit the application on sigbus handle failure?

I'd say yes :)
What else we can do in such situation, except then die gracefully? Konstantin

Quoted 69 lines

lib/librte_eal/linuxapp/eal/eal_dev.c enum eal_dev_event_subsystem { · line 87

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +9 -0
+	} else if (ret == 1) {
+		if (sigbus_action_old.sa_handler)
+			(*(sigbus_action_old.sa_handler))(signum);
+		else
+			rte_exit(EXIT_FAILURE,
+				 "Failed to handle generic SIGBUS!");
+	}
+
+	RTE_LOG(INFO, EAL, "Success to handle SIGBUS for hot-unplug!\n");

Again, does this all need to be with INFO log level? IMO it should be DEBUG.

Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +10 -0
+}
+
+static int cmp_dev_name(const struct rte_device *dev,
+	const void *_name)
+{
+	const char *name = _name;
+
+	return strcmp(dev->name, name);
+}
+
  static int

<snip>

lib/librte_eal/linuxapp/eal/eal_dev.c rte_dev_event_monitor_stop(void) · line 324

Quoted 8 lines

int __rte_experimental

lib/librte_eal/linuxapp/eal/eal_dev.c +1 -0
@@ -220,5 +320,67 @@ rte_dev_event_monitor_stop(void)
  	close(intr_handle.fd);
  	intr_handle.fd = -1;
  	monitor_started = false;
+
  	return 0;

This looks like unintended change.

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +7 -0
  }
+
+int __rte_experimental
+rte_dev_sigbus_handler_register(void)
+{
+	sigset_t mask;
+	struct sigaction action;
+

<snip>

lib/librte_eal/rte_eal_version.map EXPERIMENTAL { · line 285

Quoted 8 lines
lib/librte_eal/rte_eal_version.map +2 -0
@@ -281,6 +281,8 @@ EXPERIMENTAL {
  	rte_dev_event_callback_unregister;
  	rte_dev_event_monitor_start;
  	rte_dev_event_monitor_stop;
+	rte_dev_hotplug_handle_enable;
+	rte_dev_hotplug_handle_disable;

Nitpicking - disable should be above enable, as E follows D in alphabet :)

lib/librte_eal/rte_eal_version.map EXPERIMENTAL { · line 288

Quoted 4 lines
lib/librte_eal/rte_eal_version.map +0 -0
  	rte_dev_iterator_init;
  	rte_dev_iterator_next;
  	rte_devargs_add;

--
Thanks,
Anatoly

author
#248 lore raw

Re: [PATCH v12 6/7] eal: add failure handle mechanism for hot-unplug

In reply to Burakov, Anatoly
Quoted 28 lines · Anatoly
Quoted 15 lines · Jeff Guo

The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.

By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
   - rte_dev_hotplug_handle_enable
   - rte_dev_hotplug_handle_disable

---

<snip>

Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +9 -0
+static void sigbus_handler(int signum, siginfo_t *info,
+                void *ctx __rte_unused)
+{
+    int ret;
+
+    RTE_LOG(INFO, EAL, "Thread[%d] catch SIGBUS, fault address:%p\n",
+        (int)pthread_self(), info->si_addr);
+
+    rte_spinlock_lock(&failure_handle_lock);
Quoted 9 lines
Quoted 6 lines

+    ret = rte_bus_sigbus_handler(info->si_addr); +    rte_spinlock_unlock(&failure_handle_lock); +    if (ret == -1) {
+        rte_exit(EXIT_FAILURE,
+             "Failed to handle SIGBUS for hot-unplug, " +             "(rte_errno: %s)!", strerror(rte_errno));

Do we really want to exit the application on sigbus handle failure?

Definitely yes we want, since it is a failure of the process. Agree with Konstantin reply on other mail.

Quoted 10 lines
Quoted 9 lines

+    } else if (ret == 1) {
+        if (sigbus_action_old.sa_handler)
+            (*(sigbus_action_old.sa_handler))(signum);
+        else
+            rte_exit(EXIT_FAILURE,
+                 "Failed to handle generic SIGBUS!");
+    }
+
+    RTE_LOG(INFO, EAL, "Success to handle SIGBUS for hot-unplug!\n");

Quoted 2 lines

Again, does this all need to be with INFO log level? IMO it should be DEBUG.

I am fine for that.

Quoted 14 lines
Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +10 -0
+}
+
+static int cmp_dev_name(const struct rte_device *dev,
+    const void *_name)
+{
+    const char *name = _name;
+
+    return strcmp(dev->name, name);
+}
+

  static int

<snip>

Quoted 10 lines

lib/librte_eal/linuxapp/eal/eal_dev.c rte_dev_event_monitor_stop(void) · line 324

Quoted 7 lines

    int __rte_experimental

lib/librte_eal/linuxapp/eal/eal_dev.c +1 -0
@@ -220,5 +320,67 @@ rte_dev_event_monitor_stop(void)
       close(intr_handle.fd);
       intr_handle.fd = -1;
       monitor_started = false;
+
       return 0;

This looks like unintended change.

No, i intended to change it to consistent with the other format.

Quoted 11 lines
Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +7 -0
   }
+
+int __rte_experimental
+rte_dev_sigbus_handler_register(void)
+{
+    sigset_t mask;
+    struct sigaction action;
+

<snip>

Quoted 11 lines

lib/librte_eal/rte_eal_version.map EXPERIMENTAL { · line 285

Quoted 8 lines
lib/librte_eal/rte_eal_version.map +2 -0
@@ -281,6 +281,8 @@ EXPERIMENTAL {
       rte_dev_event_callback_unregister;
       rte_dev_event_monitor_start;
       rte_dev_event_monitor_stop;
+    rte_dev_hotplug_handle_enable;
+    rte_dev_hotplug_handle_disable;

Nitpicking - disable should be above enable, as E follows D in alphabet :)

yes, after recheck with alphabet, it definitely like what you said. :).

Quoted 6 lines
Quoted 4 lines
lib/librte_eal/rte_eal_version.map +0 -0
       rte_dev_iterator_init;
       rte_dev_iterator_next;
       rte_devargs_add;
author
#249 lore raw

PATCH v12 7/7 testpmd: use hot-unplug failure handle mechanism

In reply to Jeff Guo

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

---
v12->v11:
no change.

---
 app/test-pmd/testpmd.c | 39 +++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)
app/test-pmd/testpmd.c +31 -8
index 001f0e5..bfef483 100644
@@ -2093,14 +2093,22 @@ pmd_test_exit(void)
 
 	if (hot_plug) {
 		ret = rte_dev_event_monitor_stop();
-		if (ret)
+		if (ret) {
 			RTE_LOG(ERR, EAL,
 				"fail to stop device event monitor.");
+			return;
+		}
 
 		ret = eth_dev_event_callback_unregister();
 		if (ret)
+			return;
+
+		ret = rte_dev_hotplug_handle_disable();
+		if (ret) {
 			RTE_LOG(ERR, EAL,
-				"fail to unregister all event callbacks.");
+				"fail to disable hotplug handling.");
+			return;
+		}
 	}
 
 	printf("\nBye...\n");
@@ -2244,6 +2252,9 @@ static void
 eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 			     __rte_unused void *arg)
 {
+	uint16_t port_id;
+	int ret;
+
 	if (type >= RTE_DEV_EVENT_MAX) {
 		fprintf(stderr, "%s called upon invalid event %d\n",
 			__func__, type);
@@ -2254,9 +2265,12 @@ eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 	case RTE_DEV_EVENT_REMOVE:
 		RTE_LOG(ERR, EAL, "The device: %s has been removed!\n",
 			device_name);
-		/* TODO: After finish failure handle, begin to stop
-		 * packet forward, stop port, close port, detach port.
-		 */
+		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
+		if (ret) {
+			printf("can not get port by device %s!\n", device_name);
+			return;
+		}
+		rmv_event_callback((void *)(intptr_t)port_id);
 		break;
 	case RTE_DEV_EVENT_ADD:
 		RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
@@ -2779,14 +2793,23 @@ main(int argc, char** argv)
 	init_config();
 
 	if (hot_plug) {
-		/* enable hot plug monitoring */
+		ret = rte_dev_hotplug_handle_enable();
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail to enable hotplug handling.");
+			return -1;
+		}
+
 		ret = rte_dev_event_monitor_start();
 		if (ret) {
-			rte_errno = EINVAL;
+			RTE_LOG(ERR, EAL,
+				"fail to start device event monitoring.");
 			return -1;
 		}
-		eth_dev_event_callback_register();
 
+		ret = eth_dev_event_callback_register();
+		if (ret)
+			return -1;
 	}
 
 	if (start_port(RTE_PORT_ALL) != 0)
Signature · 1 line
2.7.4
#250 lore raw

Re: [PATCH v12 7/7] testpmd: use hot-unplug failure handle mechanism

Hi Jeff,

<snip>

Quoted 1 line

Subject: [PATCH v12 7/7] testpmd: use hot-unplug failure handle mechanism

./devtools/check-git-log.sh -1
Wrong headline label:
testpmd: use hot-unplug failure handle mechanism

Quoted 23 lines

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot- unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

---
v12->v11:
no change.

---
 app/test-pmd/testpmd.c | 39 +++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)
app/test-pmd/testpmd.c index renamed from app/test-pmd/testpmd.c +0 -0

001f0e5..bfef483 100644

app/test-pmd/testpmd.c +0 -0
@@ -2093,14 +2093,22 @@ pmd_test_exit(void)
 
 	if (hot_plug) {

app/test-pmd/testpmd.c pmd_test_exit(void) · line 2103

Quoted 10 lines
app/test-pmd/testpmd.c +3 -1
 		ret = rte_dev_event_monitor_stop();
-		if (ret)
+		if (ret) {
 			RTE_LOG(ERR, EAL,
 				"fail to stop device event monitor.");
+			return;
+		}
 
 		ret = eth_dev_event_callback_unregister();
 		if (ret)

Should there be an RTE_LOG() call here?

Quoted 24 lines
app/test-pmd/testpmd.c +10 -1
+			return;
+
+		ret = rte_dev_hotplug_handle_disable();
+		if (ret) {
 			RTE_LOG(ERR, EAL,
-				"fail to unregister all event callbacks.");
+				"fail to disable hotplug handling.");
+			return;
+		}
 	}
 
 	printf("\nBye...\n");
@@ -2244,6 +2252,9 @@ static void
 eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 			     __rte_unused void *arg)
 {
+	uint16_t port_id;
+	int ret;
+
 	if (type >= RTE_DEV_EVENT_MAX) {
 		fprintf(stderr, "%s called upon invalid event %d\n",
 			__func__, type);
@@ -2254,9 +2265,12 @@ eth_dev_event_callback(char *device_name, enum
 rte_dev_event_type type,

app/test-pmd/testpmd.c eth_dev_event_callback(char *device_name, enum · line 2272

Quoted 10 lines
app/test-pmd/testpmd.c +3 -3
 	case RTE_DEV_EVENT_REMOVE:
 		RTE_LOG(ERR, EAL, "The device: %s has been removed!\n",
 			device_name);
-		/* TODO: After finish failure handle, begin to stop
-		 * packet forward, stop port, close port, detach port.
-		 */
+		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
+		if (ret) {
+			printf("can not get port by device %s!\n",
 device_name);

It would be better to use an RTE_LOG() call here instead of printf().

Quoted 19 lines
app/test-pmd/testpmd.c +3 -0
+			return;
+		}
+		rmv_event_callback((void *)(intptr_t)port_id);
 		break;
app/test-pmd/testpmd.c +0 -0
 	case RTE_DEV_EVENT_ADD:
 		RTE_LOG(ERR, EAL, "The device: %s has been added!\n", @@ -

2779,14 +2793,23 @@ main(int argc, char** argv)

app/test-pmd/testpmd.c +7 -1
 	init_config();
 
 	if (hot_plug) {
-		/* enable hot plug monitoring */
+		ret = rte_dev_hotplug_handle_enable();
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail to enable hotplug handling.");
+			return -1;
+		}
+
 		ret = rte_dev_event_monitor_start();

app/test-pmd/testpmd.c main(int argc, char** argv) · line 2811

Quoted 10 lines
app/test-pmd/testpmd.c +4 -2
 		if (ret) {
-			rte_errno = EINVAL;
+			RTE_LOG(ERR, EAL,
+				"fail to start device event monitoring.");
 			return -1;
 		}
-		eth_dev_event_callback_register();
 
+		ret = eth_dev_event_callback_register();
+		if (ret)

Should there be an RTE_LOG() call here?

Quoted 6 lines
app/test-pmd/testpmd.c +1 -1
+			return -1;
 	}
 
 	if (start_port(RTE_PORT_ALL) != 0)
--

2.7.4

Regards,

Bernard.

author
#251 lore raw

Re: [PATCH v12 7/7] testpmd: use hot-unplug failure handle mechanism

hi, bernard

thanks for your review, comment as below.

Quoted 9 lines · Bernard

Hi Jeff,

<snip>

Quoted 1 line

Subject: [PATCH v12 7/7] testpmd: use hot-unplug failure handle mechanism

./devtools/check-git-log.sh -1
Wrong headline label:
testpmd: use hot-unplug failure handle mechanism

ok, let me check it.

Quoted 31 lines
Quoted 31 lines

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot- unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

---
v12->v11:
no change.

---
  app/test-pmd/testpmd.c | 39 +++++++++++++++++++++++++++++++--------
  1 file changed, 31 insertions(+), 8 deletions(-)
app/test-pmd/testpmd.c index renamed from app/test-pmd/testpmd.c +0 -0

001f0e5..bfef483 100644

app/test-pmd/testpmd.c +3 -1
@@ -2093,14 +2093,22 @@ pmd_test_exit(void)
 
  	if (hot_plug) {
  		ret = rte_dev_event_monitor_stop();
-		if (ret)
+		if (ret) {
  			RTE_LOG(ERR, EAL,
  				"fail to stop device event monitor.");
+			return;
+		}
 
Quoted 3 lines

app/test-pmd/testpmd.c pmd_test_exit(void) · line 2103

Quoted 2 lines
app/test-pmd/testpmd.c +0 -0
  		ret = eth_dev_event_callback_unregister();
  		if (ret)

Should there be an RTE_LOG() call here?

ok, in order to make it more clean, no need to add help here.

Quoted 27 lines
Quoted 27 lines
app/test-pmd/testpmd.c +10 -1
+			return;
+
+		ret = rte_dev_hotplug_handle_disable();
+		if (ret) {
  			RTE_LOG(ERR, EAL,
-				"fail to unregister all event callbacks.");
+				"fail to disable hotplug handling.");
+			return;
+		}
  	}
 
  	printf("\nBye...\n");
@@ -2244,6 +2252,9 @@ static void
  eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
  			     __rte_unused void *arg)
  {
+	uint16_t port_id;
+	int ret;
+
  	if (type >= RTE_DEV_EVENT_MAX) {
  		fprintf(stderr, "%s called upon invalid event %d\n",
  			__func__, type);
@@ -2254,9 +2265,12 @@ eth_dev_event_callback(char *device_name, enum
 rte_dev_event_type type,
  	case RTE_DEV_EVENT_REMOVE:
  		RTE_LOG(ERR, EAL, "The device: %s has been removed!\n",
  			device_name);
Quoted 8 lines

app/test-pmd/testpmd.c eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,

Quoted 7 lines
app/test-pmd/testpmd.c +3 -3
-		/* TODO: After finish failure handle, begin to stop
-		 * packet forward, stop port, close port, detach port.
-		 */
+		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
+		if (ret) {
+			printf("can not get port by device %s!\n",
 device_name);

It would be better to use an RTE_LOG() call here instead of printf().

ok.

Quoted 27 lines
Quoted 27 lines
app/test-pmd/testpmd.c +3 -0
+			return;
+		}
+		rmv_event_callback((void *)(intptr_t)port_id);
  		break;

case RTE_DEV_EVENT_ADD:
RTE_LOG(ERR, EAL, "The device: %s has been added!\n", @@ -
2779,14 +2793,23 @@ main(int argc, char** argv)
init_config();

  	if (hot_plug) {
app/test-pmd/testpmd.c +9 -3
-		/* enable hot plug monitoring */
+		ret = rte_dev_hotplug_handle_enable();
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail to enable hotplug handling.");
+			return -1;
+		}
+
  		ret = rte_dev_event_monitor_start();
  		if (ret) {
-			rte_errno = EINVAL;
+			RTE_LOG(ERR, EAL,
+				"fail to start device event monitoring.");
  			return -1;
  		}
-		eth_dev_event_callback_register();
Quoted 3 lines

app/test-pmd/testpmd.c main(int argc, char** argv) · line 2811

Quoted 2 lines
app/test-pmd/testpmd.c +2 -0
+		ret = eth_dev_event_callback_register();
+		if (ret)

Should there be an RTE_LOG() call here?

please see above answer.

Quoted 9 lines
Quoted 6 lines
app/test-pmd/testpmd.c +1 -1
+			return -1;
  	}
 
  	if (start_port(RTE_PORT_ALL) != 0)
--

2.7.4

Regards,

Bernard.

author
#252 lore raw

v13 cover hot-unplug failure handle mechanism

In reply to Jeff Guo

Hotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.

We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.

In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.

In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.

The mechanism should work as below:

First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.

For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.

The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.

This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.

patchset history:
v13->v12:
use local variable to rewrite the func to be more readable. add sa_flag check when invoke generic sigbus handler modify some typo
delete needless helper in app

v12->v11:
add and delete some checking about sigbus recover.

v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move the igb_uio fixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.

v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log

v9->v8:
refine commit log to be more readable.

v8->v7:
refine errno process in sigbus handler.
refine igb uio release process

v7->v6:
delete some unused part

v6->v5:
refine some description about bus ops
refine commit log
add some entry check.

v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.

v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.

v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.

v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.

Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.

"v1(21)" == v21 as below:
v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.

v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.

v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.

v18->v15:
add document, add signal bus handler, refine the code to be more clear.

the prior patch history please check the patch set "add device event monitor framework".

Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug app/testpmd: use hotplug failure handler

 app/test-pmd/testpmd.c                  |  86 ++++++++--------
 doc/guides/rel_notes/release_18_08.rst  |   5 +
 drivers/bus/pci/pci_common.c            |  82 +++++++++++++++
 drivers/bus/pci/pci_common_uio.c        |  33 +++++++
 drivers/bus/pci/private.h               |  12 +++
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  14 +++
 lib/librte_eal/common/eal_common_bus.c  |  43 ++++++++
 lib/librte_eal/common/eal_private.h     |  39 ++++++++
 lib/librte_eal/common/include/rte_bus.h |  34 +++++++
 lib/librte_eal/common/include/rte_dev.h |  26 +++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 170 +++++++++++++++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map      |   2 +
 12 files changed, 499 insertions(+), 47 deletions(-)
Signature · 1 line
2.7.4
author
#253 lore raw

PATCH v13 1/7 bus: add hot-unplug handler

A hot-unplug failure and app crash can be caused, when a device is hot-unplugged but the application still try to access the device by reading or writing from the BARs, which is already invalid but still not timely be unmap or released.

This patch introduces bus ops to handle hot-unplug failures. Each bus can implement its own case-dependent logic to handle the failures.

---
v13->v12:
no change.

---
 lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
index b7b5b08..1bb53dc 100644
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);
 typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 
 /**
+ * Implement a specific hot-unplug handler, which is responsible for
+ * handle the failure when device be hot-unplugged. When the event of
+ * hot-unplug be detected, it could call this function to handle
+ * the hot-unplug failure and avoid app crash.
+ * @param dev
+ *	Pointer of the device structure.
+ *
+ * @return
+ *	0 on success.
+ *	!0 on error.
+ */
+typedef int (*rte_bus_hot_unplug_handler_t)(struct rte_device *dev);
+
+/**
  * Bus scan policies
  */
 enum rte_bus_scan_mode {
@@ -212,6 +226,8 @@ struct rte_bus {
 	struct rte_bus_conf conf;    /**< Bus configuration */
 	rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu class */
 	rte_dev_iterate_t dev_iterate; /**< Device iterator. */
+	rte_bus_hot_unplug_handler_t hot_unplug_handler;
+				/**< handle hot-unplug failure on the bus */
 };
 
 /**
Signature · 1 line
2.7.4
author
#254 lore raw

PATCH v13 2/7 bus/pci: implement hot-unplug handler ops

In reply to Jeff Guo

This patch implements the ops to handle hot-unplug on the PCI bus. For UIO PCI, it could avoids BARs read/write errors by creating a new dummy memory to remap the memory where the failure is. For VFIO or other kernel driver, it could specific implement function to handle hot-unplug case by case.

---
v13->v12:
no change.

---
 drivers/bus/pci/pci_common.c     | 28 ++++++++++++++++++++++++++++
 drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++
 drivers/bus/pci/private.h        | 12 ++++++++++++
 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
index 7736b3f..d286234 100644
@@ -406,6 +406,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 }
 
 static int
+pci_hot_unplug_handler(struct rte_device *dev)
+{
+	struct rte_pci_device *pdev = NULL;
+	int ret = 0;
+
+	pdev = RTE_DEV_TO_PCI(dev);
+	if (!pdev)
+		return -1;
+
+	switch (pdev->kdrv) {
+	case RTE_KDRV_IGB_UIO:
+	case RTE_KDRV_UIO_GENERIC:
+	case RTE_KDRV_NIC_UIO:
+		/* BARs resource is invalid, remap it to be safe. */
+		ret = pci_uio_remap_resource(pdev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"Not managed by a supported kernel driver, skipped\n");
+		ret = -1;
+		break;
+	}
+
+	return ret;
+}
+
+static int
 pci_plug(struct rte_device *dev)
 {
 	return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
@@ -435,6 +462,7 @@ struct rte_pci_bus rte_pci_bus = {
 		.unplug = pci_unplug,
 		.parse = pci_parse,
 		.get_iommu_class = rte_pci_get_iommu_class,
+		.hot_unplug_handler = pci_hot_unplug_handler,
 	},
 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
index 54bc20b..7ea73db 100644
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
 	}
 }
 
+/* remap the PCI resource of a PCI device in anonymous virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	void *map_address;
+
+	if (dev == NULL)
+		return -1;
+
+	/* Remap all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		if (dev->mem_resource[i].phys_addr == 0)
+			continue;
+		map_address = mmap(dev->mem_resource[i].addr,
+				(size_t)dev->mem_resource[i].len,
+				PROT_READ | PROT_WRITE,
+				MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+		if (map_address == MAP_FAILED) {
+			RTE_LOG(ERR, EAL,
+				"Cannot remap resource for device %s\n",
+				dev->name);
+			return -1;
+		}
+		RTE_LOG(INFO, EAL,
+			"Successful remap resource for device %s\n",
+			dev->name);
+	}
+
+	return 0;
+}
+
 static struct mapped_pci_resource *
 pci_uio_find_resource(struct rte_pci_device *dev)
 {
drivers/bus/pci/private.h +12 -0
index 8ddd03e..6b312e5 100644
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
 
 /**
+ * Remap the PCI resource of a PCI device in anonymous virtual memory.
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
  * Map device memory to uio resource
  *
  * This function is private to EAL.
Signature · 1 line
2.7.4
author
#255 lore raw

PATCH v13 3/7 bus: add sigbus handler

In reply to Jeff Guo

When a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.

This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.

---
v13->v12:
reword the ops comment.

---
 lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
lib/librte_eal/common/include/rte_bus.h +18 -0
index 1bb53dc..6be4b5c 100644
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 typedef int (*rte_bus_hot_unplug_handler_t)(struct rte_device *dev);
 
 /**
+ * Implement a specific sigbus handler, which is responsible for handling
+ * the sigbus error which is either original memory error, or specific memory
+ * error that caused of device be hot-unplugged. When sigbus error be captured,
+ * it could call this function to handle sigbus error.
+ * @param failure_addr
+ *	Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ *	0 for success handle the sigbus for hot-unplug.
+ *	1 for not process it, because it is a generic sigbus error.
+ *	-1 for failed to handle the sigbus for hot-unplug.
+ */
+typedef int (*rte_bus_sigbus_handler_t)(const void *failure_addr);
+
+/**
  * Bus scan policies
  */
 enum rte_bus_scan_mode {
@@ -228,6 +243,9 @@ struct rte_bus {
 	rte_dev_iterate_t dev_iterate; /**< Device iterator. */
 	rte_bus_hot_unplug_handler_t hot_unplug_handler;
 				/**< handle hot-unplug failure on the bus */
+	rte_bus_sigbus_handler_t sigbus_handler;
+					/**< handle sigbus error on the bus */
+
 };
 
 /**
Signature · 1 line
2.7.4
author
#256 lore raw

PATCH v13 4/7 bus/pci: implement sigbus handler ops

In reply to Jeff Guo

This patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.

---
v13->v12:
use local variable to rewrite the func to be more readable.

---
 drivers/bus/pci/pci_common.c | 54 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/bus/pci/pci_common.c +54 -0
index d286234..b71f63a 100644
@@ -405,6 +405,37 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 	return NULL;
 }
 
+/*
+ * find the device which encounter the failure, by iterate over all device on
+ * PCI bus to check if the memory failure address is located in the range
+ * of the BARs of the device.
+ */
+static struct rte_pci_device *
+pci_find_device_by_addr(const void *failure_addr)
+{
+	struct rte_pci_device *pdev = NULL;
+	uint64_t check_point, start, end, len;
+	int i;
+
+	check_point = (uint64_t)(uintptr_t)failure_addr;
+
+	FOREACH_DEVICE_ON_PCIBUS(pdev) {
+		start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
+		len = pdev->mem_resource[i].len;
+		end = (uint64_t)(uintptr_t)RTE_PTR_ADD(start, len);
+
+		for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
+			if (check_point >= start && check_point < end) {
+				RTE_LOG(DEBUG, EAL,
+					"Failure address %16.16"PRIx64" belongs to device %s!\n",
+					check_point, pdev->device.name);
+				return pdev;
+			}
+		}
+	}
+	return NULL;
+}
+
 static int
 pci_hot_unplug_handler(struct rte_device *dev)
 {
@@ -433,6 +464,28 @@ pci_hot_unplug_handler(struct rte_device *dev)
 }
 
 static int
+pci_sigbus_handler(const void *failure_addr)
+{
+	struct rte_pci_device *pdev = NULL;
+	int ret = 0;
+
+	pdev = pci_find_device_by_addr(failure_addr);
+	if (!pdev) {
+		/* It is a generic sigbus error, no bus would handle it. */
+		ret = 1;
+	} else {
+		/* The sigbus error is caused of hot-unplug. */
+		ret = pci_hot_unplug_handler(&pdev->device);
+		if (ret) {
+			RTE_LOG(ERR, EAL, "Failed to handle hot-unplug for "
+				"device %s", pdev->name);
+			ret = -1;
+		}
+	}
+	return ret;
+}
+
+static int
 pci_plug(struct rte_device *dev)
 {
 	return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
@@ -463,6 +516,7 @@ struct rte_pci_bus rte_pci_bus = {
 		.parse = pci_parse,
 		.get_iommu_class = rte_pci_get_iommu_class,
 		.hot_unplug_handler = pci_hot_unplug_handler,
+		.sigbus_handler = pci_sigbus_handler,
 	},
 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
author
#257 lore raw

PATCH v13 5/7 bus: add helper to handle sigbus

In reply to Jeff Guo

This patch aims to add a helper to iterate over all buses to find the relevant bus to handle the sigbus error.

---
v13->v12:
no change.

---
 lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++
 lib/librte_eal/common/eal_private.h    | 13 ++++++++++
 2 files changed, 56 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
index 0943851..62b7318 100644
@@ -37,6 +37,7 @@
 #include <rte_bus.h>
 #include <rte_debug.h>
 #include <rte_string_fns.h>
+#include <rte_errno.h>
 
 #include "eal_private.h"
 
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void)
 	}
 	return mode;
 }
+
+static int
+bus_handle_sigbus(const struct rte_bus *bus,
+			const void *failure_addr)
+{
+	int ret;
+
+	if (!bus->sigbus_handler)
+		return -1;
+
+	ret = bus->sigbus_handler(failure_addr);
+
+	/* find bus but handle failed, keep the errno be set. */
+	if (ret < 0 && rte_errno == 0)
+		rte_errno = ENOTSUP;
+
+	return ret > 0;
+}
+
+int
+rte_bus_sigbus_handler(const void *failure_addr)
+{
+	struct rte_bus *bus;
+
+	int ret = 0;
+	int old_errno = rte_errno;
+
+	rte_errno = 0;
+
+	bus = rte_bus_find(NULL, bus_handle_sigbus, failure_addr);
+	/* can not find bus. */
+	if (!bus)
+		return 1;
+	/* find bus but handle failed, pass on the new errno. */
+	else if (rte_errno != 0)
+		return -1;
+
+	/* restore the old errno. */
+	rte_errno = old_errno;
+
+	return ret;
+}
lib/librte_eal/common/eal_private.h +13 -0
index 4f809a8..a2d1528 100644
@@ -304,4 +304,17 @@ int
 rte_devargs_layers_parse(struct rte_devargs *devargs,
 			 const char *devstr);
 
+/**
+ * Iterate over all buses to find the corresponding bus to handle the sigbus
+ * error.
+ * @param failure_addr
+ *	Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ *	 0 success to handle the sigbus.
+ *	-1 failed to handle the sigbus
+ *	 1 no bus can handler the sigbus
+ */
+int rte_bus_sigbus_handler(const void *failure_addr);
+
 #endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
author
#258 lore raw

PATCH v13 6/7 eal: add failure handle mechanism for hot-unplug

In reply to Jeff Guo

The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.

By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable

---
v13->v12:
add sa_flags check when invoke generic sigbus handler modify some typo

---
 doc/guides/rel_notes/release_18_08.rst  |   5 +
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  14 +++
 lib/librte_eal/common/eal_private.h     |  26 +++++
 lib/librte_eal/common/include/rte_dev.h |  26 +++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 170 +++++++++++++++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map      |   2 +
 6 files changed, 242 insertions(+), 1 deletion(-)
doc/guides/rel_notes/release_18_08.rst +5 -0
index 321fa84..fe0e60f 100644
@@ -117,6 +117,11 @@ New Features
 
   Added support for chained mbufs (input and output).
 
+* **Added hot-unplug handle mechanism.**
+
+  ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are
+  for enabling or disabling hotplug handle mechanism.
+
 
 API Changes
 -----------
lib/librte_eal/bsdapp/eal/eal_dev.c +14 -0
index 1c6c51b..255d611 100644
@@ -19,3 +19,17 @@ 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_hotplug_handle_enable(void)
+{
+	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
+	return -1;
+}
+
+int __rte_experimental
+rte_dev_hotplug_handle_disable(void)
+{
+	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
+	return -1;
+}
lib/librte_eal/common/eal_private.h +26 -0
index a2d1528..637f20d 100644
@@ -317,4 +317,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,
  */
 int rte_bus_sigbus_handler(const void *failure_addr);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Register the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_sigbus_handler_register(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Unregister the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_sigbus_handler_unregister(void);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_dev.h +26 -0
index b80a805..ff580a0 100644
@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void);
 int __rte_experimental
 rte_dev_event_monitor_stop(void);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Enable hotplug handling for devices.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_hotplug_handle_enable(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Disable hotplug handling for devices.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_hotplug_handle_disable(void);
+
 #endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/eal_dev.c +169 -1

215 lines not on this page · raw message

lib/librte_eal/rte_eal_version.map +2 -0
index 73282bb..b167b8f 100644
@@ -281,6 +281,8 @@ EXPERIMENTAL {
 	rte_dev_event_callback_unregister;
 	rte_dev_event_monitor_start;
 	rte_dev_event_monitor_stop;
+	rte_dev_hotplug_handle_disable;
+	rte_dev_hotplug_handle_enable;
 	rte_dev_iterator_init;
 	rte_dev_iterator_next;
 	rte_devargs_add;
Signature · 1 line
2.7.4
author
#259 lore raw

PATCH v13 7/7 app/testpmd: use hotplug failure handler

In reply to Jeff Guo

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except that app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

---
v13->v12:
delete needless helper in app.

---
 app/test-pmd/testpmd.c | 86 +++++++++++++++++++++++---------------------------
 1 file changed, 40 insertions(+), 46 deletions(-)
app/test-pmd/testpmd.c +40 -46
index 001f0e5..f3f8e44 100644
@@ -434,9 +434,6 @@ static int eth_event_callback(portid_t port_id,
 static void eth_dev_event_callback(char *device_name,
 				enum rte_dev_event_type type,
 				void *param);
-static int eth_dev_event_callback_register(void);
-static int eth_dev_event_callback_unregister(void);
-
 
 /*
  * Check if all the ports are started.
@@ -1954,39 +1951,6 @@ reset_port(portid_t pid)
 	printf("Done\n");
 }
 
-static int
-eth_dev_event_callback_register(void)
-{
-	int ret;
-
-	/* register the device event callback */
-	ret = rte_dev_event_callback_register(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret) {
-		printf("Failed to register device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
-
-static int
-eth_dev_event_callback_unregister(void)
-{
-	int ret;
-
-	/* unregister the device event callback */
-	ret = rte_dev_event_callback_unregister(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret < 0) {
-		printf("Failed to unregister device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
 void
 attach_port(char *identifier)
 {
@@ -2093,14 +2057,25 @@ pmd_test_exit(void)
 
 	if (hot_plug) {
 		ret = rte_dev_event_monitor_stop();
-		if (ret)
+		if (ret) {
 			RTE_LOG(ERR, EAL,
 				"fail to stop device event monitor.");
+			return;
+		}
 
-		ret = eth_dev_event_callback_unregister();
-		if (ret)
+		ret = rte_dev_event_callback_unregister(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret < 0) {
+			printf("fail to unregister device event callback.\n");
+			return;
+		}
+
+		ret = rte_dev_hotplug_handle_disable();
+		if (ret) {
 			RTE_LOG(ERR, EAL,
-				"fail to unregister all event callbacks.");
+				"fail to disable hotplug handling.\n");
+			return;
+		}
 	}
 
 	printf("\nBye...\n");
@@ -2244,6 +2219,9 @@ static void
 eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 			     __rte_unused void *arg)
 {
+	uint16_t port_id;
+	int ret;
+
 	if (type >= RTE_DEV_EVENT_MAX) {
 		fprintf(stderr, "%s called upon invalid event %d\n",
 			__func__, type);
@@ -2254,9 +2232,13 @@ eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 	case RTE_DEV_EVENT_REMOVE:
 		RTE_LOG(ERR, EAL, "The device: %s has been removed!\n",
 			device_name);
-		/* TODO: After finish failure handle, begin to stop
-		 * packet forward, stop port, close port, detach port.
-		 */
+		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
+		if (ret) {
+			RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
+				device_name);
+			return;
+		}
+		rmv_event_callback((void *)(intptr_t)port_id);
 		break;
 	case RTE_DEV_EVENT_ADD:
 		RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
@@ -2779,14 +2761,26 @@ main(int argc, char** argv)
 	init_config();
 
 	if (hot_plug) {
-		/* enable hot plug monitoring */
+		ret = rte_dev_hotplug_handle_enable();
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail to enable hotplug handling.");
+			return -1;
+		}
+
 		ret = rte_dev_event_monitor_start();
 		if (ret) {
-			rte_errno = EINVAL;
+			RTE_LOG(ERR, EAL,
+				"fail to start device event monitoring.");
 			return -1;
 		}
-		eth_dev_event_callback_register();
 
+		ret = rte_dev_event_callback_register(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret) {
+			printf("faile to register device event callback\n");
+			return -1;
+		}
 	}
 
 	if (start_port(RTE_PORT_ALL) != 0)
Signature · 1 line
2.7.4
#260 lore raw

Re: [PATCH v13 7/7] app/testpmd: use hotplug failure handler

Hi Jeff,

Quoted 95 lines

-----Original Message-----
From: Guo, Jia
Sent: Thursday, October 4, 2018 7:31 AM
To: [email protected]; Richardson, Bruce <[email protected]>; Yigit, Ferruh <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Wu, Jingjing <[email protected]>; [email protected]; [email protected]; [email protected]; Van Haaren, Harry <[email protected]>; Zhang, Qi Z <[email protected]>; He, Shaopeng <[email protected]>; Iremonger, Bernard <[email protected]>; [email protected]; Lu, Wenzhuo <[email protected]>; Burakov, Anatoly <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Guo, Jia <[email protected]>; Zhang, Helin <[email protected]>
Subject: [PATCH v13 7/7] app/testpmd: use hotplug failure handler

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except that app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

---
v13->v12:
delete needless helper in app.

---
 app/test-pmd/testpmd.c | 86 +++++++++++++++++++++++--------------------------
(quoted diff) +0 -1
-
 1 file changed, 40 insertions(+), 46 deletions(-)
app/test-pmd/testpmd.c index renamed from app/test-pmd/testpmd.c +0 -0

001f0e5..f3f8e44 100644

app/test-pmd/testpmd.c +1 -37
@@ -434,9 +434,6 @@ static int eth_event_callback(portid_t port_id,  static
 void eth_dev_event_callback(char *device_name,
 				enum rte_dev_event_type type,
 				void *param);
-static int eth_dev_event_callback_register(void);
-static int eth_dev_event_callback_unregister(void);
-
 
 /*
  * Check if all the ports are started.
@@ -1954,39 +1951,6 @@ reset_port(portid_t pid)
 	printf("Done\n");
 }
 
-static int
-eth_dev_event_callback_register(void)
-{
-	int ret;
-
-	/* register the device event callback */
-	ret = rte_dev_event_callback_register(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret) {
-		printf("Failed to register device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
-
-static int
-eth_dev_event_callback_unregister(void)
-{
-	int ret;
-
-	/* unregister the device event callback */
-	ret = rte_dev_event_callback_unregister(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret < 0) {
-		printf("Failed to unregister device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
 void
 attach_port(char *identifier)
 {
@@ -2093,14 +2057,25 @@ pmd_test_exit(void)
 
 	if (hot_plug) {
 		ret = rte_dev_event_monitor_stop();
-		if (ret)
+		if (ret) {
 			RTE_LOG(ERR, EAL,

app/test-pmd/testpmd.c pmd_test_exit(void) · line 2069

Quoted 10 lines
app/test-pmd/testpmd.c +6 -2
 				"fail to stop device event monitor.");
+			return;
+		}
 
-		ret = eth_dev_event_callback_unregister();
-		if (ret)
+		ret = rte_dev_event_callback_unregister(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret < 0) {
+			printf("fail to unregister device event callback.\n");

Better to use RTE_LOG() instead of printf().

Quoted 56 lines
app/test-pmd/testpmd.c +18 -4
+			return;
+		}
+
+		ret = rte_dev_hotplug_handle_disable();
+		if (ret) {
 			RTE_LOG(ERR, EAL,
-				"fail to unregister all event callbacks.");
+				"fail to disable hotplug handling.\n");
+			return;
+		}
 	}
 
 	printf("\nBye...\n");
@@ -2244,6 +2219,9 @@ static void
 eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 			     __rte_unused void *arg)
 {
+	uint16_t port_id;
+	int ret;
+
 	if (type >= RTE_DEV_EVENT_MAX) {
 		fprintf(stderr, "%s called upon invalid event %d\n",
 			__func__, type);
@@ -2254,9 +2232,13 @@ eth_dev_event_callback(char *device_name, enum
 rte_dev_event_type type,
 	case RTE_DEV_EVENT_REMOVE:
 		RTE_LOG(ERR, EAL, "The device: %s has been removed!\n",
 			device_name);
-		/* TODO: After finish failure handle, begin to stop
-		 * packet forward, stop port, close port, detach port.
-		 */
+		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
+		if (ret) {
+			RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
+				device_name);
+			return;
+		}
+		rmv_event_callback((void *)(intptr_t)port_id);
 		break;
 	case RTE_DEV_EVENT_ADD:

RTE_LOG(ERR, EAL, "The device: %s has been added!\n", @@ -
2779,14 +2761,26 @@ main(int argc, char** argv)

app/test-pmd/testpmd.c +7 -2
 	init_config();
 
 	if (hot_plug) {
-		/* enable hot plug monitoring */
+		ret = rte_dev_hotplug_handle_enable();
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail to enable hotplug handling.");
+			return -1;
+		}
+
 		ret = rte_dev_event_monitor_start();
 		if (ret) {
-			rte_errno = EINVAL;

app/test-pmd/testpmd.c main(int argc, char** argv) · line 2781

Quoted 10 lines
app/test-pmd/testpmd.c +6 -1
+			RTE_LOG(ERR, EAL,
+				"fail to start device event monitoring.");
 			return -1;
 		}
-		eth_dev_event_callback_register();
 
+		ret = rte_dev_event_callback_register(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret) {
+			printf("faile to register device event callback\n");

Better to use RTE_LOG() instead of peintf(). Note type in message, "faile" should be "failed"

Quoted 7 lines
app/test-pmd/testpmd.c +2 -1
+			return -1;
+		}
 	}
 
 	if (start_port(RTE_PORT_ALL) != 0)
--

2.7.4

author
#261 lore raw

Re: [PATCH v13 7/7] app/testpmd: use hotplug failure handler

thanks for your kindly review.

Quoted 184 lines · Bernard

Hi Jeff,

Quoted 95 lines

-----Original Message-----
From: Guo, Jia
Sent: Thursday, October 4, 2018 7:31 AM
To: [email protected]; Richardson, Bruce <[email protected]>; Yigit, Ferruh <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Wu, Jingjing <[email protected]>; [email protected]; [email protected]; [email protected]; Van Haaren, Harry <[email protected]>; Zhang, Qi Z <[email protected]>; He, Shaopeng <[email protected]>; Iremonger, Bernard <[email protected]>; [email protected]; Lu, Wenzhuo <[email protected]>; Burakov, Anatoly <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Guo, Jia <[email protected]>; Zhang, Helin <[email protected]>
Subject: [PATCH v13 7/7] app/testpmd: use hotplug failure handler

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except that app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

---
v13->v12:
delete needless helper in app.

---
  app/test-pmd/testpmd.c | 86 +++++++++++++++++++++++--------------------------
(quoted diff) +0 -1
-
  1 file changed, 40 insertions(+), 46 deletions(-)
app/test-pmd/testpmd.c index renamed from app/test-pmd/testpmd.c +0 -0

001f0e5..f3f8e44 100644

app/test-pmd/testpmd.c +1 -37
@@ -434,9 +434,6 @@ static int eth_event_callback(portid_t port_id,  static
 void eth_dev_event_callback(char *device_name,
  				enum rte_dev_event_type type,
  				void *param);
-static int eth_dev_event_callback_register(void);
-static int eth_dev_event_callback_unregister(void);
-
 
  /*
   * Check if all the ports are started.
@@ -1954,39 +1951,6 @@ reset_port(portid_t pid)
  	printf("Done\n");
  }
 
-static int
-eth_dev_event_callback_register(void)
-{
-	int ret;
-
-	/* register the device event callback */
-	ret = rte_dev_event_callback_register(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret) {
-		printf("Failed to register device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
-
-static int
-eth_dev_event_callback_unregister(void)
-{
-	int ret;
-
-	/* unregister the device event callback */
-	ret = rte_dev_event_callback_unregister(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret < 0) {
-		printf("Failed to unregister device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
  void
  attach_port(char *identifier)
  {
@@ -2093,14 +2057,25 @@ pmd_test_exit(void)
 
  	if (hot_plug) {
  		ret = rte_dev_event_monitor_stop();
-		if (ret)
+		if (ret) {
  			RTE_LOG(ERR, EAL,

app/test-pmd/testpmd.c pmd_test_exit(void) · line 2069

Quoted 10 lines
app/test-pmd/testpmd.c +6 -2
  				"fail to stop device event monitor.");
+			return;
+		}
 
-		ret = eth_dev_event_callback_unregister();
-		if (ret)
+		ret = rte_dev_event_callback_unregister(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret < 0) {
+			printf("fail to unregister device event callback.\n");

Better to use RTE_LOG() instead of printf().

Quoted 56 lines
app/test-pmd/testpmd.c +18 -4
+			return;
+		}
+
+		ret = rte_dev_hotplug_handle_disable();
+		if (ret) {
  			RTE_LOG(ERR, EAL,
-				"fail to unregister all event callbacks.");
+				"fail to disable hotplug handling.\n");
+			return;
+		}
  	}
 
  	printf("\nBye...\n");
@@ -2244,6 +2219,9 @@ static void
  eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
  			     __rte_unused void *arg)
  {
+	uint16_t port_id;
+	int ret;
+
  	if (type >= RTE_DEV_EVENT_MAX) {
  		fprintf(stderr, "%s called upon invalid event %d\n",
  			__func__, type);
@@ -2254,9 +2232,13 @@ eth_dev_event_callback(char *device_name, enum
 rte_dev_event_type type,
  	case RTE_DEV_EVENT_REMOVE:
  		RTE_LOG(ERR, EAL, "The device: %s has been removed!\n",
  			device_name);
-		/* TODO: After finish failure handle, begin to stop
-		 * packet forward, stop port, close port, detach port.
-		 */
+		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
+		if (ret) {
+			RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
+				device_name);
+			return;
+		}
+		rmv_event_callback((void *)(intptr_t)port_id);
  		break;
  	case RTE_DEV_EVENT_ADD:

RTE_LOG(ERR, EAL, "The device: %s has been added!\n", @@ -
2779,14 +2761,26 @@ main(int argc, char** argv)
init_config();

  	if (hot_plug) {
app/test-pmd/testpmd.c +7 -2
-		/* enable hot plug monitoring */
+		ret = rte_dev_hotplug_handle_enable();
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail to enable hotplug handling.");
+			return -1;
+		}
+
  		ret = rte_dev_event_monitor_start();
  		if (ret) {
-			rte_errno = EINVAL;

app/test-pmd/testpmd.c main(int argc, char** argv) · line 2781

Quoted 10 lines
app/test-pmd/testpmd.c +6 -1
+			RTE_LOG(ERR, EAL,
+				"fail to start device event monitoring.");
  			return -1;
  		}
-		eth_dev_event_callback_register();
 
+		ret = rte_dev_event_callback_register(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret) {
+			printf("faile to register device event callback\n");

Better to use RTE_LOG() instead of peintf(). Note type in message, "faile" should be "failed"

Quoted 7 lines
app/test-pmd/testpmd.c +2 -1
+			return -1;
+		}
  	}
 
  	if (start_port(RTE_PORT_ALL) != 0)
--

2.7.4

Acked-by on the v13 series
#262 lore raw

Re: [PATCH v13 0/7] hot-unplug failure handle mechanism

In reply to Jeff Guo
Quoted 184 lines

-----Original Message-----
From: Guo, Jia
Sent: Thursday, October 4, 2018 7:31 AM
To: [email protected]; Richardson, Bruce <[email protected]>; Yigit, Ferruh <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Wu, Jingjing <[email protected]>; [email protected]; [email protected]; [email protected]; Van Haaren, Harry <[email protected]>; Zhang, Qi Z <[email protected]>; He, Shaopeng <[email protected]>; Iremonger, Bernard <[email protected]>; [email protected]; Lu, Wenzhuo <[email protected]>; Burakov, Anatoly <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Guo, Jia <[email protected]>; Zhang, Helin <[email protected]>
Subject: [PATCH v13 0/7] hot-unplug failure handle mechanism

Hotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.

We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.

In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.

In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.

The mechanism should work as below:

First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.

For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.

The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.

This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.

patchset history:
v13->v12:
use local variable to rewrite the func to be more readable. add sa_flag check when invoke generic sigbus handler modify some typo
delete needless helper in app

v12->v11:
add and delete some checking about sigbus recover.

v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move the igb_uio fixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.

v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log

v9->v8:
refine commit log to be more readable.

v8->v7:
refine errno process in sigbus handler.
refine igb uio release process

v7->v6:
delete some unused part

v6->v5:
refine some description about bus ops
refine commit log
add some entry check.

v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.

v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.

v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.

v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.

Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.

"v1(21)" == v21 as below:
v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.

v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.

v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.

v18->v15:
add document, add signal bus handler, refine the code to be more clear.

the prior patch history please check the patch set "add device event monitor framework".

Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug app/testpmd: use hotplug failure handler

 app/test-pmd/testpmd.c                  |  86 ++++++++--------
 doc/guides/rel_notes/release_18_08.rst  |   5 +
 drivers/bus/pci/pci_common.c            |  82 +++++++++++++++
 drivers/bus/pci/pci_common_uio.c        |  33 +++++++
 drivers/bus/pci/private.h               |  12 +++
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  14 +++
 lib/librte_eal/common/eal_common_bus.c  |  43 ++++++++
 lib/librte_eal/common/eal_private.h     |  39 ++++++++
 lib/librte_eal/common/include/rte_bus.h |  34 +++++++
 lib/librte_eal/common/include/rte_dev.h |  26 +++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 170 +++++++++++++++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map      |   2 +
 12 files changed, 499 insertions(+), 47 deletions(-)
Quoted 1 line

--

Quoted 1 line

2.7.4

author
#263 lore raw

v14 cover hot-unplug failure handle mechanism

In reply to Jeff Guo

Hotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.

We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.

In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.

In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.

The mechanism should work as below:

First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could separate implement case by case.

For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate over all devices on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_disable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.

The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.

This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.

patchset history:
v14->v13:
rebase code for fix apply issue.
fix some typo and checkpatch warning issue.

v13->v12:
use local variable to rewrite the func to be more readable. add sa_flag check when invoke generic sigbus handler modify some typo
delete needless helper in app

v12->v11:
add and delete some checking about sigbus recover.

v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move the igb_uio fixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.

v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log

v9->v8:
refine commit log to be more readable.

v8->v7:
refine errno process in sigbus handler.
refine igb uio release process

v7->v6:
delete some unused part

v6->v5:
refine some description about bus ops
refine commit log
add some entry check.

v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.

v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.

v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.

v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.

Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.

"v1(21)" == v21 as below:
v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.

v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.

v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.

v18->v15:
add document, add signal bus handler, refine the code to be more clear.

the prior patch history please check the patch set "add device event monitor framework".

Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug app/testpmd: use hotplug failure handler

 app/test-pmd/testpmd.c                  |  88 ++++++++---------
 doc/guides/rel_notes/release_18_08.rst  |   5 +
 drivers/bus/pci/pci_common.c            |  83 ++++++++++++++++
 drivers/bus/pci/pci_common_uio.c        |  33 +++++++
 drivers/bus/pci/private.h               |  12 +++
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  14 +++
 lib/librte_eal/common/eal_common_bus.c  |  43 ++++++++
 lib/librte_eal/common/eal_private.h     |  39 ++++++++
 lib/librte_eal/common/include/rte_bus.h |  34 +++++++
 lib/librte_eal/common/include/rte_dev.h |  26 +++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 170 +++++++++++++++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map      |   2 +
 12 files changed, 502 insertions(+), 47 deletions(-)
Signature · 1 line
2.7.4
author
#264 lore raw

PATCH v14 1/7 bus: add hot-unplug handler

A hot-unplug failure and app crash can be caused, when a device is hot-unplugged but the application still try to access the device by reading or writing from the BARs, which is already invalid but still not timely be unmap or released.

This patch introduces bus ops to handle hot-unplug failures. Each bus can implement its own case-dependent logic to handle the failures.

---
v14->v13:
no change.

---
 lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
index b7b5b08..1bb53dc 100644
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);
 typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 
 /**
+ * Implement a specific hot-unplug handler, which is responsible for
+ * handle the failure when device be hot-unplugged. When the event of
+ * hot-unplug be detected, it could call this function to handle
+ * the hot-unplug failure and avoid app crash.
+ * @param dev
+ *	Pointer of the device structure.
+ *
+ * @return
+ *	0 on success.
+ *	!0 on error.
+ */
+typedef int (*rte_bus_hot_unplug_handler_t)(struct rte_device *dev);
+
+/**
  * Bus scan policies
  */
 enum rte_bus_scan_mode {
@@ -212,6 +226,8 @@ struct rte_bus {
 	struct rte_bus_conf conf;    /**< Bus configuration */
 	rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu class */
 	rte_dev_iterate_t dev_iterate; /**< Device iterator. */
+	rte_bus_hot_unplug_handler_t hot_unplug_handler;
+				/**< handle hot-unplug failure on the bus */
 };
 
 /**
Signature · 1 line
2.7.4
author
#265 lore raw

PATCH v14 2/7 bus/pci: implement hot-unplug handler ops

In reply to Jeff Guo

This patch implements the ops to handle hot-unplug on the PCI bus. For UIO PCI, it could avoids BARs read/write errors by creating a new dummy memory to remap the memory where the failure is. For VFIO or other kernel driver, it could specific implement function to handle hot-unplug case by case.

---
v14->v13:
no change.

---
 drivers/bus/pci/pci_common.c     | 28 ++++++++++++++++++++++++++++
 drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++
 drivers/bus/pci/private.h        | 12 ++++++++++++
 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
index c7695d1..be7cc1f 100644
@@ -404,6 +404,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 }
 
 static int
+pci_hot_unplug_handler(struct rte_device *dev)
+{
+	struct rte_pci_device *pdev = NULL;
+	int ret = 0;
+
+	pdev = RTE_DEV_TO_PCI(dev);
+	if (!pdev)
+		return -1;
+
+	switch (pdev->kdrv) {
+	case RTE_KDRV_IGB_UIO:
+	case RTE_KDRV_UIO_GENERIC:
+	case RTE_KDRV_NIC_UIO:
+		/* BARs resource is invalid, remap it to be safe. */
+		ret = pci_uio_remap_resource(pdev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"Not managed by a supported kernel driver, skipped\n");
+		ret = -1;
+		break;
+	}
+
+	return ret;
+}
+
+static int
 pci_plug(struct rte_device *dev)
 {
 	return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
@@ -434,6 +461,7 @@ struct rte_pci_bus rte_pci_bus = {
 		.parse = pci_parse,
 		.get_iommu_class = rte_pci_get_iommu_class,
 		.dev_iterate = rte_pci_dev_iterate,
+		.hot_unplug_handler = pci_hot_unplug_handler,
 	},
 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
index 54bc20b..7ea73db 100644
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
 	}
 }
 
+/* remap the PCI resource of a PCI device in anonymous virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	void *map_address;
+
+	if (dev == NULL)
+		return -1;
+
+	/* Remap all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		if (dev->mem_resource[i].phys_addr == 0)
+			continue;
+		map_address = mmap(dev->mem_resource[i].addr,
+				(size_t)dev->mem_resource[i].len,
+				PROT_READ | PROT_WRITE,
+				MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+		if (map_address == MAP_FAILED) {
+			RTE_LOG(ERR, EAL,
+				"Cannot remap resource for device %s\n",
+				dev->name);
+			return -1;
+		}
+		RTE_LOG(INFO, EAL,
+			"Successful remap resource for device %s\n",
+			dev->name);
+	}
+
+	return 0;
+}
+
 static struct mapped_pci_resource *
 pci_uio_find_resource(struct rte_pci_device *dev)
 {
drivers/bus/pci/private.h +12 -0
index 0e689fa..0883d82 100644
@@ -125,6 +125,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
 
 /**
+ * Remap the PCI resource of a PCI device in anonymous virtual memory.
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
  * Map device memory to uio resource
  *
  * This function is private to EAL.
Signature · 1 line
2.7.4
author
#266 lore raw

PATCH v14 3/7 bus: add sigbus handler

In reply to Jeff Guo

When a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.

This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.

---
v14->v13:
no change.

---
 lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
lib/librte_eal/common/include/rte_bus.h +18 -0
index 1bb53dc..6be4b5c 100644
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 typedef int (*rte_bus_hot_unplug_handler_t)(struct rte_device *dev);
 
 /**
+ * Implement a specific sigbus handler, which is responsible for handling
+ * the sigbus error which is either original memory error, or specific memory
+ * error that caused of device be hot-unplugged. When sigbus error be captured,
+ * it could call this function to handle sigbus error.
+ * @param failure_addr
+ *	Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ *	0 for success handle the sigbus for hot-unplug.
+ *	1 for not process it, because it is a generic sigbus error.
+ *	-1 for failed to handle the sigbus for hot-unplug.
+ */
+typedef int (*rte_bus_sigbus_handler_t)(const void *failure_addr);
+
+/**
  * Bus scan policies
  */
 enum rte_bus_scan_mode {
@@ -228,6 +243,9 @@ struct rte_bus {
 	rte_dev_iterate_t dev_iterate; /**< Device iterator. */
 	rte_bus_hot_unplug_handler_t hot_unplug_handler;
 				/**< handle hot-unplug failure on the bus */
+	rte_bus_sigbus_handler_t sigbus_handler;
+					/**< handle sigbus error on the bus */
+
 };
 
 /**
Signature · 1 line
2.7.4
author
#267 lore raw

PATCH v14 4/7 bus/pci: implement sigbus handler ops

In reply to Jeff Guo

This patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.

---
v14->v13:
fix some checkpatch issue.

---
 drivers/bus/pci/pci_common.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
drivers/bus/pci/pci_common.c +55 -0
index be7cc1f..0efd930 100644
@@ -403,6 +403,37 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 	return NULL;
 }
 
+/*
+ * find the device which encounter the failure, by iterate over all device on
+ * PCI bus to check if the memory failure address is located in the range
+ * of the BARs of the device.
+ */
+static struct rte_pci_device *
+pci_find_device_by_addr(const void *failure_addr)
+{
+	struct rte_pci_device *pdev = NULL;
+	uint64_t check_point, start, end, len;
+	int i;
+
+	check_point = (uint64_t)(uintptr_t)failure_addr;
+
+	FOREACH_DEVICE_ON_PCIBUS(pdev) {
+		start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
+		len = pdev->mem_resource[i].len;
+		end = (uint64_t)(uintptr_t)RTE_PTR_ADD(start, len);
+
+		for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
+			if (check_point >= start && check_point < end) {
+				RTE_LOG(DEBUG, EAL, "Failure address %16.16"
+					PRIx64" belongs to device %s!\n",
+					check_point, pdev->device.name);
+				return pdev;
+			}
+		}
+	}
+	return NULL;
+}
+
 static int
 pci_hot_unplug_handler(struct rte_device *dev)
 {
@@ -431,6 +462,29 @@ pci_hot_unplug_handler(struct rte_device *dev)
 }
 
 static int
+pci_sigbus_handler(const void *failure_addr)
+{
+	struct rte_pci_device *pdev = NULL;
+	int ret = 0;
+
+	pdev = pci_find_device_by_addr(failure_addr);
+	if (!pdev) {
+		/* It is a generic sigbus error, no bus would handle it. */
+		ret = 1;
+	} else {
+		/* The sigbus error is caused of hot-unplug. */
+		ret = pci_hot_unplug_handler(&pdev->device);
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"Failed to handle hot-unplug for device %s",
+				pdev->name);
+			ret = -1;
+		}
+	}
+	return ret;
+}
+
+static int
 pci_plug(struct rte_device *dev)
 {
 	return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
@@ -462,6 +516,7 @@ struct rte_pci_bus rte_pci_bus = {
 		.get_iommu_class = rte_pci_get_iommu_class,
 		.dev_iterate = rte_pci_dev_iterate,
 		.hot_unplug_handler = pci_hot_unplug_handler,
+		.sigbus_handler = pci_sigbus_handler,
 	},
 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
author
#268 lore raw

PATCH v14 5/7 bus: add helper to handle sigbus

In reply to Jeff Guo

This patch aims to add a helper to iterate over all buses to find the relevant bus to handle the sigbus error.

---
v14->v13:
no change.

---
 lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++
 lib/librte_eal/common/eal_private.h    | 13 ++++++++++
 2 files changed, 56 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
index 0943851..62b7318 100644
@@ -37,6 +37,7 @@
 #include <rte_bus.h>
 #include <rte_debug.h>
 #include <rte_string_fns.h>
+#include <rte_errno.h>
 
 #include "eal_private.h"
 
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void)
 	}
 	return mode;
 }
+
+static int
+bus_handle_sigbus(const struct rte_bus *bus,
+			const void *failure_addr)
+{
+	int ret;
+
+	if (!bus->sigbus_handler)
+		return -1;
+
+	ret = bus->sigbus_handler(failure_addr);
+
+	/* find bus but handle failed, keep the errno be set. */
+	if (ret < 0 && rte_errno == 0)
+		rte_errno = ENOTSUP;
+
+	return ret > 0;
+}
+
+int
+rte_bus_sigbus_handler(const void *failure_addr)
+{
+	struct rte_bus *bus;
+
+	int ret = 0;
+	int old_errno = rte_errno;
+
+	rte_errno = 0;
+
+	bus = rte_bus_find(NULL, bus_handle_sigbus, failure_addr);
+	/* can not find bus. */
+	if (!bus)
+		return 1;
+	/* find bus but handle failed, pass on the new errno. */
+	else if (rte_errno != 0)
+		return -1;
+
+	/* restore the old errno. */
+	rte_errno = old_errno;
+
+	return ret;
+}
lib/librte_eal/common/eal_private.h +13 -0
index 4f809a8..a2d1528 100644
@@ -304,4 +304,17 @@ int
 rte_devargs_layers_parse(struct rte_devargs *devargs,
 			 const char *devstr);
 
+/**
+ * Iterate over all buses to find the corresponding bus to handle the sigbus
+ * error.
+ * @param failure_addr
+ *	Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ *	 0 success to handle the sigbus.
+ *	-1 failed to handle the sigbus
+ *	 1 no bus can handler the sigbus
+ */
+int rte_bus_sigbus_handler(const void *failure_addr);
+
 #endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
author
#269 lore raw

PATCH v14 6/7 eal: add failure handle mechanism for hot-unplug

In reply to Jeff Guo

The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.

By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable

---
v14->v13:
no change.

---
 doc/guides/rel_notes/release_18_08.rst  |   5 +
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  14 +++
 lib/librte_eal/common/eal_private.h     |  26 +++++
 lib/librte_eal/common/include/rte_dev.h |  26 +++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 170 +++++++++++++++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map      |   2 +
 6 files changed, 242 insertions(+), 1 deletion(-)
doc/guides/rel_notes/release_18_08.rst +5 -0
index 8a09dee..4eca59a 100644
@@ -117,6 +117,11 @@ New Features
 
   Added support for chained mbufs (input and output).
 
+* **Added hot-unplug handle mechanism.**
+
+  ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are
+  for enabling or disabling hotplug handle mechanism.
+
 
 API Changes
 -----------
lib/librte_eal/bsdapp/eal/eal_dev.c +14 -0
index 1c6c51b..255d611 100644
@@ -19,3 +19,17 @@ 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_hotplug_handle_enable(void)
+{
+	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
+	return -1;
+}
+
+int __rte_experimental
+rte_dev_hotplug_handle_disable(void)
+{
+	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
+	return -1;
+}
lib/librte_eal/common/eal_private.h +26 -0
index a2d1528..637f20d 100644
@@ -317,4 +317,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,
  */
 int rte_bus_sigbus_handler(const void *failure_addr);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Register the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_sigbus_handler_register(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Unregister the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_sigbus_handler_unregister(void);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_dev.h +26 -0
index b80a805..ff580a0 100644
@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void);
 int __rte_experimental
 rte_dev_event_monitor_stop(void);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Enable hotplug handling for devices.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_hotplug_handle_enable(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Disable hotplug handling for devices.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_hotplug_handle_disable(void);
+
 #endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/eal_dev.c +169 -1

215 lines not on this page · raw message

lib/librte_eal/rte_eal_version.map +2 -0
index 73282bb..b167b8f 100644
@@ -281,6 +281,8 @@ EXPERIMENTAL {
 	rte_dev_event_callback_unregister;
 	rte_dev_event_monitor_start;
 	rte_dev_event_monitor_stop;
+	rte_dev_hotplug_handle_disable;
+	rte_dev_hotplug_handle_enable;
 	rte_dev_iterator_init;
 	rte_dev_iterator_next;
 	rte_devargs_add;
Signature · 1 line
2.7.4
#270 lore raw

Re: [PATCH v14 6/7] eal: add failure handle mechanism for hot-unplug

04/10/2018 16:46, Jeff Guo:

doc/guides/rel_notes/release_18_08.rst New Features · line 123

Quoted 10 lines
doc/guides/rel_notes/release_18_08.rst +4 -0
@@ -117,6 +117,11 @@ New Features
 
   Added support for chained mbufs (input and output).
 
+* **Added hot-unplug handle mechanism.**
+
+  ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are
+  for enabling or disabling hotplug handle mechanism.

EAL features should be inserted before mbuf features.
And more importantly, it should be inserted in 18.11 release notes ;)

lib/librte_eal/common/eal_private.h rte_devargs_layers_parse(struct rte_devargs *devargs, · line 322

Quoted 5 lines
lib/librte_eal/common/eal_private.h +3 -0
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice

No need of experimental mark for internal functions.

Quoted 12 lines

+ *
+ * Register the sigbus handler.
+ *
+ * @return
+ * - On success, zero.
+ * - On failure, a negative value.
+ */

lib/librte_eal/common/eal_private.h +5 -0
+int
+rte_dev_sigbus_handler_register(void);
+
+/**
+ * @warning

lib/librte_eal/common/eal_private.h rte_devargs_layers_parse(struct rte_devargs *devargs, · line 344

Quoted 10 lines
lib/librte_eal/common/eal_private.h +10 -0
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Unregister the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_sigbus_handler_unregister(void);

Why are you using rte_ prefix for private functions?

author
#271 lore raw

PATCH v14 7/7 app/testpmd: use hotplug failure handler

In reply to Jeff Guo

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except that app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

---
v14->v13:
rebase code to fix apply issue.
fix some typo.

---
 app/test-pmd/testpmd.c | 88 ++++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 46 deletions(-)
app/test-pmd/testpmd.c +42 -46
index 001f0e5..db87c63 100644
@@ -434,9 +434,6 @@ static int eth_event_callback(portid_t port_id,
 static void eth_dev_event_callback(char *device_name,
 				enum rte_dev_event_type type,
 				void *param);
-static int eth_dev_event_callback_register(void);
-static int eth_dev_event_callback_unregister(void);
-
 
 /*
  * Check if all the ports are started.
@@ -1954,39 +1951,6 @@ reset_port(portid_t pid)
 	printf("Done\n");
 }
 
-static int
-eth_dev_event_callback_register(void)
-{
-	int ret;
-
-	/* register the device event callback */
-	ret = rte_dev_event_callback_register(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret) {
-		printf("Failed to register device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
-
-static int
-eth_dev_event_callback_unregister(void)
-{
-	int ret;
-
-	/* unregister the device event callback */
-	ret = rte_dev_event_callback_unregister(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret < 0) {
-		printf("Failed to unregister device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
 void
 attach_port(char *identifier)
 {
@@ -2093,14 +2057,26 @@ pmd_test_exit(void)
 
 	if (hot_plug) {
 		ret = rte_dev_event_monitor_stop();
-		if (ret)
+		if (ret) {
 			RTE_LOG(ERR, EAL,
 				"fail to stop device event monitor.");
+			return;
+		}
 
-		ret = eth_dev_event_callback_unregister();
-		if (ret)
+		ret = rte_dev_event_callback_unregister(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret < 0) {
+			RTE_LOG(ERR, EAL,
+				"fail to unregister device event callback.\n");
+			return;
+		}
+
+		ret = rte_dev_hotplug_handle_disable();
+		if (ret) {
 			RTE_LOG(ERR, EAL,
-				"fail to unregister all event callbacks.");
+				"fail to disable hotplug handling.\n");
+			return;
+		}
 	}
 
 	printf("\nBye...\n");
@@ -2244,6 +2220,9 @@ static void
 eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 			     __rte_unused void *arg)
 {
+	uint16_t port_id;
+	int ret;
+
 	if (type >= RTE_DEV_EVENT_MAX) {
 		fprintf(stderr, "%s called upon invalid event %d\n",
 			__func__, type);
@@ -2254,9 +2233,13 @@ eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 	case RTE_DEV_EVENT_REMOVE:
 		RTE_LOG(ERR, EAL, "The device: %s has been removed!\n",
 			device_name);
-		/* TODO: After finish failure handle, begin to stop
-		 * packet forward, stop port, close port, detach port.
-		 */
+		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
+		if (ret) {
+			RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
+				device_name);
+			return;
+		}
+		rmv_event_callback((void *)(intptr_t)port_id);
 		break;
 	case RTE_DEV_EVENT_ADD:
 		RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
@@ -2779,14 +2762,27 @@ main(int argc, char** argv)
 	init_config();
 
 	if (hot_plug) {
-		/* enable hot plug monitoring */
+		ret = rte_dev_hotplug_handle_enable();
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail to enable hotplug handling.");
+			return -1;
+		}
+
 		ret = rte_dev_event_monitor_start();
 		if (ret) {
-			rte_errno = EINVAL;
+			RTE_LOG(ERR, EAL,
+				"fail to start device event monitoring.");
 			return -1;
 		}
-		eth_dev_event_callback_register();
 
+		ret = rte_dev_event_callback_register(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail  to register device event callback\n");
+			return -1;
+		}
 	}
 
 	if (start_port(RTE_PORT_ALL) != 0)
Signature · 1 line
2.7.4
Acked-by on v14 7/7
#272 lore raw

Re: [PATCH v14 7/7] app/testpmd: use hotplug failure handler

Quoted 27 lines

-----Original Message-----
From: Guo, Jia
Sent: Thursday, October 4, 2018 3:46 PM
To: [email protected]; Richardson, Bruce <[email protected]>; Yigit, Ferruh <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Wu, Jingjing <[email protected]>; [email protected]; [email protected]; [email protected]; Van Haaren, Harry <[email protected]>; Zhang, Qi Z <[email protected]>; He, Shaopeng <[email protected]>; Iremonger, Bernard <[email protected]>; [email protected]; Lu, Wenzhuo <[email protected]>; Burakov, Anatoly <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Guo, Jia <[email protected]>; Zhang, Helin <[email protected]>
Subject: [PATCH v14 7/7] app/testpmd: use hotplug failure handler

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except that app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

Quoted 2 lines
author
#273 lore raw

v15 cover hot-unplug failure handle mechanism

In reply to Jeff Guo

Hotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.

We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.

In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.

In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.

The mechanism should work as below:

First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could separate implement case by case.

For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate over all devices on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_disable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.

The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.

This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.

patchset history:
v15->v14:
fix compling and document issues

v14->v13:
rebase code for fix apply issue.
fix some typo and checkpatch warning issue.

v13->v12:
use local variable to rewrite the func to be more readable. add sa_flag check when invoke generic sigbus handler modify some typo
delete needless helper in app

v12->v11:
add and delete some checking about sigbus recover.

v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move the igb_uio fixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.

v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log

v9->v8:
refine commit log to be more readable.

v8->v7:
refine errno process in sigbus handler.
refine igb uio release process

v7->v6:
delete some unused part

v6->v5:
refine some description about bus ops
refine commit log
add some entry check.

v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.

v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.

v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.

v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.

Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.

"v1(21)" == v21 as below:
v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.

v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.

v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.

v18->v15:
add document, add signal bus handler, refine the code to be more clear.

the prior patch history please check the patch set "add device event monitor framework".

Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug app/testpmd: use hotplug failure handler

 app/test-pmd/testpmd.c                  |  88 ++++++++---------
 doc/guides/rel_notes/release_18_11.rst  |   5 +
 drivers/bus/pci/pci_common.c            |  82 +++++++++++++++
 drivers/bus/pci/pci_common_uio.c        |  33 +++++++
 drivers/bus/pci/private.h               |  12 +++
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  14 +++
 lib/librte_eal/common/eal_common_bus.c  |  43 ++++++++
 lib/librte_eal/common/eal_private.h     |  35 +++++++
 lib/librte_eal/common/include/rte_bus.h |  34 +++++++
 lib/librte_eal/common/include/rte_dev.h |  26 +++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 170 +++++++++++++++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map      |   2 +
 12 files changed, 497 insertions(+), 47 deletions(-)
Signature · 1 line
2.7.4
author
#274 lore raw

PATCH v15 1/7 bus: add hot-unplug handler

A hot-unplug failure and app crash can be caused, when a device is hot-unplugged but the application still try to access the device by reading or writing from the BARs, which is already invalid but still not timely be unmap or released.

This patch introduces bus ops to handle hot-unplug failures. Each bus can implement its own case-dependent logic to handle the failures.

---
v15->v14:
no change.

---
 lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
index b7b5b08..1bb53dc 100644
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);
 typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 
 /**
+ * Implement a specific hot-unplug handler, which is responsible for
+ * handle the failure when device be hot-unplugged. When the event of
+ * hot-unplug be detected, it could call this function to handle
+ * the hot-unplug failure and avoid app crash.
+ * @param dev
+ *	Pointer of the device structure.
+ *
+ * @return
+ *	0 on success.
+ *	!0 on error.
+ */
+typedef int (*rte_bus_hot_unplug_handler_t)(struct rte_device *dev);
+
+/**
  * Bus scan policies
  */
 enum rte_bus_scan_mode {
@@ -212,6 +226,8 @@ struct rte_bus {
 	struct rte_bus_conf conf;    /**< Bus configuration */
 	rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu class */
 	rte_dev_iterate_t dev_iterate; /**< Device iterator. */
+	rte_bus_hot_unplug_handler_t hot_unplug_handler;
+				/**< handle hot-unplug failure on the bus */
 };
 
 /**
Signature · 1 line
2.7.4
author
#275 lore raw

PATCH v15 2/7 bus/pci: implement hot-unplug handler ops

In reply to Jeff Guo

This patch implements the ops to handle hot-unplug on the PCI bus. For UIO PCI, it could avoids BARs read/write errors by creating a new dummy memory to remap the memory where the failure is. For VFIO or other kernel driver, it could specific implement function to handle hot-unplug case by case.

---
v15->v14:
no change.

---
 drivers/bus/pci/pci_common.c     | 28 ++++++++++++++++++++++++++++
 drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++
 drivers/bus/pci/private.h        | 12 ++++++++++++
 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
index c7695d1..be7cc1f 100644
@@ -404,6 +404,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 }
 
 static int
+pci_hot_unplug_handler(struct rte_device *dev)
+{
+	struct rte_pci_device *pdev = NULL;
+	int ret = 0;
+
+	pdev = RTE_DEV_TO_PCI(dev);
+	if (!pdev)
+		return -1;
+
+	switch (pdev->kdrv) {
+	case RTE_KDRV_IGB_UIO:
+	case RTE_KDRV_UIO_GENERIC:
+	case RTE_KDRV_NIC_UIO:
+		/* BARs resource is invalid, remap it to be safe. */
+		ret = pci_uio_remap_resource(pdev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"Not managed by a supported kernel driver, skipped\n");
+		ret = -1;
+		break;
+	}
+
+	return ret;
+}
+
+static int
 pci_plug(struct rte_device *dev)
 {
 	return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
@@ -434,6 +461,7 @@ struct rte_pci_bus rte_pci_bus = {
 		.parse = pci_parse,
 		.get_iommu_class = rte_pci_get_iommu_class,
 		.dev_iterate = rte_pci_dev_iterate,
+		.hot_unplug_handler = pci_hot_unplug_handler,
 	},
 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
index 54bc20b..7ea73db 100644
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
 	}
 }
 
+/* remap the PCI resource of a PCI device in anonymous virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	void *map_address;
+
+	if (dev == NULL)
+		return -1;
+
+	/* Remap all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		if (dev->mem_resource[i].phys_addr == 0)
+			continue;
+		map_address = mmap(dev->mem_resource[i].addr,
+				(size_t)dev->mem_resource[i].len,
+				PROT_READ | PROT_WRITE,
+				MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+		if (map_address == MAP_FAILED) {
+			RTE_LOG(ERR, EAL,
+				"Cannot remap resource for device %s\n",
+				dev->name);
+			return -1;
+		}
+		RTE_LOG(INFO, EAL,
+			"Successful remap resource for device %s\n",
+			dev->name);
+	}
+
+	return 0;
+}
+
 static struct mapped_pci_resource *
 pci_uio_find_resource(struct rte_pci_device *dev)
 {
drivers/bus/pci/private.h +12 -0
index 0e689fa..0883d82 100644
@@ -125,6 +125,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
 
 /**
+ * Remap the PCI resource of a PCI device in anonymous virtual memory.
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
  * Map device memory to uio resource
  *
  * This function is private to EAL.
Signature · 1 line
2.7.4
author
#276 lore raw

PATCH v15 3/7 bus: add sigbus handler

In reply to Jeff Guo

When a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.

This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.

---
v15->v14:
no change.

---
 lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
lib/librte_eal/common/include/rte_bus.h +18 -0
index 1bb53dc..6be4b5c 100644
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 typedef int (*rte_bus_hot_unplug_handler_t)(struct rte_device *dev);
 
 /**
+ * Implement a specific sigbus handler, which is responsible for handling
+ * the sigbus error which is either original memory error, or specific memory
+ * error that caused of device be hot-unplugged. When sigbus error be captured,
+ * it could call this function to handle sigbus error.
+ * @param failure_addr
+ *	Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ *	0 for success handle the sigbus for hot-unplug.
+ *	1 for not process it, because it is a generic sigbus error.
+ *	-1 for failed to handle the sigbus for hot-unplug.
+ */
+typedef int (*rte_bus_sigbus_handler_t)(const void *failure_addr);
+
+/**
  * Bus scan policies
  */
 enum rte_bus_scan_mode {
@@ -228,6 +243,9 @@ struct rte_bus {
 	rte_dev_iterate_t dev_iterate; /**< Device iterator. */
 	rte_bus_hot_unplug_handler_t hot_unplug_handler;
 				/**< handle hot-unplug failure on the bus */
+	rte_bus_sigbus_handler_t sigbus_handler;
+					/**< handle sigbus error on the bus */
+
 };
 
 /**
Signature · 1 line
2.7.4
author
#277 lore raw

PATCH v15 4/7 bus/pci: implement sigbus handler ops

In reply to Jeff Guo

This patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.

---
v15->v14:
fix compling issue.

---
 drivers/bus/pci/pci_common.c | 54 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/bus/pci/pci_common.c +54 -0
index be7cc1f..4325b0e 100644
@@ -403,6 +403,36 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 	return NULL;
 }
 
+/*
+ * find the device which encounter the failure, by iterate over all device on
+ * PCI bus to check if the memory failure address is located in the range
+ * of the BARs of the device.
+ */
+static struct rte_pci_device *
+pci_find_device_by_addr(const void *failure_addr)
+{
+	struct rte_pci_device *pdev = NULL;
+	uint64_t check_point, start, end, len;
+	int i;
+
+	check_point = (uint64_t)(uintptr_t)failure_addr;
+
+	FOREACH_DEVICE_ON_PCIBUS(pdev) {
+		for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
+			start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
+			len = pdev->mem_resource[i].len;
+			end = (uint64_t)(uintptr_t)RTE_PTR_ADD(start, len);
+			if (check_point >= start && check_point < end) {
+				RTE_LOG(DEBUG, EAL, "Failure address %16.16"
+					PRIx64" belongs to device %s!\n",
+					check_point, pdev->device.name);
+				return pdev;
+			}
+		}
+	}
+	return NULL;
+}
+
 static int
 pci_hot_unplug_handler(struct rte_device *dev)
 {
@@ -431,6 +461,29 @@ pci_hot_unplug_handler(struct rte_device *dev)
 }
 
 static int
+pci_sigbus_handler(const void *failure_addr)
+{
+	struct rte_pci_device *pdev = NULL;
+	int ret = 0;
+
+	pdev = pci_find_device_by_addr(failure_addr);
+	if (!pdev) {
+		/* It is a generic sigbus error, no bus would handle it. */
+		ret = 1;
+	} else {
+		/* The sigbus error is caused of hot-unplug. */
+		ret = pci_hot_unplug_handler(&pdev->device);
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"Failed to handle hot-unplug for device %s",
+				pdev->name);
+			ret = -1;
+		}
+	}
+	return ret;
+}
+
+static int
 pci_plug(struct rte_device *dev)
 {
 	return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
@@ -462,6 +515,7 @@ struct rte_pci_bus rte_pci_bus = {
 		.get_iommu_class = rte_pci_get_iommu_class,
 		.dev_iterate = rte_pci_dev_iterate,
 		.hot_unplug_handler = pci_hot_unplug_handler,
+		.sigbus_handler = pci_sigbus_handler,
 	},
 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
#278 lore raw

Re: [PATCH v15 4/7] bus/pci: implement sigbus handler ops

15/10/2018 13:27, Jeff Guo:

Quoted 15 lines

This patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.

---
v15->v14:
fix compling issue.
---

drivers/bus/pci/pci_common.c +4 -0
+static struct rte_pci_device *
+pci_find_device_by_addr(const void *failure_addr)
+{
+	struct rte_pci_device *pdev = NULL;

drivers/bus/pci/pci_common.c pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, · line 424

Quoted 10 lines
drivers/bus/pci/pci_common.c +10 -0
+	uint64_t check_point, start, end, len;
+	int i;
+
+	check_point = (uint64_t)(uintptr_t)failure_addr;
+
+	FOREACH_DEVICE_ON_PCIBUS(pdev) {
+		for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
+			start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
+			len = pdev->mem_resource[i].len;
+			end = (uint64_t)(uintptr_t)RTE_PTR_ADD(start, len);

When compiling for 32-bit, there is an error:
cast to pointer from integer of different size

start is not a pointer.
I think it must be replaced by a simple addition.

	end = start + len;
#279 lore raw

Re: [PATCH v15 4/7] bus/pci: implement sigbus handler ops

15/10/2018 15:41, Thomas Monjalon:

Quoted 30 lines

15/10/2018 13:27, Jeff Guo:

Quoted 15 lines

This patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.

---
v15->v14:
fix compling issue.
---

drivers/bus/pci/pci_common.c +4 -0
+static struct rte_pci_device *
+pci_find_device_by_addr(const void *failure_addr)
+{
+	struct rte_pci_device *pdev = NULL;

drivers/bus/pci/pci_common.c pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, · line 424

Quoted 10 lines
drivers/bus/pci/pci_common.c +10 -0
+	uint64_t check_point, start, end, len;
+	int i;
+
+	check_point = (uint64_t)(uintptr_t)failure_addr;
+
+	FOREACH_DEVICE_ON_PCIBUS(pdev) {
+		for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
+			start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
+			len = pdev->mem_resource[i].len;
+			end = (uint64_t)(uintptr_t)RTE_PTR_ADD(start, len);

When compiling for 32-bit, there is an error:
cast to pointer from integer of different size

Quoted 3 lines

start is not a pointer.
I think it must be replaced by a simple addition.

	end = start + len;

I will fix it on apply.

author
#280 lore raw

PATCH v15 5/7 bus: add helper to handle sigbus

In reply to Jeff Guo

This patch aims to add a helper to iterate over all buses to find the relevant bus to handle the sigbus error.

---
v15->v14:
no change.

---
 lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++
 lib/librte_eal/common/eal_private.h    | 13 ++++++++++
 2 files changed, 56 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
index 0943851..62b7318 100644
@@ -37,6 +37,7 @@
 #include <rte_bus.h>
 #include <rte_debug.h>
 #include <rte_string_fns.h>
+#include <rte_errno.h>
 
 #include "eal_private.h"
 
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void)
 	}
 	return mode;
 }
+
+static int
+bus_handle_sigbus(const struct rte_bus *bus,
+			const void *failure_addr)
+{
+	int ret;
+
+	if (!bus->sigbus_handler)
+		return -1;
+
+	ret = bus->sigbus_handler(failure_addr);
+
+	/* find bus but handle failed, keep the errno be set. */
+	if (ret < 0 && rte_errno == 0)
+		rte_errno = ENOTSUP;
+
+	return ret > 0;
+}
+
+int
+rte_bus_sigbus_handler(const void *failure_addr)
+{
+	struct rte_bus *bus;
+
+	int ret = 0;
+	int old_errno = rte_errno;
+
+	rte_errno = 0;
+
+	bus = rte_bus_find(NULL, bus_handle_sigbus, failure_addr);
+	/* can not find bus. */
+	if (!bus)
+		return 1;
+	/* find bus but handle failed, pass on the new errno. */
+	else if (rte_errno != 0)
+		return -1;
+
+	/* restore the old errno. */
+	rte_errno = old_errno;
+
+	return ret;
+}
lib/librte_eal/common/eal_private.h +13 -0
index 4f809a8..a2d1528 100644
@@ -304,4 +304,17 @@ int
 rte_devargs_layers_parse(struct rte_devargs *devargs,
 			 const char *devstr);
 
+/**
+ * Iterate over all buses to find the corresponding bus to handle the sigbus
+ * error.
+ * @param failure_addr
+ *	Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ *	 0 success to handle the sigbus.
+ *	-1 failed to handle the sigbus
+ *	 1 no bus can handler the sigbus
+ */
+int rte_bus_sigbus_handler(const void *failure_addr);
+
 #endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
author
#281 lore raw

PATCH v15 6/7 eal: add failure handle mechanism for hot-unplug

In reply to Jeff Guo

The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.

By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable

---
v15->v14:
fix document and comment issue

---
 doc/guides/rel_notes/release_18_11.rst  |   5 +
 lib/librte_eal/bsdapp/eal/eal_dev.c     |  14 +++
 lib/librte_eal/common/eal_private.h     |  22 +++++
 lib/librte_eal/common/include/rte_dev.h |  26 +++++
 lib/librte_eal/linuxapp/eal/eal_dev.c   | 170 +++++++++++++++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map      |   2 +
 6 files changed, 238 insertions(+), 1 deletion(-)
doc/guides/rel_notes/release_18_11.rst +5 -0
index 2133a5b..5eaf926 100644
@@ -104,6 +104,11 @@ New Features
   the specified port. The port must be stopped before the command call in order
   to reconfigure queues.
 
+* **Added hot-unplug handle mechanism.**
+
+  ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are
+  for enabling or disabling hotplug handle mechanism.
+
 
 API Changes
 -----------
lib/librte_eal/bsdapp/eal/eal_dev.c +14 -0
index 1c6c51b..255d611 100644
@@ -19,3 +19,17 @@ 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_hotplug_handle_enable(void)
+{
+	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
+	return -1;
+}
+
+int __rte_experimental
+rte_dev_hotplug_handle_disable(void)
+{
+	RTE_LOG(ERR, EAL, "Device event is not supported for FreeBSD\n");
+	return -1;
+}
lib/librte_eal/common/eal_private.h +22 -0
index a2d1528..4174d33 100644
@@ -317,4 +317,26 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,
  */
 int rte_bus_sigbus_handler(const void *failure_addr);
 
+/**
+ * @internal
+ * Register the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+dev_sigbus_handler_register(void);
+
+/**
+ * @internal
+ * Unregister the sigbus handler.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+dev_sigbus_handler_unregister(void);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_dev.h +26 -0
index b80a805..ff580a0 100644
@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void);
 int __rte_experimental
 rte_dev_event_monitor_stop(void);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Enable hotplug handling for devices.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_hotplug_handle_enable(void);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Disable hotplug handling for devices.
+ *
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int __rte_experimental
+rte_dev_hotplug_handle_disable(void);
+
 #endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/eal_dev.c +169 -1

215 lines not on this page · raw message

lib/librte_eal/rte_eal_version.map +2 -0
index 73282bb..b167b8f 100644
@@ -281,6 +281,8 @@ EXPERIMENTAL {
 	rte_dev_event_callback_unregister;
 	rte_dev_event_monitor_start;
 	rte_dev_event_monitor_stop;
+	rte_dev_hotplug_handle_disable;
+	rte_dev_hotplug_handle_enable;
 	rte_dev_iterator_init;
 	rte_dev_iterator_next;
 	rte_devargs_add;
Signature · 1 line
2.7.4
author
#282 lore raw

PATCH v15 7/7 app/testpmd: use hotplug failure handler

In reply to Jeff Guo

This patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except that app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.

---
v15->v14:
no change.

---
 app/test-pmd/testpmd.c | 88 ++++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 46 deletions(-)
app/test-pmd/testpmd.c +42 -46
index 001f0e5..db87c63 100644
@@ -434,9 +434,6 @@ static int eth_event_callback(portid_t port_id,
 static void eth_dev_event_callback(char *device_name,
 				enum rte_dev_event_type type,
 				void *param);
-static int eth_dev_event_callback_register(void);
-static int eth_dev_event_callback_unregister(void);
-
 
 /*
  * Check if all the ports are started.
@@ -1954,39 +1951,6 @@ reset_port(portid_t pid)
 	printf("Done\n");
 }
 
-static int
-eth_dev_event_callback_register(void)
-{
-	int ret;
-
-	/* register the device event callback */
-	ret = rte_dev_event_callback_register(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret) {
-		printf("Failed to register device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
-
-static int
-eth_dev_event_callback_unregister(void)
-{
-	int ret;
-
-	/* unregister the device event callback */
-	ret = rte_dev_event_callback_unregister(NULL,
-		eth_dev_event_callback, NULL);
-	if (ret < 0) {
-		printf("Failed to unregister device event callback\n");
-		return -1;
-	}
-
-	return 0;
-}
-
 void
 attach_port(char *identifier)
 {
@@ -2093,14 +2057,26 @@ pmd_test_exit(void)
 
 	if (hot_plug) {
 		ret = rte_dev_event_monitor_stop();
-		if (ret)
+		if (ret) {
 			RTE_LOG(ERR, EAL,
 				"fail to stop device event monitor.");
+			return;
+		}
 
-		ret = eth_dev_event_callback_unregister();
-		if (ret)
+		ret = rte_dev_event_callback_unregister(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret < 0) {
+			RTE_LOG(ERR, EAL,
+				"fail to unregister device event callback.\n");
+			return;
+		}
+
+		ret = rte_dev_hotplug_handle_disable();
+		if (ret) {
 			RTE_LOG(ERR, EAL,
-				"fail to unregister all event callbacks.");
+				"fail to disable hotplug handling.\n");
+			return;
+		}
 	}
 
 	printf("\nBye...\n");
@@ -2244,6 +2220,9 @@ static void
 eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 			     __rte_unused void *arg)
 {
+	uint16_t port_id;
+	int ret;
+
 	if (type >= RTE_DEV_EVENT_MAX) {
 		fprintf(stderr, "%s called upon invalid event %d\n",
 			__func__, type);
@@ -2254,9 +2233,13 @@ eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,
 	case RTE_DEV_EVENT_REMOVE:
 		RTE_LOG(ERR, EAL, "The device: %s has been removed!\n",
 			device_name);
-		/* TODO: After finish failure handle, begin to stop
-		 * packet forward, stop port, close port, detach port.
-		 */
+		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
+		if (ret) {
+			RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
+				device_name);
+			return;
+		}
+		rmv_event_callback((void *)(intptr_t)port_id);
 		break;
 	case RTE_DEV_EVENT_ADD:
 		RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
@@ -2779,14 +2762,27 @@ main(int argc, char** argv)
 	init_config();
 
 	if (hot_plug) {
-		/* enable hot plug monitoring */
+		ret = rte_dev_hotplug_handle_enable();
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail to enable hotplug handling.");
+			return -1;
+		}
+
 		ret = rte_dev_event_monitor_start();
 		if (ret) {
-			rte_errno = EINVAL;
+			RTE_LOG(ERR, EAL,
+				"fail to start device event monitoring.");
 			return -1;
 		}
-		eth_dev_event_callback_register();
 
+		ret = rte_dev_event_callback_register(NULL,
+			eth_dev_event_callback, NULL);
+		if (ret) {
+			RTE_LOG(ERR, EAL,
+				"fail  to register device event callback\n");
+			return -1;
+		}
 	}
 
 	if (start_port(RTE_PORT_ALL) != 0)
Signature · 1 line
2.7.4
#283 lore raw

Re: [PATCH v15 0/7] hot-unplug failure handle mechanism

In reply to Jeff Guo
Quoted 8 lines

Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug app/testpmd: use hotplug failure handler

Applied, thanks!

author
#284 lore raw

v3 cover add uevent api for hot plug

In reply to Jeff Guo

From: "Guo, Jia" <[email protected]>

This patch set aim to add a variable "uevent_fd" in structure "rte_intr_handle" for enable kernel object uevent monitoring, and add some uevent API in rte eal interrupt, that is “rte_uevent_connect” and “rte_uevent_get”. The patch use i40e for example, the driver could use these API to monitor and read out the uevent, then corresponding to handle these uevent, such as detach or attach the device.

Guo, Jia (2):
eal: add uevent api for hot plug
net/i40e: add hot plug monitor in i40e

 drivers/net/i40e/i40e_ethdev.c                     |  19 +++
 lib/librte_eal/common/eal_common_pci_uio.c         |   6 +-
 lib/librte_eal/linuxapp/eal/eal_interrupts.c       | 136 ++++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/eal_pci_uio.c          |   6 +
 .../linuxapp/eal/include/exec-env/rte_interrupts.h |  37 ++++++
 5 files changed, 201 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.3.1
author
#285 lore raw

PATCH v3 1/2 eal: add uevent api for hot plug

From: "Guo, Jia" <[email protected]>

This patch aim to add a variable "uevent_fd" in structure "rte_intr_handle" for enable kernel object uevent monitoring, and add some uevent API in rte eal interrupt, that is “rte_uevent_connect” and “rte_uevent_get”, so that all driver could use these API to monitor and read out the uevent, then corresponding to handle these uevent, such as detach or attach the device.

---
v3->v2: refine some return error
refine the string searching logic to aviod memory issue

---
 lib/librte_eal/common/eal_common_pci_uio.c         |   6 +-
 lib/librte_eal/linuxapp/eal/eal_interrupts.c       | 136 ++++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/eal_pci_uio.c          |   6 +
 .../linuxapp/eal/include/exec-env/rte_interrupts.h |  37 ++++++
 4 files changed, 182 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_common_pci_uio.c +5 -1
index 367a681..5b62f70 100644
@@ -117,6 +117,7 @@
 
 	dev->intr_handle.fd = -1;
 	dev->intr_handle.uio_cfg_fd = -1;
+	dev->intr_handle.uevent_fd = -1;
 	dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
 
 	/* secondary processes - use already recorded details */
@@ -227,7 +228,10 @@
 		close(dev->intr_handle.uio_cfg_fd);
 		dev->intr_handle.uio_cfg_fd = -1;
 	}
-
+	if (dev->intr_handle.uevent_fd >= 0) {
+		close(dev->intr_handle.uevent_fd);
+		dev->intr_handle.uevent_fd = -1;
+	}
 	dev->intr_handle.fd = -1;
 	dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
 }
lib/librte_eal/linuxapp/eal/eal_interrupts.c +134 -2

160 lines not on this page · raw message

lib/librte_eal/linuxapp/eal/eal_pci_uio.c +6 -0
index fa10329..eae9cd5 100644
@@ -231,6 +231,10 @@
 		close(dev->intr_handle.uio_cfg_fd);
 		dev->intr_handle.uio_cfg_fd = -1;
 	}
+	if (dev->intr_handle.uevent_fd >= 0) {
+		close(dev->intr_handle.uevent_fd);
+		dev->intr_handle.uevent_fd = -1;
+	}
 	if (dev->intr_handle.fd >= 0) {
 		close(dev->intr_handle.fd);
 		dev->intr_handle.fd = -1;
@@ -276,6 +280,8 @@
 		goto error;
 	}
 
+	dev->intr_handle.uevent_fd = rte_uevent_connect();
+
 	if (dev->kdrv == RTE_KDRV_IGB_UIO)
 		dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
 	else {
lib/librte_eal/linuxapp/eal/include/exec-env/rte_interrupts.h +37 -0
index 6daffeb..0b31a22 100644
@@ -90,6 +90,7 @@ struct rte_intr_handle {
 					for uio_pci_generic */
 	};
 	int fd;	 /**< interrupt event file descriptor */
+	int uevent_fd;	 /**< uevent file descriptor */
 	enum rte_intr_handle_type type;  /**< handle type */
 	uint32_t max_intr;             /**< max interrupt requested */
 	uint32_t nb_efd;               /**< number of available efd(event fd) */
@@ -99,6 +100,19 @@ struct rte_intr_handle {
 	int *intr_vec;                 /**< intr vector number array */
 };
 
+#define RTE_UEVENT_MSG_LEN 4096
+#define RTE_UEVENT_SUBSYSTEM_UIO 1
+
+enum rte_uevent_action {
+	RTE_UEVENT_ADD = 0,		/**< uevent type of device add */
+	RTE_UEVENT_REMOVE = 1,	/**< uevent type of device remove*/
+};
+
+struct rte_uevent {
+	enum rte_uevent_action action;	/**< uevent action type */
+	int subsystem;				/**< subsystem id */
+};
+
 #define RTE_EPOLL_PER_THREAD        -1  /**< to hint using per thread epfd */
 
 /**
@@ -236,4 +250,27 @@ struct rte_intr_handle {
 int
 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle);
 
+/**
+ * It read out the uevent from the specific file descriptor.
+ *
+ * @param fd
+ *   The fd which the uevent associated to
+ * @param uevent
+ *   Pointer to the uevent which read from the monitoring fd.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_uevent_get(int fd, struct rte_uevent *uevent);
+
+/**
+ * Connect to the device uevent file descriptor.
+ * @return
+ *   - On success, the connected uevent fd.
+ *   - On failure, a negative value.
+ */
+int
+rte_uevent_connect(void);
+
 #endif /* _RTE_LINUXAPP_INTERRUPTS_H_ */
Signature · 1 line
1.8.3.1
Reviewed-by on v3 1/2
#286 lore raw

Re: [PATCH v3 1/2] eal: add uevent api for hot plug

Quoted 15 lines

-----Original Message-----
From: Guo, Jia
Sent: Thursday, June 29, 2017 1:02 PM
To: Zhang, Helin <[email protected]>; Wu, Jingjing <[email protected]>
Cc: [email protected]; Guo, Jia <[email protected]>; Richardson, Bruce <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>
Subject: [PATCH v3 1/2] eal: add uevent api for hot plug

From: "Guo, Jia" <[email protected]>

Quoted 7 lines

This patch aim to add a variable "uevent_fd" in structure "rte_intr_handle" for enable kernel object uevent monitoring, and add some uevent API in rte eal interrupt, that is “rte_uevent_connect” and “rte_uevent_get”, so that all driver could use these API to monitor and read out the uevent, then corresponding to handle these uevent, such as detach or attach the device.

Looks fine from me.

author
#287 lore raw

v4 cover add uevent monitor for hot plug

In reply to Jeff Guo

So far, about hot plug in dpdk, we already have hot plug add/remove api and fail-safe driver to offload the fail-safe work from the app user. But there are still lack of a general event api, since the interrupt event, which hot plug related with, is diversity between each device and driver, such as mlx4, pci driver and others.

Use the hot removal event for example, pci drivers not all exposure the remove interrupt, so in order to make user to easy use the hot plug feature for pci driver, something must be done to detect the remove event at the kernel level and offer a new line of interrupt to the user land.

Base on the uevent of kobject mechanism in kernel, we could use it to benefit for monitoring the hot plug status of the device which not only uio/vfio of pci bus devices, but also other, such as cpu/usb/pci-express bus devices.

The idea is comming as bellow.

a.The uevent message form FD monitoring which will be useful. remove@/devices/pci0000:80/0000:80:02.2/0000:82:00.0/0000:83:03.0/0000:84:00.2/uio/uio2 ACTION=remove DEVPATH=/devices/pci0000:80/0000:80:02.2/0000:82:00.0/0000:83:03.0/0000:84:00.2/uio/uio2 SUBSYSTEM=uio
MAJOR=243
MINOR=2
DEVNAME=uio2
SEQNUM=11366

b.add uevent monitoring machanism:
add several general api to enable uevent monitoring.

c.add common uevent handler and uevent failure handler
uevent of device should be handler at bus or device layer, and the memory read and write failure when hot removal should be handle correctly before detach behaviors.

d.show example how to use uevent monitor
enable uevent monitoring in testpmd or fail-safe to show usage.

patchset history:
v4->v3:
move uevent monitor api from eal interrupt to eal device layer. create uevent type and struct in eal device.
move uevent handler for each driver to eal layer.
add uevent failure handler to process signal fault issue. add example for request and use uevent monitoring in testpmd.

v3->v2:
refine some return error
refine the string searching logic to avoid memory issue

v2->v1:
remove global variables of hotplug_fd, add uevent_fd in rte_intr_handle to let each pci device self maintain it fd, to fix dual device fd issue. refine some typo error.

Jeff Guo (2):
eal: add uevent monitor for hot plug
app/testpmd: use uevent to monitor hot removal

 app/test-pmd/testpmd.c                             |  62 ++++++
 lib/librte_eal/common/eal_common_dev.c             | 248 ++++++++++++++++++++-
 lib/librte_eal/common/eal_common_pci.c             |  20 ++
 lib/librte_eal/common/eal_private.h                |  14 ++
 lib/librte_eal/common/include/rte_dev.h            | 136 +++++++++++
 lib/librte_eal/common/include/rte_pci.h            |  17 ++
 lib/librte_eal/linuxapp/eal/eal_interrupts.c       |  28 ++-
 lib/librte_eal/linuxapp/eal/eal_pci_init.h         |   4 +
 lib/librte_eal/linuxapp/eal/eal_pci_uio.c          |  62 ++++++
 .../linuxapp/eal/include/exec-env/rte_interrupts.h |   2 +-
 10 files changed, 586 insertions(+), 7 deletions(-)
Signature · 1 line
2.7.4
author
#288 lore raw

PATCH v4 1/2 eal: add uevent monitor for hot plug

This patch aim to add a general uevent mechanism in eal device layer, to enable all kernel object hot plug monitoring, so user could use these API to monitor and read out the device status info sent from the kernel side, then corresponding to handle it, such as detach or attach the device, and even benefit to use it for do smoothly fail safe work.

1) About uevent monitoring:
a: add uevent_fd in struct rte_intr_handle, and use eal interrupt
epolling to monitoring the netlink of uevent_fd.
b: add enum of rte_eal_uevent_type and struct of rte_eal_uevent.
c: add below API in rte eal device layer.
rte_eal_uev_fd_new
rte_eal_uev_enable
rte_eal_uev_receive
rte_eal_uev_callback_register rte_eal_uev_callback_unregister

2) About uevent handler and failure handler, use pci uio for example, add below API to process it:
pci_uio_uev_handler
pci_uio_remap_resource
pci_map_private_resource

---
v4->v3:
move uevent monitor api from eal interrupt to eal device layer. create uevent type and struct in eal device.
move uevent handler for each driver to eal layer.
add uevent failure handler to process signal fault issue. add example for request and use uevent monitoring in testpmd.

---
 lib/librte_eal/common/eal_common_dev.c             | 248 ++++++++++++++++++++-
 lib/librte_eal/common/eal_common_pci.c             |  20 ++
 lib/librte_eal/common/eal_private.h                |  14 ++
 lib/librte_eal/common/include/rte_dev.h            | 136 +++++++++++
 lib/librte_eal/common/include/rte_pci.h            |  17 ++
 lib/librte_eal/linuxapp/eal/eal_interrupts.c       |  28 ++-
 lib/librte_eal/linuxapp/eal/eal_pci_init.h         |   4 +
 lib/librte_eal/linuxapp/eal/eal_pci_uio.c          |  62 ++++++
 .../linuxapp/eal/include/exec-env/rte_interrupts.h |   2 +-
 9 files changed, 524 insertions(+), 7 deletions(-)
lib/librte_eal/common/eal_common_dev.c +247 -1
index f98302d..ae8f673 100644
@@ -36,15 +36,41 @@
 #include <string.h>
 #include <inttypes.h>
 #include <sys/queue.h>
-
+#include <sys/signalfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+
+#include <rte_malloc.h>
 #include <rte_bus.h>
 #include <rte_dev.h>
 #include <rte_devargs.h>
 #include <rte_debug.h>
 #include <rte_log.h>
+#include <rte_spinlock.h>
 
 #include "eal_private.h"
 
+/* spinlock for uevent callbacks */
+static rte_spinlock_t rte_eal_uev_cb_lock = RTE_SPINLOCK_INITIALIZER;
+
+/**
+ * The user application callback description.
+ *
+ * It contains callback address to be registered by user application,
+ * the pointer to the parameters for callback, and the event type.
+ */
+struct rte_eal_uev_callback {
+	TAILQ_ENTRY(rte_eal_uev_callback) next; /**< Callbacks list */
+	rte_eal_uev_cb_fn cb_fn;                /**< Callback address */
+	void *cb_arg;                           /**< Parameter for callback */
+	void *ret_param;                        /**< Return parameter */
+	enum rte_eal_uevent_type event;          /**< Interrupt event type */
+	uint32_t active;                        /**< Callback is executing */
+};
+
 static int cmp_detached_dev_name(const struct rte_device *dev,
 	const void *_name)
 {
@@ -244,3 +270,223 @@ int rte_eal_hotplug_remove(const char *busname, const char *devname)
 	rte_eal_devargs_remove(busname, devname);
 	return ret;
 }
+
+int
+rte_eal_uev_fd_new(void)
+{
+
+	int netlink_fd = -1;
+
+	netlink_fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
+	if (netlink_fd < 0)
+		return -1;
+
+	return netlink_fd;
+}
+
+int
+rte_eal_uev_enable(int netlink_fd)
+{
+	struct sockaddr_nl addr;
+	int ret;
+	int size = 64 * 1024;
+	int nonblock = 1;
+	memset(&addr, 0, sizeof(addr));
+	addr.nl_family = AF_NETLINK;
+	addr.nl_pid = 0;
+	addr.nl_groups = 0xffffffff;
+
+	setsockopt(netlink_fd, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
+
+	ret = ioctl(netlink_fd, FIONBIO, &nonblock);
+	if (ret != 0) {
+		RTE_LOG(ERR, EAL,
+		"ioctl(FIONBIO) failed\n");
+		close(netlink_fd);
+		return -1;
+	}
+
+	if (bind(netlink_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		close(netlink_fd);
+		return -1;
+	}
+
+	return 0;
+}
+
+static int
+rte_eal_uev_parse(const char *buf, struct rte_eal_uevent *event)
+{
+	char action[RTE_EAL_UEVENT_MSG_LEN];
+	char subsystem[RTE_EAL_UEVENT_MSG_LEN];
+	char dev_path[RTE_EAL_UEVENT_MSG_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(subsystem, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(dev_path, 0, RTE_EAL_UEVENT_MSG_LEN);
+
+	while (i < RTE_EAL_UEVENT_MSG_LEN) {
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf)
+				break;
+			buf++;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		}
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;
+		}
+	}
+
+	if ((!strncmp(subsystem, "uio", 3)) ||
+		(!strncmp(subsystem, "pci", 3))) {
+		event->subsystem = RTE_EAL_UEVENT_SUBSYSTEM_UIO;
+		if (!strncmp(action, "add", 3))
+			event->type = RTE_EAL_UEVENT_ADD;
+		if (!strncmp(action, "remove", 6))
+			event->type = RTE_EAL_UEVENT_REMOVE;
+		return 0;
+	}
+
+	return -1;
+}
+
+int
+rte_eal_uev_receive(int fd, struct rte_eal_uevent *uevent)
+{
+	int ret;
+	char buf[RTE_EAL_UEVENT_MSG_LEN];
+
+	memset(uevent, 0, sizeof(struct rte_eal_uevent));
+	memset(buf, 0, RTE_EAL_UEVENT_MSG_LEN);
+
+	ret = recv(fd, buf, RTE_EAL_UEVENT_MSG_LEN - 1, MSG_DONTWAIT);
+	if (ret > 0)
+		return rte_eal_uev_parse(buf, uevent);
+	else if (ret < 0) {
+		RTE_LOG(ERR, EAL,
+		"Socket read error(%d): %s\n",
+		errno, strerror(errno));
+		return -1;
+	} else
+		/* connection closed */
+		return -1;
+}
+
+int
+rte_eal_uev_callback_register(struct rte_device *dev,
+			enum rte_eal_uevent_type event,
+			rte_eal_uev_cb_fn cb_fn, void *cb_arg)
+{
+	struct rte_eal_uev_callback *user_cb;
+
+	if (!cb_fn)
+		return -EINVAL;
+
+	rte_spinlock_lock(&rte_eal_uev_cb_lock);
+
+	TAILQ_FOREACH(user_cb, &(dev->uev_cbs), next) {
+		if (user_cb->cb_fn == cb_fn &&
+			user_cb->cb_arg == cb_arg &&
+			user_cb->event == event) {
+			break;
+		}
+	}
+
+	/* create a new callback. */
+	if (user_cb == NULL) {
+		user_cb = rte_zmalloc("EAL_UEV_CALLBACK",
+					sizeof(struct rte_eal_uev_callback), 0);
+		if (user_cb != NULL) {
+			user_cb->cb_fn = cb_fn;
+			user_cb->cb_arg = cb_arg;
+			user_cb->event = event;
+			TAILQ_INSERT_TAIL(&(dev->uev_cbs), user_cb, next);
+		}
+	}
+
+	rte_spinlock_unlock(&rte_eal_uev_cb_lock);
+	return (user_cb == NULL) ? -ENOMEM : 0;
+}
+
+int
+rte_eal_uev_callback_unregister(struct rte_device *dev,
+			enum rte_eal_uevent_type event,
+			rte_eal_uev_cb_fn cb_fn, void *cb_arg)
+{
+	int ret;
+	struct rte_eal_uev_callback *cb, *next;
+
+	if (!cb_fn)
+		return -EINVAL;
+
+	rte_spinlock_lock(&rte_eal_uev_cb_lock);
+
+	ret = 0;
+	for (cb = TAILQ_FIRST(&dev->uev_cbs); cb != NULL; cb = next) {
+
+		next = TAILQ_NEXT(cb, next);
+
+		if (cb->cb_fn != cb_fn || cb->event != event ||
+				(cb->cb_arg != (void *)-1 &&
+				cb->cb_arg != cb_arg))
+			continue;
+
+		/*
+		 * if this callback is not executing right now,
+		 * then remove it.
+		 */
+		if (cb->active == 0) {
+			TAILQ_REMOVE(&(dev->uev_cbs), cb, next);
+			rte_free(cb);
+		} else {
+			ret = -EAGAIN;
+		}
+	}
+
+	rte_spinlock_unlock(&rte_eal_uev_cb_lock);
+	return ret;
+}
+
+int
+_rte_eal_uev_callback_process(struct rte_device *dev,
+	enum rte_eal_uevent_type event, void *cb_arg, void *ret_param)
+{
+	struct rte_eal_uev_callback *cb_lst;
+	struct rte_eal_uev_callback dev_cb;
+	int rc = 0;
+
+	rte_spinlock_lock(&rte_eal_uev_cb_lock);
+	TAILQ_FOREACH(cb_lst, &(dev->uev_cbs), next) {
+		if (cb_lst->cb_fn == NULL || cb_lst->event != event)
+			continue;
+		dev_cb = *cb_lst;
+		cb_lst->active = 1;
+		if (cb_arg != NULL)
+			dev_cb.cb_arg = cb_arg;
+		if (ret_param != NULL)
+			dev_cb.ret_param = ret_param;
+
+		rte_spinlock_unlock(&rte_eal_uev_cb_lock);
+		rc = dev_cb.cb_fn(dev, dev_cb.event,
+				dev_cb.cb_arg, dev_cb.ret_param);
+		rte_spinlock_lock(&rte_eal_uev_cb_lock);
+		cb_lst->active = 0;
+	}
+	rte_spinlock_unlock(&rte_eal_uev_cb_lock);
+	return rc;
+}
lib/librte_eal/common/eal_common_pci.c +20 -0
index 52fd38c..0d640bc 100644
@@ -110,6 +110,26 @@ pci_name_set(struct rte_pci_device *dev)
 		dev->device.name = dev->name;
 }
 
+/* map a private resource from an address*/
+void *
+pci_map_private_resource(void *requested_addr, off_t offset, size_t size)
+{
+	void *mapaddr;
+
+	mapaddr = mmap(requested_addr, size,
+			   PROT_READ | PROT_WRITE,
+			   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+	if (mapaddr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "%s(): cannot mmap(%p, 0x%lx, 0x%lx): %s (%p)\n",
+			__func__, requested_addr,
+			(unsigned long)size, (unsigned long)offset,
+			strerror(errno), mapaddr);
+	} else
+		RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
+
+	return mapaddr;
+}
+
 /* map a particular resource from a file */
 void *
 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
lib/librte_eal/common/eal_private.h +14 -0
index 597d82e..8f5e283 100644
@@ -192,6 +192,8 @@ int pci_uio_map_resource(struct rte_pci_device *dev);
  */
 void pci_uio_unmap_resource(struct rte_pci_device *dev);
 
+void pci_uio_uev_handler(void *parm);
+
 /**
  * Allocate uio resource for PCI device
  *
@@ -222,6 +224,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
 
 /**
+ * remap the pci uio resource..
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
  * Map device memory to uio resource
  *
  * This function is private to EAL.
lib/librte_eal/common/include/rte_dev.h +136 -0

154 lines not on this page · raw message

lib/librte_eal/common/include/rte_pci.h +17 -0
index 8b12339..d3ced27 100644
@@ -394,6 +394,23 @@ void rte_pci_unmap_device(struct rte_pci_device *dev);
 
 /**
  * @internal
+ * Map to a particular private resource.
+ *
+ * @param requested_addr
+ *      The starting address for the new mapping range.
+ * @param offset
+ *      The offset for the mapping range.
+ * @param size
+ *      The size for the mapping range.
+ * @return
+ *   - On success, the function returns a pointer to the mapped area.
+ *   - On error, the value MAP_FAILED is returned.
+ */
+void *pci_map_private_resource(void *requested_addr, off_t offset,
+		size_t size);
+
+/**
+ * @internal
  * Map a particular resource from a file.
  *
  * @param requested_addr
lib/librte_eal/linuxapp/eal/eal_interrupts.c +23 -5
index 3e9ac41..06cbdab 100644
@@ -670,11 +670,16 @@ eal_intr_process_interrupts(struct epoll_event *events, int nfds)
 			RTE_SET_USED(r);
 			return -1;
 		}
+
 		rte_spinlock_lock(&intr_lock);
-		TAILQ_FOREACH(src, &intr_sources, next)
+		TAILQ_FOREACH(src, &intr_sources, next) {
 			if (src->intr_handle.fd ==
 					events[n].data.fd)
 				break;
+			else if (src->intr_handle.uevent_fd ==
+					events[n].data.fd)
+				break;
+		}
 		if (src == NULL){
 			rte_spinlock_unlock(&intr_lock);
 			continue;
@@ -736,17 +741,13 @@ eal_intr_process_interrupts(struct epoll_event *events, int nfds)
 		rte_spinlock_lock(&intr_lock);
 
 		if (call) {
-
 			/* Finally, call all callbacks. */
 			TAILQ_FOREACH(cb, &src->callbacks, next) {
-
 				/* make a copy and unlock. */
 				active_cb = *cb;
 				rte_spinlock_unlock(&intr_lock);
-
 				/* call the actual callback */
 				active_cb.cb_fn(active_cb.cb_arg);
-
 				/*get the lock back. */
 				rte_spinlock_lock(&intr_lock);
 			}
@@ -859,7 +860,24 @@ eal_intr_thread_main(__rte_unused void *arg)
 			}
 			else
 				numfds++;
+
+			/**
+			 * add device uevent file descriptor
+			 * into wait list for uevent monitoring.
+			 */
+			ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
+			ev.data.fd = src->intr_handle.uevent_fd;
+			if (epoll_ctl(pfd, EPOLL_CTL_ADD,
+					src->intr_handle.uevent_fd, &ev) < 0){
+				rte_panic("Error adding uevent_fd %d epoll_ctl"
+					", %s\n",
+					src->intr_handle.uevent_fd,
+					strerror(errno));
+			} else
+				numfds++;
 		}
+
+
 		rte_spinlock_unlock(&intr_lock);
 		/* serve the interrupt */
 		eal_intr_handle_interrupts(pfd, numfds);
lib/librte_eal/linuxapp/eal/eal_pci_init.h +4 -0
index ae2980d..2f040b4 100644
@@ -52,10 +52,14 @@ void *pci_find_max_end_va(void);
 int pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
 	uint64_t *end_addr, uint64_t *flags);
 
+void pci_uio_uev_handler(void *param);
 int pci_uio_alloc_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource **uio_res);
 void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
+
+int pci_uio_remap_resource(struct rte_pci_device *dev);
+
 int pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
 		struct mapped_pci_resource *uio_res, int map_idx);
 
lib/librte_eal/linuxapp/eal/eal_pci_uio.c +62 -0

86 lines not on this page · raw message

lib/librte_eal/linuxapp/eal/include/exec-env/rte_interrupts.h +1 -1
index 6daffeb..8c7fce4 100644
@@ -90,6 +90,7 @@ struct rte_intr_handle {
 					for uio_pci_generic */
 	};
 	int fd;	 /**< interrupt event file descriptor */
+	int uevent_fd;	 /**< uevent file descriptor */
 	enum rte_intr_handle_type type;  /**< handle type */
 	uint32_t max_intr;             /**< max interrupt requested */
 	uint32_t nb_efd;               /**< number of available efd(event fd) */
@@ -235,5 +236,4 @@ rte_intr_allow_others(struct rte_intr_handle *intr_handle);
  */
 int
 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle);
-
 #endif /* _RTE_LINUXAPP_INTERRUPTS_H_ */
Signature · 1 line
2.7.4
#289 lore raw

Re: [PATCH v4 1/2] eal: add uevent monitor for hot plug

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname)

Quoted 7 lines · On Sun, 3 Sep 2017 23:49:44 +0800 Jeff Guo <[email protected]> wrote:
lib/librte_eal/common/eal_common_dev.c +7 -0
+int
+rte_eal_uev_fd_new(void)
+{
+
+	int netlink_fd = -1;
+
+	netlink_fd = socket(PF

Please don't use the "initialize everything" style of programming.

Gcc has good detection and warning about uninitalized variables, and this style defeats that.

#290 lore raw

Re: [PATCH v4 1/2] eal: add uevent monitor for hot plug

In reply to Jeff Guo

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 294

Quoted 8 lines · On Sun, 3 Sep 2017 23:49:44 +0800 Jeff Guo <[email protected]> wrote:
lib/librte_eal/common/eal_common_dev.c +8 -0
+int
+rte_eal_uev_enable(int netlink_fd)
+{
+	struct sockaddr_nl addr;
+	int ret;
+	int size = 64 * 1024;
+	int nonblock = 1;
+	memset(&addr, 0, sizeof(addr));

Blank line between declarations and code.
Also use C99 style intializations not memset.

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 297

Quoted 3 lines
lib/librte_eal/common/eal_common_dev.c +3 -0
+	addr.nl_family = AF_NETLINK;
+	addr.nl_pid = 0;
+	addr.nl_groups = 0xffffffff;

You don't need or want all events. specify which group you want.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	setsockopt(netlink_fd, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));

Don't user BUFFORCE, that is only available as root. Just doing SO_RCVBUF is enough.

author
#291 lore raw

Re: [PATCH v4 1/2] eal: add uevent monitor for hot plug

Thanks Stephen for your review and suggestion, I will involve them in next version.

Best regards,
Jeff Guo

-----Original Message-----
From: Stephen Hemminger [mailto:[email protected]]
Sent: Monday, September 4, 2017 12:12 AM
To: Guo, Jia <[email protected]>
Cc: Richardson, Bruce <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Zhang, Helin <[email protected]>; Yigit, Ferruh <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Wu, Jingjing <[email protected]>
Subject: Re: [PATCH v4 1/2] eal: add uevent monitor for hot plug

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 294

Quoted 8 lines · On Sun, 3 Sep 2017 23:49:44 +0800 Jeff Guo <[email protected]> wrote:
lib/librte_eal/common/eal_common_dev.c +8 -0
+int
+rte_eal_uev_enable(int netlink_fd)
+{
+	struct sockaddr_nl addr;
+	int ret;
+	int size = 64 * 1024;
+	int nonblock = 1;
+	memset(&addr, 0, sizeof(addr));

Blank line between declarations and code.
Also use C99 style intializations not memset.

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 297

Quoted 3 lines
lib/librte_eal/common/eal_common_dev.c +3 -0
+	addr.nl_family = AF_NETLINK;
+	addr.nl_pid = 0;
+	addr.nl_groups = 0xffffffff;

You don't need or want all events. specify which group you want.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	setsockopt(netlink_fd, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));

Don't user BUFFORCE, that is only available as root. Just doing SO_RCVBUF is enough.

#292 lore raw

Re: [PATCH v4 1/2] eal: add uevent monitor for hot plug

In reply to Jeff Guo

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 375

Quoted 4 lines · On Sun, 3 Sep 2017 23:49:44 +0800 Jeff Guo <[email protected]> wrote:
lib/librte_eal/common/eal_common_dev.c +4 -0
+	char buf[RTE_EAL_UEVENT_MSG_LEN];
+
+	memset(uevent, 0, sizeof(struct rte_eal_uevent));
+	memset(buf, 0, RTE_EAL_UEVENT_MSG_LEN);

Please don't initialize everything all the time; you are even initializing receive data.

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 380

Quoted 4 lines
lib/librte_eal/common/eal_common_dev.c +4 -0
+	ret = recv(fd, buf, RTE_EAL_UEVENT_MSG_LEN - 1, MSG_DONTWAIT);
+	if (ret > 0)
+		return rte_eal_uev_parse(buf, uevent);
+	else if (ret < 0) {

else is unnecessary after return.

Checkpatch would have told you that if you ran it.

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname)

Quoted 3 lines
lib/librte_eal/common/eal_common_dev.c +3 -0
+		RTE_LOG(ERR, EAL,
+		"Socket read error(%d): %s\n",
+		errno, strerror(er

Please indent arguements to match the first line:
RTE_LOG(ERR, EAL,
"Socket read error ...

#293 lore raw

Re: [PATCH v4 1/2] eal: add uevent monitor for hot plug

In reply to Jeff Guo

lib/librte_eal/linuxapp/eal/eal_interrupts.c eal_intr_thread_main(__rte_unused void *arg) · line 875

Quoted 12 lines · On Sun, 3 Sep 2017 23:49:44 +0800 Jeff Guo <[email protected]> wrote:
lib/librte_eal/linuxapp/eal/eal_interrupts.c +12 -0
+			/**
+			 * add device uevent file descriptor
+			 * into wait list for uevent monitoring.
+			 */
+			ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
+			ev.data.fd = src->intr_handle.uevent_fd;
+			if (epoll_ctl(pfd, EPOLL_CTL_ADD,
+					src->intr_handle.uevent_fd, &ev) < 0){
+				rte_panic("Error adding uevent_fd %d epoll_ctl"
+					", %s\n",
+					src->intr_handle.uevent_fd,
+					strerror(errno));

Panicing a user application under load is not going to make people happy

author
#294 lore raw

PATCH v4 2/2 app/testpmd: use uevent to monitor hot removal

In reply to Jeff Guo

use testpmd for example, to show app how to request and use uevent monitoring to handle an event of device hot removal.

---
 app/test-pmd/testpmd.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
app/test-pmd/testpmd.c +62 -0
index 9a36e66..b5ff7d7 100644
@@ -393,6 +393,10 @@ static void check_all_ports_link_status(uint32_t port_mask);
 static int eth_event_callback(uint8_t port_id,
 			      enum rte_eth_event_type type,
 			      void *param, void *ret_param);
+static int eth_uevent_callback(struct rte_device *dev,
+			      enum rte_eal_uevent_type type,
+			      void *param, void *ret_param);
+
 
 /*
  * Check if all the ports are started.
@@ -1413,6 +1417,7 @@ start_port(portid_t pid)
 	struct rte_port *port;
 	struct ether_addr mac_addr;
 	enum rte_eth_event_type event_type;
+	enum rte_eal_uevent_type uevent_type;
 
 	if (port_id_is_invalid(pid, ENABLED_WARN))
 		return 0;
@@ -1548,6 +1553,18 @@ start_port(portid_t pid)
 			}
 		}
 
+		for (uevent_type = RTE_EAL_UEVENT_UNKNOWN;
+		     uevent_type < RTE_EAL_UEVENT_MAX;
+		     uevent_type++) {
+			diag = rte_eal_uev_callback_register(&port->dev_info.pci_dev->device,uevent_type,
+							eth_uevent_callback, NULL);
+			if (diag) {
+				printf("Failed to setup uevent callback for uevent %d\n",
+					uevent_type);
+				return -1;
+			}
+		}
+
 		/* start port */
 		if (rte_eth_dev_start(pi) < 0) {
 			printf("Fail to start port %d\n", pi);
@@ -1842,6 +1859,16 @@ rmv_event_callback(void *arg)
 			dev->device->name);
 }
 
+static void
+rmv_uevent_callback(void *arg)
+{
+	struct rte_device *dev = (struct rte_device *)arg;
+	printf("removing device %s\n", dev->name);
+	if (rte_eal_dev_detach(dev))
+		RTE_LOG(ERR, USER1, "Failed to detach device %s\n",
+			dev->name);
+}
+
 /* This function is used by the interrupt thread */
 static int
 eth_event_callback(uint8_t port_id, enum rte_eth_event_type type, void *param,
@@ -1883,6 +1910,41 @@ eth_event_callback(uint8_t port_id, enum rte_eth_event_type type, void *param,
 	return 0;
 }
 
+/* This function is used by the interrupt thread */
+static int
+eth_uevent_callback(struct rte_device *dev, enum rte_eal_uevent_type type, void *param,
+		  void *ret_param)
+{
+	static const char * const event_desc[] = {
+		[RTE_EAL_UEVENT_UNKNOWN] = "Unknown",
+		[RTE_EAL_UEVENT_REMOVE] = "remove",
+	};
+
+	RTE_SET_USED(param);
+	RTE_SET_USED(ret_param);
+
+	if (type >= RTE_EAL_UEVENT_MAX) {
+		fprintf(stderr, "%s called upon invalid event %d\n",
+			__func__, type);
+		fflush(stderr);
+	} else if (event_print_mask & (UINT32_C(1) << type)) {
+		printf("%s event\n",
+			event_desc[type]);
+		fflush(stdout);
+	}
+
+	switch (type) {
+	case RTE_EAL_UEVENT_REMOVE:
+		if (rte_eal_alarm_set(100000,
+				rmv_uevent_callback, (void *)dev))
+			fprintf(stderr, "Could not set up deferred device removal\n");
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
 static int
 set_tx_queue_stats_mapping_registers(uint8_t port_id, struct rte_port *port)
 {
Signature · 1 line
2.7.4
author
#295 lore raw

v5 cover add uevent monitor for hot plug

So far, about hot plug in dpdk, we already have hot plug add/remove api and fail-safe driver to offload the fail-safe work from the app user. But there are still lack of a general event api, since the interrupt event, which hot plug related with, is diversity between each device and driver, such as mlx4, pci driver and others.

Use the hot removal event for example, pci drivers not all exposure the remove interrupt, so in order to make user to easy use the hot plug feature for pci driver, something must be done to detect the remove event at the kernel level and offer a new line of interrupt to the user land.

Base on the uevent of kobject mechanism in kernel, we could use it to benefit for monitoring the hot plug status of the device which not only uio/vfio of pci bus devices, but also other, such as cpu/usb/pci-express bus devices.

The idea is comming as bellow.

a.The uevent message form FD monitoring which will be useful. remove@/devices/pci0000:80/0000:80:02.2/0000:82:00.0/0000:83:03.0/0000:84:00.2/uio/uio2 ACTION=remove DEVPATH=/devices/pci0000:80/0000:80:02.2/0000:82:00.0/0000:83:03.0/0000:84:00.2/uio/uio2 SUBSYSTEM=uio
MAJOR=243
MINOR=2
DEVNAME=uio2
SEQNUM=11366

b.add uevent monitoring machanism:
add several general api to enable uevent monitoring.

c.add common uevent handler and uevent failure handler
uevent of device should be handler at bus or device layer, and the memory read and write failure when hot removal should be handle correctly before detach behaviors.

d.show example how to use uevent monitor
enable uevent monitoring in testpmd or fail-safe to show usage.

patchset history:
v5->v4:
1.Move uevent monitor epolling from eal interrupt to eal device layer.
2.Redefine the eal device API for common, and distinguish between linux and bsd
3.Add failure handler helper api in bus layer.Add function of find device by name.
4.Replace of individual fd bind with single device, use a common fd to polling all device. 5.Add to register hot insertion monitoring and process, add function to auto bind driver befor user add device 6.Refine some coding style and typos issue
7.add new callback to process hot insertion

v4->v3:
1.move uevent monitor api from eal interrupt to eal device layer. 2.create uevent type and struct in eal device.
3.move uevent handler for each driver to eal layer. 4.add uevent failure handler to process signal fault issue. 5.add example for request and use uevent monitoring in testpmd.

v3->v2:
1.refine some return error
2.refine the string searching logic to avoid memory issue

v2->v1:
1.remove global variables of hotplug_fd, add uevent_fd in rte_intr_handle to let each pci device self maintain it fd, to fix dual device fd issue.
2.refine some typo error.

Jeff Guo (2):
eal: add uevent monitor for hot plug
app/testpmd: use uevent to monitor hot removal

 app/test-pmd/testpmd.c                             |  90 ++++++
 lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
 .../bsdapp/eal/include/exec-env/rte_dev.h          | 105 ++++++
 lib/librte_eal/common/eal_common_bus.c             |  31 ++
 lib/librte_eal/common/eal_common_dev.c             | 223 ++++++++++++-
 lib/librte_eal/common/eal_common_pci.c             |  69 +++-
 lib/librte_eal/common/eal_common_pci_uio.c         |  31 +-
 lib/librte_eal/common/eal_common_vdev.c            |  29 +-
 lib/librte_eal/common/eal_private.h                |  13 +-
 lib/librte_eal/common/include/rte_bus.h            |  52 +++
 lib/librte_eal/common/include/rte_dev.h            | 102 +++++-
 lib/librte_eal/common/include/rte_pci.h            |  26 ++
 lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
 lib/librte_eal/linuxapp/eal/eal_dev.c              | 351 +++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci.c              |  33 ++
 .../linuxapp/eal/include/exec-env/rte_dev.h        | 105 ++++++
 16 files changed, 1318 insertions(+), 9 deletions(-)
 create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h
 create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h
Signature · 1 line
2.7.4
author
#296 lore raw

PATCH v5 1/2 eal: add uevent monitor for hot plug

This patch aim to add a general uevent mechanism in eal device layer, to enable all kernel object hot plug monitoring, so user could use these API to monitor and read out the device status info sent from the kernel side, then corresponding to handle it, such as detach or attach the device, and even benefit to use it for do smoothly fail safe work.

1) About uevent monitoring:
a: add one epolling to poll the netlink socket, to monitor the uevent of
the device, add device_state in struct of rte_device, to identify the device state machine.
b: add enum of rte_eal_dev_event_type and struct of rte_eal_uevent.
c: add below API in rte eal device common layer.
rte_eal_dev_monitor_enable
rte_dev_callback_register
rte_dev_callback_unregister
_rte_dev_callback_process
rte_dev_bind_driver
rte_dev_monitor_start
rte_dev_monitor_stop

2) About failure handler, use pci uio for example,
add pci_remap_device in bus layer and below function to process it:
rte_pci_remap_device
pci_uio_remap_resource
pci_map_private_resource

---
v5->v4:
1.Move uevent monitor epolling from eal interrupt to eal device layer.
2.Redefine the eal device API for common, and distinguish between linux and bsd
3.Add failure handler helper api in bus layer.Add function of find device by name.
4.Replace of individual fd bind with single device, use a common fd to polling all device. 5.Add to register hot insertion monitoring and process, add function to auto bind driver befor user add device 6.Refine some coding style and typos issue

---
 lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
 .../bsdapp/eal/include/exec-env/rte_dev.h          | 105 ++++++
 lib/librte_eal/common/eal_common_bus.c             |  31 ++
 lib/librte_eal/common/eal_common_dev.c             | 223 ++++++++++++-
 lib/librte_eal/common/eal_common_pci.c             |  69 +++-
 lib/librte_eal/common/eal_common_pci_uio.c         |  31 +-
 lib/librte_eal/common/eal_common_vdev.c            |  29 +-
 lib/librte_eal/common/eal_private.h                |  13 +-
 lib/librte_eal/common/include/rte_bus.h            |  52 +++
 lib/librte_eal/common/include/rte_dev.h            | 102 +++++-
 lib/librte_eal/common/include/rte_pci.h            |  26 ++
 lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
 lib/librte_eal/linuxapp/eal/eal_dev.c              | 351 +++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci.c              |  33 ++
 .../linuxapp/eal/include/exec-env/rte_dev.h        | 105 ++++++
 15 files changed, 1228 insertions(+), 9 deletions(-)
 create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h
 create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h
lib/librte_eal/bsdapp/eal/eal_dev.c new file +64 -0
new file mode 100644
index 0000000..6ea9a74
@@ -0,0 +1,64 @@
+/*-
+ *   Copyright(c) 2010-2017 Intel Corporation.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <sys/queue.h>
+#include <sys/signalfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdbool.h>
+
+#include <rte_malloc.h>
+#include <rte_bus.h>
+#include <rte_dev.h>
+#include <rte_devargs.h>
+#include <rte_debug.h>
+#include <rte_log.h>
+
+#include "eal_thread.h"
+
+int
+rte_dev_monitor_start(void)
+{
+	return -1;
+}
+
+int
+rte_dev_monitor_stop(void)
+{
+	return -1;
+}
lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +105 -0

105 lines not on this page · raw message

lib/librte_eal/common/eal_common_bus.c +31 -0
index 08bec2d..e8ed396 100644
@@ -50,8 +50,10 @@ rte_bus_register(struct rte_bus *bus)
 	RTE_VERIFY(bus->scan);
 	RTE_VERIFY(bus->probe);
 	RTE_VERIFY(bus->find_device);
+	RTE_VERIFY(bus->find_device_by_name);
 	/* Buses supporting driver plug also require unplug. */
 	RTE_VERIFY(!bus->plug || bus->unplug);
+	RTE_VERIFY(bus->remap_device);
 
 	TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
 	RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
@@ -174,6 +176,15 @@ cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
 }
 
 static int
+cmp_rte_device_name(const char *dev_name1, const void *_dev_name2)
+{
+	const char *dev_name2 = _dev_name2;
+
+	return strcmp(dev_name1, dev_name2);
+}
+
+
+static int
 bus_find_device(const struct rte_bus *bus, const void *_dev)
 {
 	struct rte_device *dev;
@@ -182,6 +193,26 @@ bus_find_device(const struct rte_bus *bus, const void *_dev)
 	return dev == NULL;
 }
 
+static struct rte_device *
+bus_find_device_by_name(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus->find_device_by_name(NULL, cmp_rte_device_name, _dev_name);
+	return dev;
+}
+
+
+struct rte_device *
+
+rte_bus_find_device(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus_find_device_by_name(bus, _dev_name);
+	return dev;
+}
+
 struct rte_bus *
 rte_bus_find_by_device(const struct rte_device *dev)
 {
lib/librte_eal/common/eal_common_dev.c +222 -1

247 lines not on this page · raw message

lib/librte_eal/common/eal_common_pci.c +66 -3

102 lines not on this page · raw message

lib/librte_eal/common/eal_common_pci_uio.c +30 -1
index 367a681..cb12349 100644
@@ -174,6 +174,34 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
 	}
 }
 
+/* remap the PCI resource of a PCI device in private virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	uint64_t phaddr;
+	void *map_address;
+
+	/* Map all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		phaddr = dev->mem_resource[i].phys_addr;
+		if (phaddr == 0)
+			continue;
+		map_address = pci_map_private_resource(
+				dev->mem_resource[i].addr, 0,
+				(size_t)dev->mem_resource[i].len);
+		if (map_address == MAP_FAILED)
+			goto error;
+		memset(map_address, 0xFF, (size_t)dev->mem_resource[i].len);
+		dev->mem_resource[i].addr = map_address;
+	}
+
+	return 0;
+error:
+	return -1;
+}
+
 static struct mapped_pci_resource *
 pci_uio_find_resource(struct rte_pci_device *dev)
 {
@@ -222,7 +250,8 @@ pci_uio_unmap_resource(struct rte_pci_device *dev)
 	rte_free(uio_res);
 
 	/* close fd if in primary process */
-	close(dev->intr_handle.fd);
+	if (dev->device.state != DEVICE_FAULT)
+		close(dev->intr_handle.fd);
 	if (dev->intr_handle.uio_cfg_fd >= 0) {
 		close(dev->intr_handle.uio_cfg_fd);
 		dev->intr_handle.uio_cfg_fd = -1;
lib/librte_eal/common/eal_common_vdev.c +28 -1
index f7e547a..ebed0b7 100644
@@ -303,7 +303,7 @@ vdev_probe(void)
 
 static struct rte_device *
 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
-		 const void *data)
+		const void *data)
 {
 	struct rte_vdev_device *dev;
 
@@ -318,6 +318,31 @@ vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 	return NULL;
 }
 
+static struct rte_device *
+vdev_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_vdev_device *dev;
+
+	TAILQ_FOREACH(dev, &vdev_device_list, next) {
+		if (start && &dev->device == start) {
+			start = NULL;
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+	return NULL;
+}
+
+static int
+vdev_remap_device(struct rte_device *dev)
+{
+	RTE_SET_USED(dev);
+	return 0;
+}
+
 static int
 vdev_plug(struct rte_device *dev)
 {
@@ -334,9 +359,11 @@ static struct rte_bus rte_vdev_bus = {
 	.scan = vdev_scan,
 	.probe = vdev_probe,
 	.find_device = vdev_find_device,
+	.find_device_by_name = vdev_find_device_by_name,
 	.plug = vdev_plug,
 	.unplug = vdev_unplug,
 	.parse = vdev_parse,
+	.remap_device = vdev_remap_device,
 };
 
 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
lib/librte_eal/common/eal_private.h +12 -1
index 597d82e..4da42be 100644
@@ -222,6 +222,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
 
 /**
+ * remap the pci uio resource..
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
  * Map device memory to uio resource
  *
  * This function is private to EAL.
@@ -354,5 +366,4 @@ bool rte_eal_using_phys_addrs(void);
  *   NULL if no bus is able to parse this device.
  */
 struct rte_bus *rte_bus_find_by_device_name(const char *str);
-
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_bus.h +52 -0

79 lines not on this page · raw message

lib/librte_eal/common/include/rte_dev.h +101 -1

129 lines not on this page · raw message

lib/librte_eal/common/include/rte_pci.h +26 -0
index 8b12339..b33d53e 100644
@@ -393,6 +393,32 @@ int rte_pci_map_device(struct rte_pci_device *dev);
 void rte_pci_unmap_device(struct rte_pci_device *dev);
 
 /**
+ * Remap this device
+ *
+ * @param dev
+ *   A pointer to a rte_pci_device structure describing the device
+ *   to use
+ */
+int rte_pci_remap_device(struct rte_pci_device *dev);
+
+/**
+ * @internal
+ * Map to a particular private resource.
+ *
+ * @param requested_addr
+ *      The starting address for the new mapping range.
+ * @param offset
+ *      The offset for the mapping range.
+ * @param size
+ *      The size for the mapping range.
+ * @return
+ *   - On success, the function returns a pointer to the mapped area.
+ *   - On error, the value MAP_FAILED is returned.
+ */
+void *pci_map_private_resource(void *requested_addr, off_t offset,
+		size_t size);
+
+/**
  * @internal
  * Map a particular resource from a file.
  *
lib/librte_eal/linuxapp/eal/Makefile +2 -1
index 90bca4d..34cf8b0 100644
@@ -73,6 +73,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_lcore.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_timer.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_interrupts.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_alarm.c
+SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_dev.c
 
 # from common dir
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_lcore.c
@@ -130,7 +131,7 @@ ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y)
 CFLAGS_eal_thread.o += -Wno-return-type
 endif
 
-INC := rte_interrupts.h rte_kni_common.h rte_dom0_common.h
+INC := rte_interrupts.h rte_kni_common.h rte_dom0_common.h rte_dev.h
 
 SYMLINK-$(CONFIG_RTE_EXEC_ENV_LINUXAPP)-include/exec-env := \
 	$(addprefix include/exec-env/,$(INC))
lib/librte_eal/linuxapp/eal/eal_dev.c new file +351 -0

351 lines not on this page · raw message

lib/librte_eal/linuxapp/eal/eal_pci.c +33 -0
index 8951ce7..6438047 100644
@@ -141,6 +141,39 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
 	}
 }
 
+/* Map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret = -1;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	/* try mapping the NIC resources using VFIO if it exists */
+	switch (dev->kdrv) {
+	case RTE_KDRV_VFIO:
+#ifdef VFIO_PRESENT
+		/* no thing to do */
+#endif
+		break;
+	case RTE_KDRV_IGB_UIO:
+	case RTE_KDRV_UIO_GENERIC:
+		if (rte_eal_using_phys_addrs()) {
+			/* map resources for devices that use uio */
+			ret = pci_uio_remap_resource(dev);
+		}
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver, skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
 void *
 pci_find_max_end_va(void)
 {
lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +105 -0

105 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#297 lore raw

PATCH v5 2/2 app/testpmd: use uevent to monitor hot removal

In reply to Jeff Guo

use testpmd for example, to show app how to request and use uevent monitoring to handle the hot removal event and the hot insertion event.

---
v5->v4:
add new callback to process hot insertion

---
 app/test-pmd/testpmd.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
app/test-pmd/testpmd.c +90 -0
index e097ee0..df2bb48 100644
@@ -393,6 +393,9 @@ static void check_all_ports_link_status(uint32_t port_mask);
 static int eth_event_callback(uint8_t port_id,
 			      enum rte_eth_event_type type,
 			      void *param, void *ret_param);
+static int eth_uevent_callback(enum rte_eal_dev_event_type type,
+			      void *param, void *ret_param);
+
 
 /*
  * Check if all the ports are started.
@@ -1413,6 +1416,7 @@ start_port(portid_t pid)
 	struct rte_port *port;
 	struct ether_addr mac_addr;
 	enum rte_eth_event_type event_type;
+	enum rte_eal_dev_event_type dev_event_type;
 
 	if (port_id_is_invalid(pid, ENABLED_WARN))
 		return 0;
@@ -1547,6 +1551,21 @@ start_port(portid_t pid)
 				return -1;
 			}
 		}
+		rte_eal_dev_monitor_enable();
+
+		for (dev_event_type = RTE_EAL_DEV_EVENT_UNKNOWN;
+		     dev_event_type < RTE_EAL_DEV_EVENT_MAX;
+		     dev_event_type++) {
+			diag = rte_dev_callback_register(dev_event_type,
+					eth_uevent_callback,
+					&pi);
+			if (diag) {
+				printf("Failed to setup uevent callback for"
+					" device event %d\n",
+					dev_event_type);
+				return -1;
+			}
+		}
 
 		/* start port */
 		if (rte_eth_dev_start(pi) < 0) {
@@ -1883,6 +1902,35 @@ rmv_event_callback(void *arg)
 			dev->device->name);
 }
 
+static void
+rmv_uevent_callback(void *arg)
+{
+	char name[RTE_ETH_NAME_MAX_LEN];
+
+	uint8_t port_id = *(uint8_t *)arg;
+
+	printf("removing device port_id:%u\n", port_id);
+
+	if (rte_eth_dev_detach(port_id, name)) {
+		RTE_LOG(ERR, USER1, "Failed to detach port '%s'\n", name);
+		return;
+	}
+
+	nb_ports = rte_eth_dev_count();
+
+	printf("Port '%s' is detached. Now total ports is %d\n",
+			name, nb_ports);
+}
+
+static void
+add_uevent_callback(void *arg)
+{
+	char *dev_name = (char *)arg;
+
+	printf("adding device %s\n", dev_name);
+	attach_port(dev_name);
+}
+
 /* This function is used by the interrupt thread */
 static int
 eth_event_callback(uint8_t port_id, enum rte_eth_event_type type, void *param,
@@ -1924,6 +1972,48 @@ eth_event_callback(uint8_t port_id, enum rte_eth_event_type type, void *param,
 	return 0;
 }
 
+/* This function is used by the interrupt thread */
+static int
+eth_uevent_callback(enum rte_eal_dev_event_type type, void *arg,
+		  void *ret_param)
+{
+	static const char * const event_desc[] = {
+		[RTE_EAL_DEV_EVENT_UNKNOWN] = "Unknown",
+		[RTE_EAL_DEV_EVENT_ADD] = "add",
+		[RTE_EAL_DEV_EVENT_REMOVE] = "remove",
+	};
+
+	RTE_SET_USED(ret_param);
+
+	if (type >= RTE_EAL_DEV_EVENT_MAX) {
+		fprintf(stderr, "%s called upon invalid event %d\n",
+			__func__, type);
+		fflush(stderr);
+	} else if (event_print_mask & (UINT32_C(1) << type)) {
+		printf("%s event\n",
+			event_desc[type]);
+		fflush(stdout);
+	}
+
+	switch (type) {
+	case RTE_EAL_DEV_EVENT_ADD:
+		if (rte_eal_alarm_set(2000000,
+			add_uevent_callback, (void *)arg))
+			fprintf(stderr, "Could not set up deferred "
+				"device removal\n");
+		break;
+	case RTE_EAL_DEV_EVENT_REMOVE:
+		if (rte_eal_alarm_set(100000,
+			rmv_uevent_callback, (void *)arg))
+			fprintf(stderr, "Could not set up deferred "
+				"device removal\n");
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
 static int
 set_tx_queue_stats_mapping_registers(uint8_t port_id, struct rte_port *port)
 {
Signature · 1 line
2.7.4
author
#298 lore raw

v6 cover add uevent monitor for hot plug

So far, about hot plug in dpdk, we already have hot plug add/remove api and fail-safe driver to offload the fail-safe work from the app user. But there are still lack of a general event api, since the interrupt event, which hot plug related with, is diversity between each device and driver, such as mlx4, pci driver and others.

Use the hot removal event for example, pci drivers not all exposure the remove interrupt, so in order to make user to easy use the hot plug feature for pci driver, something must be done to detect the remove event at the kernel level and offer a new line of interrupt to the user land.

Base on the uevent of kobject mechanism in kernel, we could use it to benefit for monitoring the hot plug status of the device which not only uio/vfio of pci bus devices, but also other, such as cpu/usb/pci-express bus devices.

The idea is comming as bellow.

a.The uevent message form FD monitoring which will be useful. remove@/devices/pci0000:80/0000:80:02.2/0000:82:00.0/0000:83:03.0/0000:84:00.2/uio/uio2 ACTION=remove DEVPATH=/devices/pci0000:80/0000:80:02.2/0000:82:00.0/0000:83:03.0/0000:84:00.2/uio/uio2 SUBSYSTEM=uio
MAJOR=243
MINOR=2
DEVNAME=uio2
SEQNUM=11366

b.add uevent monitoring machanism:
add several general api to enable uevent monitoring.

c.add common uevent handler and uevent failure handler
uevent of device should be handler at bus or device layer, and the memory read and write failure when hot removal should be handle correctly before detach behaviors.

d.show example how to use uevent monitor
enable uevent monitoring in testpmd or fail-safe to show usage.

patchset history:
v6->v5:
1.add hot plug policy, in eal, default handle to prepare hot plug work for all pci device, then let app to manage to deside which device need to hot plug.
2.modify to manage event callback in each device.
3.fix some system hung issue when igb_uio release.
4.modify the pci part to the bus-pci base on the bus rework. 5.add hot plug policy in app, show example to use hotplug list to manage to deside which device need to hot plug.

v5->v4:
1.Move uevent monitor epolling from eal interrupt to eal device layer.
2.Redefine the eal device API for common, and distinguish between linux and bsd
3.Add failure handler helper api in bus layer.Add function of find device by name.
4.Replace of individual fd bind with single device, use a common fd to polling all device. 5.Add to register hot insertion monitoring and process, add function to auto bind driver befor user add device 6.Refine some coding style and typos issue
7.add new callback to process hot insertion

v4->v3:
1.move uevent monitor api from eal interrupt to eal device layer. 2.create uevent type and struct in eal device.
3.move uevent handler for each driver to eal layer. 4.add uevent failure handler to process signal fault issue. 5.add example for request and use uevent monitoring in testpmd.

v3->v2:
1.refine some return error
2.refine the string searching logic to avoid memory issue

v2->v1:
1.remove global variables of hotplug_fd, add uevent_fd in rte_intr_handle to let each pci device self maintain it fd, to fix dual device fd issue.
2.refine some typo error.

Jeff Guo (2):
eal: add uevent monitor for hot plug
app/testpmd: use uevent to monitor hotplug

 app/test-pmd/testpmd.c                             | 172 ++++++++++
 app/test-pmd/testpmd.h                             |   9 +
 drivers/bus/pci/bsd/pci.c                          |  23 ++
 drivers/bus/pci/linux/pci.c                        |  34 ++
 drivers/bus/pci/linux/pci_init.h                   |   1 +
 drivers/bus/pci/pci_common.c                       |  42 +++
 drivers/bus/pci/pci_common_uio.c                   |  28 ++
 drivers/bus/pci/private.h                          |  12 +
 drivers/bus/pci/rte_bus_pci.h                      |   9 +
 lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
 .../bsdapp/eal/include/exec-env/rte_dev.h          | 105 ++++++
 lib/librte_eal/common/eal_common_bus.c             |  29 ++
 lib/librte_eal/common/eal_common_dev.c             | 222 +++++++++++++
 lib/librte_eal/common/eal_common_vdev.c            |  27 ++
 lib/librte_eal/common/include/rte_bus.h            |  51 +++
 lib/librte_eal/common/include/rte_dev.h            | 107 ++++++-
 lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
 lib/librte_eal/linuxapp/eal/eal_dev.c              | 353 +++++++++++++++++++++
 .../linuxapp/eal/include/exec-env/rte_dev.h        | 105 ++++++
 lib/librte_eal/linuxapp/igb_uio/igb_uio.c          |   6 +
 lib/librte_pci/rte_pci.c                           |  20 ++
 lib/librte_pci/rte_pci.h                           |  17 +
 22 files changed, 1437 insertions(+), 2 deletions(-)
 create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h
 create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h
Signature · 1 line
2.7.4
author
#299 lore raw

PATCH v6 1/2 eal: add uevent monitor for hot plug

This patch aim to add a general uevent mechanism in eal device layer, to enable all linux kernel object hot plug monitoring, so user could use these APIs to monitor and read out the device status info that sent from the kernel side, then corresponding to handle it, such as detach or attach the device, and even benefit to use it to do smoothly fail safe work.

1) About uevent monitoring:
a: add one epolling to poll the netlink socket, to monitor the uevent of
the device, add device_state in struct of rte_device, to identify the device state machine.
b: add enum of rte_eal_dev_event_type and struct of rte_eal_uevent.
c: add below API in rte eal device common layer.
rte_eal_dev_monitor_enable
rte_dev_callback_register
rte_dev_callback_unregister
_rte_dev_callback_process
rte_dev_bind_driver
rte_dev_monitor_start
rte_dev_monitor_stop

2) About failure handler, use pci uio for example,
add pci_remap_device in bus layer and below function to process it:
rte_pci_remap_device
pci_uio_remap_resource
pci_map_private_resource

---
v6->v5:
add hot plug policy, in eal, default handle to prepare hot plug work for all pci device, then let app to manage to deside which device need to hot plug.
modify to manage event callback in each device.
fix some system hung issue when igb_uio release.
modify the pci part to the bus-pci base on the bus rework.

---
 drivers/bus/pci/bsd/pci.c                          |  23 ++
 drivers/bus/pci/linux/pci.c                        |  34 ++
 drivers/bus/pci/linux/pci_init.h                   |   1 +
 drivers/bus/pci/pci_common.c                       |  42 +++
 drivers/bus/pci/pci_common_uio.c                   |  28 ++
 drivers/bus/pci/private.h                          |  12 +
 drivers/bus/pci/rte_bus_pci.h                      |   9 +
 lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
 .../bsdapp/eal/include/exec-env/rte_dev.h          | 105 ++++++
 lib/librte_eal/common/eal_common_bus.c             |  29 ++
 lib/librte_eal/common/eal_common_dev.c             | 222 +++++++++++++
 lib/librte_eal/common/eal_common_vdev.c            |  27 ++
 lib/librte_eal/common/include/rte_bus.h            |  51 +++
 lib/librte_eal/common/include/rte_dev.h            | 107 ++++++-
 lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
 lib/librte_eal/linuxapp/eal/eal_dev.c              | 353 +++++++++++++++++++++
 .../linuxapp/eal/include/exec-env/rte_dev.h        | 105 ++++++
 lib/librte_eal/linuxapp/igb_uio/igb_uio.c          |   6 +
 lib/librte_pci/rte_pci.c                           |  20 ++
 lib/librte_pci/rte_pci.h                           |  17 +
 20 files changed, 1256 insertions(+), 2 deletions(-)
 create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h
 create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h
drivers/bus/pci/bsd/pci.c +23 -0
index 39d65c6..bc78f28 100644
@@ -127,6 +127,29 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
 	}
 }
 
+/* Map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret = -1;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_NIC_UIO:
+		ret = pci_uio_remap_resource(dev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver, skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
 void
 pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res)
drivers/bus/pci/linux/pci.c +34 -0
index cdf8106..3f8a246 100644
@@ -145,6 +145,38 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
 	}
 }
 
+/* Map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret = -1;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_VFIO:
+#ifdef VFIO_PRESENT
+		/* no thing to do */
+#endif
+		break;
+	case RTE_KDRV_IGB_UIO:
+	case RTE_KDRV_UIO_GENERIC:
+		if (rte_eal_using_phys_addrs()) {
+			/* map resources for devices that use uio */
+			ret = pci_uio_remap_resource(dev);
+		}
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver, skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
 void *
 pci_find_max_end_va(void)
 {
@@ -386,6 +418,8 @@ pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
 		rte_pci_add_device(dev);
 	}
 
+	dev->device.state = DEVICE_PARSED;
+	TAILQ_INIT(&(dev->device.uev_cbs));
 	return 0;
 }
 
drivers/bus/pci/linux/pci_init.h +1 -0
index 99d7a2e..fa71f3c 100644
@@ -58,6 +58,7 @@ int pci_uio_alloc_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource **uio_res);
 void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
+int pci_uio_remap_resource(struct rte_pci_device *dev);
 int pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
 		struct mapped_pci_resource *uio_res, int map_idx);
 
drivers/bus/pci/pci_common.c +42 -0

70 lines not on this page · raw message

drivers/bus/pci/pci_common_uio.c +28 -0
index b58bcf5..bb91fbd 100644
@@ -176,6 +176,34 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
 	}
 }
 
+/* remap the PCI resource of a PCI device in private virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	uint64_t phaddr;
+	void *map_address;
+
+	/* Map all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		phaddr = dev->mem_resource[i].phys_addr;
+		if (phaddr == 0)
+			continue;
+		map_address = pci_map_private_resource(
+				dev->mem_resource[i].addr, 0,
+				(size_t)dev->mem_resource[i].len);
+		if (map_address == MAP_FAILED)
+			goto error;
+		memset(map_address, 0xFF, (size_t)dev->mem_resource[i].len);
+		dev->mem_resource[i].addr = map_address;
+	}
+
+	return 0;
+error:
+	return -1;
+}
+
 static struct mapped_pci_resource *
 pci_uio_find_resource(struct rte_pci_device *dev)
 {
drivers/bus/pci/private.h +12 -0
index 2283f09..10baa1a 100644
@@ -202,6 +202,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
 
 /**
+ * remap the pci uio resource..
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
  * Map device memory to uio resource
  *
  * This function is private to EAL.
drivers/bus/pci/rte_bus_pci.h +9 -0
index c0b619f..56ec7a1 100644
@@ -197,6 +197,15 @@ int rte_pci_map_device(struct rte_pci_device *dev);
 void rte_pci_unmap_device(struct rte_pci_device *dev);
 
 /**
+ * Remap this device
+ *
+ * @param dev
+ *   A pointer to a rte_pci_device structure describing the device
+ *   to use
+ */
+int rte_pci_remap_device(struct rte_pci_device *dev);
+
+/**
  * Dump the content of the PCI bus.
  *
  * @param f
lib/librte_eal/bsdapp/eal/eal_dev.c new file +64 -0

64 lines not on this page · raw message

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +105 -0

105 lines not on this page · raw message

lib/librte_eal/common/eal_common_bus.c +29 -0
index 3e022d5..bdb0e54 100644
@@ -51,8 +51,10 @@ rte_bus_register(struct rte_bus *bus)
 	RTE_VERIFY(bus->scan);
 	RTE_VERIFY(bus->probe);
 	RTE_VERIFY(bus->find_device);
+	RTE_VERIFY(bus->find_device_by_name);
 	/* Buses supporting driver plug also require unplug. */
 	RTE_VERIFY(!bus->plug || bus->unplug);
+	RTE_VERIFY(bus->remap_device);
 
 	TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
 	RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
@@ -170,6 +172,14 @@ cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
 }
 
 static int
+cmp_rte_device_name(const char *dev_name1, const void *_dev_name2)
+{
+	const char *dev_name2 = _dev_name2;
+
+	return strcmp(dev_name1, dev_name2);
+}
+
+static int
 bus_find_device(const struct rte_bus *bus, const void *_dev)
 {
 	struct rte_device *dev;
@@ -178,6 +188,25 @@ bus_find_device(const struct rte_bus *bus, const void *_dev)
 	return dev == NULL;
 }
 
+static struct rte_device *
+bus_find_device_by_name(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus->find_device_by_name(NULL, cmp_rte_device_name, _dev_name);
+	return dev;
+}
+
+struct rte_device *
+
+rte_bus_find_device(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus_find_device_by_name(bus, _dev_name);
+	return dev;
+}
+
 struct rte_bus *
 rte_bus_find_by_device(const struct rte_device *dev)
 {
lib/librte_eal/common/eal_common_dev.c +222 -0

240 lines not on this page · raw message

lib/librte_eal/common/eal_common_vdev.c +27 -0
index f7e547a..2ad517c 100644
@@ -318,6 +318,31 @@ vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 	return NULL;
 }
 
+static struct rte_device *
+vdev_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_vdev_device *dev;
+
+	TAILQ_FOREACH(dev, &vdev_device_list, next) {
+		if (start && &dev->device == start) {
+			start = NULL;
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+	return NULL;
+}
+
+static int
+vdev_remap_device(struct rte_device *dev)
+{
+	RTE_SET_USED(dev);
+	return 0;
+}
+
 static int
 vdev_plug(struct rte_device *dev)
 {
@@ -334,9 +359,11 @@ static struct rte_bus rte_vdev_bus = {
 	.scan = vdev_scan,
 	.probe = vdev_probe,
 	.find_device = vdev_find_device,
+	.find_device_by_name = vdev_find_device_by_name,
 	.plug = vdev_plug,
 	.unplug = vdev_unplug,
 	.parse = vdev_parse,
+	.remap_device = vdev_remap_device,
 };
 
 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
lib/librte_eal/common/include/rte_bus.h +51 -0

78 lines not on this page · raw message

lib/librte_eal/common/include/rte_dev.h +106 -1

134 lines not on this page · raw message

lib/librte_eal/linuxapp/eal/Makefile +2 -1
index 1d3a42d..e57da21 100644
@@ -67,6 +67,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_lcore.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_timer.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_interrupts.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_alarm.c
+SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_dev.c
 
 # from common dir
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_lcore.c
@@ -139,7 +140,7 @@ ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y)
 CFLAGS_eal_thread.o += -Wno-return-type
 endif
 
-INC := rte_kni_common.h
+INC := rte_kni_common.h rte_dev.h
 
 SYMLINK-$(CONFIG_RTE_EXEC_ENV_LINUXAPP)-include/exec-env := \
 	$(addprefix include/exec-env/,$(INC))
lib/librte_eal/linuxapp/eal/eal_dev.c new file +353 -0

353 lines not on this page · raw message

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +105 -0

105 lines not on this page · raw message

lib/librte_eal/linuxapp/igb_uio/igb_uio.c +6 -0
index fd320d8..ba10a59 100644
@@ -354,6 +354,12 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)
 	struct rte_uio_pci_dev *udev = info->priv;
 	struct pci_dev *dev = udev->pdev;
 
+	/* check if device have been remove before release */
+	if ((&dev->dev.kobj)->state_remove_uevent_sent == 1) {
+		pr_info("The device have been removed\n");
+		return -1;
+	}
+
 	/* disable interrupts */
 	igbuio_pci_disable_interrupts(udev);
 
lib/librte_pci/rte_pci.c +20 -0
index 1307a18..0d37ce8 100644
@@ -180,6 +180,26 @@ pci_addr_parse(const char *str, struct rte_pci_addr *addr)
 	return -1;
 }
 
+/* map a private resource from an address*/
+void *
+pci_map_private_resource(void *requested_addr, off_t offset, size_t size)
+{
+	void *mapaddr;
+
+	mapaddr = mmap(requested_addr, size,
+			   PROT_READ | PROT_WRITE,
+			   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+	if (mapaddr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "%s(): cannot mmap(%p, 0x%lx, 0x%lx): "
+			"%s (%p)\n",
+			__func__, requested_addr,
+			(unsigned long)size, (unsigned long)offset,
+			strerror(errno), mapaddr);
+	} else
+		RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
+
+	return mapaddr;
+}
 
 /* map a particular resource from a file */
 void *
lib/librte_pci/rte_pci.h +17 -0
index ea0897c..c5392fe 100644
@@ -243,6 +243,23 @@ int pci_addr_cmp(const struct rte_pci_addr *addr,
 int pci_addr_parse(const char *str, struct rte_pci_addr *addr);
 
 /**
+ * @internal
+ * Map to a particular private resource.
+ *
+ * @param requested_addr
+ *      The starting address for the new mapping range.
+ * @param offset
+ *      The offset for the mapping range.
+ * @param size
+ *      The size for the mapping range.
+ * @return
+ *   - On success, the function returns a pointer to the mapped area.
+ *   - On error, the value MAP_FAILED is returned.
+ */
+void *pci_map_private_resource(void *requested_addr, off_t offset,
+		size_t size);
+
+/**
  * Map a particular resource from a file.
  *
  * @param requested_addr
Signature · 1 line
2.7.4
#300 lore raw

Re: [PATCH v6 1/2] eal: add uevent monitor for hot plug

drivers/bus/pci/bsd/pci.c rte_pci_unmap_device(struct rte_pci_device *dev) · line 134

Quoted 6 lines · On Thu, 2 Nov 2017 04:16:44 +0800 Jeff Guo <[email protected]> wrote:
drivers/bus/pci/bsd/pci.c +5 -0
+/* Map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret = -1;

Please don't always initialize variables. It is unnecessary, and with modern compilers a bad habit since it defeats the uninitailized variable warning message which is useful to detect buggy code paths.

Quoted 17 lines
drivers/bus/pci/bsd/pci.c +17 -0
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_NIC_UIO:
+		ret = pci_uio_remap_resource(dev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver, skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
#301 lore raw

Re: [PATCH v6 1/2] eal: add uevent monitor for hot plug

In reply to Jeff Guo
Quoted 6 lines · On Thu, 2 Nov 2017 04:16:44 +0800 Jeff Guo <[email protected]> wrote:
lib/librte_eal/linuxapp/eal/eal_dev.c new file +6 -0
+
+static int
+dev_uev_parse(const char *buf, struct rte_eal_uevent *event)
+{
+	char action[RTE_EAL_UEVENT_MSG_LEN];
+	char subsystem[RTE_EAL_UEVENT_MSG_LEN];

lib/librte_eal/linuxapp/eal/eal_dev.c line 128

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	char dev_path[RTE_EAL_UEVENT_MSG_LEN];
+	char pci_slot_name[RTE_EAL_UEVENT_MSG_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(subsystem, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(dev_path, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(pci_slot_name, 0, RTE_EAL_UEVENT_MSG_LEN);
+
+	while (i < RTE_EAL_UEVENT_MSG_LEN) {

Might be simpler, safer, clearer to use rte_strsplit here.

And then have a table of fields rather than open coding the parsing.

Quoted 33 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +33 -0
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf)
+				break;
+			buf++;
+		}
+		if (!strncmp(buf, "libudev", 7)) {
+			buf += 7;
+			i += 7;
+			event->group = UEV_MONITOR_UDEV;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		} else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
+			buf += 14;
+			i += 14;
+			snprintf(pci_slot_name, sizeof(subsystem), "%s", buf);
+		}
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;
+		}
+	}

lib/librte_eal/linuxapp/eal/eal_dev.c line 171

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+
+	if (!strncmp(subsystem, "pci", 3))
+		event->subsystem = UEV_SUBSYSTEM_PCI;
+	if (!strncmp(action, "add", 3))
+		event->type = RTE_EAL_DEV_EVENT_ADD;
+	if (!strncmp(action, "remove", 6))
+		event->type = RTE_EAL_DEV_EVENT_REMOVE;
+	event->devname = pci_slot_name;
+
+	return 0;

Function always returns 0, why is it not void?

author
#302 lore raw

Re: [PATCH v6 1/2] eal: add uevent monitor for hot plug

Thanks Stephen for your eye on review, would collect other comment and refine it better in next version.

Best regards,
Jeff Guo

-----Original Message-----
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, November 2, 2017 5:42 AM
To: Guo, Jia <[email protected]>
Cc: Richardson, Bruce <[email protected]>; Yigit, Ferruh <[email protected]>; [email protected]; [email protected]; Ananyev, Konstantin <[email protected]>; [email protected]; [email protected]; Wu, Jingjing <[email protected]>; [email protected]; Zhang, Helin <[email protected]>
Subject: Re: [PATCH v6 1/2] eal: add uevent monitor for hot plug

Quoted 5 lines · On Thu, 2 Nov 2017 04:16:44 +0800 Jeff Guo <[email protected]> wrote:
lib/librte_eal/linuxapp/eal/eal_dev.c new file +5 -0
+
+static int
+dev_uev_parse(const char *buf, struct rte_eal_uevent *event) {
+	char action[RTE_EAL_UEVENT_MSG_LEN];
+	char subsystem[RTE_EAL_UEVENT_MSG_LEN];

lib/librte_eal/linuxapp/eal/eal_dev.c line 128

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	char dev_path[RTE_EAL_UEVENT_MSG_LEN];
+	char pci_slot_name[RTE_EAL_UEVENT_MSG_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(subsystem, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(dev_path, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(pci_slot_name, 0, RTE_EAL_UEVENT_MSG_LEN);
+
+	while (i < RTE_EAL_UEVENT_MSG_LEN) {

Might be simpler, safer, clearer to use rte_strsplit here.

And then have a table of fields rather than open coding the parsing.

Quoted 33 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +33 -0
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf)
+				break;
+			buf++;
+		}
+		if (!strncmp(buf, "libudev", 7)) {
+			buf += 7;
+			i += 7;
+			event->group = UEV_MONITOR_UDEV;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		} else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
+			buf += 14;
+			i += 14;
+			snprintf(pci_slot_name, sizeof(subsystem), "%s", buf);
+		}
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;
+		}
+	}

lib/librte_eal/linuxapp/eal/eal_dev.c line 171

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+
+	if (!strncmp(subsystem, "pci", 3))
+		event->subsystem = UEV_SUBSYSTEM_PCI;
+	if (!strncmp(action, "add", 3))
+		event->type = RTE_EAL_DEV_EVENT_ADD;
+	if (!strncmp(action, "remove", 6))
+		event->type = RTE_EAL_DEV_EVENT_REMOVE;
+	event->devname = pci_slot_name;
+
+	return 0;

Function always returns 0, why is it not void?

author
#303 lore raw

Re: [PATCH v6 1/2] eal: add uevent monitor for hot plug

In reply to Stephen Hemminger
Quoted 21 lines · Stephen Hemminger
Quoted 6 lines · On Thu, 2 Nov 2017 04:16:44 +0800 Jeff Guo <[email protected]> wrote:
lib/librte_eal/linuxapp/eal/eal_dev.c new file +6 -0
+
+static int
+dev_uev_parse(const char *buf, struct rte_eal_uevent *event)
+{
+	char action[RTE_EAL_UEVENT_MSG_LEN];
+	char subsystem[RTE_EAL_UEVENT_MSG_LEN];

lib/librte_eal/linuxapp/eal/eal_dev.c line 128

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	char dev_path[RTE_EAL_UEVENT_MSG_LEN];
+	char pci_slot_name[RTE_EAL_UEVENT_MSG_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(subsystem, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(dev_path, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(pci_slot_name, 0, RTE_EAL_UEVENT_MSG_LEN);
+
+	while (i < RTE_EAL_UEVENT_MSG_LEN) {

Might be simpler, safer, clearer to use rte_strsplit here.

Quoted 2 lines

And then have a table of fields rather than open coding the parsing.

i think your point must be make sense , but it hardly use rte_strsplit here , because the tokens which need to parse is splited by '\0', even more multi adjacent '\0' in the buf witch come from the uevent massage.

Quoted 44 lines
Quoted 33 lines

f+ for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {

lib/librte_eal/linuxapp/eal/eal_dev.c new file +32 -0
+			if (*buf)
+				break;
+			buf++;
+		}
+		if (!strncmp(buf, "libudev", 7)) {
+			buf += 7;
+			i += 7;
+			event->group = UEV_MONITOR_UDEV;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		} else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
+			buf += 14;
+			i += 14;
+			snprintf(pci_slot_name, sizeof(subsystem), "%s", buf);
+		}
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;
+		}
+	}

lib/librte_eal/linuxapp/eal/eal_dev.c line 171

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+
+	if (!strncmp(subsystem, "pci", 3))
+		event->subsystem = UEV_SUBSYSTEM_PCI;
+	if (!strncmp(action, "add", 3))
+		event->type = RTE_EAL_DEV_EVENT_ADD;
+	if (!strncmp(action, "remove", 6))
+		event->type = RTE_EAL_DEV_EVENT_REMOVE;
+	event->devname = pci_slot_name;
+
+	return 0;

Function always returns 0, why is it not void?

#304 lore raw

Re: [PATCH v6 1/2] eal: add uevent monitor for hot plug

In reply to Jeff Guo

Review

Quoted 2 lines · On Thu, 2 Nov 2017 04:16:44 +0800 Jeff Guo <[email protected]> wrote:
(quoted diff) +2 -0
+int
+rte_dev_bind_driver(const char *dev_name, const char *drv_type) {

Bracket left after declaration.

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 437

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+	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;
+	}

You should not have dev functions that assume PCI. Please split into common and bus specific code.

Quoted 19 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +19 -0
+static int
+dev_uev_parse(const char *buf, struct rte_eal_uevent *event)
+{
+	char action[RTE_EAL_UEVENT_MSG_LEN];
+	char subsystem[RTE_EAL_UEVENT_MSG_LEN];
+	char dev_path[RTE_EAL_UEVENT_MSG_LEN];
+	char pci_slot_name[RTE_EAL_UEVENT_MSG_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(subsystem, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(dev_path, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(pci_slot_name, 0, RTE_EAL_UEVENT_MSG_LEN);
+
+	while (i < RTE_EAL_UEVENT_MSG_LEN) {
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf)
+				break;
+			buf++;

lib/librte_eal/linuxapp/eal/eal_dev.c line 142

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+		}
+		if (!strncmp(buf, "libudev", 7)) {
+			buf += 7;
+			i += 7;
+			event->group = UEV_MONITOR_UDEV;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);

Why snprintf rather than strncpy?

Quoted 17 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +17 -0
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		} else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
+			buf += 14;
+			i += 14;
+			snprintf(pci_slot_name, sizeof(subsystem), "%s", buf);
+		}
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;

lib/librte_eal/linuxapp/eal/eal_dev.c line 169

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+		}
+	}
+
+	if (!strncmp(subsystem, "pci", 3))
+		event->subsystem = UEV_SUBSYSTEM_PCI;
+	if (!strncmp(action, "add", 3))
+		event->type = RTE_EAL_DEV_EVENT_ADD;
+	if (!strncmp(action, "remove", 6))
+		event->type = RTE_EAL_DEV_EVENT_REMOVE;
+	event->devname = pci_slot_name;

Why do you need to first capture the strings, then set state variables? Instead why not update event->xxx directly?

author
#305 lore raw

Re: [PATCH v6 1/2] eal: add uevent monitor for hot plug

Quoted 6 lines · Stephen Hemminger

Review

Quoted 2 lines · On Thu, 2 Nov 2017 04:16:44 +0800 Jeff Guo <[email protected]> wrote:
(quoted diff) +2 -0
+int
+rte_dev_bind_driver(const char *dev_name, const char *drv_type) {

Bracket left after declaration.

thanks.

Quoted 17 lines

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 437

Quoted 12 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+	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;
+	}

You should not have dev functions that assume PCI. Please split into common and bus specific code.

make sense, will modify it into bus specific code.

Quoted 22 lines
Quoted 22 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +22 -0
+static int
+dev_uev_parse(const char *buf, struct rte_eal_uevent *event)
+{
+	char action[RTE_EAL_UEVENT_MSG_LEN];
+	char subsystem[RTE_EAL_UEVENT_MSG_LEN];
+	char dev_path[RTE_EAL_UEVENT_MSG_LEN];
+	char pci_slot_name[RTE_EAL_UEVENT_MSG_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(subsystem, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(dev_path, 0, RTE_EAL_UEVENT_MSG_LEN);
+	memset(pci_slot_name, 0, RTE_EAL_UEVENT_MSG_LEN);
+
+	while (i < RTE_EAL_UEVENT_MSG_LEN) {
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf)
+				break;
+			buf++;
+		}
+		if (!strncmp(buf, "libudev", 7)) {
+			buf += 7;
Quoted 9 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 142

Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +7 -0
+			i += 7;
+			event->group = UEV_MONITOR_UDEV;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);

Why snprintf rather than strncpy?

snprintf would no need manual write '\0' and the src length is not explicit, and if concern about the efficiency of the snprintf scan, i will constrain the value of dest buf length.

Quoted 21 lines
Quoted 21 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +21 -0
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		} else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
+			buf += 14;
+			i += 14;
+			snprintf(pci_slot_name, sizeof(subsystem), "%s", buf);
+		}
+		for (; i < RTE_EAL_UEVENT_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;
+		}
+	}
+
+	if (!strncmp(subsystem, "pci", 3))
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 169

Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +6 -0
+		event->subsystem = UEV_SUBSYSTEM_PCI;
+	if (!strncmp(action, "add", 3))
+		event->type = RTE_EAL_DEV_EVENT_ADD;
+	if (!strncmp(action, "remove", 6))
+		event->type = RTE_EAL_DEV_EVENT_REMOVE;
+	event->devname = pci_slot_name;

Why do you need to first capture the strings, then set state variables? Instead why not update event->xxx directly?

i think that would be more benefit to read and manage out of the loop.

author
#306 lore raw

PATCH v6 2/2 app/testpmd: use uevent to monitor hotplug

In reply to Jeff Guo

use testpmd for example, to show app how to request and use uevent monitoring to handle the hot removal event and the hot insertion event.

---
v6->v5:
add hot plug policy in app, show example to use hotplug list to manage to deside which device need to hot plug.

---
 app/test-pmd/testpmd.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++
 app/test-pmd/testpmd.h |   9 +++
 2 files changed, 181 insertions(+)
app/test-pmd/testpmd.c +172 -0
index 9ae5b1f..ac75617 100644
@@ -402,6 +402,8 @@ uint8_t bitrate_enabled;
 struct gro_status gro_ports[RTE_MAX_ETHPORTS];
 uint8_t gro_flush_cycles = GRO_DEFAULT_FLUSH_CYCLES;
 
+static struct hotplug_request_list hp_list;
+
 /* Forward function declarations */
 static void map_port_queue_stats_mapping_registers(portid_t pi,
 						   struct rte_port *port);
@@ -409,6 +411,13 @@ static void check_all_ports_link_status(uint32_t port_mask);
 static int eth_event_callback(portid_t port_id,
 			      enum rte_eth_event_type type,
 			      void *param, void *ret_param);
+static int eth_uevent_callback(enum rte_eal_dev_event_type type,
+			      void *param, void *ret_param);
+static int eth_uevent_callback_register(portid_t pid);
+static int in_hotplug_list(const char *dev_name);
+
+static int hotplug_list_add(const char *dev_name,
+			    enum rte_eal_dev_event_type event);
 
 /*
  * Check if all the ports are started.
@@ -1758,6 +1767,31 @@ reset_port(portid_t pid)
 	printf("Done\n");
 }
 
+static int
+eth_uevent_callback_register(portid_t pid) {
+	int diag;
+	struct rte_eth_dev *dev;
+	enum rte_eal_dev_event_type dev_event_type;
+
+	/* register the uevent callback */
+	dev = &rte_eth_devices[pid];
+	for (dev_event_type = RTE_EAL_DEV_EVENT_ADD;
+		 dev_event_type < RTE_EAL_DEV_EVENT_CHANGE;
+		 dev_event_type++) {
+		diag = rte_dev_callback_register(dev->device, dev_event_type,
+			eth_uevent_callback,
+			(void *)(intptr_t)pid);
+		if (diag) {
+			printf("Failed to setup uevent callback for"
+				" device event %d\n",
+				dev_event_type);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 void
 attach_port(char *identifier)
 {
@@ -1774,6 +1808,8 @@ attach_port(char *identifier)
 	if (rte_eth_dev_attach(identifier, &pi))
 		return;
 
+	eth_uevent_callback_register(pi);
+
 	socket_id = (unsigned)rte_eth_dev_socket_id(pi);
 	/* if socket_id is invalid, set to 0 */
 	if (check_socket_id(socket_id) < 0)
@@ -1785,6 +1821,8 @@ attach_port(char *identifier)
 
 	ports[pi].port_status = RTE_PORT_STOPPED;
 
+	hotplug_list_add(identifier, RTE_EAL_DEV_EVENT_REMOVE);
+
 	printf("Port %d is attached. Now total ports is %d\n", pi, nb_ports);
 	printf("Done\n");
 }
@@ -1811,6 +1849,9 @@ detach_port(portid_t port_id)
 
 	nb_ports = rte_eth_dev_count();
 
+	hotplug_list_add(rte_eth_devices[port_id].device->name,
+			 RTE_EAL_DEV_EVENT_ADD);
+
 	printf("Port '%s' is detached. Now total ports is %d\n",
 			name, nb_ports);
 	printf("Done\n");
@@ -1834,6 +1875,9 @@ pmd_test_exit(void)
 			close_port(pt_id);
 		}
 	}
+
+	rte_dev_monitor_stop();
+
 	printf("\nBye...\n");
 }
 
@@ -1918,6 +1962,43 @@ rmv_event_callback(void *arg)
 			dev->device->name);
 }
 
+static void
+rmv_uevent_callback(void *arg)
+{
+	char name[RTE_ETH_NAME_MAX_LEN];
+	uint8_t port_id = (intptr_t)arg;
+
+	RTE_ETH_VALID_PORTID_OR_RET(port_id);
+	printf("removing port id:%u\n", port_id);
+
+	if (!in_hotplug_list(rte_eth_devices[port_id].device->name))
+		return;
+
+	stop_port(port_id);
+	close_port(port_id);
+	if (rte_eth_dev_detach(port_id, name)) {
+		RTE_LOG(ERR, USER1, "Failed to detach port '%s'\n", name);
+		return;
+	}
+
+	nb_ports = rte_eth_dev_count();
+
+	printf("Port '%s' is detached. Now total ports is %d\n",
+			name, nb_ports);
+}
+
+static void
+add_uevent_callback(void *arg)
+{
+	char *dev_name = (char *)arg;
+
+	if (!in_hotplug_list(dev_name))
+		return;
+
+	RTE_LOG(ERR, EAL, "add device: %s\n", dev_name);
+	attach_port(dev_name);
+}
+
 /* This function is used by the interrupt thread */
 static int
 eth_event_callback(portid_t port_id, enum rte_eth_event_type type, void *param,
@@ -1960,6 +2041,88 @@ eth_event_callback(portid_t port_id, enum rte_eth_event_type type, void *param,
 }
 
 static int
+in_hotplug_list(const char *dev_name)
+{
+	struct hotplug_request *hp_request = NULL;
+
+	TAILQ_FOREACH(hp_request, &hp_list, next) {
+		if (!strcmp(hp_request->dev_name, dev_name))
+			break;
+	}
+
+	if (hp_request)
+		return 1;
+
+	return 0;
+}
+
+static int
+hotplug_list_add(const char *dev_name, enum rte_eal_dev_event_type event)
+{
+	struct hotplug_request *hp_request;
+
+	hp_request = rte_zmalloc("hoplug request",
+			sizeof(*hp_request), 0);
+	if (hp_request == NULL) {
+		fprintf(stderr, "%s can not alloc memory\n",
+			__func__);
+		return -ENOMEM;
+	}
+
+	hp_request->dev_name = dev_name;
+	hp_request->event = event;
+
+	TAILQ_INSERT_TAIL(&hp_list, hp_request, next);
+
+	return 0;
+}
+
+/* This function is used by the interrupt thread */
+static int
+eth_uevent_callback(enum rte_eal_dev_event_type type, void *arg,
+		  void *ret_param)
+{
+	static const char * const event_desc[] = {
+		[RTE_EAL_DEV_EVENT_UNKNOWN] = "Unknown",
+		[RTE_EAL_DEV_EVENT_ADD] = "add",
+		[RTE_EAL_DEV_EVENT_REMOVE] = "remove",
+	};
+	static char *device_name;
+
+	RTE_SET_USED(ret_param);
+
+	if (type >= RTE_EAL_DEV_EVENT_MAX) {
+		fprintf(stderr, "%s called upon invalid event %d\n",
+			__func__, type);
+		fflush(stderr);
+	} else if (event_print_mask & (UINT32_C(1) << type)) {
+		printf("%s event\n",
+			event_desc[type]);
+		fflush(stdout);
+	}
+
+	switch (type) {
+	case RTE_EAL_DEV_EVENT_REMOVE:
+		if (rte_eal_alarm_set(100000,
+			rmv_uevent_callback, arg))
+			fprintf(stderr, "Could not set up deferred "
+				"device removal\n");
+		break;
+	case RTE_EAL_DEV_EVENT_ADD:
+		device_name = malloc(strlen((const char *)arg) + 1);
+		strcpy(device_name, arg);
+		if (rte_eal_alarm_set(500000,
+			add_uevent_callback, device_name))
+			fprintf(stderr, "Could not set up deferred "
+				"device add\n");
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
+static int
 set_tx_queue_stats_mapping_registers(portid_t port_id, struct rte_port *port)
 {
 	uint16_t i;
@@ -2439,6 +2602,15 @@ main(int argc, char** argv)
 		       nb_rxq, nb_txq);
 
 	init_config();
+
+	/* enable hot plug monitoring */
+	TAILQ_INIT(&hp_list);
+	rte_eal_dev_monitor_enable();
+	RTE_ETH_FOREACH_DEV(port_id) {
+		hotplug_list_add(rte_eth_devices[port_id].device->name,
+			 RTE_EAL_DEV_EVENT_REMOVE);
+		eth_uevent_callback_register(port_id);
+	}
 	if (start_port(RTE_PORT_ALL) != 0)
 		rte_exit(EXIT_FAILURE, "Start ports failed\n");
 
app/test-pmd/testpmd.h +9 -0
index 1639d27..9a1088b 100644
@@ -92,6 +92,15 @@ typedef uint16_t streamid_t;
 #define TM_MODE			0
 #endif
 
+struct hotplug_request {
+	TAILQ_ENTRY(hotplug_request) next; /**< Callbacks list */
+	const char *dev_name;                /* request device name */
+	enum rte_eal_dev_event_type event;      /**< device event type */
+};
+
+/** @internal Structure to keep track of registered callbacks */
+TAILQ_HEAD(hotplug_request_list, hotplug_request);
+
 enum {
 	PORT_TOPOLOGY_PAIRED,
 	PORT_TOPOLOGY_CHAINED,
Signature · 1 line
2.7.4
author
#307 lore raw

v7 cover add uevent monitor for hot plug

So far, about hot plug in dpdk, we already have hot plug add/remove api and fail-safe driver to offload the fail-safe work from the app user. But there are still lack of a general event api, since the interrupt event, which hot plug related with, is diversity between each device and driver, such as mlx4, pci driver and others.

Use the hot removal event for example, pci drivers not all exposure the remove interrupt, so in order to make user to easy use the hot plug feature for pci driver, something must be done to detect the remove event at the kernel level and offer a new line of interrupt to the user land.

Base on the uevent of kobject mechanism in kernel, we could use it to benefit for monitoring the hot plug status of the device which not only uio/vfio of pci bus devices, but also other, such as cpu/usb/pci-express bus devices.

The idea is comming as bellow.

a.The uevent message form FD monitoring which will be useful. remove@/devices/pci0000:80/0000:80:02.2/0000:82:00.0/0000:83:03.0/0000:84:00.2/uio/uio2 ACTION=remove DEVPATH=/devices/pci0000:80/0000:80:02.2/0000:82:00.0/0000:83:03.0/0000:84:00.2/uio/uio2 SUBSYSTEM=uio
MAJOR=243
MINOR=2
DEVNAME=uio2
SEQNUM=11366

b.add uevent monitoring machanism:
add several general api to enable uevent monitoring.

c.add common uevent handler and uevent failure handler
uevent of device should be handler at bus or device layer, and the memory read and write failure when hot removal should be handle correctly before detach behaviors.

d.show example how to use uevent monitor
enable uevent monitoring in testpmd or fail-safe to show usage.

patchset history:
v7->v6:
1.modify vdev part according to the vdev rework 2.re-define and split the func into common and bus specific code 3.fix some incorrect issue.
4.fix the system hung after send packcet issue.

v6->v5:
1.add hot plug policy, in eal, default handle to prepare hot plug work for all pci device, then let app to manage to deside which device need to hot plug.
2.modify to manage event callback in each device.
3.fix some system hung issue when igb_uio release.
4.modify the pci part to the bus-pci base on the bus rework. 5.add hot plug policy in app, show example to use hotplug list to manage to deside which device need to hot plug.

v5->v4:
1.Move uevent monitor epolling from eal interrupt to eal device layer.
2.Redefine the eal device API for common, and distinguish between linux and bsd
3.Add failure handler helper api in bus layer.Add function of find device by name.
4.Replace of individual fd bind with single device, use a common fd to polling all device. 5.Add to register hot insertion monitoring and process, add function to auto bind driver befor user add device 6.Refine some coding style and typos issue
7.add new callback to process hot insertion

v4->v3:
1.move uevent monitor api from eal interrupt to eal device layer. 2.create uevent type and struct in eal device.
3.move uevent handler for each driver to eal layer. 4.add uevent failure handler to process signal fault issue. 5.add example for request and use uevent monitoring in testpmd.

v3->v2:
1.refine some return error
2.refine the string searching logic to avoid memory issue

v2->v1:
1.remove global variables of hotplug_fd, add uevent_fd in rte_intr_handle to let each pci device self maintain it fd, to fix dual device fd issue.
2.refine some typo error.

Jeff Guo (2):
eal: add uevent monitor for hot plug
app/testpmd: use uevent to monitor hotplug

 app/test-pmd/testpmd.c                             | 178 +++++++++++
 app/test-pmd/testpmd.h                             |   9 +
 drivers/bus/pci/bsd/pci.c                          |  30 ++
 drivers/bus/pci/linux/pci.c                        |  87 +++++
 drivers/bus/pci/linux/pci_init.h                   |   1 +
 drivers/bus/pci/pci_common.c                       |  43 +++
 drivers/bus/pci/pci_common_uio.c                   |  28 ++
 drivers/bus/pci/private.h                          |  12 +
 drivers/bus/pci/rte_bus_pci.h                      |  25 ++
 drivers/bus/vdev/vdev.c                            |  36 +++
 lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
 .../bsdapp/eal/include/exec-env/rte_dev.h          | 106 ++++++
 lib/librte_eal/common/eal_common_bus.c             |  30 ++
 lib/librte_eal/common/eal_common_dev.c             | 169 ++++++++++
 lib/librte_eal/common/include/rte_bus.h            |  69 ++++
 lib/librte_eal/common/include/rte_dev.h            |  89 ++++++
 lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
 lib/librte_eal/linuxapp/eal/eal_alarm.c            |   5 +
 lib/librte_eal/linuxapp/eal/eal_dev.c              | 356 +++++++++++++++++++++
 .../linuxapp/eal/include/exec-env/rte_dev.h        | 106 ++++++
 lib/librte_eal/linuxapp/igb_uio/igb_uio.c          |   6 +
 lib/librte_pci/rte_pci.c                           |  20 ++
 lib/librte_pci/rte_pci.h                           |  17 +
 23 files changed, 1488 insertions(+), 1 deletion(-)
 create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h
 create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h
Signature · 1 line
2.7.4
author
#308 lore raw

PATCH v7 1/2 eal: add uevent monitor for hot plug

This patch aim to add a general uevent mechanism in eal device layer, to enable all linux kernel object hot plug monitoring, so user could use these APIs to monitor and read out the device status info that sent from the kernel side, then corresponding to handle it, such as detach or attach the device, and even benefit to use it to do smoothly fail safe work.

1) About uevent monitoring:
a: add one epolling to poll the netlink socket, to monitor the uevent of
the device, add device_state in struct of rte_device, to identify the device state machine.
b: add enum of rte_eal_dev_event_type and struct of rte_eal_uevent.
c: add below API in rte eal device common layer.
rte_eal_dev_monitor_enable
rte_dev_callback_register
rte_dev_callback_unregister
_rte_dev_callback_process
rte_dev_monitor_start
rte_dev_monitor_stop

2) About failure handler, use pci uio for example,
add pci_remap_device in bus layer and below function to process it:
rte_pci_remap_device
pci_uio_remap_resource
pci_map_private_resource
add rte_pci_dev_bind_driver to bind pci device with explicit driver.

---
v7->v6:
a.modify vdev part according to the vdev rework b.re-define and split the func into common and bus specific code c.fix some incorrect issue.
b.fix the system hung after send packcet issue.

---
 drivers/bus/pci/bsd/pci.c                          |  30 ++
 drivers/bus/pci/linux/pci.c                        |  87 +++++
 drivers/bus/pci/linux/pci_init.h                   |   1 +
 drivers/bus/pci/pci_common.c                       |  43 +++
 drivers/bus/pci/pci_common_uio.c                   |  28 ++
 drivers/bus/pci/private.h                          |  12 +
 drivers/bus/pci/rte_bus_pci.h                      |  25 ++
 drivers/bus/vdev/vdev.c                            |  36 +++
 lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
 .../bsdapp/eal/include/exec-env/rte_dev.h          | 106 ++++++
 lib/librte_eal/common/eal_common_bus.c             |  30 ++
 lib/librte_eal/common/eal_common_dev.c             | 169 ++++++++++
 lib/librte_eal/common/include/rte_bus.h            |  69 ++++
 lib/librte_eal/common/include/rte_dev.h            |  89 ++++++
 lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
 lib/librte_eal/linuxapp/eal/eal_alarm.c            |   5 +
 lib/librte_eal/linuxapp/eal/eal_dev.c              | 356 +++++++++++++++++++++
 .../linuxapp/eal/include/exec-env/rte_dev.h        | 106 ++++++
 lib/librte_eal/linuxapp/igb_uio/igb_uio.c          |   6 +
 lib/librte_pci/rte_pci.c                           |  20 ++
 lib/librte_pci/rte_pci.h                           |  17 +
 21 files changed, 1301 insertions(+), 1 deletion(-)
 create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h
 create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h
drivers/bus/pci/bsd/pci.c +30 -0
index b8e2178..d58dbf6 100644
@@ -126,6 +126,29 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
 	}
 }
 
+/* re-map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_NIC_UIO:
+		ret = pci_uio_remap_resource(dev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver, skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
 void
 pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res)
@@ -678,3 +701,10 @@ rte_pci_ioport_unmap(struct rte_pci_ioport *p)
 
 	return ret;
 }
+
+int
+rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	return -1;
+}
+
drivers/bus/pci/linux/pci.c +87 -0

102 lines not on this page · raw message

drivers/bus/pci/linux/pci_init.h +1 -0
index f342c47..5838402 100644
@@ -58,6 +58,7 @@ int pci_uio_alloc_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource **uio_res);
 void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
+int pci_uio_remap_resource(struct rte_pci_device *dev);
 int pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
 		struct mapped_pci_resource *uio_res, int map_idx);
 
drivers/bus/pci/pci_common.c +43 -0

71 lines not on this page · raw message

drivers/bus/pci/pci_common_uio.c +28 -0
index 0671131..8cb4009 100644
@@ -176,6 +176,34 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
 	}
 }
 
+/* remap the PCI resource of a PCI device in private virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	uint64_t phaddr;
+	void *map_address;
+
+	/* Map all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		phaddr = dev->mem_resource[i].phys_addr;
+		if (phaddr == 0)
+			continue;
+		map_address = pci_map_private_resource(
+				dev->mem_resource[i].addr, 0,
+				(size_t)dev->mem_resource[i].len);
+		if (map_address == MAP_FAILED)
+			goto error;
+		memset(map_address, 0xFF, (size_t)dev->mem_resource[i].len);
+		dev->mem_resource[i].addr = map_address;
+	}
+
+	return 0;
+error:
+	return -1;
+}
+
 static struct mapped_pci_resource *
 pci_uio_find_resource(struct rte_pci_device *dev)
 {
drivers/bus/pci/private.h +12 -0
index 2283f09..10baa1a 100644
@@ -202,6 +202,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
 
 /**
+ * remap the pci uio resource..
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
  * Map device memory to uio resource
  *
  * This function is private to EAL.
drivers/bus/pci/rte_bus_pci.h +25 -0
index d4a2996..1662f3b 100644
@@ -52,6 +52,8 @@ extern "C" {
 #include <sys/queue.h>
 #include <stdint.h>
 #include <inttypes.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 #include <rte_debug.h>
 #include <rte_interrupts.h>
@@ -197,6 +199,15 @@ int rte_pci_map_device(struct rte_pci_device *dev);
 void rte_pci_unmap_device(struct rte_pci_device *dev);
 
 /**
+ * Remap this device
+ *
+ * @param dev
+ *   A pointer to a rte_pci_device structure describing the device
+ *   to use
+ */
+int rte_pci_remap_device(struct rte_pci_device *dev);
+
+/**
  * Dump the content of the PCI bus.
  *
  * @param f
@@ -333,6 +344,20 @@ void rte_pci_ioport_read(struct rte_pci_ioport *p,
 void rte_pci_ioport_write(struct rte_pci_ioport *p,
 		const void *data, size_t len, off_t offset);
 
+/**
+ * It can be used to bind a device to a specific type of driver.
+ *
+ * @param dev_name
+ *  The device name.
+ * @param drv_type
+ *  The specific driver's type.
+ *
+ * @return
+ *  - On success, zero.
+ *  - On failure, a negative value.
+ */
+int rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type);
+
 #ifdef __cplusplus
 }
 #endif
drivers/bus/vdev/vdev.c +36 -0
index fd7736d..773f6e0 100644
@@ -323,6 +323,39 @@ vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 	return NULL;
 }
 
+static struct rte_device *
+vdev_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_vdev_device *dev;
+
+	TAILQ_FOREACH(dev, &vdev_device_list, next) {
+		if (start && &dev->device == start) {
+			start = NULL;
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+	return NULL;
+}
+
+static int
+vdev_remap_device(struct rte_device *dev)
+{
+	RTE_SET_USED(dev);
+	return 0;
+}
+
+static int
+vdev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	RTE_SET_USED(dev_name);
+	RTE_SET_USED(drv_type);
+	return 0;
+}
+
 static int
 vdev_plug(struct rte_device *dev)
 {
@@ -339,9 +372,12 @@ static struct rte_bus rte_vdev_bus = {
 	.scan = vdev_scan,
 	.probe = vdev_probe,
 	.find_device = vdev_find_device,
+	.find_device_by_name = vdev_find_device_by_name,
 	.plug = vdev_plug,
 	.unplug = vdev_unplug,
 	.parse = vdev_parse,
+	.remap_device = vdev_remap_device,
+	.bind_driver = vdev_bind_driver,
 };
 
 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
lib/librte_eal/bsdapp/eal/eal_dev.c new file +64 -0

64 lines not on this page · raw message

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +106 -0

106 lines not on this page · raw message

lib/librte_eal/common/eal_common_bus.c +30 -0
index 3e022d5..b7219c9 100644
@@ -51,8 +51,11 @@ rte_bus_register(struct rte_bus *bus)
 	RTE_VERIFY(bus->scan);
 	RTE_VERIFY(bus->probe);
 	RTE_VERIFY(bus->find_device);
+	RTE_VERIFY(bus->find_device_by_name);
 	/* Buses supporting driver plug also require unplug. */
 	RTE_VERIFY(!bus->plug || bus->unplug);
+	RTE_VERIFY(bus->remap_device);
+	RTE_VERIFY(bus->bind_driver);
 
 	TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
 	RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
@@ -170,6 +173,14 @@ cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
 }
 
 static int
+cmp_rte_device_name(const char *dev_name1, const void *_dev_name2)
+{
+	const char *dev_name2 = _dev_name2;
+
+	return strcmp(dev_name1, dev_name2);
+}
+
+static int
 bus_find_device(const struct rte_bus *bus, const void *_dev)
 {
 	struct rte_device *dev;
@@ -178,6 +189,25 @@ bus_find_device(const struct rte_bus *bus, const void *_dev)
 	return dev == NULL;
 }
 
+static struct rte_device *
+bus_find_device_by_name(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus->find_device_by_name(NULL, cmp_rte_device_name, _dev_name);
+	return dev;
+}
+
+struct rte_device *
+
+rte_bus_find_device(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus_find_device_by_name(bus, _dev_name);
+	return dev;
+}
+
 struct rte_bus *
 rte_bus_find_by_device(const struct rte_device *dev)
 {
lib/librte_eal/common/eal_common_dev.c +169 -0

181 lines not on this page · raw message

lib/librte_eal/common/include/rte_bus.h +69 -0

96 lines not on this page · raw message

lib/librte_eal/common/include/rte_dev.h +89 -0

117 lines not on this page · raw message

lib/librte_eal/linuxapp/eal/Makefile +2 -1
index 5a7b8b2..05a2437 100644
@@ -67,6 +67,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_lcore.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_timer.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_interrupts.c
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_alarm.c
+SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_dev.c
 
 # from common dir
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_lcore.c
@@ -120,7 +121,7 @@ ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y)
 CFLAGS_eal_thread.o += -Wno-return-type
 endif
 
-INC := rte_kni_common.h
+INC := rte_kni_common.h rte_dev.h
 
 SYMLINK-$(CONFIG_RTE_EXEC_ENV_LINUXAPP)-include/exec-env := \
 	$(addprefix include/exec-env/,$(INC))
lib/librte_eal/linuxapp/eal/eal_alarm.c +5 -0
index 8e4a775..29e73a7 100644
@@ -209,6 +209,7 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 	int count = 0;
 	int err = 0;
 	int executing;
+	int ret;
 
 	if (!cb_fn) {
 		rte_errno = EINVAL;
@@ -259,6 +260,10 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 			}
 			ap_prev = ap;
 		}
+
+		ret |= rte_intr_callback_unregister(&intr_handle,
+				eal_alarm_callback, NULL);
+
 		rte_spinlock_unlock(&alarm_list_lk);
 	} while (executing != 0);
 
lib/librte_eal/linuxapp/eal/eal_dev.c new file +356 -0

356 lines not on this page · raw message

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +106 -0

106 lines not on this page · raw message

lib/librte_eal/linuxapp/igb_uio/igb_uio.c +6 -0
index a3a98c1..d0e07b4 100644
@@ -354,6 +354,12 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)
 	struct rte_uio_pci_dev *udev = info->priv;
 	struct pci_dev *dev = udev->pdev;
 
+	/* check if device have been remove before release */
+	if ((&dev->dev.kobj)->state_remove_uevent_sent == 1) {
+		pr_info("The device have been removed\n");
+		return -1;
+	}
+
 	/* disable interrupts */
 	igbuio_pci_disable_interrupts(udev);
 
lib/librte_pci/rte_pci.c +20 -0
index 0160fc1..feb5fd7 100644
@@ -172,6 +172,26 @@ rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
 	return -1;
 }
 
+/* map a private resource from an address*/
+void *
+pci_map_private_resource(void *requested_addr, off_t offset, size_t size)
+{
+	void *mapaddr;
+
+	mapaddr = mmap(requested_addr, size,
+			   PROT_READ | PROT_WRITE,
+			   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+	if (mapaddr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "%s(): cannot mmap(%p, 0x%lx, 0x%lx): "
+			"%s (%p)\n",
+			__func__, requested_addr,
+			(unsigned long)size, (unsigned long)offset,
+			strerror(errno), mapaddr);
+	} else
+		RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
+
+	return mapaddr;
+}
 
 /* map a particular resource from a file */
 void *
lib/librte_pci/rte_pci.h +17 -0
index 4f2cd18..f6091a6 100644
@@ -227,6 +227,23 @@ int rte_pci_addr_cmp(const struct rte_pci_addr *addr,
 int rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr);
 
 /**
+ * @internal
+ * Map to a particular private resource.
+ *
+ * @param requested_addr
+ *      The starting address for the new mapping range.
+ * @param offset
+ *      The offset for the mapping range.
+ * @param size
+ *      The size for the mapping range.
+ * @return
+ *   - On success, the function returns a pointer to the mapped area.
+ *   - On error, the value MAP_FAILED is returned.
+ */
+void *pci_map_private_resource(void *requested_addr, off_t offset,
+		size_t size);
+
+/**
  * Map a particular resource from a file.
  *
  * @param requested_addr
Signature · 1 line
2.7.4
#309 lore raw

Re: [PATCH v7 1/2] eal: add uevent monitor for hot plug

Hi Jeff

Maybe I'm touching in previous discussions but please see some comments\questions.

From: Jeff Guo:

Quoted 812 lines

This patch aim to add a general uevent mechanism in eal device layer, to enable all linux kernel object hot plug monitoring, so user could use these APIs to monitor and read out the device status info that sent from the kernel side, then corresponding to handle it, such as detach or attach the device, and even benefit to use it to do smoothly fail safe work.

1) About uevent monitoring:
a: add one epolling to poll the netlink socket, to monitor the uevent of
the device, add device_state in struct of rte_device, to identify the device state machine.
b: add enum of rte_eal_dev_event_type and struct of rte_eal_uevent.
c: add below API in rte eal device common layer.
rte_eal_dev_monitor_enable
rte_dev_callback_register
rte_dev_callback_unregister
_rte_dev_callback_process
rte_dev_monitor_start
rte_dev_monitor_stop

2) About failure handler, use pci uio for example,
add pci_remap_device in bus layer and below function to process it:
rte_pci_remap_device
pci_uio_remap_resource
pci_map_private_resource
add rte_pci_dev_bind_driver to bind pci device with explicit driver.

---
v7->v6:
a.modify vdev part according to the vdev rework b.re-define and split the func into common and bus specific code c.fix some incorrect issue.
b.fix the system hung after send packcet issue.

---
 drivers/bus/pci/bsd/pci.c                          |  30 ++
 drivers/bus/pci/linux/pci.c                        |  87 +++++
 drivers/bus/pci/linux/pci_init.h                   |   1 +
 drivers/bus/pci/pci_common.c                       |  43 +++
 drivers/bus/pci/pci_common_uio.c                   |  28 ++
 drivers/bus/pci/private.h                          |  12 +
 drivers/bus/pci/rte_bus_pci.h                      |  25 ++
 drivers/bus/vdev/vdev.c                            |  36 +++
 lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
 .../bsdapp/eal/include/exec-env/rte_dev.h          | 106 ++++++
 lib/librte_eal/common/eal_common_bus.c             |  30 ++
 lib/librte_eal/common/eal_common_dev.c             | 169 ++++++++++
 lib/librte_eal/common/include/rte_bus.h            |  69 ++++
 lib/librte_eal/common/include/rte_dev.h            |  89 ++++++
 lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
 lib/librte_eal/linuxapp/eal/eal_alarm.c            |   5 +
 lib/librte_eal/linuxapp/eal/eal_dev.c              | 356
+++++++++++++++++++++
 .../linuxapp/eal/include/exec-env/rte_dev.h        | 106 ++++++
 lib/librte_eal/linuxapp/igb_uio/igb_uio.c          |   6 +
 lib/librte_pci/rte_pci.c                           |  20 ++
 lib/librte_pci/rte_pci.h                           |  17 +
 21 files changed, 1301 insertions(+), 1 deletion(-)
 create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h
 create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c
 create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h
drivers/bus/pci/bsd/pci.c +23 -0
index b8e2178..d58dbf6 100644
@@ -126,6 +126,29 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
 	}
 }
 
+/* re-map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_NIC_UIO:
+		ret = pci_uio_remap_resource(dev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver,
 skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
 void
 pci_uio_free_resource(struct rte_pci_device *dev,
drivers/bus/pci/bsd/pci.c +7 -0
 		struct mapped_pci_resource *uio_res)
@@ -678,3 +701,10 @@ rte_pci_ioport_unmap(struct rte_pci_ioport *p)
 
 	return ret;
 }
+
+int
+rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	return -1;
+}
+
drivers/bus/pci/linux/pci.c +32 -0
index 5da6728..792fd2c 100644
@@ -145,6 +145,38 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
 	}
 }
 
+/* Map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret = -1;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_VFIO:
+#ifdef VFIO_PRESENT
+		/* no thing to do */
+#endif
+		break;
+	case RTE_KDRV_IGB_UIO:
+	case RTE_KDRV_UIO_GENERIC:
+		if (rte_eal_using_phys_addrs()) {
+			/* map resources for devices that use uio */
+			ret = pci_uio_remap_resource(dev);
+		}
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver,
 skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
 void *
 pci_find_max_end_va(void)

{

drivers/bus/pci/linux/pci.c +55 -0
@@ -386,6 +418,8 @@ pci_scan_one(const char *dirname, const struct
 rte_pci_addr *addr)
 		rte_pci_add_device(dev);
 	}
 
+	dev->device.state = DEVICE_PARSED;
+	TAILQ_INIT(&(dev->device.uev_cbs));
 	return 0;
 }
@@ -854,3 +888,56 @@ rte_pci_ioport_unmap(struct rte_pci_ioport *p)
 
 	return ret;
 }
+
+int
+rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	char drv_bind_path[1024];
+	char drv_override_path[1024]; /* contains the /dev/uioX */
+	int drv_override_fd;
+	int drv_bind_fd;
+
+	RTE_SET_USED(drv_type);
+
+	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, drv_type, sizeof(drv_type)) < 0) {
+		RTE_LOG(ERR, EAL,
+			"Error: bind failed - Cannot write "
+			"driver %s to device %s\n", drv_type, dev_name);
+		goto err;
+	}
+
+	close(drv_override_fd);
+
+	snprintf(drv_bind_path, sizeof(drv_bind_path),
+		"/sys/bus/pci/drivers/%s/bind", drv_type);
+
+	/* do the bind by writing device to the specific driver  */
+	drv_bind_fd = open(drv_bind_path, O_WRONLY | O_APPEND);
+	if (drv_bind_fd < 0) {
+		RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
+			drv_bind_path, strerror(errno));
+		goto err;
+	}
+
+	if (write(drv_bind_fd, dev_name, sizeof(dev_name)) < 0)
+		goto err;
+
+	close(drv_bind_fd);
+	return 0;
+err:
+	close(drv_override_fd);
+	close(drv_bind_fd);
+	return -1;
+}
+
drivers/bus/pci/linux/pci_init.h +1 -0
index f342c47..5838402 100644
@@ -58,6 +58,7 @@ int pci_uio_alloc_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource **uio_res);
 void pci_uio_free_resource(struct rte_pci_device *dev,
 		struct mapped_pci_resource *uio_res);
+int pci_uio_remap_resource(struct rte_pci_device *dev);
 int pci_uio_map_resource_by_index(struct rte_pci_device *dev, int
 res_idx,
 		struct mapped_pci_resource *uio_res, int map_idx);
drivers/bus/pci/pci_common.c +40 -0
index 104fdf9..5417b32 100644
@@ -282,6 +282,7 @@ pci_probe_all_drivers(struct rte_pci_device *dev)
 		if (rc > 0)
 			/* positive value means driver doesn't support it */
 			continue;
+		dev->device.state = DEVICE_PROBED;
 		return 0;
 	}
 	return 1;
@@ -481,6 +482,7 @@ rte_pci_insert_device(struct rte_pci_device
 *exist_pci_dev,
 void
 rte_pci_remove_device(struct rte_pci_device *pci_dev)
 {
+	RTE_LOG(DEBUG, EAL, " rte_pci_remove_device for device list\n");
 	TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
 }
@@ -502,6 +504,44 @@ pci_find_device(const struct rte_device *start,
 rte_dev_cmp_t cmp,
 	return NULL;
 }
 
+static struct rte_device *
+pci_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_pci_device *dev;
+
+	FOREACH_DEVICE_ON_PCIBUS(dev) {
+		if (start && &dev->device == start) {
+			start = NULL; /* starting point found */
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+
+	return NULL;
+}
+
+static int
+pci_remap_device(struct rte_device *dev)
+{
+	struct rte_pci_device *pdev;
+	int ret;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	pdev = RTE_DEV_TO_PCI(dev);
+
+	/* remap resources for devices that use igb_uio */
+	ret = rte_pci_remap_device(pdev);
+	if (ret != 0)
+		RTE_LOG(ERR, EAL, "failed to remap device %s",
+			dev->name);
+	return ret;
+}
+
 static int
 pci_plug(struct rte_device *dev)

{

drivers/bus/pci/pci_common.c +3 -0
@@ -528,10 +568,13 @@ struct rte_pci_bus rte_pci_bus = {
 		.scan = rte_pci_scan,
 		.probe = rte_pci_probe,
 		.find_device = pci_find_device,
+		.find_device_by_name = pci_find_device_by_name,
 		.plug = pci_plug,
 		.unplug = pci_unplug,
 		.parse = pci_parse,
 		.get_iommu_class = rte_pci_get_iommu_class,
+		.remap_device = pci_remap_device,
+		.bind_driver = rte_pci_dev_bind_driver,
 	},
 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +28 -0
index 0671131..8cb4009 100644
@@ -176,6 +176,34 @@ pci_uio_unmap(struct mapped_pci_resource
 *uio_res)
 	}
 }
 
+/* remap the PCI resource of a PCI device in private virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	uint64_t phaddr;
+	void *map_address;
+
+	/* Map all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		phaddr = dev->mem_resource[i].phys_addr;
+		if (phaddr == 0)
+			continue;
+		map_address = pci_map_private_resource(
+				dev->mem_resource[i].addr, 0,
+				(size_t)dev->mem_resource[i].len);
+		if (map_address == MAP_FAILED)
+			goto error;
+		memset(map_address, 0xFF, (size_t)dev-
 >mem_resource[i].len);
+		dev->mem_resource[i].addr = map_address;
+	}
+
+	return 0;
+error:
+	return -1;
+}
+
 static struct mapped_pci_resource *

pci_uio_find_resource(struct rte_pci_device *dev) {

drivers/bus/pci/private.h +12 -0
index 2283f09..10baa1a 100644
@@ -202,6 +202,18 @@ void pci_uio_free_resource(struct rte_pci_device
 *dev,
 		struct mapped_pci_resource *uio_res);
 
 /**
+ * remap the pci uio resource..
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
  * Map device memory to uio resource
  *

* This function is private to EAL.

drivers/bus/pci/rte_bus_pci.h +25 -0
index d4a2996..1662f3b 100644
@@ -52,6 +52,8 @@ extern "C" {
 #include <sys/queue.h>
 #include <stdint.h>
 #include <inttypes.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 #include <rte_debug.h>
 #include <rte_interrupts.h>
@@ -197,6 +199,15 @@ int rte_pci_map_device(struct rte_pci_device *dev);
 void rte_pci_unmap_device(struct rte_pci_device *dev);
 
 /**
+ * Remap this device
+ *
+ * @param dev
+ *   A pointer to a rte_pci_device structure describing the device
+ *   to use
+ */
+int rte_pci_remap_device(struct rte_pci_device *dev);
+
+/**
  * Dump the content of the PCI bus.
  *
  * @param f
@@ -333,6 +344,20 @@ void rte_pci_ioport_read(struct rte_pci_ioport *p,
 void rte_pci_ioport_write(struct rte_pci_ioport *p,
 		const void *data, size_t len, off_t offset);
 
+/**
+ * It can be used to bind a device to a specific type of driver.
+ *
+ * @param dev_name
+ *  The device name.
+ * @param drv_type
+ *  The specific driver's type.
+ *
+ * @return
+ *  - On success, zero.
+ *  - On failure, a negative value.
+ */
+int rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type);
+
 #ifdef __cplusplus
 }
 #endif
drivers/bus/vdev/vdev.c +33 -0
index fd7736d..773f6e0 100644
@@ -323,6 +323,39 @@ vdev_find_device(const struct rte_device *start,
 rte_dev_cmp_t cmp,
 	return NULL;
 }
 
+static struct rte_device *
+vdev_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_vdev_device *dev;
+
+	TAILQ_FOREACH(dev, &vdev_device_list, next) {
+		if (start && &dev->device == start) {
+			start = NULL;
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+	return NULL;
+}
+
+static int
+vdev_remap_device(struct rte_device *dev)
+{
+	RTE_SET_USED(dev);
+	return 0;
+}
+
+static int
+vdev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	RTE_SET_USED(dev_name);
+	RTE_SET_USED(drv_type);
+	return 0;
+}
+
 static int
 vdev_plug(struct rte_device *dev)

{

drivers/bus/vdev/vdev.c +3 -0
@@ -339,9 +372,12 @@ static struct rte_bus rte_vdev_bus = {
 	.scan = vdev_scan,
 	.probe = vdev_probe,
 	.find_device = vdev_find_device,
+	.find_device_by_name = vdev_find_device_by_name,
 	.plug = vdev_plug,
 	.unplug = vdev_unplug,
 	.parse = vdev_parse,
+	.remap_device = vdev_remap_device,
+	.bind_driver = vdev_bind_driver,
 };
 
 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
lib/librte_eal/bsdapp/eal/eal_dev.c new file +19 -0
new file mode 100644
index 0000000..6ea9a74
@@ -0,0 +1,64 @@
+/*-
+ *   Copyright(c) 2010-2017 Intel Corporation.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/bsdapp/eal/eal_dev.c new file +34 -0
+
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <sys/queue.h>
+#include <sys/signalfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdbool.h>
+
+#include <rte_malloc.h>
+#include <rte_bus.h>
+#include <rte_dev.h>
+#include <rte_devargs.h>
+#include <rte_debug.h>
+#include <rte_log.h>
+
+#include "eal_thread.h"
+
+int
+rte_dev_monitor_start(void)
+{
+	return -1;
+}
+
+int
+rte_dev_monitor_stop(void)
+{
+	return -1;
+}
lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +21 -0
new file mode 100644
index 0000000..6a6feb5
@@ -0,0 +1,106 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +38 -0
+
+#ifndef _RTE_DEV_H_
+#error "don't include this file directly, please include generic <rte_dev.h>"
+#endif
+
+#ifndef _RTE_LINUXAPP_DEV_H_
+#define _RTE_LINUXAPP_DEV_H_
+
+#include <stdio.h>
+
+#include <rte_dev.h>
+
+#define RTE_EAL_UEV_MSG_LEN 4096
+#define RTE_EAL_UEV_MSG_ELEM_LEN 128
+
+enum uev_subsystem {
+	UEV_SUBSYSTEM_UIO,
+	UEV_SUBSYSTEM_VFIO,
+	UEV_SUBSYSTEM_PCI,
+	UEV_SUBSYSTEM_MAX
+};
+
+enum uev_monitor_netlink_group {
+	UEV_MONITOR_KERNEL,
+	UEV_MONITOR_UDEV,
+};
+
+/**
+ * The device event type.
+ */
+enum rte_eal_dev_event_type {
+	RTE_EAL_DEV_EVENT_UNKNOWN,	/**< unknown event type */
+	RTE_EAL_DEV_EVENT_ADD,		/**< device adding event */
+	RTE_EAL_DEV_EVENT_REMOVE,
+					/**< device removing event */
+	RTE_EAL_DEV_EVENT_CHANGE,
+					/**< device status change event */
+	RTE_EAL_DEV_EVENT_MOVE,		/**< device sys path move

event */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +3 -0
+	RTE_EAL_DEV_EVENT_ONLINE,	/**< device online event */
+	RTE_EAL_DEV_EVENT_OFFLINE,	/**< device offline event */
+	RTE_EAL_DEV_EVENT_MAX		/**< max value of this enum

*/

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +7 -0
+};
+
+struct rte_eal_uevent {
+	enum rte_eal_dev_event_type type;	/**< device event type */
+	int subsystem;				/**< subsystem id */
+	char *devname;				/**< device name */
+	enum uev_monitor_netlink_group group;	/**< device netlink

group */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +26 -0
+};
+
+/**
+ * Start the device uevent monitoring.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_monitor_start(void);
+
+/**
+ * Stop the device uevent monitoring .
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+
+int
+rte_dev_monitor_stop(void);
+
+#endif /* _RTE_LINUXAPP_DEV_H_ */
lib/librte_eal/common/eal_common_bus.c +11 -0
index 3e022d5..b7219c9 100644
@@ -51,8 +51,11 @@ rte_bus_register(struct rte_bus *bus)
 	RTE_VERIFY(bus->scan);
 	RTE_VERIFY(bus->probe);
 	RTE_VERIFY(bus->find_device);
+	RTE_VERIFY(bus->find_device_by_name);
 	/* Buses supporting driver plug also require unplug. */
 	RTE_VERIFY(!bus->plug || bus->unplug);
+	RTE_VERIFY(bus->remap_device);
+	RTE_VERIFY(bus->bind_driver);
 
 	TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
 	RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
@@ -170,6 +173,14 @@ cmp_rte_device(const struct rte_device *dev1,
 const void *_dev2)
 }
 
 static int
+cmp_rte_device_name(const char *dev_name1, const void *_dev_name2)
+{
+	const char *dev_name2 = _dev_name2;
+
+	return strcmp(dev_name1, dev_name2);
+}
+
+static int
 bus_find_device(const struct rte_bus *bus, const void *_dev)
 {
lib/librte_eal/common/eal_common_bus.c +19 -0
 	struct rte_device *dev;
@@ -178,6 +189,25 @@ bus_find_device(const struct rte_bus *bus, const
 void *_dev)
 	return dev == NULL;
 }
 
+static struct rte_device *
+bus_find_device_by_name(const struct rte_bus *bus, const void
 *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus->find_device_by_name(NULL, cmp_rte_device_name,
 _dev_name);
+	return dev;
+}
+
+struct rte_device *
+
+rte_bus_find_device(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus_find_device_by_name(bus, _dev_name);
+	return dev;
+}
+

struct rte_bus * rte_bus_find_by_device(const struct rte_device *dev) {

lib/librte_eal/common/eal_common_dev.c +34 -0
index dda8f58..47909e8 100644
@@ -42,9 +42,31 @@
 #include <rte_devargs.h>
 #include <rte_debug.h>
 #include <rte_log.h>
+#include <rte_spinlock.h>
+#include <rte_malloc.h>
 
 #include "eal_private.h"
 
+/* spinlock for device callbacks */
+static rte_spinlock_t rte_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
+
+/**
+ * The user application callback description.
+ *
+ * It contains callback address to be registered by user application,
+ * the pointer to the parameters for callback, and the event type.
+ */
+struct rte_eal_dev_callback {
+	TAILQ_ENTRY(rte_eal_dev_callback) next; /**< Callbacks list */
+	rte_eal_dev_cb_fn cb_fn;                /**< Callback address */
+	void *cb_arg;                           /**< Parameter for callback */
+	void *ret_param;                        /**< Return parameter */
+	enum rte_eal_dev_event_type event;      /**< device event type */
+	uint32_t active;                        /**< Callback is executing */
+};
+
+static struct rte_eal_dev_callback *dev_add_cb;
+
 static int cmp_detached_dev_name(const struct rte_device *dev,
 	const void *_name)
 {
@@ -234,3 +256,150 @@ int rte_eal_hotplug_remove(const char *busname,
 const char *devname)
 	rte_eal_devargs_remove(busname, devname);
 	return ret;
 }
+
+int
+rte_eal_dev_monitor_enable(void)
+{
+	int ret;
+
+	ret = rte_dev_monitor_start();
+	if (ret)
+		RTE_LOG(ERR, EAL, "Can not init device monitor\n");
+	return ret;
+}
+

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, · line 280

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+int
+rte_dev_callback_register(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg)
+{
+	struct rte_eal_dev_callback *user_cb;
+
+	if (!cb_fn)
+		return -EINVAL;
+

What's about checking the device pointer is not NULL ?

Quoted 14 lines
lib/librte_eal/common/eal_common_dev.c +14 -0
+	rte_spinlock_lock(&rte_dev_cb_lock);
+
+	if (TAILQ_EMPTY(&(device->uev_cbs)))
+		TAILQ_INIT(&(device->uev_cbs));
+
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		user_cb = NULL;
+	} else {
+		TAILQ_FOREACH(user_cb, &(device->uev_cbs), next) {
+			if (user_cb->cb_fn == cb_fn &&
+				user_cb->cb_arg == cb_arg &&
+				user_cb->event == event) {
+				break;
+			}

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 304

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+		}
+	}
+
+	/* create a new callback. */
+	if (user_cb == NULL) {
+		/* allocate a new interrupt callback entity */
+		user_cb = rte_zmalloc("eal device event",
+					sizeof(*user_cb), 0);
+		if (user_cb == NULL) {
+			RTE_LOG(ERR, EAL, "Can not allocate memory\n");

Missing rte_spinlock_unlock.

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 311

Quoted 7 lines
lib/librte_eal/common/eal_common_dev.c +7 -0
+			return -ENOMEM;
+		}
+		user_cb->cb_fn = cb_fn;
+		user_cb->cb_arg = cb_arg;
+		user_cb->event = event;
+		if (event == RTE_EAL_DEV_EVENT_ADD)
+			dev_add_cb = user_cb;

Only one dpdk entity can register to ADD callback?

I suggest to add option to register all devices maybe by using dummy device which will include all the "ALL_DEVICES" callbacks per event. All means past, present and future devices, by this way 1 callback can be called for all the devices and more than one dpdk entity could register to an ADD\NEW event. What's about NEW instead of ADD?

I also suggest to add the device pointer as a parameter to the callback(which will be managed by EAL).

Quoted 17 lines
lib/librte_eal/common/eal_common_dev.c +2 -0
+		else
+			TAILQ_INSERT_TAIL(&(device->uev_cbs), user_cb,

next);

lib/librte_eal/common/eal_common_dev.c +14 -0
+	}
+
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return 0;
+}
+
+int
+rte_dev_callback_unregister(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg)
+{
+	int ret;
+	struct rte_eal_dev_callback *cb, *next;
+

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 337

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+	if (!cb_fn)
+		return -EINVAL;
+
+	rte_spinlock_lock(&rte_dev_cb_lock);
+
+	ret = 0;
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		rte_free(dev_add_cb);
+		dev_add_cb = NULL;
+	} else {

Device NULL checking?

Quoted 35 lines
lib/librte_eal/common/eal_common_dev.c +16 -0
+		for (cb = TAILQ_FIRST(&(device->uev_cbs)); cb != NULL;
+		      cb = next) {
+
+			next = TAILQ_NEXT(cb, next);
+
+			if (cb->cb_fn != cb_fn || cb->event != event ||
+					(cb->cb_arg != (void *)-1 &&
+					cb->cb_arg != cb_arg))
+				continue;
+
+			/*
+			 * if this callback is not executing right now,
+			 * then remove it.
+			 */
+			if (cb->active == 0) {
+				TAILQ_REMOVE(&(device->uev_cbs), cb,

next);

lib/librte_eal/common/eal_common_dev.c +18 -0
+				rte_free(cb);
+			} else {
+				ret = -EAGAIN;
+			}
+		}
+	}
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return ret;
+}
+
+int
+_rte_dev_callback_process(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			void *cb_arg, void *ret_param)
+{
+	struct rte_eal_dev_callback dev_cb;
+	struct rte_eal_dev_callback *cb_lst;
+	int rc = 0;

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 381

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+
+	rte_spinlock_lock(&rte_dev_cb_lock);
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		if (cb_arg != NULL)
+			dev_add_cb->cb_arg = cb_arg;
+
+		if (ret_param != NULL)
+			dev_add_cb->ret_param = ret_param;
+
+		rte_spinlock_unlock(&rte_dev_cb_lock);

Can't someone free it when it running?
I suggest to keep the lock locked.
Callbacks are not allowed to use this mechanism to prevent deadlock.

Quoted 6 lines
lib/librte_eal/common/eal_common_dev.c +2 -0
+		rc = dev_add_cb->cb_fn(dev_add_cb->event,
+				dev_add_cb->cb_arg, dev_add_cb-
Quoted 1 line

ret_param);

lib/librte_eal/common/eal_common_dev.c +3 -0
+		rte_spinlock_lock(&rte_dev_cb_lock);
+	} else {
+		TAILQ_FOREACH(cb_lst, &(device->uev_cbs), next) {

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 396

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+			if (cb_lst->cb_fn == NULL || cb_lst->event != event)
+				continue;
+			dev_cb = *cb_lst;
+			cb_lst->active = 1;
+			if (cb_arg != NULL)
+				dev_cb.cb_arg = cb_arg;
+			if (ret_param != NULL)
+				dev_cb.ret_param = ret_param;
+
+			rte_spinlock_unlock(&rte_dev_cb_lock);

The current active flag doesn't do it thread safe here, I suggest to keep the lock locked. Scenario:
1. Thread A see active = 0 in unregister function.
2. Context switch.
3. Thread B start the callback.
4. Context switch.
5. Thread A free it.
6. Context switch.
7. Seg fault in Thread B.

Quoted 554 lines
lib/librte_eal/common/eal_common_dev.c +9 -0
+			rc = dev_cb.cb_fn(dev_cb.event,
+					dev_cb.cb_arg, dev_cb.ret_param);
+			rte_spinlock_lock(&rte_dev_cb_lock);
+			cb_lst->active = 0;
+		}
+	}
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return rc;
+}
lib/librte_eal/common/include/rte_bus.h +7 -0
index 6fb0834..6c4ae31 100644
@@ -122,6 +122,34 @@ typedef struct rte_device *
 			 const void *data);
 
 /**
+ * Device iterator to find a device on a bus.
+ *
+ * This function returns an rte_device if one of those held by the bus
+ * matches the data passed as parameter.
+ *
+ * If the comparison function returns zero this function should stop iterating
+ * over any more devices. To continue a search the device of a previous

search
+ * can be passed via the start parameter.
+ *
+ * @param cmp
+ * the device name comparison function.
+ *
+ * @param data
+ * Data to compare each device against.
+ *
+ * @param start
+ * starting point for the iteration
+ *
+ * @return
+ * The first device matching the data, NULL if none exists.
+ */

lib/librte_eal/common/include/rte_bus.h +8 -0
+typedef struct rte_device *
+(*rte_bus_find_device_by_name_t)(const struct rte_device *start,
+			 rte_dev_cmp_name_t cmp,
+			 const void *data);
+
+
+/**
  * Implementation specific probe function which is responsible for linking
  * devices on that bus with applicable drivers.
  *
@@ -168,6 +196,37 @@ typedef int (*rte_bus_unplug_t)(struct rte_device
 *dev);
 typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 
 /**
+ * Implementation specific remap function which is responsible for

remmaping
+ * devices on that bus from original share memory resource to a private memory
+ * resource for the sake of device has been removal.
+ *
+ * @param dev
+ * Device pointer that was returned by a previous call to find_device.
+ *
+ * @return
+ * 0 on success.
+ * !0 on error.
+ */

lib/librte_eal/common/include/rte_bus.h +24 -0
+typedef int (*rte_bus_remap_device_t)(struct rte_device *dev);
+
+/**
+ * Implementation specific bind driver function which is responsible for bind
+ * a explicit type of driver with a devices on that bus.
+ *
+ * @param dev_name
+ *	device textual description.
+ *
+ * @param drv_type
+ *	driver type textual description.
+ *
+ * @return
+ *	0 on success.
+ *	!0 on error.
+ */
+typedef int (*rte_bus_bind_driver_t)(const char *dev_name,
+				const char *drv_type);
+
+/**
  * Bus scan policies
  */
 enum rte_bus_scan_mode {
@@ -206,9 +265,13 @@ struct rte_bus {
 	rte_bus_scan_t scan;         /**< Scan for devices attached to bus */
 	rte_bus_probe_t probe;       /**< Probe devices on bus */
 	rte_bus_find_device_t find_device; /**< Find a device on the bus */
+	rte_bus_find_device_by_name_t find_device_by_name;
+				     /**< Find a device on the bus */
 	rte_bus_plug_t plug;         /**< Probe single device for drivers */
 	rte_bus_unplug_t unplug;     /**< Remove single device from driver
 */
 	rte_bus_parse_t parse;       /**< Parse a device name */
+	rte_bus_remap_device_t remap_device;       /**< remap a device */
+	rte_bus_bind_driver_t bind_driver; /**< bind a driver for bus device
 */
 	struct rte_bus_conf conf;    /**< Bus configuration */

rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu
class */
};

lib/librte_eal/common/include/rte_bus.h +6 -0
@@ -306,6 +369,12 @@ struct rte_bus *rte_bus_find(const struct rte_bus
 *start, rte_bus_cmp_t cmp,
 struct rte_bus *rte_bus_find_by_device(const struct rte_device *dev);
 
 /**
+ * Find the registered bus for a particular device.
+ */
+struct rte_device *rte_bus_find_device(const struct rte_bus *bus,
+				const void *dev_name);
+
+/**
  * Find the registered bus for a given name.
  */

struct rte_bus *rte_bus_find_by_name(const char *busname);

lib/librte_eal/common/include/rte_dev.h +21 -0
index 9342e0c..19971d0 100644
@@ -51,6 +51,15 @@ extern "C" {
 
 #include <rte_log.h>
 
+#include <exec-env/rte_dev.h>
+
+typedef int (*rte_eal_dev_cb_fn)(enum rte_eal_dev_event_type event,
+					void *cb_arg, void *ret_param);
+
+struct rte_eal_dev_callback;
+/** @internal Structure to keep track of registered callbacks */
+TAILQ_HEAD(rte_eal_dev_cb_list, rte_eal_dev_callback);
+
 __attribute__((format(printf, 2, 0)))
 static inline void
 rte_pmd_debug_trace(const char *func_name, const char *fmt, ...)
@@ -157,6 +166,13 @@ struct rte_driver {
  */
 #define RTE_DEV_NAME_MAX_LEN 64
 
+enum device_state {
+	DEVICE_UNDEFINED,
+	DEVICE_FAULT,
+	DEVICE_PARSED,
+	DEVICE_PROBED,
+};
+
 /**
  * A structure describing a generic device.
  */
@@ -166,6 +182,9 @@ struct rte_device {
 	const struct rte_driver *driver;/**< Associated driver */
 	int numa_node;                /**< NUMA node connection */
 	struct rte_devargs *devargs;  /**< Device user arguments */
+	enum device_state state;  /**< Device state */
+	/** User application callbacks for device event */
+	struct rte_eal_dev_cb_list uev_cbs;
 };
 
 /**
@@ -248,6 +267,8 @@ int rte_eal_hotplug_remove(const char *busname,
 const char *devname);
  */
 typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void
 *data);
 
+typedef int (*rte_dev_cmp_name_t)(const char *dev_name, const void
 *data);
+

#define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]

#define RTE_PMD_EXPORT_NAME(name, idx) \

lib/librte_eal/common/include/rte_dev.h +68 -0
@@ -293,4 +314,72 @@ __attribute__((used)) = str
 }
 #endif
 
+/**
+ * It enable the device event monitoring for a specific event.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_eal_dev_monitor_enable(void);
+/**
+ * It registers the callback for the specific event. Multiple
+ * callbacks cal be registered at the same time.
+ * @param event
+ *  The device event type.
+ * @param cb_fn
+ *  callback address.
+ * @param cb_arg
+ *  address of parameter for callback.
+ *
+ * @return
+ *  - On success, zero.
+ *  - On failure, a negative value.
+ */
+int rte_dev_callback_register(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg);
+
+/**
+ * It unregisters the callback according to the specified event.
+ *
+ * @param event
+ *  The event type which corresponding to the callback.
+ * @param cb_fn
+ *  callback address.
+ *  address of parameter for callback, (void *)-1 means to remove all
+ *  registered which has the same callback address.
+ *
+ * @return
+ *  - On success, return the number of callback entities removed.
+ *  - On failure, a negative value.
+ */
+int rte_dev_callback_unregister(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg);
+
+/**
+ * @internal Executes all the user application registered callbacks for
+ * the specific device. It is for DPDK internal user only. User
+ * application should not call it directly.
+ *
+ * @param event
+ *  The device event type.
+ * @param cb_arg
+ *  callback parameter.
+ * @param ret_param
+ *  To pass data back to user application.
+ *  This allows the user application to decide if a particular function
+ *  is permitted or not.
+ *
+ * @return
+ *  - On success, return zero.
+ *  - On failure, a negative value.
+ */
+int
+_rte_dev_callback_process(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			void *cb_arg, void *ret_param);
 #endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/Makefile +0 -0
index 5a7b8b2..05a2437 100644
@@ -67,6 +67,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) +=

eal_lcore.c
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_interrupts.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_alarm.c

lib/librte_eal/linuxapp/eal/Makefile +2 -1
+SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_dev.c
 
 # from common dir
 SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_lcore.c
@@ -120,7 +121,7 @@ ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y)
 CFLAGS_eal_thread.o += -Wno-return-type
 endif
 
-INC := rte_kni_common.h
+INC := rte_kni_common.h rte_dev.h
 
 SYMLINK-$(CONFIG_RTE_EXEC_ENV_LINUXAPP)-include/exec-env := \
 	$(addprefix include/exec-env/,$(INC))
lib/librte_eal/linuxapp/eal/eal_alarm.c +1 -0
index 8e4a775..29e73a7 100644
@@ -209,6 +209,7 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn,
 void *cb_arg)
 	int count = 0;
 	int err = 0;
 	int executing;
+	int ret;
 
 	if (!cb_fn) {
lib/librte_eal/linuxapp/eal/eal_alarm.c +4 -0
 		rte_errno = EINVAL;
@@ -259,6 +260,10 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn,
 void *cb_arg)
 			}
 			ap_prev = ap;
 		}
+
+		ret |= rte_intr_callback_unregister(&intr_handle,
+				eal_alarm_callback, NULL);
+
 		rte_spinlock_unlock(&alarm_list_lk);
 	} while (executing != 0);
lib/librte_eal/linuxapp/eal/eal_dev.c new file +19 -0
new file mode 100644
index 0000000..49fd0dc
@@ -0,0 +1,356 @@
+/*-
+ *   Copyright(c) 2010-2017 Intel Corporation.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/linuxapp/eal/eal_dev.c new file +71 -0
+
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <sys/queue.h>
+#include <sys/signalfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdbool.h>
+
+#include <rte_malloc.h>
+#include <rte_bus.h>
+#include <rte_dev.h>
+#include <rte_devargs.h>
+#include <rte_debug.h>
+#include <rte_log.h>
+
+#include "eal_thread.h"
+
+/* uev monitoring thread */
+static pthread_t uev_monitor_thread;
+
+bool udev_exit = true;
+
+bool no_request_thread = true;
+
+static void sig_handler(int signum)
+{
+	if (signum == SIGINT || signum == SIGTERM)
+		rte_dev_monitor_stop();
+}
+
+static int
+dev_monitor_fd_new(void)
+{
+
+	int uevent_fd;
+
+	uevent_fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC |
+			SOCK_NONBLOCK,
+			NETLINK_KOBJECT_UEVENT);
+	if (uevent_fd < 0) {
+		RTE_LOG(ERR, EAL, "create uevent fd failed\n");
+		return -1;
+	}
+	return uevent_fd;
+}
+
+static int
+dev_monitor_enable(int netlink_fd)
+{
+	struct sockaddr_nl addr;
+	int ret;
+	int size = 64 * 1024;
+	int nonblock = 1;
+
+	memset(&addr, 0, sizeof(addr));
+	addr.nl_family = AF_NETLINK;
+	addr.nl_pid = 0;
+	addr.nl_groups = 0xffffffff;
+
+	if (bind(netlink_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		RTE_LOG(ERR, EAL, "bind failed\n");
+		goto err;
+	}
+
+	setsockopt(netlink_fd, SOL_SOCKET, SO_PASSCRED, &size,

sizeof(size));

lib/librte_eal/linuxapp/eal/eal_dev.c new file +53 -0
+
+	ret = ioctl(netlink_fd, FIONBIO, &nonblock);
+	if (ret != 0) {
+		RTE_LOG(ERR, EAL, "ioctl(FIONBIO) failed\n");
+		goto err;
+	}
+	return 0;
+err:
+	close(netlink_fd);
+	return -1;
+}
+
+static void
+dev_uev_parse(const char *buf, struct rte_eal_uevent *event)
+{
+	char action[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char subsystem[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char dev_path[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char pci_slot_name[RTE_EAL_UEV_MSG_ELEM_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(subsystem, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(dev_path, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(pci_slot_name, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+
+	while (i < RTE_EAL_UEV_MSG_LEN) {
+		for (; i < RTE_EAL_UEV_MSG_LEN; i++) {
+			if (*buf)
+				break;
+			buf++;
+		}
+		if (!strncmp(buf, "libudev", 7)) {
+			buf += 7;
+			i += 7;
+			event->group = UEV_MONITOR_UDEV;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		} else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
+			buf += 14;
+			i += 14;
+			snprintf(pci_slot_name, sizeof(subsystem), "%s",

buf);

lib/librte_eal/linuxapp/eal/eal_dev.c new file +67 -0
+			event->devname = pci_slot_name;
+		}
+		for (; i < RTE_EAL_UEV_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;
+		}
+	}
+
+	if (!strncmp(subsystem, "pci", 3))
+		event->subsystem = UEV_SUBSYSTEM_PCI;
+	if (!strncmp(action, "add", 3))
+		event->type = RTE_EAL_DEV_EVENT_ADD;
+	if (!strncmp(action, "remove", 6))
+		event->type = RTE_EAL_DEV_EVENT_REMOVE;
+	event->devname = pci_slot_name;
+}
+
+static int
+dev_uev_receive(int fd, struct rte_eal_uevent *uevent)
+{
+	int ret;
+	char buf[RTE_EAL_UEV_MSG_LEN];
+
+	memset(uevent, 0, sizeof(struct rte_eal_uevent));
+	memset(buf, 0, RTE_EAL_UEV_MSG_LEN);
+
+	ret = recv(fd, buf, RTE_EAL_UEV_MSG_LEN - 1, MSG_DONTWAIT);
+	if (ret < 0) {
+		RTE_LOG(ERR, EAL,
+		"Socket read error(%d): %s\n",
+		errno, strerror(errno));
+		return -1;
+	} else if (ret == 0)
+		/* connection closed */
+		return -1;
+
+	dev_uev_parse(buf, uevent);
+
+	return 0;
+}
+
+static int
+dev_uev_process(struct epoll_event *events, int nfds)
+{
+	struct rte_bus *bus;
+	struct rte_device *dev;
+	struct rte_eal_uevent uevent;
+	int ret;
+	int i;
+
+	for (i = 0; i < nfds; i++) {
+		/**
+		 * check device uevent from kernel side, no need to check
+		 * uevent from udev.
+		 */
+		if ((dev_uev_receive(events[i].data.fd, &uevent)) ||
+			(uevent.group == UEV_MONITOR_UDEV))
+			return 0;
+
+		/* default handle all pci devcie when is being hot plug */
+		if (uevent.subsystem == UEV_SUBSYSTEM_PCI) {
+			bus = rte_bus_find_by_name("pci");
+			dev = rte_bus_find_device(bus, uevent.devname);
+			if (uevent.type == RTE_EAL_DEV_EVENT_REMOVE) {
+
+				if ((!dev) || dev->state ==

DEVICE_UNDEFINED)

lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+					return 0;
+				dev->state = DEVICE_FAULT;
+
+				/**
+				 * remap the resource to be fake
+				 * before user's removal processing
+				 */
+				ret = bus->remap_device(dev);
+				if (!ret)
+

return(_rte_dev_callback_process(dev,

lib/librte_eal/linuxapp/eal/eal_dev.c line 233

Quoted 2 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +2 -0
+					  RTE_EAL_DEV_EVENT_REMOVE,
+					  NULL, NULL));

What is the reason to keep this device in EAL device list after the removal?
I suggest to remove it (driver remove, bus remove and EAL remove) after the callbacks running. By this way EAL can initiate all device removals.

lib/librte_eal/linuxapp/eal/eal_dev.c line 252

Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +1 -0
+			} else if (uevent.type == RTE_EAL_DEV_EVENT_ADD)

{

lib/librte_eal/linuxapp/eal/eal_dev.c new file +9 -0
+				if (dev == NULL) {
+					/**
+					 * bind the driver to the device
+					 * before user's add processing
+					 */
+					bus->bind_driver(
+						uevent.devname,
+						"igb_uio");
+

Similar comments here:
EAL can initiate all device probe operations by adding the device and probing it here before the callback running. Then, also the device pointer can be passed to the callbacks.

Quoted 86 lines

return(_rte_dev_callback_process(NULL,

lib/librte_eal/linuxapp/eal/eal_dev.c new file +80 -0
+					  RTE_EAL_DEV_EVENT_ADD,
+					  uevent.devname, NULL));
+				}
+			}
+		}
+	}
+	return 0;
+}
+
+/**
+ * It builds/rebuilds up the epoll file descriptor with all the
+ * file descriptors being waited on. Then handles the interrupts.
+ *
+ * @param arg
+ *  pointer. (unused)
+ *
+ * @return
+ *  never return;
+ */
+static __attribute__((noreturn)) void *
+dev_uev_monitoring(__rte_unused void *arg)
+{
+	struct sigaction act;
+	sigset_t mask;
+	int netlink_fd;
+	struct epoll_event ep_kernel;
+	int fd_ep;
+
+	udev_exit = false;
+
+	/* set signal handlers */
+	memset(&act, 0x00, sizeof(struct sigaction));
+	act.sa_handler = sig_handler;
+	sigemptyset(&act.sa_mask);
+	act.sa_flags = SA_RESTART;
+	sigaction(SIGINT, &act, NULL);
+	sigaction(SIGTERM, &act, NULL);
+	sigemptyset(&mask);
+	sigaddset(&mask, SIGINT);
+	sigaddset(&mask, SIGTERM);
+	sigprocmask(SIG_UNBLOCK, &mask, NULL);
+
+	fd_ep = epoll_create1(EPOLL_CLOEXEC);
+	if (fd_ep < 0) {
+		RTE_LOG(ERR, EAL, "error creating epoll fd: %m\n");
+		goto out;
+	}
+
+	netlink_fd = dev_monitor_fd_new();
+
+	if (dev_monitor_enable(netlink_fd) < 0) {
+		RTE_LOG(ERR, EAL, "error subscribing to kernel events\n");
+		goto out;
+	}
+
+	memset(&ep_kernel, 0, sizeof(struct epoll_event));
+	ep_kernel.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
+	ep_kernel.data.fd = netlink_fd;
+	if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, netlink_fd,
+		&ep_kernel) < 0) {
+		RTE_LOG(ERR, EAL, "error addding fd to epoll: %m\n");
+		goto out;
+	}
+
+	while (!udev_exit) {
+		int fdcount;
+		struct epoll_event ev[1];
+
+		fdcount = epoll_wait(fd_ep, ev, 1, -1);
+		if (fdcount < 0) {
+			if (errno != EINTR)
+				RTE_LOG(ERR, EAL, "error receiving uevent "
+					"message: %m\n");
+				continue;
+			}
+
+		/* epoll_wait has at least one fd ready to read */
+		if (dev_uev_process(ev, fdcount) < 0) {
+			if (errno != EINTR)
+				RTE_LOG(ERR, EAL, "error processing uevent

"

lib/librte_eal/linuxapp/eal/eal_dev.c new file +4 -0
+					"message: %m\n");
+		}
+	}
+out:

lib/librte_eal/linuxapp/eal/eal_dev.c line 337

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	if (fd_ep >= 0)
+		close(fd_ep);
+	if (netlink_fd >= 0)
+		close(netlink_fd);
+	rte_panic("uev monitoring fail\n");
+}
+
+int
+rte_dev_monitor_start(void)
+{

Maybe add option to run it also by new EAL command line parameter?

lib/librte_eal/linuxapp/eal/eal_dev.c line 346

Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +9 -0
+	int ret;
+
+	if (!no_request_thread)
+		return 0;
+	no_request_thread = false;
+
+	/* create the host thread to wait/handle the uevent from kernel */
+	ret = pthread_create(&uev_monitor_thread, NULL,
+		dev_uev_monitoring, NULL);

What is the reason to open new thread for hotplug?
Why not to use the current dpdk host thread by the alarm mechanism?

Quoted 221 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	return ret;
+}
+
+int
+rte_dev_monitor_stop(void)
+{
+	udev_exit = true;
+	no_request_thread = true;
+	return 0;
+}
lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +21 -0
new file mode 100644
index 0000000..6a6feb5
@@ -0,0 +1,106 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +38 -0
+
+#ifndef _RTE_DEV_H_
+#error "don't include this file directly, please include generic <rte_dev.h>"
+#endif
+
+#ifndef _RTE_LINUXAPP_DEV_H_
+#define _RTE_LINUXAPP_DEV_H_
+
+#include <stdio.h>
+
+#include <rte_dev.h>
+
+#define RTE_EAL_UEV_MSG_LEN 4096
+#define RTE_EAL_UEV_MSG_ELEM_LEN 128
+
+enum uev_subsystem {
+	UEV_SUBSYSTEM_UIO,
+	UEV_SUBSYSTEM_VFIO,
+	UEV_SUBSYSTEM_PCI,
+	UEV_SUBSYSTEM_MAX
+};
+
+enum uev_monitor_netlink_group {
+	UEV_MONITOR_KERNEL,
+	UEV_MONITOR_UDEV,
+};
+
+/**
+ * The device event type.
+ */
+enum rte_eal_dev_event_type {
+	RTE_EAL_DEV_EVENT_UNKNOWN,	/**< unknown event type */
+	RTE_EAL_DEV_EVENT_ADD,		/**< device adding event */
+	RTE_EAL_DEV_EVENT_REMOVE,
+					/**< device removing event */
+	RTE_EAL_DEV_EVENT_CHANGE,
+					/**< device status change event */
+	RTE_EAL_DEV_EVENT_MOVE,		/**< device sys path move

event */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +3 -0
+	RTE_EAL_DEV_EVENT_ONLINE,	/**< device online event */
+	RTE_EAL_DEV_EVENT_OFFLINE,	/**< device offline event */
+	RTE_EAL_DEV_EVENT_MAX		/**< max value of this enum

*/

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +7 -0
+};
+
+struct rte_eal_uevent {
+	enum rte_eal_dev_event_type type;	/**< device event type */
+	int subsystem;				/**< subsystem id */
+	char *devname;				/**< device name */
+	enum uev_monitor_netlink_group group;	/**< device netlink

group */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +26 -0
+};
+
+/**
+ * Start the device uevent monitoring.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_monitor_start(void);
+
+/**
+ * Stop the device uevent monitoring .
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+
+int
+rte_dev_monitor_stop(void);
+
+#endif /* _RTE_LINUXAPP_DEV_H_ */
lib/librte_eal/linuxapp/igb_uio/igb_uio.c +6 -0
index a3a98c1..d0e07b4 100644
@@ -354,6 +354,12 @@ igbuio_pci_release(struct uio_info *info, struct
 inode *inode)
 	struct rte_uio_pci_dev *udev = info->priv;
 	struct pci_dev *dev = udev->pdev;
 
+	/* check if device have been remove before release */
+	if ((&dev->dev.kobj)->state_remove_uevent_sent == 1) {
+		pr_info("The device have been removed\n");
+		return -1;
+	}
+
 	/* disable interrupts */
 	igbuio_pci_disable_interrupts(udev);
lib/librte_pci/rte_pci.c +20 -1
index 0160fc1..feb5fd7 100644
@@ -172,6 +172,26 @@ rte_pci_addr_parse(const char *str, struct
 rte_pci_addr *addr)
 	return -1;
 }
 
+/* map a private resource from an address*/
+void *
+pci_map_private_resource(void *requested_addr, off_t offset, size_t size)
+{
+	void *mapaddr;
+
+	mapaddr = mmap(requested_addr, size,
+			   PROT_READ | PROT_WRITE,
+			   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
-1, 0);
+	if (mapaddr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "%s(): cannot mmap(%p, 0x%lx, 0x%lx): "
+			"%s (%p)\n",
+			__func__, requested_addr,
+			(unsigned long)size, (unsigned long)offset,
+			strerror(errno), mapaddr);
+	} else
+		RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n",
 mapaddr);
+
+	return mapaddr;
+}
 
 /* map a particular resource from a file */
 void *
lib/librte_pci/rte_pci.h +17 -0
index 4f2cd18..f6091a6 100644
@@ -227,6 +227,23 @@ int rte_pci_addr_cmp(const struct rte_pci_addr
 *addr,
 int rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr);
 
 /**
+ * @internal
+ * Map to a particular private resource.
+ *
+ * @param requested_addr
+ *      The starting address for the new mapping range.
+ * @param offset
+ *      The offset for the mapping range.
+ * @param size
+ *      The size for the mapping range.
+ * @return
+ *   - On success, the function returns a pointer to the mapped area.
+ *   - On error, the value MAP_FAILED is returned.
+ */
+void *pci_map_private_resource(void *requested_addr, off_t offset,
+		size_t size);
+
+/**
  * Map a particular resource from a file.
  *

* @param requested_addr
--
2.7.4

author
#310 lore raw

Re: [PATCH v7 1/2] eal: add uevent monitor for hot plug

thanks , matan

Quoted 820 lines · Matan Azrad

Hi Jeff

Maybe I'm touching in previous discussions but please see some comments\questions.

From: Jeff Guo:

Quoted 815 lines

This patch aim to add a general uevent mechanism in eal device layer, to enable all linux kernel object hot plug monitoring, so user could use these APIs to monitor and read out the device status info that sent from the kernel side, then corresponding to handle it, such as detach or attach the device, and even benefit to use it to do smoothly fail safe work.

1) About uevent monitoring:
a: add one epolling to poll the netlink socket, to monitor the uevent of
the device, add device_state in struct of rte_device, to identify the device state machine.
b: add enum of rte_eal_dev_event_type and struct of rte_eal_uevent.
c: add below API in rte eal device common layer.
rte_eal_dev_monitor_enable
rte_dev_callback_register
rte_dev_callback_unregister
_rte_dev_callback_process
rte_dev_monitor_start
rte_dev_monitor_stop

2) About failure handler, use pci uio for example,
add pci_remap_device in bus layer and below function to process it:
rte_pci_remap_device
pci_uio_remap_resource
pci_map_private_resource
add rte_pci_dev_bind_driver to bind pci device with explicit driver.

---
v7->v6:
a.modify vdev part according to the vdev rework b.re-define and split the func into common and bus specific code c.fix some incorrect issue.
b.fix the system hung after send packcet issue.

---
  drivers/bus/pci/bsd/pci.c                          |  30 ++
  drivers/bus/pci/linux/pci.c                        |  87 +++++
  drivers/bus/pci/linux/pci_init.h                   |   1 +
  drivers/bus/pci/pci_common.c                       |  43 +++
  drivers/bus/pci/pci_common_uio.c                   |  28 ++
  drivers/bus/pci/private.h                          |  12 +
  drivers/bus/pci/rte_bus_pci.h                      |  25 ++
  drivers/bus/vdev/vdev.c                            |  36 +++
  lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
  .../bsdapp/eal/include/exec-env/rte_dev.h          | 106 ++++++
  lib/librte_eal/common/eal_common_bus.c             |  30 ++
  lib/librte_eal/common/eal_common_dev.c             | 169 ++++++++++
  lib/librte_eal/common/include/rte_bus.h            |  69 ++++
  lib/librte_eal/common/include/rte_dev.h            |  89 ++++++
  lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
  lib/librte_eal/linuxapp/eal/eal_alarm.c            |   5 +
  lib/librte_eal/linuxapp/eal/eal_dev.c              | 356
+++++++++++++++++++++
  .../linuxapp/eal/include/exec-env/rte_dev.h        | 106 ++++++
  lib/librte_eal/linuxapp/igb_uio/igb_uio.c          |   6 +
  lib/librte_pci/rte_pci.c                           |  20 ++
  lib/librte_pci/rte_pci.h                           |  17 +
  21 files changed, 1301 insertions(+), 1 deletion(-)

create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h

drivers/bus/pci/bsd/pci.c +23 -0
index b8e2178..d58dbf6 100644
@@ -126,6 +126,29 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
  	}
  }
 
+/* re-map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_NIC_UIO:
+		ret = pci_uio_remap_resource(dev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver,
 skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
  void
  pci_uio_free_resource(struct rte_pci_device *dev,

struct mapped_pci_resource *uio_res)

drivers/bus/pci/bsd/pci.c +7 -0
@@ -678,3 +701,10 @@ rte_pci_ioport_unmap(struct rte_pci_ioport *p)
 
  	return ret;
  }
+
+int
+rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	return -1;
+}
+
drivers/bus/pci/linux/pci.c +32 -0
index 5da6728..792fd2c 100644
@@ -145,6 +145,38 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
  	}
  }
 
+/* Map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret = -1;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_VFIO:
+#ifdef VFIO_PRESENT
+		/* no thing to do */
+#endif
+		break;
+	case RTE_KDRV_IGB_UIO:
+	case RTE_KDRV_UIO_GENERIC:
+		if (rte_eal_using_phys_addrs()) {
+			/* map resources for devices that use uio */
+			ret = pci_uio_remap_resource(dev);
+		}
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver,
 skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
  void *
  pci_find_max_end_va(void)
  {
drivers/bus/pci/linux/pci.c +55 -0
@@ -386,6 +418,8 @@ pci_scan_one(const char *dirname, const struct
 rte_pci_addr *addr)
  		rte_pci_add_device(dev);
  	}
 
+	dev->device.state = DEVICE_PARSED;
+	TAILQ_INIT(&(dev->device.uev_cbs));
  	return 0;
  }
@@ -854,3 +888,56 @@ rte_pci_ioport_unmap(struct rte_pci_ioport *p)
 
  	return ret;
  }
+
+int
+rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	char drv_bind_path[1024];
+	char drv_override_path[1024]; /* contains the /dev/uioX */
+	int drv_override_fd;
+	int drv_bind_fd;
+
+	RTE_SET_USED(drv_type);
+
+	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, drv_type, sizeof(drv_type)) < 0) {
+		RTE_LOG(ERR, EAL,
+			"Error: bind failed - Cannot write "
+			"driver %s to device %s\n", drv_type, dev_name);
+		goto err;
+	}
+
+	close(drv_override_fd);
+
+	snprintf(drv_bind_path, sizeof(drv_bind_path),
+		"/sys/bus/pci/drivers/%s/bind", drv_type);
+
+	/* do the bind by writing device to the specific driver  */
+	drv_bind_fd = open(drv_bind_path, O_WRONLY | O_APPEND);
+	if (drv_bind_fd < 0) {
+		RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
+			drv_bind_path, strerror(errno));
+		goto err;
+	}
+
+	if (write(drv_bind_fd, dev_name, sizeof(dev_name)) < 0)
+		goto err;
+
+	close(drv_bind_fd);
+	return 0;
+err:
+	close(drv_override_fd);
+	close(drv_bind_fd);
+	return -1;
+}
+
drivers/bus/pci/linux/pci_init.h +1 -0
index f342c47..5838402 100644
@@ -58,6 +58,7 @@ int pci_uio_alloc_resource(struct rte_pci_device *dev,
  		struct mapped_pci_resource **uio_res);
  void pci_uio_free_resource(struct rte_pci_device *dev,
  		struct mapped_pci_resource *uio_res);
+int pci_uio_remap_resource(struct rte_pci_device *dev);
  int pci_uio_map_resource_by_index(struct rte_pci_device *dev, int
 res_idx,
  		struct mapped_pci_resource *uio_res, int map_idx);
drivers/bus/pci/pci_common.c +40 -0
index 104fdf9..5417b32 100644
@@ -282,6 +282,7 @@ pci_probe_all_drivers(struct rte_pci_device *dev)
  		if (rc > 0)
  			/* positive value means driver doesn't support it */
  			continue;
+		dev->device.state = DEVICE_PROBED;
  		return 0;
  	}
  	return 1;
@@ -481,6 +482,7 @@ rte_pci_insert_device(struct rte_pci_device
 *exist_pci_dev,
  void
  rte_pci_remove_device(struct rte_pci_device *pci_dev)
  {
+	RTE_LOG(DEBUG, EAL, " rte_pci_remove_device for device list\n");
  	TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
  }
@@ -502,6 +504,44 @@ pci_find_device(const struct rte_device *start,
 rte_dev_cmp_t cmp,
  	return NULL;
  }
 
+static struct rte_device *
+pci_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_pci_device *dev;
+
+	FOREACH_DEVICE_ON_PCIBUS(dev) {
+		if (start && &dev->device == start) {
+			start = NULL; /* starting point found */
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+
+	return NULL;
+}
+
+static int
+pci_remap_device(struct rte_device *dev)
+{
+	struct rte_pci_device *pdev;
+	int ret;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	pdev = RTE_DEV_TO_PCI(dev);
+
+	/* remap resources for devices that use igb_uio */
+	ret = rte_pci_remap_device(pdev);
+	if (ret != 0)
+		RTE_LOG(ERR, EAL, "failed to remap device %s",
+			dev->name);
+	return ret;
+}
+
  static int
  pci_plug(struct rte_device *dev)
  {
drivers/bus/pci/pci_common.c +3 -0
@@ -528,10 +568,13 @@ struct rte_pci_bus rte_pci_bus = {
  		.scan = rte_pci_scan,
  		.probe = rte_pci_probe,
  		.find_device = pci_find_device,
+		.find_device_by_name = pci_find_device_by_name,
  		.plug = pci_plug,
  		.unplug = pci_unplug,
  		.parse = pci_parse,
  		.get_iommu_class = rte_pci_get_iommu_class,
+		.remap_device = pci_remap_device,
+		.bind_driver = rte_pci_dev_bind_driver,
  	},
  	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
  	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +28 -0
index 0671131..8cb4009 100644
@@ -176,6 +176,34 @@ pci_uio_unmap(struct mapped_pci_resource
 *uio_res)
  	}
  }
 
+/* remap the PCI resource of a PCI device in private virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	uint64_t phaddr;
+	void *map_address;
+
+	/* Map all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		phaddr = dev->mem_resource[i].phys_addr;
+		if (phaddr == 0)
+			continue;
+		map_address = pci_map_private_resource(
+				dev->mem_resource[i].addr, 0,
+				(size_t)dev->mem_resource[i].len);
+		if (map_address == MAP_FAILED)
+			goto error;
+		memset(map_address, 0xFF, (size_t)dev-
 > mem_resource[i].len);
+		dev->mem_resource[i].addr = map_address;
+	}
+
+	return 0;
+error:
+	return -1;
+}
+
  static struct mapped_pci_resource *
  pci_uio_find_resource(struct rte_pci_device *dev)
  {
drivers/bus/pci/private.h +12 -0
index 2283f09..10baa1a 100644
@@ -202,6 +202,18 @@ void pci_uio_free_resource(struct rte_pci_device
 *dev,
  		struct mapped_pci_resource *uio_res);
 
  /**
+ * remap the pci uio resource..
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
   * Map device memory to uio resource
   *

* This function is private to EAL.

drivers/bus/pci/rte_bus_pci.h +25 -0
index d4a2996..1662f3b 100644
@@ -52,6 +52,8 @@ extern "C" {
  #include <sys/queue.h>
  #include <stdint.h>
  #include <inttypes.h>
+#include <unistd.h>
+#include <fcntl.h>
 
  #include <rte_debug.h>
  #include <rte_interrupts.h>
@@ -197,6 +199,15 @@ int rte_pci_map_device(struct rte_pci_device *dev);
  void rte_pci_unmap_device(struct rte_pci_device *dev);
 
  /**
+ * Remap this device
+ *
+ * @param dev
+ *   A pointer to a rte_pci_device structure describing the device
+ *   to use
+ */
+int rte_pci_remap_device(struct rte_pci_device *dev);
+
+/**
   * Dump the content of the PCI bus.
   *
   * @param f
@@ -333,6 +344,20 @@ void rte_pci_ioport_read(struct rte_pci_ioport *p,
  void rte_pci_ioport_write(struct rte_pci_ioport *p,
  		const void *data, size_t len, off_t offset);
 
+/**
+ * It can be used to bind a device to a specific type of driver.
+ *
+ * @param dev_name
+ *  The device name.
+ * @param drv_type
+ *  The specific driver's type.
+ *
+ * @return
+ *  - On success, zero.
+ *  - On failure, a negative value.
+ */
+int rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type);
+
  #ifdef __cplusplus
  }
  #endif
drivers/bus/vdev/vdev.c +33 -0
index fd7736d..773f6e0 100644
@@ -323,6 +323,39 @@ vdev_find_device(const struct rte_device *start,
 rte_dev_cmp_t cmp,
  	return NULL;
  }
 
+static struct rte_device *
+vdev_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_vdev_device *dev;
+
+	TAILQ_FOREACH(dev, &vdev_device_list, next) {
+		if (start && &dev->device == start) {
+			start = NULL;
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+	return NULL;
+}
+
+static int
+vdev_remap_device(struct rte_device *dev)
+{
+	RTE_SET_USED(dev);
+	return 0;
+}
+
+static int
+vdev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	RTE_SET_USED(dev_name);
+	RTE_SET_USED(drv_type);
+	return 0;
+}
+
  static int
  vdev_plug(struct rte_device *dev)
  {
drivers/bus/vdev/vdev.c +3 -0
@@ -339,9 +372,12 @@ static struct rte_bus rte_vdev_bus = {
  	.scan = vdev_scan,
  	.probe = vdev_probe,
  	.find_device = vdev_find_device,
+	.find_device_by_name = vdev_find_device_by_name,
  	.plug = vdev_plug,
  	.unplug = vdev_unplug,
  	.parse = vdev_parse,
+	.remap_device = vdev_remap_device,
+	.bind_driver = vdev_bind_driver,
  };
 
  RTE_REGISTER_BUS(vdev, rte_vdev_bus);
lib/librte_eal/bsdapp/eal/eal_dev.c new file +19 -0
new file mode 100644
index 0000000..6ea9a74
@@ -0,0 +1,64 @@
+/*-
+ *   Copyright(c) 2010-2017 Intel Corporation.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/bsdapp/eal/eal_dev.c new file +34 -0
+
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <sys/queue.h>
+#include <sys/signalfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdbool.h>
+
+#include <rte_malloc.h>
+#include <rte_bus.h>
+#include <rte_dev.h>
+#include <rte_devargs.h>
+#include <rte_debug.h>
+#include <rte_log.h>
+
+#include "eal_thread.h"
+
+int
+rte_dev_monitor_start(void)
+{
+	return -1;
+}
+
+int
+rte_dev_monitor_stop(void)
+{
+	return -1;
+}
lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +21 -0
new file mode 100644
index 0000000..6a6feb5
@@ -0,0 +1,106 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +38 -0
+
+#ifndef _RTE_DEV_H_
+#error "don't include this file directly, please include generic <rte_dev.h>"
+#endif
+
+#ifndef _RTE_LINUXAPP_DEV_H_
+#define _RTE_LINUXAPP_DEV_H_
+
+#include <stdio.h>
+
+#include <rte_dev.h>
+
+#define RTE_EAL_UEV_MSG_LEN 4096
+#define RTE_EAL_UEV_MSG_ELEM_LEN 128
+
+enum uev_subsystem {
+	UEV_SUBSYSTEM_UIO,
+	UEV_SUBSYSTEM_VFIO,
+	UEV_SUBSYSTEM_PCI,
+	UEV_SUBSYSTEM_MAX
+};
+
+enum uev_monitor_netlink_group {
+	UEV_MONITOR_KERNEL,
+	UEV_MONITOR_UDEV,
+};
+
+/**
+ * The device event type.
+ */
+enum rte_eal_dev_event_type {
+	RTE_EAL_DEV_EVENT_UNKNOWN,	/**< unknown event type */
+	RTE_EAL_DEV_EVENT_ADD,		/**< device adding event */
+	RTE_EAL_DEV_EVENT_REMOVE,
+					/**< device removing event */
+	RTE_EAL_DEV_EVENT_CHANGE,
+					/**< device status change event */
+	RTE_EAL_DEV_EVENT_MOVE,		/**< device sys path move

event */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +3 -0
+	RTE_EAL_DEV_EVENT_ONLINE,	/**< device online event */
+	RTE_EAL_DEV_EVENT_OFFLINE,	/**< device offline event */
+	RTE_EAL_DEV_EVENT_MAX		/**< max value of this enum

*/

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +7 -0
+};
+
+struct rte_eal_uevent {
+	enum rte_eal_dev_event_type type;	/**< device event type */
+	int subsystem;				/**< subsystem id */
+	char *devname;				/**< device name */
+	enum uev_monitor_netlink_group group;	/**< device netlink

group */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +26 -0
+};
+
+/**
+ * Start the device uevent monitoring.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_monitor_start(void);
+
+/**
+ * Stop the device uevent monitoring .
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+
+int
+rte_dev_monitor_stop(void);
+
+#endif /* _RTE_LINUXAPP_DEV_H_ */
lib/librte_eal/common/eal_common_bus.c +11 -0
index 3e022d5..b7219c9 100644
@@ -51,8 +51,11 @@ rte_bus_register(struct rte_bus *bus)
  	RTE_VERIFY(bus->scan);
  	RTE_VERIFY(bus->probe);
  	RTE_VERIFY(bus->find_device);
+	RTE_VERIFY(bus->find_device_by_name);
  	/* Buses supporting driver plug also require unplug. */
  	RTE_VERIFY(!bus->plug || bus->unplug);
+	RTE_VERIFY(bus->remap_device);
+	RTE_VERIFY(bus->bind_driver);
 
  	TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
  	RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
@@ -170,6 +173,14 @@ cmp_rte_device(const struct rte_device *dev1,
 const void *_dev2)
  }
 
  static int
+cmp_rte_device_name(const char *dev_name1, const void *_dev_name2)
+{
+	const char *dev_name2 = _dev_name2;
+
+	return strcmp(dev_name1, dev_name2);
+}
+
+static int
  bus_find_device(const struct rte_bus *bus, const void *_dev)
  {

struct rte_device *dev;

lib/librte_eal/common/eal_common_bus.c +19 -0
@@ -178,6 +189,25 @@ bus_find_device(const struct rte_bus *bus, const
 void *_dev)
  	return dev == NULL;
  }
 
+static struct rte_device *
+bus_find_device_by_name(const struct rte_bus *bus, const void
 *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus->find_device_by_name(NULL, cmp_rte_device_name,
 _dev_name);
+	return dev;
+}
+
+struct rte_device *
+
+rte_bus_find_device(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus_find_device_by_name(bus, _dev_name);
+	return dev;
+}
+
  struct rte_bus *
  rte_bus_find_by_device(const struct rte_device *dev)
  {
lib/librte_eal/common/eal_common_dev.c +37 -0
index dda8f58..47909e8 100644
@@ -42,9 +42,31 @@
  #include <rte_devargs.h>
  #include <rte_debug.h>
  #include <rte_log.h>
+#include <rte_spinlock.h>
+#include <rte_malloc.h>
 
  #include "eal_private.h"
 
+/* spinlock for device callbacks */
+static rte_spinlock_t rte_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
+
+/**
+ * The user application callback description.
+ *
+ * It contains callback address to be registered by user application,
+ * the pointer to the parameters for callback, and the event type.
+ */
+struct rte_eal_dev_callback {
+	TAILQ_ENTRY(rte_eal_dev_callback) next; /**< Callbacks list */
+	rte_eal_dev_cb_fn cb_fn;                /**< Callback address */
+	void *cb_arg;                           /**< Parameter for callback */
+	void *ret_param;                        /**< Return parameter */
+	enum rte_eal_dev_event_type event;      /**< device event type */
+	uint32_t active;                        /**< Callback is executing */
+};
+
+static struct rte_eal_dev_callback *dev_add_cb;
+
  static int cmp_detached_dev_name(const struct rte_device *dev,
  	const void *_name)
  {
@@ -234,3 +256,150 @@ int rte_eal_hotplug_remove(const char *busname,
 const char *devname)
  	rte_eal_devargs_remove(busname, devname);
  	return ret;
  }
+
+int
+rte_eal_dev_monitor_enable(void)
+{
+	int ret;
+
+	ret = rte_dev_monitor_start();
+	if (ret)
+		RTE_LOG(ERR, EAL, "Can not init device monitor\n");
+	return ret;
+}
+
+int
+rte_dev_callback_register(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
Quoted 8 lines

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 280

Quoted 7 lines
lib/librte_eal/common/eal_common_dev.c +7 -0
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg)
+{
+	struct rte_eal_dev_callback *user_cb;
+
+	if (!cb_fn)
+		return -EINVAL;
+

What's about checking the device pointer is not NULL ?

sure.

Quoted 18 lines
Quoted 18 lines
lib/librte_eal/common/eal_common_dev.c +17 -0
+	rte_spinlock_lock(&rte_dev_cb_lock);
+
+	if (TAILQ_EMPTY(&(device->uev_cbs)))
+		TAILQ_INIT(&(device->uev_cbs));
+
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		user_cb = NULL;
+	} else {
+		TAILQ_FOREACH(user_cb, &(device->uev_cbs), next) {
+			if (user_cb->cb_fn == cb_fn &&
+				user_cb->cb_arg == cb_arg &&
+				user_cb->event == event) {
+				break;
+			}
+		}
+	}
+
Quoted 8 lines

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 304

Quoted 7 lines
lib/librte_eal/common/eal_common_dev.c +7 -0
+	/* create a new callback. */
+	if (user_cb == NULL) {
+		/* allocate a new interrupt callback entity */
+		user_cb = rte_zmalloc("eal device event",
+					sizeof(*user_cb), 0);
+		if (user_cb == NULL) {
+			RTE_LOG(ERR, EAL, "Can not allocate memory\n");

Missing rte_spinlock_unlock.

got it.

Quoted 10 lines

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 311

Quoted 8 lines
lib/librte_eal/common/eal_common_dev.c +7 -0
+			return -ENOMEM;
+		}
+		user_cb->cb_fn = cb_fn;
+		user_cb->cb_arg = cb_arg;
+		user_cb->event = event;
+		if (event == RTE_EAL_DEV_EVENT_ADD)
+			dev_add_cb = user_cb;

Only one dpdk entity can register to ADD callback?

Quoted 5 lines

I suggest to add option to register all devices maybe by using dummy device which will include all the "ALL_DEVICES" callbacks per event. All means past, present and future devices, by this way 1 callback can be called for all the devices and more than one dpdk entity could register to an ADD\NEW event. What's about NEW instead of ADD?

I also suggest to add the device pointer as a parameter to the callback(which will be managed by EAL).

if you talk about dev_add_cb, the add means device add not cb add, if you talk about dev event type, the ADD type is consistent with the type form kernel side, anyway could be find a better. but for 1 callback for all device, it is make sense , i will think about that.

Quoted 20 lines
Quoted 20 lines
lib/librte_eal/common/eal_common_dev.c +2 -0
+		else
+			TAILQ_INSERT_TAIL(&(device->uev_cbs), user_cb,

next);

lib/librte_eal/common/eal_common_dev.c +17 -0
+	}
+
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return 0;
+}
+
+int
+rte_dev_callback_unregister(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg)
+{
+	int ret;
+	struct rte_eal_dev_callback *cb, *next;
+
+	if (!cb_fn)
+		return -EINVAL;
+
Quoted 8 lines

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 337

Quoted 7 lines
lib/librte_eal/common/eal_common_dev.c +7 -0
+	rte_spinlock_lock(&rte_dev_cb_lock);
+
+	ret = 0;
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		rte_free(dev_add_cb);
+		dev_add_cb = NULL;
+	} else {

Device NULL checking?

yes.

Quoted 40 lines
Quoted 40 lines
lib/librte_eal/common/eal_common_dev.c +16 -0
+		for (cb = TAILQ_FIRST(&(device->uev_cbs)); cb != NULL;
+		      cb = next) {
+
+			next = TAILQ_NEXT(cb, next);
+
+			if (cb->cb_fn != cb_fn || cb->event != event ||
+					(cb->cb_arg != (void *)-1 &&
+					cb->cb_arg != cb_arg))
+				continue;
+
+			/*
+			 * if this callback is not executing right now,
+			 * then remove it.
+			 */
+			if (cb->active == 0) {
+				TAILQ_REMOVE(&(device->uev_cbs), cb,

next);

lib/librte_eal/common/eal_common_dev.c +23 -0
+				rte_free(cb);
+			} else {
+				ret = -EAGAIN;
+			}
+		}
+	}
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return ret;
+}
+
+int
+_rte_dev_callback_process(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			void *cb_arg, void *ret_param)
+{
+	struct rte_eal_dev_callback dev_cb;
+	struct rte_eal_dev_callback *cb_lst;
+	int rc = 0;
+
+	rte_spinlock_lock(&rte_dev_cb_lock);
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		if (cb_arg != NULL)
+			dev_add_cb->cb_arg = cb_arg;
Quoted 8 lines

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 381

Quoted 5 lines
lib/librte_eal/common/eal_common_dev.c +5 -0
+
+		if (ret_param != NULL)
+			dev_add_cb->ret_param = ret_param;
+
+		rte_spinlock_unlock(&rte_dev_cb_lock);

Can't someone free it when it running?
I suggest to keep the lock locked.
Callbacks are not allowed to use this mechanism to prevent deadlock.

seems it would bring some deadlock here, let's check it more.

Quoted 17 lines
Quoted 6 lines
lib/librte_eal/common/eal_common_dev.c +2 -0
+		rc = dev_add_cb->cb_fn(dev_add_cb->event,
+				dev_add_cb->cb_arg, dev_add_cb-
Quoted 1 line

ret_param);

lib/librte_eal/common/eal_common_dev.c +3 -0
+		rte_spinlock_lock(&rte_dev_cb_lock);
+	} else {
+		TAILQ_FOREACH(cb_lst, &(device->uev_cbs), next) {

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 396

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+			if (cb_lst->cb_fn == NULL || cb_lst->event != event)
+				continue;
+			dev_cb = *cb_lst;
+			cb_lst->active = 1;
+			if (cb_arg != NULL)
+				dev_cb.cb_arg = cb_arg;
+			if (ret_param != NULL)
+				dev_cb.ret_param = ret_param;
+
+			rte_spinlock_unlock(&rte_dev_cb_lock);

The current active flag doesn't do it thread safe here, I suggest to keep the lock locked.

Quoted 8 lines

Scenario:
1. Thread A see active = 0 in unregister function.
2. Context switch.
3. Thread B start the callback.
4. Context switch.
5. Thread A free it.
6. Context switch.
7. Seg fault in Thread B.

the same as above.

Quoted 551 lines
Quoted 551 lines
lib/librte_eal/common/eal_common_dev.c +9 -0
+			rc = dev_cb.cb_fn(dev_cb.event,
+					dev_cb.cb_arg, dev_cb.ret_param);
+			rte_spinlock_lock(&rte_dev_cb_lock);
+			cb_lst->active = 0;
+		}
+	}
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return rc;
+}
lib/librte_eal/common/include/rte_bus.h +7 -0
index 6fb0834..6c4ae31 100644
@@ -122,6 +122,34 @@ typedef struct rte_device *
  			 const void *data);
 
  /**
+ * Device iterator to find a device on a bus.
+ *
+ * This function returns an rte_device if one of those held by the bus
+ * matches the data passed as parameter.
+ *
+ * If the comparison function returns zero this function should stop iterating
+ * over any more devices. To continue a search the device of a previous

search
+ * can be passed via the start parameter.
+ *
+ * @param cmp
+ * the device name comparison function.
+ *
+ * @param data
+ * Data to compare each device against.
+ *
+ * @param start
+ * starting point for the iteration
+ *
+ * @return
+ * The first device matching the data, NULL if none exists.
+ */

lib/librte_eal/common/include/rte_bus.h +8 -0
+typedef struct rte_device *
+(*rte_bus_find_device_by_name_t)(const struct rte_device *start,
+			 rte_dev_cmp_name_t cmp,
+			 const void *data);
+
+
+/**
   * Implementation specific probe function which is responsible for linking
   * devices on that bus with applicable drivers.
   *
@@ -168,6 +196,37 @@ typedef int (*rte_bus_unplug_t)(struct rte_device
 *dev);
  typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 
  /**
+ * Implementation specific remap function which is responsible for

remmaping
+ * devices on that bus from original share memory resource to a private memory
+ * resource for the sake of device has been removal.
+ *
+ * @param dev
+ * Device pointer that was returned by a previous call to find_device.
+ *
+ * @return
+ * 0 on success.
+ * !0 on error.
+ */

lib/librte_eal/common/include/rte_bus.h +24 -0
+typedef int (*rte_bus_remap_device_t)(struct rte_device *dev);
+
+/**
+ * Implementation specific bind driver function which is responsible for bind
+ * a explicit type of driver with a devices on that bus.
+ *
+ * @param dev_name
+ *	device textual description.
+ *
+ * @param drv_type
+ *	driver type textual description.
+ *
+ * @return
+ *	0 on success.
+ *	!0 on error.
+ */
+typedef int (*rte_bus_bind_driver_t)(const char *dev_name,
+				const char *drv_type);
+
+/**
   * Bus scan policies
   */
  enum rte_bus_scan_mode {
@@ -206,9 +265,13 @@ struct rte_bus {
  	rte_bus_scan_t scan;         /**< Scan for devices attached to bus */
  	rte_bus_probe_t probe;       /**< Probe devices on bus */
  	rte_bus_find_device_t find_device; /**< Find a device on the bus */
+	rte_bus_find_device_by_name_t find_device_by_name;
+				     /**< Find a device on the bus */
  	rte_bus_plug_t plug;         /**< Probe single device for drivers */
  	rte_bus_unplug_t unplug;     /**< Remove single device from driver
 */
  	rte_bus_parse_t parse;       /**< Parse a device name */
+	rte_bus_remap_device_t remap_device;       /**< remap a device */
+	rte_bus_bind_driver_t bind_driver; /**< bind a driver for bus device
 */
  	struct rte_bus_conf conf;    /**< Bus configuration */

rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu
class */
};

lib/librte_eal/common/include/rte_bus.h +6 -0
@@ -306,6 +369,12 @@ struct rte_bus *rte_bus_find(const struct rte_bus
 *start, rte_bus_cmp_t cmp,
  struct rte_bus *rte_bus_find_by_device(const struct rte_device *dev);
 
  /**
+ * Find the registered bus for a particular device.
+ */
+struct rte_device *rte_bus_find_device(const struct rte_bus *bus,
+				const void *dev_name);
+
+/**
   * Find the registered bus for a given name.
   */
  struct rte_bus *rte_bus_find_by_name(const char *busname);
lib/librte_eal/common/include/rte_dev.h +21 -0
index 9342e0c..19971d0 100644
@@ -51,6 +51,15 @@ extern "C" {
 
  #include <rte_log.h>
 
+#include <exec-env/rte_dev.h>
+
+typedef int (*rte_eal_dev_cb_fn)(enum rte_eal_dev_event_type event,
+					void *cb_arg, void *ret_param);
+
+struct rte_eal_dev_callback;
+/** @internal Structure to keep track of registered callbacks */
+TAILQ_HEAD(rte_eal_dev_cb_list, rte_eal_dev_callback);
+
  __attribute__((format(printf, 2, 0)))
  static inline void
  rte_pmd_debug_trace(const char *func_name, const char *fmt, ...)
@@ -157,6 +166,13 @@ struct rte_driver {
   */
  #define RTE_DEV_NAME_MAX_LEN 64
 
+enum device_state {
+	DEVICE_UNDEFINED,
+	DEVICE_FAULT,
+	DEVICE_PARSED,
+	DEVICE_PROBED,
+};
+
  /**
   * A structure describing a generic device.
   */
@@ -166,6 +182,9 @@ struct rte_device {
  	const struct rte_driver *driver;/**< Associated driver */
  	int numa_node;                /**< NUMA node connection */
  	struct rte_devargs *devargs;  /**< Device user arguments */
+	enum device_state state;  /**< Device state */
+	/** User application callbacks for device event */
+	struct rte_eal_dev_cb_list uev_cbs;
  };
 
  /**
@@ -248,6 +267,8 @@ int rte_eal_hotplug_remove(const char *busname,
 const char *devname);
   */
  typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void
 *data);
 
+typedef int (*rte_dev_cmp_name_t)(const char *dev_name, const void
 *data);
+
  #define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]

  #define RTE_PMD_EXPORT_NAME(name, idx) \
lib/librte_eal/common/include/rte_dev.h +68 -0
@@ -293,4 +314,72 @@ __attribute__((used)) = str
  }
  #endif
 
+/**
+ * It enable the device event monitoring for a specific event.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_eal_dev_monitor_enable(void);
+/**
+ * It registers the callback for the specific event. Multiple
+ * callbacks cal be registered at the same time.
+ * @param event
+ *  The device event type.
+ * @param cb_fn
+ *  callback address.
+ * @param cb_arg
+ *  address of parameter for callback.
+ *
+ * @return
+ *  - On success, zero.
+ *  - On failure, a negative value.
+ */
+int rte_dev_callback_register(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg);
+
+/**
+ * It unregisters the callback according to the specified event.
+ *
+ * @param event
+ *  The event type which corresponding to the callback.
+ * @param cb_fn
+ *  callback address.
+ *  address of parameter for callback, (void *)-1 means to remove all
+ *  registered which has the same callback address.
+ *
+ * @return
+ *  - On success, return the number of callback entities removed.
+ *  - On failure, a negative value.
+ */
+int rte_dev_callback_unregister(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg);
+
+/**
+ * @internal Executes all the user application registered callbacks for
+ * the specific device. It is for DPDK internal user only. User
+ * application should not call it directly.
+ *
+ * @param event
+ *  The device event type.
+ * @param cb_arg
+ *  callback parameter.
+ * @param ret_param
+ *  To pass data back to user application.
+ *  This allows the user application to decide if a particular function
+ *  is permitted or not.
+ *
+ * @return
+ *  - On success, return zero.
+ *  - On failure, a negative value.
+ */
+int
+_rte_dev_callback_process(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			void *cb_arg, void *ret_param);
  #endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/Makefile +0 -0
index 5a7b8b2..05a2437 100644
@@ -67,6 +67,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) +=

eal_lcore.c
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_interrupts.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_alarm.c

lib/librte_eal/linuxapp/eal/Makefile +2 -1
+SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_dev.c
 
  # from common dir
  SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_lcore.c
@@ -120,7 +121,7 @@ ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y)
  CFLAGS_eal_thread.o += -Wno-return-type
  endif
 
-INC := rte_kni_common.h
+INC := rte_kni_common.h rte_dev.h
 
  SYMLINK-$(CONFIG_RTE_EXEC_ENV_LINUXAPP)-include/exec-env := \
  	$(addprefix include/exec-env/,$(INC))
lib/librte_eal/linuxapp/eal/eal_alarm.c +1 -0
index 8e4a775..29e73a7 100644
@@ -209,6 +209,7 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn,
 void *cb_arg)
  	int count = 0;
  	int err = 0;
  	int executing;
+	int ret;
 
  	if (!cb_fn) {
  		rte_errno = EINVAL;
lib/librte_eal/linuxapp/eal/eal_alarm.c +4 -0
@@ -259,6 +260,10 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn,
 void *cb_arg)
  			}
  			ap_prev = ap;
  		}
+
+		ret |= rte_intr_callback_unregister(&intr_handle,
+				eal_alarm_callback, NULL);
+
  		rte_spinlock_unlock(&alarm_list_lk);
  	} while (executing != 0);
lib/librte_eal/linuxapp/eal/eal_dev.c new file +19 -0
new file mode 100644
index 0000000..49fd0dc
@@ -0,0 +1,356 @@
+/*-
+ *   Copyright(c) 2010-2017 Intel Corporation.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/linuxapp/eal/eal_dev.c new file +71 -0
+
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <sys/queue.h>
+#include <sys/signalfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdbool.h>
+
+#include <rte_malloc.h>
+#include <rte_bus.h>
+#include <rte_dev.h>
+#include <rte_devargs.h>
+#include <rte_debug.h>
+#include <rte_log.h>
+
+#include "eal_thread.h"
+
+/* uev monitoring thread */
+static pthread_t uev_monitor_thread;
+
+bool udev_exit = true;
+
+bool no_request_thread = true;
+
+static void sig_handler(int signum)
+{
+	if (signum == SIGINT || signum == SIGTERM)
+		rte_dev_monitor_stop();
+}
+
+static int
+dev_monitor_fd_new(void)
+{
+
+	int uevent_fd;
+
+	uevent_fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC |
+			SOCK_NONBLOCK,
+			NETLINK_KOBJECT_UEVENT);
+	if (uevent_fd < 0) {
+		RTE_LOG(ERR, EAL, "create uevent fd failed\n");
+		return -1;
+	}
+	return uevent_fd;
+}
+
+static int
+dev_monitor_enable(int netlink_fd)
+{
+	struct sockaddr_nl addr;
+	int ret;
+	int size = 64 * 1024;
+	int nonblock = 1;
+
+	memset(&addr, 0, sizeof(addr));
+	addr.nl_family = AF_NETLINK;
+	addr.nl_pid = 0;
+	addr.nl_groups = 0xffffffff;
+
+	if (bind(netlink_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		RTE_LOG(ERR, EAL, "bind failed\n");
+		goto err;
+	}
+
+	setsockopt(netlink_fd, SOL_SOCKET, SO_PASSCRED, &size,

sizeof(size));

lib/librte_eal/linuxapp/eal/eal_dev.c new file +53 -0
+
+	ret = ioctl(netlink_fd, FIONBIO, &nonblock);
+	if (ret != 0) {
+		RTE_LOG(ERR, EAL, "ioctl(FIONBIO) failed\n");
+		goto err;
+	}
+	return 0;
+err:
+	close(netlink_fd);
+	return -1;
+}
+
+static void
+dev_uev_parse(const char *buf, struct rte_eal_uevent *event)
+{
+	char action[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char subsystem[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char dev_path[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char pci_slot_name[RTE_EAL_UEV_MSG_ELEM_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(subsystem, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(dev_path, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(pci_slot_name, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+
+	while (i < RTE_EAL_UEV_MSG_LEN) {
+		for (; i < RTE_EAL_UEV_MSG_LEN; i++) {
+			if (*buf)
+				break;
+			buf++;
+		}
+		if (!strncmp(buf, "libudev", 7)) {
+			buf += 7;
+			i += 7;
+			event->group = UEV_MONITOR_UDEV;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		} else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
+			buf += 14;
+			i += 14;
+			snprintf(pci_slot_name, sizeof(subsystem), "%s",

buf);

lib/librte_eal/linuxapp/eal/eal_dev.c new file +67 -0
+			event->devname = pci_slot_name;
+		}
+		for (; i < RTE_EAL_UEV_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;
+		}
+	}
+
+	if (!strncmp(subsystem, "pci", 3))
+		event->subsystem = UEV_SUBSYSTEM_PCI;
+	if (!strncmp(action, "add", 3))
+		event->type = RTE_EAL_DEV_EVENT_ADD;
+	if (!strncmp(action, "remove", 6))
+		event->type = RTE_EAL_DEV_EVENT_REMOVE;
+	event->devname = pci_slot_name;
+}
+
+static int
+dev_uev_receive(int fd, struct rte_eal_uevent *uevent)
+{
+	int ret;
+	char buf[RTE_EAL_UEV_MSG_LEN];
+
+	memset(uevent, 0, sizeof(struct rte_eal_uevent));
+	memset(buf, 0, RTE_EAL_UEV_MSG_LEN);
+
+	ret = recv(fd, buf, RTE_EAL_UEV_MSG_LEN - 1, MSG_DONTWAIT);
+	if (ret < 0) {
+		RTE_LOG(ERR, EAL,
+		"Socket read error(%d): %s\n",
+		errno, strerror(errno));
+		return -1;
+	} else if (ret == 0)
+		/* connection closed */
+		return -1;
+
+	dev_uev_parse(buf, uevent);
+
+	return 0;
+}
+
+static int
+dev_uev_process(struct epoll_event *events, int nfds)
+{
+	struct rte_bus *bus;
+	struct rte_device *dev;
+	struct rte_eal_uevent uevent;
+	int ret;
+	int i;
+
+	for (i = 0; i < nfds; i++) {
+		/**
+		 * check device uevent from kernel side, no need to check
+		 * uevent from udev.
+		 */
+		if ((dev_uev_receive(events[i].data.fd, &uevent)) ||
+			(uevent.group == UEV_MONITOR_UDEV))
+			return 0;
+
+		/* default handle all pci devcie when is being hot plug */
+		if (uevent.subsystem == UEV_SUBSYSTEM_PCI) {
+			bus = rte_bus_find_by_name("pci");
+			dev = rte_bus_find_device(bus, uevent.devname);
+			if (uevent.type == RTE_EAL_DEV_EVENT_REMOVE) {
+
+				if ((!dev) || dev->state ==

DEVICE_UNDEFINED)

lib/librte_eal/linuxapp/eal/eal_dev.c new file +8 -0
+					return 0;
+				dev->state = DEVICE_FAULT;
+
+				/**
+				 * remap the resource to be fake
+				 * before user's removal processing
+				 */
+				ret = bus->remap_device(dev);
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 233

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +2 -0
+				if (!ret)
+

return(_rte_dev_callback_process(dev,

lib/librte_eal/linuxapp/eal/eal_dev.c new file +2 -0
+					  RTE_EAL_DEV_EVENT_REMOVE,
+					  NULL, NULL));

What is the reason to keep this device in EAL device list after the removal?
I suggest to remove it (driver remove, bus remove and EAL remove) after the callbacks running. By this way EAL can initiate all device removals.

agree, device should be remove from the eal device list after the removal.

Quoted 6 lines
Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +1 -0
+			} else if (uevent.type == RTE_EAL_DEV_EVENT_ADD)

{

lib/librte_eal/linuxapp/eal/eal_dev.c new file +4 -0
+				if (dev == NULL) {
+					/**
+					 * bind the driver to the device
+					 * before user's add processing
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 252

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +5 -0
+					 */
+					bus->bind_driver(
+						uevent.devname,
+						"igb_uio");
+

Similar comments here:
EAL can initiate all device probe operations by adding the device and probing it here before the callback running. Then, also the device pointer can be passed to the callbacks.

pass a device pointer could be bring some more change, let's think about more.

Quoted 89 lines
Quoted 89 lines

return(_rte_dev_callback_process(NULL,

lib/librte_eal/linuxapp/eal/eal_dev.c new file +80 -0
+					  RTE_EAL_DEV_EVENT_ADD,
+					  uevent.devname, NULL));
+				}
+			}
+		}
+	}
+	return 0;
+}
+
+/**
+ * It builds/rebuilds up the epoll file descriptor with all the
+ * file descriptors being waited on. Then handles the interrupts.
+ *
+ * @param arg
+ *  pointer. (unused)
+ *
+ * @return
+ *  never return;
+ */
+static __attribute__((noreturn)) void *
+dev_uev_monitoring(__rte_unused void *arg)
+{
+	struct sigaction act;
+	sigset_t mask;
+	int netlink_fd;
+	struct epoll_event ep_kernel;
+	int fd_ep;
+
+	udev_exit = false;
+
+	/* set signal handlers */
+	memset(&act, 0x00, sizeof(struct sigaction));
+	act.sa_handler = sig_handler;
+	sigemptyset(&act.sa_mask);
+	act.sa_flags = SA_RESTART;
+	sigaction(SIGINT, &act, NULL);
+	sigaction(SIGTERM, &act, NULL);
+	sigemptyset(&mask);
+	sigaddset(&mask, SIGINT);
+	sigaddset(&mask, SIGTERM);
+	sigprocmask(SIG_UNBLOCK, &mask, NULL);
+
+	fd_ep = epoll_create1(EPOLL_CLOEXEC);
+	if (fd_ep < 0) {
+		RTE_LOG(ERR, EAL, "error creating epoll fd: %m\n");
+		goto out;
+	}
+
+	netlink_fd = dev_monitor_fd_new();
+
+	if (dev_monitor_enable(netlink_fd) < 0) {
+		RTE_LOG(ERR, EAL, "error subscribing to kernel events\n");
+		goto out;
+	}
+
+	memset(&ep_kernel, 0, sizeof(struct epoll_event));
+	ep_kernel.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
+	ep_kernel.data.fd = netlink_fd;
+	if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, netlink_fd,
+		&ep_kernel) < 0) {
+		RTE_LOG(ERR, EAL, "error addding fd to epoll: %m\n");
+		goto out;
+	}
+
+	while (!udev_exit) {
+		int fdcount;
+		struct epoll_event ev[1];
+
+		fdcount = epoll_wait(fd_ep, ev, 1, -1);
+		if (fdcount < 0) {
+			if (errno != EINTR)
+				RTE_LOG(ERR, EAL, "error receiving uevent "
+					"message: %m\n");
+				continue;
+			}
+
+		/* epoll_wait has at least one fd ready to read */
+		if (dev_uev_process(ev, fdcount) < 0) {
+			if (errno != EINTR)
+				RTE_LOG(ERR, EAL, "error processing uevent

"

lib/librte_eal/linuxapp/eal/eal_dev.c new file +7 -0
+					"message: %m\n");
+		}
+	}
+out:
+	if (fd_ep >= 0)
+		close(fd_ep);
+	if (netlink_fd >= 0)
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 337

Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +7 -0
+		close(netlink_fd);
+	rte_panic("uev monitoring fail\n");
+}
+
+int
+rte_dev_monitor_start(void)
+{

Maybe add option to run it also by new EAL command line parameter?

good idea.

Quoted 11 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 346

Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +9 -0
+	int ret;
+
+	if (!no_request_thread)
+		return 0;
+	no_request_thread = false;
+
+	/* create the host thread to wait/handle the uevent from kernel */
+	ret = pthread_create(&uev_monitor_thread, NULL,
+		dev_uev_monitoring, NULL);

What is the reason to open new thread for hotplug?
Why not to use the current dpdk host thread by the alarm mechanism?

appropriate if you could talk about what you mean the disadvantage of new thread here and the advantage of alarm mechanism at the case?

Quoted 221 lines
Quoted 221 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	return ret;
+}
+
+int
+rte_dev_monitor_stop(void)
+{
+	udev_exit = true;
+	no_request_thread = true;
+	return 0;
+}
lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +21 -0
new file mode 100644
index 0000000..6a6feb5
@@ -0,0 +1,106 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +38 -0
+
+#ifndef _RTE_DEV_H_
+#error "don't include this file directly, please include generic <rte_dev.h>"
+#endif
+
+#ifndef _RTE_LINUXAPP_DEV_H_
+#define _RTE_LINUXAPP_DEV_H_
+
+#include <stdio.h>
+
+#include <rte_dev.h>
+
+#define RTE_EAL_UEV_MSG_LEN 4096
+#define RTE_EAL_UEV_MSG_ELEM_LEN 128
+
+enum uev_subsystem {
+	UEV_SUBSYSTEM_UIO,
+	UEV_SUBSYSTEM_VFIO,
+	UEV_SUBSYSTEM_PCI,
+	UEV_SUBSYSTEM_MAX
+};
+
+enum uev_monitor_netlink_group {
+	UEV_MONITOR_KERNEL,
+	UEV_MONITOR_UDEV,
+};
+
+/**
+ * The device event type.
+ */
+enum rte_eal_dev_event_type {
+	RTE_EAL_DEV_EVENT_UNKNOWN,	/**< unknown event type */
+	RTE_EAL_DEV_EVENT_ADD,		/**< device adding event */
+	RTE_EAL_DEV_EVENT_REMOVE,
+					/**< device removing event */
+	RTE_EAL_DEV_EVENT_CHANGE,
+					/**< device status change event */
+	RTE_EAL_DEV_EVENT_MOVE,		/**< device sys path move

event */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +3 -0
+	RTE_EAL_DEV_EVENT_ONLINE,	/**< device online event */
+	RTE_EAL_DEV_EVENT_OFFLINE,	/**< device offline event */
+	RTE_EAL_DEV_EVENT_MAX		/**< max value of this enum

*/

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +7 -0
+};
+
+struct rte_eal_uevent {
+	enum rte_eal_dev_event_type type;	/**< device event type */
+	int subsystem;				/**< subsystem id */
+	char *devname;				/**< device name */
+	enum uev_monitor_netlink_group group;	/**< device netlink

group */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +26 -0
+};
+
+/**
+ * Start the device uevent monitoring.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_monitor_start(void);
+
+/**
+ * Stop the device uevent monitoring .
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+
+int
+rte_dev_monitor_stop(void);
+
+#endif /* _RTE_LINUXAPP_DEV_H_ */
lib/librte_eal/linuxapp/igb_uio/igb_uio.c +6 -0
index a3a98c1..d0e07b4 100644
@@ -354,6 +354,12 @@ igbuio_pci_release(struct uio_info *info, struct
 inode *inode)
  	struct rte_uio_pci_dev *udev = info->priv;
  	struct pci_dev *dev = udev->pdev;
 
+	/* check if device have been remove before release */
+	if ((&dev->dev.kobj)->state_remove_uevent_sent == 1) {
+		pr_info("The device have been removed\n");
+		return -1;
+	}
+
  	/* disable interrupts */
  	igbuio_pci_disable_interrupts(udev);
lib/librte_pci/rte_pci.c +20 -1
index 0160fc1..feb5fd7 100644
@@ -172,6 +172,26 @@ rte_pci_addr_parse(const char *str, struct
 rte_pci_addr *addr)
  	return -1;
  }
 
+/* map a private resource from an address*/
+void *
+pci_map_private_resource(void *requested_addr, off_t offset, size_t size)
+{
+	void *mapaddr;
+
+	mapaddr = mmap(requested_addr, size,
+			   PROT_READ | PROT_WRITE,
+			   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
-1, 0);
+	if (mapaddr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "%s(): cannot mmap(%p, 0x%lx, 0x%lx): "
+			"%s (%p)\n",
+			__func__, requested_addr,
+			(unsigned long)size, (unsigned long)offset,
+			strerror(errno), mapaddr);
+	} else
+		RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n",
 mapaddr);
+
+	return mapaddr;
+}
 
  /* map a particular resource from a file */
  void *
lib/librte_pci/rte_pci.h +17 -0
index 4f2cd18..f6091a6 100644
@@ -227,6 +227,23 @@ int rte_pci_addr_cmp(const struct rte_pci_addr
 *addr,
  int rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr);
 
  /**
+ * @internal
+ * Map to a particular private resource.
+ *
+ * @param requested_addr
+ *      The starting address for the new mapping range.
+ * @param offset
+ *      The offset for the mapping range.
+ * @param size
+ *      The size for the mapping range.
+ * @return
+ *   - On success, the function returns a pointer to the mapped area.
+ *   - On error, the value MAP_FAILED is returned.
+ */
+void *pci_map_private_resource(void *requested_addr, off_t offset,
+		size_t size);
+
+/**
   * Map a particular resource from a file.
   *

* @param requested_addr
--
2.7.4

#311 lore raw

Re: [PATCH v7 1/2] eal: add uevent monitor for hot plug

Hi Guo
I answered for the 2 threads here.

From: Guo, Jia, Monday, January 8, 2018 7:27 AM

Quoted 36 lines · Matan Azrad
Quoted 3 lines

Hi Jeff

Maybe I'm touching in previous discussions but please see some

comments\questions.

Quoted 4 lines

From: Jeff Guo:

Quoted 2 lines

This patch aim to add a general uevent mechanism in eal device layer, to enable all linux kernel object hot plug monitoring, so user could use

these

Quoted 1 line
Quoted 1 line

APIs to monitor and read out the device status info that sent from the

kernel

Quoted 24 lines
Quoted 24 lines

side, then corresponding to handle it, such as detach or attach the device, and even benefit to use it to do smoothly fail safe work.

1) About uevent monitoring:
a: add one epolling to poll the netlink socket, to monitor the uevent of
the device, add device_state in struct of rte_device, to identify the device state machine.
b: add enum of rte_eal_dev_event_type and struct of rte_eal_uevent.
c: add below API in rte eal device common layer.
rte_eal_dev_monitor_enable
rte_dev_callback_register
rte_dev_callback_unregister
_rte_dev_callback_process
rte_dev_monitor_start
rte_dev_monitor_stop

2) About failure handler, use pci uio for example,
add pci_remap_device in bus layer and below function to process it:
rte_pci_remap_device
pci_uio_remap_resource
pci_map_private_resource
add rte_pci_dev_bind_driver to bind pci device with explicit driver.

<snip>

Quoted 11 lines
Quoted 7 lines

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 311

Quoted 4 lines
lib/librte_eal/common/eal_common_dev.c +4 -0
+		user_cb->cb_arg = cb_arg;
+		user_cb->event = event;
+		if (event == RTE_EAL_DEV_EVENT_ADD)
+			dev_add_cb = user_cb;

Only one dpdk entity can register to ADD callback?

I suggest to add option to register all devices maybe by using dummy

device which will include all the "ALL_DEVICES" callbacks per event.

Quoted 1 line

All means past, present and future devices, by this way 1 callback can be

called for all the devices and more than one dpdk entity could register to an ADD\NEW event.

Quoted 7 lines
Quoted 3 lines

What's about NEW instead of ADD?

I also suggest to add the device pointer as a parameter to the

callback(which will be managed by EAL).
if you talk about dev_add_cb, the add means device add not cb add, if you talk about dev event type, the ADD type is consistent with the type form kernel side, anyway could be find a better.

I'm talking about next:
1. dev_add_cb can hold only 1 callback, why? Can't 2 callbacks to be registered to RTE_EAL_DEV_EVENT_ADD event? (actually there is memory leak in this case)
2. Suggestion to register same callback to "all" devices by 1 call.
3. Suggestion to add parameter for the callback functions - the device pointer.
4. Suggestion to change name from RTE_EAL_DEV_EVENT_ADD to RTE_EAL_DEV_EVENT_NEW.
5. Clue how to implement 1,2 by dummy device.

Quoted 9 lines

but for 1 callback for all device, it is make sense , i will think about that.

Quoted 8 lines
Quoted 8 lines
(quoted diff) +2 -0
+		else
+			TAILQ_INSERT_TAIL(&(device->uev_cbs), user_cb,

next);

lib/librte_eal/common/eal_common_dev.c +5 -0
+	}
+
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return 0;
+}

<snip>

Quoted 21 lines
Quoted 13 lines
Quoted 13 lines
lib/librte_eal/common/eal_common_dev.c +13 -0
+int
+_rte_dev_callback_process(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			void *cb_arg, void *ret_param)
+{
+	struct rte_eal_dev_callback dev_cb;
+	struct rte_eal_dev_callback *cb_lst;
+	int rc = 0;
+
+	rte_spinlock_lock(&rte_dev_cb_lock);
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		if (cb_arg != NULL)
+			dev_add_cb->cb_arg = cb_arg;
Quoted 8 lines

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 381

Quoted 5 lines
lib/librte_eal/common/eal_common_dev.c +5 -0
+
+		if (ret_param != NULL)
+			dev_add_cb->ret_param = ret_param;
+
+		rte_spinlock_unlock(&rte_dev_cb_lock);

Can't someone free it when it running?
I suggest to keep the lock locked.
Callbacks are not allowed to use this mechanism to prevent deadlock.

Quoted 1 line

seems it would bring some deadlock here, let's check it more.

A deadlock should occur only when a callback tries to use this mechanism - I think it is OK, you just need to document it for the user.

Quoted 26 lines
Quoted 17 lines
Quoted 6 lines
(quoted diff) +2 -0
+		rc = dev_add_cb->cb_fn(dev_add_cb->event,
+				dev_add_cb->cb_arg, dev_add_cb-
Quoted 1 line

ret_param);

lib/librte_eal/common/eal_common_dev.c +3 -0
+		rte_spinlock_lock(&rte_dev_cb_lock);
+	} else {
+		TAILQ_FOREACH(cb_lst, &(device->uev_cbs), next) {

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 396

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+			if (cb_lst->cb_fn == NULL || cb_lst->event != event)
+				continue;
+			dev_cb = *cb_lst;
+			cb_lst->active = 1;
+			if (cb_arg != NULL)
+				dev_cb.cb_arg = cb_arg;
+			if (ret_param != NULL)
+				dev_cb.ret_param = ret_param;
+
+			rte_spinlock_unlock(&rte_dev_cb_lock);

The current active flag doesn't do it thread safe here, I suggest to keep the

lock locked.

Quoted 8 lines

Scenario:
1. Thread A see active = 0 in unregister function.
2. Context switch.
3. Thread B start the callback.
4. Context switch.
5. Thread A free it.
6. Context switch.
7. Seg fault in Thread B.

Quoted 1 line

the same as above.

The same as above, and I think the active flag doesn't solve the race and you must solve it for the both cases. I suggest just to keep the lock locked and document the optional deadlock by the callback code.

<snip>

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
lib/librte_eal/common/eal_common_dev.c +6 -0
+			rc = dev_cb.cb_fn(dev_cb.event,
+					dev_cb.cb_arg, dev_cb.ret_param);
+			rte_spinlock_lock(&rte_dev_cb_lock);
+			cb_lst->active = 0;
+		}
+	}
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 233

Quoted 6 lines
lib/librte_eal/common/eal_common_dev.c +3 -0
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return rc;
+}

return(_rte_dev_callback_process(dev,

lib/librte_eal/linuxapp/eal/eal_dev.c new file +2 -0
+					  RTE_EAL_DEV_EVENT_REMOVE,
+					  NULL, NULL));

What is the reason to keep this device in EAL device list after the removal? I suggest to remove it (driver remove, bus remove and EAL remove) after

Quoted 3 lines

the callbacks running.

Quoted 1 line

By this way EAL can initiate all device removals.

agree, device should be remove from the eal device list after the removal.

I suggest using rte_eal_hotplug_remove().

<Bring from the second thread>

Quoted 2 lines

it will do device removal from the device list by the eal device detach function in the call backs running. does it fulfill your concerns.

I mean the removal\probe should be initiated by the EAL and not by the users callbacks.

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +1 -0
+			} else if (uevent.type == RTE_EAL_DEV_EVENT_ADD)

{

lib/librte_eal/linuxapp/eal/eal_dev.c new file +3 -0
+				if (dev == NULL) {
+					/**
+					 * bind the driver to the device
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 252

Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +6 -0
+					 * before user's add processing
+					 */
+					bus->bind_driver(
+						uevent.devname,
+						"igb_uio");
+

Similar comments here:
EAL can initiate all device probe operations by adding the device and

Quoted 4 lines

probing it here before the callback running.

Quoted 1 line

Then, also the device pointer can be passed to the callbacks.

pass a device pointer could be bring some more change, let's think about more.

Yes, I know, it will help to the user especially in ADD(NEW) and REMOVE events.

Here you can use rte_eal_hotplug_add().

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

return(_rte_dev_callback_process(NULL,

lib/librte_eal/linuxapp/eal/eal_dev.c new file +8 -0
+					  RTE_EAL_DEV_EVENT_ADD,
+					  uevent.devname, NULL));
+				}
+			}
+		}
+	}
+	return 0;
+}

<snip>

Quoted 9 lines
Quoted 4 lines

lib/librte_eal/bsdapp/eal/eal_dev.c line 56

Quoted 3 lines
lib/librte_eal/bsdapp/eal/eal_dev.c new file +3 -0
+int
+rte_dev_monitor_start(void)
+{

Maybe add option to run it also by new EAL command line parameter?

good idea.

Quoted 4 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 341

Quoted 4 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +4 -0
+	int ret;
+
+	if (!no_request_thread)
+		return 0;

Look, also here there is race, no_request_thread doesn't solve it.
Maybe the EAL parameter should be the only way to run it(just don't expose this API), I think the default should be TRUE.

Quoted 9 lines
Quoted 7 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 346

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +5 -0
+	no_request_thread = false;
+
+	/* create the host thread to wait/handle the uevent from kernel */
+	ret = pthread_create(&uev_monitor_thread, NULL,
+		dev_uev_monitoring, NULL);

What is the reason to open new thread for hotplug?
Why not to use the current dpdk host thread by the alarm mechanism?

appropriate if you could talk about what you mean the disadvantage of new thread here and the advantage of alarm mechanism at the case?

One more thread can complicate things - the user will need to synchronize his alarm\interrupt callbacks code(host thread) with his hotplug callbacks code(hotplug thread).

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	return ret;
+}
+
+int
+rte_dev_monitor_stop(void)
+{
+	udev_exit = true;
+	no_request_thread = true;
+	return 0;
+}

<snip>

author
#312 lore raw

Re: [PATCH v7 1/2] eal: add uevent monitor for hot plug

In reply to Matan Azrad

add one more comment.

Quoted 1,525 lines · Matan Azrad

Hi Jeff

Maybe I'm touching in previous discussions but please see some comments\questions.

From: Jeff Guo:

Quoted 812 lines

This patch aim to add a general uevent mechanism in eal device layer, to enable all linux kernel object hot plug monitoring, so user could use these APIs to monitor and read out the device status info that sent from the kernel side, then corresponding to handle it, such as detach or attach the device, and even benefit to use it to do smoothly fail safe work.

1) About uevent monitoring:
a: add one epolling to poll the netlink socket, to monitor the uevent of
the device, add device_state in struct of rte_device, to identify the device state machine.
b: add enum of rte_eal_dev_event_type and struct of rte_eal_uevent.
c: add below API in rte eal device common layer.
rte_eal_dev_monitor_enable
rte_dev_callback_register
rte_dev_callback_unregister
_rte_dev_callback_process
rte_dev_monitor_start
rte_dev_monitor_stop

2) About failure handler, use pci uio for example,
add pci_remap_device in bus layer and below function to process it:
rte_pci_remap_device
pci_uio_remap_resource
pci_map_private_resource
add rte_pci_dev_bind_driver to bind pci device with explicit driver.

---
v7->v6:
a.modify vdev part according to the vdev rework b.re-define and split the func into common and bus specific code c.fix some incorrect issue.
b.fix the system hung after send packcet issue.

---
  drivers/bus/pci/bsd/pci.c                          |  30 ++
  drivers/bus/pci/linux/pci.c                        |  87 +++++
  drivers/bus/pci/linux/pci_init.h                   |   1 +
  drivers/bus/pci/pci_common.c                       |  43 +++
  drivers/bus/pci/pci_common_uio.c                   |  28 ++
  drivers/bus/pci/private.h                          |  12 +
  drivers/bus/pci/rte_bus_pci.h                      |  25 ++
  drivers/bus/vdev/vdev.c                            |  36 +++
  lib/librte_eal/bsdapp/eal/eal_dev.c                |  64 ++++
  .../bsdapp/eal/include/exec-env/rte_dev.h          | 106 ++++++
  lib/librte_eal/common/eal_common_bus.c             |  30 ++
  lib/librte_eal/common/eal_common_dev.c             | 169 ++++++++++
  lib/librte_eal/common/include/rte_bus.h            |  69 ++++
  lib/librte_eal/common/include/rte_dev.h            |  89 ++++++
  lib/librte_eal/linuxapp/eal/Makefile               |   3 +-
  lib/librte_eal/linuxapp/eal/eal_alarm.c            |   5 +
  lib/librte_eal/linuxapp/eal/eal_dev.c              | 356
+++++++++++++++++++++
  .../linuxapp/eal/include/exec-env/rte_dev.h        | 106 ++++++
  lib/librte_eal/linuxapp/igb_uio/igb_uio.c          |   6 +
  lib/librte_pci/rte_pci.c                           |  20 ++
  lib/librte_pci/rte_pci.h                           |  17 +
  21 files changed, 1301 insertions(+), 1 deletion(-)

create mode 100644 lib/librte_eal/bsdapp/eal/eal_dev.c
create mode 100644 lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h create mode 100644 lib/librte_eal/linuxapp/eal/eal_dev.c create mode 100644 lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h

drivers/bus/pci/bsd/pci.c +23 -0
index b8e2178..d58dbf6 100644
@@ -126,6 +126,29 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
  	}
  }
 
+/* re-map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_NIC_UIO:
+		ret = pci_uio_remap_resource(dev);
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver,
 skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
  void
  pci_uio_free_resource(struct rte_pci_device *dev,

struct mapped_pci_resource *uio_res)

drivers/bus/pci/bsd/pci.c +7 -0
@@ -678,3 +701,10 @@ rte_pci_ioport_unmap(struct rte_pci_ioport *p)
 
  	return ret;
  }
+
+int
+rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	return -1;
+}
+
drivers/bus/pci/linux/pci.c +32 -0
index 5da6728..792fd2c 100644
@@ -145,6 +145,38 @@ rte_pci_unmap_device(struct rte_pci_device *dev)
  	}
  }
 
+/* Map pci device */
+int
+rte_pci_remap_device(struct rte_pci_device *dev)
+{
+	int ret = -1;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_VFIO:
+#ifdef VFIO_PRESENT
+		/* no thing to do */
+#endif
+		break;
+	case RTE_KDRV_IGB_UIO:
+	case RTE_KDRV_UIO_GENERIC:
+		if (rte_eal_using_phys_addrs()) {
+			/* map resources for devices that use uio */
+			ret = pci_uio_remap_resource(dev);
+		}
+		break;
+	default:
+		RTE_LOG(DEBUG, EAL,
+			"  Not managed by a supported kernel driver,
 skipped\n");
+		ret = 1;
+		break;
+	}
+
+	return ret;
+}
+
  void *
  pci_find_max_end_va(void)
  {
drivers/bus/pci/linux/pci.c +55 -0
@@ -386,6 +418,8 @@ pci_scan_one(const char *dirname, const struct
 rte_pci_addr *addr)
  		rte_pci_add_device(dev);
  	}
 
+	dev->device.state = DEVICE_PARSED;
+	TAILQ_INIT(&(dev->device.uev_cbs));
  	return 0;
  }
@@ -854,3 +888,56 @@ rte_pci_ioport_unmap(struct rte_pci_ioport *p)
 
  	return ret;
  }
+
+int
+rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	char drv_bind_path[1024];
+	char drv_override_path[1024]; /* contains the /dev/uioX */
+	int drv_override_fd;
+	int drv_bind_fd;
+
+	RTE_SET_USED(drv_type);
+
+	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, drv_type, sizeof(drv_type)) < 0) {
+		RTE_LOG(ERR, EAL,
+			"Error: bind failed - Cannot write "
+			"driver %s to device %s\n", drv_type, dev_name);
+		goto err;
+	}
+
+	close(drv_override_fd);
+
+	snprintf(drv_bind_path, sizeof(drv_bind_path),
+		"/sys/bus/pci/drivers/%s/bind", drv_type);
+
+	/* do the bind by writing device to the specific driver  */
+	drv_bind_fd = open(drv_bind_path, O_WRONLY | O_APPEND);
+	if (drv_bind_fd < 0) {
+		RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
+			drv_bind_path, strerror(errno));
+		goto err;
+	}
+
+	if (write(drv_bind_fd, dev_name, sizeof(dev_name)) < 0)
+		goto err;
+
+	close(drv_bind_fd);
+	return 0;
+err:
+	close(drv_override_fd);
+	close(drv_bind_fd);
+	return -1;
+}
+
drivers/bus/pci/linux/pci_init.h +1 -0
index f342c47..5838402 100644
@@ -58,6 +58,7 @@ int pci_uio_alloc_resource(struct rte_pci_device *dev,
  		struct mapped_pci_resource **uio_res);
  void pci_uio_free_resource(struct rte_pci_device *dev,
  		struct mapped_pci_resource *uio_res);
+int pci_uio_remap_resource(struct rte_pci_device *dev);
  int pci_uio_map_resource_by_index(struct rte_pci_device *dev, int
 res_idx,
  		struct mapped_pci_resource *uio_res, int map_idx);
drivers/bus/pci/pci_common.c +40 -0
index 104fdf9..5417b32 100644
@@ -282,6 +282,7 @@ pci_probe_all_drivers(struct rte_pci_device *dev)
  		if (rc > 0)
  			/* positive value means driver doesn't support it */
  			continue;
+		dev->device.state = DEVICE_PROBED;
  		return 0;
  	}
  	return 1;
@@ -481,6 +482,7 @@ rte_pci_insert_device(struct rte_pci_device
 *exist_pci_dev,
  void
  rte_pci_remove_device(struct rte_pci_device *pci_dev)
  {
+	RTE_LOG(DEBUG, EAL, " rte_pci_remove_device for device list\n");
  	TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
  }
@@ -502,6 +504,44 @@ pci_find_device(const struct rte_device *start,
 rte_dev_cmp_t cmp,
  	return NULL;
  }
 
+static struct rte_device *
+pci_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_pci_device *dev;
+
+	FOREACH_DEVICE_ON_PCIBUS(dev) {
+		if (start && &dev->device == start) {
+			start = NULL; /* starting point found */
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+
+	return NULL;
+}
+
+static int
+pci_remap_device(struct rte_device *dev)
+{
+	struct rte_pci_device *pdev;
+	int ret;
+
+	if (dev == NULL)
+		return -EINVAL;
+
+	pdev = RTE_DEV_TO_PCI(dev);
+
+	/* remap resources for devices that use igb_uio */
+	ret = rte_pci_remap_device(pdev);
+	if (ret != 0)
+		RTE_LOG(ERR, EAL, "failed to remap device %s",
+			dev->name);
+	return ret;
+}
+
  static int
  pci_plug(struct rte_device *dev)
  {
drivers/bus/pci/pci_common.c +3 -0
@@ -528,10 +568,13 @@ struct rte_pci_bus rte_pci_bus = {
  		.scan = rte_pci_scan,
  		.probe = rte_pci_probe,
  		.find_device = pci_find_device,
+		.find_device_by_name = pci_find_device_by_name,
  		.plug = pci_plug,
  		.unplug = pci_unplug,
  		.parse = pci_parse,
  		.get_iommu_class = rte_pci_get_iommu_class,
+		.remap_device = pci_remap_device,
+		.bind_driver = rte_pci_dev_bind_driver,
  	},
  	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
  	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +28 -0
index 0671131..8cb4009 100644
@@ -176,6 +176,34 @@ pci_uio_unmap(struct mapped_pci_resource
 *uio_res)
  	}
  }
 
+/* remap the PCI resource of a PCI device in private virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+	int i;
+	uint64_t phaddr;
+	void *map_address;
+
+	/* Map all BARs */
+	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		/* skip empty BAR */
+		phaddr = dev->mem_resource[i].phys_addr;
+		if (phaddr == 0)
+			continue;
+		map_address = pci_map_private_resource(
+				dev->mem_resource[i].addr, 0,
+				(size_t)dev->mem_resource[i].len);
+		if (map_address == MAP_FAILED)
+			goto error;
+		memset(map_address, 0xFF, (size_t)dev-
 > mem_resource[i].len);
+		dev->mem_resource[i].addr = map_address;
+	}
+
+	return 0;
+error:
+	return -1;
+}
+
  static struct mapped_pci_resource *
  pci_uio_find_resource(struct rte_pci_device *dev)
  {
drivers/bus/pci/private.h +12 -0
index 2283f09..10baa1a 100644
@@ -202,6 +202,18 @@ void pci_uio_free_resource(struct rte_pci_device
 *dev,
  		struct mapped_pci_resource *uio_res);
 
  /**
+ * remap the pci uio resource..
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
+/**
   * Map device memory to uio resource
   *

* This function is private to EAL.

drivers/bus/pci/rte_bus_pci.h +25 -0
index d4a2996..1662f3b 100644
@@ -52,6 +52,8 @@ extern "C" {
  #include <sys/queue.h>
  #include <stdint.h>
  #include <inttypes.h>
+#include <unistd.h>
+#include <fcntl.h>
 
  #include <rte_debug.h>
  #include <rte_interrupts.h>
@@ -197,6 +199,15 @@ int rte_pci_map_device(struct rte_pci_device *dev);
  void rte_pci_unmap_device(struct rte_pci_device *dev);
 
  /**
+ * Remap this device
+ *
+ * @param dev
+ *   A pointer to a rte_pci_device structure describing the device
+ *   to use
+ */
+int rte_pci_remap_device(struct rte_pci_device *dev);
+
+/**
   * Dump the content of the PCI bus.
   *
   * @param f
@@ -333,6 +344,20 @@ void rte_pci_ioport_read(struct rte_pci_ioport *p,
  void rte_pci_ioport_write(struct rte_pci_ioport *p,
  		const void *data, size_t len, off_t offset);
 
+/**
+ * It can be used to bind a device to a specific type of driver.
+ *
+ * @param dev_name
+ *  The device name.
+ * @param drv_type
+ *  The specific driver's type.
+ *
+ * @return
+ *  - On success, zero.
+ *  - On failure, a negative value.
+ */
+int rte_pci_dev_bind_driver(const char *dev_name, const char *drv_type);
+
  #ifdef __cplusplus
  }
  #endif
drivers/bus/vdev/vdev.c +33 -0
index fd7736d..773f6e0 100644
@@ -323,6 +323,39 @@ vdev_find_device(const struct rte_device *start,
 rte_dev_cmp_t cmp,
  	return NULL;
  }
 
+static struct rte_device *
+vdev_find_device_by_name(const struct rte_device *start,
+		rte_dev_cmp_name_t cmp_name,
+		const void *data)
+{
+	struct rte_vdev_device *dev;
+
+	TAILQ_FOREACH(dev, &vdev_device_list, next) {
+		if (start && &dev->device == start) {
+			start = NULL;
+			continue;
+		}
+		if (cmp_name(dev->device.name, data) == 0)
+			return &dev->device;
+	}
+	return NULL;
+}
+
+static int
+vdev_remap_device(struct rte_device *dev)
+{
+	RTE_SET_USED(dev);
+	return 0;
+}
+
+static int
+vdev_bind_driver(const char *dev_name, const char *drv_type)
+{
+	RTE_SET_USED(dev_name);
+	RTE_SET_USED(drv_type);
+	return 0;
+}
+
  static int
  vdev_plug(struct rte_device *dev)
  {
drivers/bus/vdev/vdev.c +3 -0
@@ -339,9 +372,12 @@ static struct rte_bus rte_vdev_bus = {
  	.scan = vdev_scan,
  	.probe = vdev_probe,
  	.find_device = vdev_find_device,
+	.find_device_by_name = vdev_find_device_by_name,
  	.plug = vdev_plug,
  	.unplug = vdev_unplug,
  	.parse = vdev_parse,
+	.remap_device = vdev_remap_device,
+	.bind_driver = vdev_bind_driver,
  };
 
  RTE_REGISTER_BUS(vdev, rte_vdev_bus);
lib/librte_eal/bsdapp/eal/eal_dev.c new file +19 -0
new file mode 100644
index 0000000..6ea9a74
@@ -0,0 +1,64 @@
+/*-
+ *   Copyright(c) 2010-2017 Intel Corporation.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/bsdapp/eal/eal_dev.c new file +34 -0
+
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <sys/queue.h>
+#include <sys/signalfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdbool.h>
+
+#include <rte_malloc.h>
+#include <rte_bus.h>
+#include <rte_dev.h>
+#include <rte_devargs.h>
+#include <rte_debug.h>
+#include <rte_log.h>
+
+#include "eal_thread.h"
+
+int
+rte_dev_monitor_start(void)
+{
+	return -1;
+}
+
+int
+rte_dev_monitor_stop(void)
+{
+	return -1;
+}
lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +21 -0
new file mode 100644
index 0000000..6a6feb5
@@ -0,0 +1,106 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +38 -0
+
+#ifndef _RTE_DEV_H_
+#error "don't include this file directly, please include generic <rte_dev.h>"
+#endif
+
+#ifndef _RTE_LINUXAPP_DEV_H_
+#define _RTE_LINUXAPP_DEV_H_
+
+#include <stdio.h>
+
+#include <rte_dev.h>
+
+#define RTE_EAL_UEV_MSG_LEN 4096
+#define RTE_EAL_UEV_MSG_ELEM_LEN 128
+
+enum uev_subsystem {
+	UEV_SUBSYSTEM_UIO,
+	UEV_SUBSYSTEM_VFIO,
+	UEV_SUBSYSTEM_PCI,
+	UEV_SUBSYSTEM_MAX
+};
+
+enum uev_monitor_netlink_group {
+	UEV_MONITOR_KERNEL,
+	UEV_MONITOR_UDEV,
+};
+
+/**
+ * The device event type.
+ */
+enum rte_eal_dev_event_type {
+	RTE_EAL_DEV_EVENT_UNKNOWN,	/**< unknown event type */
+	RTE_EAL_DEV_EVENT_ADD,		/**< device adding event */
+	RTE_EAL_DEV_EVENT_REMOVE,
+					/**< device removing event */
+	RTE_EAL_DEV_EVENT_CHANGE,
+					/**< device status change event */
+	RTE_EAL_DEV_EVENT_MOVE,		/**< device sys path move

event */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +3 -0
+	RTE_EAL_DEV_EVENT_ONLINE,	/**< device online event */
+	RTE_EAL_DEV_EVENT_OFFLINE,	/**< device offline event */
+	RTE_EAL_DEV_EVENT_MAX		/**< max value of this enum

*/

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +7 -0
+};
+
+struct rte_eal_uevent {
+	enum rte_eal_dev_event_type type;	/**< device event type */
+	int subsystem;				/**< subsystem id */
+	char *devname;				/**< device name */
+	enum uev_monitor_netlink_group group;	/**< device netlink

group */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +26 -0
+};
+
+/**
+ * Start the device uevent monitoring.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_monitor_start(void);
+
+/**
+ * Stop the device uevent monitoring .
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+
+int
+rte_dev_monitor_stop(void);
+
+#endif /* _RTE_LINUXAPP_DEV_H_ */
lib/librte_eal/common/eal_common_bus.c +11 -0
index 3e022d5..b7219c9 100644
@@ -51,8 +51,11 @@ rte_bus_register(struct rte_bus *bus)
  	RTE_VERIFY(bus->scan);
  	RTE_VERIFY(bus->probe);
  	RTE_VERIFY(bus->find_device);
+	RTE_VERIFY(bus->find_device_by_name);
  	/* Buses supporting driver plug also require unplug. */
  	RTE_VERIFY(!bus->plug || bus->unplug);
+	RTE_VERIFY(bus->remap_device);
+	RTE_VERIFY(bus->bind_driver);
 
  	TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
  	RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
@@ -170,6 +173,14 @@ cmp_rte_device(const struct rte_device *dev1,
 const void *_dev2)
  }
 
  static int
+cmp_rte_device_name(const char *dev_name1, const void *_dev_name2)
+{
+	const char *dev_name2 = _dev_name2;
+
+	return strcmp(dev_name1, dev_name2);
+}
+
+static int
  bus_find_device(const struct rte_bus *bus, const void *_dev)
  {

struct rte_device *dev;

lib/librte_eal/common/eal_common_bus.c +19 -0
@@ -178,6 +189,25 @@ bus_find_device(const struct rte_bus *bus, const
 void *_dev)
  	return dev == NULL;
  }
 
+static struct rte_device *
+bus_find_device_by_name(const struct rte_bus *bus, const void
 *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus->find_device_by_name(NULL, cmp_rte_device_name,
 _dev_name);
+	return dev;
+}
+
+struct rte_device *
+
+rte_bus_find_device(const struct rte_bus *bus, const void *_dev_name)
+{
+	struct rte_device *dev;
+
+	dev = bus_find_device_by_name(bus, _dev_name);
+	return dev;
+}
+
  struct rte_bus *
  rte_bus_find_by_device(const struct rte_device *dev)
  {
lib/librte_eal/common/eal_common_dev.c +34 -0
index dda8f58..47909e8 100644
@@ -42,9 +42,31 @@
  #include <rte_devargs.h>
  #include <rte_debug.h>
  #include <rte_log.h>
+#include <rte_spinlock.h>
+#include <rte_malloc.h>
 
  #include "eal_private.h"
 
+/* spinlock for device callbacks */
+static rte_spinlock_t rte_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
+
+/**
+ * The user application callback description.
+ *
+ * It contains callback address to be registered by user application,
+ * the pointer to the parameters for callback, and the event type.
+ */
+struct rte_eal_dev_callback {
+	TAILQ_ENTRY(rte_eal_dev_callback) next; /**< Callbacks list */
+	rte_eal_dev_cb_fn cb_fn;                /**< Callback address */
+	void *cb_arg;                           /**< Parameter for callback */
+	void *ret_param;                        /**< Return parameter */
+	enum rte_eal_dev_event_type event;      /**< device event type */
+	uint32_t active;                        /**< Callback is executing */
+};
+
+static struct rte_eal_dev_callback *dev_add_cb;
+
  static int cmp_detached_dev_name(const struct rte_device *dev,
  	const void *_name)
  {
@@ -234,3 +256,150 @@ int rte_eal_hotplug_remove(const char *busname,
 const char *devname)
  	rte_eal_devargs_remove(busname, devname);
  	return ret;
  }
+
+int
+rte_eal_dev_monitor_enable(void)
+{
+	int ret;
+
+	ret = rte_dev_monitor_start();
+	if (ret)
+		RTE_LOG(ERR, EAL, "Can not init device monitor\n");
+	return ret;
+}
+

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, · line 280

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+int
+rte_dev_callback_register(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg)
+{
+	struct rte_eal_dev_callback *user_cb;
+
+	if (!cb_fn)
+		return -EINVAL;
+

What's about checking the device pointer is not NULL ?

Quoted 14 lines
lib/librte_eal/common/eal_common_dev.c +14 -0
+	rte_spinlock_lock(&rte_dev_cb_lock);
+
+	if (TAILQ_EMPTY(&(device->uev_cbs)))
+		TAILQ_INIT(&(device->uev_cbs));
+
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		user_cb = NULL;
+	} else {
+		TAILQ_FOREACH(user_cb, &(device->uev_cbs), next) {
+			if (user_cb->cb_fn == cb_fn &&
+				user_cb->cb_arg == cb_arg &&
+				user_cb->event == event) {
+				break;
+			}

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 304

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+		}
+	}
+
+	/* create a new callback. */
+	if (user_cb == NULL) {
+		/* allocate a new interrupt callback entity */
+		user_cb = rte_zmalloc("eal device event",
+					sizeof(*user_cb), 0);
+		if (user_cb == NULL) {
+			RTE_LOG(ERR, EAL, "Can not allocate memory\n");

Missing rte_spinlock_unlock.

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 311

Quoted 7 lines
lib/librte_eal/common/eal_common_dev.c +7 -0
+			return -ENOMEM;
+		}
+		user_cb->cb_fn = cb_fn;
+		user_cb->cb_arg = cb_arg;
+		user_cb->event = event;
+		if (event == RTE_EAL_DEV_EVENT_ADD)
+			dev_add_cb = user_cb;

Only one dpdk entity can register to ADD callback?

I suggest to add option to register all devices maybe by using dummy device which will include all the "ALL_DEVICES" callbacks per event. All means past, present and future devices, by this way 1 callback can be called for all the devices and more than one dpdk entity could register to an ADD\NEW event. What's about NEW instead of ADD?

I also suggest to add the device pointer as a parameter to the callback(which will be managed by EAL).

Quoted 17 lines
lib/librte_eal/common/eal_common_dev.c +2 -0
+		else
+			TAILQ_INSERT_TAIL(&(device->uev_cbs), user_cb,

next);

lib/librte_eal/common/eal_common_dev.c +14 -0
+	}
+
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return 0;
+}
+
+int
+rte_dev_callback_unregister(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg)
+{
+	int ret;
+	struct rte_eal_dev_callback *cb, *next;
+

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 337

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+	if (!cb_fn)
+		return -EINVAL;
+
+	rte_spinlock_lock(&rte_dev_cb_lock);
+
+	ret = 0;
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		rte_free(dev_add_cb);
+		dev_add_cb = NULL;
+	} else {

Device NULL checking?

Quoted 35 lines
lib/librte_eal/common/eal_common_dev.c +16 -0
+		for (cb = TAILQ_FIRST(&(device->uev_cbs)); cb != NULL;
+		      cb = next) {
+
+			next = TAILQ_NEXT(cb, next);
+
+			if (cb->cb_fn != cb_fn || cb->event != event ||
+					(cb->cb_arg != (void *)-1 &&
+					cb->cb_arg != cb_arg))
+				continue;
+
+			/*
+			 * if this callback is not executing right now,
+			 * then remove it.
+			 */
+			if (cb->active == 0) {
+				TAILQ_REMOVE(&(device->uev_cbs), cb,

next);

lib/librte_eal/common/eal_common_dev.c +18 -0
+				rte_free(cb);
+			} else {
+				ret = -EAGAIN;
+			}
+		}
+	}
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return ret;
+}
+
+int
+_rte_dev_callback_process(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			void *cb_arg, void *ret_param)
+{
+	struct rte_eal_dev_callback dev_cb;
+	struct rte_eal_dev_callback *cb_lst;
+	int rc = 0;

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 381

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+
+	rte_spinlock_lock(&rte_dev_cb_lock);
+	if (event == RTE_EAL_DEV_EVENT_ADD) {
+		if (cb_arg != NULL)
+			dev_add_cb->cb_arg = cb_arg;
+
+		if (ret_param != NULL)
+			dev_add_cb->ret_param = ret_param;
+
+		rte_spinlock_unlock(&rte_dev_cb_lock);

Can't someone free it when it running?
I suggest to keep the lock locked.
Callbacks are not allowed to use this mechanism to prevent deadlock.

Quoted 6 lines
lib/librte_eal/common/eal_common_dev.c +2 -0
+		rc = dev_add_cb->cb_fn(dev_add_cb->event,
+				dev_add_cb->cb_arg, dev_add_cb-
Quoted 1 line

ret_param);

lib/librte_eal/common/eal_common_dev.c +3 -0
+		rte_spinlock_lock(&rte_dev_cb_lock);
+	} else {
+		TAILQ_FOREACH(cb_lst, &(device->uev_cbs), next) {

lib/librte_eal/common/eal_common_dev.c int rte_eal_hotplug_remove(const char *busname, const char *devname) · line 396

Quoted 10 lines
lib/librte_eal/common/eal_common_dev.c +10 -0
+			if (cb_lst->cb_fn == NULL || cb_lst->event != event)
+				continue;
+			dev_cb = *cb_lst;
+			cb_lst->active = 1;
+			if (cb_arg != NULL)
+				dev_cb.cb_arg = cb_arg;
+			if (ret_param != NULL)
+				dev_cb.ret_param = ret_param;
+
+			rte_spinlock_unlock(&rte_dev_cb_lock);

The current active flag doesn't do it thread safe here, I suggest to keep the lock locked. Scenario:
1. Thread A see active = 0 in unregister function.
2. Context switch.
3. Thread B start the callback.
4. Context switch.
5. Thread A free it.
6. Context switch.
7. Seg fault in Thread B.

Quoted 551 lines
lib/librte_eal/common/eal_common_dev.c +9 -0
+			rc = dev_cb.cb_fn(dev_cb.event,
+					dev_cb.cb_arg, dev_cb.ret_param);
+			rte_spinlock_lock(&rte_dev_cb_lock);
+			cb_lst->active = 0;
+		}
+	}
+	rte_spinlock_unlock(&rte_dev_cb_lock);
+	return rc;
+}
lib/librte_eal/common/include/rte_bus.h +7 -0
index 6fb0834..6c4ae31 100644
@@ -122,6 +122,34 @@ typedef struct rte_device *
  			 const void *data);
 
  /**
+ * Device iterator to find a device on a bus.
+ *
+ * This function returns an rte_device if one of those held by the bus
+ * matches the data passed as parameter.
+ *
+ * If the comparison function returns zero this function should stop iterating
+ * over any more devices. To continue a search the device of a previous

search
+ * can be passed via the start parameter.
+ *
+ * @param cmp
+ * the device name comparison function.
+ *
+ * @param data
+ * Data to compare each device against.
+ *
+ * @param start
+ * starting point for the iteration
+ *
+ * @return
+ * The first device matching the data, NULL if none exists.
+ */

lib/librte_eal/common/include/rte_bus.h +8 -0
+typedef struct rte_device *
+(*rte_bus_find_device_by_name_t)(const struct rte_device *start,
+			 rte_dev_cmp_name_t cmp,
+			 const void *data);
+
+
+/**
   * Implementation specific probe function which is responsible for linking
   * devices on that bus with applicable drivers.
   *
@@ -168,6 +196,37 @@ typedef int (*rte_bus_unplug_t)(struct rte_device
 *dev);
  typedef int (*rte_bus_parse_t)(const char *name, void *addr);
 
  /**
+ * Implementation specific remap function which is responsible for

remmaping
+ * devices on that bus from original share memory resource to a private memory
+ * resource for the sake of device has been removal.
+ *
+ * @param dev
+ * Device pointer that was returned by a previous call to find_device.
+ *
+ * @return
+ * 0 on success.
+ * !0 on error.
+ */

lib/librte_eal/common/include/rte_bus.h +24 -0
+typedef int (*rte_bus_remap_device_t)(struct rte_device *dev);
+
+/**
+ * Implementation specific bind driver function which is responsible for bind
+ * a explicit type of driver with a devices on that bus.
+ *
+ * @param dev_name
+ *	device textual description.
+ *
+ * @param drv_type
+ *	driver type textual description.
+ *
+ * @return
+ *	0 on success.
+ *	!0 on error.
+ */
+typedef int (*rte_bus_bind_driver_t)(const char *dev_name,
+				const char *drv_type);
+
+/**
   * Bus scan policies
   */
  enum rte_bus_scan_mode {
@@ -206,9 +265,13 @@ struct rte_bus {
  	rte_bus_scan_t scan;         /**< Scan for devices attached to bus */
  	rte_bus_probe_t probe;       /**< Probe devices on bus */
  	rte_bus_find_device_t find_device; /**< Find a device on the bus */
+	rte_bus_find_device_by_name_t find_device_by_name;
+				     /**< Find a device on the bus */
  	rte_bus_plug_t plug;         /**< Probe single device for drivers */
  	rte_bus_unplug_t unplug;     /**< Remove single device from driver
 */
  	rte_bus_parse_t parse;       /**< Parse a device name */
+	rte_bus_remap_device_t remap_device;       /**< remap a device */
+	rte_bus_bind_driver_t bind_driver; /**< bind a driver for bus device
 */
  	struct rte_bus_conf conf;    /**< Bus configuration */

rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu
class */
};

lib/librte_eal/common/include/rte_bus.h +6 -0
@@ -306,6 +369,12 @@ struct rte_bus *rte_bus_find(const struct rte_bus
 *start, rte_bus_cmp_t cmp,
  struct rte_bus *rte_bus_find_by_device(const struct rte_device *dev);
 
  /**
+ * Find the registered bus for a particular device.
+ */
+struct rte_device *rte_bus_find_device(const struct rte_bus *bus,
+				const void *dev_name);
+
+/**
   * Find the registered bus for a given name.
   */
  struct rte_bus *rte_bus_find_by_name(const char *busname);
lib/librte_eal/common/include/rte_dev.h +21 -0
index 9342e0c..19971d0 100644
@@ -51,6 +51,15 @@ extern "C" {
 
  #include <rte_log.h>
 
+#include <exec-env/rte_dev.h>
+
+typedef int (*rte_eal_dev_cb_fn)(enum rte_eal_dev_event_type event,
+					void *cb_arg, void *ret_param);
+
+struct rte_eal_dev_callback;
+/** @internal Structure to keep track of registered callbacks */
+TAILQ_HEAD(rte_eal_dev_cb_list, rte_eal_dev_callback);
+
  __attribute__((format(printf, 2, 0)))
  static inline void
  rte_pmd_debug_trace(const char *func_name, const char *fmt, ...)
@@ -157,6 +166,13 @@ struct rte_driver {
   */
  #define RTE_DEV_NAME_MAX_LEN 64
 
+enum device_state {
+	DEVICE_UNDEFINED,
+	DEVICE_FAULT,
+	DEVICE_PARSED,
+	DEVICE_PROBED,
+};
+
  /**
   * A structure describing a generic device.
   */
@@ -166,6 +182,9 @@ struct rte_device {
  	const struct rte_driver *driver;/**< Associated driver */
  	int numa_node;                /**< NUMA node connection */
  	struct rte_devargs *devargs;  /**< Device user arguments */
+	enum device_state state;  /**< Device state */
+	/** User application callbacks for device event */
+	struct rte_eal_dev_cb_list uev_cbs;
  };
 
  /**
@@ -248,6 +267,8 @@ int rte_eal_hotplug_remove(const char *busname,
 const char *devname);
   */
  typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void
 *data);
 
+typedef int (*rte_dev_cmp_name_t)(const char *dev_name, const void
 *data);
+
  #define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]

  #define RTE_PMD_EXPORT_NAME(name, idx) \
lib/librte_eal/common/include/rte_dev.h +68 -0
@@ -293,4 +314,72 @@ __attribute__((used)) = str
  }
  #endif
 
+/**
+ * It enable the device event monitoring for a specific event.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_eal_dev_monitor_enable(void);
+/**
+ * It registers the callback for the specific event. Multiple
+ * callbacks cal be registered at the same time.
+ * @param event
+ *  The device event type.
+ * @param cb_fn
+ *  callback address.
+ * @param cb_arg
+ *  address of parameter for callback.
+ *
+ * @return
+ *  - On success, zero.
+ *  - On failure, a negative value.
+ */
+int rte_dev_callback_register(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg);
+
+/**
+ * It unregisters the callback according to the specified event.
+ *
+ * @param event
+ *  The event type which corresponding to the callback.
+ * @param cb_fn
+ *  callback address.
+ *  address of parameter for callback, (void *)-1 means to remove all
+ *  registered which has the same callback address.
+ *
+ * @return
+ *  - On success, return the number of callback entities removed.
+ *  - On failure, a negative value.
+ */
+int rte_dev_callback_unregister(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			rte_eal_dev_cb_fn cb_fn, void *cb_arg);
+
+/**
+ * @internal Executes all the user application registered callbacks for
+ * the specific device. It is for DPDK internal user only. User
+ * application should not call it directly.
+ *
+ * @param event
+ *  The device event type.
+ * @param cb_arg
+ *  callback parameter.
+ * @param ret_param
+ *  To pass data back to user application.
+ *  This allows the user application to decide if a particular function
+ *  is permitted or not.
+ *
+ * @return
+ *  - On success, return zero.
+ *  - On failure, a negative value.
+ */
+int
+_rte_dev_callback_process(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			void *cb_arg, void *ret_param);
  #endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/Makefile +0 -0
index 5a7b8b2..05a2437 100644
@@ -67,6 +67,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) +=

eal_lcore.c
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_interrupts.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_alarm.c

lib/librte_eal/linuxapp/eal/Makefile +2 -1
+SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_dev.c
 
  # from common dir
  SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_lcore.c
@@ -120,7 +121,7 @@ ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y)
  CFLAGS_eal_thread.o += -Wno-return-type
  endif
 
-INC := rte_kni_common.h
+INC := rte_kni_common.h rte_dev.h
 
  SYMLINK-$(CONFIG_RTE_EXEC_ENV_LINUXAPP)-include/exec-env := \
  	$(addprefix include/exec-env/,$(INC))
lib/librte_eal/linuxapp/eal/eal_alarm.c +1 -0
index 8e4a775..29e73a7 100644
@@ -209,6 +209,7 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn,
 void *cb_arg)
  	int count = 0;
  	int err = 0;
  	int executing;
+	int ret;
 
  	if (!cb_fn) {
  		rte_errno = EINVAL;
lib/librte_eal/linuxapp/eal/eal_alarm.c +4 -0
@@ -259,6 +260,10 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn,
 void *cb_arg)
  			}
  			ap_prev = ap;
  		}
+
+		ret |= rte_intr_callback_unregister(&intr_handle,
+				eal_alarm_callback, NULL);
+
  		rte_spinlock_unlock(&alarm_list_lk);
  	} while (executing != 0);
lib/librte_eal/linuxapp/eal/eal_dev.c new file +19 -0
new file mode 100644
index 0000000..49fd0dc
@@ -0,0 +1,356 @@
+/*-
+ *   Copyright(c) 2010-2017 Intel Corporation.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/linuxapp/eal/eal_dev.c new file +71 -0
+
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <sys/queue.h>
+#include <sys/signalfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdbool.h>
+
+#include <rte_malloc.h>
+#include <rte_bus.h>
+#include <rte_dev.h>
+#include <rte_devargs.h>
+#include <rte_debug.h>
+#include <rte_log.h>
+
+#include "eal_thread.h"
+
+/* uev monitoring thread */
+static pthread_t uev_monitor_thread;
+
+bool udev_exit = true;
+
+bool no_request_thread = true;
+
+static void sig_handler(int signum)
+{
+	if (signum == SIGINT || signum == SIGTERM)
+		rte_dev_monitor_stop();
+}
+
+static int
+dev_monitor_fd_new(void)
+{
+
+	int uevent_fd;
+
+	uevent_fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC |
+			SOCK_NONBLOCK,
+			NETLINK_KOBJECT_UEVENT);
+	if (uevent_fd < 0) {
+		RTE_LOG(ERR, EAL, "create uevent fd failed\n");
+		return -1;
+	}
+	return uevent_fd;
+}
+
+static int
+dev_monitor_enable(int netlink_fd)
+{
+	struct sockaddr_nl addr;
+	int ret;
+	int size = 64 * 1024;
+	int nonblock = 1;
+
+	memset(&addr, 0, sizeof(addr));
+	addr.nl_family = AF_NETLINK;
+	addr.nl_pid = 0;
+	addr.nl_groups = 0xffffffff;
+
+	if (bind(netlink_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		RTE_LOG(ERR, EAL, "bind failed\n");
+		goto err;
+	}
+
+	setsockopt(netlink_fd, SOL_SOCKET, SO_PASSCRED, &size,

sizeof(size));

lib/librte_eal/linuxapp/eal/eal_dev.c new file +53 -0
+
+	ret = ioctl(netlink_fd, FIONBIO, &nonblock);
+	if (ret != 0) {
+		RTE_LOG(ERR, EAL, "ioctl(FIONBIO) failed\n");
+		goto err;
+	}
+	return 0;
+err:
+	close(netlink_fd);
+	return -1;
+}
+
+static void
+dev_uev_parse(const char *buf, struct rte_eal_uevent *event)
+{
+	char action[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char subsystem[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char dev_path[RTE_EAL_UEV_MSG_ELEM_LEN];
+	char pci_slot_name[RTE_EAL_UEV_MSG_ELEM_LEN];
+	int i = 0;
+
+	memset(action, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(subsystem, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(dev_path, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+	memset(pci_slot_name, 0, RTE_EAL_UEV_MSG_ELEM_LEN);
+
+	while (i < RTE_EAL_UEV_MSG_LEN) {
+		for (; i < RTE_EAL_UEV_MSG_LEN; i++) {
+			if (*buf)
+				break;
+			buf++;
+		}
+		if (!strncmp(buf, "libudev", 7)) {
+			buf += 7;
+			i += 7;
+			event->group = UEV_MONITOR_UDEV;
+		}
+		if (!strncmp(buf, "ACTION=", 7)) {
+			buf += 7;
+			i += 7;
+			snprintf(action, sizeof(action), "%s", buf);
+		} else if (!strncmp(buf, "DEVPATH=", 8)) {
+			buf += 8;
+			i += 8;
+			snprintf(dev_path, sizeof(dev_path), "%s", buf);
+		} else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
+			buf += 10;
+			i += 10;
+			snprintf(subsystem, sizeof(subsystem), "%s", buf);
+		} else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
+			buf += 14;
+			i += 14;
+			snprintf(pci_slot_name, sizeof(subsystem), "%s",

buf);

lib/librte_eal/linuxapp/eal/eal_dev.c new file +67 -0
+			event->devname = pci_slot_name;
+		}
+		for (; i < RTE_EAL_UEV_MSG_LEN; i++) {
+			if (*buf == '\0')
+				break;
+			buf++;
+		}
+	}
+
+	if (!strncmp(subsystem, "pci", 3))
+		event->subsystem = UEV_SUBSYSTEM_PCI;
+	if (!strncmp(action, "add", 3))
+		event->type = RTE_EAL_DEV_EVENT_ADD;
+	if (!strncmp(action, "remove", 6))
+		event->type = RTE_EAL_DEV_EVENT_REMOVE;
+	event->devname = pci_slot_name;
+}
+
+static int
+dev_uev_receive(int fd, struct rte_eal_uevent *uevent)
+{
+	int ret;
+	char buf[RTE_EAL_UEV_MSG_LEN];
+
+	memset(uevent, 0, sizeof(struct rte_eal_uevent));
+	memset(buf, 0, RTE_EAL_UEV_MSG_LEN);
+
+	ret = recv(fd, buf, RTE_EAL_UEV_MSG_LEN - 1, MSG_DONTWAIT);
+	if (ret < 0) {
+		RTE_LOG(ERR, EAL,
+		"Socket read error(%d): %s\n",
+		errno, strerror(errno));
+		return -1;
+	} else if (ret == 0)
+		/* connection closed */
+		return -1;
+
+	dev_uev_parse(buf, uevent);
+
+	return 0;
+}
+
+static int
+dev_uev_process(struct epoll_event *events, int nfds)
+{
+	struct rte_bus *bus;
+	struct rte_device *dev;
+	struct rte_eal_uevent uevent;
+	int ret;
+	int i;
+
+	for (i = 0; i < nfds; i++) {
+		/**
+		 * check device uevent from kernel side, no need to check
+		 * uevent from udev.
+		 */
+		if ((dev_uev_receive(events[i].data.fd, &uevent)) ||
+			(uevent.group == UEV_MONITOR_UDEV))
+			return 0;
+
+		/* default handle all pci devcie when is being hot plug */
+		if (uevent.subsystem == UEV_SUBSYSTEM_PCI) {
+			bus = rte_bus_find_by_name("pci");
+			dev = rte_bus_find_device(bus, uevent.devname);
+			if (uevent.type == RTE_EAL_DEV_EVENT_REMOVE) {
+
+				if ((!dev) || dev->state ==

DEVICE_UNDEFINED)

lib/librte_eal/linuxapp/eal/eal_dev.c new file +8 -0
+					return 0;
+				dev->state = DEVICE_FAULT;
+
+				/**
+				 * remap the resource to be fake
+				 * before user's removal processing
+				 */
+				ret = bus->remap_device(dev);
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 233

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +2 -0
+				if (!ret)
+

return(_rte_dev_callback_process(dev,

lib/librte_eal/linuxapp/eal/eal_dev.c new file +2 -0
+					  RTE_EAL_DEV_EVENT_REMOVE,
+					  NULL, NULL));

What is the reason to keep this device in EAL device list after the removal?
I suggest to remove it (driver remove, bus remove and EAL remove) after the callbacks running. By this way EAL can initiate all device removals.

it will do device removal from the device list by the eal device detach function in the call backs running. does it fulfill your concerns.

Quoted 346 lines

lib/librte_eal/linuxapp/eal/eal_dev.c line 252

Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +1 -0
+			} else if (uevent.type == RTE_EAL_DEV_EVENT_ADD)

{

lib/librte_eal/linuxapp/eal/eal_dev.c new file +9 -0
+				if (dev == NULL) {
+					/**
+					 * bind the driver to the device
+					 * before user's add processing
+					 */
+					bus->bind_driver(
+						uevent.devname,
+						"igb_uio");
+

Similar comments here:
EAL can initiate all device probe operations by adding the device and probing it here before the callback running. Then, also the device pointer can be passed to the callbacks.

Quoted 86 lines

return(_rte_dev_callback_process(NULL,

lib/librte_eal/linuxapp/eal/eal_dev.c new file +80 -0
+					  RTE_EAL_DEV_EVENT_ADD,
+					  uevent.devname, NULL));
+				}
+			}
+		}
+	}
+	return 0;
+}
+
+/**
+ * It builds/rebuilds up the epoll file descriptor with all the
+ * file descriptors being waited on. Then handles the interrupts.
+ *
+ * @param arg
+ *  pointer. (unused)
+ *
+ * @return
+ *  never return;
+ */
+static __attribute__((noreturn)) void *
+dev_uev_monitoring(__rte_unused void *arg)
+{
+	struct sigaction act;
+	sigset_t mask;
+	int netlink_fd;
+	struct epoll_event ep_kernel;
+	int fd_ep;
+
+	udev_exit = false;
+
+	/* set signal handlers */
+	memset(&act, 0x00, sizeof(struct sigaction));
+	act.sa_handler = sig_handler;
+	sigemptyset(&act.sa_mask);
+	act.sa_flags = SA_RESTART;
+	sigaction(SIGINT, &act, NULL);
+	sigaction(SIGTERM, &act, NULL);
+	sigemptyset(&mask);
+	sigaddset(&mask, SIGINT);
+	sigaddset(&mask, SIGTERM);
+	sigprocmask(SIG_UNBLOCK, &mask, NULL);
+
+	fd_ep = epoll_create1(EPOLL_CLOEXEC);
+	if (fd_ep < 0) {
+		RTE_LOG(ERR, EAL, "error creating epoll fd: %m\n");
+		goto out;
+	}
+
+	netlink_fd = dev_monitor_fd_new();
+
+	if (dev_monitor_enable(netlink_fd) < 0) {
+		RTE_LOG(ERR, EAL, "error subscribing to kernel events\n");
+		goto out;
+	}
+
+	memset(&ep_kernel, 0, sizeof(struct epoll_event));
+	ep_kernel.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
+	ep_kernel.data.fd = netlink_fd;
+	if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, netlink_fd,
+		&ep_kernel) < 0) {
+		RTE_LOG(ERR, EAL, "error addding fd to epoll: %m\n");
+		goto out;
+	}
+
+	while (!udev_exit) {
+		int fdcount;
+		struct epoll_event ev[1];
+
+		fdcount = epoll_wait(fd_ep, ev, 1, -1);
+		if (fdcount < 0) {
+			if (errno != EINTR)
+				RTE_LOG(ERR, EAL, "error receiving uevent "
+					"message: %m\n");
+				continue;
+			}
+
+		/* epoll_wait has at least one fd ready to read */
+		if (dev_uev_process(ev, fdcount) < 0) {
+			if (errno != EINTR)
+				RTE_LOG(ERR, EAL, "error processing uevent

"

lib/librte_eal/linuxapp/eal/eal_dev.c new file +4 -0
+					"message: %m\n");
+		}
+	}
+out:

lib/librte_eal/linuxapp/eal/eal_dev.c line 337

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	if (fd_ep >= 0)
+		close(fd_ep);
+	if (netlink_fd >= 0)
+		close(netlink_fd);
+	rte_panic("uev monitoring fail\n");
+}
+
+int
+rte_dev_monitor_start(void)
+{

Maybe add option to run it also by new EAL command line parameter?

lib/librte_eal/linuxapp/eal/eal_dev.c line 346

Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +9 -0
+	int ret;
+
+	if (!no_request_thread)
+		return 0;
+	no_request_thread = false;
+
+	/* create the host thread to wait/handle the uevent from kernel */
+	ret = pthread_create(&uev_monitor_thread, NULL,
+		dev_uev_monitoring, NULL);

What is the reason to open new thread for hotplug?
Why not to use the current dpdk host thread by the alarm mechanism?

Quoted 221 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +10 -0
+	return ret;
+}
+
+int
+rte_dev_monitor_stop(void)
+{
+	udev_exit = true;
+	no_request_thread = true;
+	return 0;
+}
lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +21 -0
new file mode 100644
index 0000000..6a6feb5
@@ -0,0 +1,106 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND

CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
+ */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +38 -0
+
+#ifndef _RTE_DEV_H_
+#error "don't include this file directly, please include generic <rte_dev.h>"
+#endif
+
+#ifndef _RTE_LINUXAPP_DEV_H_
+#define _RTE_LINUXAPP_DEV_H_
+
+#include <stdio.h>
+
+#include <rte_dev.h>
+
+#define RTE_EAL_UEV_MSG_LEN 4096
+#define RTE_EAL_UEV_MSG_ELEM_LEN 128
+
+enum uev_subsystem {
+	UEV_SUBSYSTEM_UIO,
+	UEV_SUBSYSTEM_VFIO,
+	UEV_SUBSYSTEM_PCI,
+	UEV_SUBSYSTEM_MAX
+};
+
+enum uev_monitor_netlink_group {
+	UEV_MONITOR_KERNEL,
+	UEV_MONITOR_UDEV,
+};
+
+/**
+ * The device event type.
+ */
+enum rte_eal_dev_event_type {
+	RTE_EAL_DEV_EVENT_UNKNOWN,	/**< unknown event type */
+	RTE_EAL_DEV_EVENT_ADD,		/**< device adding event */
+	RTE_EAL_DEV_EVENT_REMOVE,
+					/**< device removing event */
+	RTE_EAL_DEV_EVENT_CHANGE,
+					/**< device status change event */
+	RTE_EAL_DEV_EVENT_MOVE,		/**< device sys path move

event */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +3 -0
+	RTE_EAL_DEV_EVENT_ONLINE,	/**< device online event */
+	RTE_EAL_DEV_EVENT_OFFLINE,	/**< device offline event */
+	RTE_EAL_DEV_EVENT_MAX		/**< max value of this enum

*/

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +7 -0
+};
+
+struct rte_eal_uevent {
+	enum rte_eal_dev_event_type type;	/**< device event type */
+	int subsystem;				/**< subsystem id */
+	char *devname;				/**< device name */
+	enum uev_monitor_netlink_group group;	/**< device netlink

group */

lib/librte_eal/linuxapp/eal/include/exec-env/rte_dev.h new file +26 -0
+};
+
+/**
+ * Start the device uevent monitoring.
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+rte_dev_monitor_start(void);
+
+/**
+ * Stop the device uevent monitoring .
+ *
+ * @param none
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+
+int
+rte_dev_monitor_stop(void);
+
+#endif /* _RTE_LINUXAPP_DEV_H_ */
lib/librte_eal/linuxapp/igb_uio/igb_uio.c +6 -0
index a3a98c1..d0e07b4 100644
@@ -354,6 +354,12 @@ igbuio_pci_release(struct uio_info *info, struct
 inode *inode)
  	struct rte_uio_pci_dev *udev = info->priv;
  	struct pci_dev *dev = udev->pdev;
 
+	/* check if device have been remove before release */
+	if ((&dev->dev.kobj)->state_remove_uevent_sent == 1) {
+		pr_info("The device have been removed\n");
+		return -1;
+	}
+
  	/* disable interrupts */
  	igbuio_pci_disable_interrupts(udev);
lib/librte_pci/rte_pci.c +20 -1
index 0160fc1..feb5fd7 100644
@@ -172,6 +172,26 @@ rte_pci_addr_parse(const char *str, struct
 rte_pci_addr *addr)
  	return -1;
  }
 
+/* map a private resource from an address*/
+void *
+pci_map_private_resource(void *requested_addr, off_t offset, size_t size)
+{
+	void *mapaddr;
+
+	mapaddr = mmap(requested_addr, size,
+			   PROT_READ | PROT_WRITE,
+			   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
-1, 0);
+	if (mapaddr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "%s(): cannot mmap(%p, 0x%lx, 0x%lx): "
+			"%s (%p)\n",
+			__func__, requested_addr,
+			(unsigned long)size, (unsigned long)offset,
+			strerror(errno), mapaddr);
+	} else
+		RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n",
 mapaddr);
+
+	return mapaddr;
+}
 
  /* map a particular resource from a file */
  void *
lib/librte_pci/rte_pci.h +17 -0
index 4f2cd18..f6091a6 100644
@@ -227,6 +227,23 @@ int rte_pci_addr_cmp(const struct rte_pci_addr
 *addr,
  int rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr);
 
  /**
+ * @internal
+ * Map to a particular private resource.
+ *
+ * @param requested_addr
+ *      The starting address for the new mapping range.
+ * @param offset
+ *      The offset for the mapping range.
+ * @param size
+ *      The size for the mapping range.
+ * @return
+ *   - On success, the function returns a pointer to the mapped area.
+ *   - On error, the value MAP_FAILED is returned.
+ */
+void *pci_map_private_resource(void *requested_addr, off_t offset,
+		size_t size);
+
+/**
   * Map a particular resource from a file.
   *

* @param requested_addr
--
2.7.4

#313 lore raw

Re: [PATCH v7 1/2] eal: add uevent monitor for hot plug

In reply to Jeff Guo

Hi Jeff,

I am surprised that there is not a lot of review of these very important patches. Maybe it is not easy to review. Let's try to progress in the following days.

This patch is really too big with a lot of concepts spread in separate files, so it is difficult to properly review. Please, try to split in several patches, bringing only one concept per patch.

At first, you can introduce the new events and the callback API. The second patch (and the most important one) would be to bring the uevent parsing for Linux (and void implementation for BSD). Then you can add and explain some patches around PCI mapping.

At last there is the kernel binding effort - this one will probably be ignored for 18.02, because it is another huge topic. Without bothering with kernel binding, we can at least remove a device, get a notification, and eventually re-add it. It is a good first step. Anyway your testpmd patch tests exactly this scenario (totally new devices are not seen).

More comments below:

03/01/2018 02:42, Jeff Guo:

lib/librte_eal/bsdapp/eal/eal_dev.c line 7

Quoted 10 lines
lib/librte_eal/bsdapp/eal/eal_dev.c new file +7 -0
@@ -0,0 +1,64 @@
+/*-
+ *   Copyright(c) 2010-2017 Intel Corporation.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:

Please check how Intel Copyright and BSD license is newly formatted with SPDX tag.

Quoted 4 lines
lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +2 -0
+enum rte_eal_dev_event_type {
+	RTE_EAL_DEV_EVENT_UNKNOWN,	/**< unknown event type */

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h line 74

Quoted 10 lines
lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +10 -0
+	RTE_EAL_DEV_EVENT_ADD,		/**< device adding event */
+	RTE_EAL_DEV_EVENT_REMOVE,
+					/**< device removing event */
+	RTE_EAL_DEV_EVENT_CHANGE,
+					/**< device status change event */
+	RTE_EAL_DEV_EVENT_MOVE,		/**< device sys path move event */
+	RTE_EAL_DEV_EVENT_ONLINE,	/**< device online event */
+	RTE_EAL_DEV_EVENT_OFFLINE,	/**< device offline event */
+	RTE_EAL_DEV_EVENT_MAX		/**< max value of this enum */
+};

The comments are not useful.
Please better explain what is change, move, online, etc.

The shorter prefix RTE_DEV is preferred over RTE_EAL_DEV.

This file is full of definitions which must be common, not specific to BSD or Linux. Please move it.

lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h

Quoted 4 lines
lib/librte_eal/bsdapp/eal/include/exec-env/rte_dev.h new file +4 -0
+int
+_rte_dev_callback_process(struct rte_device *device,
+			enum rte_eal_dev_event_type event,
+			void *cb_arg, void *ret_param)

cb_arg must be an opaque parameter which is registered with the callback and passed later. No need as parameter of this function.

ret_param is not needed at all. The kernel event will be just translated as rte_eal_dev_event_type (rte_dev_event after rename).

Quoted 18 lines
lib/librte_eal/common/include/rte_bus.h +15 -0
 /**
+ * Device iterator to find a device on a bus.
+ *
+ * This function returns an rte_device if one of those held by the bus
+ * matches the data passed as parameter.
+ *
+ * If the comparison function returns zero this function should stop iterating
+ * over any more devices. To continue a search the device of a previous search
+ * can be passed via the start parameter.
+ *
+ * @param cmp
+ *	the device name comparison function.
+ *
+ * @param data
+ *	Data to compare each device against.
+ *

lib/librte_eal/common/include/rte_bus.h typedef struct rte_device * · line 149

Quoted 10 lines
lib/librte_eal/common/include/rte_bus.h +10 -0
+ * @param start
+ *	starting point for the iteration
+ *
+ * @return
+ *	The first device matching the data, NULL if none exists.
+ */
+typedef struct rte_device *
+(*rte_bus_find_device_by_name_t)(const struct rte_device *start,
+			 rte_dev_cmp_name_t cmp,
+			 const void *data);

Why is it needed? There is already rte_bus_find_device_t.

lib/librte_eal/common/include/rte_bus.h typedef int (*rte_bus_unplug_t)(struct rte_device *dev); · line 210

Quoted 13 lines
lib/librte_eal/common/include/rte_bus.h +13 -0
+/**
+ * Implementation specific remap function which is responsible for remmaping
+ * devices on that bus from original share memory resource to a private memory
+ * resource for the sake of device has been removal.
+ *
+ * @param dev
+ *	Device pointer that was returned by a previous call to find_device.
+ *
+ * @return
+ *	0 on success.
+ *	!0 on error.
+ */
+typedef int (*rte_bus_remap_device_t)(struct rte_device *dev);

You need to better explain why this remap op is needed, and when it is called exactly?

Quoted 4 lines
lib/librte_eal/common/include/rte_bus.h +0 -0
@@ -206,9 +265,13 @@ struct rte_bus {
 	rte_bus_scan_t scan;         /**< Scan for devices attached to bus */
 	rte_bus_probe_t probe;       /**< Probe devices on bus */
 	rte_bus_find_device_t find_device; /**< Find a device on the bus */

lib/librte_eal/common/include/rte_bus.h struct rte_bus { · line 277

Quoted 10 lines
lib/librte_eal/common/include/rte_bus.h +4 -0
+	rte_bus_find_device_by_name_t find_device_by_name;
+				     /**< Find a device on the bus */
 	rte_bus_plug_t plug;         /**< Probe single device for drivers */
 	rte_bus_unplug_t unplug;     /**< Remove single device from driver */
 	rte_bus_parse_t parse;       /**< Parse a device name */
+	rte_bus_remap_device_t remap_device;       /**< remap a device */
+	rte_bus_bind_driver_t bind_driver; /**< bind a driver for bus device */
 	struct rte_bus_conf conf;    /**< Bus configuration */
 	rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu class */
 };

Every new op must be introduced in a separate patch (if not completely removed).

lib/librte_eal/common/include/rte_dev.h struct rte_driver { · line 174

Quoted 8 lines
lib/librte_eal/common/include/rte_dev.h +6 -0
+enum device_state {
+	DEVICE_UNDEFINED,
+	DEVICE_FAULT,
+	DEVICE_PARSED,
+	DEVICE_PROBED,
+};

These constants must prefixed with RTE_
and documented with doxygen please.

Quoted 2 lines

+/**
+ * It enable the device event monitoring for a specific event.

This comment must be reworded.

Quoted 2 lines

+ *
+ * @param none

useless

lib/librte_eal/common/include/rte_dev.h __attribute__((used)) = str · line 326

Quoted 6 lines

+ * @return
+ * - On success, zero.
+ * - On failure, a negative value.
+ */

lib/librte_eal/common/include/rte_dev.h +2 -0
+int
+rte_eal_dev_monitor_enable(void);

I suggest to drop this function which is just calling rte_dev_monitor_start.

lib/librte_eal/linuxapp/eal/eal_alarm.c rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg) · line 268

Quoted 12 lines
lib/librte_eal/linuxapp/eal/eal_alarm.c +4 -0
@@ -259,6 +260,10 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 			}
 			ap_prev = ap;
 		}
+
+		ret |= rte_intr_callback_unregister(&intr_handle,
+				eal_alarm_callback, NULL);
+
 		rte_spinlock_unlock(&alarm_list_lk);
 	} while (executing != 0);

Looks to be unrelated.
If it is a fix, please do a separate patch.

lib/librte_eal/linuxapp/eal/eal_dev.c

Quoted 4 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +2 -0
+int
+rte_dev_monitor_start(void)

What about adding "event" in the name of this function?
rte_dev_event_monitor_start

lib/librte_eal/linuxapp/eal/eal_dev.c line 348

Quoted 12 lines
lib/librte_eal/linuxapp/eal/eal_dev.c new file +12 -0
+{
+	int ret;
+
+	if (!no_request_thread)
+		return 0;
+	no_request_thread = false;
+
+	/* create the host thread to wait/handle the uevent from kernel */
+	ret = pthread_create(&uev_monitor_thread, NULL,
+		dev_uev_monitoring, NULL);
+	return ret;
+}

I think you should use rte_service for thread management.

lib/librte_eal/linuxapp/igb_uio/igb_uio.c igbuio_pci_release(struct uio_info *info, struct inode *inode) · line 361

Quoted 11 lines
lib/librte_eal/linuxapp/igb_uio/igb_uio.c +5 -0
@@ -354,6 +354,12 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)
 	struct rte_uio_pci_dev *udev = info->priv;
 	struct pci_dev *dev = udev->pdev;
 
+	/* check if device have been remove before release */
+	if ((&dev->dev.kobj)->state_remove_uevent_sent == 1) {
+		pr_info("The device have been removed\n");
+		return -1;
+	}

This looks to be unrelated. Separate patch please.

End of first pass review. There are some basic requirements that other maintainers (especially at Intel) could have reviewed earlier. Let's try to improve it quickly for 18.02, thanks.
If we are short in time, we should at least focus on adding the events/callback API and the Linux events implementation.