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