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_kvargs.h>
36 #define VDEV_NETVSC_DRIVER net_vdev_netvsc
37 #define VDEV_NETVSC_ARG_IFACE "iface"
38 #define VDEV_NETVSC_ARG_MAC "mac"
39 #define VDEV_NETVSC_ARG_FORCE "force"
40 #define VDEV_NETVSC_PROBE_MS 1000
42 #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}"
43 #define NETVSC_MAX_ROUTE_LINE_SIZE 300
45 #define DRV_LOG(level, ...) \
46 rte_log(RTE_LOG_ ## level, \
47 vdev_netvsc_logtype, \
48 RTE_FMT(RTE_STR(VDEV_NETVSC_DRIVER) ": " \
49 RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
50 RTE_FMT_TAIL(__VA_ARGS__,)))
52 /** Driver-specific log messages type. */
53 static int vdev_netvsc_logtype;
55 /** Context structure for a vdev_netvsc instance. */
56 struct vdev_netvsc_ctx {
57 LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */
58 unsigned int id; /**< Unique ID. */
59 char name[64]; /**< Unique name. */
60 char devname[64]; /**< Fail-safe instance name. */
61 char devargs[256]; /**< Fail-safe device arguments. */
62 char if_name[IF_NAMESIZE]; /**< NetVSC netdevice name. */
63 unsigned int if_index; /**< NetVSC netdevice index. */
64 struct ether_addr if_addr; /**< NetVSC MAC address. */
65 int pipe[2]; /**< Fail-safe communication pipe. */
66 char yield[256]; /**< PCI sub-device arguments. */
69 /** Context list is common to all driver instances. */
70 static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list =
71 LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list);
73 /** Number of entries in context list. */
74 static unsigned int vdev_netvsc_ctx_count;
76 /** Number of driver instances relying on context list. */
77 static unsigned int vdev_netvsc_ctx_inst;
80 * Destroy a vdev_netvsc context instance.
86 vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
88 if (ctx->pipe[0] != -1)
90 if (ctx->pipe[1] != -1)
96 * Iterate over system network interfaces.
98 * This function runs a given callback function for each netdevice found on
102 * Callback function pointer. List traversal is aborted when this function
103 * returns a nonzero value.
105 * Variable parameter list passed as @p va_list to @p func.
108 * 0 when the entire list is traversed successfully, a negative error code
109 * in case or failure, or the nonzero value returned by @p func when list
110 * traversal is aborted.
113 vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
114 const struct ether_addr *eth_addr,
117 struct if_nameindex *iface = if_nameindex();
118 int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
124 DRV_LOG(ERR, "cannot retrieve system network interfaces");
129 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
132 for (i = 0; iface[i].if_name; ++i) {
134 struct ether_addr eth_addr;
137 strncpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name));
138 if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
139 DRV_LOG(WARNING, "cannot retrieve information about"
140 " interface \"%s\": %s",
141 req.ifr_name, rte_strerror(errno));
144 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
145 DRV_LOG(DEBUG, "interface %s is non-ethernet device",
149 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
150 RTE_DIM(eth_addr.addr_bytes));
152 ret = func(&iface[i], ð_addr, ap);
161 if_freenameindex(iface);
166 * Determine if a network interface is NetVSC.
169 * Pointer to netdevice description structure (name and index).
172 * A nonzero value when interface is detected as NetVSC. In case of error,
173 * rte_errno is updated and 0 returned.
176 vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
178 static const char temp[] = "/sys/class/net/%s/device/class_id";
179 char path[sizeof(temp) + IF_NAMESIZE];
184 ret = snprintf(path, sizeof(path), temp, iface->if_name);
185 if (ret == -1 || (size_t)ret >= sizeof(path)) {
189 f = fopen(path, "r");
194 ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
197 ret = len == (int)strlen(NETVSC_CLASS_ID);
203 * Determine if a network interface has a route.
206 * Network device name.
209 * A nonzero value when interface has an route. In case of error,
210 * rte_errno is updated and 0 returned.
213 vdev_netvsc_has_route(const char *name)
217 char route[NETVSC_MAX_ROUTE_LINE_SIZE];
220 fp = fopen("/proc/net/route", "r");
225 while (fgets(route, NETVSC_MAX_ROUTE_LINE_SIZE, fp) != NULL) {
226 netdev = strtok(route, "\t");
227 if (strcmp(netdev, name) == 0) {
231 /* Move file pointer to the next line. */
232 while (strchr(route, '\n') == NULL &&
233 fgets(route, NETVSC_MAX_ROUTE_LINE_SIZE, fp) != NULL)
241 * Retrieve network interface data from sysfs symbolic link.
244 * Output data buffer.
246 * Output buffer size.
250 * Symbolic link path relative to netdevice sysfs entry.
253 * 0 on success, a negative error code otherwise.
256 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
261 ret = snprintf(buf, size, "/sys/class/net/%s/%s", if_name, relpath);
262 if (ret == -1 || (size_t)ret >= size)
264 ret = readlink(buf, buf, size);
267 if ((size_t)ret >= size - 1)
274 * Probe a network interface to associate with vdev_netvsc context.
276 * This function determines if the network device matches the properties of
277 * the NetVSC interface associated with the vdev_netvsc context and
278 * communicates its bus address to the fail-safe PMD instance if so.
280 * It is normally used with vdev_netvsc_foreach_iface().
283 * Pointer to netdevice description structure (name and index).
284 * @param[in] eth_addr
285 * MAC address associated with @p iface.
287 * Variable arguments list comprising:
289 * - struct vdev_netvsc_ctx *ctx:
290 * Context to associate network interface with.
293 * A nonzero value when interface matches, 0 otherwise or in case of
297 vdev_netvsc_device_probe(const struct if_nameindex *iface,
298 const struct ether_addr *eth_addr,
301 struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
302 char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
307 /* Skip non-matching or unwanted NetVSC interfaces. */
308 if (ctx->if_index == iface->if_index) {
309 if (!strcmp(ctx->if_name, iface->if_name))
312 "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
313 ctx->if_name, ctx->if_index, iface->if_name);
314 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
317 if (vdev_netvsc_iface_is_netvsc(iface))
319 if (!is_same_ether_addr(eth_addr, &ctx->if_addr))
321 /* Look for associated PCI device. */
322 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
326 addr = strrchr(buf, '/');
327 addr = addr ? addr + 1 : buf;
328 if (strcmp(addr, "pci"))
330 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
334 addr = strrchr(buf, '/');
335 addr = addr ? addr + 1 : buf;
339 /* Send PCI device argument to fail-safe PMD instance. */
340 if (strcmp(addr, ctx->yield))
341 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
342 " interface \"%s\" (index %u)", addr, ctx->if_name,
344 memmove(buf, addr, len + 1);
347 ret = write(ctx->pipe[1], addr, len + 1);
350 if (errno == EINTR || errno == EAGAIN)
352 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
353 " interface \"%s\": %s", addr, ctx->if_name,
354 rte_strerror(errno));
357 if ((size_t)ret != len + 1) {
359 * Attempt to override previous partial write, no need to
360 * recover if that fails.
362 ret = write(ctx->pipe[1], "\n", 1);
367 memcpy(ctx->yield, addr, len + 1);
372 * Alarm callback that regularly probes system network interfaces.
374 * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
375 * long as an vdev_netvsc context instance exists.
381 vdev_netvsc_alarm(__rte_unused void *arg)
383 struct vdev_netvsc_ctx *ctx;
386 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
387 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
391 if (!vdev_netvsc_ctx_count)
393 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
394 vdev_netvsc_alarm, NULL);
396 DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
402 * Probe a NetVSC interface to generate a vdev_netvsc context from.
404 * This function instantiates vdev_netvsc contexts either for all NetVSC
405 * devices found on the system or only a subset provided as device
408 * It is normally used with vdev_netvsc_foreach_iface().
411 * Pointer to netdevice description structure (name and index).
412 * @param[in] eth_addr
413 * MAC address associated with @p iface.
415 * Variable arguments list comprising:
417 * - const char *name:
418 * Name associated with current driver instance.
420 * - struct rte_kvargs *kvargs:
421 * Device arguments provided to current driver instance.
424 * Accept specified interface even if not detected as NetVSC.
426 * - unsigned int specified:
427 * Number of specific netdevices provided as device arguments.
429 * - unsigned int *matched:
430 * The number of specified netdevices matched by this function.
433 * A nonzero value when interface matches, 0 otherwise or in case of
437 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
438 const struct ether_addr *eth_addr,
441 const char *name = va_arg(ap, const char *);
442 struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
443 int force = va_arg(ap, int);
444 unsigned int specified = va_arg(ap, unsigned int);
445 unsigned int *matched = va_arg(ap, unsigned int *);
447 struct vdev_netvsc_ctx *ctx;
450 /* Probe all interfaces when none are specified. */
452 for (i = 0; i != kvargs->count; ++i) {
453 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
455 if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
456 if (!strcmp(pair->value, iface->if_name))
458 } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
459 struct ether_addr tmp;
461 if (sscanf(pair->value,
462 "%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":"
463 "%" SCNx8 ":%" SCNx8 ":%" SCNx8,
469 &tmp.addr_bytes[5]) != 6) {
471 "invalid MAC address format"
476 if (is_same_ether_addr(eth_addr, &tmp))
480 if (i == kvargs->count)
484 /* Weed out interfaces already handled. */
485 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
486 if (ctx->if_index == iface->if_index)
492 "interface \"%s\" (index %u) is already handled,"
494 iface->if_name, iface->if_index);
497 if (!vdev_netvsc_iface_is_netvsc(iface)) {
498 if (!specified || !force)
501 "using non-NetVSC interface \"%s\" (index %u)",
502 iface->if_name, iface->if_index);
504 /* Routed NetVSC should not be probed. */
505 if (vdev_netvsc_has_route(iface->if_name)) {
506 if (!specified || !force)
508 DRV_LOG(WARNING, "using routed NetVSC interface \"%s\""
509 " (index %u)", iface->if_name, iface->if_index);
511 /* Create interface context. */
512 ctx = calloc(1, sizeof(*ctx));
515 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
516 iface->if_name, rte_strerror(errno));
519 ctx->id = vdev_netvsc_ctx_count;
520 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
521 ctx->if_index = iface->if_index;
522 ctx->if_addr = *eth_addr;
525 ctx->yield[0] = '\0';
526 if (pipe(ctx->pipe) == -1) {
529 "cannot allocate control pipe for interface \"%s\": %s",
530 ctx->if_name, rte_strerror(errno));
533 for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
534 int flf = fcntl(ctx->pipe[i], F_GETFL);
537 fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
540 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
541 " descriptor #%u (%d): %s", i, ctx->pipe[i],
542 rte_strerror(errno));
545 /* Generate virtual device name and arguments. */
547 ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
549 if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
551 ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_%s",
553 if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
555 ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
556 "fd(%d),dev(net_tap_%s,remote=%s)",
557 ctx->pipe[0], ctx->name, ctx->if_name);
558 if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
562 DRV_LOG(ERR, "generated virtual device name or argument list"
563 " too long for interface \"%s\"", ctx->if_name);
566 /* Request virtual device generation. */
567 DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
568 ctx->devname, ctx->devargs);
569 vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
570 ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
573 LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
574 ++vdev_netvsc_ctx_count;
575 DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
580 vdev_netvsc_ctx_destroy(ctx);
585 * Probe NetVSC interfaces.
587 * This function probes system netdevices according to the specified device
588 * arguments and starts a periodic alarm callback to notify the resulting
589 * fail-safe PMD instances of their sub-devices whereabouts.
592 * Virtual device context for driver instance.
595 * Always 0, even in case of errors.
598 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
600 static const char *const vdev_netvsc_arg[] = {
601 VDEV_NETVSC_ARG_IFACE,
603 VDEV_NETVSC_ARG_FORCE,
606 const char *name = rte_vdev_device_name(dev);
607 const char *args = rte_vdev_device_args(dev);
608 struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
610 unsigned int specified = 0;
611 unsigned int matched = 0;
616 DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
618 DRV_LOG(ERR, "cannot parse arguments list");
621 for (i = 0; i != kvargs->count; ++i) {
622 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
624 if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE))
625 force = !!atoi(pair->value);
626 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
627 !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
630 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
631 /* Gather interfaces. */
632 ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, name, kvargs,
633 force, specified, &matched);
636 if (matched < specified)
638 "some of the specified parameters did not match"
639 " recognized network interfaces");
640 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
641 vdev_netvsc_alarm, NULL);
643 DRV_LOG(ERR, "unable to schedule alarm callback: %s",
649 rte_kvargs_free(kvargs);
650 ++vdev_netvsc_ctx_inst;
655 * Remove driver instance.
657 * The alarm callback and underlying vdev_netvsc context instances are only
658 * destroyed after the last PMD instance is removed.
661 * Virtual device context for driver instance.
667 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
669 if (--vdev_netvsc_ctx_inst)
671 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
672 while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
673 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
675 LIST_REMOVE(ctx, entry);
676 --vdev_netvsc_ctx_count;
677 vdev_netvsc_ctx_destroy(ctx);
682 /** Virtual device descriptor. */
683 static struct rte_vdev_driver vdev_netvsc_vdev = {
684 .probe = vdev_netvsc_vdev_probe,
685 .remove = vdev_netvsc_vdev_remove,
688 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
689 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
690 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
691 VDEV_NETVSC_ARG_IFACE "=<string> "
692 VDEV_NETVSC_ARG_MAC "=<string> "
693 VDEV_NETVSC_ARG_FORCE "=<int>");
695 /** Initialize driver log type. */
696 RTE_INIT(vdev_netvsc_init_log)
698 vdev_netvsc_logtype = rte_log_register("pmd.vdev_netvsc");
699 if (vdev_netvsc_logtype >= 0)
700 rte_log_set_level(vdev_netvsc_logtype, RTE_LOG_NOTICE);