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.
34 #include <rte_atomic.h>
35 #include <rte_branch_prediction.h>
36 #include <rte_common.h>
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
41 #include <rte_kvargs.h>
44 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/utsname.h>
55 #include <arpa/inet.h>
57 #include <linux/if_tun.h>
58 #include <linux/if_ether.h>
59 #include <linux/version.h>
62 #include <rte_eth_tap.h>
64 #include <tap_tcmsgs.h>
66 /* Linux based path to the TUN device */
67 #define TUN_TAP_DEV_PATH "/dev/net/tun"
68 #define DEFAULT_TAP_NAME "dtap"
70 #define ETH_TAP_IFACE_ARG "iface"
71 #define ETH_TAP_SPEED_ARG "speed"
72 #define ETH_TAP_REMOTE_ARG "remote"
74 #ifdef IFF_MULTI_QUEUE
75 #define RTE_PMD_TAP_MAX_QUEUES 16
77 #define RTE_PMD_TAP_MAX_QUEUES 1
80 #define FLOWER_KERNEL_VERSION KERNEL_VERSION(4, 2, 0)
81 #define FLOWER_VLAN_KERNEL_VERSION KERNEL_VERSION(4, 9, 0)
83 static struct rte_vdev_driver pmd_tap_drv;
85 static const char *valid_arguments[] = {
94 static volatile uint32_t tap_trigger; /* Rx trigger */
96 static struct rte_eth_link pmd_link = {
97 .link_speed = ETH_SPEED_NUM_10G,
98 .link_duplex = ETH_LINK_FULL_DUPLEX,
99 .link_status = ETH_LINK_DOWN,
100 .link_autoneg = ETH_LINK_SPEED_AUTONEG
104 tap_trigger_cb(int sig __rte_unused)
106 /* Valid trigger values are nonzero */
107 tap_trigger = (tap_trigger + 1) | 0x80000000;
111 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
112 struct ifreq *ifr, int set);
114 /* Tun/Tap allocation routine
116 * name is the number of the interface to use, unless NULL to take the host
120 tun_alloc(struct pmd_internals *pmd, uint16_t qid)
123 #ifdef IFF_MULTI_QUEUE
124 unsigned int features;
128 memset(&ifr, 0, sizeof(struct ifreq));
131 * Do not set IFF_NO_PI as packet information header will be needed
132 * to check if a received packet has been truncated.
134 ifr.ifr_flags = IFF_TAP;
135 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", pmd->name);
137 RTE_LOG(DEBUG, PMD, "ifr_name '%s'\n", ifr.ifr_name);
139 fd = open(TUN_TAP_DEV_PATH, O_RDWR);
141 RTE_LOG(ERR, PMD, "Unable to create TAP interface");
145 #ifdef IFF_MULTI_QUEUE
146 /* Grab the TUN features to verify we can work multi-queue */
147 if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
148 RTE_LOG(ERR, PMD, "TAP unable to get TUN/TAP features\n");
151 RTE_LOG(DEBUG, PMD, " TAP Features %08x\n", features);
153 if (features & IFF_MULTI_QUEUE) {
154 RTE_LOG(DEBUG, PMD, " Multi-queue support for %d queues\n",
155 RTE_PMD_TAP_MAX_QUEUES);
156 ifr.ifr_flags |= IFF_MULTI_QUEUE;
160 ifr.ifr_flags |= IFF_ONE_QUEUE;
161 RTE_LOG(DEBUG, PMD, " Single queue only support\n");
164 /* Set the TUN/TAP configuration and set the name if needed */
165 if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
166 RTE_LOG(WARNING, PMD,
167 "Unable to set TUNSETIFF for %s\n",
173 /* Always set the file descriptor to non-blocking */
174 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
175 RTE_LOG(WARNING, PMD,
176 "Unable to set %s to nonblocking\n",
178 perror("F_SETFL, NONBLOCK");
182 /* Set up trigger to optimize empty Rx bursts */
186 int flags = fcntl(fd, F_GETFL);
188 if (flags == -1 || sigaction(SIGIO, NULL, &sa) == -1)
190 if (sa.sa_handler != tap_trigger_cb) {
192 * Make sure SIGIO is not already taken. This is done
193 * as late as possible to leave the application a
194 * chance to set up its own signal handler first.
196 if (sa.sa_handler != SIG_IGN &&
197 sa.sa_handler != SIG_DFL) {
201 sa = (struct sigaction){
202 .sa_flags = SA_RESTART,
203 .sa_handler = tap_trigger_cb,
205 if (sigaction(SIGIO, &sa, NULL) == -1)
208 /* Enable SIGIO on file descriptor */
209 fcntl(fd, F_SETFL, flags | O_ASYNC);
210 fcntl(fd, F_SETOWN, getpid());
213 /* Disable trigger globally in case of error */
215 RTE_LOG(WARNING, PMD, "Rx trigger disabled: %s\n",
222 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0) < 0)
224 rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
227 pmd->if_index = if_nametoindex(pmd->name);
228 if (!pmd->if_index) {
230 "Could not find ifindex for %s: rte_flow won't be usable.\n",
234 if (!pmd->flower_support)
236 if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
238 "Could not create multiq qdisc for %s: rte_flow won't be usable.\n",
242 if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
244 "Could not create multiq qdisc for %s: rte_flow won't be usable.\n",
248 if (pmd->remote_if_index) {
250 * Flush usually returns negative value because it tries
251 * to delete every QDISC (and on a running device, one
252 * QDISC at least is needed). Ignore negative return
255 qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
256 if (qdisc_create_ingress(pmd->nlsk_fd,
257 pmd->remote_if_index) < 0)
259 LIST_INIT(&pmd->implicit_flows);
260 if (tap_flow_implicit_create(
261 pmd, TAP_REMOTE_LOCAL_MAC) < 0)
263 if (tap_flow_implicit_create(
264 pmd, TAP_REMOTE_BROADCAST) < 0)
266 if (tap_flow_implicit_create(
267 pmd, TAP_REMOTE_BROADCASTV6) < 0)
269 if (tap_flow_implicit_create(
270 pmd, TAP_REMOTE_TX) < 0)
279 "Could not set up remote flow rules for %s: remote disabled.\n",
281 pmd->remote_if_index = 0;
282 tap_flow_implicit_flush(pmd, NULL);
291 /* Callback to handle the rx burst of packets to the correct interface and
292 * file descriptor(s) in a multi-queue setup.
295 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
297 struct rx_queue *rxq = queue;
299 unsigned long num_rx_bytes = 0;
300 uint32_t trigger = tap_trigger;
302 if (trigger == rxq->trigger_seen)
305 rxq->trigger_seen = trigger;
306 rte_compiler_barrier();
307 for (num_rx = 0; num_rx < nb_pkts; ) {
308 struct rte_mbuf *mbuf = rxq->pool;
309 struct rte_mbuf *seg = NULL;
310 struct rte_mbuf *new_tail = NULL;
311 uint16_t data_off = rte_pktmbuf_headroom(mbuf);
314 len = readv(rxq->fd, *rxq->iovecs,
315 1 + (rxq->rxmode->enable_scatter ?
316 rxq->nb_rx_desc : 1));
317 if (len < (int)sizeof(struct tun_pi))
320 /* Packet couldn't fit in the provided mbuf */
321 if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) {
322 rxq->stats.ierrors++;
326 len -= sizeof(struct tun_pi);
329 mbuf->port = rxq->in_port;
331 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
333 if (unlikely(!buf)) {
334 rxq->stats.rx_nombuf++;
335 /* No new buf has been allocated: do nothing */
336 if (!new_tail || !seg)
340 rte_pktmbuf_free(mbuf);
344 seg = seg ? seg->next : mbuf;
345 if (rxq->pool == mbuf)
348 new_tail->next = buf;
350 new_tail->next = seg->next;
352 /* iovecs[0] is reserved for packet info (pi) */
353 (*rxq->iovecs)[mbuf->nb_segs].iov_len =
354 buf->buf_len - data_off;
355 (*rxq->iovecs)[mbuf->nb_segs].iov_base =
356 (char *)buf->buf_addr + data_off;
358 seg->data_len = RTE_MIN(seg->buf_len - data_off, len);
359 seg->data_off = data_off;
361 len -= seg->data_len;
365 /* First segment has headroom, not the others */
369 mbuf->packet_type = rte_net_get_ptype(mbuf, NULL,
372 /* account for the receive frame */
373 bufs[num_rx++] = mbuf;
374 num_rx_bytes += mbuf->pkt_len;
377 rxq->stats.ipackets += num_rx;
378 rxq->stats.ibytes += num_rx_bytes;
383 /* Callback to handle sending packets from the tap interface
386 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
388 struct tx_queue *txq = queue;
390 unsigned long num_tx_bytes = 0;
394 if (unlikely(nb_pkts == 0))
397 max_size = *txq->mtu + (ETHER_HDR_LEN + ETHER_CRC_LEN + 4);
398 for (i = 0; i < nb_pkts; i++) {
399 struct rte_mbuf *mbuf = bufs[num_tx];
400 struct iovec iovecs[mbuf->nb_segs + 1];
401 struct tun_pi pi = { .flags = 0 };
402 struct rte_mbuf *seg = mbuf;
406 /* stats.errs will be incremented */
407 if (rte_pktmbuf_pkt_len(mbuf) > max_size)
410 iovecs[0].iov_base = π
411 iovecs[0].iov_len = sizeof(pi);
412 for (j = 1; j <= mbuf->nb_segs; j++) {
413 iovecs[j].iov_len = rte_pktmbuf_data_len(seg);
415 rte_pktmbuf_mtod(seg, void *);
418 /* copy the tx frame data */
419 n = writev(txq->fd, iovecs, mbuf->nb_segs + 1);
424 num_tx_bytes += mbuf->pkt_len;
425 rte_pktmbuf_free(mbuf);
428 txq->stats.opackets += num_tx;
429 txq->stats.errs += nb_pkts - num_tx;
430 txq->stats.obytes += num_tx_bytes;
436 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
437 struct ifreq *ifr, int set)
439 short req_flags = ifr->ifr_flags;
440 int remote = !!pmd->remote_if_index;
443 * If there is a remote netdevice, apply ioctl on it, then apply it on
446 if (request == SIOCGIFFLAGS && !set) {
448 * Special case for getting flags. If set is given,
449 * then return the flags from the remote netdevice only.
450 * Otherwise return the flags from the tap netdevice.
456 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->remote_iface);
458 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->name);
461 /* fetch current flags to leave other flags untouched */
462 if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
465 ifr->ifr_flags |= req_flags;
467 ifr->ifr_flags &= ~req_flags;
471 remote = 0; /* don't loop */
474 /* Set remote MAC on the tap netdevice */
475 if (!remote && pmd->remote_if_index) {
476 request = SIOCSIFHWADDR;
484 RTE_LOG(WARNING, PMD, "%s: ioctl() called with wrong arg\n",
488 if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
495 RTE_LOG(ERR, PMD, "%s: ioctl(%lu) failed with error: %s\n",
496 ifr->ifr_name, request, strerror(errno));
501 tap_link_set_down(struct rte_eth_dev *dev)
503 struct pmd_internals *pmd = dev->data->dev_private;
504 struct ifreq ifr = { .ifr_flags = IFF_UP };
506 dev->data->dev_link.link_status = ETH_LINK_DOWN;
507 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
511 tap_link_set_up(struct rte_eth_dev *dev)
513 struct pmd_internals *pmd = dev->data->dev_private;
514 struct ifreq ifr = { .ifr_flags = IFF_UP };
516 dev->data->dev_link.link_status = ETH_LINK_UP;
517 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
521 tap_dev_start(struct rte_eth_dev *dev)
523 return tap_link_set_up(dev);
526 /* This function gets called when the current port gets stopped.
529 tap_dev_stop(struct rte_eth_dev *dev)
531 tap_link_set_down(dev);
535 tap_dev_configure(struct rte_eth_dev *dev __rte_unused)
541 tap_dev_speed_capa(void)
543 uint32_t speed = pmd_link.link_speed;
546 if (speed >= ETH_SPEED_NUM_10M)
547 capa |= ETH_LINK_SPEED_10M;
548 if (speed >= ETH_SPEED_NUM_100M)
549 capa |= ETH_LINK_SPEED_100M;
550 if (speed >= ETH_SPEED_NUM_1G)
551 capa |= ETH_LINK_SPEED_1G;
552 if (speed >= ETH_SPEED_NUM_5G)
553 capa |= ETH_LINK_SPEED_2_5G;
554 if (speed >= ETH_SPEED_NUM_5G)
555 capa |= ETH_LINK_SPEED_5G;
556 if (speed >= ETH_SPEED_NUM_10G)
557 capa |= ETH_LINK_SPEED_10G;
558 if (speed >= ETH_SPEED_NUM_20G)
559 capa |= ETH_LINK_SPEED_20G;
560 if (speed >= ETH_SPEED_NUM_25G)
561 capa |= ETH_LINK_SPEED_25G;
562 if (speed >= ETH_SPEED_NUM_40G)
563 capa |= ETH_LINK_SPEED_40G;
564 if (speed >= ETH_SPEED_NUM_50G)
565 capa |= ETH_LINK_SPEED_50G;
566 if (speed >= ETH_SPEED_NUM_56G)
567 capa |= ETH_LINK_SPEED_56G;
568 if (speed >= ETH_SPEED_NUM_100G)
569 capa |= ETH_LINK_SPEED_100G;
575 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
577 struct pmd_internals *internals = dev->data->dev_private;
579 dev_info->if_index = internals->if_index;
580 dev_info->max_mac_addrs = 1;
581 dev_info->max_rx_pktlen = (uint32_t)ETHER_MAX_VLAN_FRAME_LEN;
582 dev_info->max_rx_queues = internals->nb_queues;
583 dev_info->max_tx_queues = internals->nb_queues;
584 dev_info->min_rx_bufsize = 0;
585 dev_info->pci_dev = NULL;
586 dev_info->speed_capa = tap_dev_speed_capa();
590 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
592 unsigned int i, imax;
593 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
594 unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
595 unsigned long rx_nombuf = 0, ierrors = 0;
596 const struct pmd_internals *pmd = dev->data->dev_private;
598 imax = (pmd->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
599 pmd->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
601 for (i = 0; i < imax; i++) {
602 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
603 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
604 rx_total += tap_stats->q_ipackets[i];
605 rx_bytes_total += tap_stats->q_ibytes[i];
606 rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
607 ierrors += pmd->rxq[i].stats.ierrors;
609 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
610 tap_stats->q_errors[i] = pmd->txq[i].stats.errs;
611 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
612 tx_total += tap_stats->q_opackets[i];
613 tx_err_total += tap_stats->q_errors[i];
614 tx_bytes_total += tap_stats->q_obytes[i];
617 tap_stats->ipackets = rx_total;
618 tap_stats->ibytes = rx_bytes_total;
619 tap_stats->ierrors = ierrors;
620 tap_stats->rx_nombuf = rx_nombuf;
621 tap_stats->opackets = tx_total;
622 tap_stats->oerrors = tx_err_total;
623 tap_stats->obytes = tx_bytes_total;
627 tap_stats_reset(struct rte_eth_dev *dev)
630 struct pmd_internals *pmd = dev->data->dev_private;
632 for (i = 0; i < pmd->nb_queues; i++) {
633 pmd->rxq[i].stats.ipackets = 0;
634 pmd->rxq[i].stats.ibytes = 0;
635 pmd->rxq[i].stats.ierrors = 0;
636 pmd->rxq[i].stats.rx_nombuf = 0;
638 pmd->txq[i].stats.opackets = 0;
639 pmd->txq[i].stats.errs = 0;
640 pmd->txq[i].stats.obytes = 0;
645 tap_dev_close(struct rte_eth_dev *dev __rte_unused)
648 struct pmd_internals *internals = dev->data->dev_private;
650 tap_link_set_down(dev);
651 tap_flow_flush(dev, NULL);
652 tap_flow_implicit_flush(internals, NULL);
654 for (i = 0; i < internals->nb_queues; i++) {
655 if (internals->rxq[i].fd != -1)
656 close(internals->rxq[i].fd);
657 internals->rxq[i].fd = -1;
658 internals->txq[i].fd = -1;
663 tap_rx_queue_release(void *queue)
665 struct rx_queue *rxq = queue;
667 if (rxq && (rxq->fd > 0)) {
670 rte_pktmbuf_free(rxq->pool);
671 rte_free(rxq->iovecs);
678 tap_tx_queue_release(void *queue)
680 struct tx_queue *txq = queue;
682 if (txq && (txq->fd > 0)) {
689 tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
691 struct rte_eth_link *dev_link = &dev->data->dev_link;
692 struct pmd_internals *pmd = dev->data->dev_private;
693 struct ifreq ifr = { .ifr_flags = 0 };
695 if (pmd->remote_if_index) {
696 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 1);
697 if (!(ifr.ifr_flags & IFF_UP) ||
698 !(ifr.ifr_flags & IFF_RUNNING)) {
699 dev_link->link_status = ETH_LINK_DOWN;
703 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0);
704 dev_link->link_status =
705 ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
712 tap_promisc_enable(struct rte_eth_dev *dev)
714 struct pmd_internals *pmd = dev->data->dev_private;
715 struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
717 dev->data->promiscuous = 1;
718 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
719 if (pmd->remote_if_index)
720 tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
724 tap_promisc_disable(struct rte_eth_dev *dev)
726 struct pmd_internals *pmd = dev->data->dev_private;
727 struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
729 dev->data->promiscuous = 0;
730 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
731 if (pmd->remote_if_index)
732 tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
736 tap_allmulti_enable(struct rte_eth_dev *dev)
738 struct pmd_internals *pmd = dev->data->dev_private;
739 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
741 dev->data->all_multicast = 1;
742 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
743 if (pmd->remote_if_index)
744 tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
748 tap_allmulti_disable(struct rte_eth_dev *dev)
750 struct pmd_internals *pmd = dev->data->dev_private;
751 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
753 dev->data->all_multicast = 0;
754 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
755 if (pmd->remote_if_index)
756 tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
761 tap_mac_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
763 struct pmd_internals *pmd = dev->data->dev_private;
766 if (is_zero_ether_addr(mac_addr)) {
767 RTE_LOG(ERR, PMD, "%s: can't set an empty MAC address\n",
772 ifr.ifr_hwaddr.sa_family = AF_LOCAL;
773 rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETHER_ADDR_LEN);
774 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1) < 0)
776 rte_memcpy(&pmd->eth_addr, mac_addr, ETHER_ADDR_LEN);
780 tap_setup_queue(struct rte_eth_dev *dev,
781 struct pmd_internals *internals,
784 struct pmd_internals *pmd = dev->data->dev_private;
785 struct rx_queue *rx = &internals->rxq[qid];
786 struct tx_queue *tx = &internals->txq[qid];
793 RTE_LOG(INFO, PMD, "Add queue to TAP %s for qid %d\n",
795 fd = tun_alloc(pmd, qid);
797 RTE_LOG(ERR, PMD, "tun_alloc(%s, %d) failed\n",
804 ifr.ifr_mtu = dev->data->mtu;
805 if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1) < 0) {
815 tx->mtu = &dev->data->mtu;
816 rx->rxmode = &dev->data->dev_conf.rxmode;
822 rx_setup_queue(struct rte_eth_dev *dev,
823 struct pmd_internals *internals,
826 dev->data->rx_queues[qid] = &internals->rxq[qid];
828 return tap_setup_queue(dev, internals, qid);
832 tx_setup_queue(struct rte_eth_dev *dev,
833 struct pmd_internals *internals,
836 dev->data->tx_queues[qid] = &internals->txq[qid];
838 return tap_setup_queue(dev, internals, qid);
842 tap_rx_queue_setup(struct rte_eth_dev *dev,
843 uint16_t rx_queue_id,
845 unsigned int socket_id,
846 const struct rte_eth_rxconf *rx_conf __rte_unused,
847 struct rte_mempool *mp)
849 struct pmd_internals *internals = dev->data->dev_private;
850 struct rx_queue *rxq = &internals->rxq[rx_queue_id];
851 struct rte_mbuf **tmp = &rxq->pool;
852 struct iovec (*iovecs)[nb_rx_desc + 1];
853 int data_off = RTE_PKTMBUF_HEADROOM;
859 if ((rx_queue_id >= internals->nb_queues) || !mp) {
860 RTE_LOG(WARNING, PMD,
861 "nb_queues %d too small or mempool NULL\n",
862 internals->nb_queues);
867 rxq->trigger_seen = 1; /* force initial burst */
868 rxq->in_port = dev->data->port_id;
869 rxq->nb_rx_desc = nb_rx_desc;
870 iovecs = rte_zmalloc_socket(dev->data->name, sizeof(*iovecs), 0,
873 RTE_LOG(WARNING, PMD,
874 "%s: Couldn't allocate %d RX descriptors\n",
875 dev->data->name, nb_rx_desc);
878 rxq->iovecs = iovecs;
880 /* Now get the space available for data in the mbuf */
881 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
882 RTE_PKTMBUF_HEADROOM);
884 if (buf_size < ETH_FRAME_LEN) {
885 RTE_LOG(WARNING, PMD,
886 "%s: %d bytes will not fit in mbuf (%d bytes)\n",
887 dev->data->name, ETH_FRAME_LEN, buf_size);
892 fd = rx_setup_queue(dev, internals, rx_queue_id);
898 (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
899 (*rxq->iovecs)[0].iov_base = &rxq->pi;
901 for (i = 1; i <= nb_rx_desc; i++) {
902 *tmp = rte_pktmbuf_alloc(rxq->mp);
904 RTE_LOG(WARNING, PMD,
905 "%s: couldn't allocate memory for queue %d\n",
906 dev->data->name, rx_queue_id);
910 (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
911 (*rxq->iovecs)[i].iov_base =
912 (char *)(*tmp)->buf_addr + data_off;
917 RTE_LOG(DEBUG, PMD, " RX TAP device name %s, qid %d on fd %d\n",
918 internals->name, rx_queue_id, internals->rxq[rx_queue_id].fd);
923 rte_pktmbuf_free(rxq->pool);
925 rte_free(rxq->iovecs);
931 tap_tx_queue_setup(struct rte_eth_dev *dev,
932 uint16_t tx_queue_id,
933 uint16_t nb_tx_desc __rte_unused,
934 unsigned int socket_id __rte_unused,
935 const struct rte_eth_txconf *tx_conf __rte_unused)
937 struct pmd_internals *internals = dev->data->dev_private;
940 if (tx_queue_id >= internals->nb_queues)
943 ret = tx_setup_queue(dev, internals, tx_queue_id);
947 RTE_LOG(DEBUG, PMD, " TX TAP device name %s, qid %d on fd %d\n",
948 internals->name, tx_queue_id, internals->txq[tx_queue_id].fd);
954 tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
956 struct pmd_internals *pmd = dev->data->dev_private;
957 struct ifreq ifr = { .ifr_mtu = mtu };
960 err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1);
962 dev->data->mtu = mtu;
968 tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
969 struct ether_addr *mc_addr_set __rte_unused,
970 uint32_t nb_mc_addr __rte_unused)
973 * Nothing to do actually: the tap has no filtering whatsoever, every
974 * packet is received.
979 static const uint32_t*
980 tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
982 static const uint32_t ptypes[] = {
983 RTE_PTYPE_INNER_L2_ETHER,
984 RTE_PTYPE_INNER_L2_ETHER_VLAN,
985 RTE_PTYPE_INNER_L2_ETHER_QINQ,
986 RTE_PTYPE_INNER_L3_IPV4,
987 RTE_PTYPE_INNER_L3_IPV4_EXT,
988 RTE_PTYPE_INNER_L3_IPV6,
989 RTE_PTYPE_INNER_L3_IPV6_EXT,
990 RTE_PTYPE_INNER_L4_FRAG,
991 RTE_PTYPE_INNER_L4_UDP,
992 RTE_PTYPE_INNER_L4_TCP,
993 RTE_PTYPE_INNER_L4_SCTP,
995 RTE_PTYPE_L2_ETHER_VLAN,
996 RTE_PTYPE_L2_ETHER_QINQ,
998 RTE_PTYPE_L3_IPV4_EXT,
999 RTE_PTYPE_L3_IPV6_EXT,
1011 tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1012 struct rte_eth_fc_conf *fc_conf)
1014 fc_conf->mode = RTE_FC_NONE;
1019 tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1020 struct rte_eth_fc_conf *fc_conf)
1022 if (fc_conf->mode != RTE_FC_NONE)
1027 static const struct eth_dev_ops ops = {
1028 .dev_start = tap_dev_start,
1029 .dev_stop = tap_dev_stop,
1030 .dev_close = tap_dev_close,
1031 .dev_configure = tap_dev_configure,
1032 .dev_infos_get = tap_dev_info,
1033 .rx_queue_setup = tap_rx_queue_setup,
1034 .tx_queue_setup = tap_tx_queue_setup,
1035 .rx_queue_release = tap_rx_queue_release,
1036 .tx_queue_release = tap_tx_queue_release,
1037 .flow_ctrl_get = tap_flow_ctrl_get,
1038 .flow_ctrl_set = tap_flow_ctrl_set,
1039 .link_update = tap_link_update,
1040 .dev_set_link_up = tap_link_set_up,
1041 .dev_set_link_down = tap_link_set_down,
1042 .promiscuous_enable = tap_promisc_enable,
1043 .promiscuous_disable = tap_promisc_disable,
1044 .allmulticast_enable = tap_allmulti_enable,
1045 .allmulticast_disable = tap_allmulti_disable,
1046 .mac_addr_set = tap_mac_set,
1047 .mtu_set = tap_mtu_set,
1048 .set_mc_addr_list = tap_set_mc_addr_list,
1049 .stats_get = tap_stats_get,
1050 .stats_reset = tap_stats_reset,
1051 .dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1052 .filter_ctrl = tap_dev_filter_ctrl,
1056 tap_kernel_support(struct pmd_internals *pmd)
1058 struct utsname utsname;
1061 if (uname(&utsname) == -1 ||
1062 sscanf(utsname.release, "%d.%d.%d",
1063 &ver[0], &ver[1], &ver[2]) != 3)
1065 if (KERNEL_VERSION(ver[0], ver[1], ver[2]) >= FLOWER_KERNEL_VERSION)
1066 pmd->flower_support = 1;
1067 if (KERNEL_VERSION(ver[0], ver[1], ver[2]) >=
1068 FLOWER_VLAN_KERNEL_VERSION)
1069 pmd->flower_vlan_support = 1;
1074 eth_dev_tap_create(const char *name, char *tap_name, char *remote_iface)
1076 int numa_node = rte_socket_id();
1077 struct rte_eth_dev *dev = NULL;
1078 struct pmd_internals *pmd = NULL;
1079 struct rte_eth_dev_data *data = NULL;
1082 RTE_LOG(DEBUG, PMD, " TAP device on numa %u\n", rte_socket_id());
1084 data = rte_zmalloc_socket(tap_name, sizeof(*data), 0, numa_node);
1086 RTE_LOG(ERR, PMD, "TAP Failed to allocate data\n");
1090 pmd = rte_zmalloc_socket(tap_name, sizeof(*pmd), 0, numa_node);
1092 RTE_LOG(ERR, PMD, "TAP Unable to allocate internal struct\n");
1096 /* name in allocation and data->name must be consistent */
1097 snprintf(data->name, sizeof(data->name), "%s", name);
1098 dev = rte_eth_dev_allocate(name);
1100 RTE_LOG(ERR, PMD, "TAP Unable to allocate device struct\n");
1104 snprintf(pmd->name, sizeof(pmd->name), "%s", tap_name);
1106 pmd->nb_queues = RTE_PMD_TAP_MAX_QUEUES;
1108 pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1109 if (pmd->ioctl_sock == -1) {
1111 "TAP Unable to get a socket for management: %s\n",
1116 /* Setup some default values */
1117 data->dev_private = pmd;
1118 data->port_id = dev->data->port_id;
1119 data->mtu = dev->data->mtu;
1120 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
1121 data->kdrv = RTE_KDRV_NONE;
1122 data->drv_name = pmd_tap_drv.driver.name;
1123 data->numa_node = numa_node;
1125 data->dev_link = pmd_link;
1126 data->mac_addrs = &pmd->eth_addr;
1127 data->nb_rx_queues = pmd->nb_queues;
1128 data->nb_tx_queues = pmd->nb_queues;
1131 dev->dev_ops = &ops;
1133 dev->rx_pkt_burst = pmd_rx_burst;
1134 dev->tx_pkt_burst = pmd_tx_burst;
1136 /* Presetup the fds to -1 as being not valid */
1137 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1138 pmd->rxq[i].fd = -1;
1139 pmd->txq[i].fd = -1;
1142 tap_kernel_support(pmd);
1143 if (!pmd->flower_support)
1145 LIST_INIT(&pmd->flows);
1147 * If no netlink socket can be created, then it will fail when
1148 * creating/destroying flow rules.
1150 pmd->nlsk_fd = nl_init();
1151 if (strlen(remote_iface)) {
1152 pmd->remote_if_index = if_nametoindex(remote_iface);
1153 snprintf(pmd->remote_iface, RTE_ETH_NAME_MAX_LEN,
1154 "%s", remote_iface);
1155 if (!pmd->remote_if_index)
1156 RTE_LOG(ERR, PMD, "Could not find %s ifindex: "
1157 "remote interface will remain unconfigured\n",
1164 RTE_LOG(DEBUG, PMD, "TAP Unable to initialize %s\n", name);
1169 rte_eth_dev_release_port(dev);
1175 set_interface_name(const char *key __rte_unused,
1179 char *name = (char *)extra_args;
1182 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s", value);
1184 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s%d",
1185 DEFAULT_TAP_NAME, (tap_unit - 1));
1191 set_interface_speed(const char *key __rte_unused,
1195 *(int *)extra_args = (value) ? atoi(value) : ETH_SPEED_NUM_10G;
1201 set_remote_iface(const char *key __rte_unused,
1205 char *name = (char *)extra_args;
1208 snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s", value);
1213 /* Open a TAP interface device.
1216 rte_pmd_tap_probe(const char *name, const char *params)
1219 struct rte_kvargs *kvlist = NULL;
1221 char tap_name[RTE_ETH_NAME_MAX_LEN];
1222 char remote_iface[RTE_ETH_NAME_MAX_LEN];
1224 speed = ETH_SPEED_NUM_10G;
1225 snprintf(tap_name, sizeof(tap_name), "%s%d",
1226 DEFAULT_TAP_NAME, tap_unit++);
1227 memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
1229 if (params && (params[0] != '\0')) {
1230 RTE_LOG(DEBUG, PMD, "paramaters (%s)\n", params);
1232 kvlist = rte_kvargs_parse(params, valid_arguments);
1234 if (rte_kvargs_count(kvlist, ETH_TAP_SPEED_ARG) == 1) {
1235 ret = rte_kvargs_process(kvlist,
1237 &set_interface_speed,
1243 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
1244 ret = rte_kvargs_process(kvlist,
1246 &set_interface_name,
1252 if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
1253 ret = rte_kvargs_process(kvlist,
1262 pmd_link.link_speed = speed;
1264 RTE_LOG(NOTICE, PMD, "Initializing pmd_tap for %s as %s\n",
1267 ret = eth_dev_tap_create(name, tap_name, remote_iface);
1271 RTE_LOG(ERR, PMD, "Failed to create pmd for %s as %s\n",
1273 tap_unit--; /* Restore the unit number */
1275 rte_kvargs_free(kvlist);
1280 /* detach a TAP device.
1283 rte_pmd_tap_remove(const char *name)
1285 struct rte_eth_dev *eth_dev = NULL;
1286 struct pmd_internals *internals;
1289 RTE_LOG(DEBUG, PMD, "Closing TUN/TAP Ethernet device on numa %u\n",
1292 /* find the ethdev entry */
1293 eth_dev = rte_eth_dev_allocated(name);
1297 internals = eth_dev->data->dev_private;
1298 if (internals->flower_support && internals->nlsk_fd) {
1299 tap_flow_flush(eth_dev, NULL);
1300 tap_flow_implicit_flush(internals, NULL);
1301 nl_final(internals->nlsk_fd);
1303 for (i = 0; i < internals->nb_queues; i++)
1304 if (internals->rxq[i].fd != -1)
1305 close(internals->rxq[i].fd);
1307 close(internals->ioctl_sock);
1308 rte_free(eth_dev->data->dev_private);
1309 rte_free(eth_dev->data);
1311 rte_eth_dev_release_port(eth_dev);
1316 static struct rte_vdev_driver pmd_tap_drv = {
1317 .probe = rte_pmd_tap_probe,
1318 .remove = rte_pmd_tap_remove,
1320 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
1321 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
1322 RTE_PMD_REGISTER_PARAM_STRING(net_tap, "iface=<string>,speed=N");