4 * Copyright(c) 2016-2017 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_byteorder.h>
37 #include <rte_common.h>
39 #include <rte_ethdev.h>
40 #include <rte_ethdev_vdev.h>
41 #include <rte_malloc.h>
43 #include <rte_kvargs.h>
45 #include <rte_debug.h>
48 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <sys/utsname.h>
59 #include <arpa/inet.h>
61 #include <linux/if_tun.h>
62 #include <linux/if_ether.h>
63 #include <linux/version.h>
66 #include <rte_eth_tap.h>
68 #include <tap_netlink.h>
69 #include <tap_tcmsgs.h>
71 /* Linux based path to the TUN device */
72 #define TUN_TAP_DEV_PATH "/dev/net/tun"
73 #define DEFAULT_TAP_NAME "dtap"
75 #define ETH_TAP_IFACE_ARG "iface"
76 #define ETH_TAP_SPEED_ARG "speed"
77 #define ETH_TAP_REMOTE_ARG "remote"
78 #define ETH_TAP_MAC_ARG "mac"
79 #define ETH_TAP_MAC_FIXED "fixed"
81 #define FLOWER_KERNEL_VERSION KERNEL_VERSION(4, 2, 0)
82 #define FLOWER_VLAN_KERNEL_VERSION KERNEL_VERSION(4, 9, 0)
84 static struct rte_vdev_driver pmd_tap_drv;
86 static const char *valid_arguments[] = {
96 static volatile uint32_t tap_trigger; /* Rx trigger */
98 static struct rte_eth_link pmd_link = {
99 .link_speed = ETH_SPEED_NUM_10G,
100 .link_duplex = ETH_LINK_FULL_DUPLEX,
101 .link_status = ETH_LINK_DOWN,
102 .link_autoneg = ETH_LINK_SPEED_AUTONEG
106 tap_trigger_cb(int sig __rte_unused)
108 /* Valid trigger values are nonzero */
109 tap_trigger = (tap_trigger + 1) | 0x80000000;
112 /* Specifies on what netdevices the ioctl should be applied */
119 static int tap_intr_handle_set(struct rte_eth_dev *dev, int set);
121 /* Tun/Tap allocation routine
123 * name is the number of the interface to use, unless NULL to take the host
127 tun_alloc(struct pmd_internals *pmd)
130 #ifdef IFF_MULTI_QUEUE
131 unsigned int features;
135 memset(&ifr, 0, sizeof(struct ifreq));
138 * Do not set IFF_NO_PI as packet information header will be needed
139 * to check if a received packet has been truncated.
141 ifr.ifr_flags = IFF_TAP;
142 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", pmd->name);
144 RTE_LOG(DEBUG, PMD, "ifr_name '%s'\n", ifr.ifr_name);
146 fd = open(TUN_TAP_DEV_PATH, O_RDWR);
148 RTE_LOG(ERR, PMD, "Unable to create TAP interface\n");
152 #ifdef IFF_MULTI_QUEUE
153 /* Grab the TUN features to verify we can work multi-queue */
154 if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
155 RTE_LOG(ERR, PMD, "TAP unable to get TUN/TAP features\n");
158 RTE_LOG(DEBUG, PMD, " TAP Features %08x\n", features);
160 if (features & IFF_MULTI_QUEUE) {
161 RTE_LOG(DEBUG, PMD, " Multi-queue support for %d queues\n",
162 RTE_PMD_TAP_MAX_QUEUES);
163 ifr.ifr_flags |= IFF_MULTI_QUEUE;
167 ifr.ifr_flags |= IFF_ONE_QUEUE;
168 RTE_LOG(DEBUG, PMD, " Single queue only support\n");
171 /* Set the TUN/TAP configuration and set the name if needed */
172 if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
173 RTE_LOG(WARNING, PMD,
174 "Unable to set TUNSETIFF for %s\n",
180 /* Always set the file descriptor to non-blocking */
181 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
182 RTE_LOG(WARNING, PMD,
183 "Unable to set %s to nonblocking\n",
185 perror("F_SETFL, NONBLOCK");
189 /* Set up trigger to optimize empty Rx bursts */
193 int flags = fcntl(fd, F_GETFL);
195 if (flags == -1 || sigaction(SIGIO, NULL, &sa) == -1)
197 if (sa.sa_handler != tap_trigger_cb) {
199 * Make sure SIGIO is not already taken. This is done
200 * as late as possible to leave the application a
201 * chance to set up its own signal handler first.
203 if (sa.sa_handler != SIG_IGN &&
204 sa.sa_handler != SIG_DFL) {
208 sa = (struct sigaction){
209 .sa_flags = SA_RESTART,
210 .sa_handler = tap_trigger_cb,
212 if (sigaction(SIGIO, &sa, NULL) == -1)
215 /* Enable SIGIO on file descriptor */
216 fcntl(fd, F_SETFL, flags | O_ASYNC);
217 fcntl(fd, F_SETOWN, getpid());
220 /* Disable trigger globally in case of error */
222 RTE_LOG(WARNING, PMD, "Rx trigger disabled: %s\n",
235 tap_verify_csum(struct rte_mbuf *mbuf)
237 uint32_t l2 = mbuf->packet_type & RTE_PTYPE_L2_MASK;
238 uint32_t l3 = mbuf->packet_type & RTE_PTYPE_L3_MASK;
239 uint32_t l4 = mbuf->packet_type & RTE_PTYPE_L4_MASK;
240 unsigned int l2_len = sizeof(struct ether_hdr);
246 if (l2 == RTE_PTYPE_L2_ETHER_VLAN)
248 else if (l2 == RTE_PTYPE_L2_ETHER_QINQ)
250 /* Don't verify checksum for packets with discontinuous L2 header */
251 if (unlikely(l2_len + sizeof(struct ipv4_hdr) >
252 rte_pktmbuf_data_len(mbuf)))
254 l3_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len);
255 if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
256 struct ipv4_hdr *iph = l3_hdr;
258 /* ihl contains the number of 4-byte words in the header */
259 l3_len = 4 * (iph->version_ihl & 0xf);
260 if (unlikely(l2_len + l3_len > rte_pktmbuf_data_len(mbuf)))
263 cksum = ~rte_raw_cksum(iph, l3_len);
264 mbuf->ol_flags |= cksum ?
265 PKT_RX_IP_CKSUM_BAD :
266 PKT_RX_IP_CKSUM_GOOD;
267 } else if (l3 == RTE_PTYPE_L3_IPV6) {
268 l3_len = sizeof(struct ipv6_hdr);
270 /* IPv6 extensions are not supported */
273 if (l4 == RTE_PTYPE_L4_UDP || l4 == RTE_PTYPE_L4_TCP) {
274 l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len);
275 /* Don't verify checksum for multi-segment packets. */
276 if (mbuf->nb_segs > 1)
278 if (l3 == RTE_PTYPE_L3_IPV4)
279 cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
280 else if (l3 == RTE_PTYPE_L3_IPV6)
281 cksum = ~rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
282 mbuf->ol_flags |= cksum ?
283 PKT_RX_L4_CKSUM_BAD :
284 PKT_RX_L4_CKSUM_GOOD;
288 /* Callback to handle the rx burst of packets to the correct interface and
289 * file descriptor(s) in a multi-queue setup.
292 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
294 struct rx_queue *rxq = queue;
296 unsigned long num_rx_bytes = 0;
297 uint32_t trigger = tap_trigger;
299 if (trigger == rxq->trigger_seen)
302 rxq->trigger_seen = trigger;
303 rte_compiler_barrier();
304 for (num_rx = 0; num_rx < nb_pkts; ) {
305 struct rte_mbuf *mbuf = rxq->pool;
306 struct rte_mbuf *seg = NULL;
307 struct rte_mbuf *new_tail = NULL;
308 uint16_t data_off = rte_pktmbuf_headroom(mbuf);
311 len = readv(rxq->fd, *rxq->iovecs,
312 1 + (rxq->rxmode->enable_scatter ?
313 rxq->nb_rx_desc : 1));
314 if (len < (int)sizeof(struct tun_pi))
317 /* Packet couldn't fit in the provided mbuf */
318 if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) {
319 rxq->stats.ierrors++;
323 len -= sizeof(struct tun_pi);
326 mbuf->port = rxq->in_port;
328 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
330 if (unlikely(!buf)) {
331 rxq->stats.rx_nombuf++;
332 /* No new buf has been allocated: do nothing */
333 if (!new_tail || !seg)
337 rte_pktmbuf_free(mbuf);
341 seg = seg ? seg->next : mbuf;
342 if (rxq->pool == mbuf)
345 new_tail->next = buf;
347 new_tail->next = seg->next;
349 /* iovecs[0] is reserved for packet info (pi) */
350 (*rxq->iovecs)[mbuf->nb_segs].iov_len =
351 buf->buf_len - data_off;
352 (*rxq->iovecs)[mbuf->nb_segs].iov_base =
353 (char *)buf->buf_addr + data_off;
355 seg->data_len = RTE_MIN(seg->buf_len - data_off, len);
356 seg->data_off = data_off;
358 len -= seg->data_len;
362 /* First segment has headroom, not the others */
366 mbuf->packet_type = rte_net_get_ptype(mbuf, NULL,
368 if (rxq->rxmode->hw_ip_checksum)
369 tap_verify_csum(mbuf);
371 /* account for the receive frame */
372 bufs[num_rx++] = mbuf;
373 num_rx_bytes += mbuf->pkt_len;
376 rxq->stats.ipackets += num_rx;
377 rxq->stats.ibytes += num_rx_bytes;
383 tap_tx_offload(char *packet, uint64_t ol_flags, unsigned int l2_len,
386 void *l3_hdr = packet + l2_len;
388 if (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4)) {
389 struct ipv4_hdr *iph = l3_hdr;
392 iph->hdr_checksum = 0;
393 cksum = rte_raw_cksum(iph, l3_len);
394 iph->hdr_checksum = (cksum == 0xffff) ? cksum : ~cksum;
396 if (ol_flags & PKT_TX_L4_MASK) {
402 l4_hdr = packet + l2_len + l3_len;
403 if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM)
404 l4_cksum = &((struct udp_hdr *)l4_hdr)->dgram_cksum;
405 else if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM)
406 l4_cksum = &((struct tcp_hdr *)l4_hdr)->cksum;
410 if (ol_flags & PKT_TX_IPV4) {
411 struct ipv4_hdr *iph = l3_hdr;
413 l4_len = rte_be_to_cpu_16(iph->total_length) - l3_len;
414 cksum = rte_ipv4_phdr_cksum(l3_hdr, 0);
416 struct ipv6_hdr *ip6h = l3_hdr;
418 /* payload_len does not include ext headers */
419 l4_len = rte_be_to_cpu_16(ip6h->payload_len) -
420 l3_len + sizeof(struct ipv6_hdr);
421 cksum = rte_ipv6_phdr_cksum(l3_hdr, 0);
423 cksum += rte_raw_cksum(l4_hdr, l4_len);
424 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
425 cksum = (~cksum) & 0xffff;
432 /* Callback to handle sending packets from the tap interface
435 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
437 struct tx_queue *txq = queue;
439 unsigned long num_tx_bytes = 0;
443 if (unlikely(nb_pkts == 0))
446 max_size = *txq->mtu + (ETHER_HDR_LEN + ETHER_CRC_LEN + 4);
447 for (i = 0; i < nb_pkts; i++) {
448 struct rte_mbuf *mbuf = bufs[num_tx];
449 struct iovec iovecs[mbuf->nb_segs + 1];
450 struct tun_pi pi = { .flags = 0 };
451 struct rte_mbuf *seg = mbuf;
452 char m_copy[mbuf->data_len];
456 /* stats.errs will be incremented */
457 if (rte_pktmbuf_pkt_len(mbuf) > max_size)
460 iovecs[0].iov_base = π
461 iovecs[0].iov_len = sizeof(pi);
462 for (j = 1; j <= mbuf->nb_segs; j++) {
463 iovecs[j].iov_len = rte_pktmbuf_data_len(seg);
465 rte_pktmbuf_mtod(seg, void *);
468 if (mbuf->ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4) ||
469 (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM ||
470 (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM) {
471 /* Support only packets with all data in the same seg */
472 if (mbuf->nb_segs > 1)
474 /* To change checksums, work on a copy of data. */
475 rte_memcpy(m_copy, rte_pktmbuf_mtod(mbuf, void *),
476 rte_pktmbuf_data_len(mbuf));
477 tap_tx_offload(m_copy, mbuf->ol_flags,
478 mbuf->l2_len, mbuf->l3_len);
479 iovecs[1].iov_base = m_copy;
481 /* copy the tx frame data */
482 n = writev(txq->fd, iovecs, mbuf->nb_segs + 1);
487 num_tx_bytes += mbuf->pkt_len;
488 rte_pktmbuf_free(mbuf);
491 txq->stats.opackets += num_tx;
492 txq->stats.errs += nb_pkts - num_tx;
493 txq->stats.obytes += num_tx_bytes;
499 tap_ioctl_req2str(unsigned long request)
503 return "SIOCSIFFLAGS";
505 return "SIOCGIFFLAGS";
507 return "SIOCGIFHWADDR";
509 return "SIOCSIFHWADDR";
517 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
518 struct ifreq *ifr, int set, enum ioctl_mode mode)
520 short req_flags = ifr->ifr_flags;
521 int remote = pmd->remote_if_index &&
522 (mode == REMOTE_ONLY || mode == LOCAL_AND_REMOTE);
524 if (!pmd->remote_if_index && mode == REMOTE_ONLY)
527 * If there is a remote netdevice, apply ioctl on it, then apply it on
532 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->remote_iface);
533 else if (mode == LOCAL_ONLY || mode == LOCAL_AND_REMOTE)
534 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->name);
537 /* fetch current flags to leave other flags untouched */
538 if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
541 ifr->ifr_flags |= req_flags;
543 ifr->ifr_flags &= ~req_flags;
551 RTE_ASSERT(!"unsupported request type: must not happen");
553 if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
555 if (remote-- && mode == LOCAL_AND_REMOTE)
560 RTE_LOG(DEBUG, PMD, "%s: %s(%s) failed: %s(%d)\n", ifr->ifr_name,
561 __func__, tap_ioctl_req2str(request), strerror(errno), errno);
566 tap_link_set_down(struct rte_eth_dev *dev)
568 struct pmd_internals *pmd = dev->data->dev_private;
569 struct ifreq ifr = { .ifr_flags = IFF_UP };
571 dev->data->dev_link.link_status = ETH_LINK_DOWN;
572 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_ONLY);
576 tap_link_set_up(struct rte_eth_dev *dev)
578 struct pmd_internals *pmd = dev->data->dev_private;
579 struct ifreq ifr = { .ifr_flags = IFF_UP };
581 dev->data->dev_link.link_status = ETH_LINK_UP;
582 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
586 tap_dev_start(struct rte_eth_dev *dev)
590 err = tap_intr_handle_set(dev, 1);
593 return tap_link_set_up(dev);
596 /* This function gets called when the current port gets stopped.
599 tap_dev_stop(struct rte_eth_dev *dev)
601 tap_intr_handle_set(dev, 0);
602 tap_link_set_down(dev);
606 tap_dev_configure(struct rte_eth_dev *dev)
608 if (dev->data->nb_rx_queues > RTE_PMD_TAP_MAX_QUEUES) {
610 "%s: number of rx queues %d exceeds max num of queues %d\n",
612 dev->data->nb_rx_queues,
613 RTE_PMD_TAP_MAX_QUEUES);
616 if (dev->data->nb_tx_queues > RTE_PMD_TAP_MAX_QUEUES) {
618 "%s: number of tx queues %d exceeds max num of queues %d\n",
620 dev->data->nb_tx_queues,
621 RTE_PMD_TAP_MAX_QUEUES);
625 RTE_LOG(INFO, PMD, "%s: %p: TX configured queues number: %u\n",
626 dev->device->name, (void *)dev, dev->data->nb_tx_queues);
628 RTE_LOG(INFO, PMD, "%s: %p: RX configured queues number: %u\n",
629 dev->device->name, (void *)dev, dev->data->nb_rx_queues);
635 tap_dev_speed_capa(void)
637 uint32_t speed = pmd_link.link_speed;
640 if (speed >= ETH_SPEED_NUM_10M)
641 capa |= ETH_LINK_SPEED_10M;
642 if (speed >= ETH_SPEED_NUM_100M)
643 capa |= ETH_LINK_SPEED_100M;
644 if (speed >= ETH_SPEED_NUM_1G)
645 capa |= ETH_LINK_SPEED_1G;
646 if (speed >= ETH_SPEED_NUM_5G)
647 capa |= ETH_LINK_SPEED_2_5G;
648 if (speed >= ETH_SPEED_NUM_5G)
649 capa |= ETH_LINK_SPEED_5G;
650 if (speed >= ETH_SPEED_NUM_10G)
651 capa |= ETH_LINK_SPEED_10G;
652 if (speed >= ETH_SPEED_NUM_20G)
653 capa |= ETH_LINK_SPEED_20G;
654 if (speed >= ETH_SPEED_NUM_25G)
655 capa |= ETH_LINK_SPEED_25G;
656 if (speed >= ETH_SPEED_NUM_40G)
657 capa |= ETH_LINK_SPEED_40G;
658 if (speed >= ETH_SPEED_NUM_50G)
659 capa |= ETH_LINK_SPEED_50G;
660 if (speed >= ETH_SPEED_NUM_56G)
661 capa |= ETH_LINK_SPEED_56G;
662 if (speed >= ETH_SPEED_NUM_100G)
663 capa |= ETH_LINK_SPEED_100G;
669 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
671 struct pmd_internals *internals = dev->data->dev_private;
673 dev_info->if_index = internals->if_index;
674 dev_info->max_mac_addrs = 1;
675 dev_info->max_rx_pktlen = (uint32_t)ETHER_MAX_VLAN_FRAME_LEN;
676 dev_info->max_rx_queues = RTE_PMD_TAP_MAX_QUEUES;
677 dev_info->max_tx_queues = RTE_PMD_TAP_MAX_QUEUES;
678 dev_info->min_rx_bufsize = 0;
679 dev_info->pci_dev = NULL;
680 dev_info->speed_capa = tap_dev_speed_capa();
681 dev_info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
682 DEV_RX_OFFLOAD_UDP_CKSUM |
683 DEV_RX_OFFLOAD_TCP_CKSUM);
684 dev_info->tx_offload_capa =
685 (DEV_TX_OFFLOAD_IPV4_CKSUM |
686 DEV_TX_OFFLOAD_UDP_CKSUM |
687 DEV_TX_OFFLOAD_TCP_CKSUM);
691 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
693 unsigned int i, imax;
694 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
695 unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
696 unsigned long rx_nombuf = 0, ierrors = 0;
697 const struct pmd_internals *pmd = dev->data->dev_private;
699 /* rx queue statistics */
700 imax = (dev->data->nb_rx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
701 dev->data->nb_rx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
702 for (i = 0; i < imax; i++) {
703 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
704 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
705 rx_total += tap_stats->q_ipackets[i];
706 rx_bytes_total += tap_stats->q_ibytes[i];
707 rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
708 ierrors += pmd->rxq[i].stats.ierrors;
711 /* tx queue statistics */
712 imax = (dev->data->nb_tx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
713 dev->data->nb_tx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
715 for (i = 0; i < imax; i++) {
716 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
717 tap_stats->q_errors[i] = pmd->txq[i].stats.errs;
718 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
719 tx_total += tap_stats->q_opackets[i];
720 tx_err_total += tap_stats->q_errors[i];
721 tx_bytes_total += tap_stats->q_obytes[i];
724 tap_stats->ipackets = rx_total;
725 tap_stats->ibytes = rx_bytes_total;
726 tap_stats->ierrors = ierrors;
727 tap_stats->rx_nombuf = rx_nombuf;
728 tap_stats->opackets = tx_total;
729 tap_stats->oerrors = tx_err_total;
730 tap_stats->obytes = tx_bytes_total;
734 tap_stats_reset(struct rte_eth_dev *dev)
737 struct pmd_internals *pmd = dev->data->dev_private;
739 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
740 pmd->rxq[i].stats.ipackets = 0;
741 pmd->rxq[i].stats.ibytes = 0;
742 pmd->rxq[i].stats.ierrors = 0;
743 pmd->rxq[i].stats.rx_nombuf = 0;
745 pmd->txq[i].stats.opackets = 0;
746 pmd->txq[i].stats.errs = 0;
747 pmd->txq[i].stats.obytes = 0;
752 tap_dev_close(struct rte_eth_dev *dev)
755 struct pmd_internals *internals = dev->data->dev_private;
757 tap_link_set_down(dev);
758 tap_flow_flush(dev, NULL);
759 tap_flow_implicit_flush(internals, NULL);
761 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
762 if (internals->rxq[i].fd != -1) {
763 close(internals->rxq[i].fd);
764 internals->rxq[i].fd = -1;
766 if (internals->txq[i].fd != -1) {
767 close(internals->txq[i].fd);
768 internals->txq[i].fd = -1;
772 if (internals->remote_if_index) {
773 /* Restore initial remote state */
774 ioctl(internals->ioctl_sock, SIOCSIFFLAGS,
775 &internals->remote_initial_flags);
780 tap_rx_queue_release(void *queue)
782 struct rx_queue *rxq = queue;
784 if (rxq && (rxq->fd > 0)) {
787 rte_pktmbuf_free(rxq->pool);
788 rte_free(rxq->iovecs);
795 tap_tx_queue_release(void *queue)
797 struct tx_queue *txq = queue;
799 if (txq && (txq->fd > 0)) {
806 tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
808 struct rte_eth_link *dev_link = &dev->data->dev_link;
809 struct pmd_internals *pmd = dev->data->dev_private;
810 struct ifreq ifr = { .ifr_flags = 0 };
812 if (pmd->remote_if_index) {
813 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY);
814 if (!(ifr.ifr_flags & IFF_UP) ||
815 !(ifr.ifr_flags & IFF_RUNNING)) {
816 dev_link->link_status = ETH_LINK_DOWN;
820 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY);
821 dev_link->link_status =
822 ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
829 tap_promisc_enable(struct rte_eth_dev *dev)
831 struct pmd_internals *pmd = dev->data->dev_private;
832 struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
834 dev->data->promiscuous = 1;
835 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
836 if (pmd->remote_if_index && !pmd->flow_isolate)
837 tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
841 tap_promisc_disable(struct rte_eth_dev *dev)
843 struct pmd_internals *pmd = dev->data->dev_private;
844 struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
846 dev->data->promiscuous = 0;
847 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
848 if (pmd->remote_if_index && !pmd->flow_isolate)
849 tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
853 tap_allmulti_enable(struct rte_eth_dev *dev)
855 struct pmd_internals *pmd = dev->data->dev_private;
856 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
858 dev->data->all_multicast = 1;
859 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
860 if (pmd->remote_if_index && !pmd->flow_isolate)
861 tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
865 tap_allmulti_disable(struct rte_eth_dev *dev)
867 struct pmd_internals *pmd = dev->data->dev_private;
868 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
870 dev->data->all_multicast = 0;
871 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
872 if (pmd->remote_if_index && !pmd->flow_isolate)
873 tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
877 tap_mac_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
879 struct pmd_internals *pmd = dev->data->dev_private;
880 enum ioctl_mode mode = LOCAL_ONLY;
883 if (is_zero_ether_addr(mac_addr)) {
884 RTE_LOG(ERR, PMD, "%s: can't set an empty MAC address\n",
888 /* Check the actual current MAC address on the tap netdevice */
889 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
891 if (is_same_ether_addr((struct ether_addr *)&ifr.ifr_hwaddr.sa_data,
894 /* Check the current MAC address on the remote */
895 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0)
897 if (!is_same_ether_addr((struct ether_addr *)&ifr.ifr_hwaddr.sa_data,
899 mode = LOCAL_AND_REMOTE;
900 ifr.ifr_hwaddr.sa_family = AF_LOCAL;
901 rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETHER_ADDR_LEN);
902 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, mode) < 0)
904 rte_memcpy(&pmd->eth_addr, mac_addr, ETHER_ADDR_LEN);
905 if (pmd->remote_if_index && !pmd->flow_isolate) {
906 /* Replace MAC redirection rule after a MAC change */
907 if (tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC) < 0) {
909 "%s: Couldn't delete MAC redirection rule\n",
913 if (tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0)
915 "%s: Couldn't add MAC redirection rule\n",
921 tap_setup_queue(struct rte_eth_dev *dev,
922 struct pmd_internals *internals,
929 struct pmd_internals *pmd = dev->data->dev_private;
930 struct rx_queue *rx = &internals->rxq[qid];
931 struct tx_queue *tx = &internals->txq[qid];
943 /* fd for this queue already exists */
944 RTE_LOG(DEBUG, PMD, "%s: fd %d for %s queue qid %d exists\n",
945 pmd->name, *fd, dir, qid);
946 } else if (*other_fd != -1) {
947 /* Only other_fd exists. dup it */
948 *fd = dup(*other_fd);
951 RTE_LOG(ERR, PMD, "%s: dup() failed.\n",
955 RTE_LOG(DEBUG, PMD, "%s: dup fd %d for %s queue qid %d (%d)\n",
956 pmd->name, *other_fd, dir, qid, *fd);
958 /* Both RX and TX fds do not exist (equal -1). Create fd */
959 *fd = tun_alloc(pmd);
961 *fd = -1; /* restore original value */
962 RTE_LOG(ERR, PMD, "%s: tun_alloc() failed.\n",
966 RTE_LOG(DEBUG, PMD, "%s: add %s queue for qid %d fd %d\n",
967 pmd->name, dir, qid, *fd);
970 tx->mtu = &dev->data->mtu;
971 rx->rxmode = &dev->data->dev_conf.rxmode;
977 tap_rx_queue_setup(struct rte_eth_dev *dev,
978 uint16_t rx_queue_id,
980 unsigned int socket_id,
981 const struct rte_eth_rxconf *rx_conf __rte_unused,
982 struct rte_mempool *mp)
984 struct pmd_internals *internals = dev->data->dev_private;
985 struct rx_queue *rxq = &internals->rxq[rx_queue_id];
986 struct rte_mbuf **tmp = &rxq->pool;
987 long iov_max = sysconf(_SC_IOV_MAX);
988 uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1);
989 struct iovec (*iovecs)[nb_desc + 1];
990 int data_off = RTE_PKTMBUF_HEADROOM;
995 if (rx_queue_id >= dev->data->nb_rx_queues || !mp) {
996 RTE_LOG(WARNING, PMD,
997 "nb_rx_queues %d too small or mempool NULL\n",
998 dev->data->nb_rx_queues);
1003 rxq->trigger_seen = 1; /* force initial burst */
1004 rxq->in_port = dev->data->port_id;
1005 rxq->nb_rx_desc = nb_desc;
1006 iovecs = rte_zmalloc_socket(dev->device->name, sizeof(*iovecs), 0,
1009 RTE_LOG(WARNING, PMD,
1010 "%s: Couldn't allocate %d RX descriptors\n",
1011 dev->device->name, nb_desc);
1014 rxq->iovecs = iovecs;
1016 dev->data->rx_queues[rx_queue_id] = rxq;
1017 fd = tap_setup_queue(dev, internals, rx_queue_id, 1);
1023 (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
1024 (*rxq->iovecs)[0].iov_base = &rxq->pi;
1026 for (i = 1; i <= nb_desc; i++) {
1027 *tmp = rte_pktmbuf_alloc(rxq->mp);
1029 RTE_LOG(WARNING, PMD,
1030 "%s: couldn't allocate memory for queue %d\n",
1031 dev->device->name, rx_queue_id);
1035 (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
1036 (*rxq->iovecs)[i].iov_base =
1037 (char *)(*tmp)->buf_addr + data_off;
1039 tmp = &(*tmp)->next;
1042 RTE_LOG(DEBUG, PMD, " RX TAP device name %s, qid %d on fd %d\n",
1043 internals->name, rx_queue_id, internals->rxq[rx_queue_id].fd);
1048 rte_pktmbuf_free(rxq->pool);
1050 rte_free(rxq->iovecs);
1056 tap_tx_queue_setup(struct rte_eth_dev *dev,
1057 uint16_t tx_queue_id,
1058 uint16_t nb_tx_desc __rte_unused,
1059 unsigned int socket_id __rte_unused,
1060 const struct rte_eth_txconf *tx_conf __rte_unused)
1062 struct pmd_internals *internals = dev->data->dev_private;
1065 if (tx_queue_id >= dev->data->nb_tx_queues)
1068 dev->data->tx_queues[tx_queue_id] = &internals->txq[tx_queue_id];
1069 ret = tap_setup_queue(dev, internals, tx_queue_id, 0);
1073 RTE_LOG(DEBUG, PMD, " TX TAP device name %s, qid %d on fd %d\n",
1074 internals->name, tx_queue_id, internals->txq[tx_queue_id].fd);
1080 tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1082 struct pmd_internals *pmd = dev->data->dev_private;
1083 struct ifreq ifr = { .ifr_mtu = mtu };
1086 err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE);
1088 dev->data->mtu = mtu;
1094 tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
1095 struct ether_addr *mc_addr_set __rte_unused,
1096 uint32_t nb_mc_addr __rte_unused)
1099 * Nothing to do actually: the tap has no filtering whatsoever, every
1100 * packet is received.
1106 tap_nl_msg_handler(struct nlmsghdr *nh, void *arg)
1108 struct rte_eth_dev *dev = arg;
1109 struct pmd_internals *pmd = dev->data->dev_private;
1110 struct ifinfomsg *info = NLMSG_DATA(nh);
1112 if (nh->nlmsg_type != RTM_NEWLINK ||
1113 (info->ifi_index != pmd->if_index &&
1114 info->ifi_index != pmd->remote_if_index))
1116 return tap_link_update(dev, 0);
1120 tap_dev_intr_handler(void *cb_arg)
1122 struct rte_eth_dev *dev = cb_arg;
1123 struct pmd_internals *pmd = dev->data->dev_private;
1125 nl_recv(pmd->intr_handle.fd, tap_nl_msg_handler, dev);
1129 tap_intr_handle_set(struct rte_eth_dev *dev, int set)
1131 struct pmd_internals *pmd = dev->data->dev_private;
1133 /* In any case, disable interrupt if the conf is no longer there. */
1134 if (!dev->data->dev_conf.intr_conf.lsc) {
1135 if (pmd->intr_handle.fd != -1) {
1136 nl_final(pmd->intr_handle.fd);
1137 rte_intr_callback_unregister(&pmd->intr_handle,
1138 tap_dev_intr_handler, dev);
1143 pmd->intr_handle.fd = nl_init(RTMGRP_LINK);
1144 if (unlikely(pmd->intr_handle.fd == -1))
1146 return rte_intr_callback_register(
1147 &pmd->intr_handle, tap_dev_intr_handler, dev);
1149 nl_final(pmd->intr_handle.fd);
1150 return rte_intr_callback_unregister(&pmd->intr_handle,
1151 tap_dev_intr_handler, dev);
1154 static const uint32_t*
1155 tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
1157 static const uint32_t ptypes[] = {
1158 RTE_PTYPE_INNER_L2_ETHER,
1159 RTE_PTYPE_INNER_L2_ETHER_VLAN,
1160 RTE_PTYPE_INNER_L2_ETHER_QINQ,
1161 RTE_PTYPE_INNER_L3_IPV4,
1162 RTE_PTYPE_INNER_L3_IPV4_EXT,
1163 RTE_PTYPE_INNER_L3_IPV6,
1164 RTE_PTYPE_INNER_L3_IPV6_EXT,
1165 RTE_PTYPE_INNER_L4_FRAG,
1166 RTE_PTYPE_INNER_L4_UDP,
1167 RTE_PTYPE_INNER_L4_TCP,
1168 RTE_PTYPE_INNER_L4_SCTP,
1170 RTE_PTYPE_L2_ETHER_VLAN,
1171 RTE_PTYPE_L2_ETHER_QINQ,
1173 RTE_PTYPE_L3_IPV4_EXT,
1174 RTE_PTYPE_L3_IPV6_EXT,
1186 tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1187 struct rte_eth_fc_conf *fc_conf)
1189 fc_conf->mode = RTE_FC_NONE;
1194 tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1195 struct rte_eth_fc_conf *fc_conf)
1197 if (fc_conf->mode != RTE_FC_NONE)
1202 static const struct eth_dev_ops ops = {
1203 .dev_start = tap_dev_start,
1204 .dev_stop = tap_dev_stop,
1205 .dev_close = tap_dev_close,
1206 .dev_configure = tap_dev_configure,
1207 .dev_infos_get = tap_dev_info,
1208 .rx_queue_setup = tap_rx_queue_setup,
1209 .tx_queue_setup = tap_tx_queue_setup,
1210 .rx_queue_release = tap_rx_queue_release,
1211 .tx_queue_release = tap_tx_queue_release,
1212 .flow_ctrl_get = tap_flow_ctrl_get,
1213 .flow_ctrl_set = tap_flow_ctrl_set,
1214 .link_update = tap_link_update,
1215 .dev_set_link_up = tap_link_set_up,
1216 .dev_set_link_down = tap_link_set_down,
1217 .promiscuous_enable = tap_promisc_enable,
1218 .promiscuous_disable = tap_promisc_disable,
1219 .allmulticast_enable = tap_allmulti_enable,
1220 .allmulticast_disable = tap_allmulti_disable,
1221 .mac_addr_set = tap_mac_set,
1222 .mtu_set = tap_mtu_set,
1223 .set_mc_addr_list = tap_set_mc_addr_list,
1224 .stats_get = tap_stats_get,
1225 .stats_reset = tap_stats_reset,
1226 .dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1227 .filter_ctrl = tap_dev_filter_ctrl,
1231 eth_dev_tap_create(struct rte_vdev_device *vdev, char *tap_name,
1232 char *remote_iface, int fixed_mac_type)
1234 int numa_node = rte_socket_id();
1235 struct rte_eth_dev *dev;
1236 struct pmd_internals *pmd;
1237 struct rte_eth_dev_data *data;
1241 RTE_LOG(DEBUG, PMD, " TAP device on numa %u\n", rte_socket_id());
1243 data = rte_zmalloc_socket(tap_name, sizeof(*data), 0, numa_node);
1245 RTE_LOG(ERR, PMD, "TAP Failed to allocate data\n");
1249 dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1251 RTE_LOG(ERR, PMD, "TAP Unable to allocate device struct\n");
1255 pmd = dev->data->dev_private;
1257 snprintf(pmd->name, sizeof(pmd->name), "%s", tap_name);
1259 pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1260 if (pmd->ioctl_sock == -1) {
1262 "TAP Unable to get a socket for management: %s\n",
1267 /* Setup some default values */
1268 rte_memcpy(data, dev->data, sizeof(*data));
1269 data->dev_private = pmd;
1270 data->dev_flags = RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1271 data->numa_node = numa_node;
1273 data->dev_link = pmd_link;
1274 data->mac_addrs = &pmd->eth_addr;
1275 /* Set the number of RX and TX queues */
1276 data->nb_rx_queues = 0;
1277 data->nb_tx_queues = 0;
1280 dev->dev_ops = &ops;
1281 dev->rx_pkt_burst = pmd_rx_burst;
1282 dev->tx_pkt_burst = pmd_tx_burst;
1284 pmd->intr_handle.type = RTE_INTR_HANDLE_EXT;
1285 pmd->intr_handle.fd = -1;
1287 /* Presetup the fds to -1 as being not valid */
1288 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1289 pmd->rxq[i].fd = -1;
1290 pmd->txq[i].fd = -1;
1293 if (fixed_mac_type) {
1294 /* fixed mac = 00:64:74:61:70:<iface_idx> */
1295 static int iface_idx;
1296 char mac[ETHER_ADDR_LEN] = "\0dtap";
1298 mac[ETHER_ADDR_LEN - 1] = iface_idx++;
1299 rte_memcpy(&pmd->eth_addr, mac, ETHER_ADDR_LEN);
1301 eth_random_addr((uint8_t *)&pmd->eth_addr);
1304 /* Immediately create the netdevice (this will create the 1st queue). */
1306 if (tap_setup_queue(dev, pmd, 0, 1) == -1)
1309 if (tap_setup_queue(dev, pmd, 0, 0) == -1)
1312 ifr.ifr_mtu = dev->data->mtu;
1313 if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE) < 0)
1316 memset(&ifr, 0, sizeof(struct ifreq));
1317 ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1318 rte_memcpy(ifr.ifr_hwaddr.sa_data, &pmd->eth_addr, ETHER_ADDR_LEN);
1319 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
1323 * Set up everything related to rte_flow:
1325 * - tap / remote if_index
1326 * - mandatory QDISCs
1327 * - rte_flow actual/implicit lists
1330 pmd->nlsk_fd = nl_init(0);
1331 if (pmd->nlsk_fd == -1) {
1332 RTE_LOG(WARNING, PMD, "%s: failed to create netlink socket.\n",
1334 goto disable_rte_flow;
1336 pmd->if_index = if_nametoindex(pmd->name);
1337 if (!pmd->if_index) {
1338 RTE_LOG(ERR, PMD, "%s: failed to get if_index.\n", pmd->name);
1339 goto disable_rte_flow;
1341 if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
1342 RTE_LOG(ERR, PMD, "%s: failed to create multiq qdisc.\n",
1344 goto disable_rte_flow;
1346 if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
1347 RTE_LOG(ERR, PMD, "%s: failed to create ingress qdisc.\n",
1349 goto disable_rte_flow;
1351 LIST_INIT(&pmd->flows);
1353 if (strlen(remote_iface)) {
1354 pmd->remote_if_index = if_nametoindex(remote_iface);
1355 if (!pmd->remote_if_index) {
1356 RTE_LOG(ERR, PMD, "%s: failed to get %s if_index.\n",
1357 pmd->name, remote_iface);
1360 snprintf(pmd->remote_iface, RTE_ETH_NAME_MAX_LEN,
1361 "%s", remote_iface);
1363 /* Save state of remote device */
1364 tap_ioctl(pmd, SIOCGIFFLAGS, &pmd->remote_initial_flags, 0, REMOTE_ONLY);
1366 /* Replicate remote MAC address */
1367 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) {
1368 RTE_LOG(ERR, PMD, "%s: failed to get %s MAC address.\n",
1369 pmd->name, pmd->remote_iface);
1372 rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
1374 /* The desired MAC is already in ifreq after SIOCGIFHWADDR. */
1375 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) {
1376 RTE_LOG(ERR, PMD, "%s: failed to get %s MAC address.\n",
1377 pmd->name, remote_iface);
1382 * Flush usually returns negative value because it tries to
1383 * delete every QDISC (and on a running device, one QDISC at
1384 * least is needed). Ignore negative return value.
1386 qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
1387 if (qdisc_create_ingress(pmd->nlsk_fd,
1388 pmd->remote_if_index) < 0) {
1389 RTE_LOG(ERR, PMD, "%s: failed to create ingress qdisc.\n",
1393 LIST_INIT(&pmd->implicit_flows);
1394 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0 ||
1395 tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0 ||
1396 tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0 ||
1397 tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0) {
1399 "%s: failed to create implicit rules.\n",
1408 RTE_LOG(ERR, PMD, " Disabling rte flow support: %s(%d)\n",
1409 strerror(errno), errno);
1410 if (strlen(remote_iface)) {
1411 RTE_LOG(ERR, PMD, "Remote feature requires flow support.\n");
1417 RTE_LOG(ERR, PMD, " Can't set up remote feature: %s(%d)\n",
1418 strerror(errno), errno);
1419 tap_flow_implicit_flush(pmd, NULL);
1422 RTE_LOG(ERR, PMD, "TAP Unable to initialize %s\n",
1423 rte_vdev_device_name(vdev));
1430 set_interface_name(const char *key __rte_unused,
1434 char *name = (char *)extra_args;
1437 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s", value);
1439 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s%d",
1440 DEFAULT_TAP_NAME, (tap_unit - 1));
1446 set_interface_speed(const char *key __rte_unused,
1450 *(int *)extra_args = (value) ? atoi(value) : ETH_SPEED_NUM_10G;
1456 set_remote_iface(const char *key __rte_unused,
1460 char *name = (char *)extra_args;
1463 snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s", value);
1469 set_mac_type(const char *key __rte_unused,
1474 !strncasecmp(ETH_TAP_MAC_FIXED, value, strlen(ETH_TAP_MAC_FIXED)))
1475 *(int *)extra_args = 1;
1479 /* Open a TAP interface device.
1482 rte_pmd_tap_probe(struct rte_vdev_device *dev)
1484 const char *name, *params;
1486 struct rte_kvargs *kvlist = NULL;
1488 char tap_name[RTE_ETH_NAME_MAX_LEN];
1489 char remote_iface[RTE_ETH_NAME_MAX_LEN];
1490 int fixed_mac_type = 0;
1492 name = rte_vdev_device_name(dev);
1493 params = rte_vdev_device_args(dev);
1495 speed = ETH_SPEED_NUM_10G;
1496 snprintf(tap_name, sizeof(tap_name), "%s%d",
1497 DEFAULT_TAP_NAME, tap_unit++);
1498 memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
1500 if (params && (params[0] != '\0')) {
1501 RTE_LOG(DEBUG, PMD, "parameters (%s)\n", params);
1503 kvlist = rte_kvargs_parse(params, valid_arguments);
1505 if (rte_kvargs_count(kvlist, ETH_TAP_SPEED_ARG) == 1) {
1506 ret = rte_kvargs_process(kvlist,
1508 &set_interface_speed,
1514 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
1515 ret = rte_kvargs_process(kvlist,
1517 &set_interface_name,
1523 if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
1524 ret = rte_kvargs_process(kvlist,
1532 if (rte_kvargs_count(kvlist, ETH_TAP_MAC_ARG) == 1) {
1533 ret = rte_kvargs_process(kvlist,
1542 pmd_link.link_speed = speed;
1544 RTE_LOG(NOTICE, PMD, "Initializing pmd_tap for %s as %s\n",
1547 ret = eth_dev_tap_create(dev, tap_name, remote_iface, fixed_mac_type);
1551 RTE_LOG(ERR, PMD, "Failed to create pmd for %s as %s\n",
1553 tap_unit--; /* Restore the unit number */
1555 rte_kvargs_free(kvlist);
1560 /* detach a TAP device.
1563 rte_pmd_tap_remove(struct rte_vdev_device *dev)
1565 struct rte_eth_dev *eth_dev = NULL;
1566 struct pmd_internals *internals;
1569 RTE_LOG(DEBUG, PMD, "Closing TUN/TAP Ethernet device on numa %u\n",
1572 /* find the ethdev entry */
1573 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1577 internals = eth_dev->data->dev_private;
1578 if (internals->nlsk_fd) {
1579 tap_flow_flush(eth_dev, NULL);
1580 tap_flow_implicit_flush(internals, NULL);
1581 nl_final(internals->nlsk_fd);
1583 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1584 if (internals->rxq[i].fd != -1) {
1585 close(internals->rxq[i].fd);
1586 internals->rxq[i].fd = -1;
1588 if (internals->txq[i].fd != -1) {
1589 close(internals->txq[i].fd);
1590 internals->txq[i].fd = -1;
1594 close(internals->ioctl_sock);
1595 rte_free(eth_dev->data->dev_private);
1596 rte_free(eth_dev->data);
1598 rte_eth_dev_release_port(eth_dev);
1603 static struct rte_vdev_driver pmd_tap_drv = {
1604 .probe = rte_pmd_tap_probe,
1605 .remove = rte_pmd_tap_remove,
1607 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
1608 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
1609 RTE_PMD_REGISTER_PARAM_STRING(net_tap,
1610 ETH_TAP_IFACE_ARG "=<string> "
1611 ETH_TAP_SPEED_ARG "=<int> "
1612 ETH_TAP_MAC_ARG "=" ETH_TAP_MAC_FIXED " "
1613 ETH_TAP_REMOTE_ARG "=<string>");