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