New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / net / vdev_netvsc / vdev_netvsc.c
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                         DRV_LOG(DEBUG, "interface %s is non-ethernet device",
196                                 req.ifr_name);
197                         continue;
198                 }
199                 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
200                        RTE_DIM(eth_addr.addr_bytes));
201                 va_start(ap, is_netvsc);
202                 ret = func(&iface[i], &eth_addr, ap);
203                 va_end(ap);
204                 if (ret)
205                         break;
206         }
207 error:
208         if (s != -1)
209                 close(s);
210         if (iface)
211                 if_freenameindex(iface);
212         return ret;
213 }
214
215 /**
216  * Determine if a network interface has a route.
217  *
218  * @param[in] name
219  *   Network device name.
220  * @param[in] family
221  *   Address family: AF_INET for IPv4 or AF_INET6 for IPv6.
222  *
223  * @return
224  *   1 when interface has a route, negative errno value in case of error and
225  *   0 otherwise.
226  */
227 static int
228 vdev_netvsc_has_route(const struct if_nameindex *iface,
229                       const unsigned char family)
230 {
231         /*
232          * The implementation can be simpler by getifaddrs() function usage but
233          * it works for IPv6 only starting from glibc 2.3.3.
234          */
235         char buf[4096];
236         int len;
237         int ret = 0;
238         int res;
239         int sock;
240         struct nlmsghdr *retmsg = (struct nlmsghdr *)buf;
241         struct sockaddr_nl sa;
242         struct {
243                 struct nlmsghdr nlhdr;
244                 struct ifaddrmsg addrmsg;
245         } msg;
246
247         if (!iface || (family != AF_INET && family != AF_INET6)) {
248                 DRV_LOG(ERR, "%s", rte_strerror(EINVAL));
249                 return -EINVAL;
250         }
251         sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
252         if (sock == -1) {
253                 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
254                 return -errno;
255         }
256         memset(&sa, 0, sizeof(sa));
257         sa.nl_family = AF_NETLINK;
258         sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
259         res = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
260         if (res == -1) {
261                 ret = -errno;
262                 DRV_LOG(ERR, "cannot bind socket: %s", rte_strerror(errno));
263                 goto close;
264         }
265         memset(&msg, 0, sizeof(msg));
266         msg.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
267         msg.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
268         msg.nlhdr.nlmsg_type = RTM_GETADDR;
269         msg.nlhdr.nlmsg_pid = getpid();
270         msg.addrmsg.ifa_family = family;
271         msg.addrmsg.ifa_index = iface->if_index;
272         res = send(sock, &msg, msg.nlhdr.nlmsg_len, 0);
273         if (res == -1) {
274                 ret = -errno;
275                 DRV_LOG(ERR, "cannot send socket message: %s",
276                         rte_strerror(errno));
277                 goto close;
278         }
279         memset(buf, 0, sizeof(buf));
280         len = recv(sock, buf, sizeof(buf), 0);
281         if (len == -1) {
282                 ret = -errno;
283                 DRV_LOG(ERR, "cannot receive socket message: %s",
284                         rte_strerror(errno));
285                 goto close;
286         }
287         while (NLMSG_OK(retmsg, (unsigned int)len)) {
288                 struct ifaddrmsg *retaddr =
289                                 (struct ifaddrmsg *)NLMSG_DATA(retmsg);
290
291                 if (retaddr->ifa_family == family &&
292                     retaddr->ifa_index == iface->if_index) {
293                         struct rtattr *retrta = IFA_RTA(retaddr);
294                         int attlen = IFA_PAYLOAD(retmsg);
295
296                         while (RTA_OK(retrta, attlen)) {
297                                 if (retrta->rta_type == IFA_ADDRESS) {
298                                         ret = 1;
299                                         DRV_LOG(DEBUG, "interface %s has IP",
300                                                 iface->if_name);
301                                         goto close;
302                                 }
303                                 retrta = RTA_NEXT(retrta, attlen);
304                         }
305                 }
306                 retmsg = NLMSG_NEXT(retmsg, len);
307         }
308 close:
309         close(sock);
310         return ret;
311 }
312
313 /**
314  * Retrieve network interface data from sysfs symbolic link.
315  *
316  * @param[out] buf
317  *   Output data buffer.
318  * @param size
319  *   Output buffer size.
320  * @param[in] if_name
321  *   Netdevice name.
322  * @param[in] relpath
323  *   Symbolic link path relative to netdevice sysfs entry.
324  *
325  * @return
326  *   0 on success, a negative error code otherwise.
327  */
328 static int
329 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
330                            const char *relpath)
331 {
332         struct vdev_netvsc_ctx *ctx;
333         char in[RTE_MAX(sizeof(ctx->yield), 256u)];
334         int ret;
335
336         ret = snprintf(in, sizeof(in) - 1, "/sys/class/net/%s/%s",
337                        if_name, relpath);
338         if (ret == -1 || (size_t)ret >= sizeof(in))
339                 return -ENOBUFS;
340         ret = readlink(in, buf, size);
341         if (ret == -1)
342                 return -errno;
343         if ((size_t)ret >= size - 1)
344                 return -ENOBUFS;
345         buf[ret] = '\0';
346         return 0;
347 }
348
349 /**
350  * Probe a network interface to associate with vdev_netvsc context.
351  *
352  * This function determines if the network device matches the properties of
353  * the NetVSC interface associated with the vdev_netvsc context and
354  * communicates its bus address to the fail-safe PMD instance if so.
355  *
356  * It is normally used with vdev_netvsc_foreach_iface().
357  *
358  * @param[in] iface
359  *   Pointer to netdevice description structure (name and index).
360  * @param[in] eth_addr
361  *   MAC address associated with @p iface.
362  * @param ap
363  *   Variable arguments list comprising:
364  *
365  *   - struct vdev_netvsc_ctx *ctx:
366  *     Context to associate network interface with.
367  *
368  * @return
369  *   A nonzero value when interface matches, 0 otherwise or in case of
370  *   error.
371  */
372 static int
373 vdev_netvsc_device_probe(const struct if_nameindex *iface,
374                     const struct ether_addr *eth_addr,
375                     va_list ap)
376 {
377         struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
378         char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
379         const char *addr;
380         size_t len;
381         int ret;
382
383         /* Skip non-matching or unwanted NetVSC interfaces. */
384         if (ctx->if_index == iface->if_index) {
385                 if (!strcmp(ctx->if_name, iface->if_name))
386                         return 0;
387                 DRV_LOG(DEBUG,
388                         "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
389                         ctx->if_name, ctx->if_index, iface->if_name);
390                 strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
391                 return 0;
392         }
393         if (!is_same_ether_addr(eth_addr, &ctx->if_addr))
394                 return 0;
395         /* Look for associated PCI device. */
396         ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
397                                          "device/subsystem");
398         if (ret)
399                 return 0;
400         addr = strrchr(buf, '/');
401         addr = addr ? addr + 1 : buf;
402         if (strcmp(addr, "pci"))
403                 return 0;
404         ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
405                                          "device");
406         if (ret)
407                 return 0;
408         addr = strrchr(buf, '/');
409         addr = addr ? addr + 1 : buf;
410         len = strlen(addr);
411         if (!len)
412                 return 0;
413         /* Send PCI device argument to fail-safe PMD instance. */
414         if (strcmp(addr, ctx->yield))
415                 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
416                         " interface \"%s\" (index %u)", addr, ctx->if_name,
417                         ctx->if_index);
418         memmove(buf, addr, len + 1);
419         addr = buf;
420         buf[len] = '\n';
421         ret = write(ctx->pipe[1], addr, len + 1);
422         buf[len] = '\0';
423         if (ret == -1) {
424                 if (errno == EINTR || errno == EAGAIN)
425                         return 1;
426                 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
427                         " interface \"%s\": %s", addr, ctx->if_name,
428                         rte_strerror(errno));
429                 return 1;
430         }
431         if ((size_t)ret != len + 1) {
432                 /*
433                  * Attempt to override previous partial write, no need to
434                  * recover if that fails.
435                  */
436                 ret = write(ctx->pipe[1], "\n", 1);
437                 (void)ret;
438                 return 1;
439         }
440         fsync(ctx->pipe[1]);
441         memcpy(ctx->yield, addr, len + 1);
442         return 1;
443 }
444
445 /**
446  * Alarm callback that regularly probes system network interfaces.
447  *
448  * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
449  * long as an vdev_netvsc context instance exists.
450  *
451  * @param arg
452  *   Ignored.
453  */
454 static void
455 vdev_netvsc_alarm(__rte_unused void *arg)
456 {
457         struct vdev_netvsc_ctx *ctx;
458         int ret;
459
460         LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
461                 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0,
462                       ctx);
463                 if (ret < 0)
464                         break;
465         }
466         if (!vdev_netvsc_ctx_count)
467                 return;
468         ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
469                                 vdev_netvsc_alarm, NULL);
470         if (ret < 0) {
471                 DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
472                         rte_strerror(-ret));
473         }
474 }
475
476 /**
477  * Probe a NetVSC interface to generate a vdev_netvsc context from.
478  *
479  * This function instantiates vdev_netvsc contexts either for all NetVSC
480  * devices found on the system or only a subset provided as device
481  * arguments.
482  *
483  * It is normally used with vdev_netvsc_foreach_iface().
484  *
485  * @param[in] iface
486  *   Pointer to netdevice description structure (name and index).
487  * @param[in] eth_addr
488  *   MAC address associated with @p iface.
489  * @param ap
490  *   Variable arguments list comprising:
491  *
492  *   - const char *name:
493  *     Name associated with current driver instance.
494  *
495  *   - struct rte_kvargs *kvargs:
496  *     Device arguments provided to current driver instance.
497  *
498  *   - int force:
499  *     Accept specified interface even if not detected as NetVSC.
500  *
501  *   - unsigned int specified:
502  *     Number of specific netdevices provided as device arguments.
503  *
504  *   - unsigned int *matched:
505  *     The number of specified netdevices matched by this function.
506  *
507  * @return
508  *   A nonzero value when interface matches, 0 otherwise or in case of
509  *   error.
510  */
511 static int
512 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
513                          const struct ether_addr *eth_addr,
514                          va_list ap)
515 {
516         const char *name = va_arg(ap, const char *);
517         struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
518         unsigned int specified = va_arg(ap, unsigned int);
519         unsigned int *matched = va_arg(ap, unsigned int *);
520         unsigned int i;
521         struct vdev_netvsc_ctx *ctx;
522         int ret;
523
524         /* Probe all interfaces when none are specified. */
525         if (specified) {
526                 for (i = 0; i != kvargs->count; ++i) {
527                         const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
528
529                         if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
530                                 if (!strcmp(pair->value, iface->if_name))
531                                         break;
532                         } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
533                                 struct ether_addr tmp;
534
535                                 if (sscanf(pair->value,
536                                            "%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":"
537                                            "%" SCNx8 ":%" SCNx8 ":%" SCNx8,
538                                            &tmp.addr_bytes[0],
539                                            &tmp.addr_bytes[1],
540                                            &tmp.addr_bytes[2],
541                                            &tmp.addr_bytes[3],
542                                            &tmp.addr_bytes[4],
543                                            &tmp.addr_bytes[5]) != 6) {
544                                         DRV_LOG(ERR,
545                                                 "invalid MAC address format"
546                                                 " \"%s\"",
547                                                 pair->value);
548                                         return -EINVAL;
549                                 }
550                                 if (is_same_ether_addr(eth_addr, &tmp))
551                                         break;
552                         }
553                 }
554                 if (i == kvargs->count)
555                         return 0;
556                 ++(*matched);
557         }
558         /* Weed out interfaces already handled. */
559         LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
560                 if (ctx->if_index == iface->if_index)
561                         break;
562         if (ctx) {
563                 if (!specified)
564                         return 0;
565                 DRV_LOG(WARNING,
566                         "interface \"%s\" (index %u) is already handled,"
567                         " skipping",
568                         iface->if_name, iface->if_index);
569                 return 0;
570         }
571         /* Routed NetVSC should not be probed. */
572         if (vdev_netvsc_has_route(iface, AF_INET) ||
573             vdev_netvsc_has_route(iface, AF_INET6)) {
574                 if (!specified)
575                         return 0;
576                 DRV_LOG(WARNING, "probably using routed NetVSC interface \"%s\""
577                         " (index %u)", iface->if_name, iface->if_index);
578         }
579         /* Create interface context. */
580         ctx = calloc(1, sizeof(*ctx));
581         if (!ctx) {
582                 ret = -errno;
583                 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
584                         iface->if_name, rte_strerror(errno));
585                 goto error;
586         }
587         ctx->id = vdev_netvsc_ctx_count;
588         strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
589         ctx->if_index = iface->if_index;
590         ctx->if_addr = *eth_addr;
591         ctx->pipe[0] = -1;
592         ctx->pipe[1] = -1;
593         ctx->yield[0] = '\0';
594         if (pipe(ctx->pipe) == -1) {
595                 ret = -errno;
596                 DRV_LOG(ERR,
597                         "cannot allocate control pipe for interface \"%s\": %s",
598                         ctx->if_name, rte_strerror(errno));
599                 goto error;
600         }
601         for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
602                 int flf = fcntl(ctx->pipe[i], F_GETFL);
603
604                 if (flf != -1 &&
605                     fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
606                         continue;
607                 ret = -errno;
608                 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
609                         " descriptor #%u (%d): %s", i, ctx->pipe[i],
610                         rte_strerror(errno));
611                 goto error;
612         }
613         /* Generate virtual device name and arguments. */
614         i = 0;
615         ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
616                        name, ctx->id);
617         if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
618                 ++i;
619         ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_vsc%u",
620                        ctx->id);
621         if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
622                 ++i;
623         ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
624                        "fd(%d),dev(net_tap_vsc%u,remote=%s)",
625                        ctx->pipe[0], ctx->id, ctx->if_name);
626         if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
627                 ++i;
628         if (i) {
629                 ret = -ENOBUFS;
630                 DRV_LOG(ERR, "generated virtual device name or argument list"
631                         " too long for interface \"%s\"", ctx->if_name);
632                 goto error;
633         }
634         /* Request virtual device generation. */
635         DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
636                 ctx->devname, ctx->devargs);
637         vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0, ctx);
638         ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
639         if (ret)
640                 goto error;
641         LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
642         ++vdev_netvsc_ctx_count;
643         DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
644                 ctx->if_name);
645         return 0;
646 error:
647         if (ctx)
648                 vdev_netvsc_ctx_destroy(ctx);
649         return ret;
650 }
651
652 /**
653  * Probe NetVSC interfaces.
654  *
655  * This function probes system netdevices according to the specified device
656  * arguments and starts a periodic alarm callback to notify the resulting
657  * fail-safe PMD instances of their sub-devices whereabouts.
658  *
659  * @param dev
660  *   Virtual device context for driver instance.
661  *
662  * @return
663  *    Always 0, even in case of errors.
664  */
665 static int
666 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
667 {
668         static const char *const vdev_netvsc_arg[] = {
669                 VDEV_NETVSC_ARG_IFACE,
670                 VDEV_NETVSC_ARG_MAC,
671                 VDEV_NETVSC_ARG_FORCE,
672                 VDEV_NETVSC_ARG_IGNORE,
673                 NULL,
674         };
675         const char *name = rte_vdev_device_name(dev);
676         const char *args = rte_vdev_device_args(dev);
677         struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
678                                                      vdev_netvsc_arg);
679         unsigned int specified = 0;
680         unsigned int matched = 0;
681         int force = 0;
682         int ignore = 0;
683         unsigned int i;
684         int ret;
685
686         DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
687         if (!kvargs) {
688                 DRV_LOG(ERR, "cannot parse arguments list");
689                 goto error;
690         }
691         for (i = 0; i != kvargs->count; ++i) {
692                 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
693
694                 if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE))
695                         force = !!atoi(pair->value);
696                 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IGNORE))
697                         ignore = !!atoi(pair->value);
698                 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
699                          !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
700                         ++specified;
701         }
702         if (ignore) {
703                 if (kvargs)
704                         rte_kvargs_free(kvargs);
705                 return 0;
706         }
707         if (specified > 1) {
708                 DRV_LOG(ERR, "More than one way used to specify the netvsc"
709                         " device.");
710                 goto error;
711         }
712         rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
713         /* Gather interfaces. */
714         ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 1, name,
715                                         kvargs, specified, &matched);
716         if (ret < 0)
717                 goto error;
718         if (specified && matched < specified) {
719                 if (!force) {
720                         DRV_LOG(ERR, "Cannot find the specified netvsc device");
721                         goto error;
722                 }
723                 /* Try to force probing on non-netvsc specified device. */
724                 if (vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 0, name,
725                                               kvargs, specified, &matched) < 0)
726                         goto error;
727                 if (matched < specified) {
728                         DRV_LOG(ERR, "Cannot find the specified device");
729                         goto error;
730                 }
731                 DRV_LOG(WARNING, "non-netvsc device was probed as netvsc");
732         }
733         ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
734                                 vdev_netvsc_alarm, NULL);
735         if (ret < 0) {
736                 DRV_LOG(ERR, "unable to schedule alarm callback: %s",
737                         rte_strerror(-ret));
738                 goto error;
739         }
740 error:
741         if (kvargs)
742                 rte_kvargs_free(kvargs);
743         ++vdev_netvsc_ctx_inst;
744         return 0;
745 }
746
747 /**
748  * Remove driver instance.
749  *
750  * The alarm callback and underlying vdev_netvsc context instances are only
751  * destroyed after the last PMD instance is removed.
752  *
753  * @param dev
754  *   Virtual device context for driver instance.
755  *
756  * @return
757  *   Always 0.
758  */
759 static int
760 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
761 {
762         if (--vdev_netvsc_ctx_inst)
763                 return 0;
764         rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
765         while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
766                 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
767
768                 LIST_REMOVE(ctx, entry);
769                 --vdev_netvsc_ctx_count;
770                 vdev_netvsc_ctx_destroy(ctx);
771         }
772         return 0;
773 }
774
775 /** Virtual device descriptor. */
776 static struct rte_vdev_driver vdev_netvsc_vdev = {
777         .probe = vdev_netvsc_vdev_probe,
778         .remove = vdev_netvsc_vdev_remove,
779 };
780
781 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
782 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
783 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
784                               VDEV_NETVSC_ARG_IFACE "=<string> "
785                               VDEV_NETVSC_ARG_MAC "=<string> "
786                               VDEV_NETVSC_ARG_FORCE "=<int> "
787                               VDEV_NETVSC_ARG_IGNORE "=<int>");
788
789 /** Initialize driver log type. */
790 RTE_INIT(vdev_netvsc_init_log)
791 {
792         vdev_netvsc_logtype = rte_log_register("pmd.net.vdev_netvsc");
793         if (vdev_netvsc_logtype >= 0)
794                 rte_log_set_level(vdev_netvsc_logtype, RTE_LOG_NOTICE);
795 }
796
797 /** Compare function for vdev find device operation. */
798 static int
799 vdev_netvsc_cmp_rte_device(const struct rte_device *dev1,
800                            __rte_unused const void *_dev2)
801 {
802         return strncmp(dev1->devargs->name, VDEV_NETVSC_DRIVER_NAME,
803                        VDEV_NETVSC_DRIVER_NAME_LEN);
804 }
805
806 /**
807  * A callback called by vdev bus scan function to ensure this driver probing
808  * automatically in Hyper-V VM system unless it already exists in the
809  * devargs list.
810  */
811 static void
812 vdev_netvsc_scan_callback(__rte_unused void *arg)
813 {
814         struct rte_vdev_device *dev;
815         struct rte_devargs *devargs;
816         struct rte_bus *vbus = rte_bus_find_by_name("vdev");
817
818         RTE_EAL_DEVARGS_FOREACH("vdev", devargs)
819                 if (!strncmp(devargs->name, VDEV_NETVSC_DRIVER_NAME,
820                              VDEV_NETVSC_DRIVER_NAME_LEN))
821                         return;
822         dev = (struct rte_vdev_device *)vbus->find_device(NULL,
823                 vdev_netvsc_cmp_rte_device, VDEV_NETVSC_DRIVER_NAME);
824         if (dev)
825                 return;
826         if (rte_devargs_add(RTE_DEVTYPE_VIRTUAL, VDEV_NETVSC_DRIVER_NAME))
827                 DRV_LOG(ERR, "unable to add netvsc devargs.");
828 }
829
830 /** Initialize the custom scan. */
831 RTE_INIT(vdev_netvsc_custom_scan_add)
832 {
833         if (rte_hypervisor_get() == RTE_HYPERVISOR_HYPERV)
834                 rte_vdev_add_custom_scan(vdev_netvsc_scan_callback, NULL);
835 }