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