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