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