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