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