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