4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <rte_ethdev.h>
36 #include <rte_malloc.h>
38 #include <rte_kvargs.h>
40 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
47 #include <arpa/inet.h>
49 #include <linux/if_tun.h>
50 #include <linux/if_ether.h>
53 /* Linux based path to the TUN device */
54 #define TUN_TAP_DEV_PATH "/dev/net/tun"
55 #define DEFAULT_TAP_NAME "dtap"
57 #define ETH_TAP_IFACE_ARG "iface"
58 #define ETH_TAP_SPEED_ARG "speed"
60 #define RTE_PMD_TAP_MAX_QUEUES 16
62 static struct rte_vdev_driver pmd_tap_drv;
64 static const char *valid_arguments[] = {
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
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 */
88 struct rte_mempool *mp; /* Mempool for RX packets */
89 uint16_t in_port; /* Port ID */
92 struct pkt_stats stats; /* Stats for this RX queue */
97 struct pkt_stats stats; /* Stats for this TX queue */
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 */
105 int if_index; /* IF_INDEX for the port */
106 int fds[RTE_PMD_TAP_MAX_QUEUES]; /* List of all file descriptors */
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 */
112 /* Tun/Tap allocation routine
114 * name is the number of the interface to use, unless NULL to take the host
118 tun_alloc(char *name)
121 unsigned int features;
124 memset(&ifr, 0, sizeof(struct ifreq));
126 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
128 strncpy(ifr.ifr_name, name, IFNAMSIZ);
130 fd = open(TUN_TAP_DEV_PATH, O_RDWR);
132 RTE_LOG(ERR, PMD, "Unable to create TAP interface");
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");
141 RTE_LOG(DEBUG, PMD, "TUN/TAP Features %08x\n", features);
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");
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");
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);
157 if (RTE_PMD_TAP_MAX_QUEUES > 1) {
158 RTE_LOG(DEBUG, PMD, "TUN/TAP device only one queue\n");
161 ifr.ifr_flags |= IFF_ONE_QUEUE;
162 RTE_LOG(DEBUG, PMD, "Single queue only support\n");
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",
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");
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);
193 /* Callback to handle the rx burst of packets to the correct interface and
194 * file descriptor(s) in a multi-queue setup.
197 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
200 struct rte_mbuf *mbuf;
201 struct rx_queue *rxq = queue;
203 unsigned long num_rx_bytes = 0;
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");
213 len = read(rxq->fd, rte_pktmbuf_mtod(mbuf, char *),
214 rte_pktmbuf_tailroom(mbuf));
216 rte_pktmbuf_free(mbuf);
220 mbuf->data_len = len;
222 mbuf->port = rxq->in_port;
224 /* account for the receive frame */
225 bufs[num_rx++] = mbuf;
226 num_rx_bytes += mbuf->pkt_len;
228 rxq->stats.ipackets += num_rx;
229 rxq->stats.ibytes += num_rx_bytes;
234 /* Callback to handle sending packets from the tap interface
237 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
239 struct rte_mbuf *mbuf;
240 struct tx_queue *txq = queue;
243 unsigned long num_tx_bytes = 0;
246 if (unlikely(nb_pkts == 0))
249 pfd.events = POLLOUT;
251 for (i = 0; i < nb_pkts; i++) {
252 n = poll(&pfd, 1, 0);
257 if (pfd.revents & POLLOUT) {
258 /* copy the tx frame data */
260 n = write(pfd.fd, rte_pktmbuf_mtod(mbuf, void*),
261 rte_pktmbuf_pkt_len(mbuf));
266 num_tx_bytes += mbuf->pkt_len;
267 rte_pktmbuf_free(mbuf);
271 txq->stats.opackets += num_tx;
272 txq->stats.errs += nb_pkts - num_tx;
273 txq->stats.obytes += num_tx_bytes;
278 static int tap_link_set_flags(struct pmd_internals *pmd, short flags, int add)
284 * An AF_INET/DGRAM socket is needed for
285 * SIOCGIFFLAGS/SIOCSIFFLAGS, using fd won't work.
287 s = socket(AF_INET, SOCK_DGRAM, 0);
290 "Unable to get a socket to set flags: %s\n",
294 memset(&ifr, 0, sizeof(ifr));
295 strncpy(ifr.ifr_name, pmd->name, IFNAMSIZ);
296 err = ioctl(s, SIOCGIFFLAGS, &ifr);
298 RTE_LOG(ERR, PMD, "Unable to get tap netdevice flags: %s\n",
304 ifr.ifr_flags |= flags;
306 ifr.ifr_flags &= ~flags;
307 err = ioctl(s, SIOCSIFFLAGS, &ifr);
309 RTE_LOG(ERR, PMD, "Unable to %s flags 0x%x: %s\n",
310 add ? "set" : "unset", flags, strerror(errno));
320 tap_link_set_down(struct rte_eth_dev *dev)
322 struct pmd_internals *pmd = dev->data->dev_private;
324 dev->data->dev_link.link_status = ETH_LINK_DOWN;
325 return tap_link_set_flags(pmd, IFF_UP | IFF_NOARP, 0);
329 tap_link_set_up(struct rte_eth_dev *dev)
331 struct pmd_internals *pmd = dev->data->dev_private;
333 dev->data->dev_link.link_status = ETH_LINK_UP;
334 return tap_link_set_flags(pmd, IFF_UP | IFF_NOARP, 1);
338 tap_dev_start(struct rte_eth_dev *dev)
340 return tap_link_set_up(dev);
343 /* This function gets called when the current port gets stopped.
346 tap_dev_stop(struct rte_eth_dev *dev)
349 struct pmd_internals *internals = dev->data->dev_private;
351 for (i = 0; i < internals->nb_queues; i++)
352 if (internals->fds[i] != -1)
353 close(internals->fds[i]);
354 tap_link_set_down(dev);
358 tap_dev_configure(struct rte_eth_dev *dev __rte_unused)
364 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
366 struct pmd_internals *internals = dev->data->dev_private;
368 dev_info->if_index = internals->if_index;
369 dev_info->max_mac_addrs = 1;
370 dev_info->max_rx_pktlen = (uint32_t)ETHER_MAX_VLAN_FRAME_LEN;
371 dev_info->max_rx_queues = internals->nb_queues;
372 dev_info->max_tx_queues = internals->nb_queues;
373 dev_info->min_rx_bufsize = 0;
374 dev_info->pci_dev = NULL;
378 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
380 unsigned int i, imax;
381 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
382 unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
383 const struct pmd_internals *pmd = dev->data->dev_private;
385 imax = (pmd->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
386 pmd->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
388 for (i = 0; i < imax; i++) {
389 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
390 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
391 rx_total += tap_stats->q_ipackets[i];
392 rx_bytes_total += tap_stats->q_ibytes[i];
395 for (i = 0; i < imax; i++) {
396 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
397 tap_stats->q_errors[i] = pmd->txq[i].stats.errs;
398 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
399 tx_total += tap_stats->q_opackets[i];
400 tx_err_total += tap_stats->q_errors[i];
401 tx_bytes_total += tap_stats->q_obytes[i];
404 tap_stats->ipackets = rx_total;
405 tap_stats->ibytes = rx_bytes_total;
406 tap_stats->opackets = tx_total;
407 tap_stats->oerrors = tx_err_total;
408 tap_stats->obytes = tx_bytes_total;
412 tap_stats_reset(struct rte_eth_dev *dev)
415 struct pmd_internals *pmd = dev->data->dev_private;
417 for (i = 0; i < pmd->nb_queues; i++) {
418 pmd->rxq[i].stats.ipackets = 0;
419 pmd->rxq[i].stats.ibytes = 0;
422 for (i = 0; i < pmd->nb_queues; i++) {
423 pmd->txq[i].stats.opackets = 0;
424 pmd->txq[i].stats.errs = 0;
425 pmd->txq[i].stats.obytes = 0;
430 tap_dev_close(struct rte_eth_dev *dev __rte_unused)
435 tap_rx_queue_release(void *queue)
437 struct rx_queue *rxq = queue;
439 if (rxq && (rxq->fd > 0)) {
446 tap_tx_queue_release(void *queue)
448 struct tx_queue *txq = queue;
450 if (txq && (txq->fd > 0)) {
457 tap_link_update(struct rte_eth_dev *dev __rte_unused,
458 int wait_to_complete __rte_unused)
464 tap_setup_queue(struct rte_eth_dev *dev,
465 struct pmd_internals *internals,
468 struct pmd_internals *pmd = dev->data->dev_private;
469 struct rx_queue *rx = &internals->rxq[qid];
470 struct tx_queue *tx = &internals->txq[qid];
477 RTE_LOG(INFO, PMD, "Add queue to TAP %s for qid %d\n",
479 fd = tun_alloc(pmd->name);
481 RTE_LOG(ERR, PMD, "tun_alloc(%s) failed\n",
495 rx_setup_queue(struct rte_eth_dev *dev,
496 struct pmd_internals *internals,
499 dev->data->rx_queues[qid] = &internals->rxq[qid];
501 return tap_setup_queue(dev, internals, qid);
505 tx_setup_queue(struct rte_eth_dev *dev,
506 struct pmd_internals *internals,
509 dev->data->tx_queues[qid] = &internals->txq[qid];
511 return tap_setup_queue(dev, internals, qid);
515 tap_rx_queue_setup(struct rte_eth_dev *dev,
516 uint16_t rx_queue_id,
517 uint16_t nb_rx_desc __rte_unused,
518 unsigned int socket_id __rte_unused,
519 const struct rte_eth_rxconf *rx_conf __rte_unused,
520 struct rte_mempool *mp)
522 struct pmd_internals *internals = dev->data->dev_private;
526 if ((rx_queue_id >= internals->nb_queues) || !mp) {
527 RTE_LOG(ERR, PMD, "nb_queues %d mp %p\n",
528 internals->nb_queues, mp);
532 internals->rxq[rx_queue_id].mp = mp;
533 internals->rxq[rx_queue_id].in_port = dev->data->port_id;
535 /* Now get the space available for data in the mbuf */
536 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
537 RTE_PKTMBUF_HEADROOM);
539 if (buf_size < ETH_FRAME_LEN) {
541 "%s: %d bytes will not fit in mbuf (%d bytes)\n",
542 dev->data->name, ETH_FRAME_LEN, buf_size);
546 fd = rx_setup_queue(dev, internals, rx_queue_id);
550 internals->fds[rx_queue_id] = fd;
551 RTE_LOG(INFO, PMD, "RX TAP device name %s, qid %d on fd %d\n",
552 internals->name, rx_queue_id, internals->rxq[rx_queue_id].fd);
558 tap_tx_queue_setup(struct rte_eth_dev *dev,
559 uint16_t tx_queue_id,
560 uint16_t nb_tx_desc __rte_unused,
561 unsigned int socket_id __rte_unused,
562 const struct rte_eth_txconf *tx_conf __rte_unused)
564 struct pmd_internals *internals = dev->data->dev_private;
567 if (tx_queue_id >= internals->nb_queues)
570 ret = tx_setup_queue(dev, internals, tx_queue_id);
574 RTE_LOG(INFO, PMD, "TX TAP device name %s, qid %d on fd %d\n",
575 internals->name, tx_queue_id, internals->txq[tx_queue_id].fd);
580 static const struct eth_dev_ops ops = {
581 .dev_start = tap_dev_start,
582 .dev_stop = tap_dev_stop,
583 .dev_close = tap_dev_close,
584 .dev_configure = tap_dev_configure,
585 .dev_infos_get = tap_dev_info,
586 .rx_queue_setup = tap_rx_queue_setup,
587 .tx_queue_setup = tap_tx_queue_setup,
588 .rx_queue_release = tap_rx_queue_release,
589 .tx_queue_release = tap_tx_queue_release,
590 .link_update = tap_link_update,
591 .dev_set_link_up = tap_link_set_up,
592 .dev_set_link_down = tap_link_set_down,
593 .stats_get = tap_stats_get,
594 .stats_reset = tap_stats_reset,
598 pmd_mac_address(int fd, struct rte_eth_dev *dev, struct ether_addr *addr)
602 if ((fd <= 0) || !dev || !addr)
605 memset(&ifr, 0, sizeof(ifr));
607 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
608 RTE_LOG(ERR, PMD, "ioctl failed (SIOCGIFHWADDR) (%s)\n",
613 rte_memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
619 eth_dev_tap_create(const char *name, char *tap_name)
621 int numa_node = rte_socket_id();
622 struct rte_eth_dev *dev = NULL;
623 struct pmd_internals *pmd = NULL;
624 struct rte_eth_dev_data *data = NULL;
628 "%s: Create TAP Ethernet device with %d queues on numa %u\n",
629 name, RTE_PMD_TAP_MAX_QUEUES, rte_socket_id());
631 data = rte_zmalloc_socket(tap_name, sizeof(*data), 0, numa_node);
633 RTE_LOG(ERR, PMD, "Failed to allocate data\n");
637 pmd = rte_zmalloc_socket(tap_name, sizeof(*pmd), 0, numa_node);
639 RTE_LOG(ERR, PMD, "Unable to allocate internal struct\n");
643 /* Use the name and not the tap_name */
644 dev = rte_eth_dev_allocate(tap_name);
646 RTE_LOG(ERR, PMD, "Unable to allocate device struct\n");
650 snprintf(pmd->name, sizeof(pmd->name), "%s", tap_name);
652 pmd->nb_queues = RTE_PMD_TAP_MAX_QUEUES;
654 /* Setup some default values */
655 data->dev_private = pmd;
656 data->port_id = dev->data->port_id;
657 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
658 data->kdrv = RTE_KDRV_NONE;
659 data->drv_name = pmd_tap_drv.driver.name;
660 data->numa_node = numa_node;
662 data->dev_link = pmd_link;
663 data->mac_addrs = &pmd->eth_addr;
664 data->nb_rx_queues = pmd->nb_queues;
665 data->nb_tx_queues = pmd->nb_queues;
670 dev->rx_pkt_burst = pmd_rx_burst;
671 dev->tx_pkt_burst = pmd_tx_burst;
672 snprintf(dev->data->name, sizeof(dev->data->name), "%s", name);
674 /* Create the first Tap device */
675 fd = tun_alloc(tap_name);
677 RTE_LOG(ERR, PMD, "tun_alloc() failed\n");
681 /* Presetup the fds to -1 as being not working */
682 for (i = 1; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
688 /* Take the TUN/TAP fd and place in the first location */
693 if (pmd_mac_address(fd, dev, &pmd->eth_addr) < 0) {
694 RTE_LOG(ERR, PMD, "Unable to get MAC address\n");
701 RTE_PMD_DEBUG_TRACE("Unable to initialize %s\n", name);
706 rte_eth_dev_release_port(dev);
712 set_interface_name(const char *key __rte_unused,
716 char *name = (char *)extra_args;
719 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s", value);
721 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s%d",
722 DEFAULT_TAP_NAME, (tap_unit - 1));
728 set_interface_speed(const char *key __rte_unused,
732 *(int *)extra_args = (value) ? atoi(value) : ETH_SPEED_NUM_10G;
737 /* Open a TAP interface device.
740 rte_pmd_tap_probe(const char *name, const char *params)
743 struct rte_kvargs *kvlist = NULL;
745 char tap_name[RTE_ETH_NAME_MAX_LEN];
747 speed = ETH_SPEED_NUM_10G;
748 snprintf(tap_name, sizeof(tap_name), "%s%d",
749 DEFAULT_TAP_NAME, tap_unit++);
751 if (params && (params[0] != '\0')) {
752 RTE_LOG(INFO, PMD, "paramaters (%s)\n", params);
754 kvlist = rte_kvargs_parse(params, valid_arguments);
756 if (rte_kvargs_count(kvlist, ETH_TAP_SPEED_ARG) == 1) {
757 ret = rte_kvargs_process(kvlist,
759 &set_interface_speed,
765 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
766 ret = rte_kvargs_process(kvlist,
775 pmd_link.link_speed = speed;
777 RTE_LOG(INFO, PMD, "Initializing pmd_tap for %s as %s\n",
780 ret = eth_dev_tap_create(name, tap_name);
784 RTE_LOG(INFO, PMD, "Failed to create pmd for %s as %s\n",
786 tap_unit--; /* Restore the unit number */
788 rte_kvargs_free(kvlist);
793 /* detach a TAP device.
796 rte_pmd_tap_remove(const char *name)
798 struct rte_eth_dev *eth_dev = NULL;
799 struct pmd_internals *internals;
802 RTE_LOG(INFO, PMD, "Closing TUN/TAP Ethernet device on numa %u\n",
805 /* find the ethdev entry */
806 eth_dev = rte_eth_dev_allocated(name);
810 internals = eth_dev->data->dev_private;
811 for (i = 0; i < internals->nb_queues; i++)
812 if (internals->fds[i] != -1)
813 close(internals->fds[i]);
815 rte_free(eth_dev->data->dev_private);
816 rte_free(eth_dev->data);
818 rte_eth_dev_release_port(eth_dev);
823 static struct rte_vdev_driver pmd_tap_drv = {
824 .probe = rte_pmd_tap_probe,
825 .remove = rte_pmd_tap_remove,
827 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
828 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
829 RTE_PMD_REGISTER_PARAM_STRING(net_tap, "iface=<string>,speed=N");