examples/vhost: remove DMA type option help info
[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_cycles.h>
20 #include <rte_prefetch.h>
21 #include <rte_lcore.h>
22 #include <rte_per_lcore.h>
23 #include <rte_branch_prediction.h>
24 #include <rte_interrupts.h>
25 #include <rte_debug.h>
26 #include <rte_ether.h>
27 #include <rte_ethdev.h>
28 #include <rte_mempool.h>
29 #include <rte_mbuf.h>
30 #include <rte_spinlock.h>
31
32 #include <rte_errno.h>
33 #include <rte_jobstats.h>
34 #include <rte_timer.h>
35 #include <rte_alarm.h>
36 #include <rte_pause.h>
37
38 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
39
40 #define NB_MBUF   8192
41
42 #define MAX_PKT_BURST 32
43 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
44
45 /*
46  * Configurable number of RX/TX ring descriptors
47  */
48 #define RTE_TEST_RX_DESC_DEFAULT 1024
49 #define RTE_TEST_TX_DESC_DEFAULT 1024
50 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
51 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
52
53 /* ethernet addresses of ports */
54 static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
55
56 /* mask of enabled ports */
57 static uint32_t l2fwd_enabled_port_mask;
58
59 /* list of enabled ports */
60 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
61
62 #define UPDATE_STEP_UP 1
63 #define UPDATE_STEP_DOWN 32
64
65 static unsigned int l2fwd_rx_queue_per_lcore = 1;
66
67 #define MAX_RX_QUEUE_PER_LCORE 16
68 #define MAX_TX_QUEUE_PER_PORT 16
69 /* List of queues to be polled for given lcore. 8< */
70 struct 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         uint16_t stats_read_pending;
84         rte_spinlock_t lock;
85 } __rte_cache_aligned;
86 /* >8 End of list of queues to be polled for given lcore. */
87 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
88
89 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
90
91 static struct rte_eth_conf port_conf = {
92         .rxmode = {
93                 .split_hdr_size = 0,
94         },
95         .txmode = {
96                 .mq_mode = RTE_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         __atomic_store_n(&qconf->stats_read_pending, 1, __ATOMIC_RELAXED);
158         rte_spinlock_lock(&qconf->lock);
159         __atomic_store_n(&qconf->stats_read_pending, 0, __ATOMIC_RELAXED);
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
334         fflush(stdout);
335
336         rte_eal_alarm_set(timer_period * US_PER_S, show_stats_cb, NULL);
337 }
338
339 /* Start of l2fwd_simple_forward. 8< */
340 static void
341 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
342 {
343         struct rte_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 rte_ether_hdr *);
351
352         /* 02:00:00:00:00:xx */
353         tmp = &eth->dst_addr.addr_bytes[0];
354         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
355
356         /* src addr */
357         rte_ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->src_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 /* >8 End of l2fwd_simple_forward. */
365
366 static void
367 l2fwd_job_update_cb(struct rte_jobstats *job, int64_t result)
368 {
369         int64_t err = job->target - result;
370         int64_t histeresis = job->target / 8;
371
372         if (err < -histeresis) {
373                 if (job->min_period + UPDATE_STEP_DOWN < job->period)
374                         job->period -= UPDATE_STEP_DOWN;
375         } else if (err > histeresis) {
376                 if (job->period + UPDATE_STEP_UP < job->max_period)
377                         job->period += UPDATE_STEP_UP;
378         }
379 }
380
381 static void
382 l2fwd_fwd_job(__rte_unused struct rte_timer *timer, void *arg)
383 {
384         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
385         struct rte_mbuf *m;
386
387         const uint16_t port_idx = (uintptr_t) arg;
388         const unsigned lcore_id = rte_lcore_id();
389         struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
390         struct rte_jobstats *job = &qconf->port_fwd_jobs[port_idx];
391         const uint16_t portid = qconf->rx_port_list[port_idx];
392
393         uint8_t j;
394         uint16_t total_nb_rx;
395
396         rte_jobstats_start(&qconf->jobs_context, job);
397
398         /* Call rx burst 2 times. This allow rte_jobstats logic to see if this
399          * function must be called more frequently. */
400
401         /* Call rx burst 2 times. 8< */
402         total_nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
403                         MAX_PKT_BURST);
404
405         for (j = 0; j < total_nb_rx; j++) {
406                 m = pkts_burst[j];
407                 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
408                 l2fwd_simple_forward(m, portid);
409         }
410         /* >8 End of call rx burst 2 times. */
411
412         /* Read second try. 8< */
413         if (total_nb_rx == MAX_PKT_BURST) {
414                 const uint16_t nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
415                                 MAX_PKT_BURST);
416
417                 total_nb_rx += nb_rx;
418                 for (j = 0; j < nb_rx; j++) {
419                         m = pkts_burst[j];
420                         rte_prefetch0(rte_pktmbuf_mtod(m, void *));
421                         l2fwd_simple_forward(m, portid);
422                 }
423         }
424         /* >8 End of read second try. */
425
426         port_statistics[portid].rx += total_nb_rx;
427
428         /* Adjust period time in which we are running here. 8< */
429         if (rte_jobstats_finish(job, total_nb_rx) != 0) {
430                 rte_timer_reset(&qconf->rx_timers[port_idx], job->period, PERIODICAL,
431                                 lcore_id, l2fwd_fwd_job, arg);
432         }
433         /* >8 End of adjust period time in which we are running. */
434 }
435
436 /* Draining TX queue of each port. 8< */
437 static void
438 l2fwd_flush_job(__rte_unused struct rte_timer *timer, __rte_unused void *arg)
439 {
440         uint64_t now;
441         unsigned lcore_id;
442         struct lcore_queue_conf *qconf;
443         uint16_t portid;
444         unsigned i;
445         uint32_t sent;
446         struct rte_eth_dev_tx_buffer *buffer;
447
448         lcore_id = rte_lcore_id();
449         qconf = &lcore_queue_conf[lcore_id];
450
451         rte_jobstats_start(&qconf->jobs_context, &qconf->flush_job);
452
453         now = rte_get_timer_cycles();
454         lcore_id = rte_lcore_id();
455         qconf = &lcore_queue_conf[lcore_id];
456
457         for (i = 0; i < qconf->n_rx_port; i++) {
458                 portid = l2fwd_dst_ports[qconf->rx_port_list[i]];
459
460                 if (qconf->next_flush_time[portid] <= now)
461                         continue;
462
463                 buffer = tx_buffer[portid];
464                 sent = rte_eth_tx_buffer_flush(portid, 0, buffer);
465                 if (sent)
466                         port_statistics[portid].tx += sent;
467
468                 qconf->next_flush_time[portid] = rte_get_timer_cycles() + drain_tsc;
469         }
470
471         /* Pass target to indicate that this job is happy of time interval
472          * in which it was called. */
473         rte_jobstats_finish(&qconf->flush_job, qconf->flush_job.target);
474 }
475 /* >8 End of draining TX queue of each port. */
476
477 /* main processing loop */
478 static void
479 l2fwd_main_loop(void)
480 {
481         unsigned lcore_id;
482         unsigned i, portid;
483         struct lcore_queue_conf *qconf;
484         uint8_t stats_read_pending = 0;
485         uint8_t need_manage;
486
487         lcore_id = rte_lcore_id();
488         qconf = &lcore_queue_conf[lcore_id];
489
490         if (qconf->n_rx_port == 0) {
491                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
492                 return;
493         }
494
495         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
496
497         for (i = 0; i < qconf->n_rx_port; i++) {
498
499                 portid = qconf->rx_port_list[i];
500                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
501                         portid);
502         }
503
504         rte_jobstats_init(&qconf->idle_job, "idle", 0, 0, 0, 0);
505
506         /* Minimize impact of stats reading. 8< */
507         for (;;) {
508                 rte_spinlock_lock(&qconf->lock);
509
510                 do {
511                         rte_jobstats_context_start(&qconf->jobs_context);
512
513                         /* Do the Idle job:
514                          * - Read stats_read_pending flag
515                          * - check if some real job need to be executed
516                          */
517                         rte_jobstats_start(&qconf->jobs_context, &qconf->idle_job);
518
519                         uint64_t repeats = 0;
520
521                         do {
522                                 uint8_t i;
523                                 uint64_t now = rte_get_timer_cycles();
524
525                                 repeats++;
526                                 need_manage = qconf->flush_timer.expire < now;
527                                 /* Check if we was esked to give a stats. */
528                                 stats_read_pending = __atomic_load_n(&qconf->stats_read_pending,
529                                                 __ATOMIC_RELAXED);
530                                 need_manage |= stats_read_pending;
531
532                                 for (i = 0; i < qconf->n_rx_port && !need_manage; i++)
533                                         need_manage = qconf->rx_timers[i].expire < now;
534
535                         } while (!need_manage);
536
537                         if (likely(repeats != 1))
538                                 rte_jobstats_finish(&qconf->idle_job, qconf->idle_job.target);
539                         else
540                                 rte_jobstats_abort(&qconf->idle_job);
541
542                         rte_timer_manage();
543                         rte_jobstats_context_finish(&qconf->jobs_context);
544                 } while (likely(stats_read_pending == 0));
545
546                 rte_spinlock_unlock(&qconf->lock);
547                 rte_pause();
548         }
549         /* >8 End of minimize impact of stats reading. */
550 }
551
552 static int
553 l2fwd_launch_one_lcore(__rte_unused void *dummy)
554 {
555         l2fwd_main_loop();
556         return 0;
557 }
558
559 /* display usage */
560 static void
561 l2fwd_usage(const char *prgname)
562 {
563         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
564                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
565                "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
566                    "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n"
567                    "  -l set system default locale instead of default (\"C\" locale) for thousands separator in stats.",
568                prgname);
569 }
570
571 static int
572 l2fwd_parse_portmask(const char *portmask)
573 {
574         char *end = NULL;
575         unsigned long pm;
576
577         /* parse hexadecimal string */
578         pm = strtoul(portmask, &end, 16);
579         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
580                 return 0;
581
582         return pm;
583 }
584
585 static unsigned int
586 l2fwd_parse_nqueue(const char *q_arg)
587 {
588         char *end = NULL;
589         unsigned long n;
590
591         /* parse hexadecimal string */
592         n = strtoul(q_arg, &end, 10);
593         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
594                 return 0;
595         if (n == 0)
596                 return 0;
597         if (n >= MAX_RX_QUEUE_PER_LCORE)
598                 return 0;
599
600         return n;
601 }
602
603 static int
604 l2fwd_parse_timer_period(const char *q_arg)
605 {
606         char *end = NULL;
607         int n;
608
609         /* parse number string */
610         n = strtol(q_arg, &end, 10);
611         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
612                 return -1;
613         if (n >= MAX_TIMER_PERIOD)
614                 return -1;
615
616         return n;
617 }
618
619 /* Parse the argument given in the command line of the application */
620 static int
621 l2fwd_parse_args(int argc, char **argv)
622 {
623         int opt, ret;
624         char **argvopt;
625         int option_index;
626         char *prgname = argv[0];
627         static struct option lgopts[] = {
628                 {NULL, 0, 0, 0}
629         };
630
631         argvopt = argv;
632
633         while ((opt = getopt_long(argc, argvopt, "p:q:T:l",
634                                   lgopts, &option_index)) != EOF) {
635
636                 switch (opt) {
637                 /* portmask */
638                 case 'p':
639                         l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
640                         if (l2fwd_enabled_port_mask == 0) {
641                                 printf("invalid portmask\n");
642                                 l2fwd_usage(prgname);
643                                 return -1;
644                         }
645                         break;
646
647                 /* nqueue */
648                 case 'q':
649                         l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
650                         if (l2fwd_rx_queue_per_lcore == 0) {
651                                 printf("invalid queue number\n");
652                                 l2fwd_usage(prgname);
653                                 return -1;
654                         }
655                         break;
656
657                 /* timer period */
658                 case 'T':
659                         timer_period = l2fwd_parse_timer_period(optarg);
660                         if (timer_period < 0) {
661                                 printf("invalid timer period\n");
662                                 l2fwd_usage(prgname);
663                                 return -1;
664                         }
665                         break;
666
667                 /* For thousands separator in printf. */
668                 case 'l':
669                         setlocale(LC_ALL, "");
670                         break;
671
672                 /* long options */
673                 case 0:
674                         l2fwd_usage(prgname);
675                         return -1;
676
677                 default:
678                         l2fwd_usage(prgname);
679                         return -1;
680                 }
681         }
682
683         if (optind >= 0)
684                 argv[optind-1] = prgname;
685
686         ret = optind-1;
687         optind = 1; /* reset getopt lib */
688         return ret;
689 }
690
691 /* Check the link status of all ports in up to 9s, and print them finally */
692 static void
693 check_all_ports_link_status(uint32_t port_mask)
694 {
695 #define CHECK_INTERVAL 100 /* 100ms */
696 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
697         uint16_t portid;
698         uint8_t count, all_ports_up, print_flag = 0;
699         struct rte_eth_link link;
700         int ret;
701         char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
702
703         printf("\nChecking link status");
704         fflush(stdout);
705         for (count = 0; count <= MAX_CHECK_TIME; count++) {
706                 all_ports_up = 1;
707                 RTE_ETH_FOREACH_DEV(portid) {
708                         if ((port_mask & (1 << portid)) == 0)
709                                 continue;
710                         memset(&link, 0, sizeof(link));
711                         ret = rte_eth_link_get_nowait(portid, &link);
712                         if (ret < 0) {
713                                 all_ports_up = 0;
714                                 if (print_flag == 1)
715                                         printf("Port %u link get failed: %s\n",
716                                                 portid, rte_strerror(-ret));
717                                 continue;
718                         }
719                         /* print link status if flag set */
720                         if (print_flag == 1) {
721                                 rte_eth_link_to_str(link_status_text,
722                                         sizeof(link_status_text), &link);
723                                 printf("Port %d %s\n", portid,
724                                        link_status_text);
725                                 continue;
726                         }
727                         /* clear all_ports_up flag if any link down */
728                         if (link.link_status == RTE_ETH_LINK_DOWN) {
729                                 all_ports_up = 0;
730                                 break;
731                         }
732                 }
733                 /* after finally printing all link status, get out */
734                 if (print_flag == 1)
735                         break;
736
737                 if (all_ports_up == 0) {
738                         printf(".");
739                         fflush(stdout);
740                         rte_delay_ms(CHECK_INTERVAL);
741                 }
742
743                 /* set the print_flag if all ports up or timeout */
744                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
745                         print_flag = 1;
746                         printf("done\n");
747                 }
748         }
749 }
750
751 int
752 main(int argc, char **argv)
753 {
754         struct lcore_queue_conf *qconf;
755         unsigned lcore_id, rx_lcore_id;
756         unsigned nb_ports_in_mask = 0;
757         int ret;
758         char name[RTE_JOBSTATS_NAMESIZE];
759         uint16_t nb_ports;
760         uint16_t nb_ports_available = 0;
761         uint16_t portid, last_port;
762         uint8_t i;
763
764         /* Init EAL. 8< */
765         ret = rte_eal_init(argc, argv);
766         if (ret < 0)
767                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
768         argc -= ret;
769         argv += ret;
770
771         /* parse application arguments (after the EAL ones) */
772         ret = l2fwd_parse_args(argc, argv);
773         if (ret < 0)
774                 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
775                 /* >8 End of init EAL. */
776
777         rte_timer_subsystem_init();
778
779         /* fetch default timer frequency. */
780         hz = rte_get_timer_hz();
781
782         /* Create the mbuf pool. 8< */
783         l2fwd_pktmbuf_pool =
784                 rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
785                         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
786         if (l2fwd_pktmbuf_pool == NULL)
787                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
788         /* >8 End of creation of mbuf pool. */
789         nb_ports = rte_eth_dev_count_avail();
790         if (nb_ports == 0)
791                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
792
793         /* Reset l2fwd_dst_ports. 8< */
794         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
795                 l2fwd_dst_ports[portid] = 0;
796         last_port = 0;
797
798         /*
799          * Each logical core is assigned a dedicated TX queue on each port.
800          */
801         RTE_ETH_FOREACH_DEV(portid) {
802                 /* skip ports that are not enabled */
803                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
804                         continue;
805
806                 if (nb_ports_in_mask % 2) {
807                         l2fwd_dst_ports[portid] = last_port;
808                         l2fwd_dst_ports[last_port] = portid;
809                 } else
810                         last_port = portid;
811
812                 nb_ports_in_mask++;
813         }
814         /* >8 End of reset l2fwd_dst_ports. */
815         if (nb_ports_in_mask % 2) {
816                 printf("Notice: odd number of ports in portmask.\n");
817                 l2fwd_dst_ports[last_port] = last_port;
818         }
819
820         rx_lcore_id = 0;
821         qconf = NULL;
822
823         /* Initialize the port/queue configuration of each logical core */
824         RTE_ETH_FOREACH_DEV(portid) {
825                 /* skip ports that are not enabled */
826                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
827                         continue;
828
829                 /* get the lcore_id for this port */
830                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
831                        lcore_queue_conf[rx_lcore_id].n_rx_port ==
832                        l2fwd_rx_queue_per_lcore) {
833                         rx_lcore_id++;
834                         if (rx_lcore_id >= RTE_MAX_LCORE)
835                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
836                 }
837
838                 if (qconf != &lcore_queue_conf[rx_lcore_id])
839                         /* Assigned a new logical core in the loop above. */
840                         qconf = &lcore_queue_conf[rx_lcore_id];
841
842                 qconf->rx_port_list[qconf->n_rx_port] = portid;
843                 qconf->n_rx_port++;
844                 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
845         }
846
847         /* Initialise each port */
848         RTE_ETH_FOREACH_DEV(portid) {
849                 struct rte_eth_dev_info dev_info;
850                 struct rte_eth_rxconf rxq_conf;
851                 struct rte_eth_txconf txq_conf;
852                 struct rte_eth_conf local_port_conf = port_conf;
853
854                 /* skip ports that are not enabled */
855                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
856                         printf("Skipping disabled port %u\n", portid);
857                         continue;
858                 }
859                 nb_ports_available++;
860
861                 /* init port */
862                 printf("Initializing port %u... ", portid);
863                 fflush(stdout);
864
865                 ret = rte_eth_dev_info_get(portid, &dev_info);
866                 if (ret != 0)
867                         rte_exit(EXIT_FAILURE,
868                                 "Error during getting device (port %u) info: %s\n",
869                                 portid, strerror(-ret));
870
871                 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
872                         local_port_conf.txmode.offloads |=
873                                 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
874                 /* Configure the RX and TX queues. 8< */
875                 ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
876                 if (ret < 0)
877                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
878                                   ret, portid);
879                 /* >8 End of configuring the RX and TX queues. */
880
881                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
882                                                        &nb_txd);
883                 if (ret < 0)
884                         rte_exit(EXIT_FAILURE,
885                                  "Cannot adjust number of descriptors: err=%d, port=%u\n",
886                                  ret, portid);
887
888                 ret = rte_eth_macaddr_get(portid,
889                                           &l2fwd_ports_eth_addr[portid]);
890                 if (ret < 0)
891                         rte_exit(EXIT_FAILURE,
892                                  "Cannot get MAC address: err=%d, port=%u\n",
893                                  ret, portid);
894
895                 /* init one RX queue */
896                 fflush(stdout);
897                 rxq_conf = dev_info.default_rxconf;
898                 rxq_conf.offloads = local_port_conf.rxmode.offloads;
899                 /* RX queue initialization. 8< */
900                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
901                                              rte_eth_dev_socket_id(portid),
902                                              &rxq_conf,
903                                              l2fwd_pktmbuf_pool);
904                 if (ret < 0)
905                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
906                                   ret, portid);
907                 /* >8 End of RX queue initialization. */
908
909                 /* Init one TX queue on each port. 8< */
910                 txq_conf = dev_info.default_txconf;
911                 txq_conf.offloads = local_port_conf.txmode.offloads;
912                 fflush(stdout);
913                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
914                                 rte_eth_dev_socket_id(portid),
915                                 &txq_conf);
916                 if (ret < 0)
917                         rte_exit(EXIT_FAILURE,
918                         "rte_eth_tx_queue_setup:err=%d, port=%u\n",
919                                 ret, portid);
920                 /* >8 End of init one TX queue on each port. */
921
922                 /* Initialize TX buffers */
923                 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
924                                 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
925                                 rte_eth_dev_socket_id(portid));
926                 if (tx_buffer[portid] == NULL)
927                         rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
928                                         portid);
929
930                 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
931
932                 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
933                                 rte_eth_tx_buffer_count_callback,
934                                 &port_statistics[portid].dropped);
935                 if (ret < 0)
936                         rte_exit(EXIT_FAILURE,
937                         "Cannot set error callback for tx buffer on port %u\n",
938                                  portid);
939
940                 /* Start device */
941                 ret = rte_eth_dev_start(portid);
942                 if (ret < 0)
943                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
944                                   ret, portid);
945
946                 printf("done:\n");
947
948                 ret = rte_eth_promiscuous_enable(portid);
949                 if (ret != 0) {
950                         rte_exit(EXIT_FAILURE,
951                                  "rte_eth_promiscuous_enable:err=%s, port=%u\n",
952                                  rte_strerror(-ret), portid);
953                         return ret;
954
955                 }
956
957                 printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
958                         portid,
959                         RTE_ETHER_ADDR_BYTES(&l2fwd_ports_eth_addr[portid]));
960
961                 /* initialize port stats */
962                 memset(&port_statistics, 0, sizeof(port_statistics));
963         }
964
965         if (!nb_ports_available) {
966                 rte_exit(EXIT_FAILURE,
967                         "All available ports are disabled. Please set portmask.\n");
968         }
969
970         check_all_ports_link_status(l2fwd_enabled_port_mask);
971
972         drain_tsc = (hz + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
973
974         RTE_LCORE_FOREACH(lcore_id) {
975                 qconf = &lcore_queue_conf[lcore_id];
976
977                 rte_spinlock_init(&qconf->lock);
978
979                 if (rte_jobstats_context_init(&qconf->jobs_context) != 0)
980                         rte_panic("Jobs stats context for core %u init failed\n", lcore_id);
981
982                 if (qconf->n_rx_port == 0) {
983                         RTE_LOG(INFO, L2FWD,
984                                 "lcore %u: no ports so no jobs stats context initialization\n",
985                                 lcore_id);
986                         continue;
987                 }
988                 /* Add flush job. 8< */
989
990                 /* Set fixed period by setting min = max = initial period. Set target to
991                  * zero as it is irrelevant for this job.
992                  */
993                 rte_jobstats_init(&qconf->flush_job, "flush", drain_tsc, drain_tsc,
994                                 drain_tsc, 0);
995
996                 rte_timer_init(&qconf->flush_timer);
997                 ret = rte_timer_reset(&qconf->flush_timer, drain_tsc, PERIODICAL,
998                                 lcore_id, &l2fwd_flush_job, NULL);
999
1000                 if (ret < 0) {
1001                         rte_exit(1, "Failed to reset flush job timer for lcore %u: %s",
1002                                         lcore_id, rte_strerror(-ret));
1003                 }
1004                 /* >8 End of add flush job. */
1005
1006                 for (i = 0; i < qconf->n_rx_port; i++) {
1007                         struct rte_jobstats *job = &qconf->port_fwd_jobs[i];
1008
1009                         portid = qconf->rx_port_list[i];
1010                         printf("Setting forward job for port %u\n", portid);
1011
1012                         snprintf(name, RTE_DIM(name), "port %u fwd", portid);
1013                         /* Setup forward job. 8< */
1014
1015                         /* Set min, max and initial period. Set target to MAX_PKT_BURST as
1016                          * this is desired optimal RX/TX burst size.
1017                          */
1018                         rte_jobstats_init(job, name, 0, drain_tsc, 0, MAX_PKT_BURST);
1019                         rte_jobstats_set_update_period_function(job, l2fwd_job_update_cb);
1020
1021                         rte_timer_init(&qconf->rx_timers[i]);
1022                         ret = rte_timer_reset(&qconf->rx_timers[i], 0, PERIODICAL, lcore_id,
1023                                         &l2fwd_fwd_job, (void *)(uintptr_t)i);
1024
1025                         if (ret < 0) {
1026                                 rte_exit(1, "Failed to reset lcore %u port %u job timer: %s",
1027                                                 lcore_id, qconf->rx_port_list[i], rte_strerror(-ret));
1028                         }
1029                         /* >8 End of forward job. */
1030                 }
1031         }
1032
1033         if (timer_period)
1034                 rte_eal_alarm_set(timer_period * MS_PER_S, show_stats_cb, NULL);
1035         else
1036                 RTE_LOG(INFO, L2FWD, "Stats display disabled\n");
1037
1038         /* launch per-lcore init on every lcore */
1039         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MAIN);
1040         RTE_LCORE_FOREACH_WORKER(lcore_id) {
1041                 if (rte_eal_wait_lcore(lcore_id) < 0)
1042                         return -1;
1043         }
1044
1045         /* clean up the EAL */
1046         rte_eal_cleanup();
1047
1048         return 0;
1049 }