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