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