1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <sys/socket.h>
10 #include <linux/netlink.h>
12 #include <rte_string_fns.h>
14 #include <rte_compat.h>
16 #include <rte_malloc.h>
17 #include <rte_interrupts.h>
18 #include <rte_alarm.h>
21 #include <rte_spinlock.h>
22 #include <rte_errno.h>
24 #include "eal_private.h"
26 static struct rte_intr_handle intr_handle = {
27 .type = RTE_INTR_HANDLE_DEV_EVENT,
30 static rte_rwlock_t monitor_lock = RTE_RWLOCK_INITIALIZER;
31 static uint32_t monitor_refcount;
32 static bool hotplug_handle;
34 #define EAL_UEV_MSG_LEN 4096
35 #define EAL_UEV_MSG_ELEM_LEN 128
38 * spinlock for device hot-unplug failure handling. If it try to access bus or
39 * device, such as handle sigbus on bus or handle memory failure for device
40 * just need to use this lock. It could protect the bus and the device to avoid
43 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
45 static struct sigaction sigbus_action_old;
47 static int sigbus_need_recover;
49 static void dev_uev_handler(__rte_unused void *param);
51 /* identify the system layer which reports this event. */
52 enum eal_dev_event_subsystem {
53 EAL_DEV_EVENT_SUBSYSTEM_PCI, /* PCI bus device event */
54 EAL_DEV_EVENT_SUBSYSTEM_UIO, /* UIO driver device event */
55 EAL_DEV_EVENT_SUBSYSTEM_VFIO, /* VFIO driver device event */
56 EAL_DEV_EVENT_SUBSYSTEM_MAX
60 sigbus_action_recover(void)
62 if (sigbus_need_recover) {
63 sigaction(SIGBUS, &sigbus_action_old, NULL);
64 sigbus_need_recover = 0;
68 static void sigbus_handler(int signum, siginfo_t *info,
69 void *ctx __rte_unused)
73 RTE_LOG(DEBUG, EAL, "Thread catch SIGBUS, fault address:%p\n",
76 rte_spinlock_lock(&failure_handle_lock);
77 ret = rte_bus_sigbus_handler(info->si_addr);
78 rte_spinlock_unlock(&failure_handle_lock);
80 rte_exit(EXIT_FAILURE,
81 "Failed to handle SIGBUS for hot-unplug, "
82 "(rte_errno: %s)!", strerror(rte_errno));
83 } else if (ret == 1) {
84 if (sigbus_action_old.sa_flags == SA_SIGINFO
85 && sigbus_action_old.sa_sigaction) {
86 (*(sigbus_action_old.sa_sigaction))(signum,
88 } else if (sigbus_action_old.sa_flags != SA_SIGINFO
89 && sigbus_action_old.sa_handler) {
90 (*(sigbus_action_old.sa_handler))(signum);
92 rte_exit(EXIT_FAILURE,
93 "Failed to handle generic SIGBUS!");
97 RTE_LOG(DEBUG, EAL, "Success to handle SIGBUS for hot-unplug!\n");
100 static int cmp_dev_name(const struct rte_device *dev,
103 const char *name = _name;
105 return strcmp(dev->name, name);
109 dev_uev_socket_fd_create(void)
111 struct sockaddr_nl addr;
114 intr_handle.fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC |
116 NETLINK_KOBJECT_UEVENT);
117 if (intr_handle.fd < 0) {
118 RTE_LOG(ERR, EAL, "create uevent fd failed.\n");
122 memset(&addr, 0, sizeof(addr));
123 addr.nl_family = AF_NETLINK;
125 addr.nl_groups = 0xffffffff;
127 ret = bind(intr_handle.fd, (struct sockaddr *) &addr, sizeof(addr));
129 RTE_LOG(ERR, EAL, "Failed to bind uevent socket.\n");
135 close(intr_handle.fd);
140 struct rte_dev_event {
141 enum rte_dev_event_type type; /**< device event type */
142 int subsystem; /**< subsystem id */
143 char *devname; /**< device name */
147 dev_uev_parse(const char *buf, struct rte_dev_event *event, int length)
149 char action[EAL_UEV_MSG_ELEM_LEN];
150 char subsystem[EAL_UEV_MSG_ELEM_LEN];
151 char pci_slot_name[EAL_UEV_MSG_ELEM_LEN];
154 memset(action, 0, EAL_UEV_MSG_ELEM_LEN);
155 memset(subsystem, 0, EAL_UEV_MSG_ELEM_LEN);
156 memset(pci_slot_name, 0, EAL_UEV_MSG_ELEM_LEN);
159 for (; i < length; i++) {
165 * check device uevent from kernel side, no need to check
168 if (!strncmp(buf, "libudev", 7)) {
173 if (!strncmp(buf, "ACTION=", 7)) {
176 strlcpy(action, buf, sizeof(action));
177 } else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
180 strlcpy(subsystem, buf, sizeof(subsystem));
181 } else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
184 strlcpy(pci_slot_name, buf, sizeof(subsystem));
185 event->devname = strdup(pci_slot_name);
187 for (; i < length; i++) {
194 /* parse the subsystem layer */
195 if (!strncmp(subsystem, "uio", 3))
196 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_UIO;
197 else if (!strncmp(subsystem, "pci", 3))
198 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_PCI;
199 else if (!strncmp(subsystem, "vfio", 4))
200 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_VFIO;
204 /* parse the action type */
205 if (!strncmp(action, "add", 3))
206 event->type = RTE_DEV_EVENT_ADD;
207 else if (!strncmp(action, "remove", 6))
208 event->type = RTE_DEV_EVENT_REMOVE;
213 free(event->devname);
218 dev_delayed_unregister(void *param)
220 rte_intr_callback_unregister(&intr_handle, dev_uev_handler, param);
221 close(intr_handle.fd);
226 dev_uev_handler(__rte_unused void *param)
228 struct rte_dev_event uevent;
230 char buf[EAL_UEV_MSG_LEN];
232 struct rte_device *dev;
233 const char *busname = "";
235 memset(&uevent, 0, sizeof(struct rte_dev_event));
236 memset(buf, 0, EAL_UEV_MSG_LEN);
238 ret = recv(intr_handle.fd, buf, EAL_UEV_MSG_LEN, MSG_DONTWAIT);
239 if (ret < 0 && errno == EAGAIN)
242 /* connection is closed or broken, can not up again. */
243 RTE_LOG(ERR, EAL, "uevent socket connection is broken.\n");
244 rte_eal_alarm_set(1, dev_delayed_unregister, NULL);
248 ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
250 RTE_LOG(DEBUG, EAL, "Ignoring uevent '%s'\n", buf);
254 RTE_LOG(DEBUG, EAL, "receive uevent(name:%s, type:%d, subsystem:%d)\n",
255 uevent.devname, uevent.type, uevent.subsystem);
257 switch (uevent.subsystem) {
258 case EAL_DEV_EVENT_SUBSYSTEM_PCI:
259 case EAL_DEV_EVENT_SUBSYSTEM_UIO:
266 if (uevent.devname) {
267 if (uevent.type == RTE_DEV_EVENT_REMOVE && hotplug_handle) {
268 rte_spinlock_lock(&failure_handle_lock);
269 bus = rte_bus_find_by_name(busname);
271 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n",
273 goto failure_handle_err;
276 dev = bus->find_device(NULL, cmp_dev_name,
279 RTE_LOG(ERR, EAL, "Cannot find device (%s) on "
280 "bus (%s)\n", uevent.devname, busname);
281 goto failure_handle_err;
284 ret = bus->hot_unplug_handler(dev);
286 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
287 "for device (%s)\n", dev->name);
289 rte_spinlock_unlock(&failure_handle_lock);
291 rte_dev_event_callback_process(uevent.devname, uevent.type);
292 free(uevent.devname);
298 rte_spinlock_unlock(&failure_handle_lock);
299 free(uevent.devname);
303 rte_dev_event_monitor_start(void)
307 rte_rwlock_write_lock(&monitor_lock);
309 if (monitor_refcount) {
314 ret = dev_uev_socket_fd_create();
316 RTE_LOG(ERR, EAL, "error create device event fd.\n");
320 ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL);
323 RTE_LOG(ERR, EAL, "fail to register uevent callback.\n");
324 close(intr_handle.fd);
332 rte_rwlock_write_unlock(&monitor_lock);
337 rte_dev_event_monitor_stop(void)
341 rte_rwlock_write_lock(&monitor_lock);
343 if (!monitor_refcount) {
344 RTE_LOG(ERR, EAL, "device event monitor already stopped\n");
348 if (monitor_refcount > 1) {
353 ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
356 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
360 close(intr_handle.fd);
366 rte_rwlock_write_unlock(&monitor_lock);
372 dev_sigbus_handler_register(void)
375 struct sigaction action;
379 if (sigbus_need_recover)
383 sigaddset(&mask, SIGBUS);
384 action.sa_flags = SA_SIGINFO;
385 action.sa_mask = mask;
386 action.sa_sigaction = sigbus_handler;
387 sigbus_need_recover = !sigaction(SIGBUS, &action, &sigbus_action_old);
393 dev_sigbus_handler_unregister(void)
397 sigbus_action_recover();
403 rte_dev_hotplug_handle_enable(void)
407 ret = dev_sigbus_handler_register();
410 "fail to register sigbus handler for devices.\n");
412 hotplug_handle = true;
418 rte_dev_hotplug_handle_disable(void)
422 ret = dev_sigbus_handler_unregister();
425 "fail to unregister sigbus handler for devices.\n");
427 hotplug_handle = false;