50a07249dff21c4c733886a5f7da386c58dfd7ee
[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_common.h>
36 #include <rte_mbuf.h>
37 #include <rte_ethdev.h>
38 #include <rte_malloc.h>
39 #include <rte_vdev.h>
40 #include <rte_kvargs.h>
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/socket.h>
45 #include <sys/ioctl.h>
46 #include <sys/mman.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <stdint.h>
50 #include <unistd.h>
51 #include <arpa/inet.h>
52 #include <linux/if.h>
53 #include <linux/if_tun.h>
54 #include <linux/if_ether.h>
55 #include <fcntl.h>
56
57 /* Linux based path to the TUN device */
58 #define TUN_TAP_DEV_PATH        "/dev/net/tun"
59 #define DEFAULT_TAP_NAME        "dtap"
60
61 #define ETH_TAP_IFACE_ARG       "iface"
62 #define ETH_TAP_SPEED_ARG       "speed"
63
64 #ifdef IFF_MULTI_QUEUE
65 #define RTE_PMD_TAP_MAX_QUEUES  16
66 #else
67 #define RTE_PMD_TAP_MAX_QUEUES  1
68 #endif
69
70 static struct rte_vdev_driver pmd_tap_drv;
71
72 static const char *valid_arguments[] = {
73         ETH_TAP_IFACE_ARG,
74         ETH_TAP_SPEED_ARG,
75         NULL
76 };
77
78 static int tap_unit;
79
80 static volatile uint32_t tap_trigger;   /* Rx trigger */
81
82 static struct rte_eth_link pmd_link = {
83         .link_speed = ETH_SPEED_NUM_10G,
84         .link_duplex = ETH_LINK_FULL_DUPLEX,
85         .link_status = ETH_LINK_DOWN,
86         .link_autoneg = ETH_LINK_SPEED_AUTONEG
87 };
88
89 struct pkt_stats {
90         uint64_t opackets;              /* Number of output packets */
91         uint64_t ipackets;              /* Number of input packets */
92         uint64_t obytes;                /* Number of bytes on output */
93         uint64_t ibytes;                /* Number of bytes on input */
94         uint64_t errs;                  /* Number of error packets */
95 };
96
97 struct rx_queue {
98         struct rte_mempool *mp;         /* Mempool for RX packets */
99         uint32_t trigger_seen;          /* Last seen Rx trigger value */
100         uint16_t in_port;               /* Port ID */
101         int fd;
102
103         struct pkt_stats stats;         /* Stats for this RX queue */
104 };
105
106 struct tx_queue {
107         int fd;
108         struct pkt_stats stats;         /* Stats for this TX queue */
109 };
110
111 struct pmd_internals {
112         char name[RTE_ETH_NAME_MAX_LEN];        /* Internal Tap device name */
113         uint16_t nb_queues;             /* Number of queues supported */
114         struct ether_addr eth_addr;     /* Mac address of the device port */
115
116         int if_index;                   /* IF_INDEX for the port */
117         int ioctl_sock;                 /* socket for ioctl calls */
118
119         struct rx_queue rxq[RTE_PMD_TAP_MAX_QUEUES];    /* List of RX queues */
120         struct tx_queue txq[RTE_PMD_TAP_MAX_QUEUES];    /* List of TX queues */
121 };
122
123 static void
124 tap_trigger_cb(int sig __rte_unused)
125 {
126         /* Valid trigger values are nonzero */
127         tap_trigger = (tap_trigger + 1) | 0x80000000;
128 }
129
130 /* Tun/Tap allocation routine
131  *
132  * name is the number of the interface to use, unless NULL to take the host
133  * supplied name.
134  */
135 static int
136 tun_alloc(struct pmd_internals *pmd, uint16_t qid)
137 {
138         struct ifreq ifr;
139 #ifdef IFF_MULTI_QUEUE
140         unsigned int features;
141 #endif
142         int fd;
143
144         memset(&ifr, 0, sizeof(struct ifreq));
145
146         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
147         snprintf(ifr.ifr_name, IFNAMSIZ, "%s", pmd->name);
148
149         RTE_LOG(DEBUG, PMD, "ifr_name '%s'\n", ifr.ifr_name);
150
151         fd = open(TUN_TAP_DEV_PATH, O_RDWR);
152         if (fd < 0) {
153                 RTE_LOG(ERR, PMD, "Unable to create TAP interface");
154                 goto error;
155         }
156
157 #ifdef IFF_MULTI_QUEUE
158         /* Grab the TUN features to verify we can work multi-queue */
159         if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
160                 RTE_LOG(ERR, PMD, "TAP unable to get TUN/TAP features\n");
161                 goto error;
162         }
163         RTE_LOG(DEBUG, PMD, "  TAP Features %08x\n", features);
164
165         if (features & IFF_MULTI_QUEUE) {
166                 RTE_LOG(DEBUG, PMD, "  Multi-queue support for %d queues\n",
167                         RTE_PMD_TAP_MAX_QUEUES);
168                 ifr.ifr_flags |= IFF_MULTI_QUEUE;
169         } else
170 #endif
171         {
172                 ifr.ifr_flags |= IFF_ONE_QUEUE;
173                 RTE_LOG(DEBUG, PMD, "  Single queue only support\n");
174         }
175
176         /* Set the TUN/TAP configuration and set the name if needed */
177         if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
178                 RTE_LOG(WARNING, PMD,
179                         "Unable to set TUNSETIFF for %s\n",
180                         ifr.ifr_name);
181                 perror("TUNSETIFF");
182                 goto error;
183         }
184
185         /* Always set the file descriptor to non-blocking */
186         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
187                 RTE_LOG(WARNING, PMD,
188                         "Unable to set %s to nonblocking\n",
189                         ifr.ifr_name);
190                 perror("F_SETFL, NONBLOCK");
191                 goto error;
192         }
193
194         /* Set up trigger to optimize empty Rx bursts */
195         errno = 0;
196         do {
197                 struct sigaction sa;
198                 int flags = fcntl(fd, F_GETFL);
199
200                 if (flags == -1 || sigaction(SIGIO, NULL, &sa) == -1)
201                         break;
202                 if (sa.sa_handler != tap_trigger_cb) {
203                         /*
204                          * Make sure SIGIO is not already taken. This is done
205                          * as late as possible to leave the application a
206                          * chance to set up its own signal handler first.
207                          */
208                         if (sa.sa_handler != SIG_IGN &&
209                             sa.sa_handler != SIG_DFL) {
210                                 errno = EBUSY;
211                                 break;
212                         }
213                         sa = (struct sigaction){
214                                 .sa_flags = SA_RESTART,
215                                 .sa_handler = tap_trigger_cb,
216                         };
217                         if (sigaction(SIGIO, &sa, NULL) == -1)
218                                 break;
219                 }
220                 /* Enable SIGIO on file descriptor */
221                 fcntl(fd, F_SETFL, flags | O_ASYNC);
222                 fcntl(fd, F_SETOWN, getpid());
223         } while (0);
224         if (errno) {
225                 /* Disable trigger globally in case of error */
226                 tap_trigger = 0;
227                 RTE_LOG(WARNING, PMD, "Rx trigger disabled: %s\n",
228                         strerror(errno));
229         }
230
231         if (qid == 0) {
232                 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
233                         RTE_LOG(ERR, PMD, "ioctl failed (SIOCGIFHWADDR) (%s)\n",
234                                 ifr.ifr_name);
235                         goto error;
236                 }
237
238                 rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
239         }
240
241         return fd;
242
243 error:
244         if (fd > 0)
245                 close(fd);
246         return -1;
247 }
248
249 /* Callback to handle the rx burst of packets to the correct interface and
250  * file descriptor(s) in a multi-queue setup.
251  */
252 static uint16_t
253 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
254 {
255         int len;
256         struct rte_mbuf *mbuf;
257         struct rx_queue *rxq = queue;
258         uint16_t num_rx;
259         unsigned long num_rx_bytes = 0;
260         uint32_t trigger = tap_trigger;
261
262         if (trigger == rxq->trigger_seen)
263                 return 0;
264         if (trigger)
265                 rxq->trigger_seen = trigger;
266         rte_compiler_barrier();
267         for (num_rx = 0; num_rx < nb_pkts; ) {
268                 /* allocate the next mbuf */
269                 mbuf = rte_pktmbuf_alloc(rxq->mp);
270                 if (unlikely(!mbuf)) {
271                         RTE_LOG(WARNING, PMD, "TAP unable to allocate mbuf\n");
272                         break;
273                 }
274
275                 len = read(rxq->fd, rte_pktmbuf_mtod(mbuf, char *),
276                            rte_pktmbuf_tailroom(mbuf));
277                 if (len <= 0) {
278                         rte_pktmbuf_free(mbuf);
279                         break;
280                 }
281
282                 mbuf->data_len = len;
283                 mbuf->pkt_len = len;
284                 mbuf->port = rxq->in_port;
285
286                 /* account for the receive frame */
287                 bufs[num_rx++] = mbuf;
288                 num_rx_bytes += mbuf->pkt_len;
289         }
290         rxq->stats.ipackets += num_rx;
291         rxq->stats.ibytes += num_rx_bytes;
292
293         return num_rx;
294 }
295
296 /* Callback to handle sending packets from the tap interface
297  */
298 static uint16_t
299 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
300 {
301         struct rte_mbuf *mbuf;
302         struct tx_queue *txq = queue;
303         uint16_t num_tx = 0;
304         unsigned long num_tx_bytes = 0;
305         int i, n;
306
307         if (unlikely(nb_pkts == 0))
308                 return 0;
309
310         for (i = 0; i < nb_pkts; i++) {
311                 /* copy the tx frame data */
312                 mbuf = bufs[num_tx];
313                 n = write(txq->fd,
314                           rte_pktmbuf_mtod(mbuf, void *),
315                           rte_pktmbuf_pkt_len(mbuf));
316                 if (n <= 0)
317                         break;
318
319                 num_tx++;
320                 num_tx_bytes += mbuf->pkt_len;
321                 rte_pktmbuf_free(mbuf);
322         }
323
324         txq->stats.opackets += num_tx;
325         txq->stats.errs += nb_pkts - num_tx;
326         txq->stats.obytes += num_tx_bytes;
327
328         return num_tx;
329 }
330
331 static int
332 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
333           struct ifreq *ifr, int set)
334 {
335         short req_flags = ifr->ifr_flags;
336
337         snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->name);
338         switch (request) {
339         case SIOCSIFFLAGS:
340                 /* fetch current flags to leave other flags untouched */
341                 if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
342                         goto error;
343                 if (set)
344                         ifr->ifr_flags |= req_flags;
345                 else
346                         ifr->ifr_flags &= ~req_flags;
347                 break;
348         default:
349                 RTE_LOG(WARNING, PMD, "%s: ioctl() called with wrong arg\n",
350                         pmd->name);
351                 return -EINVAL;
352         }
353         if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
354                 goto error;
355         return 0;
356
357 error:
358         RTE_LOG(ERR, PMD, "%s: ioctl(%lu) failed with error: %s\n",
359                 ifr->ifr_name, request, strerror(errno));
360         return -errno;
361 }
362
363 static int
364 tap_link_set_down(struct rte_eth_dev *dev)
365 {
366         struct pmd_internals *pmd = dev->data->dev_private;
367         struct ifreq ifr = { .ifr_flags = IFF_UP };
368
369         dev->data->dev_link.link_status = ETH_LINK_DOWN;
370         return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
371 }
372
373 static int
374 tap_link_set_up(struct rte_eth_dev *dev)
375 {
376         struct pmd_internals *pmd = dev->data->dev_private;
377         struct ifreq ifr = { .ifr_flags = IFF_UP };
378
379         dev->data->dev_link.link_status = ETH_LINK_UP;
380         return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
381 }
382
383 static int
384 tap_dev_start(struct rte_eth_dev *dev)
385 {
386         return tap_link_set_up(dev);
387 }
388
389 /* This function gets called when the current port gets stopped.
390  */
391 static void
392 tap_dev_stop(struct rte_eth_dev *dev)
393 {
394         tap_link_set_down(dev);
395 }
396
397 static int
398 tap_dev_configure(struct rte_eth_dev *dev __rte_unused)
399 {
400         return 0;
401 }
402
403 static void
404 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
405 {
406         struct pmd_internals *internals = dev->data->dev_private;
407
408         dev_info->if_index = internals->if_index;
409         dev_info->max_mac_addrs = 1;
410         dev_info->max_rx_pktlen = (uint32_t)ETHER_MAX_VLAN_FRAME_LEN;
411         dev_info->max_rx_queues = internals->nb_queues;
412         dev_info->max_tx_queues = internals->nb_queues;
413         dev_info->min_rx_bufsize = 0;
414         dev_info->pci_dev = NULL;
415 }
416
417 static void
418 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
419 {
420         unsigned int i, imax;
421         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
422         unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
423         const struct pmd_internals *pmd = dev->data->dev_private;
424
425         imax = (pmd->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
426                 pmd->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
427
428         for (i = 0; i < imax; i++) {
429                 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
430                 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
431                 rx_total += tap_stats->q_ipackets[i];
432                 rx_bytes_total += tap_stats->q_ibytes[i];
433
434                 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
435                 tap_stats->q_errors[i] = pmd->txq[i].stats.errs;
436                 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
437                 tx_total += tap_stats->q_opackets[i];
438                 tx_err_total += tap_stats->q_errors[i];
439                 tx_bytes_total += tap_stats->q_obytes[i];
440         }
441
442         tap_stats->ipackets = rx_total;
443         tap_stats->ibytes = rx_bytes_total;
444         tap_stats->opackets = tx_total;
445         tap_stats->oerrors = tx_err_total;
446         tap_stats->obytes = tx_bytes_total;
447 }
448
449 static void
450 tap_stats_reset(struct rte_eth_dev *dev)
451 {
452         int i;
453         struct pmd_internals *pmd = dev->data->dev_private;
454
455         for (i = 0; i < pmd->nb_queues; i++) {
456                 pmd->rxq[i].stats.ipackets = 0;
457                 pmd->rxq[i].stats.ibytes = 0;
458
459                 pmd->txq[i].stats.opackets = 0;
460                 pmd->txq[i].stats.errs = 0;
461                 pmd->txq[i].stats.obytes = 0;
462         }
463 }
464
465 static void
466 tap_dev_close(struct rte_eth_dev *dev __rte_unused)
467 {
468         int i;
469         struct pmd_internals *internals = dev->data->dev_private;
470
471         tap_link_set_down(dev);
472
473         for (i = 0; i < internals->nb_queues; i++) {
474                 if (internals->rxq[i].fd != -1)
475                         close(internals->rxq[i].fd);
476                 internals->rxq[i].fd = -1;
477                 internals->txq[i].fd = -1;
478         }
479 }
480
481 static void
482 tap_rx_queue_release(void *queue)
483 {
484         struct rx_queue *rxq = queue;
485
486         if (rxq && (rxq->fd > 0)) {
487                 close(rxq->fd);
488                 rxq->fd = -1;
489         }
490 }
491
492 static void
493 tap_tx_queue_release(void *queue)
494 {
495         struct tx_queue *txq = queue;
496
497         if (txq && (txq->fd > 0)) {
498                 close(txq->fd);
499                 txq->fd = -1;
500         }
501 }
502
503 static int
504 tap_link_update(struct rte_eth_dev *dev __rte_unused,
505                 int wait_to_complete __rte_unused)
506 {
507         return 0;
508 }
509
510 static void
511 tap_promisc_enable(struct rte_eth_dev *dev)
512 {
513         struct pmd_internals *pmd = dev->data->dev_private;
514         struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
515
516         dev->data->promiscuous = 1;
517         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
518 }
519
520 static void
521 tap_promisc_disable(struct rte_eth_dev *dev)
522 {
523         struct pmd_internals *pmd = dev->data->dev_private;
524         struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
525
526         dev->data->promiscuous = 0;
527         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
528 }
529
530 static void
531 tap_allmulti_enable(struct rte_eth_dev *dev)
532 {
533         struct pmd_internals *pmd = dev->data->dev_private;
534         struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
535
536         dev->data->all_multicast = 1;
537         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
538 }
539
540 static void
541 tap_allmulti_disable(struct rte_eth_dev *dev)
542 {
543         struct pmd_internals *pmd = dev->data->dev_private;
544         struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
545
546         dev->data->all_multicast = 0;
547         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
548 }
549
550 static int
551 tap_setup_queue(struct rte_eth_dev *dev,
552                 struct pmd_internals *internals,
553                 uint16_t qid)
554 {
555         struct pmd_internals *pmd = dev->data->dev_private;
556         struct rx_queue *rx = &internals->rxq[qid];
557         struct tx_queue *tx = &internals->txq[qid];
558         int fd;
559
560         fd = rx->fd;
561         if (fd < 0) {
562                 fd = tx->fd;
563                 if (fd < 0) {
564                         RTE_LOG(INFO, PMD, "Add queue to TAP %s for qid %d\n",
565                                 pmd->name, qid);
566                         fd = tun_alloc(pmd, qid);
567                         if (fd < 0) {
568                                 RTE_LOG(ERR, PMD, "tun_alloc(%s, %d) failed\n",
569                                         pmd->name, qid);
570                                 return -1;
571                         }
572                 }
573         }
574
575         rx->fd = fd;
576         tx->fd = fd;
577
578         return fd;
579 }
580
581 static int
582 rx_setup_queue(struct rte_eth_dev *dev,
583                 struct pmd_internals *internals,
584                 uint16_t qid)
585 {
586         dev->data->rx_queues[qid] = &internals->rxq[qid];
587
588         return tap_setup_queue(dev, internals, qid);
589 }
590
591 static int
592 tx_setup_queue(struct rte_eth_dev *dev,
593                 struct pmd_internals *internals,
594                 uint16_t qid)
595 {
596         dev->data->tx_queues[qid] = &internals->txq[qid];
597
598         return tap_setup_queue(dev, internals, qid);
599 }
600
601 static int
602 tap_rx_queue_setup(struct rte_eth_dev *dev,
603                    uint16_t rx_queue_id,
604                    uint16_t nb_rx_desc __rte_unused,
605                    unsigned int socket_id __rte_unused,
606                    const struct rte_eth_rxconf *rx_conf __rte_unused,
607                    struct rte_mempool *mp)
608 {
609         struct pmd_internals *internals = dev->data->dev_private;
610         uint16_t buf_size;
611         int fd;
612
613         if ((rx_queue_id >= internals->nb_queues) || !mp) {
614                 RTE_LOG(WARNING, PMD,
615                         "nb_queues %d too small or mempool NULL\n",
616                         internals->nb_queues);
617                 return -1;
618         }
619
620         internals->rxq[rx_queue_id].mp = mp;
621         internals->rxq[rx_queue_id].trigger_seen = 1; /* force initial burst */
622         internals->rxq[rx_queue_id].in_port = dev->data->port_id;
623
624         /* Now get the space available for data in the mbuf */
625         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
626                                 RTE_PKTMBUF_HEADROOM);
627
628         if (buf_size < ETH_FRAME_LEN) {
629                 RTE_LOG(WARNING, PMD,
630                         "%s: %d bytes will not fit in mbuf (%d bytes)\n",
631                         dev->data->name, ETH_FRAME_LEN, buf_size);
632                 return -ENOMEM;
633         }
634
635         fd = rx_setup_queue(dev, internals, rx_queue_id);
636         if (fd == -1)
637                 return -1;
638
639         RTE_LOG(DEBUG, PMD, "  RX TAP device name %s, qid %d on fd %d\n",
640                 internals->name, rx_queue_id, internals->rxq[rx_queue_id].fd);
641
642         return 0;
643 }
644
645 static int
646 tap_tx_queue_setup(struct rte_eth_dev *dev,
647                    uint16_t tx_queue_id,
648                    uint16_t nb_tx_desc __rte_unused,
649                    unsigned int socket_id __rte_unused,
650                    const struct rte_eth_txconf *tx_conf __rte_unused)
651 {
652         struct pmd_internals *internals = dev->data->dev_private;
653         int ret;
654
655         if (tx_queue_id >= internals->nb_queues)
656                 return -1;
657
658         ret = tx_setup_queue(dev, internals, tx_queue_id);
659         if (ret == -1)
660                 return -1;
661
662         RTE_LOG(DEBUG, PMD, "  TX TAP device name %s, qid %d on fd %d\n",
663                 internals->name, tx_queue_id, internals->txq[tx_queue_id].fd);
664
665         return 0;
666 }
667
668 static const struct eth_dev_ops ops = {
669         .dev_start              = tap_dev_start,
670         .dev_stop               = tap_dev_stop,
671         .dev_close              = tap_dev_close,
672         .dev_configure          = tap_dev_configure,
673         .dev_infos_get          = tap_dev_info,
674         .rx_queue_setup         = tap_rx_queue_setup,
675         .tx_queue_setup         = tap_tx_queue_setup,
676         .rx_queue_release       = tap_rx_queue_release,
677         .tx_queue_release       = tap_tx_queue_release,
678         .link_update            = tap_link_update,
679         .dev_set_link_up        = tap_link_set_up,
680         .dev_set_link_down      = tap_link_set_down,
681         .promiscuous_enable     = tap_promisc_enable,
682         .promiscuous_disable    = tap_promisc_disable,
683         .allmulticast_enable    = tap_allmulti_enable,
684         .allmulticast_disable   = tap_allmulti_disable,
685         .stats_get              = tap_stats_get,
686         .stats_reset            = tap_stats_reset,
687 };
688
689 static int
690 eth_dev_tap_create(const char *name, char *tap_name)
691 {
692         int numa_node = rte_socket_id();
693         struct rte_eth_dev *dev = NULL;
694         struct pmd_internals *pmd = NULL;
695         struct rte_eth_dev_data *data = NULL;
696         int i;
697
698         RTE_LOG(DEBUG, PMD, "  TAP device on numa %u\n", rte_socket_id());
699
700         data = rte_zmalloc_socket(tap_name, sizeof(*data), 0, numa_node);
701         if (!data) {
702                 RTE_LOG(ERR, PMD, "TAP Failed to allocate data\n");
703                 goto error_exit;
704         }
705
706         pmd = rte_zmalloc_socket(tap_name, sizeof(*pmd), 0, numa_node);
707         if (!pmd) {
708                 RTE_LOG(ERR, PMD, "TAP Unable to allocate internal struct\n");
709                 goto error_exit;
710         }
711
712         /* name in allocation and data->name must be consistent */
713         snprintf(data->name, sizeof(data->name), "%s", name);
714         dev = rte_eth_dev_allocate(name);
715         if (!dev) {
716                 RTE_LOG(ERR, PMD, "TAP Unable to allocate device struct\n");
717                 goto error_exit;
718         }
719
720         snprintf(pmd->name, sizeof(pmd->name), "%s", tap_name);
721
722         pmd->nb_queues = RTE_PMD_TAP_MAX_QUEUES;
723
724         pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
725         if (pmd->ioctl_sock == -1) {
726                 RTE_LOG(ERR, PMD,
727                         "TAP Unable to get a socket for management: %s\n",
728                         strerror(errno));
729                 goto error_exit;
730         }
731
732         /* Setup some default values */
733         data->dev_private = pmd;
734         data->port_id = dev->data->port_id;
735         data->dev_flags = RTE_ETH_DEV_DETACHABLE;
736         data->kdrv = RTE_KDRV_NONE;
737         data->drv_name = pmd_tap_drv.driver.name;
738         data->numa_node = numa_node;
739
740         data->dev_link = pmd_link;
741         data->mac_addrs = &pmd->eth_addr;
742         data->nb_rx_queues = pmd->nb_queues;
743         data->nb_tx_queues = pmd->nb_queues;
744
745         dev->data = data;
746         dev->dev_ops = &ops;
747         dev->driver = NULL;
748         dev->rx_pkt_burst = pmd_rx_burst;
749         dev->tx_pkt_burst = pmd_tx_burst;
750
751         /* Presetup the fds to -1 as being not valid */
752         for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
753                 pmd->rxq[i].fd = -1;
754                 pmd->txq[i].fd = -1;
755         }
756
757         return 0;
758
759 error_exit:
760         RTE_LOG(DEBUG, PMD, "TAP Unable to initialize %s\n", name);
761
762         rte_free(data);
763         rte_free(pmd);
764
765         rte_eth_dev_release_port(dev);
766
767         return -EINVAL;
768 }
769
770 static int
771 set_interface_name(const char *key __rte_unused,
772                    const char *value,
773                    void *extra_args)
774 {
775         char *name = (char *)extra_args;
776
777         if (value)
778                 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s", value);
779         else
780                 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s%d",
781                          DEFAULT_TAP_NAME, (tap_unit - 1));
782
783         return 0;
784 }
785
786 static int
787 set_interface_speed(const char *key __rte_unused,
788                     const char *value,
789                     void *extra_args)
790 {
791         *(int *)extra_args = (value) ? atoi(value) : ETH_SPEED_NUM_10G;
792
793         return 0;
794 }
795
796 /* Open a TAP interface device.
797  */
798 static int
799 rte_pmd_tap_probe(const char *name, const char *params)
800 {
801         int ret;
802         struct rte_kvargs *kvlist = NULL;
803         int speed;
804         char tap_name[RTE_ETH_NAME_MAX_LEN];
805
806         speed = ETH_SPEED_NUM_10G;
807         snprintf(tap_name, sizeof(tap_name), "%s%d",
808                  DEFAULT_TAP_NAME, tap_unit++);
809
810         if (params && (params[0] != '\0')) {
811                 RTE_LOG(DEBUG, PMD, "paramaters (%s)\n", params);
812
813                 kvlist = rte_kvargs_parse(params, valid_arguments);
814                 if (kvlist) {
815                         if (rte_kvargs_count(kvlist, ETH_TAP_SPEED_ARG) == 1) {
816                                 ret = rte_kvargs_process(kvlist,
817                                                          ETH_TAP_SPEED_ARG,
818                                                          &set_interface_speed,
819                                                          &speed);
820                                 if (ret == -1)
821                                         goto leave;
822                         }
823
824                         if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
825                                 ret = rte_kvargs_process(kvlist,
826                                                          ETH_TAP_IFACE_ARG,
827                                                          &set_interface_name,
828                                                          tap_name);
829                                 if (ret == -1)
830                                         goto leave;
831                         }
832                 }
833         }
834         pmd_link.link_speed = speed;
835
836         RTE_LOG(NOTICE, PMD, "Initializing pmd_tap for %s as %s\n",
837                 name, tap_name);
838
839         ret = eth_dev_tap_create(name, tap_name);
840
841 leave:
842         if (ret == -1) {
843                 RTE_LOG(ERR, PMD, "Failed to create pmd for %s as %s\n",
844                         name, tap_name);
845                 tap_unit--;             /* Restore the unit number */
846         }
847         rte_kvargs_free(kvlist);
848
849         return ret;
850 }
851
852 /* detach a TAP device.
853  */
854 static int
855 rte_pmd_tap_remove(const char *name)
856 {
857         struct rte_eth_dev *eth_dev = NULL;
858         struct pmd_internals *internals;
859         int i;
860
861         RTE_LOG(DEBUG, PMD, "Closing TUN/TAP Ethernet device on numa %u\n",
862                 rte_socket_id());
863
864         /* find the ethdev entry */
865         eth_dev = rte_eth_dev_allocated(name);
866         if (!eth_dev)
867                 return 0;
868
869         internals = eth_dev->data->dev_private;
870         for (i = 0; i < internals->nb_queues; i++)
871                 if (internals->rxq[i].fd != -1)
872                         close(internals->rxq[i].fd);
873
874         close(internals->ioctl_sock);
875         rte_free(eth_dev->data->dev_private);
876         rte_free(eth_dev->data);
877
878         rte_eth_dev_release_port(eth_dev);
879
880         return 0;
881 }
882
883 static struct rte_vdev_driver pmd_tap_drv = {
884         .probe = rte_pmd_tap_probe,
885         .remove = rte_pmd_tap_remove,
886 };
887 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
888 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
889 RTE_PMD_REGISTER_PARAM_STRING(net_tap, "iface=<string>,speed=N");