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}"
43 #define DRV_LOG(level, ...) \
44 rte_log(RTE_LOG_ ## level, \
45 vdev_netvsc_logtype, \
46 RTE_FMT(RTE_STR(VDEV_NETVSC_DRIVER) ": " \
47 RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
48 RTE_FMT_TAIL(__VA_ARGS__,)))
50 /** Driver-specific log messages type. */
51 static int vdev_netvsc_logtype;
53 /** Context structure for a vdev_netvsc instance. */
54 struct vdev_netvsc_ctx {
55 LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */
56 unsigned int id; /**< Unique ID. */
57 char name[64]; /**< Unique name. */
58 char devname[64]; /**< Fail-safe instance name. */
59 char devargs[256]; /**< Fail-safe device arguments. */
60 char if_name[IF_NAMESIZE]; /**< NetVSC netdevice name. */
61 unsigned int if_index; /**< NetVSC netdevice index. */
62 struct ether_addr if_addr; /**< NetVSC MAC address. */
63 int pipe[2]; /**< Fail-safe communication pipe. */
64 char yield[256]; /**< PCI sub-device arguments. */
67 /** Context list is common to all driver instances. */
68 static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list =
69 LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list);
71 /** Number of entries in context list. */
72 static unsigned int vdev_netvsc_ctx_count;
74 /** Number of driver instances relying on context list. */
75 static unsigned int vdev_netvsc_ctx_inst;
78 * Destroy a vdev_netvsc context instance.
84 vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
86 if (ctx->pipe[0] != -1)
88 if (ctx->pipe[1] != -1)
94 * Iterate over system network interfaces.
96 * This function runs a given callback function for each netdevice found on
100 * Callback function pointer. List traversal is aborted when this function
101 * returns a nonzero value.
103 * Variable parameter list passed as @p va_list to @p func.
106 * 0 when the entire list is traversed successfully, a negative error code
107 * in case or failure, or the nonzero value returned by @p func when list
108 * traversal is aborted.
111 vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
112 const struct ether_addr *eth_addr,
115 struct if_nameindex *iface = if_nameindex();
116 int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
122 DRV_LOG(ERR, "cannot retrieve system network interfaces");
127 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
130 for (i = 0; iface[i].if_name; ++i) {
132 struct ether_addr eth_addr;
135 strncpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name));
136 if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
137 DRV_LOG(WARNING, "cannot retrieve information about"
138 " interface \"%s\": %s",
139 req.ifr_name, rte_strerror(errno));
142 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
143 DRV_LOG(DEBUG, "interface %s is non-ethernet device",
147 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
148 RTE_DIM(eth_addr.addr_bytes));
150 ret = func(&iface[i], ð_addr, ap);
159 if_freenameindex(iface);
164 * Determine if a network interface is NetVSC.
167 * Pointer to netdevice description structure (name and index).
170 * A nonzero value when interface is detected as NetVSC. In case of error,
171 * rte_errno is updated and 0 returned.
174 vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
176 static const char temp[] = "/sys/class/net/%s/device/class_id";
177 char path[sizeof(temp) + IF_NAMESIZE];
182 ret = snprintf(path, sizeof(path), temp, iface->if_name);
183 if (ret == -1 || (size_t)ret >= sizeof(path)) {
187 f = fopen(path, "r");
192 ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
195 ret = len == (int)strlen(NETVSC_CLASS_ID);
201 * Retrieve network interface data from sysfs symbolic link.
204 * Output data buffer.
206 * Output buffer size.
210 * Symbolic link path relative to netdevice sysfs entry.
213 * 0 on success, a negative error code otherwise.
216 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
221 ret = snprintf(buf, size, "/sys/class/net/%s/%s", if_name, relpath);
222 if (ret == -1 || (size_t)ret >= size)
224 ret = readlink(buf, buf, size);
227 if ((size_t)ret >= size - 1)
234 * Probe a network interface to associate with vdev_netvsc context.
236 * This function determines if the network device matches the properties of
237 * the NetVSC interface associated with the vdev_netvsc context and
238 * communicates its bus address to the fail-safe PMD instance if so.
240 * It is normally used with vdev_netvsc_foreach_iface().
243 * Pointer to netdevice description structure (name and index).
244 * @param[in] eth_addr
245 * MAC address associated with @p iface.
247 * Variable arguments list comprising:
249 * - struct vdev_netvsc_ctx *ctx:
250 * Context to associate network interface with.
253 * A nonzero value when interface matches, 0 otherwise or in case of
257 vdev_netvsc_device_probe(const struct if_nameindex *iface,
258 const struct ether_addr *eth_addr,
261 struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
262 char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
267 /* Skip non-matching or unwanted NetVSC interfaces. */
268 if (ctx->if_index == iface->if_index) {
269 if (!strcmp(ctx->if_name, iface->if_name))
272 "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
273 ctx->if_name, ctx->if_index, iface->if_name);
274 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
277 if (vdev_netvsc_iface_is_netvsc(iface))
279 if (!is_same_ether_addr(eth_addr, &ctx->if_addr))
281 /* Look for associated PCI device. */
282 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
286 addr = strrchr(buf, '/');
287 addr = addr ? addr + 1 : buf;
288 if (strcmp(addr, "pci"))
290 ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
294 addr = strrchr(buf, '/');
295 addr = addr ? addr + 1 : buf;
299 /* Send PCI device argument to fail-safe PMD instance. */
300 if (strcmp(addr, ctx->yield))
301 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
302 " interface \"%s\" (index %u)", addr, ctx->if_name,
304 memmove(buf, addr, len + 1);
307 ret = write(ctx->pipe[1], addr, len + 1);
310 if (errno == EINTR || errno == EAGAIN)
312 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
313 " interface \"%s\": %s", addr, ctx->if_name,
314 rte_strerror(errno));
317 if ((size_t)ret != len + 1) {
319 * Attempt to override previous partial write, no need to
320 * recover if that fails.
322 ret = write(ctx->pipe[1], "\n", 1);
327 memcpy(ctx->yield, addr, len + 1);
332 * Alarm callback that regularly probes system network interfaces.
334 * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
335 * long as an vdev_netvsc context instance exists.
341 vdev_netvsc_alarm(__rte_unused void *arg)
343 struct vdev_netvsc_ctx *ctx;
346 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
347 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
351 if (!vdev_netvsc_ctx_count)
353 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
354 vdev_netvsc_alarm, NULL);
356 DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
362 * Probe a NetVSC interface to generate a vdev_netvsc context from.
364 * This function instantiates vdev_netvsc contexts either for all NetVSC
365 * devices found on the system or only a subset provided as device
368 * It is normally used with vdev_netvsc_foreach_iface().
371 * Pointer to netdevice description structure (name and index).
372 * @param[in] eth_addr
373 * MAC address associated with @p iface.
375 * Variable arguments list comprising:
377 * - const char *name:
378 * Name associated with current driver instance.
380 * - struct rte_kvargs *kvargs:
381 * Device arguments provided to current driver instance.
383 * - unsigned int specified:
384 * Number of specific netdevices provided as device arguments.
386 * - unsigned int *matched:
387 * The number of specified netdevices matched by this function.
390 * A nonzero value when interface matches, 0 otherwise or in case of
394 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
395 const struct ether_addr *eth_addr,
398 const char *name = va_arg(ap, const char *);
399 struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
400 unsigned int specified = va_arg(ap, unsigned int);
401 unsigned int *matched = va_arg(ap, unsigned int *);
403 struct vdev_netvsc_ctx *ctx;
406 /* Probe all interfaces when none are specified. */
408 for (i = 0; i != kvargs->count; ++i) {
409 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
411 if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
412 if (!strcmp(pair->value, iface->if_name))
414 } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
415 struct ether_addr tmp;
417 if (sscanf(pair->value,
418 "%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":"
419 "%" SCNx8 ":%" SCNx8 ":%" SCNx8,
425 &tmp.addr_bytes[5]) != 6) {
427 "invalid MAC address format"
432 if (is_same_ether_addr(eth_addr, &tmp))
436 if (i == kvargs->count)
440 /* Weed out interfaces already handled. */
441 LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
442 if (ctx->if_index == iface->if_index)
448 "interface \"%s\" (index %u) is already handled,"
450 iface->if_name, iface->if_index);
453 if (!vdev_netvsc_iface_is_netvsc(iface)) {
457 "interface \"%s\" (index %u) is not NetVSC,"
459 iface->if_name, iface->if_index);
462 /* Create interface context. */
463 ctx = calloc(1, sizeof(*ctx));
466 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
467 iface->if_name, rte_strerror(errno));
470 ctx->id = vdev_netvsc_ctx_count;
471 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
472 ctx->if_index = iface->if_index;
473 ctx->if_addr = *eth_addr;
476 ctx->yield[0] = '\0';
477 if (pipe(ctx->pipe) == -1) {
480 "cannot allocate control pipe for interface \"%s\": %s",
481 ctx->if_name, rte_strerror(errno));
484 for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
485 int flf = fcntl(ctx->pipe[i], F_GETFL);
488 fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
491 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
492 " descriptor #%u (%d): %s", i, ctx->pipe[i],
493 rte_strerror(errno));
496 /* Generate virtual device name and arguments. */
498 ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
500 if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
502 ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_%s",
504 if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
506 ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
507 "fd(%d),dev(net_tap_%s,remote=%s)",
508 ctx->pipe[0], ctx->name, ctx->if_name);
509 if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
513 DRV_LOG(ERR, "generated virtual device name or argument list"
514 " too long for interface \"%s\"", ctx->if_name);
517 /* Request virtual device generation. */
518 DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
519 ctx->devname, ctx->devargs);
520 vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
521 ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
524 LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
525 ++vdev_netvsc_ctx_count;
526 DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
531 vdev_netvsc_ctx_destroy(ctx);
536 * Probe NetVSC interfaces.
538 * This function probes system netdevices according to the specified device
539 * arguments and starts a periodic alarm callback to notify the resulting
540 * fail-safe PMD instances of their sub-devices whereabouts.
543 * Virtual device context for driver instance.
546 * Always 0, even in case of errors.
549 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
551 static const char *const vdev_netvsc_arg[] = {
552 VDEV_NETVSC_ARG_IFACE,
556 const char *name = rte_vdev_device_name(dev);
557 const char *args = rte_vdev_device_args(dev);
558 struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
560 unsigned int specified = 0;
561 unsigned int matched = 0;
565 DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
567 DRV_LOG(ERR, "cannot parse arguments list");
570 for (i = 0; i != kvargs->count; ++i) {
571 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
573 if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
574 !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
577 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
578 /* Gather interfaces. */
579 ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, name, kvargs,
580 specified, &matched);
583 if (matched < specified)
585 "some of the specified parameters did not match"
586 " recognized network interfaces");
587 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
588 vdev_netvsc_alarm, NULL);
590 DRV_LOG(ERR, "unable to schedule alarm callback: %s",
596 rte_kvargs_free(kvargs);
597 ++vdev_netvsc_ctx_inst;
602 * Remove driver instance.
604 * The alarm callback and underlying vdev_netvsc context instances are only
605 * destroyed after the last PMD instance is removed.
608 * Virtual device context for driver instance.
614 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
616 if (--vdev_netvsc_ctx_inst)
618 rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
619 while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
620 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
622 LIST_REMOVE(ctx, entry);
623 --vdev_netvsc_ctx_count;
624 vdev_netvsc_ctx_destroy(ctx);
629 /** Virtual device descriptor. */
630 static struct rte_vdev_driver vdev_netvsc_vdev = {
631 .probe = vdev_netvsc_vdev_probe,
632 .remove = vdev_netvsc_vdev_remove,
635 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
636 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
637 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
638 VDEV_NETVSC_ARG_IFACE "=<string> "
639 VDEV_NETVSC_ARG_MAC "=<string>");
641 /** Initialize driver log type. */
642 RTE_INIT(vdev_netvsc_init_log)
644 vdev_netvsc_logtype = rte_log_register("pmd.vdev_netvsc");
645 if (vdev_netvsc_logtype >= 0)
646 rte_log_set_level(vdev_netvsc_logtype, RTE_LOG_NOTICE);