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 = {.fd = -1 };
27 static bool monitor_started;
28 static bool hotplug_handle;
30 #define EAL_UEV_MSG_LEN 4096
31 #define EAL_UEV_MSG_ELEM_LEN 128
34 * spinlock for device hot-unplug failure handling. If it try to access bus or
35 * device, such as handle sigbus on bus or handle memory failure for device
36 * just need to use this lock. It could protect the bus and the device to avoid
39 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
41 static struct sigaction sigbus_action_old;
43 static int sigbus_need_recover;
45 static void dev_uev_handler(__rte_unused void *param);
47 /* identify the system layer which reports this event. */
48 enum eal_dev_event_subsystem {
49 EAL_DEV_EVENT_SUBSYSTEM_PCI, /* PCI bus device event */
50 EAL_DEV_EVENT_SUBSYSTEM_UIO, /* UIO driver device event */
51 EAL_DEV_EVENT_SUBSYSTEM_VFIO, /* VFIO driver device event */
52 EAL_DEV_EVENT_SUBSYSTEM_MAX
56 sigbus_action_recover(void)
58 if (sigbus_need_recover) {
59 sigaction(SIGBUS, &sigbus_action_old, NULL);
60 sigbus_need_recover = 0;
64 static void sigbus_handler(int signum, siginfo_t *info,
65 void *ctx __rte_unused)
69 RTE_LOG(DEBUG, EAL, "Thread catch SIGBUS, fault address:%p\n",
72 rte_spinlock_lock(&failure_handle_lock);
73 ret = rte_bus_sigbus_handler(info->si_addr);
74 rte_spinlock_unlock(&failure_handle_lock);
76 rte_exit(EXIT_FAILURE,
77 "Failed to handle SIGBUS for hot-unplug, "
78 "(rte_errno: %s)!", strerror(rte_errno));
79 } else if (ret == 1) {
80 if (sigbus_action_old.sa_flags == SA_SIGINFO
81 && sigbus_action_old.sa_sigaction) {
82 (*(sigbus_action_old.sa_sigaction))(signum,
84 } else if (sigbus_action_old.sa_flags != SA_SIGINFO
85 && sigbus_action_old.sa_handler) {
86 (*(sigbus_action_old.sa_handler))(signum);
88 rte_exit(EXIT_FAILURE,
89 "Failed to handle generic SIGBUS!");
93 RTE_LOG(DEBUG, EAL, "Success to handle SIGBUS for hot-unplug!\n");
96 static int cmp_dev_name(const struct rte_device *dev,
99 const char *name = _name;
101 return strcmp(dev->name, name);
105 dev_uev_socket_fd_create(void)
107 struct sockaddr_nl addr;
110 intr_handle.fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC |
112 NETLINK_KOBJECT_UEVENT);
113 if (intr_handle.fd < 0) {
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(intr_handle.fd, (struct sockaddr *) &addr, sizeof(addr));
125 RTE_LOG(ERR, EAL, "Failed to bind uevent socket.\n");
131 close(intr_handle.fd);
137 dev_uev_parse(const char *buf, struct rte_dev_event *event, int length)
139 char action[EAL_UEV_MSG_ELEM_LEN];
140 char subsystem[EAL_UEV_MSG_ELEM_LEN];
141 char pci_slot_name[EAL_UEV_MSG_ELEM_LEN];
144 memset(action, 0, EAL_UEV_MSG_ELEM_LEN);
145 memset(subsystem, 0, EAL_UEV_MSG_ELEM_LEN);
146 memset(pci_slot_name, 0, EAL_UEV_MSG_ELEM_LEN);
149 for (; i < length; i++) {
155 * check device uevent from kernel side, no need to check
158 if (!strncmp(buf, "libudev", 7)) {
163 if (!strncmp(buf, "ACTION=", 7)) {
166 strlcpy(action, buf, sizeof(action));
167 } else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
170 strlcpy(subsystem, buf, sizeof(subsystem));
171 } else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
174 strlcpy(pci_slot_name, buf, sizeof(subsystem));
175 event->devname = strdup(pci_slot_name);
177 for (; i < length; i++) {
184 /* parse the subsystem layer */
185 if (!strncmp(subsystem, "uio", 3))
186 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_UIO;
187 else if (!strncmp(subsystem, "pci", 3))
188 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_PCI;
189 else if (!strncmp(subsystem, "vfio", 4))
190 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_VFIO;
194 /* parse the action type */
195 if (!strncmp(action, "add", 3))
196 event->type = RTE_DEV_EVENT_ADD;
197 else if (!strncmp(action, "remove", 6))
198 event->type = RTE_DEV_EVENT_REMOVE;
205 dev_delayed_unregister(void *param)
207 rte_intr_callback_unregister(&intr_handle, dev_uev_handler, param);
208 close(intr_handle.fd);
213 dev_uev_handler(__rte_unused void *param)
215 struct rte_dev_event uevent;
217 char buf[EAL_UEV_MSG_LEN];
219 struct rte_device *dev;
220 const char *busname = "";
222 memset(&uevent, 0, sizeof(struct rte_dev_event));
223 memset(buf, 0, EAL_UEV_MSG_LEN);
225 ret = recv(intr_handle.fd, buf, EAL_UEV_MSG_LEN, MSG_DONTWAIT);
226 if (ret < 0 && errno == EAGAIN)
229 /* connection is closed or broken, can not up again. */
230 RTE_LOG(ERR, EAL, "uevent socket connection is broken.\n");
231 rte_eal_alarm_set(1, dev_delayed_unregister, NULL);
235 ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
237 RTE_LOG(DEBUG, EAL, "It is not an valid event "
238 "that need to be handle.\n");
242 RTE_LOG(DEBUG, EAL, "receive uevent(name:%s, type:%d, subsystem:%d)\n",
243 uevent.devname, uevent.type, uevent.subsystem);
245 switch (uevent.subsystem) {
246 case EAL_DEV_EVENT_SUBSYSTEM_PCI:
247 case EAL_DEV_EVENT_SUBSYSTEM_UIO:
254 if (uevent.devname) {
255 if (uevent.type == RTE_DEV_EVENT_REMOVE && hotplug_handle) {
256 rte_spinlock_lock(&failure_handle_lock);
257 bus = rte_bus_find_by_name(busname);
259 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n",
261 goto failure_handle_err;
264 dev = bus->find_device(NULL, cmp_dev_name,
267 RTE_LOG(ERR, EAL, "Cannot find device (%s) on "
268 "bus (%s)\n", uevent.devname, busname);
269 goto failure_handle_err;
272 ret = bus->hot_unplug_handler(dev);
274 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
275 "for device (%s)\n", dev->name);
277 rte_spinlock_unlock(&failure_handle_lock);
279 rte_dev_event_callback_process(uevent.devname, uevent.type);
285 rte_spinlock_unlock(&failure_handle_lock);
288 int __rte_experimental
289 rte_dev_event_monitor_start(void)
296 ret = dev_uev_socket_fd_create();
298 RTE_LOG(ERR, EAL, "error create device event fd.\n");
302 intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT;
303 ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL);
306 RTE_LOG(ERR, EAL, "fail to register uevent callback.\n");
310 monitor_started = true;
315 int __rte_experimental
316 rte_dev_event_monitor_stop(void)
320 if (!monitor_started)
323 ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
326 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
330 close(intr_handle.fd);
332 monitor_started = false;
338 dev_sigbus_handler_register(void)
341 struct sigaction action;
345 if (sigbus_need_recover)
349 sigaddset(&mask, SIGBUS);
350 action.sa_flags = SA_SIGINFO;
351 action.sa_mask = mask;
352 action.sa_sigaction = sigbus_handler;
353 sigbus_need_recover = !sigaction(SIGBUS, &action, &sigbus_action_old);
359 dev_sigbus_handler_unregister(void)
363 sigbus_action_recover();
368 int __rte_experimental
369 rte_dev_hotplug_handle_enable(void)
373 ret = dev_sigbus_handler_register();
376 "fail to register sigbus handler for devices.\n");
378 hotplug_handle = true;
383 int __rte_experimental
384 rte_dev_hotplug_handle_disable(void)
388 ret = dev_sigbus_handler_unregister();
391 "fail to unregister sigbus handler for devices.\n");
393 hotplug_handle = false;