54122fd13c66b4d92d48a18bcd2fb4ae7b2fc402
[dpdk.git] / drivers / net / tap / rte_eth_tap.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_atomic.h>
35 #include <rte_branch_prediction.h>
36 #include <rte_common.h>
37 #include <rte_mbuf.h>
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
40 #include <rte_vdev.h>
41 #include <rte_kvargs.h>
42 #include <rte_net.h>
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/utsname.h>
49 #include <sys/mman.h>
50 #include <errno.h>
51 #include <signal.h>
52 #include <stdint.h>
53 #include <sys/uio.h>
54 #include <unistd.h>
55 #include <arpa/inet.h>
56 #include <net/if.h>
57 #include <linux/if_tun.h>
58 #include <linux/if_ether.h>
59 #include <linux/version.h>
60 #include <fcntl.h>
61
62 #include <rte_eth_tap.h>
63 #include <tap_flow.h>
64 #include <tap_tcmsgs.h>
65
66 /* Linux based path to the TUN device */
67 #define TUN_TAP_DEV_PATH        "/dev/net/tun"
68 #define DEFAULT_TAP_NAME        "dtap"
69
70 #define ETH_TAP_IFACE_ARG       "iface"
71 #define ETH_TAP_SPEED_ARG       "speed"
72 #define ETH_TAP_REMOTE_ARG      "remote"
73
74 #ifdef IFF_MULTI_QUEUE
75 #define RTE_PMD_TAP_MAX_QUEUES  16
76 #else
77 #define RTE_PMD_TAP_MAX_QUEUES  1
78 #endif
79
80 #define FLOWER_KERNEL_VERSION KERNEL_VERSION(4, 2, 0)
81 #define FLOWER_VLAN_KERNEL_VERSION KERNEL_VERSION(4, 9, 0)
82
83 static struct rte_vdev_driver pmd_tap_drv;
84
85 static const char *valid_arguments[] = {
86         ETH_TAP_IFACE_ARG,
87         ETH_TAP_SPEED_ARG,
88         ETH_TAP_REMOTE_ARG,
89         NULL
90 };
91
92 static int tap_unit;
93
94 static volatile uint32_t tap_trigger;   /* Rx trigger */
95
96 static struct rte_eth_link pmd_link = {
97         .link_speed = ETH_SPEED_NUM_10G,
98         .link_duplex = ETH_LINK_FULL_DUPLEX,
99         .link_status = ETH_LINK_DOWN,
100         .link_autoneg = ETH_LINK_SPEED_AUTONEG
101 };
102
103 static void
104 tap_trigger_cb(int sig __rte_unused)
105 {
106         /* Valid trigger values are nonzero */
107         tap_trigger = (tap_trigger + 1) | 0x80000000;
108 }
109
110 static int
111 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
112           struct ifreq *ifr, int set);
113
114 /* Tun/Tap allocation routine
115  *
116  * name is the number of the interface to use, unless NULL to take the host
117  * supplied name.
118  */
119 static int
120 tun_alloc(struct pmd_internals *pmd, uint16_t qid)
121 {
122         struct ifreq ifr;
123 #ifdef IFF_MULTI_QUEUE
124         unsigned int features;
125 #endif
126         int fd;
127
128         memset(&ifr, 0, sizeof(struct ifreq));
129
130         /*
131          * Do not set IFF_NO_PI as packet information header will be needed
132          * to check if a received packet has been truncated.
133          */
134         ifr.ifr_flags = IFF_TAP;
135         snprintf(ifr.ifr_name, IFNAMSIZ, "%s", pmd->name);
136
137         RTE_LOG(DEBUG, PMD, "ifr_name '%s'\n", ifr.ifr_name);
138
139         fd = open(TUN_TAP_DEV_PATH, O_RDWR);
140         if (fd < 0) {
141                 RTE_LOG(ERR, PMD, "Unable to create TAP interface");
142                 goto error;
143         }
144
145 #ifdef IFF_MULTI_QUEUE
146         /* Grab the TUN features to verify we can work multi-queue */
147         if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
148                 RTE_LOG(ERR, PMD, "TAP unable to get TUN/TAP features\n");
149                 goto error;
150         }
151         RTE_LOG(DEBUG, PMD, "  TAP Features %08x\n", features);
152
153         if (features & IFF_MULTI_QUEUE) {
154                 RTE_LOG(DEBUG, PMD, "  Multi-queue support for %d queues\n",
155                         RTE_PMD_TAP_MAX_QUEUES);
156                 ifr.ifr_flags |= IFF_MULTI_QUEUE;
157         } else
158 #endif
159         {
160                 ifr.ifr_flags |= IFF_ONE_QUEUE;
161                 RTE_LOG(DEBUG, PMD, "  Single queue only support\n");
162         }
163
164         /* Set the TUN/TAP configuration and set the name if needed */
165         if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
166                 RTE_LOG(WARNING, PMD,
167                         "Unable to set TUNSETIFF for %s\n",
168                         ifr.ifr_name);
169                 perror("TUNSETIFF");
170                 goto error;
171         }
172
173         /* Always set the file descriptor to non-blocking */
174         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
175                 RTE_LOG(WARNING, PMD,
176                         "Unable to set %s to nonblocking\n",
177                         ifr.ifr_name);
178                 perror("F_SETFL, NONBLOCK");
179                 goto error;
180         }
181
182         /* Set up trigger to optimize empty Rx bursts */
183         errno = 0;
184         do {
185                 struct sigaction sa;
186                 int flags = fcntl(fd, F_GETFL);
187
188                 if (flags == -1 || sigaction(SIGIO, NULL, &sa) == -1)
189                         break;
190                 if (sa.sa_handler != tap_trigger_cb) {
191                         /*
192                          * Make sure SIGIO is not already taken. This is done
193                          * as late as possible to leave the application a
194                          * chance to set up its own signal handler first.
195                          */
196                         if (sa.sa_handler != SIG_IGN &&
197                             sa.sa_handler != SIG_DFL) {
198                                 errno = EBUSY;
199                                 break;
200                         }
201                         sa = (struct sigaction){
202                                 .sa_flags = SA_RESTART,
203                                 .sa_handler = tap_trigger_cb,
204                         };
205                         if (sigaction(SIGIO, &sa, NULL) == -1)
206                                 break;
207                 }
208                 /* Enable SIGIO on file descriptor */
209                 fcntl(fd, F_SETFL, flags | O_ASYNC);
210                 fcntl(fd, F_SETOWN, getpid());
211         } while (0);
212         if (errno) {
213                 /* Disable trigger globally in case of error */
214                 tap_trigger = 0;
215                 RTE_LOG(WARNING, PMD, "Rx trigger disabled: %s\n",
216                         strerror(errno));
217         }
218
219         if (qid == 0) {
220                 struct ifreq ifr;
221
222                 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0) < 0)
223                         goto error;
224                 rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
225                            ETHER_ADDR_LEN);
226
227                 pmd->if_index = if_nametoindex(pmd->name);
228                 if (!pmd->if_index) {
229                         RTE_LOG(ERR, PMD,
230                                 "Could not find ifindex for %s: rte_flow won't be usable.\n",
231                                 pmd->name);
232                         return fd;
233                 }
234                 if (!pmd->flower_support)
235                         return fd;
236                 if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
237                         RTE_LOG(ERR, PMD,
238                                 "Could not create multiq qdisc for %s: rte_flow won't be usable.\n",
239                                 pmd->name);
240                         return fd;
241                 }
242                 if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
243                         RTE_LOG(ERR, PMD,
244                                 "Could not create multiq qdisc for %s: rte_flow won't be usable.\n",
245                                 pmd->name);
246                         return fd;
247                 }
248                 if (pmd->remote_if_index) {
249                         /*
250                          * Flush usually returns negative value because it tries
251                          * to delete every QDISC (and on a running device, one
252                          * QDISC at least is needed). Ignore negative return
253                          * value.
254                          */
255                         qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
256                         if (qdisc_create_ingress(pmd->nlsk_fd,
257                                                  pmd->remote_if_index) < 0)
258                                 goto remote_fail;
259                         LIST_INIT(&pmd->implicit_flows);
260                         if (tap_flow_implicit_create(
261                                     pmd, TAP_REMOTE_LOCAL_MAC) < 0)
262                                 goto remote_fail;
263                         if (tap_flow_implicit_create(
264                                     pmd, TAP_REMOTE_BROADCAST) < 0)
265                                 goto remote_fail;
266                         if (tap_flow_implicit_create(
267                                     pmd, TAP_REMOTE_BROADCASTV6) < 0)
268                                 goto remote_fail;
269                         if (tap_flow_implicit_create(
270                                     pmd, TAP_REMOTE_TX) < 0)
271                                 goto remote_fail;
272                 }
273         }
274
275         return fd;
276
277 remote_fail:
278         RTE_LOG(ERR, PMD,
279                 "Could not set up remote flow rules for %s: remote disabled.\n",
280                 pmd->name);
281         pmd->remote_if_index = 0;
282         tap_flow_implicit_flush(pmd, NULL);
283         return fd;
284
285 error:
286         if (fd > 0)
287                 close(fd);
288         return -1;
289 }
290
291 /* Callback to handle the rx burst of packets to the correct interface and
292  * file descriptor(s) in a multi-queue setup.
293  */
294 static uint16_t
295 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
296 {
297         struct rx_queue *rxq = queue;
298         uint16_t num_rx;
299         unsigned long num_rx_bytes = 0;
300         uint32_t trigger = tap_trigger;
301
302         if (trigger == rxq->trigger_seen)
303                 return 0;
304         if (trigger)
305                 rxq->trigger_seen = trigger;
306         rte_compiler_barrier();
307         for (num_rx = 0; num_rx < nb_pkts; ) {
308                 struct rte_mbuf *mbuf = rxq->pool;
309                 struct rte_mbuf *seg = NULL;
310                 struct rte_mbuf *new_tail = NULL;
311                 uint16_t data_off = rte_pktmbuf_headroom(mbuf);
312                 int len;
313
314                 len = readv(rxq->fd, *rxq->iovecs,
315                             1 + (rxq->rxmode->enable_scatter ?
316                                  rxq->nb_rx_desc : 1));
317                 if (len < (int)sizeof(struct tun_pi))
318                         break;
319
320                 /* Packet couldn't fit in the provided mbuf */
321                 if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) {
322                         rxq->stats.ierrors++;
323                         continue;
324                 }
325
326                 len -= sizeof(struct tun_pi);
327
328                 mbuf->pkt_len = len;
329                 mbuf->port = rxq->in_port;
330                 while (1) {
331                         struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
332
333                         if (unlikely(!buf)) {
334                                 rxq->stats.rx_nombuf++;
335                                 /* No new buf has been allocated: do nothing */
336                                 if (!new_tail || !seg)
337                                         goto end;
338
339                                 seg->next = NULL;
340                                 rte_pktmbuf_free(mbuf);
341
342                                 goto end;
343                         }
344                         seg = seg ? seg->next : mbuf;
345                         if (rxq->pool == mbuf)
346                                 rxq->pool = buf;
347                         if (new_tail)
348                                 new_tail->next = buf;
349                         new_tail = buf;
350                         new_tail->next = seg->next;
351
352                         /* iovecs[0] is reserved for packet info (pi) */
353                         (*rxq->iovecs)[mbuf->nb_segs].iov_len =
354                                 buf->buf_len - data_off;
355                         (*rxq->iovecs)[mbuf->nb_segs].iov_base =
356                                 (char *)buf->buf_addr + data_off;
357
358                         seg->data_len = RTE_MIN(seg->buf_len - data_off, len);
359                         seg->data_off = data_off;
360
361                         len -= seg->data_len;
362                         if (len <= 0)
363                                 break;
364                         mbuf->nb_segs++;
365                         /* First segment has headroom, not the others */
366                         data_off = 0;
367                 }
368                 seg->next = NULL;
369                 mbuf->packet_type = rte_net_get_ptype(mbuf, NULL,
370                                                       RTE_PTYPE_ALL_MASK);
371
372                 /* account for the receive frame */
373                 bufs[num_rx++] = mbuf;
374                 num_rx_bytes += mbuf->pkt_len;
375         }
376 end:
377         rxq->stats.ipackets += num_rx;
378         rxq->stats.ibytes += num_rx_bytes;
379
380         return num_rx;
381 }
382
383 /* Callback to handle sending packets from the tap interface
384  */
385 static uint16_t
386 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
387 {
388         struct tx_queue *txq = queue;
389         uint16_t num_tx = 0;
390         unsigned long num_tx_bytes = 0;
391         uint32_t max_size;
392         int i;
393
394         if (unlikely(nb_pkts == 0))
395                 return 0;
396
397         max_size = *txq->mtu + (ETHER_HDR_LEN + ETHER_CRC_LEN + 4);
398         for (i = 0; i < nb_pkts; i++) {
399                 struct rte_mbuf *mbuf = bufs[num_tx];
400                 struct iovec iovecs[mbuf->nb_segs + 1];
401                 struct tun_pi pi = { .flags = 0 };
402                 struct rte_mbuf *seg = mbuf;
403                 int n;
404                 int j;
405
406                 /* stats.errs will be incremented */
407                 if (rte_pktmbuf_pkt_len(mbuf) > max_size)
408                         break;
409
410                 iovecs[0].iov_base = &pi;
411                 iovecs[0].iov_len = sizeof(pi);
412                 for (j = 1; j <= mbuf->nb_segs; j++) {
413                         iovecs[j].iov_len = rte_pktmbuf_data_len(seg);
414                         iovecs[j].iov_base =
415                                 rte_pktmbuf_mtod(seg, void *);
416                         seg = seg->next;
417                 }
418                 /* copy the tx frame data */
419                 n = writev(txq->fd, iovecs, mbuf->nb_segs + 1);
420                 if (n <= 0)
421                         break;
422
423                 num_tx++;
424                 num_tx_bytes += mbuf->pkt_len;
425                 rte_pktmbuf_free(mbuf);
426         }
427
428         txq->stats.opackets += num_tx;
429         txq->stats.errs += nb_pkts - num_tx;
430         txq->stats.obytes += num_tx_bytes;
431
432         return num_tx;
433 }
434
435 static int
436 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
437           struct ifreq *ifr, int set)
438 {
439         short req_flags = ifr->ifr_flags;
440         int remote = !!pmd->remote_if_index;
441
442         /*
443          * If there is a remote netdevice, apply ioctl on it, then apply it on
444          * the tap netdevice.
445          */
446         if (request == SIOCGIFFLAGS && !set) {
447                 /*
448                  * Special case for getting flags. If set is given,
449                  * then return the flags from the remote netdevice only.
450                  * Otherwise return the flags from the tap netdevice.
451                  */
452                 remote = 0;
453         }
454 apply:
455         if (remote)
456                 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->remote_iface);
457         else
458                 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->name);
459         switch (request) {
460         case SIOCSIFFLAGS:
461                 /* fetch current flags to leave other flags untouched */
462                 if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
463                         goto error;
464                 if (set)
465                         ifr->ifr_flags |= req_flags;
466                 else
467                         ifr->ifr_flags &= ~req_flags;
468                 break;
469         case SIOCGIFFLAGS:
470                 if (remote && set)
471                         remote = 0; /* don't loop */
472                 break;
473         case SIOCGIFHWADDR:
474                 /* Set remote MAC on the tap netdevice */
475                 if (!remote && pmd->remote_if_index) {
476                         request = SIOCSIFHWADDR;
477                         goto apply;
478                 }
479                 break;
480         case SIOCSIFHWADDR:
481         case SIOCSIFMTU:
482                 break;
483         default:
484                 RTE_LOG(WARNING, PMD, "%s: ioctl() called with wrong arg\n",
485                         pmd->name);
486                 return -EINVAL;
487         }
488         if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
489                 goto error;
490         if (remote--)
491                 goto apply;
492         return 0;
493
494 error:
495         RTE_LOG(ERR, PMD, "%s: ioctl(%lu) failed with error: %s\n",
496                 ifr->ifr_name, request, strerror(errno));
497         return -errno;
498 }
499
500 static int
501 tap_link_set_down(struct rte_eth_dev *dev)
502 {
503         struct pmd_internals *pmd = dev->data->dev_private;
504         struct ifreq ifr = { .ifr_flags = IFF_UP };
505
506         dev->data->dev_link.link_status = ETH_LINK_DOWN;
507         return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
508 }
509
510 static int
511 tap_link_set_up(struct rte_eth_dev *dev)
512 {
513         struct pmd_internals *pmd = dev->data->dev_private;
514         struct ifreq ifr = { .ifr_flags = IFF_UP };
515
516         dev->data->dev_link.link_status = ETH_LINK_UP;
517         return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
518 }
519
520 static int
521 tap_dev_start(struct rte_eth_dev *dev)
522 {
523         return tap_link_set_up(dev);
524 }
525
526 /* This function gets called when the current port gets stopped.
527  */
528 static void
529 tap_dev_stop(struct rte_eth_dev *dev)
530 {
531         tap_link_set_down(dev);
532 }
533
534 static int
535 tap_dev_configure(struct rte_eth_dev *dev __rte_unused)
536 {
537         return 0;
538 }
539
540 static uint32_t
541 tap_dev_speed_capa(void)
542 {
543         uint32_t speed = pmd_link.link_speed;
544         uint32_t capa = 0;
545
546         if (speed >= ETH_SPEED_NUM_10M)
547                 capa |= ETH_LINK_SPEED_10M;
548         if (speed >= ETH_SPEED_NUM_100M)
549                 capa |= ETH_LINK_SPEED_100M;
550         if (speed >= ETH_SPEED_NUM_1G)
551                 capa |= ETH_LINK_SPEED_1G;
552         if (speed >= ETH_SPEED_NUM_5G)
553                 capa |= ETH_LINK_SPEED_2_5G;
554         if (speed >= ETH_SPEED_NUM_5G)
555                 capa |= ETH_LINK_SPEED_5G;
556         if (speed >= ETH_SPEED_NUM_10G)
557                 capa |= ETH_LINK_SPEED_10G;
558         if (speed >= ETH_SPEED_NUM_20G)
559                 capa |= ETH_LINK_SPEED_20G;
560         if (speed >= ETH_SPEED_NUM_25G)
561                 capa |= ETH_LINK_SPEED_25G;
562         if (speed >= ETH_SPEED_NUM_40G)
563                 capa |= ETH_LINK_SPEED_40G;
564         if (speed >= ETH_SPEED_NUM_50G)
565                 capa |= ETH_LINK_SPEED_50G;
566         if (speed >= ETH_SPEED_NUM_56G)
567                 capa |= ETH_LINK_SPEED_56G;
568         if (speed >= ETH_SPEED_NUM_100G)
569                 capa |= ETH_LINK_SPEED_100G;
570
571         return capa;
572 }
573
574 static void
575 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
576 {
577         struct pmd_internals *internals = dev->data->dev_private;
578
579         dev_info->if_index = internals->if_index;
580         dev_info->max_mac_addrs = 1;
581         dev_info->max_rx_pktlen = (uint32_t)ETHER_MAX_VLAN_FRAME_LEN;
582         dev_info->max_rx_queues = internals->nb_queues;
583         dev_info->max_tx_queues = internals->nb_queues;
584         dev_info->min_rx_bufsize = 0;
585         dev_info->pci_dev = NULL;
586         dev_info->speed_capa = tap_dev_speed_capa();
587 }
588
589 static void
590 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
591 {
592         unsigned int i, imax;
593         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
594         unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
595         unsigned long rx_nombuf = 0, ierrors = 0;
596         const struct pmd_internals *pmd = dev->data->dev_private;
597
598         imax = (pmd->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
599                 pmd->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
600
601         for (i = 0; i < imax; i++) {
602                 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
603                 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
604                 rx_total += tap_stats->q_ipackets[i];
605                 rx_bytes_total += tap_stats->q_ibytes[i];
606                 rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
607                 ierrors += pmd->rxq[i].stats.ierrors;
608
609                 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
610                 tap_stats->q_errors[i] = pmd->txq[i].stats.errs;
611                 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
612                 tx_total += tap_stats->q_opackets[i];
613                 tx_err_total += tap_stats->q_errors[i];
614                 tx_bytes_total += tap_stats->q_obytes[i];
615         }
616
617         tap_stats->ipackets = rx_total;
618         tap_stats->ibytes = rx_bytes_total;
619         tap_stats->ierrors = ierrors;
620         tap_stats->rx_nombuf = rx_nombuf;
621         tap_stats->opackets = tx_total;
622         tap_stats->oerrors = tx_err_total;
623         tap_stats->obytes = tx_bytes_total;
624 }
625
626 static void
627 tap_stats_reset(struct rte_eth_dev *dev)
628 {
629         int i;
630         struct pmd_internals *pmd = dev->data->dev_private;
631
632         for (i = 0; i < pmd->nb_queues; i++) {
633                 pmd->rxq[i].stats.ipackets = 0;
634                 pmd->rxq[i].stats.ibytes = 0;
635                 pmd->rxq[i].stats.ierrors = 0;
636                 pmd->rxq[i].stats.rx_nombuf = 0;
637
638                 pmd->txq[i].stats.opackets = 0;
639                 pmd->txq[i].stats.errs = 0;
640                 pmd->txq[i].stats.obytes = 0;
641         }
642 }
643
644 static void
645 tap_dev_close(struct rte_eth_dev *dev __rte_unused)
646 {
647         int i;
648         struct pmd_internals *internals = dev->data->dev_private;
649
650         tap_link_set_down(dev);
651         tap_flow_flush(dev, NULL);
652         tap_flow_implicit_flush(internals, NULL);
653
654         for (i = 0; i < internals->nb_queues; i++) {
655                 if (internals->rxq[i].fd != -1)
656                         close(internals->rxq[i].fd);
657                 internals->rxq[i].fd = -1;
658                 internals->txq[i].fd = -1;
659         }
660 }
661
662 static void
663 tap_rx_queue_release(void *queue)
664 {
665         struct rx_queue *rxq = queue;
666
667         if (rxq && (rxq->fd > 0)) {
668                 close(rxq->fd);
669                 rxq->fd = -1;
670                 rte_pktmbuf_free(rxq->pool);
671                 rte_free(rxq->iovecs);
672                 rxq->pool = NULL;
673                 rxq->iovecs = NULL;
674         }
675 }
676
677 static void
678 tap_tx_queue_release(void *queue)
679 {
680         struct tx_queue *txq = queue;
681
682         if (txq && (txq->fd > 0)) {
683                 close(txq->fd);
684                 txq->fd = -1;
685         }
686 }
687
688 static int
689 tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
690 {
691         struct rte_eth_link *dev_link = &dev->data->dev_link;
692         struct pmd_internals *pmd = dev->data->dev_private;
693         struct ifreq ifr = { .ifr_flags = 0 };
694
695         if (pmd->remote_if_index) {
696                 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 1);
697                 if (!(ifr.ifr_flags & IFF_UP) ||
698                     !(ifr.ifr_flags & IFF_RUNNING)) {
699                         dev_link->link_status = ETH_LINK_DOWN;
700                         return 0;
701                 }
702         }
703         tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0);
704         dev_link->link_status =
705                 ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
706                  ETH_LINK_UP :
707                  ETH_LINK_DOWN);
708         return 0;
709 }
710
711 static void
712 tap_promisc_enable(struct rte_eth_dev *dev)
713 {
714         struct pmd_internals *pmd = dev->data->dev_private;
715         struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
716
717         dev->data->promiscuous = 1;
718         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
719         if (pmd->remote_if_index)
720                 tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
721 }
722
723 static void
724 tap_promisc_disable(struct rte_eth_dev *dev)
725 {
726         struct pmd_internals *pmd = dev->data->dev_private;
727         struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
728
729         dev->data->promiscuous = 0;
730         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
731         if (pmd->remote_if_index)
732                 tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
733 }
734
735 static void
736 tap_allmulti_enable(struct rte_eth_dev *dev)
737 {
738         struct pmd_internals *pmd = dev->data->dev_private;
739         struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
740
741         dev->data->all_multicast = 1;
742         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
743         if (pmd->remote_if_index)
744                 tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
745 }
746
747 static void
748 tap_allmulti_disable(struct rte_eth_dev *dev)
749 {
750         struct pmd_internals *pmd = dev->data->dev_private;
751         struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
752
753         dev->data->all_multicast = 0;
754         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
755         if (pmd->remote_if_index)
756                 tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
757 }
758
759
760 static void
761 tap_mac_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
762 {
763         struct pmd_internals *pmd = dev->data->dev_private;
764         struct ifreq ifr;
765
766         if (is_zero_ether_addr(mac_addr)) {
767                 RTE_LOG(ERR, PMD, "%s: can't set an empty MAC address\n",
768                         dev->data->name);
769                 return;
770         }
771
772         ifr.ifr_hwaddr.sa_family = AF_LOCAL;
773         rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETHER_ADDR_LEN);
774         if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1) < 0)
775                 return;
776         rte_memcpy(&pmd->eth_addr, mac_addr, ETHER_ADDR_LEN);
777 }
778
779 static int
780 tap_setup_queue(struct rte_eth_dev *dev,
781                 struct pmd_internals *internals,
782                 uint16_t qid)
783 {
784         struct pmd_internals *pmd = dev->data->dev_private;
785         struct rx_queue *rx = &internals->rxq[qid];
786         struct tx_queue *tx = &internals->txq[qid];
787         int fd;
788
789         fd = rx->fd;
790         if (fd < 0) {
791                 fd = tx->fd;
792                 if (fd < 0) {
793                         RTE_LOG(INFO, PMD, "Add queue to TAP %s for qid %d\n",
794                                 pmd->name, qid);
795                         fd = tun_alloc(pmd, qid);
796                         if (fd < 0) {
797                                 RTE_LOG(ERR, PMD, "tun_alloc(%s, %d) failed\n",
798                                         pmd->name, qid);
799                                 return -1;
800                         }
801                         if (qid == 0) {
802                                 struct ifreq ifr;
803
804                                 ifr.ifr_mtu = dev->data->mtu;
805                                 if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1) < 0) {
806                                         close(fd);
807                                         return -1;
808                                 }
809                         }
810                 }
811         }
812
813         rx->fd = fd;
814         tx->fd = fd;
815         tx->mtu = &dev->data->mtu;
816         rx->rxmode = &dev->data->dev_conf.rxmode;
817
818         return fd;
819 }
820
821 static int
822 rx_setup_queue(struct rte_eth_dev *dev,
823                 struct pmd_internals *internals,
824                 uint16_t qid)
825 {
826         dev->data->rx_queues[qid] = &internals->rxq[qid];
827
828         return tap_setup_queue(dev, internals, qid);
829 }
830
831 static int
832 tx_setup_queue(struct rte_eth_dev *dev,
833                 struct pmd_internals *internals,
834                 uint16_t qid)
835 {
836         dev->data->tx_queues[qid] = &internals->txq[qid];
837
838         return tap_setup_queue(dev, internals, qid);
839 }
840
841 static int
842 tap_rx_queue_setup(struct rte_eth_dev *dev,
843                    uint16_t rx_queue_id,
844                    uint16_t nb_rx_desc,
845                    unsigned int socket_id,
846                    const struct rte_eth_rxconf *rx_conf __rte_unused,
847                    struct rte_mempool *mp)
848 {
849         struct pmd_internals *internals = dev->data->dev_private;
850         struct rx_queue *rxq = &internals->rxq[rx_queue_id];
851         struct rte_mbuf **tmp = &rxq->pool;
852         struct iovec (*iovecs)[nb_rx_desc + 1];
853         int data_off = RTE_PKTMBUF_HEADROOM;
854         uint16_t buf_size;
855         int ret = 0;
856         int fd;
857         int i;
858
859         if ((rx_queue_id >= internals->nb_queues) || !mp) {
860                 RTE_LOG(WARNING, PMD,
861                         "nb_queues %d too small or mempool NULL\n",
862                         internals->nb_queues);
863                 return -1;
864         }
865
866         rxq->mp = mp;
867         rxq->trigger_seen = 1; /* force initial burst */
868         rxq->in_port = dev->data->port_id;
869         rxq->nb_rx_desc = nb_rx_desc;
870         iovecs = rte_zmalloc_socket(dev->data->name, sizeof(*iovecs), 0,
871                                     socket_id);
872         if (!iovecs) {
873                 RTE_LOG(WARNING, PMD,
874                         "%s: Couldn't allocate %d RX descriptors\n",
875                         dev->data->name, nb_rx_desc);
876                 return -ENOMEM;
877         }
878         rxq->iovecs = iovecs;
879
880         /* Now get the space available for data in the mbuf */
881         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
882                                 RTE_PKTMBUF_HEADROOM);
883
884         if (buf_size < ETH_FRAME_LEN) {
885                 RTE_LOG(WARNING, PMD,
886                         "%s: %d bytes will not fit in mbuf (%d bytes)\n",
887                         dev->data->name, ETH_FRAME_LEN, buf_size);
888                 ret = -ENOMEM;
889                 goto error;
890         }
891
892         fd = rx_setup_queue(dev, internals, rx_queue_id);
893         if (fd == -1) {
894                 ret = fd;
895                 goto error;
896         }
897
898         (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
899         (*rxq->iovecs)[0].iov_base = &rxq->pi;
900
901         for (i = 1; i <= nb_rx_desc; i++) {
902                 *tmp = rte_pktmbuf_alloc(rxq->mp);
903                 if (!*tmp) {
904                         RTE_LOG(WARNING, PMD,
905                                 "%s: couldn't allocate memory for queue %d\n",
906                                 dev->data->name, rx_queue_id);
907                         ret = -ENOMEM;
908                         goto error;
909                 }
910                 (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
911                 (*rxq->iovecs)[i].iov_base =
912                         (char *)(*tmp)->buf_addr + data_off;
913                 data_off = 0;
914                 tmp = &(*tmp)->next;
915         }
916
917         RTE_LOG(DEBUG, PMD, "  RX TAP device name %s, qid %d on fd %d\n",
918                 internals->name, rx_queue_id, internals->rxq[rx_queue_id].fd);
919
920         return 0;
921
922 error:
923         rte_pktmbuf_free(rxq->pool);
924         rxq->pool = NULL;
925         rte_free(rxq->iovecs);
926         rxq->iovecs = NULL;
927         return ret;
928 }
929
930 static int
931 tap_tx_queue_setup(struct rte_eth_dev *dev,
932                    uint16_t tx_queue_id,
933                    uint16_t nb_tx_desc __rte_unused,
934                    unsigned int socket_id __rte_unused,
935                    const struct rte_eth_txconf *tx_conf __rte_unused)
936 {
937         struct pmd_internals *internals = dev->data->dev_private;
938         int ret;
939
940         if (tx_queue_id >= internals->nb_queues)
941                 return -1;
942
943         ret = tx_setup_queue(dev, internals, tx_queue_id);
944         if (ret == -1)
945                 return -1;
946
947         RTE_LOG(DEBUG, PMD, "  TX TAP device name %s, qid %d on fd %d\n",
948                 internals->name, tx_queue_id, internals->txq[tx_queue_id].fd);
949
950         return 0;
951 }
952
953 static int
954 tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
955 {
956         struct pmd_internals *pmd = dev->data->dev_private;
957         struct ifreq ifr = { .ifr_mtu = mtu };
958         int err = 0;
959
960         err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1);
961         if (!err)
962                 dev->data->mtu = mtu;
963
964         return err;
965 }
966
967 static int
968 tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
969                      struct ether_addr *mc_addr_set __rte_unused,
970                      uint32_t nb_mc_addr __rte_unused)
971 {
972         /*
973          * Nothing to do actually: the tap has no filtering whatsoever, every
974          * packet is received.
975          */
976         return 0;
977 }
978
979 static const uint32_t*
980 tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
981 {
982         static const uint32_t ptypes[] = {
983                 RTE_PTYPE_INNER_L2_ETHER,
984                 RTE_PTYPE_INNER_L2_ETHER_VLAN,
985                 RTE_PTYPE_INNER_L2_ETHER_QINQ,
986                 RTE_PTYPE_INNER_L3_IPV4,
987                 RTE_PTYPE_INNER_L3_IPV4_EXT,
988                 RTE_PTYPE_INNER_L3_IPV6,
989                 RTE_PTYPE_INNER_L3_IPV6_EXT,
990                 RTE_PTYPE_INNER_L4_FRAG,
991                 RTE_PTYPE_INNER_L4_UDP,
992                 RTE_PTYPE_INNER_L4_TCP,
993                 RTE_PTYPE_INNER_L4_SCTP,
994                 RTE_PTYPE_L2_ETHER,
995                 RTE_PTYPE_L2_ETHER_VLAN,
996                 RTE_PTYPE_L2_ETHER_QINQ,
997                 RTE_PTYPE_L3_IPV4,
998                 RTE_PTYPE_L3_IPV4_EXT,
999                 RTE_PTYPE_L3_IPV6_EXT,
1000                 RTE_PTYPE_L3_IPV6,
1001                 RTE_PTYPE_L4_FRAG,
1002                 RTE_PTYPE_L4_UDP,
1003                 RTE_PTYPE_L4_TCP,
1004                 RTE_PTYPE_L4_SCTP,
1005         };
1006
1007         return ptypes;
1008 }
1009
1010 static int
1011 tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1012                   struct rte_eth_fc_conf *fc_conf)
1013 {
1014         fc_conf->mode = RTE_FC_NONE;
1015         return 0;
1016 }
1017
1018 static int
1019 tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1020                   struct rte_eth_fc_conf *fc_conf)
1021 {
1022         if (fc_conf->mode != RTE_FC_NONE)
1023                 return -ENOTSUP;
1024         return 0;
1025 }
1026
1027 static const struct eth_dev_ops ops = {
1028         .dev_start              = tap_dev_start,
1029         .dev_stop               = tap_dev_stop,
1030         .dev_close              = tap_dev_close,
1031         .dev_configure          = tap_dev_configure,
1032         .dev_infos_get          = tap_dev_info,
1033         .rx_queue_setup         = tap_rx_queue_setup,
1034         .tx_queue_setup         = tap_tx_queue_setup,
1035         .rx_queue_release       = tap_rx_queue_release,
1036         .tx_queue_release       = tap_tx_queue_release,
1037         .flow_ctrl_get          = tap_flow_ctrl_get,
1038         .flow_ctrl_set          = tap_flow_ctrl_set,
1039         .link_update            = tap_link_update,
1040         .dev_set_link_up        = tap_link_set_up,
1041         .dev_set_link_down      = tap_link_set_down,
1042         .promiscuous_enable     = tap_promisc_enable,
1043         .promiscuous_disable    = tap_promisc_disable,
1044         .allmulticast_enable    = tap_allmulti_enable,
1045         .allmulticast_disable   = tap_allmulti_disable,
1046         .mac_addr_set           = tap_mac_set,
1047         .mtu_set                = tap_mtu_set,
1048         .set_mc_addr_list       = tap_set_mc_addr_list,
1049         .stats_get              = tap_stats_get,
1050         .stats_reset            = tap_stats_reset,
1051         .dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1052         .filter_ctrl            = tap_dev_filter_ctrl,
1053 };
1054
1055 static int
1056 tap_kernel_support(struct pmd_internals *pmd)
1057 {
1058         struct utsname utsname;
1059         int ver[3];
1060
1061         if (uname(&utsname) == -1 ||
1062             sscanf(utsname.release, "%d.%d.%d",
1063                    &ver[0], &ver[1], &ver[2]) != 3)
1064                 return 0;
1065         if (KERNEL_VERSION(ver[0], ver[1], ver[2]) >= FLOWER_KERNEL_VERSION)
1066                 pmd->flower_support = 1;
1067         if (KERNEL_VERSION(ver[0], ver[1], ver[2]) >=
1068             FLOWER_VLAN_KERNEL_VERSION)
1069                 pmd->flower_vlan_support = 1;
1070         return 1;
1071 }
1072
1073 static int
1074 eth_dev_tap_create(const char *name, char *tap_name, char *remote_iface)
1075 {
1076         int numa_node = rte_socket_id();
1077         struct rte_eth_dev *dev = NULL;
1078         struct pmd_internals *pmd = NULL;
1079         struct rte_eth_dev_data *data = NULL;
1080         int i;
1081
1082         RTE_LOG(DEBUG, PMD, "  TAP device on numa %u\n", rte_socket_id());
1083
1084         data = rte_zmalloc_socket(tap_name, sizeof(*data), 0, numa_node);
1085         if (!data) {
1086                 RTE_LOG(ERR, PMD, "TAP Failed to allocate data\n");
1087                 goto error_exit;
1088         }
1089
1090         pmd = rte_zmalloc_socket(tap_name, sizeof(*pmd), 0, numa_node);
1091         if (!pmd) {
1092                 RTE_LOG(ERR, PMD, "TAP Unable to allocate internal struct\n");
1093                 goto error_exit;
1094         }
1095
1096         /* name in allocation and data->name must be consistent */
1097         snprintf(data->name, sizeof(data->name), "%s", name);
1098         dev = rte_eth_dev_allocate(name);
1099         if (!dev) {
1100                 RTE_LOG(ERR, PMD, "TAP Unable to allocate device struct\n");
1101                 goto error_exit;
1102         }
1103
1104         snprintf(pmd->name, sizeof(pmd->name), "%s", tap_name);
1105
1106         pmd->nb_queues = RTE_PMD_TAP_MAX_QUEUES;
1107
1108         pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1109         if (pmd->ioctl_sock == -1) {
1110                 RTE_LOG(ERR, PMD,
1111                         "TAP Unable to get a socket for management: %s\n",
1112                         strerror(errno));
1113                 goto error_exit;
1114         }
1115
1116         /* Setup some default values */
1117         data->dev_private = pmd;
1118         data->port_id = dev->data->port_id;
1119         data->mtu = dev->data->mtu;
1120         data->dev_flags = RTE_ETH_DEV_DETACHABLE;
1121         data->kdrv = RTE_KDRV_NONE;
1122         data->drv_name = pmd_tap_drv.driver.name;
1123         data->numa_node = numa_node;
1124
1125         data->dev_link = pmd_link;
1126         data->mac_addrs = &pmd->eth_addr;
1127         data->nb_rx_queues = pmd->nb_queues;
1128         data->nb_tx_queues = pmd->nb_queues;
1129
1130         dev->data = data;
1131         dev->dev_ops = &ops;
1132         dev->driver = NULL;
1133         dev->rx_pkt_burst = pmd_rx_burst;
1134         dev->tx_pkt_burst = pmd_tx_burst;
1135
1136         /* Presetup the fds to -1 as being not valid */
1137         for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1138                 pmd->rxq[i].fd = -1;
1139                 pmd->txq[i].fd = -1;
1140         }
1141
1142         tap_kernel_support(pmd);
1143         if (!pmd->flower_support)
1144                 return 0;
1145         LIST_INIT(&pmd->flows);
1146         /*
1147          * If no netlink socket can be created, then it will fail when
1148          * creating/destroying flow rules.
1149          */
1150         pmd->nlsk_fd = nl_init();
1151         if (strlen(remote_iface)) {
1152                 pmd->remote_if_index = if_nametoindex(remote_iface);
1153                 snprintf(pmd->remote_iface, RTE_ETH_NAME_MAX_LEN,
1154                          "%s", remote_iface);
1155                 if (!pmd->remote_if_index)
1156                         RTE_LOG(ERR, PMD, "Could not find %s ifindex: "
1157                                 "remote interface will remain unconfigured\n",
1158                                 remote_iface);
1159         }
1160
1161         return 0;
1162
1163 error_exit:
1164         RTE_LOG(DEBUG, PMD, "TAP Unable to initialize %s\n", name);
1165
1166         rte_free(data);
1167         rte_free(pmd);
1168
1169         rte_eth_dev_release_port(dev);
1170
1171         return -EINVAL;
1172 }
1173
1174 static int
1175 set_interface_name(const char *key __rte_unused,
1176                    const char *value,
1177                    void *extra_args)
1178 {
1179         char *name = (char *)extra_args;
1180
1181         if (value)
1182                 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s", value);
1183         else
1184                 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s%d",
1185                          DEFAULT_TAP_NAME, (tap_unit - 1));
1186
1187         return 0;
1188 }
1189
1190 static int
1191 set_interface_speed(const char *key __rte_unused,
1192                     const char *value,
1193                     void *extra_args)
1194 {
1195         *(int *)extra_args = (value) ? atoi(value) : ETH_SPEED_NUM_10G;
1196
1197         return 0;
1198 }
1199
1200 static int
1201 set_remote_iface(const char *key __rte_unused,
1202                  const char *value,
1203                  void *extra_args)
1204 {
1205         char *name = (char *)extra_args;
1206
1207         if (value)
1208                 snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s", value);
1209
1210         return 0;
1211 }
1212
1213 /* Open a TAP interface device.
1214  */
1215 static int
1216 rte_pmd_tap_probe(const char *name, const char *params)
1217 {
1218         int ret;
1219         struct rte_kvargs *kvlist = NULL;
1220         int speed;
1221         char tap_name[RTE_ETH_NAME_MAX_LEN];
1222         char remote_iface[RTE_ETH_NAME_MAX_LEN];
1223
1224         speed = ETH_SPEED_NUM_10G;
1225         snprintf(tap_name, sizeof(tap_name), "%s%d",
1226                  DEFAULT_TAP_NAME, tap_unit++);
1227         memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
1228
1229         if (params && (params[0] != '\0')) {
1230                 RTE_LOG(DEBUG, PMD, "paramaters (%s)\n", params);
1231
1232                 kvlist = rte_kvargs_parse(params, valid_arguments);
1233                 if (kvlist) {
1234                         if (rte_kvargs_count(kvlist, ETH_TAP_SPEED_ARG) == 1) {
1235                                 ret = rte_kvargs_process(kvlist,
1236                                                          ETH_TAP_SPEED_ARG,
1237                                                          &set_interface_speed,
1238                                                          &speed);
1239                                 if (ret == -1)
1240                                         goto leave;
1241                         }
1242
1243                         if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
1244                                 ret = rte_kvargs_process(kvlist,
1245                                                          ETH_TAP_IFACE_ARG,
1246                                                          &set_interface_name,
1247                                                          tap_name);
1248                                 if (ret == -1)
1249                                         goto leave;
1250                         }
1251
1252                         if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
1253                                 ret = rte_kvargs_process(kvlist,
1254                                                          ETH_TAP_REMOTE_ARG,
1255                                                          &set_remote_iface,
1256                                                          remote_iface);
1257                                 if (ret == -1)
1258                                         goto leave;
1259                         }
1260                 }
1261         }
1262         pmd_link.link_speed = speed;
1263
1264         RTE_LOG(NOTICE, PMD, "Initializing pmd_tap for %s as %s\n",
1265                 name, tap_name);
1266
1267         ret = eth_dev_tap_create(name, tap_name, remote_iface);
1268
1269 leave:
1270         if (ret == -1) {
1271                 RTE_LOG(ERR, PMD, "Failed to create pmd for %s as %s\n",
1272                         name, tap_name);
1273                 tap_unit--;             /* Restore the unit number */
1274         }
1275         rte_kvargs_free(kvlist);
1276
1277         return ret;
1278 }
1279
1280 /* detach a TAP device.
1281  */
1282 static int
1283 rte_pmd_tap_remove(const char *name)
1284 {
1285         struct rte_eth_dev *eth_dev = NULL;
1286         struct pmd_internals *internals;
1287         int i;
1288
1289         RTE_LOG(DEBUG, PMD, "Closing TUN/TAP Ethernet device on numa %u\n",
1290                 rte_socket_id());
1291
1292         /* find the ethdev entry */
1293         eth_dev = rte_eth_dev_allocated(name);
1294         if (!eth_dev)
1295                 return 0;
1296
1297         internals = eth_dev->data->dev_private;
1298         if (internals->flower_support && internals->nlsk_fd) {
1299                 tap_flow_flush(eth_dev, NULL);
1300                 tap_flow_implicit_flush(internals, NULL);
1301                 nl_final(internals->nlsk_fd);
1302         }
1303         for (i = 0; i < internals->nb_queues; i++)
1304                 if (internals->rxq[i].fd != -1)
1305                         close(internals->rxq[i].fd);
1306
1307         close(internals->ioctl_sock);
1308         rte_free(eth_dev->data->dev_private);
1309         rte_free(eth_dev->data);
1310
1311         rte_eth_dev_release_port(eth_dev);
1312
1313         return 0;
1314 }
1315
1316 static struct rte_vdev_driver pmd_tap_drv = {
1317         .probe = rte_pmd_tap_probe,
1318         .remove = rte_pmd_tap_remove,
1319 };
1320 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
1321 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
1322 RTE_PMD_REGISTER_PARAM_STRING(net_tap, "iface=<string>,speed=N");