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