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