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