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