1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
9 #include <linux/sockios.h>
10 #include <linux/netlink.h>
11 #include <linux/rtnetlink.h>
13 #include <net/if_arp.h>
14 #include <netinet/ip.h>
21 #include <sys/ioctl.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
26 #include <rte_alarm.h>
28 #include <rte_bus_vdev.h>
29 #include <rte_common.h>
31 #include <rte_errno.h>
32 #include <rte_ethdev.h>
33 #include <rte_ether.h>
34 #include <rte_hypervisor.h>
35 #include <rte_kvargs.h>
37 #include <rte_string_fns.h>
39 #define VDEV_NETVSC_DRIVER net_vdev_netvsc
40 #define VDEV_NETVSC_DRIVER_NAME RTE_STR(VDEV_NETVSC_DRIVER)
41 #define VDEV_NETVSC_DRIVER_NAME_LEN 15
42 #define VDEV_NETVSC_ARG_IFACE "iface"
43 #define VDEV_NETVSC_ARG_MAC "mac"
44 #define VDEV_NETVSC_ARG_FORCE "force"
45 #define VDEV_NETVSC_ARG_IGNORE "ignore"
46 #define VDEV_NETVSC_PROBE_MS 1000
48 #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}"
49 #define NETVSC_MAX_ROUTE_LINE_SIZE 300
51 RTE_LOG_REGISTER(vdev_netvsc_logtype, pmd.net.vdev_netvsc, NOTICE);
53 #define DRV_LOG(level, ...) \
54 rte_log(RTE_LOG_ ## level, \
55 vdev_netvsc_logtype, \
56 RTE_FMT(VDEV_NETVSC_DRIVER_NAME ": " \
57 RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
58 RTE_FMT_TAIL(__VA_ARGS__,)))
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 rte_ether_addr if_addr; /**< NetVSC MAC address. */
70 int pipe[2]; /**< Fail-safe communication pipe. */
71 char yield[256]; /**< PCI sub-device arguments. */
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);
78 /** Number of entries in context list. */
79 static unsigned int vdev_netvsc_ctx_count;
81 /** Number of driver instances relying on context list. */
82 static unsigned int vdev_netvsc_ctx_inst;
85 * Destroy a vdev_netvsc context instance.
91 vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
93 if (ctx->pipe[0] != -1)
95 if (ctx->pipe[1] != -1)
101 * Determine if a network interface is NetVSC.
104 * Pointer to netdevice description structure (name and index).
107 * A nonzero value when interface is detected as NetVSC. In case of error,
108 * rte_errno is updated and 0 returned.
111 vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
113 static const char temp[] = "/sys/class/net/%s/device/class_id";
114 char path[sizeof(temp) + IF_NAMESIZE];
119 ret = snprintf(path, sizeof(path), temp, iface->if_name);
120 if (ret == -1 || (size_t)ret >= sizeof(path)) {
124 f = fopen(path, "r");
129 ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
132 ret = len == (int)strlen(NETVSC_CLASS_ID);
138 * Iterate over system network interfaces.
140 * This function runs a given callback function for each netdevice found on
144 * Callback function pointer. List traversal is aborted when this function
145 * returns a nonzero value.
147 * Indicates the device type to iterate - netvsc or non-netvsc.
149 * Variable parameter list passed as @p va_list to @p func.
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.
157 vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
158 const struct rte_ether_addr *eth_addr,
159 va_list ap), int is_netvsc, ...)
161 struct if_nameindex *iface = if_nameindex();
162 int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
168 DRV_LOG(ERR, "cannot retrieve system network interfaces");
173 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
176 for (i = 0; iface[i].if_name; ++i) {
179 struct rte_ether_addr eth_addr;
182 is_netvsc_ret = vdev_netvsc_iface_is_netvsc(&iface[i]) ? 1 : 0;
183 if (is_netvsc ^ is_netvsc_ret)
185 strlcpy(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));
192 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER)
194 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
195 RTE_DIM(eth_addr.addr_bytes));
196 va_start(ap, is_netvsc);
197 ret = func(&iface[i], ð_addr, ap);
206 if_freenameindex(iface);
211 * Determine if a network interface has a route.
214 * Network device name.
216 * Address family: AF_INET for IPv4 or AF_INET6 for IPv6.
219 * 1 when interface has a route, negative errno value in case of error and
223 vdev_netvsc_has_route(const struct if_nameindex *iface,
224 const unsigned char family)
227 * The implementation can be simpler by getifaddrs() function usage but
228 * it works for IPv6 only starting from glibc 2.3.3.
235 struct nlmsghdr *retmsg = (struct nlmsghdr *)buf;
236 struct sockaddr_nl sa;
238 struct nlmsghdr nlhdr;
239 struct ifaddrmsg addrmsg;
242 if (!iface || (family != AF_INET && family != AF_INET6)) {
243 DRV_LOG(ERR, "%s", rte_strerror(EINVAL));
246 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
248 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
251 memset(&sa, 0, sizeof(sa));
252 sa.nl_family = AF_NETLINK;
253 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
254 res = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
257 DRV_LOG(ERR, "cannot bind socket: %s", rte_strerror(errno));
260 memset(&msg, 0, sizeof(msg));
261 msg.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
262 msg.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
263 msg.nlhdr.nlmsg_type = RTM_GETADDR;
264 msg.nlhdr.nlmsg_pid = getpid();
265 msg.addrmsg.ifa_family = family;
266 msg.addrmsg.ifa_index = iface->if_index;
267 res = send(sock, &msg, msg.nlhdr.nlmsg_len, 0);
270 DRV_LOG(ERR, "cannot send socket message: %s",
271 rte_strerror(errno));
274 memset(buf, 0, sizeof(buf));
275 len = recv(sock, buf, sizeof(buf), 0);
278 DRV_LOG(ERR, "cannot receive socket message: %s",
279 rte_strerror(errno));
282 while (NLMSG_OK(retmsg, (unsigned int)len)) {
283 struct ifaddrmsg *retaddr =
284 (struct ifaddrmsg *)NLMSG_DATA(retmsg);
286 if (retaddr->ifa_family == family &&
287 retaddr->ifa_index == iface->if_index) {
288 struct rtattr *retrta = IFA_RTA(retaddr);
289 int attlen = IFA_PAYLOAD(retmsg);
291 while (RTA_OK(retrta, attlen)) {
292 if (retrta->rta_type == IFA_ADDRESS) {
294 DRV_LOG(DEBUG, "interface %s has IP",
298 retrta = RTA_NEXT(retrta, attlen);
301 retmsg = NLMSG_NEXT(retmsg, len);
309 * Retrieve network interface data from sysfs symbolic link.
312 * Output data buffer.
314 * Output buffer size.
318 * Symbolic link path relative to netdevice sysfs entry.
321 * 0 on success, a negative error code otherwise.
324 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
327 struct vdev_netvsc_ctx *ctx;
328 char in[RTE_MAX(sizeof(ctx->yield), 256u)];
331 ret = snprintf(in, sizeof(in), "/sys/class/net/%s/%s",
333 if (ret == -1 || (size_t)ret >= sizeof(in))
335 ret = readlink(in, buf, size);
338 if ((size_t)ret >= size - 1)
345 * Probe a network interface to associate with vdev_netvsc context.
347 * This function determines if the network device matches the properties of
348 * the NetVSC interface associated with the vdev_netvsc context and
349 * communicates its bus address to the fail-safe PMD instance if so.
351 * It is normally used with vdev_netvsc_foreach_iface().
354 * Pointer to netdevice description structure (name and index).
355 * @param[in] eth_addr
356 * MAC address associated with @p iface.
358 * Variable arguments list comprising:
360 * - struct vdev_netvsc_ctx *ctx:
361 * Context to associate network interface with.
364 * A nonzero value when interface matches, 0 otherwise or in case of
368 vdev_netvsc_device_probe(const struct if_nameindex *iface,
369 const struct rte_ether_addr *eth_addr,
372 struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
373 char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
378 /* Skip non-matching or unwanted NetVSC interfaces. */
379 if (ctx->if_index == iface->if_index) {
380 if (!strcmp(ctx->if_name, iface->if_name))
383 "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
384 ctx->if_name, ctx->if_index, iface->if_name);
385 strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
388 if (!rte_is_same_ether_addr(eth_addr, &ctx->if_addr))
390 /* Look for associated PCI device. */
391 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
395 addr = strrchr(buf, '/');
396 addr = addr ? addr + 1 : buf;
397 if (strcmp(addr, "pci"))
399 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
403 addr = strrchr(buf, '/');
404 addr = addr ? addr + 1 : buf;
408 /* Send PCI device argument to fail-safe PMD instance. */
409 if (strcmp(addr, ctx->yield))
410 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
411 " interface \"%s\" (index %u)", addr, ctx->if_name,
413 memmove(buf, addr, len + 1);
416 ret = write(ctx->pipe[1], addr, len + 1);
419 if (errno == EINTR || errno == EAGAIN)
421 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
422 " interface \"%s\": %s", addr, ctx->if_name,
423 rte_strerror(errno));
426 if ((size_t)ret != len + 1) {
428 * Attempt to override previous partial write, no need to
429 * recover if that fails.
431 ret = write(ctx->pipe[1], "\n", 1);
436 memcpy(ctx->yield, addr, len + 1);
441 * Alarm callback that regularly probes system network interfaces.
443 * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
444 * long as an vdev_netvsc context instance exists.
450 vdev_netvsc_alarm(__rte_unused void *arg)
452 struct vdev_netvsc_ctx *ctx;
455 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
456 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0,
461 if (!vdev_netvsc_ctx_count)
463 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
464 vdev_netvsc_alarm, NULL);
466 DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
472 * Probe a NetVSC interface to generate a vdev_netvsc context from.
474 * This function instantiates vdev_netvsc contexts either for all NetVSC
475 * devices found on the system or only a subset provided as device
478 * It is normally used with vdev_netvsc_foreach_iface().
481 * Pointer to netdevice description structure (name and index).
482 * @param[in] eth_addr
483 * MAC address associated with @p iface.
485 * Variable arguments list comprising:
487 * - const char *name:
488 * Name associated with current driver instance.
490 * - struct rte_kvargs *kvargs:
491 * Device arguments provided to current driver instance.
494 * Accept specified interface even if not detected as NetVSC.
496 * - unsigned int specified:
497 * Number of specific netdevices provided as device arguments.
499 * - unsigned int *matched:
500 * The number of specified netdevices matched by this function.
503 * A nonzero value when interface matches, 0 otherwise or in case of
507 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
508 const struct rte_ether_addr *eth_addr,
511 const char *name = va_arg(ap, const char *);
512 struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
513 unsigned int specified = va_arg(ap, unsigned int);
514 unsigned int *matched = va_arg(ap, unsigned int *);
516 struct vdev_netvsc_ctx *ctx;
519 /* Probe all interfaces when none are specified. */
521 for (i = 0; i != kvargs->count; ++i) {
522 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
524 if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
525 if (!strcmp(pair->value, iface->if_name))
527 } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
528 struct rte_ether_addr tmp;
530 if (rte_ether_unformat_addr(pair->value, &tmp) != 0) {
532 "invalid MAC address format"
537 if (rte_is_same_ether_addr(eth_addr, &tmp))
541 if (i == kvargs->count)
545 /* Weed out interfaces already handled. */
546 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
547 if (ctx->if_index == iface->if_index)
553 "interface \"%s\" (index %u) is already handled,"
555 iface->if_name, iface->if_index);
558 /* Routed NetVSC should not be probed. */
559 if (vdev_netvsc_has_route(iface, AF_INET) ||
560 vdev_netvsc_has_route(iface, AF_INET6)) {
563 DRV_LOG(WARNING, "probably using routed NetVSC interface \"%s\""
564 " (index %u)", iface->if_name, iface->if_index);
566 /* Create interface context. */
567 ctx = calloc(1, sizeof(*ctx));
570 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
571 iface->if_name, rte_strerror(errno));
574 ctx->id = vdev_netvsc_ctx_count;
575 strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
576 ctx->if_index = iface->if_index;
577 ctx->if_addr = *eth_addr;
580 ctx->yield[0] = '\0';
581 if (pipe(ctx->pipe) == -1) {
584 "cannot allocate control pipe for interface \"%s\": %s",
585 ctx->if_name, rte_strerror(errno));
588 for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
589 int flf = fcntl(ctx->pipe[i], F_GETFL);
592 fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
595 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
596 " descriptor #%u (%d): %s", i, ctx->pipe[i],
597 rte_strerror(errno));
600 /* Generate virtual device name and arguments. */
602 ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
604 if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
606 ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_vsc%u",
608 if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
610 ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
611 "fd(%d),dev(net_tap_vsc%u,remote=%s)",
612 ctx->pipe[0], ctx->id, ctx->if_name);
613 if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
617 DRV_LOG(ERR, "generated virtual device name or argument list"
618 " too long for interface \"%s\"", ctx->if_name);
621 /* Request virtual device generation. */
622 DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
623 ctx->devname, ctx->devargs);
624 vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0, ctx);
625 ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
628 LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
629 ++vdev_netvsc_ctx_count;
630 DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
635 vdev_netvsc_ctx_destroy(ctx);
640 * Probe NetVSC interfaces.
642 * This function probes system netdevices according to the specified device
643 * arguments and starts a periodic alarm callback to notify the resulting
644 * fail-safe PMD instances of their sub-devices whereabouts.
647 * Virtual device context for driver instance.
650 * Always 0, even in case of errors.
653 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
655 static const char *const vdev_netvsc_arg[] = {
656 VDEV_NETVSC_ARG_IFACE,
658 VDEV_NETVSC_ARG_FORCE,
659 VDEV_NETVSC_ARG_IGNORE,
662 const char *name = rte_vdev_device_name(dev);
663 const char *args = rte_vdev_device_args(dev);
664 struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
666 unsigned int specified = 0;
667 unsigned int matched = 0;
673 DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
675 DRV_LOG(ERR, "cannot parse arguments list");
678 for (i = 0; i != kvargs->count; ++i) {
679 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
681 if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE))
682 force = !!atoi(pair->value);
683 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IGNORE))
684 ignore = !!atoi(pair->value);
685 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
686 !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
691 rte_kvargs_free(kvargs);
695 DRV_LOG(ERR, "More than one way used to specify the netvsc"
699 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
700 /* Gather interfaces. */
701 ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 1, name,
702 kvargs, specified, &matched);
705 if (specified && matched < specified) {
707 DRV_LOG(ERR, "Cannot find the specified netvsc device");
710 /* Try to force probing on non-netvsc specified device. */
711 if (vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 0, name,
712 kvargs, specified, &matched) < 0)
714 if (matched < specified) {
715 DRV_LOG(ERR, "Cannot find the specified device");
718 DRV_LOG(WARNING, "non-netvsc device was probed as netvsc");
720 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
721 vdev_netvsc_alarm, NULL);
723 DRV_LOG(ERR, "unable to schedule alarm callback: %s",
729 rte_kvargs_free(kvargs);
730 ++vdev_netvsc_ctx_inst;
735 * Remove driver instance.
737 * The alarm callback and underlying vdev_netvsc context instances are only
738 * destroyed after the last PMD instance is removed.
741 * Virtual device context for driver instance.
747 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
749 if (--vdev_netvsc_ctx_inst)
751 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
752 while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
753 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
755 LIST_REMOVE(ctx, entry);
756 --vdev_netvsc_ctx_count;
757 vdev_netvsc_ctx_destroy(ctx);
762 /** Virtual device descriptor. */
763 static struct rte_vdev_driver vdev_netvsc_vdev = {
764 .probe = vdev_netvsc_vdev_probe,
765 .remove = vdev_netvsc_vdev_remove,
768 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
769 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
770 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
771 VDEV_NETVSC_ARG_IFACE "=<string> "
772 VDEV_NETVSC_ARG_MAC "=<string> "
773 VDEV_NETVSC_ARG_FORCE "=<int> "
774 VDEV_NETVSC_ARG_IGNORE "=<int>");
776 /** Compare function for vdev find device operation. */
778 vdev_netvsc_cmp_rte_device(const struct rte_device *dev1,
779 __rte_unused const void *_dev2)
781 return strncmp(dev1->devargs->name, VDEV_NETVSC_DRIVER_NAME,
782 VDEV_NETVSC_DRIVER_NAME_LEN);
786 * A callback called by vdev bus scan function to ensure this driver probing
787 * automatically in Hyper-V VM system unless it already exists in the
791 vdev_netvsc_scan_callback(__rte_unused void *arg)
793 struct rte_device *dev;
794 struct rte_devargs *devargs;
795 struct rte_bus *vbus = rte_bus_find_by_name("vdev");
797 RTE_EAL_DEVARGS_FOREACH("vdev", devargs)
798 if (!strncmp(devargs->name, VDEV_NETVSC_DRIVER_NAME,
799 VDEV_NETVSC_DRIVER_NAME_LEN))
802 dev = vbus->find_device(NULL, vdev_netvsc_cmp_rte_device,
803 VDEV_NETVSC_DRIVER_NAME);
806 if (rte_devargs_add(RTE_DEVTYPE_VIRTUAL, VDEV_NETVSC_DRIVER_NAME))
807 DRV_LOG(ERR, "unable to add netvsc devargs.");
810 /** Initialize the custom scan. */
811 RTE_INIT(vdev_netvsc_custom_scan_add)
813 if (rte_hypervisor_get() == RTE_HYPERVISOR_HYPERV)
814 rte_vdev_add_custom_scan(vdev_netvsc_scan_callback, NULL);