remove version in all files
[dpdk.git] / examples / load_balancer / config.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <sys/types.h>
40 #include <string.h>
41 #include <sys/queue.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include <getopt.h>
45
46 #include <rte_common.h>
47 #include <rte_byteorder.h>
48 #include <rte_log.h>
49 #include <rte_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_tailq.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_launch.h>
56 #include <rte_atomic.h>
57 #include <rte_cycles.h>
58 #include <rte_prefetch.h>
59 #include <rte_lcore.h>
60 #include <rte_per_lcore.h>
61 #include <rte_branch_prediction.h>
62 #include <rte_interrupts.h>
63 #include <rte_pci.h>
64 #include <rte_random.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
68 #include <rte_ring.h>
69 #include <rte_mempool.h>
70 #include <rte_mbuf.h>
71 #include <rte_ip.h>
72 #include <rte_tcp.h>
73 #include <rte_lpm.h>
74 #include <rte_string_fns.h>
75
76 #include "main.h"
77
78 struct app_params app;
79
80 static const char usage[] =
81 "                                                                               \n"
82 "    load_balancer <EAL PARAMS> -- <APP PARAMS>                                 \n"
83 "                                                                               \n"
84 "Application manadatory parameters:                                             \n"
85 "    --rx \"(PORT, QUEUE, LCORE), ...\" : List of NIC RX ports and queues       \n"
86 "           handled by the I/O RX lcores                                        \n"
87 "    --tx \"(PORT, LCORE), ...\" : List of NIC TX ports handled by the I/O TX   \n"
88 "           lcores                                                              \n"
89 "    --w \"LCORE, ...\" : List of the worker lcores                             \n"
90 "    --lpm \"IP / PREFIX => PORT; ...\" : List of LPM rules used by the worker  \n"
91 "           lcores for packet forwarding                                        \n"
92 "                                                                               \n"
93 "Application optional parameters:                                               \n"
94 "    --rsz \"A, B, C, D\" : Ring sizes                                          \n"
95 "           A = Size (in number of buffer descriptors) of each of the NIC RX    \n"
96 "               rings read by the I/O RX lcores (default value is %u)           \n"
97 "           B = Size (in number of elements) of each of the SW rings used by the\n"
98 "               I/O RX lcores to send packets to worker lcores (default value is\n"
99 "               %u)                                                             \n"
100 "           C = Size (in number of elements) of each of the SW rings used by the\n"
101 "               worker lcores to send packets to I/O TX lcores (default value is\n"
102 "               %u)                                                             \n"
103 "           D = Size (in number of buffer descriptors) of each of the NIC TX    \n"
104 "               rings written by I/O TX lcores (default value is %u)            \n"
105 "    --bsz \"(A, B), (C, D), (E, F)\" :  Burst sizes                            \n"
106 "           A = I/O RX lcore read burst size from NIC RX (default value is %u)  \n"
107 "           B = I/O RX lcore write burst size to output SW rings (default value \n"
108 "               is %u)                                                          \n"
109 "           C = Worker lcore read burst size from input SW rings (default value \n"
110 "               is %u)                                                          \n"
111 "           D = Worker lcore write burst size to output SW rings (default value \n"
112 "               is %u)                                                          \n"
113 "           E = I/O TX lcore read burst size from input SW rings (default value \n"
114 "               is %u)                                                          \n"
115 "           F = I/O TX lcore write burst size to NIC TX (default value is %u)   \n"
116 "    --pos-lb POS : Position of the 1-byte field within the input packet used by\n"
117 "           the I/O RX lcores to identify the worker lcore for the current      \n"
118 "           packet (default value is %u)                                        \n";
119
120 void
121 app_print_usage(void)
122 {
123         printf(usage,
124                 APP_DEFAULT_NIC_RX_RING_SIZE,
125                 APP_DEFAULT_RING_RX_SIZE,
126                 APP_DEFAULT_RING_TX_SIZE,
127                 APP_DEFAULT_NIC_TX_RING_SIZE,
128                 APP_DEFAULT_BURST_SIZE_IO_RX_READ,
129                 APP_DEFAULT_BURST_SIZE_IO_RX_WRITE,
130                 APP_DEFAULT_BURST_SIZE_WORKER_READ,
131                 APP_DEFAULT_BURST_SIZE_WORKER_WRITE,
132                 APP_DEFAULT_BURST_SIZE_IO_TX_READ,
133                 APP_DEFAULT_BURST_SIZE_IO_TX_WRITE,
134                 APP_DEFAULT_IO_RX_LB_POS
135         );
136 }
137
138 #ifndef APP_ARG_RX_MAX_CHARS
139 #define APP_ARG_RX_MAX_CHARS     4096
140 #endif
141
142 #ifndef APP_ARG_RX_MAX_TUPLES
143 #define APP_ARG_RX_MAX_TUPLES    128
144 #endif
145
146 static int
147 str_to_unsigned_array(
148         const char *s, size_t sbuflen,
149         char separator,
150         unsigned num_vals,
151         unsigned *vals)
152 {
153         char str[sbuflen+1];
154         char *splits[num_vals];
155         char *endptr = NULL;
156         int i, num_splits = 0;
157
158         /* copy s so we don't modify original string */
159         rte_snprintf(str, sizeof(str), "%s", s);
160         num_splits = rte_strsplit(str, sizeof(str), splits, num_vals, separator);
161
162         errno = 0;
163         for (i = 0; i < num_splits; i++) {
164                 vals[i] = strtoul(splits[i], &endptr, 0);
165                 if (errno != 0 || *endptr != '\0')
166                         return -1;
167         }
168
169         return num_splits;
170 }
171
172 static int
173 str_to_unsigned_vals(
174         const char *s,
175         size_t sbuflen,
176         char separator,
177         unsigned num_vals, ...)
178 {
179         unsigned i, vals[num_vals];
180         va_list ap;
181
182         num_vals = str_to_unsigned_array(s, sbuflen, separator, num_vals, vals);
183
184         va_start(ap, num_vals);
185         for (i = 0; i < num_vals; i++) {
186                 unsigned *u = va_arg(ap, unsigned *);
187                 *u = vals[i];
188         }
189         va_end(ap);
190         return num_vals;
191 }
192
193 static int
194 parse_arg_rx(const char *arg)
195 {
196         const char *p0 = arg, *p = arg;
197         uint32_t n_tuples;
198
199         if (strnlen(arg, APP_ARG_RX_MAX_CHARS + 1) == APP_ARG_RX_MAX_CHARS + 1) {
200                 return -1;
201         }
202
203         n_tuples = 0;
204         while ((p = strchr(p0,'(')) != NULL) {
205                 struct app_lcore_params *lp;
206                 uint32_t port, queue, lcore, i;
207
208                 p0 = strchr(p++, ')');
209                 if ((p0 == NULL) ||
210                     (str_to_unsigned_vals(p, p0 - p, ',', 3, &port, &queue, &lcore) !=  3)) {
211                         return -2;
212                 }
213
214                 /* Enable port and queue for later initialization */
215                 if ((port >= APP_MAX_NIC_PORTS) || (queue >= APP_MAX_RX_QUEUES_PER_NIC_PORT)) {
216                         return -3;
217                 }
218                 if (app.nic_rx_queue_mask[port][queue] != 0) {
219                         return -4;
220                 }
221                 app.nic_rx_queue_mask[port][queue] = 1;
222
223                 /* Check and assign (port, queue) to I/O lcore */
224                 if (rte_lcore_is_enabled(lcore) == 0) {
225                         return -5;
226                 }
227
228                 if (lcore >= APP_MAX_LCORES) {
229                         return -6;
230                 }
231                 lp = &app.lcore_params[lcore];
232                 if (lp->type == e_APP_LCORE_WORKER) {
233                         return -7;
234                 }
235                 lp->type = e_APP_LCORE_IO;
236                 for (i = 0; i < lp->io.rx.n_nic_queues; i ++) {
237                         if ((lp->io.rx.nic_queues[i].port == port) &&
238                             (lp->io.rx.nic_queues[i].queue == queue)) {
239                                 return -8;
240                         }
241                 }
242                 if (lp->io.rx.n_nic_queues >= APP_MAX_NIC_RX_QUEUES_PER_IO_LCORE) {
243                         return -9;
244                 }
245                 lp->io.rx.nic_queues[lp->io.rx.n_nic_queues].port = (uint8_t) port;
246                 lp->io.rx.nic_queues[lp->io.rx.n_nic_queues].queue = (uint8_t) queue;
247                 lp->io.rx.n_nic_queues ++;
248
249                 n_tuples ++;
250                 if (n_tuples > APP_ARG_RX_MAX_TUPLES) {
251                         return -10;
252                 }
253         }
254
255         if (n_tuples == 0) {
256                 return -11;
257         }
258
259         return 0;
260 }
261
262 #ifndef APP_ARG_TX_MAX_CHARS
263 #define APP_ARG_TX_MAX_CHARS     4096
264 #endif
265
266 #ifndef APP_ARG_TX_MAX_TUPLES
267 #define APP_ARG_TX_MAX_TUPLES    128
268 #endif
269
270 static int
271 parse_arg_tx(const char *arg)
272 {
273         const char *p0 = arg, *p = arg;
274         uint32_t n_tuples;
275
276         if (strnlen(arg, APP_ARG_TX_MAX_CHARS + 1) == APP_ARG_TX_MAX_CHARS + 1) {
277                 return -1;
278         }
279
280         n_tuples = 0;
281         while ((p = strchr(p0,'(')) != NULL) {
282                 struct app_lcore_params *lp;
283                 uint32_t port, lcore, i;
284
285                 p0 = strchr(p++, ')');
286                 if ((p0 == NULL) ||
287                     (str_to_unsigned_vals(p, p0 - p, ',', 2, &port, &lcore) !=  2)) {
288                         return -2;
289                 }
290
291                 /* Enable port and queue for later initialization */
292                 if (port >= APP_MAX_NIC_PORTS) {
293                         return -3;
294                 }
295                 if (app.nic_tx_port_mask[port] != 0) {
296                         return -4;
297                 }
298                 app.nic_tx_port_mask[port] = 1;
299
300                 /* Check and assign (port, queue) to I/O lcore */
301                 if (rte_lcore_is_enabled(lcore) == 0) {
302                         return -5;
303                 }
304
305                 if (lcore >= APP_MAX_LCORES) {
306                         return -6;
307                 }
308                 lp = &app.lcore_params[lcore];
309                 if (lp->type == e_APP_LCORE_WORKER) {
310                         return -7;
311                 }
312                 lp->type = e_APP_LCORE_IO;
313                 for (i = 0; i < lp->io.tx.n_nic_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 = 0; /* 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                 for (i = 0; i < lp->rx.n_nic_queues; i ++) {
797                         if ((lp->rx.nic_queues[i].port == port) &&
798                             (lp->rx.nic_queues[i].queue == queue)) {
799                                 *lcore_out = lcore;
800                                 return 0;
801                         }
802                 }
803         }
804
805         return -1;
806 }
807
808 int
809 app_get_lcore_for_nic_tx(uint8_t port, uint32_t *lcore_out)
810 {
811         uint32_t lcore;
812
813         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
814                 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
815                 uint32_t i;
816
817                 if (app.lcore_params[lcore].type != e_APP_LCORE_IO) {
818                         continue;
819                 }
820
821                 for (i = 0; i < lp->tx.n_nic_ports; i ++) {
822                         if (lp->tx.nic_ports[i] == port) {
823                                 *lcore_out = lcore;
824                                 return 0;
825                         }
826                 }
827         }
828
829         return -1;
830 }
831
832 int
833 app_is_socket_used(uint32_t socket)
834 {
835         uint32_t lcore;
836
837         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
838                 if (app.lcore_params[lcore].type == e_APP_LCORE_DISABLED) {
839                         continue;
840                 }
841
842                 if (socket == rte_lcore_to_socket_id(lcore)) {
843                         return 1;
844                 }
845         }
846
847         return 0;
848 }
849
850 uint32_t
851 app_get_lcores_io_rx(void)
852 {
853         uint32_t lcore, count;
854
855         count = 0;
856         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
857                 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
858
859                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
860                     (lp_io->rx.n_nic_queues == 0)) {
861                         continue;
862                 }
863
864                 count ++;
865         }
866
867         return count;
868 }
869
870 uint32_t
871 app_get_lcores_worker(void)
872 {
873         uint32_t lcore, count;
874
875         count = 0;
876         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
877                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
878                         continue;
879                 }
880
881                 count ++;
882         }
883
884         if (count > APP_MAX_WORKER_LCORES) {
885                 rte_panic("Algorithmic error (too many worker lcores)\n");
886                 return 0;
887         }
888
889         return count;
890 }
891
892 void
893 app_print_params(void)
894 {
895         uint32_t port, queue, lcore, rule, i, j;
896
897         /* Print NIC RX configuration */
898         printf("NIC RX ports: ");
899         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
900                 uint32_t n_rx_queues = app_get_nic_rx_queues_per_port((uint8_t) port);
901
902                 if (n_rx_queues == 0) {
903                         continue;
904                 }
905
906                 printf("%u (", port);
907                 for (queue = 0; queue < APP_MAX_RX_QUEUES_PER_NIC_PORT; queue ++) {
908                         if (app.nic_rx_queue_mask[port][queue] == 1) {
909                                 printf("%u ", queue);
910                         }
911                 }
912                 printf(")  ");
913         }
914         printf(";\n");
915
916         /* Print I/O lcore RX params */
917         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
918                 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
919
920                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
921                     (lp->rx.n_nic_queues == 0)) {
922                         continue;
923                 }
924
925                 printf("I/O lcore %u (socket %u): ", lcore, rte_lcore_to_socket_id(lcore));
926
927                 printf("RX ports  ");
928                 for (i = 0; i < lp->rx.n_nic_queues; i ++) {
929                         printf("(%u, %u)  ",
930                                 (uint32_t) lp->rx.nic_queues[i].port,
931                                 (uint32_t) lp->rx.nic_queues[i].queue);
932                 }
933                 printf("; ");
934
935                 printf("Output rings  ");
936                 for (i = 0; i < lp->rx.n_rings; i ++) {
937                         printf("%p  ", lp->rx.rings[i]);
938                 }
939                 printf(";\n");
940         }
941
942         /* Print worker lcore RX params */
943         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
944                 struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
945
946                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
947                         continue;
948                 }
949
950                 printf("Worker lcore %u (socket %u) ID %u: ",
951                         lcore,
952                         rte_lcore_to_socket_id(lcore),
953                         lp->worker_id);
954
955                 printf("Input rings  ");
956                 for (i = 0; i < lp->n_rings_in; i ++) {
957                         printf("%p  ", lp->rings_in[i]);
958                 }
959
960                 printf(";\n");
961         }
962
963         printf("\n");
964
965         /* Print NIC TX configuration */
966         printf("NIC TX ports:  ");
967         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
968                 if (app.nic_tx_port_mask[port] == 1) {
969                         printf("%u  ", port);
970                 }
971         }
972         printf(";\n");
973
974         /* Print I/O TX lcore params */
975         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
976                 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
977                 uint32_t n_workers = app_get_lcores_worker();
978
979                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
980                      (lp->tx.n_nic_ports == 0)) {
981                         continue;
982                 }
983
984                 printf("I/O lcore %u (socket %u): ", lcore, rte_lcore_to_socket_id(lcore));
985
986                 printf("Input rings per TX port  ");
987                 for (i = 0; i < lp->tx.n_nic_ports; i ++) {
988                         port = lp->tx.nic_ports[i];
989
990                         printf("%u (", port);
991                         for (j = 0; j < n_workers; j ++) {
992                                 printf("%p  ", lp->tx.rings[port][j]);
993                         }
994                         printf(")  ");
995
996                 }
997
998                 printf(";\n");
999         }
1000
1001         /* Print worker lcore TX params */
1002         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
1003                 struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
1004
1005                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
1006                         continue;
1007                 }
1008
1009                 printf("Worker lcore %u (socket %u) ID %u: \n",
1010                         lcore,
1011                         rte_lcore_to_socket_id(lcore),
1012                         lp->worker_id);
1013
1014                 printf("Output rings per TX port  ");
1015                 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
1016                         if (lp->rings_out[port] != NULL) {
1017                                 printf("%u (%p)  ", port, lp->rings_out[port]);
1018                         }
1019                 }
1020
1021                 printf(";\n");
1022         }
1023
1024         /* Print LPM rules */
1025         printf("LPM rules: \n");
1026         for (rule = 0; rule < app.n_lpm_rules; rule ++) {
1027                 uint32_t ip = app.lpm_rules[rule].ip;
1028                 uint8_t depth = app.lpm_rules[rule].depth;
1029                 uint8_t if_out = app.lpm_rules[rule].if_out;
1030
1031                 printf("\t%u: %u.%u.%u.%u/%u => %u;\n",
1032                         rule,
1033                         (ip & 0xFF000000) >> 24,
1034                         (ip & 0x00FF0000) >> 16,
1035                         (ip & 0x0000FF00) >> 8,
1036                         ip & 0x000000FF,
1037                         (uint32_t) depth,
1038                         (uint32_t) if_out
1039                 );
1040         }
1041
1042         /* Rings */
1043         printf("Ring sizes: NIC RX = %u; Worker in = %u; Worker out = %u; NIC TX = %u;\n",
1044                 app.nic_rx_ring_size,
1045                 app.ring_rx_size,
1046                 app.ring_tx_size,
1047                 app.nic_tx_ring_size);
1048
1049         /* Bursts */
1050         printf("Burst sizes: I/O RX (rd = %u, wr = %u); Worker (rd = %u, wr = %u); I/O TX (rd = %u, wr = %u)\n",
1051                 app.burst_size_io_rx_read,
1052                 app.burst_size_io_rx_write,
1053                 app.burst_size_worker_read,
1054                 app.burst_size_worker_write,
1055                 app.burst_size_io_tx_read,
1056                 app.burst_size_io_tx_write);
1057 }