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