examples/ip_pipeline: clean config parser
[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 #include <sys/wait.h>
44
45 #include <rte_errno.h>
46 #include <rte_cfgfile.h>
47 #include <rte_string_fns.h>
48
49 #include "app.h"
50 #include "parser.h"
51
52 /**
53  * Default config values
54  **/
55
56 static struct app_params app_params_default = {
57         .config_file = "./config/ip_pipeline.cfg",
58         .log_level = APP_LOG_LEVEL_HIGH,
59
60         .eal_params = {
61                 .channels = 4,
62         },
63 };
64
65 static const struct app_mempool_params mempool_params_default = {
66         .parsed = 0,
67         .buffer_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
68         .pool_size = 32 * 1024,
69         .cache_size = 256,
70         .cpu_socket_id = 0,
71 };
72
73 static const struct app_link_params link_params_default = {
74         .parsed = 0,
75         .pmd_id = 0,
76         .arp_q = 0,
77         .tcp_syn_local_q = 0,
78         .ip_local_q = 0,
79         .tcp_local_q = 0,
80         .udp_local_q = 0,
81         .sctp_local_q = 0,
82         .state = 0,
83         .ip = 0,
84         .depth = 0,
85         .mac_addr = 0,
86
87         .conf = {
88                 .link_speed = 0,
89                 .link_duplex = 0,
90                 .rxmode = {
91                         .mq_mode = ETH_MQ_RX_NONE,
92
93                         .header_split   = 0, /* Header split */
94                         .hw_ip_checksum = 0, /* IP checksum offload */
95                         .hw_vlan_filter = 0, /* VLAN filtering */
96                         .hw_vlan_strip  = 0, /* VLAN strip */
97                         .hw_vlan_extend = 0, /* Extended VLAN */
98                         .jumbo_frame    = 0, /* Jumbo frame support */
99                         .hw_strip_crc   = 0, /* CRC strip by HW */
100                         .enable_scatter = 0, /* Scattered packets RX handler */
101
102                         .max_rx_pkt_len = 9000, /* Jumbo frame max packet len */
103                         .split_hdr_size = 0, /* Header split buffer size */
104                 },
105                 .txmode = {
106                         .mq_mode = ETH_MQ_TX_NONE,
107                 },
108                 .lpbk_mode = 0,
109         },
110
111         .promisc = 1,
112 };
113
114 static const struct app_pktq_hwq_in_params default_hwq_in_params = {
115         .parsed = 0,
116         .mempool_id = 0,
117         .size = 128,
118         .burst = 32,
119
120         .conf = {
121                 .rx_thresh = {
122                                 .pthresh = 8,
123                                 .hthresh = 8,
124                                 .wthresh = 4,
125                 },
126                 .rx_free_thresh = 64,
127                 .rx_drop_en = 0,
128                 .rx_deferred_start = 0,
129         }
130 };
131
132 static const struct app_pktq_hwq_out_params default_hwq_out_params = {
133         .parsed = 0,
134         .size = 512,
135         .burst = 32,
136         .dropless = 0,
137         .n_retries = 0,
138
139         .conf = {
140                 .tx_thresh = {
141                         .pthresh = 36,
142                         .hthresh = 0,
143                         .wthresh = 0,
144                 },
145                 .tx_rs_thresh = 0,
146                 .tx_free_thresh = 0,
147                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
148                         ETH_TXQ_FLAGS_NOOFFLOADS,
149                 .tx_deferred_start = 0,
150         }
151 };
152
153 static const struct app_pktq_swq_params default_swq_params = {
154         .parsed = 0,
155         .size = 256,
156         .burst_read = 32,
157         .burst_write = 32,
158         .dropless = 0,
159         .n_retries = 0,
160         .cpu_socket_id = 0,
161         .ipv4_frag = 0,
162         .ipv6_frag = 0,
163         .ipv4_ras = 0,
164         .ipv6_ras = 0,
165         .mtu = 0,
166         .metadata_size = 0,
167         .mempool_direct_id = 0,
168         .mempool_indirect_id = 0,
169 };
170
171 struct app_pktq_tm_params default_tm_params = {
172         .parsed = 0,
173         .file_name = "./config/tm_profile.cfg",
174         .burst_read = 64,
175         .burst_write = 32,
176 };
177
178 struct app_pktq_source_params default_source_params = {
179         .parsed = 0,
180         .mempool_id = 0,
181         .burst = 32,
182 };
183
184 struct app_pktq_sink_params default_sink_params = {
185         .parsed = 0,
186 };
187
188 struct app_msgq_params default_msgq_params = {
189         .parsed = 0,
190         .size = 64,
191         .cpu_socket_id = 0,
192 };
193
194 struct app_pipeline_params default_pipeline_params = {
195         .parsed = 0,
196         .socket_id = 0,
197         .core_id = 0,
198         .hyper_th_id = 0,
199         .n_pktq_in = 0,
200         .n_pktq_out = 0,
201         .n_msgq_in = 0,
202         .n_msgq_out = 0,
203         .timer_period = 1,
204         .n_args = 0,
205 };
206
207 static const char app_usage[] =
208         "Usage: %s [-f CONFIG_FILE] [-s SCRIPT_FILE] -p PORT_MASK "
209         "[-l LOG_LEVEL] [--preproc PREPROCESSOR] [--preproc-args ARGS]\n"
210         "\n"
211         "Arguments:\n"
212         "\t-f CONFIG_FILE: Default config file is %s\n"
213         "\t-p PORT_MASK: Mask of NIC port IDs in hexadecimal format\n"
214         "\t-s SCRIPT_FILE: No CLI script file is run when not specified\n"
215         "\t-l LOG_LEVEL: 0 = NONE, 1 = HIGH PRIO (default), 2 = LOW PRIO\n"
216         "\t--preproc PREPROCESSOR: Configuration file pre-processor\n"
217         "\t--preproc-args ARGS: Arguments to be passed to pre-processor\n"
218         "\n";
219
220 static void
221 app_print_usage(char *prgname)
222 {
223         rte_exit(0, app_usage, prgname, app_params_default.config_file);
224 }
225
226 #define skip_white_spaces(pos)                  \
227 ({                                              \
228         __typeof__(pos) _p = (pos);             \
229         for ( ; isspace(*_p); _p++);            \
230         _p;                                     \
231 })
232
233 #define PARSER_PARAM_ADD_CHECK(result, params_array, section_name)      \
234 do {                                                                    \
235         APP_CHECK((result != -EINVAL),                                  \
236                 "Parse error: no free memory");                         \
237         APP_CHECK((result != -ENOMEM),                                  \
238                 "Parse error: too many \"%s\" sections", section_name); \
239         APP_CHECK(((result >= 0) && (params_array)[result].parsed == 0),\
240                 "Parse error: duplicate \"%s\" section", section_name); \
241         APP_CHECK((result >= 0),                                        \
242                 "Parse error in section \"%s\"", section_name);         \
243 } while (0)
244
245 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 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 through */
329         case 'G':
330                 val *= 1024ULL;
331                 /* fall through */
332         case 'M':
333                 val *= 1024ULL;
334                 /* fall through */
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 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
359         if (val > UINT32_MAX)
360                 return -ERANGE;
361
362         *value = val;
363         return 0;
364 }
365
366 int
367 parse_pipeline_core(uint32_t *socket,
368         uint32_t *core,
369         uint32_t *ht,
370         const char *entry)
371 {
372         size_t num_len;
373         char num[8];
374
375         uint32_t s = 0, c = 0, h = 0, val;
376         uint8_t s_parsed = 0, c_parsed = 0, h_parsed = 0;
377         const char *next = skip_white_spaces(entry);
378         char type;
379
380         /* Expect <CORE> or [sX][cY][h]. At least one parameter is required. */
381         while (*next != '\0') {
382                 /* If everything parsed nothing should left */
383                 if (s_parsed && c_parsed && h_parsed)
384                         return -EINVAL;
385
386                 type = *next;
387                 switch (type) {
388                 case 's':
389                 case 'S':
390                         if (s_parsed || c_parsed || h_parsed)
391                                 return -EINVAL;
392                         s_parsed = 1;
393                         next++;
394                         break;
395                 case 'c':
396                 case 'C':
397                         if (c_parsed || h_parsed)
398                                 return -EINVAL;
399                         c_parsed = 1;
400                         next++;
401                         break;
402                 case 'h':
403                 case 'H':
404                         if (h_parsed)
405                                 return -EINVAL;
406                         h_parsed = 1;
407                         next++;
408                         break;
409                 default:
410                         /* If it start from digit it must be only core id. */
411                         if (!isdigit(*next) || s_parsed || c_parsed || h_parsed)
412                                 return -EINVAL;
413
414                         type = 'C';
415                 }
416
417                 for (num_len = 0; *next != '\0'; next++, num_len++) {
418                         if (num_len == RTE_DIM(num))
419                                 return -EINVAL;
420
421                         if (!isdigit(*next))
422                                 break;
423
424                         num[num_len] = *next;
425                 }
426
427                 if (num_len == 0 && type != 'h' && type != 'H')
428                         return -EINVAL;
429
430                 if (num_len != 0 && (type == 'h' || type == 'H'))
431                         return -EINVAL;
432
433                 num[num_len] = '\0';
434                 val = strtol(num, NULL, 10);
435
436                 h = 0;
437                 switch (type) {
438                 case 's':
439                 case 'S':
440                         s = val;
441                         break;
442                 case 'c':
443                 case 'C':
444                         c = val;
445                         break;
446                 case 'h':
447                 case 'H':
448                         h = 1;
449                         break;
450                 }
451         }
452
453         *socket = s;
454         *core = c;
455         *ht = h;
456         return 0;
457 }
458
459 static uint32_t
460 get_hex_val(char c)
461 {
462         switch (c) {
463         case '0': case '1': case '2': case '3': case '4': case '5':
464         case '6': case '7': case '8': case '9':
465                 return c - '0';
466         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
467                 return c - 'A' + 10;
468         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
469                 return c - 'a' + 10;
470         default:
471                 return 0;
472         }
473 }
474
475 int
476 parse_hex_string(char *src, uint8_t *dst, uint32_t *size)
477 {
478         char *c;
479         uint32_t len, i;
480
481         /* Check input parameters */
482         if ((src == NULL) ||
483                 (dst == NULL) ||
484                 (size == NULL) ||
485                 (*size == 0))
486                 return -1;
487
488         len = strlen(src);
489         if (((len & 3) != 0) ||
490                 (len > (*size) * 2))
491                 return -1;
492         *size = len / 2;
493
494         for (c = src; *c != 0; c++) {
495                 if ((((*c) >= '0') && ((*c) <= '9')) ||
496                         (((*c) >= 'A') && ((*c) <= 'F')) ||
497                         (((*c) >= 'a') && ((*c) <= 'f')))
498                         continue;
499
500                 return -1;
501         }
502
503         /* Convert chars to bytes */
504         for (i = 0; i < *size; i++)
505                 dst[i] = get_hex_val(src[2 * i]) * 16 +
506                         get_hex_val(src[2 * i + 1]);
507
508         return 0;
509 }
510
511 static size_t
512 skip_digits(const char *src)
513 {
514         size_t i;
515
516         for (i = 0; isdigit(src[i]); i++);
517
518         return i;
519 }
520
521 static int
522 validate_name(const char *name, const char *prefix, int num)
523 {
524         size_t i, j;
525
526         for (i = 0; (name[i] != '\0') && (prefix[i] != '\0'); i++) {
527                 if (name[i] != prefix[i])
528                         return -1;
529         }
530
531         if (prefix[i] != '\0')
532                 return -1;
533
534         if (!num) {
535                 if (name[i] != '\0')
536                         return -1;
537                 else
538                         return 0;
539         }
540
541         if (num == 2) {
542                 j = skip_digits(&name[i]);
543                 i += j;
544                 if ((j == 0) || (name[i] != '.'))
545                         return -1;
546                 i++;
547         }
548
549         if (num == 1) {
550                 j = skip_digits(&name[i]);
551                 i += j;
552                 if ((j == 0) || (name[i] != '\0'))
553                         return -1;
554         }
555
556         return 0;
557 }
558
559 static void
560 parse_eal(struct app_params *app,
561         const char *section_name,
562         struct rte_cfgfile *cfg)
563 {
564         struct app_eal_params *p = &app->eal_params;
565         struct rte_cfgfile_entry *entries;
566         int n_entries, i;
567
568         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
569         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
570
571         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
572         PARSE_ERROR_MALLOC(entries != NULL);
573
574         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
575
576         for (i = 0; i < n_entries; i++) {
577                 struct rte_cfgfile_entry *entry = &entries[i];
578
579                 /* coremask */
580                 if (strcmp(entry->name, "c") == 0) {
581                         PARSE_WARNING_IGNORED(0, section_name, entry->name);
582                         continue;
583                 }
584
585                 /* corelist */
586                 if (strcmp(entry->name, "l") == 0) {
587                         PARSE_WARNING_IGNORED(0, section_name, entry->name);
588                         continue;
589                 }
590
591                 /* coremap */
592                 if (strcmp(entry->name, "lcores") == 0) {
593                         PARSE_ERROR_DUPLICATE((p->coremap == NULL),
594                                 section_name,
595                                 entry->name);
596                         p->coremap = strdup(entry->value);
597                         continue;
598                 }
599
600                 /* master_lcore */
601                 if (strcmp(entry->name, "master_lcore") == 0) {
602                         int status;
603
604                         PARSE_ERROR_DUPLICATE((p->master_lcore_present == 0),
605                                 section_name,
606                                 entry->name);
607                         p->master_lcore_present = 1;
608
609                         status = parser_read_uint32(&p->master_lcore,
610                                 entry->value);
611                         PARSE_ERROR((status == 0), section_name, entry->name);
612                         continue;
613                 }
614
615                 /* channels */
616                 if (strcmp(entry->name, "n") == 0) {
617                         int status;
618
619                         PARSE_ERROR_DUPLICATE((p->channels_present == 0),
620                                 section_name,
621                                 entry->name);
622                         p->channels_present = 1;
623
624                         status = parser_read_uint32(&p->channels, entry->value);
625                         PARSE_ERROR((status == 0), section_name, entry->name);
626                         continue;
627                 }
628
629                 /* memory */
630                 if (strcmp(entry->name, "m") == 0) {
631                         int status;
632
633                         PARSE_ERROR_DUPLICATE((p->memory_present == 0),
634                                 section_name,
635                                 entry->name);
636                         p->memory_present = 1;
637
638                         status = parser_read_uint32(&p->memory, entry->value);
639                         PARSE_ERROR((status == 0), section_name, entry->name);
640                         continue;
641                 }
642
643                 /* ranks */
644                 if (strcmp(entry->name, "r") == 0) {
645                         int status;
646
647                         PARSE_ERROR_DUPLICATE((p->ranks_present == 0),
648                                 section_name,
649                                 entry->name);
650                         p->ranks_present = 1;
651
652                         status = parser_read_uint32(&p->ranks, entry->value);
653                         PARSE_ERROR((status == 0), section_name, entry->name);
654                         continue;
655                 }
656
657                 /* pci_blacklist */
658                 if ((strcmp(entry->name, "pci_blacklist") == 0) ||
659                         (strcmp(entry->name, "b") == 0)) {
660                         PARSE_ERROR_DUPLICATE((p->pci_blacklist == NULL),
661                                 section_name,
662                                 entry->name);
663                         p->pci_blacklist = strdup(entry->value);
664                         continue;
665                 }
666
667                 /* pci_whitelist */
668                 if ((strcmp(entry->name, "pci_whitelist") == 0) ||
669                         (strcmp(entry->name, "w") == 0)) {
670                         PARSE_ERROR_DUPLICATE((p->pci_whitelist == NULL),
671                                 section_name,
672                                 entry->name);
673                         p->pci_whitelist = strdup(entry->value);
674                         continue;
675                 }
676
677                 /* vdev */
678                 if (strcmp(entry->name, "vdev") == 0) {
679                         PARSE_ERROR_DUPLICATE((p->vdev == NULL),
680                                 section_name,
681                                 entry->name);
682                         p->vdev = strdup(entry->value);
683                         continue;
684                 }
685
686                 /* vmware_tsc_map */
687                 if (strcmp(entry->name, "vmware_tsc_map") == 0) {
688                         int val;
689
690                         PARSE_ERROR_DUPLICATE((p->vmware_tsc_map_present == 0),
691                                 section_name,
692                                 entry->name);
693                         p->vmware_tsc_map_present = 1;
694
695                         val = parser_read_arg_bool(entry->value);
696                         PARSE_ERROR((val >= 0), section_name, entry->name);
697                         p->vmware_tsc_map = val;
698                         continue;
699                 }
700
701                 /* proc_type */
702                 if (strcmp(entry->name, "proc_type") == 0) {
703                         PARSE_ERROR_DUPLICATE((p->proc_type == NULL),
704                                 section_name,
705                                 entry->name);
706                         p->proc_type = strdup(entry->value);
707                         continue;
708                 }
709
710                 /* syslog */
711                 if (strcmp(entry->name, "syslog") == 0) {
712                         PARSE_ERROR_DUPLICATE((p->syslog == NULL),
713                                 section_name,
714                                 entry->name);
715                         p->syslog = strdup(entry->value);
716                         continue;
717                 }
718
719                 /* log_level */
720                 if (strcmp(entry->name, "log_level") == 0) {
721                         int status;
722
723                         PARSE_ERROR_DUPLICATE((p->log_level_present == 0),
724                                 section_name,
725                                 entry->name);
726                         p->log_level_present = 1;
727
728                         status = parser_read_uint32(&p->log_level,
729                                 entry->value);
730                         PARSE_ERROR((status == 0), section_name, entry->name);
731                         continue;
732                 }
733
734                 /* version */
735                 if (strcmp(entry->name, "v") == 0) {
736                         int val;
737
738                         PARSE_ERROR_DUPLICATE((p->version_present == 0),
739                                 section_name,
740                                 entry->name);
741                         p->version_present = 1;
742
743                         val = parser_read_arg_bool(entry->value);
744                         PARSE_ERROR((val >= 0), section_name, entry->name);
745                         p->version = val;
746                         continue;
747                 }
748
749                 /* help */
750                 if ((strcmp(entry->name, "help") == 0) ||
751                         (strcmp(entry->name, "h") == 0)) {
752                         int val;
753
754                         PARSE_ERROR_DUPLICATE((p->help_present == 0),
755                                 section_name,
756                                 entry->name);
757                         p->help_present = 1;
758
759                         val = parser_read_arg_bool(entry->value);
760                         PARSE_ERROR((val >= 0), section_name, entry->name);
761                         p->help = val;
762                         continue;
763                 }
764
765                 /* no_huge */
766                 if (strcmp(entry->name, "no_huge") == 0) {
767                         int val;
768
769                         PARSE_ERROR_DUPLICATE((p->no_huge_present == 0),
770                                 section_name,
771                                 entry->name);
772                         p->no_huge_present = 1;
773
774                         val = parser_read_arg_bool(entry->value);
775                         PARSE_ERROR((val >= 0), section_name, entry->name);
776                         p->no_huge = val;
777                         continue;
778                 }
779
780                 /* no_pci */
781                 if (strcmp(entry->name, "no_pci") == 0) {
782                         int val;
783
784                         PARSE_ERROR_DUPLICATE((p->no_pci_present == 0),
785                                 section_name,
786                                 entry->name);
787                         p->no_pci_present = 1;
788
789                         val = parser_read_arg_bool(entry->value);
790                         PARSE_ERROR((val >= 0), section_name, entry->name);
791                         p->no_pci = val;
792                         continue;
793                 }
794
795                 /* no_hpet */
796                 if (strcmp(entry->name, "no_hpet") == 0) {
797                         int val;
798
799                         PARSE_ERROR_DUPLICATE((p->no_hpet_present == 0),
800                                 section_name,
801                                 entry->name);
802                         p->no_hpet_present = 1;
803
804                         val = parser_read_arg_bool(entry->value);
805                         PARSE_ERROR((val >= 0), section_name, entry->name);
806                         p->no_hpet = val;
807                         continue;
808                 }
809
810                 /* no_shconf */
811                 if (strcmp(entry->name, "no_shconf") == 0) {
812                         int val;
813
814                         PARSE_ERROR_DUPLICATE((p->no_shconf_present == 0),
815                                 section_name,
816                                 entry->name);
817                         p->no_shconf_present = 1;
818
819                         val = parser_read_arg_bool(entry->value);
820                         PARSE_ERROR((val >= 0), section_name, entry->name);
821                         p->no_shconf = val;
822                         continue;
823                 }
824
825                 /* add_driver */
826                 if (strcmp(entry->name, "d") == 0) {
827                         PARSE_ERROR_DUPLICATE((p->add_driver == NULL),
828                                 section_name,
829                                 entry->name);
830                         p->add_driver = strdup(entry->value);
831                         continue;
832                 }
833
834                 /* socket_mem */
835                 if (strcmp(entry->name, "socket_mem") == 0) {
836                         PARSE_ERROR_DUPLICATE((p->socket_mem == NULL),
837                                 section_name,
838                                 entry->name);
839                         p->socket_mem = strdup(entry->value);
840                         continue;
841                 }
842
843                 /* huge_dir */
844                 if (strcmp(entry->name, "huge_dir") == 0) {
845                         PARSE_ERROR_DUPLICATE((p->huge_dir == NULL),
846                                 section_name,
847                                 entry->name);
848                         p->huge_dir = strdup(entry->value);
849                         continue;
850                 }
851
852                 /* file_prefix */
853                 if (strcmp(entry->name, "file_prefix") == 0) {
854                         PARSE_ERROR_DUPLICATE((p->file_prefix == NULL),
855                                 section_name,
856                                 entry->name);
857                         p->file_prefix = strdup(entry->value);
858                         continue;
859                 }
860
861                 /* base_virtaddr */
862                 if (strcmp(entry->name, "base_virtaddr") == 0) {
863                         PARSE_ERROR_DUPLICATE((p->base_virtaddr == NULL),
864                                 section_name,
865                                 entry->name);
866                         p->base_virtaddr = strdup(entry->value);
867                         continue;
868                 }
869
870                 /* create_uio_dev */
871                 if (strcmp(entry->name, "create_uio_dev") == 0) {
872                         int val;
873
874                         PARSE_ERROR_DUPLICATE((p->create_uio_dev_present == 0),
875                                 section_name,
876                                 entry->name);
877                         p->create_uio_dev_present = 1;
878
879                         val = parser_read_arg_bool(entry->value);
880                         PARSE_ERROR((val >= 0), section_name, entry->name);
881                         p->create_uio_dev = val;
882                         continue;
883                 }
884
885                 /* vfio_intr */
886                 if (strcmp(entry->name, "vfio_intr") == 0) {
887                         PARSE_ERROR_DUPLICATE((p->vfio_intr == NULL),
888                                 section_name,
889                                 entry->name);
890                         p->vfio_intr = strdup(entry->value);
891                         continue;
892                 }
893
894                 /* xen_dom0 */
895                 if (strcmp(entry->name, "xen_dom0") == 0) {
896                         int val;
897
898                         PARSE_ERROR_DUPLICATE((p->xen_dom0_present == 0),
899                                 section_name,
900                                 entry->name);
901                         p->xen_dom0_present = 1;
902
903                         val = parser_read_arg_bool(entry->value);
904                         PARSE_ERROR((val >= 0), section_name, entry->name);
905                         p->xen_dom0 = val;
906                         continue;
907                 }
908
909                 /* unrecognized */
910                 PARSE_ERROR_INVALID(0, section_name, entry->name);
911         }
912
913         free(entries);
914 }
915
916 static int
917 parse_pipeline_pktq_in(struct app_params *app,
918         struct app_pipeline_params *p,
919         const char *value)
920 {
921         const char *next = value;
922         char *end;
923         char name[APP_PARAM_NAME_SIZE];
924         size_t name_len;
925
926         while (*next != '\0') {
927                 enum app_pktq_in_type type;
928                 int id;
929                 char *end_space;
930                 char *end_tab;
931
932                 next = skip_white_spaces(next);
933                 if (!next)
934                         break;
935
936                 end_space = strchr(next, ' ');
937                 end_tab = strchr(next, '        ');
938
939                 if (end_space && (!end_tab))
940                         end = end_space;
941                 else if ((!end_space) && end_tab)
942                         end = end_tab;
943                 else if (end_space && end_tab)
944                         end = RTE_MIN(end_space, end_tab);
945                 else
946                         end = NULL;
947
948                 if (!end)
949                         name_len = strlen(next);
950                 else
951                         name_len = end - next;
952
953                 if (name_len == 0 || name_len == sizeof(name))
954                         return -EINVAL;
955
956                 strncpy(name, next, name_len);
957                 name[name_len] = '\0';
958                 next += name_len;
959                 if (*next != '\0')
960                         next++;
961
962                 if (validate_name(name, "RXQ", 2) == 0) {
963                         type = APP_PKTQ_IN_HWQ;
964                         id = APP_PARAM_ADD(app->hwq_in_params, name);
965                 } else if (validate_name(name, "SWQ", 1) == 0) {
966                         type = APP_PKTQ_IN_SWQ;
967                         id = APP_PARAM_ADD(app->swq_params, name);
968                 } else if (validate_name(name, "TM", 1) == 0) {
969                         type = APP_PKTQ_IN_TM;
970                         id = APP_PARAM_ADD(app->tm_params, name);
971                 } else if (validate_name(name, "SOURCE", 1) == 0) {
972                         type = APP_PKTQ_IN_SOURCE;
973                         id = APP_PARAM_ADD(app->source_params, name);
974                 } else
975                         return -EINVAL;
976
977                 if (id < 0)
978                         return id;
979
980                 p->pktq_in[p->n_pktq_in].type = type;
981                 p->pktq_in[p->n_pktq_in].id = (uint32_t) id;
982                 p->n_pktq_in++;
983         }
984
985         return 0;
986 }
987
988 static int
989 parse_pipeline_pktq_out(struct app_params *app,
990         struct app_pipeline_params *p,
991         const char *value)
992 {
993         const char *next = value;
994         char *end;
995         char name[APP_PARAM_NAME_SIZE];
996         size_t name_len;
997
998         while (*next != '\0') {
999                 enum app_pktq_out_type type;
1000                 int id;
1001                 char *end_space;
1002                 char *end_tab;
1003
1004                 next = skip_white_spaces(next);
1005                 if (!next)
1006                         break;
1007
1008                 end_space = strchr(next, ' ');
1009                 end_tab = strchr(next, '        ');
1010
1011                 if (end_space && (!end_tab))
1012                         end = end_space;
1013                 else if ((!end_space) && end_tab)
1014                         end = end_tab;
1015                 else if (end_space && end_tab)
1016                         end = RTE_MIN(end_space, end_tab);
1017                 else
1018                         end = NULL;
1019
1020                 if (!end)
1021                         name_len = strlen(next);
1022                 else
1023                         name_len = end - next;
1024
1025                 if (name_len == 0 || name_len == sizeof(name))
1026                         return -EINVAL;
1027
1028                 strncpy(name, next, name_len);
1029                 name[name_len] = '\0';
1030                 next += name_len;
1031                 if (*next != '\0')
1032                         next++;
1033                 if (validate_name(name, "TXQ", 2) == 0) {
1034                         type = APP_PKTQ_OUT_HWQ;
1035                         id = APP_PARAM_ADD(app->hwq_out_params, name);
1036                 } else if (validate_name(name, "SWQ", 1) == 0) {
1037                         type = APP_PKTQ_OUT_SWQ;
1038                         id = APP_PARAM_ADD(app->swq_params, name);
1039                 } else if (validate_name(name, "TM", 1) == 0) {
1040                         type = APP_PKTQ_OUT_TM;
1041                         id = APP_PARAM_ADD(app->tm_params, name);
1042                 } else if (validate_name(name, "SINK", 1) == 0) {
1043                         type = APP_PKTQ_OUT_SINK;
1044                         id = APP_PARAM_ADD(app->sink_params, name);
1045                 } else
1046                         return -EINVAL;
1047
1048                 if (id < 0)
1049                         return id;
1050
1051                 p->pktq_out[p->n_pktq_out].type = type;
1052                 p->pktq_out[p->n_pktq_out].id = id;
1053                 p->n_pktq_out++;
1054         }
1055
1056         return 0;
1057 }
1058
1059 static int
1060 parse_pipeline_msgq_in(struct app_params *app,
1061         struct app_pipeline_params *p,
1062         const char *value)
1063 {
1064         const char *next = value;
1065         char *end;
1066         char name[APP_PARAM_NAME_SIZE];
1067         size_t name_len;
1068         ssize_t idx;
1069
1070         while (*next != '\0') {
1071                 char *end_space;
1072                 char *end_tab;
1073
1074                 next = skip_white_spaces(next);
1075                 if (!next)
1076                         break;
1077
1078                 end_space = strchr(next, ' ');
1079                 end_tab = strchr(next, '        ');
1080
1081                 if (end_space && (!end_tab))
1082                         end = end_space;
1083                 else if ((!end_space) && end_tab)
1084                         end = end_tab;
1085                 else if (end_space && end_tab)
1086                         end = RTE_MIN(end_space, end_tab);
1087                 else
1088                         end = NULL;
1089
1090                 if (!end)
1091                         name_len = strlen(next);
1092                 else
1093                         name_len = end - next;
1094
1095                 if (name_len == 0 || name_len == sizeof(name))
1096                         return -EINVAL;
1097
1098                 strncpy(name, next, name_len);
1099                 name[name_len] = '\0';
1100                 next += name_len;
1101                 if (*next != '\0')
1102                         next++;
1103
1104                 if (validate_name(name, "MSGQ", 1) != 0)
1105                         return -EINVAL;
1106
1107                 idx = APP_PARAM_ADD(app->msgq_params, name);
1108                 if (idx < 0)
1109                         return idx;
1110
1111                 p->msgq_in[p->n_msgq_in] = idx;
1112                 p->n_msgq_in++;
1113         }
1114
1115         return 0;
1116 }
1117
1118 static int
1119 parse_pipeline_msgq_out(struct app_params *app,
1120         struct app_pipeline_params *p,
1121         const char *value)
1122 {
1123         const char *next = value;
1124         char *end;
1125         char name[APP_PARAM_NAME_SIZE];
1126         size_t name_len;
1127         ssize_t idx;
1128
1129         while (*next != '\0') {
1130                 char *end_space;
1131                 char *end_tab;
1132
1133                 next = skip_white_spaces(next);
1134                 if (!next)
1135                         break;
1136
1137                 end_space = strchr(next, ' ');
1138                 end_tab = strchr(next, '        ');
1139
1140                 if (end_space && (!end_tab))
1141                         end = end_space;
1142                 else if ((!end_space) && end_tab)
1143                         end = end_tab;
1144                 else if (end_space && end_tab)
1145                         end = RTE_MIN(end_space, end_tab);
1146                 else
1147                         end = NULL;
1148
1149                 if (!end)
1150                         name_len = strlen(next);
1151                 else
1152                         name_len = end - next;
1153
1154                 if (name_len == 0 || name_len == sizeof(name))
1155                         return -EINVAL;
1156
1157                 strncpy(name, next, name_len);
1158                 name[name_len] = '\0';
1159                 next += name_len;
1160                 if (*next != '\0')
1161                         next++;
1162
1163                 if (validate_name(name, "MSGQ", 1) != 0)
1164                         return -EINVAL;
1165
1166                 idx = APP_PARAM_ADD(app->msgq_params, name);
1167                 if (idx < 0)
1168                         return idx;
1169
1170                 p->msgq_out[p->n_msgq_out] = idx;
1171                 p->n_msgq_out++;
1172         }
1173
1174         return 0;
1175 }
1176
1177 static void
1178 parse_pipeline(struct app_params *app,
1179         const char *section_name,
1180         struct rte_cfgfile *cfg)
1181 {
1182         char name[CFG_NAME_LEN];
1183         struct app_pipeline_params *param;
1184         struct rte_cfgfile_entry *entries;
1185         ssize_t param_idx;
1186         int n_entries, i;
1187
1188         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1189         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1190
1191         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1192         PARSE_ERROR_MALLOC(entries != NULL);
1193
1194         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1195
1196         param_idx = APP_PARAM_ADD(app->pipeline_params, section_name);
1197         PARSER_PARAM_ADD_CHECK(param_idx, app->pipeline_params, section_name);
1198
1199         param = &app->pipeline_params[param_idx];
1200
1201         for (i = 0; i < n_entries; i++) {
1202                 struct rte_cfgfile_entry *ent = &entries[i];
1203
1204                 if (strcmp(ent->name, "type") == 0) {
1205                         int w_size = snprintf(param->type, RTE_DIM(param->type),
1206                                         "%s", ent->value);
1207
1208                         PARSE_ERROR(((w_size > 0) &&
1209                                 (w_size < (int)RTE_DIM(param->type))),
1210                                 section_name,
1211                                 ent->name);
1212                         continue;
1213                 }
1214
1215                 if (strcmp(ent->name, "core") == 0) {
1216                         int status = parse_pipeline_core(
1217                                 &param->socket_id, &param->core_id,
1218                                 &param->hyper_th_id, ent->value);
1219
1220                         PARSE_ERROR((status == 0), section_name,
1221                                 ent->name);
1222                         continue;
1223                 }
1224
1225                 if (strcmp(ent->name, "pktq_in") == 0) {
1226                         int status = parse_pipeline_pktq_in(app, param,
1227                                 ent->value);
1228
1229                         PARSE_ERROR((status == 0), section_name,
1230                                 ent->name);
1231                         continue;
1232                 }
1233
1234                 if (strcmp(ent->name, "pktq_out") == 0) {
1235                         int status = parse_pipeline_pktq_out(app, param,
1236                                 ent->value);
1237
1238                         PARSE_ERROR((status == 0), section_name,
1239                                 ent->name);
1240                         continue;
1241                 }
1242
1243                 if (strcmp(ent->name, "msgq_in") == 0) {
1244                         int status = parse_pipeline_msgq_in(app, param,
1245                                 ent->value);
1246
1247                         PARSE_ERROR((status == 0), section_name,
1248                                 ent->name);
1249                         continue;
1250                 }
1251
1252                 if (strcmp(ent->name, "msgq_out") == 0) {
1253                         int status = parse_pipeline_msgq_out(app, param,
1254                                 ent->value);
1255
1256                         PARSE_ERROR((status == 0), section_name,
1257                                 ent->name);
1258                         continue;
1259                 }
1260
1261                 if (strcmp(ent->name, "timer_period") == 0) {
1262                         int status = parser_read_uint32(
1263                                 &param->timer_period,
1264                                 ent->value);
1265
1266                         PARSE_ERROR((status == 0), section_name,
1267                                 ent->name);
1268                         continue;
1269                 }
1270
1271                 /* pipeline type specific items */
1272                 APP_CHECK((param->n_args < APP_MAX_PIPELINE_ARGS),
1273                         "Parse error in section \"%s\": too many "
1274                         "pipeline specified parameters", section_name);
1275
1276                 param->args_name[param->n_args] = strdup(ent->name);
1277                 param->args_value[param->n_args] = strdup(ent->value);
1278
1279                 APP_CHECK((param->args_name[param->n_args] != NULL) &&
1280                         (param->args_value[param->n_args] != NULL),
1281                         "Parse error: no free memory");
1282
1283                 param->n_args++;
1284         }
1285
1286         param->parsed = 1;
1287
1288         snprintf(name, sizeof(name), "MSGQ-REQ-%s", section_name);
1289         param_idx = APP_PARAM_ADD(app->msgq_params, name);
1290         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, name);
1291         app->msgq_params[param_idx].cpu_socket_id = param->socket_id;
1292         param->msgq_in[param->n_msgq_in++] = param_idx;
1293
1294         snprintf(name, sizeof(name), "MSGQ-RSP-%s", section_name);
1295         param_idx = APP_PARAM_ADD(app->msgq_params, name);
1296         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, name);
1297         app->msgq_params[param_idx].cpu_socket_id = param->socket_id;
1298         param->msgq_out[param->n_msgq_out++] = param_idx;
1299
1300         snprintf(name, sizeof(name), "MSGQ-REQ-CORE-s%" PRIu32 "c%" PRIu32 "%s",
1301                 param->socket_id,
1302                 param->core_id,
1303                 (param->hyper_th_id) ? "h" : "");
1304         param_idx = APP_PARAM_ADD(app->msgq_params, name);
1305         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, name);
1306         app->msgq_params[param_idx].cpu_socket_id = param->socket_id;
1307
1308         snprintf(name, sizeof(name), "MSGQ-RSP-CORE-s%" PRIu32 "c%" PRIu32 "%s",
1309                 param->socket_id,
1310                 param->core_id,
1311                 (param->hyper_th_id) ? "h" : "");
1312         param_idx = APP_PARAM_ADD(app->msgq_params, name);
1313         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, name);
1314         app->msgq_params[param_idx].cpu_socket_id = param->socket_id;
1315
1316         free(entries);
1317 }
1318
1319 static void
1320 parse_mempool(struct app_params *app,
1321         const char *section_name,
1322         struct rte_cfgfile *cfg)
1323 {
1324         struct app_mempool_params *param;
1325         struct rte_cfgfile_entry *entries;
1326         ssize_t param_idx;
1327         int n_entries, i;
1328
1329         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1330         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1331
1332         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1333         PARSE_ERROR_MALLOC(entries != NULL);
1334
1335         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1336
1337         param_idx = APP_PARAM_ADD(app->mempool_params, section_name);
1338         PARSER_PARAM_ADD_CHECK(param_idx, app->mempool_params, section_name);
1339
1340         param = &app->mempool_params[param_idx];
1341
1342         for (i = 0; i < n_entries; i++) {
1343                 struct rte_cfgfile_entry *ent = &entries[i];
1344
1345                 if (strcmp(ent->name, "buffer_size") == 0) {
1346                         int status = parser_read_uint32(
1347                                 &param->buffer_size, ent->value);
1348
1349                         PARSE_ERROR((status == 0), section_name,
1350                                 ent->name);
1351                         continue;
1352                 }
1353
1354                 if (strcmp(ent->name, "pool_size") == 0) {
1355                         int status = parser_read_uint32(
1356                                 &param->pool_size, ent->value);
1357
1358                         PARSE_ERROR((status == 0), section_name,
1359                                 ent->name);
1360                         continue;
1361                 }
1362
1363                 if (strcmp(ent->name, "cache_size") == 0) {
1364                         int status = parser_read_uint32(
1365                                 &param->cache_size, ent->value);
1366
1367                         PARSE_ERROR((status == 0), section_name,
1368                                 ent->name);
1369                         continue;
1370                 }
1371
1372                 if (strcmp(ent->name, "cpu") == 0) {
1373                         int status = parser_read_uint32(
1374                                 &param->cpu_socket_id, ent->value);
1375
1376                         PARSE_ERROR((status == 0), section_name,
1377                                 ent->name);
1378                         continue;
1379                 }
1380
1381                 /* unrecognized */
1382                 PARSE_ERROR_INVALID(0, section_name, ent->name);
1383         }
1384
1385         param->parsed = 1;
1386
1387         free(entries);
1388 }
1389
1390 static void
1391 parse_link(struct app_params *app,
1392         const char *section_name,
1393         struct rte_cfgfile *cfg)
1394 {
1395         struct app_link_params *param;
1396         struct rte_cfgfile_entry *entries;
1397         int n_entries, i;
1398         ssize_t param_idx;
1399
1400         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1401         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1402
1403         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1404         PARSE_ERROR_MALLOC(entries != NULL);
1405
1406         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1407
1408         param_idx = APP_PARAM_ADD(app->link_params, section_name);
1409         PARSER_PARAM_ADD_CHECK(param_idx, app->link_params, section_name);
1410
1411         param = &app->link_params[param_idx];
1412
1413         for (i = 0; i < n_entries; i++) {
1414                 struct rte_cfgfile_entry *ent = &entries[i];
1415
1416                 if (strcmp(ent->name, "promisc") == 0) {
1417                         int status = parser_read_arg_bool(ent->value);
1418
1419                         PARSE_ERROR((status != -EINVAL), section_name,
1420                                 ent->name);
1421                         param->promisc = status;
1422                         continue;
1423                 }
1424
1425                 if (strcmp(ent->name, "arp_q") == 0) {
1426                         int status = parser_read_uint32(&param->arp_q,
1427                                 ent->value);
1428
1429                         PARSE_ERROR((status == 0), section_name,
1430                                 ent->name);
1431                         continue;
1432                 }
1433
1434                 if (strcmp(ent->name, "tcp_syn_q") == 0) {
1435                         int status = parser_read_uint32(
1436                                 &param->tcp_syn_local_q, ent->value);
1437
1438                         PARSE_ERROR((status == 0), section_name, ent->name);
1439                         continue;
1440                 }
1441
1442                 if (strcmp(ent->name, "ip_local_q") == 0) {
1443                         int status = parser_read_uint32(
1444                                 &param->ip_local_q, ent->value);
1445
1446                         PARSE_ERROR((status == 0), section_name,
1447                                 ent->name);
1448                         continue;
1449                 }
1450
1451
1452                 if (strcmp(ent->name, "tcp_local_q") == 0) {
1453                         int status = parser_read_uint32(
1454                                 &param->tcp_local_q, ent->value);
1455
1456                         PARSE_ERROR((status == 0), section_name,
1457                                 ent->name);
1458                         continue;
1459                 }
1460
1461                 if (strcmp(ent->name, "udp_local_q") == 0) {
1462                         int status = parser_read_uint32(
1463                                 &param->udp_local_q, ent->value);
1464
1465                         PARSE_ERROR((status == 0), section_name,
1466                                 ent->name);
1467                         continue;
1468                 }
1469
1470                 if (strcmp(ent->name, "sctp_local_q") == 0) {
1471                         int status = parser_read_uint32(
1472                                 &param->sctp_local_q, ent->value);
1473
1474                         PARSE_ERROR((status == 0), section_name,
1475                                 ent->name);
1476                         continue;
1477                 }
1478
1479                 /* unrecognized */
1480                 PARSE_ERROR_INVALID(0, section_name, ent->name);
1481         }
1482
1483         param->parsed = 1;
1484
1485         free(entries);
1486 }
1487
1488 static void
1489 parse_rxq(struct app_params *app,
1490         const char *section_name,
1491         struct rte_cfgfile *cfg)
1492 {
1493         struct app_pktq_hwq_in_params *param;
1494         struct rte_cfgfile_entry *entries;
1495         int n_entries, i;
1496         ssize_t param_idx;
1497
1498         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1499         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1500
1501         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1502         PARSE_ERROR_MALLOC(entries != NULL);
1503
1504         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1505
1506         param_idx = APP_PARAM_ADD(app->hwq_in_params, section_name);
1507         PARSER_PARAM_ADD_CHECK(param_idx, app->hwq_in_params, section_name);
1508
1509         param = &app->hwq_in_params[param_idx];
1510
1511         for (i = 0; i < n_entries; i++) {
1512                 struct rte_cfgfile_entry *ent = &entries[i];
1513
1514                 if (strcmp(ent->name, "mempool") == 0) {
1515                         int status = validate_name(ent->value,
1516                                 "MEMPOOL", 1);
1517                         ssize_t idx;
1518
1519                         PARSE_ERROR((status == 0), section_name,
1520                                 ent->name);
1521                         idx = APP_PARAM_ADD(app->mempool_params,
1522                                 ent->value);
1523                         PARSER_PARAM_ADD_CHECK(idx, app->mempool_params,
1524                                 section_name);
1525                         param->mempool_id = idx;
1526                         continue;
1527                 }
1528
1529                 if (strcmp(ent->name, "size") == 0) {
1530                         int status = parser_read_uint32(&param->size,
1531                                 ent->value);
1532
1533                         PARSE_ERROR((status == 0), section_name,
1534                                 ent->name);
1535                         continue;
1536                 }
1537
1538                 if (strcmp(ent->name, "burst") == 0) {
1539                         int status = parser_read_uint32(&param->burst,
1540                                 ent->value);
1541
1542                         PARSE_ERROR((status == 0), section_name,
1543                                 ent->name);
1544                         continue;
1545                 }
1546
1547                 /* unrecognized */
1548                 PARSE_ERROR_INVALID(0, section_name, ent->name);
1549         }
1550
1551         param->parsed = 1;
1552
1553         free(entries);
1554 }
1555
1556 static void
1557 parse_txq(struct app_params *app,
1558         const char *section_name,
1559         struct rte_cfgfile *cfg)
1560 {
1561         struct app_pktq_hwq_out_params *param;
1562         struct rte_cfgfile_entry *entries;
1563         int n_entries, i;
1564         ssize_t param_idx;
1565
1566         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1567         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1568
1569         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1570         PARSE_ERROR_MALLOC(entries != NULL);
1571
1572         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1573
1574         param_idx = APP_PARAM_ADD(app->hwq_out_params, section_name);
1575         PARSER_PARAM_ADD_CHECK(param_idx, app->hwq_out_params, section_name);
1576
1577         param = &app->hwq_out_params[param_idx];
1578
1579         for (i = 0; i < n_entries; i++) {
1580                 struct rte_cfgfile_entry *ent = &entries[i];
1581
1582                 if (strcmp(ent->name, "size") == 0) {
1583                         int status = parser_read_uint32(&param->size,
1584                                 ent->value);
1585
1586                         PARSE_ERROR((status == 0), section_name,
1587                                 ent->name);
1588                         continue;
1589                 }
1590
1591                 if (strcmp(ent->name, "burst") == 0) {
1592                         int status = parser_read_uint32(&param->burst,
1593                                 ent->value);
1594
1595                         PARSE_ERROR((status == 0), section_name,
1596                                 ent->name);
1597                         continue;
1598                 }
1599
1600                 if (strcmp(ent->name, "dropless") == 0) {
1601                         int status = parser_read_arg_bool(ent->value);
1602
1603
1604                         PARSE_ERROR((status != -EINVAL), section_name,
1605                                 ent->name);
1606                         param->dropless = status;
1607                         continue;
1608                 }
1609
1610                 /* unrecognized */
1611                 PARSE_ERROR_INVALID(0, section_name, ent->name);
1612         }
1613
1614         param->parsed = 1;
1615
1616         free(entries);
1617 }
1618
1619 static void
1620 parse_swq(struct app_params *app,
1621         const char *section_name,
1622         struct rte_cfgfile *cfg)
1623 {
1624         struct app_pktq_swq_params *param;
1625         struct rte_cfgfile_entry *entries;
1626         int n_entries, i;
1627         uint32_t mtu_present = 0;
1628         uint32_t metadata_size_present = 0;
1629         uint32_t mempool_direct_present = 0;
1630         uint32_t mempool_indirect_present = 0;
1631
1632         ssize_t param_idx;
1633
1634         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1635         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1636
1637         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1638         PARSE_ERROR_MALLOC(entries != NULL);
1639
1640         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1641
1642         param_idx = APP_PARAM_ADD(app->swq_params, section_name);
1643         PARSER_PARAM_ADD_CHECK(param_idx, app->swq_params, section_name);
1644
1645         param = &app->swq_params[param_idx];
1646
1647         for (i = 0; i < n_entries; i++) {
1648                 struct rte_cfgfile_entry *ent = &entries[i];
1649
1650                 if (strcmp(ent->name, "size") == 0) {
1651                         int status = parser_read_uint32(&param->size,
1652                                 ent->value);
1653
1654                         PARSE_ERROR((status == 0), section_name,
1655                                 ent->name);
1656                         continue;
1657                 }
1658
1659                 if (strcmp(ent->name, "burst_read") == 0) {
1660                         int status = parser_read_uint32(&
1661                                 param->burst_read, ent->value);
1662
1663                         PARSE_ERROR((status == 0), section_name,
1664                                 ent->name);
1665                         continue;
1666                 }
1667
1668                 if (strcmp(ent->name, "burst_write") == 0) {
1669                         int status = parser_read_uint32(
1670                                 &param->burst_write, ent->value);
1671
1672                         PARSE_ERROR((status == 0), section_name,
1673                                 ent->name);
1674                         continue;
1675                 }
1676
1677                 if (strcmp(ent->name, "dropless") == 0) {
1678                         int status = parser_read_arg_bool(ent->value);
1679
1680                         PARSE_ERROR((status != -EINVAL), section_name,
1681                                 ent->name);
1682                         param->dropless = status;
1683                         continue;
1684                 }
1685
1686                 if (strcmp(ent->name, "n_retries") == 0) {
1687                         int status = parser_read_uint64(&param->n_retries,
1688                                 ent->value);
1689
1690                         PARSE_ERROR((status == 0), section_name,
1691                                 ent->name);
1692                         continue;
1693                 }
1694
1695                 if (strcmp(ent->name, "cpu") == 0) {
1696                         int status = parser_read_uint32(
1697                                 &param->cpu_socket_id, ent->value);
1698
1699                         PARSE_ERROR((status == 0), section_name, ent->name);
1700                         continue;
1701                 }
1702
1703                 if (strcmp(ent->name, "ipv4_frag") == 0) {
1704                         int status = parser_read_arg_bool(ent->value);
1705
1706                         PARSE_ERROR((status != -EINVAL), section_name,
1707                                 ent->name);
1708
1709                         param->ipv4_frag = status;
1710                         if (param->mtu == 0)
1711                                 param->mtu = 1500;
1712
1713                         continue;
1714                 }
1715
1716                 if (strcmp(ent->name, "ipv6_frag") == 0) {
1717                         int status = parser_read_arg_bool(ent->value);
1718
1719                         PARSE_ERROR((status != -EINVAL), section_name,
1720                                 ent->name);
1721                         param->ipv6_frag = status;
1722                         if (param->mtu == 0)
1723                                 param->mtu = 1320;
1724                         continue;
1725                 }
1726
1727                 if (strcmp(ent->name, "ipv4_ras") == 0) {
1728                         int status = parser_read_arg_bool(ent->value);
1729
1730                         PARSE_ERROR((status != -EINVAL), section_name,
1731                                 ent->name);
1732                         param->ipv4_ras = status;
1733                         continue;
1734                 }
1735
1736                 if (strcmp(ent->name, "ipv6_ras") == 0) {
1737                         int status = parser_read_arg_bool(ent->value);
1738
1739                         PARSE_ERROR((status != -EINVAL), section_name,
1740                                 ent->name);
1741                         param->ipv6_ras = status;
1742                         continue;
1743                 }
1744
1745                 if (strcmp(ent->name, "mtu") == 0) {
1746                         int status = parser_read_uint32(&param->mtu,
1747                                         ent->value);
1748
1749                         PARSE_ERROR((status == 0), section_name,
1750                                 ent->name);
1751                         mtu_present = 1;
1752                         continue;
1753                 }
1754
1755                 if (strcmp(ent->name, "metadata_size") == 0) {
1756                         int status = parser_read_uint32(
1757                                 &param->metadata_size, ent->value);
1758
1759                         PARSE_ERROR((status == 0), section_name,
1760                                 ent->name);
1761                         metadata_size_present = 1;
1762                         continue;
1763                 }
1764
1765                 if (strcmp(ent->name, "mempool_direct") == 0) {
1766                         int status = validate_name(ent->value,
1767                                 "MEMPOOL", 1);
1768                         ssize_t idx;
1769
1770                         PARSE_ERROR((status == 0), section_name,
1771                                 ent->name);
1772
1773                         idx = APP_PARAM_ADD(app->mempool_params,
1774                                 ent->value);
1775                         PARSER_PARAM_ADD_CHECK(idx, app->mempool_params,
1776                                 section_name);
1777                         param->mempool_direct_id = idx;
1778                         mempool_direct_present = 1;
1779                         continue;
1780                 }
1781
1782                 if (strcmp(ent->name, "mempool_indirect") == 0) {
1783                         int status = validate_name(ent->value,
1784                                 "MEMPOOL", 1);
1785                         ssize_t idx;
1786
1787                         PARSE_ERROR((status == 0), section_name,
1788                                 ent->name);
1789                         idx = APP_PARAM_ADD(app->mempool_params,
1790                                 ent->value);
1791                         PARSER_PARAM_ADD_CHECK(idx, app->mempool_params,
1792                                 section_name);
1793                         param->mempool_indirect_id = idx;
1794                         mempool_indirect_present = 1;
1795                         continue;
1796                 }
1797
1798                 /* unrecognized */
1799                 PARSE_ERROR_INVALID(0, section_name, ent->name);
1800         }
1801
1802         APP_CHECK(((mtu_present) &&
1803                 ((param->ipv4_frag == 1) || (param->ipv6_frag == 1))),
1804                 "Parse error in section \"%s\": IPv4/IPv6 fragmentation "
1805                 "is off, therefore entry \"mtu\" is not allowed",
1806                 section_name);
1807
1808         APP_CHECK(((metadata_size_present) &&
1809                 ((param->ipv4_frag == 1) || (param->ipv6_frag == 1))),
1810                 "Parse error in section \"%s\": IPv4/IPv6 fragmentation "
1811                 "is off, therefore entry \"metadata_size\" is "
1812                 "not allowed", section_name);
1813
1814         APP_CHECK(((mempool_direct_present) &&
1815                 ((param->ipv4_frag == 1) || (param->ipv6_frag == 1))),
1816                 "Parse error in section \"%s\": IPv4/IPv6 fragmentation "
1817                 "is off, therefore entry \"mempool_direct\" is "
1818                 "not allowed", section_name);
1819
1820         APP_CHECK(((mempool_indirect_present) &&
1821                 ((param->ipv4_frag == 1) || (param->ipv6_frag == 1))),
1822                 "Parse error in section \"%s\": IPv4/IPv6 fragmentation "
1823                 "is off, therefore entry \"mempool_indirect\" is "
1824                 "not allowed", section_name);
1825
1826         param->parsed = 1;
1827
1828         free(entries);
1829 }
1830
1831 static void
1832 parse_tm(struct app_params *app,
1833         const char *section_name,
1834         struct rte_cfgfile *cfg)
1835 {
1836         struct app_pktq_tm_params *param;
1837         struct rte_cfgfile_entry *entries;
1838         int n_entries, i;
1839         ssize_t param_idx;
1840
1841         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1842         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1843
1844         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1845         PARSE_ERROR_MALLOC(entries != NULL);
1846
1847         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1848
1849         param_idx = APP_PARAM_ADD(app->tm_params, section_name);
1850         PARSER_PARAM_ADD_CHECK(param_idx, app->tm_params, section_name);
1851
1852         param = &app->tm_params[param_idx];
1853
1854         for (i = 0; i < n_entries; i++) {
1855                 struct rte_cfgfile_entry *ent = &entries[i];
1856
1857                 if (strcmp(ent->name, "cfg") == 0) {
1858                         param->file_name = strdup(ent->value);
1859                         PARSE_ERROR_MALLOC(param->file_name != NULL);
1860                         continue;
1861                 }
1862
1863                 if (strcmp(ent->name, "burst_read") == 0) {
1864                         int status = parser_read_uint32(
1865                                 &param->burst_read, ent->value);
1866
1867                         PARSE_ERROR((status == 0), section_name,
1868                                 ent->name);
1869                         continue;
1870                 }
1871
1872                 if (strcmp(ent->name, "burst_write") == 0) {
1873                         int status = parser_read_uint32(
1874                                 &param->burst_write, ent->value);
1875
1876                         PARSE_ERROR((status == 0), section_name,
1877                                 ent->name);
1878                         continue;
1879                 }
1880
1881                 /* unrecognized */
1882                 PARSE_ERROR_INVALID(0, section_name, ent->name);
1883         }
1884
1885         param->parsed = 1;
1886
1887         free(entries);
1888 }
1889
1890 static void
1891 parse_source(struct app_params *app,
1892         const char *section_name,
1893         struct rte_cfgfile *cfg)
1894 {
1895         struct app_pktq_source_params *param;
1896         struct rte_cfgfile_entry *entries;
1897         int n_entries, i;
1898         ssize_t param_idx;
1899
1900         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1901         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1902
1903         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1904         PARSE_ERROR_MALLOC(entries != NULL);
1905
1906         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1907
1908         param_idx = APP_PARAM_ADD(app->source_params, section_name);
1909         PARSER_PARAM_ADD_CHECK(param_idx, app->source_params, section_name);
1910
1911         param = &app->source_params[param_idx];
1912
1913         for (i = 0; i < n_entries; i++) {
1914                 struct rte_cfgfile_entry *ent = &entries[i];
1915
1916                 if (strcmp(ent->name, "mempool") == 0) {
1917                         int status = validate_name(ent->value,
1918                                 "MEMPOOL", 1);
1919                         ssize_t idx;
1920
1921                         PARSE_ERROR((status == 0), section_name,
1922                                 ent->name);
1923                         idx = APP_PARAM_ADD(app->mempool_params,
1924                                 ent->value);
1925                         PARSER_PARAM_ADD_CHECK(idx, app->mempool_params,
1926                                 section_name);
1927                         param->mempool_id = idx;
1928                         continue;
1929                 }
1930
1931                 if (strcmp(ent->name, "burst") == 0) {
1932                         int status = parser_read_uint32(&param->burst,
1933                                 ent->value);
1934
1935                         PARSE_ERROR((status == 0), section_name,
1936                                 ent->name);
1937                         continue;
1938                 }
1939
1940                 /* unrecognized */
1941                 PARSE_ERROR_INVALID(0, section_name, ent->name);
1942         }
1943
1944         param->parsed = 1;
1945
1946         free(entries);
1947 }
1948
1949 static void
1950 parse_msgq_req_pipeline(struct app_params *app,
1951         const char *section_name,
1952         struct rte_cfgfile *cfg)
1953 {
1954         struct app_msgq_params *param;
1955         struct rte_cfgfile_entry *entries;
1956         int n_entries, i;
1957         ssize_t param_idx;
1958
1959         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
1960         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
1961
1962         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
1963         PARSE_ERROR_MALLOC(entries != NULL);
1964
1965         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
1966
1967         param_idx = APP_PARAM_ADD(app->msgq_params, section_name);
1968         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, section_name);
1969
1970         param = &app->msgq_params[param_idx];
1971
1972         for (i = 0; i < n_entries; i++) {
1973                 struct rte_cfgfile_entry *ent = &entries[i];
1974
1975                 if (strcmp(ent->name, "size") == 0) {
1976                         int status = parser_read_uint32(&param->size,
1977                                 ent->value);
1978
1979                         PARSE_ERROR((status == 0), section_name,
1980                                 ent->name);
1981                         continue;
1982                 }
1983
1984                 /* unrecognized */
1985                 PARSE_ERROR_INVALID(0, section_name, ent->name);
1986         }
1987
1988         param->parsed = 1;
1989         free(entries);
1990 }
1991
1992 static void
1993 parse_msgq_rsp_pipeline(struct app_params *app,
1994         const char *section_name,
1995         struct rte_cfgfile *cfg)
1996 {
1997         struct app_msgq_params *param;
1998         struct rte_cfgfile_entry *entries;
1999         int n_entries, i;
2000         ssize_t param_idx;
2001
2002         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
2003         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
2004
2005         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
2006         PARSE_ERROR_MALLOC(entries != NULL);
2007
2008         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
2009
2010         param_idx = APP_PARAM_ADD(app->msgq_params, section_name);
2011         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, section_name);
2012
2013         param = &app->msgq_params[param_idx];
2014
2015         for (i = 0; i < n_entries; i++) {
2016                 struct rte_cfgfile_entry *ent = &entries[i];
2017
2018                 if (strcmp(ent->name, "size") == 0) {
2019                         int status = parser_read_uint32(&param->size,
2020                                 ent->value);
2021
2022                         PARSE_ERROR((status == 0), section_name,
2023                                 ent->name);
2024                         continue;
2025                 }
2026
2027                 /* unrecognized */
2028                 PARSE_ERROR_INVALID(0, section_name, ent->name);
2029         }
2030
2031         param->parsed = 1;
2032
2033         free(entries);
2034 }
2035
2036 static void
2037 parse_msgq(struct app_params *app,
2038         const char *section_name,
2039         struct rte_cfgfile *cfg)
2040 {
2041         struct app_msgq_params *param;
2042         struct rte_cfgfile_entry *entries;
2043         int n_entries, i;
2044         ssize_t param_idx;
2045
2046         n_entries = rte_cfgfile_section_num_entries(cfg, section_name);
2047         PARSE_ERROR_SECTION_NO_ENTRIES((n_entries > 0), section_name);
2048
2049         entries = malloc(n_entries * sizeof(struct rte_cfgfile_entry));
2050         PARSE_ERROR_MALLOC(entries != NULL);
2051
2052         rte_cfgfile_section_entries(cfg, section_name, entries, n_entries);
2053
2054         param_idx = APP_PARAM_ADD(app->msgq_params, section_name);
2055         PARSER_PARAM_ADD_CHECK(param_idx, app->msgq_params, section_name);
2056
2057         param = &app->msgq_params[param_idx];
2058
2059         for (i = 0; i < n_entries; i++) {
2060                 struct rte_cfgfile_entry *ent = &entries[i];
2061
2062                 if (strcmp(ent->name, "size") == 0) {
2063                         int status = parser_read_uint32(&param->size,
2064                                 ent->value);
2065
2066                         PARSE_ERROR((status == 0), section_name,
2067                                 ent->name);
2068                         continue;
2069                 }
2070
2071                 if (strcmp(ent->name, "cpu") == 0) {
2072                         int status = parser_read_uint32(
2073                                 &param->cpu_socket_id, ent->value);
2074
2075                         PARSE_ERROR((status == 0), section_name,
2076                                 ent->name);
2077                         continue;
2078                 }
2079
2080                 /* unrecognized */
2081                 PARSE_ERROR_INVALID(0, section_name, ent->name);
2082         }
2083
2084         param->parsed = 1;
2085
2086         free(entries);
2087 }
2088
2089 typedef void (*config_section_load)(struct app_params *p,
2090         const char *section_name,
2091         struct rte_cfgfile *cfg);
2092
2093 struct config_section {
2094         const char prefix[CFG_NAME_LEN];
2095         int numbers;
2096         config_section_load load;
2097 };
2098
2099 static const struct config_section cfg_file_scheme[] = {
2100         {"EAL", 0, parse_eal},
2101         {"PIPELINE", 1, parse_pipeline},
2102         {"MEMPOOL", 1, parse_mempool},
2103         {"LINK", 1, parse_link},
2104         {"RXQ", 2, parse_rxq},
2105         {"TXQ", 2, parse_txq},
2106         {"SWQ", 1, parse_swq},
2107         {"TM", 1, parse_tm},
2108         {"SOURCE", 1, parse_source},
2109         {"MSGQ-REQ-PIPELINE", 1, parse_msgq_req_pipeline},
2110         {"MSGQ-RSP-PIPELINE", 1, parse_msgq_rsp_pipeline},
2111         {"MSGQ", 1, parse_msgq},
2112 };
2113
2114 static void
2115 create_implicit_mempools(struct app_params *app)
2116 {
2117         ssize_t idx;
2118
2119         idx = APP_PARAM_ADD(app->mempool_params, "MEMPOOL0");
2120         PARSER_PARAM_ADD_CHECK(idx, app->mempool_params, "start-up");
2121 }
2122
2123 static void
2124 parse_port_mask(struct app_params *app, uint64_t port_mask)
2125 {
2126         uint32_t pmd_id, link_id;
2127
2128         link_id = 0;
2129         for (pmd_id = 0; pmd_id < RTE_MAX_ETHPORTS; pmd_id++) {
2130                 char name[APP_PARAM_NAME_SIZE];
2131                 ssize_t idx;
2132
2133                 if ((port_mask & (1LLU << pmd_id)) == 0)
2134                         continue;
2135
2136                 snprintf(name, sizeof(name), "LINK%" PRIu32, link_id);
2137                 idx = APP_PARAM_ADD(app->link_params, name);
2138                 PARSER_PARAM_ADD_CHECK(idx, app->link_params, name);
2139
2140                 app->link_params[idx].pmd_id = pmd_id;
2141                 link_id++;
2142         }
2143 }
2144
2145 int
2146 app_config_parse(struct app_params *app, const char *file_name)
2147 {
2148         struct rte_cfgfile *cfg;
2149         char **section_names;
2150         int i, j, sect_count;
2151
2152         /* Implicit mempools */
2153         create_implicit_mempools(app);
2154
2155         /* Port mask */
2156         parse_port_mask(app, app->port_mask);
2157
2158         /* Load application configuration file */
2159         cfg = rte_cfgfile_load(file_name, 0);
2160         APP_CHECK((cfg != NULL), "Parse error: Unable to load config "
2161                 "file %s", file_name);
2162
2163         sect_count = rte_cfgfile_num_sections(cfg, NULL, 0);
2164         APP_CHECK((sect_count > 0), "Parse error: number of sections "
2165                 "in file \"%s\" return %d", file_name,
2166                 sect_count);
2167
2168         section_names = malloc(sect_count * sizeof(char *));
2169         PARSE_ERROR_MALLOC(section_names != NULL);
2170
2171         for (i = 0; i < sect_count; i++)
2172                 section_names[i] = malloc(CFG_NAME_LEN);
2173
2174         rte_cfgfile_sections(cfg, section_names, sect_count);
2175
2176         for (i = 0; i < sect_count; i++) {
2177                 const struct config_section *sch_s;
2178                 int len, cfg_name_len;
2179
2180                 cfg_name_len = strlen(section_names[i]);
2181
2182                 /* Find section type */
2183                 for (j = 0; j < (int)RTE_DIM(cfg_file_scheme); j++) {
2184                         sch_s = &cfg_file_scheme[j];
2185                         len = strlen(sch_s->prefix);
2186
2187                         if (cfg_name_len < len)
2188                                 continue;
2189
2190                         /* After section name we expect only '\0' or digit or
2191                          * digit dot digit, so protect against false matching,
2192                          * for example: "ABC" should match section name
2193                          * "ABC0.0", but it should not match section_name
2194                          * "ABCDEF".
2195                          */
2196                         if ((section_names[i][len] != '\0') &&
2197                                 !isdigit(section_names[i][len]))
2198                                 continue;
2199
2200                         if (strncmp(sch_s->prefix, section_names[i], len) == 0)
2201                                 break;
2202                 }
2203
2204                 APP_CHECK(j < (int)RTE_DIM(cfg_file_scheme),
2205                         "Parse error: unknown section %s",
2206                         section_names[i]);
2207
2208                 APP_CHECK(validate_name(section_names[i],
2209                         sch_s->prefix,
2210                         sch_s->numbers) == 0,
2211                         "Parse error: invalid section name \"%s\"",
2212                         section_names[i]);
2213
2214                 sch_s->load(app, section_names[i], cfg);
2215         }
2216
2217         for (i = 0; i < sect_count; i++)
2218                 free(section_names[i]);
2219
2220         free(section_names);
2221
2222         rte_cfgfile_close(cfg);
2223
2224         APP_PARAM_COUNT(app->mempool_params, app->n_mempools);
2225         APP_PARAM_COUNT(app->link_params, app->n_links);
2226         APP_PARAM_COUNT(app->hwq_in_params, app->n_pktq_hwq_in);
2227         APP_PARAM_COUNT(app->hwq_out_params, app->n_pktq_hwq_out);
2228         APP_PARAM_COUNT(app->swq_params, app->n_pktq_swq);
2229         APP_PARAM_COUNT(app->tm_params, app->n_pktq_tm);
2230         APP_PARAM_COUNT(app->source_params, app->n_pktq_source);
2231         APP_PARAM_COUNT(app->sink_params, app->n_pktq_sink);
2232         APP_PARAM_COUNT(app->msgq_params, app->n_msgq);
2233         APP_PARAM_COUNT(app->pipeline_params, app->n_pipelines);
2234
2235         /* Save configuration to output file */
2236         app_config_save(app, app->output_file);
2237
2238         /* Load TM configuration files */
2239         app_config_parse_tm(app);
2240
2241         return 0;
2242 }
2243
2244 static void
2245 save_eal_params(struct app_params *app, FILE *f)
2246 {
2247         struct app_eal_params *p = &app->eal_params;
2248
2249         fprintf(f, "[EAL]\n");
2250
2251         if (p->coremap)
2252                 fprintf(f, "%s = %s\n", "lcores", p->coremap);
2253
2254         if (p->master_lcore_present)
2255                 fprintf(f, "%s = %" PRIu32 "\n",
2256                         "master_lcore", p->master_lcore);
2257
2258         fprintf(f, "%s = %" PRIu32 "\n", "n", p->channels);
2259
2260         if (p->memory_present)
2261                 fprintf(f, "%s = %" PRIu32 "\n", "m", p->memory);
2262
2263         if (p->ranks_present)
2264                 fprintf(f, "%s = %" PRIu32 "\n", "r", p->ranks);
2265
2266         if (p->pci_blacklist)
2267                 fprintf(f, "%s = %s\n", "pci_blacklist", p->pci_blacklist);
2268
2269         if (p->pci_whitelist)
2270                 fprintf(f, "%s = %s\n", "pci_whitelist", p->pci_whitelist);
2271
2272         if (p->vdev)
2273                 fprintf(f, "%s = %s\n", "vdev", p->vdev);
2274
2275         if (p->vmware_tsc_map_present)
2276                 fprintf(f, "%s = %s\n", "vmware_tsc_map",
2277                         (p->vmware_tsc_map) ? "yes" : "no");
2278
2279         if (p->proc_type)
2280                 fprintf(f, "%s = %s\n", "proc_type", p->proc_type);
2281
2282         if (p->syslog)
2283                 fprintf(f, "%s = %s\n", "syslog", p->syslog);
2284
2285         if (p->log_level_present)
2286                 fprintf(f, "%s = %" PRIu32 "\n", "log_level", p->log_level);
2287
2288         if (p->version_present)
2289                 fprintf(f, "%s = %s\n", "v", (p->version) ? "yes" : "no");
2290
2291         if (p->help_present)
2292                 fprintf(f, "%s = %s\n", "help", (p->help) ? "yes" : "no");
2293
2294         if (p->no_huge_present)
2295                 fprintf(f, "%s = %s\n", "no_huge", (p->no_huge) ? "yes" : "no");
2296
2297         if (p->no_pci_present)
2298                 fprintf(f, "%s = %s\n", "no_pci", (p->no_pci) ? "yes" : "no");
2299
2300         if (p->no_hpet_present)
2301                 fprintf(f, "%s = %s\n", "no_hpet", (p->no_hpet) ? "yes" : "no");
2302
2303         if (p->no_shconf_present)
2304                 fprintf(f, "%s = %s\n", "no_shconf",
2305                         (p->no_shconf) ? "yes" : "no");
2306
2307         if (p->add_driver)
2308                 fprintf(f, "%s = %s\n", "d", p->add_driver);
2309
2310         if (p->socket_mem)
2311                 fprintf(f, "%s = %s\n", "socket_mem", p->socket_mem);
2312
2313         if (p->huge_dir)
2314                 fprintf(f, "%s = %s\n", "huge_dir", p->huge_dir);
2315
2316         if (p->file_prefix)
2317                 fprintf(f, "%s = %s\n", "file_prefix", p->file_prefix);
2318
2319         if (p->base_virtaddr)
2320                 fprintf(f, "%s = %s\n", "base_virtaddr", p->base_virtaddr);
2321
2322         if (p->create_uio_dev_present)
2323                 fprintf(f, "%s = %s\n", "create_uio_dev",
2324                         (p->create_uio_dev) ? "yes" : "no");
2325
2326         if (p->vfio_intr)
2327                 fprintf(f, "%s = %s\n", "vfio_intr", p->vfio_intr);
2328
2329         if (p->xen_dom0_present)
2330                 fprintf(f, "%s = %s\n", "xen_dom0",
2331                         (p->xen_dom0) ? "yes" : "no");
2332
2333         fputc('\n', f);
2334 }
2335
2336 static void
2337 save_mempool_params(struct app_params *app, FILE *f)
2338 {
2339         struct app_mempool_params *p;
2340         size_t i, count;
2341
2342         count = RTE_DIM(app->mempool_params);
2343         for (i = 0; i < count; i++) {
2344                 p = &app->mempool_params[i];
2345                 if (!APP_PARAM_VALID(p))
2346                         continue;
2347
2348                 fprintf(f, "[%s]\n", p->name);
2349                 fprintf(f, "%s = %" PRIu32 "\n", "buffer_size", p->buffer_size);
2350                 fprintf(f, "%s = %" PRIu32 "\n", "pool_size", p->pool_size);
2351                 fprintf(f, "%s = %" PRIu32 "\n", "cache_size", p->cache_size);
2352                 fprintf(f, "%s = %" PRIu32 "\n", "cpu", p->cpu_socket_id);
2353
2354                 fputc('\n', f);
2355         }
2356 }
2357
2358 static void
2359 save_links_params(struct app_params *app, FILE *f)
2360 {
2361         struct app_link_params *p;
2362         size_t i, count;
2363
2364         count = RTE_DIM(app->link_params);
2365         for (i = 0; i < count; i++) {
2366                 p = &app->link_params[i];
2367                 if (!APP_PARAM_VALID(p))
2368                         continue;
2369
2370                 fprintf(f, "[%s]\n", p->name);
2371                 fprintf(f, "; %s = %" PRIu32 "\n", "pmd_id", p->pmd_id);
2372                 fprintf(f, "%s = %s\n", "promisc", p->promisc ? "yes" : "no");
2373                 fprintf(f, "%s = %" PRIu32 "\n", "arp_q", p->arp_q);
2374                 fprintf(f, "%s = %" PRIu32 "\n", "tcp_syn_local_q",
2375                         p->tcp_syn_local_q);
2376                 fprintf(f, "%s = %" PRIu32 "\n", "ip_local_q", p->ip_local_q);
2377                 fprintf(f, "%s = %" PRIu32 "\n", "tcp_local_q", p->tcp_local_q);
2378                 fprintf(f, "%s = %" PRIu32 "\n", "udp_local_q", p->udp_local_q);
2379                 fprintf(f, "%s = %" PRIu32 "\n", "sctp_local_q",
2380                         p->sctp_local_q);
2381
2382                 fputc('\n', f);
2383         }
2384 }
2385
2386 static void
2387 save_rxq_params(struct app_params *app, FILE *f)
2388 {
2389         struct app_pktq_hwq_in_params *p;
2390         size_t i, count;
2391
2392         count = RTE_DIM(app->hwq_in_params);
2393         for (i = 0; i < count; i++) {
2394                 p = &app->hwq_in_params[i];
2395                 if (!APP_PARAM_VALID(p))
2396                         continue;
2397
2398                 fprintf(f, "[%s]\n", p->name);
2399                 fprintf(f, "%s = %s\n",
2400                         "mempool",
2401                         app->mempool_params[p->mempool_id].name);
2402                 fprintf(f, "%s = %" PRIu32 "\n", "size", p->size);
2403                 fprintf(f, "%s = %" PRIu32 "\n", "burst", p->burst);
2404
2405                 fputc('\n', f);
2406         }
2407 }
2408
2409 static void
2410 save_txq_params(struct app_params *app, FILE *f)
2411 {
2412         struct app_pktq_hwq_out_params *p;
2413         size_t i, count;
2414
2415         count = RTE_DIM(app->hwq_out_params);
2416         for (i = 0; i < count; i++) {
2417                 p = &app->hwq_out_params[i];
2418                 if (!APP_PARAM_VALID(p))
2419                         continue;
2420
2421                 fprintf(f, "[%s]\n", p->name);
2422                 fprintf(f, "%s = %" PRIu32 "\n", "size", p->size);
2423                 fprintf(f, "%s = %" PRIu32 "\n", "burst", p->burst);
2424                 fprintf(f, "%s = %s\n",
2425                         "dropless",
2426                         p->dropless ? "yes" : "no");
2427
2428                 fputc('\n', f);
2429         }
2430 }
2431
2432 static void
2433 save_swq_params(struct app_params *app, FILE *f)
2434 {
2435         struct app_pktq_swq_params *p;
2436         size_t i, count;
2437
2438         count = RTE_DIM(app->swq_params);
2439         for (i = 0; i < count; i++) {
2440                 p = &app->swq_params[i];
2441                 if (!APP_PARAM_VALID(p))
2442                         continue;
2443
2444                 fprintf(f, "[%s]\n", p->name);
2445                 fprintf(f, "%s = %" PRIu32 "\n", "size", p->size);
2446                 fprintf(f, "%s = %" PRIu32 "\n", "burst_read", p->burst_read);
2447                 fprintf(f, "%s = %" PRIu32 "\n", "burst_write", p->burst_write);
2448                 fprintf(f, "%s = %s\n", "dropless", p->dropless ? "yes" : "no");
2449                 fprintf(f, "%s = %" PRIu64 "\n", "n_retries", p->n_retries);
2450                 fprintf(f, "%s = %" PRIu32 "\n", "cpu", p->cpu_socket_id);
2451                 fprintf(f, "%s = %s\n", "ipv4_frag", p->ipv4_frag ? "yes" : "no");
2452                 fprintf(f, "%s = %s\n", "ipv6_frag", p->ipv6_frag ? "yes" : "no");
2453                 fprintf(f, "%s = %s\n", "ipv4_ras", p->ipv4_ras ? "yes" : "no");
2454                 fprintf(f, "%s = %s\n", "ipv6_ras", p->ipv6_ras ? "yes" : "no");
2455                 if ((p->ipv4_frag == 1) || (p->ipv6_frag == 1)) {
2456                         fprintf(f, "%s = %" PRIu32 "\n", "mtu", p->mtu);
2457                         fprintf(f, "%s = %" PRIu32 "\n", "metadata_size", p->metadata_size);
2458                         fprintf(f, "%s = %s\n",
2459                                 "mempool_direct",
2460                                 app->mempool_params[p->mempool_direct_id].name);
2461                         fprintf(f, "%s = %s\n",
2462                                 "mempool_indirect",
2463                                 app->mempool_params[p->mempool_indirect_id].name);
2464                 }
2465
2466                 fputc('\n', f);
2467         }
2468 }
2469
2470 static void
2471 save_tm_params(struct app_params *app, FILE *f)
2472 {
2473         struct app_pktq_tm_params *p;
2474         size_t i, count;
2475
2476         count = RTE_DIM(app->tm_params);
2477         for (i = 0; i < count; i++) {
2478                 p = &app->tm_params[i];
2479                 if (!APP_PARAM_VALID(p))
2480                         continue;
2481
2482                 fprintf(f, "[%s]\n", p->name);
2483                 fprintf(f, "%s = %s\n", "cfg", p->file_name);
2484                 fprintf(f, "%s = %" PRIu32 "\n", "burst_read", p->burst_read);
2485                 fprintf(f, "%s = %" PRIu32 "\n", "burst_write", p->burst_write);
2486
2487                 fputc('\n', f);
2488         }
2489 }
2490
2491 static void
2492 save_source_params(struct app_params *app, FILE *f)
2493 {
2494         struct app_pktq_source_params *p;
2495         size_t i, count;
2496
2497         count = RTE_DIM(app->source_params);
2498         for (i = 0; i < count; i++) {
2499                 p = &app->source_params[i];
2500                 if (!APP_PARAM_VALID(p))
2501                         continue;
2502
2503                 fprintf(f, "[%s]\n", p->name);
2504                 fprintf(f, "%s = %s\n",
2505                         "mempool",
2506                         app->mempool_params[p->mempool_id].name);
2507                 fprintf(f, "%s = %" PRIu32 "\n", "burst", p->burst);
2508                 fputc('\n', f);
2509         }
2510 }
2511
2512 static void
2513 save_msgq_params(struct app_params *app, FILE *f)
2514 {
2515         struct app_msgq_params *p;
2516         size_t i, count;
2517
2518         count = RTE_DIM(app->msgq_params);
2519         for (i = 0; i < count; i++) {
2520                 p = &app->msgq_params[i];
2521                 if (!APP_PARAM_VALID(p))
2522                         continue;
2523
2524                 fprintf(f, "[%s]\n", p->name);
2525                 fprintf(f, "%s = %" PRIu32 "\n", "size", p->size);
2526                 fprintf(f, "%s = %" PRIu32 "\n", "cpu", p->cpu_socket_id);
2527
2528                 fputc('\n', f);
2529         }
2530 }
2531
2532 static void
2533 save_pipeline_params(struct app_params *app, FILE *f)
2534 {
2535         size_t i, count;
2536
2537         count = RTE_DIM(app->pipeline_params);
2538         for (i = 0; i < count; i++) {
2539                 struct app_pipeline_params *p = &app->pipeline_params[i];
2540
2541                 if (!APP_PARAM_VALID(p))
2542                         continue;
2543
2544                 /* section name */
2545                 fprintf(f, "[%s]\n", p->name);
2546
2547                 /* type */
2548                 fprintf(f, "type = %s\n", p->type);
2549
2550                 /* core */
2551                 fprintf(f, "core = s%" PRIu32 "c%" PRIu32 "%s\n",
2552                         p->socket_id,
2553                         p->core_id,
2554                         (p->hyper_th_id) ? "h" : "");
2555
2556                 /* pktq_in */
2557                 if (p->n_pktq_in) {
2558                         uint32_t j;
2559
2560                         fprintf(f, "pktq_in =");
2561                         for (j = 0; j < p->n_pktq_in; j++) {
2562                                 struct app_pktq_in_params *pp = &p->pktq_in[j];
2563                                 char *name;
2564
2565                                 switch (pp->type) {
2566                                 case APP_PKTQ_IN_HWQ:
2567                                         name = app->hwq_in_params[pp->id].name;
2568                                         break;
2569                                 case APP_PKTQ_IN_SWQ:
2570                                         name = app->swq_params[pp->id].name;
2571                                         break;
2572                                 case APP_PKTQ_IN_TM:
2573                                         name = app->tm_params[pp->id].name;
2574                                         break;
2575                                 case APP_PKTQ_IN_SOURCE:
2576                                         name = app->source_params[pp->id].name;
2577                                         break;
2578                                 default:
2579                                         APP_CHECK(0, "System error "
2580                                                 "occurred while saving "
2581                                                 "parameter to file");
2582                                 }
2583
2584                                 fprintf(f, " %s", name);
2585                         }
2586                         fprintf(f, "\n");
2587                 }
2588
2589                 /* pktq_in */
2590                 if (p->n_pktq_out) {
2591                         uint32_t j;
2592
2593                         fprintf(f, "pktq_out =");
2594                         for (j = 0; j < p->n_pktq_out; j++) {
2595                                 struct app_pktq_out_params *pp =
2596                                         &p->pktq_out[j];
2597                                 char *name;
2598
2599                                 switch (pp->type) {
2600                                 case APP_PKTQ_OUT_HWQ:
2601                                         name = app->hwq_out_params[pp->id].name;
2602                                         break;
2603                                 case APP_PKTQ_OUT_SWQ:
2604                                         name = app->swq_params[pp->id].name;
2605                                         break;
2606                                 case APP_PKTQ_OUT_TM:
2607                                         name = app->tm_params[pp->id].name;
2608                                         break;
2609                                 case APP_PKTQ_OUT_SINK:
2610                                         name = app->sink_params[pp->id].name;
2611                                         break;
2612                                 default:
2613                                         APP_CHECK(0, "System error "
2614                                                 "occurred while saving "
2615                                                 "parameter to file");
2616                                 }
2617
2618                                 fprintf(f, " %s", name);
2619                         }
2620                         fprintf(f, "\n");
2621                 }
2622
2623                 /* msgq_in */
2624                 if (p->n_msgq_in) {
2625                         uint32_t j;
2626
2627                         fprintf(f, "msgq_in =");
2628                         for (j = 0; j < p->n_msgq_in; j++) {
2629                                 uint32_t id = p->msgq_in[j];
2630                                 char *name = app->msgq_params[id].name;
2631
2632                                 fprintf(f, " %s", name);
2633                         }
2634                         fprintf(f, "\n");
2635                 }
2636
2637                 /* msgq_out */
2638                 if (p->n_msgq_out) {
2639                         uint32_t j;
2640
2641                         fprintf(f, "msgq_out =");
2642                         for (j = 0; j < p->n_msgq_out; j++) {
2643                                 uint32_t id = p->msgq_out[j];
2644                                 char *name = app->msgq_params[id].name;
2645
2646                                 fprintf(f, " %s", name);
2647                         }
2648                         fprintf(f, "\n");
2649                 }
2650
2651                 /* timer_period */
2652                 fprintf(f, "timer_period = %" PRIu32 "\n", p->timer_period);
2653
2654                 /* args */
2655                 if (p->n_args) {
2656                         uint32_t j;
2657
2658                         for (j = 0; j < p->n_args; j++)
2659                                 fprintf(f, "%s = %s\n", p->args_name[j],
2660                                         p->args_value[j]);
2661                 }
2662
2663                 fprintf(f, "\n");
2664         }
2665 }
2666
2667 void
2668 app_config_save(struct app_params *app, const char *file_name)
2669 {
2670         FILE *file;
2671         char *name, *dir_name;
2672         int status;
2673
2674         name = strdup(file_name);
2675         dir_name = dirname(name);
2676         status = access(dir_name, W_OK);
2677         APP_CHECK((status == 0),
2678                 "Error: need write access privilege to directory "
2679                 "\"%s\" to save configuration\n", dir_name);
2680
2681         file = fopen(file_name, "w");
2682         APP_CHECK((file != NULL),
2683                 "Error: failed to save configuration to file \"%s\"",
2684                 file_name);
2685
2686         save_eal_params(app, file);
2687         save_pipeline_params(app, file);
2688         save_mempool_params(app, file);
2689         save_links_params(app, file);
2690         save_rxq_params(app, file);
2691         save_txq_params(app, file);
2692         save_swq_params(app, file);
2693         save_tm_params(app, file);
2694         save_source_params(app, file);
2695         save_msgq_params(app, file);
2696
2697         fclose(file);
2698         free(name);
2699 }
2700
2701 int
2702 app_config_init(struct app_params *app)
2703 {
2704         size_t i;
2705
2706         memcpy(app, &app_params_default, sizeof(struct app_params));
2707
2708         for (i = 0; i < RTE_DIM(app->mempool_params); i++)
2709                 memcpy(&app->mempool_params[i],
2710                         &mempool_params_default,
2711                         sizeof(struct app_mempool_params));
2712
2713         for (i = 0; i < RTE_DIM(app->link_params); i++)
2714                 memcpy(&app->link_params[i],
2715                         &link_params_default,
2716                         sizeof(struct app_link_params));
2717
2718         for (i = 0; i < RTE_DIM(app->hwq_in_params); i++)
2719                 memcpy(&app->hwq_in_params[i],
2720                         &default_hwq_in_params,
2721                         sizeof(default_hwq_in_params));
2722
2723         for (i = 0; i < RTE_DIM(app->hwq_out_params); i++)
2724                 memcpy(&app->hwq_out_params[i],
2725                         &default_hwq_out_params,
2726                         sizeof(default_hwq_out_params));
2727
2728         for (i = 0; i < RTE_DIM(app->swq_params); i++)
2729                 memcpy(&app->swq_params[i],
2730                         &default_swq_params,
2731                         sizeof(default_swq_params));
2732
2733         for (i = 0; i < RTE_DIM(app->tm_params); i++)
2734                 memcpy(&app->tm_params[i],
2735                         &default_tm_params,
2736                         sizeof(default_tm_params));
2737
2738         for (i = 0; i < RTE_DIM(app->source_params); i++)
2739                 memcpy(&app->source_params[i],
2740                         &default_source_params,
2741                         sizeof(default_source_params));
2742
2743         for (i = 0; i < RTE_DIM(app->sink_params); i++)
2744                 memcpy(&app->sink_params[i],
2745                         &default_sink_params,
2746                         sizeof(default_sink_params));
2747
2748         for (i = 0; i < RTE_DIM(app->msgq_params); i++)
2749                 memcpy(&app->msgq_params[i],
2750                         &default_msgq_params,
2751                         sizeof(default_msgq_params));
2752
2753         for (i = 0; i < RTE_DIM(app->pipeline_params); i++)
2754                 memcpy(&app->pipeline_params[i],
2755                         &default_pipeline_params,
2756                         sizeof(default_pipeline_params));
2757
2758         return 0;
2759 }
2760
2761 static char *
2762 filenamedup(const char *filename, const char *suffix)
2763 {
2764         char *s = malloc(strlen(filename) + strlen(suffix) + 1);
2765
2766         if (!s)
2767                 return NULL;
2768
2769         sprintf(s, "%s%s", filename, suffix);
2770         return s;
2771 }
2772
2773 int
2774 app_config_args(struct app_params *app, int argc, char **argv)
2775 {
2776         const char *optname;
2777         int opt, option_index;
2778         int f_present, s_present, p_present, l_present;
2779         int preproc_present, preproc_params_present;
2780         int scaned = 0;
2781
2782         static struct option lgopts[] = {
2783                 { "preproc", 1, 0, 0 },
2784                 { "preproc-args", 1, 0, 0 },
2785                 { NULL,  0, 0, 0 }
2786         };
2787
2788         /* Copy application name */
2789         strncpy(app->app_name, argv[0], APP_APPNAME_SIZE - 1);
2790
2791         f_present = 0;
2792         s_present = 0;
2793         p_present = 0;
2794         l_present = 0;
2795         preproc_present = 0;
2796         preproc_params_present = 0;
2797
2798         while ((opt = getopt_long(argc, argv, "f:s:p:l:", lgopts,
2799                         &option_index)) != EOF)
2800                 switch (opt) {
2801                 case 'f':
2802                         if (f_present)
2803                                 rte_panic("Error: Config file is provided "
2804                                         "more than once\n");
2805                         f_present = 1;
2806
2807                         if (!strlen(optarg))
2808                                 rte_panic("Error: Config file name is null\n");
2809
2810                         app->config_file = strdup(optarg);
2811                         if (app->config_file == NULL)
2812                                 rte_panic("Error: Memory allocation failure\n");
2813
2814                         break;
2815
2816                 case 's':
2817                         if (s_present)
2818                                 rte_panic("Error: Script file is provided "
2819                                         "more than once\n");
2820                         s_present = 1;
2821
2822                         if (!strlen(optarg))
2823                                 rte_panic("Error: Script file name is null\n");
2824
2825                         app->script_file = strdup(optarg);
2826                         if (app->script_file == NULL)
2827                                 rte_panic("Error: Memory allocation failure\n");
2828
2829                         break;
2830
2831                 case 'p':
2832                         if (p_present)
2833                                 rte_panic("Error: PORT_MASK is provided "
2834                                         "more than once\n");
2835                         p_present = 1;
2836
2837                         if ((sscanf(optarg, "%" SCNx64 "%n", &app->port_mask,
2838                                 &scaned) != 1) ||
2839                                 ((size_t) scaned != strlen(optarg)))
2840                                 rte_panic("Error: PORT_MASK is not "
2841                                         "a hexadecimal integer\n");
2842
2843                         if (app->port_mask == 0)
2844                                 rte_panic("Error: PORT_MASK is null\n");
2845
2846                         break;
2847
2848                 case 'l':
2849                         if (l_present)
2850                                 rte_panic("Error: LOG_LEVEL is provided "
2851                                         "more than once\n");
2852                         l_present = 1;
2853
2854                         if ((sscanf(optarg, "%" SCNu32 "%n", &app->log_level,
2855                                 &scaned) != 1) ||
2856                                 ((size_t) scaned != strlen(optarg)) ||
2857                                 (app->log_level >= APP_LOG_LEVELS))
2858                                 rte_panic("Error: LOG_LEVEL invalid value\n");
2859
2860                         break;
2861
2862                 case 0:
2863                         optname = lgopts[option_index].name;
2864
2865                         if (strcmp(optname, "preproc") == 0) {
2866                                 if (preproc_present)
2867                                         rte_panic("Error: Preprocessor argument "
2868                                                 "is provided more than once\n");
2869                                 preproc_present = 1;
2870
2871                                 app->preproc = strdup(optarg);
2872                                 break;
2873                         }
2874
2875                         if (strcmp(optname, "preproc-args") == 0) {
2876                                 if (preproc_params_present)
2877                                         rte_panic("Error: Preprocessor args "
2878                                                 "are provided more than once\n");
2879                                 preproc_params_present = 1;
2880
2881                                 app->preproc_args = strdup(optarg);
2882                                 break;
2883                         }
2884
2885                         app_print_usage(argv[0]);
2886                         break;
2887
2888                 default:
2889                         app_print_usage(argv[0]);
2890                 }
2891
2892         optind = 0; /* reset getopt lib */
2893
2894         /* Check that mandatory args have been provided */
2895         if (!p_present)
2896                 rte_panic("Error: PORT_MASK is not provided\n");
2897
2898         /* Check dependencies between args */
2899         if (preproc_params_present && (preproc_present == 0))
2900                 rte_panic("Error: Preprocessor args specified while "
2901                         "preprocessor is not defined\n");
2902
2903         app->parser_file = preproc_present ?
2904                 filenamedup(app->config_file, ".preproc") :
2905                 strdup(app->config_file);
2906         app->output_file = filenamedup(app->config_file, ".out");
2907
2908         return 0;
2909 }
2910
2911 int
2912 app_config_preproc(struct app_params *app)
2913 {
2914         char buffer[256];
2915         int status;
2916
2917         if (app->preproc == NULL)
2918                 return 0;
2919
2920         status = access(app->config_file, F_OK | R_OK);
2921         APP_CHECK((status == 0), "Error: Unable to open file %s",
2922                 app->config_file);
2923
2924         snprintf(buffer, sizeof(buffer), "%s %s %s > %s",
2925                 app->preproc,
2926                 app->preproc_args ? app->preproc_args : "",
2927                 app->config_file,
2928                 app->parser_file);
2929
2930         status = system(buffer);
2931         APP_CHECK((WIFEXITED(status) && (WEXITSTATUS(status) == 0)),
2932                 "Error occurred while pre-processing file \"%s\"\n",
2933                 app->config_file);
2934
2935         return status;
2936 }