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