vmxnet3: get descriptor limits
[dpdk.git] / examples / ip_pipeline / config_parse.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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 #include <stdint.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <getopt.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <libgen.h>
42 #include <unistd.h>
43
44 #include <rte_errno.h>
45 #include <rte_cfgfile.h>
46 #include <rte_string_fns.h>
47
48 #include "app.h"
49
50 /**
51  * Default config values
52  **/
53
54 static struct app_params app_params_default = {
55         .config_file = "./config/ip_pipeline.cfg",
56         .log_level = APP_LOG_LEVEL_HIGH,
57
58         .eal_params = {
59                 .channels = 4,
60         },
61 };
62
63 static const struct app_mempool_params mempool_params_default = {
64         .parsed = 0,
65         .buffer_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
66         .pool_size = 32 * 1024,
67         .cache_size = 256,
68         .cpu_socket_id = 0,
69 };
70
71 static const struct app_link_params link_params_default = {
72         .parsed = 0,
73         .pmd_id = 0,
74         .arp_q = 0,
75         .tcp_syn_local_q = 0,
76         .ip_local_q = 0,
77         .tcp_local_q = 0,
78         .udp_local_q = 0,
79         .sctp_local_q = 0,
80         .state = 0,
81         .ip = 0,
82         .depth = 0,
83         .mac_addr = 0,
84
85         .conf = {
86                 .link_speed = 0,
87                 .link_duplex = 0,
88                 .rxmode = {
89                         .mq_mode = ETH_MQ_RX_NONE,
90
91                         .header_split   = 0, /* Header split */
92                         .hw_ip_checksum = 0, /* IP checksum offload */
93                         .hw_vlan_filter = 0, /* VLAN filtering */
94                         .hw_vlan_strip  = 0, /* VLAN strip */
95                         .hw_vlan_extend = 0, /* Extended VLAN */
96                         .jumbo_frame    = 0, /* Jumbo frame support */
97                         .hw_strip_crc   = 0, /* CRC strip by HW */
98                         .enable_scatter = 0, /* Scattered packets RX handler */
99
100                         .max_rx_pkt_len = 9000, /* Jumbo frame max packet len */
101                         .split_hdr_size = 0, /* Header split buffer size */
102                 },
103                 .txmode = {
104                         .mq_mode = ETH_MQ_TX_NONE,
105                 },
106                 .lpbk_mode = 0,
107         },
108
109         .promisc = 1,
110 };
111
112 static const struct app_pktq_hwq_in_params default_hwq_in_params = {
113         .parsed = 0,
114         .mempool_id = 0,
115         .size = 128,
116         .burst = 32,
117
118         .conf = {
119                 .rx_thresh = {
120                                 .pthresh = 8,
121                                 .hthresh = 8,
122                                 .wthresh = 4,
123                 },
124                 .rx_free_thresh = 64,
125                 .rx_drop_en = 0,
126                 .rx_deferred_start = 0,
127         }
128 };
129
130 static const struct app_pktq_hwq_out_params default_hwq_out_params = {
131         .parsed = 0,
132         .size = 512,
133         .burst = 32,
134         .dropless = 0,
135         .n_retries = 0,
136
137         .conf = {
138                 .tx_thresh = {
139                         .pthresh = 36,
140                         .hthresh = 0,
141                         .wthresh = 0,
142                 },
143                 .tx_rs_thresh = 0,
144                 .tx_free_thresh = 0,
145                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
146                         ETH_TXQ_FLAGS_NOOFFLOADS,
147                 .tx_deferred_start = 0,
148         }
149 };
150
151 static const struct app_pktq_swq_params default_swq_params = {
152         .parsed = 0,
153         .size = 256,
154         .burst_read = 32,
155         .burst_write = 32,
156         .dropless = 0,
157         .n_retries = 0,
158         .cpu_socket_id = 0,
159 };
160
161 struct app_pktq_tm_params default_tm_params = {
162         .parsed = 0,
163         .file_name = "./config/tm_profile.cfg",
164         .burst_read = 64,
165         .burst_write = 32,
166 };
167
168 struct app_pktq_source_params default_source_params = {
169         .parsed = 0,
170         .mempool_id = 0,
171         .burst = 32,
172 };
173
174 struct app_pktq_sink_params default_sink_params = {
175         .parsed = 0,
176 };
177
178 struct app_msgq_params default_msgq_params = {
179         .parsed = 0,
180         .size = 64,
181         .cpu_socket_id = 0,
182 };
183
184 struct app_pipeline_params default_pipeline_params = {
185         .parsed = 0,
186         .socket_id = 0,
187         .core_id = 0,
188         .hyper_th_id = 0,
189         .n_pktq_in = 0,
190         .n_pktq_out = 0,
191         .n_msgq_in = 0,
192         .n_msgq_out = 0,
193         .timer_period = 1,
194         .n_args = 0,
195 };
196
197 static const char app_usage[] =
198         "Usage: %s [-f CONFIG_FILE] [-s SCRIPT_FILE] -p PORT_MASK "
199         "[-l LOG_LEVEL]\n"
200         "\n"
201         "Arguments:\n"
202         "\t-f CONFIG_FILE: Default config file is %s\n"
203         "\t-p PORT_MASK: Mask of NIC port IDs in hexadecimal format\n"
204         "\t-s SCRIPT_FILE: No CLI script file is run when not specified\n"
205         "\t-l LOG_LEVEL: 0 = NONE, 1 = HIGH PRIO (default), 2 = LOW PRIO\n"
206         "\n";
207
208 static void
209 app_print_usage(char *prgname)
210 {
211         rte_exit(0, app_usage, prgname, app_params_default.config_file);
212 }
213
214 #define skip_white_spaces(pos)                  \
215 ({                                              \
216         __typeof__(pos) _p = (pos);             \
217         for ( ; isspace(*_p); _p++);            \
218         _p;                                     \
219 })
220
221 #define PARSER_IMPLICIT_PARAM_ADD_CHECK(result, section_name)           \
222 do {                                                                    \
223         APP_CHECK((result != -EINVAL),                                  \
224                 "CFG: [%s] name too long", section_name);               \
225         APP_CHECK(result != -ENOMEM,                                    \
226                 "CFG: [%s] too much sections", section_name);           \
227         APP_CHECK(result >= 0,                                          \
228                 "CFG: [%s] Unknown error while adding '%s'",            \
229                 section_name, section_name);                            \
230 } while (0)
231
232 #define PARSER_PARAM_ADD_CHECK(result, params_array, section_name)      \
233 do {                                                                    \
234         APP_CHECK((result != -EINVAL),                                  \
235                 "CFG: [%s] name too long", section_name);               \
236         APP_CHECK((result != -ENOMEM),                                  \
237                 "CFG: [%s] too much sections", section_name);           \
238         APP_CHECK(((result >= 0) && (params_array)[result].parsed == 0),\
239                 "CFG: [%s] duplicate section", section_name);           \
240         APP_CHECK((result >= 0),                                        \
241                 "CFG: [%s] Unknown error while adding '%s'",            \
242                 section_name, section_name);                            \
243 } while (0)
244
245 static int
246 parser_read_arg_bool(const char *p)
247 {
248         p = skip_white_spaces(p);
249         int result = -EINVAL;
250
251         if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) ||
252                 ((p[0] == 'Y') && (p[1] == 'E') && (p[2] == 'S'))) {
253                 p += 3;
254                 result = 1;
255         }
256
257         if (((p[0] == 'o') && (p[1] == 'n')) ||
258                 ((p[0] == 'O') && (p[1] == 'N'))) {
259                 p += 2;
260                 result = 1;
261         }
262
263         if (((p[0] == 'n') && (p[1] == 'o')) ||
264                 ((p[0] == 'N') && (p[1] == 'O'))) {
265                 p += 2;
266                 result = 0;
267         }
268
269         if (((p[0] == 'o') && (p[1] == 'f') && (p[2] == 'f')) ||
270                 ((p[0] == 'O') && (p[1] == 'F') && (p[2] == 'F'))) {
271                 p += 3;
272                 result = 0;
273         }
274
275         p = skip_white_spaces(p);
276
277         if (p[0] != '\0')
278                 return -EINVAL;
279
280         return result;
281 }
282
283 #define PARSE_ERROR(exp, section, entry)                                \
284 APP_CHECK(exp, "Parse error in section \"%s\": entry \"%s\"\n", section, entry)
285
286 #define PARSE_ERROR_MALLOC(exp)                                         \
287 APP_CHECK(exp, "Parse error: no free memory\n")
288
289 #define PARSE_ERROR_SECTION(exp, section)                               \
290 APP_CHECK(exp, "Parse error in section \"%s\"", section)
291
292 #define PARSE_ERROR_SECTION_NO_ENTRIES(exp, section)                    \
293 APP_CHECK(exp, "Parse error in section \"%s\": no entries\n", section)
294
295 #define PARSE_WARNING_IGNORED(exp, section, entry)                      \
296 do                                                                      \
297 if (!(exp))                                                             \
298         fprintf(stderr, "Parse warning in section \"%s\": "             \
299                 "entry \"%s\" is ignored\n", section, entry);           \
300 while (0)
301
302 #define PARSE_ERROR_INVALID(exp, section, entry)                        \
303 APP_CHECK(exp, "Parse error in section \"%s\": unrecognized entry \"%s\"\n",\
304         section, entry)
305
306 #define PARSE_ERROR_DUPLICATE(exp, section, entry)                      \
307 APP_CHECK(exp, "Parse error in section \"%s\": duplicate entry \"%s\"\n",\
308         section, entry)
309
310 static int
311 parser_read_uint64(uint64_t *value, const char *p)
312 {
313         char *next;
314         uint64_t val;
315
316         p = skip_white_spaces(p);
317         if (!isdigit(*p))
318                 return -EINVAL;
319
320         val = strtoul(p, &next, 10);
321         if (p == next)
322                 return -EINVAL;
323
324         p = next;
325         switch (*p) {
326         case 'T':
327                 val *= 1024ULL;
328                 /* fall trought */
329         case 'G':
330                 val *= 1024ULL;
331                 /* fall trought */
332         case 'M':
333                 val *= 1024ULL;
334                 /* fall trought */
335         case 'k':
336         case 'K':
337                 val *= 1024ULL;
338                 p++;
339                 break;
340         }
341
342         p = skip_white_spaces(p);
343         if (*p != '\0')
344                 return -EINVAL;
345
346         *value = val;
347         return 0;
348 }
349
350 static int
351 parser_read_uint32(uint32_t *value, const char *p)
352 {
353         uint64_t val = 0;
354         int ret = parser_read_uint64(&val, p);
355
356         if (ret < 0)
357                 return ret;
358         else if (val > UINT32_MAX)
359                 return -ERANGE;
360
361         *value = val;
362         return 0;
363 }
364
365 static int
366 parse_pipeline_core(uint32_t *socket,
367         uint32_t *core,
368         uint32_t *ht,
369         const char *entry)
370 {
371         size_t num_len;
372         char num[8];
373
374         uint32_t s = 0, c = 0, h = 0, val;
375         uint8_t s_parsed = 0, c_parsed = 0, h_parsed = 0;
376         const char *next = skip_white_spaces(entry);
377         char type;
378
379         /* Expect <CORE> or [sX][cY][h]. At least one parameter is required. */
380         while (*next != '\0') {
381                 /* If everything parsed nothing should left */
382                 if (s_parsed && c_parsed && h_parsed)
383                         return -EINVAL;
384
385                 type = *next;
386                 switch (type) {
387                 case 's':
388                 case 'S':
389                         if (s_parsed || c_parsed || h_parsed)
390                                 return -EINVAL;
391                         s_parsed = 1;
392                         next++;
393                         break;
394                 case 'c':
395                 case 'C':
396                         if (c_parsed || h_parsed)
397                                 return -EINVAL;
398                         c_parsed = 1;
399                         next++;
400                         break;
401                 case 'h':
402                 case 'H':
403                         if (h_parsed)
404                                 return -EINVAL;
405                         h_parsed = 1;
406                         next++;
407                         break;
408                 default:
409                         /* If it start from digit it must be only core id. */
410                         if (!isdigit(*next) || s_parsed || c_parsed || h_parsed)
411                                 return -EINVAL;
412
413                         type = 'C';
414                 }
415
416                 for (num_len = 0; *next != '\0'; next++, num_len++) {
417                         if (num_len == RTE_DIM(num))
418                                 return -EINVAL;
419
420                         if (!isdigit(*next))
421                                 break;
422
423                         num[num_len] = *next;
424                 }
425
426                 if (num_len == 0 && type != 'h' && type != 'H')
427                         return -EINVAL;
428
429                 if (num_len != 0 && (type == 'h' || type == 'H'))
430                         return -EINVAL;
431
432                 num[num_len] = '\0';
433                 val = strtol(num, NULL, 10);
434
435                 h = 0;
436                 switch (type) {
437                 case 's':
438                 case 'S':
439                         s = val;
440                         break;
441                 case 'c':
442                 case 'C':
443                         c = val;
444                         break;
445                 case 'h':
446                 case 'H':
447                         h = 1;
448                         break;
449                 }
450         }
451
452         *socket = s;
453         *core = c;
454         *ht = h;
455         return 0;
456 }
457
458 static size_t
459 skip_digits(const char *src)
460 {
461         size_t i;
462
463         for (i = 0; isdigit(src[i]); i++);
464
465         return i;
466 }
467
468 static int
469 validate_name(const char *name, const char *prefix, int num)
470 {
471         size_t i, j;
472
473         for (i = 0; (name[i] != '\0') && (prefix[i] != '\0'); i++) {
474                 if (name[i] != prefix[i])
475                         return -1;
476         }
477
478         if (prefix[i] != '\0')
479                 return -1;
480
481         if (!num) {
482                 if (name[i] != '\0')
483                         return -1;
484                 else
485                         return 0;
486         }
487
488         if (num == 2) {
489                 j = skip_digits(&name[i]);
490                 i += j;
491                 if ((j == 0) || (name[i] != '.'))
492                         return -1;
493                 i++;
494         }
495
496         if (num == 1) {
497                 j = skip_digits(&name[i]);
498                 i += j;
499                 if ((j == 0) || (name[i] != '\0'))
500                         return -1;
501         }
502
503         return 0;
504 }
505
506 static void
507 parse_eal(struct app_params *app,
508         const char *section_name,
509         struct rte_cfgfile *cfg)
510 {
511         struct app_eal_params *p = &app->eal_params;
512         struct rte_cfgfile_entry *entries;
513         int n_entries, i;
514
515         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
516         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
517
518         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
519         PARSE_ERROR_MALLOC(entries != NULL);
520
521         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
522
523         for (i = 0; i < n_entries; i++) {
524                 struct rte_cfgfile_entry *entry = &entries[i];
525
526                 /* coremask */
527                 if (strcmp(entry->name, "c") == 0) {
528                         PARSE_WARNING_IGNORED(0, section_name, entry->name);
529                         continue;
530                 }
531
532                 /* corelist */
533                 if (strcmp(entry->name, "l") == 0) {
534                         PARSE_WARNING_IGNORED(0, section_name, entry->name);
535                         continue;
536                 }
537
538                 /* coremap */
539                 if (strcmp(entry->name, "lcores") == 0) {
540                         PARSE_ERROR_DUPLICATE((p->coremap == NULL),
541                                 section_name,
542                                 entry->name);
543                         p->coremap = strdup(entry->value);
544                         continue;
545                 }
546
547                 /* master_lcore */
548                 if (strcmp(entry->name, "master_lcore") == 0) {
549                         int status;
550
551                         PARSE_ERROR_DUPLICATE((p->master_lcore_present == 0),
552                                 section_name,
553                                 entry->name);
554                         p->master_lcore_present = 1;
555
556                         status = parser_read_uint32(&p->master_lcore,
557                                 entry->value);
558                         PARSE_ERROR((status == 0), section_name, entry->name);
559                         continue;
560                 }
561
562                 /* channels */
563                 if (strcmp(entry->name, "n") == 0) {
564                         int status;
565
566                         PARSE_ERROR_DUPLICATE((p->channels_present == 0),
567                                 section_name,
568                                 entry->name);
569                         p->channels_present = 1;
570
571                         status = parser_read_uint32(&p->channels, entry->value);
572                         PARSE_ERROR((status == 0), section_name, entry->name);
573                         continue;
574                 }
575
576                 /* memory */
577                 if (strcmp(entry->name, "m") == 0) {
578                         int status;
579
580                         PARSE_ERROR_DUPLICATE((p->memory_present == 0),
581                                 section_name,
582                                 entry->name);
583                         p->memory_present = 1;
584
585                         status = parser_read_uint32(&p->memory, entry->value);
586                         PARSE_ERROR((status == 0), section_name, entry->name);
587                         continue;
588                 }
589
590                 /* ranks */
591                 if (strcmp(entry->name, "r") == 0) {
592                         int status;
593
594                         PARSE_ERROR_DUPLICATE((p->ranks_present == 0),
595                                 section_name,
596                                 entry->name);
597                         p->ranks_present = 1;
598
599                         status = parser_read_uint32(&p->ranks, entry->value);
600                         PARSE_ERROR((status == 0), section_name, entry->name);
601                         continue;
602                 }
603
604                 /* pci_blacklist */
605                 if ((strcmp(entry->name, "pci_blacklist") == 0) ||
606                         (strcmp(entry->name, "b") == 0)) {
607                         PARSE_ERROR_DUPLICATE((p->pci_blacklist == NULL),
608                                 section_name,
609                                 entry->name);
610                         p->pci_blacklist = strdup(entry->value);
611                         continue;
612                 }
613
614                 /* pci_whitelist */
615                 if ((strcmp(entry->name, "pci_whitelist") == 0) ||
616                         (strcmp(entry->name, "w") == 0)) {
617                         PARSE_ERROR_DUPLICATE((p->pci_whitelist == NULL),
618                                 section_name,
619                                 entry->name);
620                         p->pci_whitelist = strdup(entry->value);
621                         continue;
622                 }
623
624                 /* vdev */
625                 if (strcmp(entry->name, "vdev") == 0) {
626                         PARSE_ERROR_DUPLICATE((p->vdev == NULL),
627                                 section_name,
628                                 entry->name);
629                         p->vdev = strdup(entry->value);
630                         continue;
631                 }
632
633                 /* vmware_tsc_map */
634                 if (strcmp(entry->name, "vmware_tsc_map") == 0) {
635                         int val;
636
637                         PARSE_ERROR_DUPLICATE((p->vmware_tsc_map_present == 0),
638                                 section_name,
639                                 entry->name);
640                         p->vmware_tsc_map_present = 1;
641
642                         val = parser_read_arg_bool(entry->value);
643                         PARSE_ERROR((val >= 0), section_name, entry->name);
644                         p->vmware_tsc_map = val;
645                         continue;
646                 }
647
648                 /* proc_type */
649                 if (strcmp(entry->name, "proc_type") == 0) {
650                         PARSE_ERROR_DUPLICATE((p->proc_type == NULL),
651                                 section_name,
652                                 entry->name);
653                         p->proc_type = strdup(entry->value);
654                         continue;
655                 }
656
657                 /* syslog */
658                 if (strcmp(entry->name, "syslog") == 0) {
659                         PARSE_ERROR_DUPLICATE((p->syslog == NULL),
660                                 section_name,
661                                 entry->name);
662                         p->syslog = strdup(entry->value);
663                         continue;
664                 }
665
666                 /* log_level */
667                 if (strcmp(entry->name, "log_level") == 0) {
668                         int status;
669
670                         PARSE_ERROR_DUPLICATE((p->log_level_present == 0),
671                                 section_name,
672                                 entry->name);
673                         p->log_level_present = 1;
674
675                         status = parser_read_uint32(&p->log_level,
676                                 entry->value);
677                         PARSE_ERROR((status == 0), section_name, entry->name);
678                         continue;
679                 }
680
681                 /* version */
682                 if (strcmp(entry->name, "v") == 0) {
683                         int val;
684
685                         PARSE_ERROR_DUPLICATE((p->version_present == 0),
686                                 section_name,
687                                 entry->name);
688                         p->version_present = 1;
689
690                         val = parser_read_arg_bool(entry->value);
691                         PARSE_ERROR((val >= 0), section_name, entry->name);
692                         p->version = val;
693                         continue;
694                 }
695
696                 /* help */
697                 if ((strcmp(entry->name, "help") == 0) ||
698                         (strcmp(entry->name, "h") == 0)) {
699                         int val;
700
701                         PARSE_ERROR_DUPLICATE((p->help_present == 0),
702                                 section_name,
703                                 entry->name);
704                         p->help_present = 1;
705
706                         val = parser_read_arg_bool(entry->value);
707                         PARSE_ERROR((val >= 0), section_name, entry->name);
708                         p->help = val;
709                         continue;
710                 }
711
712                 /* no_huge */
713                 if (strcmp(entry->name, "no_huge") == 0) {
714                         int val;
715
716                         PARSE_ERROR_DUPLICATE((p->no_huge_present == 0),
717                                 section_name,
718                                 entry->name);
719                         p->no_huge_present = 1;
720
721                         val = parser_read_arg_bool(entry->value);
722                         PARSE_ERROR((val >= 0), section_name, entry->name);
723                         p->no_huge = val;
724                         continue;
725                 }
726
727                 /* no_pci */
728                 if (strcmp(entry->name, "no_pci") == 0) {
729                         int val;
730
731                         PARSE_ERROR_DUPLICATE((p->no_pci_present == 0),
732                                 section_name,
733                                 entry->name);
734                         p->no_pci_present = 1;
735
736                         val = parser_read_arg_bool(entry->value);
737                         PARSE_ERROR((val >= 0), section_name, entry->name);
738                         p->no_pci = val;
739                         continue;
740                 }
741
742                 /* no_hpet */
743                 if (strcmp(entry->name, "no_hpet") == 0) {
744                         int val;
745
746                         PARSE_ERROR_DUPLICATE((p->no_hpet_present == 0),
747                                 section_name,
748                                 entry->name);
749                         p->no_hpet_present = 1;
750
751                         val = parser_read_arg_bool(entry->value);
752                         PARSE_ERROR((val >= 0), section_name, entry->name);
753                         p->no_hpet = val;
754                         continue;
755                 }
756
757                 /* no_shconf */
758                 if (strcmp(entry->name, "no_shconf") == 0) {
759                         int val;
760
761                         PARSE_ERROR_DUPLICATE((p->no_shconf_present == 0),
762                                 section_name,
763                                 entry->name);
764                         p->no_shconf_present = 1;
765
766                         val = parser_read_arg_bool(entry->value);
767                         PARSE_ERROR((val >= 0), section_name, entry->name);
768                         p->no_shconf = val;
769                         continue;
770                 }
771
772                 /* add_driver */
773                 if (strcmp(entry->name, "d") == 0) {
774                         PARSE_ERROR_DUPLICATE((p->add_driver == NULL),
775                                 section_name,
776                                 entry->name);
777                         p->add_driver = strdup(entry->value);
778                         continue;
779                 }
780
781                 /* socket_mem */
782                 if (strcmp(entry->name, "socket_mem") == 0) {
783                         PARSE_ERROR_DUPLICATE((p->socket_mem == NULL),
784                                 section_name,
785                                 entry->name);
786                         p->socket_mem = strdup(entry->value);
787                         continue;
788                 }
789
790                 /* huge_dir */
791                 if (strcmp(entry->name, "huge_dir") == 0) {
792                         PARSE_ERROR_DUPLICATE((p->huge_dir == NULL),
793                                 section_name,
794                                 entry->name);
795                         p->huge_dir = strdup(entry->value);
796                         continue;
797                 }
798
799                 /* file_prefix */
800                 if (strcmp(entry->name, "file_prefix") == 0) {
801                         PARSE_ERROR_DUPLICATE((p->file_prefix == NULL),
802                                 section_name,
803                                 entry->name);
804                         p->file_prefix = strdup(entry->value);
805                         continue;
806                 }
807
808                 /* base_virtaddr */
809                 if (strcmp(entry->name, "base_virtaddr") == 0) {
810                         PARSE_ERROR_DUPLICATE((p->base_virtaddr == NULL),
811                                 section_name,
812                                 entry->name);
813                         p->base_virtaddr = strdup(entry->value);
814                         continue;
815                 }
816
817                 /* create_uio_dev */
818                 if (strcmp(entry->name, "create_uio_dev") == 0) {
819                         int val;
820
821                         PARSE_ERROR_DUPLICATE((p->create_uio_dev_present == 0),
822                                 section_name,
823                                 entry->name);
824                         p->create_uio_dev_present = 1;
825
826                         val = parser_read_arg_bool(entry->value);
827                         PARSE_ERROR((val >= 0), section_name, entry->name);
828                         p->create_uio_dev = val;
829                         continue;
830                 }
831
832                 /* vfio_intr */
833                 if (strcmp(entry->name, "vfio_intr") == 0) {
834                         PARSE_ERROR_DUPLICATE((p->vfio_intr == NULL),
835                                 section_name,
836                                 entry->name);
837                         p->vfio_intr = strdup(entry->value);
838                         continue;
839                 }
840
841                 /* xen_dom0 */
842                 if (strcmp(entry->name, "xen_dom0") == 0) {
843                         int val;
844
845                         PARSE_ERROR_DUPLICATE((p->xen_dom0_present == 0),
846                                 section_name,
847                                 entry->name);
848                         p->xen_dom0_present = 1;
849
850                         val = parser_read_arg_bool(entry->value);
851                         PARSE_ERROR((val >= 0), section_name, entry->name);
852                         p->xen_dom0 = val;
853                         continue;
854                 }
855
856                 /* unrecognized */
857                 PARSE_ERROR_INVALID(0, section_name, entry->name);
858         }
859
860         free(entries);
861 }
862
863 static int
864 parse_pipeline_pktq_in(struct app_params *app,
865         struct app_pipeline_params *p,
866         const char *value)
867 {
868         const char *next = value;
869         char *end;
870         char name[APP_PARAM_NAME_SIZE];
871         size_t name_len;
872
873         while (*next != '\0') {
874                 enum app_pktq_in_type type;
875                 int id;
876
877                 end = strchr(next, ' ');
878                 if (!end)
879                         name_len = strlen(next);
880                 else
881                         name_len = end - next;
882
883                 if (name_len == 0 || name_len == sizeof(name))
884                         return -EINVAL;
885
886                 strncpy(name, next, name_len);
887                 name[name_len] = '\0';
888                 next += name_len;
889                 if (*next != '\0')
890                         next++;
891
892                 if (validate_name(name, "RXQ", 2) == 0) {
893                         type = APP_PKTQ_IN_HWQ;
894                         id = APP_PARAM_ADD(app->hwq_in_params, name);
895                 } else if (validate_name(name, "SWQ", 1) == 0) {
896                         type = APP_PKTQ_IN_SWQ;
897                         id = APP_PARAM_ADD(app->swq_params, name);
898                 } else if (validate_name(name, "TM", 1) == 0) {
899                         type = APP_PKTQ_IN_TM;
900                         id = APP_PARAM_ADD(app->tm_params, name);
901                 } else if (validate_name(name, "SOURCE", 1) == 0) {
902                         type = APP_PKTQ_IN_SOURCE;
903                         id = APP_PARAM_ADD(app->source_params, name);
904                 } else
905                         return -EINVAL;
906
907                 if (id < 0)
908                         return id;
909
910                 p->pktq_in[p->n_pktq_in].type = type;
911                 p->pktq_in[p->n_pktq_in].id = (uint32_t) id;
912                 p->n_pktq_in++;
913         }
914
915         return 0;
916 }
917
918 static int
919 parse_pipeline_pktq_out(struct app_params *app,
920         struct app_pipeline_params *p,
921         const char *value)
922 {
923         const char *next = value;
924         char *end;
925         char name[APP_PARAM_NAME_SIZE];
926         size_t name_len;
927
928         while (*next != '\0') {
929                 enum app_pktq_out_type type;
930                 int id;
931
932                 end = strchr(next, ' ');
933                 if (!end)
934                         name_len = strlen(next);
935                 else
936                         name_len = end - next;
937
938                 if (name_len == 0 || name_len == sizeof(name))
939                         return -EINVAL;
940
941                 strncpy(name, next, name_len);
942                 name[name_len] = '\0';
943                 next += name_len;
944                 if (*next != '\0')
945                         next++;
946
947                 if (validate_name(name, "TXQ", 2) == 0) {
948                         type = APP_PKTQ_OUT_HWQ;
949                         id = APP_PARAM_ADD(app->hwq_out_params, name);
950                 } else if (validate_name(name, "SWQ", 1) == 0) {
951                         type = APP_PKTQ_OUT_SWQ;
952                         id = APP_PARAM_ADD(app->swq_params, name);
953                 } else if (validate_name(name, "TM", 1) == 0) {
954                         type = APP_PKTQ_OUT_TM;
955                         id = APP_PARAM_ADD(app->tm_params, name);
956                 } else if (validate_name(name, "SINK", 1) == 0) {
957                         type = APP_PKTQ_OUT_SINK;
958                         id = APP_PARAM_ADD(app->sink_params, name);
959                 } else
960                         return -EINVAL;
961
962                 if (id < 0)
963                         return id;
964
965                 p->pktq_out[p->n_pktq_out].type = type;
966                 p->pktq_out[p->n_pktq_out].id = id;
967                 p->n_pktq_out++;
968         }
969
970         return 0;
971 }
972
973 static int
974 parse_pipeline_msgq_in(struct app_params *app,
975         struct app_pipeline_params *p,
976         const char *value)
977 {
978         const char *next = value;
979         char *end;
980         char name[APP_PARAM_NAME_SIZE];
981         size_t name_len;
982         ssize_t idx;
983
984         while (*next != '\0') {
985                 end = strchr(next, ' ');
986                 if (!end)
987                         name_len = strlen(next);
988                 else
989                         name_len = end - next;
990
991                 if (name_len == 0 || name_len == sizeof(name))
992                         return -EINVAL;
993
994                 strncpy(name, next, name_len);
995                 name[name_len] = '\0';
996                 next += name_len;
997                 if (*next != '\0')
998                         next++;
999
1000                 if (validate_name(name, "MSGQ", 1) != 0)
1001                         return -EINVAL;
1002
1003                 idx = APP_PARAM_ADD(app->msgq_params, name);
1004                 if (idx < 0)
1005                         return idx;
1006
1007                 p->msgq_in[p->n_msgq_in] = idx;
1008                 p->n_msgq_in++;
1009         }
1010
1011         return 0;
1012 }
1013
1014 static int
1015 parse_pipeline_msgq_out(struct app_params *app,
1016         struct app_pipeline_params *p,
1017         const char *value)
1018 {
1019         const char *next = value;
1020         char *end;
1021         char name[APP_PARAM_NAME_SIZE];
1022         size_t name_len;
1023         ssize_t idx;
1024
1025         while (*next != '\0') {
1026                 end = strchr(next, ' ');
1027                 if (!end)
1028                         name_len = strlen(next);
1029                 else
1030                         name_len = end - next;
1031
1032                 if (name_len == 0 || name_len == sizeof(name))
1033                         return -EINVAL;
1034
1035                 strncpy(name, next, name_len);
1036                 name[name_len] = '\0';
1037                 next += name_len;
1038                 if (*next != '\0')
1039                         next++;
1040
1041                 if (validate_name(name, "MSGQ", 1) != 0)
1042                         return -EINVAL;
1043
1044                 idx = APP_PARAM_ADD(app->msgq_params, name);
1045                 if (idx < 0)
1046                         return idx;
1047
1048                 p->msgq_out[p->n_msgq_out] = idx;
1049                 p->n_msgq_out++;
1050         }
1051
1052         return 0;
1053 }
1054
1055
1056 static void
1057 parse_pipeline(struct app_params *app,
1058         const char *section_name,
1059         struct rte_cfgfile *cfg)
1060 {
1061         char name[CFG_NAME_LEN];
1062         struct app_pipeline_params *param;
1063         struct rte_cfgfile_entry *entries;
1064         ssize_t param_idx;
1065         int n_entries, ret, i;
1066
1067         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1068         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1069
1070         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1071         PARSE_ERROR_MALLOC(entries != NULL);
1072
1073         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1074
1075         param_idx = APP_PARAM_ADD(app->pipeline_params, section_name);
1076         PARSER_PARAM_ADD_CHECK(param_idx, app->pipeline_params, section_name);
1077
1078         param = &app->pipeline_params[param_idx];
1079         param->parsed = 1;
1080
1081         for (i = 0; i < n_entries; i++) {
1082                 struct rte_cfgfile_entry *ent = &entries[i];
1083
1084                 if (strcmp(ent->name, "type") == 0) {
1085                         ret = snprintf(param->type,
1086                                 RTE_DIM(param->type),
1087                                 "%s",
1088                                 ent->value);
1089                         if ((ret > 0) && (ret < (int)RTE_DIM(param->type)))
1090                                 ret = 0;
1091                         else
1092                                 ret = -EINVAL;
1093                 } else if (strcmp(ent->name, "core") == 0)
1094                         ret = parse_pipeline_core(&param->socket_id,
1095                                 &param->core_id,
1096                                 &param->hyper_th_id,
1097                                 ent->value);
1098                 else if (strcmp(ent->name, "pktq_in") == 0)
1099                         ret = parse_pipeline_pktq_in(app, param, ent->value);
1100                 else if (strcmp(ent->name, "pktq_out") == 0)
1101                         ret = parse_pipeline_pktq_out(app, param, ent->value);
1102                 else if (strcmp(ent->name, "msgq_in") == 0)
1103                         ret = parse_pipeline_msgq_in(app, param, ent->value);
1104                 else if (strcmp(ent->name, "msgq_out") == 0)
1105                         ret = parse_pipeline_msgq_out(app, param, ent->value);
1106                 else if (strcmp(ent->name, "timer_period") == 0)
1107                         ret = parser_read_uint32(&param->timer_period,
1108                                 ent->value);
1109                 else {
1110                         param->args_name[param->n_args] = strdup(ent->name);
1111                         param->args_value[param->n_args] = strdup(ent->value);
1112
1113                         APP_CHECK((param->args_name[param->n_args] != NULL) &&
1114                                 (param->args_value[param->n_args] != NULL),
1115                                 "CFG: [%s] out of memory",
1116                                 section_name);
1117
1118                         param->n_args++;
1119                         ret = 0;
1120                 }
1121
1122                 APP_CHECK(ret == 0,
1123                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1124                         section_name,
1125                         ent->name,
1126                         ent->value);
1127         }
1128
1129         snprintf(name, sizeof(name), "MSGQ-REQ-%s", section_name);
1130         param_idx = APP_PARAM_ADD(app->msgq_params, name);
1131         PARSER_IMPLICIT_PARAM_ADD_CHECK(param_idx, name);
1132         app->msgq_params[param_idx].cpu_socket_id = param->socket_id;
1133         param->msgq_in[param->n_msgq_in++] = param_idx;
1134
1135         snprintf(name, sizeof(name), "MSGQ-RSP-%s", section_name);
1136         param_idx = APP_PARAM_ADD(app->msgq_params, name);
1137         PARSER_IMPLICIT_PARAM_ADD_CHECK(param_idx, name);
1138         app->msgq_params[param_idx].cpu_socket_id = param->socket_id;
1139         param->msgq_out[param->n_msgq_out++] = param_idx;
1140
1141         snprintf(name, sizeof(name), "MSGQ-REQ-CORE-s%" PRIu32 "c%" PRIu32 "%s",
1142                 param->socket_id,
1143                 param->core_id,
1144                 (param->hyper_th_id) ? "h" : "");
1145         param_idx = APP_PARAM_ADD(app->msgq_params, name);
1146         PARSER_IMPLICIT_PARAM_ADD_CHECK(param_idx, name);
1147         app->msgq_params[param_idx].cpu_socket_id = param->socket_id;
1148
1149         snprintf(name, sizeof(name), "MSGQ-RSP-CORE-s%" PRIu32 "c%" PRIu32 "%s",
1150                 param->socket_id,
1151                 param->core_id,
1152                 (param->hyper_th_id) ? "h" : "");
1153         param_idx = APP_PARAM_ADD(app->msgq_params, name);
1154         PARSER_IMPLICIT_PARAM_ADD_CHECK(param_idx, name);
1155         app->msgq_params[param_idx].cpu_socket_id = param->socket_id;
1156
1157         free(entries);
1158 }
1159
1160 static void
1161 parse_mempool(struct app_params *app,
1162         const char *section_name,
1163         struct rte_cfgfile *cfg)
1164 {
1165         struct app_mempool_params *param;
1166         struct rte_cfgfile_entry *entries;
1167         ssize_t param_idx;
1168         int n_entries, ret, i;
1169
1170         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1171         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1172
1173         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1174         PARSE_ERROR_MALLOC(entries != NULL);
1175
1176         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1177
1178         param_idx = APP_PARAM_ADD(app->mempool_params, section_name);
1179         PARSER_PARAM_ADD_CHECK(param_idx, app->mempool_params, section_name);
1180
1181         param = &app->mempool_params[param_idx];
1182         param->parsed = 1;
1183
1184         for (i = 0; i < n_entries; i++) {
1185                 struct rte_cfgfile_entry *ent = &entries[i];
1186
1187                 ret = -ESRCH;
1188                 if (strcmp(ent->name, "buffer_size") == 0)
1189                         ret = parser_read_uint32(&param->buffer_size,
1190                                 ent->value);
1191                 else if (strcmp(ent->name, "pool_size") == 0)
1192                         ret = parser_read_uint32(&param->pool_size,
1193                                 ent->value);
1194                 else if (strcmp(ent->name, "cache_size") == 0)
1195                         ret = parser_read_uint32(&param->cache_size,
1196                                 ent->value);
1197                 else if (strcmp(ent->name, "cpu") == 0)
1198                         ret = parser_read_uint32(&param->cpu_socket_id,
1199                                 ent->value);
1200
1201                 APP_CHECK(ret != -ESRCH,
1202                         "CFG: [%s] entry '%s': unknown entry\n",
1203                         section_name,
1204                         ent->name);
1205                 APP_CHECK(ret == 0,
1206                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1207                         section_name,
1208                         ent->name,
1209                         ent->value);
1210         }
1211
1212         free(entries);
1213 }
1214
1215 static void
1216 parse_link(struct app_params *app,
1217         const char *section_name,
1218         struct rte_cfgfile *cfg)
1219 {
1220         struct app_link_params *param;
1221         struct rte_cfgfile_entry *entries;
1222         int n_entries, ret, i;
1223         ssize_t param_idx;
1224
1225         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1226         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1227
1228         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1229         PARSE_ERROR_MALLOC(entries != NULL);
1230
1231         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1232
1233         param_idx = APP_PARAM_ADD(app->link_params, section_name);
1234         PARSER_PARAM_ADD_CHECK(param_idx, app->link_params, section_name);
1235
1236         param = &app->link_params[param_idx];
1237         param->parsed = 1;
1238
1239         for (i = 0; i < n_entries; i++) {
1240                 struct rte_cfgfile_entry *ent = &entries[i];
1241
1242                 ret = -ESRCH;
1243                 if (strcmp(ent->name, "arp_q") == 0)
1244                         ret = parser_read_uint32(&param->arp_q,
1245                                 ent->value);
1246                 else if (strcmp(ent->name, "tcp_syn_q") == 0)
1247                         ret = parser_read_uint32(&param->tcp_syn_local_q,
1248                                 ent->value);
1249                 else if (strcmp(ent->name, "ip_local_q") == 0)
1250                         ret = parser_read_uint32(&param->ip_local_q,
1251                                 ent->value);
1252                 else if (strcmp(ent->name, "tcp_local_q") == 0)
1253                         ret = parser_read_uint32(&param->tcp_local_q,
1254                                 ent->value);
1255                 else if (strcmp(ent->name, "udp_local_q") == 0)
1256                         ret = parser_read_uint32(&param->udp_local_q,
1257                                 ent->value);
1258                 else if (strcmp(ent->name, "sctp_local_q") == 0)
1259                         ret = parser_read_uint32(&param->sctp_local_q,
1260                                 ent->value);
1261
1262                 APP_CHECK(ret != -ESRCH,
1263                         "CFG: [%s] entry '%s': unknown entry\n",
1264                         section_name,
1265                         ent->name);
1266                 APP_CHECK(ret == 0,
1267                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1268                         section_name,
1269                         ent->name,
1270                         ent->value);
1271         }
1272
1273         free(entries);
1274 }
1275
1276 static void
1277 parse_rxq(struct app_params *app,
1278         const char *section_name,
1279         struct rte_cfgfile *cfg)
1280 {
1281         struct app_pktq_hwq_in_params *param;
1282         struct rte_cfgfile_entry *entries;
1283         int n_entries, ret, i;
1284         ssize_t param_idx;
1285
1286         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1287         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1288
1289         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1290         PARSE_ERROR_MALLOC(entries != NULL);
1291
1292         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1293
1294         param_idx = APP_PARAM_ADD(app->hwq_in_params, section_name);
1295         PARSER_PARAM_ADD_CHECK(param_idx, app->hwq_in_params, section_name);
1296
1297         param = &app->hwq_in_params[param_idx];
1298         param->parsed = 1;
1299
1300         for (i = 0; i < n_entries; i++) {
1301                 struct rte_cfgfile_entry *ent = &entries[i];
1302
1303                 ret = -ESRCH;
1304                 if (strcmp(ent->name, "mempool") == 0) {
1305                         int status = validate_name(ent->value, "MEMPOOL", 1);
1306                         ssize_t idx;
1307
1308                         APP_CHECK((status == 0),
1309                                 "CFG: [%s] entry '%s': invalid mempool\n",
1310                                 section_name,
1311                                 ent->name);
1312
1313                         idx = APP_PARAM_ADD(app->mempool_params, ent->value);
1314                         PARSER_IMPLICIT_PARAM_ADD_CHECK(idx, section_name);
1315                         param->mempool_id = idx;
1316                         ret = 0;
1317                 } else if (strcmp(ent->name, "size") == 0)
1318                         ret = parser_read_uint32(&param->size,
1319                                 ent->value);
1320                 else if (strcmp(ent->name, "burst") == 0)
1321                         ret = parser_read_uint32(&param->burst,
1322                                 ent->value);
1323
1324                 APP_CHECK(ret != -ESRCH,
1325                         "CFG: [%s] entry '%s': unknown entry\n",
1326                         section_name,
1327                         ent->name);
1328                 APP_CHECK(ret == 0,
1329                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1330                         section_name,
1331                         ent->name,
1332                         ent->value);
1333         }
1334
1335         free(entries);
1336 }
1337
1338 static void
1339 parse_txq(struct app_params *app,
1340         const char *section_name,
1341         struct rte_cfgfile *cfg)
1342 {
1343         struct app_pktq_hwq_out_params *param;
1344         struct rte_cfgfile_entry *entries;
1345         int n_entries, ret, i;
1346         ssize_t param_idx;
1347
1348         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1349         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1350
1351         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1352         PARSE_ERROR_MALLOC(entries != NULL);
1353
1354         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1355
1356         param_idx = APP_PARAM_ADD(app->hwq_out_params, section_name);
1357         PARSER_PARAM_ADD_CHECK(param_idx, app->hwq_out_params, section_name);
1358
1359         param = &app->hwq_out_params[param_idx];
1360         param->parsed = 1;
1361
1362         for (i = 0; i < n_entries; i++) {
1363                 struct rte_cfgfile_entry *ent = &entries[i];
1364
1365                 ret = -ESRCH;
1366                 if (strcmp(ent->name, "size") == 0)
1367                         ret = parser_read_uint32(&param->size, ent->value);
1368                 else if (strcmp(ent->name, "burst") == 0)
1369                         ret = parser_read_uint32(&param->burst, ent->value);
1370                 else if (strcmp(ent->name, "dropless") == 0) {
1371                         ret = parser_read_arg_bool(ent->value);
1372                         if (ret >= 0) {
1373                                 param->dropless = ret;
1374                                 ret = 0;
1375                         }
1376                 }
1377
1378                 APP_CHECK(ret != -ESRCH,
1379                         "CFG: [%s] entry '%s': unknown entry\n",
1380                         section_name,
1381                         ent->name);
1382                 APP_CHECK(ret == 0,
1383                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1384                         section_name,
1385                         ent->name,
1386                         ent->value);
1387         }
1388
1389         free(entries);
1390 }
1391
1392 static void
1393 parse_swq(struct app_params *app,
1394         const char *section_name,
1395         struct rte_cfgfile *cfg)
1396 {
1397         struct app_pktq_swq_params *param;
1398         struct rte_cfgfile_entry *entries;
1399         int n_entries, ret, i;
1400         ssize_t param_idx;
1401
1402         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1403         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1404
1405         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1406         PARSE_ERROR_MALLOC(entries != NULL);
1407
1408         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1409
1410         param_idx = APP_PARAM_ADD(app->swq_params, section_name);
1411         PARSER_PARAM_ADD_CHECK(param_idx, app->swq_params, section_name);
1412
1413         param = &app->swq_params[param_idx];
1414         param->parsed = 1;
1415
1416         for (i = 0; i < n_entries; i++) {
1417                 struct rte_cfgfile_entry *ent = &entries[i];
1418
1419                 ret = -ESRCH;
1420                 if (strcmp(ent->name, "size") == 0)
1421                         ret = parser_read_uint32(&param->size,
1422                                 ent->value);
1423                 else if (strcmp(ent->name, "burst_read") == 0)
1424                         ret = parser_read_uint32(&param->burst_read,
1425                                 ent->value);
1426                 else if (strcmp(ent->name, "burst_write") == 0)
1427                         ret = parser_read_uint32(&param->burst_write,
1428                                 ent->value);
1429                 else if (strcmp(ent->name, "dropless") == 0) {
1430                         ret = parser_read_arg_bool(ent->value);
1431                         if (ret >= 0) {
1432                                 param->dropless = ret;
1433                                 ret = 0;
1434                         }
1435                 } else if (strcmp(ent->name, "n_retries") == 0)
1436                         ret = parser_read_uint64(&param->n_retries,
1437                                 ent->value);
1438                 else if (strcmp(ent->name, "cpu") == 0)
1439                         ret = parser_read_uint32(&param->cpu_socket_id,
1440                                 ent->value);
1441
1442                 APP_CHECK(ret != -ESRCH,
1443                         "CFG: [%s] entry '%s': unknown entry\n",
1444                         section_name,
1445                         ent->name);
1446                 APP_CHECK(ret == 0,
1447                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1448                         section_name,
1449                         ent->name,
1450                         ent->value);
1451         }
1452
1453         free(entries);
1454 }
1455
1456 static void
1457 parse_tm(struct app_params *app,
1458         const char *section_name,
1459         struct rte_cfgfile *cfg)
1460 {
1461         struct app_pktq_tm_params *param;
1462         struct rte_cfgfile_entry *entries;
1463         int n_entries, ret, i;
1464         ssize_t param_idx;
1465
1466         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1467         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1468
1469         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1470         PARSE_ERROR_MALLOC(entries != NULL);
1471
1472         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1473
1474         param_idx = APP_PARAM_ADD(app->tm_params, section_name);
1475         PARSER_PARAM_ADD_CHECK(param_idx, app->tm_params, section_name);
1476
1477         param = &app->tm_params[param_idx];
1478         param->parsed = 1;
1479
1480         for (i = 0; i < n_entries; i++) {
1481                 struct rte_cfgfile_entry *ent = &entries[i];
1482
1483                 ret = -ESRCH;
1484                 if (strcmp(ent->name, "cfg") == 0) {
1485                         param->file_name = strdup(ent->value);
1486                         if (param->file_name == NULL)
1487                                 ret = -EINVAL;
1488
1489                         ret = 0;
1490                 } else if (strcmp(ent->name, "burst_read") == 0)
1491                         ret = parser_read_uint32(&param->burst_read,
1492                                 ent->value);
1493                 else if (strcmp(ent->name, "burst_write") == 0)
1494                         ret = parser_read_uint32(&param->burst_write,
1495                                 ent->value);
1496
1497                 APP_CHECK(ret != -ESRCH,
1498                         "CFG: [%s] entry '%s': unknown entry\n",
1499                         section_name,
1500                         ent->name);
1501                 APP_CHECK(ret != -EBADF,
1502                         "CFG: [%s] entry '%s': TM cfg parse error '%s'\n",
1503                         section_name,
1504                         ent->name,
1505                         ent->value);
1506                 APP_CHECK(ret == 0,
1507                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1508                         section_name,
1509                         ent->name,
1510                         ent->value);
1511         }
1512
1513         free(entries);
1514 }
1515
1516 static void
1517 parse_source(struct app_params *app,
1518         const char *section_name,
1519         struct rte_cfgfile *cfg)
1520 {
1521         struct app_pktq_source_params *param;
1522         struct rte_cfgfile_entry *entries;
1523         int n_entries, ret, i;
1524         ssize_t param_idx;
1525
1526         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1527         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1528
1529         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1530         PARSE_ERROR_MALLOC(entries != NULL);
1531
1532         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1533
1534         param_idx = APP_PARAM_ADD(app->source_params, section_name);
1535         PARSER_PARAM_ADD_CHECK(param_idx, app->source_params, section_name);
1536
1537         param = &app->source_params[param_idx];
1538         param->parsed = 1;
1539
1540         for (i = 0; i < n_entries; i++) {
1541                 struct rte_cfgfile_entry *ent = &entries[i];
1542
1543                 ret = -ESRCH;
1544                 if (strcmp(ent->name, "mempool") == 0) {
1545                         int status = validate_name(ent->value, "MEMPOOL", 1);
1546                         ssize_t idx;
1547
1548                         APP_CHECK((status == 0),
1549                                 "CFG: [%s] entry '%s': invalid mempool\n",
1550                                         section_name,
1551                                         ent->name);
1552
1553                         idx = APP_PARAM_ADD(app->mempool_params, ent->value);
1554                         PARSER_IMPLICIT_PARAM_ADD_CHECK(idx, section_name);
1555                         param->mempool_id = idx;
1556                         ret = 0;
1557                 } else if (strcmp(ent->name, "burst") == 0)
1558                         ret = parser_read_uint32(&param->burst, ent->value);
1559
1560                 APP_CHECK(ret != -ESRCH,
1561                         "CFG: [%s] entry '%s': unknown entry\n",
1562                         section_name,
1563                         ent->name);
1564                 APP_CHECK(ret == 0,
1565                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1566                         section_name,
1567                         ent->name,
1568                         ent->value);
1569         }
1570
1571         free(entries);
1572 }
1573
1574 static void
1575 parse_msgq_req_pipeline(struct app_params *app,
1576         const char *section_name,
1577         struct rte_cfgfile *cfg)
1578 {
1579         struct app_msgq_params *param;
1580         struct rte_cfgfile_entry *entries;
1581         int n_entries, ret, i;
1582         ssize_t param_idx;
1583
1584         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1585         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1586
1587         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1588         PARSE_ERROR_MALLOC(entries != NULL);
1589
1590         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1591
1592         param_idx = APP_PARAM_ADD(app->msgq_params, section_name);
1593         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, section_name);
1594
1595         param = &app->msgq_params[param_idx];
1596         param->parsed = 1;
1597
1598         for (i = 0; i < n_entries; i++) {
1599                 struct rte_cfgfile_entry *ent = &entries[i];
1600
1601                 ret = -ESRCH;
1602                 if (strcmp(ent->name, "size") == 0)
1603                         ret = parser_read_uint32(&param->size, ent->value);
1604
1605                 APP_CHECK(ret != -ESRCH,
1606                         "CFG: [%s] entry '%s': unknown entry\n",
1607                         section_name,
1608                         ent->name);
1609                 APP_CHECK(ret == 0,
1610                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1611                         section_name,
1612                         ent->name,
1613                         ent->value);
1614         }
1615
1616         free(entries);
1617 }
1618
1619 static void
1620 parse_msgq_rsp_pipeline(struct app_params *app,
1621         const char *section_name,
1622         struct rte_cfgfile *cfg)
1623 {
1624         struct app_msgq_params *param;
1625         struct rte_cfgfile_entry *entries;
1626         int n_entries, ret, i;
1627         ssize_t param_idx;
1628
1629         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1630         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1631
1632         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1633         PARSE_ERROR_MALLOC(entries != NULL);
1634
1635         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1636
1637         param_idx = APP_PARAM_ADD(app->msgq_params, section_name);
1638         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, section_name);
1639
1640         param = &app->msgq_params[param_idx];
1641         param->parsed = 1;
1642
1643         for (i = 0; i < n_entries; i++) {
1644                 struct rte_cfgfile_entry *ent = &entries[i];
1645
1646                 ret = -ESRCH;
1647                 if (strcmp(ent->name, "size") == 0)
1648                         ret = parser_read_uint32(&param->size, ent->value);
1649
1650                 APP_CHECK(ret != -ESRCH,
1651                         "CFG: [%s] entry '%s': unknown entry\n",
1652                         section_name,
1653                         ent->name);
1654                 APP_CHECK(ret == 0,
1655                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1656                         section_name,
1657                         ent->name,
1658                         ent->value);
1659         }
1660
1661         free(entries);
1662 }
1663
1664 static void
1665 parse_msgq(struct app_params *app,
1666         const char *section_name,
1667         struct rte_cfgfile *cfg)
1668 {
1669         struct app_msgq_params *param;
1670         struct rte_cfgfile_entry *entries;
1671         int n_entries, ret, i;
1672         ssize_t param_idx;
1673
1674         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1675         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1676
1677         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1678         PARSE_ERROR_MALLOC(entries != NULL);
1679
1680         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1681
1682         param_idx = APP_PARAM_ADD(app->msgq_params, section_name);
1683         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, section_name);
1684
1685         param = &app->msgq_params[param_idx];
1686         param->parsed = 1;
1687
1688         for (i = 0; i < n_entries; i++) {
1689                 struct rte_cfgfile_entry *ent = &entries[i];
1690
1691                 ret = -ESRCH;
1692                 if (strcmp(ent->name, "size") == 0)
1693                         ret = parser_read_uint32(&param->size,
1694                                 ent->value);
1695                 else if (strcmp(ent->name, "cpu") == 0)
1696                         ret = parser_read_uint32(&param->cpu_socket_id,
1697                                 ent->value);
1698
1699                 APP_CHECK(ret != -ESRCH,
1700                         "CFG: [%s] entry '%s': unknown entry\n",
1701                         section_name,
1702                         ent->name);
1703                 APP_CHECK(ret == 0,
1704                         "CFG: [%s] entry '%s': Invalid value '%s'\n",
1705                         section_name,
1706                         ent->name,
1707                         ent->value);
1708         }
1709
1710         free(entries);
1711 }
1712
1713 typedef void (*config_section_load)(struct app_params *p,
1714         const char *section_name,
1715         struct rte_cfgfile *cfg);
1716
1717 struct config_section {
1718         const char prefix[CFG_NAME_LEN];
1719         int numbers;
1720         config_section_load load;
1721 };
1722
1723 static const struct config_section cfg_file_scheme[] = {
1724         {"EAL", 0, parse_eal},
1725         {"PIPELINE", 1, parse_pipeline},
1726         {"MEMPOOL", 1, parse_mempool},
1727         {"LINK", 1, parse_link},
1728         {"RXQ", 2, parse_rxq},
1729         {"TXQ", 2, parse_txq},
1730         {"SWQ", 1, parse_swq},
1731         {"TM", 1, parse_tm},
1732         {"SOURCE", 1, parse_source},
1733         {"MSGQ-REQ-PIPELINE", 1, parse_msgq_req_pipeline},
1734         {"MSGQ-RSP-PIPELINE", 1, parse_msgq_rsp_pipeline},
1735         {"MSGQ", 1, parse_msgq},
1736 };
1737
1738 static void
1739 create_implicit_mempools(struct app_params *app)
1740 {
1741         ssize_t idx;
1742
1743         idx = APP_PARAM_ADD(app->mempool_params, "MEMPOOL0");
1744         PARSER_IMPLICIT_PARAM_ADD_CHECK(idx, "start-up");
1745 }
1746
1747 static void
1748 parse_port_mask(struct app_params *app, uint64_t port_mask)
1749 {
1750         uint32_t pmd_id, link_id;
1751
1752         link_id = 0;
1753         for (pmd_id = 0; pmd_id < RTE_MAX_ETHPORTS; pmd_id++) {
1754                 char name[APP_PARAM_NAME_SIZE];
1755                 ssize_t idx;
1756
1757                 if ((port_mask & (1LLU << pmd_id)) == 0)
1758                         continue;
1759
1760                 snprintf(name, sizeof(name), "LINK%" PRIu32, link_id);
1761                 idx = APP_PARAM_ADD(app->link_params, name);
1762                 PARSER_IMPLICIT_PARAM_ADD_CHECK(idx, name);
1763
1764                 app->link_params[idx].pmd_id = pmd_id;
1765                 link_id++;
1766         }
1767 }
1768
1769 int
1770 app_config_parse(struct app_params *app, const char *file_name)
1771 {
1772         char config_file_out[APP_FILE_NAME_SIZE];
1773         struct rte_cfgfile *cfg;
1774         char **section_names;
1775         int i, j, sect_count;
1776
1777         /* Implicit mempools */
1778         create_implicit_mempools(app);
1779
1780         /* Port mask */
1781         parse_port_mask(app, app->port_mask);
1782
1783         /* Load application configuration file */
1784         cfg = rte_cfgfile_load(file_name, 0);
1785         APP_CHECK(cfg != NULL, "Unable to load config file %s", file_name);
1786
1787         sect_count = rte_cfgfile_num_sections(cfg, NULL, 0);
1788         section_names = malloc(sect_count * sizeof(char *));
1789         for (i = 0; i < sect_count; i++)
1790                 section_names[i] = malloc(CFG_NAME_LEN);
1791
1792         rte_cfgfile_sections(cfg, section_names, sect_count);
1793
1794         for (i = 0; i < sect_count; i++) {
1795                 const struct config_section *sch_s;
1796                 int len, cfg_name_len;
1797
1798                 cfg_name_len = strlen(section_names[i]);
1799
1800                 /* Find section type */
1801                 for (j = 0; j < (int)RTE_DIM(cfg_file_scheme); j++) {
1802                         sch_s = &cfg_file_scheme[j];
1803                         len = strlen(sch_s->prefix);
1804
1805                         if (cfg_name_len < len)
1806                                 continue;
1807
1808                         /* After section name we expect only '\0' or digit or
1809                          * digit dot digit, so protect against false matching,
1810                          * for example: "ABC" should match section name
1811                          * "ABC0.0", but it should not match section_name
1812                          * "ABCDEF".
1813                          */
1814                         if ((section_names[i][len] != '\0') &&
1815                                 !isdigit(section_names[i][len]))
1816                                 continue;
1817
1818                         if (strncmp(sch_s->prefix, section_names[i], len) == 0)
1819                                 break;
1820                 }
1821
1822                 APP_CHECK(j < (int)RTE_DIM(cfg_file_scheme),
1823                         "Unknown section %s",
1824                         section_names[i]);
1825
1826                 APP_CHECK(validate_name(section_names[i],
1827                         sch_s->prefix,
1828                         sch_s->numbers) == 0,
1829                         "Invalid section name '%s'",
1830                         section_names[i]);
1831
1832                 sch_s->load(app, section_names[i], cfg);
1833         }
1834
1835         for (i = 0; i < sect_count; i++)
1836                 free(section_names[i]);
1837
1838         free(section_names);
1839
1840         rte_cfgfile_close(cfg);
1841
1842         APP_PARAM_COUNT(app->mempool_params, app->n_mempools);
1843         APP_PARAM_COUNT(app->link_params, app->n_links);
1844         APP_PARAM_COUNT(app->hwq_in_params, app->n_pktq_hwq_in);
1845         APP_PARAM_COUNT(app->hwq_out_params, app->n_pktq_hwq_out);
1846         APP_PARAM_COUNT(app->swq_params, app->n_pktq_swq);
1847         APP_PARAM_COUNT(app->tm_params, app->n_pktq_tm);
1848         APP_PARAM_COUNT(app->source_params, app->n_pktq_source);
1849         APP_PARAM_COUNT(app->sink_params, app->n_pktq_sink);
1850         APP_PARAM_COUNT(app->msgq_params, app->n_msgq);
1851         APP_PARAM_COUNT(app->pipeline_params, app->n_pipelines);
1852
1853         /* Save configuration to output file */
1854         snprintf(config_file_out,
1855                 APP_FILE_NAME_SIZE,
1856                 "%s.out",
1857                 app->config_file);
1858         app_config_save(app, config_file_out);
1859
1860         /* Load TM configuration files */
1861         app_config_parse_tm(app);
1862
1863         return 0;
1864 }
1865
1866 static void
1867 save_eal_params(struct app_params *app, FILE *f)
1868 {
1869         struct app_eal_params *p = &app->eal_params;
1870
1871         fprintf(f, "[EAL]\n");
1872
1873         if (p->coremap)
1874                 fprintf(f, "%s = %s\n", "lcores", p->coremap);
1875
1876         if (p->master_lcore_present)
1877                 fprintf(f, "%s = %" PRIu32 "\n",
1878                         "master_lcore", p->master_lcore);
1879
1880         fprintf(f, "%s = %" PRIu32 "\n", "n", p->channels);
1881
1882         if (p->memory_present)
1883                 fprintf(f, "%s = %" PRIu32 "\n", "m", p->memory);
1884
1885         if (p->ranks_present)
1886                 fprintf(f, "%s = %" PRIu32 "\n", "r", p->ranks);
1887
1888         if (p->pci_blacklist)
1889                 fprintf(f, "%s = %s\n", "pci_blacklist", p->pci_blacklist);
1890
1891         if (p->pci_whitelist)
1892                 fprintf(f, "%s = %s\n", "pci_whitelist", p->pci_whitelist);
1893
1894         if (p->vdev)
1895                 fprintf(f, "%s = %s\n", "vdev", p->vdev);
1896
1897         if (p->vmware_tsc_map_present)
1898                 fprintf(f, "%s = %s\n", "vmware_tsc_map",
1899                         (p->vmware_tsc_map) ? "yes" : "no");
1900
1901         if (p->proc_type)
1902                 fprintf(f, "%s = %s\n", "proc_type", p->proc_type);
1903
1904         if (p->syslog)
1905                 fprintf(f, "%s = %s\n", "syslog", p->syslog);
1906
1907         if (p->log_level_present)
1908                 fprintf(f, "%s = %" PRIu32 "\n", "log_level", p->log_level);
1909
1910         if (p->version_present)
1911                 fprintf(f, "%s = %s\n", "v", (p->version) ? "yes" : "no");
1912
1913         if (p->help_present)
1914                 fprintf(f, "%s = %s\n", "help", (p->help) ? "yes" : "no");
1915
1916         if (p->no_huge_present)
1917                 fprintf(f, "%s = %s\n", "no_huge", (p->no_huge) ? "yes" : "no");
1918
1919         if (p->no_pci_present)
1920                 fprintf(f, "%s = %s\n", "no_pci", (p->no_pci) ? "yes" : "no");
1921
1922         if (p->no_hpet_present)
1923                 fprintf(f, "%s = %s\n", "no_hpet", (p->no_hpet) ? "yes" : "no");
1924
1925         if (p->no_shconf_present)
1926                 fprintf(f, "%s = %s\n", "no_shconf",
1927                         (p->no_shconf) ? "yes" : "no");
1928
1929         if (p->add_driver)
1930                 fprintf(f, "%s = %s\n", "d", p->add_driver);
1931
1932         if (p->socket_mem)
1933                 fprintf(f, "%s = %s\n", "socket_mem", p->socket_mem);
1934
1935         if (p->huge_dir)
1936                 fprintf(f, "%s = %s\n", "huge_dir", p->huge_dir);
1937
1938         if (p->file_prefix)
1939                 fprintf(f, "%s = %s\n", "file_prefix", p->file_prefix);
1940
1941         if (p->base_virtaddr)
1942                 fprintf(f, "%s = %s\n", "base_virtaddr", p->base_virtaddr);
1943
1944         if (p->create_uio_dev_present)
1945                 fprintf(f, "%s = %s\n", "create_uio_dev",
1946                         (p->create_uio_dev) ? "yes" : "no");
1947
1948         if (p->vfio_intr)
1949                 fprintf(f, "%s = %s\n", "vfio_intr", p->vfio_intr);
1950
1951         if (p->xen_dom0_present)
1952                 fprintf(f, "%s = %s\n", "xen_dom0",
1953                         (p->xen_dom0) ? "yes" : "no");
1954
1955         fputc('\n', f);
1956 }
1957
1958 static void
1959 save_mempool_params(struct app_params *app, FILE *f)
1960 {
1961         struct app_mempool_params *p;
1962         size_t i, count;
1963
1964         count = RTE_DIM(app->mempool_params);
1965         for (i = 0; i < count; i++) {
1966                 p = &app->mempool_params[i];
1967                 if (!APP_PARAM_VALID(p))
1968                         continue;
1969
1970                 fprintf(f, "[%s]\n", p->name);
1971                 fprintf(f, "%s = %" PRIu32 "\n", "buffer_size", p->buffer_size);
1972                 fprintf(f, "%s = %" PRIu32 "\n", "pool_size", p->pool_size);
1973                 fprintf(f, "%s = %" PRIu32 "\n", "cache_size", p->cache_size);
1974                 fprintf(f, "%s = %" PRIu32 "\n", "cpu", p->cpu_socket_id);
1975
1976                 fputc('\n', f);
1977         }
1978 }
1979
1980 static void
1981 save_links_params(struct app_params *app, FILE *f)
1982 {
1983         struct app_link_params *p;
1984         size_t i, count;
1985
1986         count = RTE_DIM(app->link_params);
1987         for (i = 0; i < count; i++) {
1988                 p = &app->link_params[i];
1989                 if (!APP_PARAM_VALID(p))
1990                         continue;
1991
1992                 fprintf(f, "[%s]\n", p->name);
1993                 fprintf(f, "; %s = %" PRIu32 "\n", "pmd_id", p->pmd_id);
1994                 fprintf(f, "%s = %" PRIu32 "\n", "arp_q", p->arp_q);
1995                 fprintf(f, "%s = %" PRIu32 "\n", "tcp_syn_local_q",
1996                         p->tcp_syn_local_q);
1997                 fprintf(f, "%s = %" PRIu32 "\n", "ip_local_q", p->ip_local_q);
1998                 fprintf(f, "%s = %" PRIu32 "\n", "tcp_local_q", p->tcp_local_q);
1999                 fprintf(f, "%s = %" PRIu32 "\n", "udp_local_q", p->udp_local_q);
2000                 fprintf(f, "%s = %" PRIu32 "\n", "sctp_local_q",
2001                         p->sctp_local_q);
2002
2003                 fputc('\n', f);
2004         }
2005 }
2006
2007 static void
2008 save_rxq_params(struct app_params *app, FILE *f)
2009 {
2010         struct app_pktq_hwq_in_params *p;
2011         size_t i, count;
2012
2013         count = RTE_DIM(app->hwq_in_params);
2014         for (i = 0; i < count; i++) {
2015                 p = &app->hwq_in_params[i];
2016                 if (!APP_PARAM_VALID(p))
2017                         continue;
2018
2019                 fprintf(f, "[%s]\n", p->name);
2020                 fprintf(f, "%s = %s\n",
2021                         "mempool",
2022                         app->mempool_params[p->mempool_id].name);
2023                 fprintf(f, "%s = %" PRIu32 "\n", "size", p->size);
2024                 fprintf(f, "%s = %" PRIu32 "\n", "burst", p->burst);
2025
2026                 fputc('\n', f);
2027         }
2028 }
2029
2030 static void
2031 save_txq_params(struct app_params *app, FILE *f)
2032 {
2033         struct app_pktq_hwq_out_params *p;
2034         size_t i, count;
2035
2036         count = RTE_DIM(app->hwq_out_params);
2037         for (i = 0; i < count; i++) {
2038                 p = &app->hwq_out_params[i];
2039                 if (!APP_PARAM_VALID(p))
2040                         continue;
2041
2042                 fprintf(f, "[%s]\n", p->name);
2043                 fprintf(f, "%s = %" PRIu32 "\n", "size", p->size);
2044                 fprintf(f, "%s = %" PRIu32 "\n", "burst", p->burst);
2045                 fprintf(f, "%s = %s\n",
2046                         "dropless",
2047                         p->dropless ? "yes" : "no");
2048
2049                 fputc('\n', f);
2050         }
2051 }
2052
2053 static void
2054 save_swq_params(struct app_params *app, FILE *f)
2055 {
2056         struct app_pktq_swq_params *p;
2057         size_t i, count;
2058
2059         count = RTE_DIM(app->swq_params);
2060         for (i = 0; i < count; i++) {
2061                 p = &app->swq_params[i];
2062                 if (!APP_PARAM_VALID(p))
2063                         continue;
2064
2065                 fprintf(f, "[%s]\n", p->name);
2066                 fprintf(f, "%s = %" PRIu32 "\n", "size", p->size);
2067                 fprintf(f, "%s = %" PRIu32 "\n", "burst_read", p->burst_read);
2068                 fprintf(f, "%s = %" PRIu32 "\n", "burst_write", p->burst_write);
2069                 fprintf(f, "%s = %s\n", "dropless", p->dropless ? "yes" : "no");
2070                 fprintf(f, "%s = %" PRIu64 "\n", "n_retries", p->n_retries);
2071                 fprintf(f, "%s = %" PRIu32 "\n", "cpu", p->cpu_socket_id);
2072
2073                 fputc('\n', f);
2074         }
2075 }
2076
2077 static void
2078 save_tm_params(struct app_params *app, FILE *f)
2079 {
2080         struct app_pktq_tm_params *p;
2081         size_t i, count;
2082
2083         count = RTE_DIM(app->tm_params);
2084         for (i = 0; i < count; i++) {
2085                 p = &app->tm_params[i];
2086                 if (!APP_PARAM_VALID(p))
2087                         continue;
2088
2089                 fprintf(f, "[%s]\n", p->name);
2090                 fprintf(f, "%s = %s\n", "cfg", p->file_name);
2091                 fprintf(f, "%s = %" PRIu32 "\n", "burst_read", p->burst_read);
2092                 fprintf(f, "%s = %" PRIu32 "\n", "burst_write", p->burst_write);
2093
2094                 fputc('\n', f);
2095         }
2096 }
2097
2098 static void
2099 save_source_params(struct app_params *app, FILE *f)
2100 {
2101         struct app_pktq_source_params *p;
2102         size_t i, count;
2103
2104         count = RTE_DIM(app->source_params);
2105         for (i = 0; i < count; i++) {
2106                 p = &app->source_params[i];
2107                 if (!APP_PARAM_VALID(p))
2108                         continue;
2109
2110                 fprintf(f, "[%s]\n", p->name);
2111                 fprintf(f, "%s = %s\n",
2112                         "mempool",
2113                         app->mempool_params[p->mempool_id].name);
2114                 fprintf(f, "%s = %" PRIu32 "\n", "burst", p->burst);
2115                 fputc('\n', f);
2116         }
2117 }
2118
2119 static void
2120 save_msgq_params(struct app_params *app, FILE *f)
2121 {
2122         struct app_msgq_params *p;
2123         size_t i, count;
2124
2125         count = RTE_DIM(app->msgq_params);
2126         for (i = 0; i < count; i++) {
2127                 p = &app->msgq_params[i];
2128                 if (!APP_PARAM_VALID(p))
2129                         continue;
2130
2131                 fprintf(f, "[%s]\n", p->name);
2132                 fprintf(f, "%s = %" PRIu32 "\n", "size", p->size);
2133                 fprintf(f, "%s = %" PRIu32 "\n", "cpu", p->cpu_socket_id);
2134
2135                 fputc('\n', f);
2136         }
2137 }
2138
2139 static void
2140 save_pipeline_params(struct app_params *app, FILE *f)
2141 {
2142         size_t i, count;
2143
2144         count = RTE_DIM(app->pipeline_params);
2145         for (i = 0; i < count; i++) {
2146                 struct app_pipeline_params *p = &app->pipeline_params[i];
2147
2148                 if (!APP_PARAM_VALID(p))
2149                         continue;
2150
2151                 /* section name */
2152                 fprintf(f, "[%s]\n", p->name);
2153
2154                 /* type */
2155                 fprintf(f, "type = %s\n", p->type);
2156
2157                 /* core */
2158                 fprintf(f, "core = s%" PRIu32 "c%" PRIu32 "%s\n",
2159                         p->socket_id,
2160                         p->core_id,
2161                         (p->hyper_th_id) ? "h" : "");
2162
2163                 /* pktq_in */
2164                 if (p->n_pktq_in) {
2165                         uint32_t j;
2166
2167                         fprintf(f, "pktq_in =");
2168                         for (j = 0; j < p->n_pktq_in; j++) {
2169                                 struct app_pktq_in_params *pp = &p->pktq_in[j];
2170                                 char *name;
2171
2172                                 switch (pp->type) {
2173                                 case APP_PKTQ_IN_HWQ:
2174                                         name = app->hwq_in_params[pp->id].name;
2175                                         break;
2176                                 case APP_PKTQ_IN_SWQ:
2177                                         name = app->swq_params[pp->id].name;
2178                                         break;
2179                                 case APP_PKTQ_IN_TM:
2180                                         name = app->tm_params[pp->id].name;
2181                                         break;
2182                                 case APP_PKTQ_IN_SOURCE:
2183                                         name = app->source_params[pp->id].name;
2184                                         break;
2185                                 default:
2186                                         APP_CHECK(0, "Error\n");
2187                                 }
2188
2189                                 fprintf(f, " %s", name);
2190                         }
2191                         fprintf(f, "\n");
2192                 }
2193
2194                 /* pktq_in */
2195                 if (p->n_pktq_out) {
2196                         uint32_t j;
2197
2198                         fprintf(f, "pktq_out =");
2199                         for (j = 0; j < p->n_pktq_out; j++) {
2200                                 struct app_pktq_out_params *pp =
2201                                         &p->pktq_out[j];
2202                                 char *name;
2203
2204                                 switch (pp->type) {
2205                                 case APP_PKTQ_OUT_HWQ:
2206                                         name = app->hwq_out_params[pp->id].name;
2207                                         break;
2208                                 case APP_PKTQ_OUT_SWQ:
2209                                         name = app->swq_params[pp->id].name;
2210                                         break;
2211                                 case APP_PKTQ_OUT_TM:
2212                                         name = app->tm_params[pp->id].name;
2213                                         break;
2214                                 case APP_PKTQ_OUT_SINK:
2215                                         name = app->sink_params[pp->id].name;
2216                                         break;
2217                                 default:
2218                                         APP_CHECK(0, "Error\n");
2219                                 }
2220
2221                                 fprintf(f, " %s", name);
2222                         }
2223                         fprintf(f, "\n");
2224                 }
2225
2226                 /* msgq_in */
2227                 if (p->n_msgq_in) {
2228                         uint32_t j;
2229
2230                         fprintf(f, "msgq_in =");
2231                         for (j = 0; j < p->n_msgq_in; j++) {
2232                                 uint32_t id = p->msgq_in[j];
2233                                 char *name = app->msgq_params[id].name;
2234
2235                                 fprintf(f, " %s", name);
2236                         }
2237                         fprintf(f, "\n");
2238                 }
2239
2240                 /* msgq_out */
2241                 if (p->n_msgq_in) {
2242                         uint32_t j;
2243
2244                         fprintf(f, "msgq_out =");
2245                         for (j = 0; j < p->n_msgq_out; j++) {
2246                                 uint32_t id = p->msgq_out[j];
2247                                 char *name = app->msgq_params[id].name;
2248
2249                                 fprintf(f, " %s", name);
2250                         }
2251                         fprintf(f, "\n");
2252                 }
2253
2254                 /* timer_period */
2255                 fprintf(f, "timer_period = %" PRIu32 "\n", p->timer_period);
2256
2257                 /* args */
2258                 if (p->n_args) {
2259                         uint32_t j;
2260
2261                         for (j = 0; j < p->n_args; j++)
2262                                 fprintf(f, "%s = %s\n", p->args_name[j],
2263                                         p->args_value[j]);
2264                 }
2265
2266                 fprintf(f, "\n");
2267         }
2268 }
2269
2270 void
2271 app_config_save(struct app_params *app, const char *file_name)
2272 {
2273         FILE *file;
2274         char *name, *dir_name;
2275         int status;
2276
2277         name = strdup(file_name);
2278         dir_name = dirname(name);
2279         status = access(dir_name, W_OK);
2280         APP_CHECK((status == 0),
2281                 "Need write access to directory \"%s\" to save configuration\n",
2282                 dir_name);
2283
2284         file = fopen(file_name, "w");
2285         APP_CHECK((file != NULL),
2286                 "Failed to save configuration to file \"%s\"", file_name);
2287
2288         save_eal_params(app, file);
2289         save_pipeline_params(app, file);
2290         save_mempool_params(app, file);
2291         save_links_params(app, file);
2292         save_rxq_params(app, file);
2293         save_txq_params(app, file);
2294         save_swq_params(app, file);
2295         save_tm_params(app, file);
2296         save_source_params(app, file);
2297         save_msgq_params(app, file);
2298
2299         fclose(file);
2300         free(name);
2301 }
2302
2303 int
2304 app_config_init(struct app_params *app)
2305 {
2306         size_t i;
2307
2308         memcpy(app, &app_params_default, sizeof(struct app_params));
2309
2310         for (i = 0; i < RTE_DIM(app->mempool_params); i++)
2311                 memcpy(&app->mempool_params[i],
2312                         &mempool_params_default,
2313                         sizeof(struct app_mempool_params));
2314
2315         for (i = 0; i < RTE_DIM(app->link_params); i++)
2316                 memcpy(&app->link_params[i],
2317                         &link_params_default,
2318                         sizeof(struct app_link_params));
2319
2320         for (i = 0; i < RTE_DIM(app->hwq_in_params); i++)
2321                 memcpy(&app->hwq_in_params[i],
2322                         &default_hwq_in_params,
2323                         sizeof(default_hwq_in_params));
2324
2325         for (i = 0; i < RTE_DIM(app->hwq_out_params); i++)
2326                 memcpy(&app->hwq_out_params[i],
2327                         &default_hwq_out_params,
2328                         sizeof(default_hwq_out_params));
2329
2330         for (i = 0; i < RTE_DIM(app->swq_params); i++)
2331                 memcpy(&app->swq_params[i],
2332                         &default_swq_params,
2333                         sizeof(default_swq_params));
2334
2335         for (i = 0; i < RTE_DIM(app->tm_params); i++)
2336                 memcpy(&app->tm_params[i],
2337                         &default_tm_params,
2338                         sizeof(default_tm_params));
2339
2340         for (i = 0; i < RTE_DIM(app->source_params); i++)
2341                 memcpy(&app->source_params[i],
2342                         &default_source_params,
2343                         sizeof(default_source_params));
2344
2345         for (i = 0; i < RTE_DIM(app->sink_params); i++)
2346                 memcpy(&app->sink_params[i],
2347                         &default_sink_params,
2348                         sizeof(default_sink_params));
2349
2350         for (i = 0; i < RTE_DIM(app->msgq_params); i++)
2351                 memcpy(&app->msgq_params[i],
2352                         &default_msgq_params,
2353                         sizeof(default_msgq_params));
2354
2355         for (i = 0; i < RTE_DIM(app->pipeline_params); i++)
2356                 memcpy(&app->pipeline_params[i],
2357                         &default_pipeline_params,
2358                         sizeof(default_pipeline_params));
2359
2360         return 0;
2361 }
2362
2363 int
2364 app_config_args(struct app_params *app, int argc, char **argv)
2365 {
2366         int opt;
2367         int option_index, f_present, s_present, p_present, l_present;
2368         int scaned = 0;
2369
2370         static struct option lgopts[] = {
2371                 {NULL, 0, 0, 0}
2372         };
2373
2374         /* Copy application name */
2375         strncpy(app->app_name, argv[0], APP_APPNAME_SIZE - 1);
2376
2377         f_present = 0;
2378         s_present = 0;
2379         p_present = 0;
2380         l_present = 0;
2381
2382         while ((opt = getopt_long(argc, argv, "f:s:p:l:", lgopts,
2383                         &option_index)) != EOF)
2384                 switch (opt) {
2385                 case 'f':
2386                         if (f_present)
2387                                 rte_panic("Error: Config file is provided "
2388                                         "more than once\n");
2389                         f_present = 1;
2390
2391                         if (!strlen(optarg))
2392                                 rte_panic("Error: Config file name is null\n");
2393
2394                         app->config_file = strdup(optarg);
2395                         if (app->config_file == NULL)
2396                                 rte_panic("Error: Memory allocation failure\n");
2397
2398                         break;
2399
2400                 case 's':
2401                         if (s_present)
2402                                 rte_panic("Error: Script file is provided "
2403                                         "more than once\n");
2404                         s_present = 1;
2405
2406                         if (!strlen(optarg))
2407                                 rte_panic("Error: Script file name is null\n");
2408
2409                         app->script_file = strdup(optarg);
2410                         if (app->script_file == NULL)
2411                                 rte_panic("Error: Memory allocation failure\n");
2412
2413                         break;
2414
2415                 case 'p':
2416                         if (p_present)
2417                                 rte_panic("Error: PORT_MASK is provided "
2418                                         "more than once\n");
2419                         p_present = 1;
2420
2421                         if ((sscanf(optarg, "%" SCNx64 "%n", &app->port_mask,
2422                                 &scaned) != 1) ||
2423                                 ((size_t) scaned != strlen(optarg)))
2424                                 rte_panic("Error: PORT_MASK is not "
2425                                         "a hexadecimal integer\n");
2426
2427                         if (app->port_mask == 0)
2428                                 rte_panic("Error: PORT_MASK is null\n");
2429
2430                         break;
2431
2432                 case 'l':
2433                         if (l_present)
2434                                 rte_panic("Error: LOG_LEVEL is provided "
2435                                         "more than once\n");
2436                         l_present = 1;
2437
2438                         if ((sscanf(optarg, "%" SCNu32 "%n", &app->log_level,
2439                                 &scaned) != 1) ||
2440                                 ((size_t) scaned != strlen(optarg)) ||
2441                                 (app->log_level >= APP_LOG_LEVELS))
2442                                 rte_panic("Error: LOG_LEVEL invalid value\n");
2443
2444                         break;
2445
2446                 default:
2447                         app_print_usage(argv[0]);
2448                 }
2449
2450         optind = 0; /* reset getopt lib */
2451
2452         /* Check that mandatory args have been provided */
2453         if (!p_present)
2454                 rte_panic("Error: PORT_MASK is not provided\n");
2455
2456         return 0;
2457 }