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