1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2017 Intel Corporation 3 */ 4 5 #include <rte_atomic.h> 6 #include <rte_branch_prediction.h> 7 #include <rte_byteorder.h> 8 #include <rte_common.h> 9 #include <rte_mbuf.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> 15 #include <rte_net.h> 16 #include <rte_debug.h> 17 #include <rte_ip.h> 18 #include <rte_string_fns.h> 19 #include <rte_ethdev.h> 20 #include <rte_errno.h> 21 #include <rte_cycles.h> 22 23 #include <sys/types.h> 24 #include <sys/stat.h> 25 #include <sys/socket.h> 26 #include <sys/ioctl.h> 27 #include <sys/utsname.h> 28 #include <sys/mman.h> 29 #include <errno.h> 30 #include <signal.h> 31 #include <stdbool.h> 32 #include <stdint.h> 33 #include <sys/uio.h> 34 #include <unistd.h> 35 #include <arpa/inet.h> 36 #include <net/if.h> 37 #include <linux/if_tun.h> 38 #include <linux/if_ether.h> 39 #include <fcntl.h> 40 #include <ctype.h> 41 42 #include <tap_rss.h> 43 #include <rte_eth_tap.h> 44 #include <tap_flow.h> 45 #include <tap_netlink.h> 46 #include <tap_tcmsgs.h> 47 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" 52 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" 57 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 61 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) 67 68 /* IPC key for queue fds sync */ 69 #define TAP_MP_KEY "tap_mp_sync_queues" 70 71 #define TAP_IOV_DEFAULT_MAX 1024 72 73 static int tap_devices_count; 74 75 static const char *tuntap_types[ETH_TUNTAP_TYPE_MAX] = { 76 "UNKNOWN", "TUN", "TAP" 77 }; 78 79 static const char *valid_arguments[] = { 80 ETH_TAP_IFACE_ARG, 81 ETH_TAP_REMOTE_ARG, 82 ETH_TAP_MAC_ARG, 83 NULL 84 }; 85 86 static volatile uint32_t tap_trigger; /* Rx trigger */ 87 88 static struct rte_eth_link pmd_link = { 89 .link_speed = ETH_SPEED_NUM_10G, 90 .link_duplex = ETH_LINK_FULL_DUPLEX, 91 .link_status = ETH_LINK_DOWN, 92 .link_autoneg = ETH_LINK_FIXED, 93 }; 94 95 static void 96 tap_trigger_cb(int sig __rte_unused) 97 { 98 /* Valid trigger values are nonzero */ 99 tap_trigger = (tap_trigger + 1) | 0x80000000; 100 } 101 102 /* Specifies on what netdevices the ioctl should be applied */ 103 enum ioctl_mode { 104 LOCAL_AND_REMOTE, 105 LOCAL_ONLY, 106 REMOTE_ONLY, 107 }; 108 109 /* Message header to synchronize queues via IPC */ 110 struct ipc_queues { 111 char port_name[RTE_DEV_NAME_MAX_LEN]; 112 int rxq_count; 113 int txq_count; 114 /* 115 * The file descriptors are in the dedicated part 116 * of the Unix message to be translated by the kernel. 117 */ 118 }; 119 120 static int tap_intr_handle_set(struct rte_eth_dev *dev, int set); 121 122 /** 123 * Tun/Tap allocation routine 124 * 125 * @param[in] pmd 126 * Pointer to private structure. 127 * 128 * @param[in] is_keepalive 129 * Keepalive flag 130 * 131 * @return 132 * -1 on failure, fd on success 133 */ 134 static int 135 tun_alloc(struct pmd_internals *pmd, int is_keepalive) 136 { 137 struct ifreq ifr; 138 #ifdef IFF_MULTI_QUEUE 139 unsigned int features; 140 #endif 141 int fd, signo, flags; 142 143 memset(&ifr, 0, sizeof(struct ifreq)); 144 145 /* 146 * Do not set IFF_NO_PI as packet information header will be needed 147 * to check if a received packet has been truncated. 148 */ 149 ifr.ifr_flags = (pmd->type == ETH_TUNTAP_TYPE_TAP) ? 150 IFF_TAP : IFF_TUN | IFF_POINTOPOINT; 151 strlcpy(ifr.ifr_name, pmd->name, IFNAMSIZ); 152 153 fd = open(TUN_TAP_DEV_PATH, O_RDWR); 154 if (fd < 0) { 155 TAP_LOG(ERR, "Unable to open %s interface", TUN_TAP_DEV_PATH); 156 goto error; 157 } 158 159 #ifdef IFF_MULTI_QUEUE 160 /* Grab the TUN features to verify we can work multi-queue */ 161 if (ioctl(fd, TUNGETFEATURES, &features) < 0) { 162 TAP_LOG(ERR, "unable to get TUN/TAP features"); 163 goto error; 164 } 165 TAP_LOG(DEBUG, "%s Features %08x", TUN_TAP_DEV_PATH, features); 166 167 if (features & IFF_MULTI_QUEUE) { 168 TAP_LOG(DEBUG, " Multi-queue support for %d queues", 169 RTE_PMD_TAP_MAX_QUEUES); 170 ifr.ifr_flags |= IFF_MULTI_QUEUE; 171 } else 172 #endif 173 { 174 ifr.ifr_flags |= IFF_ONE_QUEUE; 175 TAP_LOG(DEBUG, " Single queue only support"); 176 } 177 178 /* Set the TUN/TAP configuration and set the name if needed */ 179 if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) { 180 TAP_LOG(WARNING, "Unable to set TUNSETIFF for %s: %s", 181 ifr.ifr_name, strerror(errno)); 182 goto error; 183 } 184 185 /* 186 * Name passed to kernel might be wildcard like dtun%d 187 * and need to find the resulting device. 188 */ 189 TAP_LOG(DEBUG, "Device name is '%s'", ifr.ifr_name); 190 strlcpy(pmd->name, ifr.ifr_name, RTE_ETH_NAME_MAX_LEN); 191 192 if (is_keepalive) { 193 /* 194 * Detach the TUN/TAP keep-alive queue 195 * to avoid traffic through it 196 */ 197 ifr.ifr_flags = IFF_DETACH_QUEUE; 198 if (ioctl(fd, TUNSETQUEUE, (void *)&ifr) < 0) { 199 TAP_LOG(WARNING, 200 "Unable to detach keep-alive queue for %s: %s", 201 ifr.ifr_name, strerror(errno)); 202 goto error; 203 } 204 } 205 206 flags = fcntl(fd, F_GETFL); 207 if (flags == -1) { 208 TAP_LOG(WARNING, 209 "Unable to get %s current flags\n", 210 ifr.ifr_name); 211 goto error; 212 } 213 214 /* Always set the file descriptor to non-blocking */ 215 flags |= O_NONBLOCK; 216 if (fcntl(fd, F_SETFL, flags) < 0) { 217 TAP_LOG(WARNING, 218 "Unable to set %s to nonblocking: %s", 219 ifr.ifr_name, strerror(errno)); 220 goto error; 221 } 222 223 /* Find a free realtime signal */ 224 for (signo = SIGRTMIN + 1; signo < SIGRTMAX; signo++) { 225 struct sigaction sa; 226 227 if (sigaction(signo, NULL, &sa) == -1) { 228 TAP_LOG(WARNING, 229 "Unable to get current rt-signal %d handler", 230 signo); 231 goto error; 232 } 233 234 /* Already have the handler we want on this signal */ 235 if (sa.sa_handler == tap_trigger_cb) 236 break; 237 238 /* Is handler in use by application */ 239 if (sa.sa_handler != SIG_DFL) { 240 TAP_LOG(DEBUG, 241 "Skipping used rt-signal %d", signo); 242 continue; 243 } 244 245 sa = (struct sigaction) { 246 .sa_flags = SA_RESTART, 247 .sa_handler = tap_trigger_cb, 248 }; 249 250 if (sigaction(signo, &sa, NULL) == -1) { 251 TAP_LOG(WARNING, 252 "Unable to set rt-signal %d handler\n", signo); 253 goto error; 254 } 255 256 /* Found a good signal to use */ 257 TAP_LOG(DEBUG, 258 "Using rt-signal %d", signo); 259 break; 260 } 261 262 if (signo == SIGRTMAX) { 263 TAP_LOG(WARNING, "All rt-signals are in use\n"); 264 265 /* Disable trigger globally in case of error */ 266 tap_trigger = 0; 267 TAP_LOG(NOTICE, "No Rx trigger signal available\n"); 268 } else { 269 /* Enable signal on file descriptor */ 270 if (fcntl(fd, F_SETSIG, signo) < 0) { 271 TAP_LOG(WARNING, "Unable to set signo %d for fd %d: %s", 272 signo, fd, strerror(errno)); 273 goto error; 274 } 275 if (fcntl(fd, F_SETFL, flags | O_ASYNC) < 0) { 276 TAP_LOG(WARNING, "Unable to set fcntl flags: %s", 277 strerror(errno)); 278 goto error; 279 } 280 281 if (fcntl(fd, F_SETOWN, getpid()) < 0) { 282 TAP_LOG(WARNING, "Unable to set fcntl owner: %s", 283 strerror(errno)); 284 goto error; 285 } 286 } 287 return fd; 288 289 error: 290 if (fd >= 0) 291 close(fd); 292 return -1; 293 } 294 295 static void 296 tap_verify_csum(struct rte_mbuf *mbuf) 297 { 298 uint32_t l2 = mbuf->packet_type & RTE_PTYPE_L2_MASK; 299 uint32_t l3 = mbuf->packet_type & RTE_PTYPE_L3_MASK; 300 uint32_t l4 = mbuf->packet_type & RTE_PTYPE_L4_MASK; 301 unsigned int l2_len = sizeof(struct rte_ether_hdr); 302 unsigned int l3_len; 303 uint16_t cksum = 0; 304 void *l3_hdr; 305 void *l4_hdr; 306 struct rte_udp_hdr *udp_hdr; 307 308 if (l2 == RTE_PTYPE_L2_ETHER_VLAN) 309 l2_len += 4; 310 else if (l2 == RTE_PTYPE_L2_ETHER_QINQ) 311 l2_len += 8; 312 /* Don't verify checksum for packets with discontinuous L2 header */ 313 if (unlikely(l2_len + sizeof(struct rte_ipv4_hdr) > 314 rte_pktmbuf_data_len(mbuf))) 315 return; 316 l3_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len); 317 if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) { 318 struct rte_ipv4_hdr *iph = l3_hdr; 319 320 l3_len = rte_ipv4_hdr_len(iph); 321 if (unlikely(l2_len + l3_len > rte_pktmbuf_data_len(mbuf))) 322 return; 323 /* check that the total length reported by header is not 324 * greater than the total received size 325 */ 326 if (l2_len + rte_be_to_cpu_16(iph->total_length) > 327 rte_pktmbuf_data_len(mbuf)) 328 return; 329 330 cksum = ~rte_raw_cksum(iph, l3_len); 331 mbuf->ol_flags |= cksum ? 332 PKT_RX_IP_CKSUM_BAD : 333 PKT_RX_IP_CKSUM_GOOD; 334 } else if (l3 == RTE_PTYPE_L3_IPV6) { 335 struct rte_ipv6_hdr *iph = l3_hdr; 336 337 l3_len = sizeof(struct rte_ipv6_hdr); 338 /* check that the total length reported by header is not 339 * greater than the total received size 340 */ 341 if (l2_len + l3_len + rte_be_to_cpu_16(iph->payload_len) > 342 rte_pktmbuf_data_len(mbuf)) 343 return; 344 } else { 345 /* IPv6 extensions are not supported */ 346 return; 347 } 348 if (l4 == RTE_PTYPE_L4_UDP || l4 == RTE_PTYPE_L4_TCP) { 349 l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len); 350 /* Don't verify checksum for multi-segment packets. */ 351 if (mbuf->nb_segs > 1) 352 return; 353 if (l3 == RTE_PTYPE_L3_IPV4) { 354 if (l4 == RTE_PTYPE_L4_UDP) { 355 udp_hdr = (struct rte_udp_hdr *)l4_hdr; 356 if (udp_hdr->dgram_cksum == 0) { 357 /* 358 * For IPv4, a zero UDP checksum 359 * indicates that the sender did not 360 * generate one [RFC 768]. 361 */ 362 mbuf->ol_flags |= PKT_RX_L4_CKSUM_NONE; 363 return; 364 } 365 } 366 cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr); 367 } else if (l3 == RTE_PTYPE_L3_IPV6) { 368 cksum = ~rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr); 369 } 370 mbuf->ol_flags |= cksum ? 371 PKT_RX_L4_CKSUM_BAD : 372 PKT_RX_L4_CKSUM_GOOD; 373 } 374 } 375 376 static uint64_t 377 tap_rx_offload_get_port_capa(void) 378 { 379 /* 380 * No specific port Rx offload capabilities. 381 */ 382 return 0; 383 } 384 385 static uint64_t 386 tap_rx_offload_get_queue_capa(void) 387 { 388 return DEV_RX_OFFLOAD_SCATTER | 389 DEV_RX_OFFLOAD_IPV4_CKSUM | 390 DEV_RX_OFFLOAD_UDP_CKSUM | 391 DEV_RX_OFFLOAD_TCP_CKSUM; 392 } 393 394 static void 395 tap_rxq_pool_free(struct rte_mbuf *pool) 396 { 397 struct rte_mbuf *mbuf = pool; 398 uint16_t nb_segs = 1; 399 400 if (mbuf == NULL) 401 return; 402 403 while (mbuf->next) { 404 mbuf = mbuf->next; 405 nb_segs++; 406 } 407 pool->nb_segs = nb_segs; 408 rte_pktmbuf_free(pool); 409 } 410 411 /* Callback to handle the rx burst of packets to the correct interface and 412 * file descriptor(s) in a multi-queue setup. 413 */ 414 static uint16_t 415 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) 416 { 417 struct rx_queue *rxq = queue; 418 struct pmd_process_private *process_private; 419 uint16_t num_rx; 420 unsigned long num_rx_bytes = 0; 421 uint32_t trigger = tap_trigger; 422 423 if (trigger == rxq->trigger_seen) 424 return 0; 425 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); 432 int len; 433 434 len = readv(process_private->rxq_fds[rxq->queue_id], 435 *rxq->iovecs, 436 1 + (rxq->rxmode->offloads & DEV_RX_OFFLOAD_SCATTER ? 437 rxq->nb_rx_desc : 1)); 438 if (len < (int)sizeof(struct tun_pi)) 439 break; 440 441 /* Packet couldn't fit in the provided mbuf */ 442 if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) { 443 rxq->stats.ierrors++; 444 continue; 445 } 446 447 len -= sizeof(struct tun_pi); 448 449 mbuf->pkt_len = len; 450 mbuf->port = rxq->in_port; 451 while (1) { 452 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp); 453 454 if (unlikely(!buf)) { 455 rxq->stats.rx_nombuf++; 456 /* No new buf has been allocated: do nothing */ 457 if (!new_tail || !seg) 458 goto end; 459 460 seg->next = NULL; 461 tap_rxq_pool_free(mbuf); 462 463 goto end; 464 } 465 seg = seg ? seg->next : mbuf; 466 if (rxq->pool == mbuf) 467 rxq->pool = buf; 468 if (new_tail) 469 new_tail->next = buf; 470 new_tail = buf; 471 new_tail->next = seg->next; 472 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; 478 479 seg->data_len = RTE_MIN(seg->buf_len - data_off, len); 480 seg->data_off = data_off; 481 482 len -= seg->data_len; 483 if (len <= 0) 484 break; 485 mbuf->nb_segs++; 486 /* First segment has headroom, not the others */ 487 data_off = 0; 488 } 489 seg->next = NULL; 490 mbuf->packet_type = rte_net_get_ptype(mbuf, NULL, 491 RTE_PTYPE_ALL_MASK); 492 if (rxq->rxmode->offloads & DEV_RX_OFFLOAD_CHECKSUM) 493 tap_verify_csum(mbuf); 494 495 /* account for the receive frame */ 496 bufs[num_rx++] = mbuf; 497 num_rx_bytes += mbuf->pkt_len; 498 } 499 end: 500 rxq->stats.ipackets += num_rx; 501 rxq->stats.ibytes += num_rx_bytes; 502 503 if (trigger && num_rx < nb_pkts) 504 rxq->trigger_seen = trigger; 505 506 return num_rx; 507 } 508 509 static uint64_t 510 tap_tx_offload_get_port_capa(void) 511 { 512 /* 513 * No specific port Tx offload capabilities. 514 */ 515 return 0; 516 } 517 518 static uint64_t 519 tap_tx_offload_get_queue_capa(void) 520 { 521 return DEV_TX_OFFLOAD_MULTI_SEGS | 522 DEV_TX_OFFLOAD_IPV4_CKSUM | 523 DEV_TX_OFFLOAD_UDP_CKSUM | 524 DEV_TX_OFFLOAD_TCP_CKSUM | 525 DEV_TX_OFFLOAD_TCP_TSO; 526 } 527 528 /* Finalize l4 checksum calculation */ 529 static void 530 tap_tx_l4_cksum(uint16_t *l4_cksum, uint16_t l4_phdr_cksum, 531 uint32_t l4_raw_cksum) 532 { 533 if (l4_cksum) { 534 uint32_t cksum; 535 536 cksum = __rte_raw_cksum_reduce(l4_raw_cksum); 537 cksum += l4_phdr_cksum; 538 539 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff); 540 cksum = (~cksum) & 0xffff; 541 if (cksum == 0) 542 cksum = 0xffff; 543 *l4_cksum = cksum; 544 } 545 } 546 547 /* Accumaulate L4 raw checksums */ 548 static void 549 tap_tx_l4_add_rcksum(char *l4_data, unsigned int l4_len, uint16_t *l4_cksum, 550 uint32_t *l4_raw_cksum) 551 { 552 if (l4_cksum == NULL) 553 return; 554 555 *l4_raw_cksum = __rte_raw_cksum(l4_data, l4_len, *l4_raw_cksum); 556 } 557 558 /* L3 and L4 pseudo headers checksum offloads */ 559 static void 560 tap_tx_l3_cksum(char *packet, uint64_t ol_flags, unsigned int l2_len, 561 unsigned int l3_len, unsigned int l4_len, uint16_t **l4_cksum, 562 uint16_t *l4_phdr_cksum, uint32_t *l4_raw_cksum) 563 { 564 void *l3_hdr = packet + l2_len; 565 566 if (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4)) { 567 struct rte_ipv4_hdr *iph = l3_hdr; 568 uint16_t cksum; 569 570 iph->hdr_checksum = 0; 571 cksum = rte_raw_cksum(iph, l3_len); 572 iph->hdr_checksum = (cksum == 0xffff) ? cksum : ~cksum; 573 } 574 if (ol_flags & PKT_TX_L4_MASK) { 575 void *l4_hdr; 576 577 l4_hdr = packet + l2_len + l3_len; 578 if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM) 579 *l4_cksum = &((struct rte_udp_hdr *)l4_hdr)->dgram_cksum; 580 else if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM) 581 *l4_cksum = &((struct rte_tcp_hdr *)l4_hdr)->cksum; 582 else 583 return; 584 **l4_cksum = 0; 585 if (ol_flags & PKT_TX_IPV4) 586 *l4_phdr_cksum = rte_ipv4_phdr_cksum(l3_hdr, 0); 587 else 588 *l4_phdr_cksum = rte_ipv6_phdr_cksum(l3_hdr, 0); 589 *l4_raw_cksum = __rte_raw_cksum(l4_hdr, l4_len, 0); 590 } 591 } 592 593 static inline int 594 tap_write_mbufs(struct tx_queue *txq, uint16_t num_mbufs, 595 struct rte_mbuf **pmbufs, 596 uint16_t *num_packets, unsigned long *num_tx_bytes) 597 { 598 int i; 599 uint16_t l234_hlen; 600 struct pmd_process_private *process_private; 601 602 process_private = rte_eth_devices[txq->out_port].process_private; 603 604 for (i = 0; i < num_mbufs; i++) { 605 struct rte_mbuf *mbuf = pmbufs[i]; 606 struct iovec iovecs[mbuf->nb_segs + 2]; 607 struct tun_pi pi = { .flags = 0, .proto = 0x00 }; 608 struct rte_mbuf *seg = mbuf; 609 char m_copy[mbuf->data_len]; 610 int proto; 611 int n; 612 int j; 613 int k; /* current index in iovecs for copying segments */ 614 uint16_t seg_len; /* length of first segment */ 615 uint16_t nb_segs; 616 uint16_t *l4_cksum; /* l4 checksum (pseudo header + payload) */ 617 uint32_t l4_raw_cksum = 0; /* TCP/UDP payload raw checksum */ 618 uint16_t l4_phdr_cksum = 0; /* TCP/UDP pseudo header checksum */ 619 uint16_t is_cksum = 0; /* in case cksum should be offloaded */ 620 621 l4_cksum = NULL; 622 if (txq->type == ETH_TUNTAP_TYPE_TUN) { 623 /* 624 * TUN and TAP are created with IFF_NO_PI disabled. 625 * For TUN PMD this mandatory as fields are used by 626 * Kernel tun.c to determine whether its IP or non IP 627 * packets. 628 * 629 * The logic fetches the first byte of data from mbuf 630 * then compares whether its v4 or v6. If first byte 631 * is 4 or 6, then protocol field is updated. 632 */ 633 char *buff_data = rte_pktmbuf_mtod(seg, void *); 634 proto = (*buff_data & 0xf0); 635 pi.proto = (proto == 0x40) ? 636 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) : 637 ((proto == 0x60) ? 638 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) : 639 0x00); 640 } 641 642 k = 0; 643 iovecs[k].iov_base = π 644 iovecs[k].iov_len = sizeof(pi); 645 k++; 646 647 nb_segs = mbuf->nb_segs; 648 if (txq->csum && 649 ((mbuf->ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4) || 650 (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM || 651 (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM))) { 652 is_cksum = 1; 653 654 /* Support only packets with at least layer 4 655 * header included in the first segment 656 */ 657 seg_len = rte_pktmbuf_data_len(mbuf); 658 l234_hlen = mbuf->l2_len + mbuf->l3_len + mbuf->l4_len; 659 if (seg_len < l234_hlen) 660 return -1; 661 662 /* To change checksums, work on a * copy of l2, l3 663 * headers + l4 pseudo header 664 */ 665 rte_memcpy(m_copy, rte_pktmbuf_mtod(mbuf, void *), 666 l234_hlen); 667 tap_tx_l3_cksum(m_copy, mbuf->ol_flags, 668 mbuf->l2_len, mbuf->l3_len, mbuf->l4_len, 669 &l4_cksum, &l4_phdr_cksum, 670 &l4_raw_cksum); 671 iovecs[k].iov_base = m_copy; 672 iovecs[k].iov_len = l234_hlen; 673 k++; 674 675 /* Update next iovecs[] beyond l2, l3, l4 headers */ 676 if (seg_len > l234_hlen) { 677 iovecs[k].iov_len = seg_len - l234_hlen; 678 iovecs[k].iov_base = 679 rte_pktmbuf_mtod(seg, char *) + 680 l234_hlen; 681 tap_tx_l4_add_rcksum(iovecs[k].iov_base, 682 iovecs[k].iov_len, l4_cksum, 683 &l4_raw_cksum); 684 k++; 685 nb_segs++; 686 } 687 seg = seg->next; 688 } 689 690 for (j = k; j <= nb_segs; j++) { 691 iovecs[j].iov_len = rte_pktmbuf_data_len(seg); 692 iovecs[j].iov_base = rte_pktmbuf_mtod(seg, void *); 693 if (is_cksum) 694 tap_tx_l4_add_rcksum(iovecs[j].iov_base, 695 iovecs[j].iov_len, l4_cksum, 696 &l4_raw_cksum); 697 seg = seg->next; 698 } 699 700 if (is_cksum) 701 tap_tx_l4_cksum(l4_cksum, l4_phdr_cksum, l4_raw_cksum); 702 703 /* copy the tx frame data */ 704 n = writev(process_private->txq_fds[txq->queue_id], iovecs, j); 705 if (n <= 0) 706 return -1; 707 708 (*num_packets)++; 709 (*num_tx_bytes) += rte_pktmbuf_pkt_len(mbuf); 710 } 711 return 0; 712 } 713 714 /* Callback to handle sending packets from the tap interface 715 */ 716 static uint16_t 717 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) 718 { 719 struct tx_queue *txq = queue; 720 uint16_t num_tx = 0; 721 uint16_t num_packets = 0; 722 unsigned long num_tx_bytes = 0; 723 uint32_t max_size; 724 int i; 725 726 if (unlikely(nb_pkts == 0)) 727 return 0; 728 729 struct rte_mbuf *gso_mbufs[MAX_GSO_MBUFS]; 730 max_size = *txq->mtu + (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN + 4); 731 for (i = 0; i < nb_pkts; i++) { 732 struct rte_mbuf *mbuf_in = bufs[num_tx]; 733 struct rte_mbuf **mbuf; 734 uint16_t num_mbufs = 0; 735 uint16_t tso_segsz = 0; 736 int ret; 737 int num_tso_mbufs; 738 uint16_t hdrs_len; 739 uint64_t tso; 740 741 tso = mbuf_in->ol_flags & PKT_TX_TCP_SEG; 742 if (tso) { 743 struct rte_gso_ctx *gso_ctx = &txq->gso_ctx; 744 745 /* TCP segmentation implies TCP checksum offload */ 746 mbuf_in->ol_flags |= PKT_TX_TCP_CKSUM; 747 748 /* gso size is calculated without RTE_ETHER_CRC_LEN */ 749 hdrs_len = mbuf_in->l2_len + mbuf_in->l3_len + 750 mbuf_in->l4_len; 751 tso_segsz = mbuf_in->tso_segsz + hdrs_len; 752 if (unlikely(tso_segsz == hdrs_len) || 753 tso_segsz > *txq->mtu) { 754 txq->stats.errs++; 755 break; 756 } 757 gso_ctx->gso_size = tso_segsz; 758 /* 'mbuf_in' packet to segment */ 759 num_tso_mbufs = rte_gso_segment(mbuf_in, 760 gso_ctx, /* gso control block */ 761 (struct rte_mbuf **)&gso_mbufs, /* out mbufs */ 762 RTE_DIM(gso_mbufs)); /* max tso mbufs */ 763 764 /* ret contains the number of new created mbufs */ 765 if (num_tso_mbufs < 0) 766 break; 767 768 if (num_tso_mbufs >= 1) { 769 mbuf = gso_mbufs; 770 num_mbufs = num_tso_mbufs; 771 } else { 772 /* 0 means it can be transmitted directly 773 * without gso. 774 */ 775 mbuf = &mbuf_in; 776 num_mbufs = 1; 777 } 778 } else { 779 /* stats.errs will be incremented */ 780 if (rte_pktmbuf_pkt_len(mbuf_in) > max_size) 781 break; 782 783 /* ret 0 indicates no new mbufs were created */ 784 num_tso_mbufs = 0; 785 mbuf = &mbuf_in; 786 num_mbufs = 1; 787 } 788 789 ret = tap_write_mbufs(txq, num_mbufs, mbuf, 790 &num_packets, &num_tx_bytes); 791 if (ret == -1) { 792 txq->stats.errs++; 793 /* free tso mbufs */ 794 if (num_tso_mbufs > 0) 795 rte_pktmbuf_free_bulk(mbuf, num_tso_mbufs); 796 break; 797 } 798 num_tx++; 799 /* free original mbuf */ 800 rte_pktmbuf_free(mbuf_in); 801 /* free tso mbufs */ 802 if (num_tso_mbufs > 0) 803 rte_pktmbuf_free_bulk(mbuf, num_tso_mbufs); 804 } 805 806 txq->stats.opackets += num_packets; 807 txq->stats.errs += nb_pkts - num_tx; 808 txq->stats.obytes += num_tx_bytes; 809 810 return num_tx; 811 } 812 813 static const char * 814 tap_ioctl_req2str(unsigned long request) 815 { 816 switch (request) { 817 case SIOCSIFFLAGS: 818 return "SIOCSIFFLAGS"; 819 case SIOCGIFFLAGS: 820 return "SIOCGIFFLAGS"; 821 case SIOCGIFHWADDR: 822 return "SIOCGIFHWADDR"; 823 case SIOCSIFHWADDR: 824 return "SIOCSIFHWADDR"; 825 case SIOCSIFMTU: 826 return "SIOCSIFMTU"; 827 } 828 return "UNKNOWN"; 829 } 830 831 static int 832 tap_ioctl(struct pmd_internals *pmd, unsigned long request, 833 struct ifreq *ifr, int set, enum ioctl_mode mode) 834 { 835 short req_flags = ifr->ifr_flags; 836 int remote = pmd->remote_if_index && 837 (mode == REMOTE_ONLY || mode == LOCAL_AND_REMOTE); 838 839 if (!pmd->remote_if_index && mode == REMOTE_ONLY) 840 return 0; 841 /* 842 * If there is a remote netdevice, apply ioctl on it, then apply it on 843 * the tap netdevice. 844 */ 845 apply: 846 if (remote) 847 strlcpy(ifr->ifr_name, pmd->remote_iface, IFNAMSIZ); 848 else if (mode == LOCAL_ONLY || mode == LOCAL_AND_REMOTE) 849 strlcpy(ifr->ifr_name, pmd->name, IFNAMSIZ); 850 switch (request) { 851 case SIOCSIFFLAGS: 852 /* fetch current flags to leave other flags untouched */ 853 if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0) 854 goto error; 855 if (set) 856 ifr->ifr_flags |= req_flags; 857 else 858 ifr->ifr_flags &= ~req_flags; 859 break; 860 case SIOCGIFFLAGS: 861 case SIOCGIFHWADDR: 862 case SIOCSIFHWADDR: 863 case SIOCSIFMTU: 864 break; 865 default: 866 TAP_LOG(WARNING, "%s: ioctl() called with wrong arg", 867 pmd->name); 868 return -EINVAL; 869 } 870 if (ioctl(pmd->ioctl_sock, request, ifr) < 0) 871 goto error; 872 if (remote-- && mode == LOCAL_AND_REMOTE) 873 goto apply; 874 return 0; 875 876 error: 877 TAP_LOG(DEBUG, "%s(%s) failed: %s(%d)", ifr->ifr_name, 878 tap_ioctl_req2str(request), strerror(errno), errno); 879 return -errno; 880 } 881 882 static int 883 tap_link_set_down(struct rte_eth_dev *dev) 884 { 885 struct pmd_internals *pmd = dev->data->dev_private; 886 struct ifreq ifr = { .ifr_flags = IFF_UP }; 887 888 dev->data->dev_link.link_status = ETH_LINK_DOWN; 889 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_ONLY); 890 } 891 892 static int 893 tap_link_set_up(struct rte_eth_dev *dev) 894 { 895 struct pmd_internals *pmd = dev->data->dev_private; 896 struct ifreq ifr = { .ifr_flags = IFF_UP }; 897 898 dev->data->dev_link.link_status = ETH_LINK_UP; 899 return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE); 900 } 901 902 static int 903 tap_dev_start(struct rte_eth_dev *dev) 904 { 905 int err, i; 906 907 err = tap_intr_handle_set(dev, 1); 908 if (err) 909 return err; 910 911 err = tap_link_set_up(dev); 912 if (err) 913 return err; 914 915 for (i = 0; i < dev->data->nb_tx_queues; i++) 916 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED; 917 for (i = 0; i < dev->data->nb_rx_queues; i++) 918 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED; 919 920 return err; 921 } 922 923 /* This function gets called when the current port gets stopped. 924 */ 925 static int 926 tap_dev_stop(struct rte_eth_dev *dev) 927 { 928 int i; 929 930 for (i = 0; i < dev->data->nb_tx_queues; i++) 931 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED; 932 for (i = 0; i < dev->data->nb_rx_queues; i++) 933 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED; 934 935 tap_intr_handle_set(dev, 0); 936 tap_link_set_down(dev); 937 938 return 0; 939 } 940 941 static int 942 tap_dev_configure(struct rte_eth_dev *dev) 943 { 944 struct pmd_internals *pmd = dev->data->dev_private; 945 946 if (dev->data->nb_rx_queues > RTE_PMD_TAP_MAX_QUEUES) { 947 TAP_LOG(ERR, 948 "%s: number of rx queues %d exceeds max num of queues %d", 949 dev->device->name, 950 dev->data->nb_rx_queues, 951 RTE_PMD_TAP_MAX_QUEUES); 952 return -1; 953 } 954 if (dev->data->nb_tx_queues > RTE_PMD_TAP_MAX_QUEUES) { 955 TAP_LOG(ERR, 956 "%s: number of tx queues %d exceeds max num of queues %d", 957 dev->device->name, 958 dev->data->nb_tx_queues, 959 RTE_PMD_TAP_MAX_QUEUES); 960 return -1; 961 } 962 963 TAP_LOG(INFO, "%s: %s: TX configured queues number: %u", 964 dev->device->name, pmd->name, dev->data->nb_tx_queues); 965 966 TAP_LOG(INFO, "%s: %s: RX configured queues number: %u", 967 dev->device->name, pmd->name, dev->data->nb_rx_queues); 968 969 return 0; 970 } 971 972 static uint32_t 973 tap_dev_speed_capa(void) 974 { 975 uint32_t speed = pmd_link.link_speed; 976 uint32_t capa = 0; 977 978 if (speed >= ETH_SPEED_NUM_10M) 979 capa |= ETH_LINK_SPEED_10M; 980 if (speed >= ETH_SPEED_NUM_100M) 981 capa |= ETH_LINK_SPEED_100M; 982 if (speed >= ETH_SPEED_NUM_1G) 983 capa |= ETH_LINK_SPEED_1G; 984 if (speed >= ETH_SPEED_NUM_5G) 985 capa |= ETH_LINK_SPEED_2_5G; 986 if (speed >= ETH_SPEED_NUM_5G) 987 capa |= ETH_LINK_SPEED_5G; 988 if (speed >= ETH_SPEED_NUM_10G) 989 capa |= ETH_LINK_SPEED_10G; 990 if (speed >= ETH_SPEED_NUM_20G) 991 capa |= ETH_LINK_SPEED_20G; 992 if (speed >= ETH_SPEED_NUM_25G) 993 capa |= ETH_LINK_SPEED_25G; 994 if (speed >= ETH_SPEED_NUM_40G) 995 capa |= ETH_LINK_SPEED_40G; 996 if (speed >= ETH_SPEED_NUM_50G) 997 capa |= ETH_LINK_SPEED_50G; 998 if (speed >= ETH_SPEED_NUM_56G) 999 capa |= ETH_LINK_SPEED_56G; 1000 if (speed >= ETH_SPEED_NUM_100G) 1001 capa |= ETH_LINK_SPEED_100G; 1002 1003 return capa; 1004 } 1005 1006 static int 1007 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 1008 { 1009 struct pmd_internals *internals = dev->data->dev_private; 1010 1011 dev_info->if_index = internals->if_index; 1012 dev_info->max_mac_addrs = 1; 1013 dev_info->max_rx_pktlen = (uint32_t)RTE_ETHER_MAX_VLAN_FRAME_LEN; 1014 dev_info->max_rx_queues = RTE_PMD_TAP_MAX_QUEUES; 1015 dev_info->max_tx_queues = RTE_PMD_TAP_MAX_QUEUES; 1016 dev_info->min_rx_bufsize = 0; 1017 dev_info->speed_capa = tap_dev_speed_capa(); 1018 dev_info->rx_queue_offload_capa = tap_rx_offload_get_queue_capa(); 1019 dev_info->rx_offload_capa = tap_rx_offload_get_port_capa() | 1020 dev_info->rx_queue_offload_capa; 1021 dev_info->tx_queue_offload_capa = tap_tx_offload_get_queue_capa(); 1022 dev_info->tx_offload_capa = tap_tx_offload_get_port_capa() | 1023 dev_info->tx_queue_offload_capa; 1024 dev_info->hash_key_size = TAP_RSS_HASH_KEY_SIZE; 1025 /* 1026 * limitation: TAP supports all of IP, UDP and TCP hash 1027 * functions together and not in partial combinations 1028 */ 1029 dev_info->flow_type_rss_offloads = ~TAP_RSS_HF_MASK; 1030 1031 return 0; 1032 } 1033 1034 static int 1035 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats) 1036 { 1037 unsigned int i, imax; 1038 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0; 1039 unsigned long rx_bytes_total = 0, tx_bytes_total = 0; 1040 unsigned long rx_nombuf = 0, ierrors = 0; 1041 const struct pmd_internals *pmd = dev->data->dev_private; 1042 1043 /* rx queue statistics */ 1044 imax = (dev->data->nb_rx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ? 1045 dev->data->nb_rx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS; 1046 for (i = 0; i < imax; i++) { 1047 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets; 1048 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes; 1049 rx_total += tap_stats->q_ipackets[i]; 1050 rx_bytes_total += tap_stats->q_ibytes[i]; 1051 rx_nombuf += pmd->rxq[i].stats.rx_nombuf; 1052 ierrors += pmd->rxq[i].stats.ierrors; 1053 } 1054 1055 /* tx queue statistics */ 1056 imax = (dev->data->nb_tx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ? 1057 dev->data->nb_tx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS; 1058 1059 for (i = 0; i < imax; i++) { 1060 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets; 1061 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes; 1062 tx_total += tap_stats->q_opackets[i]; 1063 tx_err_total += pmd->txq[i].stats.errs; 1064 tx_bytes_total += tap_stats->q_obytes[i]; 1065 } 1066 1067 tap_stats->ipackets = rx_total; 1068 tap_stats->ibytes = rx_bytes_total; 1069 tap_stats->ierrors = ierrors; 1070 tap_stats->rx_nombuf = rx_nombuf; 1071 tap_stats->opackets = tx_total; 1072 tap_stats->oerrors = tx_err_total; 1073 tap_stats->obytes = tx_bytes_total; 1074 return 0; 1075 } 1076 1077 static int 1078 tap_stats_reset(struct rte_eth_dev *dev) 1079 { 1080 int i; 1081 struct pmd_internals *pmd = dev->data->dev_private; 1082 1083 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) { 1084 pmd->rxq[i].stats.ipackets = 0; 1085 pmd->rxq[i].stats.ibytes = 0; 1086 pmd->rxq[i].stats.ierrors = 0; 1087 pmd->rxq[i].stats.rx_nombuf = 0; 1088 1089 pmd->txq[i].stats.opackets = 0; 1090 pmd->txq[i].stats.errs = 0; 1091 pmd->txq[i].stats.obytes = 0; 1092 } 1093 1094 return 0; 1095 } 1096 1097 static int 1098 tap_dev_close(struct rte_eth_dev *dev) 1099 { 1100 int i; 1101 struct pmd_internals *internals = dev->data->dev_private; 1102 struct pmd_process_private *process_private = dev->process_private; 1103 struct rx_queue *rxq; 1104 1105 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 1106 rte_free(dev->process_private); 1107 return 0; 1108 } 1109 1110 tap_link_set_down(dev); 1111 if (internals->nlsk_fd != -1) { 1112 tap_flow_flush(dev, NULL); 1113 tap_flow_implicit_flush(internals, NULL); 1114 tap_nl_final(internals->nlsk_fd); 1115 internals->nlsk_fd = -1; 1116 } 1117 1118 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) { 1119 if (process_private->rxq_fds[i] != -1) { 1120 rxq = &internals->rxq[i]; 1121 close(process_private->rxq_fds[i]); 1122 process_private->rxq_fds[i] = -1; 1123 tap_rxq_pool_free(rxq->pool); 1124 rte_free(rxq->iovecs); 1125 rxq->pool = NULL; 1126 rxq->iovecs = NULL; 1127 } 1128 if (process_private->txq_fds[i] != -1) { 1129 close(process_private->txq_fds[i]); 1130 process_private->txq_fds[i] = -1; 1131 } 1132 } 1133 1134 if (internals->remote_if_index) { 1135 /* Restore initial remote state */ 1136 int ret = ioctl(internals->ioctl_sock, SIOCSIFFLAGS, 1137 &internals->remote_initial_flags); 1138 if (ret) 1139 TAP_LOG(ERR, "restore remote state failed: %d", ret); 1140 1141 } 1142 1143 rte_mempool_free(internals->gso_ctx_mp); 1144 internals->gso_ctx_mp = NULL; 1145 1146 if (internals->ka_fd != -1) { 1147 close(internals->ka_fd); 1148 internals->ka_fd = -1; 1149 } 1150 1151 /* mac_addrs must not be freed alone because part of dev_private */ 1152 dev->data->mac_addrs = NULL; 1153 1154 internals = dev->data->dev_private; 1155 TAP_LOG(DEBUG, "Closing %s Ethernet device on numa %u", 1156 tuntap_types[internals->type], rte_socket_id()); 1157 1158 if (internals->ioctl_sock != -1) { 1159 close(internals->ioctl_sock); 1160 internals->ioctl_sock = -1; 1161 } 1162 rte_free(dev->process_private); 1163 if (tap_devices_count == 1) 1164 rte_mp_action_unregister(TAP_MP_KEY); 1165 tap_devices_count--; 1166 /* 1167 * Since TUN device has no more opened file descriptors 1168 * it will be removed from kernel 1169 */ 1170 1171 return 0; 1172 } 1173 1174 static void 1175 tap_rx_queue_release(void *queue) 1176 { 1177 struct rx_queue *rxq = queue; 1178 struct pmd_process_private *process_private; 1179 1180 if (!rxq) 1181 return; 1182 process_private = rte_eth_devices[rxq->in_port].process_private; 1183 if (process_private->rxq_fds[rxq->queue_id] != -1) { 1184 close(process_private->rxq_fds[rxq->queue_id]); 1185 process_private->rxq_fds[rxq->queue_id] = -1; 1186 tap_rxq_pool_free(rxq->pool); 1187 rte_free(rxq->iovecs); 1188 rxq->pool = NULL; 1189 rxq->iovecs = NULL; 1190 } 1191 } 1192 1193 static void 1194 tap_tx_queue_release(void *queue) 1195 { 1196 struct tx_queue *txq = queue; 1197 struct pmd_process_private *process_private; 1198 1199 if (!txq) 1200 return; 1201 process_private = rte_eth_devices[txq->out_port].process_private; 1202 1203 if (process_private->txq_fds[txq->queue_id] != -1) { 1204 close(process_private->txq_fds[txq->queue_id]); 1205 process_private->txq_fds[txq->queue_id] = -1; 1206 } 1207 } 1208 1209 static int 1210 tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused) 1211 { 1212 struct rte_eth_link *dev_link = &dev->data->dev_link; 1213 struct pmd_internals *pmd = dev->data->dev_private; 1214 struct ifreq ifr = { .ifr_flags = 0 }; 1215 1216 if (pmd->remote_if_index) { 1217 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY); 1218 if (!(ifr.ifr_flags & IFF_UP) || 1219 !(ifr.ifr_flags & IFF_RUNNING)) { 1220 dev_link->link_status = ETH_LINK_DOWN; 1221 return 0; 1222 } 1223 } 1224 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY); 1225 dev_link->link_status = 1226 ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ? 1227 ETH_LINK_UP : 1228 ETH_LINK_DOWN); 1229 return 0; 1230 } 1231 1232 static int 1233 tap_promisc_enable(struct rte_eth_dev *dev) 1234 { 1235 struct pmd_internals *pmd = dev->data->dev_private; 1236 struct ifreq ifr = { .ifr_flags = IFF_PROMISC }; 1237 int ret; 1238 1239 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE); 1240 if (ret != 0) 1241 return ret; 1242 1243 if (pmd->remote_if_index && !pmd->flow_isolate) { 1244 dev->data->promiscuous = 1; 1245 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC); 1246 if (ret != 0) { 1247 /* Rollback promisc flag */ 1248 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE); 1249 /* 1250 * rte_eth_dev_promiscuous_enable() rollback 1251 * dev->data->promiscuous in the case of failure. 1252 */ 1253 return ret; 1254 } 1255 } 1256 1257 return 0; 1258 } 1259 1260 static int 1261 tap_promisc_disable(struct rte_eth_dev *dev) 1262 { 1263 struct pmd_internals *pmd = dev->data->dev_private; 1264 struct ifreq ifr = { .ifr_flags = IFF_PROMISC }; 1265 int ret; 1266 1267 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE); 1268 if (ret != 0) 1269 return ret; 1270 1271 if (pmd->remote_if_index && !pmd->flow_isolate) { 1272 dev->data->promiscuous = 0; 1273 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC); 1274 if (ret != 0) { 1275 /* Rollback promisc flag */ 1276 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE); 1277 /* 1278 * rte_eth_dev_promiscuous_disable() rollback 1279 * dev->data->promiscuous in the case of failure. 1280 */ 1281 return ret; 1282 } 1283 } 1284 1285 return 0; 1286 } 1287 1288 static int 1289 tap_allmulti_enable(struct rte_eth_dev *dev) 1290 { 1291 struct pmd_internals *pmd = dev->data->dev_private; 1292 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI }; 1293 int ret; 1294 1295 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE); 1296 if (ret != 0) 1297 return ret; 1298 1299 if (pmd->remote_if_index && !pmd->flow_isolate) { 1300 dev->data->all_multicast = 1; 1301 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI); 1302 if (ret != 0) { 1303 /* Rollback allmulti flag */ 1304 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE); 1305 /* 1306 * rte_eth_dev_allmulticast_enable() rollback 1307 * dev->data->all_multicast in the case of failure. 1308 */ 1309 return ret; 1310 } 1311 } 1312 1313 return 0; 1314 } 1315 1316 static int 1317 tap_allmulti_disable(struct rte_eth_dev *dev) 1318 { 1319 struct pmd_internals *pmd = dev->data->dev_private; 1320 struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI }; 1321 int ret; 1322 1323 ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE); 1324 if (ret != 0) 1325 return ret; 1326 1327 if (pmd->remote_if_index && !pmd->flow_isolate) { 1328 dev->data->all_multicast = 0; 1329 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI); 1330 if (ret != 0) { 1331 /* Rollback allmulti flag */ 1332 tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE); 1333 /* 1334 * rte_eth_dev_allmulticast_disable() rollback 1335 * dev->data->all_multicast in the case of failure. 1336 */ 1337 return ret; 1338 } 1339 } 1340 1341 return 0; 1342 } 1343 1344 static int 1345 tap_mac_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr) 1346 { 1347 struct pmd_internals *pmd = dev->data->dev_private; 1348 enum ioctl_mode mode = LOCAL_ONLY; 1349 struct ifreq ifr; 1350 int ret; 1351 1352 if (pmd->type == ETH_TUNTAP_TYPE_TUN) { 1353 TAP_LOG(ERR, "%s: can't MAC address for TUN", 1354 dev->device->name); 1355 return -ENOTSUP; 1356 } 1357 1358 if (rte_is_zero_ether_addr(mac_addr)) { 1359 TAP_LOG(ERR, "%s: can't set an empty MAC address", 1360 dev->device->name); 1361 return -EINVAL; 1362 } 1363 /* Check the actual current MAC address on the tap netdevice */ 1364 ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY); 1365 if (ret < 0) 1366 return ret; 1367 if (rte_is_same_ether_addr( 1368 (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data, 1369 mac_addr)) 1370 return 0; 1371 /* Check the current MAC address on the remote */ 1372 ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY); 1373 if (ret < 0) 1374 return ret; 1375 if (!rte_is_same_ether_addr( 1376 (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data, 1377 mac_addr)) 1378 mode = LOCAL_AND_REMOTE; 1379 ifr.ifr_hwaddr.sa_family = AF_LOCAL; 1380 rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, RTE_ETHER_ADDR_LEN); 1381 ret = tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, mode); 1382 if (ret < 0) 1383 return ret; 1384 rte_memcpy(&pmd->eth_addr, mac_addr, RTE_ETHER_ADDR_LEN); 1385 if (pmd->remote_if_index && !pmd->flow_isolate) { 1386 /* Replace MAC redirection rule after a MAC change */ 1387 ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC); 1388 if (ret < 0) { 1389 TAP_LOG(ERR, 1390 "%s: Couldn't delete MAC redirection rule", 1391 dev->device->name); 1392 return ret; 1393 } 1394 ret = tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC); 1395 if (ret < 0) { 1396 TAP_LOG(ERR, 1397 "%s: Couldn't add MAC redirection rule", 1398 dev->device->name); 1399 return ret; 1400 } 1401 } 1402 1403 return 0; 1404 } 1405 1406 static int 1407 tap_gso_ctx_setup(struct rte_gso_ctx *gso_ctx, struct rte_eth_dev *dev) 1408 { 1409 uint32_t gso_types; 1410 char pool_name[64]; 1411 struct pmd_internals *pmd = dev->data->dev_private; 1412 int ret; 1413 1414 /* initialize GSO context */ 1415 gso_types = DEV_TX_OFFLOAD_TCP_TSO; 1416 if (!pmd->gso_ctx_mp) { 1417 /* 1418 * Create private mbuf pool with TAP_GSO_MBUF_SEG_SIZE 1419 * bytes size per mbuf use this pool for both direct and 1420 * indirect mbufs 1421 */ 1422 ret = snprintf(pool_name, sizeof(pool_name), "mp_%s", 1423 dev->device->name); 1424 if (ret < 0 || ret >= (int)sizeof(pool_name)) { 1425 TAP_LOG(ERR, 1426 "%s: failed to create mbuf pool name for device %s," 1427 "device name too long or output error, ret: %d\n", 1428 pmd->name, dev->device->name, ret); 1429 return -ENAMETOOLONG; 1430 } 1431 pmd->gso_ctx_mp = rte_pktmbuf_pool_create(pool_name, 1432 TAP_GSO_MBUFS_NUM, TAP_GSO_MBUF_CACHE_SIZE, 0, 1433 RTE_PKTMBUF_HEADROOM + TAP_GSO_MBUF_SEG_SIZE, 1434 SOCKET_ID_ANY); 1435 if (!pmd->gso_ctx_mp) { 1436 TAP_LOG(ERR, 1437 "%s: failed to create mbuf pool for device %s\n", 1438 pmd->name, dev->device->name); 1439 return -1; 1440 } 1441 } 1442 1443 gso_ctx->direct_pool = pmd->gso_ctx_mp; 1444 gso_ctx->indirect_pool = pmd->gso_ctx_mp; 1445 gso_ctx->gso_types = gso_types; 1446 gso_ctx->gso_size = 0; /* gso_size is set in tx_burst() per packet */ 1447 gso_ctx->flag = 0; 1448 1449 return 0; 1450 } 1451 1452 static int 1453 tap_setup_queue(struct rte_eth_dev *dev, 1454 struct pmd_internals *internals, 1455 uint16_t qid, 1456 int is_rx) 1457 { 1458 int ret; 1459 int *fd; 1460 int *other_fd; 1461 const char *dir; 1462 struct pmd_internals *pmd = dev->data->dev_private; 1463 struct pmd_process_private *process_private = dev->process_private; 1464 struct rx_queue *rx = &internals->rxq[qid]; 1465 struct tx_queue *tx = &internals->txq[qid]; 1466 struct rte_gso_ctx *gso_ctx; 1467 1468 if (is_rx) { 1469 fd = &process_private->rxq_fds[qid]; 1470 other_fd = &process_private->txq_fds[qid]; 1471 dir = "rx"; 1472 gso_ctx = NULL; 1473 } else { 1474 fd = &process_private->txq_fds[qid]; 1475 other_fd = &process_private->rxq_fds[qid]; 1476 dir = "tx"; 1477 gso_ctx = &tx->gso_ctx; 1478 } 1479 if (*fd != -1) { 1480 /* fd for this queue already exists */ 1481 TAP_LOG(DEBUG, "%s: fd %d for %s queue qid %d exists", 1482 pmd->name, *fd, dir, qid); 1483 gso_ctx = NULL; 1484 } else if (*other_fd != -1) { 1485 /* Only other_fd exists. dup it */ 1486 *fd = dup(*other_fd); 1487 if (*fd < 0) { 1488 *fd = -1; 1489 TAP_LOG(ERR, "%s: dup() failed.", pmd->name); 1490 return -1; 1491 } 1492 TAP_LOG(DEBUG, "%s: dup fd %d for %s queue qid %d (%d)", 1493 pmd->name, *other_fd, dir, qid, *fd); 1494 } else { 1495 /* Both RX and TX fds do not exist (equal -1). Create fd */ 1496 *fd = tun_alloc(pmd, 0); 1497 if (*fd < 0) { 1498 *fd = -1; /* restore original value */ 1499 TAP_LOG(ERR, "%s: tun_alloc() failed.", pmd->name); 1500 return -1; 1501 } 1502 TAP_LOG(DEBUG, "%s: add %s queue for qid %d fd %d", 1503 pmd->name, dir, qid, *fd); 1504 } 1505 1506 tx->mtu = &dev->data->mtu; 1507 rx->rxmode = &dev->data->dev_conf.rxmode; 1508 if (gso_ctx) { 1509 ret = tap_gso_ctx_setup(gso_ctx, dev); 1510 if (ret) 1511 return -1; 1512 } 1513 1514 tx->type = pmd->type; 1515 1516 return *fd; 1517 } 1518 1519 static int 1520 tap_rx_queue_setup(struct rte_eth_dev *dev, 1521 uint16_t rx_queue_id, 1522 uint16_t nb_rx_desc, 1523 unsigned int socket_id, 1524 const struct rte_eth_rxconf *rx_conf __rte_unused, 1525 struct rte_mempool *mp) 1526 { 1527 struct pmd_internals *internals = dev->data->dev_private; 1528 struct pmd_process_private *process_private = dev->process_private; 1529 struct rx_queue *rxq = &internals->rxq[rx_queue_id]; 1530 struct rte_mbuf **tmp = &rxq->pool; 1531 long iov_max = sysconf(_SC_IOV_MAX); 1532 1533 if (iov_max <= 0) { 1534 TAP_LOG(WARNING, 1535 "_SC_IOV_MAX is not defined. Using %d as default", 1536 TAP_IOV_DEFAULT_MAX); 1537 iov_max = TAP_IOV_DEFAULT_MAX; 1538 } 1539 uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1); 1540 struct iovec (*iovecs)[nb_desc + 1]; 1541 int data_off = RTE_PKTMBUF_HEADROOM; 1542 int ret = 0; 1543 int fd; 1544 int i; 1545 1546 if (rx_queue_id >= dev->data->nb_rx_queues || !mp) { 1547 TAP_LOG(WARNING, 1548 "nb_rx_queues %d too small or mempool NULL", 1549 dev->data->nb_rx_queues); 1550 return -1; 1551 } 1552 1553 rxq->mp = mp; 1554 rxq->trigger_seen = 1; /* force initial burst */ 1555 rxq->in_port = dev->data->port_id; 1556 rxq->queue_id = rx_queue_id; 1557 rxq->nb_rx_desc = nb_desc; 1558 iovecs = rte_zmalloc_socket(dev->device->name, sizeof(*iovecs), 0, 1559 socket_id); 1560 if (!iovecs) { 1561 TAP_LOG(WARNING, 1562 "%s: Couldn't allocate %d RX descriptors", 1563 dev->device->name, nb_desc); 1564 return -ENOMEM; 1565 } 1566 rxq->iovecs = iovecs; 1567 1568 dev->data->rx_queues[rx_queue_id] = rxq; 1569 fd = tap_setup_queue(dev, internals, rx_queue_id, 1); 1570 if (fd == -1) { 1571 ret = fd; 1572 goto error; 1573 } 1574 1575 (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi); 1576 (*rxq->iovecs)[0].iov_base = &rxq->pi; 1577 1578 for (i = 1; i <= nb_desc; i++) { 1579 *tmp = rte_pktmbuf_alloc(rxq->mp); 1580 if (!*tmp) { 1581 TAP_LOG(WARNING, 1582 "%s: couldn't allocate memory for queue %d", 1583 dev->device->name, rx_queue_id); 1584 ret = -ENOMEM; 1585 goto error; 1586 } 1587 (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off; 1588 (*rxq->iovecs)[i].iov_base = 1589 (char *)(*tmp)->buf_addr + data_off; 1590 data_off = 0; 1591 tmp = &(*tmp)->next; 1592 } 1593 1594 TAP_LOG(DEBUG, " RX TUNTAP device name %s, qid %d on fd %d", 1595 internals->name, rx_queue_id, 1596 process_private->rxq_fds[rx_queue_id]); 1597 1598 return 0; 1599 1600 error: 1601 tap_rxq_pool_free(rxq->pool); 1602 rxq->pool = NULL; 1603 rte_free(rxq->iovecs); 1604 rxq->iovecs = NULL; 1605 return ret; 1606 } 1607 1608 static int 1609 tap_tx_queue_setup(struct rte_eth_dev *dev, 1610 uint16_t tx_queue_id, 1611 uint16_t nb_tx_desc __rte_unused, 1612 unsigned int socket_id __rte_unused, 1613 const struct rte_eth_txconf *tx_conf) 1614 { 1615 struct pmd_internals *internals = dev->data->dev_private; 1616 struct pmd_process_private *process_private = dev->process_private; 1617 struct tx_queue *txq; 1618 int ret; 1619 uint64_t offloads; 1620 1621 if (tx_queue_id >= dev->data->nb_tx_queues) 1622 return -1; 1623 dev->data->tx_queues[tx_queue_id] = &internals->txq[tx_queue_id]; 1624 txq = dev->data->tx_queues[tx_queue_id]; 1625 txq->out_port = dev->data->port_id; 1626 txq->queue_id = tx_queue_id; 1627 1628 offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads; 1629 txq->csum = !!(offloads & 1630 (DEV_TX_OFFLOAD_IPV4_CKSUM | 1631 DEV_TX_OFFLOAD_UDP_CKSUM | 1632 DEV_TX_OFFLOAD_TCP_CKSUM)); 1633 1634 ret = tap_setup_queue(dev, internals, tx_queue_id, 0); 1635 if (ret == -1) 1636 return -1; 1637 TAP_LOG(DEBUG, 1638 " TX TUNTAP device name %s, qid %d on fd %d csum %s", 1639 internals->name, tx_queue_id, 1640 process_private->txq_fds[tx_queue_id], 1641 txq->csum ? "on" : "off"); 1642 1643 return 0; 1644 } 1645 1646 static int 1647 tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu) 1648 { 1649 struct pmd_internals *pmd = dev->data->dev_private; 1650 struct ifreq ifr = { .ifr_mtu = mtu }; 1651 int err = 0; 1652 1653 err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE); 1654 if (!err) 1655 dev->data->mtu = mtu; 1656 1657 return err; 1658 } 1659 1660 static int 1661 tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused, 1662 struct rte_ether_addr *mc_addr_set __rte_unused, 1663 uint32_t nb_mc_addr __rte_unused) 1664 { 1665 /* 1666 * Nothing to do actually: the tap has no filtering whatsoever, every 1667 * packet is received. 1668 */ 1669 return 0; 1670 } 1671 1672 static int 1673 tap_nl_msg_handler(struct nlmsghdr *nh, void *arg) 1674 { 1675 struct rte_eth_dev *dev = arg; 1676 struct pmd_internals *pmd = dev->data->dev_private; 1677 struct ifinfomsg *info = NLMSG_DATA(nh); 1678 1679 if (nh->nlmsg_type != RTM_NEWLINK || 1680 (info->ifi_index != pmd->if_index && 1681 info->ifi_index != pmd->remote_if_index)) 1682 return 0; 1683 return tap_link_update(dev, 0); 1684 } 1685 1686 static void 1687 tap_dev_intr_handler(void *cb_arg) 1688 { 1689 struct rte_eth_dev *dev = cb_arg; 1690 struct pmd_internals *pmd = dev->data->dev_private; 1691 1692 tap_nl_recv(pmd->intr_handle.fd, tap_nl_msg_handler, dev); 1693 } 1694 1695 static int 1696 tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set) 1697 { 1698 struct pmd_internals *pmd = dev->data->dev_private; 1699 int ret; 1700 1701 /* In any case, disable interrupt if the conf is no longer there. */ 1702 if (!dev->data->dev_conf.intr_conf.lsc) { 1703 if (pmd->intr_handle.fd != -1) { 1704 goto clean; 1705 } 1706 return 0; 1707 } 1708 if (set) { 1709 pmd->intr_handle.fd = tap_nl_init(RTMGRP_LINK); 1710 if (unlikely(pmd->intr_handle.fd == -1)) 1711 return -EBADF; 1712 return rte_intr_callback_register( 1713 &pmd->intr_handle, tap_dev_intr_handler, dev); 1714 } 1715 1716 clean: 1717 do { 1718 ret = rte_intr_callback_unregister(&pmd->intr_handle, 1719 tap_dev_intr_handler, dev); 1720 if (ret >= 0) { 1721 break; 1722 } else if (ret == -EAGAIN) { 1723 rte_delay_ms(100); 1724 } else { 1725 TAP_LOG(ERR, "intr callback unregister failed: %d", 1726 ret); 1727 break; 1728 } 1729 } while (true); 1730 1731 tap_nl_final(pmd->intr_handle.fd); 1732 pmd->intr_handle.fd = -1; 1733 1734 return 0; 1735 } 1736 1737 static int 1738 tap_intr_handle_set(struct rte_eth_dev *dev, int set) 1739 { 1740 int err; 1741 1742 err = tap_lsc_intr_handle_set(dev, set); 1743 if (err < 0) { 1744 if (!set) 1745 tap_rx_intr_vec_set(dev, 0); 1746 return err; 1747 } 1748 err = tap_rx_intr_vec_set(dev, set); 1749 if (err && set) 1750 tap_lsc_intr_handle_set(dev, 0); 1751 return err; 1752 } 1753 1754 static const uint32_t* 1755 tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused) 1756 { 1757 static const uint32_t ptypes[] = { 1758 RTE_PTYPE_INNER_L2_ETHER, 1759 RTE_PTYPE_INNER_L2_ETHER_VLAN, 1760 RTE_PTYPE_INNER_L2_ETHER_QINQ, 1761 RTE_PTYPE_INNER_L3_IPV4, 1762 RTE_PTYPE_INNER_L3_IPV4_EXT, 1763 RTE_PTYPE_INNER_L3_IPV6, 1764 RTE_PTYPE_INNER_L3_IPV6_EXT, 1765 RTE_PTYPE_INNER_L4_FRAG, 1766 RTE_PTYPE_INNER_L4_UDP, 1767 RTE_PTYPE_INNER_L4_TCP, 1768 RTE_PTYPE_INNER_L4_SCTP, 1769 RTE_PTYPE_L2_ETHER, 1770 RTE_PTYPE_L2_ETHER_VLAN, 1771 RTE_PTYPE_L2_ETHER_QINQ, 1772 RTE_PTYPE_L3_IPV4, 1773 RTE_PTYPE_L3_IPV4_EXT, 1774 RTE_PTYPE_L3_IPV6_EXT, 1775 RTE_PTYPE_L3_IPV6, 1776 RTE_PTYPE_L4_FRAG, 1777 RTE_PTYPE_L4_UDP, 1778 RTE_PTYPE_L4_TCP, 1779 RTE_PTYPE_L4_SCTP, 1780 }; 1781 1782 return ptypes; 1783 } 1784 1785 static int 1786 tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused, 1787 struct rte_eth_fc_conf *fc_conf) 1788 { 1789 fc_conf->mode = RTE_FC_NONE; 1790 return 0; 1791 } 1792 1793 static int 1794 tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused, 1795 struct rte_eth_fc_conf *fc_conf) 1796 { 1797 if (fc_conf->mode != RTE_FC_NONE) 1798 return -ENOTSUP; 1799 return 0; 1800 } 1801 1802 /** 1803 * DPDK callback to update the RSS hash configuration. 1804 * 1805 * @param dev 1806 * Pointer to Ethernet device structure. 1807 * @param[in] rss_conf 1808 * RSS configuration data. 1809 * 1810 * @return 1811 * 0 on success, a negative errno value otherwise and rte_errno is set. 1812 */ 1813 static int 1814 tap_rss_hash_update(struct rte_eth_dev *dev, 1815 struct rte_eth_rss_conf *rss_conf) 1816 { 1817 if (rss_conf->rss_hf & TAP_RSS_HF_MASK) { 1818 rte_errno = EINVAL; 1819 return -rte_errno; 1820 } 1821 if (rss_conf->rss_key && rss_conf->rss_key_len) { 1822 /* 1823 * Currently TAP RSS key is hard coded 1824 * and cannot be updated 1825 */ 1826 TAP_LOG(ERR, 1827 "port %u RSS key cannot be updated", 1828 dev->data->port_id); 1829 rte_errno = EINVAL; 1830 return -rte_errno; 1831 } 1832 return 0; 1833 } 1834 1835 static int 1836 tap_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id) 1837 { 1838 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED; 1839 1840 return 0; 1841 } 1842 1843 static int 1844 tap_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id) 1845 { 1846 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED; 1847 1848 return 0; 1849 } 1850 1851 static int 1852 tap_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id) 1853 { 1854 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED; 1855 1856 return 0; 1857 } 1858 1859 static int 1860 tap_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id) 1861 { 1862 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED; 1863 1864 return 0; 1865 } 1866 static const struct eth_dev_ops ops = { 1867 .dev_start = tap_dev_start, 1868 .dev_stop = tap_dev_stop, 1869 .dev_close = tap_dev_close, 1870 .dev_configure = tap_dev_configure, 1871 .dev_infos_get = tap_dev_info, 1872 .rx_queue_setup = tap_rx_queue_setup, 1873 .tx_queue_setup = tap_tx_queue_setup, 1874 .rx_queue_start = tap_rx_queue_start, 1875 .tx_queue_start = tap_tx_queue_start, 1876 .rx_queue_stop = tap_rx_queue_stop, 1877 .tx_queue_stop = tap_tx_queue_stop, 1878 .rx_queue_release = tap_rx_queue_release, 1879 .tx_queue_release = tap_tx_queue_release, 1880 .flow_ctrl_get = tap_flow_ctrl_get, 1881 .flow_ctrl_set = tap_flow_ctrl_set, 1882 .link_update = tap_link_update, 1883 .dev_set_link_up = tap_link_set_up, 1884 .dev_set_link_down = tap_link_set_down, 1885 .promiscuous_enable = tap_promisc_enable, 1886 .promiscuous_disable = tap_promisc_disable, 1887 .allmulticast_enable = tap_allmulti_enable, 1888 .allmulticast_disable = tap_allmulti_disable, 1889 .mac_addr_set = tap_mac_set, 1890 .mtu_set = tap_mtu_set, 1891 .set_mc_addr_list = tap_set_mc_addr_list, 1892 .stats_get = tap_stats_get, 1893 .stats_reset = tap_stats_reset, 1894 .dev_supported_ptypes_get = tap_dev_supported_ptypes_get, 1895 .rss_hash_update = tap_rss_hash_update, 1896 .flow_ops_get = tap_dev_flow_ops_get, 1897 }; 1898 1899 static int 1900 eth_dev_tap_create(struct rte_vdev_device *vdev, const char *tap_name, 1901 char *remote_iface, struct rte_ether_addr *mac_addr, 1902 enum rte_tuntap_type type) 1903 { 1904 int numa_node = rte_socket_id(); 1905 struct rte_eth_dev *dev; 1906 struct pmd_internals *pmd; 1907 struct pmd_process_private *process_private; 1908 const char *tuntap_name = tuntap_types[type]; 1909 struct rte_eth_dev_data *data; 1910 struct ifreq ifr; 1911 int i; 1912 1913 TAP_LOG(DEBUG, "%s device on numa %u", tuntap_name, rte_socket_id()); 1914 1915 dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd)); 1916 if (!dev) { 1917 TAP_LOG(ERR, "%s Unable to allocate device struct", 1918 tuntap_name); 1919 goto error_exit_nodev; 1920 } 1921 1922 process_private = (struct pmd_process_private *) 1923 rte_zmalloc_socket(tap_name, sizeof(struct pmd_process_private), 1924 RTE_CACHE_LINE_SIZE, dev->device->numa_node); 1925 1926 if (process_private == NULL) { 1927 TAP_LOG(ERR, "Failed to alloc memory for process private"); 1928 return -1; 1929 } 1930 pmd = dev->data->dev_private; 1931 dev->process_private = process_private; 1932 pmd->dev = dev; 1933 strlcpy(pmd->name, tap_name, sizeof(pmd->name)); 1934 pmd->type = type; 1935 pmd->ka_fd = -1; 1936 pmd->nlsk_fd = -1; 1937 pmd->gso_ctx_mp = NULL; 1938 1939 pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0); 1940 if (pmd->ioctl_sock == -1) { 1941 TAP_LOG(ERR, 1942 "%s Unable to get a socket for management: %s", 1943 tuntap_name, strerror(errno)); 1944 goto error_exit; 1945 } 1946 1947 /* Setup some default values */ 1948 data = dev->data; 1949 data->dev_private = pmd; 1950 data->dev_flags = RTE_ETH_DEV_INTR_LSC | 1951 RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS; 1952 data->numa_node = numa_node; 1953 1954 data->dev_link = pmd_link; 1955 data->mac_addrs = &pmd->eth_addr; 1956 /* Set the number of RX and TX queues */ 1957 data->nb_rx_queues = 0; 1958 data->nb_tx_queues = 0; 1959 1960 dev->dev_ops = &ops; 1961 dev->rx_pkt_burst = pmd_rx_burst; 1962 dev->tx_pkt_burst = pmd_tx_burst; 1963 1964 pmd->intr_handle.type = RTE_INTR_HANDLE_EXT; 1965 pmd->intr_handle.fd = -1; 1966 dev->intr_handle = &pmd->intr_handle; 1967 1968 /* Presetup the fds to -1 as being not valid */ 1969 for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) { 1970 process_private->rxq_fds[i] = -1; 1971 process_private->txq_fds[i] = -1; 1972 } 1973 1974 if (pmd->type == ETH_TUNTAP_TYPE_TAP) { 1975 if (rte_is_zero_ether_addr(mac_addr)) 1976 rte_eth_random_addr((uint8_t *)&pmd->eth_addr); 1977 else 1978 rte_memcpy(&pmd->eth_addr, mac_addr, sizeof(*mac_addr)); 1979 } 1980 1981 /* 1982 * Allocate a TUN device keep-alive file descriptor that will only be 1983 * closed when the TUN device itself is closed or removed. 1984 * This keep-alive file descriptor will guarantee that the TUN device 1985 * exists even when all of its queues are closed 1986 */ 1987 pmd->ka_fd = tun_alloc(pmd, 1); 1988 if (pmd->ka_fd == -1) { 1989 TAP_LOG(ERR, "Unable to create %s interface", tuntap_name); 1990 goto error_exit; 1991 } 1992 TAP_LOG(DEBUG, "allocated %s", pmd->name); 1993 1994 ifr.ifr_mtu = dev->data->mtu; 1995 if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE) < 0) 1996 goto error_exit; 1997 1998 if (pmd->type == ETH_TUNTAP_TYPE_TAP) { 1999 memset(&ifr, 0, sizeof(struct ifreq)); 2000 ifr.ifr_hwaddr.sa_family = AF_LOCAL; 2001 rte_memcpy(ifr.ifr_hwaddr.sa_data, &pmd->eth_addr, 2002 RTE_ETHER_ADDR_LEN); 2003 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) 2004 goto error_exit; 2005 } 2006 2007 /* 2008 * Set up everything related to rte_flow: 2009 * - netlink socket 2010 * - tap / remote if_index 2011 * - mandatory QDISCs 2012 * - rte_flow actual/implicit lists 2013 * - implicit rules 2014 */ 2015 pmd->nlsk_fd = tap_nl_init(0); 2016 if (pmd->nlsk_fd == -1) { 2017 TAP_LOG(WARNING, "%s: failed to create netlink socket.", 2018 pmd->name); 2019 goto disable_rte_flow; 2020 } 2021 pmd->if_index = if_nametoindex(pmd->name); 2022 if (!pmd->if_index) { 2023 TAP_LOG(ERR, "%s: failed to get if_index.", pmd->name); 2024 goto disable_rte_flow; 2025 } 2026 if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) { 2027 TAP_LOG(ERR, "%s: failed to create multiq qdisc.", 2028 pmd->name); 2029 goto disable_rte_flow; 2030 } 2031 if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) { 2032 TAP_LOG(ERR, "%s: failed to create ingress qdisc.", 2033 pmd->name); 2034 goto disable_rte_flow; 2035 } 2036 LIST_INIT(&pmd->flows); 2037 2038 if (strlen(remote_iface)) { 2039 pmd->remote_if_index = if_nametoindex(remote_iface); 2040 if (!pmd->remote_if_index) { 2041 TAP_LOG(ERR, "%s: failed to get %s if_index.", 2042 pmd->name, remote_iface); 2043 goto error_remote; 2044 } 2045 strlcpy(pmd->remote_iface, remote_iface, RTE_ETH_NAME_MAX_LEN); 2046 2047 /* Save state of remote device */ 2048 tap_ioctl(pmd, SIOCGIFFLAGS, &pmd->remote_initial_flags, 0, REMOTE_ONLY); 2049 2050 /* Replicate remote MAC address */ 2051 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) { 2052 TAP_LOG(ERR, "%s: failed to get %s MAC address.", 2053 pmd->name, pmd->remote_iface); 2054 goto error_remote; 2055 } 2056 rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data, 2057 RTE_ETHER_ADDR_LEN); 2058 /* The desired MAC is already in ifreq after SIOCGIFHWADDR. */ 2059 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) { 2060 TAP_LOG(ERR, "%s: failed to get %s MAC address.", 2061 pmd->name, remote_iface); 2062 goto error_remote; 2063 } 2064 2065 /* 2066 * Flush usually returns negative value because it tries to 2067 * delete every QDISC (and on a running device, one QDISC at 2068 * least is needed). Ignore negative return value. 2069 */ 2070 qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index); 2071 if (qdisc_create_ingress(pmd->nlsk_fd, 2072 pmd->remote_if_index) < 0) { 2073 TAP_LOG(ERR, "%s: failed to create ingress qdisc.", 2074 pmd->remote_iface); 2075 goto error_remote; 2076 } 2077 LIST_INIT(&pmd->implicit_flows); 2078 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0 || 2079 tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0 || 2080 tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0 || 2081 tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0) { 2082 TAP_LOG(ERR, 2083 "%s: failed to create implicit rules.", 2084 pmd->name); 2085 goto error_remote; 2086 } 2087 } 2088 2089 rte_eth_dev_probing_finish(dev); 2090 return 0; 2091 2092 disable_rte_flow: 2093 TAP_LOG(ERR, " Disabling rte flow support: %s(%d)", 2094 strerror(errno), errno); 2095 if (strlen(remote_iface)) { 2096 TAP_LOG(ERR, "Remote feature requires flow support."); 2097 goto error_exit; 2098 } 2099 rte_eth_dev_probing_finish(dev); 2100 return 0; 2101 2102 error_remote: 2103 TAP_LOG(ERR, " Can't set up remote feature: %s(%d)", 2104 strerror(errno), errno); 2105 tap_flow_implicit_flush(pmd, NULL); 2106 2107 error_exit: 2108 if (pmd->nlsk_fd != -1) 2109 close(pmd->nlsk_fd); 2110 if (pmd->ka_fd != -1) 2111 close(pmd->ka_fd); 2112 if (pmd->ioctl_sock != -1) 2113 close(pmd->ioctl_sock); 2114 /* mac_addrs must not be freed alone because part of dev_private */ 2115 dev->data->mac_addrs = NULL; 2116 rte_eth_dev_release_port(dev); 2117 2118 error_exit_nodev: 2119 TAP_LOG(ERR, "%s Unable to initialize %s", 2120 tuntap_name, rte_vdev_device_name(vdev)); 2121 2122 return -EINVAL; 2123 } 2124 2125 /* make sure name is a possible Linux network device name */ 2126 static bool 2127 is_valid_iface(const char *name) 2128 { 2129 if (*name == '\0') 2130 return false; 2131 2132 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) 2133 return false; 2134 2135 while (*name) { 2136 if (*name == '/' || *name == ':' || isspace(*name)) 2137 return false; 2138 name++; 2139 } 2140 return true; 2141 } 2142 2143 static int 2144 set_interface_name(const char *key __rte_unused, 2145 const char *value, 2146 void *extra_args) 2147 { 2148 char *name = (char *)extra_args; 2149 2150 if (value) { 2151 if (!is_valid_iface(value)) { 2152 TAP_LOG(ERR, "TAP invalid remote interface name (%s)", 2153 value); 2154 return -1; 2155 } 2156 strlcpy(name, value, RTE_ETH_NAME_MAX_LEN); 2157 } else { 2158 /* use tap%d which causes kernel to choose next available */ 2159 strlcpy(name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN); 2160 } 2161 return 0; 2162 } 2163 2164 static int 2165 set_remote_iface(const char *key __rte_unused, 2166 const char *value, 2167 void *extra_args) 2168 { 2169 char *name = (char *)extra_args; 2170 2171 if (value) { 2172 if (!is_valid_iface(value)) { 2173 TAP_LOG(ERR, "TAP invalid remote interface name (%s)", 2174 value); 2175 return -1; 2176 } 2177 strlcpy(name, value, RTE_ETH_NAME_MAX_LEN); 2178 } 2179 2180 return 0; 2181 } 2182 2183 static int parse_user_mac(struct rte_ether_addr *user_mac, 2184 const char *value) 2185 { 2186 unsigned int index = 0; 2187 char mac_temp[strlen(ETH_TAP_USR_MAC_FMT) + 1], *mac_byte = NULL; 2188 2189 if (user_mac == NULL || value == NULL) 2190 return 0; 2191 2192 strlcpy(mac_temp, value, sizeof(mac_temp)); 2193 mac_byte = strtok(mac_temp, ":"); 2194 2195 while ((mac_byte != NULL) && 2196 (strlen(mac_byte) <= 2) && 2197 (strlen(mac_byte) == strspn(mac_byte, 2198 ETH_TAP_CMP_MAC_FMT))) { 2199 user_mac->addr_bytes[index++] = strtoul(mac_byte, NULL, 16); 2200 mac_byte = strtok(NULL, ":"); 2201 } 2202 2203 return index; 2204 } 2205 2206 static int 2207 set_mac_type(const char *key __rte_unused, 2208 const char *value, 2209 void *extra_args) 2210 { 2211 struct rte_ether_addr *user_mac = extra_args; 2212 2213 if (!value) 2214 return 0; 2215 2216 if (!strncasecmp(ETH_TAP_MAC_FIXED, value, strlen(ETH_TAP_MAC_FIXED))) { 2217 static int iface_idx; 2218 2219 /* fixed mac = 00:64:74:61:70:<iface_idx> */ 2220 memcpy((char *)user_mac->addr_bytes, "\0dtap", 2221 RTE_ETHER_ADDR_LEN); 2222 user_mac->addr_bytes[RTE_ETHER_ADDR_LEN - 1] = 2223 iface_idx++ + '0'; 2224 goto success; 2225 } 2226 2227 if (parse_user_mac(user_mac, value) != 6) 2228 goto error; 2229 success: 2230 TAP_LOG(DEBUG, "TAP user MAC param (%s)", value); 2231 return 0; 2232 2233 error: 2234 TAP_LOG(ERR, "TAP user MAC (%s) is not in format (%s|%s)", 2235 value, ETH_TAP_MAC_FIXED, ETH_TAP_USR_MAC_FMT); 2236 return -1; 2237 } 2238 2239 /* 2240 * Open a TUN interface device. TUN PMD 2241 * 1) sets tap_type as false 2242 * 2) intakes iface as argument. 2243 * 3) as interface is virtual set speed to 10G 2244 */ 2245 static int 2246 rte_pmd_tun_probe(struct rte_vdev_device *dev) 2247 { 2248 const char *name, *params; 2249 int ret; 2250 struct rte_kvargs *kvlist = NULL; 2251 char tun_name[RTE_ETH_NAME_MAX_LEN]; 2252 char remote_iface[RTE_ETH_NAME_MAX_LEN]; 2253 struct rte_eth_dev *eth_dev; 2254 2255 name = rte_vdev_device_name(dev); 2256 params = rte_vdev_device_args(dev); 2257 memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN); 2258 2259 if (rte_eal_process_type() == RTE_PROC_SECONDARY && 2260 strlen(params) == 0) { 2261 eth_dev = rte_eth_dev_attach_secondary(name); 2262 if (!eth_dev) { 2263 TAP_LOG(ERR, "Failed to probe %s", name); 2264 return -1; 2265 } 2266 eth_dev->dev_ops = &ops; 2267 eth_dev->device = &dev->device; 2268 rte_eth_dev_probing_finish(eth_dev); 2269 return 0; 2270 } 2271 2272 /* use tun%d which causes kernel to choose next available */ 2273 strlcpy(tun_name, DEFAULT_TUN_NAME "%d", RTE_ETH_NAME_MAX_LEN); 2274 2275 if (params && (params[0] != '\0')) { 2276 TAP_LOG(DEBUG, "parameters (%s)", params); 2277 2278 kvlist = rte_kvargs_parse(params, valid_arguments); 2279 if (kvlist) { 2280 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) { 2281 ret = rte_kvargs_process(kvlist, 2282 ETH_TAP_IFACE_ARG, 2283 &set_interface_name, 2284 tun_name); 2285 2286 if (ret == -1) 2287 goto leave; 2288 } 2289 } 2290 } 2291 pmd_link.link_speed = ETH_SPEED_NUM_10G; 2292 2293 TAP_LOG(DEBUG, "Initializing pmd_tun for %s", name); 2294 2295 ret = eth_dev_tap_create(dev, tun_name, remote_iface, 0, 2296 ETH_TUNTAP_TYPE_TUN); 2297 2298 leave: 2299 if (ret == -1) { 2300 TAP_LOG(ERR, "Failed to create pmd for %s as %s", 2301 name, tun_name); 2302 } 2303 rte_kvargs_free(kvlist); 2304 2305 return ret; 2306 } 2307 2308 /* Request queue file descriptors from secondary to primary. */ 2309 static int 2310 tap_mp_attach_queues(const char *port_name, struct rte_eth_dev *dev) 2311 { 2312 int ret; 2313 struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0}; 2314 struct rte_mp_msg request, *reply; 2315 struct rte_mp_reply replies; 2316 struct ipc_queues *request_param = (struct ipc_queues *)request.param; 2317 struct ipc_queues *reply_param; 2318 struct pmd_process_private *process_private = dev->process_private; 2319 int queue, fd_iterator; 2320 2321 /* Prepare the request */ 2322 memset(&request, 0, sizeof(request)); 2323 strlcpy(request.name, TAP_MP_KEY, sizeof(request.name)); 2324 strlcpy(request_param->port_name, port_name, 2325 sizeof(request_param->port_name)); 2326 request.len_param = sizeof(*request_param); 2327 /* Send request and receive reply */ 2328 ret = rte_mp_request_sync(&request, &replies, &timeout); 2329 if (ret < 0 || replies.nb_received != 1) { 2330 TAP_LOG(ERR, "Failed to request queues from primary: %d", 2331 rte_errno); 2332 return -1; 2333 } 2334 reply = &replies.msgs[0]; 2335 reply_param = (struct ipc_queues *)reply->param; 2336 TAP_LOG(DEBUG, "Received IPC reply for %s", reply_param->port_name); 2337 2338 /* Attach the queues from received file descriptors */ 2339 if (reply_param->rxq_count + reply_param->txq_count != reply->num_fds) { 2340 TAP_LOG(ERR, "Unexpected number of fds received"); 2341 return -1; 2342 } 2343 2344 dev->data->nb_rx_queues = reply_param->rxq_count; 2345 dev->data->nb_tx_queues = reply_param->txq_count; 2346 fd_iterator = 0; 2347 for (queue = 0; queue < reply_param->rxq_count; queue++) 2348 process_private->rxq_fds[queue] = reply->fds[fd_iterator++]; 2349 for (queue = 0; queue < reply_param->txq_count; queue++) 2350 process_private->txq_fds[queue] = reply->fds[fd_iterator++]; 2351 free(reply); 2352 return 0; 2353 } 2354 2355 /* Send the queue file descriptors from the primary process to secondary. */ 2356 static int 2357 tap_mp_sync_queues(const struct rte_mp_msg *request, const void *peer) 2358 { 2359 struct rte_eth_dev *dev; 2360 struct pmd_process_private *process_private; 2361 struct rte_mp_msg reply; 2362 const struct ipc_queues *request_param = 2363 (const struct ipc_queues *)request->param; 2364 struct ipc_queues *reply_param = 2365 (struct ipc_queues *)reply.param; 2366 uint16_t port_id; 2367 int queue; 2368 int ret; 2369 2370 /* Get requested port */ 2371 TAP_LOG(DEBUG, "Received IPC request for %s", request_param->port_name); 2372 ret = rte_eth_dev_get_port_by_name(request_param->port_name, &port_id); 2373 if (ret) { 2374 TAP_LOG(ERR, "Failed to get port id for %s", 2375 request_param->port_name); 2376 return -1; 2377 } 2378 dev = &rte_eth_devices[port_id]; 2379 process_private = dev->process_private; 2380 2381 /* Fill file descriptors for all queues */ 2382 reply.num_fds = 0; 2383 reply_param->rxq_count = 0; 2384 if (dev->data->nb_rx_queues + dev->data->nb_tx_queues > 2385 RTE_MP_MAX_FD_NUM){ 2386 TAP_LOG(ERR, "Number of rx/tx queues exceeds max number of fds"); 2387 return -1; 2388 } 2389 2390 for (queue = 0; queue < dev->data->nb_rx_queues; queue++) { 2391 reply.fds[reply.num_fds++] = process_private->rxq_fds[queue]; 2392 reply_param->rxq_count++; 2393 } 2394 RTE_ASSERT(reply_param->rxq_count == dev->data->nb_rx_queues); 2395 2396 reply_param->txq_count = 0; 2397 for (queue = 0; queue < dev->data->nb_tx_queues; queue++) { 2398 reply.fds[reply.num_fds++] = process_private->txq_fds[queue]; 2399 reply_param->txq_count++; 2400 } 2401 RTE_ASSERT(reply_param->txq_count == dev->data->nb_tx_queues); 2402 2403 /* Send reply */ 2404 strlcpy(reply.name, request->name, sizeof(reply.name)); 2405 strlcpy(reply_param->port_name, request_param->port_name, 2406 sizeof(reply_param->port_name)); 2407 reply.len_param = sizeof(*reply_param); 2408 if (rte_mp_reply(&reply, peer) < 0) { 2409 TAP_LOG(ERR, "Failed to reply an IPC request to sync queues"); 2410 return -1; 2411 } 2412 return 0; 2413 } 2414 2415 /* Open a TAP interface device. 2416 */ 2417 static int 2418 rte_pmd_tap_probe(struct rte_vdev_device *dev) 2419 { 2420 const char *name, *params; 2421 int ret; 2422 struct rte_kvargs *kvlist = NULL; 2423 int speed; 2424 char tap_name[RTE_ETH_NAME_MAX_LEN]; 2425 char remote_iface[RTE_ETH_NAME_MAX_LEN]; 2426 struct rte_ether_addr user_mac = { .addr_bytes = {0} }; 2427 struct rte_eth_dev *eth_dev; 2428 int tap_devices_count_increased = 0; 2429 2430 name = rte_vdev_device_name(dev); 2431 params = rte_vdev_device_args(dev); 2432 2433 if (rte_eal_process_type() == RTE_PROC_SECONDARY) { 2434 eth_dev = rte_eth_dev_attach_secondary(name); 2435 if (!eth_dev) { 2436 TAP_LOG(ERR, "Failed to probe %s", name); 2437 return -1; 2438 } 2439 eth_dev->dev_ops = &ops; 2440 eth_dev->device = &dev->device; 2441 eth_dev->rx_pkt_burst = pmd_rx_burst; 2442 eth_dev->tx_pkt_burst = pmd_tx_burst; 2443 if (!rte_eal_primary_proc_alive(NULL)) { 2444 TAP_LOG(ERR, "Primary process is missing"); 2445 return -1; 2446 } 2447 eth_dev->process_private = (struct pmd_process_private *) 2448 rte_zmalloc_socket(name, 2449 sizeof(struct pmd_process_private), 2450 RTE_CACHE_LINE_SIZE, 2451 eth_dev->device->numa_node); 2452 if (eth_dev->process_private == NULL) { 2453 TAP_LOG(ERR, 2454 "Failed to alloc memory for process private"); 2455 return -1; 2456 } 2457 2458 ret = tap_mp_attach_queues(name, eth_dev); 2459 if (ret != 0) 2460 return -1; 2461 rte_eth_dev_probing_finish(eth_dev); 2462 return 0; 2463 } 2464 2465 speed = ETH_SPEED_NUM_10G; 2466 2467 /* use tap%d which causes kernel to choose next available */ 2468 strlcpy(tap_name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN); 2469 memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN); 2470 2471 if (params && (params[0] != '\0')) { 2472 TAP_LOG(DEBUG, "parameters (%s)", params); 2473 2474 kvlist = rte_kvargs_parse(params, valid_arguments); 2475 if (kvlist) { 2476 if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) { 2477 ret = rte_kvargs_process(kvlist, 2478 ETH_TAP_IFACE_ARG, 2479 &set_interface_name, 2480 tap_name); 2481 if (ret == -1) 2482 goto leave; 2483 } 2484 2485 if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) { 2486 ret = rte_kvargs_process(kvlist, 2487 ETH_TAP_REMOTE_ARG, 2488 &set_remote_iface, 2489 remote_iface); 2490 if (ret == -1) 2491 goto leave; 2492 } 2493 2494 if (rte_kvargs_count(kvlist, ETH_TAP_MAC_ARG) == 1) { 2495 ret = rte_kvargs_process(kvlist, 2496 ETH_TAP_MAC_ARG, 2497 &set_mac_type, 2498 &user_mac); 2499 if (ret == -1) 2500 goto leave; 2501 } 2502 } 2503 } 2504 pmd_link.link_speed = speed; 2505 2506 TAP_LOG(DEBUG, "Initializing pmd_tap for %s", name); 2507 2508 /* Register IPC feed callback */ 2509 if (!tap_devices_count) { 2510 ret = rte_mp_action_register(TAP_MP_KEY, tap_mp_sync_queues); 2511 if (ret < 0 && rte_errno != ENOTSUP) { 2512 TAP_LOG(ERR, "tap: Failed to register IPC callback: %s", 2513 strerror(rte_errno)); 2514 goto leave; 2515 } 2516 } 2517 tap_devices_count++; 2518 tap_devices_count_increased = 1; 2519 ret = eth_dev_tap_create(dev, tap_name, remote_iface, &user_mac, 2520 ETH_TUNTAP_TYPE_TAP); 2521 2522 leave: 2523 if (ret == -1) { 2524 TAP_LOG(ERR, "Failed to create pmd for %s as %s", 2525 name, tap_name); 2526 if (tap_devices_count_increased == 1) { 2527 if (tap_devices_count == 1) 2528 rte_mp_action_unregister(TAP_MP_KEY); 2529 tap_devices_count--; 2530 } 2531 } 2532 rte_kvargs_free(kvlist); 2533 2534 return ret; 2535 } 2536 2537 /* detach a TUNTAP device. 2538 */ 2539 static int 2540 rte_pmd_tap_remove(struct rte_vdev_device *dev) 2541 { 2542 struct rte_eth_dev *eth_dev = NULL; 2543 2544 /* find the ethdev entry */ 2545 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev)); 2546 if (!eth_dev) 2547 return 0; 2548 2549 tap_dev_close(eth_dev); 2550 rte_eth_dev_release_port(eth_dev); 2551 2552 return 0; 2553 } 2554 2555 static struct rte_vdev_driver pmd_tun_drv = { 2556 .probe = rte_pmd_tun_probe, 2557 .remove = rte_pmd_tap_remove, 2558 }; 2559 2560 static struct rte_vdev_driver pmd_tap_drv = { 2561 .probe = rte_pmd_tap_probe, 2562 .remove = rte_pmd_tap_remove, 2563 }; 2564 2565 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv); 2566 RTE_PMD_REGISTER_VDEV(net_tun, pmd_tun_drv); 2567 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap); 2568 RTE_PMD_REGISTER_PARAM_STRING(net_tun, 2569 ETH_TAP_IFACE_ARG "=<string> "); 2570 RTE_PMD_REGISTER_PARAM_STRING(net_tap, 2571 ETH_TAP_IFACE_ARG "=<string> " 2572 ETH_TAP_MAC_ARG "=" ETH_TAP_MAC_ARG_FMT " " 2573 ETH_TAP_REMOTE_ARG "=<string>"); 2574 RTE_LOG_REGISTER(tap_logtype, pmd.net.tap, NOTICE); 2575