xref: /dpdk/drivers/net/vdev_netvsc/vdev_netvsc.c (revision 2d0c29a37a9c080c1cccb1ad7941aba2ccf5437e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5 
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <inttypes.h>
9 #include <linux/sockios.h>
10 #include <linux/netlink.h>
11 #include <linux/rtnetlink.h>
12 #include <net/if.h>
13 #include <net/if_arp.h>
14 #include <netinet/ip.h>
15 #include <stdarg.h>
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/ioctl.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
24 #include <unistd.h>
25 
26 #include <rte_alarm.h>
27 #include <rte_bus.h>
28 #include <rte_bus_vdev.h>
29 #include <rte_common.h>
30 #include <rte_config.h>
31 #include <rte_dev.h>
32 #include <rte_errno.h>
33 #include <rte_ethdev.h>
34 #include <rte_ether.h>
35 #include <rte_hypervisor.h>
36 #include <rte_kvargs.h>
37 #include <rte_log.h>
38 #include <rte_string_fns.h>
39 
40 #define VDEV_NETVSC_DRIVER net_vdev_netvsc
41 #define VDEV_NETVSC_DRIVER_NAME RTE_STR(VDEV_NETVSC_DRIVER)
42 #define VDEV_NETVSC_DRIVER_NAME_LEN 15
43 #define VDEV_NETVSC_ARG_IFACE "iface"
44 #define VDEV_NETVSC_ARG_MAC "mac"
45 #define VDEV_NETVSC_ARG_FORCE "force"
46 #define VDEV_NETVSC_ARG_IGNORE "ignore"
47 #define VDEV_NETVSC_PROBE_MS 1000
48 
49 #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}"
50 #define NETVSC_MAX_ROUTE_LINE_SIZE 300
51 
52 #define DRV_LOG(level, ...) \
53 	rte_log(RTE_LOG_ ## level, \
54 		vdev_netvsc_logtype, \
55 		RTE_FMT(VDEV_NETVSC_DRIVER_NAME ": " \
56 			RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
57 		RTE_FMT_TAIL(__VA_ARGS__,)))
58 
59 /** Driver-specific log messages type. */
60 static int vdev_netvsc_logtype;
61 
62 /** Context structure for a vdev_netvsc instance. */
63 struct vdev_netvsc_ctx {
64 	LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */
65 	unsigned int id;		   /**< Unique ID. */
66 	char name[64];			   /**< Unique name. */
67 	char devname[64];		   /**< Fail-safe instance name. */
68 	char devargs[256];		   /**< Fail-safe device arguments. */
69 	char if_name[IF_NAMESIZE];	   /**< NetVSC netdevice name. */
70 	unsigned int if_index;		   /**< NetVSC netdevice index. */
71 	struct ether_addr if_addr;	   /**< NetVSC MAC address. */
72 	int pipe[2];			   /**< Fail-safe communication pipe. */
73 	char yield[256];		   /**< PCI sub-device arguments. */
74 };
75 
76 /** Context list is common to all driver instances. */
77 static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list =
78 	LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list);
79 
80 /** Number of entries in context list. */
81 static unsigned int vdev_netvsc_ctx_count;
82 
83 /** Number of driver instances relying on context list. */
84 static unsigned int vdev_netvsc_ctx_inst;
85 
86 /**
87  * Destroy a vdev_netvsc context instance.
88  *
89  * @param ctx
90  *   Context to destroy.
91  */
92 static void
93 vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
94 {
95 	if (ctx->pipe[0] != -1)
96 		close(ctx->pipe[0]);
97 	if (ctx->pipe[1] != -1)
98 		close(ctx->pipe[1]);
99 	free(ctx);
100 }
101 
102 /**
103  * Determine if a network interface is NetVSC.
104  *
105  * @param[in] iface
106  *   Pointer to netdevice description structure (name and index).
107  *
108  * @return
109  *   A nonzero value when interface is detected as NetVSC. In case of error,
110  *   rte_errno is updated and 0 returned.
111  */
112 static int
113 vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
114 {
115 	static const char temp[] = "/sys/class/net/%s/device/class_id";
116 	char path[sizeof(temp) + IF_NAMESIZE];
117 	FILE *f;
118 	int ret;
119 	int len = 0;
120 
121 	ret = snprintf(path, sizeof(path), temp, iface->if_name);
122 	if (ret == -1 || (size_t)ret >= sizeof(path)) {
123 		rte_errno = ENOBUFS;
124 		return 0;
125 	}
126 	f = fopen(path, "r");
127 	if (!f) {
128 		rte_errno = errno;
129 		return 0;
130 	}
131 	ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
132 	if (ret == EOF)
133 		rte_errno = errno;
134 	ret = len == (int)strlen(NETVSC_CLASS_ID);
135 	fclose(f);
136 	return ret;
137 }
138 
139 /**
140  * Iterate over system network interfaces.
141  *
142  * This function runs a given callback function for each netdevice found on
143  * the system.
144  *
145  * @param func
146  *   Callback function pointer. List traversal is aborted when this function
147  *   returns a nonzero value.
148  * @param is_netvsc
149  *   Indicates the device type to iterate - netvsc or non-netvsc.
150  * @param ...
151  *   Variable parameter list passed as @p va_list to @p func.
152  *
153  * @return
154  *   0 when the entire list is traversed successfully, a negative error code
155  *   in case or failure, or the nonzero value returned by @p func when list
156  *   traversal is aborted.
157  */
158 static int
159 vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
160 				      const struct ether_addr *eth_addr,
161 				      va_list ap), int is_netvsc, ...)
162 {
163 	struct if_nameindex *iface = if_nameindex();
164 	int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
165 	unsigned int i;
166 	int ret = 0;
167 
168 	if (!iface) {
169 		ret = -ENOBUFS;
170 		DRV_LOG(ERR, "cannot retrieve system network interfaces");
171 		goto error;
172 	}
173 	if (s == -1) {
174 		ret = -errno;
175 		DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
176 		goto error;
177 	}
178 	for (i = 0; iface[i].if_name; ++i) {
179 		int is_netvsc_ret;
180 		struct ifreq req;
181 		struct ether_addr eth_addr;
182 		va_list ap;
183 
184 		is_netvsc_ret = vdev_netvsc_iface_is_netvsc(&iface[i]) ? 1 : 0;
185 		if (is_netvsc ^ is_netvsc_ret)
186 			continue;
187 		strlcpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name));
188 		if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
189 			DRV_LOG(WARNING, "cannot retrieve information about"
190 					 " interface \"%s\": %s",
191 					 req.ifr_name, rte_strerror(errno));
192 			continue;
193 		}
194 		if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER)
195 			continue;
196 		memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
197 		       RTE_DIM(eth_addr.addr_bytes));
198 		va_start(ap, is_netvsc);
199 		ret = func(&iface[i], &eth_addr, ap);
200 		va_end(ap);
201 		if (ret)
202 			break;
203 	}
204 error:
205 	if (s != -1)
206 		close(s);
207 	if (iface)
208 		if_freenameindex(iface);
209 	return ret;
210 }
211 
212 /**
213  * Determine if a network interface has a route.
214  *
215  * @param[in] name
216  *   Network device name.
217  * @param[in] family
218  *   Address family: AF_INET for IPv4 or AF_INET6 for IPv6.
219  *
220  * @return
221  *   1 when interface has a route, negative errno value in case of error and
222  *   0 otherwise.
223  */
224 static int
225 vdev_netvsc_has_route(const struct if_nameindex *iface,
226 		      const unsigned char family)
227 {
228 	/*
229 	 * The implementation can be simpler by getifaddrs() function usage but
230 	 * it works for IPv6 only starting from glibc 2.3.3.
231 	 */
232 	char buf[4096];
233 	int len;
234 	int ret = 0;
235 	int res;
236 	int sock;
237 	struct nlmsghdr *retmsg = (struct nlmsghdr *)buf;
238 	struct sockaddr_nl sa;
239 	struct {
240 		struct nlmsghdr nlhdr;
241 		struct ifaddrmsg addrmsg;
242 	} msg;
243 
244 	if (!iface || (family != AF_INET && family != AF_INET6)) {
245 		DRV_LOG(ERR, "%s", rte_strerror(EINVAL));
246 		return -EINVAL;
247 	}
248 	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
249 	if (sock == -1) {
250 		DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
251 		return -errno;
252 	}
253 	memset(&sa, 0, sizeof(sa));
254 	sa.nl_family = AF_NETLINK;
255 	sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
256 	res = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
257 	if (res == -1) {
258 		ret = -errno;
259 		DRV_LOG(ERR, "cannot bind socket: %s", rte_strerror(errno));
260 		goto close;
261 	}
262 	memset(&msg, 0, sizeof(msg));
263 	msg.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
264 	msg.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
265 	msg.nlhdr.nlmsg_type = RTM_GETADDR;
266 	msg.nlhdr.nlmsg_pid = getpid();
267 	msg.addrmsg.ifa_family = family;
268 	msg.addrmsg.ifa_index = iface->if_index;
269 	res = send(sock, &msg, msg.nlhdr.nlmsg_len, 0);
270 	if (res == -1) {
271 		ret = -errno;
272 		DRV_LOG(ERR, "cannot send socket message: %s",
273 			rte_strerror(errno));
274 		goto close;
275 	}
276 	memset(buf, 0, sizeof(buf));
277 	len = recv(sock, buf, sizeof(buf), 0);
278 	if (len == -1) {
279 		ret = -errno;
280 		DRV_LOG(ERR, "cannot receive socket message: %s",
281 			rte_strerror(errno));
282 		goto close;
283 	}
284 	while (NLMSG_OK(retmsg, (unsigned int)len)) {
285 		struct ifaddrmsg *retaddr =
286 				(struct ifaddrmsg *)NLMSG_DATA(retmsg);
287 
288 		if (retaddr->ifa_family == family &&
289 		    retaddr->ifa_index == iface->if_index) {
290 			struct rtattr *retrta = IFA_RTA(retaddr);
291 			int attlen = IFA_PAYLOAD(retmsg);
292 
293 			while (RTA_OK(retrta, attlen)) {
294 				if (retrta->rta_type == IFA_ADDRESS) {
295 					ret = 1;
296 					DRV_LOG(DEBUG, "interface %s has IP",
297 						iface->if_name);
298 					goto close;
299 				}
300 				retrta = RTA_NEXT(retrta, attlen);
301 			}
302 		}
303 		retmsg = NLMSG_NEXT(retmsg, len);
304 	}
305 close:
306 	close(sock);
307 	return ret;
308 }
309 
310 /**
311  * Retrieve network interface data from sysfs symbolic link.
312  *
313  * @param[out] buf
314  *   Output data buffer.
315  * @param size
316  *   Output buffer size.
317  * @param[in] if_name
318  *   Netdevice name.
319  * @param[in] relpath
320  *   Symbolic link path relative to netdevice sysfs entry.
321  *
322  * @return
323  *   0 on success, a negative error code otherwise.
324  */
325 static int
326 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
327 			   const char *relpath)
328 {
329 	struct vdev_netvsc_ctx *ctx;
330 	char in[RTE_MAX(sizeof(ctx->yield), 256u)];
331 	int ret;
332 
333 	ret = snprintf(in, sizeof(in) - 1, "/sys/class/net/%s/%s",
334 		       if_name, relpath);
335 	if (ret == -1 || (size_t)ret >= sizeof(in))
336 		return -ENOBUFS;
337 	ret = readlink(in, buf, size);
338 	if (ret == -1)
339 		return -errno;
340 	if ((size_t)ret >= size - 1)
341 		return -ENOBUFS;
342 	buf[ret] = '\0';
343 	return 0;
344 }
345 
346 /**
347  * Probe a network interface to associate with vdev_netvsc context.
348  *
349  * This function determines if the network device matches the properties of
350  * the NetVSC interface associated with the vdev_netvsc context and
351  * communicates its bus address to the fail-safe PMD instance if so.
352  *
353  * It is normally used with vdev_netvsc_foreach_iface().
354  *
355  * @param[in] iface
356  *   Pointer to netdevice description structure (name and index).
357  * @param[in] eth_addr
358  *   MAC address associated with @p iface.
359  * @param ap
360  *   Variable arguments list comprising:
361  *
362  *   - struct vdev_netvsc_ctx *ctx:
363  *     Context to associate network interface with.
364  *
365  * @return
366  *   A nonzero value when interface matches, 0 otherwise or in case of
367  *   error.
368  */
369 static int
370 vdev_netvsc_device_probe(const struct if_nameindex *iface,
371 		    const struct ether_addr *eth_addr,
372 		    va_list ap)
373 {
374 	struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
375 	char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
376 	const char *addr;
377 	size_t len;
378 	int ret;
379 
380 	/* Skip non-matching or unwanted NetVSC interfaces. */
381 	if (ctx->if_index == iface->if_index) {
382 		if (!strcmp(ctx->if_name, iface->if_name))
383 			return 0;
384 		DRV_LOG(DEBUG,
385 			"NetVSC interface \"%s\" (index %u) renamed \"%s\"",
386 			ctx->if_name, ctx->if_index, iface->if_name);
387 		strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
388 		return 0;
389 	}
390 	if (!is_same_ether_addr(eth_addr, &ctx->if_addr))
391 		return 0;
392 	/* Look for associated PCI device. */
393 	ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
394 					 "device/subsystem");
395 	if (ret)
396 		return 0;
397 	addr = strrchr(buf, '/');
398 	addr = addr ? addr + 1 : buf;
399 	if (strcmp(addr, "pci"))
400 		return 0;
401 	ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
402 					 "device");
403 	if (ret)
404 		return 0;
405 	addr = strrchr(buf, '/');
406 	addr = addr ? addr + 1 : buf;
407 	len = strlen(addr);
408 	if (!len)
409 		return 0;
410 	/* Send PCI device argument to fail-safe PMD instance. */
411 	if (strcmp(addr, ctx->yield))
412 		DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
413 			" interface \"%s\" (index %u)", addr, ctx->if_name,
414 			ctx->if_index);
415 	memmove(buf, addr, len + 1);
416 	addr = buf;
417 	buf[len] = '\n';
418 	ret = write(ctx->pipe[1], addr, len + 1);
419 	buf[len] = '\0';
420 	if (ret == -1) {
421 		if (errno == EINTR || errno == EAGAIN)
422 			return 1;
423 		DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
424 			" interface \"%s\": %s", addr, ctx->if_name,
425 			rte_strerror(errno));
426 		return 1;
427 	}
428 	if ((size_t)ret != len + 1) {
429 		/*
430 		 * Attempt to override previous partial write, no need to
431 		 * recover if that fails.
432 		 */
433 		ret = write(ctx->pipe[1], "\n", 1);
434 		(void)ret;
435 		return 1;
436 	}
437 	fsync(ctx->pipe[1]);
438 	memcpy(ctx->yield, addr, len + 1);
439 	return 1;
440 }
441 
442 /**
443  * Alarm callback that regularly probes system network interfaces.
444  *
445  * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
446  * long as an vdev_netvsc context instance exists.
447  *
448  * @param arg
449  *   Ignored.
450  */
451 static void
452 vdev_netvsc_alarm(__rte_unused void *arg)
453 {
454 	struct vdev_netvsc_ctx *ctx;
455 	int ret;
456 
457 	LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
458 		ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0,
459 		      ctx);
460 		if (ret < 0)
461 			break;
462 	}
463 	if (!vdev_netvsc_ctx_count)
464 		return;
465 	ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
466 				vdev_netvsc_alarm, NULL);
467 	if (ret < 0) {
468 		DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
469 			rte_strerror(-ret));
470 	}
471 }
472 
473 /**
474  * Probe a NetVSC interface to generate a vdev_netvsc context from.
475  *
476  * This function instantiates vdev_netvsc contexts either for all NetVSC
477  * devices found on the system or only a subset provided as device
478  * arguments.
479  *
480  * It is normally used with vdev_netvsc_foreach_iface().
481  *
482  * @param[in] iface
483  *   Pointer to netdevice description structure (name and index).
484  * @param[in] eth_addr
485  *   MAC address associated with @p iface.
486  * @param ap
487  *   Variable arguments list comprising:
488  *
489  *   - const char *name:
490  *     Name associated with current driver instance.
491  *
492  *   - struct rte_kvargs *kvargs:
493  *     Device arguments provided to current driver instance.
494  *
495  *   - int force:
496  *     Accept specified interface even if not detected as NetVSC.
497  *
498  *   - unsigned int specified:
499  *     Number of specific netdevices provided as device arguments.
500  *
501  *   - unsigned int *matched:
502  *     The number of specified netdevices matched by this function.
503  *
504  * @return
505  *   A nonzero value when interface matches, 0 otherwise or in case of
506  *   error.
507  */
508 static int
509 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
510 			 const struct ether_addr *eth_addr,
511 			 va_list ap)
512 {
513 	const char *name = va_arg(ap, const char *);
514 	struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
515 	unsigned int specified = va_arg(ap, unsigned int);
516 	unsigned int *matched = va_arg(ap, unsigned int *);
517 	unsigned int i;
518 	struct vdev_netvsc_ctx *ctx;
519 	int ret;
520 
521 	/* Probe all interfaces when none are specified. */
522 	if (specified) {
523 		for (i = 0; i != kvargs->count; ++i) {
524 			const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
525 
526 			if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
527 				if (!strcmp(pair->value, iface->if_name))
528 					break;
529 			} else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
530 				struct ether_addr tmp;
531 
532 				if (sscanf(pair->value,
533 					   "%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":"
534 					   "%" SCNx8 ":%" SCNx8 ":%" SCNx8,
535 					   &tmp.addr_bytes[0],
536 					   &tmp.addr_bytes[1],
537 					   &tmp.addr_bytes[2],
538 					   &tmp.addr_bytes[3],
539 					   &tmp.addr_bytes[4],
540 					   &tmp.addr_bytes[5]) != 6) {
541 					DRV_LOG(ERR,
542 						"invalid MAC address format"
543 						" \"%s\"",
544 						pair->value);
545 					return -EINVAL;
546 				}
547 				if (is_same_ether_addr(eth_addr, &tmp))
548 					break;
549 			}
550 		}
551 		if (i == kvargs->count)
552 			return 0;
553 		++(*matched);
554 	}
555 	/* Weed out interfaces already handled. */
556 	LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
557 		if (ctx->if_index == iface->if_index)
558 			break;
559 	if (ctx) {
560 		if (!specified)
561 			return 0;
562 		DRV_LOG(WARNING,
563 			"interface \"%s\" (index %u) is already handled,"
564 			" skipping",
565 			iface->if_name, iface->if_index);
566 		return 0;
567 	}
568 	/* Routed NetVSC should not be probed. */
569 	if (vdev_netvsc_has_route(iface, AF_INET) ||
570 	    vdev_netvsc_has_route(iface, AF_INET6)) {
571 		if (!specified)
572 			return 0;
573 		DRV_LOG(WARNING, "probably using routed NetVSC interface \"%s\""
574 			" (index %u)", iface->if_name, iface->if_index);
575 	}
576 	/* Create interface context. */
577 	ctx = calloc(1, sizeof(*ctx));
578 	if (!ctx) {
579 		ret = -errno;
580 		DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
581 			iface->if_name, rte_strerror(errno));
582 		goto error;
583 	}
584 	ctx->id = vdev_netvsc_ctx_count;
585 	strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
586 	ctx->if_index = iface->if_index;
587 	ctx->if_addr = *eth_addr;
588 	ctx->pipe[0] = -1;
589 	ctx->pipe[1] = -1;
590 	ctx->yield[0] = '\0';
591 	if (pipe(ctx->pipe) == -1) {
592 		ret = -errno;
593 		DRV_LOG(ERR,
594 			"cannot allocate control pipe for interface \"%s\": %s",
595 			ctx->if_name, rte_strerror(errno));
596 		goto error;
597 	}
598 	for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
599 		int flf = fcntl(ctx->pipe[i], F_GETFL);
600 
601 		if (flf != -1 &&
602 		    fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
603 			continue;
604 		ret = -errno;
605 		DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
606 			" descriptor #%u (%d): %s", i, ctx->pipe[i],
607 			rte_strerror(errno));
608 		goto error;
609 	}
610 	/* Generate virtual device name and arguments. */
611 	i = 0;
612 	ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
613 		       name, ctx->id);
614 	if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
615 		++i;
616 	ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_vsc%u",
617 		       ctx->id);
618 	if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
619 		++i;
620 	ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
621 		       "fd(%d),dev(net_tap_vsc%u,remote=%s)",
622 		       ctx->pipe[0], ctx->id, ctx->if_name);
623 	if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
624 		++i;
625 	if (i) {
626 		ret = -ENOBUFS;
627 		DRV_LOG(ERR, "generated virtual device name or argument list"
628 			" too long for interface \"%s\"", ctx->if_name);
629 		goto error;
630 	}
631 	/* Request virtual device generation. */
632 	DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
633 		ctx->devname, ctx->devargs);
634 	vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0, ctx);
635 	ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
636 	if (ret)
637 		goto error;
638 	LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
639 	++vdev_netvsc_ctx_count;
640 	DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
641 		ctx->if_name);
642 	return 0;
643 error:
644 	if (ctx)
645 		vdev_netvsc_ctx_destroy(ctx);
646 	return ret;
647 }
648 
649 /**
650  * Probe NetVSC interfaces.
651  *
652  * This function probes system netdevices according to the specified device
653  * arguments and starts a periodic alarm callback to notify the resulting
654  * fail-safe PMD instances of their sub-devices whereabouts.
655  *
656  * @param dev
657  *   Virtual device context for driver instance.
658  *
659  * @return
660  *    Always 0, even in case of errors.
661  */
662 static int
663 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
664 {
665 	static const char *const vdev_netvsc_arg[] = {
666 		VDEV_NETVSC_ARG_IFACE,
667 		VDEV_NETVSC_ARG_MAC,
668 		VDEV_NETVSC_ARG_FORCE,
669 		VDEV_NETVSC_ARG_IGNORE,
670 		NULL,
671 	};
672 	const char *name = rte_vdev_device_name(dev);
673 	const char *args = rte_vdev_device_args(dev);
674 	struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
675 						     vdev_netvsc_arg);
676 	unsigned int specified = 0;
677 	unsigned int matched = 0;
678 	int force = 0;
679 	int ignore = 0;
680 	unsigned int i;
681 	int ret;
682 
683 	DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
684 	if (!kvargs) {
685 		DRV_LOG(ERR, "cannot parse arguments list");
686 		goto error;
687 	}
688 	for (i = 0; i != kvargs->count; ++i) {
689 		const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
690 
691 		if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE))
692 			force = !!atoi(pair->value);
693 		else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IGNORE))
694 			ignore = !!atoi(pair->value);
695 		else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
696 			 !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
697 			++specified;
698 	}
699 	if (ignore) {
700 		if (kvargs)
701 			rte_kvargs_free(kvargs);
702 		return 0;
703 	}
704 	if (specified > 1) {
705 		DRV_LOG(ERR, "More than one way used to specify the netvsc"
706 			" device.");
707 		goto error;
708 	}
709 	rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
710 	/* Gather interfaces. */
711 	ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 1, name,
712 					kvargs, specified, &matched);
713 	if (ret < 0)
714 		goto error;
715 	if (specified && matched < specified) {
716 		if (!force) {
717 			DRV_LOG(ERR, "Cannot find the specified netvsc device");
718 			goto error;
719 		}
720 		/* Try to force probing on non-netvsc specified device. */
721 		if (vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 0, name,
722 					      kvargs, specified, &matched) < 0)
723 			goto error;
724 		if (matched < specified) {
725 			DRV_LOG(ERR, "Cannot find the specified device");
726 			goto error;
727 		}
728 		DRV_LOG(WARNING, "non-netvsc device was probed as netvsc");
729 	}
730 	ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
731 				vdev_netvsc_alarm, NULL);
732 	if (ret < 0) {
733 		DRV_LOG(ERR, "unable to schedule alarm callback: %s",
734 			rte_strerror(-ret));
735 		goto error;
736 	}
737 error:
738 	if (kvargs)
739 		rte_kvargs_free(kvargs);
740 	++vdev_netvsc_ctx_inst;
741 	return 0;
742 }
743 
744 /**
745  * Remove driver instance.
746  *
747  * The alarm callback and underlying vdev_netvsc context instances are only
748  * destroyed after the last PMD instance is removed.
749  *
750  * @param dev
751  *   Virtual device context for driver instance.
752  *
753  * @return
754  *   Always 0.
755  */
756 static int
757 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
758 {
759 	if (--vdev_netvsc_ctx_inst)
760 		return 0;
761 	rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
762 	while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
763 		struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
764 
765 		LIST_REMOVE(ctx, entry);
766 		--vdev_netvsc_ctx_count;
767 		vdev_netvsc_ctx_destroy(ctx);
768 	}
769 	return 0;
770 }
771 
772 /** Virtual device descriptor. */
773 static struct rte_vdev_driver vdev_netvsc_vdev = {
774 	.probe = vdev_netvsc_vdev_probe,
775 	.remove = vdev_netvsc_vdev_remove,
776 };
777 
778 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
779 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
780 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
781 			      VDEV_NETVSC_ARG_IFACE "=<string> "
782 			      VDEV_NETVSC_ARG_MAC "=<string> "
783 			      VDEV_NETVSC_ARG_FORCE "=<int> "
784 			      VDEV_NETVSC_ARG_IGNORE "=<int>");
785 
786 /** Initialize driver log type. */
787 RTE_INIT(vdev_netvsc_init_log)
788 {
789 	vdev_netvsc_logtype = rte_log_register("pmd.net.vdev_netvsc");
790 	if (vdev_netvsc_logtype >= 0)
791 		rte_log_set_level(vdev_netvsc_logtype, RTE_LOG_NOTICE);
792 }
793 
794 /** Compare function for vdev find device operation. */
795 static int
796 vdev_netvsc_cmp_rte_device(const struct rte_device *dev1,
797 			   __rte_unused const void *_dev2)
798 {
799 	return strncmp(dev1->devargs->name, VDEV_NETVSC_DRIVER_NAME,
800 		       VDEV_NETVSC_DRIVER_NAME_LEN);
801 }
802 
803 /**
804  * A callback called by vdev bus scan function to ensure this driver probing
805  * automatically in Hyper-V VM system unless it already exists in the
806  * devargs list.
807  */
808 static void
809 vdev_netvsc_scan_callback(__rte_unused void *arg)
810 {
811 	struct rte_device *dev;
812 	struct rte_devargs *devargs;
813 	struct rte_bus *vbus = rte_bus_find_by_name("vdev");
814 
815 	RTE_EAL_DEVARGS_FOREACH("vdev", devargs)
816 		if (!strncmp(devargs->name, VDEV_NETVSC_DRIVER_NAME,
817 			     VDEV_NETVSC_DRIVER_NAME_LEN))
818 			return;
819 
820 	dev = vbus->find_device(NULL, vdev_netvsc_cmp_rte_device,
821 				VDEV_NETVSC_DRIVER_NAME);
822 	if (dev)
823 		return;
824 	if (rte_devargs_add(RTE_DEVTYPE_VIRTUAL, VDEV_NETVSC_DRIVER_NAME))
825 		DRV_LOG(ERR, "unable to add netvsc devargs.");
826 }
827 
828 /** Initialize the custom scan. */
829 RTE_INIT(vdev_netvsc_custom_scan_add)
830 {
831 	if (rte_hypervisor_get() == RTE_HYPERVISOR_HYPERV)
832 		rte_vdev_add_custom_scan(vdev_netvsc_scan_callback, NULL);
833 }
834