1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 #include <sys/socket.h>
9 #include <linux/netlink.h>
11 #include <rte_string_fns.h>
14 #include <rte_interrupts.h>
15 #include <rte_alarm.h>
17 #include <rte_spinlock.h>
18 #include <rte_errno.h>
20 #include "eal_private.h"
22 static struct rte_intr_handle *intr_handle;
23 static rte_rwlock_t monitor_lock = RTE_RWLOCK_INITIALIZER;
24 static uint32_t monitor_refcount;
25 static bool hotplug_handle;
27 #define EAL_UEV_MSG_LEN 4096
28 #define EAL_UEV_MSG_ELEM_LEN 128
31 * spinlock for device hot-unplug failure handling. If it try to access bus or
32 * device, such as handle sigbus on bus or handle memory failure for device
33 * just need to use this lock. It could protect the bus and the device to avoid
36 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
38 static struct sigaction sigbus_action_old;
40 static int sigbus_need_recover;
42 static void dev_uev_handler(__rte_unused void *param);
44 /* identify the system layer which reports this event. */
45 enum eal_dev_event_subsystem {
46 EAL_DEV_EVENT_SUBSYSTEM_PCI, /* PCI bus device event */
47 EAL_DEV_EVENT_SUBSYSTEM_UIO, /* UIO driver device event */
48 EAL_DEV_EVENT_SUBSYSTEM_VFIO, /* VFIO driver device event */
49 EAL_DEV_EVENT_SUBSYSTEM_MAX
53 sigbus_action_recover(void)
55 if (sigbus_need_recover) {
56 sigaction(SIGBUS, &sigbus_action_old, NULL);
57 sigbus_need_recover = 0;
61 static void sigbus_handler(int signum, siginfo_t *info,
62 void *ctx __rte_unused)
66 RTE_LOG(DEBUG, EAL, "Thread catch SIGBUS, fault address:%p\n",
69 rte_spinlock_lock(&failure_handle_lock);
70 ret = rte_bus_sigbus_handler(info->si_addr);
71 rte_spinlock_unlock(&failure_handle_lock);
73 rte_exit(EXIT_FAILURE,
74 "Failed to handle SIGBUS for hot-unplug, "
75 "(rte_errno: %s)!", strerror(rte_errno));
76 } else if (ret == 1) {
77 if (sigbus_action_old.sa_flags == SA_SIGINFO
78 && sigbus_action_old.sa_sigaction) {
79 (*(sigbus_action_old.sa_sigaction))(signum,
81 } else if (sigbus_action_old.sa_flags != SA_SIGINFO
82 && sigbus_action_old.sa_handler) {
83 (*(sigbus_action_old.sa_handler))(signum);
85 rte_exit(EXIT_FAILURE,
86 "Failed to handle generic SIGBUS!");
90 RTE_LOG(DEBUG, EAL, "Success to handle SIGBUS for hot-unplug!\n");
93 static int cmp_dev_name(const struct rte_device *dev,
96 const char *name = _name;
98 return strcmp(dev->name, name);
102 dev_uev_socket_fd_create(void)
104 struct sockaddr_nl addr;
107 fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
108 NETLINK_KOBJECT_UEVENT);
110 RTE_LOG(ERR, EAL, "create uevent fd failed.\n");
114 memset(&addr, 0, sizeof(addr));
115 addr.nl_family = AF_NETLINK;
117 addr.nl_groups = 0xffffffff;
119 ret = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
121 RTE_LOG(ERR, EAL, "Failed to bind uevent socket.\n");
125 if (rte_intr_fd_set(intr_handle, fd))
135 struct rte_dev_event {
136 enum rte_dev_event_type type; /**< device event type */
137 int subsystem; /**< subsystem id */
138 char *devname; /**< device name */
142 dev_uev_parse(const char *buf, struct rte_dev_event *event, int length)
144 char action[EAL_UEV_MSG_ELEM_LEN];
145 char subsystem[EAL_UEV_MSG_ELEM_LEN];
146 char pci_slot_name[EAL_UEV_MSG_ELEM_LEN];
149 memset(action, 0, EAL_UEV_MSG_ELEM_LEN);
150 memset(subsystem, 0, EAL_UEV_MSG_ELEM_LEN);
151 memset(pci_slot_name, 0, EAL_UEV_MSG_ELEM_LEN);
154 for (; i < length; i++) {
163 * check device uevent from kernel side, no need to check
166 if (!strncmp(buf, "libudev", 7)) {
171 if (!strncmp(buf, "ACTION=", 7)) {
174 strlcpy(action, buf, sizeof(action));
175 } else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
178 strlcpy(subsystem, buf, sizeof(subsystem));
179 } else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
182 strlcpy(pci_slot_name, buf, sizeof(subsystem));
183 event->devname = strdup(pci_slot_name);
185 for (; i < length; i++) {
192 /* parse the subsystem layer */
193 if (!strncmp(subsystem, "uio", 3))
194 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_UIO;
195 else if (!strncmp(subsystem, "pci", 3))
196 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_PCI;
197 else if (!strncmp(subsystem, "vfio", 4))
198 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_VFIO;
202 /* parse the action type */
203 if (!strncmp(action, "add", 3))
204 event->type = RTE_DEV_EVENT_ADD;
205 else if (!strncmp(action, "remove", 6))
206 event->type = RTE_DEV_EVENT_REMOVE;
211 free(event->devname);
216 dev_delayed_unregister(void *param)
218 rte_intr_callback_unregister(intr_handle, dev_uev_handler, param);
219 if (rte_intr_fd_get(intr_handle) >= 0) {
220 close(rte_intr_fd_get(intr_handle));
221 rte_intr_fd_set(intr_handle, -1);
226 dev_uev_handler(__rte_unused void *param)
228 struct rte_dev_event uevent;
230 char buf[EAL_UEV_MSG_LEN + 1];
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 + 1);
238 if (rte_intr_fd_get(intr_handle) < 0)
241 ret = recv(rte_intr_fd_get(intr_handle), buf, EAL_UEV_MSG_LEN,
243 if (ret < 0 && errno == EAGAIN)
246 /* connection is closed or broken, can not up again. */
247 RTE_LOG(ERR, EAL, "uevent socket connection is broken.\n");
248 rte_eal_alarm_set(1, dev_delayed_unregister, NULL);
252 ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
254 RTE_LOG(DEBUG, EAL, "Ignoring uevent '%s'\n", buf);
258 RTE_LOG(DEBUG, EAL, "receive uevent(name:%s, type:%d, subsystem:%d)\n",
259 uevent.devname, uevent.type, uevent.subsystem);
261 switch (uevent.subsystem) {
262 case EAL_DEV_EVENT_SUBSYSTEM_PCI:
263 case EAL_DEV_EVENT_SUBSYSTEM_UIO:
270 if (uevent.devname) {
271 if (uevent.type == RTE_DEV_EVENT_REMOVE && hotplug_handle) {
272 rte_spinlock_lock(&failure_handle_lock);
273 bus = rte_bus_find_by_name(busname);
275 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n",
277 goto failure_handle_err;
280 dev = bus->find_device(NULL, cmp_dev_name,
283 RTE_LOG(ERR, EAL, "Cannot find device (%s) on "
284 "bus (%s)\n", uevent.devname, busname);
285 goto failure_handle_err;
288 ret = bus->hot_unplug_handler(dev);
290 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
291 "for device (%s)\n", dev->name);
293 rte_spinlock_unlock(&failure_handle_lock);
295 rte_dev_event_callback_process(uevent.devname, uevent.type);
296 free(uevent.devname);
302 rte_spinlock_unlock(&failure_handle_lock);
303 free(uevent.devname);
307 rte_dev_event_monitor_start(void)
311 rte_rwlock_write_lock(&monitor_lock);
313 if (monitor_refcount) {
318 intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
319 if (intr_handle == NULL) {
320 RTE_LOG(ERR, EAL, "Fail to allocate intr_handle\n");
324 ret = rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_DEV_EVENT);
328 ret = rte_intr_fd_set(intr_handle, -1);
332 ret = dev_uev_socket_fd_create();
334 RTE_LOG(ERR, EAL, "error create device event fd.\n");
338 ret = rte_intr_callback_register(intr_handle, dev_uev_handler, NULL);
341 close(rte_intr_fd_get(intr_handle));
349 rte_intr_instance_free(intr_handle);
352 rte_rwlock_write_unlock(&monitor_lock);
357 rte_dev_event_monitor_stop(void)
361 rte_rwlock_write_lock(&monitor_lock);
363 if (!monitor_refcount) {
364 RTE_LOG(ERR, EAL, "device event monitor already stopped\n");
368 if (monitor_refcount > 1) {
373 ret = rte_intr_callback_unregister(intr_handle, dev_uev_handler,
376 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
380 close(rte_intr_fd_get(intr_handle));
381 rte_intr_instance_free(intr_handle);
388 rte_rwlock_write_unlock(&monitor_lock);
394 dev_sigbus_handler_register(void)
397 struct sigaction action;
401 if (sigbus_need_recover)
405 sigaddset(&mask, SIGBUS);
406 action.sa_flags = SA_SIGINFO;
407 action.sa_mask = mask;
408 action.sa_sigaction = sigbus_handler;
409 sigbus_need_recover = !sigaction(SIGBUS, &action, &sigbus_action_old);
415 dev_sigbus_handler_unregister(void)
419 sigbus_action_recover();
425 rte_dev_hotplug_handle_enable(void)
429 ret = dev_sigbus_handler_register();
432 "fail to register sigbus handler for devices.\n");
434 hotplug_handle = true;
440 rte_dev_hotplug_handle_disable(void)
444 ret = dev_sigbus_handler_unregister();
447 "fail to unregister sigbus handler for devices.\n");
449 hotplug_handle = false;