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 <rte_ethdev_driver.h>
11 #include <rte_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>
22 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
34 #include <arpa/inet.h>
36 #include <linux/if_tun.h>
37 #include <linux/if_ether.h>
42 #include <rte_eth_tap.h>
44 #include <tap_netlink.h>
45 #include <tap_tcmsgs.h>
47 /* Linux based path to the TUN device */
48 #define TUN_TAP_DEV_PATH "/dev/net/tun"
49 #define DEFAULT_TAP_NAME "dtap"
50 #define DEFAULT_TUN_NAME "dtun"
52 #define ETH_TAP_IFACE_ARG "iface"
53 #define ETH_TAP_REMOTE_ARG "remote"
54 #define ETH_TAP_MAC_ARG "mac"
55 #define ETH_TAP_MAC_FIXED "fixed"
57 #define ETH_TAP_USR_MAC_FMT "xx:xx:xx:xx:xx:xx"
58 #define ETH_TAP_CMP_MAC_FMT "0123456789ABCDEFabcdef"
59 #define ETH_TAP_MAC_ARG_FMT ETH_TAP_MAC_FIXED "|" ETH_TAP_USR_MAC_FMT
61 #define TAP_GSO_MBUFS_PER_CORE 128
62 #define TAP_GSO_MBUF_SEG_SIZE 128
63 #define TAP_GSO_MBUF_CACHE_SIZE 4
64 #define TAP_GSO_MBUFS_NUM \
65 (TAP_GSO_MBUFS_PER_CORE * TAP_GSO_MBUF_CACHE_SIZE)
67 /* IPC key for queue fds sync */
68 #define TAP_MP_KEY "tap_mp_sync_queues"
70 #define TAP_IOV_DEFAULT_MAX 1024
72 static int tap_devices_count;
74 static const char *valid_arguments[] = {
81 static volatile uint32_t tap_trigger; /* Rx trigger */
83 static struct rte_eth_link pmd_link = {
84 .link_speed = ETH_SPEED_NUM_10G,
85 .link_duplex = ETH_LINK_FULL_DUPLEX,
86 .link_status = ETH_LINK_DOWN,
87 .link_autoneg = ETH_LINK_FIXED,
91 tap_trigger_cb(int sig __rte_unused)
93 /* Valid trigger values are nonzero */
94 tap_trigger = (tap_trigger + 1) | 0x80000000;
97 /* Specifies on what netdevices the ioctl should be applied */
104 /* Message header to synchronize queues via IPC */
106 char port_name[RTE_DEV_NAME_MAX_LEN];
110 * The file descriptors are in the dedicated part
111 * of the Unix message to be translated by the kernel.
115 static int tap_intr_handle_set(struct rte_eth_dev *dev, int set);
118 * Tun/Tap allocation routine
121 * Pointer to private structure.
123 * @param[in] is_keepalive
127 * -1 on failure, fd on success
130 tun_alloc(struct pmd_internals *pmd, int is_keepalive)
133 #ifdef IFF_MULTI_QUEUE
134 unsigned int features;
138 memset(&ifr, 0, sizeof(struct ifreq));
141 * Do not set IFF_NO_PI as packet information header will be needed
142 * to check if a received packet has been truncated.
144 ifr.ifr_flags = (pmd->type == ETH_TUNTAP_TYPE_TAP) ?
145 IFF_TAP : IFF_TUN | IFF_POINTOPOINT;
146 strlcpy(ifr.ifr_name, pmd->name, IFNAMSIZ);
148 fd = open(TUN_TAP_DEV_PATH, O_RDWR);
150 TAP_LOG(ERR, "Unable to open %s interface", TUN_TAP_DEV_PATH);
154 #ifdef IFF_MULTI_QUEUE
155 /* Grab the TUN features to verify we can work multi-queue */
156 if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
157 TAP_LOG(ERR, "unable to get TUN/TAP features");
160 TAP_LOG(DEBUG, "%s Features %08x", TUN_TAP_DEV_PATH, features);
162 if (features & IFF_MULTI_QUEUE) {
163 TAP_LOG(DEBUG, " Multi-queue support for %d queues",
164 RTE_PMD_TAP_MAX_QUEUES);
165 ifr.ifr_flags |= IFF_MULTI_QUEUE;
169 ifr.ifr_flags |= IFF_ONE_QUEUE;
170 TAP_LOG(DEBUG, " Single queue only support");
173 /* Set the TUN/TAP configuration and set the name if needed */
174 if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
175 TAP_LOG(WARNING, "Unable to set TUNSETIFF for %s: %s",
176 ifr.ifr_name, strerror(errno));
181 * Name passed to kernel might be wildcard like dtun%d
182 * and need to find the resulting device.
184 TAP_LOG(DEBUG, "Device name is '%s'", ifr.ifr_name);
185 strlcpy(pmd->name, ifr.ifr_name, RTE_ETH_NAME_MAX_LEN);
189 * Detach the TUN/TAP keep-alive queue
190 * to avoid traffic through it
192 ifr.ifr_flags = IFF_DETACH_QUEUE;
193 if (ioctl(fd, TUNSETQUEUE, (void *)&ifr) < 0) {
195 "Unable to detach keep-alive queue for %s: %s",
196 ifr.ifr_name, strerror(errno));
201 /* Always set the file descriptor to non-blocking */
202 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
204 "Unable to set %s to nonblocking: %s",
205 ifr.ifr_name, strerror(errno));
209 /* Set up trigger to optimize empty Rx bursts */
213 int flags = fcntl(fd, F_GETFL);
215 if (flags == -1 || sigaction(SIGIO, NULL, &sa) == -1)
217 if (sa.sa_handler != tap_trigger_cb) {
219 * Make sure SIGIO is not already taken. This is done
220 * as late as possible to leave the application a
221 * chance to set up its own signal handler first.
223 if (sa.sa_handler != SIG_IGN &&
224 sa.sa_handler != SIG_DFL) {
228 sa = (struct sigaction){
229 .sa_flags = SA_RESTART,
230 .sa_handler = tap_trigger_cb,
232 if (sigaction(SIGIO, &sa, NULL) == -1)
235 /* Enable SIGIO on file descriptor */
236 fcntl(fd, F_SETFL, flags | O_ASYNC);
237 fcntl(fd, F_SETOWN, getpid());
241 /* Disable trigger globally in case of error */
243 TAP_LOG(WARNING, "Rx trigger disabled: %s",
256 tap_verify_csum(struct rte_mbuf *mbuf)
258 uint32_t l2 = mbuf->packet_type & RTE_PTYPE_L2_MASK;
259 uint32_t l3 = mbuf->packet_type & RTE_PTYPE_L3_MASK;
260 uint32_t l4 = mbuf->packet_type & RTE_PTYPE_L4_MASK;
261 unsigned int l2_len = sizeof(struct rte_ether_hdr);
267 if (l2 == RTE_PTYPE_L2_ETHER_VLAN)
269 else if (l2 == RTE_PTYPE_L2_ETHER_QINQ)
271 /* Don't verify checksum for packets with discontinuous L2 header */
272 if (unlikely(l2_len + sizeof(struct rte_ipv4_hdr) >
273 rte_pktmbuf_data_len(mbuf)))
275 l3_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len);
276 if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
277 struct rte_ipv4_hdr *iph = l3_hdr;
279 /* ihl contains the number of 4-byte words in the header */
280 l3_len = 4 * (iph->version_ihl & 0xf);
281 if (unlikely(l2_len + l3_len > rte_pktmbuf_data_len(mbuf)))
283 /* check that the total length reported by header is not
284 * greater than the total received size
286 if (l2_len + rte_be_to_cpu_16(iph->total_length) >
287 rte_pktmbuf_data_len(mbuf))
290 cksum = ~rte_raw_cksum(iph, l3_len);
291 mbuf->ol_flags |= cksum ?
292 PKT_RX_IP_CKSUM_BAD :
293 PKT_RX_IP_CKSUM_GOOD;
294 } else if (l3 == RTE_PTYPE_L3_IPV6) {
295 struct rte_ipv6_hdr *iph = l3_hdr;
297 l3_len = sizeof(struct rte_ipv6_hdr);
298 /* check that the total length reported by header is not
299 * greater than the total received size
301 if (l2_len + l3_len + rte_be_to_cpu_16(iph->payload_len) >
302 rte_pktmbuf_data_len(mbuf))
305 /* IPv6 extensions are not supported */
308 if (l4 == RTE_PTYPE_L4_UDP || l4 == RTE_PTYPE_L4_TCP) {
309 l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len);
310 /* Don't verify checksum for multi-segment packets. */
311 if (mbuf->nb_segs > 1)
313 if (l3 == RTE_PTYPE_L3_IPV4)
314 cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
315 else if (l3 == RTE_PTYPE_L3_IPV6)
316 cksum = ~rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
317 mbuf->ol_flags |= cksum ?
318 PKT_RX_L4_CKSUM_BAD :
319 PKT_RX_L4_CKSUM_GOOD;
324 tap_rx_offload_get_port_capa(void)
327 * No specific port Rx offload capabilities.
333 tap_rx_offload_get_queue_capa(void)
335 return DEV_RX_OFFLOAD_SCATTER |
336 DEV_RX_OFFLOAD_IPV4_CKSUM |
337 DEV_RX_OFFLOAD_UDP_CKSUM |
338 DEV_RX_OFFLOAD_TCP_CKSUM;
341 /* Callback to handle the rx burst of packets to the correct interface and
342 * file descriptor(s) in a multi-queue setup.
345 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
347 struct rx_queue *rxq = queue;
348 struct pmd_process_private *process_private;
350 unsigned long num_rx_bytes = 0;
351 uint32_t trigger = tap_trigger;
353 if (trigger == rxq->trigger_seen)
356 process_private = rte_eth_devices[rxq->in_port].process_private;
357 for (num_rx = 0; num_rx < nb_pkts; ) {
358 struct rte_mbuf *mbuf = rxq->pool;
359 struct rte_mbuf *seg = NULL;
360 struct rte_mbuf *new_tail = NULL;
361 uint16_t data_off = rte_pktmbuf_headroom(mbuf);
364 len = readv(process_private->rxq_fds[rxq->queue_id],
366 1 + (rxq->rxmode->offloads & DEV_RX_OFFLOAD_SCATTER ?
367 rxq->nb_rx_desc : 1));
368 if (len < (int)sizeof(struct tun_pi))
371 /* Packet couldn't fit in the provided mbuf */
372 if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) {
373 rxq->stats.ierrors++;
377 len -= sizeof(struct tun_pi);
380 mbuf->port = rxq->in_port;
382 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
384 if (unlikely(!buf)) {
385 rxq->stats.rx_nombuf++;
386 /* No new buf has been allocated: do nothing */
387 if (!new_tail || !seg)
391 rte_pktmbuf_free(mbuf);
395 seg = seg ? seg->next : mbuf;
396 if (rxq->pool == mbuf)
399 new_tail->next = buf;
401 new_tail->next = seg->next;
403 /* iovecs[0] is reserved for packet info (pi) */
404 (*rxq->iovecs)[mbuf->nb_segs].iov_len =
405 buf->buf_len - data_off;
406 (*rxq->iovecs)[mbuf->nb_segs].iov_base =
407 (char *)buf->buf_addr + data_off;
409 seg->data_len = RTE_MIN(seg->buf_len - data_off, len);
410 seg->data_off = data_off;
412 len -= seg->data_len;
416 /* First segment has headroom, not the others */
420 mbuf->packet_type = rte_net_get_ptype(mbuf, NULL,
422 if (rxq->rxmode->offloads & DEV_RX_OFFLOAD_CHECKSUM)
423 tap_verify_csum(mbuf);
425 /* account for the receive frame */
426 bufs[num_rx++] = mbuf;
427 num_rx_bytes += mbuf->pkt_len;
430 rxq->stats.ipackets += num_rx;
431 rxq->stats.ibytes += num_rx_bytes;
433 if (trigger && num_rx < nb_pkts)
434 rxq->trigger_seen = trigger;
440 tap_tx_offload_get_port_capa(void)
443 * No specific port Tx offload capabilities.
449 tap_tx_offload_get_queue_capa(void)
451 return DEV_TX_OFFLOAD_MULTI_SEGS |
452 DEV_TX_OFFLOAD_IPV4_CKSUM |
453 DEV_TX_OFFLOAD_UDP_CKSUM |
454 DEV_TX_OFFLOAD_TCP_CKSUM |
455 DEV_TX_OFFLOAD_TCP_TSO;
458 /* Finalize l4 checksum calculation */
460 tap_tx_l4_cksum(uint16_t *l4_cksum, uint16_t l4_phdr_cksum,
461 uint32_t l4_raw_cksum)
466 cksum = __rte_raw_cksum_reduce(l4_raw_cksum);
467 cksum += l4_phdr_cksum;
469 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
470 cksum = (~cksum) & 0xffff;
477 /* Accumaulate L4 raw checksums */
479 tap_tx_l4_add_rcksum(char *l4_data, unsigned int l4_len, uint16_t *l4_cksum,
480 uint32_t *l4_raw_cksum)
482 if (l4_cksum == NULL)
485 *l4_raw_cksum = __rte_raw_cksum(l4_data, l4_len, *l4_raw_cksum);
488 /* L3 and L4 pseudo headers checksum offloads */
490 tap_tx_l3_cksum(char *packet, uint64_t ol_flags, unsigned int l2_len,
491 unsigned int l3_len, unsigned int l4_len, uint16_t **l4_cksum,
492 uint16_t *l4_phdr_cksum, uint32_t *l4_raw_cksum)
494 void *l3_hdr = packet + l2_len;
496 if (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4)) {
497 struct rte_ipv4_hdr *iph = l3_hdr;
500 iph->hdr_checksum = 0;
501 cksum = rte_raw_cksum(iph, l3_len);
502 iph->hdr_checksum = (cksum == 0xffff) ? cksum : ~cksum;
504 if (ol_flags & PKT_TX_L4_MASK) {
507 l4_hdr = packet + l2_len + l3_len;
508 if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM)
509 *l4_cksum = &((struct rte_udp_hdr *)l4_hdr)->dgram_cksum;
510 else if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM)
511 *l4_cksum = &((struct rte_tcp_hdr *)l4_hdr)->cksum;
515 if (ol_flags & PKT_TX_IPV4)
516 *l4_phdr_cksum = rte_ipv4_phdr_cksum(l3_hdr, 0);
518 *l4_phdr_cksum = rte_ipv6_phdr_cksum(l3_hdr, 0);
519 *l4_raw_cksum = __rte_raw_cksum(l4_hdr, l4_len, 0);
524 tap_write_mbufs(struct tx_queue *txq, uint16_t num_mbufs,
525 struct rte_mbuf **pmbufs,
526 uint16_t *num_packets, unsigned long *num_tx_bytes)
530 struct pmd_process_private *process_private;
532 process_private = rte_eth_devices[txq->out_port].process_private;
534 for (i = 0; i < num_mbufs; i++) {
535 struct rte_mbuf *mbuf = pmbufs[i];
536 struct iovec iovecs[mbuf->nb_segs + 2];
537 struct tun_pi pi = { .flags = 0, .proto = 0x00 };
538 struct rte_mbuf *seg = mbuf;
539 char m_copy[mbuf->data_len];
543 int k; /* current index in iovecs for copying segments */
544 uint16_t seg_len; /* length of first segment */
546 uint16_t *l4_cksum; /* l4 checksum (pseudo header + payload) */
547 uint32_t l4_raw_cksum = 0; /* TCP/UDP payload raw checksum */
548 uint16_t l4_phdr_cksum = 0; /* TCP/UDP pseudo header checksum */
549 uint16_t is_cksum = 0; /* in case cksum should be offloaded */
552 if (txq->type == ETH_TUNTAP_TYPE_TUN) {
554 * TUN and TAP are created with IFF_NO_PI disabled.
555 * For TUN PMD this mandatory as fields are used by
556 * Kernel tun.c to determine whether its IP or non IP
559 * The logic fetches the first byte of data from mbuf
560 * then compares whether its v4 or v6. If first byte
561 * is 4 or 6, then protocol field is updated.
563 char *buff_data = rte_pktmbuf_mtod(seg, void *);
564 proto = (*buff_data & 0xf0);
565 pi.proto = (proto == 0x40) ?
566 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) :
568 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) :
573 iovecs[k].iov_base = π
574 iovecs[k].iov_len = sizeof(pi);
577 nb_segs = mbuf->nb_segs;
579 ((mbuf->ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4) ||
580 (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM ||
581 (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM))) {
584 /* Support only packets with at least layer 4
585 * header included in the first segment
587 seg_len = rte_pktmbuf_data_len(mbuf);
588 l234_hlen = mbuf->l2_len + mbuf->l3_len + mbuf->l4_len;
589 if (seg_len < l234_hlen)
592 /* To change checksums, work on a * copy of l2, l3
593 * headers + l4 pseudo header
595 rte_memcpy(m_copy, rte_pktmbuf_mtod(mbuf, void *),
597 tap_tx_l3_cksum(m_copy, mbuf->ol_flags,
598 mbuf->l2_len, mbuf->l3_len, mbuf->l4_len,
599 &l4_cksum, &l4_phdr_cksum,
601 iovecs[k].iov_base = m_copy;
602 iovecs[k].iov_len = l234_hlen;
605 /* Update next iovecs[] beyond l2, l3, l4 headers */
606 if (seg_len > l234_hlen) {
607 iovecs[k].iov_len = seg_len - l234_hlen;
609 rte_pktmbuf_mtod(seg, char *) +
611 tap_tx_l4_add_rcksum(iovecs[k].iov_base,
612 iovecs[k].iov_len, l4_cksum,
620 for (j = k; j <= nb_segs; j++) {
621 iovecs[j].iov_len = rte_pktmbuf_data_len(seg);
622 iovecs[j].iov_base = rte_pktmbuf_mtod(seg, void *);
624 tap_tx_l4_add_rcksum(iovecs[j].iov_base,
625 iovecs[j].iov_len, l4_cksum,
631 tap_tx_l4_cksum(l4_cksum, l4_phdr_cksum, l4_raw_cksum);
633 /* copy the tx frame data */
634 n = writev(process_private->txq_fds[txq->queue_id], iovecs, j);
639 (*num_tx_bytes) += rte_pktmbuf_pkt_len(mbuf);
644 /* Callback to handle sending packets from the tap interface
647 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
649 struct tx_queue *txq = queue;
651 uint16_t num_packets = 0;
652 unsigned long num_tx_bytes = 0;
656 if (unlikely(nb_pkts == 0))
659 struct rte_mbuf *gso_mbufs[MAX_GSO_MBUFS];
660 max_size = *txq->mtu + (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN + 4);
661 for (i = 0; i < nb_pkts; i++) {
662 struct rte_mbuf *mbuf_in = bufs[num_tx];
663 struct rte_mbuf **mbuf;
664 uint16_t num_mbufs = 0;
665 uint16_t tso_segsz = 0;
671 tso = mbuf_in->ol_flags & PKT_TX_TCP_SEG;
673 struct rte_gso_ctx *gso_ctx = &txq->gso_ctx;
675 /* TCP segmentation implies TCP checksum offload */
676 mbuf_in->ol_flags |= PKT_TX_TCP_CKSUM;
678 /* gso size is calculated without RTE_ETHER_CRC_LEN */
679 hdrs_len = mbuf_in->l2_len + mbuf_in->l3_len +
681 tso_segsz = mbuf_in->tso_segsz + hdrs_len;
682 if (unlikely(tso_segsz == hdrs_len) ||
683 tso_segsz > *txq->mtu) {
687 gso_ctx->gso_size = tso_segsz;
688 /* 'mbuf_in' packet to segment */
689 num_tso_mbufs = rte_gso_segment(mbuf_in,
690 gso_ctx, /* gso control block */
691 (struct rte_mbuf **)&gso_mbufs, /* out mbufs */
692 RTE_DIM(gso_mbufs)); /* max tso mbufs */
694 /* ret contains the number of new created mbufs */
695 if (num_tso_mbufs < 0)
699 num_mbufs = num_tso_mbufs;
701 /* stats.errs will be incremented */
702 if (rte_pktmbuf_pkt_len(mbuf_in) > max_size)
705 /* ret 0 indicates no new mbufs were created */
711 ret = tap_write_mbufs(txq, num_mbufs, mbuf,
712 &num_packets, &num_tx_bytes);
716 if (num_tso_mbufs > 0)
717 rte_pktmbuf_free_bulk(mbuf, num_tso_mbufs);
721 /* free original mbuf */
722 rte_pktmbuf_free(mbuf_in);
724 if (num_tso_mbufs > 0)
725 rte_pktmbuf_free_bulk(mbuf, num_tso_mbufs);
728 txq->stats.opackets += num_packets;
729 txq->stats.errs += nb_pkts - num_tx;
730 txq->stats.obytes += num_tx_bytes;
736 tap_ioctl_req2str(unsigned long request)
740 return "SIOCSIFFLAGS";
742 return "SIOCGIFFLAGS";
744 return "SIOCGIFHWADDR";
746 return "SIOCSIFHWADDR";
754 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
755 struct ifreq *ifr, int set, enum ioctl_mode mode)
757 short req_flags = ifr->ifr_flags;
758 int remote = pmd->remote_if_index &&
759 (mode == REMOTE_ONLY || mode == LOCAL_AND_REMOTE);
761 if (!pmd->remote_if_index && mode == REMOTE_ONLY)
764 * If there is a remote netdevice, apply ioctl on it, then apply it on
769 strlcpy(ifr->ifr_name, pmd->remote_iface, IFNAMSIZ);
770 else if (mode == LOCAL_ONLY || mode == LOCAL_AND_REMOTE)
771 strlcpy(ifr->ifr_name, pmd->name, IFNAMSIZ);
774 /* fetch current flags to leave other flags untouched */
775 if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
778 ifr->ifr_flags |= req_flags;
780 ifr->ifr_flags &= ~req_flags;
788 TAP_LOG(WARNING, "%s: ioctl() called with wrong arg",
792 if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
794 if (remote-- && mode == LOCAL_AND_REMOTE)
799 TAP_LOG(DEBUG, "%s(%s) failed: %s(%d)", ifr->ifr_name,
800 tap_ioctl_req2str(request), strerror(errno), errno);
805 tap_link_set_down(struct rte_eth_dev *dev)
807 struct pmd_internals *pmd = dev->data->dev_private;
808 struct ifreq ifr = { .ifr_flags = IFF_UP };
810 dev->data->dev_link.link_status = ETH_LINK_DOWN;
811 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_ONLY);
815 tap_link_set_up(struct rte_eth_dev *dev)
817 struct pmd_internals *pmd = dev->data->dev_private;
818 struct ifreq ifr = { .ifr_flags = IFF_UP };
820 dev->data->dev_link.link_status = ETH_LINK_UP;
821 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
825 tap_dev_start(struct rte_eth_dev *dev)
829 err = tap_intr_handle_set(dev, 1);
833 err = tap_link_set_up(dev);
837 for (i = 0; i < dev->data->nb_tx_queues; i++)
838 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
839 for (i = 0; i < dev->data->nb_rx_queues; i++)
840 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
845 /* This function gets called when the current port gets stopped.
848 tap_dev_stop(struct rte_eth_dev *dev)
852 for (i = 0; i < dev->data->nb_tx_queues; i++)
853 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
854 for (i = 0; i < dev->data->nb_rx_queues; i++)
855 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
857 tap_intr_handle_set(dev, 0);
858 tap_link_set_down(dev);
862 tap_dev_configure(struct rte_eth_dev *dev)
864 struct pmd_internals *pmd = dev->data->dev_private;
866 if (dev->data->nb_rx_queues > RTE_PMD_TAP_MAX_QUEUES) {
868 "%s: number of rx queues %d exceeds max num of queues %d",
870 dev->data->nb_rx_queues,
871 RTE_PMD_TAP_MAX_QUEUES);
874 if (dev->data->nb_tx_queues > RTE_PMD_TAP_MAX_QUEUES) {
876 "%s: number of tx queues %d exceeds max num of queues %d",
878 dev->data->nb_tx_queues,
879 RTE_PMD_TAP_MAX_QUEUES);
883 TAP_LOG(INFO, "%s: %s: TX configured queues number: %u",
884 dev->device->name, pmd->name, dev->data->nb_tx_queues);
886 TAP_LOG(INFO, "%s: %s: RX configured queues number: %u",
887 dev->device->name, pmd->name, dev->data->nb_rx_queues);
893 tap_dev_speed_capa(void)
895 uint32_t speed = pmd_link.link_speed;
898 if (speed >= ETH_SPEED_NUM_10M)
899 capa |= ETH_LINK_SPEED_10M;
900 if (speed >= ETH_SPEED_NUM_100M)
901 capa |= ETH_LINK_SPEED_100M;
902 if (speed >= ETH_SPEED_NUM_1G)
903 capa |= ETH_LINK_SPEED_1G;
904 if (speed >= ETH_SPEED_NUM_5G)
905 capa |= ETH_LINK_SPEED_2_5G;
906 if (speed >= ETH_SPEED_NUM_5G)
907 capa |= ETH_LINK_SPEED_5G;
908 if (speed >= ETH_SPEED_NUM_10G)
909 capa |= ETH_LINK_SPEED_10G;
910 if (speed >= ETH_SPEED_NUM_20G)
911 capa |= ETH_LINK_SPEED_20G;
912 if (speed >= ETH_SPEED_NUM_25G)
913 capa |= ETH_LINK_SPEED_25G;
914 if (speed >= ETH_SPEED_NUM_40G)
915 capa |= ETH_LINK_SPEED_40G;
916 if (speed >= ETH_SPEED_NUM_50G)
917 capa |= ETH_LINK_SPEED_50G;
918 if (speed >= ETH_SPEED_NUM_56G)
919 capa |= ETH_LINK_SPEED_56G;
920 if (speed >= ETH_SPEED_NUM_100G)
921 capa |= ETH_LINK_SPEED_100G;
927 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
929 struct pmd_internals *internals = dev->data->dev_private;
931 dev_info->if_index = internals->if_index;
932 dev_info->max_mac_addrs = 1;
933 dev_info->max_rx_pktlen = (uint32_t)RTE_ETHER_MAX_VLAN_FRAME_LEN;
934 dev_info->max_rx_queues = RTE_PMD_TAP_MAX_QUEUES;
935 dev_info->max_tx_queues = RTE_PMD_TAP_MAX_QUEUES;
936 dev_info->min_rx_bufsize = 0;
937 dev_info->speed_capa = tap_dev_speed_capa();
938 dev_info->rx_queue_offload_capa = tap_rx_offload_get_queue_capa();
939 dev_info->rx_offload_capa = tap_rx_offload_get_port_capa() |
940 dev_info->rx_queue_offload_capa;
941 dev_info->tx_queue_offload_capa = tap_tx_offload_get_queue_capa();
942 dev_info->tx_offload_capa = tap_tx_offload_get_port_capa() |
943 dev_info->tx_queue_offload_capa;
944 dev_info->hash_key_size = TAP_RSS_HASH_KEY_SIZE;
946 * limitation: TAP supports all of IP, UDP and TCP hash
947 * functions together and not in partial combinations
949 dev_info->flow_type_rss_offloads = ~TAP_RSS_HF_MASK;
955 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
957 unsigned int i, imax;
958 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
959 unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
960 unsigned long rx_nombuf = 0, ierrors = 0;
961 const struct pmd_internals *pmd = dev->data->dev_private;
963 /* rx queue statistics */
964 imax = (dev->data->nb_rx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
965 dev->data->nb_rx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
966 for (i = 0; i < imax; i++) {
967 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
968 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
969 rx_total += tap_stats->q_ipackets[i];
970 rx_bytes_total += tap_stats->q_ibytes[i];
971 rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
972 ierrors += pmd->rxq[i].stats.ierrors;
975 /* tx queue statistics */
976 imax = (dev->data->nb_tx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
977 dev->data->nb_tx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
979 for (i = 0; i < imax; i++) {
980 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
981 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
982 tx_total += tap_stats->q_opackets[i];
983 tx_err_total += pmd->txq[i].stats.errs;
984 tx_bytes_total += tap_stats->q_obytes[i];
987 tap_stats->ipackets = rx_total;
988 tap_stats->ibytes = rx_bytes_total;
989 tap_stats->ierrors = ierrors;
990 tap_stats->rx_nombuf = rx_nombuf;
991 tap_stats->opackets = tx_total;
992 tap_stats->oerrors = tx_err_total;
993 tap_stats->obytes = tx_bytes_total;
998 tap_stats_reset(struct rte_eth_dev *dev)
1001 struct pmd_internals *pmd = dev->data->dev_private;
1003 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1004 pmd->rxq[i].stats.ipackets = 0;
1005 pmd->rxq[i].stats.ibytes = 0;
1006 pmd->rxq[i].stats.ierrors = 0;
1007 pmd->rxq[i].stats.rx_nombuf = 0;
1009 pmd->txq[i].stats.opackets = 0;
1010 pmd->txq[i].stats.errs = 0;
1011 pmd->txq[i].stats.obytes = 0;
1018 tap_dev_close(struct rte_eth_dev *dev)
1021 struct pmd_internals *internals = dev->data->dev_private;
1022 struct pmd_process_private *process_private = dev->process_private;
1023 struct rx_queue *rxq;
1025 tap_link_set_down(dev);
1026 if (internals->nlsk_fd != -1) {
1027 tap_flow_flush(dev, NULL);
1028 tap_flow_implicit_flush(internals, NULL);
1029 tap_nl_final(internals->nlsk_fd);
1030 internals->nlsk_fd = -1;
1033 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1034 if (process_private->rxq_fds[i] != -1) {
1035 rxq = &internals->rxq[i];
1036 close(process_private->rxq_fds[i]);
1037 process_private->rxq_fds[i] = -1;
1038 rte_pktmbuf_free(rxq->pool);
1039 rte_free(rxq->iovecs);
1043 if (process_private->txq_fds[i] != -1) {
1044 close(process_private->txq_fds[i]);
1045 process_private->txq_fds[i] = -1;
1049 if (internals->remote_if_index) {
1050 /* Restore initial remote state */
1051 ioctl(internals->ioctl_sock, SIOCSIFFLAGS,
1052 &internals->remote_initial_flags);
1055 if (internals->ka_fd != -1) {
1056 close(internals->ka_fd);
1057 internals->ka_fd = -1;
1060 * Since TUN device has no more opened file descriptors
1061 * it will be removed from kernel
1066 tap_rx_queue_release(void *queue)
1068 struct rx_queue *rxq = queue;
1069 struct pmd_process_private *process_private;
1073 process_private = rte_eth_devices[rxq->in_port].process_private;
1074 if (process_private->rxq_fds[rxq->queue_id] > 0) {
1075 close(process_private->rxq_fds[rxq->queue_id]);
1076 process_private->rxq_fds[rxq->queue_id] = -1;
1077 rte_pktmbuf_free(rxq->pool);
1078 rte_free(rxq->iovecs);
1085 tap_tx_queue_release(void *queue)
1087 struct tx_queue *txq = queue;
1088 struct pmd_process_private *process_private;
1092 process_private = rte_eth_devices[txq->out_port].process_private;
1094 if (process_private->txq_fds[txq->queue_id] > 0) {
1095 close(process_private->txq_fds[txq->queue_id]);
1096 process_private->txq_fds[txq->queue_id] = -1;
1101 tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
1103 struct rte_eth_link *dev_link = &dev->data->dev_link;
1104 struct pmd_internals *pmd = dev->data->dev_private;
1105 struct ifreq ifr = { .ifr_flags = 0 };
1107 if (pmd->remote_if_index) {
1108 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY);
1109 if (!(ifr.ifr_flags & IFF_UP) ||
1110 !(ifr.ifr_flags & IFF_RUNNING)) {
1111 dev_link->link_status = ETH_LINK_DOWN;
1115 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY);
1116 dev_link->link_status =
1117 ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
1124 tap_promisc_enable(struct rte_eth_dev *dev)
1126 struct pmd_internals *pmd = dev->data->dev_private;
1127 struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1130 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1134 if (pmd->remote_if_index && !pmd->flow_isolate) {
1135 dev->data->promiscuous = 1;
1136 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
1138 /* Rollback promisc flag */
1139 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1141 * rte_eth_dev_promiscuous_enable() rollback
1142 * dev->data->promiscuous in the case of failure.
1152 tap_promisc_disable(struct rte_eth_dev *dev)
1154 struct pmd_internals *pmd = dev->data->dev_private;
1155 struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1158 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1162 if (pmd->remote_if_index && !pmd->flow_isolate) {
1163 dev->data->promiscuous = 0;
1164 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
1166 /* Rollback promisc flag */
1167 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1169 * rte_eth_dev_promiscuous_disable() rollback
1170 * dev->data->promiscuous in the case of failure.
1180 tap_allmulti_enable(struct rte_eth_dev *dev)
1182 struct pmd_internals *pmd = dev->data->dev_private;
1183 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1186 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1190 if (pmd->remote_if_index && !pmd->flow_isolate) {
1191 dev->data->all_multicast = 1;
1192 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
1194 /* Rollback allmulti flag */
1195 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1197 * rte_eth_dev_allmulticast_enable() rollback
1198 * dev->data->all_multicast in the case of failure.
1208 tap_allmulti_disable(struct rte_eth_dev *dev)
1210 struct pmd_internals *pmd = dev->data->dev_private;
1211 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1214 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1218 if (pmd->remote_if_index && !pmd->flow_isolate) {
1219 dev->data->all_multicast = 0;
1220 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
1222 /* Rollback allmulti flag */
1223 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1225 * rte_eth_dev_allmulticast_disable() rollback
1226 * dev->data->all_multicast in the case of failure.
1236 tap_mac_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1238 struct pmd_internals *pmd = dev->data->dev_private;
1239 enum ioctl_mode mode = LOCAL_ONLY;
1243 if (pmd->type == ETH_TUNTAP_TYPE_TUN) {
1244 TAP_LOG(ERR, "%s: can't MAC address for TUN",
1249 if (rte_is_zero_ether_addr(mac_addr)) {
1250 TAP_LOG(ERR, "%s: can't set an empty MAC address",
1254 /* Check the actual current MAC address on the tap netdevice */
1255 ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY);
1258 if (rte_is_same_ether_addr(
1259 (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data,
1262 /* Check the current MAC address on the remote */
1263 ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY);
1266 if (!rte_is_same_ether_addr(
1267 (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data,
1269 mode = LOCAL_AND_REMOTE;
1270 ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1271 rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, RTE_ETHER_ADDR_LEN);
1272 ret = tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, mode);
1275 rte_memcpy(&pmd->eth_addr, mac_addr, RTE_ETHER_ADDR_LEN);
1276 if (pmd->remote_if_index && !pmd->flow_isolate) {
1277 /* Replace MAC redirection rule after a MAC change */
1278 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC);
1281 "%s: Couldn't delete MAC redirection rule",
1285 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC);
1288 "%s: Couldn't add MAC redirection rule",
1298 tap_gso_ctx_setup(struct rte_gso_ctx *gso_ctx, struct rte_eth_dev *dev)
1304 * Create private mbuf pool with TAP_GSO_MBUF_SEG_SIZE bytes
1305 * size per mbuf use this pool for both direct and indirect mbufs
1308 struct rte_mempool *mp; /* Mempool for GSO packets */
1310 /* initialize GSO context */
1311 gso_types = DEV_TX_OFFLOAD_TCP_TSO;
1312 snprintf(pool_name, sizeof(pool_name), "mp_%s", dev->device->name);
1313 mp = rte_mempool_lookup((const char *)pool_name);
1315 mp = rte_pktmbuf_pool_create(pool_name, TAP_GSO_MBUFS_NUM,
1316 TAP_GSO_MBUF_CACHE_SIZE, 0,
1317 RTE_PKTMBUF_HEADROOM + TAP_GSO_MBUF_SEG_SIZE,
1320 struct pmd_internals *pmd = dev->data->dev_private;
1323 "%s: failed to create mbuf pool for device %s\n",
1324 pmd->name, dev->device->name);
1329 gso_ctx->direct_pool = mp;
1330 gso_ctx->indirect_pool = mp;
1331 gso_ctx->gso_types = gso_types;
1332 gso_ctx->gso_size = 0; /* gso_size is set in tx_burst() per packet */
1339 tap_setup_queue(struct rte_eth_dev *dev,
1340 struct pmd_internals *internals,
1348 struct pmd_internals *pmd = dev->data->dev_private;
1349 struct pmd_process_private *process_private = dev->process_private;
1350 struct rx_queue *rx = &internals->rxq[qid];
1351 struct tx_queue *tx = &internals->txq[qid];
1352 struct rte_gso_ctx *gso_ctx;
1355 fd = &process_private->rxq_fds[qid];
1356 other_fd = &process_private->txq_fds[qid];
1360 fd = &process_private->txq_fds[qid];
1361 other_fd = &process_private->rxq_fds[qid];
1363 gso_ctx = &tx->gso_ctx;
1366 /* fd for this queue already exists */
1367 TAP_LOG(DEBUG, "%s: fd %d for %s queue qid %d exists",
1368 pmd->name, *fd, dir, qid);
1370 } else if (*other_fd != -1) {
1371 /* Only other_fd exists. dup it */
1372 *fd = dup(*other_fd);
1375 TAP_LOG(ERR, "%s: dup() failed.", pmd->name);
1378 TAP_LOG(DEBUG, "%s: dup fd %d for %s queue qid %d (%d)",
1379 pmd->name, *other_fd, dir, qid, *fd);
1381 /* Both RX and TX fds do not exist (equal -1). Create fd */
1382 *fd = tun_alloc(pmd, 0);
1384 *fd = -1; /* restore original value */
1385 TAP_LOG(ERR, "%s: tun_alloc() failed.", pmd->name);
1388 TAP_LOG(DEBUG, "%s: add %s queue for qid %d fd %d",
1389 pmd->name, dir, qid, *fd);
1392 tx->mtu = &dev->data->mtu;
1393 rx->rxmode = &dev->data->dev_conf.rxmode;
1395 ret = tap_gso_ctx_setup(gso_ctx, dev);
1400 tx->type = pmd->type;
1406 tap_rx_queue_setup(struct rte_eth_dev *dev,
1407 uint16_t rx_queue_id,
1408 uint16_t nb_rx_desc,
1409 unsigned int socket_id,
1410 const struct rte_eth_rxconf *rx_conf __rte_unused,
1411 struct rte_mempool *mp)
1413 struct pmd_internals *internals = dev->data->dev_private;
1414 struct pmd_process_private *process_private = dev->process_private;
1415 struct rx_queue *rxq = &internals->rxq[rx_queue_id];
1416 struct rte_mbuf **tmp = &rxq->pool;
1417 long iov_max = sysconf(_SC_IOV_MAX);
1421 "_SC_IOV_MAX is not defined. Using %d as default",
1422 TAP_IOV_DEFAULT_MAX);
1423 iov_max = TAP_IOV_DEFAULT_MAX;
1425 uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1);
1426 struct iovec (*iovecs)[nb_desc + 1];
1427 int data_off = RTE_PKTMBUF_HEADROOM;
1432 if (rx_queue_id >= dev->data->nb_rx_queues || !mp) {
1434 "nb_rx_queues %d too small or mempool NULL",
1435 dev->data->nb_rx_queues);
1440 rxq->trigger_seen = 1; /* force initial burst */
1441 rxq->in_port = dev->data->port_id;
1442 rxq->queue_id = rx_queue_id;
1443 rxq->nb_rx_desc = nb_desc;
1444 iovecs = rte_zmalloc_socket(dev->device->name, sizeof(*iovecs), 0,
1448 "%s: Couldn't allocate %d RX descriptors",
1449 dev->device->name, nb_desc);
1452 rxq->iovecs = iovecs;
1454 dev->data->rx_queues[rx_queue_id] = rxq;
1455 fd = tap_setup_queue(dev, internals, rx_queue_id, 1);
1461 (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
1462 (*rxq->iovecs)[0].iov_base = &rxq->pi;
1464 for (i = 1; i <= nb_desc; i++) {
1465 *tmp = rte_pktmbuf_alloc(rxq->mp);
1468 "%s: couldn't allocate memory for queue %d",
1469 dev->device->name, rx_queue_id);
1473 (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
1474 (*rxq->iovecs)[i].iov_base =
1475 (char *)(*tmp)->buf_addr + data_off;
1477 tmp = &(*tmp)->next;
1480 TAP_LOG(DEBUG, " RX TUNTAP device name %s, qid %d on fd %d",
1481 internals->name, rx_queue_id,
1482 process_private->rxq_fds[rx_queue_id]);
1487 rte_pktmbuf_free(rxq->pool);
1489 rte_free(rxq->iovecs);
1495 tap_tx_queue_setup(struct rte_eth_dev *dev,
1496 uint16_t tx_queue_id,
1497 uint16_t nb_tx_desc __rte_unused,
1498 unsigned int socket_id __rte_unused,
1499 const struct rte_eth_txconf *tx_conf)
1501 struct pmd_internals *internals = dev->data->dev_private;
1502 struct pmd_process_private *process_private = dev->process_private;
1503 struct tx_queue *txq;
1507 if (tx_queue_id >= dev->data->nb_tx_queues)
1509 dev->data->tx_queues[tx_queue_id] = &internals->txq[tx_queue_id];
1510 txq = dev->data->tx_queues[tx_queue_id];
1511 txq->out_port = dev->data->port_id;
1512 txq->queue_id = tx_queue_id;
1514 offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1515 txq->csum = !!(offloads &
1516 (DEV_TX_OFFLOAD_IPV4_CKSUM |
1517 DEV_TX_OFFLOAD_UDP_CKSUM |
1518 DEV_TX_OFFLOAD_TCP_CKSUM));
1520 ret = tap_setup_queue(dev, internals, tx_queue_id, 0);
1524 " TX TUNTAP device name %s, qid %d on fd %d csum %s",
1525 internals->name, tx_queue_id,
1526 process_private->txq_fds[tx_queue_id],
1527 txq->csum ? "on" : "off");
1533 tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1535 struct pmd_internals *pmd = dev->data->dev_private;
1536 struct ifreq ifr = { .ifr_mtu = mtu };
1539 err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE);
1541 dev->data->mtu = mtu;
1547 tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
1548 struct rte_ether_addr *mc_addr_set __rte_unused,
1549 uint32_t nb_mc_addr __rte_unused)
1552 * Nothing to do actually: the tap has no filtering whatsoever, every
1553 * packet is received.
1559 tap_nl_msg_handler(struct nlmsghdr *nh, void *arg)
1561 struct rte_eth_dev *dev = arg;
1562 struct pmd_internals *pmd = dev->data->dev_private;
1563 struct ifinfomsg *info = NLMSG_DATA(nh);
1565 if (nh->nlmsg_type != RTM_NEWLINK ||
1566 (info->ifi_index != pmd->if_index &&
1567 info->ifi_index != pmd->remote_if_index))
1569 return tap_link_update(dev, 0);
1573 tap_dev_intr_handler(void *cb_arg)
1575 struct rte_eth_dev *dev = cb_arg;
1576 struct pmd_internals *pmd = dev->data->dev_private;
1578 tap_nl_recv(pmd->intr_handle.fd, tap_nl_msg_handler, dev);
1582 tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
1584 struct pmd_internals *pmd = dev->data->dev_private;
1586 /* In any case, disable interrupt if the conf is no longer there. */
1587 if (!dev->data->dev_conf.intr_conf.lsc) {
1588 if (pmd->intr_handle.fd != -1) {
1589 tap_nl_final(pmd->intr_handle.fd);
1590 rte_intr_callback_unregister(&pmd->intr_handle,
1591 tap_dev_intr_handler, dev);
1596 pmd->intr_handle.fd = tap_nl_init(RTMGRP_LINK);
1597 if (unlikely(pmd->intr_handle.fd == -1))
1599 return rte_intr_callback_register(
1600 &pmd->intr_handle, tap_dev_intr_handler, dev);
1602 tap_nl_final(pmd->intr_handle.fd);
1603 return rte_intr_callback_unregister(&pmd->intr_handle,
1604 tap_dev_intr_handler, dev);
1608 tap_intr_handle_set(struct rte_eth_dev *dev, int set)
1612 err = tap_lsc_intr_handle_set(dev, set);
1615 tap_rx_intr_vec_set(dev, 0);
1618 err = tap_rx_intr_vec_set(dev, set);
1620 tap_lsc_intr_handle_set(dev, 0);
1624 static const uint32_t*
1625 tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
1627 static const uint32_t ptypes[] = {
1628 RTE_PTYPE_INNER_L2_ETHER,
1629 RTE_PTYPE_INNER_L2_ETHER_VLAN,
1630 RTE_PTYPE_INNER_L2_ETHER_QINQ,
1631 RTE_PTYPE_INNER_L3_IPV4,
1632 RTE_PTYPE_INNER_L3_IPV4_EXT,
1633 RTE_PTYPE_INNER_L3_IPV6,
1634 RTE_PTYPE_INNER_L3_IPV6_EXT,
1635 RTE_PTYPE_INNER_L4_FRAG,
1636 RTE_PTYPE_INNER_L4_UDP,
1637 RTE_PTYPE_INNER_L4_TCP,
1638 RTE_PTYPE_INNER_L4_SCTP,
1640 RTE_PTYPE_L2_ETHER_VLAN,
1641 RTE_PTYPE_L2_ETHER_QINQ,
1643 RTE_PTYPE_L3_IPV4_EXT,
1644 RTE_PTYPE_L3_IPV6_EXT,
1656 tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1657 struct rte_eth_fc_conf *fc_conf)
1659 fc_conf->mode = RTE_FC_NONE;
1664 tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1665 struct rte_eth_fc_conf *fc_conf)
1667 if (fc_conf->mode != RTE_FC_NONE)
1673 * DPDK callback to update the RSS hash configuration.
1676 * Pointer to Ethernet device structure.
1677 * @param[in] rss_conf
1678 * RSS configuration data.
1681 * 0 on success, a negative errno value otherwise and rte_errno is set.
1684 tap_rss_hash_update(struct rte_eth_dev *dev,
1685 struct rte_eth_rss_conf *rss_conf)
1687 if (rss_conf->rss_hf & TAP_RSS_HF_MASK) {
1691 if (rss_conf->rss_key && rss_conf->rss_key_len) {
1693 * Currently TAP RSS key is hard coded
1694 * and cannot be updated
1697 "port %u RSS key cannot be updated",
1698 dev->data->port_id);
1706 tap_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1708 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1714 tap_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1716 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1722 tap_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1724 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1730 tap_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1732 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1736 static const struct eth_dev_ops ops = {
1737 .dev_start = tap_dev_start,
1738 .dev_stop = tap_dev_stop,
1739 .dev_close = tap_dev_close,
1740 .dev_configure = tap_dev_configure,
1741 .dev_infos_get = tap_dev_info,
1742 .rx_queue_setup = tap_rx_queue_setup,
1743 .tx_queue_setup = tap_tx_queue_setup,
1744 .rx_queue_start = tap_rx_queue_start,
1745 .tx_queue_start = tap_tx_queue_start,
1746 .rx_queue_stop = tap_rx_queue_stop,
1747 .tx_queue_stop = tap_tx_queue_stop,
1748 .rx_queue_release = tap_rx_queue_release,
1749 .tx_queue_release = tap_tx_queue_release,
1750 .flow_ctrl_get = tap_flow_ctrl_get,
1751 .flow_ctrl_set = tap_flow_ctrl_set,
1752 .link_update = tap_link_update,
1753 .dev_set_link_up = tap_link_set_up,
1754 .dev_set_link_down = tap_link_set_down,
1755 .promiscuous_enable = tap_promisc_enable,
1756 .promiscuous_disable = tap_promisc_disable,
1757 .allmulticast_enable = tap_allmulti_enable,
1758 .allmulticast_disable = tap_allmulti_disable,
1759 .mac_addr_set = tap_mac_set,
1760 .mtu_set = tap_mtu_set,
1761 .set_mc_addr_list = tap_set_mc_addr_list,
1762 .stats_get = tap_stats_get,
1763 .stats_reset = tap_stats_reset,
1764 .dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1765 .rss_hash_update = tap_rss_hash_update,
1766 .filter_ctrl = tap_dev_filter_ctrl,
1769 static const char *tuntap_types[ETH_TUNTAP_TYPE_MAX] = {
1770 "UNKNOWN", "TUN", "TAP"
1774 eth_dev_tap_create(struct rte_vdev_device *vdev, const char *tap_name,
1775 char *remote_iface, struct rte_ether_addr *mac_addr,
1776 enum rte_tuntap_type type)
1778 int numa_node = rte_socket_id();
1779 struct rte_eth_dev *dev;
1780 struct pmd_internals *pmd;
1781 struct pmd_process_private *process_private;
1782 const char *tuntap_name = tuntap_types[type];
1783 struct rte_eth_dev_data *data;
1787 TAP_LOG(DEBUG, "%s device on numa %u", tuntap_name, rte_socket_id());
1789 dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1791 TAP_LOG(ERR, "%s Unable to allocate device struct",
1793 goto error_exit_nodev;
1796 process_private = (struct pmd_process_private *)
1797 rte_zmalloc_socket(tap_name, sizeof(struct pmd_process_private),
1798 RTE_CACHE_LINE_SIZE, dev->device->numa_node);
1800 if (process_private == NULL) {
1801 TAP_LOG(ERR, "Failed to alloc memory for process private");
1804 pmd = dev->data->dev_private;
1805 dev->process_private = process_private;
1807 strlcpy(pmd->name, tap_name, sizeof(pmd->name));
1810 pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1811 if (pmd->ioctl_sock == -1) {
1813 "%s Unable to get a socket for management: %s",
1814 tuntap_name, strerror(errno));
1818 /* Setup some default values */
1820 data->dev_private = pmd;
1821 data->dev_flags = RTE_ETH_DEV_INTR_LSC;
1822 data->numa_node = numa_node;
1824 data->dev_link = pmd_link;
1825 data->mac_addrs = &pmd->eth_addr;
1826 /* Set the number of RX and TX queues */
1827 data->nb_rx_queues = 0;
1828 data->nb_tx_queues = 0;
1830 dev->dev_ops = &ops;
1831 dev->rx_pkt_burst = pmd_rx_burst;
1832 dev->tx_pkt_burst = pmd_tx_burst;
1834 pmd->intr_handle.type = RTE_INTR_HANDLE_EXT;
1835 pmd->intr_handle.fd = -1;
1836 dev->intr_handle = &pmd->intr_handle;
1838 /* Presetup the fds to -1 as being not valid */
1840 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1841 process_private->rxq_fds[i] = -1;
1842 process_private->txq_fds[i] = -1;
1845 if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
1846 if (rte_is_zero_ether_addr(mac_addr))
1847 rte_eth_random_addr((uint8_t *)&pmd->eth_addr);
1849 rte_memcpy(&pmd->eth_addr, mac_addr, sizeof(*mac_addr));
1853 * Allocate a TUN device keep-alive file descriptor that will only be
1854 * closed when the TUN device itself is closed or removed.
1855 * This keep-alive file descriptor will guarantee that the TUN device
1856 * exists even when all of its queues are closed
1858 pmd->ka_fd = tun_alloc(pmd, 1);
1859 if (pmd->ka_fd == -1) {
1860 TAP_LOG(ERR, "Unable to create %s interface", tuntap_name);
1863 TAP_LOG(DEBUG, "allocated %s", pmd->name);
1865 ifr.ifr_mtu = dev->data->mtu;
1866 if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE) < 0)
1869 if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
1870 memset(&ifr, 0, sizeof(struct ifreq));
1871 ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1872 rte_memcpy(ifr.ifr_hwaddr.sa_data, &pmd->eth_addr,
1873 RTE_ETHER_ADDR_LEN);
1874 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
1879 * Set up everything related to rte_flow:
1881 * - tap / remote if_index
1882 * - mandatory QDISCs
1883 * - rte_flow actual/implicit lists
1886 pmd->nlsk_fd = tap_nl_init(0);
1887 if (pmd->nlsk_fd == -1) {
1888 TAP_LOG(WARNING, "%s: failed to create netlink socket.",
1890 goto disable_rte_flow;
1892 pmd->if_index = if_nametoindex(pmd->name);
1893 if (!pmd->if_index) {
1894 TAP_LOG(ERR, "%s: failed to get if_index.", pmd->name);
1895 goto disable_rte_flow;
1897 if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
1898 TAP_LOG(ERR, "%s: failed to create multiq qdisc.",
1900 goto disable_rte_flow;
1902 if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
1903 TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
1905 goto disable_rte_flow;
1907 LIST_INIT(&pmd->flows);
1909 if (strlen(remote_iface)) {
1910 pmd->remote_if_index = if_nametoindex(remote_iface);
1911 if (!pmd->remote_if_index) {
1912 TAP_LOG(ERR, "%s: failed to get %s if_index.",
1913 pmd->name, remote_iface);
1916 strlcpy(pmd->remote_iface, remote_iface, RTE_ETH_NAME_MAX_LEN);
1918 /* Save state of remote device */
1919 tap_ioctl(pmd, SIOCGIFFLAGS, &pmd->remote_initial_flags, 0, REMOTE_ONLY);
1921 /* Replicate remote MAC address */
1922 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) {
1923 TAP_LOG(ERR, "%s: failed to get %s MAC address.",
1924 pmd->name, pmd->remote_iface);
1927 rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
1928 RTE_ETHER_ADDR_LEN);
1929 /* The desired MAC is already in ifreq after SIOCGIFHWADDR. */
1930 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) {
1931 TAP_LOG(ERR, "%s: failed to get %s MAC address.",
1932 pmd->name, remote_iface);
1937 * Flush usually returns negative value because it tries to
1938 * delete every QDISC (and on a running device, one QDISC at
1939 * least is needed). Ignore negative return value.
1941 qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
1942 if (qdisc_create_ingress(pmd->nlsk_fd,
1943 pmd->remote_if_index) < 0) {
1944 TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
1948 LIST_INIT(&pmd->implicit_flows);
1949 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0 ||
1950 tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0 ||
1951 tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0 ||
1952 tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0) {
1954 "%s: failed to create implicit rules.",
1960 rte_eth_dev_probing_finish(dev);
1964 TAP_LOG(ERR, " Disabling rte flow support: %s(%d)",
1965 strerror(errno), errno);
1966 if (strlen(remote_iface)) {
1967 TAP_LOG(ERR, "Remote feature requires flow support.");
1970 rte_eth_dev_probing_finish(dev);
1974 TAP_LOG(ERR, " Can't set up remote feature: %s(%d)",
1975 strerror(errno), errno);
1976 tap_flow_implicit_flush(pmd, NULL);
1979 if (pmd->ioctl_sock > 0)
1980 close(pmd->ioctl_sock);
1981 /* mac_addrs must not be freed alone because part of dev_private */
1982 dev->data->mac_addrs = NULL;
1983 rte_eth_dev_release_port(dev);
1986 TAP_LOG(ERR, "%s Unable to initialize %s",
1987 tuntap_name, rte_vdev_device_name(vdev));
1992 /* make sure name is a possible Linux network device name */
1994 is_valid_iface(const char *name)
1999 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
2003 if (*name == '/' || *name == ':' || isspace(*name))
2011 set_interface_name(const char *key __rte_unused,
2015 char *name = (char *)extra_args;
2018 if (!is_valid_iface(value)) {
2019 TAP_LOG(ERR, "TAP invalid remote interface name (%s)",
2023 strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
2025 /* use tap%d which causes kernel to choose next available */
2026 strlcpy(name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2032 set_remote_iface(const char *key __rte_unused,
2036 char *name = (char *)extra_args;
2039 if (!is_valid_iface(value)) {
2040 TAP_LOG(ERR, "TAP invalid remote interface name (%s)",
2044 strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
2050 static int parse_user_mac(struct rte_ether_addr *user_mac,
2053 unsigned int index = 0;
2054 char mac_temp[strlen(ETH_TAP_USR_MAC_FMT) + 1], *mac_byte = NULL;
2056 if (user_mac == NULL || value == NULL)
2059 strlcpy(mac_temp, value, sizeof(mac_temp));
2060 mac_byte = strtok(mac_temp, ":");
2062 while ((mac_byte != NULL) &&
2063 (strlen(mac_byte) <= 2) &&
2064 (strlen(mac_byte) == strspn(mac_byte,
2065 ETH_TAP_CMP_MAC_FMT))) {
2066 user_mac->addr_bytes[index++] = strtoul(mac_byte, NULL, 16);
2067 mac_byte = strtok(NULL, ":");
2074 set_mac_type(const char *key __rte_unused,
2078 struct rte_ether_addr *user_mac = extra_args;
2083 if (!strncasecmp(ETH_TAP_MAC_FIXED, value, strlen(ETH_TAP_MAC_FIXED))) {
2084 static int iface_idx;
2086 /* fixed mac = 00:64:74:61:70:<iface_idx> */
2087 memcpy((char *)user_mac->addr_bytes, "\0dtap",
2088 RTE_ETHER_ADDR_LEN);
2089 user_mac->addr_bytes[RTE_ETHER_ADDR_LEN - 1] =
2094 if (parse_user_mac(user_mac, value) != 6)
2097 TAP_LOG(DEBUG, "TAP user MAC param (%s)", value);
2101 TAP_LOG(ERR, "TAP user MAC (%s) is not in format (%s|%s)",
2102 value, ETH_TAP_MAC_FIXED, ETH_TAP_USR_MAC_FMT);
2107 * Open a TUN interface device. TUN PMD
2108 * 1) sets tap_type as false
2109 * 2) intakes iface as argument.
2110 * 3) as interface is virtual set speed to 10G
2113 rte_pmd_tun_probe(struct rte_vdev_device *dev)
2115 const char *name, *params;
2117 struct rte_kvargs *kvlist = NULL;
2118 char tun_name[RTE_ETH_NAME_MAX_LEN];
2119 char remote_iface[RTE_ETH_NAME_MAX_LEN];
2120 struct rte_eth_dev *eth_dev;
2122 name = rte_vdev_device_name(dev);
2123 params = rte_vdev_device_args(dev);
2124 memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2126 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
2127 strlen(params) == 0) {
2128 eth_dev = rte_eth_dev_attach_secondary(name);
2130 TAP_LOG(ERR, "Failed to probe %s", name);
2133 eth_dev->dev_ops = &ops;
2134 eth_dev->device = &dev->device;
2135 rte_eth_dev_probing_finish(eth_dev);
2139 /* use tun%d which causes kernel to choose next available */
2140 strlcpy(tun_name, DEFAULT_TUN_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2142 if (params && (params[0] != '\0')) {
2143 TAP_LOG(DEBUG, "parameters (%s)", params);
2145 kvlist = rte_kvargs_parse(params, valid_arguments);
2147 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2148 ret = rte_kvargs_process(kvlist,
2150 &set_interface_name,
2158 pmd_link.link_speed = ETH_SPEED_NUM_10G;
2160 TAP_LOG(DEBUG, "Initializing pmd_tun for %s", name);
2162 ret = eth_dev_tap_create(dev, tun_name, remote_iface, 0,
2163 ETH_TUNTAP_TYPE_TUN);
2167 TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2170 rte_kvargs_free(kvlist);
2175 /* Request queue file descriptors from secondary to primary. */
2177 tap_mp_attach_queues(const char *port_name, struct rte_eth_dev *dev)
2180 struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0};
2181 struct rte_mp_msg request, *reply;
2182 struct rte_mp_reply replies;
2183 struct ipc_queues *request_param = (struct ipc_queues *)request.param;
2184 struct ipc_queues *reply_param;
2185 struct pmd_process_private *process_private = dev->process_private;
2186 int queue, fd_iterator;
2188 /* Prepare the request */
2189 memset(&request, 0, sizeof(request));
2190 strlcpy(request.name, TAP_MP_KEY, sizeof(request.name));
2191 strlcpy(request_param->port_name, port_name,
2192 sizeof(request_param->port_name));
2193 request.len_param = sizeof(*request_param);
2194 /* Send request and receive reply */
2195 ret = rte_mp_request_sync(&request, &replies, &timeout);
2196 if (ret < 0 || replies.nb_received != 1) {
2197 TAP_LOG(ERR, "Failed to request queues from primary: %d",
2201 reply = &replies.msgs[0];
2202 reply_param = (struct ipc_queues *)reply->param;
2203 TAP_LOG(DEBUG, "Received IPC reply for %s", reply_param->port_name);
2205 /* Attach the queues from received file descriptors */
2206 if (reply_param->rxq_count + reply_param->txq_count != reply->num_fds) {
2207 TAP_LOG(ERR, "Unexpected number of fds received");
2211 dev->data->nb_rx_queues = reply_param->rxq_count;
2212 dev->data->nb_tx_queues = reply_param->txq_count;
2214 for (queue = 0; queue < reply_param->rxq_count; queue++)
2215 process_private->rxq_fds[queue] = reply->fds[fd_iterator++];
2216 for (queue = 0; queue < reply_param->txq_count; queue++)
2217 process_private->txq_fds[queue] = reply->fds[fd_iterator++];
2222 /* Send the queue file descriptors from the primary process to secondary. */
2224 tap_mp_sync_queues(const struct rte_mp_msg *request, const void *peer)
2226 struct rte_eth_dev *dev;
2227 struct pmd_process_private *process_private;
2228 struct rte_mp_msg reply;
2229 const struct ipc_queues *request_param =
2230 (const struct ipc_queues *)request->param;
2231 struct ipc_queues *reply_param =
2232 (struct ipc_queues *)reply.param;
2237 /* Get requested port */
2238 TAP_LOG(DEBUG, "Received IPC request for %s", request_param->port_name);
2239 ret = rte_eth_dev_get_port_by_name(request_param->port_name, &port_id);
2241 TAP_LOG(ERR, "Failed to get port id for %s",
2242 request_param->port_name);
2245 dev = &rte_eth_devices[port_id];
2246 process_private = dev->process_private;
2248 /* Fill file descriptors for all queues */
2250 reply_param->rxq_count = 0;
2251 if (dev->data->nb_rx_queues + dev->data->nb_tx_queues >
2253 TAP_LOG(ERR, "Number of rx/tx queues exceeds max number of fds");
2257 for (queue = 0; queue < dev->data->nb_rx_queues; queue++) {
2258 reply.fds[reply.num_fds++] = process_private->rxq_fds[queue];
2259 reply_param->rxq_count++;
2261 RTE_ASSERT(reply_param->rxq_count == dev->data->nb_rx_queues);
2263 reply_param->txq_count = 0;
2264 for (queue = 0; queue < dev->data->nb_tx_queues; queue++) {
2265 reply.fds[reply.num_fds++] = process_private->txq_fds[queue];
2266 reply_param->txq_count++;
2268 RTE_ASSERT(reply_param->txq_count == dev->data->nb_tx_queues);
2271 strlcpy(reply.name, request->name, sizeof(reply.name));
2272 strlcpy(reply_param->port_name, request_param->port_name,
2273 sizeof(reply_param->port_name));
2274 reply.len_param = sizeof(*reply_param);
2275 if (rte_mp_reply(&reply, peer) < 0) {
2276 TAP_LOG(ERR, "Failed to reply an IPC request to sync queues");
2282 /* Open a TAP interface device.
2285 rte_pmd_tap_probe(struct rte_vdev_device *dev)
2287 const char *name, *params;
2289 struct rte_kvargs *kvlist = NULL;
2291 char tap_name[RTE_ETH_NAME_MAX_LEN];
2292 char remote_iface[RTE_ETH_NAME_MAX_LEN];
2293 struct rte_ether_addr user_mac = { .addr_bytes = {0} };
2294 struct rte_eth_dev *eth_dev;
2295 int tap_devices_count_increased = 0;
2297 name = rte_vdev_device_name(dev);
2298 params = rte_vdev_device_args(dev);
2300 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
2301 eth_dev = rte_eth_dev_attach_secondary(name);
2303 TAP_LOG(ERR, "Failed to probe %s", name);
2306 eth_dev->dev_ops = &ops;
2307 eth_dev->device = &dev->device;
2308 eth_dev->rx_pkt_burst = pmd_rx_burst;
2309 eth_dev->tx_pkt_burst = pmd_tx_burst;
2310 if (!rte_eal_primary_proc_alive(NULL)) {
2311 TAP_LOG(ERR, "Primary process is missing");
2314 eth_dev->process_private = (struct pmd_process_private *)
2315 rte_zmalloc_socket(name,
2316 sizeof(struct pmd_process_private),
2317 RTE_CACHE_LINE_SIZE,
2318 eth_dev->device->numa_node);
2319 if (eth_dev->process_private == NULL) {
2321 "Failed to alloc memory for process private");
2325 ret = tap_mp_attach_queues(name, eth_dev);
2328 rte_eth_dev_probing_finish(eth_dev);
2332 speed = ETH_SPEED_NUM_10G;
2334 /* use tap%d which causes kernel to choose next available */
2335 strlcpy(tap_name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2336 memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2338 if (params && (params[0] != '\0')) {
2339 TAP_LOG(DEBUG, "parameters (%s)", params);
2341 kvlist = rte_kvargs_parse(params, valid_arguments);
2343 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2344 ret = rte_kvargs_process(kvlist,
2346 &set_interface_name,
2352 if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
2353 ret = rte_kvargs_process(kvlist,
2361 if (rte_kvargs_count(kvlist, ETH_TAP_MAC_ARG) == 1) {
2362 ret = rte_kvargs_process(kvlist,
2371 pmd_link.link_speed = speed;
2373 TAP_LOG(DEBUG, "Initializing pmd_tap for %s", name);
2375 /* Register IPC feed callback */
2376 if (!tap_devices_count) {
2377 ret = rte_mp_action_register(TAP_MP_KEY, tap_mp_sync_queues);
2378 if (ret < 0 && rte_errno != ENOTSUP) {
2379 TAP_LOG(ERR, "tap: Failed to register IPC callback: %s",
2380 strerror(rte_errno));
2384 tap_devices_count++;
2385 tap_devices_count_increased = 1;
2386 ret = eth_dev_tap_create(dev, tap_name, remote_iface, &user_mac,
2387 ETH_TUNTAP_TYPE_TAP);
2391 TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2393 if (tap_devices_count_increased == 1) {
2394 if (tap_devices_count == 1)
2395 rte_mp_action_unregister(TAP_MP_KEY);
2396 tap_devices_count--;
2399 rte_kvargs_free(kvlist);
2404 /* detach a TUNTAP device.
2407 rte_pmd_tap_remove(struct rte_vdev_device *dev)
2409 struct rte_eth_dev *eth_dev = NULL;
2410 struct pmd_internals *internals;
2412 /* find the ethdev entry */
2413 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
2417 /* mac_addrs must not be freed alone because part of dev_private */
2418 eth_dev->data->mac_addrs = NULL;
2420 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2421 return rte_eth_dev_release_port(eth_dev);
2423 tap_dev_close(eth_dev);
2425 internals = eth_dev->data->dev_private;
2426 TAP_LOG(DEBUG, "Closing %s Ethernet device on numa %u",
2427 tuntap_types[internals->type], rte_socket_id());
2429 close(internals->ioctl_sock);
2430 rte_free(eth_dev->process_private);
2431 if (tap_devices_count == 1)
2432 rte_mp_action_unregister(TAP_MP_KEY);
2433 tap_devices_count--;
2434 rte_eth_dev_release_port(eth_dev);
2436 if (internals->ka_fd != -1) {
2437 close(internals->ka_fd);
2438 internals->ka_fd = -1;
2443 static struct rte_vdev_driver pmd_tun_drv = {
2444 .probe = rte_pmd_tun_probe,
2445 .remove = rte_pmd_tap_remove,
2448 static struct rte_vdev_driver pmd_tap_drv = {
2449 .probe = rte_pmd_tap_probe,
2450 .remove = rte_pmd_tap_remove,
2453 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
2454 RTE_PMD_REGISTER_VDEV(net_tun, pmd_tun_drv);
2455 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
2456 RTE_PMD_REGISTER_PARAM_STRING(net_tun,
2457 ETH_TAP_IFACE_ARG "=<string> ");
2458 RTE_PMD_REGISTER_PARAM_STRING(net_tap,
2459 ETH_TAP_IFACE_ARG "=<string> "
2460 ETH_TAP_MAC_ARG "=" ETH_TAP_MAC_ARG_FMT " "
2461 ETH_TAP_REMOTE_ARG "=<string>");
2464 RTE_INIT(tap_init_log)
2466 tap_logtype = rte_log_register("pmd.net.tap");
2467 if (tap_logtype >= 0)
2468 rte_log_set_level(tap_logtype, RTE_LOG_NOTICE);