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