net/vdev_netvsc: add force parameter
[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 <net/if.h>
11 #include <net/if_arp.h>
12 #include <netinet/ip.h>
13 #include <stdarg.h>
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/ioctl.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <unistd.h>
23
24 #include <rte_alarm.h>
25 #include <rte_bus.h>
26 #include <rte_bus_vdev.h>
27 #include <rte_common.h>
28 #include <rte_config.h>
29 #include <rte_dev.h>
30 #include <rte_errno.h>
31 #include <rte_ethdev.h>
32 #include <rte_ether.h>
33 #include <rte_kvargs.h>
34 #include <rte_log.h>
35
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_ARG_FORCE "force"
40 #define VDEV_NETVSC_PROBE_MS 1000
41
42 #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}"
43 #define NETVSC_MAX_ROUTE_LINE_SIZE 300
44
45 #define DRV_LOG(level, ...) \
46         rte_log(RTE_LOG_ ## level, \
47                 vdev_netvsc_logtype, \
48                 RTE_FMT(RTE_STR(VDEV_NETVSC_DRIVER) ": " \
49                         RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
50                 RTE_FMT_TAIL(__VA_ARGS__,)))
51
52 /** Driver-specific log messages type. */
53 static int vdev_netvsc_logtype;
54
55 /** Context structure for a vdev_netvsc instance. */
56 struct vdev_netvsc_ctx {
57         LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */
58         unsigned int id;                   /**< Unique ID. */
59         char name[64];                     /**< Unique name. */
60         char devname[64];                  /**< Fail-safe instance name. */
61         char devargs[256];                 /**< Fail-safe device arguments. */
62         char if_name[IF_NAMESIZE];         /**< NetVSC netdevice name. */
63         unsigned int if_index;             /**< NetVSC netdevice index. */
64         struct ether_addr if_addr;         /**< NetVSC MAC address. */
65         int pipe[2];                       /**< Fail-safe communication pipe. */
66         char yield[256];                   /**< PCI sub-device arguments. */
67 };
68
69 /** Context list is common to all driver instances. */
70 static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list =
71         LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list);
72
73 /** Number of entries in context list. */
74 static unsigned int vdev_netvsc_ctx_count;
75
76 /** Number of driver instances relying on context list. */
77 static unsigned int vdev_netvsc_ctx_inst;
78
79 /**
80  * Destroy a vdev_netvsc context instance.
81  *
82  * @param ctx
83  *   Context to destroy.
84  */
85 static void
86 vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
87 {
88         if (ctx->pipe[0] != -1)
89                 close(ctx->pipe[0]);
90         if (ctx->pipe[1] != -1)
91                 close(ctx->pipe[1]);
92         free(ctx);
93 }
94
95 /**
96  * Iterate over system network interfaces.
97  *
98  * This function runs a given callback function for each netdevice found on
99  * the system.
100  *
101  * @param func
102  *   Callback function pointer. List traversal is aborted when this function
103  *   returns a nonzero value.
104  * @param ...
105  *   Variable parameter list passed as @p va_list to @p func.
106  *
107  * @return
108  *   0 when the entire list is traversed successfully, a negative error code
109  *   in case or failure, or the nonzero value returned by @p func when list
110  *   traversal is aborted.
111  */
112 static int
113 vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
114                                       const struct ether_addr *eth_addr,
115                                       va_list ap), ...)
116 {
117         struct if_nameindex *iface = if_nameindex();
118         int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
119         unsigned int i;
120         int ret = 0;
121
122         if (!iface) {
123                 ret = -ENOBUFS;
124                 DRV_LOG(ERR, "cannot retrieve system network interfaces");
125                 goto error;
126         }
127         if (s == -1) {
128                 ret = -errno;
129                 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
130                 goto error;
131         }
132         for (i = 0; iface[i].if_name; ++i) {
133                 struct ifreq req;
134                 struct ether_addr eth_addr;
135                 va_list ap;
136
137                 strncpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name));
138                 if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
139                         DRV_LOG(WARNING, "cannot retrieve information about"
140                                          " interface \"%s\": %s",
141                                          req.ifr_name, rte_strerror(errno));
142                         continue;
143                 }
144                 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
145                         DRV_LOG(DEBUG, "interface %s is non-ethernet device",
146                                 req.ifr_name);
147                         continue;
148                 }
149                 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
150                        RTE_DIM(eth_addr.addr_bytes));
151                 va_start(ap, func);
152                 ret = func(&iface[i], &eth_addr, ap);
153                 va_end(ap);
154                 if (ret)
155                         break;
156         }
157 error:
158         if (s != -1)
159                 close(s);
160         if (iface)
161                 if_freenameindex(iface);
162         return ret;
163 }
164
165 /**
166  * Determine if a network interface is NetVSC.
167  *
168  * @param[in] iface
169  *   Pointer to netdevice description structure (name and index).
170  *
171  * @return
172  *   A nonzero value when interface is detected as NetVSC. In case of error,
173  *   rte_errno is updated and 0 returned.
174  */
175 static int
176 vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
177 {
178         static const char temp[] = "/sys/class/net/%s/device/class_id";
179         char path[sizeof(temp) + IF_NAMESIZE];
180         FILE *f;
181         int ret;
182         int len = 0;
183
184         ret = snprintf(path, sizeof(path), temp, iface->if_name);
185         if (ret == -1 || (size_t)ret >= sizeof(path)) {
186                 rte_errno = ENOBUFS;
187                 return 0;
188         }
189         f = fopen(path, "r");
190         if (!f) {
191                 rte_errno = errno;
192                 return 0;
193         }
194         ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
195         if (ret == EOF)
196                 rte_errno = errno;
197         ret = len == (int)strlen(NETVSC_CLASS_ID);
198         fclose(f);
199         return ret;
200 }
201
202 /**
203  * Determine if a network interface has a route.
204  *
205  * @param[in] name
206  *   Network device name.
207  *
208  * @return
209  *   A nonzero value when interface has an route. In case of error,
210  *   rte_errno is updated and 0 returned.
211  */
212 static int
213 vdev_netvsc_has_route(const char *name)
214 {
215         FILE *fp;
216         int ret = 0;
217         char route[NETVSC_MAX_ROUTE_LINE_SIZE];
218         char *netdev;
219
220         fp = fopen("/proc/net/route", "r");
221         if (!fp) {
222                 rte_errno = errno;
223                 return 0;
224         }
225         while (fgets(route, NETVSC_MAX_ROUTE_LINE_SIZE, fp) != NULL) {
226                 netdev = strtok(route, "\t");
227                 if (strcmp(netdev, name) == 0) {
228                         ret = 1;
229                         break;
230                 }
231                 /* Move file pointer to the next line. */
232                 while (strchr(route, '\n') == NULL &&
233                        fgets(route, NETVSC_MAX_ROUTE_LINE_SIZE, fp) != NULL)
234                         ;
235         }
236         fclose(fp);
237         return ret;
238 }
239
240 /**
241  * Retrieve network interface data from sysfs symbolic link.
242  *
243  * @param[out] buf
244  *   Output data buffer.
245  * @param size
246  *   Output buffer size.
247  * @param[in] if_name
248  *   Netdevice name.
249  * @param[in] relpath
250  *   Symbolic link path relative to netdevice sysfs entry.
251  *
252  * @return
253  *   0 on success, a negative error code otherwise.
254  */
255 static int
256 vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
257                            const char *relpath)
258 {
259         int ret;
260
261         ret = snprintf(buf, size, "/sys/class/net/%s/%s", if_name, relpath);
262         if (ret == -1 || (size_t)ret >= size)
263                 return -ENOBUFS;
264         ret = readlink(buf, buf, size);
265         if (ret == -1)
266                 return -errno;
267         if ((size_t)ret >= size - 1)
268                 return -ENOBUFS;
269         buf[ret] = '\0';
270         return 0;
271 }
272
273 /**
274  * Probe a network interface to associate with vdev_netvsc context.
275  *
276  * This function determines if the network device matches the properties of
277  * the NetVSC interface associated with the vdev_netvsc context and
278  * communicates its bus address to the fail-safe PMD instance if so.
279  *
280  * It is normally used with vdev_netvsc_foreach_iface().
281  *
282  * @param[in] iface
283  *   Pointer to netdevice description structure (name and index).
284  * @param[in] eth_addr
285  *   MAC address associated with @p iface.
286  * @param ap
287  *   Variable arguments list comprising:
288  *
289  *   - struct vdev_netvsc_ctx *ctx:
290  *     Context to associate network interface with.
291  *
292  * @return
293  *   A nonzero value when interface matches, 0 otherwise or in case of
294  *   error.
295  */
296 static int
297 vdev_netvsc_device_probe(const struct if_nameindex *iface,
298                     const struct ether_addr *eth_addr,
299                     va_list ap)
300 {
301         struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
302         char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
303         const char *addr;
304         size_t len;
305         int ret;
306
307         /* Skip non-matching or unwanted NetVSC interfaces. */
308         if (ctx->if_index == iface->if_index) {
309                 if (!strcmp(ctx->if_name, iface->if_name))
310                         return 0;
311                 DRV_LOG(DEBUG,
312                         "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
313                         ctx->if_name, ctx->if_index, iface->if_name);
314                 strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
315                 return 0;
316         }
317         if (vdev_netvsc_iface_is_netvsc(iface))
318                 return 0;
319         if (!is_same_ether_addr(eth_addr, &ctx->if_addr))
320                 return 0;
321         /* Look for associated PCI device. */
322         ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
323                                          "device/subsystem");
324         if (ret)
325                 return 0;
326         addr = strrchr(buf, '/');
327         addr = addr ? addr + 1 : buf;
328         if (strcmp(addr, "pci"))
329                 return 0;
330         ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
331                                          "device");
332         if (ret)
333                 return 0;
334         addr = strrchr(buf, '/');
335         addr = addr ? addr + 1 : buf;
336         len = strlen(addr);
337         if (!len)
338                 return 0;
339         /* Send PCI device argument to fail-safe PMD instance. */
340         if (strcmp(addr, ctx->yield))
341                 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
342                         " interface \"%s\" (index %u)", addr, ctx->if_name,
343                         ctx->if_index);
344         memmove(buf, addr, len + 1);
345         addr = buf;
346         buf[len] = '\n';
347         ret = write(ctx->pipe[1], addr, len + 1);
348         buf[len] = '\0';
349         if (ret == -1) {
350                 if (errno == EINTR || errno == EAGAIN)
351                         return 1;
352                 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
353                         " interface \"%s\": %s", addr, ctx->if_name,
354                         rte_strerror(errno));
355                 return 1;
356         }
357         if ((size_t)ret != len + 1) {
358                 /*
359                  * Attempt to override previous partial write, no need to
360                  * recover if that fails.
361                  */
362                 ret = write(ctx->pipe[1], "\n", 1);
363                 (void)ret;
364                 return 1;
365         }
366         fsync(ctx->pipe[1]);
367         memcpy(ctx->yield, addr, len + 1);
368         return 1;
369 }
370
371 /**
372  * Alarm callback that regularly probes system network interfaces.
373  *
374  * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
375  * long as an vdev_netvsc context instance exists.
376  *
377  * @param arg
378  *   Ignored.
379  */
380 static void
381 vdev_netvsc_alarm(__rte_unused void *arg)
382 {
383         struct vdev_netvsc_ctx *ctx;
384         int ret;
385
386         LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
387                 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
388                 if (ret)
389                         break;
390         }
391         if (!vdev_netvsc_ctx_count)
392                 return;
393         ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
394                                 vdev_netvsc_alarm, NULL);
395         if (ret < 0) {
396                 DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
397                         rte_strerror(-ret));
398         }
399 }
400
401 /**
402  * Probe a NetVSC interface to generate a vdev_netvsc context from.
403  *
404  * This function instantiates vdev_netvsc contexts either for all NetVSC
405  * devices found on the system or only a subset provided as device
406  * arguments.
407  *
408  * It is normally used with vdev_netvsc_foreach_iface().
409  *
410  * @param[in] iface
411  *   Pointer to netdevice description structure (name and index).
412  * @param[in] eth_addr
413  *   MAC address associated with @p iface.
414  * @param ap
415  *   Variable arguments list comprising:
416  *
417  *   - const char *name:
418  *     Name associated with current driver instance.
419  *
420  *   - struct rte_kvargs *kvargs:
421  *     Device arguments provided to current driver instance.
422  *
423  *   - int force:
424  *     Accept specified interface even if not detected as NetVSC.
425  *
426  *   - unsigned int specified:
427  *     Number of specific netdevices provided as device arguments.
428  *
429  *   - unsigned int *matched:
430  *     The number of specified netdevices matched by this function.
431  *
432  * @return
433  *   A nonzero value when interface matches, 0 otherwise or in case of
434  *   error.
435  */
436 static int
437 vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
438                          const struct ether_addr *eth_addr,
439                          va_list ap)
440 {
441         const char *name = va_arg(ap, const char *);
442         struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
443         int force = va_arg(ap, int);
444         unsigned int specified = va_arg(ap, unsigned int);
445         unsigned int *matched = va_arg(ap, unsigned int *);
446         unsigned int i;
447         struct vdev_netvsc_ctx *ctx;
448         int ret;
449
450         /* Probe all interfaces when none are specified. */
451         if (specified) {
452                 for (i = 0; i != kvargs->count; ++i) {
453                         const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
454
455                         if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
456                                 if (!strcmp(pair->value, iface->if_name))
457                                         break;
458                         } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
459                                 struct ether_addr tmp;
460
461                                 if (sscanf(pair->value,
462                                            "%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":"
463                                            "%" SCNx8 ":%" SCNx8 ":%" SCNx8,
464                                            &tmp.addr_bytes[0],
465                                            &tmp.addr_bytes[1],
466                                            &tmp.addr_bytes[2],
467                                            &tmp.addr_bytes[3],
468                                            &tmp.addr_bytes[4],
469                                            &tmp.addr_bytes[5]) != 6) {
470                                         DRV_LOG(ERR,
471                                                 "invalid MAC address format"
472                                                 " \"%s\"",
473                                                 pair->value);
474                                         return -EINVAL;
475                                 }
476                                 if (is_same_ether_addr(eth_addr, &tmp))
477                                         break;
478                         }
479                 }
480                 if (i == kvargs->count)
481                         return 0;
482                 ++(*matched);
483         }
484         /* Weed out interfaces already handled. */
485         LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
486                 if (ctx->if_index == iface->if_index)
487                         break;
488         if (ctx) {
489                 if (!specified)
490                         return 0;
491                 DRV_LOG(WARNING,
492                         "interface \"%s\" (index %u) is already handled,"
493                         " skipping",
494                         iface->if_name, iface->if_index);
495                 return 0;
496         }
497         if (!vdev_netvsc_iface_is_netvsc(iface)) {
498                 if (!specified || !force)
499                         return 0;
500                 DRV_LOG(WARNING,
501                         "using non-NetVSC interface \"%s\" (index %u)",
502                         iface->if_name, iface->if_index);
503         }
504         /* Routed NetVSC should not be probed. */
505         if (vdev_netvsc_has_route(iface->if_name)) {
506                 if (!specified || !force)
507                         return 0;
508                 DRV_LOG(WARNING, "using routed NetVSC interface \"%s\""
509                         " (index %u)", iface->if_name, iface->if_index);
510         }
511         /* Create interface context. */
512         ctx = calloc(1, sizeof(*ctx));
513         if (!ctx) {
514                 ret = -errno;
515                 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
516                         iface->if_name, rte_strerror(errno));
517                 goto error;
518         }
519         ctx->id = vdev_netvsc_ctx_count;
520         strncpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
521         ctx->if_index = iface->if_index;
522         ctx->if_addr = *eth_addr;
523         ctx->pipe[0] = -1;
524         ctx->pipe[1] = -1;
525         ctx->yield[0] = '\0';
526         if (pipe(ctx->pipe) == -1) {
527                 ret = -errno;
528                 DRV_LOG(ERR,
529                         "cannot allocate control pipe for interface \"%s\": %s",
530                         ctx->if_name, rte_strerror(errno));
531                 goto error;
532         }
533         for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
534                 int flf = fcntl(ctx->pipe[i], F_GETFL);
535
536                 if (flf != -1 &&
537                     fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
538                         continue;
539                 ret = -errno;
540                 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
541                         " descriptor #%u (%d): %s", i, ctx->pipe[i],
542                         rte_strerror(errno));
543                 goto error;
544         }
545         /* Generate virtual device name and arguments. */
546         i = 0;
547         ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
548                        name, ctx->id);
549         if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
550                 ++i;
551         ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_%s",
552                        ctx->name);
553         if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
554                 ++i;
555         ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
556                        "fd(%d),dev(net_tap_%s,remote=%s)",
557                        ctx->pipe[0], ctx->name, ctx->if_name);
558         if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
559                 ++i;
560         if (i) {
561                 ret = -ENOBUFS;
562                 DRV_LOG(ERR, "generated virtual device name or argument list"
563                         " too long for interface \"%s\"", ctx->if_name);
564                 goto error;
565         }
566         /* Request virtual device generation. */
567         DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
568                 ctx->devname, ctx->devargs);
569         vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, ctx);
570         ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
571         if (ret)
572                 goto error;
573         LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
574         ++vdev_netvsc_ctx_count;
575         DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
576                 ctx->if_name);
577         return 0;
578 error:
579         if (ctx)
580                 vdev_netvsc_ctx_destroy(ctx);
581         return ret;
582 }
583
584 /**
585  * Probe NetVSC interfaces.
586  *
587  * This function probes system netdevices according to the specified device
588  * arguments and starts a periodic alarm callback to notify the resulting
589  * fail-safe PMD instances of their sub-devices whereabouts.
590  *
591  * @param dev
592  *   Virtual device context for driver instance.
593  *
594  * @return
595  *    Always 0, even in case of errors.
596  */
597 static int
598 vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
599 {
600         static const char *const vdev_netvsc_arg[] = {
601                 VDEV_NETVSC_ARG_IFACE,
602                 VDEV_NETVSC_ARG_MAC,
603                 VDEV_NETVSC_ARG_FORCE,
604                 NULL,
605         };
606         const char *name = rte_vdev_device_name(dev);
607         const char *args = rte_vdev_device_args(dev);
608         struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
609                                                      vdev_netvsc_arg);
610         unsigned int specified = 0;
611         unsigned int matched = 0;
612         int force = 0;
613         unsigned int i;
614         int ret;
615
616         DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
617         if (!kvargs) {
618                 DRV_LOG(ERR, "cannot parse arguments list");
619                 goto error;
620         }
621         for (i = 0; i != kvargs->count; ++i) {
622                 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
623
624                 if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE))
625                         force = !!atoi(pair->value);
626                 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
627                          !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
628                         ++specified;
629         }
630         rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
631         /* Gather interfaces. */
632         ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, name, kvargs,
633                                         force, specified, &matched);
634         if (ret < 0)
635                 goto error;
636         if (matched < specified)
637                 DRV_LOG(WARNING,
638                         "some of the specified parameters did not match"
639                         " recognized network interfaces");
640         ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
641                                 vdev_netvsc_alarm, NULL);
642         if (ret < 0) {
643                 DRV_LOG(ERR, "unable to schedule alarm callback: %s",
644                         rte_strerror(-ret));
645                 goto error;
646         }
647 error:
648         if (kvargs)
649                 rte_kvargs_free(kvargs);
650         ++vdev_netvsc_ctx_inst;
651         return 0;
652 }
653
654 /**
655  * Remove driver instance.
656  *
657  * The alarm callback and underlying vdev_netvsc context instances are only
658  * destroyed after the last PMD instance is removed.
659  *
660  * @param dev
661  *   Virtual device context for driver instance.
662  *
663  * @return
664  *   Always 0.
665  */
666 static int
667 vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
668 {
669         if (--vdev_netvsc_ctx_inst)
670                 return 0;
671         rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
672         while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
673                 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
674
675                 LIST_REMOVE(ctx, entry);
676                 --vdev_netvsc_ctx_count;
677                 vdev_netvsc_ctx_destroy(ctx);
678         }
679         return 0;
680 }
681
682 /** Virtual device descriptor. */
683 static struct rte_vdev_driver vdev_netvsc_vdev = {
684         .probe = vdev_netvsc_vdev_probe,
685         .remove = vdev_netvsc_vdev_remove,
686 };
687
688 RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
689 RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
690 RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
691                               VDEV_NETVSC_ARG_IFACE "=<string> "
692                               VDEV_NETVSC_ARG_MAC "=<string> "
693                               VDEV_NETVSC_ARG_FORCE "=<int>");
694
695 /** Initialize driver log type. */
696 RTE_INIT(vdev_netvsc_init_log)
697 {
698         vdev_netvsc_logtype = rte_log_register("pmd.vdev_netvsc");
699         if (vdev_netvsc_logtype >= 0)
700                 rte_log_set_level(vdev_netvsc_logtype, RTE_LOG_NOTICE);
701 }