d6c1639d2d666ac9c93d7369fb525d7ba4bf0931
[samplevnf.git] / VNFs / DPPD-PROX / prox_args.c
1 /*
2 // Copyright (c) 2010-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <unistd.h>
18 #include <string.h>
19
20 #include <rte_sched.h>
21 #include <rte_string_fns.h>
22 #include <rte_version.h>
23
24 #include "prox_malloc.h"
25 #include "version.h"
26 #include "defines.h"
27 #include "prox_args.h"
28 #include "prox_assert.h"
29 #include "prox_cfg.h"
30 #include "cfgfile.h"
31 #include "quit.h"
32 #include "log.h"
33 #include "parse_utils.h"
34 #include "prox_port_cfg.h"
35 #include "defaults.h"
36 #include "prox_lua.h"
37 #include "cqm.h"
38 #include "defines.h"
39 #include "prox_ipv6.h"
40 #include "prox_compat.h"
41 #include "ip_subnet.h"
42
43 #define MAX_RTE_ARGV 64
44 #define MAX_ARG_LEN  64
45
46 struct cfg_depr {
47         const char *opt;
48         const char *info;
49 };
50
51 /* Helper macro */
52 #define STR_EQ(s1, s2)  (!strcmp((s1), (s2)))
53
54 /* configuration files support */
55 static int get_rte_cfg(unsigned sindex, char *str, void *data);
56 static int get_global_cfg(unsigned sindex, char *str, void *data);
57 static int get_port_cfg(unsigned sindex, char *str, void *data);
58 static int get_defaults_cfg(unsigned sindex, char *str, void *data);
59 static int get_cache_set_cfg(unsigned sindex, char *str, void *data);
60 static int get_var_cfg(unsigned sindex, char *str, void *data);
61 static int get_lua_cfg(unsigned sindex, char *str, void *data);
62 static int get_core_cfg(unsigned sindex, char *str, void *data);
63
64 static const char *cfg_file = DEFAULT_CONFIG_FILE;
65 static struct rte_cfg    rte_cfg;
66 struct prox_cache_set_cfg  prox_cache_set_cfg[PROX_MAX_CACHE_SET];
67
68 static char format_err_str[1024];
69 static const char *err_str = "Unknown error";
70
71 static struct cfg_section eal_default_cfg = {
72         .name   = "eal options",
73         .parser = get_rte_cfg,
74         .data   = &rte_cfg,
75         .indexp[0]  = 0,
76         .nbindex = 1,
77         .error  = 0
78 };
79
80 static struct cfg_section port_cfg = {
81         .name   = "port #",
82         .parser = get_port_cfg,
83         .data   = &prox_port_cfg,
84         .indexp[0]  = 0,
85         .nbindex = 1,
86         .error  = 0
87 };
88
89 static struct cfg_section var_cfg = {
90         .name   = "variables",
91         .parser = get_var_cfg,
92         .data   = 0,
93         .indexp[0]  = 0,
94         .nbindex = 1,
95         .error  = 0
96 };
97
98 static struct cfg_section cache_set_cfg = {
99         .name   = "cache set #",
100         .parser = get_cache_set_cfg,
101         .data   = &prox_cache_set_cfg,
102         .indexp[0]  = 0,
103         .nbindex = 1,
104         .error  = 0
105 };
106
107 static struct cfg_section defaults_cfg = {
108         .name   = "defaults",
109         .parser = get_defaults_cfg,
110         .data   = 0,
111         .indexp[0]  = 0,
112         .nbindex = 1,
113         .error  = 0
114 };
115
116 static struct cfg_section settings_cfg = {
117         .name   = "global",
118         .parser = get_global_cfg,
119         .data   = &prox_cfg,
120         .indexp[0]  = 0,
121         .nbindex = 1,
122         .error  = 0
123 };
124
125 static struct cfg_section lua_cfg = {
126         .name = "lua",
127         .parser = get_lua_cfg,
128         .raw_lines = 1,
129         .indexp[0] = 0,
130         .nbindex = 1,
131         .error = 0,
132 };
133
134 static struct cfg_section core_cfg = {
135         .name   = "core #",
136         .parser = get_core_cfg,
137         .data   = lcore_cfg_init,
138         .indexp[0]  = 0,
139         .nbindex = 1,
140         .error  = 0
141 };
142
143 struct deferred_port {
144         struct task_args *targ;
145         char name[256];
146         uint8_t is_rx_port;
147 };
148
149 static struct deferred_port deferred_port[PROX_MAX_PORTS];
150 static int n_deferred_ports = 0;
151
152 static void set_errf(const char *format, ...)
153 {
154         va_list ap;
155         va_start(ap, format);
156         vsnprintf(format_err_str, sizeof(format_err_str), format, ap);
157         va_end(ap);
158         err_str = format_err_str;
159 }
160
161 /* [eal options] parser */
162 static int get_rte_cfg(__attribute__((unused))unsigned sindex, char *str, void *data)
163 {
164         struct rte_cfg *pconfig = (struct rte_cfg *)data;
165
166         if (str == NULL || pconfig == NULL) {
167                 return -1;
168         }
169
170         char *pkey = get_cfg_key(str);
171         if (pkey == NULL) {
172                 set_errf("Missing key after option");
173                 return -1;
174         }
175
176         if (STR_EQ(str, "-m")) {
177                 return parse_int(&pconfig->memory, pkey);
178         }
179         if (STR_EQ(str, "-n")) {
180                 if (parse_int(&pconfig->force_nchannel, pkey)) {
181                         return -1;
182                 }
183                 if (pconfig->force_nchannel == 0) {
184                         set_errf("Invalid number of memory channels");
185                         return -1;
186                 }
187                 return 0;
188         }
189         if (STR_EQ(str, "-r")) {
190                 if (parse_int(&pconfig->force_nrank, pkey)) {
191                         return -1;
192                 }
193                 if (pconfig->force_nrank == 0 || pconfig->force_nrank > 16) {
194                         set_errf("Invalid number of memory ranks");
195                         return -1;
196                 }
197                 return 0;
198         }
199         /* debug options */
200         if (STR_EQ(str, "no-pci")) {
201                 return parse_bool(&pconfig->no_pci, pkey);
202         }
203         if (STR_EQ(str, "no-hpet")) {
204                 return parse_bool(&pconfig->no_hpet, pkey);
205         }
206         if (STR_EQ(str, "no-shconf")) {
207                 return parse_bool(&pconfig->no_shconf, pkey);
208         }
209         if (STR_EQ(str, "no-huge")) {
210                 return parse_bool(&pconfig->no_hugetlbfs, pkey);
211         }
212         if (STR_EQ(str, "no-output")) {
213                 return parse_bool(&pconfig->no_output, pkey);
214         }
215
216         if (STR_EQ(str, "huge-dir")) {
217                 if (pconfig->hugedir) {
218                         free(pconfig->hugedir);
219                 }
220                 pconfig->hugedir = strdup(pkey);
221                 return 0;
222         }
223
224         if (STR_EQ(str, "eal")) {
225                 char eal[MAX_STR_LEN_PROC];
226                 if (pconfig->eal) {
227                         free(pconfig->eal);
228                         pconfig->eal = NULL;
229                 }
230                 if (parse_str(eal, pkey, sizeof(eal)))
231                         return -1;
232                 pkey = eal;
233                 strip_spaces(&pkey, 1);
234                 if (*pkey)
235                         pconfig->eal = strdup(pkey);
236                 return 0;
237         }
238
239         set_errf("Option '%s' is not known", str);
240         return -1;
241 }
242
243 struct cfg_depr global_cfg_depr[] = {
244         {"virtualization", "This is now set automatically if needed"},
245         {"qinq_tag", "This option is deprecated"},
246         {"wait on quit", "This is now set automatically if needed"},
247         {"version", ""}
248 };
249
250 const char *get_cfg_dir(void)
251 {
252         static char dir[PATH_MAX];
253         size_t end = strlen(cfg_file) - 1;
254         while (end > 0 && cfg_file[end] != '/')
255                 end--;
256
257         strncpy(dir, cfg_file, end);
258         return dir;
259 }
260
261 static int get_lua_cfg(__attribute__((unused)) unsigned sindex, __attribute__((unused)) char *str, __attribute__((unused)) void *data)
262 {
263         int status;
264         char cwd[1024];
265         if (NULL == getcwd(cwd, sizeof(cwd))) {
266                 set_errf("Failed to get current directory while loading Lua file\n");
267                 return -1;
268         }
269         status = chdir(get_cfg_dir());
270         if (status) {
271                 set_errf("Failed to change directory to '%s' while loading Lua file\n", get_cfg_dir());
272                 return -1;
273         }
274
275         struct lua_State *l = prox_lua();
276
277         char str_cpy[1024];
278         prox_strncpy(str_cpy, str, sizeof(str_cpy));
279         uint32_t len = strlen(str_cpy);
280         str_cpy[len++] = '\n';
281         str_cpy[len++] = 0;
282
283         status = luaL_loadstring(l, str_cpy);
284         if (status) {
285                 set_errf("Lua error: '%s'\n", lua_tostring(l, -1));
286                 status = chdir(cwd);
287                 return -1;
288         }
289
290         status = lua_pcall(l, 0, LUA_MULTRET, 0);
291         if (status) {
292                 set_errf("Lua error: '%s'\n", lua_tostring(l, -1));
293                 status = chdir(cwd);
294                 return -1;
295         }
296
297         status = chdir(cwd);
298         if (status) {
299                 set_errf("Failed to restore current directory to '%s' while loading Lua file\n", cwd);
300                 return -1;
301         }
302
303         return 0;
304 }
305
306 /* [global] parser */
307 static int get_global_cfg(__attribute__((unused))unsigned sindex, char *str, void *data)
308 {
309         struct prox_cfg *pset = (struct prox_cfg *)data;
310
311         if (str == NULL || pset == NULL) {
312                 return -1;
313         }
314
315         char *pkey = get_cfg_key(str);
316         if (pkey == NULL) {
317                 set_errf("Missing key after option");
318                 return -1;
319         }
320
321         for (uint32_t i = 0; i < RTE_DIM(global_cfg_depr); ++i) {
322                 if (STR_EQ(str, global_cfg_depr[i].opt)) {
323                         set_errf("Option '%s' is deprecated%s%s",
324                                  global_cfg_depr[i].opt, strlen(global_cfg_depr[i].info)? ": ": "", global_cfg_depr[i].info);
325                         return -1;
326                 }
327         }
328
329         if (STR_EQ(str, "name")) {
330                 return parse_str(pset->name, pkey, sizeof(pset->name));
331         }
332
333         if (STR_EQ(str, "start time")) {
334                 return parse_int(&pset->start_time, pkey);
335         }
336
337         if (STR_EQ(str, "duration time")) {
338                 return parse_int(&pset->duration_time, pkey);
339         }
340
341         if (STR_EQ(str, "shuffle")) {
342                 return parse_flag(&pset->flags, DSF_SHUFFLE, pkey);
343         }
344         if (STR_EQ(str, "disable cmt")) {
345                 return parse_flag(&pset->flags, DSF_DISABLE_CMT, pkey);
346         }
347         if (STR_EQ(str, "mp rings")) {
348                 return parse_flag(&pset->flags, DSF_MP_RINGS, pkey);
349         }
350         if (STR_EQ(str, "enable bypass")) {
351                 return parse_flag(&pset->flags, DSF_ENABLE_BYPASS, pkey);
352         }
353         if (STR_EQ(str, "poll timeout")) {
354                 return parse_int(&pset->poll_timeout, pkey);
355         }
356         if (STR_EQ(str, "heartbeat timeout")) {
357                 return parse_int(&pset->heartbeat_timeout, pkey);
358         }
359
360         if (STR_EQ(str, "cpe table map")) {
361                 /* The config defined ports through 0, 1, 2 ... which
362                    need to be associated with ports. This is done
363                    through defining it using "cpe table map=" */
364                 return parse_port_name_list((uint32_t*)pset->cpe_table_ports, NULL, PROX_MAX_PORTS, pkey);
365         }
366
367         if (STR_EQ(str, "pre cmd")) {
368                 return system(pkey);
369         }
370
371         if (STR_EQ(str, "unique mempool per socket")) {
372                 return parse_flag(&pset->flags, UNIQUE_MEMPOOL_PER_SOCKET, pkey);
373         }
374
375         if (STR_EQ(str, "log buffer size")) {
376                 if (parse_kmg(&pset->logbuf_size, pkey)) {
377                         return -1;
378                 }
379                 plog_info("Logging to buffer with size = %d\n", pset->logbuf_size);
380                 return 0;
381         }
382
383         set_errf("Option '%s' is not known", str);
384         return -1;
385 }
386
387 /* [variable] parser */
388 static int get_var_cfg(__attribute__((unused)) unsigned sindex, char *str, __attribute__((unused)) void *data)
389 {
390         return add_var(str, get_cfg_key(str), 0);
391 }
392
393 /* [defaults] parser */
394 static int get_defaults_cfg(__attribute__((unused)) unsigned sindex, char *str, __attribute__((unused)) void *data)
395 {
396         uint32_t val;
397         char *pkey;
398
399         pkey = get_cfg_key(str);
400         if (pkey == NULL) {
401                 set_errf("Missing key after option");
402                 return -1;
403         }
404
405         if (STR_EQ(str, "mempool size")) {
406
407                 if (parse_kmg(&val, pkey)) {
408                         return -1;
409                 }
410
411                 for (uint8_t lcore_id = 0; lcore_id < RTE_MAX_LCORE; ++lcore_id) {
412                         struct lcore_cfg *cur_lcore_cfg_init = &lcore_cfg_init[lcore_id];
413                         cur_lcore_cfg_init->id = lcore_id;
414                         for (uint8_t task_id = 0; task_id < MAX_TASKS_PER_CORE; ++task_id) {
415                                 struct task_args *targ = &cur_lcore_cfg_init->targs[task_id];
416                                 targ->nb_mbuf = val;
417                                 targ->id = task_id;
418                         }
419                 }
420                 return 0;
421         }
422
423         if (STR_EQ(str, "qinq tag")) {
424                 for (uint8_t lcore_id = 0; lcore_id < RTE_MAX_LCORE; ++lcore_id) {
425                         struct lcore_cfg *cur_lcore_cfg_init = &lcore_cfg_init[lcore_id];
426                         cur_lcore_cfg_init->id = lcore_id;
427                         for (uint8_t task_id = 0; task_id < MAX_TASKS_PER_CORE; ++task_id) {
428                                 struct task_args *targ = &cur_lcore_cfg_init->targs[task_id];
429                                 parse_int(&targ->qinq_tag, pkey);
430                         }
431                 }
432                 return 0;
433         }
434         if (STR_EQ(str, "memcache size")) {
435
436                 if (parse_kmg(&val, pkey)) {
437                         return -1;
438                 }
439
440                 for (uint8_t lcore_id = 0; lcore_id < RTE_MAX_LCORE; ++lcore_id) {
441                         struct lcore_cfg *cur_lcore_cfg_init = &lcore_cfg_init[lcore_id];
442                         cur_lcore_cfg_init->id = lcore_id;
443                         for (uint8_t task_id = 0; task_id < MAX_TASKS_PER_CORE; ++task_id) {
444                                 struct task_args *targ = &cur_lcore_cfg_init->targs[task_id];
445                                 targ->nb_cache_mbuf = val;
446                         }
447                 }
448                 return 0;
449         }
450
451         set_errf("Option '%s' is not known", str);
452         return -1;
453 }
454
455 /* [cache set] parser */
456 static int get_cache_set_cfg(unsigned sindex, char *str, void *data)
457 {
458         struct prox_cache_set_cfg *cfg = (struct prox_cache_set_cfg *)data;
459
460         uint8_t cur_if = sindex & ~CFG_INDEXED;
461
462         if (cur_if >= PROX_MAX_CACHE_SET) {
463                 set_errf("Cache set ID is too high (max allowed %d)", PROX_MAX_CACHE_SET - 1 );
464                 return -1;
465         }
466
467         cfg = &prox_cache_set_cfg[cur_if];
468
469         if (str == NULL || data == NULL) {
470                 return -1;
471         }
472
473         char *pkey = get_cfg_key(str);
474
475         if (pkey == NULL) {
476                 set_errf("Missing key after option");
477                 return -1;
478         }
479
480         if (STR_EQ(str, "mask")) {
481                 uint32_t val;
482                 int err = parse_int(&val, pkey);
483                 if (err) {
484                         return -1;
485                 }
486                 cfg->mask = val;
487                 cfg->socket_id = -1;
488                 plog_info("\tCache set %d has mask %x\n", cur_if, cfg->mask);
489                 return 0;
490         }
491         return 0;
492 }
493
494 /* [port] parser */
495 static int get_port_cfg(unsigned sindex, char *str, void *data)
496 {
497         struct prox_port_cfg *cfg = (struct prox_port_cfg *)data;
498
499         uint8_t cur_if = sindex & ~CFG_INDEXED;
500
501         if (cur_if >= PROX_MAX_PORTS) {
502                 set_errf("Port ID is too high (max allowed %d)", PROX_MAX_PORTS - 1 );
503                 return -1;
504         }
505
506         cfg = &prox_port_cfg[cur_if];
507
508         if (str == NULL || data == NULL) {
509                 return -1;
510         }
511
512         char *pkey = get_cfg_key(str);
513
514         if (pkey == NULL) {
515                 set_errf("Missing key after option");
516                 return -1;
517         }
518
519         if (STR_EQ(str, "mac")) {
520                 if (STR_EQ(pkey, "hardware")) {
521                         cfg->type = PROX_PORT_MAC_HW;
522                 }
523                 else if (STR_EQ(pkey, "random")) {
524                         cfg->type = PROX_PORT_MAC_RAND;
525                 }
526                 else {
527                         cfg->type = PROX_PORT_MAC_SET;
528                         if (parse_mac(&cfg->eth_addr, pkey)) {
529                                 return -1;
530                         }
531                 }
532         }
533         else if (STR_EQ(str, "name")) {
534                 uint32_t val;
535                 prox_strncpy(cfg->names[0], pkey, MAX_NAME_SIZE);
536                 PROX_ASSERT(cur_if < PROX_MAX_PORTS);
537                 return add_port_name(cur_if, pkey);
538         }
539         else if (STR_EQ(str, "rx desc")) {
540                 return parse_int(&cfg->n_rxd, pkey);
541         }
542         else if (STR_EQ(str, "tx desc")) {
543                 return parse_int(&cfg->n_txd, pkey);
544         }
545         else if (STR_EQ(str, "all_rx_queues")) {
546                 uint32_t val;
547                 if (parse_bool(&val, pkey)) {
548                         return -1;
549                 }
550                 cfg->all_rx_queues = val;
551         }
552         else if (STR_EQ(str, "promiscuous")) {
553                 uint32_t val;
554                 if (parse_bool(&val, pkey)) {
555                         return -1;
556                 }
557                 cfg->promiscuous = val;
558         }
559         else if (STR_EQ(str, "multicast")) {
560                 uint32_t val;
561                 if (cfg->nb_mc_addr >= NB_MCAST_ADDR) {
562                         plog_err("too many multicast addresses\n");
563                         return -1;
564                 }
565                 if (parse_mac(&cfg->mc_addr[cfg->nb_mc_addr], pkey)) {
566                         return -1;
567                 }
568                 cfg->nb_mc_addr++ ;
569         }
570         else if (STR_EQ(str, "lsc")) {
571                 cfg->lsc_set_explicitely = 1;
572                 uint32_t val;
573                 if (parse_bool(&val, pkey)) {
574                         return -1;
575                 }
576                 cfg->lsc_val = val;
577         }
578         else if (STR_EQ(str, "local ipv4")) {
579                 if (parse_ip_set(cfg->ip_addr, pkey, PROX_MAX_VLAN_TAGS) != 0) {
580                         cfg->ip_addr[0].ip = 24;
581                         return parse_ip(&cfg->ip_addr[0].ip, pkey);
582                 }
583                 return 0;
584         }
585         else if (STR_EQ(str, "vdev")) {
586                 prox_strncpy(cfg->vdev, pkey, MAX_NAME_SIZE);
587         }
588 #if RTE_VERSION >= RTE_VERSION_NUM(18,8,0,1)
589         else if (STR_EQ(str, "disable tx offload")) {
590                 uint32_t val;
591                 if (parse_int(&val, pkey)) {
592                         return -1;
593                 }
594                 if (val)
595                         cfg->disabled_tx_offload = val;
596         }
597 #endif
598         else if (STR_EQ(str, "strip crc")) {
599                 uint32_t val;
600                 if (parse_bool(&val, pkey)) {
601                         return -1;
602                 }
603 #if defined(DEV_RX_OFFLOAD_CRC_STRIP)
604                 if (val)
605                         cfg->requested_rx_offload |= DEV_RX_OFFLOAD_CRC_STRIP;
606                 else
607                         cfg->requested_rx_offload &= ~DEV_RX_OFFLOAD_CRC_STRIP;
608 #else
609 #if defined (DEV_RX_OFFLOAD_KEEP_CRC)
610                 if (val)
611                         cfg->requested_rx_offload &= ~DEV_RX_OFFLOAD_KEEP_CRC;
612                 else
613 #endif
614                         cfg->requested_rx_offload |= DEV_RX_OFFLOAD_KEEP_CRC;
615 #endif
616
617         }
618         else if (STR_EQ(str, "vlan tag")) {
619                 return parse_int_set(cfg->vlan_tags, pkey, sizeof(cfg->vlan_tags) / sizeof(cfg->vlan_tags[0]));
620         }
621         else if (STR_EQ(str, "vlan")) {
622 #if RTE_VERSION >= RTE_VERSION_NUM(18,8,0,1)
623                 uint32_t val;
624                 if (parse_bool(&val, pkey)) {
625                         return -1;
626                 }
627                 if (val) {
628                         cfg->requested_rx_offload |= DEV_RX_OFFLOAD_VLAN_STRIP;
629                         cfg->requested_tx_offload |= DEV_TX_OFFLOAD_VLAN_INSERT;
630                 } else {
631                         cfg->requested_rx_offload &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
632                         cfg->requested_tx_offload &= ~DEV_TX_OFFLOAD_VLAN_INSERT;
633                 }
634 #else
635                 plog_warn("vlan option not supported : update DPDK at least to 18.08 to support this option\n");
636 #endif
637         }
638         else if (STR_EQ(str, "mtu size")) {
639                 uint32_t val;
640                 if (parse_int(&val, pkey)) {
641                         return -1;
642                 }
643                 if (val) {
644                         cfg->mtu = val;
645                         // A frame of 1526 bytes (1500 bytes mtu, 14 bytes hdr, 4 bytes crc and 8 bytes vlan)
646                         // should not be considered as a jumbo frame. However rte_ethdev.c considers that
647                         // the max_rx_pkt_len for a non jumbo frame is 1518
648                         cfg->port_conf.rxmode.max_rx_pkt_len = cfg->mtu + PROX_RTE_ETHER_HDR_LEN + PROX_RTE_ETHER_CRC_LEN;
649                         if (cfg->port_conf.rxmode.max_rx_pkt_len > PROX_RTE_ETHER_MAX_LEN) {
650                                 cfg->requested_rx_offload |= DEV_RX_OFFLOAD_JUMBO_FRAME;
651                         }
652                 }
653         }
654
655         else if (STR_EQ(str, "rss")) {
656                 uint32_t val;
657                 if (parse_bool(&val, pkey)) {
658                         return -1;
659                 }
660                 if (val) {
661                         cfg->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
662                         cfg->port_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IPV4;
663                 }
664         }
665         else if (STR_EQ(str, "rx_ring")) {
666                 parse_str(cfg->rx_ring, pkey, sizeof(cfg->rx_ring));
667         }
668         else if (STR_EQ(str, "tx_ring")) {
669                 parse_str(cfg->tx_ring, pkey, sizeof(cfg->tx_ring));
670         }
671
672         return 0;
673 }
674
675 static enum police_action str_to_color(const char *str)
676 {
677         if (STR_EQ(str, "green"))
678                 return ACT_GREEN;
679         if (STR_EQ(str, "yellow"))
680                 return ACT_YELLOW;
681         if (STR_EQ(str, "red"))
682                 return ACT_RED;
683         if (STR_EQ(str, "drop"))
684                 return ACT_DROP;
685         return ACT_INVALID;
686 }
687
688 struct cfg_depr task_cfg_depr[] = {
689         {"sig", ""},
690 };
691
692 struct cfg_depr core_cfg_depr[] = {
693         {"do sig", ""},
694         {"lat", ""},
695         {"network side", ""},
696 };
697
698 /* [core] parser */
699 static int get_core_cfg(unsigned sindex, char *str, void *data)
700 {
701         char *pkey;
702         struct lcore_cfg *lconf = (struct lcore_cfg *)data;
703
704         if (str == NULL || lconf == NULL || !(sindex & CFG_INDEXED)) {
705                 return -1;
706         }
707
708         pkey = get_cfg_key(str);
709         if (pkey == NULL) {
710                 set_errf("Missing key after option");
711                 return -1;
712         }
713
714         uint32_t ncore = sindex & ~CFG_INDEXED;
715         if (ncore >= RTE_MAX_LCORE) {
716                 set_errf("Core index too high (max allowed %d)", RTE_MAX_LCORE - 1);
717                 return -1;
718         }
719
720         lconf = &lconf[ncore];
721
722         for (uint32_t i = 0; i < RTE_DIM(core_cfg_depr); ++i) {
723                 if (STR_EQ(str, core_cfg_depr[i].opt)) {
724                         set_errf("Option '%s' is deprecated%s%s",
725                                  core_cfg_depr[i].opt, strlen(core_cfg_depr[i].info)? ": ": "", core_cfg_depr[i].info);
726                         return -1;
727                 }
728         }
729
730         char buff[128];
731         lcore_to_socket_core_ht(ncore, buff, sizeof(buff));
732         set_self_var(buff);
733         if (STR_EQ(str, "task")) {
734
735                 uint32_t val;
736                 if (parse_int(&val, pkey)) {
737                         return -1;
738                 }
739                 if (val >= MAX_TASKS_PER_CORE) {
740                         set_errf("Too many tasks for core (max allowed %d)", MAX_TASKS_PER_CORE - 1);
741                         return -1;
742                 }
743                 if (val != lconf->n_tasks_all) {
744                         set_errf("Task ID skipped or defined twice");
745                         return -1;
746                 }
747
748                 lconf->active_task = val;
749
750                 lconf->targs[lconf->active_task].task = lconf->active_task;
751
752                 if (lconf->n_tasks_all < lconf->active_task + 1) {
753                         lconf->n_tasks_all = lconf->active_task + 1;
754                 }
755                 return 0;
756         }
757
758         struct task_args *targ = &lconf->targs[lconf->active_task];
759         if (STR_EQ(str, "tx ports from routing table")) {
760                 uint32_t vals[PROX_MAX_PORTS];
761                 uint32_t n_if;
762                 if (!(targ->task_init->flag_features & TASK_FEATURE_ROUTING)) {
763                         set_errf("tx port form route not supported mode %s",  targ->task_init->mode_str);
764                         return -1;
765                 }
766
767                 if (parse_port_name_list(vals, &n_if, PROX_MAX_PORTS, pkey)) {
768                         return -1;
769                 }
770
771                 for (uint8_t i = 0; i < n_if; ++i) {
772                         targ->tx_port_queue[i].port = vals[i];
773                         targ->nb_txports++;
774                 }
775                 targ->runtime_flags |= TASK_ROUTING;
776                 return 0;
777         }
778         if (STR_EQ(str, "tx ports from cpe table")) {
779                 uint32_t vals[PROX_MAX_PORTS];
780                 int n_remap = -1;
781                 uint32_t ret;
782                 uint32_t val;
783                 char* mapping_str = strstr(pkey, " remap=");
784
785                 if (mapping_str != NULL) {
786                         *mapping_str = 0;
787                         mapping_str += strlen(" remap=");
788                         n_remap = parse_remap(targ->mapping, mapping_str);
789                 }
790
791                 if (parse_port_name_list(vals, &ret, PROX_MAX_PORTS, pkey)) {
792                         return -1;
793                 }
794
795                 if (n_remap != -1 && ret != (uint32_t)n_remap) {
796                         set_errf("Expected %d remap elements but had %d", n_remap, ret);
797                         return -1;
798                 }
799
800                 for (uint8_t i = 0; i < ret; ++i) {
801                         targ->tx_port_queue[i].port = vals[i];
802
803                         /* default mapping this case is port0 -> port0 */
804                         if (n_remap == -1) {
805                                 targ->mapping[vals[i]] = i;
806                         }
807                 }
808
809                 targ->nb_txports = ret;
810
811                 return 0;
812         }
813         if (STR_EQ(str, "tx cores from routing table")) {
814                 if (!(targ->task_init->flag_features & TASK_FEATURE_ROUTING)) {
815                         set_errf("tx port form route not supported mode %s",  targ->task_init->mode_str);
816                         return -1;
817                 }
818
819                 struct core_task_set *cts = &targ->core_task_set[0];
820
821                 if (parse_task_set(cts, pkey))
822                         return -1;
823
824                 if (cts->n_elems > MAX_WT_PER_LB) {
825                         set_errf("Maximum worker threads allowed is %u but have %u", MAX_WT_PER_LB, cts->n_elems);
826                         return -1;
827                 }
828
829                 targ->nb_worker_threads = cts->n_elems;
830                 targ->nb_txrings = cts->n_elems;
831
832                 if (targ->nb_txrings > MAX_RINGS_PER_TASK) {
833                         set_errf("Maximum allowed TX rings is %u but have %u", MAX_RINGS_PER_TASK, targ->nb_txrings);
834                         return -1;
835                 }
836
837                 targ->runtime_flags |= TASK_ROUTING;
838                 return 0;
839         }
840         if (STR_EQ(str, "tx cores from cpe table")) {
841                 struct core_task_set *core_task_set =  &targ->core_task_set[0];
842                 int ret, ret2;
843                 char *mapping_str;
844
845                 mapping_str = strstr(pkey, " remap=");
846                 if (mapping_str == NULL) {
847                         set_errf("There is no default mapping for tx cores from cpe table. Please specify it through remap=");
848                         return -1;
849                 }
850                 *mapping_str = 0;
851                 mapping_str += strlen(" remap=");
852                 ret = parse_remap(targ->mapping, mapping_str);
853                 if (ret <= 0) {
854                         return -1;
855                 }
856
857                 struct core_task_set *cts = &targ->core_task_set[0];
858
859                 if (parse_task_set(cts, pkey))
860                         return -1;
861                 if (cts->n_elems > MAX_RINGS_PER_TASK) {
862                         set_errf("Maximum cores to route to is %u\n", MAX_RINGS_PER_TASK);
863                         return -1;
864                 }
865
866                 targ->nb_txrings = cts->n_elems;
867
868                 if (ret != targ->nb_txrings) {
869                         set_errf("Expecting same number of remaps as cores\n", str);
870                         return -1;
871                 }
872                 return 0;
873         }
874
875         if (STR_EQ(str, "delay ms")) {
876                 if (targ->delay_us) {
877                         set_errf("delay ms and delay us are mutually exclusive\n", str);
878                         return -1;
879                 }
880                 uint32_t delay_ms;
881                 int rc = parse_int(&delay_ms, pkey);
882                 targ->delay_us = delay_ms * 1000;
883                 return rc;
884         }
885         if (STR_EQ(str, "delay us")) {
886                 if (targ->delay_us) {
887                         set_errf("delay ms and delay us are mutually exclusive\n", str);
888                         return -1;
889                 }
890                 return parse_int(&targ->delay_us, pkey);
891         }
892         if (STR_EQ(str, "random delay us")) {
893                 return parse_int(&targ->random_delay_us, pkey);
894         }
895         if (STR_EQ(str, "cpe table timeout ms")) {
896                 return parse_int(&targ->cpe_table_timeout_ms, pkey);
897         }
898         if (STR_EQ(str, "ctrl path polling frequency")) {
899                 int rc = parse_int(&targ->ctrl_freq, pkey);
900                 if (rc == 0) {
901                         if (targ->ctrl_freq == 0) {
902                                 set_errf("ctrl frequency must be non null.");
903                                 return -1;
904                         }
905                 }
906                 return rc;
907         }
908
909         if (STR_EQ(str, "handle arp")) {
910                 return parse_flag(&targ->runtime_flags, TASK_CTRL_HANDLE_ARP, pkey);
911         }
912         if (STR_EQ(str, "fast path handle arp")) {
913                 return parse_flag(&targ->runtime_flags, TASK_FP_HANDLE_ARP, pkey);
914         }
915
916         /* Using tx port name, only a _single_ port can be assigned to a task. */
917         if (STR_EQ(str, "tx port")) {
918                 if (targ->nb_txports > 0) {
919                         set_errf("Only one tx port can be defined per task. Use a LB task or routing instead.");
920                         return -1;
921                 }
922
923                 uint32_t n_if = 0;
924                 uint32_t ports[PROX_MAX_PORTS];
925
926                 if(parse_port_name_list(ports, &n_if, PROX_MAX_PORTS, pkey)) {
927                         // Port name not found, but could be a virtual device of a secondary process
928                         // As DPDK not started yet, we can only check the config file to see whether we are a secondary process
929                         if (rte_cfg.eal &&
930                                         (strstr(rte_cfg.eal, "secondary") || strstr(rte_cfg.eal, "auto")) &&
931                                         (n_deferred_ports < PROX_MAX_PORTS)) {
932                                 prox_strncpy(deferred_port[n_deferred_ports].name, pkey, sizeof(deferred_port[n_deferred_ports].name));
933                                 deferred_port[n_deferred_ports].is_rx_port = 0;
934                                 deferred_port[n_deferred_ports++].targ = targ;
935                                 return 0;
936                         } else
937                                 return -1;
938                 }
939
940                 PROX_ASSERT(n_if-1 < PROX_MAX_PORTS);
941
942                 for (uint8_t i = 0; i < n_if; ++i) {
943                         targ->tx_port_queue[i].port = ports[i];
944                         targ->nb_txports++;
945                 }
946
947                 if (n_if > 1) {
948                         targ->nb_worker_threads = targ->nb_txports;
949                 }
950
951                 return 0;
952         }
953         if (STR_EQ(str, "rx ring")) {
954                 uint32_t val;
955                 int err = parse_bool(&val, pkey);
956                 if (!err && val && targ->rx_port_queue[0].port != OUT_DISCARD) {
957                         set_errf("Can't read both from internal ring and external port from the same task. Use multiple tasks instead.");
958                         return -1;
959                 }
960
961                 return parse_flag(&targ->flags, TASK_ARG_RX_RING, pkey);
962         }
963         if (STR_EQ(str, "private")) {
964                 return parse_bool(&targ->use_src, pkey);
965         }
966         if (STR_EQ(str, "use src ip")) {
967                 return parse_bool(&targ->use_src, pkey);
968         }
969         if (STR_EQ(str, "nat table")) {
970                 return parse_str(targ->nat_table, pkey, sizeof(targ->nat_table));
971         }
972         if (STR_EQ(str, "rules")) {
973                 return parse_str(targ->rules, pkey, sizeof(targ->rules));
974         }
975         if (STR_EQ(str, "route table")) {
976                 return parse_str(targ->route_table, pkey, sizeof(targ->route_table));
977         }
978         if (STR_EQ(str, "dscp")) {
979                 return parse_str(targ->dscp, pkey, sizeof(targ->dscp));
980         }
981         if (STR_EQ(str, "tun_bindings")) {
982                 return parse_str(targ->tun_bindings, pkey, sizeof(targ->tun_bindings));
983         }
984         if (STR_EQ(str, "cpe table")) {
985                 return parse_str(targ->cpe_table_name, pkey, sizeof(targ->cpe_table_name));
986         }
987         if (STR_EQ(str, "user table")) {
988                 return parse_str(targ->user_table, pkey, sizeof(targ->user_table));
989         }
990         if (STR_EQ(str, "streams")) {
991                 return parse_str(targ->streams, pkey, sizeof(targ->streams));
992         }
993         if (STR_EQ(str, "Unsollicited NA")) {
994                 return parse_flag(&targ->flags, TASK_ARG_SEND_NA_AT_STARTUP, pkey);
995         }
996         if (STR_EQ(str, "local lpm")) {
997                 return parse_flag(&targ->flags, TASK_ARG_LOCAL_LPM, pkey);
998         }
999         if (STR_EQ(str, "drop")) {
1000                 return parse_flag(&targ->flags, TASK_ARG_DROP, pkey);
1001         }
1002         if (STR_EQ(str, "loop")) {
1003                 parse_flag(&targ->loop, 1, pkey);
1004                 return parse_flag(&targ->loop, 1, pkey);
1005         }
1006         if (STR_EQ(str, "qinq")) {
1007                 return parse_flag(&targ->flags, TASK_ARG_QINQ_ACL, pkey);
1008         }
1009         if (STR_EQ(str, "bps")) {
1010                 return parse_u64(&targ->rate_bps, pkey);
1011         }
1012         if (STR_EQ(str, "random")) {
1013                 return parse_str(targ->rand_str[targ->n_rand_str++], pkey, sizeof(targ->rand_str[0]));
1014         }
1015         if (STR_EQ(str, "rand_offset")) {
1016                 if (targ->n_rand_str == 0) {
1017                         set_errf("No random defined previously (use random=...)");
1018                         return -1;
1019                 }
1020
1021                 return parse_int(&targ->rand_offset[targ->n_rand_str - 1], pkey);
1022         }
1023         if (STR_EQ(str, "keep src mac")) {
1024                 return parse_flag(&targ->flags, DSF_KEEP_SRC_MAC, pkey);
1025         }
1026         if (STR_EQ(str, "pcap file")) {
1027                 return parse_str(targ->pcap_file, pkey, sizeof(targ->pcap_file));
1028         }
1029         if (STR_EQ(str, "imix")) {
1030                 char pkey2[MAX_CFG_STRING_LEN], *ptr;
1031                 if (parse_str(pkey2, pkey, sizeof(pkey2)) != 0) {
1032                         set_errf("Error while parsing imix, too long\n");
1033                         return -1;
1034                 }
1035                 const size_t pkey_len = strlen(pkey2);
1036                 targ->imix_nb_pkts = 0;
1037                 ptr = pkey2;
1038                 while (targ->imix_nb_pkts < MAX_IMIX_PKTS) {
1039                         if (parse_int(&targ->imix_pkt_sizes[targ->imix_nb_pkts], ptr) != 0)
1040                                 break;
1041                         targ->imix_nb_pkts++;
1042                         if ((ptr = strchr(ptr, ',')) == NULL)
1043                                 break;
1044                         ptr++;
1045                         if (targ->imix_nb_pkts == MAX_IMIX_PKTS) {
1046                                 set_errf("Too many packet sizes specified");
1047                                 return -1;
1048                         }
1049                 }
1050                 plog_info("%d IMIX packets:", targ->imix_nb_pkts);
1051                 for (size_t i = 0; i < targ->imix_nb_pkts; ++i) {
1052                         plog_info("%d ", targ->imix_pkt_sizes[i]);
1053                 }
1054                 plog_info("\n");
1055                 return 0;
1056         }
1057         if (STR_EQ(str, "pkt inline")) {
1058                 char pkey2[MAX_CFG_STRING_LEN];
1059                 if (parse_str(pkey2, pkey, sizeof(pkey2)) != 0) {
1060                         set_errf("Error while parsing pkt line, too long\n");
1061                         return -1;
1062                 }
1063
1064                 const size_t pkey_len = strlen(pkey2);
1065                 targ->pkt_size = 0;
1066
1067                 for (size_t i = 0; i < pkey_len; ++i) {
1068                         if (pkey2[i] == ' ')
1069                                 continue;
1070
1071                         if (i + 1 == pkey_len) {
1072                                 set_errf("Incomplete byte at character %z", i);
1073                                 return -1;
1074                         }
1075
1076                         uint8_t byte = 0;
1077
1078                         if (pkey2[i] >= '0' && pkey2[i] <= '9') {
1079                                 byte = (pkey2[i] - '0') << 4;
1080                         }
1081                         else if (pkey2[i] >= 'a' && pkey2[i] <= 'f') {
1082                                 byte = (pkey2[i] - 'a' + 10) << 4;
1083                         }
1084                         else if (pkey2[i] >= 'A' && pkey2[i] <= 'F') {
1085                                 byte = (pkey2[i] - 'A' + 10) << 4;
1086                         }
1087                         else {
1088                                 set_errf("Invalid character in pkt inline at byte %d (%c)", i, pkey2[i]);
1089                                 return -1;
1090                         }
1091
1092                         if (pkey2[i + 1] >= '0' && pkey2[i + 1] <= '9') {
1093                                 byte |= (pkey2[i + 1] - '0');
1094                         }
1095                         else if (pkey2[i + 1] >= 'a' && pkey2[i + 1] <= 'f') {
1096                                 byte |= (pkey2[i + 1] - 'a' + 10);
1097                         }
1098                         else if (pkey2[i + 1] >= 'A' && pkey2[i + 1] <= 'F') {
1099                                 byte |= (pkey2[i + 1] - 'A' + 10);
1100                         }
1101                         else {
1102                                 set_errf("Invalid character in pkt inline at byte %d (%c)", i, pkey2[i + 1]);
1103                                 return -1;
1104                         }
1105                         if (targ->pkt_size == sizeof(targ->pkt_inline)) {
1106                                 set_errf("Inline packet definition can't be longer than %u", sizeof(targ->pkt_inline));
1107                                 return -1;
1108                         }
1109
1110                         targ->pkt_inline[targ->pkt_size++] = byte;
1111                         i += 1;
1112                 }
1113
1114                 return 0;
1115         }
1116         if (STR_EQ(str, "accuracy limit nsec")) {
1117                 return parse_int(&targ->accuracy_limit_nsec, pkey);
1118         }
1119         if (STR_EQ(str, "latency bucket size")) {
1120                 return parse_int(&targ->bucket_size, pkey);
1121         }
1122         if (STR_EQ(str, "latency buffer size")) {
1123                 return parse_int(&targ->latency_buffer_size, pkey);
1124         }
1125         if (STR_EQ(str, "accuracy pos")) {
1126                 return parse_int(&targ->accur_pos, pkey);
1127         }
1128         if (STR_EQ(str, "signature")) {
1129                 return parse_int(&targ->sig, pkey);
1130         }
1131         if (STR_EQ(str, "signature pos")) {
1132                 return parse_int(&targ->sig_pos, pkey);
1133         }
1134         if (STR_EQ(str, "lat pos")) {
1135                 targ->lat_enabled = 1;
1136                 return parse_int(&targ->lat_pos, pkey);
1137         }
1138         if (STR_EQ(str, "packet id pos")) {
1139                 return parse_int(&targ->packet_id_pos, pkey);
1140         }
1141         if (STR_EQ(str, "probability")) { // old - use "probability no drop" instead
1142                 float probability;
1143                 int rc = parse_float(&probability, pkey);
1144                 if (probability == 0) {
1145                         set_errf("Probability must be != 0\n");
1146                         return -1;
1147                 } else if (probability > 100.0) {
1148                         set_errf("Probability must be < 100\n");
1149                         return -1;
1150                 }
1151                 targ->probability_no_drop = probability * 10000;
1152                 return rc;
1153         }
1154         if (STR_EQ(str, "proba no drop")) {
1155                 float probability;
1156                 int rc = parse_float(&probability, pkey);
1157                 if (probability == 0) {
1158                         set_errf("probability no drop must be != 0\n");
1159                         return -1;
1160                 } else if (probability > 100.0) {
1161                         set_errf("Probability must be < 100\n");
1162                         return -1;
1163                 }
1164                 targ->probability_no_drop = probability * 10000;
1165                 return rc;
1166         }
1167         if (STR_EQ(str, "proba delay")) {
1168                 float probability;
1169                 int rc = parse_float(&probability, pkey);
1170                 if (probability > 100.0) {
1171                         set_errf("Probability must be < 100\n");
1172                         return -1;
1173                 }
1174                 targ->probability_delay = probability * 10000;
1175                 return rc;
1176         }
1177 #if RTE_VERSION >= RTE_VERSION_NUM(19,11,0,0)
1178         if (STR_EQ(str, "proba duplicate")) {
1179                 float probability;
1180                 int rc = parse_float(&probability, pkey);
1181                 if (probability > 100.0) {
1182                         set_errf("probability duplicate must be < 100\n");
1183                         return -1;
1184                 }
1185                 targ->probability_duplicate = probability * 10000;
1186                 return rc;
1187         }
1188 #endif
1189         if (STR_EQ(str, "concur conn")) {
1190                 return parse_int(&targ->n_concur_conn, pkey);
1191         }
1192         if (STR_EQ(str, "max setup rate")) {
1193                 return parse_int(&targ->max_setup_rate, pkey);
1194         }
1195         if (STR_EQ(str, "pkt size")) {
1196                 return parse_int(&targ->pkt_size, pkey);
1197         }
1198         if (STR_EQ(str, "min bulk size")) {
1199                 return parse_int(&targ->min_bulk_size, pkey);
1200         }
1201         if (STR_EQ(str, "max bulk size")) {
1202                 return parse_int(&targ->max_bulk_size, pkey);
1203         }
1204         if (STR_EQ(str, "rx port")) {
1205                 if (targ->flags & TASK_ARG_RX_RING) {
1206                         set_errf("Can't read both from internal ring and external port from the same task. Use multiple tasks instead.");
1207                         return -1;
1208                 }
1209                 uint32_t vals[PROX_MAX_PORTS];
1210                 uint32_t n_if;
1211
1212                 if (parse_port_name_list(vals, &n_if, PROX_MAX_PORTS, pkey)) {
1213                         // Port name not found, but could be a virtual device of a secondary process
1214                         // As DPDK not started yet, we can only check the config file to see whether we are a secondary process
1215                         if (rte_cfg.eal &&
1216                                         (strstr(rte_cfg.eal, "secondary") || strstr(rte_cfg.eal, "auto")) &&
1217                                         (n_deferred_ports < PROX_MAX_PORTS)) {
1218                                 prox_strncpy(deferred_port[n_deferred_ports].name, pkey, sizeof(deferred_port[n_deferred_ports].name));
1219                                 deferred_port[n_deferred_ports].is_rx_port = 1;
1220                                 deferred_port[n_deferred_ports++].targ = targ;
1221                                 return 0;
1222                         } else
1223                                 return -1;
1224                 }
1225
1226                 for (uint8_t i = 0; i < n_if; ++i) {
1227                         PROX_ASSERT(vals[i] < PROX_MAX_PORTS);
1228                         targ->rx_port_queue[i].port = vals[i];
1229                         targ->nb_rxports++;
1230                 }
1231                 return 0;
1232         }
1233
1234         if (STR_EQ(str, "mode")) {
1235                 /* Check deprecated task modes */
1236                 char mode[255];
1237                 int ret = parse_str(mode, pkey, sizeof(mode));
1238                 if (ret)
1239                         return ret;
1240
1241                 for (uint32_t i = 0; i < RTE_DIM(task_cfg_depr); ++i) {
1242                         if (STR_EQ(mode, task_cfg_depr[i].opt)) {
1243                                 set_errf("Task mode '%s' is deprecated%s%s",
1244                                          task_cfg_depr[i].opt, strlen(task_cfg_depr[i].info)? ": ": "", task_cfg_depr[i].info);
1245                                 return -1;
1246                         }
1247                 }
1248
1249                 /* master is a special mode that is always needed (cannot be turned off) */
1250                 if (STR_EQ(mode, "master")) {
1251                         prox_cfg.master = ncore;
1252                         targ->mode = MASTER;
1253                         if (lconf->n_tasks_all > 1 || targ->task != 0) {
1254                                 set_errf("Master core can only have one task\n");
1255                                 return -1;
1256                         }
1257                         // Initialize number of tasks to 1 for master, even if no task specified
1258                         lconf->n_tasks_all = 1;
1259                         lconf->active_task = 0;
1260                         lconf->targs[lconf->active_task].task = 0;
1261                         struct task_init* task_init = to_task_init(mode, "");
1262                         if (task_init) {
1263                                 targ->mode = task_init->mode;
1264                         }
1265                         targ->task_init = task_init;
1266                         return 0;
1267                 }
1268
1269                 struct task_init* task_init = to_task_init(mode, "");
1270                 if (task_init) {
1271                         targ->mode = task_init->mode;
1272                 }
1273                 else {
1274                         set_errf("Task mode '%s' is invalid", mode);
1275                         tasks_list();
1276                         return -1;
1277                 }
1278                 targ->task_init = task_init;
1279                 return 0;
1280         }
1281         if (STR_EQ(str, "users")) {
1282                 return parse_int(&targ->n_flows, pkey);
1283         }
1284
1285         if (STR_EQ(str, "mark")) {
1286                 return parse_flag(&targ->runtime_flags, TASK_MARK, pkey);
1287         }
1288
1289         if (STR_EQ(str, "mark green")) {
1290                 return parse_int(&targ->marking[0], pkey);
1291         }
1292
1293         if (STR_EQ(str, "mark yellow")) {
1294                 return parse_int(&targ->marking[1], pkey);
1295         }
1296
1297         if (STR_EQ(str, "mark red")) {
1298                 return parse_int(&targ->marking[2], pkey);
1299         }
1300
1301         if (STR_EQ(str, "tx cores")) {
1302                 uint8_t dest_task = 0;
1303                 /* if user did not specify, dest_port is left at default (first type) */
1304                 uint8_t dest_proto = 0;
1305                 uint8_t ctrl = CTRL_TYPE_DP;
1306                 char *task_str = strstr(pkey, "proto=");
1307                 if (task_str) {
1308                         task_str += strlen("proto=");
1309
1310                         if (STR_EQ(task_str, "ipv4")) {
1311                                 dest_proto = IPV4;
1312                         }
1313                         else if (STR_EQ(task_str, "arp")) {
1314                                 dest_proto = ARP;
1315                         }
1316                         else if (STR_EQ(task_str, "ipv6")) {
1317                                 dest_proto = IPV6;
1318                         }
1319                         else {
1320                                 set_errf("proto needs to be either ipv4, arp or ipv6");
1321                                 return -1;
1322                         }
1323
1324                 }
1325
1326                 task_str = strstr(pkey, "task=");
1327
1328                 if (task_str) {
1329                         --task_str;
1330                         *task_str = 0;
1331                         task_str++;
1332                         task_str += strlen("task=");
1333                         char *task_str_end = strstr(task_str, " ");
1334                         if (task_str_end) {
1335                                 *task_str_end = 0;
1336                         }
1337                         if (0 == strlen(task_str)) {
1338                                 set_errf("Invalid task= syntax");
1339                                 return -1;
1340                         }
1341
1342                         switch (task_str[strlen(task_str) - 1]) {
1343                         case 'p':
1344                                 ctrl = CTRL_TYPE_PKT;
1345                                 break;
1346                         case 'm':
1347                                 ctrl = CTRL_TYPE_MSG;
1348                                 break;
1349                         case '\n':
1350                         case 0:
1351                                 break;
1352                         default:
1353                                 if (task_str[strlen(task_str) -1] < '0' ||
1354                                     task_str[strlen(task_str) -1] > '9') {
1355                                         set_errf("Unknown ring type %c.\n",
1356                                                  task_str[strlen(task_str) - 1]);
1357                                         return -1;
1358                                 }
1359                         }
1360
1361                         dest_task = atoi(task_str);
1362                         if (dest_task >= MAX_TASKS_PER_CORE) {
1363                                 set_errf("Destination task too high (max allowed %d)", MAX_TASKS_PER_CORE - 1);
1364                                 return -1;
1365                         }
1366                 }
1367                 else {
1368                         dest_task = 0;
1369                 }
1370
1371                 struct core_task_set *cts = &targ->core_task_set[dest_proto];
1372
1373                 if (parse_task_set(cts, pkey))
1374                         return -1;
1375
1376                 if (cts->n_elems > MAX_WT_PER_LB) {
1377                         set_errf("Too many worker threads (max allowed %d)", MAX_WT_PER_LB - 1);
1378                         return -1;
1379                 }
1380
1381                 targ->nb_worker_threads = cts->n_elems;
1382                 targ->nb_txrings += cts->n_elems;
1383
1384                 return 0;
1385         }
1386         if (STR_EQ(str, "tx crc")) {
1387                 return parse_flag(&targ->runtime_flags, TASK_TX_CRC, pkey);
1388         }
1389         if (STR_EQ(str, "ring size")) {
1390                 return parse_int(&targ->ring_size, pkey);
1391         }
1392         if (STR_EQ(str, "mempool size")) {
1393                 return parse_kmg(&targ->nb_mbuf, pkey);
1394         }
1395
1396         else if (STR_EQ(str, "mbuf size")) {
1397                 return parse_int(&targ->mbuf_size, pkey);
1398         }
1399         if (STR_EQ(str, "memcache size")) {
1400                 return parse_kmg(&targ->nb_cache_mbuf, pkey);
1401         }
1402
1403         if (STR_EQ(str, "byte offset")) {
1404                 return parse_int(&targ->byte_offset, pkey);
1405         }
1406
1407         if (STR_EQ(str, "realtime scheduling")) {
1408                 return parse_flag(&lconf->flags, LCONF_FLAG_SCHED_RR, pkey);
1409         }
1410         if (STR_EQ(str, "name")) {
1411                 return parse_str(lconf->name, pkey, sizeof(lconf->name));
1412         }
1413         /* MPLS configuration */
1414         if (STR_EQ(str, "untag mpls")) {
1415                 return parse_flag(&targ->runtime_flags, TASK_MPLS_TAGGING, pkey);
1416         }
1417
1418         if (STR_EQ(str, "add mpls")) {
1419                 return parse_flag(&targ->runtime_flags, TASK_MPLS_TAGGING, pkey);
1420         }
1421
1422         if (STR_EQ(str, "ether type")) {
1423                 return parse_int(&targ->etype, pkey);
1424         }
1425
1426         if (STR_EQ(str, "cache set")) {
1427                 return parse_int(&lconf->cache_set, pkey);
1428         }
1429
1430         if (STR_EQ(str, "sub mode")) {
1431                 const char* mode_str = targ->task_init->mode_str;
1432                 const char *sub_mode_str = pkey;
1433
1434                 targ->task_init = to_task_init(mode_str, sub_mode_str);
1435                 if (!targ->task_init) {
1436                         if ((strcmp(sub_mode_str, "l3") != 0) && (strcmp(sub_mode_str, "ndp") != 0)) {
1437                                 set_errf("sub mode %s not supported for mode %s", sub_mode_str, mode_str);
1438                                 return -1;
1439                         }
1440                         targ->task_init = to_task_init(mode_str, "");
1441                         if (!targ->task_init) {
1442                                 set_errf("sub mode %s not supported for mode %s", sub_mode_str, mode_str);
1443                                 return -1;
1444                         }
1445                 }
1446                 if (strcmp(sub_mode_str, "l3") == 0) {
1447                         prox_cfg.flags |= DSF_L3_ENABLED;
1448                         targ->flags |= TASK_ARG_L3;
1449                         strcpy(targ->sub_mode_str, "l3");
1450                 } else if (strcmp(sub_mode_str, "ndp") == 0) {
1451                         prox_cfg.flags |= DSF_NDP_ENABLED;
1452                         targ->flags |= TASK_ARG_NDP;
1453                         strcpy(targ->sub_mode_str, "ndp");
1454                 } else {
1455                         strcpy(targ->sub_mode_str, targ->task_init->sub_mode_str);
1456                 }
1457                 return 0;
1458         }
1459
1460         if (STR_EQ(str, "mempool name")) {
1461                 return parse_str(targ->pool_name, pkey, sizeof(targ->pool_name));
1462         }
1463         if (STR_EQ(str, "dpi engine")) {
1464                 return parse_str(targ->dpi_engine_path, pkey, sizeof(targ->dpi_engine_path));
1465         }
1466         if (STR_EQ(str, "dpi engine arg")) {
1467                 return parse_str(targ->dpi_engine_args[targ->n_dpi_engine_args++], pkey,
1468                                  sizeof(targ->dpi_engine_args[0]));
1469         }
1470         if (STR_EQ(str, "dst mac")) { /* destination MAC address to be used for packets */
1471                 if (parse_mac(&targ->edaddr, pkey)) {
1472                         if (STR_EQ(pkey, "no")) {
1473                                 targ->flags |= TASK_ARG_DO_NOT_SET_DST_MAC;
1474                                 return 0;
1475                         }
1476                         if (STR_EQ(pkey, "packet") == 0)
1477                                 return -1;
1478                         else
1479                                 return 0;
1480                 }
1481                 targ->flags |= TASK_ARG_DST_MAC_SET;
1482                 return 0;
1483         }
1484         if (STR_EQ(str, "src mac")) {
1485                 if (parse_mac(&targ->esaddr, pkey)) {
1486                         if (STR_EQ(pkey, "no")) {
1487                                 targ->flags |= TASK_ARG_DO_NOT_SET_SRC_MAC;
1488                                 return 0;
1489                         }
1490                         else if (STR_EQ(pkey, "packet"))
1491                                 return 0;
1492                         else if (STR_EQ(pkey, "hw")) {
1493                                 targ->flags |= TASK_ARG_HW_SRC_MAC;
1494                                 return 0;
1495                         } else {
1496                                 return -1;
1497                         }
1498                 }
1499                 targ->flags |= TASK_ARG_SRC_MAC_SET;
1500                 return 0;
1501         }
1502         if (STR_EQ(str, "igmp ipv4")) { /* IGMP Group */
1503                 return parse_ip(&targ->igmp_address, pkey);
1504         }
1505         if (STR_EQ(str, "gateway ipv4")) { /* Gateway IP address used when generating */
1506                 if ((targ->flags & TASK_ARG_L3) == 0)
1507                         plog_warn("gateway ipv4 configured but L3 sub mode not enabled\n");
1508                 if (targ->local_ipv4)
1509                         targ->local_prefix = 32;
1510                 return parse_ip(&targ->gateway_ipv4, pkey);
1511         }
1512         if (STR_EQ(str, "ipv6 router")) { /* we simulate an IPV6 router */
1513                 int rc = parse_flag(&targ->ipv6_router, 1, pkey);
1514                 if (!rc && targ->ipv6_router) {
1515                         plog_info("\tipv6 router configured => NDP enabled\n");
1516                         prox_cfg.flags |= DSF_NDP_ENABLED;
1517                         targ->flags |= TASK_ARG_NDP;
1518                         strcpy(targ->sub_mode_str, "ndp");
1519                 }
1520                 return 0;
1521         }
1522         if (STR_EQ(str, "gateway ipv6")) { /* Gateway IP address used when generating */
1523                 if ((targ->flags & TASK_ARG_NDP) == 0)
1524                         plog_warn("gateway ipv6 configured but NDP sub mode not enabled\n");
1525                 return parse_ip6(&targ->gateway_ipv6, pkey);
1526         }
1527         if (STR_EQ(str, "local ipv4")) { /* source IP address to be used for packets */
1528                 struct ip4_subnet cidr;
1529                 if (parse_ip4_and_prefix(&cidr, pkey) != 0) {
1530                         if (targ->gateway_ipv4)
1531                                 targ->local_prefix = 32;
1532                         else
1533                                 targ->local_prefix = 0;
1534                         return parse_ip(&targ->local_ipv4, pkey);
1535                 } else {
1536                         targ->local_ipv4 = cidr.ip;
1537                         targ->local_prefix = cidr.prefix;
1538                         return 0;
1539                 }
1540         }
1541         if (STR_EQ(str, "remote ipv4")) { /* source IP address to be used for packets */
1542                 return parse_ip(&targ->remote_ipv4, pkey);
1543         }
1544         if (STR_EQ(str, "global ipv6")) {
1545                 if (parse_ip6(&targ->global_ipv6, pkey) == 0) {
1546                         plog_info("\tglobal ipv6 configured => NDP enabled\n");
1547                         targ->flags |= TASK_ARG_NDP;
1548                         prox_cfg.flags |= DSF_NDP_ENABLED;
1549                         strcpy(targ->sub_mode_str, "ndp");
1550                 } else {
1551                         plog_err("Unable to parse content of local ipv6: %s\n", pkey);
1552                         return -1;
1553                 }
1554                 return 0;
1555         }
1556         if (STR_EQ(str, "local ipv6")) { /* source IPv6 address to be used for packets */
1557                 if (parse_ip6(&targ->local_ipv6, pkey) == 0) {
1558                         plog_info("\tlocal ipv6 configured => NDP enabled\n");
1559                         targ->flags |= TASK_ARG_NDP;
1560                         prox_cfg.flags |= DSF_NDP_ENABLED;
1561                         strcpy(targ->sub_mode_str, "ndp");
1562                 } else {
1563                         plog_err("Unable to parse content of local ipv6: %s\n", pkey);
1564                         return -1;
1565                 }
1566                 return 0;
1567         }
1568         if (STR_EQ(str, "router prefix")) {
1569                 if (parse_ip6(&targ->router_prefix, pkey) == 0) {
1570                         plog_info("\trouter prefix set to "IPv6_BYTES_FMT" (%s)\n", IPv6_BYTES(targ->router_prefix.bytes), IP6_Canonical(&targ->router_prefix));
1571                 } else {
1572                         plog_err("Unable to parse content of router prefix: %s\n", pkey);
1573                         return -1;
1574                 }
1575                 return 0;
1576         }
1577         if (STR_EQ(str, "arp timeout"))
1578                 return parse_int(&targ->reachable_timeout, pkey);
1579         if (STR_EQ(str, "arp update time"))
1580                 return parse_int(&targ->arp_ndp_retransmit_timeout, pkey);
1581         if (STR_EQ(str, "number of packets"))
1582                 return parse_int(&targ->n_pkts, pkey);
1583         if (STR_EQ(str, "pipes")) {
1584                 uint32_t val;
1585                 int err = parse_int(&val, pkey);
1586                 if (err)
1587                         return -1;
1588                 if (!val || !rte_is_power_of_2(val)) {
1589                         set_errf("Number of pipes has to be power of 2 and not zero");
1590                         return -1;
1591                 }
1592
1593                 targ->qos_conf.port_params.n_pipes_per_subport = val;
1594                 return 0;
1595         }
1596         if (STR_EQ(str, "queue size")) {
1597                 uint32_t val;
1598                 int err = parse_int(&val, pkey);
1599                 if (err) {
1600                         return -1;
1601                 }
1602 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1603                 targ->qos_conf.subport_params[0].qsize[0] = val;
1604                 targ->qos_conf.subport_params[0].qsize[1] = val;
1605                 targ->qos_conf.subport_params[0].qsize[2] = val;
1606                 targ->qos_conf.subport_params[0].qsize[3] = val;
1607 #else
1608                 targ->qos_conf.port_params.qsize[0] = val;
1609                 targ->qos_conf.port_params.qsize[1] = val;
1610                 targ->qos_conf.port_params.qsize[2] = val;
1611                 targ->qos_conf.port_params.qsize[3] = val;
1612 #endif
1613                 return 0;
1614         }
1615         if (STR_EQ(str, "subport tb rate")) {
1616 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1617                 return parse_u64(&targ->qos_conf.subport_params[0].tb_rate, pkey);
1618 #else
1619                 return parse_int(&targ->qos_conf.subport_params[0].tb_rate, pkey);
1620 #endif
1621         }
1622         if (STR_EQ(str, "subport tb size")) {
1623 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1624                 return parse_u64(&targ->qos_conf.subport_params[0].tb_size, pkey);
1625 #else
1626                 return parse_int(&targ->qos_conf.subport_params[0].tb_size, pkey);
1627 #endif
1628         }
1629         if (STR_EQ(str, "subport tc 0 rate")) {
1630 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1631                 return parse_u64(&targ->qos_conf.subport_params[0].tc_rate[0], pkey);
1632 #else
1633                 return parse_int(&targ->qos_conf.subport_params[0].tc_rate[0], pkey);
1634 #endif
1635         }
1636         if (STR_EQ(str, "subport tc 1 rate")) {
1637 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1638                 return parse_u64(&targ->qos_conf.subport_params[0].tc_rate[1], pkey);
1639 #else
1640                 return parse_int(&targ->qos_conf.subport_params[0].tc_rate[1], pkey);
1641 #endif
1642         }
1643         if (STR_EQ(str, "subport tc 2 rate")) {
1644 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1645                 return parse_u64(&targ->qos_conf.subport_params[0].tc_rate[2], pkey);
1646 #else
1647                 return parse_int(&targ->qos_conf.subport_params[0].tc_rate[2], pkey);
1648 #endif
1649         }
1650         if (STR_EQ(str, "subport tc 3 rate")) {
1651 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1652                 return parse_u64(&targ->qos_conf.subport_params[0].tc_rate[3], pkey);
1653 #else
1654                 return parse_int(&targ->qos_conf.subport_params[0].tc_rate[3], pkey);
1655 #endif
1656         }
1657
1658         if (STR_EQ(str, "subport tc rate")) {
1659                 uint32_t val;
1660                 int err = parse_int(&val, pkey);
1661                 if (err) {
1662                         return -1;
1663                 }
1664
1665                 targ->qos_conf.subport_params[0].tc_rate[0] = val;
1666                 targ->qos_conf.subport_params[0].tc_rate[1] = val;
1667                 targ->qos_conf.subport_params[0].tc_rate[2] = val;
1668                 targ->qos_conf.subport_params[0].tc_rate[3] = val;
1669
1670                 return 0;
1671         }
1672         if (STR_EQ(str, "subport tc period")) {
1673 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1674                 return parse_u64(&targ->qos_conf.subport_params[0].tc_period, pkey);
1675 #else
1676                 return parse_int(&targ->qos_conf.subport_params[0].tc_period, pkey);
1677 #endif
1678         }
1679         if (STR_EQ(str, "pipe tb rate")) {
1680 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1681                 return parse_u64(&targ->qos_conf.pipe_params[0].tb_rate, pkey);
1682 #else
1683                 return parse_int(&targ->qos_conf.pipe_params[0].tb_rate, pkey);
1684 #endif
1685         }
1686         if (STR_EQ(str, "pipe tb size")) {
1687 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1688                 return parse_u64(&targ->qos_conf.pipe_params[0].tb_size, pkey);
1689 #else
1690                 return parse_int(&targ->qos_conf.pipe_params[0].tb_size, pkey);
1691 #endif
1692         }
1693         if (STR_EQ(str, "pipe tc rate")) {
1694                 uint32_t val;
1695                 int err = parse_int(&val, pkey);
1696                 if (err) {
1697                         return -1;
1698                 }
1699
1700                 targ->qos_conf.pipe_params[0].tc_rate[0] = val;
1701                 targ->qos_conf.pipe_params[0].tc_rate[1] = val;
1702                 targ->qos_conf.pipe_params[0].tc_rate[2] = val;
1703                 targ->qos_conf.pipe_params[0].tc_rate[3] = val;
1704                 return 0;
1705         }
1706         if (STR_EQ(str, "pipe tc 0 rate")) {
1707 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1708                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_rate[0], pkey);
1709 #else
1710                 return parse_int(&targ->qos_conf.pipe_params[0].tc_rate[0], pkey);
1711 #endif
1712         }
1713         if (STR_EQ(str, "pipe tc 1 rate")) {
1714 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1715                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_rate[1], pkey);
1716 #else
1717                 return parse_int(&targ->qos_conf.pipe_params[0].tc_rate[1], pkey);
1718 #endif
1719         }
1720         if (STR_EQ(str, "pipe tc 2 rate")) {
1721 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1722                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_rate[2], pkey);
1723 #else
1724                 return parse_int(&targ->qos_conf.pipe_params[0].tc_rate[2], pkey);
1725 #endif
1726         }
1727         if (STR_EQ(str, "pipe tc 3 rate")) {
1728 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1729                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_rate[3], pkey);
1730 #else
1731                 return parse_int(&targ->qos_conf.pipe_params[0].tc_rate[3], pkey);
1732 #endif
1733         }
1734         if (STR_EQ(str, "pipe tc period")) {
1735 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1736                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_period, pkey);
1737 #else
1738                 return parse_int(&targ->qos_conf.pipe_params[0].tc_period, pkey);
1739 #endif
1740         }
1741         if (STR_EQ(str, "police action")) {
1742                 char *in = strstr(pkey, " io=");
1743                 if (in == NULL) {
1744                         set_errf("Need to specify io colors using io=in_color,out_color\n");
1745                         return -1;
1746                 }
1747                 *in = 0;
1748                 in += strlen(" io=");
1749
1750                 char *out = strstr(in, ",");
1751                 if (out == NULL) {
1752                         set_errf("Output color not specified\n");
1753                 }
1754                 *out = 0;
1755                 out++;
1756
1757                 enum police_action in_color = str_to_color(in);
1758                 enum police_action out_color = str_to_color(out);
1759
1760                 if (in_color == ACT_INVALID) {
1761                         set_errf("Invalid input color %s. Expected green, yellow or red", in);
1762                         return -1;
1763                 }
1764                 if (out_color == ACT_INVALID) {
1765                         set_errf("Invalid output color %s. Expected green, yellow or red", out);
1766                         return -1;
1767                 }
1768                 enum police_action action = str_to_color(pkey);
1769                 if (action == ACT_INVALID) {
1770                         set_errf("Error action %s. Expected green, yellow, red or drop", pkey);
1771                         return -1;
1772                 }
1773                 targ->police_act[in_color][out_color] = action;
1774
1775                 return 0;
1776         }
1777         if (STR_EQ(str, "qinq tag")) {
1778                 return parse_int(&targ->qinq_tag, pkey);
1779         }
1780         if (STR_EQ(str, "cir")) {
1781                 return parse_int(&targ->cir, pkey);
1782         }
1783         if (STR_EQ(str, "cbs")) {
1784                 return parse_int(&targ->cbs, pkey);
1785         }
1786         if (STR_EQ(str, "pir")) {
1787                 return parse_int(&targ->pir, pkey);
1788         }
1789         if (STR_EQ(str, "pbs")) {
1790                 return parse_int(&targ->pbs, pkey);
1791         }
1792         if (STR_EQ(str, "ebs")) {
1793                 return parse_int(&targ->ebs, pkey);
1794         }
1795         uint32_t queue_id = 0;
1796         if (sscanf(str, "queue %d weight", &queue_id) == 1) {
1797                 uint32_t val;
1798                 int err = parse_int(&val, pkey);
1799                 if (err) {
1800                         return -1;
1801                 }
1802                 if (queue_id >= RTE_SCHED_BE_QUEUES_PER_PIPE) {
1803                         set_errf("queue_id must be < %d", RTE_SCHED_BE_QUEUES_PER_PIPE);
1804                         return -1;
1805                 }
1806                 targ->qos_conf.pipe_params[0].wrr_weights[queue_id] = val;
1807                 return 0;
1808         }
1809         if (STR_EQ(str, "classify")) {
1810                 if (!(targ->task_init->flag_features & TASK_FEATURE_CLASSIFY)) {
1811                         set_errf("Classify is not supported in '%s' mode", targ->task_init->mode_str);
1812                         return -1;
1813                 }
1814
1815                 return parse_flag(&targ->runtime_flags, TASK_CLASSIFY, pkey);
1816         }
1817         if (STR_EQ(str, "flow table size")) {
1818                 return parse_int(&targ->flow_table_size, pkey);
1819         }
1820 #ifdef GRE_TP
1821         if (STR_EQ(str, "tbf rate")) {
1822                 return parse_int(&targ->tb_rate, pkey);
1823         }
1824         if (STR_EQ(str, "tbf size")) {
1825                 return parse_int(&targ->tb_size, pkey);
1826         }
1827 #endif
1828         if (STR_EQ(str, "max rules")) {
1829                 return parse_int(&targ->n_max_rules, pkey);
1830         }
1831
1832         if (STR_EQ(str, "tunnel hop limit")) {
1833                 uint32_t val;
1834                 int err = parse_int(&val, pkey);
1835                 if (err) {
1836                         return -1;
1837                 }
1838                 targ->tunnel_hop_limit = val;
1839                 return 0;
1840         }
1841
1842         if (STR_EQ(str, "lookup port mask")) {
1843                 uint32_t val;
1844                 int err = parse_int(&val, pkey);
1845                 if (err) {
1846                         return -1;
1847                 }
1848                 targ->lookup_port_mask = val;
1849                 return 0;
1850         }
1851
1852         if (STR_EQ(str, "irq debug")) {
1853                 parse_int(&targ->irq_debug, pkey);
1854                 return 0;
1855         }
1856
1857         set_errf("Option '%s' is not known", str);
1858         /* fail on unknown keys */
1859         return -1;
1860 }
1861
1862 static int str_is_number(const char *in)
1863 {
1864         int dot_once = 0;
1865
1866         for (size_t i = 0; i < strlen(in); ++i) {
1867                 if (!dot_once && in[i] == '.') {
1868                         dot_once = 1;
1869                         continue;
1870                 }
1871
1872                 if (in[i] < '0' || in[i] > '9')
1873                         return 0;
1874         }
1875
1876         return 1;
1877 }
1878
1879 /* command line parameters parsing procedure */
1880 int prox_parse_args(int argc, char **argv)
1881 {
1882         int i, opt, ret;
1883         char *tmp, *tmp2;
1884         char tmp3[64];
1885
1886         /* Default settings */
1887         prox_cfg.flags |= DSF_AUTOSTART | DSF_WAIT_ON_QUIT;
1888         prox_cfg.ui = PROX_UI_CURSES;
1889
1890         plog_info("\tCommand line:");
1891         for (i = 0; i < argc; ++i) {
1892                 plog_info(" %s", argv[i]);
1893         }
1894         plog_info("\n");
1895
1896         while ((opt = getopt(argc, argv, "f:dnzpo:tkuar:emsiw:l:v:q:")) != EOF) {
1897                 switch (opt) {
1898                 case 'f':
1899                         /* path to config file */
1900                         cfg_file = optarg;
1901                         size_t offset = 0;
1902                         for (size_t i = 0; i < strlen(cfg_file); ++i) {
1903                                 if (cfg_file[i] == '/') {
1904                                         offset = i + 1;
1905                                 }
1906                         }
1907
1908                         prox_strncpy(prox_cfg.name, cfg_file + offset, MAX_NAME_SIZE);
1909                         break;
1910                 case 'v':
1911                         plog_set_lvl(atoi(optarg));
1912                         break;
1913                 case 'l':
1914                         prox_cfg.log_name_pid = 0;
1915                         prox_strncpy(prox_cfg.log_name, optarg, MAX_NAME_SIZE);
1916                         break;
1917                 case 'p':
1918                         prox_cfg.log_name_pid = 1;
1919                         break;
1920                 case 'k':
1921                         prox_cfg.use_stats_logger = 1;
1922                         break;
1923                 case 'd':
1924                         prox_cfg.flags |= DSF_DAEMON;
1925                         prox_cfg.ui = PROX_UI_NONE;
1926                         break;
1927                 case 'z':
1928                         prox_cfg.flags |= DSF_USE_DUMMY_CPU_TOPO;
1929                         prox_cfg.flags |= DSF_CHECK_INIT;
1930                         break;
1931                 case 'n':
1932                         prox_cfg.flags |= DSF_USE_DUMMY_DEVICES;
1933                         break;
1934                 case 'r':
1935                         if (!str_is_number(optarg) || strlen(optarg) > 11)
1936                                 return -1;
1937                         prox_strncpy(prox_cfg.update_interval_str, optarg, sizeof(prox_cfg.update_interval_str));
1938                         break;
1939                 case 'o':
1940                         if (prox_cfg.flags & DSF_DAEMON)
1941                                 break;
1942
1943                         if (!strcmp(optarg, "curses")) {
1944                                 prox_cfg.ui = PROX_UI_CURSES;
1945                         }
1946                         else if (!strcmp(optarg, "cli")) {
1947                                 prox_cfg.ui = PROX_UI_CLI;
1948                         }
1949                         else if (!strcmp(optarg, "none")) {
1950                                 prox_cfg.ui = PROX_UI_NONE;
1951                         }
1952                         else {
1953                                 plog_err("Invalid local UI '%s', local UI can be 'curses', 'cli' or 'none'.", optarg);
1954                                 return -1;
1955                         }
1956                         break;
1957                 case 'q':
1958                         if (luaL_loadstring(prox_lua(), optarg)) {
1959                                 set_errf("Lua error: '%s'\n", lua_tostring(prox_lua(), -1));
1960                                 return -1;
1961                         }
1962
1963                         if (lua_pcall(prox_lua(), 0, LUA_MULTRET, 0)) {
1964                                 set_errf("Lua error: '%s'\n", lua_tostring(prox_lua(), -1));
1965                                 return -1;
1966                         }
1967
1968                         break;
1969                 case 'a':
1970                         /* autostart all cores */
1971                         prox_cfg.flags |= DSF_AUTOSTART;
1972                         break;
1973                 case 'e':
1974                         /* don't autostart */
1975                         prox_cfg.flags &= ~DSF_AUTOSTART;
1976                         break;
1977                 case 't':
1978                         prox_cfg.flags |= DSF_LISTEN_TCP;
1979                         break;
1980                 case 'u':
1981                         prox_cfg.flags |= DSF_LISTEN_UDS;
1982                         break;
1983                 case 'm':
1984                         /* list supported task modes and exit */
1985                         prox_cfg.flags |= DSF_LIST_TASK_MODES;
1986                         break;
1987                 case 's':
1988                         /* check configuration file syntax and exit */
1989                         prox_cfg.flags |= DSF_CHECK_SYNTAX;
1990                         break;
1991                 case 'i':
1992                         /* check initialization sequence and exit */
1993                         prox_cfg.flags |= DSF_CHECK_INIT;
1994                         break;
1995                 case 'w':
1996                         tmp = optarg;
1997                         tmp2 = 0;
1998                         if (strlen(tmp) >= 3 &&
1999                             (tmp2 = strchr(tmp, '='))) {
2000                                 *tmp2 = 0;
2001                                 tmp3[0] = '$';
2002                                 prox_strncpy(tmp3 + 1, tmp, 63);
2003                                 plog_info("\tAdding variable: %s = %s\n", tmp3, tmp2 + 1);
2004                                 ret = add_var(tmp3, tmp2 + 1, 1);
2005                                 if (ret == -2) {
2006                                         plog_err("\tFailed to add variable, too many variables defines\n");
2007                                         return -1;
2008                                 }
2009                                 else if(ret == -3) {
2010                                         plog_err("\tFailed to add variable, already defined\n");
2011                                         return -1;
2012                                 }
2013                                 break;
2014                         }
2015                         /* fall-through */
2016                 default:
2017                         plog_err("\tUnknown option\n");
2018                         return -1;
2019                 }
2020         }
2021
2022         /* reset getopt lib for DPDK */
2023         optind = 0;
2024
2025         return 0;
2026 }
2027
2028 static int check_cfg(void)
2029 {
2030         /* Sanity check */
2031 #define RETURN_IF(cond, err)                    \
2032         if (cond) {                             \
2033                 plog_err(err);                  \
2034                 return -1;                      \
2035         };
2036
2037         RETURN_IF(rte_cfg.force_nchannel == 0, "\tError: number of memory channels not specified in [eal options] section\n");
2038         RETURN_IF(prox_cfg.master >= RTE_MAX_LCORE, "\tError: No master core specified (one core needs to have mode=master)\n");
2039
2040 #undef RETURN_IF
2041
2042         return 0;
2043 }
2044
2045 static int calc_tot_rxrings(void)
2046 {
2047         struct lcore_cfg *slconf, *dlconf;
2048         struct task_args *starg, *dtarg;
2049         uint32_t dlcore_id;
2050         uint8_t dtask_id;
2051         struct core_task ct;
2052
2053         dlconf = NULL;
2054         while (core_targ_next_early(&dlconf, &dtarg, 1) == 0) {
2055                 dtarg->tot_rxrings = 0;
2056         }
2057
2058         slconf = NULL;
2059         while (core_targ_next_early(&slconf, &starg, 1) == 0) {
2060                 for (uint8_t idx = 0; idx < MAX_PROTOCOLS; ++idx) {
2061                         for (uint8_t ring_idx = 0; ring_idx < starg->core_task_set[idx].n_elems; ++ring_idx) {
2062                                 ct = starg->core_task_set[idx].core_task[ring_idx];
2063                                 if (!prox_core_active(ct.core, 0)) {
2064                                         set_errf("Core %u is disabled but Core %u task %u is sending to it\n",
2065                                                  ct.core, slconf->id, starg->id);
2066                                         return -1;
2067                                 }
2068
2069                                 dlconf = &lcore_cfg_init[ct.core];
2070
2071                                 if (ct.task >= dlconf->n_tasks_all) {
2072                                         set_errf("Core %u task %u not enabled\n", ct.core, ct.task);
2073                                         return -1;
2074                                 }
2075
2076                                 dtarg = &dlconf->targs[ct.task];
2077
2078                                 /* Control rings are not relevant at this point. */
2079                                 if (ct.type)
2080                                         continue;
2081
2082                                 if (!(dtarg->flags & TASK_ARG_RX_RING)) {
2083                                         set_errf("Core %u task %u is not expecting to receive through a ring\n",
2084                                                  ct.core, ct.task);
2085                                         return -1;
2086                                 }
2087
2088                                 dtarg->tot_rxrings++;
2089                                 if (dtarg->tot_rxrings > MAX_RINGS_PER_TASK) {
2090                                         set_errf("Core %u task %u is receiving from too many tasks",
2091                                                  ct.core, ct.task);
2092                                         return -1;
2093                                 }
2094                         }
2095                 }
2096         }
2097
2098         return 0;
2099 }
2100
2101 static void prox_set_core_mask(void)
2102 {
2103         struct lcore_cfg *lconf;
2104
2105         prox_core_clr();
2106         for (uint8_t lcore_id = 0; lcore_id < RTE_MAX_LCORE; ++lcore_id) {
2107                 lconf = &lcore_cfg_init[lcore_id];
2108                 if (lconf->n_tasks_all > 0 && lconf->targs[0].mode != MASTER) {
2109                         prox_core_set_active(lcore_id);
2110                 }
2111         }
2112 }
2113
2114 static int is_using_no_drop(void)
2115 {
2116         uint32_t lcore_id;
2117         struct lcore_cfg *lconf;
2118         struct task_args *targs;
2119
2120         lcore_id = -1;
2121         while(prox_core_next(&lcore_id, 1) == 0) {
2122                 lconf = &lcore_cfg_init[lcore_id];
2123                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
2124                         targs = &lconf->targs[task_id];
2125                         if (!(targs->flags & TASK_ARG_DROP))
2126                                 return 1;
2127                 }
2128         }
2129         return 0;
2130 }
2131
2132 int prox_read_config_file(void)
2133 {
2134         set_global_defaults(&prox_cfg);
2135         set_task_defaults(&prox_cfg, lcore_cfg_init);
2136         set_port_defaults();
2137         plog_info("=== Parsing configuration file '%s' ===\n", cfg_file);
2138         struct cfg_file *pcfg = cfg_open(cfg_file);
2139         if (pcfg == NULL) {
2140                 return -1;
2141         }
2142
2143         struct cfg_section* config_sections[] = {
2144                 &lua_cfg          ,
2145                 &var_cfg          ,
2146                 &eal_default_cfg  ,
2147                 &cache_set_cfg    ,
2148                 &port_cfg         ,
2149                 &defaults_cfg     ,
2150                 &settings_cfg     ,
2151                 &core_cfg         ,
2152                 NULL
2153         };
2154
2155         for (struct cfg_section** section = config_sections; *section != NULL; ++section) {
2156                 const char* name = (*section)->name;
2157                 size_t len = strlen(name);
2158                 plog_info("\t*** Reading [%s] section%s ***\n", name, name[len - 1] == '#'? "s": "");
2159                 cfg_parse(pcfg, *section);
2160
2161                 if ((*section)->error) {
2162                         plog_err("At line %u, section [%s], entry %u: '%s'\n\t%s\n"
2163                                  , pcfg->err_line, pcfg->err_section, pcfg->err_entry + 1, pcfg->cur_line,
2164                                  strlen(get_parse_err())? get_parse_err() : err_str);
2165                         cfg_close(pcfg); /* cannot close before printing error, print uses internal buffer */
2166                         return -1;
2167                 }
2168         }
2169
2170         cfg_close(pcfg);
2171
2172         prox_set_core_mask();
2173
2174         if (is_using_no_drop()) {
2175                 prox_cfg.flags &= ~DSF_WAIT_ON_QUIT;
2176         }
2177
2178         if (calc_tot_rxrings()) {
2179                 plog_err("Error in configuration: %s\n", err_str);
2180                 return -1;
2181         }
2182
2183         return check_cfg();
2184 }
2185
2186 static void failed_rte_eal_init(__attribute__((unused))const char *prog_name)
2187 {
2188         plog_err("\tError in rte_eal_init()\n");
2189 }
2190
2191 int prox_setup_rte(const char *prog_name)
2192 {
2193         char *rte_argv[MAX_RTE_ARGV];
2194         char  rte_arg[MAX_RTE_ARGV][MAX_ARG_LEN];
2195         char tmp[PROX_CM_STR_LEN];
2196         /* create mask of used cores */
2197         plog_info("=== Setting up RTE EAL ===\n");
2198
2199         if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO) {
2200                 plog_info("Using dummy cpu topology\n");
2201                 snprintf(tmp, sizeof(tmp), "0x1");
2202         } else {
2203                 prox_core_to_hex(tmp, sizeof(tmp), 0);
2204                 plog_info("\tWorker threads core mask is %s\n", tmp);
2205                 prox_core_to_hex(tmp, sizeof(tmp), 1);
2206                 plog_info("\tWith master core index %u, full core mask is %s\n", prox_cfg.master, tmp);
2207         }
2208
2209         /* fake command line parameters for rte_eal_init() */
2210         int argc = 0;
2211         rte_argv[argc] = strdup(prog_name);
2212         sprintf(rte_arg[++argc], "-c%s", tmp);
2213         rte_argv[argc] = rte_arg[argc];
2214 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
2215         if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO)
2216                 sprintf(rte_arg[++argc], "--master-lcore=%u", 0);
2217         else
2218                 sprintf(rte_arg[++argc], "--master-lcore=%u", prox_cfg.master);
2219         rte_argv[argc] = rte_arg[argc];
2220 #else
2221         /* For old DPDK versions, the master core had to be the first
2222            core. */
2223         uint32_t first_core = -1;
2224
2225         if (prox_core_next(&first_core, 1) == -1) {
2226                 plog_err("Can't core ID of first core in use\n");
2227                 return -1;
2228         }
2229         if (first_core != prox_cfg.master) {
2230                 plog_err("The master core needs to be the first core (master core = %u, first core = %u).\n", first_core, prox_cfg.master);
2231                 return -1;
2232         }
2233 #endif
2234
2235         if (rte_cfg.memory) {
2236                 sprintf(rte_arg[++argc], "-m%u", rte_cfg.memory);
2237                 rte_argv[argc] = rte_arg[argc];
2238         }
2239
2240         if (rte_cfg.force_nchannel) {
2241                 sprintf(rte_arg[++argc], "-n%u", rte_cfg.force_nchannel);
2242                 rte_argv[argc] = rte_arg[argc];
2243         }
2244
2245         if (rte_cfg.force_nrank) {
2246                 sprintf(rte_arg[++argc], "-r%u", rte_cfg.force_nrank);
2247                 rte_argv[argc] = rte_arg[argc];
2248         }
2249
2250         if (rte_cfg.no_hugetlbfs) {
2251                 strcpy(rte_arg[++argc], "--no-huge");
2252                 rte_argv[argc] = rte_arg[argc];
2253         }
2254
2255         if (rte_cfg.no_pci) {
2256                 strcpy(rte_arg[++argc], "--no-pci");
2257                 rte_argv[argc] = rte_arg[argc];
2258         }
2259
2260         if (rte_cfg.no_hpet) {
2261                 strcpy(rte_arg[++argc], "--no-hpet");
2262                 rte_argv[argc] = rte_arg[argc];
2263         }
2264
2265         if (rte_cfg.no_shconf) {
2266                 strcpy(rte_arg[++argc], "--no-shconf");
2267                 rte_argv[argc] = rte_arg[argc];
2268         }
2269
2270         if (rte_cfg.eal != NULL) {
2271                 char *ptr = rte_cfg.eal;
2272                 char *ptr2;
2273                 while (ptr != NULL) {
2274                         while (isspace(*ptr))
2275                                 ptr++;
2276                         ptr2 = ptr;
2277                         ptr = strchr(ptr, ' ');
2278                         if (ptr) {
2279                                 *ptr++ = '\0';
2280                         }
2281                         strcpy(rte_arg[++argc], ptr2);
2282                         rte_argv[argc] = rte_arg[argc];
2283                 }
2284         }
2285
2286         if (rte_cfg.hugedir != NULL) {
2287                 strcpy(rte_arg[++argc], "--huge-dir");
2288                 rte_argv[argc] = rte_arg[argc];
2289                 rte_argv[++argc] = rte_cfg.hugedir;
2290         }
2291
2292         if (rte_cfg.no_output) {
2293                 rte_log_set_global_level(0);
2294         }
2295         /* init EAL */
2296         plog_info("\tEAL command line:");
2297         if (argc >= MAX_RTE_ARGV) {
2298                 plog_err("too many arguments for EAL\n");
2299                 return -1;
2300         }
2301
2302         for (int h = 0; h <= argc; ++h) {
2303                 plog_info(" %s", rte_argv[h]);
2304         }
2305         plog_info("\n");
2306
2307         rte_set_application_usage_hook(failed_rte_eal_init);
2308         if (rte_eal_init(++argc, rte_argv) < 0) {
2309                 plog_err("\tError in rte_eal_init()\n");
2310                 return -1;
2311         }
2312         plog_info("\tEAL Initialized\n");
2313
2314         if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO)
2315                 return 0;
2316
2317         /* check if all active cores are in enabled in DPDK */
2318         for (uint32_t lcore_id = 0; lcore_id < RTE_MAX_LCORE; ++lcore_id) {
2319                 if (lcore_id == prox_cfg.master) {
2320                         if (!rte_lcore_is_enabled(lcore_id))
2321                                 return -1;
2322                 }
2323                 else if (rte_lcore_is_enabled(lcore_id) != prox_core_active(lcore_id, 0)) {
2324                         plog_err("\tFailed to enable lcore %u\n", lcore_id);
2325                         return -1;
2326                 }
2327                 else if (lcore_cfg_init[lcore_id].n_tasks_all != 0 && !rte_lcore_is_enabled(lcore_id)) {
2328                         plog_err("\tFailed to enable lcore %u\n", lcore_id);
2329                         return -1;
2330                 }
2331         }
2332         uint16_t port_id;
2333         for (int i = 0; i < n_deferred_ports; i++) {
2334                 if (prox_rte_eth_dev_get_port_by_name(deferred_port[i].name, &port_id) != 0) {
2335                         plog_err("Did not find port name %s used while reading %s\n", deferred_port[i].name, deferred_port[i].is_rx_port ? "rx port" : "tx_port");
2336                         return -1;
2337                 }
2338                 plog_info("\tport %s is port id %d\n", deferred_port[i].name, port_id);
2339                 if (deferred_port[i].is_rx_port) {
2340                         deferred_port[i].targ->rx_port_queue[0].port = port_id;
2341                         deferred_port[i].targ->nb_rxports = 1;
2342                 } else {
2343                         deferred_port[i].targ->tx_port_queue[0].port = port_id;
2344                         deferred_port[i].targ->nb_txports = 1;
2345                 }
2346         }
2347         return 0;
2348 }