1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2017 6WIND S.A. 3 * Copyright 2017 Mellanox Technologies, Ltd 4 */ 5 6 #include <errno.h> 7 #include <fcntl.h> 8 #include <inttypes.h> 9 #include <linux/sockios.h> 10 #include <linux/netlink.h> 11 #include <linux/rtnetlink.h> 12 #include <net/if.h> 13 #include <net/if_arp.h> 14 #include <netinet/ip.h> 15 #include <stdarg.h> 16 #include <stddef.h> 17 #include <stdlib.h> 18 #include <stdint.h> 19 #include <stdio.h> 20 #include <string.h> 21 #include <sys/ioctl.h> 22 #include <sys/queue.h> 23 #include <sys/socket.h> 24 #include <unistd.h> 25 26 #include <rte_alarm.h> 27 #include <rte_bus.h> 28 #include <rte_bus_vdev.h> 29 #include <rte_common.h> 30 #include <rte_config.h> 31 #include <rte_dev.h> 32 #include <rte_errno.h> 33 #include <rte_ethdev.h> 34 #include <rte_ether.h> 35 #include <rte_hypervisor.h> 36 #include <rte_kvargs.h> 37 #include <rte_log.h> 38 39 #define VDEV_NETVSC_DRIVER net_vdev_netvsc 40 #define VDEV_NETVSC_DRIVER_NAME RTE_STR(VDEV_NETVSC_DRIVER) 41 #define VDEV_NETVSC_ARG_IFACE "iface" 42 #define VDEV_NETVSC_ARG_MAC "mac" 43 #define VDEV_NETVSC_ARG_FORCE "force" 44 #define VDEV_NETVSC_ARG_IGNORE "ignore" 45 #define VDEV_NETVSC_PROBE_MS 1000 46 47 #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}" 48 #define NETVSC_MAX_ROUTE_LINE_SIZE 300 49 50 #define DRV_LOG(level, ...) \ 51 rte_log(RTE_LOG_ ## level, \ 52 vdev_netvsc_logtype, \ 53 RTE_FMT(VDEV_NETVSC_DRIVER_NAME ": " \ 54 RTE_FMT_HEAD(__VA_ARGS__,) "\n", \ 55 RTE_FMT_TAIL(__VA_ARGS__,))) 56 57 /** Driver-specific log messages type. */ 58 static int vdev_netvsc_logtype; 59 60 /** Context structure for a vdev_netvsc instance. */ 61 struct vdev_netvsc_ctx { 62 LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */ 63 unsigned int id; /**< Unique ID. */ 64 char name[64]; /**< Unique name. */ 65 char devname[64]; /**< Fail-safe instance name. */ 66 char devargs[256]; /**< Fail-safe device arguments. */ 67 char if_name[IF_NAMESIZE]; /**< NetVSC netdevice name. */ 68 unsigned int if_index; /**< NetVSC netdevice index. */ 69 struct ether_addr if_addr; /**< NetVSC MAC address. */ 70 int pipe[2]; /**< Fail-safe communication pipe. */ 71 char yield[256]; /**< PCI sub-device arguments. */ 72 }; 73 74 /** Context list is common to all driver instances. */ 75 static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list = 76 LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list); 77 78 /** Number of entries in context list. */ 79 static unsigned int vdev_netvsc_ctx_count; 80 81 /** Number of driver instances relying on context list. */ 82 static unsigned int vdev_netvsc_ctx_inst; 83 84 /** 85 * Destroy a vdev_netvsc context instance. 86 * 87 * @param ctx 88 * Context to destroy. 89 */ 90 static void 91 vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx) 92 { 93 if (ctx->pipe[0] != -1) 94 close(ctx->pipe[0]); 95 if (ctx->pipe[1] != -1) 96 close(ctx->pipe[1]); 97 free(ctx); 98 } 99 100 /** 101 * Determine if a network interface is NetVSC. 102 * 103 * @param[in] iface 104 * Pointer to netdevice description structure (name and index). 105 * 106 * @return 107 * A nonzero value when interface is detected as NetVSC. In case of error, 108 * rte_errno is updated and 0 returned. 109 */ 110 static int 111 vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface) 112 { 113 static const char temp[] = "/sys/class/net/%s/device/class_id"; 114 char path[sizeof(temp) + IF_NAMESIZE]; 115 FILE *f; 116 int ret; 117 int len = 0; 118 119 ret = snprintf(path, sizeof(path), temp, iface->if_name); 120 if (ret == -1 || (size_t)ret >= sizeof(path)) { 121 rte_errno = ENOBUFS; 122 return 0; 123 } 124 f = fopen(path, "r"); 125 if (!f) { 126 rte_errno = errno; 127 return 0; 128 } 129 ret = fscanf(f, NETVSC_CLASS_ID "%n", &len); 130 if (ret == EOF) 131 rte_errno = errno; 132 ret = len == (int)strlen(NETVSC_CLASS_ID); 133 fclose(f); 134 return ret; 135 } 136 137 /** 138 * Iterate over system network interfaces. 139 * 140 * This function runs a given callback function for each netdevice found on 141 * the system. 142 * 143 * @param func 144 * Callback function pointer. List traversal is aborted when this function 145 * returns a nonzero value. 146 * @param is_netvsc 147 * Indicates the device type to iterate - netvsc or non-netvsc. 148 * @param ... 149 * Variable parameter list passed as @p va_list to @p func. 150 * 151 * @return 152 * 0 when the entire list is traversed successfully, a negative error code 153 * in case or failure, or the nonzero value returned by @p func when list 154 * traversal is aborted. 155 */ 156 static int 157 vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface, 158 const struct ether_addr *eth_addr, 159 va_list ap), int is_netvsc, ...) 160 { 161 struct if_nameindex *iface = if_nameindex(); 162 int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 163 unsigned int i; 164 int ret = 0; 165 166 if (!iface) { 167 ret = -ENOBUFS; 168 DRV_LOG(ERR, "cannot retrieve system network interfaces"); 169 goto error; 170 } 171 if (s == -1) { 172 ret = -errno; 173 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno)); 174 goto error; 175 } 176 for (i = 0; iface[i].if_name; ++i) { 177 int is_netvsc_ret; 178 struct ifreq req; 179 struct ether_addr eth_addr; 180 va_list ap; 181 182 is_netvsc_ret = vdev_netvsc_iface_is_netvsc(&iface[i]) ? 1 : 0; 183 if (is_netvsc ^ is_netvsc_ret) 184 continue; 185 strncpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name)); 186 if (ioctl(s, SIOCGIFHWADDR, &req) == -1) { 187 DRV_LOG(WARNING, "cannot retrieve information about" 188 " interface \"%s\": %s", 189 req.ifr_name, rte_strerror(errno)); 190 continue; 191 } 192 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) { 193 DRV_LOG(DEBUG, "interface %s is non-ethernet device", 194 req.ifr_name); 195 continue; 196 } 197 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data, 198 RTE_DIM(eth_addr.addr_bytes)); 199 va_start(ap, is_netvsc); 200 ret = func(&iface[i], ð_addr, ap); 201 va_end(ap); 202 if (ret) 203 break; 204 } 205 error: 206 if (s != -1) 207 close(s); 208 if (iface) 209 if_freenameindex(iface); 210 return ret; 211 } 212 213 /** 214 * Determine if a network interface has a route. 215 * 216 * @param[in] name 217 * Network device name. 218 * @param[in] family 219 * Address family: AF_INET for IPv4 or AF_INET6 for IPv6. 220 * 221 * @return 222 * 1 when interface has a route, negative errno value in case of error and 223 * 0 otherwise. 224 */ 225 static int 226 vdev_netvsc_has_route(const struct if_nameindex *iface, 227 const unsigned char family) 228 { 229 /* 230 * The implementation can be simpler by getifaddrs() function usage but 231 * it works for IPv6 only starting from glibc 2.3.3. 232 */ 233 char buf[4096]; 234 int len; 235 int ret = 0; 236 int res; 237 int sock; 238 struct nlmsghdr *retmsg = (struct nlmsghdr *)buf; 239 struct sockaddr_nl sa; 240 struct { 241 struct nlmsghdr nlhdr; 242 struct ifaddrmsg addrmsg; 243 } msg; 244 245 if (!iface || (family != AF_INET && family != AF_INET6)) { 246 DRV_LOG(ERR, "%s", rte_strerror(EINVAL)); 247 return -EINVAL; 248 } 249 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); 250 if (sock == -1) { 251 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno)); 252 return -errno; 253 } 254 memset(&sa, 0, sizeof(sa)); 255 sa.nl_family = AF_NETLINK; 256 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR; 257 res = bind(sock, (struct sockaddr *)&sa, sizeof(sa)); 258 if (res == -1) { 259 ret = -errno; 260 DRV_LOG(ERR, "cannot bind socket: %s", rte_strerror(errno)); 261 goto close; 262 } 263 memset(&msg, 0, sizeof(msg)); 264 msg.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); 265 msg.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; 266 msg.nlhdr.nlmsg_type = RTM_GETADDR; 267 msg.nlhdr.nlmsg_pid = getpid(); 268 msg.addrmsg.ifa_family = family; 269 msg.addrmsg.ifa_index = iface->if_index; 270 res = send(sock, &msg, msg.nlhdr.nlmsg_len, 0); 271 if (res == -1) { 272 ret = -errno; 273 DRV_LOG(ERR, "cannot send socket message: %s", 274 rte_strerror(errno)); 275 goto close; 276 } 277 memset(buf, 0, sizeof(buf)); 278 len = recv(sock, buf, sizeof(buf), 0); 279 if (len == -1) { 280 ret = -errno; 281 DRV_LOG(ERR, "cannot receive socket message: %s", 282 rte_strerror(errno)); 283 goto close; 284 } 285 while (NLMSG_OK(retmsg, (unsigned int)len)) { 286 struct ifaddrmsg *retaddr = 287 (struct ifaddrmsg *)NLMSG_DATA(retmsg); 288 289 if (retaddr->ifa_family == family && 290 retaddr->ifa_index == iface->if_index) { 291 struct rtattr *retrta = IFA_RTA(retaddr); 292 int attlen = IFA_PAYLOAD(retmsg); 293 294 while (RTA_OK(retrta, attlen)) { 295 if (retrta->rta_type == IFA_ADDRESS) { 296 ret = 1; 297 DRV_LOG(DEBUG, "interface %s has IP", 298 iface->if_name); 299 goto close; 300 } 301 retrta = RTA_NEXT(retrta, attlen); 302 } 303 } 304 retmsg = NLMSG_NEXT(retmsg, len); 305 } 306 close: 307 close(sock); 308 return ret; 309 } 310 311 /** 312 * Retrieve network interface data from sysfs symbolic link. 313 * 314 * @param[out] buf 315 * Output data buffer. 316 * @param size 317 * Output buffer size. 318 * @param[in] if_name 319 * Netdevice name. 320 * @param[in] relpath 321 * Symbolic link path relative to netdevice sysfs entry. 322 * 323 * @return 324 * 0 on success, a negative error code otherwise. 325 */ 326 static int 327 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name, 328 const char *relpath) 329 { 330 struct vdev_netvsc_ctx *ctx; 331 char in[RTE_MAX(sizeof(ctx->yield), 256u)]; 332 int ret; 333 334 ret = snprintf(in, sizeof(in) - 1, "/sys/class/net/%s/%s", 335 if_name, relpath); 336 if (ret == -1 || (size_t)ret >= sizeof(in)) 337 return -ENOBUFS; 338 ret = readlink(in, buf, size); 339 if (ret == -1) 340 return -errno; 341 if ((size_t)ret >= size - 1) 342 return -ENOBUFS; 343 buf[ret] = '\0'; 344 return 0; 345 } 346 347 /** 348 * Probe a network interface to associate with vdev_netvsc context. 349 * 350 * This function determines if the network device matches the properties of 351 * the NetVSC interface associated with the vdev_netvsc context and 352 * communicates its bus address to the fail-safe PMD instance if so. 353 * 354 * It is normally used with vdev_netvsc_foreach_iface(). 355 * 356 * @param[in] iface 357 * Pointer to netdevice description structure (name and index). 358 * @param[in] eth_addr 359 * MAC address associated with @p iface. 360 * @param ap 361 * Variable arguments list comprising: 362 * 363 * - struct vdev_netvsc_ctx *ctx: 364 * Context to associate network interface with. 365 * 366 * @return 367 * A nonzero value when interface matches, 0 otherwise or in case of 368 * error. 369 */ 370 static int 371 vdev_netvsc_device_probe(const struct if_nameindex *iface, 372 const struct ether_addr *eth_addr, 373 va_list ap) 374 { 375 struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *); 376 char buf[RTE_MAX(sizeof(ctx->yield), 256u)]; 377 const char *addr; 378 size_t len; 379 int ret; 380 381 /* Skip non-matching or unwanted NetVSC interfaces. */ 382 if (ctx->if_index == iface->if_index) { 383 if (!strcmp(ctx->if_name, iface->if_name)) 384 return 0; 385 DRV_LOG(DEBUG, 386 "NetVSC interface \"%s\" (index %u) renamed \"%s\"", 387 ctx->if_name, ctx->if_index, iface->if_name); 388 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name)); 389 return 0; 390 } 391 if (!is_same_ether_addr(eth_addr, &ctx->if_addr)) 392 return 0; 393 /* Look for associated PCI device. */ 394 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name, 395 "device/subsystem"); 396 if (ret) 397 return 0; 398 addr = strrchr(buf, '/'); 399 addr = addr ? addr + 1 : buf; 400 if (strcmp(addr, "pci")) 401 return 0; 402 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name, 403 "device"); 404 if (ret) 405 return 0; 406 addr = strrchr(buf, '/'); 407 addr = addr ? addr + 1 : buf; 408 len = strlen(addr); 409 if (!len) 410 return 0; 411 /* Send PCI device argument to fail-safe PMD instance. */ 412 if (strcmp(addr, ctx->yield)) 413 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC" 414 " interface \"%s\" (index %u)", addr, ctx->if_name, 415 ctx->if_index); 416 memmove(buf, addr, len + 1); 417 addr = buf; 418 buf[len] = '\n'; 419 ret = write(ctx->pipe[1], addr, len + 1); 420 buf[len] = '\0'; 421 if (ret == -1) { 422 if (errno == EINTR || errno == EAGAIN) 423 return 1; 424 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with" 425 " interface \"%s\": %s", addr, ctx->if_name, 426 rte_strerror(errno)); 427 return 1; 428 } 429 if ((size_t)ret != len + 1) { 430 /* 431 * Attempt to override previous partial write, no need to 432 * recover if that fails. 433 */ 434 ret = write(ctx->pipe[1], "\n", 1); 435 (void)ret; 436 return 1; 437 } 438 fsync(ctx->pipe[1]); 439 memcpy(ctx->yield, addr, len + 1); 440 return 1; 441 } 442 443 /** 444 * Alarm callback that regularly probes system network interfaces. 445 * 446 * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as 447 * long as an vdev_netvsc context instance exists. 448 * 449 * @param arg 450 * Ignored. 451 */ 452 static void 453 vdev_netvsc_alarm(__rte_unused void *arg) 454 { 455 struct vdev_netvsc_ctx *ctx; 456 int ret; 457 458 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) { 459 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0, 460 ctx); 461 if (ret < 0) 462 break; 463 } 464 if (!vdev_netvsc_ctx_count) 465 return; 466 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000, 467 vdev_netvsc_alarm, NULL); 468 if (ret < 0) { 469 DRV_LOG(ERR, "unable to reschedule alarm callback: %s", 470 rte_strerror(-ret)); 471 } 472 } 473 474 /** 475 * Probe a NetVSC interface to generate a vdev_netvsc context from. 476 * 477 * This function instantiates vdev_netvsc contexts either for all NetVSC 478 * devices found on the system or only a subset provided as device 479 * arguments. 480 * 481 * It is normally used with vdev_netvsc_foreach_iface(). 482 * 483 * @param[in] iface 484 * Pointer to netdevice description structure (name and index). 485 * @param[in] eth_addr 486 * MAC address associated with @p iface. 487 * @param ap 488 * Variable arguments list comprising: 489 * 490 * - const char *name: 491 * Name associated with current driver instance. 492 * 493 * - struct rte_kvargs *kvargs: 494 * Device arguments provided to current driver instance. 495 * 496 * - int force: 497 * Accept specified interface even if not detected as NetVSC. 498 * 499 * - unsigned int specified: 500 * Number of specific netdevices provided as device arguments. 501 * 502 * - unsigned int *matched: 503 * The number of specified netdevices matched by this function. 504 * 505 * @return 506 * A nonzero value when interface matches, 0 otherwise or in case of 507 * error. 508 */ 509 static int 510 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface, 511 const struct ether_addr *eth_addr, 512 va_list ap) 513 { 514 const char *name = va_arg(ap, const char *); 515 struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *); 516 unsigned int specified = va_arg(ap, unsigned int); 517 unsigned int *matched = va_arg(ap, unsigned int *); 518 unsigned int i; 519 struct vdev_netvsc_ctx *ctx; 520 int ret; 521 522 /* Probe all interfaces when none are specified. */ 523 if (specified) { 524 for (i = 0; i != kvargs->count; ++i) { 525 const struct rte_kvargs_pair *pair = &kvargs->pairs[i]; 526 527 if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) { 528 if (!strcmp(pair->value, iface->if_name)) 529 break; 530 } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) { 531 struct ether_addr tmp; 532 533 if (sscanf(pair->value, 534 "%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":" 535 "%" SCNx8 ":%" SCNx8 ":%" SCNx8, 536 &tmp.addr_bytes[0], 537 &tmp.addr_bytes[1], 538 &tmp.addr_bytes[2], 539 &tmp.addr_bytes[3], 540 &tmp.addr_bytes[4], 541 &tmp.addr_bytes[5]) != 6) { 542 DRV_LOG(ERR, 543 "invalid MAC address format" 544 " \"%s\"", 545 pair->value); 546 return -EINVAL; 547 } 548 if (is_same_ether_addr(eth_addr, &tmp)) 549 break; 550 } 551 } 552 if (i == kvargs->count) 553 return 0; 554 ++(*matched); 555 } 556 /* Weed out interfaces already handled. */ 557 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) 558 if (ctx->if_index == iface->if_index) 559 break; 560 if (ctx) { 561 if (!specified) 562 return 0; 563 DRV_LOG(WARNING, 564 "interface \"%s\" (index %u) is already handled," 565 " skipping", 566 iface->if_name, iface->if_index); 567 return 0; 568 } 569 /* Routed NetVSC should not be probed. */ 570 if (vdev_netvsc_has_route(iface, AF_INET) || 571 vdev_netvsc_has_route(iface, AF_INET6)) { 572 if (!specified) 573 return 0; 574 DRV_LOG(WARNING, "probably using routed NetVSC interface \"%s\"" 575 " (index %u)", iface->if_name, iface->if_index); 576 } 577 /* Create interface context. */ 578 ctx = calloc(1, sizeof(*ctx)); 579 if (!ctx) { 580 ret = -errno; 581 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s", 582 iface->if_name, rte_strerror(errno)); 583 goto error; 584 } 585 ctx->id = vdev_netvsc_ctx_count; 586 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name)); 587 ctx->if_index = iface->if_index; 588 ctx->if_addr = *eth_addr; 589 ctx->pipe[0] = -1; 590 ctx->pipe[1] = -1; 591 ctx->yield[0] = '\0'; 592 if (pipe(ctx->pipe) == -1) { 593 ret = -errno; 594 DRV_LOG(ERR, 595 "cannot allocate control pipe for interface \"%s\": %s", 596 ctx->if_name, rte_strerror(errno)); 597 goto error; 598 } 599 for (i = 0; i != RTE_DIM(ctx->pipe); ++i) { 600 int flf = fcntl(ctx->pipe[i], F_GETFL); 601 602 if (flf != -1 && 603 fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1) 604 continue; 605 ret = -errno; 606 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file" 607 " descriptor #%u (%d): %s", i, ctx->pipe[i], 608 rte_strerror(errno)); 609 goto error; 610 } 611 /* Generate virtual device name and arguments. */ 612 i = 0; 613 ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u", 614 name, ctx->id); 615 if (ret == -1 || (size_t)ret >= sizeof(ctx->name)) 616 ++i; 617 ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_vsc%u", 618 ctx->id); 619 if (ret == -1 || (size_t)ret >= sizeof(ctx->devname)) 620 ++i; 621 ret = snprintf(ctx->devargs, sizeof(ctx->devargs), 622 "fd(%d),dev(net_tap_vsc%u,remote=%s)", 623 ctx->pipe[0], ctx->id, ctx->if_name); 624 if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs)) 625 ++i; 626 if (i) { 627 ret = -ENOBUFS; 628 DRV_LOG(ERR, "generated virtual device name or argument list" 629 " too long for interface \"%s\"", ctx->if_name); 630 goto error; 631 } 632 /* Request virtual device generation. */ 633 DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"", 634 ctx->devname, ctx->devargs); 635 vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0, ctx); 636 ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs); 637 if (ret) 638 goto error; 639 LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry); 640 ++vdev_netvsc_ctx_count; 641 DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list", 642 ctx->if_name); 643 return 0; 644 error: 645 if (ctx) 646 vdev_netvsc_ctx_destroy(ctx); 647 return ret; 648 } 649 650 /** 651 * Probe NetVSC interfaces. 652 * 653 * This function probes system netdevices according to the specified device 654 * arguments and starts a periodic alarm callback to notify the resulting 655 * fail-safe PMD instances of their sub-devices whereabouts. 656 * 657 * @param dev 658 * Virtual device context for driver instance. 659 * 660 * @return 661 * Always 0, even in case of errors. 662 */ 663 static int 664 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev) 665 { 666 static const char *const vdev_netvsc_arg[] = { 667 VDEV_NETVSC_ARG_IFACE, 668 VDEV_NETVSC_ARG_MAC, 669 VDEV_NETVSC_ARG_FORCE, 670 VDEV_NETVSC_ARG_IGNORE, 671 NULL, 672 }; 673 const char *name = rte_vdev_device_name(dev); 674 const char *args = rte_vdev_device_args(dev); 675 struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "", 676 vdev_netvsc_arg); 677 unsigned int specified = 0; 678 unsigned int matched = 0; 679 int force = 0; 680 int ignore = 0; 681 unsigned int i; 682 int ret; 683 684 DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args); 685 if (!kvargs) { 686 DRV_LOG(ERR, "cannot parse arguments list"); 687 goto error; 688 } 689 for (i = 0; i != kvargs->count; ++i) { 690 const struct rte_kvargs_pair *pair = &kvargs->pairs[i]; 691 692 if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE)) 693 force = !!atoi(pair->value); 694 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IGNORE)) 695 ignore = !!atoi(pair->value); 696 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) || 697 !strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) 698 ++specified; 699 } 700 if (ignore) { 701 if (kvargs) 702 rte_kvargs_free(kvargs); 703 return 0; 704 } 705 if (specified > 1) { 706 DRV_LOG(ERR, "More than one way used to specify the netvsc" 707 " device."); 708 goto error; 709 } 710 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL); 711 /* Gather interfaces. */ 712 ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 1, name, 713 kvargs, specified, &matched); 714 if (ret < 0) 715 goto error; 716 if (specified && matched < specified) { 717 if (!force) { 718 DRV_LOG(ERR, "Cannot find the specified netvsc device"); 719 goto error; 720 } 721 /* Try to force probing on non-netvsc specified device. */ 722 if (vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 0, name, 723 kvargs, specified, &matched) < 0) 724 goto error; 725 if (matched < specified) { 726 DRV_LOG(ERR, "Cannot find the specified device"); 727 goto error; 728 } 729 DRV_LOG(WARNING, "non-netvsc device was probed as netvsc"); 730 } 731 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000, 732 vdev_netvsc_alarm, NULL); 733 if (ret < 0) { 734 DRV_LOG(ERR, "unable to schedule alarm callback: %s", 735 rte_strerror(-ret)); 736 goto error; 737 } 738 error: 739 if (kvargs) 740 rte_kvargs_free(kvargs); 741 ++vdev_netvsc_ctx_inst; 742 return 0; 743 } 744 745 /** 746 * Remove driver instance. 747 * 748 * The alarm callback and underlying vdev_netvsc context instances are only 749 * destroyed after the last PMD instance is removed. 750 * 751 * @param dev 752 * Virtual device context for driver instance. 753 * 754 * @return 755 * Always 0. 756 */ 757 static int 758 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev) 759 { 760 if (--vdev_netvsc_ctx_inst) 761 return 0; 762 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL); 763 while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) { 764 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list); 765 766 LIST_REMOVE(ctx, entry); 767 --vdev_netvsc_ctx_count; 768 vdev_netvsc_ctx_destroy(ctx); 769 } 770 return 0; 771 } 772 773 /** Virtual device descriptor. */ 774 static struct rte_vdev_driver vdev_netvsc_vdev = { 775 .probe = vdev_netvsc_vdev_probe, 776 .remove = vdev_netvsc_vdev_remove, 777 }; 778 779 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev); 780 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc); 781 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc, 782 VDEV_NETVSC_ARG_IFACE "=<string> " 783 VDEV_NETVSC_ARG_MAC "=<string> " 784 VDEV_NETVSC_ARG_FORCE "=<int> " 785 VDEV_NETVSC_ARG_IGNORE "=<int>"); 786 787 /** Initialize driver log type. */ 788 RTE_INIT(vdev_netvsc_init_log) 789 { 790 vdev_netvsc_logtype = rte_log_register("pmd.vdev_netvsc"); 791 if (vdev_netvsc_logtype >= 0) 792 rte_log_set_level(vdev_netvsc_logtype, RTE_LOG_NOTICE); 793 } 794 795 /** Compare function for vdev find device operation. */ 796 static int 797 vdev_netvsc_cmp_rte_device(const struct rte_device *dev1, 798 __rte_unused const void *_dev2) 799 { 800 return strcmp(dev1->devargs->name, VDEV_NETVSC_DRIVER_NAME); 801 } 802 803 /** 804 * A callback called by vdev bus scan function to ensure this driver probing 805 * automatically in Hyper-V VM system unless it already exists in the 806 * devargs list. 807 */ 808 static void 809 vdev_netvsc_scan_callback(__rte_unused void *arg) 810 { 811 struct rte_vdev_device *dev; 812 struct rte_devargs *devargs; 813 struct rte_bus *vbus = rte_bus_find_by_name("vdev"); 814 815 RTE_EAL_DEVARGS_FOREACH("vdev", devargs) 816 if (!strcmp(devargs->name, VDEV_NETVSC_DRIVER_NAME)) 817 return; 818 dev = (struct rte_vdev_device *)vbus->find_device(NULL, 819 vdev_netvsc_cmp_rte_device, VDEV_NETVSC_DRIVER_NAME); 820 if (dev) 821 return; 822 if (rte_devargs_add(RTE_DEVTYPE_VIRTUAL, VDEV_NETVSC_DRIVER_NAME)) 823 DRV_LOG(ERR, "unable to add netvsc devargs."); 824 } 825 826 /** Initialize the custom scan. */ 827 RTE_INIT(vdev_netvsc_custom_scan_add) 828 { 829 if (rte_hypervisor_get() == RTE_HYPERVISOR_HYPERV) 830 rte_vdev_add_custom_scan(vdev_netvsc_scan_callback, NULL); 831 } 832