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