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 <string.h> 40 #include <sys/queue.h> 41 #include <stdarg.h> 42 #include <errno.h> 43 #include <getopt.h> 44 #include <signal.h> 45 #include <sys/param.h> 46 47 #include <rte_common.h> 48 #include <rte_byteorder.h> 49 #include <rte_log.h> 50 #include <rte_memory.h> 51 #include <rte_memcpy.h> 52 #include <rte_memzone.h> 53 #include <rte_eal.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_malloc.h> 70 #include <rte_ip.h> 71 #include <rte_tcp.h> 72 #include <rte_udp.h> 73 #include <rte_string_fns.h> 74 #include <rte_lpm.h> 75 #include <rte_lpm6.h> 76 77 #include <rte_ip_frag.h> 78 79 #define MAX_PKT_BURST 32 80 81 82 #define RTE_LOGTYPE_IP_RSMBL RTE_LOGTYPE_USER1 83 84 #define MAX_JUMBO_PKT_LEN 9600 85 86 #define BUF_SIZE RTE_MBUF_DEFAULT_DATAROOM 87 #define MBUF_SIZE \ 88 (BUF_SIZE + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM) 89 90 #define NB_MBUF 8192 91 92 /* allow max jumbo frame 9.5 KB */ 93 #define JUMBO_FRAME_MAX_SIZE 0x2600 94 95 #define MAX_FLOW_NUM UINT16_MAX 96 #define MIN_FLOW_NUM 1 97 #define DEF_FLOW_NUM 0x1000 98 99 /* TTL numbers are in ms. */ 100 #define MAX_FLOW_TTL (3600 * MS_PER_S) 101 #define MIN_FLOW_TTL 1 102 #define DEF_FLOW_TTL MS_PER_S 103 104 #define MAX_FRAG_NUM RTE_LIBRTE_IP_FRAG_MAX_FRAG 105 106 /* Should be power of two. */ 107 #define IP_FRAG_TBL_BUCKET_ENTRIES 16 108 109 static uint32_t max_flow_num = DEF_FLOW_NUM; 110 static uint32_t max_flow_ttl = DEF_FLOW_TTL; 111 112 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 113 114 #define NB_SOCKETS 8 115 116 /* Configure how many packets ahead to prefetch, when reading packets */ 117 #define PREFETCH_OFFSET 3 118 119 /* 120 * Configurable number of RX/TX ring descriptors 121 */ 122 #define RTE_TEST_RX_DESC_DEFAULT 128 123 #define RTE_TEST_TX_DESC_DEFAULT 512 124 125 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; 126 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; 127 128 /* ethernet addresses of ports */ 129 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS]; 130 131 #ifndef IPv4_BYTES 132 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 133 #define IPv4_BYTES(addr) \ 134 (uint8_t) (((addr) >> 24) & 0xFF),\ 135 (uint8_t) (((addr) >> 16) & 0xFF),\ 136 (uint8_t) (((addr) >> 8) & 0xFF),\ 137 (uint8_t) ((addr) & 0xFF) 138 #endif 139 140 #ifndef IPv6_BYTES 141 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\ 142 "%02x%02x:%02x%02x:%02x%02x:%02x%02x" 143 #define IPv6_BYTES(addr) \ 144 addr[0], addr[1], addr[2], addr[3], \ 145 addr[4], addr[5], addr[6], addr[7], \ 146 addr[8], addr[9], addr[10], addr[11],\ 147 addr[12], addr[13],addr[14], addr[15] 148 #endif 149 150 #define IPV6_ADDR_LEN 16 151 152 /* mask of enabled ports */ 153 static uint32_t enabled_port_mask = 0; 154 155 static int rx_queue_per_lcore = 1; 156 157 struct mbuf_table { 158 uint32_t len; 159 uint32_t head; 160 uint32_t tail; 161 struct rte_mbuf *m_table[0]; 162 }; 163 164 struct rx_queue { 165 struct rte_ip_frag_tbl *frag_tbl; 166 struct rte_mempool *pool; 167 struct rte_lpm *lpm; 168 struct rte_lpm6 *lpm6; 169 uint16_t portid; 170 }; 171 172 struct tx_lcore_stat { 173 uint64_t call; 174 uint64_t drop; 175 uint64_t queue; 176 uint64_t send; 177 }; 178 179 #define MAX_RX_QUEUE_PER_LCORE 16 180 #define MAX_TX_QUEUE_PER_PORT 16 181 #define MAX_RX_QUEUE_PER_PORT 128 182 183 struct lcore_queue_conf { 184 uint16_t n_rx_queue; 185 struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE]; 186 uint16_t tx_queue_id[RTE_MAX_ETHPORTS]; 187 struct rte_ip_frag_death_row death_row; 188 struct mbuf_table *tx_mbufs[RTE_MAX_ETHPORTS]; 189 struct tx_lcore_stat tx_stat; 190 } __rte_cache_aligned; 191 static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; 192 193 static struct rte_eth_conf port_conf = { 194 .rxmode = { 195 .mq_mode = ETH_MQ_RX_RSS, 196 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE, 197 .split_hdr_size = 0, 198 .header_split = 0, /**< Header Split disabled */ 199 .hw_ip_checksum = 1, /**< IP checksum offload enabled */ 200 .hw_vlan_filter = 0, /**< VLAN filtering disabled */ 201 .jumbo_frame = 1, /**< Jumbo Frame Support disabled */ 202 .hw_strip_crc = 1, /**< CRC stripped by hardware */ 203 }, 204 .rx_adv_conf = { 205 .rss_conf = { 206 .rss_key = NULL, 207 .rss_hf = ETH_RSS_IP, 208 }, 209 }, 210 .txmode = { 211 .mq_mode = ETH_MQ_TX_NONE, 212 }, 213 }; 214 215 /* 216 * IPv4 forwarding table 217 */ 218 struct l3fwd_ipv4_route { 219 uint32_t ip; 220 uint8_t depth; 221 uint8_t if_out; 222 }; 223 224 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = { 225 {IPv4(100,10,0,0), 16, 0}, 226 {IPv4(100,20,0,0), 16, 1}, 227 {IPv4(100,30,0,0), 16, 2}, 228 {IPv4(100,40,0,0), 16, 3}, 229 {IPv4(100,50,0,0), 16, 4}, 230 {IPv4(100,60,0,0), 16, 5}, 231 {IPv4(100,70,0,0), 16, 6}, 232 {IPv4(100,80,0,0), 16, 7}, 233 }; 234 235 /* 236 * IPv6 forwarding table 237 */ 238 239 struct l3fwd_ipv6_route { 240 uint8_t ip[IPV6_ADDR_LEN]; 241 uint8_t depth; 242 uint8_t if_out; 243 }; 244 245 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = { 246 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0}, 247 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1}, 248 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2}, 249 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3}, 250 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4}, 251 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5}, 252 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6}, 253 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7}, 254 }; 255 256 #define LPM_MAX_RULES 1024 257 #define LPM6_MAX_RULES 1024 258 #define LPM6_NUMBER_TBL8S (1 << 16) 259 260 struct rte_lpm6_config lpm6_config = { 261 .max_rules = LPM6_MAX_RULES, 262 .number_tbl8s = LPM6_NUMBER_TBL8S, 263 .flags = 0 264 }; 265 266 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES]; 267 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES]; 268 269 #ifdef RTE_LIBRTE_IP_FRAG_TBL_STAT 270 #define TX_LCORE_STAT_UPDATE(s, f, v) ((s)->f += (v)) 271 #else 272 #define TX_LCORE_STAT_UPDATE(s, f, v) do {} while (0) 273 #endif /* RTE_LIBRTE_IP_FRAG_TBL_STAT */ 274 275 /* 276 * If number of queued packets reached given threahold, then 277 * send burst of packets on an output interface. 278 */ 279 static inline uint32_t 280 send_burst(struct lcore_queue_conf *qconf, uint32_t thresh, uint16_t port) 281 { 282 uint32_t fill, len, k, n; 283 struct mbuf_table *txmb; 284 285 txmb = qconf->tx_mbufs[port]; 286 len = txmb->len; 287 288 if ((int32_t)(fill = txmb->head - txmb->tail) < 0) 289 fill += len; 290 291 if (fill >= thresh) { 292 n = RTE_MIN(len - txmb->tail, fill); 293 294 k = rte_eth_tx_burst(port, qconf->tx_queue_id[port], 295 txmb->m_table + txmb->tail, (uint16_t)n); 296 297 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, call, 1); 298 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, send, k); 299 300 fill -= k; 301 if ((txmb->tail += k) == len) 302 txmb->tail = 0; 303 } 304 305 return fill; 306 } 307 308 /* Enqueue a single packet, and send burst if queue is filled */ 309 static inline int 310 send_single_packet(struct rte_mbuf *m, uint16_t port) 311 { 312 uint32_t fill, lcore_id, len; 313 struct lcore_queue_conf *qconf; 314 struct mbuf_table *txmb; 315 316 lcore_id = rte_lcore_id(); 317 qconf = &lcore_queue_conf[lcore_id]; 318 319 txmb = qconf->tx_mbufs[port]; 320 len = txmb->len; 321 322 fill = send_burst(qconf, MAX_PKT_BURST, port); 323 324 if (fill == len - 1) { 325 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, drop, 1); 326 rte_pktmbuf_free(txmb->m_table[txmb->tail]); 327 if (++txmb->tail == len) 328 txmb->tail = 0; 329 } 330 331 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, queue, 1); 332 txmb->m_table[txmb->head] = m; 333 if(++txmb->head == len) 334 txmb->head = 0; 335 336 return 0; 337 } 338 339 static inline void 340 reassemble(struct rte_mbuf *m, uint16_t portid, uint32_t queue, 341 struct lcore_queue_conf *qconf, uint64_t tms) 342 { 343 struct ether_hdr *eth_hdr; 344 struct rte_ip_frag_tbl *tbl; 345 struct rte_ip_frag_death_row *dr; 346 struct rx_queue *rxq; 347 void *d_addr_bytes; 348 uint32_t next_hop; 349 uint16_t dst_port; 350 351 rxq = &qconf->rx_queue_list[queue]; 352 353 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *); 354 355 dst_port = portid; 356 357 /* if packet is IPv4 */ 358 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) { 359 struct ipv4_hdr *ip_hdr; 360 uint32_t ip_dst; 361 362 ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1); 363 364 /* if it is a fragmented packet, then try to reassemble. */ 365 if (rte_ipv4_frag_pkt_is_fragmented(ip_hdr)) { 366 struct rte_mbuf *mo; 367 368 tbl = rxq->frag_tbl; 369 dr = &qconf->death_row; 370 371 /* prepare mbuf: setup l2_len/l3_len. */ 372 m->l2_len = sizeof(*eth_hdr); 373 m->l3_len = sizeof(*ip_hdr); 374 375 /* process this fragment. */ 376 mo = rte_ipv4_frag_reassemble_packet(tbl, dr, m, tms, ip_hdr); 377 if (mo == NULL) 378 /* no packet to send out. */ 379 return; 380 381 /* we have our packet reassembled. */ 382 if (mo != m) { 383 m = mo; 384 eth_hdr = rte_pktmbuf_mtod(m, 385 struct ether_hdr *); 386 ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1); 387 } 388 } 389 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr); 390 391 /* Find destination port */ 392 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 && 393 (enabled_port_mask & 1 << next_hop) != 0) { 394 dst_port = next_hop; 395 } 396 397 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4); 398 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) { 399 /* if packet is IPv6 */ 400 struct ipv6_extension_fragment *frag_hdr; 401 struct ipv6_hdr *ip_hdr; 402 403 ip_hdr = (struct ipv6_hdr *)(eth_hdr + 1); 404 405 frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(ip_hdr); 406 407 if (frag_hdr != NULL) { 408 struct rte_mbuf *mo; 409 410 tbl = rxq->frag_tbl; 411 dr = &qconf->death_row; 412 413 /* prepare mbuf: setup l2_len/l3_len. */ 414 m->l2_len = sizeof(*eth_hdr); 415 m->l3_len = sizeof(*ip_hdr) + sizeof(*frag_hdr); 416 417 mo = rte_ipv6_frag_reassemble_packet(tbl, dr, m, tms, ip_hdr, frag_hdr); 418 if (mo == NULL) 419 return; 420 421 if (mo != m) { 422 m = mo; 423 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *); 424 ip_hdr = (struct ipv6_hdr *)(eth_hdr + 1); 425 } 426 } 427 428 /* Find destination port */ 429 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr, 430 &next_hop) == 0 && 431 (enabled_port_mask & 1 << next_hop) != 0) { 432 dst_port = next_hop; 433 } 434 435 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6); 436 } 437 /* if packet wasn't IPv4 or IPv6, it's forwarded to the port it came from */ 438 439 /* 02:00:00:00:00:xx */ 440 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0]; 441 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)dst_port << 40); 442 443 /* src addr */ 444 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr); 445 446 send_single_packet(m, dst_port); 447 } 448 449 /* main processing loop */ 450 static int 451 main_loop(__attribute__((unused)) void *dummy) 452 { 453 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 454 unsigned lcore_id; 455 uint64_t diff_tsc, cur_tsc, prev_tsc; 456 int i, j, nb_rx; 457 uint16_t portid; 458 struct lcore_queue_conf *qconf; 459 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US; 460 461 prev_tsc = 0; 462 463 lcore_id = rte_lcore_id(); 464 qconf = &lcore_queue_conf[lcore_id]; 465 466 if (qconf->n_rx_queue == 0) { 467 RTE_LOG(INFO, IP_RSMBL, "lcore %u has nothing to do\n", lcore_id); 468 return 0; 469 } 470 471 RTE_LOG(INFO, IP_RSMBL, "entering main loop on lcore %u\n", lcore_id); 472 473 for (i = 0; i < qconf->n_rx_queue; i++) { 474 475 portid = qconf->rx_queue_list[i].portid; 476 RTE_LOG(INFO, IP_RSMBL, " -- lcoreid=%u portid=%u\n", lcore_id, 477 portid); 478 } 479 480 while (1) { 481 482 cur_tsc = rte_rdtsc(); 483 484 /* 485 * TX burst queue drain 486 */ 487 diff_tsc = cur_tsc - prev_tsc; 488 if (unlikely(diff_tsc > drain_tsc)) { 489 490 /* 491 * This could be optimized (use queueid instead of 492 * portid), but it is not called so often 493 */ 494 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 495 if ((enabled_port_mask & (1 << portid)) != 0) 496 send_burst(qconf, 1, portid); 497 } 498 499 prev_tsc = cur_tsc; 500 } 501 502 /* 503 * Read packet from RX queues 504 */ 505 for (i = 0; i < qconf->n_rx_queue; ++i) { 506 507 portid = qconf->rx_queue_list[i].portid; 508 509 nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst, 510 MAX_PKT_BURST); 511 512 /* Prefetch first packets */ 513 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) { 514 rte_prefetch0(rte_pktmbuf_mtod( 515 pkts_burst[j], void *)); 516 } 517 518 /* Prefetch and forward already prefetched packets */ 519 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) { 520 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[ 521 j + PREFETCH_OFFSET], void *)); 522 reassemble(pkts_burst[j], portid, 523 i, qconf, cur_tsc); 524 } 525 526 /* Forward remaining prefetched packets */ 527 for (; j < nb_rx; j++) { 528 reassemble(pkts_burst[j], portid, 529 i, qconf, cur_tsc); 530 } 531 532 rte_ip_frag_free_death_row(&qconf->death_row, 533 PREFETCH_OFFSET); 534 } 535 } 536 } 537 538 /* display usage */ 539 static void 540 print_usage(const char *prgname) 541 { 542 printf("%s [EAL options] -- -p PORTMASK [-q NQ]" 543 " [--max-pkt-len PKTLEN]" 544 " [--maxflows=<flows>] [--flowttl=<ttl>[(s|ms)]]\n" 545 " -p PORTMASK: hexadecimal bitmask of ports to configure\n" 546 " -q NQ: number of RX queues per lcore\n" 547 " --maxflows=<flows>: optional, maximum number of flows " 548 "supported\n" 549 " --flowttl=<ttl>[(s|ms)]: optional, maximum TTL for each " 550 "flow\n", 551 prgname); 552 } 553 554 static uint32_t 555 parse_flow_num(const char *str, uint32_t min, uint32_t max, uint32_t *val) 556 { 557 char *end; 558 uint64_t v; 559 560 /* parse decimal string */ 561 errno = 0; 562 v = strtoul(str, &end, 10); 563 if (errno != 0 || *end != '\0') 564 return -EINVAL; 565 566 if (v < min || v > max) 567 return -EINVAL; 568 569 *val = (uint32_t)v; 570 return 0; 571 } 572 573 static int 574 parse_flow_ttl(const char *str, uint32_t min, uint32_t max, uint32_t *val) 575 { 576 char *end; 577 uint64_t v; 578 579 static const char frmt_sec[] = "s"; 580 static const char frmt_msec[] = "ms"; 581 582 /* parse decimal string */ 583 errno = 0; 584 v = strtoul(str, &end, 10); 585 if (errno != 0) 586 return -EINVAL; 587 588 if (*end != '\0') { 589 if (strncmp(frmt_sec, end, sizeof(frmt_sec)) == 0) 590 v *= MS_PER_S; 591 else if (strncmp(frmt_msec, end, sizeof (frmt_msec)) != 0) 592 return -EINVAL; 593 } 594 595 if (v < min || v > max) 596 return -EINVAL; 597 598 *val = (uint32_t)v; 599 return 0; 600 } 601 602 static int 603 parse_portmask(const char *portmask) 604 { 605 char *end = NULL; 606 unsigned long pm; 607 608 /* parse hexadecimal string */ 609 pm = strtoul(portmask, &end, 16); 610 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 611 return -1; 612 613 if (pm == 0) 614 return -1; 615 616 return pm; 617 } 618 619 static int 620 parse_nqueue(const char *q_arg) 621 { 622 char *end = NULL; 623 unsigned long n; 624 625 printf("%p\n", q_arg); 626 627 /* parse hexadecimal string */ 628 n = strtoul(q_arg, &end, 10); 629 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 630 return -1; 631 if (n == 0) 632 return -1; 633 if (n >= MAX_RX_QUEUE_PER_LCORE) 634 return -1; 635 636 return n; 637 } 638 639 /* Parse the argument given in the command line of the application */ 640 static int 641 parse_args(int argc, char **argv) 642 { 643 int opt, ret; 644 char **argvopt; 645 int option_index; 646 char *prgname = argv[0]; 647 static struct option lgopts[] = { 648 {"max-pkt-len", 1, 0, 0}, 649 {"maxflows", 1, 0, 0}, 650 {"flowttl", 1, 0, 0}, 651 {NULL, 0, 0, 0} 652 }; 653 654 argvopt = argv; 655 656 while ((opt = getopt_long(argc, argvopt, "p:q:", 657 lgopts, &option_index)) != EOF) { 658 659 switch (opt) { 660 /* portmask */ 661 case 'p': 662 enabled_port_mask = parse_portmask(optarg); 663 if (enabled_port_mask == 0) { 664 printf("invalid portmask\n"); 665 print_usage(prgname); 666 return -1; 667 } 668 break; 669 670 /* nqueue */ 671 case 'q': 672 rx_queue_per_lcore = parse_nqueue(optarg); 673 if (rx_queue_per_lcore < 0) { 674 printf("invalid queue number\n"); 675 print_usage(prgname); 676 return -1; 677 } 678 break; 679 680 /* long options */ 681 case 0: 682 if (!strncmp(lgopts[option_index].name, 683 "maxflows", 8)) { 684 if ((ret = parse_flow_num(optarg, MIN_FLOW_NUM, 685 MAX_FLOW_NUM, 686 &max_flow_num)) != 0) { 687 printf("invalid value: \"%s\" for " 688 "parameter %s\n", 689 optarg, 690 lgopts[option_index].name); 691 print_usage(prgname); 692 return ret; 693 } 694 } 695 696 if (!strncmp(lgopts[option_index].name, "flowttl", 7)) { 697 if ((ret = parse_flow_ttl(optarg, MIN_FLOW_TTL, 698 MAX_FLOW_TTL, 699 &max_flow_ttl)) != 0) { 700 printf("invalid value: \"%s\" for " 701 "parameter %s\n", 702 optarg, 703 lgopts[option_index].name); 704 print_usage(prgname); 705 return ret; 706 } 707 } 708 709 break; 710 711 default: 712 print_usage(prgname); 713 return -1; 714 } 715 } 716 717 if (optind >= 0) 718 argv[optind-1] = prgname; 719 720 ret = optind-1; 721 optind = 1; /* reset getopt lib */ 722 return ret; 723 } 724 725 static void 726 print_ethaddr(const char *name, const struct ether_addr *eth_addr) 727 { 728 char buf[ETHER_ADDR_FMT_SIZE]; 729 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr); 730 printf("%s%s", name, buf); 731 } 732 733 /* Check the link status of all ports in up to 9s, and print them finally */ 734 static void 735 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask) 736 { 737 #define CHECK_INTERVAL 100 /* 100ms */ 738 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 739 uint16_t portid; 740 uint8_t count, all_ports_up, print_flag = 0; 741 struct rte_eth_link link; 742 743 printf("\nChecking link status"); 744 fflush(stdout); 745 for (count = 0; count <= MAX_CHECK_TIME; count++) { 746 all_ports_up = 1; 747 for (portid = 0; portid < port_num; portid++) { 748 if ((port_mask & (1 << portid)) == 0) 749 continue; 750 memset(&link, 0, sizeof(link)); 751 rte_eth_link_get_nowait(portid, &link); 752 /* print link status if flag set */ 753 if (print_flag == 1) { 754 if (link.link_status) 755 printf( 756 "Port%d Link Up. Speed %u Mbps - %s\n", 757 portid, link.link_speed, 758 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? 759 ("full-duplex") : ("half-duplex\n")); 760 else 761 printf("Port %d Link Down\n", portid); 762 continue; 763 } 764 /* clear all_ports_up flag if any link down */ 765 if (link.link_status == ETH_LINK_DOWN) { 766 all_ports_up = 0; 767 break; 768 } 769 } 770 /* after finally printing all link status, get out */ 771 if (print_flag == 1) 772 break; 773 774 if (all_ports_up == 0) { 775 printf("."); 776 fflush(stdout); 777 rte_delay_ms(CHECK_INTERVAL); 778 } 779 780 /* set the print_flag if all ports up or timeout */ 781 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 782 print_flag = 1; 783 printf("\ndone\n"); 784 } 785 } 786 } 787 788 static int 789 init_routing_table(void) 790 { 791 struct rte_lpm *lpm; 792 struct rte_lpm6 *lpm6; 793 int socket, ret; 794 unsigned i; 795 796 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) { 797 if (socket_lpm[socket]) { 798 lpm = socket_lpm[socket]; 799 /* populate the LPM table */ 800 for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) { 801 ret = rte_lpm_add(lpm, 802 l3fwd_ipv4_route_array[i].ip, 803 l3fwd_ipv4_route_array[i].depth, 804 l3fwd_ipv4_route_array[i].if_out); 805 806 if (ret < 0) { 807 RTE_LOG(ERR, IP_RSMBL, "Unable to add entry %i to the l3fwd " 808 "LPM table\n", i); 809 return -1; 810 } 811 812 RTE_LOG(INFO, IP_RSMBL, "Socket %i: adding route " IPv4_BYTES_FMT 813 "/%d (port %d)\n", 814 socket, 815 IPv4_BYTES(l3fwd_ipv4_route_array[i].ip), 816 l3fwd_ipv4_route_array[i].depth, 817 l3fwd_ipv4_route_array[i].if_out); 818 } 819 } 820 821 if (socket_lpm6[socket]) { 822 lpm6 = socket_lpm6[socket]; 823 /* populate the LPM6 table */ 824 for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) { 825 ret = rte_lpm6_add(lpm6, 826 l3fwd_ipv6_route_array[i].ip, 827 l3fwd_ipv6_route_array[i].depth, 828 l3fwd_ipv6_route_array[i].if_out); 829 830 if (ret < 0) { 831 RTE_LOG(ERR, IP_RSMBL, "Unable to add entry %i to the l3fwd " 832 "LPM6 table\n", i); 833 return -1; 834 } 835 836 RTE_LOG(INFO, IP_RSMBL, "Socket %i: adding route " IPv6_BYTES_FMT 837 "/%d (port %d)\n", 838 socket, 839 IPv6_BYTES(l3fwd_ipv6_route_array[i].ip), 840 l3fwd_ipv6_route_array[i].depth, 841 l3fwd_ipv6_route_array[i].if_out); 842 } 843 } 844 } 845 return 0; 846 } 847 848 static int 849 setup_port_tbl(struct lcore_queue_conf *qconf, uint32_t lcore, int socket, 850 uint32_t port) 851 { 852 struct mbuf_table *mtb; 853 uint32_t n; 854 size_t sz; 855 856 n = RTE_MAX(max_flow_num, 2UL * MAX_PKT_BURST); 857 sz = sizeof (*mtb) + sizeof (mtb->m_table[0]) * n; 858 859 if ((mtb = rte_zmalloc_socket(__func__, sz, RTE_CACHE_LINE_SIZE, 860 socket)) == NULL) { 861 RTE_LOG(ERR, IP_RSMBL, "%s() for lcore: %u, port: %u " 862 "failed to allocate %zu bytes\n", 863 __func__, lcore, port, sz); 864 return -1; 865 } 866 867 mtb->len = n; 868 qconf->tx_mbufs[port] = mtb; 869 870 return 0; 871 } 872 873 static int 874 setup_queue_tbl(struct rx_queue *rxq, uint32_t lcore, uint32_t queue) 875 { 876 int socket; 877 uint32_t nb_mbuf; 878 uint64_t frag_cycles; 879 char buf[RTE_MEMPOOL_NAMESIZE]; 880 881 socket = rte_lcore_to_socket_id(lcore); 882 if (socket == SOCKET_ID_ANY) 883 socket = 0; 884 885 frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * 886 max_flow_ttl; 887 888 if ((rxq->frag_tbl = rte_ip_frag_table_create(max_flow_num, 889 IP_FRAG_TBL_BUCKET_ENTRIES, max_flow_num, frag_cycles, 890 socket)) == NULL) { 891 RTE_LOG(ERR, IP_RSMBL, "ip_frag_tbl_create(%u) on " 892 "lcore: %u for queue: %u failed\n", 893 max_flow_num, lcore, queue); 894 return -1; 895 } 896 897 /* 898 * At any given moment up to <max_flow_num * (MAX_FRAG_NUM)> 899 * mbufs could be stored int the fragment table. 900 * Plus, each TX queue can hold up to <max_flow_num> packets. 901 */ 902 903 nb_mbuf = RTE_MAX(max_flow_num, 2UL * MAX_PKT_BURST) * MAX_FRAG_NUM; 904 nb_mbuf *= (port_conf.rxmode.max_rx_pkt_len + BUF_SIZE - 1) / BUF_SIZE; 905 nb_mbuf *= 2; /* ipv4 and ipv6 */ 906 nb_mbuf += nb_rxd + nb_txd; 907 908 nb_mbuf = RTE_MAX(nb_mbuf, (uint32_t)NB_MBUF); 909 910 snprintf(buf, sizeof(buf), "mbuf_pool_%u_%u", lcore, queue); 911 912 if ((rxq->pool = rte_mempool_create(buf, nb_mbuf, MBUF_SIZE, 0, 913 sizeof(struct rte_pktmbuf_pool_private), 914 rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, NULL, 915 socket, MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET)) == NULL) { 916 RTE_LOG(ERR, IP_RSMBL, "mempool_create(%s) failed", buf); 917 return -1; 918 } 919 920 return 0; 921 } 922 923 static int 924 init_mem(void) 925 { 926 char buf[PATH_MAX]; 927 struct rte_lpm *lpm; 928 struct rte_lpm6 *lpm6; 929 struct rte_lpm_config lpm_config; 930 int socket; 931 unsigned lcore_id; 932 933 /* traverse through lcores and initialize structures on each socket */ 934 935 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 936 937 if (rte_lcore_is_enabled(lcore_id) == 0) 938 continue; 939 940 socket = rte_lcore_to_socket_id(lcore_id); 941 942 if (socket == SOCKET_ID_ANY) 943 socket = 0; 944 945 if (socket_lpm[socket] == NULL) { 946 RTE_LOG(INFO, IP_RSMBL, "Creating LPM table on socket %i\n", socket); 947 snprintf(buf, sizeof(buf), "IP_RSMBL_LPM_%i", socket); 948 949 lpm_config.max_rules = LPM_MAX_RULES; 950 lpm_config.number_tbl8s = 256; 951 lpm_config.flags = 0; 952 953 lpm = rte_lpm_create(buf, socket, &lpm_config); 954 if (lpm == NULL) { 955 RTE_LOG(ERR, IP_RSMBL, "Cannot create LPM table\n"); 956 return -1; 957 } 958 socket_lpm[socket] = lpm; 959 } 960 961 if (socket_lpm6[socket] == NULL) { 962 RTE_LOG(INFO, IP_RSMBL, "Creating LPM6 table on socket %i\n", socket); 963 snprintf(buf, sizeof(buf), "IP_RSMBL_LPM_%i", socket); 964 965 lpm6 = rte_lpm6_create(buf, socket, &lpm6_config); 966 if (lpm6 == NULL) { 967 RTE_LOG(ERR, IP_RSMBL, "Cannot create LPM table\n"); 968 return -1; 969 } 970 socket_lpm6[socket] = lpm6; 971 } 972 } 973 974 return 0; 975 } 976 977 static void 978 queue_dump_stat(void) 979 { 980 uint32_t i, lcore; 981 const struct lcore_queue_conf *qconf; 982 983 for (lcore = 0; lcore < RTE_MAX_LCORE; lcore++) { 984 if (rte_lcore_is_enabled(lcore) == 0) 985 continue; 986 987 qconf = &lcore_queue_conf[lcore]; 988 for (i = 0; i < qconf->n_rx_queue; i++) { 989 990 fprintf(stdout, " -- lcoreid=%u portid=%u " 991 "frag tbl stat:\n", 992 lcore, qconf->rx_queue_list[i].portid); 993 rte_ip_frag_table_statistics_dump(stdout, 994 qconf->rx_queue_list[i].frag_tbl); 995 fprintf(stdout, "TX bursts:\t%" PRIu64 "\n" 996 "TX packets _queued:\t%" PRIu64 "\n" 997 "TX packets dropped:\t%" PRIu64 "\n" 998 "TX packets send:\t%" PRIu64 "\n", 999 qconf->tx_stat.call, 1000 qconf->tx_stat.queue, 1001 qconf->tx_stat.drop, 1002 qconf->tx_stat.send); 1003 } 1004 } 1005 } 1006 1007 static void 1008 signal_handler(int signum) 1009 { 1010 queue_dump_stat(); 1011 if (signum != SIGUSR1) 1012 rte_exit(0, "received signal: %d, exiting\n", signum); 1013 } 1014 1015 int 1016 main(int argc, char **argv) 1017 { 1018 struct lcore_queue_conf *qconf; 1019 struct rte_eth_dev_info dev_info; 1020 struct rte_eth_txconf *txconf; 1021 struct rx_queue *rxq; 1022 int ret, socket; 1023 unsigned nb_ports; 1024 uint16_t queueid; 1025 unsigned lcore_id = 0, rx_lcore_id = 0; 1026 uint32_t n_tx_queue, nb_lcores; 1027 uint16_t portid; 1028 1029 /* init EAL */ 1030 ret = rte_eal_init(argc, argv); 1031 if (ret < 0) 1032 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n"); 1033 argc -= ret; 1034 argv += ret; 1035 1036 /* parse application arguments (after the EAL ones) */ 1037 ret = parse_args(argc, argv); 1038 if (ret < 0) 1039 rte_exit(EXIT_FAILURE, "Invalid IP reassembly parameters\n"); 1040 1041 nb_ports = rte_eth_dev_count(); 1042 if (nb_ports == 0) 1043 rte_exit(EXIT_FAILURE, "No ports found!\n"); 1044 1045 nb_lcores = rte_lcore_count(); 1046 1047 /* initialize structures (mempools, lpm etc.) */ 1048 if (init_mem() < 0) 1049 rte_panic("Cannot initialize memory structures!\n"); 1050 1051 /* check if portmask has non-existent ports */ 1052 if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned))) 1053 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n"); 1054 1055 /* initialize all ports */ 1056 for (portid = 0; portid < nb_ports; portid++) { 1057 /* skip ports that are not enabled */ 1058 if ((enabled_port_mask & (1 << portid)) == 0) { 1059 printf("\nSkipping disabled port %d\n", portid); 1060 continue; 1061 } 1062 1063 qconf = &lcore_queue_conf[rx_lcore_id]; 1064 1065 /* limit the frame size to the maximum supported by NIC */ 1066 rte_eth_dev_info_get(portid, &dev_info); 1067 port_conf.rxmode.max_rx_pkt_len = RTE_MIN( 1068 dev_info.max_rx_pktlen, port_conf.rxmode.max_rx_pkt_len); 1069 1070 /* get the lcore_id for this port */ 1071 while (rte_lcore_is_enabled(rx_lcore_id) == 0 || 1072 qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) { 1073 1074 rx_lcore_id++; 1075 if (rx_lcore_id >= RTE_MAX_LCORE) 1076 rte_exit(EXIT_FAILURE, "Not enough cores\n"); 1077 1078 qconf = &lcore_queue_conf[rx_lcore_id]; 1079 } 1080 1081 socket = rte_lcore_to_socket_id(portid); 1082 if (socket == SOCKET_ID_ANY) 1083 socket = 0; 1084 1085 queueid = qconf->n_rx_queue; 1086 rxq = &qconf->rx_queue_list[queueid]; 1087 rxq->portid = portid; 1088 rxq->lpm = socket_lpm[socket]; 1089 rxq->lpm6 = socket_lpm6[socket]; 1090 1091 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, 1092 &nb_txd); 1093 if (ret < 0) 1094 rte_exit(EXIT_FAILURE, 1095 "Cannot adjust number of descriptors: err=%d, port=%d\n", 1096 ret, portid); 1097 1098 if (setup_queue_tbl(rxq, rx_lcore_id, queueid) < 0) 1099 rte_exit(EXIT_FAILURE, "Failed to set up queue table\n"); 1100 qconf->n_rx_queue++; 1101 1102 /* init port */ 1103 printf("Initializing port %d ... ", portid ); 1104 fflush(stdout); 1105 1106 n_tx_queue = nb_lcores; 1107 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT) 1108 n_tx_queue = MAX_TX_QUEUE_PER_PORT; 1109 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue, 1110 &port_conf); 1111 if (ret < 0) { 1112 printf("\n"); 1113 rte_exit(EXIT_FAILURE, "Cannot configure device: " 1114 "err=%d, port=%d\n", 1115 ret, portid); 1116 } 1117 1118 /* init one RX queue */ 1119 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd, 1120 socket, NULL, 1121 rxq->pool); 1122 if (ret < 0) { 1123 printf("\n"); 1124 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: " 1125 "err=%d, port=%d\n", 1126 ret, portid); 1127 } 1128 1129 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]); 1130 print_ethaddr(" Address:", &ports_eth_addr[portid]); 1131 printf("\n"); 1132 1133 /* init one TX queue per couple (lcore,port) */ 1134 queueid = 0; 1135 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1136 if (rte_lcore_is_enabled(lcore_id) == 0) 1137 continue; 1138 1139 socket = (int) rte_lcore_to_socket_id(lcore_id); 1140 1141 printf("txq=%u,%d,%d ", lcore_id, queueid, socket); 1142 fflush(stdout); 1143 1144 txconf = &dev_info.default_txconf; 1145 txconf->txq_flags = 0; 1146 1147 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd, 1148 socket, txconf); 1149 if (ret < 0) 1150 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, " 1151 "port=%d\n", ret, portid); 1152 1153 qconf = &lcore_queue_conf[lcore_id]; 1154 qconf->tx_queue_id[portid] = queueid; 1155 setup_port_tbl(qconf, lcore_id, socket, portid); 1156 queueid++; 1157 } 1158 printf("\n"); 1159 } 1160 1161 printf("\n"); 1162 1163 /* start ports */ 1164 for (portid = 0; portid < nb_ports; portid++) { 1165 if ((enabled_port_mask & (1 << portid)) == 0) { 1166 continue; 1167 } 1168 /* Start device */ 1169 ret = rte_eth_dev_start(portid); 1170 if (ret < 0) 1171 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n", 1172 ret, portid); 1173 1174 rte_eth_promiscuous_enable(portid); 1175 } 1176 1177 if (init_routing_table() < 0) 1178 rte_exit(EXIT_FAILURE, "Cannot init routing table\n"); 1179 1180 check_all_ports_link_status(nb_ports, enabled_port_mask); 1181 1182 signal(SIGUSR1, signal_handler); 1183 signal(SIGTERM, signal_handler); 1184 signal(SIGINT, signal_handler); 1185 1186 /* launch per-lcore init on every lcore */ 1187 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER); 1188 RTE_LCORE_FOREACH_SLAVE(lcore_id) { 1189 if (rte_eal_wait_lcore(lcore_id) < 0) 1190 return -1; 1191 } 1192 1193 return 0; 1194 } 1195