1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
5 #include <rte_atomic.h>
6 #include <rte_branch_prediction.h>
7 #include <rte_byteorder.h>
8 #include <rte_common.h>
10 #include <ethdev_driver.h>
11 #include <ethdev_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_bus_vdev.h>
14 #include <rte_kvargs.h>
16 #include <rte_debug.h>
18 #include <rte_string_fns.h>
19 #include <rte_ethdev.h>
20 #include <rte_errno.h>
21 #include <rte_cycles.h>
23 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
35 #include <arpa/inet.h>
37 #include <linux/if_tun.h>
38 #include <linux/if_ether.h>
43 #include <rte_eth_tap.h>
45 #include <tap_netlink.h>
46 #include <tap_tcmsgs.h>
48 /* Linux based path to the TUN device */
49 #define TUN_TAP_DEV_PATH "/dev/net/tun"
50 #define DEFAULT_TAP_NAME "dtap"
51 #define DEFAULT_TUN_NAME "dtun"
53 #define ETH_TAP_IFACE_ARG "iface"
54 #define ETH_TAP_REMOTE_ARG "remote"
55 #define ETH_TAP_MAC_ARG "mac"
56 #define ETH_TAP_MAC_FIXED "fixed"
58 #define ETH_TAP_USR_MAC_FMT "xx:xx:xx:xx:xx:xx"
59 #define ETH_TAP_CMP_MAC_FMT "0123456789ABCDEFabcdef"
60 #define ETH_TAP_MAC_ARG_FMT ETH_TAP_MAC_FIXED "|" ETH_TAP_USR_MAC_FMT
62 #define TAP_GSO_MBUFS_PER_CORE 128
63 #define TAP_GSO_MBUF_SEG_SIZE 128
64 #define TAP_GSO_MBUF_CACHE_SIZE 4
65 #define TAP_GSO_MBUFS_NUM \
66 (TAP_GSO_MBUFS_PER_CORE * TAP_GSO_MBUF_CACHE_SIZE)
68 /* IPC key for queue fds sync */
69 #define TAP_MP_KEY "tap_mp_sync_queues"
71 #define TAP_IOV_DEFAULT_MAX 1024
73 #define TAP_RX_OFFLOAD (RTE_ETH_RX_OFFLOAD_SCATTER | \
74 RTE_ETH_RX_OFFLOAD_IPV4_CKSUM | \
75 RTE_ETH_RX_OFFLOAD_UDP_CKSUM | \
76 RTE_ETH_RX_OFFLOAD_TCP_CKSUM)
78 #define TAP_TX_OFFLOAD (RTE_ETH_TX_OFFLOAD_MULTI_SEGS | \
79 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | \
80 RTE_ETH_TX_OFFLOAD_UDP_CKSUM | \
81 RTE_ETH_TX_OFFLOAD_TCP_CKSUM | \
82 RTE_ETH_TX_OFFLOAD_TCP_TSO)
84 static int tap_devices_count;
86 static const char *tuntap_types[ETH_TUNTAP_TYPE_MAX] = {
87 "UNKNOWN", "TUN", "TAP"
90 static const char *valid_arguments[] = {
97 static volatile uint32_t tap_trigger; /* Rx trigger */
99 static struct rte_eth_link pmd_link = {
100 .link_speed = RTE_ETH_SPEED_NUM_10G,
101 .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
102 .link_status = RTE_ETH_LINK_DOWN,
103 .link_autoneg = RTE_ETH_LINK_FIXED,
107 tap_trigger_cb(int sig __rte_unused)
109 /* Valid trigger values are nonzero */
110 tap_trigger = (tap_trigger + 1) | 0x80000000;
113 /* Specifies on what netdevices the ioctl should be applied */
120 /* Message header to synchronize queues via IPC */
122 char port_name[RTE_DEV_NAME_MAX_LEN];
126 * The file descriptors are in the dedicated part
127 * of the Unix message to be translated by the kernel.
131 static int tap_intr_handle_set(struct rte_eth_dev *dev, int set);
134 * Tun/Tap allocation routine
137 * Pointer to private structure.
139 * @param[in] is_keepalive
143 * -1 on failure, fd on success
146 tun_alloc(struct pmd_internals *pmd, int is_keepalive)
149 #ifdef IFF_MULTI_QUEUE
150 unsigned int features;
152 int fd, signo, flags;
154 memset(&ifr, 0, sizeof(struct ifreq));
157 * Do not set IFF_NO_PI as packet information header will be needed
158 * to check if a received packet has been truncated.
160 ifr.ifr_flags = (pmd->type == ETH_TUNTAP_TYPE_TAP) ?
161 IFF_TAP : IFF_TUN | IFF_POINTOPOINT;
162 strlcpy(ifr.ifr_name, pmd->name, IFNAMSIZ);
164 fd = open(TUN_TAP_DEV_PATH, O_RDWR);
166 TAP_LOG(ERR, "Unable to open %s interface", TUN_TAP_DEV_PATH);
170 #ifdef IFF_MULTI_QUEUE
171 /* Grab the TUN features to verify we can work multi-queue */
172 if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
173 TAP_LOG(ERR, "unable to get TUN/TAP features");
176 TAP_LOG(DEBUG, "%s Features %08x", TUN_TAP_DEV_PATH, features);
178 if (features & IFF_MULTI_QUEUE) {
179 TAP_LOG(DEBUG, " Multi-queue support for %d queues",
180 RTE_PMD_TAP_MAX_QUEUES);
181 ifr.ifr_flags |= IFF_MULTI_QUEUE;
185 ifr.ifr_flags |= IFF_ONE_QUEUE;
186 TAP_LOG(DEBUG, " Single queue only support");
189 /* Set the TUN/TAP configuration and set the name if needed */
190 if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
191 TAP_LOG(WARNING, "Unable to set TUNSETIFF for %s: %s",
192 ifr.ifr_name, strerror(errno));
197 * Name passed to kernel might be wildcard like dtun%d
198 * and need to find the resulting device.
200 TAP_LOG(DEBUG, "Device name is '%s'", ifr.ifr_name);
201 strlcpy(pmd->name, ifr.ifr_name, RTE_ETH_NAME_MAX_LEN);
205 * Detach the TUN/TAP keep-alive queue
206 * to avoid traffic through it
208 ifr.ifr_flags = IFF_DETACH_QUEUE;
209 if (ioctl(fd, TUNSETQUEUE, (void *)&ifr) < 0) {
211 "Unable to detach keep-alive queue for %s: %s",
212 ifr.ifr_name, strerror(errno));
217 flags = fcntl(fd, F_GETFL);
220 "Unable to get %s current flags\n",
225 /* Always set the file descriptor to non-blocking */
227 if (fcntl(fd, F_SETFL, flags) < 0) {
229 "Unable to set %s to nonblocking: %s",
230 ifr.ifr_name, strerror(errno));
234 /* Find a free realtime signal */
235 for (signo = SIGRTMIN + 1; signo < SIGRTMAX; signo++) {
238 if (sigaction(signo, NULL, &sa) == -1) {
240 "Unable to get current rt-signal %d handler",
245 /* Already have the handler we want on this signal */
246 if (sa.sa_handler == tap_trigger_cb)
249 /* Is handler in use by application */
250 if (sa.sa_handler != SIG_DFL) {
252 "Skipping used rt-signal %d", signo);
256 sa = (struct sigaction) {
257 .sa_flags = SA_RESTART,
258 .sa_handler = tap_trigger_cb,
261 if (sigaction(signo, &sa, NULL) == -1) {
263 "Unable to set rt-signal %d handler\n", signo);
267 /* Found a good signal to use */
269 "Using rt-signal %d", signo);
273 if (signo == SIGRTMAX) {
274 TAP_LOG(WARNING, "All rt-signals are in use\n");
276 /* Disable trigger globally in case of error */
278 TAP_LOG(NOTICE, "No Rx trigger signal available\n");
280 /* Enable signal on file descriptor */
281 if (fcntl(fd, F_SETSIG, signo) < 0) {
282 TAP_LOG(WARNING, "Unable to set signo %d for fd %d: %s",
283 signo, fd, strerror(errno));
286 if (fcntl(fd, F_SETFL, flags | O_ASYNC) < 0) {
287 TAP_LOG(WARNING, "Unable to set fcntl flags: %s",
292 if (fcntl(fd, F_SETOWN, getpid()) < 0) {
293 TAP_LOG(WARNING, "Unable to set fcntl owner: %s",
307 tap_verify_csum(struct rte_mbuf *mbuf)
309 uint32_t l2 = mbuf->packet_type & RTE_PTYPE_L2_MASK;
310 uint32_t l3 = mbuf->packet_type & RTE_PTYPE_L3_MASK;
311 uint32_t l4 = mbuf->packet_type & RTE_PTYPE_L4_MASK;
312 unsigned int l2_len = sizeof(struct rte_ether_hdr);
317 struct rte_udp_hdr *udp_hdr;
319 if (l2 == RTE_PTYPE_L2_ETHER_VLAN)
321 else if (l2 == RTE_PTYPE_L2_ETHER_QINQ)
323 /* Don't verify checksum for packets with discontinuous L2 header */
324 if (unlikely(l2_len + sizeof(struct rte_ipv4_hdr) >
325 rte_pktmbuf_data_len(mbuf)))
327 l3_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len);
328 if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
329 struct rte_ipv4_hdr *iph = l3_hdr;
331 l3_len = rte_ipv4_hdr_len(iph);
332 if (unlikely(l2_len + l3_len > rte_pktmbuf_data_len(mbuf)))
334 /* check that the total length reported by header is not
335 * greater than the total received size
337 if (l2_len + rte_be_to_cpu_16(iph->total_length) >
338 rte_pktmbuf_data_len(mbuf))
341 cksum = ~rte_raw_cksum(iph, l3_len);
342 mbuf->ol_flags |= cksum ?
343 RTE_MBUF_F_RX_IP_CKSUM_BAD :
344 RTE_MBUF_F_RX_IP_CKSUM_GOOD;
345 } else if (l3 == RTE_PTYPE_L3_IPV6) {
346 struct rte_ipv6_hdr *iph = l3_hdr;
348 l3_len = sizeof(struct rte_ipv6_hdr);
349 /* check that the total length reported by header is not
350 * greater than the total received size
352 if (l2_len + l3_len + rte_be_to_cpu_16(iph->payload_len) >
353 rte_pktmbuf_data_len(mbuf))
356 /* - RTE_PTYPE_L3_IPV4_EXT_UNKNOWN cannot happen because
357 * mbuf->packet_type is filled by rte_net_get_ptype() which
358 * never returns this value.
359 * - IPv6 extensions are not supported.
363 if (l4 == RTE_PTYPE_L4_UDP || l4 == RTE_PTYPE_L4_TCP) {
366 l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len);
367 /* Don't verify checksum for multi-segment packets. */
368 if (mbuf->nb_segs > 1)
370 if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
371 if (l4 == RTE_PTYPE_L4_UDP) {
372 udp_hdr = (struct rte_udp_hdr *)l4_hdr;
373 if (udp_hdr->dgram_cksum == 0) {
375 * For IPv4, a zero UDP checksum
376 * indicates that the sender did not
377 * generate one [RFC 768].
379 mbuf->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
383 cksum_ok = !rte_ipv4_udptcp_cksum_verify(l3_hdr,
385 } else { /* l3 == RTE_PTYPE_L3_IPV6, checked above */
386 cksum_ok = !rte_ipv6_udptcp_cksum_verify(l3_hdr,
389 mbuf->ol_flags |= cksum_ok ?
390 RTE_MBUF_F_RX_L4_CKSUM_GOOD : RTE_MBUF_F_RX_L4_CKSUM_BAD;
395 tap_rxq_pool_free(struct rte_mbuf *pool)
397 struct rte_mbuf *mbuf = pool;
398 uint16_t nb_segs = 1;
407 pool->nb_segs = nb_segs;
408 rte_pktmbuf_free(pool);
411 /* Callback to handle the rx burst of packets to the correct interface and
412 * file descriptor(s) in a multi-queue setup.
415 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
417 struct rx_queue *rxq = queue;
418 struct pmd_process_private *process_private;
420 unsigned long num_rx_bytes = 0;
421 uint32_t trigger = tap_trigger;
423 if (trigger == rxq->trigger_seen)
426 process_private = rte_eth_devices[rxq->in_port].process_private;
427 for (num_rx = 0; num_rx < nb_pkts; ) {
428 struct rte_mbuf *mbuf = rxq->pool;
429 struct rte_mbuf *seg = NULL;
430 struct rte_mbuf *new_tail = NULL;
431 uint16_t data_off = rte_pktmbuf_headroom(mbuf);
434 len = readv(process_private->rxq_fds[rxq->queue_id],
436 1 + (rxq->rxmode->offloads & RTE_ETH_RX_OFFLOAD_SCATTER ?
437 rxq->nb_rx_desc : 1));
438 if (len < (int)sizeof(struct tun_pi))
441 /* Packet couldn't fit in the provided mbuf */
442 if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) {
443 rxq->stats.ierrors++;
447 len -= sizeof(struct tun_pi);
450 mbuf->port = rxq->in_port;
452 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
454 if (unlikely(!buf)) {
455 rxq->stats.rx_nombuf++;
456 /* No new buf has been allocated: do nothing */
457 if (!new_tail || !seg)
461 tap_rxq_pool_free(mbuf);
465 seg = seg ? seg->next : mbuf;
466 if (rxq->pool == mbuf)
469 new_tail->next = buf;
471 new_tail->next = seg->next;
473 /* iovecs[0] is reserved for packet info (pi) */
474 (*rxq->iovecs)[mbuf->nb_segs].iov_len =
475 buf->buf_len - data_off;
476 (*rxq->iovecs)[mbuf->nb_segs].iov_base =
477 (char *)buf->buf_addr + data_off;
479 seg->data_len = RTE_MIN(seg->buf_len - data_off, len);
480 seg->data_off = data_off;
482 len -= seg->data_len;
486 /* First segment has headroom, not the others */
490 mbuf->packet_type = rte_net_get_ptype(mbuf, NULL,
492 if (rxq->rxmode->offloads & RTE_ETH_RX_OFFLOAD_CHECKSUM)
493 tap_verify_csum(mbuf);
495 /* account for the receive frame */
496 bufs[num_rx++] = mbuf;
497 num_rx_bytes += mbuf->pkt_len;
500 rxq->stats.ipackets += num_rx;
501 rxq->stats.ibytes += num_rx_bytes;
503 if (trigger && num_rx < nb_pkts)
504 rxq->trigger_seen = trigger;
509 /* Finalize l4 checksum calculation */
511 tap_tx_l4_cksum(uint16_t *l4_cksum, uint16_t l4_phdr_cksum,
512 uint32_t l4_raw_cksum)
517 cksum = __rte_raw_cksum_reduce(l4_raw_cksum);
518 cksum += l4_phdr_cksum;
520 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
521 cksum = (~cksum) & 0xffff;
528 /* Accumaulate L4 raw checksums */
530 tap_tx_l4_add_rcksum(char *l4_data, unsigned int l4_len, uint16_t *l4_cksum,
531 uint32_t *l4_raw_cksum)
533 if (l4_cksum == NULL)
536 *l4_raw_cksum = __rte_raw_cksum(l4_data, l4_len, *l4_raw_cksum);
539 /* L3 and L4 pseudo headers checksum offloads */
541 tap_tx_l3_cksum(char *packet, uint64_t ol_flags, unsigned int l2_len,
542 unsigned int l3_len, unsigned int l4_len, uint16_t **l4_cksum,
543 uint16_t *l4_phdr_cksum, uint32_t *l4_raw_cksum)
545 void *l3_hdr = packet + l2_len;
547 if (ol_flags & (RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_IPV4)) {
548 struct rte_ipv4_hdr *iph = l3_hdr;
551 iph->hdr_checksum = 0;
552 cksum = rte_raw_cksum(iph, l3_len);
553 iph->hdr_checksum = (cksum == 0xffff) ? cksum : ~cksum;
555 if (ol_flags & RTE_MBUF_F_TX_L4_MASK) {
558 l4_hdr = packet + l2_len + l3_len;
559 if ((ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_UDP_CKSUM)
560 *l4_cksum = &((struct rte_udp_hdr *)l4_hdr)->dgram_cksum;
561 else if ((ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_TCP_CKSUM)
562 *l4_cksum = &((struct rte_tcp_hdr *)l4_hdr)->cksum;
566 if (ol_flags & RTE_MBUF_F_TX_IPV4)
567 *l4_phdr_cksum = rte_ipv4_phdr_cksum(l3_hdr, 0);
569 *l4_phdr_cksum = rte_ipv6_phdr_cksum(l3_hdr, 0);
570 *l4_raw_cksum = __rte_raw_cksum(l4_hdr, l4_len, 0);
575 tap_write_mbufs(struct tx_queue *txq, uint16_t num_mbufs,
576 struct rte_mbuf **pmbufs,
577 uint16_t *num_packets, unsigned long *num_tx_bytes)
581 struct pmd_process_private *process_private;
583 process_private = rte_eth_devices[txq->out_port].process_private;
585 for (i = 0; i < num_mbufs; i++) {
586 struct rte_mbuf *mbuf = pmbufs[i];
587 struct iovec iovecs[mbuf->nb_segs + 2];
588 struct tun_pi pi = { .flags = 0, .proto = 0x00 };
589 struct rte_mbuf *seg = mbuf;
590 char m_copy[mbuf->data_len];
594 int k; /* current index in iovecs for copying segments */
595 uint16_t seg_len; /* length of first segment */
597 uint16_t *l4_cksum; /* l4 checksum (pseudo header + payload) */
598 uint32_t l4_raw_cksum = 0; /* TCP/UDP payload raw checksum */
599 uint16_t l4_phdr_cksum = 0; /* TCP/UDP pseudo header checksum */
600 uint16_t is_cksum = 0; /* in case cksum should be offloaded */
603 if (txq->type == ETH_TUNTAP_TYPE_TUN) {
605 * TUN and TAP are created with IFF_NO_PI disabled.
606 * For TUN PMD this mandatory as fields are used by
607 * Kernel tun.c to determine whether its IP or non IP
610 * The logic fetches the first byte of data from mbuf
611 * then compares whether its v4 or v6. If first byte
612 * is 4 or 6, then protocol field is updated.
614 char *buff_data = rte_pktmbuf_mtod(seg, void *);
615 proto = (*buff_data & 0xf0);
616 pi.proto = (proto == 0x40) ?
617 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) :
619 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) :
624 iovecs[k].iov_base = π
625 iovecs[k].iov_len = sizeof(pi);
628 nb_segs = mbuf->nb_segs;
630 ((mbuf->ol_flags & (RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_IPV4) ||
631 (mbuf->ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_UDP_CKSUM ||
632 (mbuf->ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_TCP_CKSUM))) {
635 /* Support only packets with at least layer 4
636 * header included in the first segment
638 seg_len = rte_pktmbuf_data_len(mbuf);
639 l234_hlen = mbuf->l2_len + mbuf->l3_len + mbuf->l4_len;
640 if (seg_len < l234_hlen)
643 /* To change checksums, work on a * copy of l2, l3
644 * headers + l4 pseudo header
646 rte_memcpy(m_copy, rte_pktmbuf_mtod(mbuf, void *),
648 tap_tx_l3_cksum(m_copy, mbuf->ol_flags,
649 mbuf->l2_len, mbuf->l3_len, mbuf->l4_len,
650 &l4_cksum, &l4_phdr_cksum,
652 iovecs[k].iov_base = m_copy;
653 iovecs[k].iov_len = l234_hlen;
656 /* Update next iovecs[] beyond l2, l3, l4 headers */
657 if (seg_len > l234_hlen) {
658 iovecs[k].iov_len = seg_len - l234_hlen;
660 rte_pktmbuf_mtod(seg, char *) +
662 tap_tx_l4_add_rcksum(iovecs[k].iov_base,
663 iovecs[k].iov_len, l4_cksum,
671 for (j = k; j <= nb_segs; j++) {
672 iovecs[j].iov_len = rte_pktmbuf_data_len(seg);
673 iovecs[j].iov_base = rte_pktmbuf_mtod(seg, void *);
675 tap_tx_l4_add_rcksum(iovecs[j].iov_base,
676 iovecs[j].iov_len, l4_cksum,
682 tap_tx_l4_cksum(l4_cksum, l4_phdr_cksum, l4_raw_cksum);
684 /* copy the tx frame data */
685 n = writev(process_private->txq_fds[txq->queue_id], iovecs, j);
690 (*num_tx_bytes) += rte_pktmbuf_pkt_len(mbuf);
695 /* Callback to handle sending packets from the tap interface
698 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
700 struct tx_queue *txq = queue;
702 uint16_t num_packets = 0;
703 unsigned long num_tx_bytes = 0;
707 if (unlikely(nb_pkts == 0))
710 struct rte_mbuf *gso_mbufs[MAX_GSO_MBUFS];
711 max_size = *txq->mtu + (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN + 4);
712 for (i = 0; i < nb_pkts; i++) {
713 struct rte_mbuf *mbuf_in = bufs[num_tx];
714 struct rte_mbuf **mbuf;
715 uint16_t num_mbufs = 0;
716 uint16_t tso_segsz = 0;
722 tso = mbuf_in->ol_flags & RTE_MBUF_F_TX_TCP_SEG;
724 struct rte_gso_ctx *gso_ctx = &txq->gso_ctx;
726 /* TCP segmentation implies TCP checksum offload */
727 mbuf_in->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
729 /* gso size is calculated without RTE_ETHER_CRC_LEN */
730 hdrs_len = mbuf_in->l2_len + mbuf_in->l3_len +
732 tso_segsz = mbuf_in->tso_segsz + hdrs_len;
733 if (unlikely(tso_segsz == hdrs_len) ||
734 tso_segsz > *txq->mtu) {
738 gso_ctx->gso_size = tso_segsz;
739 /* 'mbuf_in' packet to segment */
740 num_tso_mbufs = rte_gso_segment(mbuf_in,
741 gso_ctx, /* gso control block */
742 (struct rte_mbuf **)&gso_mbufs, /* out mbufs */
743 RTE_DIM(gso_mbufs)); /* max tso mbufs */
745 /* ret contains the number of new created mbufs */
746 if (num_tso_mbufs < 0)
749 if (num_tso_mbufs >= 1) {
751 num_mbufs = num_tso_mbufs;
753 /* 0 means it can be transmitted directly
760 /* stats.errs will be incremented */
761 if (rte_pktmbuf_pkt_len(mbuf_in) > max_size)
764 /* ret 0 indicates no new mbufs were created */
770 ret = tap_write_mbufs(txq, num_mbufs, mbuf,
771 &num_packets, &num_tx_bytes);
775 if (num_tso_mbufs > 0)
776 rte_pktmbuf_free_bulk(mbuf, num_tso_mbufs);
780 /* free original mbuf */
781 rte_pktmbuf_free(mbuf_in);
783 if (num_tso_mbufs > 0)
784 rte_pktmbuf_free_bulk(mbuf, num_tso_mbufs);
787 txq->stats.opackets += num_packets;
788 txq->stats.errs += nb_pkts - num_tx;
789 txq->stats.obytes += num_tx_bytes;
795 tap_ioctl_req2str(unsigned long request)
799 return "SIOCSIFFLAGS";
801 return "SIOCGIFFLAGS";
803 return "SIOCGIFHWADDR";
805 return "SIOCSIFHWADDR";
813 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
814 struct ifreq *ifr, int set, enum ioctl_mode mode)
816 short req_flags = ifr->ifr_flags;
817 int remote = pmd->remote_if_index &&
818 (mode == REMOTE_ONLY || mode == LOCAL_AND_REMOTE);
820 if (!pmd->remote_if_index && mode == REMOTE_ONLY)
823 * If there is a remote netdevice, apply ioctl on it, then apply it on
828 strlcpy(ifr->ifr_name, pmd->remote_iface, IFNAMSIZ);
829 else if (mode == LOCAL_ONLY || mode == LOCAL_AND_REMOTE)
830 strlcpy(ifr->ifr_name, pmd->name, IFNAMSIZ);
833 /* fetch current flags to leave other flags untouched */
834 if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
837 ifr->ifr_flags |= req_flags;
839 ifr->ifr_flags &= ~req_flags;
847 TAP_LOG(WARNING, "%s: ioctl() called with wrong arg",
851 if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
853 if (remote-- && mode == LOCAL_AND_REMOTE)
858 TAP_LOG(DEBUG, "%s(%s) failed: %s(%d)", ifr->ifr_name,
859 tap_ioctl_req2str(request), strerror(errno), errno);
864 tap_link_set_down(struct rte_eth_dev *dev)
866 struct pmd_internals *pmd = dev->data->dev_private;
867 struct ifreq ifr = { .ifr_flags = IFF_UP };
869 dev->data->dev_link.link_status = RTE_ETH_LINK_DOWN;
870 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_ONLY);
874 tap_link_set_up(struct rte_eth_dev *dev)
876 struct pmd_internals *pmd = dev->data->dev_private;
877 struct ifreq ifr = { .ifr_flags = IFF_UP };
879 dev->data->dev_link.link_status = RTE_ETH_LINK_UP;
880 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
884 tap_dev_start(struct rte_eth_dev *dev)
888 err = tap_intr_handle_set(dev, 1);
892 err = tap_link_set_up(dev);
896 for (i = 0; i < dev->data->nb_tx_queues; i++)
897 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
898 for (i = 0; i < dev->data->nb_rx_queues; i++)
899 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
904 /* This function gets called when the current port gets stopped.
907 tap_dev_stop(struct rte_eth_dev *dev)
911 for (i = 0; i < dev->data->nb_tx_queues; i++)
912 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
913 for (i = 0; i < dev->data->nb_rx_queues; i++)
914 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
916 tap_intr_handle_set(dev, 0);
917 tap_link_set_down(dev);
923 tap_dev_configure(struct rte_eth_dev *dev)
925 struct pmd_internals *pmd = dev->data->dev_private;
927 if (dev->data->nb_rx_queues > RTE_PMD_TAP_MAX_QUEUES) {
929 "%s: number of rx queues %d exceeds max num of queues %d",
931 dev->data->nb_rx_queues,
932 RTE_PMD_TAP_MAX_QUEUES);
935 if (dev->data->nb_tx_queues > RTE_PMD_TAP_MAX_QUEUES) {
937 "%s: number of tx queues %d exceeds max num of queues %d",
939 dev->data->nb_tx_queues,
940 RTE_PMD_TAP_MAX_QUEUES);
944 TAP_LOG(INFO, "%s: %s: TX configured queues number: %u",
945 dev->device->name, pmd->name, dev->data->nb_tx_queues);
947 TAP_LOG(INFO, "%s: %s: RX configured queues number: %u",
948 dev->device->name, pmd->name, dev->data->nb_rx_queues);
954 tap_dev_speed_capa(void)
956 uint32_t speed = pmd_link.link_speed;
959 if (speed >= RTE_ETH_SPEED_NUM_10M)
960 capa |= RTE_ETH_LINK_SPEED_10M;
961 if (speed >= RTE_ETH_SPEED_NUM_100M)
962 capa |= RTE_ETH_LINK_SPEED_100M;
963 if (speed >= RTE_ETH_SPEED_NUM_1G)
964 capa |= RTE_ETH_LINK_SPEED_1G;
965 if (speed >= RTE_ETH_SPEED_NUM_5G)
966 capa |= RTE_ETH_LINK_SPEED_2_5G;
967 if (speed >= RTE_ETH_SPEED_NUM_5G)
968 capa |= RTE_ETH_LINK_SPEED_5G;
969 if (speed >= RTE_ETH_SPEED_NUM_10G)
970 capa |= RTE_ETH_LINK_SPEED_10G;
971 if (speed >= RTE_ETH_SPEED_NUM_20G)
972 capa |= RTE_ETH_LINK_SPEED_20G;
973 if (speed >= RTE_ETH_SPEED_NUM_25G)
974 capa |= RTE_ETH_LINK_SPEED_25G;
975 if (speed >= RTE_ETH_SPEED_NUM_40G)
976 capa |= RTE_ETH_LINK_SPEED_40G;
977 if (speed >= RTE_ETH_SPEED_NUM_50G)
978 capa |= RTE_ETH_LINK_SPEED_50G;
979 if (speed >= RTE_ETH_SPEED_NUM_56G)
980 capa |= RTE_ETH_LINK_SPEED_56G;
981 if (speed >= RTE_ETH_SPEED_NUM_100G)
982 capa |= RTE_ETH_LINK_SPEED_100G;
988 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
990 struct pmd_internals *internals = dev->data->dev_private;
992 dev_info->if_index = internals->if_index;
993 dev_info->max_mac_addrs = 1;
994 dev_info->max_rx_pktlen = (uint32_t)RTE_ETHER_MAX_VLAN_FRAME_LEN;
995 dev_info->max_rx_queues = RTE_PMD_TAP_MAX_QUEUES;
996 dev_info->max_tx_queues = RTE_PMD_TAP_MAX_QUEUES;
997 dev_info->min_rx_bufsize = 0;
998 dev_info->speed_capa = tap_dev_speed_capa();
999 dev_info->rx_queue_offload_capa = TAP_RX_OFFLOAD;
1000 dev_info->rx_offload_capa = dev_info->rx_queue_offload_capa;
1001 dev_info->tx_queue_offload_capa = TAP_TX_OFFLOAD;
1002 dev_info->tx_offload_capa = dev_info->tx_queue_offload_capa;
1003 dev_info->hash_key_size = TAP_RSS_HASH_KEY_SIZE;
1005 * limitation: TAP supports all of IP, UDP and TCP hash
1006 * functions together and not in partial combinations
1008 dev_info->flow_type_rss_offloads = ~TAP_RSS_HF_MASK;
1009 dev_info->dev_capa &= ~RTE_ETH_DEV_CAPA_FLOW_RULE_KEEP;
1015 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
1017 unsigned int i, imax;
1018 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
1019 unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
1020 unsigned long rx_nombuf = 0, ierrors = 0;
1021 const struct pmd_internals *pmd = dev->data->dev_private;
1023 /* rx queue statistics */
1024 imax = (dev->data->nb_rx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
1025 dev->data->nb_rx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
1026 for (i = 0; i < imax; i++) {
1027 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
1028 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
1029 rx_total += tap_stats->q_ipackets[i];
1030 rx_bytes_total += tap_stats->q_ibytes[i];
1031 rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
1032 ierrors += pmd->rxq[i].stats.ierrors;
1035 /* tx queue statistics */
1036 imax = (dev->data->nb_tx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
1037 dev->data->nb_tx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
1039 for (i = 0; i < imax; i++) {
1040 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
1041 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
1042 tx_total += tap_stats->q_opackets[i];
1043 tx_err_total += pmd->txq[i].stats.errs;
1044 tx_bytes_total += tap_stats->q_obytes[i];
1047 tap_stats->ipackets = rx_total;
1048 tap_stats->ibytes = rx_bytes_total;
1049 tap_stats->ierrors = ierrors;
1050 tap_stats->rx_nombuf = rx_nombuf;
1051 tap_stats->opackets = tx_total;
1052 tap_stats->oerrors = tx_err_total;
1053 tap_stats->obytes = tx_bytes_total;
1058 tap_stats_reset(struct rte_eth_dev *dev)
1061 struct pmd_internals *pmd = dev->data->dev_private;
1063 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1064 pmd->rxq[i].stats.ipackets = 0;
1065 pmd->rxq[i].stats.ibytes = 0;
1066 pmd->rxq[i].stats.ierrors = 0;
1067 pmd->rxq[i].stats.rx_nombuf = 0;
1069 pmd->txq[i].stats.opackets = 0;
1070 pmd->txq[i].stats.errs = 0;
1071 pmd->txq[i].stats.obytes = 0;
1078 tap_dev_close(struct rte_eth_dev *dev)
1081 struct pmd_internals *internals = dev->data->dev_private;
1082 struct pmd_process_private *process_private = dev->process_private;
1083 struct rx_queue *rxq;
1085 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1086 rte_free(dev->process_private);
1090 tap_link_set_down(dev);
1091 if (internals->nlsk_fd != -1) {
1092 tap_flow_flush(dev, NULL);
1093 tap_flow_implicit_flush(internals, NULL);
1094 tap_nl_final(internals->nlsk_fd);
1095 internals->nlsk_fd = -1;
1098 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1099 if (process_private->rxq_fds[i] != -1) {
1100 rxq = &internals->rxq[i];
1101 close(process_private->rxq_fds[i]);
1102 process_private->rxq_fds[i] = -1;
1103 tap_rxq_pool_free(rxq->pool);
1104 rte_free(rxq->iovecs);
1108 if (process_private->txq_fds[i] != -1) {
1109 close(process_private->txq_fds[i]);
1110 process_private->txq_fds[i] = -1;
1114 if (internals->remote_if_index) {
1115 /* Restore initial remote state */
1116 int ret = ioctl(internals->ioctl_sock, SIOCSIFFLAGS,
1117 &internals->remote_initial_flags);
1119 TAP_LOG(ERR, "restore remote state failed: %d", ret);
1123 rte_mempool_free(internals->gso_ctx_mp);
1124 internals->gso_ctx_mp = NULL;
1126 if (internals->ka_fd != -1) {
1127 close(internals->ka_fd);
1128 internals->ka_fd = -1;
1131 /* mac_addrs must not be freed alone because part of dev_private */
1132 dev->data->mac_addrs = NULL;
1134 internals = dev->data->dev_private;
1135 TAP_LOG(DEBUG, "Closing %s Ethernet device on numa %u",
1136 tuntap_types[internals->type], rte_socket_id());
1138 if (internals->ioctl_sock != -1) {
1139 close(internals->ioctl_sock);
1140 internals->ioctl_sock = -1;
1142 rte_free(dev->process_private);
1143 if (tap_devices_count == 1)
1144 rte_mp_action_unregister(TAP_MP_KEY);
1145 tap_devices_count--;
1147 * Since TUN device has no more opened file descriptors
1148 * it will be removed from kernel
1155 tap_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1157 struct rx_queue *rxq = dev->data->rx_queues[qid];
1158 struct pmd_process_private *process_private;
1162 process_private = rte_eth_devices[rxq->in_port].process_private;
1163 if (process_private->rxq_fds[rxq->queue_id] != -1) {
1164 close(process_private->rxq_fds[rxq->queue_id]);
1165 process_private->rxq_fds[rxq->queue_id] = -1;
1166 tap_rxq_pool_free(rxq->pool);
1167 rte_free(rxq->iovecs);
1174 tap_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1176 struct tx_queue *txq = dev->data->tx_queues[qid];
1177 struct pmd_process_private *process_private;
1181 process_private = rte_eth_devices[txq->out_port].process_private;
1183 if (process_private->txq_fds[txq->queue_id] != -1) {
1184 close(process_private->txq_fds[txq->queue_id]);
1185 process_private->txq_fds[txq->queue_id] = -1;
1190 tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
1192 struct rte_eth_link *dev_link = &dev->data->dev_link;
1193 struct pmd_internals *pmd = dev->data->dev_private;
1194 struct ifreq ifr = { .ifr_flags = 0 };
1196 if (pmd->remote_if_index) {
1197 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY);
1198 if (!(ifr.ifr_flags & IFF_UP) ||
1199 !(ifr.ifr_flags & IFF_RUNNING)) {
1200 dev_link->link_status = RTE_ETH_LINK_DOWN;
1204 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY);
1205 dev_link->link_status =
1206 ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
1213 tap_promisc_enable(struct rte_eth_dev *dev)
1215 struct pmd_internals *pmd = dev->data->dev_private;
1216 struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1219 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1223 if (pmd->remote_if_index && !pmd->flow_isolate) {
1224 dev->data->promiscuous = 1;
1225 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
1227 /* Rollback promisc flag */
1228 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1230 * rte_eth_dev_promiscuous_enable() rollback
1231 * dev->data->promiscuous in the case of failure.
1241 tap_promisc_disable(struct rte_eth_dev *dev)
1243 struct pmd_internals *pmd = dev->data->dev_private;
1244 struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1247 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1251 if (pmd->remote_if_index && !pmd->flow_isolate) {
1252 dev->data->promiscuous = 0;
1253 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
1255 /* Rollback promisc flag */
1256 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1258 * rte_eth_dev_promiscuous_disable() rollback
1259 * dev->data->promiscuous in the case of failure.
1269 tap_allmulti_enable(struct rte_eth_dev *dev)
1271 struct pmd_internals *pmd = dev->data->dev_private;
1272 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1275 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1279 if (pmd->remote_if_index && !pmd->flow_isolate) {
1280 dev->data->all_multicast = 1;
1281 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
1283 /* Rollback allmulti flag */
1284 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1286 * rte_eth_dev_allmulticast_enable() rollback
1287 * dev->data->all_multicast in the case of failure.
1297 tap_allmulti_disable(struct rte_eth_dev *dev)
1299 struct pmd_internals *pmd = dev->data->dev_private;
1300 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1303 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1307 if (pmd->remote_if_index && !pmd->flow_isolate) {
1308 dev->data->all_multicast = 0;
1309 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
1311 /* Rollback allmulti flag */
1312 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1314 * rte_eth_dev_allmulticast_disable() rollback
1315 * dev->data->all_multicast in the case of failure.
1325 tap_mac_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1327 struct pmd_internals *pmd = dev->data->dev_private;
1328 enum ioctl_mode mode = LOCAL_ONLY;
1332 if (pmd->type == ETH_TUNTAP_TYPE_TUN) {
1333 TAP_LOG(ERR, "%s: can't MAC address for TUN",
1338 if (rte_is_zero_ether_addr(mac_addr)) {
1339 TAP_LOG(ERR, "%s: can't set an empty MAC address",
1343 /* Check the actual current MAC address on the tap netdevice */
1344 ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY);
1347 if (rte_is_same_ether_addr(
1348 (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data,
1351 /* Check the current MAC address on the remote */
1352 ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY);
1355 if (!rte_is_same_ether_addr(
1356 (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data,
1358 mode = LOCAL_AND_REMOTE;
1359 ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1360 rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, RTE_ETHER_ADDR_LEN);
1361 ret = tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, mode);
1364 rte_memcpy(&pmd->eth_addr, mac_addr, RTE_ETHER_ADDR_LEN);
1365 if (pmd->remote_if_index && !pmd->flow_isolate) {
1366 /* Replace MAC redirection rule after a MAC change */
1367 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC);
1370 "%s: Couldn't delete MAC redirection rule",
1374 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC);
1377 "%s: Couldn't add MAC redirection rule",
1387 tap_gso_ctx_setup(struct rte_gso_ctx *gso_ctx, struct rte_eth_dev *dev)
1391 struct pmd_internals *pmd = dev->data->dev_private;
1394 /* initialize GSO context */
1395 gso_types = RTE_ETH_TX_OFFLOAD_TCP_TSO;
1396 if (!pmd->gso_ctx_mp) {
1398 * Create private mbuf pool with TAP_GSO_MBUF_SEG_SIZE
1399 * bytes size per mbuf use this pool for both direct and
1402 ret = snprintf(pool_name, sizeof(pool_name), "mp_%s",
1404 if (ret < 0 || ret >= (int)sizeof(pool_name)) {
1406 "%s: failed to create mbuf pool name for device %s,"
1407 "device name too long or output error, ret: %d\n",
1408 pmd->name, dev->device->name, ret);
1409 return -ENAMETOOLONG;
1411 pmd->gso_ctx_mp = rte_pktmbuf_pool_create(pool_name,
1412 TAP_GSO_MBUFS_NUM, TAP_GSO_MBUF_CACHE_SIZE, 0,
1413 RTE_PKTMBUF_HEADROOM + TAP_GSO_MBUF_SEG_SIZE,
1415 if (!pmd->gso_ctx_mp) {
1417 "%s: failed to create mbuf pool for device %s\n",
1418 pmd->name, dev->device->name);
1423 gso_ctx->direct_pool = pmd->gso_ctx_mp;
1424 gso_ctx->indirect_pool = pmd->gso_ctx_mp;
1425 gso_ctx->gso_types = gso_types;
1426 gso_ctx->gso_size = 0; /* gso_size is set in tx_burst() per packet */
1433 tap_setup_queue(struct rte_eth_dev *dev,
1434 struct pmd_internals *internals,
1442 struct pmd_internals *pmd = dev->data->dev_private;
1443 struct pmd_process_private *process_private = dev->process_private;
1444 struct rx_queue *rx = &internals->rxq[qid];
1445 struct tx_queue *tx = &internals->txq[qid];
1446 struct rte_gso_ctx *gso_ctx;
1449 fd = &process_private->rxq_fds[qid];
1450 other_fd = &process_private->txq_fds[qid];
1454 fd = &process_private->txq_fds[qid];
1455 other_fd = &process_private->rxq_fds[qid];
1457 gso_ctx = &tx->gso_ctx;
1460 /* fd for this queue already exists */
1461 TAP_LOG(DEBUG, "%s: fd %d for %s queue qid %d exists",
1462 pmd->name, *fd, dir, qid);
1464 } else if (*other_fd != -1) {
1465 /* Only other_fd exists. dup it */
1466 *fd = dup(*other_fd);
1469 TAP_LOG(ERR, "%s: dup() failed.", pmd->name);
1472 TAP_LOG(DEBUG, "%s: dup fd %d for %s queue qid %d (%d)",
1473 pmd->name, *other_fd, dir, qid, *fd);
1475 /* Both RX and TX fds do not exist (equal -1). Create fd */
1476 *fd = tun_alloc(pmd, 0);
1478 *fd = -1; /* restore original value */
1479 TAP_LOG(ERR, "%s: tun_alloc() failed.", pmd->name);
1482 TAP_LOG(DEBUG, "%s: add %s queue for qid %d fd %d",
1483 pmd->name, dir, qid, *fd);
1486 tx->mtu = &dev->data->mtu;
1487 rx->rxmode = &dev->data->dev_conf.rxmode;
1489 ret = tap_gso_ctx_setup(gso_ctx, dev);
1494 tx->type = pmd->type;
1500 tap_rx_queue_setup(struct rte_eth_dev *dev,
1501 uint16_t rx_queue_id,
1502 uint16_t nb_rx_desc,
1503 unsigned int socket_id,
1504 const struct rte_eth_rxconf *rx_conf __rte_unused,
1505 struct rte_mempool *mp)
1507 struct pmd_internals *internals = dev->data->dev_private;
1508 struct pmd_process_private *process_private = dev->process_private;
1509 struct rx_queue *rxq = &internals->rxq[rx_queue_id];
1510 struct rte_mbuf **tmp = &rxq->pool;
1511 long iov_max = sysconf(_SC_IOV_MAX);
1515 "_SC_IOV_MAX is not defined. Using %d as default",
1516 TAP_IOV_DEFAULT_MAX);
1517 iov_max = TAP_IOV_DEFAULT_MAX;
1519 uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1);
1520 struct iovec (*iovecs)[nb_desc + 1];
1521 int data_off = RTE_PKTMBUF_HEADROOM;
1526 if (rx_queue_id >= dev->data->nb_rx_queues || !mp) {
1528 "nb_rx_queues %d too small or mempool NULL",
1529 dev->data->nb_rx_queues);
1534 rxq->trigger_seen = 1; /* force initial burst */
1535 rxq->in_port = dev->data->port_id;
1536 rxq->queue_id = rx_queue_id;
1537 rxq->nb_rx_desc = nb_desc;
1538 iovecs = rte_zmalloc_socket(dev->device->name, sizeof(*iovecs), 0,
1542 "%s: Couldn't allocate %d RX descriptors",
1543 dev->device->name, nb_desc);
1546 rxq->iovecs = iovecs;
1548 dev->data->rx_queues[rx_queue_id] = rxq;
1549 fd = tap_setup_queue(dev, internals, rx_queue_id, 1);
1555 (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
1556 (*rxq->iovecs)[0].iov_base = &rxq->pi;
1558 for (i = 1; i <= nb_desc; i++) {
1559 *tmp = rte_pktmbuf_alloc(rxq->mp);
1562 "%s: couldn't allocate memory for queue %d",
1563 dev->device->name, rx_queue_id);
1567 (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
1568 (*rxq->iovecs)[i].iov_base =
1569 (char *)(*tmp)->buf_addr + data_off;
1571 tmp = &(*tmp)->next;
1574 TAP_LOG(DEBUG, " RX TUNTAP device name %s, qid %d on fd %d",
1575 internals->name, rx_queue_id,
1576 process_private->rxq_fds[rx_queue_id]);
1581 tap_rxq_pool_free(rxq->pool);
1583 rte_free(rxq->iovecs);
1589 tap_tx_queue_setup(struct rte_eth_dev *dev,
1590 uint16_t tx_queue_id,
1591 uint16_t nb_tx_desc __rte_unused,
1592 unsigned int socket_id __rte_unused,
1593 const struct rte_eth_txconf *tx_conf)
1595 struct pmd_internals *internals = dev->data->dev_private;
1596 struct pmd_process_private *process_private = dev->process_private;
1597 struct tx_queue *txq;
1601 if (tx_queue_id >= dev->data->nb_tx_queues)
1603 dev->data->tx_queues[tx_queue_id] = &internals->txq[tx_queue_id];
1604 txq = dev->data->tx_queues[tx_queue_id];
1605 txq->out_port = dev->data->port_id;
1606 txq->queue_id = tx_queue_id;
1608 offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1609 txq->csum = !!(offloads &
1610 (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
1611 RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
1612 RTE_ETH_TX_OFFLOAD_TCP_CKSUM));
1614 ret = tap_setup_queue(dev, internals, tx_queue_id, 0);
1618 " TX TUNTAP device name %s, qid %d on fd %d csum %s",
1619 internals->name, tx_queue_id,
1620 process_private->txq_fds[tx_queue_id],
1621 txq->csum ? "on" : "off");
1627 tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1629 struct pmd_internals *pmd = dev->data->dev_private;
1630 struct ifreq ifr = { .ifr_mtu = mtu };
1632 return tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE);
1636 tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
1637 struct rte_ether_addr *mc_addr_set __rte_unused,
1638 uint32_t nb_mc_addr __rte_unused)
1641 * Nothing to do actually: the tap has no filtering whatsoever, every
1642 * packet is received.
1648 tap_nl_msg_handler(struct nlmsghdr *nh, void *arg)
1650 struct rte_eth_dev *dev = arg;
1651 struct pmd_internals *pmd = dev->data->dev_private;
1652 struct ifinfomsg *info = NLMSG_DATA(nh);
1654 if (nh->nlmsg_type != RTM_NEWLINK ||
1655 (info->ifi_index != pmd->if_index &&
1656 info->ifi_index != pmd->remote_if_index))
1658 return tap_link_update(dev, 0);
1662 tap_dev_intr_handler(void *cb_arg)
1664 struct rte_eth_dev *dev = cb_arg;
1665 struct pmd_internals *pmd = dev->data->dev_private;
1667 if (rte_intr_fd_get(pmd->intr_handle) >= 0)
1668 tap_nl_recv(rte_intr_fd_get(pmd->intr_handle),
1669 tap_nl_msg_handler, dev);
1673 tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
1675 struct pmd_internals *pmd = dev->data->dev_private;
1678 /* In any case, disable interrupt if the conf is no longer there. */
1679 if (!dev->data->dev_conf.intr_conf.lsc) {
1680 if (rte_intr_fd_get(pmd->intr_handle) != -1)
1686 rte_intr_fd_set(pmd->intr_handle, tap_nl_init(RTMGRP_LINK));
1687 if (unlikely(rte_intr_fd_get(pmd->intr_handle) == -1))
1689 return rte_intr_callback_register(
1690 pmd->intr_handle, tap_dev_intr_handler, dev);
1695 ret = rte_intr_callback_unregister(pmd->intr_handle,
1696 tap_dev_intr_handler, dev);
1699 } else if (ret == -EAGAIN) {
1702 TAP_LOG(ERR, "intr callback unregister failed: %d",
1708 if (rte_intr_fd_get(pmd->intr_handle) >= 0) {
1709 tap_nl_final(rte_intr_fd_get(pmd->intr_handle));
1710 rte_intr_fd_set(pmd->intr_handle, -1);
1717 tap_intr_handle_set(struct rte_eth_dev *dev, int set)
1721 err = tap_lsc_intr_handle_set(dev, set);
1724 tap_rx_intr_vec_set(dev, 0);
1727 err = tap_rx_intr_vec_set(dev, set);
1729 tap_lsc_intr_handle_set(dev, 0);
1733 static const uint32_t*
1734 tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
1736 static const uint32_t ptypes[] = {
1737 RTE_PTYPE_INNER_L2_ETHER,
1738 RTE_PTYPE_INNER_L2_ETHER_VLAN,
1739 RTE_PTYPE_INNER_L2_ETHER_QINQ,
1740 RTE_PTYPE_INNER_L3_IPV4,
1741 RTE_PTYPE_INNER_L3_IPV4_EXT,
1742 RTE_PTYPE_INNER_L3_IPV6,
1743 RTE_PTYPE_INNER_L3_IPV6_EXT,
1744 RTE_PTYPE_INNER_L4_FRAG,
1745 RTE_PTYPE_INNER_L4_UDP,
1746 RTE_PTYPE_INNER_L4_TCP,
1747 RTE_PTYPE_INNER_L4_SCTP,
1749 RTE_PTYPE_L2_ETHER_VLAN,
1750 RTE_PTYPE_L2_ETHER_QINQ,
1752 RTE_PTYPE_L3_IPV4_EXT,
1753 RTE_PTYPE_L3_IPV6_EXT,
1765 tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1766 struct rte_eth_fc_conf *fc_conf)
1768 fc_conf->mode = RTE_ETH_FC_NONE;
1773 tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1774 struct rte_eth_fc_conf *fc_conf)
1776 if (fc_conf->mode != RTE_ETH_FC_NONE)
1782 * DPDK callback to update the RSS hash configuration.
1785 * Pointer to Ethernet device structure.
1786 * @param[in] rss_conf
1787 * RSS configuration data.
1790 * 0 on success, a negative errno value otherwise and rte_errno is set.
1793 tap_rss_hash_update(struct rte_eth_dev *dev,
1794 struct rte_eth_rss_conf *rss_conf)
1796 if (rss_conf->rss_hf & TAP_RSS_HF_MASK) {
1800 if (rss_conf->rss_key && rss_conf->rss_key_len) {
1802 * Currently TAP RSS key is hard coded
1803 * and cannot be updated
1806 "port %u RSS key cannot be updated",
1807 dev->data->port_id);
1815 tap_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1817 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1823 tap_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1825 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1831 tap_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1833 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1839 tap_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1841 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1845 static const struct eth_dev_ops ops = {
1846 .dev_start = tap_dev_start,
1847 .dev_stop = tap_dev_stop,
1848 .dev_close = tap_dev_close,
1849 .dev_configure = tap_dev_configure,
1850 .dev_infos_get = tap_dev_info,
1851 .rx_queue_setup = tap_rx_queue_setup,
1852 .tx_queue_setup = tap_tx_queue_setup,
1853 .rx_queue_start = tap_rx_queue_start,
1854 .tx_queue_start = tap_tx_queue_start,
1855 .rx_queue_stop = tap_rx_queue_stop,
1856 .tx_queue_stop = tap_tx_queue_stop,
1857 .rx_queue_release = tap_rx_queue_release,
1858 .tx_queue_release = tap_tx_queue_release,
1859 .flow_ctrl_get = tap_flow_ctrl_get,
1860 .flow_ctrl_set = tap_flow_ctrl_set,
1861 .link_update = tap_link_update,
1862 .dev_set_link_up = tap_link_set_up,
1863 .dev_set_link_down = tap_link_set_down,
1864 .promiscuous_enable = tap_promisc_enable,
1865 .promiscuous_disable = tap_promisc_disable,
1866 .allmulticast_enable = tap_allmulti_enable,
1867 .allmulticast_disable = tap_allmulti_disable,
1868 .mac_addr_set = tap_mac_set,
1869 .mtu_set = tap_mtu_set,
1870 .set_mc_addr_list = tap_set_mc_addr_list,
1871 .stats_get = tap_stats_get,
1872 .stats_reset = tap_stats_reset,
1873 .dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1874 .rss_hash_update = tap_rss_hash_update,
1875 .flow_ops_get = tap_dev_flow_ops_get,
1879 eth_dev_tap_create(struct rte_vdev_device *vdev, const char *tap_name,
1880 char *remote_iface, struct rte_ether_addr *mac_addr,
1881 enum rte_tuntap_type type)
1883 int numa_node = rte_socket_id();
1884 struct rte_eth_dev *dev;
1885 struct pmd_internals *pmd;
1886 struct pmd_process_private *process_private;
1887 const char *tuntap_name = tuntap_types[type];
1888 struct rte_eth_dev_data *data;
1892 TAP_LOG(DEBUG, "%s device on numa %u", tuntap_name, rte_socket_id());
1894 dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1896 TAP_LOG(ERR, "%s Unable to allocate device struct",
1898 goto error_exit_nodev;
1901 process_private = (struct pmd_process_private *)
1902 rte_zmalloc_socket(tap_name, sizeof(struct pmd_process_private),
1903 RTE_CACHE_LINE_SIZE, dev->device->numa_node);
1905 if (process_private == NULL) {
1906 TAP_LOG(ERR, "Failed to alloc memory for process private");
1909 pmd = dev->data->dev_private;
1910 dev->process_private = process_private;
1912 strlcpy(pmd->name, tap_name, sizeof(pmd->name));
1916 pmd->gso_ctx_mp = NULL;
1918 pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1919 if (pmd->ioctl_sock == -1) {
1921 "%s Unable to get a socket for management: %s",
1922 tuntap_name, strerror(errno));
1926 /* Allocate interrupt instance */
1927 pmd->intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
1928 if (pmd->intr_handle == NULL) {
1929 TAP_LOG(ERR, "Failed to allocate intr handle");
1933 /* Setup some default values */
1935 data->dev_private = pmd;
1936 data->dev_flags = RTE_ETH_DEV_INTR_LSC |
1937 RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1938 data->numa_node = numa_node;
1940 data->dev_link = pmd_link;
1941 data->mac_addrs = &pmd->eth_addr;
1942 /* Set the number of RX and TX queues */
1943 data->nb_rx_queues = 0;
1944 data->nb_tx_queues = 0;
1946 dev->dev_ops = &ops;
1947 dev->rx_pkt_burst = pmd_rx_burst;
1948 dev->tx_pkt_burst = pmd_tx_burst;
1950 rte_intr_type_set(pmd->intr_handle, RTE_INTR_HANDLE_EXT);
1951 rte_intr_fd_set(pmd->intr_handle, -1);
1952 dev->intr_handle = pmd->intr_handle;
1954 /* Presetup the fds to -1 as being not valid */
1955 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1956 process_private->rxq_fds[i] = -1;
1957 process_private->txq_fds[i] = -1;
1960 if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
1961 if (rte_is_zero_ether_addr(mac_addr))
1962 rte_eth_random_addr((uint8_t *)&pmd->eth_addr);
1964 rte_memcpy(&pmd->eth_addr, mac_addr, sizeof(*mac_addr));
1968 * Allocate a TUN device keep-alive file descriptor that will only be
1969 * closed when the TUN device itself is closed or removed.
1970 * This keep-alive file descriptor will guarantee that the TUN device
1971 * exists even when all of its queues are closed
1973 pmd->ka_fd = tun_alloc(pmd, 1);
1974 if (pmd->ka_fd == -1) {
1975 TAP_LOG(ERR, "Unable to create %s interface", tuntap_name);
1978 TAP_LOG(DEBUG, "allocated %s", pmd->name);
1980 ifr.ifr_mtu = dev->data->mtu;
1981 if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE) < 0)
1984 if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
1985 memset(&ifr, 0, sizeof(struct ifreq));
1986 ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1987 rte_memcpy(ifr.ifr_hwaddr.sa_data, &pmd->eth_addr,
1988 RTE_ETHER_ADDR_LEN);
1989 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
1994 * Set up everything related to rte_flow:
1996 * - tap / remote if_index
1997 * - mandatory QDISCs
1998 * - rte_flow actual/implicit lists
2001 pmd->nlsk_fd = tap_nl_init(0);
2002 if (pmd->nlsk_fd == -1) {
2003 TAP_LOG(WARNING, "%s: failed to create netlink socket.",
2005 goto disable_rte_flow;
2007 pmd->if_index = if_nametoindex(pmd->name);
2008 if (!pmd->if_index) {
2009 TAP_LOG(ERR, "%s: failed to get if_index.", pmd->name);
2010 goto disable_rte_flow;
2012 if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
2013 TAP_LOG(ERR, "%s: failed to create multiq qdisc.",
2015 goto disable_rte_flow;
2017 if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
2018 TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
2020 goto disable_rte_flow;
2022 LIST_INIT(&pmd->flows);
2024 if (strlen(remote_iface)) {
2025 pmd->remote_if_index = if_nametoindex(remote_iface);
2026 if (!pmd->remote_if_index) {
2027 TAP_LOG(ERR, "%s: failed to get %s if_index.",
2028 pmd->name, remote_iface);
2031 strlcpy(pmd->remote_iface, remote_iface, RTE_ETH_NAME_MAX_LEN);
2033 /* Save state of remote device */
2034 tap_ioctl(pmd, SIOCGIFFLAGS, &pmd->remote_initial_flags, 0, REMOTE_ONLY);
2036 /* Replicate remote MAC address */
2037 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) {
2038 TAP_LOG(ERR, "%s: failed to get %s MAC address.",
2039 pmd->name, pmd->remote_iface);
2042 rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
2043 RTE_ETHER_ADDR_LEN);
2044 /* The desired MAC is already in ifreq after SIOCGIFHWADDR. */
2045 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) {
2046 TAP_LOG(ERR, "%s: failed to get %s MAC address.",
2047 pmd->name, remote_iface);
2052 * Flush usually returns negative value because it tries to
2053 * delete every QDISC (and on a running device, one QDISC at
2054 * least is needed). Ignore negative return value.
2056 qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
2057 if (qdisc_create_ingress(pmd->nlsk_fd,
2058 pmd->remote_if_index) < 0) {
2059 TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
2063 LIST_INIT(&pmd->implicit_flows);
2064 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0 ||
2065 tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0 ||
2066 tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0 ||
2067 tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0) {
2069 "%s: failed to create implicit rules.",
2075 rte_eth_dev_probing_finish(dev);
2079 TAP_LOG(ERR, " Disabling rte flow support: %s(%d)",
2080 strerror(errno), errno);
2081 if (strlen(remote_iface)) {
2082 TAP_LOG(ERR, "Remote feature requires flow support.");
2085 rte_eth_dev_probing_finish(dev);
2089 TAP_LOG(ERR, " Can't set up remote feature: %s(%d)",
2090 strerror(errno), errno);
2091 tap_flow_implicit_flush(pmd, NULL);
2094 if (pmd->nlsk_fd != -1)
2095 close(pmd->nlsk_fd);
2096 if (pmd->ka_fd != -1)
2098 if (pmd->ioctl_sock != -1)
2099 close(pmd->ioctl_sock);
2100 /* mac_addrs must not be freed alone because part of dev_private */
2101 dev->data->mac_addrs = NULL;
2102 rte_eth_dev_release_port(dev);
2103 rte_intr_instance_free(pmd->intr_handle);
2106 TAP_LOG(ERR, "%s Unable to initialize %s",
2107 tuntap_name, rte_vdev_device_name(vdev));
2112 /* make sure name is a possible Linux network device name */
2114 is_valid_iface(const char *name)
2119 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
2123 if (*name == '/' || *name == ':' || isspace(*name))
2131 set_interface_name(const char *key __rte_unused,
2135 char *name = (char *)extra_args;
2138 if (!is_valid_iface(value)) {
2139 TAP_LOG(ERR, "TAP invalid remote interface name (%s)",
2143 strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
2145 /* use tap%d which causes kernel to choose next available */
2146 strlcpy(name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2152 set_remote_iface(const char *key __rte_unused,
2156 char *name = (char *)extra_args;
2159 if (!is_valid_iface(value)) {
2160 TAP_LOG(ERR, "TAP invalid remote interface name (%s)",
2164 strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
2170 static int parse_user_mac(struct rte_ether_addr *user_mac,
2173 unsigned int index = 0;
2174 char mac_temp[strlen(ETH_TAP_USR_MAC_FMT) + 1], *mac_byte = NULL;
2176 if (user_mac == NULL || value == NULL)
2179 strlcpy(mac_temp, value, sizeof(mac_temp));
2180 mac_byte = strtok(mac_temp, ":");
2182 while ((mac_byte != NULL) &&
2183 (strlen(mac_byte) <= 2) &&
2184 (strlen(mac_byte) == strspn(mac_byte,
2185 ETH_TAP_CMP_MAC_FMT))) {
2186 user_mac->addr_bytes[index++] = strtoul(mac_byte, NULL, 16);
2187 mac_byte = strtok(NULL, ":");
2194 set_mac_type(const char *key __rte_unused,
2198 struct rte_ether_addr *user_mac = extra_args;
2203 if (!strncasecmp(ETH_TAP_MAC_FIXED, value, strlen(ETH_TAP_MAC_FIXED))) {
2204 static int iface_idx;
2206 /* fixed mac = 00:64:74:61:70:<iface_idx> */
2207 memcpy((char *)user_mac->addr_bytes, "\0dtap",
2208 RTE_ETHER_ADDR_LEN);
2209 user_mac->addr_bytes[RTE_ETHER_ADDR_LEN - 1] =
2214 if (parse_user_mac(user_mac, value) != 6)
2217 TAP_LOG(DEBUG, "TAP user MAC param (%s)", value);
2221 TAP_LOG(ERR, "TAP user MAC (%s) is not in format (%s|%s)",
2222 value, ETH_TAP_MAC_FIXED, ETH_TAP_USR_MAC_FMT);
2227 * Open a TUN interface device. TUN PMD
2228 * 1) sets tap_type as false
2229 * 2) intakes iface as argument.
2230 * 3) as interface is virtual set speed to 10G
2233 rte_pmd_tun_probe(struct rte_vdev_device *dev)
2235 const char *name, *params;
2237 struct rte_kvargs *kvlist = NULL;
2238 char tun_name[RTE_ETH_NAME_MAX_LEN];
2239 char remote_iface[RTE_ETH_NAME_MAX_LEN];
2240 struct rte_eth_dev *eth_dev;
2242 name = rte_vdev_device_name(dev);
2243 params = rte_vdev_device_args(dev);
2244 memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2246 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
2247 strlen(params) == 0) {
2248 eth_dev = rte_eth_dev_attach_secondary(name);
2250 TAP_LOG(ERR, "Failed to probe %s", name);
2253 eth_dev->dev_ops = &ops;
2254 eth_dev->device = &dev->device;
2255 rte_eth_dev_probing_finish(eth_dev);
2259 /* use tun%d which causes kernel to choose next available */
2260 strlcpy(tun_name, DEFAULT_TUN_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2262 if (params && (params[0] != '\0')) {
2263 TAP_LOG(DEBUG, "parameters (%s)", params);
2265 kvlist = rte_kvargs_parse(params, valid_arguments);
2267 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2268 ret = rte_kvargs_process(kvlist,
2270 &set_interface_name,
2278 pmd_link.link_speed = RTE_ETH_SPEED_NUM_10G;
2280 TAP_LOG(DEBUG, "Initializing pmd_tun for %s", name);
2282 ret = eth_dev_tap_create(dev, tun_name, remote_iface, 0,
2283 ETH_TUNTAP_TYPE_TUN);
2287 TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2290 rte_kvargs_free(kvlist);
2295 /* Request queue file descriptors from secondary to primary. */
2297 tap_mp_attach_queues(const char *port_name, struct rte_eth_dev *dev)
2300 struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0};
2301 struct rte_mp_msg request, *reply;
2302 struct rte_mp_reply replies;
2303 struct ipc_queues *request_param = (struct ipc_queues *)request.param;
2304 struct ipc_queues *reply_param;
2305 struct pmd_process_private *process_private = dev->process_private;
2306 int queue, fd_iterator;
2308 /* Prepare the request */
2309 memset(&request, 0, sizeof(request));
2310 strlcpy(request.name, TAP_MP_KEY, sizeof(request.name));
2311 strlcpy(request_param->port_name, port_name,
2312 sizeof(request_param->port_name));
2313 request.len_param = sizeof(*request_param);
2314 /* Send request and receive reply */
2315 ret = rte_mp_request_sync(&request, &replies, &timeout);
2316 if (ret < 0 || replies.nb_received != 1) {
2317 TAP_LOG(ERR, "Failed to request queues from primary: %d",
2321 reply = &replies.msgs[0];
2322 reply_param = (struct ipc_queues *)reply->param;
2323 TAP_LOG(DEBUG, "Received IPC reply for %s", reply_param->port_name);
2325 /* Attach the queues from received file descriptors */
2326 if (reply_param->rxq_count + reply_param->txq_count != reply->num_fds) {
2327 TAP_LOG(ERR, "Unexpected number of fds received");
2331 dev->data->nb_rx_queues = reply_param->rxq_count;
2332 dev->data->nb_tx_queues = reply_param->txq_count;
2334 for (queue = 0; queue < reply_param->rxq_count; queue++)
2335 process_private->rxq_fds[queue] = reply->fds[fd_iterator++];
2336 for (queue = 0; queue < reply_param->txq_count; queue++)
2337 process_private->txq_fds[queue] = reply->fds[fd_iterator++];
2342 /* Send the queue file descriptors from the primary process to secondary. */
2344 tap_mp_sync_queues(const struct rte_mp_msg *request, const void *peer)
2346 struct rte_eth_dev *dev;
2347 struct pmd_process_private *process_private;
2348 struct rte_mp_msg reply;
2349 const struct ipc_queues *request_param =
2350 (const struct ipc_queues *)request->param;
2351 struct ipc_queues *reply_param =
2352 (struct ipc_queues *)reply.param;
2357 /* Get requested port */
2358 TAP_LOG(DEBUG, "Received IPC request for %s", request_param->port_name);
2359 ret = rte_eth_dev_get_port_by_name(request_param->port_name, &port_id);
2361 TAP_LOG(ERR, "Failed to get port id for %s",
2362 request_param->port_name);
2365 dev = &rte_eth_devices[port_id];
2366 process_private = dev->process_private;
2368 /* Fill file descriptors for all queues */
2370 reply_param->rxq_count = 0;
2371 if (dev->data->nb_rx_queues + dev->data->nb_tx_queues >
2373 TAP_LOG(ERR, "Number of rx/tx queues exceeds max number of fds");
2377 for (queue = 0; queue < dev->data->nb_rx_queues; queue++) {
2378 reply.fds[reply.num_fds++] = process_private->rxq_fds[queue];
2379 reply_param->rxq_count++;
2381 RTE_ASSERT(reply_param->rxq_count == dev->data->nb_rx_queues);
2383 reply_param->txq_count = 0;
2384 for (queue = 0; queue < dev->data->nb_tx_queues; queue++) {
2385 reply.fds[reply.num_fds++] = process_private->txq_fds[queue];
2386 reply_param->txq_count++;
2388 RTE_ASSERT(reply_param->txq_count == dev->data->nb_tx_queues);
2391 strlcpy(reply.name, request->name, sizeof(reply.name));
2392 strlcpy(reply_param->port_name, request_param->port_name,
2393 sizeof(reply_param->port_name));
2394 reply.len_param = sizeof(*reply_param);
2395 if (rte_mp_reply(&reply, peer) < 0) {
2396 TAP_LOG(ERR, "Failed to reply an IPC request to sync queues");
2402 /* Open a TAP interface device.
2405 rte_pmd_tap_probe(struct rte_vdev_device *dev)
2407 const char *name, *params;
2409 struct rte_kvargs *kvlist = NULL;
2411 char tap_name[RTE_ETH_NAME_MAX_LEN];
2412 char remote_iface[RTE_ETH_NAME_MAX_LEN];
2413 struct rte_ether_addr user_mac = { .addr_bytes = {0} };
2414 struct rte_eth_dev *eth_dev;
2415 int tap_devices_count_increased = 0;
2417 name = rte_vdev_device_name(dev);
2418 params = rte_vdev_device_args(dev);
2420 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
2421 eth_dev = rte_eth_dev_attach_secondary(name);
2423 TAP_LOG(ERR, "Failed to probe %s", name);
2426 eth_dev->dev_ops = &ops;
2427 eth_dev->device = &dev->device;
2428 eth_dev->rx_pkt_burst = pmd_rx_burst;
2429 eth_dev->tx_pkt_burst = pmd_tx_burst;
2430 if (!rte_eal_primary_proc_alive(NULL)) {
2431 TAP_LOG(ERR, "Primary process is missing");
2434 eth_dev->process_private = (struct pmd_process_private *)
2435 rte_zmalloc_socket(name,
2436 sizeof(struct pmd_process_private),
2437 RTE_CACHE_LINE_SIZE,
2438 eth_dev->device->numa_node);
2439 if (eth_dev->process_private == NULL) {
2441 "Failed to alloc memory for process private");
2445 ret = tap_mp_attach_queues(name, eth_dev);
2448 rte_eth_dev_probing_finish(eth_dev);
2452 speed = RTE_ETH_SPEED_NUM_10G;
2454 /* use tap%d which causes kernel to choose next available */
2455 strlcpy(tap_name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2456 memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2458 if (params && (params[0] != '\0')) {
2459 TAP_LOG(DEBUG, "parameters (%s)", params);
2461 kvlist = rte_kvargs_parse(params, valid_arguments);
2463 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2464 ret = rte_kvargs_process(kvlist,
2466 &set_interface_name,
2472 if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
2473 ret = rte_kvargs_process(kvlist,
2481 if (rte_kvargs_count(kvlist, ETH_TAP_MAC_ARG) == 1) {
2482 ret = rte_kvargs_process(kvlist,
2491 pmd_link.link_speed = speed;
2493 TAP_LOG(DEBUG, "Initializing pmd_tap for %s", name);
2495 /* Register IPC feed callback */
2496 if (!tap_devices_count) {
2497 ret = rte_mp_action_register(TAP_MP_KEY, tap_mp_sync_queues);
2498 if (ret < 0 && rte_errno != ENOTSUP) {
2499 TAP_LOG(ERR, "tap: Failed to register IPC callback: %s",
2500 strerror(rte_errno));
2504 tap_devices_count++;
2505 tap_devices_count_increased = 1;
2506 ret = eth_dev_tap_create(dev, tap_name, remote_iface, &user_mac,
2507 ETH_TUNTAP_TYPE_TAP);
2511 TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2513 if (tap_devices_count_increased == 1) {
2514 if (tap_devices_count == 1)
2515 rte_mp_action_unregister(TAP_MP_KEY);
2516 tap_devices_count--;
2519 rte_kvargs_free(kvlist);
2524 /* detach a TUNTAP device.
2527 rte_pmd_tap_remove(struct rte_vdev_device *dev)
2529 struct rte_eth_dev *eth_dev = NULL;
2531 /* find the ethdev entry */
2532 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
2536 tap_dev_close(eth_dev);
2537 rte_eth_dev_release_port(eth_dev);
2542 static struct rte_vdev_driver pmd_tun_drv = {
2543 .probe = rte_pmd_tun_probe,
2544 .remove = rte_pmd_tap_remove,
2547 static struct rte_vdev_driver pmd_tap_drv = {
2548 .probe = rte_pmd_tap_probe,
2549 .remove = rte_pmd_tap_remove,
2552 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
2553 RTE_PMD_REGISTER_VDEV(net_tun, pmd_tun_drv);
2554 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
2555 RTE_PMD_REGISTER_PARAM_STRING(net_tun,
2556 ETH_TAP_IFACE_ARG "=<string> ");
2557 RTE_PMD_REGISTER_PARAM_STRING(net_tap,
2558 ETH_TAP_IFACE_ARG "=<string> "
2559 ETH_TAP_MAC_ARG "=" ETH_TAP_MAC_ARG_FMT " "
2560 ETH_TAP_REMOTE_ARG "=<string>");
2561 RTE_LOG_REGISTER_DEFAULT(tap_logtype, NOTICE);