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_PROBE_MS 1000
41 #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}"
42 #define NETVSC_MAX_ROUTE_LINE_SIZE 300
44 #define DRV_LOG(level, ...) \
45 rte_log(RTE_LOG_ ## level, \
46 vdev_netvsc_logtype, \
47 RTE_FMT(RTE_STR(VDEV_NETVSC_DRIVER) ": " \
48 RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
49 RTE_FMT_TAIL(__VA_ARGS__,)))
51 /** Driver-specific log messages type. */
52 static int vdev_netvsc_logtype;
54 /** Context structure for a vdev_netvsc instance. */
55 struct vdev_netvsc_ctx {
56 LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */
57 unsigned int id; /**< Unique ID. */
58 char name[64]; /**< Unique name. */
59 char devname[64]; /**< Fail-safe instance name. */
60 char devargs[256]; /**< Fail-safe device arguments. */
61 char if_name[IF_NAMESIZE]; /**< NetVSC netdevice name. */
62 unsigned int if_index; /**< NetVSC netdevice index. */
63 struct ether_addr if_addr; /**< NetVSC MAC address. */
64 int pipe[2]; /**< Fail-safe communication pipe. */
65 char yield[256]; /**< PCI sub-device arguments. */
68 /** Context list is common to all driver instances. */
69 static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list =
70 LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list);
72 /** Number of entries in context list. */
73 static unsigned int vdev_netvsc_ctx_count;
75 /** Number of driver instances relying on context list. */
76 static unsigned int vdev_netvsc_ctx_inst;
79 * Destroy a vdev_netvsc context instance.
85 vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
87 if (ctx->pipe[0] != -1)
89 if (ctx->pipe[1] != -1)
95 * Iterate over system network interfaces.
97 * This function runs a given callback function for each netdevice found on
101 * Callback function pointer. List traversal is aborted when this function
102 * returns a nonzero value.
104 * Variable parameter list passed as @p va_list to @p func.
107 * 0 when the entire list is traversed successfully, a negative error code
108 * in case or failure, or the nonzero value returned by @p func when list
109 * traversal is aborted.
112 vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
113 const struct ether_addr *eth_addr,
116 struct if_nameindex *iface = if_nameindex();
117 int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
123 DRV_LOG(ERR, "cannot retrieve system network interfaces");
128 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
131 for (i = 0; iface[i].if_name; ++i) {
133 struct ether_addr eth_addr;
136 strncpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name));
137 if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
138 DRV_LOG(WARNING, "cannot retrieve information about"
139 " interface \"%s\": %s",
140 req.ifr_name, rte_strerror(errno));
143 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
144 DRV_LOG(DEBUG, "interface %s is non-ethernet device",
148 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
149 RTE_DIM(eth_addr.addr_bytes));
151 ret = func(&iface[i], ð_addr, ap);
160 if_freenameindex(iface);
165 * Determine if a network interface is NetVSC.
168 * Pointer to netdevice description structure (name and index).
171 * A nonzero value when interface is detected as NetVSC. In case of error,
172 * rte_errno is updated and 0 returned.
175 vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
177 static const char temp[] = "/sys/class/net/%s/device/class_id";
178 char path[sizeof(temp) + IF_NAMESIZE];
183 ret = snprintf(path, sizeof(path), temp, iface->if_name);
184 if (ret == -1 || (size_t)ret >= sizeof(path)) {
188 f = fopen(path, "r");
193 ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
196 ret = len == (int)strlen(NETVSC_CLASS_ID);
202 * Determine if a network interface has a route.
205 * Network device name.
208 * A nonzero value when interface has an route. In case of error,
209 * rte_errno is updated and 0 returned.
212 vdev_netvsc_has_route(const char *name)
216 char route[NETVSC_MAX_ROUTE_LINE_SIZE];
219 fp = fopen("/proc/net/route", "r");
224 while (fgets(route, NETVSC_MAX_ROUTE_LINE_SIZE, fp) != NULL) {
225 netdev = strtok(route, "\t");
226 if (strcmp(netdev, name) == 0) {
230 /* Move file pointer to the next line. */
231 while (strchr(route, '\n') == NULL &&
232 fgets(route, NETVSC_MAX_ROUTE_LINE_SIZE, fp) != NULL)
240 * Retrieve network interface data from sysfs symbolic link.
243 * Output data buffer.
245 * Output buffer size.
249 * Symbolic link path relative to netdevice sysfs entry.
252 * 0 on success, a negative error code otherwise.
255 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
260 ret = snprintf(buf, size, "/sys/class/net/%s/%s", if_name, relpath);
261 if (ret == -1 || (size_t)ret >= size)
263 ret = readlink(buf, buf, size);
266 if ((size_t)ret >= size - 1)
273 * Probe a network interface to associate with vdev_netvsc context.
275 * This function determines if the network device matches the properties of
276 * the NetVSC interface associated with the vdev_netvsc context and
277 * communicates its bus address to the fail-safe PMD instance if so.
279 * It is normally used with vdev_netvsc_foreach_iface().
282 * Pointer to netdevice description structure (name and index).
283 * @param[in] eth_addr
284 * MAC address associated with @p iface.
286 * Variable arguments list comprising:
288 * - struct vdev_netvsc_ctx *ctx:
289 * Context to associate network interface with.
292 * A nonzero value when interface matches, 0 otherwise or in case of
296 vdev_netvsc_device_probe(const struct if_nameindex *iface,
297 const struct ether_addr *eth_addr,
300 struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
301 char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
306 /* Skip non-matching or unwanted NetVSC interfaces. */
307 if (ctx->if_index == iface->if_index) {
308 if (!strcmp(ctx->if_name, iface->if_name))
311 "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
312 ctx->if_name, ctx->if_index, iface->if_name);
313 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
316 if (vdev_netvsc_iface_is_netvsc(iface))
318 if (!is_same_ether_addr(eth_addr, &ctx->if_addr))
320 /* Look for associated PCI device. */
321 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
325 addr = strrchr(buf, '/');
326 addr = addr ? addr + 1 : buf;
327 if (strcmp(addr, "pci"))
329 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
333 addr = strrchr(buf, '/');
334 addr = addr ? addr + 1 : buf;
338 /* Send PCI device argument to fail-safe PMD instance. */
339 if (strcmp(addr, ctx->yield))
340 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
341 " interface \"%s\" (index %u)", addr, ctx->if_name,
343 memmove(buf, addr, len + 1);
346 ret = write(ctx->pipe[1], addr, len + 1);
349 if (errno == EINTR || errno == EAGAIN)
351 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
352 " interface \"%s\": %s", addr, ctx->if_name,
353 rte_strerror(errno));
356 if ((size_t)ret != len + 1) {
358 * Attempt to override previous partial write, no need to
359 * recover if that fails.
361 ret = write(ctx->pipe[1], "\n", 1);
366 memcpy(ctx->yield, addr, len + 1);
371 * Alarm callback that regularly probes system network interfaces.
373 * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
374 * long as an vdev_netvsc context instance exists.
380 vdev_netvsc_alarm(__rte_unused void *arg)
382 struct vdev_netvsc_ctx *ctx;
385 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
386 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
390 if (!vdev_netvsc_ctx_count)
392 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
393 vdev_netvsc_alarm, NULL);
395 DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
401 * Probe a NetVSC interface to generate a vdev_netvsc context from.
403 * This function instantiates vdev_netvsc contexts either for all NetVSC
404 * devices found on the system or only a subset provided as device
407 * It is normally used with vdev_netvsc_foreach_iface().
410 * Pointer to netdevice description structure (name and index).
411 * @param[in] eth_addr
412 * MAC address associated with @p iface.
414 * Variable arguments list comprising:
416 * - const char *name:
417 * Name associated with current driver instance.
419 * - struct rte_kvargs *kvargs:
420 * Device arguments provided to current driver instance.
422 * - unsigned int specified:
423 * Number of specific netdevices provided as device arguments.
425 * - unsigned int *matched:
426 * The number of specified netdevices matched by this function.
429 * A nonzero value when interface matches, 0 otherwise or in case of
433 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
434 const struct ether_addr *eth_addr,
437 const char *name = va_arg(ap, const char *);
438 struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
439 unsigned int specified = va_arg(ap, unsigned int);
440 unsigned int *matched = va_arg(ap, unsigned int *);
442 struct vdev_netvsc_ctx *ctx;
445 /* Probe all interfaces when none are specified. */
447 for (i = 0; i != kvargs->count; ++i) {
448 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
450 if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
451 if (!strcmp(pair->value, iface->if_name))
453 } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
454 struct ether_addr tmp;
456 if (sscanf(pair->value,
457 "%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":"
458 "%" SCNx8 ":%" SCNx8 ":%" SCNx8,
464 &tmp.addr_bytes[5]) != 6) {
466 "invalid MAC address format"
471 if (is_same_ether_addr(eth_addr, &tmp))
475 if (i == kvargs->count)
479 /* Weed out interfaces already handled. */
480 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
481 if (ctx->if_index == iface->if_index)
487 "interface \"%s\" (index %u) is already handled,"
489 iface->if_name, iface->if_index);
492 if (!vdev_netvsc_iface_is_netvsc(iface)) {
496 "interface \"%s\" (index %u) is not NetVSC,"
498 iface->if_name, iface->if_index);
501 /* Routed NetVSC should not be probed. */
502 if (vdev_netvsc_has_route(iface->if_name)) {
503 DRV_LOG(WARNING, "NetVSC interface \"%s\" (index %u) is routed",
504 iface->if_name, iface->if_index);
508 /* Create interface context. */
509 ctx = calloc(1, sizeof(*ctx));
512 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
513 iface->if_name, rte_strerror(errno));
516 ctx->id = vdev_netvsc_ctx_count;
517 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
518 ctx->if_index = iface->if_index;
519 ctx->if_addr = *eth_addr;
522 ctx->yield[0] = '\0';
523 if (pipe(ctx->pipe) == -1) {
526 "cannot allocate control pipe for interface \"%s\": %s",
527 ctx->if_name, rte_strerror(errno));
530 for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
531 int flf = fcntl(ctx->pipe[i], F_GETFL);
534 fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
537 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
538 " descriptor #%u (%d): %s", i, ctx->pipe[i],
539 rte_strerror(errno));
542 /* Generate virtual device name and arguments. */
544 ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
546 if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
548 ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_%s",
550 if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
552 ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
553 "fd(%d),dev(net_tap_%s,remote=%s)",
554 ctx->pipe[0], ctx->name, ctx->if_name);
555 if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
559 DRV_LOG(ERR, "generated virtual device name or argument list"
560 " too long for interface \"%s\"", ctx->if_name);
563 /* Request virtual device generation. */
564 DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
565 ctx->devname, ctx->devargs);
566 vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
567 ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
570 LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
571 ++vdev_netvsc_ctx_count;
572 DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
577 vdev_netvsc_ctx_destroy(ctx);
582 * Probe NetVSC interfaces.
584 * This function probes system netdevices according to the specified device
585 * arguments and starts a periodic alarm callback to notify the resulting
586 * fail-safe PMD instances of their sub-devices whereabouts.
589 * Virtual device context for driver instance.
592 * Always 0, even in case of errors.
595 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
597 static const char *const vdev_netvsc_arg[] = {
598 VDEV_NETVSC_ARG_IFACE,
602 const char *name = rte_vdev_device_name(dev);
603 const char *args = rte_vdev_device_args(dev);
604 struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
606 unsigned int specified = 0;
607 unsigned int matched = 0;
611 DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
613 DRV_LOG(ERR, "cannot parse arguments list");
616 for (i = 0; i != kvargs->count; ++i) {
617 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
619 if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
620 !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
623 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
624 /* Gather interfaces. */
625 ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, name, kvargs,
626 specified, &matched);
629 if (matched < specified)
631 "some of the specified parameters did not match"
632 " recognized network interfaces");
633 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
634 vdev_netvsc_alarm, NULL);
636 DRV_LOG(ERR, "unable to schedule alarm callback: %s",
642 rte_kvargs_free(kvargs);
643 ++vdev_netvsc_ctx_inst;
648 * Remove driver instance.
650 * The alarm callback and underlying vdev_netvsc context instances are only
651 * destroyed after the last PMD instance is removed.
654 * Virtual device context for driver instance.
660 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
662 if (--vdev_netvsc_ctx_inst)
664 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
665 while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
666 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
668 LIST_REMOVE(ctx, entry);
669 --vdev_netvsc_ctx_count;
670 vdev_netvsc_ctx_destroy(ctx);
675 /** Virtual device descriptor. */
676 static struct rte_vdev_driver vdev_netvsc_vdev = {
677 .probe = vdev_netvsc_vdev_probe,
678 .remove = vdev_netvsc_vdev_remove,
681 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
682 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
683 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
684 VDEV_NETVSC_ARG_IFACE "=<string> "
685 VDEV_NETVSC_ARG_MAC "=<string>");
687 /** Initialize driver log type. */
688 RTE_INIT(vdev_netvsc_init_log)
690 vdev_netvsc_logtype = rte_log_register("pmd.vdev_netvsc");
691 if (vdev_netvsc_logtype >= 0)
692 rte_log_set_level(vdev_netvsc_logtype, RTE_LOG_NOTICE);