1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2016 Intel Corporation 3 */ 4 5 #include <locale.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <stdint.h> 9 #include <ctype.h> 10 #include <getopt.h> 11 12 #include <rte_common.h> 13 #include <rte_log.h> 14 #include <rte_malloc.h> 15 #include <rte_memory.h> 16 #include <rte_memcpy.h> 17 #include <rte_eal.h> 18 #include <rte_launch.h> 19 #include <rte_cycles.h> 20 #include <rte_prefetch.h> 21 #include <rte_lcore.h> 22 #include <rte_per_lcore.h> 23 #include <rte_branch_prediction.h> 24 #include <rte_interrupts.h> 25 #include <rte_debug.h> 26 #include <rte_ether.h> 27 #include <rte_ethdev.h> 28 #include <rte_mempool.h> 29 #include <rte_mbuf.h> 30 #include <rte_spinlock.h> 31 32 #include <rte_errno.h> 33 #include <rte_jobstats.h> 34 #include <rte_timer.h> 35 #include <rte_alarm.h> 36 #include <rte_pause.h> 37 38 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1 39 40 #define NB_MBUF 8192 41 42 #define MAX_PKT_BURST 32 43 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 44 45 /* 46 * Configurable number of RX/TX ring descriptors 47 */ 48 #define RX_DESC_DEFAULT 1024 49 #define TX_DESC_DEFAULT 1024 50 static uint16_t nb_rxd = RX_DESC_DEFAULT; 51 static uint16_t nb_txd = TX_DESC_DEFAULT; 52 53 /* ethernet addresses of ports */ 54 static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS]; 55 56 /* mask of enabled ports */ 57 static uint32_t l2fwd_enabled_port_mask; 58 59 /* list of enabled ports */ 60 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS]; 61 62 #define UPDATE_STEP_UP 1 63 #define UPDATE_STEP_DOWN 32 64 65 static unsigned int l2fwd_rx_queue_per_lcore = 1; 66 67 #define MAX_RX_QUEUE_PER_LCORE 16 68 #define MAX_TX_QUEUE_PER_PORT 16 69 /* List of queues to be polled for given lcore. 8< */ 70 struct __rte_cache_aligned lcore_queue_conf { 71 unsigned n_rx_port; 72 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE]; 73 uint64_t next_flush_time[RTE_MAX_ETHPORTS]; 74 75 struct rte_timer rx_timers[MAX_RX_QUEUE_PER_LCORE]; 76 struct rte_jobstats port_fwd_jobs[MAX_RX_QUEUE_PER_LCORE]; 77 78 struct rte_timer flush_timer; 79 struct rte_jobstats flush_job; 80 struct rte_jobstats idle_job; 81 struct rte_jobstats_context jobs_context; 82 83 RTE_ATOMIC(uint16_t) stats_read_pending; 84 rte_spinlock_t lock; 85 }; 86 /* >8 End of list of queues to be polled for given lcore. */ 87 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; 88 89 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS]; 90 91 static struct rte_eth_conf port_conf = { 92 .txmode = { 93 .mq_mode = RTE_ETH_MQ_TX_NONE, 94 }, 95 }; 96 97 struct rte_mempool *l2fwd_pktmbuf_pool = NULL; 98 99 /* Per-port statistics struct */ 100 struct __rte_cache_aligned l2fwd_port_statistics { 101 uint64_t tx; 102 uint64_t rx; 103 uint64_t dropped; 104 }; 105 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS]; 106 107 /* 1 day max */ 108 #define MAX_TIMER_PERIOD 86400 109 /* default period is 10 seconds */ 110 static int64_t timer_period = 10; 111 /* default timer frequency */ 112 static double hz; 113 /* BURST_TX_DRAIN_US converted to cycles */ 114 uint64_t drain_tsc; 115 /* Convert cycles to ns */ 116 static inline double 117 cycles_to_ns(uint64_t cycles) 118 { 119 double t = cycles; 120 121 t *= (double)NS_PER_S; 122 t /= hz; 123 return t; 124 } 125 126 static void 127 show_lcore_stats(unsigned lcore_id) 128 { 129 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id]; 130 struct rte_jobstats_context *ctx = &qconf->jobs_context; 131 struct rte_jobstats *job; 132 uint8_t i; 133 134 /* LCore statistics. */ 135 uint64_t stats_period, loop_count; 136 uint64_t exec, exec_min, exec_max; 137 uint64_t management, management_min, management_max; 138 uint64_t busy, busy_min, busy_max; 139 140 /* Jobs statistics. */ 141 const uint16_t port_cnt = qconf->n_rx_port; 142 uint64_t jobs_exec_cnt[port_cnt], jobs_period[port_cnt]; 143 uint64_t jobs_exec[port_cnt], jobs_exec_min[port_cnt], 144 jobs_exec_max[port_cnt]; 145 146 uint64_t flush_exec_cnt, flush_period; 147 uint64_t flush_exec, flush_exec_min, flush_exec_max; 148 149 uint64_t idle_exec_cnt; 150 uint64_t idle_exec, idle_exec_min, idle_exec_max; 151 uint64_t collection_time = rte_get_timer_cycles(); 152 153 /* Ask forwarding thread to give us stats. */ 154 rte_atomic_store_explicit(&qconf->stats_read_pending, 1, rte_memory_order_relaxed); 155 rte_spinlock_lock(&qconf->lock); 156 rte_atomic_store_explicit(&qconf->stats_read_pending, 0, rte_memory_order_relaxed); 157 158 /* Collect context statistics. */ 159 stats_period = ctx->state_time - ctx->start_time; 160 loop_count = ctx->loop_cnt; 161 162 exec = ctx->exec_time; 163 exec_min = ctx->min_exec_time; 164 exec_max = ctx->max_exec_time; 165 166 management = ctx->management_time; 167 management_min = ctx->min_management_time; 168 management_max = ctx->max_management_time; 169 170 rte_jobstats_context_reset(ctx); 171 172 for (i = 0; i < port_cnt; i++) { 173 job = &qconf->port_fwd_jobs[i]; 174 175 jobs_exec_cnt[i] = job->exec_cnt; 176 jobs_period[i] = job->period; 177 178 jobs_exec[i] = job->exec_time; 179 jobs_exec_min[i] = job->min_exec_time; 180 jobs_exec_max[i] = job->max_exec_time; 181 182 rte_jobstats_reset(job); 183 } 184 185 flush_exec_cnt = qconf->flush_job.exec_cnt; 186 flush_period = qconf->flush_job.period; 187 flush_exec = qconf->flush_job.exec_time; 188 flush_exec_min = qconf->flush_job.min_exec_time; 189 flush_exec_max = qconf->flush_job.max_exec_time; 190 rte_jobstats_reset(&qconf->flush_job); 191 192 idle_exec_cnt = qconf->idle_job.exec_cnt; 193 idle_exec = qconf->idle_job.exec_time; 194 idle_exec_min = qconf->idle_job.min_exec_time; 195 idle_exec_max = qconf->idle_job.max_exec_time; 196 rte_jobstats_reset(&qconf->idle_job); 197 198 rte_spinlock_unlock(&qconf->lock); 199 200 exec -= idle_exec; 201 busy = exec + management; 202 busy_min = exec_min + management_min; 203 busy_max = exec_max + management_max; 204 205 206 collection_time = rte_get_timer_cycles() - collection_time; 207 208 #define STAT_FMT "\n%-18s %'14.0f %6.1f%% %'10.0f %'10.0f %'10.0f" 209 210 printf("\n----------------" 211 "\nLCore %3u: statistics (time in ns, collected in %'9.0f)" 212 "\n%-18s %14s %7s %10s %10s %10s " 213 "\n%-18s %'14.0f" 214 "\n%-18s %'14" PRIu64 215 STAT_FMT /* Exec */ 216 STAT_FMT /* Management */ 217 STAT_FMT /* Busy */ 218 STAT_FMT, /* Idle */ 219 lcore_id, cycles_to_ns(collection_time), 220 "Stat type", "total", "%total", "avg", "min", "max", 221 "Stats duration:", cycles_to_ns(stats_period), 222 "Loop count:", loop_count, 223 "Exec time", 224 cycles_to_ns(exec), exec * 100.0 / stats_period, 225 cycles_to_ns(loop_count ? exec / loop_count : 0), 226 cycles_to_ns(exec_min), 227 cycles_to_ns(exec_max), 228 "Management time", 229 cycles_to_ns(management), management * 100.0 / stats_period, 230 cycles_to_ns(loop_count ? management / loop_count : 0), 231 cycles_to_ns(management_min), 232 cycles_to_ns(management_max), 233 "Exec + management", 234 cycles_to_ns(busy), busy * 100.0 / stats_period, 235 cycles_to_ns(loop_count ? busy / loop_count : 0), 236 cycles_to_ns(busy_min), 237 cycles_to_ns(busy_max), 238 "Idle (job)", 239 cycles_to_ns(idle_exec), idle_exec * 100.0 / stats_period, 240 cycles_to_ns(idle_exec_cnt ? idle_exec / idle_exec_cnt : 0), 241 cycles_to_ns(idle_exec_min), 242 cycles_to_ns(idle_exec_max)); 243 244 for (i = 0; i < qconf->n_rx_port; i++) { 245 job = &qconf->port_fwd_jobs[i]; 246 printf("\n\nJob %" PRIu32 ": %-20s " 247 "\n%-18s %'14" PRIu64 248 "\n%-18s %'14.0f" 249 STAT_FMT, 250 i, job->name, 251 "Exec count:", jobs_exec_cnt[i], 252 "Exec period: ", cycles_to_ns(jobs_period[i]), 253 "Exec time", 254 cycles_to_ns(jobs_exec[i]), jobs_exec[i] * 100.0 / stats_period, 255 cycles_to_ns(jobs_exec_cnt[i] ? jobs_exec[i] / jobs_exec_cnt[i] 256 : 0), 257 cycles_to_ns(jobs_exec_min[i]), 258 cycles_to_ns(jobs_exec_max[i])); 259 } 260 261 if (qconf->n_rx_port > 0) { 262 job = &qconf->flush_job; 263 printf("\n\nJob %" PRIu32 ": %-20s " 264 "\n%-18s %'14" PRIu64 265 "\n%-18s %'14.0f" 266 STAT_FMT, 267 i, job->name, 268 "Exec count:", flush_exec_cnt, 269 "Exec period: ", cycles_to_ns(flush_period), 270 "Exec time", 271 cycles_to_ns(flush_exec), flush_exec * 100.0 / stats_period, 272 cycles_to_ns(flush_exec_cnt ? flush_exec / flush_exec_cnt : 0), 273 cycles_to_ns(flush_exec_min), 274 cycles_to_ns(flush_exec_max)); 275 } 276 } 277 278 /* Print out statistics on packets dropped */ 279 static void 280 show_stats_cb(__rte_unused void *param) 281 { 282 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx; 283 unsigned portid, lcore_id; 284 285 total_packets_dropped = 0; 286 total_packets_tx = 0; 287 total_packets_rx = 0; 288 289 const char clr[] = { 27, '[', '2', 'J', '\0' }; 290 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' }; 291 292 /* Clear screen and move to top left */ 293 printf("%s%s" 294 "\nPort statistics ===================================", 295 clr, topLeft); 296 297 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 298 /* skip disabled ports */ 299 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) 300 continue; 301 printf("\nStatistics for port %u ------------------------------" 302 "\nPackets sent: %24"PRIu64 303 "\nPackets received: %20"PRIu64 304 "\nPackets dropped: %21"PRIu64, 305 portid, 306 port_statistics[portid].tx, 307 port_statistics[portid].rx, 308 port_statistics[portid].dropped); 309 310 total_packets_dropped += port_statistics[portid].dropped; 311 total_packets_tx += port_statistics[portid].tx; 312 total_packets_rx += port_statistics[portid].rx; 313 } 314 315 printf("\nAggregate statistics ===============================" 316 "\nTotal packets sent: %18"PRIu64 317 "\nTotal packets received: %14"PRIu64 318 "\nTotal packets dropped: %15"PRIu64 319 "\n====================================================", 320 total_packets_tx, 321 total_packets_rx, 322 total_packets_dropped); 323 324 RTE_LCORE_FOREACH(lcore_id) { 325 if (lcore_queue_conf[lcore_id].n_rx_port > 0) 326 show_lcore_stats(lcore_id); 327 } 328 329 printf("\n====================================================\n"); 330 331 fflush(stdout); 332 333 rte_eal_alarm_set(timer_period * US_PER_S, show_stats_cb, NULL); 334 } 335 336 /* Start of l2fwd_simple_forward. 8< */ 337 static void 338 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid) 339 { 340 struct rte_ether_hdr *eth; 341 void *tmp; 342 int sent; 343 unsigned dst_port; 344 struct rte_eth_dev_tx_buffer *buffer; 345 346 dst_port = l2fwd_dst_ports[portid]; 347 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); 348 349 /* 02:00:00:00:00:xx */ 350 tmp = ð->dst_addr.addr_bytes[0]; 351 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40); 352 353 /* src addr */ 354 rte_ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->src_addr); 355 356 buffer = tx_buffer[dst_port]; 357 sent = rte_eth_tx_buffer(dst_port, 0, buffer, m); 358 if (sent) 359 port_statistics[dst_port].tx += sent; 360 } 361 /* >8 End of l2fwd_simple_forward. */ 362 363 static void 364 l2fwd_job_update_cb(struct rte_jobstats *job, int64_t result) 365 { 366 int64_t err = job->target - result; 367 int64_t histeresis = job->target / 8; 368 369 if (err < -histeresis) { 370 if (job->min_period + UPDATE_STEP_DOWN < job->period) 371 job->period -= UPDATE_STEP_DOWN; 372 } else if (err > histeresis) { 373 if (job->period + UPDATE_STEP_UP < job->max_period) 374 job->period += UPDATE_STEP_UP; 375 } 376 } 377 378 static void 379 l2fwd_fwd_job(__rte_unused struct rte_timer *timer, void *arg) 380 { 381 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 382 struct rte_mbuf *m; 383 384 const uint16_t port_idx = (uintptr_t) arg; 385 const unsigned lcore_id = rte_lcore_id(); 386 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id]; 387 struct rte_jobstats *job = &qconf->port_fwd_jobs[port_idx]; 388 const uint16_t portid = qconf->rx_port_list[port_idx]; 389 390 uint8_t j; 391 uint16_t total_nb_rx; 392 393 rte_jobstats_start(&qconf->jobs_context, job); 394 395 /* Call rx burst 2 times. This allow rte_jobstats logic to see if this 396 * function must be called more frequently. */ 397 398 /* Call rx burst 2 times. 8< */ 399 total_nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst, 400 MAX_PKT_BURST); 401 402 for (j = 0; j < total_nb_rx; j++) { 403 m = pkts_burst[j]; 404 rte_prefetch0(rte_pktmbuf_mtod(m, void *)); 405 l2fwd_simple_forward(m, portid); 406 } 407 /* >8 End of call rx burst 2 times. */ 408 409 /* Read second try. 8< */ 410 if (total_nb_rx == MAX_PKT_BURST) { 411 const uint16_t nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst, 412 MAX_PKT_BURST); 413 414 total_nb_rx += nb_rx; 415 for (j = 0; j < nb_rx; j++) { 416 m = pkts_burst[j]; 417 rte_prefetch0(rte_pktmbuf_mtod(m, void *)); 418 l2fwd_simple_forward(m, portid); 419 } 420 } 421 /* >8 End of read second try. */ 422 423 port_statistics[portid].rx += total_nb_rx; 424 425 /* Adjust period time in which we are running here. 8< */ 426 if (rte_jobstats_finish(job, total_nb_rx) != 0) { 427 rte_timer_reset(&qconf->rx_timers[port_idx], job->period, PERIODICAL, 428 lcore_id, l2fwd_fwd_job, arg); 429 } 430 /* >8 End of adjust period time in which we are running. */ 431 } 432 433 /* Draining TX queue of each port. 8< */ 434 static void 435 l2fwd_flush_job(__rte_unused struct rte_timer *timer, __rte_unused void *arg) 436 { 437 uint64_t now; 438 unsigned lcore_id; 439 struct lcore_queue_conf *qconf; 440 uint16_t portid; 441 unsigned i; 442 uint32_t sent; 443 struct rte_eth_dev_tx_buffer *buffer; 444 445 lcore_id = rte_lcore_id(); 446 qconf = &lcore_queue_conf[lcore_id]; 447 448 rte_jobstats_start(&qconf->jobs_context, &qconf->flush_job); 449 450 now = rte_get_timer_cycles(); 451 lcore_id = rte_lcore_id(); 452 qconf = &lcore_queue_conf[lcore_id]; 453 454 for (i = 0; i < qconf->n_rx_port; i++) { 455 portid = l2fwd_dst_ports[qconf->rx_port_list[i]]; 456 457 if (qconf->next_flush_time[portid] <= now) 458 continue; 459 460 buffer = tx_buffer[portid]; 461 sent = rte_eth_tx_buffer_flush(portid, 0, buffer); 462 if (sent) 463 port_statistics[portid].tx += sent; 464 465 qconf->next_flush_time[portid] = rte_get_timer_cycles() + drain_tsc; 466 } 467 468 /* Pass target to indicate that this job is happy of time interval 469 * in which it was called. */ 470 rte_jobstats_finish(&qconf->flush_job, qconf->flush_job.target); 471 } 472 /* >8 End of draining TX queue of each port. */ 473 474 /* main processing loop */ 475 static void 476 l2fwd_main_loop(void) 477 { 478 unsigned lcore_id; 479 unsigned i, portid; 480 struct lcore_queue_conf *qconf; 481 uint8_t stats_read_pending = 0; 482 uint8_t need_manage; 483 484 lcore_id = rte_lcore_id(); 485 qconf = &lcore_queue_conf[lcore_id]; 486 487 if (qconf->n_rx_port == 0) { 488 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id); 489 return; 490 } 491 492 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id); 493 494 for (i = 0; i < qconf->n_rx_port; i++) { 495 496 portid = qconf->rx_port_list[i]; 497 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id, 498 portid); 499 } 500 501 rte_jobstats_init(&qconf->idle_job, "idle", 0, 0, 0, 0); 502 503 /* Minimize impact of stats reading. 8< */ 504 for (;;) { 505 rte_spinlock_lock(&qconf->lock); 506 507 do { 508 rte_jobstats_context_start(&qconf->jobs_context); 509 510 /* Do the Idle job: 511 * - Read stats_read_pending flag 512 * - check if some real job need to be executed 513 */ 514 rte_jobstats_start(&qconf->jobs_context, &qconf->idle_job); 515 516 uint64_t repeats = 0; 517 518 do { 519 uint8_t i; 520 uint64_t now = rte_get_timer_cycles(); 521 522 repeats++; 523 need_manage = qconf->flush_timer.expire < now; 524 /* Check if we was esked to give a stats. */ 525 stats_read_pending = rte_atomic_load_explicit( 526 &qconf->stats_read_pending, 527 rte_memory_order_relaxed); 528 need_manage |= stats_read_pending; 529 530 for (i = 0; i < qconf->n_rx_port && !need_manage; i++) 531 need_manage = qconf->rx_timers[i].expire < now; 532 533 } while (!need_manage); 534 535 if (likely(repeats != 1)) 536 rte_jobstats_finish(&qconf->idle_job, qconf->idle_job.target); 537 else 538 rte_jobstats_abort(&qconf->idle_job); 539 540 rte_timer_manage(); 541 rte_jobstats_context_finish(&qconf->jobs_context); 542 } while (likely(stats_read_pending == 0)); 543 544 rte_spinlock_unlock(&qconf->lock); 545 rte_pause(); 546 } 547 /* >8 End of minimize impact of stats reading. */ 548 } 549 550 static int 551 l2fwd_launch_one_lcore(__rte_unused void *dummy) 552 { 553 l2fwd_main_loop(); 554 return 0; 555 } 556 557 /* display usage */ 558 static void 559 l2fwd_usage(const char *prgname) 560 { 561 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n" 562 " -p PORTMASK: hexadecimal bitmask of ports to configure\n" 563 " -q NQ: number of queue (=ports) per lcore (default is 1)\n" 564 " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n" 565 " -l set system default locale instead of default (\"C\" locale) for thousands separator in stats.", 566 prgname); 567 } 568 569 static int 570 l2fwd_parse_portmask(const char *portmask) 571 { 572 char *end = NULL; 573 unsigned long pm; 574 575 /* parse hexadecimal string */ 576 pm = strtoul(portmask, &end, 16); 577 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 578 return 0; 579 580 return pm; 581 } 582 583 static unsigned int 584 l2fwd_parse_nqueue(const char *q_arg) 585 { 586 char *end = NULL; 587 unsigned long n; 588 589 /* parse hexadecimal string */ 590 n = strtoul(q_arg, &end, 10); 591 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 592 return 0; 593 if (n == 0) 594 return 0; 595 if (n >= MAX_RX_QUEUE_PER_LCORE) 596 return 0; 597 598 return n; 599 } 600 601 static int 602 l2fwd_parse_timer_period(const char *q_arg) 603 { 604 char *end = NULL; 605 int n; 606 607 /* parse number string */ 608 n = strtol(q_arg, &end, 10); 609 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 610 return -1; 611 if (n >= MAX_TIMER_PERIOD) 612 return -1; 613 614 return n; 615 } 616 617 /* Parse the argument given in the command line of the application */ 618 static int 619 l2fwd_parse_args(int argc, char **argv) 620 { 621 int opt, ret; 622 char **argvopt; 623 int option_index; 624 char *prgname = argv[0]; 625 static struct option lgopts[] = { 626 {NULL, 0, 0, 0} 627 }; 628 629 argvopt = argv; 630 631 while ((opt = getopt_long(argc, argvopt, "p:q:T:l", 632 lgopts, &option_index)) != EOF) { 633 634 switch (opt) { 635 /* portmask */ 636 case 'p': 637 l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg); 638 if (l2fwd_enabled_port_mask == 0) { 639 printf("invalid portmask\n"); 640 l2fwd_usage(prgname); 641 return -1; 642 } 643 break; 644 645 /* nqueue */ 646 case 'q': 647 l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg); 648 if (l2fwd_rx_queue_per_lcore == 0) { 649 printf("invalid queue number\n"); 650 l2fwd_usage(prgname); 651 return -1; 652 } 653 break; 654 655 /* timer period */ 656 case 'T': 657 timer_period = l2fwd_parse_timer_period(optarg); 658 if (timer_period < 0) { 659 printf("invalid timer period\n"); 660 l2fwd_usage(prgname); 661 return -1; 662 } 663 break; 664 665 /* For thousands separator in printf. */ 666 case 'l': 667 setlocale(LC_ALL, ""); 668 break; 669 670 /* long options */ 671 case 0: 672 l2fwd_usage(prgname); 673 return -1; 674 675 default: 676 l2fwd_usage(prgname); 677 return -1; 678 } 679 } 680 681 if (optind >= 0) 682 argv[optind-1] = prgname; 683 684 ret = optind-1; 685 optind = 1; /* reset getopt lib */ 686 return ret; 687 } 688 689 /* Check the link status of all ports in up to 9s, and print them finally */ 690 static void 691 check_all_ports_link_status(uint32_t port_mask) 692 { 693 #define CHECK_INTERVAL 100 /* 100ms */ 694 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 695 uint16_t portid; 696 uint8_t count, all_ports_up, print_flag = 0; 697 struct rte_eth_link link; 698 int ret; 699 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN]; 700 701 printf("\nChecking link status"); 702 fflush(stdout); 703 for (count = 0; count <= MAX_CHECK_TIME; count++) { 704 all_ports_up = 1; 705 RTE_ETH_FOREACH_DEV(portid) { 706 if ((port_mask & (1 << portid)) == 0) 707 continue; 708 memset(&link, 0, sizeof(link)); 709 ret = rte_eth_link_get_nowait(portid, &link); 710 if (ret < 0) { 711 all_ports_up = 0; 712 if (print_flag == 1) 713 printf("Port %u link get failed: %s\n", 714 portid, rte_strerror(-ret)); 715 continue; 716 } 717 /* print link status if flag set */ 718 if (print_flag == 1) { 719 rte_eth_link_to_str(link_status_text, 720 sizeof(link_status_text), &link); 721 printf("Port %d %s\n", portid, 722 link_status_text); 723 continue; 724 } 725 /* clear all_ports_up flag if any link down */ 726 if (link.link_status == RTE_ETH_LINK_DOWN) { 727 all_ports_up = 0; 728 break; 729 } 730 } 731 /* after finally printing all link status, get out */ 732 if (print_flag == 1) 733 break; 734 735 if (all_ports_up == 0) { 736 printf("."); 737 fflush(stdout); 738 rte_delay_ms(CHECK_INTERVAL); 739 } 740 741 /* set the print_flag if all ports up or timeout */ 742 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 743 print_flag = 1; 744 printf("done\n"); 745 } 746 } 747 } 748 749 int 750 main(int argc, char **argv) 751 { 752 struct lcore_queue_conf *qconf; 753 unsigned lcore_id, rx_lcore_id; 754 unsigned nb_ports_in_mask = 0; 755 int ret; 756 char name[RTE_JOBSTATS_NAMESIZE]; 757 uint16_t nb_ports; 758 uint16_t nb_ports_available = 0; 759 uint16_t portid, last_port; 760 uint8_t i; 761 762 /* Init EAL. 8< */ 763 ret = rte_eal_init(argc, argv); 764 if (ret < 0) 765 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n"); 766 argc -= ret; 767 argv += ret; 768 769 /* parse application arguments (after the EAL ones) */ 770 ret = l2fwd_parse_args(argc, argv); 771 if (ret < 0) 772 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n"); 773 /* >8 End of init EAL. */ 774 775 rte_timer_subsystem_init(); 776 777 /* fetch default timer frequency. */ 778 hz = rte_get_timer_hz(); 779 780 /* Create the mbuf pool. 8< */ 781 l2fwd_pktmbuf_pool = 782 rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32, 783 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 784 if (l2fwd_pktmbuf_pool == NULL) 785 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n"); 786 /* >8 End of creation of mbuf pool. */ 787 nb_ports = rte_eth_dev_count_avail(); 788 if (nb_ports == 0) 789 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n"); 790 791 /* Reset l2fwd_dst_ports. 8< */ 792 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) 793 l2fwd_dst_ports[portid] = 0; 794 last_port = 0; 795 796 /* 797 * Each logical core is assigned a dedicated TX queue on each port. 798 */ 799 RTE_ETH_FOREACH_DEV(portid) { 800 /* skip ports that are not enabled */ 801 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) 802 continue; 803 804 if (nb_ports_in_mask % 2) { 805 l2fwd_dst_ports[portid] = last_port; 806 l2fwd_dst_ports[last_port] = portid; 807 } else 808 last_port = portid; 809 810 nb_ports_in_mask++; 811 } 812 /* >8 End of reset l2fwd_dst_ports. */ 813 if (nb_ports_in_mask % 2) { 814 printf("Notice: odd number of ports in portmask.\n"); 815 l2fwd_dst_ports[last_port] = last_port; 816 } 817 818 rx_lcore_id = 0; 819 qconf = NULL; 820 821 /* Initialize the port/queue configuration of each logical core */ 822 RTE_ETH_FOREACH_DEV(portid) { 823 /* skip ports that are not enabled */ 824 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) 825 continue; 826 827 /* get the lcore_id for this port */ 828 while (rte_lcore_is_enabled(rx_lcore_id) == 0 || 829 lcore_queue_conf[rx_lcore_id].n_rx_port == 830 l2fwd_rx_queue_per_lcore) { 831 rx_lcore_id++; 832 if (rx_lcore_id >= RTE_MAX_LCORE) 833 rte_exit(EXIT_FAILURE, "Not enough cores\n"); 834 } 835 836 if (qconf != &lcore_queue_conf[rx_lcore_id]) 837 /* Assigned a new logical core in the loop above. */ 838 qconf = &lcore_queue_conf[rx_lcore_id]; 839 840 qconf->rx_port_list[qconf->n_rx_port] = portid; 841 qconf->n_rx_port++; 842 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid); 843 } 844 845 /* Initialise each port */ 846 RTE_ETH_FOREACH_DEV(portid) { 847 struct rte_eth_dev_info dev_info; 848 struct rte_eth_rxconf rxq_conf; 849 struct rte_eth_txconf txq_conf; 850 struct rte_eth_conf local_port_conf = port_conf; 851 852 /* skip ports that are not enabled */ 853 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) { 854 printf("Skipping disabled port %u\n", portid); 855 continue; 856 } 857 nb_ports_available++; 858 859 /* init port */ 860 printf("Initializing port %u... ", portid); 861 fflush(stdout); 862 863 ret = rte_eth_dev_info_get(portid, &dev_info); 864 if (ret != 0) 865 rte_exit(EXIT_FAILURE, 866 "Error during getting device (port %u) info: %s\n", 867 portid, strerror(-ret)); 868 869 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE) 870 local_port_conf.txmode.offloads |= 871 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE; 872 /* Configure the RX and TX queues. 8< */ 873 ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf); 874 if (ret < 0) 875 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n", 876 ret, portid); 877 /* >8 End of configuring the RX and TX queues. */ 878 879 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, 880 &nb_txd); 881 if (ret < 0) 882 rte_exit(EXIT_FAILURE, 883 "Cannot adjust number of descriptors: err=%d, port=%u\n", 884 ret, portid); 885 886 ret = rte_eth_macaddr_get(portid, 887 &l2fwd_ports_eth_addr[portid]); 888 if (ret < 0) 889 rte_exit(EXIT_FAILURE, 890 "Cannot get MAC address: err=%d, port=%u\n", 891 ret, portid); 892 893 /* init one RX queue */ 894 fflush(stdout); 895 rxq_conf = dev_info.default_rxconf; 896 rxq_conf.offloads = local_port_conf.rxmode.offloads; 897 /* RX queue initialization. 8< */ 898 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd, 899 rte_eth_dev_socket_id(portid), 900 &rxq_conf, 901 l2fwd_pktmbuf_pool); 902 if (ret < 0) 903 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n", 904 ret, portid); 905 /* >8 End of RX queue initialization. */ 906 907 /* Init one TX queue on each port. 8< */ 908 txq_conf = dev_info.default_txconf; 909 txq_conf.offloads = local_port_conf.txmode.offloads; 910 fflush(stdout); 911 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd, 912 rte_eth_dev_socket_id(portid), 913 &txq_conf); 914 if (ret < 0) 915 rte_exit(EXIT_FAILURE, 916 "rte_eth_tx_queue_setup:err=%d, port=%u\n", 917 ret, portid); 918 /* >8 End of init one TX queue on each port. */ 919 920 /* Initialize TX buffers */ 921 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer", 922 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0, 923 rte_eth_dev_socket_id(portid)); 924 if (tx_buffer[portid] == NULL) 925 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n", 926 portid); 927 928 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST); 929 930 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid], 931 rte_eth_tx_buffer_count_callback, 932 &port_statistics[portid].dropped); 933 if (ret < 0) 934 rte_exit(EXIT_FAILURE, 935 "Cannot set error callback for tx buffer on port %u\n", 936 portid); 937 938 /* Start device */ 939 ret = rte_eth_dev_start(portid); 940 if (ret < 0) 941 rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n", 942 ret, portid); 943 944 printf("done:\n"); 945 946 ret = rte_eth_promiscuous_enable(portid); 947 if (ret != 0) { 948 rte_exit(EXIT_FAILURE, 949 "rte_eth_promiscuous_enable:err=%s, port=%u\n", 950 rte_strerror(-ret), portid); 951 return ret; 952 953 } 954 955 printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n", 956 portid, 957 RTE_ETHER_ADDR_BYTES(&l2fwd_ports_eth_addr[portid])); 958 959 /* initialize port stats */ 960 memset(&port_statistics, 0, sizeof(port_statistics)); 961 } 962 963 if (!nb_ports_available) { 964 rte_exit(EXIT_FAILURE, 965 "All available ports are disabled. Please set portmask.\n"); 966 } 967 968 check_all_ports_link_status(l2fwd_enabled_port_mask); 969 970 drain_tsc = (hz + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US; 971 972 RTE_LCORE_FOREACH(lcore_id) { 973 qconf = &lcore_queue_conf[lcore_id]; 974 975 rte_spinlock_init(&qconf->lock); 976 977 if (rte_jobstats_context_init(&qconf->jobs_context) != 0) 978 rte_panic("Jobs stats context for core %u init failed\n", lcore_id); 979 980 if (qconf->n_rx_port == 0) { 981 RTE_LOG(INFO, L2FWD, 982 "lcore %u: no ports so no jobs stats context initialization\n", 983 lcore_id); 984 continue; 985 } 986 /* Add flush job. 8< */ 987 988 /* Set fixed period by setting min = max = initial period. Set target to 989 * zero as it is irrelevant for this job. 990 */ 991 rte_jobstats_init(&qconf->flush_job, "flush", drain_tsc, drain_tsc, 992 drain_tsc, 0); 993 994 rte_timer_init(&qconf->flush_timer); 995 ret = rte_timer_reset(&qconf->flush_timer, drain_tsc, PERIODICAL, 996 lcore_id, &l2fwd_flush_job, NULL); 997 998 if (ret < 0) { 999 rte_exit(1, "Failed to reset flush job timer for lcore %u: %s", 1000 lcore_id, rte_strerror(-ret)); 1001 } 1002 /* >8 End of add flush job. */ 1003 1004 for (i = 0; i < qconf->n_rx_port; i++) { 1005 struct rte_jobstats *job = &qconf->port_fwd_jobs[i]; 1006 1007 portid = qconf->rx_port_list[i]; 1008 printf("Setting forward job for port %u\n", portid); 1009 1010 snprintf(name, RTE_DIM(name), "port %u fwd", portid); 1011 /* Setup forward job. 8< */ 1012 1013 /* Set min, max and initial period. Set target to MAX_PKT_BURST as 1014 * this is desired optimal RX/TX burst size. 1015 */ 1016 rte_jobstats_init(job, name, 0, drain_tsc, 0, MAX_PKT_BURST); 1017 rte_jobstats_set_update_period_function(job, l2fwd_job_update_cb); 1018 1019 rte_timer_init(&qconf->rx_timers[i]); 1020 ret = rte_timer_reset(&qconf->rx_timers[i], 0, PERIODICAL, lcore_id, 1021 &l2fwd_fwd_job, (void *)(uintptr_t)i); 1022 1023 if (ret < 0) { 1024 rte_exit(1, "Failed to reset lcore %u port %u job timer: %s", 1025 lcore_id, qconf->rx_port_list[i], rte_strerror(-ret)); 1026 } 1027 /* >8 End of forward job. */ 1028 } 1029 } 1030 1031 if (timer_period) 1032 rte_eal_alarm_set(timer_period * MS_PER_S, show_stats_cb, NULL); 1033 else 1034 RTE_LOG(INFO, L2FWD, "Stats display disabled\n"); 1035 1036 /* launch per-lcore init on every lcore */ 1037 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MAIN); 1038 RTE_LCORE_FOREACH_WORKER(lcore_id) { 1039 if (rte_eal_wait_lcore(lcore_id) < 0) 1040 return -1; 1041 } 1042 1043 /* clean up the EAL */ 1044 rte_eal_cleanup(); 1045 1046 return 0; 1047 } 1048