1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <stdint.h> 8 #include <inttypes.h> 9 #include <sys/types.h> 10 #include <string.h> 11 #include <sys/queue.h> 12 #include <stdarg.h> 13 #include <errno.h> 14 #include <getopt.h> 15 16 #include <rte_common.h> 17 #include <rte_byteorder.h> 18 #include <rte_log.h> 19 #include <rte_memory.h> 20 #include <rte_memcpy.h> 21 #include <rte_eal.h> 22 #include <rte_launch.h> 23 #include <rte_cycles.h> 24 #include <rte_prefetch.h> 25 #include <rte_lcore.h> 26 #include <rte_per_lcore.h> 27 #include <rte_branch_prediction.h> 28 #include <rte_interrupts.h> 29 #include <rte_random.h> 30 #include <rte_debug.h> 31 #include <rte_ether.h> 32 #include <rte_ethdev.h> 33 #include <rte_mempool.h> 34 #include <rte_mbuf.h> 35 #include <rte_malloc.h> 36 #include <rte_fbk_hash.h> 37 #include <rte_ip.h> 38 39 #define RTE_LOGTYPE_IPv4_MULTICAST RTE_LOGTYPE_USER1 40 41 #define MAX_PORTS 16 42 43 #define MCAST_CLONE_PORTS 2 44 #define MCAST_CLONE_SEGS 2 45 46 #define PKT_MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE 47 #define NB_PKT_MBUF 8192 48 49 #define HDR_MBUF_DATA_SIZE (2 * RTE_PKTMBUF_HEADROOM) 50 #define NB_HDR_MBUF (NB_PKT_MBUF * MAX_PORTS) 51 52 #define NB_CLONE_MBUF (NB_PKT_MBUF * MCAST_CLONE_PORTS * MCAST_CLONE_SEGS * 2) 53 54 /* allow max jumbo frame 9.5 KB */ 55 #define JUMBO_FRAME_MAX_SIZE 0x2600 56 57 #define MAX_PKT_BURST 32 58 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 59 60 /* Configure how many packets ahead to prefetch, when reading packets */ 61 #define PREFETCH_OFFSET 3 62 63 /* 64 * Construct Ethernet multicast address from IPv4 multicast address. 65 * Citing RFC 1112, section 6.4: 66 * "An IP host group address is mapped to an Ethernet multicast address 67 * by placing the low-order 23-bits of the IP address into the low-order 68 * 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex)." 69 */ 70 71 /* Construct Ethernet multicast address from IPv4 multicast Address. 8< */ 72 #define ETHER_ADDR_FOR_IPV4_MCAST(x) \ 73 (rte_cpu_to_be_64(0x01005e000000ULL | ((x) & 0x7fffff)) >> 16) 74 /* >8 End of Construction of multicast address from IPv4 multicast address. */ 75 76 /* 77 * Configurable number of RX/TX ring descriptors 78 */ 79 #define RX_DESC_DEFAULT 1024 80 #define TX_DESC_DEFAULT 1024 81 static uint16_t nb_rxd = RX_DESC_DEFAULT; 82 static uint16_t nb_txd = TX_DESC_DEFAULT; 83 84 /* ethernet addresses of ports */ 85 static struct rte_ether_addr ports_eth_addr[MAX_PORTS]; 86 87 /* mask of enabled ports */ 88 static uint32_t enabled_port_mask = 0; 89 90 static uint16_t nb_ports; 91 92 static int rx_queue_per_lcore = 1; 93 94 struct mbuf_table { 95 uint16_t len; 96 struct rte_mbuf *m_table[MAX_PKT_BURST]; 97 }; 98 99 #define MAX_RX_QUEUE_PER_LCORE 16 100 #define MAX_TX_QUEUE_PER_PORT 16 101 struct lcore_queue_conf { 102 uint64_t tx_tsc; 103 uint16_t n_rx_queue; 104 uint8_t rx_queue_list[MAX_RX_QUEUE_PER_LCORE]; 105 uint16_t tx_queue_id[MAX_PORTS]; 106 struct mbuf_table tx_mbufs[MAX_PORTS]; 107 } __rte_cache_aligned; 108 static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; 109 110 static struct rte_eth_conf port_conf = { 111 .rxmode = { 112 .mtu = JUMBO_FRAME_MAX_SIZE - RTE_ETHER_HDR_LEN - 113 RTE_ETHER_CRC_LEN, 114 }, 115 .txmode = { 116 .mq_mode = RTE_ETH_MQ_TX_NONE, 117 .offloads = RTE_ETH_TX_OFFLOAD_MULTI_SEGS, 118 }, 119 }; 120 121 static struct rte_mempool *packet_pool, *header_pool, *clone_pool; 122 123 124 /* Multicast */ 125 static struct rte_fbk_hash_params mcast_hash_params = { 126 .name = "MCAST_HASH", 127 .entries = 1024, 128 .entries_per_bucket = 4, 129 .socket_id = 0, 130 .hash_func = NULL, 131 .init_val = 0, 132 }; 133 134 struct rte_fbk_hash_table *mcast_hash = NULL; 135 136 struct mcast_group_params { 137 uint32_t ip; 138 uint16_t port_mask; 139 }; 140 141 static struct mcast_group_params mcast_group_table[] = { 142 {RTE_IPV4(224,0,0,101), 0x1}, 143 {RTE_IPV4(224,0,0,102), 0x2}, 144 {RTE_IPV4(224,0,0,103), 0x3}, 145 {RTE_IPV4(224,0,0,104), 0x4}, 146 {RTE_IPV4(224,0,0,105), 0x5}, 147 {RTE_IPV4(224,0,0,106), 0x6}, 148 {RTE_IPV4(224,0,0,107), 0x7}, 149 {RTE_IPV4(224,0,0,108), 0x8}, 150 {RTE_IPV4(224,0,0,109), 0x9}, 151 {RTE_IPV4(224,0,0,110), 0xA}, 152 {RTE_IPV4(224,0,0,111), 0xB}, 153 {RTE_IPV4(224,0,0,112), 0xC}, 154 {RTE_IPV4(224,0,0,113), 0xD}, 155 {RTE_IPV4(224,0,0,114), 0xE}, 156 {RTE_IPV4(224,0,0,115), 0xF}, 157 }; 158 159 /* Send burst of packets on an output interface */ 160 static void 161 send_burst(struct lcore_queue_conf *qconf, uint16_t port) 162 { 163 struct rte_mbuf **m_table; 164 uint16_t n, queueid; 165 int ret; 166 167 queueid = qconf->tx_queue_id[port]; 168 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table; 169 n = qconf->tx_mbufs[port].len; 170 171 ret = rte_eth_tx_burst(port, queueid, m_table, n); 172 while (unlikely (ret < n)) { 173 rte_pktmbuf_free(m_table[ret]); 174 ret++; 175 } 176 177 qconf->tx_mbufs[port].len = 0; 178 } 179 180 /* Get number of bits set. 8< */ 181 static inline uint32_t 182 bitcnt(uint32_t v) 183 { 184 uint32_t n; 185 186 for (n = 0; v != 0; v &= v - 1, n++) 187 ; 188 189 return n; 190 } 191 /* >8 End of getting number of bits set. */ 192 193 /** 194 * Create the output multicast packet based on the given input packet. 195 * There are two approaches for creating outgoing packet, though both 196 * are based on data zero-copy idea, they differ in few details: 197 * First one creates a clone of the input packet, e.g - walk though all 198 * segments of the input packet, and for each of them create a new packet 199 * mbuf and attach that new mbuf to the segment (refer to rte_pktmbuf_clone() 200 * for more details). Then new mbuf is allocated for the packet header 201 * and is prepended to the 'clone' mbuf. 202 * Second approach doesn't make a clone, it just increment refcnt for all 203 * input packet segments. Then it allocates new mbuf for the packet header 204 * and prepends it to the input packet. 205 * Basically first approach reuses only input packet's data, but creates 206 * it's own copy of packet's metadata. Second approach reuses both input's 207 * packet data and metadata. 208 * The advantage of first approach - is that each outgoing packet has it's 209 * own copy of metadata, so we can safely modify data pointer of the 210 * input packet. That allows us to skip creation if the output packet for 211 * the last destination port, but instead modify input packet's header inplace, 212 * e.g: for N destination ports we need to invoke mcast_out_pkt (N-1) times. 213 * The advantage of second approach - less work for each outgoing packet, 214 * e.g: we skip "clone" operation completely. Though it comes with a price - 215 * input packet's metadata has to be intact. So for N destination ports we 216 * need to invoke mcast_out_pkt N times. 217 * So for small number of outgoing ports (and segments in the input packet) 218 * first approach will be faster. 219 * As number of outgoing ports (and/or input segments) will grow, 220 * second way will become more preferable. 221 * 222 * @param pkt 223 * Input packet mbuf. 224 * @param use_clone 225 * Control which of the two approaches described above should be used: 226 * - 0 - use second approach: 227 * Don't "clone" input packet. 228 * Prepend new header directly to the input packet 229 * - 1 - use first approach: 230 * Make a "clone" of input packet first. 231 * Prepend new header to the clone of the input packet 232 * @return 233 * - The pointer to the new outgoing packet. 234 * - NULL if operation failed. 235 */ 236 237 /* mcast_out_pkt 8< */ 238 static inline struct rte_mbuf * 239 mcast_out_pkt(struct rte_mbuf *pkt, int use_clone) 240 { 241 struct rte_mbuf *hdr; 242 243 /* Create new mbuf for the header. */ 244 if (unlikely ((hdr = rte_pktmbuf_alloc(header_pool)) == NULL)) 245 return NULL; 246 247 /* If requested, then make a new clone packet. */ 248 if (use_clone != 0 && 249 unlikely ((pkt = rte_pktmbuf_clone(pkt, clone_pool)) == NULL)) { 250 rte_pktmbuf_free(hdr); 251 return NULL; 252 } 253 254 /* prepend new header */ 255 hdr->next = pkt; 256 257 /* update header's fields */ 258 hdr->pkt_len = (uint16_t)(hdr->data_len + pkt->pkt_len); 259 hdr->nb_segs = pkt->nb_segs + 1; 260 261 __rte_mbuf_sanity_check(hdr, 1); 262 return hdr; 263 } 264 /* >8 End of mcast_out_kt. */ 265 266 /* 267 * Write new Ethernet header to the outgoing packet, 268 * and put it into the outgoing queue for the given port. 269 */ 270 271 /* Write new Ethernet header to outgoing packets. 8< */ 272 static inline void 273 mcast_send_pkt(struct rte_mbuf *pkt, struct rte_ether_addr *dest_addr, 274 struct lcore_queue_conf *qconf, uint16_t port) 275 { 276 struct rte_ether_hdr *ethdr; 277 uint16_t len; 278 279 /* Construct Ethernet header. */ 280 ethdr = (struct rte_ether_hdr *) 281 rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(*ethdr)); 282 RTE_ASSERT(ethdr != NULL); 283 284 rte_ether_addr_copy(dest_addr, ðdr->dst_addr); 285 rte_ether_addr_copy(&ports_eth_addr[port], ðdr->src_addr); 286 ethdr->ether_type = rte_be_to_cpu_16(RTE_ETHER_TYPE_IPV4); 287 288 /* Put new packet into the output queue */ 289 len = qconf->tx_mbufs[port].len; 290 qconf->tx_mbufs[port].m_table[len] = pkt; 291 qconf->tx_mbufs[port].len = ++len; 292 293 /* Transmit packets */ 294 if (unlikely(MAX_PKT_BURST == len)) 295 send_burst(qconf, port); 296 } 297 /* >8 End of writing new Ethernet headers. */ 298 299 /* Multicast forward of the input packet */ 300 static inline void 301 mcast_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf) 302 { 303 struct rte_mbuf *mc; 304 struct rte_ipv4_hdr *iphdr; 305 uint32_t dest_addr, port_mask, port_num, use_clone; 306 int32_t hash; 307 uint16_t port; 308 union { 309 uint64_t as_int; 310 struct rte_ether_addr as_addr; 311 } dst_eth_addr; 312 313 /* Remove the Ethernet header from the input packet. 8< */ 314 iphdr = (struct rte_ipv4_hdr *) 315 rte_pktmbuf_adj(m, (uint16_t)sizeof(struct rte_ether_hdr)); 316 RTE_ASSERT(iphdr != NULL); 317 318 dest_addr = rte_be_to_cpu_32(iphdr->dst_addr); 319 /* >8 End of removing the Ethernet header from the input packet. */ 320 321 /* 322 * Check that it is a valid multicast address and 323 * we have some active ports assigned to it. 324 */ 325 326 /* Check valid multicast address. 8< */ 327 if (!RTE_IS_IPV4_MCAST(dest_addr) || 328 (hash = rte_fbk_hash_lookup(mcast_hash, dest_addr)) <= 0 || 329 (port_mask = hash & enabled_port_mask) == 0) { 330 rte_pktmbuf_free(m); 331 return; 332 } 333 /* >8 End of valid multicast address check. */ 334 335 /* Calculate number of destination ports. */ 336 port_num = bitcnt(port_mask); 337 338 /* Should we use rte_pktmbuf_clone() or not. 8< */ 339 use_clone = (port_num <= MCAST_CLONE_PORTS && 340 m->nb_segs <= MCAST_CLONE_SEGS); 341 /* >8 End of using rte_pktmbuf_clone(). */ 342 343 /* Mark all packet's segments as referenced port_num times */ 344 if (use_clone == 0) 345 rte_pktmbuf_refcnt_update(m, (uint16_t)port_num); 346 347 /* Construct destination ethernet address. 8< */ 348 dst_eth_addr.as_int = ETHER_ADDR_FOR_IPV4_MCAST(dest_addr); 349 /* >8 End of constructing destination ethernet address. */ 350 351 /* Packets dispatched to destination ports. 8< */ 352 for (port = 0; use_clone != port_mask; port_mask >>= 1, port++) { 353 354 /* Prepare output packet and send it out. */ 355 if ((port_mask & 1) != 0) { 356 if (likely ((mc = mcast_out_pkt(m, use_clone)) != NULL)) 357 mcast_send_pkt(mc, &dst_eth_addr.as_addr, 358 qconf, port); 359 else if (use_clone == 0) 360 rte_pktmbuf_free(m); 361 } 362 } 363 /* >8 End of packets dispatched to destination ports. */ 364 365 /* 366 * If we making clone packets, then, for the last destination port, 367 * we can overwrite input packet's metadata. 368 */ 369 if (use_clone != 0) 370 mcast_send_pkt(m, &dst_eth_addr.as_addr, qconf, port); 371 else 372 rte_pktmbuf_free(m); 373 } 374 375 /* Send burst of outgoing packet, if timeout expires. */ 376 static inline void 377 send_timeout_burst(struct lcore_queue_conf *qconf) 378 { 379 uint64_t cur_tsc; 380 uint16_t portid; 381 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US; 382 383 cur_tsc = rte_rdtsc(); 384 if (likely (cur_tsc < qconf->tx_tsc + drain_tsc)) 385 return; 386 387 for (portid = 0; portid < MAX_PORTS; portid++) { 388 if (qconf->tx_mbufs[portid].len != 0) 389 send_burst(qconf, portid); 390 } 391 qconf->tx_tsc = cur_tsc; 392 } 393 394 /* main processing loop */ 395 static int 396 main_loop(__rte_unused void *dummy) 397 { 398 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 399 unsigned lcore_id; 400 int i, j, nb_rx; 401 uint16_t portid; 402 struct lcore_queue_conf *qconf; 403 404 lcore_id = rte_lcore_id(); 405 qconf = &lcore_queue_conf[lcore_id]; 406 407 408 if (qconf->n_rx_queue == 0) { 409 RTE_LOG(INFO, IPv4_MULTICAST, "lcore %u has nothing to do\n", 410 lcore_id); 411 return 0; 412 } 413 414 RTE_LOG(INFO, IPv4_MULTICAST, "entering main loop on lcore %u\n", 415 lcore_id); 416 417 for (i = 0; i < qconf->n_rx_queue; i++) { 418 419 portid = qconf->rx_queue_list[i]; 420 RTE_LOG(INFO, IPv4_MULTICAST, " -- lcoreid=%u portid=%d\n", 421 lcore_id, portid); 422 } 423 424 while (1) { 425 426 /* 427 * Read packet from RX queues 428 */ 429 for (i = 0; i < qconf->n_rx_queue; i++) { 430 431 portid = qconf->rx_queue_list[i]; 432 nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst, 433 MAX_PKT_BURST); 434 435 /* Prefetch first packets */ 436 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) { 437 rte_prefetch0(rte_pktmbuf_mtod( 438 pkts_burst[j], void *)); 439 } 440 441 /* Prefetch and forward already prefetched packets */ 442 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) { 443 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[ 444 j + PREFETCH_OFFSET], void *)); 445 mcast_forward(pkts_burst[j], qconf); 446 } 447 448 /* Forward remaining prefetched packets */ 449 for (; j < nb_rx; j++) { 450 mcast_forward(pkts_burst[j], qconf); 451 } 452 } 453 454 /* Send out packets from TX queues */ 455 send_timeout_burst(qconf); 456 } 457 } 458 459 /* display usage */ 460 static void 461 print_usage(const char *prgname) 462 { 463 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n" 464 " -p PORTMASK: hexadecimal bitmask of ports to configure\n" 465 " -q NQ: number of queue (=ports) per lcore (default is 1)\n", 466 prgname); 467 } 468 469 static uint32_t 470 parse_portmask(const char *portmask) 471 { 472 char *end = NULL; 473 unsigned long pm; 474 475 /* parse hexadecimal string */ 476 pm = strtoul(portmask, &end, 16); 477 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 478 return 0; 479 480 return (uint32_t)pm; 481 } 482 483 static int 484 parse_nqueue(const char *q_arg) 485 { 486 char *end = NULL; 487 unsigned long n; 488 489 /* parse numerical string */ 490 errno = 0; 491 n = strtoul(q_arg, &end, 0); 492 if (errno != 0 || end == NULL || *end != '\0' || 493 n == 0 || n >= MAX_RX_QUEUE_PER_LCORE) 494 return -1; 495 496 return n; 497 } 498 499 /* Parse the argument given in the command line of the application */ 500 static int 501 parse_args(int argc, char **argv) 502 { 503 int opt, ret; 504 char **argvopt; 505 int option_index; 506 char *prgname = argv[0]; 507 static struct option lgopts[] = { 508 {NULL, 0, 0, 0} 509 }; 510 511 argvopt = argv; 512 513 while ((opt = getopt_long(argc, argvopt, "p:q:", 514 lgopts, &option_index)) != EOF) { 515 516 switch (opt) { 517 /* portmask */ 518 case 'p': 519 enabled_port_mask = parse_portmask(optarg); 520 if (enabled_port_mask == 0) { 521 printf("invalid portmask\n"); 522 print_usage(prgname); 523 return -1; 524 } 525 break; 526 527 /* nqueue */ 528 case 'q': 529 rx_queue_per_lcore = parse_nqueue(optarg); 530 if (rx_queue_per_lcore < 0) { 531 printf("invalid queue number\n"); 532 print_usage(prgname); 533 return -1; 534 } 535 break; 536 537 default: 538 print_usage(prgname); 539 return -1; 540 } 541 } 542 543 if (optind >= 0) 544 argv[optind-1] = prgname; 545 546 ret = optind-1; 547 optind = 1; /* reset getopt lib */ 548 return ret; 549 } 550 551 static void 552 print_ethaddr(const char *name, struct rte_ether_addr *eth_addr) 553 { 554 char buf[RTE_ETHER_ADDR_FMT_SIZE]; 555 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr); 556 printf("%s%s", name, buf); 557 } 558 559 /* Hash object is created and loaded. 8< */ 560 static int 561 init_mcast_hash(void) 562 { 563 uint32_t i; 564 565 mcast_hash_params.socket_id = rte_socket_id(); 566 mcast_hash = rte_fbk_hash_create(&mcast_hash_params); 567 if (mcast_hash == NULL){ 568 return -1; 569 } 570 571 for (i = 0; i < RTE_DIM(mcast_group_table); i++) { 572 if (rte_fbk_hash_add_key(mcast_hash, 573 mcast_group_table[i].ip, 574 mcast_group_table[i].port_mask) < 0) { 575 return -1; 576 } 577 } 578 579 return 0; 580 } 581 /* >8 End of hash object is created and loaded. */ 582 583 /* Check the link status of all ports in up to 9s, and print them finally */ 584 static void 585 check_all_ports_link_status(uint32_t port_mask) 586 { 587 #define CHECK_INTERVAL 100 /* 100ms */ 588 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 589 uint16_t portid; 590 uint8_t count, all_ports_up, print_flag = 0; 591 struct rte_eth_link link; 592 int ret; 593 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN]; 594 595 printf("\nChecking link status"); 596 fflush(stdout); 597 for (count = 0; count <= MAX_CHECK_TIME; count++) { 598 all_ports_up = 1; 599 RTE_ETH_FOREACH_DEV(portid) { 600 if ((port_mask & (1 << portid)) == 0) 601 continue; 602 memset(&link, 0, sizeof(link)); 603 ret = rte_eth_link_get_nowait(portid, &link); 604 if (ret < 0) { 605 all_ports_up = 0; 606 if (print_flag == 1) 607 printf("Port %u link get failed: %s\n", 608 portid, rte_strerror(-ret)); 609 continue; 610 } 611 /* print link status if flag set */ 612 if (print_flag == 1) { 613 rte_eth_link_to_str(link_status_text, 614 sizeof(link_status_text), 615 &link); 616 printf("Port %d %s\n", portid, 617 link_status_text); 618 continue; 619 } 620 /* clear all_ports_up flag if any link down */ 621 if (link.link_status == RTE_ETH_LINK_DOWN) { 622 all_ports_up = 0; 623 break; 624 } 625 } 626 /* after finally printing all link status, get out */ 627 if (print_flag == 1) 628 break; 629 630 if (all_ports_up == 0) { 631 printf("."); 632 fflush(stdout); 633 rte_delay_ms(CHECK_INTERVAL); 634 } 635 636 /* set the print_flag if all ports up or timeout */ 637 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 638 print_flag = 1; 639 printf("done\n"); 640 } 641 } 642 } 643 644 int 645 main(int argc, char **argv) 646 { 647 struct lcore_queue_conf *qconf; 648 struct rte_eth_dev_info dev_info; 649 struct rte_eth_txconf *txconf; 650 int ret; 651 uint16_t queueid; 652 unsigned lcore_id = 0, rx_lcore_id = 0; 653 uint32_t n_tx_queue, nb_lcores; 654 uint16_t portid; 655 656 /* init EAL */ 657 ret = rte_eal_init(argc, argv); 658 if (ret < 0) 659 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n"); 660 argc -= ret; 661 argv += ret; 662 663 /* parse application arguments (after the EAL ones) */ 664 ret = parse_args(argc, argv); 665 if (ret < 0) 666 rte_exit(EXIT_FAILURE, "Invalid IPV4_MULTICAST parameters\n"); 667 668 /* Create the mbuf pools. 8< */ 669 packet_pool = rte_pktmbuf_pool_create("packet_pool", NB_PKT_MBUF, 32, 670 0, PKT_MBUF_DATA_SIZE, rte_socket_id()); 671 672 if (packet_pool == NULL) 673 rte_exit(EXIT_FAILURE, "Cannot init packet mbuf pool\n"); 674 675 header_pool = rte_pktmbuf_pool_create("header_pool", NB_HDR_MBUF, 32, 676 0, HDR_MBUF_DATA_SIZE, rte_socket_id()); 677 678 if (header_pool == NULL) 679 rte_exit(EXIT_FAILURE, "Cannot init header mbuf pool\n"); 680 681 clone_pool = rte_pktmbuf_pool_create("clone_pool", NB_CLONE_MBUF, 32, 682 0, 0, rte_socket_id()); 683 684 if (clone_pool == NULL) 685 rte_exit(EXIT_FAILURE, "Cannot init clone mbuf pool\n"); 686 /* >8 End of create mbuf pools. */ 687 688 nb_ports = rte_eth_dev_count_avail(); 689 if (nb_ports == 0) 690 rte_exit(EXIT_FAILURE, "No physical ports!\n"); 691 if (nb_ports > MAX_PORTS) 692 nb_ports = MAX_PORTS; 693 694 nb_lcores = rte_lcore_count(); 695 696 /* initialize all ports */ 697 RTE_ETH_FOREACH_DEV(portid) { 698 struct rte_eth_rxconf rxq_conf; 699 struct rte_eth_conf local_port_conf = port_conf; 700 701 /* skip ports that are not enabled */ 702 if ((enabled_port_mask & (1 << portid)) == 0) { 703 printf("Skipping disabled port %d\n", portid); 704 continue; 705 } 706 707 qconf = &lcore_queue_conf[rx_lcore_id]; 708 709 /* limit the frame size to the maximum supported by NIC */ 710 ret = rte_eth_dev_info_get(portid, &dev_info); 711 if (ret != 0) 712 rte_exit(EXIT_FAILURE, 713 "Error during getting device (port %u) info: %s\n", 714 portid, strerror(-ret)); 715 716 local_port_conf.rxmode.mtu = RTE_MIN( 717 dev_info.max_mtu, 718 local_port_conf.rxmode.mtu); 719 720 /* get the lcore_id for this port */ 721 while (rte_lcore_is_enabled(rx_lcore_id) == 0 || 722 qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) { 723 724 rx_lcore_id ++; 725 qconf = &lcore_queue_conf[rx_lcore_id]; 726 727 if (rx_lcore_id >= RTE_MAX_LCORE) 728 rte_exit(EXIT_FAILURE, "Not enough cores\n"); 729 } 730 qconf->rx_queue_list[qconf->n_rx_queue] = portid; 731 qconf->n_rx_queue++; 732 733 /* init port */ 734 printf("Initializing port %d on lcore %u... ", portid, 735 rx_lcore_id); 736 fflush(stdout); 737 738 n_tx_queue = nb_lcores; 739 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT) 740 n_tx_queue = MAX_TX_QUEUE_PER_PORT; 741 742 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue, 743 &local_port_conf); 744 if (ret < 0) 745 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n", 746 ret, portid); 747 748 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, 749 &nb_txd); 750 if (ret < 0) 751 rte_exit(EXIT_FAILURE, 752 "Cannot adjust number of descriptors: err=%d, port=%d\n", 753 ret, portid); 754 755 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]); 756 if (ret < 0) 757 rte_exit(EXIT_FAILURE, 758 "Cannot get MAC address: err=%d, port=%d\n", 759 ret, portid); 760 761 print_ethaddr(" Address:", &ports_eth_addr[portid]); 762 printf(", "); 763 764 /* init one RX queue */ 765 queueid = 0; 766 printf("rxq=%hu ", queueid); 767 fflush(stdout); 768 rxq_conf = dev_info.default_rxconf; 769 rxq_conf.offloads = local_port_conf.rxmode.offloads; 770 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd, 771 rte_eth_dev_socket_id(portid), 772 &rxq_conf, 773 packet_pool); 774 if (ret < 0) 775 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, port=%d\n", 776 ret, portid); 777 778 /* init one TX queue per couple (lcore,port) */ 779 queueid = 0; 780 781 RTE_LCORE_FOREACH(lcore_id) { 782 if (rte_lcore_is_enabled(lcore_id) == 0) 783 continue; 784 printf("txq=%u,%hu ", lcore_id, queueid); 785 fflush(stdout); 786 787 txconf = &dev_info.default_txconf; 788 txconf->offloads = local_port_conf.txmode.offloads; 789 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd, 790 rte_lcore_to_socket_id(lcore_id), txconf); 791 if (ret < 0) 792 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, " 793 "port=%d\n", ret, portid); 794 795 qconf = &lcore_queue_conf[lcore_id]; 796 qconf->tx_queue_id[portid] = queueid; 797 queueid++; 798 } 799 ret = rte_eth_allmulticast_enable(portid); 800 if (ret < 0) 801 rte_exit(EXIT_FAILURE, 802 "rte_eth_allmulticast_enable: err=%d, port=%d\n", 803 ret, portid); 804 /* Start device */ 805 ret = rte_eth_dev_start(portid); 806 if (ret < 0) 807 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n", 808 ret, portid); 809 810 printf("done:\n"); 811 } 812 813 check_all_ports_link_status(enabled_port_mask); 814 815 /* initialize the multicast hash */ 816 int retval = init_mcast_hash(); 817 if (retval != 0) 818 rte_exit(EXIT_FAILURE, "Cannot build the multicast hash\n"); 819 820 /* launch per-lcore init on every lcore */ 821 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN); 822 RTE_LCORE_FOREACH_WORKER(lcore_id) { 823 if (rte_eal_wait_lcore(lcore_id) < 0) 824 return -1; 825 } 826 827 /* clean up the EAL */ 828 rte_eal_cleanup(); 829 830 return 0; 831 } 832