New upstream version 18.11-rc1
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_dev.c
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 <fcntl.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_compat.h>
15 #include <rte_dev.h>
16 #include <rte_malloc.h>
17 #include <rte_interrupts.h>
18 #include <rte_alarm.h>
19 #include <rte_bus.h>
20 #include <rte_eal.h>
21 #include <rte_spinlock.h>
22 #include <rte_errno.h>
23
24 #include "eal_private.h"
25
26 static struct rte_intr_handle intr_handle = {.fd = -1 };
27 static bool monitor_started;
28 static bool hotplug_handle;
29
30 #define EAL_UEV_MSG_LEN 4096
31 #define EAL_UEV_MSG_ELEM_LEN 128
32
33 /*
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
37  * race condition.
38  */
39 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
40
41 static struct sigaction sigbus_action_old;
42
43 static int sigbus_need_recover;
44
45 static void dev_uev_handler(__rte_unused void *param);
46
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
53 };
54
55 static void
56 sigbus_action_recover(void)
57 {
58         if (sigbus_need_recover) {
59                 sigaction(SIGBUS, &sigbus_action_old, NULL);
60                 sigbus_need_recover = 0;
61         }
62 }
63
64 static void sigbus_handler(int signum, siginfo_t *info,
65                                 void *ctx __rte_unused)
66 {
67         int ret;
68
69         RTE_LOG(DEBUG, EAL, "Thread[%d] catch SIGBUS, fault address:%p\n",
70                 (int)pthread_self(), info->si_addr);
71
72         rte_spinlock_lock(&failure_handle_lock);
73         ret = rte_bus_sigbus_handler(info->si_addr);
74         rte_spinlock_unlock(&failure_handle_lock);
75         if (ret == -1) {
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,
83                                                             info, ctx);
84                 } else if (sigbus_action_old.sa_flags != SA_SIGINFO
85                            && sigbus_action_old.sa_handler) {
86                         (*(sigbus_action_old.sa_handler))(signum);
87                 } else {
88                         rte_exit(EXIT_FAILURE,
89                                  "Failed to handle generic SIGBUS!");
90                 }
91         }
92
93         RTE_LOG(DEBUG, EAL, "Success to handle SIGBUS for hot-unplug!\n");
94 }
95
96 static int cmp_dev_name(const struct rte_device *dev,
97         const void *_name)
98 {
99         const char *name = _name;
100
101         return strcmp(dev->name, name);
102 }
103
104 static int
105 dev_uev_socket_fd_create(void)
106 {
107         struct sockaddr_nl addr;
108         int ret;
109
110         intr_handle.fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC |
111                         SOCK_NONBLOCK,
112                         NETLINK_KOBJECT_UEVENT);
113         if (intr_handle.fd < 0) {
114                 RTE_LOG(ERR, EAL, "create uevent fd failed.\n");
115                 return -1;
116         }
117
118         memset(&addr, 0, sizeof(addr));
119         addr.nl_family = AF_NETLINK;
120         addr.nl_pid = 0;
121         addr.nl_groups = 0xffffffff;
122
123         ret = bind(intr_handle.fd, (struct sockaddr *) &addr, sizeof(addr));
124         if (ret < 0) {
125                 RTE_LOG(ERR, EAL, "Failed to bind uevent socket.\n");
126                 goto err;
127         }
128
129         return 0;
130 err:
131         close(intr_handle.fd);
132         intr_handle.fd = -1;
133         return ret;
134 }
135
136 static int
137 dev_uev_parse(const char *buf, struct rte_dev_event *event, int length)
138 {
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];
142         int i = 0;
143
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);
147
148         while (i < length) {
149                 for (; i < length; i++) {
150                         if (*buf)
151                                 break;
152                         buf++;
153                 }
154                 /**
155                  * check device uevent from kernel side, no need to check
156                  * uevent from udev.
157                  */
158                 if (!strncmp(buf, "libudev", 7)) {
159                         buf += 7;
160                         i += 7;
161                         return -1;
162                 }
163                 if (!strncmp(buf, "ACTION=", 7)) {
164                         buf += 7;
165                         i += 7;
166                         strlcpy(action, buf, sizeof(action));
167                 } else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
168                         buf += 10;
169                         i += 10;
170                         strlcpy(subsystem, buf, sizeof(subsystem));
171                 } else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
172                         buf += 14;
173                         i += 14;
174                         strlcpy(pci_slot_name, buf, sizeof(subsystem));
175                         event->devname = strdup(pci_slot_name);
176                 }
177                 for (; i < length; i++) {
178                         if (*buf == '\0')
179                                 break;
180                         buf++;
181                 }
182         }
183
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;
191         else
192                 return -1;
193
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;
199         else
200                 return -1;
201         return 0;
202 }
203
204 static void
205 dev_delayed_unregister(void *param)
206 {
207         rte_intr_callback_unregister(&intr_handle, dev_uev_handler, param);
208         close(intr_handle.fd);
209         intr_handle.fd = -1;
210 }
211
212 static void
213 dev_uev_handler(__rte_unused void *param)
214 {
215         struct rte_dev_event uevent;
216         int ret;
217         char buf[EAL_UEV_MSG_LEN];
218         struct rte_bus *bus;
219         struct rte_device *dev;
220         const char *busname = "";
221
222         memset(&uevent, 0, sizeof(struct rte_dev_event));
223         memset(buf, 0, EAL_UEV_MSG_LEN);
224
225         ret = recv(intr_handle.fd, buf, EAL_UEV_MSG_LEN, MSG_DONTWAIT);
226         if (ret < 0 && errno == EAGAIN)
227                 return;
228         else if (ret <= 0) {
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);
232                 return;
233         }
234
235         ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
236         if (ret < 0) {
237                 RTE_LOG(DEBUG, EAL, "It is not an valid event "
238                         "that need to be handle.\n");
239                 return;
240         }
241
242         RTE_LOG(DEBUG, EAL, "receive uevent(name:%s, type:%d, subsystem:%d)\n",
243                 uevent.devname, uevent.type, uevent.subsystem);
244
245         switch (uevent.subsystem) {
246         case EAL_DEV_EVENT_SUBSYSTEM_PCI:
247         case EAL_DEV_EVENT_SUBSYSTEM_UIO:
248                 busname = "pci";
249                 break;
250         default:
251                 break;
252         }
253
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);
258                         if (bus == NULL) {
259                                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n",
260                                         busname);
261                                 return;
262                         }
263
264                         dev = bus->find_device(NULL, cmp_dev_name,
265                                                uevent.devname);
266                         if (dev == NULL) {
267                                 RTE_LOG(ERR, EAL, "Cannot find device (%s) on "
268                                         "bus (%s)\n", uevent.devname, busname);
269                                 return;
270                         }
271
272                         ret = bus->hot_unplug_handler(dev);
273                         rte_spinlock_unlock(&failure_handle_lock);
274                         if (ret) {
275                                 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
276                                         "for device (%s)\n", dev->name);
277                                 return;
278                         }
279                 }
280                 rte_dev_event_callback_process(uevent.devname, uevent.type);
281         }
282 }
283
284 int __rte_experimental
285 rte_dev_event_monitor_start(void)
286 {
287         int ret;
288
289         if (monitor_started)
290                 return 0;
291
292         ret = dev_uev_socket_fd_create();
293         if (ret) {
294                 RTE_LOG(ERR, EAL, "error create device event fd.\n");
295                 return -1;
296         }
297
298         intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT;
299         ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL);
300
301         if (ret) {
302                 RTE_LOG(ERR, EAL, "fail to register uevent callback.\n");
303                 return -1;
304         }
305
306         monitor_started = true;
307
308         return 0;
309 }
310
311 int __rte_experimental
312 rte_dev_event_monitor_stop(void)
313 {
314         int ret;
315
316         if (!monitor_started)
317                 return 0;
318
319         ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
320                                            (void *)-1);
321         if (ret < 0) {
322                 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
323                 return ret;
324         }
325
326         close(intr_handle.fd);
327         intr_handle.fd = -1;
328         monitor_started = false;
329
330         return 0;
331 }
332
333 int
334 dev_sigbus_handler_register(void)
335 {
336         sigset_t mask;
337         struct sigaction action;
338
339         rte_errno = 0;
340
341         if (sigbus_need_recover)
342                 return 0;
343
344         sigemptyset(&mask);
345         sigaddset(&mask, SIGBUS);
346         action.sa_flags = SA_SIGINFO;
347         action.sa_mask = mask;
348         action.sa_sigaction = sigbus_handler;
349         sigbus_need_recover = !sigaction(SIGBUS, &action, &sigbus_action_old);
350
351         return rte_errno;
352 }
353
354 int
355 dev_sigbus_handler_unregister(void)
356 {
357         rte_errno = 0;
358
359         sigbus_action_recover();
360
361         return rte_errno;
362 }
363
364 int __rte_experimental
365 rte_dev_hotplug_handle_enable(void)
366 {
367         int ret = 0;
368
369         ret = dev_sigbus_handler_register();
370         if (ret < 0)
371                 RTE_LOG(ERR, EAL,
372                         "fail to register sigbus handler for devices.\n");
373
374         hotplug_handle = true;
375
376         return ret;
377 }
378
379 int __rte_experimental
380 rte_dev_hotplug_handle_disable(void)
381 {
382         int ret = 0;
383
384         ret = dev_sigbus_handler_unregister();
385         if (ret < 0)
386                 RTE_LOG(ERR, EAL,
387                         "fail to unregister sigbus handler for devices.\n");
388
389         hotplug_handle = false;
390
391         return ret;
392 }