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 static rte_rwlock_t monitor_lock = RTE_RWLOCK_INITIALIZER;
28 static uint32_t monitor_refcount;
29 static bool hotplug_handle;
31 #define EAL_UEV_MSG_LEN 4096
32 #define EAL_UEV_MSG_ELEM_LEN 128
35 * spinlock for device hot-unplug failure handling. If it try to access bus or
36 * device, such as handle sigbus on bus or handle memory failure for device
37 * just need to use this lock. It could protect the bus and the device to avoid
40 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
42 static struct sigaction sigbus_action_old;
44 static int sigbus_need_recover;
46 static void dev_uev_handler(__rte_unused void *param);
48 /* identify the system layer which reports this event. */
49 enum eal_dev_event_subsystem {
50 EAL_DEV_EVENT_SUBSYSTEM_PCI, /* PCI bus device event */
51 EAL_DEV_EVENT_SUBSYSTEM_UIO, /* UIO driver device event */
52 EAL_DEV_EVENT_SUBSYSTEM_VFIO, /* VFIO driver device event */
53 EAL_DEV_EVENT_SUBSYSTEM_MAX
57 sigbus_action_recover(void)
59 if (sigbus_need_recover) {
60 sigaction(SIGBUS, &sigbus_action_old, NULL);
61 sigbus_need_recover = 0;
65 static void sigbus_handler(int signum, siginfo_t *info,
66 void *ctx __rte_unused)
70 RTE_LOG(DEBUG, EAL, "Thread catch SIGBUS, fault address:%p\n",
73 rte_spinlock_lock(&failure_handle_lock);
74 ret = rte_bus_sigbus_handler(info->si_addr);
75 rte_spinlock_unlock(&failure_handle_lock);
77 rte_exit(EXIT_FAILURE,
78 "Failed to handle SIGBUS for hot-unplug, "
79 "(rte_errno: %s)!", strerror(rte_errno));
80 } else if (ret == 1) {
81 if (sigbus_action_old.sa_flags == SA_SIGINFO
82 && sigbus_action_old.sa_sigaction) {
83 (*(sigbus_action_old.sa_sigaction))(signum,
85 } else if (sigbus_action_old.sa_flags != SA_SIGINFO
86 && sigbus_action_old.sa_handler) {
87 (*(sigbus_action_old.sa_handler))(signum);
89 rte_exit(EXIT_FAILURE,
90 "Failed to handle generic SIGBUS!");
94 RTE_LOG(DEBUG, EAL, "Success to handle SIGBUS for hot-unplug!\n");
97 static int cmp_dev_name(const struct rte_device *dev,
100 const char *name = _name;
102 return strcmp(dev->name, name);
106 dev_uev_socket_fd_create(void)
108 struct sockaddr_nl addr;
111 fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
112 NETLINK_KOBJECT_UEVENT);
114 RTE_LOG(ERR, EAL, "create uevent fd failed.\n");
118 memset(&addr, 0, sizeof(addr));
119 addr.nl_family = AF_NETLINK;
121 addr.nl_groups = 0xffffffff;
123 ret = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
125 RTE_LOG(ERR, EAL, "Failed to bind uevent socket.\n");
129 if (rte_intr_fd_set(intr_handle, fd))
139 struct rte_dev_event {
140 enum rte_dev_event_type type; /**< device event type */
141 int subsystem; /**< subsystem id */
142 char *devname; /**< device name */
146 dev_uev_parse(const char *buf, struct rte_dev_event *event, int length)
148 char action[EAL_UEV_MSG_ELEM_LEN];
149 char subsystem[EAL_UEV_MSG_ELEM_LEN];
150 char pci_slot_name[EAL_UEV_MSG_ELEM_LEN];
153 memset(action, 0, EAL_UEV_MSG_ELEM_LEN);
154 memset(subsystem, 0, EAL_UEV_MSG_ELEM_LEN);
155 memset(pci_slot_name, 0, EAL_UEV_MSG_ELEM_LEN);
158 for (; i < length; i++) {
167 * check device uevent from kernel side, no need to check
170 if (!strncmp(buf, "libudev", 7)) {
175 if (!strncmp(buf, "ACTION=", 7)) {
178 strlcpy(action, buf, sizeof(action));
179 } else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
182 strlcpy(subsystem, buf, sizeof(subsystem));
183 } else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
186 strlcpy(pci_slot_name, buf, sizeof(subsystem));
187 event->devname = strdup(pci_slot_name);
189 for (; i < length; i++) {
196 /* parse the subsystem layer */
197 if (!strncmp(subsystem, "uio", 3))
198 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_UIO;
199 else if (!strncmp(subsystem, "pci", 3))
200 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_PCI;
201 else if (!strncmp(subsystem, "vfio", 4))
202 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_VFIO;
206 /* parse the action type */
207 if (!strncmp(action, "add", 3))
208 event->type = RTE_DEV_EVENT_ADD;
209 else if (!strncmp(action, "remove", 6))
210 event->type = RTE_DEV_EVENT_REMOVE;
215 free(event->devname);
220 dev_delayed_unregister(void *param)
222 rte_intr_callback_unregister(intr_handle, dev_uev_handler, param);
223 if (rte_intr_fd_get(intr_handle) >= 0) {
224 close(rte_intr_fd_get(intr_handle));
225 rte_intr_fd_set(intr_handle, -1);
230 dev_uev_handler(__rte_unused void *param)
232 struct rte_dev_event uevent;
234 char buf[EAL_UEV_MSG_LEN];
236 struct rte_device *dev;
237 const char *busname = "";
239 memset(&uevent, 0, sizeof(struct rte_dev_event));
240 memset(buf, 0, EAL_UEV_MSG_LEN);
242 if (rte_intr_fd_get(intr_handle) < 0)
245 ret = recv(rte_intr_fd_get(intr_handle), buf, EAL_UEV_MSG_LEN,
247 if (ret < 0 && errno == EAGAIN)
250 /* connection is closed or broken, can not up again. */
251 RTE_LOG(ERR, EAL, "uevent socket connection is broken.\n");
252 rte_eal_alarm_set(1, dev_delayed_unregister, NULL);
256 ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
258 RTE_LOG(DEBUG, EAL, "Ignoring uevent '%s'\n", buf);
262 RTE_LOG(DEBUG, EAL, "receive uevent(name:%s, type:%d, subsystem:%d)\n",
263 uevent.devname, uevent.type, uevent.subsystem);
265 switch (uevent.subsystem) {
266 case EAL_DEV_EVENT_SUBSYSTEM_PCI:
267 case EAL_DEV_EVENT_SUBSYSTEM_UIO:
274 if (uevent.devname) {
275 if (uevent.type == RTE_DEV_EVENT_REMOVE && hotplug_handle) {
276 rte_spinlock_lock(&failure_handle_lock);
277 bus = rte_bus_find_by_name(busname);
279 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n",
281 goto failure_handle_err;
284 dev = bus->find_device(NULL, cmp_dev_name,
287 RTE_LOG(ERR, EAL, "Cannot find device (%s) on "
288 "bus (%s)\n", uevent.devname, busname);
289 goto failure_handle_err;
292 ret = bus->hot_unplug_handler(dev);
294 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
295 "for device (%s)\n", dev->name);
297 rte_spinlock_unlock(&failure_handle_lock);
299 rte_dev_event_callback_process(uevent.devname, uevent.type);
300 free(uevent.devname);
306 rte_spinlock_unlock(&failure_handle_lock);
307 free(uevent.devname);
311 rte_dev_event_monitor_start(void)
315 rte_rwlock_write_lock(&monitor_lock);
317 if (monitor_refcount) {
322 intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
323 if (intr_handle == NULL) {
324 RTE_LOG(ERR, EAL, "Fail to allocate intr_handle\n");
328 ret = rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_DEV_EVENT);
332 ret = rte_intr_fd_set(intr_handle, -1);
336 ret = dev_uev_socket_fd_create();
338 RTE_LOG(ERR, EAL, "error create device event fd.\n");
342 ret = rte_intr_callback_register(intr_handle, dev_uev_handler, NULL);
345 close(rte_intr_fd_get(intr_handle));
353 rte_intr_instance_free(intr_handle);
356 rte_rwlock_write_unlock(&monitor_lock);
361 rte_dev_event_monitor_stop(void)
365 rte_rwlock_write_lock(&monitor_lock);
367 if (!monitor_refcount) {
368 RTE_LOG(ERR, EAL, "device event monitor already stopped\n");
372 if (monitor_refcount > 1) {
377 ret = rte_intr_callback_unregister(intr_handle, dev_uev_handler,
380 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
384 close(rte_intr_fd_get(intr_handle));
385 rte_intr_instance_free(intr_handle);
391 rte_rwlock_write_unlock(&monitor_lock);
397 dev_sigbus_handler_register(void)
400 struct sigaction action;
404 if (sigbus_need_recover)
408 sigaddset(&mask, SIGBUS);
409 action.sa_flags = SA_SIGINFO;
410 action.sa_mask = mask;
411 action.sa_sigaction = sigbus_handler;
412 sigbus_need_recover = !sigaction(SIGBUS, &action, &sigbus_action_old);
418 dev_sigbus_handler_unregister(void)
422 sigbus_action_recover();
428 rte_dev_hotplug_handle_enable(void)
432 ret = dev_sigbus_handler_register();
435 "fail to register sigbus handler for devices.\n");
437 hotplug_handle = true;
443 rte_dev_hotplug_handle_disable(void)
447 ret = dev_sigbus_handler_unregister();
450 "fail to unregister sigbus handler for devices.\n");
452 hotplug_handle = false;