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