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