Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018 Intel Corporation
3 : : */
4 : :
5 : : #include <stdlib.h>
6 : : #include <string.h>
7 : : #include <unistd.h>
8 : : #include <signal.h>
9 : : #include <sys/socket.h>
10 : : #include <linux/netlink.h>
11 : :
12 : : #include <rte_string_fns.h>
13 : : #include <rte_log.h>
14 : : #include <rte_dev.h>
15 : : #include <rte_interrupts.h>
16 : : #include <rte_alarm.h>
17 : : #include <bus_driver.h>
18 : : #include <rte_spinlock.h>
19 : : #include <rte_errno.h>
20 : :
21 : : #include <eal_export.h>
22 : : #include "eal_private.h"
23 : :
24 : : static struct rte_intr_handle *intr_handle;
25 : : static rte_rwlock_t monitor_lock = RTE_RWLOCK_INITIALIZER;
26 : : static uint32_t monitor_refcount;
27 : : static bool hotplug_handle;
28 : :
29 : : #define EAL_UEV_MSG_LEN 4096
30 : : #define EAL_UEV_MSG_ELEM_LEN 128
31 : :
32 : : /* Listen only to messages from kernel (not libudev) */
33 : : #define EAL_UEV_GROUP_KERNEL 1
34 : :
35 : : /* Large uevent buffer */
36 : : #define EAL_UEV_MSG_RCVBUF (4 * 1024 * 1024)
37 : :
38 : : /*
39 : : * spinlock for device hot-unplug failure handling. If it try to access bus or
40 : : * device, such as handle sigbus on bus or handle memory failure for device
41 : : * just need to use this lock. It could protect the bus and the device to avoid
42 : : * race condition.
43 : : */
44 : : static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
45 : :
46 : : static struct sigaction sigbus_action_old;
47 : :
48 : : static int sigbus_need_recover;
49 : :
50 : : static void dev_uev_handler(__rte_unused void *param);
51 : :
52 : : /* identify the system layer which reports this event. */
53 : : enum eal_dev_event_subsystem {
54 : : EAL_DEV_EVENT_SUBSYSTEM_PCI, /* PCI bus device event */
55 : : EAL_DEV_EVENT_SUBSYSTEM_UIO, /* UIO driver device event */
56 : : EAL_DEV_EVENT_SUBSYSTEM_VFIO, /* VFIO driver device event */
57 : : EAL_DEV_EVENT_SUBSYSTEM_MAX
58 : : };
59 : :
60 : : static void
61 : : sigbus_action_recover(void)
62 : : {
63 : 0 : if (sigbus_need_recover) {
64 : 0 : sigaction(SIGBUS, &sigbus_action_old, NULL);
65 : 0 : sigbus_need_recover = 0;
66 : : }
67 : : }
68 : :
69 : 0 : static void sigbus_handler(int signum, siginfo_t *info,
70 : : void *ctx __rte_unused)
71 : : {
72 : : int ret;
73 : :
74 : 0 : EAL_LOG(DEBUG, "Thread catch SIGBUS, fault address:%p",
75 : : info->si_addr);
76 : :
77 : : rte_spinlock_lock(&failure_handle_lock);
78 : 0 : ret = rte_bus_sigbus_handler(info->si_addr);
79 : : rte_spinlock_unlock(&failure_handle_lock);
80 [ # # ]: 0 : if (ret == -1) {
81 : 0 : rte_exit(EXIT_FAILURE,
82 : : "Failed to handle SIGBUS for hot-unplug, "
83 : : "(rte_errno: %s)!", strerror(rte_errno));
84 [ # # ]: 0 : } else if (ret == 1) {
85 [ # # ]: 0 : if (sigbus_action_old.sa_flags == SA_SIGINFO
86 [ # # ]: 0 : && sigbus_action_old.sa_sigaction) {
87 : 0 : (*(sigbus_action_old.sa_sigaction))(signum,
88 : : info, ctx);
89 [ # # ]: 0 : } else if (sigbus_action_old.sa_flags != SA_SIGINFO
90 [ # # ]: 0 : && sigbus_action_old.sa_handler) {
91 : 0 : (*(sigbus_action_old.sa_handler))(signum);
92 : : } else {
93 : 0 : rte_exit(EXIT_FAILURE,
94 : : "Failed to handle generic SIGBUS!");
95 : : }
96 : : }
97 : :
98 : 0 : EAL_LOG(DEBUG, "Success to handle SIGBUS for hot-unplug!");
99 : 0 : }
100 : :
101 : 0 : static int cmp_dev_name(const struct rte_device *dev,
102 : : const void *_name)
103 : : {
104 : : const char *name = _name;
105 : :
106 : 0 : return strcmp(dev->name, name);
107 : : }
108 : :
109 : : /*
110 : : * To avoid losing uevents increase the netlink receive buffer size,
111 : : * and override the kernel clamp value if necessary.
112 : : */
113 : : static int
114 : 0 : dev_uev_set_rcvbuf(int fd, int n)
115 : : {
116 : : int ret, val;
117 : 0 : socklen_t len = sizeof(val);
118 : :
119 : 0 : ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
120 [ # # ]: 0 : if (ret < 0)
121 : : return ret;
122 : :
123 : : /* kernel may have clamped our request */
124 : 0 : ret = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &val, &len);
125 [ # # # # : 0 : if (ret >= 0 && len == sizeof(val) && val == n * 2)
# # ]
126 : : return 0; /* request worked */
127 : :
128 : : /* try again to override kernel restriction */
129 : 0 : return setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &n, sizeof(n));
130 : : }
131 : :
132 : : static int
133 : 0 : dev_uev_socket_fd_create(void)
134 : : {
135 : : struct sockaddr_nl addr;
136 : : int ret, fd;
137 : :
138 : 0 : fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
139 : : NETLINK_KOBJECT_UEVENT);
140 [ # # ]: 0 : if (fd < 0) {
141 : 0 : EAL_LOG(ERR, "create uevent fd failed.");
142 : 0 : return -1;
143 : : }
144 : :
145 : : memset(&addr, 0, sizeof(addr));
146 : 0 : addr.nl_family = AF_NETLINK;
147 : : addr.nl_pid = 0;
148 : 0 : addr.nl_groups = EAL_UEV_GROUP_KERNEL;
149 : :
150 : 0 : ret = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
151 [ # # ]: 0 : if (ret < 0) {
152 : 0 : EAL_LOG(ERR, "Failed to bind uevent socket.");
153 : 0 : goto err;
154 : : }
155 : :
156 [ # # ]: 0 : if (dev_uev_set_rcvbuf(fd, EAL_UEV_MSG_RCVBUF) < 0)
157 : 0 : EAL_LOG(NOTICE, "Failed to set rcvbuf.");
158 : :
159 [ # # ]: 0 : if (rte_intr_fd_set(intr_handle, fd))
160 : 0 : goto err;
161 : :
162 : : return 0;
163 : 0 : err:
164 : 0 : close(fd);
165 : 0 : return ret;
166 : : }
167 : :
168 : : struct rte_dev_event {
169 : : enum rte_dev_event_type type; /**< device event type */
170 : : int subsystem; /**< subsystem id */
171 : : char *devname; /**< device name */
172 : : };
173 : :
174 : : static int
175 : 0 : dev_uev_parse(const char *buf, struct rte_dev_event *event, int length)
176 : : {
177 : : char action[EAL_UEV_MSG_ELEM_LEN];
178 : : char subsystem[EAL_UEV_MSG_ELEM_LEN];
179 : : char pci_slot_name[EAL_UEV_MSG_ELEM_LEN];
180 : : int i = 0;
181 : :
182 : : memset(action, 0, EAL_UEV_MSG_ELEM_LEN);
183 : : memset(subsystem, 0, EAL_UEV_MSG_ELEM_LEN);
184 : : memset(pci_slot_name, 0, EAL_UEV_MSG_ELEM_LEN);
185 : :
186 [ # # ]: 0 : while (i < length) {
187 [ # # ]: 0 : for (; i < length; i++) {
188 [ # # ]: 0 : if (*buf)
189 : : break;
190 : 0 : buf++;
191 : : }
192 [ # # ]: 0 : if (i >= length)
193 : : break;
194 : :
195 [ # # ]: 0 : if (!strncmp(buf, "ACTION=", 7)) {
196 : 0 : buf += 7;
197 : 0 : i += 7;
198 : : strlcpy(action, buf, sizeof(action));
199 [ # # ]: 0 : } else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
200 : 0 : buf += 10;
201 : 0 : i += 10;
202 : : strlcpy(subsystem, buf, sizeof(subsystem));
203 [ # # ]: 0 : } else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
204 : 0 : buf += 14;
205 : 0 : i += 14;
206 : : strlcpy(pci_slot_name, buf, sizeof(subsystem));
207 : 0 : event->devname = strdup(pci_slot_name);
208 [ # # ]: 0 : if (event->devname == NULL)
209 : : return -1;
210 : : }
211 [ # # ]: 0 : for (; i < length; i++) {
212 [ # # ]: 0 : if (*buf == '\0')
213 : : break;
214 : 0 : buf++;
215 : : }
216 : : }
217 : :
218 : : /* parse the subsystem layer */
219 [ # # ]: 0 : if (!strncmp(subsystem, "uio", 3))
220 : 0 : event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_UIO;
221 [ # # ]: 0 : else if (!strncmp(subsystem, "pci", 3))
222 : 0 : event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_PCI;
223 [ # # ]: 0 : else if (!strncmp(subsystem, "vfio", 4))
224 : 0 : event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_VFIO;
225 : : else
226 : 0 : goto err;
227 : :
228 : : /* parse the action type */
229 [ # # ]: 0 : if (!strncmp(action, "add", 3))
230 : 0 : event->type = RTE_DEV_EVENT_ADD;
231 [ # # ]: 0 : else if (!strncmp(action, "remove", 6))
232 : 0 : event->type = RTE_DEV_EVENT_REMOVE;
233 : : else
234 : 0 : goto err;
235 : : return 0;
236 : 0 : err:
237 : 0 : free(event->devname);
238 : 0 : return -1;
239 : : }
240 : :
241 : : static void
242 : 0 : dev_delayed_unregister(void *param)
243 : : {
244 : 0 : rte_intr_callback_unregister(intr_handle, dev_uev_handler, param);
245 : 0 : rte_intr_fd_close(intr_handle);
246 : 0 : }
247 : :
248 : : static void
249 : 0 : dev_uev_handler(__rte_unused void *param)
250 : : {
251 : : struct rte_dev_event uevent;
252 : : int ret;
253 : : char buf[EAL_UEV_MSG_LEN + 1];
254 : : struct rte_bus *bus;
255 : : struct rte_device *dev;
256 : : const char *busname = "";
257 : :
258 : : memset(&uevent, 0, sizeof(struct rte_dev_event));
259 : : memset(buf, 0, EAL_UEV_MSG_LEN + 1);
260 : :
261 [ # # ]: 0 : if (rte_intr_fd_get(intr_handle) < 0)
262 : 0 : return;
263 : :
264 : 0 : ret = recv(rte_intr_fd_get(intr_handle), buf, EAL_UEV_MSG_LEN,
265 : : MSG_DONTWAIT);
266 [ # # ]: 0 : if (ret < 0) {
267 : : /* transient error */
268 [ # # ]: 0 : if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
269 : : return;
270 : :
271 : : /* kernel netlink messages lost */
272 [ # # ]: 0 : if (errno == ENOBUFS) {
273 : 0 : EAL_LOG(NOTICE, "kernel receive buffer overrun");
274 : 0 : return;
275 : : }
276 : :
277 : 0 : EAL_LOG(ERR, "unexpected error on uevent recv: %s",
278 : : strerror(errno));
279 : :
280 : 0 : rte_eal_alarm_set(1, dev_delayed_unregister, NULL);
281 : 0 : return;
282 : : }
283 : :
284 : 0 : ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
285 [ # # ]: 0 : if (ret < 0) {
286 : 0 : EAL_LOG(DEBUG, "Ignoring uevent '%s'", buf);
287 : 0 : return;
288 : : }
289 : :
290 : 0 : EAL_LOG(DEBUG, "receive uevent(name:%s, type:%d, subsystem:%d)",
291 : : uevent.devname, uevent.type, uevent.subsystem);
292 : :
293 [ # # ]: 0 : switch (uevent.subsystem) {
294 : 0 : case EAL_DEV_EVENT_SUBSYSTEM_PCI:
295 : : case EAL_DEV_EVENT_SUBSYSTEM_UIO:
296 : : busname = "pci";
297 : 0 : break;
298 : : default:
299 : : break;
300 : : }
301 : :
302 [ # # ]: 0 : if (uevent.devname) {
303 [ # # # # ]: 0 : if (uevent.type == RTE_DEV_EVENT_REMOVE && hotplug_handle) {
304 : : rte_spinlock_lock(&failure_handle_lock);
305 : 0 : bus = rte_bus_find_by_name(busname);
306 [ # # ]: 0 : if (bus == NULL) {
307 : 0 : EAL_LOG(ERR, "Cannot find bus (%s)",
308 : : busname);
309 : 0 : goto failure_handle_err;
310 : : }
311 : :
312 : 0 : dev = bus->find_device(bus, NULL, cmp_dev_name, uevent.devname);
313 [ # # ]: 0 : if (dev == NULL) {
314 : 0 : EAL_LOG(ERR, "Cannot find device (%s) on "
315 : : "bus (%s)", uevent.devname, busname);
316 : 0 : goto failure_handle_err;
317 : : }
318 : :
319 : 0 : ret = bus->hot_unplug_handler(dev);
320 [ # # ]: 0 : if (ret) {
321 : 0 : EAL_LOG(ERR, "Can not handle hot-unplug "
322 : : "for device (%s)", dev->name);
323 : : }
324 : : rte_spinlock_unlock(&failure_handle_lock);
325 : : }
326 : 0 : rte_dev_event_callback_process(uevent.devname, uevent.type);
327 : 0 : free(uevent.devname);
328 : : }
329 : :
330 : : return;
331 : :
332 : 0 : failure_handle_err:
333 : : rte_spinlock_unlock(&failure_handle_lock);
334 : 0 : free(uevent.devname);
335 : : }
336 : :
337 : : RTE_EXPORT_SYMBOL(rte_dev_event_monitor_start)
338 : : int
339 : 0 : rte_dev_event_monitor_start(void)
340 : : {
341 : : int ret = 0;
342 : :
343 : 0 : rte_rwlock_write_lock(&monitor_lock);
344 : :
345 [ # # ]: 0 : if (monitor_refcount) {
346 : 0 : monitor_refcount++;
347 : 0 : goto exit;
348 : : }
349 : :
350 : 0 : intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
351 [ # # ]: 0 : if (intr_handle == NULL) {
352 : 0 : EAL_LOG(ERR, "Fail to allocate intr_handle");
353 : 0 : goto exit;
354 : : }
355 : :
356 : 0 : ret = rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_DEV_EVENT);
357 [ # # ]: 0 : if (ret)
358 : 0 : goto exit;
359 : :
360 : 0 : ret = dev_uev_socket_fd_create();
361 [ # # ]: 0 : if (ret) {
362 : 0 : EAL_LOG(ERR, "error create device event fd.");
363 : 0 : goto exit;
364 : : }
365 : :
366 : 0 : ret = rte_intr_callback_register(intr_handle, dev_uev_handler, NULL);
367 : :
368 [ # # ]: 0 : if (ret) {
369 : 0 : rte_intr_fd_close(intr_handle);
370 : 0 : goto exit;
371 : : }
372 : :
373 : 0 : monitor_refcount++;
374 : :
375 : 0 : exit:
376 [ # # ]: 0 : if (ret) {
377 : 0 : rte_intr_instance_free(intr_handle);
378 : 0 : intr_handle = NULL;
379 : : }
380 : : rte_rwlock_write_unlock(&monitor_lock);
381 : 0 : return ret;
382 : : }
383 : :
384 : : RTE_EXPORT_SYMBOL(rte_dev_event_monitor_stop)
385 : : int
386 : 0 : rte_dev_event_monitor_stop(void)
387 : : {
388 : : int ret = 0;
389 : :
390 : 0 : rte_rwlock_write_lock(&monitor_lock);
391 : :
392 [ # # ]: 0 : if (!monitor_refcount) {
393 : 0 : EAL_LOG(ERR, "device event monitor already stopped");
394 : 0 : goto exit;
395 : : }
396 : :
397 [ # # ]: 0 : if (monitor_refcount > 1) {
398 : 0 : monitor_refcount--;
399 : 0 : goto exit;
400 : : }
401 : :
402 : 0 : ret = rte_intr_callback_unregister(intr_handle, dev_uev_handler,
403 : : (void *)-1);
404 [ # # ]: 0 : if (ret < 0) {
405 : 0 : EAL_LOG(ERR, "fail to unregister uevent callback.");
406 : 0 : goto exit;
407 : : }
408 : :
409 : 0 : rte_intr_fd_close(intr_handle);
410 : 0 : rte_intr_instance_free(intr_handle);
411 : 0 : intr_handle = NULL;
412 : : ret = 0;
413 : :
414 : 0 : monitor_refcount--;
415 : :
416 : 0 : exit:
417 : : rte_rwlock_write_unlock(&monitor_lock);
418 : :
419 : 0 : return ret;
420 : : }
421 : :
422 : : static int
423 : 0 : dev_sigbus_handler_register(void)
424 : : {
425 : : sigset_t mask;
426 : : struct sigaction action;
427 : :
428 : 0 : rte_errno = 0;
429 : :
430 [ # # ]: 0 : if (sigbus_need_recover)
431 : : return 0;
432 : :
433 : 0 : sigemptyset(&mask);
434 : 0 : sigaddset(&mask, SIGBUS);
435 : 0 : action.sa_flags = SA_SIGINFO;
436 : 0 : action.sa_mask = mask;
437 : 0 : action.sa_sigaction = sigbus_handler;
438 : 0 : sigbus_need_recover = !sigaction(SIGBUS, &action, &sigbus_action_old);
439 : :
440 : 0 : return rte_errno;
441 : : }
442 : :
443 : : static int
444 : 0 : dev_sigbus_handler_unregister(void)
445 : : {
446 [ # # ]: 0 : rte_errno = 0;
447 : :
448 : : sigbus_action_recover();
449 : :
450 : 0 : return rte_errno;
451 : : }
452 : :
453 : : RTE_EXPORT_SYMBOL(rte_dev_hotplug_handle_enable)
454 : : int
455 : 0 : rte_dev_hotplug_handle_enable(void)
456 : : {
457 : : int ret = 0;
458 : :
459 : 0 : ret = dev_sigbus_handler_register();
460 [ # # ]: 0 : if (ret < 0)
461 : 0 : EAL_LOG(ERR,
462 : : "fail to register sigbus handler for devices.");
463 : :
464 : 0 : hotplug_handle = true;
465 : :
466 : 0 : return ret;
467 : : }
468 : :
469 : : RTE_EXPORT_SYMBOL(rte_dev_hotplug_handle_disable)
470 : : int
471 : 0 : rte_dev_hotplug_handle_disable(void)
472 : : {
473 : : int ret = 0;
474 : :
475 : 0 : ret = dev_sigbus_handler_unregister();
476 [ # # ]: 0 : if (ret < 0)
477 : 0 : EAL_LOG(ERR,
478 : : "fail to unregister sigbus handler for devices.");
479 : :
480 : 0 : hotplug_handle = false;
481 : :
482 : 0 : return ret;
483 : : }
|