1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd.
9 #include <linux/sockios.h>
11 #include <net/if_arp.h>
12 #include <netinet/ip.h>
19 #include <sys/ioctl.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <rte_alarm.h>
26 #include <rte_bus_vdev.h>
27 #include <rte_common.h>
28 #include <rte_config.h>
30 #include <rte_errno.h>
31 #include <rte_ethdev.h>
32 #include <rte_ether.h>
33 #include <rte_hypervisor.h>
34 #include <rte_kvargs.h>
37 #define VDEV_NETVSC_DRIVER net_vdev_netvsc
38 #define VDEV_NETVSC_DRIVER_NAME RTE_STR(VDEV_NETVSC_DRIVER)
39 #define VDEV_NETVSC_ARG_IFACE "iface"
40 #define VDEV_NETVSC_ARG_MAC "mac"
41 #define VDEV_NETVSC_ARG_FORCE "force"
42 #define VDEV_NETVSC_ARG_IGNORE "ignore"
43 #define VDEV_NETVSC_PROBE_MS 1000
45 #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}"
46 #define NETVSC_MAX_ROUTE_LINE_SIZE 300
48 #define DRV_LOG(level, ...) \
49 rte_log(RTE_LOG_ ## level, \
50 vdev_netvsc_logtype, \
51 RTE_FMT(VDEV_NETVSC_DRIVER_NAME ": " \
52 RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
53 RTE_FMT_TAIL(__VA_ARGS__,)))
55 /** Driver-specific log messages type. */
56 static int vdev_netvsc_logtype;
58 /** Context structure for a vdev_netvsc instance. */
59 struct vdev_netvsc_ctx {
60 LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */
61 unsigned int id; /**< Unique ID. */
62 char name[64]; /**< Unique name. */
63 char devname[64]; /**< Fail-safe instance name. */
64 char devargs[256]; /**< Fail-safe device arguments. */
65 char if_name[IF_NAMESIZE]; /**< NetVSC netdevice name. */
66 unsigned int if_index; /**< NetVSC netdevice index. */
67 struct ether_addr if_addr; /**< NetVSC MAC address. */
68 int pipe[2]; /**< Fail-safe communication pipe. */
69 char yield[256]; /**< PCI sub-device arguments. */
72 /** Context list is common to all driver instances. */
73 static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list =
74 LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list);
76 /** Number of entries in context list. */
77 static unsigned int vdev_netvsc_ctx_count;
79 /** Number of driver instances relying on context list. */
80 static unsigned int vdev_netvsc_ctx_inst;
83 * Destroy a vdev_netvsc context instance.
89 vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
91 if (ctx->pipe[0] != -1)
93 if (ctx->pipe[1] != -1)
99 * Iterate over system network interfaces.
101 * This function runs a given callback function for each netdevice found on
105 * Callback function pointer. List traversal is aborted when this function
106 * returns a nonzero value.
108 * Variable parameter list passed as @p va_list to @p func.
111 * 0 when the entire list is traversed successfully, a negative error code
112 * in case or failure, or the nonzero value returned by @p func when list
113 * traversal is aborted.
116 vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
117 const struct ether_addr *eth_addr,
120 struct if_nameindex *iface = if_nameindex();
121 int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
127 DRV_LOG(ERR, "cannot retrieve system network interfaces");
132 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
135 for (i = 0; iface[i].if_name; ++i) {
137 struct ether_addr eth_addr;
140 strncpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name));
141 if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
142 DRV_LOG(WARNING, "cannot retrieve information about"
143 " interface \"%s\": %s",
144 req.ifr_name, rte_strerror(errno));
147 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
148 DRV_LOG(DEBUG, "interface %s is non-ethernet device",
152 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
153 RTE_DIM(eth_addr.addr_bytes));
155 ret = func(&iface[i], ð_addr, ap);
164 if_freenameindex(iface);
169 * Determine if a network interface is NetVSC.
172 * Pointer to netdevice description structure (name and index).
175 * A nonzero value when interface is detected as NetVSC. In case of error,
176 * rte_errno is updated and 0 returned.
179 vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
181 static const char temp[] = "/sys/class/net/%s/device/class_id";
182 char path[sizeof(temp) + IF_NAMESIZE];
187 ret = snprintf(path, sizeof(path), temp, iface->if_name);
188 if (ret == -1 || (size_t)ret >= sizeof(path)) {
192 f = fopen(path, "r");
197 ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
200 ret = len == (int)strlen(NETVSC_CLASS_ID);
206 * Determine if a network interface has a route.
209 * Network device name.
212 * A nonzero value when interface has an route. In case of error,
213 * rte_errno is updated and 0 returned.
216 vdev_netvsc_has_route(const char *name)
220 char route[NETVSC_MAX_ROUTE_LINE_SIZE];
223 fp = fopen("/proc/net/route", "r");
228 while (fgets(route, NETVSC_MAX_ROUTE_LINE_SIZE, fp) != NULL) {
229 netdev = strtok(route, "\t");
230 if (strcmp(netdev, name) == 0) {
234 /* Move file pointer to the next line. */
235 while (strchr(route, '\n') == NULL &&
236 fgets(route, NETVSC_MAX_ROUTE_LINE_SIZE, fp) != NULL)
244 * Retrieve network interface data from sysfs symbolic link.
247 * Output data buffer.
249 * Output buffer size.
253 * Symbolic link path relative to netdevice sysfs entry.
256 * 0 on success, a negative error code otherwise.
259 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
264 ret = snprintf(buf, size, "/sys/class/net/%s/%s", if_name, relpath);
265 if (ret == -1 || (size_t)ret >= size)
267 ret = readlink(buf, buf, size);
270 if ((size_t)ret >= size - 1)
277 * Probe a network interface to associate with vdev_netvsc context.
279 * This function determines if the network device matches the properties of
280 * the NetVSC interface associated with the vdev_netvsc context and
281 * communicates its bus address to the fail-safe PMD instance if so.
283 * It is normally used with vdev_netvsc_foreach_iface().
286 * Pointer to netdevice description structure (name and index).
287 * @param[in] eth_addr
288 * MAC address associated with @p iface.
290 * Variable arguments list comprising:
292 * - struct vdev_netvsc_ctx *ctx:
293 * Context to associate network interface with.
296 * A nonzero value when interface matches, 0 otherwise or in case of
300 vdev_netvsc_device_probe(const struct if_nameindex *iface,
301 const struct ether_addr *eth_addr,
304 struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
305 char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
310 /* Skip non-matching or unwanted NetVSC interfaces. */
311 if (ctx->if_index == iface->if_index) {
312 if (!strcmp(ctx->if_name, iface->if_name))
315 "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
316 ctx->if_name, ctx->if_index, iface->if_name);
317 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
320 if (vdev_netvsc_iface_is_netvsc(iface))
322 if (!is_same_ether_addr(eth_addr, &ctx->if_addr))
324 /* Look for associated PCI device. */
325 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
329 addr = strrchr(buf, '/');
330 addr = addr ? addr + 1 : buf;
331 if (strcmp(addr, "pci"))
333 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
337 addr = strrchr(buf, '/');
338 addr = addr ? addr + 1 : buf;
342 /* Send PCI device argument to fail-safe PMD instance. */
343 if (strcmp(addr, ctx->yield))
344 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
345 " interface \"%s\" (index %u)", addr, ctx->if_name,
347 memmove(buf, addr, len + 1);
350 ret = write(ctx->pipe[1], addr, len + 1);
353 if (errno == EINTR || errno == EAGAIN)
355 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
356 " interface \"%s\": %s", addr, ctx->if_name,
357 rte_strerror(errno));
360 if ((size_t)ret != len + 1) {
362 * Attempt to override previous partial write, no need to
363 * recover if that fails.
365 ret = write(ctx->pipe[1], "\n", 1);
370 memcpy(ctx->yield, addr, len + 1);
375 * Alarm callback that regularly probes system network interfaces.
377 * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
378 * long as an vdev_netvsc context instance exists.
384 vdev_netvsc_alarm(__rte_unused void *arg)
386 struct vdev_netvsc_ctx *ctx;
389 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
390 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
394 if (!vdev_netvsc_ctx_count)
396 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
397 vdev_netvsc_alarm, NULL);
399 DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
405 * Probe a NetVSC interface to generate a vdev_netvsc context from.
407 * This function instantiates vdev_netvsc contexts either for all NetVSC
408 * devices found on the system or only a subset provided as device
411 * It is normally used with vdev_netvsc_foreach_iface().
414 * Pointer to netdevice description structure (name and index).
415 * @param[in] eth_addr
416 * MAC address associated with @p iface.
418 * Variable arguments list comprising:
420 * - const char *name:
421 * Name associated with current driver instance.
423 * - struct rte_kvargs *kvargs:
424 * Device arguments provided to current driver instance.
427 * Accept specified interface even if not detected as NetVSC.
429 * - unsigned int specified:
430 * Number of specific netdevices provided as device arguments.
432 * - unsigned int *matched:
433 * The number of specified netdevices matched by this function.
436 * A nonzero value when interface matches, 0 otherwise or in case of
440 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
441 const struct ether_addr *eth_addr,
444 const char *name = va_arg(ap, const char *);
445 struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
446 int force = va_arg(ap, int);
447 unsigned int specified = va_arg(ap, unsigned int);
448 unsigned int *matched = va_arg(ap, unsigned int *);
450 struct vdev_netvsc_ctx *ctx;
453 /* Probe all interfaces when none are specified. */
455 for (i = 0; i != kvargs->count; ++i) {
456 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
458 if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
459 if (!strcmp(pair->value, iface->if_name))
461 } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
462 struct ether_addr tmp;
464 if (sscanf(pair->value,
465 "%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":"
466 "%" SCNx8 ":%" SCNx8 ":%" SCNx8,
472 &tmp.addr_bytes[5]) != 6) {
474 "invalid MAC address format"
479 if (is_same_ether_addr(eth_addr, &tmp))
483 if (i == kvargs->count)
487 /* Weed out interfaces already handled. */
488 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
489 if (ctx->if_index == iface->if_index)
495 "interface \"%s\" (index %u) is already handled,"
497 iface->if_name, iface->if_index);
500 if (!vdev_netvsc_iface_is_netvsc(iface)) {
501 if (!specified || !force)
504 "using non-NetVSC interface \"%s\" (index %u)",
505 iface->if_name, iface->if_index);
507 /* Routed NetVSC should not be probed. */
508 if (vdev_netvsc_has_route(iface->if_name)) {
509 if (!specified || !force)
511 DRV_LOG(WARNING, "using routed NetVSC interface \"%s\""
512 " (index %u)", iface->if_name, iface->if_index);
514 /* Create interface context. */
515 ctx = calloc(1, sizeof(*ctx));
518 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
519 iface->if_name, rte_strerror(errno));
522 ctx->id = vdev_netvsc_ctx_count;
523 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
524 ctx->if_index = iface->if_index;
525 ctx->if_addr = *eth_addr;
528 ctx->yield[0] = '\0';
529 if (pipe(ctx->pipe) == -1) {
532 "cannot allocate control pipe for interface \"%s\": %s",
533 ctx->if_name, rte_strerror(errno));
536 for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
537 int flf = fcntl(ctx->pipe[i], F_GETFL);
540 fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
543 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
544 " descriptor #%u (%d): %s", i, ctx->pipe[i],
545 rte_strerror(errno));
548 /* Generate virtual device name and arguments. */
550 ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
552 if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
554 ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_%s",
556 if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
558 ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
559 "fd(%d),dev(net_tap_%s,remote=%s)",
560 ctx->pipe[0], ctx->name, ctx->if_name);
561 if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
565 DRV_LOG(ERR, "generated virtual device name or argument list"
566 " too long for interface \"%s\"", ctx->if_name);
569 /* Request virtual device generation. */
570 DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
571 ctx->devname, ctx->devargs);
572 vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
573 ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
576 LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
577 ++vdev_netvsc_ctx_count;
578 DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
583 vdev_netvsc_ctx_destroy(ctx);
588 * Probe NetVSC interfaces.
590 * This function probes system netdevices according to the specified device
591 * arguments and starts a periodic alarm callback to notify the resulting
592 * fail-safe PMD instances of their sub-devices whereabouts.
595 * Virtual device context for driver instance.
598 * Always 0, even in case of errors.
601 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
603 static const char *const vdev_netvsc_arg[] = {
604 VDEV_NETVSC_ARG_IFACE,
606 VDEV_NETVSC_ARG_FORCE,
607 VDEV_NETVSC_ARG_IGNORE,
610 const char *name = rte_vdev_device_name(dev);
611 const char *args = rte_vdev_device_args(dev);
612 struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
614 unsigned int specified = 0;
615 unsigned int matched = 0;
621 DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
623 DRV_LOG(ERR, "cannot parse arguments list");
626 for (i = 0; i != kvargs->count; ++i) {
627 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
629 if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE))
630 force = !!atoi(pair->value);
631 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IGNORE))
632 ignore = !!atoi(pair->value);
633 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
634 !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
639 rte_kvargs_free(kvargs);
642 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
643 /* Gather interfaces. */
644 ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, name, kvargs,
645 force, specified, &matched);
648 if (matched < specified)
650 "some of the specified parameters did not match"
651 " recognized network interfaces");
652 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
653 vdev_netvsc_alarm, NULL);
655 DRV_LOG(ERR, "unable to schedule alarm callback: %s",
661 rte_kvargs_free(kvargs);
662 ++vdev_netvsc_ctx_inst;
667 * Remove driver instance.
669 * The alarm callback and underlying vdev_netvsc context instances are only
670 * destroyed after the last PMD instance is removed.
673 * Virtual device context for driver instance.
679 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
681 if (--vdev_netvsc_ctx_inst)
683 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
684 while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
685 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
687 LIST_REMOVE(ctx, entry);
688 --vdev_netvsc_ctx_count;
689 vdev_netvsc_ctx_destroy(ctx);
694 /** Virtual device descriptor. */
695 static struct rte_vdev_driver vdev_netvsc_vdev = {
696 .probe = vdev_netvsc_vdev_probe,
697 .remove = vdev_netvsc_vdev_remove,
700 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
701 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
702 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
703 VDEV_NETVSC_ARG_IFACE "=<string> "
704 VDEV_NETVSC_ARG_MAC "=<string> "
705 VDEV_NETVSC_ARG_FORCE "=<int> "
706 VDEV_NETVSC_ARG_IGNORE "=<int>");
708 /** Initialize driver log type. */
709 RTE_INIT(vdev_netvsc_init_log)
711 vdev_netvsc_logtype = rte_log_register("pmd.vdev_netvsc");
712 if (vdev_netvsc_logtype >= 0)
713 rte_log_set_level(vdev_netvsc_logtype, RTE_LOG_NOTICE);
716 /** Compare function for vdev find device operation. */
718 vdev_netvsc_cmp_rte_device(const struct rte_device *dev1,
719 __rte_unused const void *_dev2)
721 return strcmp(dev1->devargs->name, VDEV_NETVSC_DRIVER_NAME);
725 * A callback called by vdev bus scan function to ensure this driver probing
726 * automatically in Hyper-V VM system unless it already exists in the
730 vdev_netvsc_scan_callback(__rte_unused void *arg)
732 struct rte_vdev_device *dev;
733 struct rte_devargs *devargs;
734 struct rte_bus *vbus = rte_bus_find_by_name("vdev");
736 TAILQ_FOREACH(devargs, &devargs_list, next)
737 if (!strcmp(devargs->name, VDEV_NETVSC_DRIVER_NAME))
739 dev = (struct rte_vdev_device *)vbus->find_device(NULL,
740 vdev_netvsc_cmp_rte_device, VDEV_NETVSC_DRIVER_NAME);
743 if (rte_eal_devargs_add(RTE_DEVTYPE_VIRTUAL, VDEV_NETVSC_DRIVER_NAME))
744 DRV_LOG(ERR, "unable to add netvsc devargs.");
747 /** Initialize the custom scan. */
748 RTE_INIT(vdev_netvsc_custom_scan_add)
750 if (rte_hypervisor_get() == RTE_HYPERVISOR_HYPERV)
751 rte_vdev_add_custom_scan(vdev_netvsc_scan_callback, NULL);