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