New upstream version 18.02
[deb_dpdk.git] / examples / load_balancer / config.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <sys/queue.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <getopt.h>
15
16 #include <rte_common.h>
17 #include <rte_byteorder.h>
18 #include <rte_log.h>
19 #include <rte_memory.h>
20 #include <rte_memcpy.h>
21 #include <rte_eal.h>
22 #include <rte_launch.h>
23 #include <rte_atomic.h>
24 #include <rte_cycles.h>
25 #include <rte_prefetch.h>
26 #include <rte_lcore.h>
27 #include <rte_per_lcore.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_interrupts.h>
30 #include <rte_random.h>
31 #include <rte_debug.h>
32 #include <rte_ether.h>
33 #include <rte_ethdev.h>
34 #include <rte_mempool.h>
35 #include <rte_mbuf.h>
36 #include <rte_ip.h>
37 #include <rte_tcp.h>
38 #include <rte_lpm.h>
39 #include <rte_string_fns.h>
40
41 #include "main.h"
42
43 struct app_params app;
44
45 static const char usage[] =
46 "                                                                               \n"
47 "    load_balancer <EAL PARAMS> -- <APP PARAMS>                                 \n"
48 "                                                                               \n"
49 "Application manadatory parameters:                                             \n"
50 "    --rx \"(PORT, QUEUE, LCORE), ...\" : List of NIC RX ports and queues       \n"
51 "           handled by the I/O RX lcores                                        \n"
52 "    --tx \"(PORT, LCORE), ...\" : List of NIC TX ports handled by the I/O TX   \n"
53 "           lcores                                                              \n"
54 "    --w \"LCORE, ...\" : List of the worker lcores                             \n"
55 "    --lpm \"IP / PREFIX => PORT; ...\" : List of LPM rules used by the worker  \n"
56 "           lcores for packet forwarding                                        \n"
57 "                                                                               \n"
58 "Application optional parameters:                                               \n"
59 "    --rsz \"A, B, C, D\" : Ring sizes                                          \n"
60 "           A = Size (in number of buffer descriptors) of each of the NIC RX    \n"
61 "               rings read by the I/O RX lcores (default value is %u)           \n"
62 "           B = Size (in number of elements) of each of the SW rings used by the\n"
63 "               I/O RX lcores to send packets to worker lcores (default value is\n"
64 "               %u)                                                             \n"
65 "           C = Size (in number of elements) of each of the SW rings used by the\n"
66 "               worker lcores to send packets to I/O TX lcores (default value is\n"
67 "               %u)                                                             \n"
68 "           D = Size (in number of buffer descriptors) of each of the NIC TX    \n"
69 "               rings written by I/O TX lcores (default value is %u)            \n"
70 "    --bsz \"(A, B), (C, D), (E, F)\" :  Burst sizes                            \n"
71 "           A = I/O RX lcore read burst size from NIC RX (default value is %u)  \n"
72 "           B = I/O RX lcore write burst size to output SW rings (default value \n"
73 "               is %u)                                                          \n"
74 "           C = Worker lcore read burst size from input SW rings (default value \n"
75 "               is %u)                                                          \n"
76 "           D = Worker lcore write burst size to output SW rings (default value \n"
77 "               is %u)                                                          \n"
78 "           E = I/O TX lcore read burst size from input SW rings (default value \n"
79 "               is %u)                                                          \n"
80 "           F = I/O TX lcore write burst size to NIC TX (default value is %u)   \n"
81 "    --pos-lb POS : Position of the 1-byte field within the input packet used by\n"
82 "           the I/O RX lcores to identify the worker lcore for the current      \n"
83 "           packet (default value is %u)                                        \n";
84
85 void
86 app_print_usage(void)
87 {
88         printf(usage,
89                 APP_DEFAULT_NIC_RX_RING_SIZE,
90                 APP_DEFAULT_RING_RX_SIZE,
91                 APP_DEFAULT_RING_TX_SIZE,
92                 APP_DEFAULT_NIC_TX_RING_SIZE,
93                 APP_DEFAULT_BURST_SIZE_IO_RX_READ,
94                 APP_DEFAULT_BURST_SIZE_IO_RX_WRITE,
95                 APP_DEFAULT_BURST_SIZE_WORKER_READ,
96                 APP_DEFAULT_BURST_SIZE_WORKER_WRITE,
97                 APP_DEFAULT_BURST_SIZE_IO_TX_READ,
98                 APP_DEFAULT_BURST_SIZE_IO_TX_WRITE,
99                 APP_DEFAULT_IO_RX_LB_POS
100         );
101 }
102
103 #ifndef APP_ARG_RX_MAX_CHARS
104 #define APP_ARG_RX_MAX_CHARS     4096
105 #endif
106
107 #ifndef APP_ARG_RX_MAX_TUPLES
108 #define APP_ARG_RX_MAX_TUPLES    128
109 #endif
110
111 static int
112 str_to_unsigned_array(
113         const char *s, size_t sbuflen,
114         char separator,
115         unsigned num_vals,
116         unsigned *vals)
117 {
118         char str[sbuflen+1];
119         char *splits[num_vals];
120         char *endptr = NULL;
121         int i, num_splits = 0;
122
123         /* copy s so we don't modify original string */
124         snprintf(str, sizeof(str), "%s", s);
125         num_splits = rte_strsplit(str, sizeof(str), splits, num_vals, separator);
126
127         errno = 0;
128         for (i = 0; i < num_splits; i++) {
129                 vals[i] = strtoul(splits[i], &endptr, 0);
130                 if (errno != 0 || *endptr != '\0')
131                         return -1;
132         }
133
134         return num_splits;
135 }
136
137 static int
138 str_to_unsigned_vals(
139         const char *s,
140         size_t sbuflen,
141         char separator,
142         unsigned num_vals, ...)
143 {
144         unsigned i, vals[num_vals];
145         va_list ap;
146
147         num_vals = str_to_unsigned_array(s, sbuflen, separator, num_vals, vals);
148
149         va_start(ap, num_vals);
150         for (i = 0; i < num_vals; i++) {
151                 unsigned *u = va_arg(ap, unsigned *);
152                 *u = vals[i];
153         }
154         va_end(ap);
155         return num_vals;
156 }
157
158 static int
159 parse_arg_rx(const char *arg)
160 {
161         const char *p0 = arg, *p = arg;
162         uint32_t n_tuples;
163
164         if (strnlen(arg, APP_ARG_RX_MAX_CHARS + 1) == APP_ARG_RX_MAX_CHARS + 1) {
165                 return -1;
166         }
167
168         n_tuples = 0;
169         while ((p = strchr(p0,'(')) != NULL) {
170                 struct app_lcore_params *lp;
171                 uint32_t port, queue, lcore, i;
172
173                 p0 = strchr(p++, ')');
174                 if ((p0 == NULL) ||
175                     (str_to_unsigned_vals(p, p0 - p, ',', 3, &port, &queue, &lcore) !=  3)) {
176                         return -2;
177                 }
178
179                 /* Enable port and queue for later initialization */
180                 if ((port >= APP_MAX_NIC_PORTS) || (queue >= APP_MAX_RX_QUEUES_PER_NIC_PORT)) {
181                         return -3;
182                 }
183                 if (app.nic_rx_queue_mask[port][queue] != 0) {
184                         return -4;
185                 }
186                 app.nic_rx_queue_mask[port][queue] = 1;
187
188                 /* Check and assign (port, queue) to I/O lcore */
189                 if (rte_lcore_is_enabled(lcore) == 0) {
190                         return -5;
191                 }
192
193                 if (lcore >= APP_MAX_LCORES) {
194                         return -6;
195                 }
196                 lp = &app.lcore_params[lcore];
197                 if (lp->type == e_APP_LCORE_WORKER) {
198                         return -7;
199                 }
200                 lp->type = e_APP_LCORE_IO;
201                 const size_t n_queues = RTE_MIN(lp->io.rx.n_nic_queues,
202                                                 RTE_DIM(lp->io.rx.nic_queues));
203                 for (i = 0; i < n_queues; i ++) {
204                         if ((lp->io.rx.nic_queues[i].port == port) &&
205                             (lp->io.rx.nic_queues[i].queue == queue)) {
206                                 return -8;
207                         }
208                 }
209                 if (lp->io.rx.n_nic_queues >= APP_MAX_NIC_RX_QUEUES_PER_IO_LCORE) {
210                         return -9;
211                 }
212                 lp->io.rx.nic_queues[lp->io.rx.n_nic_queues].port = port;
213                 lp->io.rx.nic_queues[lp->io.rx.n_nic_queues].queue = (uint8_t) queue;
214                 lp->io.rx.n_nic_queues ++;
215
216                 n_tuples ++;
217                 if (n_tuples > APP_ARG_RX_MAX_TUPLES) {
218                         return -10;
219                 }
220         }
221
222         if (n_tuples == 0) {
223                 return -11;
224         }
225
226         return 0;
227 }
228
229 #ifndef APP_ARG_TX_MAX_CHARS
230 #define APP_ARG_TX_MAX_CHARS     4096
231 #endif
232
233 #ifndef APP_ARG_TX_MAX_TUPLES
234 #define APP_ARG_TX_MAX_TUPLES    128
235 #endif
236
237 static int
238 parse_arg_tx(const char *arg)
239 {
240         const char *p0 = arg, *p = arg;
241         uint32_t n_tuples;
242
243         if (strnlen(arg, APP_ARG_TX_MAX_CHARS + 1) == APP_ARG_TX_MAX_CHARS + 1) {
244                 return -1;
245         }
246
247         n_tuples = 0;
248         while ((p = strchr(p0,'(')) != NULL) {
249                 struct app_lcore_params *lp;
250                 uint32_t port, lcore, i;
251
252                 p0 = strchr(p++, ')');
253                 if ((p0 == NULL) ||
254                     (str_to_unsigned_vals(p, p0 - p, ',', 2, &port, &lcore) !=  2)) {
255                         return -2;
256                 }
257
258                 /* Enable port and queue for later initialization */
259                 if (port >= APP_MAX_NIC_PORTS) {
260                         return -3;
261                 }
262                 if (app.nic_tx_port_mask[port] != 0) {
263                         return -4;
264                 }
265                 app.nic_tx_port_mask[port] = 1;
266
267                 /* Check and assign (port, queue) to I/O lcore */
268                 if (rte_lcore_is_enabled(lcore) == 0) {
269                         return -5;
270                 }
271
272                 if (lcore >= APP_MAX_LCORES) {
273                         return -6;
274                 }
275                 lp = &app.lcore_params[lcore];
276                 if (lp->type == e_APP_LCORE_WORKER) {
277                         return -7;
278                 }
279                 lp->type = e_APP_LCORE_IO;
280                 const size_t n_ports = RTE_MIN(lp->io.tx.n_nic_ports,
281                                                RTE_DIM(lp->io.tx.nic_ports));
282                 for (i = 0; i < n_ports; i ++) {
283                         if (lp->io.tx.nic_ports[i] == port) {
284                                 return -8;
285                         }
286                 }
287                 if (lp->io.tx.n_nic_ports >= APP_MAX_NIC_TX_PORTS_PER_IO_LCORE) {
288                         return -9;
289                 }
290                 lp->io.tx.nic_ports[lp->io.tx.n_nic_ports] = port;
291                 lp->io.tx.n_nic_ports ++;
292
293                 n_tuples ++;
294                 if (n_tuples > APP_ARG_TX_MAX_TUPLES) {
295                         return -10;
296                 }
297         }
298
299         if (n_tuples == 0) {
300                 return -11;
301         }
302
303         return 0;
304 }
305
306 #ifndef APP_ARG_W_MAX_CHARS
307 #define APP_ARG_W_MAX_CHARS     4096
308 #endif
309
310 #ifndef APP_ARG_W_MAX_TUPLES
311 #define APP_ARG_W_MAX_TUPLES    APP_MAX_WORKER_LCORES
312 #endif
313
314 static int
315 parse_arg_w(const char *arg)
316 {
317         const char *p = arg;
318         uint32_t n_tuples;
319
320         if (strnlen(arg, APP_ARG_W_MAX_CHARS + 1) == APP_ARG_W_MAX_CHARS + 1) {
321                 return -1;
322         }
323
324         n_tuples = 0;
325         while (*p != 0) {
326                 struct app_lcore_params *lp;
327                 uint32_t lcore;
328
329                 errno = 0;
330                 lcore = strtoul(p, NULL, 0);
331                 if (errno != 0) {
332                         return -2;
333                 }
334
335                 /* Check and enable worker lcore */
336                 if (rte_lcore_is_enabled(lcore) == 0) {
337                         return -3;
338                 }
339
340                 if (lcore >= APP_MAX_LCORES) {
341                         return -4;
342                 }
343                 lp = &app.lcore_params[lcore];
344                 if (lp->type == e_APP_LCORE_IO) {
345                         return -5;
346                 }
347                 lp->type = e_APP_LCORE_WORKER;
348
349                 n_tuples ++;
350                 if (n_tuples > APP_ARG_W_MAX_TUPLES) {
351                         return -6;
352                 }
353
354                 p = strchr(p, ',');
355                 if (p == NULL) {
356                         break;
357                 }
358                 p ++;
359         }
360
361         if (n_tuples == 0) {
362                 return -7;
363         }
364
365         if ((n_tuples & (n_tuples - 1)) != 0) {
366                 return -8;
367         }
368
369         return 0;
370 }
371
372 #ifndef APP_ARG_LPM_MAX_CHARS
373 #define APP_ARG_LPM_MAX_CHARS     4096
374 #endif
375
376 static int
377 parse_arg_lpm(const char *arg)
378 {
379         const char *p = arg, *p0;
380
381         if (strnlen(arg, APP_ARG_LPM_MAX_CHARS + 1) == APP_ARG_TX_MAX_CHARS + 1) {
382                 return -1;
383         }
384
385         while (*p != 0) {
386                 uint32_t ip_a, ip_b, ip_c, ip_d, ip, depth, if_out;
387                 char *endptr;
388
389                 p0 = strchr(p, '/');
390                 if ((p0 == NULL) ||
391                     (str_to_unsigned_vals(p, p0 - p, '.', 4, &ip_a, &ip_b, &ip_c, &ip_d) != 4)) {
392                         return -2;
393                 }
394
395                 p = p0 + 1;
396                 errno = 0;
397                 depth = strtoul(p, &endptr, 0);
398                 if (errno != 0 || *endptr != '=') {
399                         return -3;
400                 }
401                 p = strchr(p, '>');
402                 if (p == NULL) {
403                         return -4;
404                 }
405                 if_out = strtoul(++p, &endptr, 0);
406                 if (errno != 0 || (*endptr != '\0' && *endptr != ';')) {
407                         return -5;
408                 }
409
410                 if ((ip_a >= 256) || (ip_b >= 256) || (ip_c >= 256) || (ip_d >= 256) ||
411                      (depth == 0) || (depth >= 32) ||
412                          (if_out >= APP_MAX_NIC_PORTS)) {
413                         return -6;
414                 }
415                 ip = (ip_a << 24) | (ip_b << 16) | (ip_c << 8) | ip_d;
416
417                 if (app.n_lpm_rules >= APP_MAX_LPM_RULES) {
418                         return -7;
419                 }
420                 app.lpm_rules[app.n_lpm_rules].ip = ip;
421                 app.lpm_rules[app.n_lpm_rules].depth = (uint8_t) depth;
422                 app.lpm_rules[app.n_lpm_rules].if_out = (uint8_t) if_out;
423                 app.n_lpm_rules ++;
424
425                 p = strchr(p, ';');
426                 if (p == NULL) {
427                         return -8;
428                 }
429                 p ++;
430         }
431
432         if (app.n_lpm_rules == 0) {
433                 return -9;
434         }
435
436         return 0;
437 }
438
439 static int
440 app_check_lpm_table(void)
441 {
442         uint32_t rule;
443
444         /* For each rule, check that the output I/F is enabled */
445         for (rule = 0; rule < app.n_lpm_rules; rule ++)
446         {
447                 uint32_t port = app.lpm_rules[rule].if_out;
448
449                 if (app.nic_tx_port_mask[port] == 0) {
450                         return -1;
451                 }
452         }
453
454         return 0;
455 }
456
457 static int
458 app_check_every_rx_port_is_tx_enabled(void)
459 {
460         uint16_t port;
461
462         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
463                 if ((app_get_nic_rx_queues_per_port(port) > 0) && (app.nic_tx_port_mask[port] == 0)) {
464                         return -1;
465                 }
466         }
467
468         return 0;
469 }
470
471 #ifndef APP_ARG_RSZ_CHARS
472 #define APP_ARG_RSZ_CHARS 63
473 #endif
474
475 static int
476 parse_arg_rsz(const char *arg)
477 {
478         if (strnlen(arg, APP_ARG_RSZ_CHARS + 1) == APP_ARG_RSZ_CHARS + 1) {
479                 return -1;
480         }
481
482         if (str_to_unsigned_vals(arg, APP_ARG_RSZ_CHARS, ',', 4,
483                         &app.nic_rx_ring_size,
484                         &app.ring_rx_size,
485                         &app.ring_tx_size,
486                         &app.nic_tx_ring_size) !=  4)
487                 return -2;
488
489
490         if ((app.nic_rx_ring_size == 0) ||
491                 (app.nic_tx_ring_size == 0) ||
492                 (app.ring_rx_size == 0) ||
493                 (app.ring_tx_size == 0)) {
494                 return -3;
495         }
496
497         return 0;
498 }
499
500 #ifndef APP_ARG_BSZ_CHARS
501 #define APP_ARG_BSZ_CHARS 63
502 #endif
503
504 static int
505 parse_arg_bsz(const char *arg)
506 {
507         const char *p = arg, *p0;
508         if (strnlen(arg, APP_ARG_BSZ_CHARS + 1) == APP_ARG_BSZ_CHARS + 1) {
509                 return -1;
510         }
511
512         p0 = strchr(p++, ')');
513         if ((p0 == NULL) ||
514             (str_to_unsigned_vals(p, p0 - p, ',', 2, &app.burst_size_io_rx_read, &app.burst_size_io_rx_write) !=  2)) {
515                 return -2;
516         }
517
518         p = strchr(p0, '(');
519         if (p == NULL) {
520                 return -3;
521         }
522
523         p0 = strchr(p++, ')');
524         if ((p0 == NULL) ||
525             (str_to_unsigned_vals(p, p0 - p, ',', 2, &app.burst_size_worker_read, &app.burst_size_worker_write) !=  2)) {
526                 return -4;
527         }
528
529         p = strchr(p0, '(');
530         if (p == NULL) {
531                 return -5;
532         }
533
534         p0 = strchr(p++, ')');
535         if ((p0 == NULL) ||
536             (str_to_unsigned_vals(p, p0 - p, ',', 2, &app.burst_size_io_tx_read, &app.burst_size_io_tx_write) !=  2)) {
537                 return -6;
538         }
539
540         if ((app.burst_size_io_rx_read == 0) ||
541                 (app.burst_size_io_rx_write == 0) ||
542                 (app.burst_size_worker_read == 0) ||
543                 (app.burst_size_worker_write == 0) ||
544                 (app.burst_size_io_tx_read == 0) ||
545                 (app.burst_size_io_tx_write == 0)) {
546                 return -7;
547         }
548
549         if ((app.burst_size_io_rx_read > APP_MBUF_ARRAY_SIZE) ||
550                 (app.burst_size_io_rx_write > APP_MBUF_ARRAY_SIZE) ||
551                 (app.burst_size_worker_read > APP_MBUF_ARRAY_SIZE) ||
552                 (app.burst_size_worker_write > APP_MBUF_ARRAY_SIZE) ||
553                 ((2 * app.burst_size_io_tx_read) > APP_MBUF_ARRAY_SIZE) ||
554                 (app.burst_size_io_tx_write > APP_MBUF_ARRAY_SIZE)) {
555                 return -8;
556         }
557
558         return 0;
559 }
560
561 #ifndef APP_ARG_NUMERICAL_SIZE_CHARS
562 #define APP_ARG_NUMERICAL_SIZE_CHARS 15
563 #endif
564
565 static int
566 parse_arg_pos_lb(const char *arg)
567 {
568         uint32_t x;
569         char *endpt;
570
571         if (strnlen(arg, APP_ARG_NUMERICAL_SIZE_CHARS + 1) == APP_ARG_NUMERICAL_SIZE_CHARS + 1) {
572                 return -1;
573         }
574
575         errno = 0;
576         x = strtoul(arg, &endpt, 10);
577         if (errno != 0 || endpt == arg || *endpt != '\0'){
578                 return -2;
579         }
580
581         if (x >= 64) {
582                 return -3;
583         }
584
585         app.pos_lb = (uint8_t) x;
586
587         return 0;
588 }
589
590 /* Parse the argument given in the command line of the application */
591 int
592 app_parse_args(int argc, char **argv)
593 {
594         int opt, ret;
595         char **argvopt;
596         int option_index;
597         char *prgname = argv[0];
598         static struct option lgopts[] = {
599                 {"rx", 1, 0, 0},
600                 {"tx", 1, 0, 0},
601                 {"w", 1, 0, 0},
602                 {"lpm", 1, 0, 0},
603                 {"rsz", 1, 0, 0},
604                 {"bsz", 1, 0, 0},
605                 {"pos-lb", 1, 0, 0},
606                 {NULL, 0, 0, 0}
607         };
608         uint32_t arg_w = 0;
609         uint32_t arg_rx = 0;
610         uint32_t arg_tx = 0;
611         uint32_t arg_lpm = 0;
612         uint32_t arg_rsz = 0;
613         uint32_t arg_bsz = 0;
614         uint32_t arg_pos_lb = 0;
615
616         argvopt = argv;
617
618         while ((opt = getopt_long(argc, argvopt, "",
619                                 lgopts, &option_index)) != EOF) {
620
621                 switch (opt) {
622                 /* long options */
623                 case 0:
624                         if (!strcmp(lgopts[option_index].name, "rx")) {
625                                 arg_rx = 1;
626                                 ret = parse_arg_rx(optarg);
627                                 if (ret) {
628                                         printf("Incorrect value for --rx argument (%d)\n", ret);
629                                         return -1;
630                                 }
631                         }
632                         if (!strcmp(lgopts[option_index].name, "tx")) {
633                                 arg_tx = 1;
634                                 ret = parse_arg_tx(optarg);
635                                 if (ret) {
636                                         printf("Incorrect value for --tx argument (%d)\n", ret);
637                                         return -1;
638                                 }
639                         }
640                         if (!strcmp(lgopts[option_index].name, "w")) {
641                                 arg_w = 1;
642                                 ret = parse_arg_w(optarg);
643                                 if (ret) {
644                                         printf("Incorrect value for --w argument (%d)\n", ret);
645                                         return -1;
646                                 }
647                         }
648                         if (!strcmp(lgopts[option_index].name, "lpm")) {
649                                 arg_lpm = 1;
650                                 ret = parse_arg_lpm(optarg);
651                                 if (ret) {
652                                         printf("Incorrect value for --lpm argument (%d)\n", ret);
653                                         return -1;
654                                 }
655                         }
656                         if (!strcmp(lgopts[option_index].name, "rsz")) {
657                                 arg_rsz = 1;
658                                 ret = parse_arg_rsz(optarg);
659                                 if (ret) {
660                                         printf("Incorrect value for --rsz argument (%d)\n", ret);
661                                         return -1;
662                                 }
663                         }
664                         if (!strcmp(lgopts[option_index].name, "bsz")) {
665                                 arg_bsz = 1;
666                                 ret = parse_arg_bsz(optarg);
667                                 if (ret) {
668                                         printf("Incorrect value for --bsz argument (%d)\n", ret);
669                                         return -1;
670                                 }
671                         }
672                         if (!strcmp(lgopts[option_index].name, "pos-lb")) {
673                                 arg_pos_lb = 1;
674                                 ret = parse_arg_pos_lb(optarg);
675                                 if (ret) {
676                                         printf("Incorrect value for --pos-lb argument (%d)\n", ret);
677                                         return -1;
678                                 }
679                         }
680                         break;
681
682                 default:
683                         return -1;
684                 }
685         }
686
687         /* Check that all mandatory arguments are provided */
688         if ((arg_rx == 0) || (arg_tx == 0) || (arg_w == 0) || (arg_lpm == 0)){
689                 printf("Not all mandatory arguments are present\n");
690                 return -1;
691         }
692
693         /* Assign default values for the optional arguments not provided */
694         if (arg_rsz == 0) {
695                 app.nic_rx_ring_size = APP_DEFAULT_NIC_RX_RING_SIZE;
696                 app.nic_tx_ring_size = APP_DEFAULT_NIC_TX_RING_SIZE;
697                 app.ring_rx_size = APP_DEFAULT_RING_RX_SIZE;
698                 app.ring_tx_size = APP_DEFAULT_RING_TX_SIZE;
699         }
700
701         if (arg_bsz == 0) {
702                 app.burst_size_io_rx_read = APP_DEFAULT_BURST_SIZE_IO_RX_READ;
703                 app.burst_size_io_rx_write = APP_DEFAULT_BURST_SIZE_IO_RX_WRITE;
704                 app.burst_size_io_tx_read = APP_DEFAULT_BURST_SIZE_IO_TX_READ;
705                 app.burst_size_io_tx_write = APP_DEFAULT_BURST_SIZE_IO_TX_WRITE;
706                 app.burst_size_worker_read = APP_DEFAULT_BURST_SIZE_WORKER_READ;
707                 app.burst_size_worker_write = APP_DEFAULT_BURST_SIZE_WORKER_WRITE;
708         }
709
710         if (arg_pos_lb == 0) {
711                 app.pos_lb = APP_DEFAULT_IO_RX_LB_POS;
712         }
713
714         /* Check cross-consistency of arguments */
715         if ((ret = app_check_lpm_table()) < 0) {
716                 printf("At least one LPM rule is inconsistent (%d)\n", ret);
717                 return -1;
718         }
719         if (app_check_every_rx_port_is_tx_enabled() < 0) {
720                 printf("On LPM lookup miss, packet is sent back on the input port.\n");
721                 printf("At least one RX port is not enabled for TX.\n");
722                 return -2;
723         }
724
725         if (optind >= 0)
726                 argv[optind - 1] = prgname;
727
728         ret = optind - 1;
729         optind = 1; /* reset getopt lib */
730         return ret;
731 }
732
733 int
734 app_get_nic_rx_queues_per_port(uint16_t port)
735 {
736         uint32_t i, count;
737
738         if (port >= APP_MAX_NIC_PORTS) {
739                 return -1;
740         }
741
742         count = 0;
743         for (i = 0; i < APP_MAX_RX_QUEUES_PER_NIC_PORT; i ++) {
744                 if (app.nic_rx_queue_mask[port][i] == 1) {
745                         count ++;
746                 }
747         }
748
749         return count;
750 }
751
752 int
753 app_get_lcore_for_nic_rx(uint16_t port, uint8_t queue, uint32_t *lcore_out)
754 {
755         uint32_t lcore;
756
757         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
758                 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
759                 uint32_t i;
760
761                 if (app.lcore_params[lcore].type != e_APP_LCORE_IO) {
762                         continue;
763                 }
764
765                 const size_t n_queues = RTE_MIN(lp->rx.n_nic_queues,
766                                                 RTE_DIM(lp->rx.nic_queues));
767                 for (i = 0; i < n_queues; i ++) {
768                         if ((lp->rx.nic_queues[i].port == port) &&
769                             (lp->rx.nic_queues[i].queue == queue)) {
770                                 *lcore_out = lcore;
771                                 return 0;
772                         }
773                 }
774         }
775
776         return -1;
777 }
778
779 int
780 app_get_lcore_for_nic_tx(uint16_t port, uint32_t *lcore_out)
781 {
782         uint32_t lcore;
783
784         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
785                 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
786                 uint32_t i;
787
788                 if (app.lcore_params[lcore].type != e_APP_LCORE_IO) {
789                         continue;
790                 }
791
792                 const size_t n_ports = RTE_MIN(lp->tx.n_nic_ports,
793                                                RTE_DIM(lp->tx.nic_ports));
794                 for (i = 0; i < n_ports; i ++) {
795                         if (lp->tx.nic_ports[i] == port) {
796                                 *lcore_out = lcore;
797                                 return 0;
798                         }
799                 }
800         }
801
802         return -1;
803 }
804
805 int
806 app_is_socket_used(uint32_t socket)
807 {
808         uint32_t lcore;
809
810         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
811                 if (app.lcore_params[lcore].type == e_APP_LCORE_DISABLED) {
812                         continue;
813                 }
814
815                 if (socket == rte_lcore_to_socket_id(lcore)) {
816                         return 1;
817                 }
818         }
819
820         return 0;
821 }
822
823 uint32_t
824 app_get_lcores_io_rx(void)
825 {
826         uint32_t lcore, count;
827
828         count = 0;
829         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
830                 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
831
832                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
833                     (lp_io->rx.n_nic_queues == 0)) {
834                         continue;
835                 }
836
837                 count ++;
838         }
839
840         return count;
841 }
842
843 uint32_t
844 app_get_lcores_worker(void)
845 {
846         uint32_t lcore, count;
847
848         count = 0;
849         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
850                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
851                         continue;
852                 }
853
854                 count ++;
855         }
856
857         if (count > APP_MAX_WORKER_LCORES) {
858                 rte_panic("Algorithmic error (too many worker lcores)\n");
859                 return 0;
860         }
861
862         return count;
863 }
864
865 void
866 app_print_params(void)
867 {
868         unsigned port, queue, lcore, rule, i, j;
869
870         /* Print NIC RX configuration */
871         printf("NIC RX ports: ");
872         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
873                 uint32_t n_rx_queues = app_get_nic_rx_queues_per_port(port);
874
875                 if (n_rx_queues == 0) {
876                         continue;
877                 }
878
879                 printf("%u (", port);
880                 for (queue = 0; queue < APP_MAX_RX_QUEUES_PER_NIC_PORT; queue ++) {
881                         if (app.nic_rx_queue_mask[port][queue] == 1) {
882                                 printf("%u ", queue);
883                         }
884                 }
885                 printf(")  ");
886         }
887         printf(";\n");
888
889         /* Print I/O lcore RX params */
890         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
891                 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
892
893                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
894                     (lp->rx.n_nic_queues == 0)) {
895                         continue;
896                 }
897
898                 printf("I/O lcore %u (socket %u): ", lcore, rte_lcore_to_socket_id(lcore));
899
900                 printf("RX ports  ");
901                 for (i = 0; i < lp->rx.n_nic_queues; i ++) {
902                         printf("(%u, %u)  ",
903                                 (unsigned) lp->rx.nic_queues[i].port,
904                                 (unsigned) lp->rx.nic_queues[i].queue);
905                 }
906                 printf("; ");
907
908                 printf("Output rings  ");
909                 for (i = 0; i < lp->rx.n_rings; i ++) {
910                         printf("%p  ", lp->rx.rings[i]);
911                 }
912                 printf(";\n");
913         }
914
915         /* Print worker lcore RX params */
916         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
917                 struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
918
919                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
920                         continue;
921                 }
922
923                 printf("Worker lcore %u (socket %u) ID %u: ",
924                         lcore,
925                         rte_lcore_to_socket_id(lcore),
926                         (unsigned)lp->worker_id);
927
928                 printf("Input rings  ");
929                 for (i = 0; i < lp->n_rings_in; i ++) {
930                         printf("%p  ", lp->rings_in[i]);
931                 }
932
933                 printf(";\n");
934         }
935
936         printf("\n");
937
938         /* Print NIC TX configuration */
939         printf("NIC TX ports:  ");
940         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
941                 if (app.nic_tx_port_mask[port] == 1) {
942                         printf("%u  ", port);
943                 }
944         }
945         printf(";\n");
946
947         /* Print I/O TX lcore params */
948         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
949                 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
950                 uint32_t n_workers = app_get_lcores_worker();
951
952                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
953                      (lp->tx.n_nic_ports == 0)) {
954                         continue;
955                 }
956
957                 printf("I/O lcore %u (socket %u): ", lcore, rte_lcore_to_socket_id(lcore));
958
959                 printf("Input rings per TX port  ");
960                 for (i = 0; i < lp->tx.n_nic_ports; i ++) {
961                         port = lp->tx.nic_ports[i];
962
963                         printf("%u (", port);
964                         for (j = 0; j < n_workers; j ++) {
965                                 printf("%p  ", lp->tx.rings[port][j]);
966                         }
967                         printf(")  ");
968
969                 }
970
971                 printf(";\n");
972         }
973
974         /* Print worker lcore TX params */
975         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
976                 struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
977
978                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
979                         continue;
980                 }
981
982                 printf("Worker lcore %u (socket %u) ID %u: \n",
983                         lcore,
984                         rte_lcore_to_socket_id(lcore),
985                         (unsigned)lp->worker_id);
986
987                 printf("Output rings per TX port  ");
988                 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
989                         if (lp->rings_out[port] != NULL) {
990                                 printf("%u (%p)  ", port, lp->rings_out[port]);
991                         }
992                 }
993
994                 printf(";\n");
995         }
996
997         /* Print LPM rules */
998         printf("LPM rules: \n");
999         for (rule = 0; rule < app.n_lpm_rules; rule ++) {
1000                 uint32_t ip = app.lpm_rules[rule].ip;
1001                 uint8_t depth = app.lpm_rules[rule].depth;
1002                 uint8_t if_out = app.lpm_rules[rule].if_out;
1003
1004                 printf("\t%u: %u.%u.%u.%u/%u => %u;\n",
1005                         rule,
1006                         (unsigned) (ip & 0xFF000000) >> 24,
1007                         (unsigned) (ip & 0x00FF0000) >> 16,
1008                         (unsigned) (ip & 0x0000FF00) >> 8,
1009                         (unsigned) ip & 0x000000FF,
1010                         (unsigned) depth,
1011                         (unsigned) if_out
1012                 );
1013         }
1014
1015         /* Rings */
1016         printf("Ring sizes: NIC RX = %u; Worker in = %u; Worker out = %u; NIC TX = %u;\n",
1017                 (unsigned) app.nic_rx_ring_size,
1018                 (unsigned) app.ring_rx_size,
1019                 (unsigned) app.ring_tx_size,
1020                 (unsigned) app.nic_tx_ring_size);
1021
1022         /* Bursts */
1023         printf("Burst sizes: I/O RX (rd = %u, wr = %u); Worker (rd = %u, wr = %u); I/O TX (rd = %u, wr = %u)\n",
1024                 (unsigned) app.burst_size_io_rx_read,
1025                 (unsigned) app.burst_size_io_rx_write,
1026                 (unsigned) app.burst_size_worker_read,
1027                 (unsigned) app.burst_size_worker_write,
1028                 (unsigned) app.burst_size_io_tx_read,
1029                 (unsigned) app.burst_size_io_tx_write);
1030 }