test/bonding: fix MAC assignment for re-run
[dpdk.git] / lib / librte_eal / linux / 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 catch SIGBUS, fault address:%p\n",
70                 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                                 goto failure_handle_err;
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                                 goto failure_handle_err;
270                         }
271
272                         ret = bus->hot_unplug_handler(dev);
273                         if (ret) {
274                                 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
275                                         "for device (%s)\n", dev->name);
276                         }
277                         rte_spinlock_unlock(&failure_handle_lock);
278                 }
279                 rte_dev_event_callback_process(uevent.devname, uevent.type);
280         }
281
282         return;
283
284 failure_handle_err:
285         rte_spinlock_unlock(&failure_handle_lock);
286 }
287
288 int __rte_experimental
289 rte_dev_event_monitor_start(void)
290 {
291         int ret;
292
293         if (monitor_started)
294                 return 0;
295
296         ret = dev_uev_socket_fd_create();
297         if (ret) {
298                 RTE_LOG(ERR, EAL, "error create device event fd.\n");
299                 return -1;
300         }
301
302         intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT;
303         ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL);
304
305         if (ret) {
306                 RTE_LOG(ERR, EAL, "fail to register uevent callback.\n");
307                 return -1;
308         }
309
310         monitor_started = true;
311
312         return 0;
313 }
314
315 int __rte_experimental
316 rte_dev_event_monitor_stop(void)
317 {
318         int ret;
319
320         if (!monitor_started)
321                 return 0;
322
323         ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
324                                            (void *)-1);
325         if (ret < 0) {
326                 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
327                 return ret;
328         }
329
330         close(intr_handle.fd);
331         intr_handle.fd = -1;
332         monitor_started = false;
333
334         return 0;
335 }
336
337 int
338 dev_sigbus_handler_register(void)
339 {
340         sigset_t mask;
341         struct sigaction action;
342
343         rte_errno = 0;
344
345         if (sigbus_need_recover)
346                 return 0;
347
348         sigemptyset(&mask);
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);
354
355         return rte_errno;
356 }
357
358 int
359 dev_sigbus_handler_unregister(void)
360 {
361         rte_errno = 0;
362
363         sigbus_action_recover();
364
365         return rte_errno;
366 }
367
368 int __rte_experimental
369 rte_dev_hotplug_handle_enable(void)
370 {
371         int ret = 0;
372
373         ret = dev_sigbus_handler_register();
374         if (ret < 0)
375                 RTE_LOG(ERR, EAL,
376                         "fail to register sigbus handler for devices.\n");
377
378         hotplug_handle = true;
379
380         return ret;
381 }
382
383 int __rte_experimental
384 rte_dev_hotplug_handle_disable(void)
385 {
386         int ret = 0;
387
388         ret = dev_sigbus_handler_unregister();
389         if (ret < 0)
390                 RTE_LOG(ERR, EAL,
391                         "fail to unregister sigbus handler for devices.\n");
392
393         hotplug_handle = false;
394
395         return ret;
396 }