1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <stdint.h> 37 #include <inttypes.h> 38 #include <sys/types.h> 39 #include <sys/param.h> 40 #include <string.h> 41 #include <sys/queue.h> 42 #include <stdarg.h> 43 #include <errno.h> 44 #include <getopt.h> 45 46 #include <rte_common.h> 47 #include <rte_byteorder.h> 48 #include <rte_log.h> 49 #include <rte_memory.h> 50 #include <rte_memcpy.h> 51 #include <rte_memzone.h> 52 #include <rte_eal.h> 53 #include <rte_per_lcore.h> 54 #include <rte_launch.h> 55 #include <rte_atomic.h> 56 #include <rte_cycles.h> 57 #include <rte_prefetch.h> 58 #include <rte_lcore.h> 59 #include <rte_per_lcore.h> 60 #include <rte_branch_prediction.h> 61 #include <rte_interrupts.h> 62 #include <rte_pci.h> 63 #include <rte_random.h> 64 #include <rte_debug.h> 65 #include <rte_ether.h> 66 #include <rte_ethdev.h> 67 #include <rte_ring.h> 68 #include <rte_mempool.h> 69 #include <rte_mbuf.h> 70 #include <rte_lpm.h> 71 #include <rte_lpm6.h> 72 #include <rte_ip.h> 73 #include <rte_string_fns.h> 74 75 #include <rte_ip_frag.h> 76 77 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1 78 79 #define MBUF_SIZE (2048 + RTE_PKTMBUF_HEADROOM) 80 81 /* allow max jumbo frame 9.5 KB */ 82 #define JUMBO_FRAME_MAX_SIZE 0x2600 83 84 #define ROUNDUP_DIV(a, b) (((a) + (b) - 1) / (b)) 85 86 /* 87 * Default byte size for the IPv6 Maximum Transfer Unit (MTU). 88 * This value includes the size of IPv6 header. 89 */ 90 #define IPV4_MTU_DEFAULT ETHER_MTU 91 #define IPV6_MTU_DEFAULT ETHER_MTU 92 93 /* 94 * Default payload in bytes for the IPv6 packet. 95 */ 96 #define IPV4_DEFAULT_PAYLOAD (IPV4_MTU_DEFAULT - sizeof(struct ipv4_hdr)) 97 #define IPV6_DEFAULT_PAYLOAD (IPV6_MTU_DEFAULT - sizeof(struct ipv6_hdr)) 98 99 /* 100 * Max number of fragments per packet expected - defined by config file. 101 */ 102 #define MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG 103 104 #define NB_MBUF 8192 105 106 #define MAX_PKT_BURST 32 107 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 108 109 /* Configure how many packets ahead to prefetch, when reading packets */ 110 #define PREFETCH_OFFSET 3 111 112 /* 113 * Configurable number of RX/TX ring descriptors 114 */ 115 #define RTE_TEST_RX_DESC_DEFAULT 128 116 #define RTE_TEST_TX_DESC_DEFAULT 512 117 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; 118 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; 119 120 /* ethernet addresses of ports */ 121 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS]; 122 123 #ifndef IPv4_BYTES 124 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 125 #define IPv4_BYTES(addr) \ 126 (uint8_t) (((addr) >> 24) & 0xFF),\ 127 (uint8_t) (((addr) >> 16) & 0xFF),\ 128 (uint8_t) (((addr) >> 8) & 0xFF),\ 129 (uint8_t) ((addr) & 0xFF) 130 #endif 131 132 #ifndef IPv6_BYTES 133 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\ 134 "%02x%02x:%02x%02x:%02x%02x:%02x%02x" 135 #define IPv6_BYTES(addr) \ 136 addr[0], addr[1], addr[2], addr[3], \ 137 addr[4], addr[5], addr[6], addr[7], \ 138 addr[8], addr[9], addr[10], addr[11],\ 139 addr[12], addr[13],addr[14], addr[15] 140 #endif 141 142 #define IPV6_ADDR_LEN 16 143 144 /* mask of enabled ports */ 145 static int enabled_port_mask = 0; 146 147 static int rx_queue_per_lcore = 1; 148 149 #define MBUF_TABLE_SIZE (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG)) 150 151 struct mbuf_table { 152 uint16_t len; 153 struct rte_mbuf *m_table[MBUF_TABLE_SIZE]; 154 }; 155 156 struct rx_queue { 157 struct rte_mempool *direct_pool; 158 struct rte_mempool *indirect_pool; 159 struct rte_lpm *lpm; 160 struct rte_lpm6 *lpm6; 161 uint8_t portid; 162 }; 163 164 #define MAX_RX_QUEUE_PER_LCORE 16 165 #define MAX_TX_QUEUE_PER_PORT 16 166 struct lcore_queue_conf { 167 uint16_t n_rx_queue; 168 uint16_t tx_queue_id[RTE_MAX_ETHPORTS]; 169 struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE]; 170 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS]; 171 } __rte_cache_aligned; 172 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; 173 174 static const struct rte_eth_conf port_conf = { 175 .rxmode = { 176 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE, 177 .split_hdr_size = 0, 178 .header_split = 0, /**< Header Split disabled */ 179 .hw_ip_checksum = 1, /**< IP checksum offload enabled */ 180 .hw_vlan_filter = 0, /**< VLAN filtering disabled */ 181 .jumbo_frame = 1, /**< Jumbo Frame Support enabled */ 182 .hw_strip_crc = 0, /**< CRC stripped by hardware */ 183 }, 184 .txmode = { 185 .mq_mode = ETH_MQ_TX_NONE, 186 }, 187 }; 188 189 /* 190 * IPv4 forwarding table 191 */ 192 struct l3fwd_ipv4_route { 193 uint32_t ip; 194 uint8_t depth; 195 uint8_t if_out; 196 }; 197 198 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = { 199 {IPv4(100,10,0,0), 16, 0}, 200 {IPv4(100,20,0,0), 16, 1}, 201 {IPv4(100,30,0,0), 16, 2}, 202 {IPv4(100,40,0,0), 16, 3}, 203 {IPv4(100,50,0,0), 16, 4}, 204 {IPv4(100,60,0,0), 16, 5}, 205 {IPv4(100,70,0,0), 16, 6}, 206 {IPv4(100,80,0,0), 16, 7}, 207 }; 208 209 /* 210 * IPv6 forwarding table 211 */ 212 213 struct l3fwd_ipv6_route { 214 uint8_t ip[IPV6_ADDR_LEN]; 215 uint8_t depth; 216 uint8_t if_out; 217 }; 218 219 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = { 220 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0}, 221 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1}, 222 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2}, 223 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3}, 224 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4}, 225 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5}, 226 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6}, 227 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7}, 228 }; 229 230 #define LPM_MAX_RULES 1024 231 #define LPM6_MAX_RULES 1024 232 #define LPM6_NUMBER_TBL8S (1 << 16) 233 234 struct rte_lpm6_config lpm6_config = { 235 .max_rules = LPM6_MAX_RULES, 236 .number_tbl8s = LPM6_NUMBER_TBL8S, 237 .flags = 0 238 }; 239 240 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES]; 241 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES]; 242 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES]; 243 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES]; 244 245 /* Send burst of packets on an output interface */ 246 static inline int 247 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint8_t port) 248 { 249 struct rte_mbuf **m_table; 250 int ret; 251 uint16_t queueid; 252 253 queueid = qconf->tx_queue_id[port]; 254 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table; 255 256 ret = rte_eth_tx_burst(port, queueid, m_table, n); 257 if (unlikely(ret < n)) { 258 do { 259 rte_pktmbuf_free(m_table[ret]); 260 } while (++ret < n); 261 } 262 263 return 0; 264 } 265 266 static inline void 267 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf, 268 uint8_t queueid, uint8_t port_in) 269 { 270 struct rx_queue *rxq; 271 uint32_t i, len; 272 uint8_t next_hop, port_out, ipv6; 273 int32_t len2; 274 275 ipv6 = 0; 276 rxq = &qconf->rx_queue_list[queueid]; 277 278 /* by default, send everything back to the source port */ 279 port_out = port_in; 280 281 /* Remove the Ethernet header and trailer from the input packet */ 282 rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr)); 283 284 /* Build transmission burst */ 285 len = qconf->tx_mbufs[port_out].len; 286 287 /* if this is an IPv4 packet */ 288 if (m->ol_flags & PKT_RX_IPV4_HDR) { 289 struct ipv4_hdr *ip_hdr; 290 uint32_t ip_dst; 291 /* Read the lookup key (i.e. ip_dst) from the input packet */ 292 ip_hdr = rte_pktmbuf_mtod(m, struct ipv4_hdr *); 293 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr); 294 295 /* Find destination port */ 296 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 && 297 (enabled_port_mask & 1 << next_hop) != 0) { 298 port_out = next_hop; 299 300 /* Build transmission burst for new port */ 301 len = qconf->tx_mbufs[port_out].len; 302 } 303 304 /* if we don't need to do any fragmentation */ 305 if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) { 306 qconf->tx_mbufs[port_out].m_table[len] = m; 307 len2 = 1; 308 } else { 309 len2 = rte_ipv4_fragment_packet(m, 310 &qconf->tx_mbufs[port_out].m_table[len], 311 (uint16_t)(MBUF_TABLE_SIZE - len), 312 IPV4_MTU_DEFAULT, 313 rxq->direct_pool, rxq->indirect_pool); 314 315 /* Free input packet */ 316 rte_pktmbuf_free(m); 317 318 /* If we fail to fragment the packet */ 319 if (unlikely (len2 < 0)) 320 return; 321 } 322 } 323 /* if this is an IPv6 packet */ 324 else if (m->ol_flags & PKT_RX_IPV6_HDR) { 325 struct ipv6_hdr *ip_hdr; 326 327 ipv6 = 1; 328 329 /* Read the lookup key (i.e. ip_dst) from the input packet */ 330 ip_hdr = rte_pktmbuf_mtod(m, struct ipv6_hdr *); 331 332 /* Find destination port */ 333 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr, &next_hop) == 0 && 334 (enabled_port_mask & 1 << next_hop) != 0) { 335 port_out = next_hop; 336 337 /* Build transmission burst for new port */ 338 len = qconf->tx_mbufs[port_out].len; 339 } 340 341 /* if we don't need to do any fragmentation */ 342 if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) { 343 qconf->tx_mbufs[port_out].m_table[len] = m; 344 len2 = 1; 345 } else { 346 len2 = rte_ipv6_fragment_packet(m, 347 &qconf->tx_mbufs[port_out].m_table[len], 348 (uint16_t)(MBUF_TABLE_SIZE - len), 349 IPV6_MTU_DEFAULT, 350 rxq->direct_pool, rxq->indirect_pool); 351 352 /* Free input packet */ 353 rte_pktmbuf_free(m); 354 355 /* If we fail to fragment the packet */ 356 if (unlikely (len2 < 0)) 357 return; 358 } 359 } 360 /* else, just forward the packet */ 361 else { 362 qconf->tx_mbufs[port_out].m_table[len] = m; 363 len2 = 1; 364 } 365 366 for (i = len; i < len + len2; i ++) { 367 void *d_addr_bytes; 368 369 m = qconf->tx_mbufs[port_out].m_table[i]; 370 struct ether_hdr *eth_hdr = (struct ether_hdr *) 371 rte_pktmbuf_prepend(m, (uint16_t)sizeof(struct ether_hdr)); 372 if (eth_hdr == NULL) { 373 rte_panic("No headroom in mbuf.\n"); 374 } 375 376 m->l2_len = sizeof(struct ether_hdr); 377 378 /* 02:00:00:00:00:xx */ 379 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0]; 380 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)port_out << 40); 381 382 /* src addr */ 383 ether_addr_copy(&ports_eth_addr[port_out], ð_hdr->s_addr); 384 if (ipv6) 385 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6); 386 else 387 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4); 388 } 389 390 len += len2; 391 392 if (likely(len < MAX_PKT_BURST)) { 393 qconf->tx_mbufs[port_out].len = (uint16_t)len; 394 return; 395 } 396 397 /* Transmit packets */ 398 send_burst(qconf, (uint16_t)len, port_out); 399 qconf->tx_mbufs[port_out].len = 0; 400 } 401 402 /* main processing loop */ 403 static int 404 main_loop(__attribute__((unused)) void *dummy) 405 { 406 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 407 unsigned lcore_id; 408 uint64_t prev_tsc, diff_tsc, cur_tsc; 409 int i, j, nb_rx; 410 uint8_t portid; 411 struct lcore_queue_conf *qconf; 412 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US; 413 414 prev_tsc = 0; 415 416 lcore_id = rte_lcore_id(); 417 qconf = &lcore_queue_conf[lcore_id]; 418 419 if (qconf->n_rx_queue == 0) { 420 RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id); 421 return 0; 422 } 423 424 RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id); 425 426 for (i = 0; i < qconf->n_rx_queue; i++) { 427 428 portid = qconf->rx_queue_list[i].portid; 429 RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id, 430 (int) portid); 431 } 432 433 while (1) { 434 435 cur_tsc = rte_rdtsc(); 436 437 /* 438 * TX burst queue drain 439 */ 440 diff_tsc = cur_tsc - prev_tsc; 441 if (unlikely(diff_tsc > drain_tsc)) { 442 443 /* 444 * This could be optimized (use queueid instead of 445 * portid), but it is not called so often 446 */ 447 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 448 if (qconf->tx_mbufs[portid].len == 0) 449 continue; 450 send_burst(&lcore_queue_conf[lcore_id], 451 qconf->tx_mbufs[portid].len, 452 portid); 453 qconf->tx_mbufs[portid].len = 0; 454 } 455 456 prev_tsc = cur_tsc; 457 } 458 459 /* 460 * Read packet from RX queues 461 */ 462 for (i = 0; i < qconf->n_rx_queue; i++) { 463 464 portid = qconf->rx_queue_list[i].portid; 465 nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst, 466 MAX_PKT_BURST); 467 468 /* Prefetch first packets */ 469 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) { 470 rte_prefetch0(rte_pktmbuf_mtod( 471 pkts_burst[j], void *)); 472 } 473 474 /* Prefetch and forward already prefetched packets */ 475 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) { 476 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[ 477 j + PREFETCH_OFFSET], void *)); 478 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid); 479 } 480 481 /* Forward remaining prefetched packets */ 482 for (; j < nb_rx; j++) { 483 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid); 484 } 485 } 486 } 487 } 488 489 /* display usage */ 490 static void 491 print_usage(const char *prgname) 492 { 493 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n" 494 " -p PORTMASK: hexadecimal bitmask of ports to configure\n" 495 " -q NQ: number of queue (=ports) per lcore (default is 1)\n", 496 prgname); 497 } 498 499 static int 500 parse_portmask(const char *portmask) 501 { 502 char *end = NULL; 503 unsigned long pm; 504 505 /* parse hexadecimal string */ 506 pm = strtoul(portmask, &end, 16); 507 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 508 return -1; 509 510 if (pm == 0) 511 return -1; 512 513 return pm; 514 } 515 516 static int 517 parse_nqueue(const char *q_arg) 518 { 519 char *end = NULL; 520 unsigned long n; 521 522 /* parse hexadecimal string */ 523 n = strtoul(q_arg, &end, 10); 524 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 525 return -1; 526 if (n == 0) 527 return -1; 528 if (n >= MAX_RX_QUEUE_PER_LCORE) 529 return -1; 530 531 return n; 532 } 533 534 /* Parse the argument given in the command line of the application */ 535 static int 536 parse_args(int argc, char **argv) 537 { 538 int opt, ret; 539 char **argvopt; 540 int option_index; 541 char *prgname = argv[0]; 542 static struct option lgopts[] = { 543 {NULL, 0, 0, 0} 544 }; 545 546 argvopt = argv; 547 548 while ((opt = getopt_long(argc, argvopt, "p:q:", 549 lgopts, &option_index)) != EOF) { 550 551 switch (opt) { 552 /* portmask */ 553 case 'p': 554 enabled_port_mask = parse_portmask(optarg); 555 if (enabled_port_mask < 0) { 556 printf("invalid portmask\n"); 557 print_usage(prgname); 558 return -1; 559 } 560 break; 561 562 /* nqueue */ 563 case 'q': 564 rx_queue_per_lcore = parse_nqueue(optarg); 565 if (rx_queue_per_lcore < 0) { 566 printf("invalid queue number\n"); 567 print_usage(prgname); 568 return -1; 569 } 570 break; 571 572 /* long options */ 573 case 0: 574 print_usage(prgname); 575 return -1; 576 577 default: 578 print_usage(prgname); 579 return -1; 580 } 581 } 582 583 if (enabled_port_mask == 0) { 584 printf("portmask not specified\n"); 585 print_usage(prgname); 586 return -1; 587 } 588 589 if (optind >= 0) 590 argv[optind-1] = prgname; 591 592 ret = optind-1; 593 optind = 0; /* reset getopt lib */ 594 return ret; 595 } 596 597 static void 598 print_ethaddr(const char *name, struct ether_addr *eth_addr) 599 { 600 char buf[ETHER_ADDR_FMT_SIZE]; 601 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr); 602 printf("%s%s", name, buf); 603 } 604 605 /* Check the link status of all ports in up to 9s, and print them finally */ 606 static void 607 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask) 608 { 609 #define CHECK_INTERVAL 100 /* 100ms */ 610 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 611 uint8_t portid, count, all_ports_up, print_flag = 0; 612 struct rte_eth_link link; 613 614 printf("\nChecking link status"); 615 fflush(stdout); 616 for (count = 0; count <= MAX_CHECK_TIME; count++) { 617 all_ports_up = 1; 618 for (portid = 0; portid < port_num; portid++) { 619 if ((port_mask & (1 << portid)) == 0) 620 continue; 621 memset(&link, 0, sizeof(link)); 622 rte_eth_link_get_nowait(portid, &link); 623 /* print link status if flag set */ 624 if (print_flag == 1) { 625 if (link.link_status) 626 printf("Port %d Link Up - speed %u " 627 "Mbps - %s\n", (uint8_t)portid, 628 (unsigned)link.link_speed, 629 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? 630 ("full-duplex") : ("half-duplex\n")); 631 else 632 printf("Port %d Link Down\n", 633 (uint8_t)portid); 634 continue; 635 } 636 /* clear all_ports_up flag if any link down */ 637 if (link.link_status == 0) { 638 all_ports_up = 0; 639 break; 640 } 641 } 642 /* after finally printing all link status, get out */ 643 if (print_flag == 1) 644 break; 645 646 if (all_ports_up == 0) { 647 printf("."); 648 fflush(stdout); 649 rte_delay_ms(CHECK_INTERVAL); 650 } 651 652 /* set the print_flag if all ports up or timeout */ 653 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 654 print_flag = 1; 655 printf("\ndone\n"); 656 } 657 } 658 } 659 660 static int 661 init_routing_table(void) 662 { 663 struct rte_lpm *lpm; 664 struct rte_lpm6 *lpm6; 665 int socket, ret; 666 unsigned i; 667 668 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) { 669 if (socket_lpm[socket]) { 670 lpm = socket_lpm[socket]; 671 /* populate the LPM table */ 672 for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) { 673 ret = rte_lpm_add(lpm, 674 l3fwd_ipv4_route_array[i].ip, 675 l3fwd_ipv4_route_array[i].depth, 676 l3fwd_ipv4_route_array[i].if_out); 677 678 if (ret < 0) { 679 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd " 680 "LPM table\n", i); 681 return -1; 682 } 683 684 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT 685 "/%d (port %d)\n", 686 socket, 687 IPv4_BYTES(l3fwd_ipv4_route_array[i].ip), 688 l3fwd_ipv4_route_array[i].depth, 689 l3fwd_ipv4_route_array[i].if_out); 690 } 691 } 692 693 if (socket_lpm6[socket]) { 694 lpm6 = socket_lpm6[socket]; 695 /* populate the LPM6 table */ 696 for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) { 697 ret = rte_lpm6_add(lpm6, 698 l3fwd_ipv6_route_array[i].ip, 699 l3fwd_ipv6_route_array[i].depth, 700 l3fwd_ipv6_route_array[i].if_out); 701 702 if (ret < 0) { 703 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd " 704 "LPM6 table\n", i); 705 return -1; 706 } 707 708 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT 709 "/%d (port %d)\n", 710 socket, 711 IPv6_BYTES(l3fwd_ipv6_route_array[i].ip), 712 l3fwd_ipv6_route_array[i].depth, 713 l3fwd_ipv6_route_array[i].if_out); 714 } 715 } 716 } 717 return 0; 718 } 719 720 static int 721 init_mem(void) 722 { 723 char buf[PATH_MAX]; 724 struct rte_mempool *mp; 725 struct rte_lpm *lpm; 726 struct rte_lpm6 *lpm6; 727 int socket; 728 unsigned lcore_id; 729 730 /* traverse through lcores and initialize structures on each socket */ 731 732 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 733 734 if (rte_lcore_is_enabled(lcore_id) == 0) 735 continue; 736 737 socket = rte_lcore_to_socket_id(lcore_id); 738 739 if (socket == SOCKET_ID_ANY) 740 socket = 0; 741 742 if (socket_direct_pool[socket] == NULL) { 743 RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n", 744 socket); 745 snprintf(buf, sizeof(buf), "pool_direct_%i", socket); 746 747 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 748 0, MBUF_DATA_SIZE, socket); 749 if (mp == NULL) { 750 RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n"); 751 return -1; 752 } 753 socket_direct_pool[socket] = mp; 754 } 755 756 if (socket_indirect_pool[socket] == NULL) { 757 RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n", 758 socket); 759 snprintf(buf, sizeof(buf), "pool_indirect_%i", socket); 760 761 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0, 762 socket); 763 if (mp == NULL) { 764 RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n"); 765 return -1; 766 } 767 socket_indirect_pool[socket] = mp; 768 } 769 770 if (socket_lpm[socket] == NULL) { 771 RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket); 772 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket); 773 774 lpm = rte_lpm_create(buf, socket, LPM_MAX_RULES, 0); 775 if (lpm == NULL) { 776 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n"); 777 return -1; 778 } 779 socket_lpm[socket] = lpm; 780 } 781 782 if (socket_lpm6[socket] == NULL) { 783 RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket); 784 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket); 785 786 lpm6 = rte_lpm6_create("IP_FRAG_LPM6", socket, &lpm6_config); 787 if (lpm6 == NULL) { 788 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n"); 789 return -1; 790 } 791 socket_lpm6[socket] = lpm6; 792 } 793 } 794 795 return 0; 796 } 797 798 int 799 main(int argc, char **argv) 800 { 801 struct lcore_queue_conf *qconf; 802 struct rte_eth_dev_info dev_info; 803 struct rte_eth_txconf *txconf; 804 struct rx_queue *rxq; 805 int socket, ret; 806 unsigned nb_ports; 807 uint16_t queueid = 0; 808 unsigned lcore_id = 0, rx_lcore_id = 0; 809 uint32_t n_tx_queue, nb_lcores; 810 uint8_t portid; 811 812 /* init EAL */ 813 ret = rte_eal_init(argc, argv); 814 if (ret < 0) 815 rte_exit(EXIT_FAILURE, "rte_eal_init failed"); 816 argc -= ret; 817 argv += ret; 818 819 /* parse application arguments (after the EAL ones) */ 820 ret = parse_args(argc, argv); 821 if (ret < 0) 822 rte_exit(EXIT_FAILURE, "Invalid arguments"); 823 824 nb_ports = rte_eth_dev_count(); 825 if (nb_ports > RTE_MAX_ETHPORTS) 826 nb_ports = RTE_MAX_ETHPORTS; 827 else if (nb_ports == 0) 828 rte_exit(EXIT_FAILURE, "No ports found!\n"); 829 830 nb_lcores = rte_lcore_count(); 831 832 /* initialize structures (mempools, lpm etc.) */ 833 if (init_mem() < 0) 834 rte_panic("Cannot initialize memory structures!\n"); 835 836 /* check if portmask has non-existent ports */ 837 if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned))) 838 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n"); 839 840 /* initialize all ports */ 841 for (portid = 0; portid < nb_ports; portid++) { 842 /* skip ports that are not enabled */ 843 if ((enabled_port_mask & (1 << portid)) == 0) { 844 printf("Skipping disabled port %d\n", portid); 845 continue; 846 } 847 848 qconf = &lcore_queue_conf[rx_lcore_id]; 849 850 /* get the lcore_id for this port */ 851 while (rte_lcore_is_enabled(rx_lcore_id) == 0 || 852 qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) { 853 854 rx_lcore_id ++; 855 if (rx_lcore_id >= RTE_MAX_LCORE) 856 rte_exit(EXIT_FAILURE, "Not enough cores\n"); 857 858 qconf = &lcore_queue_conf[rx_lcore_id]; 859 } 860 861 socket = (int) rte_lcore_to_socket_id(rx_lcore_id); 862 if (socket == SOCKET_ID_ANY) 863 socket = 0; 864 865 rxq = &qconf->rx_queue_list[qconf->n_rx_queue]; 866 rxq->portid = portid; 867 rxq->direct_pool = socket_direct_pool[socket]; 868 rxq->indirect_pool = socket_indirect_pool[socket]; 869 rxq->lpm = socket_lpm[socket]; 870 rxq->lpm6 = socket_lpm6[socket]; 871 qconf->n_rx_queue++; 872 873 /* init port */ 874 printf("Initializing port %d on lcore %u...", portid, 875 rx_lcore_id); 876 fflush(stdout); 877 878 n_tx_queue = nb_lcores; 879 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT) 880 n_tx_queue = MAX_TX_QUEUE_PER_PORT; 881 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue, 882 &port_conf); 883 if (ret < 0) { 884 printf("\n"); 885 rte_exit(EXIT_FAILURE, "Cannot configure device: " 886 "err=%d, port=%d\n", 887 ret, portid); 888 } 889 890 /* init one RX queue */ 891 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd, 892 socket, NULL, 893 socket_direct_pool[socket]); 894 if (ret < 0) { 895 printf("\n"); 896 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: " 897 "err=%d, port=%d\n", 898 ret, portid); 899 } 900 901 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]); 902 print_ethaddr(" Address:", &ports_eth_addr[portid]); 903 printf("\n"); 904 905 /* init one TX queue per couple (lcore,port) */ 906 queueid = 0; 907 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 908 if (rte_lcore_is_enabled(lcore_id) == 0) 909 continue; 910 911 socket = (int) rte_lcore_to_socket_id(lcore_id); 912 printf("txq=%u,%d ", lcore_id, queueid); 913 fflush(stdout); 914 915 rte_eth_dev_info_get(portid, &dev_info); 916 txconf = &dev_info.default_txconf; 917 txconf->txq_flags = 0; 918 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd, 919 socket, txconf); 920 if (ret < 0) { 921 printf("\n"); 922 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: " 923 "err=%d, port=%d\n", ret, portid); 924 } 925 926 qconf = &lcore_queue_conf[lcore_id]; 927 qconf->tx_queue_id[portid] = queueid; 928 queueid++; 929 } 930 931 printf("\n"); 932 } 933 934 printf("\n"); 935 936 /* start ports */ 937 for (portid = 0; portid < nb_ports; portid++) { 938 if ((enabled_port_mask & (1 << portid)) == 0) { 939 continue; 940 } 941 /* Start device */ 942 ret = rte_eth_dev_start(portid); 943 if (ret < 0) 944 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n", 945 ret, portid); 946 947 rte_eth_promiscuous_enable(portid); 948 } 949 950 if (init_routing_table() < 0) 951 rte_exit(EXIT_FAILURE, "Cannot init routing table\n"); 952 953 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask); 954 955 /* launch per-lcore init on every lcore */ 956 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER); 957 RTE_LCORE_FOREACH_SLAVE(lcore_id) { 958 if (rte_eal_wait_lcore(lcore_id) < 0) 959 return -1; 960 } 961 962 return 0; 963 } 964