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