1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
3 */
4
5 #include <string.h>
6 #include <unistd.h>
7 #include <signal.h>
8 #include <sys/socket.h>
9 #include <linux/netlink.h>
10
11 #include <rte_string_fns.h>
12 #include <rte_log.h>
13 #include <rte_dev.h>
14 #include <rte_interrupts.h>
15 #include <rte_alarm.h>
16 #include <rte_bus.h>
17 #include <rte_spinlock.h>
18 #include <rte_errno.h>
19
20 #include "eal_private.h"
21
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;
26
27 #define EAL_UEV_MSG_LEN 4096
28 #define EAL_UEV_MSG_ELEM_LEN 128
29
30 /*
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
34 * race condition.
35 */
36 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
37
38 static struct sigaction sigbus_action_old;
39
40 static int sigbus_need_recover;
41
42 static void dev_uev_handler(__rte_unused void *param);
43
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
50 };
51
52 static void
sigbus_action_recover(void)53 sigbus_action_recover(void)
54 {
55 if (sigbus_need_recover) {
56 sigaction(SIGBUS, &sigbus_action_old, NULL);
57 sigbus_need_recover = 0;
58 }
59 }
60
sigbus_handler(int signum,siginfo_t * info,void * ctx __rte_unused)61 static void sigbus_handler(int signum, siginfo_t *info,
62 void *ctx __rte_unused)
63 {
64 int ret;
65
66 RTE_LOG(DEBUG, EAL, "Thread catch SIGBUS, fault address:%p\n",
67 info->si_addr);
68
69 rte_spinlock_lock(&failure_handle_lock);
70 ret = rte_bus_sigbus_handler(info->si_addr);
71 rte_spinlock_unlock(&failure_handle_lock);
72 if (ret == -1) {
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,
80 info, ctx);
81 } else if (sigbus_action_old.sa_flags != SA_SIGINFO
82 && sigbus_action_old.sa_handler) {
83 (*(sigbus_action_old.sa_handler))(signum);
84 } else {
85 rte_exit(EXIT_FAILURE,
86 "Failed to handle generic SIGBUS!");
87 }
88 }
89
90 RTE_LOG(DEBUG, EAL, "Success to handle SIGBUS for hot-unplug!\n");
91 }
92
cmp_dev_name(const struct rte_device * dev,const void * _name)93 static int cmp_dev_name(const struct rte_device *dev,
94 const void *_name)
95 {
96 const char *name = _name;
97
98 return strcmp(dev->name, name);
99 }
100
101 static int
dev_uev_socket_fd_create(void)102 dev_uev_socket_fd_create(void)
103 {
104 struct sockaddr_nl addr;
105 int ret, fd;
106
107 fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
108 NETLINK_KOBJECT_UEVENT);
109 if (fd < 0) {
110 RTE_LOG(ERR, EAL, "create uevent fd failed.\n");
111 return -1;
112 }
113
114 memset(&addr, 0, sizeof(addr));
115 addr.nl_family = AF_NETLINK;
116 addr.nl_pid = 0;
117 addr.nl_groups = 0xffffffff;
118
119 ret = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
120 if (ret < 0) {
121 RTE_LOG(ERR, EAL, "Failed to bind uevent socket.\n");
122 goto err;
123 }
124
125 if (rte_intr_fd_set(intr_handle, fd))
126 goto err;
127
128 return 0;
129 err:
130 close(fd);
131 fd = -1;
132 return ret;
133 }
134
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 */
139 };
140
141 static int
dev_uev_parse(const char * buf,struct rte_dev_event * event,int length)142 dev_uev_parse(const char *buf, struct rte_dev_event *event, int length)
143 {
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];
147 int i = 0;
148
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);
152
153 while (i < length) {
154 for (; i < length; i++) {
155 if (*buf)
156 break;
157 buf++;
158 }
159 if (i >= length)
160 break;
161
162 /**
163 * check device uevent from kernel side, no need to check
164 * uevent from udev.
165 */
166 if (!strncmp(buf, "libudev", 7)) {
167 buf += 7;
168 i += 7;
169 return -1;
170 }
171 if (!strncmp(buf, "ACTION=", 7)) {
172 buf += 7;
173 i += 7;
174 strlcpy(action, buf, sizeof(action));
175 } else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
176 buf += 10;
177 i += 10;
178 strlcpy(subsystem, buf, sizeof(subsystem));
179 } else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
180 buf += 14;
181 i += 14;
182 strlcpy(pci_slot_name, buf, sizeof(subsystem));
183 event->devname = strdup(pci_slot_name);
184 }
185 for (; i < length; i++) {
186 if (*buf == '\0')
187 break;
188 buf++;
189 }
190 }
191
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;
199 else
200 goto err;
201
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;
207 else
208 goto err;
209 return 0;
210 err:
211 free(event->devname);
212 return -1;
213 }
214
215 static void
dev_delayed_unregister(void * param)216 dev_delayed_unregister(void *param)
217 {
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);
222 }
223 }
224
225 static void
dev_uev_handler(__rte_unused void * param)226 dev_uev_handler(__rte_unused void *param)
227 {
228 struct rte_dev_event uevent;
229 int ret;
230 char buf[EAL_UEV_MSG_LEN + 1];
231 struct rte_bus *bus;
232 struct rte_device *dev;
233 const char *busname = "";
234
235 memset(&uevent, 0, sizeof(struct rte_dev_event));
236 memset(buf, 0, EAL_UEV_MSG_LEN + 1);
237
238 if (rte_intr_fd_get(intr_handle) < 0)
239 return;
240
241 ret = recv(rte_intr_fd_get(intr_handle), buf, EAL_UEV_MSG_LEN,
242 MSG_DONTWAIT);
243 if (ret < 0 && errno == EAGAIN)
244 return;
245 else if (ret <= 0) {
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);
249 return;
250 }
251
252 ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
253 if (ret < 0) {
254 RTE_LOG(DEBUG, EAL, "Ignoring uevent '%s'\n", buf);
255 return;
256 }
257
258 RTE_LOG(DEBUG, EAL, "receive uevent(name:%s, type:%d, subsystem:%d)\n",
259 uevent.devname, uevent.type, uevent.subsystem);
260
261 switch (uevent.subsystem) {
262 case EAL_DEV_EVENT_SUBSYSTEM_PCI:
263 case EAL_DEV_EVENT_SUBSYSTEM_UIO:
264 busname = "pci";
265 break;
266 default:
267 break;
268 }
269
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);
274 if (bus == NULL) {
275 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n",
276 busname);
277 goto failure_handle_err;
278 }
279
280 dev = bus->find_device(NULL, cmp_dev_name,
281 uevent.devname);
282 if (dev == NULL) {
283 RTE_LOG(ERR, EAL, "Cannot find device (%s) on "
284 "bus (%s)\n", uevent.devname, busname);
285 goto failure_handle_err;
286 }
287
288 ret = bus->hot_unplug_handler(dev);
289 if (ret) {
290 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
291 "for device (%s)\n", dev->name);
292 }
293 rte_spinlock_unlock(&failure_handle_lock);
294 }
295 rte_dev_event_callback_process(uevent.devname, uevent.type);
296 free(uevent.devname);
297 }
298
299 return;
300
301 failure_handle_err:
302 rte_spinlock_unlock(&failure_handle_lock);
303 free(uevent.devname);
304 }
305
306 int
rte_dev_event_monitor_start(void)307 rte_dev_event_monitor_start(void)
308 {
309 int ret = 0;
310
311 rte_rwlock_write_lock(&monitor_lock);
312
313 if (monitor_refcount) {
314 monitor_refcount++;
315 goto exit;
316 }
317
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");
321 goto exit;
322 }
323
324 ret = rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_DEV_EVENT);
325 if (ret)
326 goto exit;
327
328 ret = rte_intr_fd_set(intr_handle, -1);
329 if (ret)
330 goto exit;
331
332 ret = dev_uev_socket_fd_create();
333 if (ret) {
334 RTE_LOG(ERR, EAL, "error create device event fd.\n");
335 goto exit;
336 }
337
338 ret = rte_intr_callback_register(intr_handle, dev_uev_handler, NULL);
339
340 if (ret) {
341 close(rte_intr_fd_get(intr_handle));
342 goto exit;
343 }
344
345 monitor_refcount++;
346
347 exit:
348 if (ret) {
349 rte_intr_instance_free(intr_handle);
350 intr_handle = NULL;
351 }
352 rte_rwlock_write_unlock(&monitor_lock);
353 return ret;
354 }
355
356 int
rte_dev_event_monitor_stop(void)357 rte_dev_event_monitor_stop(void)
358 {
359 int ret = 0;
360
361 rte_rwlock_write_lock(&monitor_lock);
362
363 if (!monitor_refcount) {
364 RTE_LOG(ERR, EAL, "device event monitor already stopped\n");
365 goto exit;
366 }
367
368 if (monitor_refcount > 1) {
369 monitor_refcount--;
370 goto exit;
371 }
372
373 ret = rte_intr_callback_unregister(intr_handle, dev_uev_handler,
374 (void *)-1);
375 if (ret < 0) {
376 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
377 goto exit;
378 }
379
380 close(rte_intr_fd_get(intr_handle));
381 rte_intr_instance_free(intr_handle);
382 intr_handle = NULL;
383 ret = 0;
384
385 monitor_refcount--;
386
387 exit:
388 rte_rwlock_write_unlock(&monitor_lock);
389
390 return ret;
391 }
392
393 int
dev_sigbus_handler_register(void)394 dev_sigbus_handler_register(void)
395 {
396 sigset_t mask;
397 struct sigaction action;
398
399 rte_errno = 0;
400
401 if (sigbus_need_recover)
402 return 0;
403
404 sigemptyset(&mask);
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);
410
411 return rte_errno;
412 }
413
414 int
dev_sigbus_handler_unregister(void)415 dev_sigbus_handler_unregister(void)
416 {
417 rte_errno = 0;
418
419 sigbus_action_recover();
420
421 return rte_errno;
422 }
423
424 int
rte_dev_hotplug_handle_enable(void)425 rte_dev_hotplug_handle_enable(void)
426 {
427 int ret = 0;
428
429 ret = dev_sigbus_handler_register();
430 if (ret < 0)
431 RTE_LOG(ERR, EAL,
432 "fail to register sigbus handler for devices.\n");
433
434 hotplug_handle = true;
435
436 return ret;
437 }
438
439 int
rte_dev_hotplug_handle_disable(void)440 rte_dev_hotplug_handle_disable(void)
441 {
442 int ret = 0;
443
444 ret = dev_sigbus_handler_unregister();
445 if (ret < 0)
446 RTE_LOG(ERR, EAL,
447 "fail to unregister sigbus handler for devices.\n");
448
449 hotplug_handle = false;
450
451 return ret;
452 }
453