Fix the search algorithm
[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  256
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, "virtual")) {
586                 uint32_t val;
587                 if (parse_bool(&val, pkey)) {
588                         return -1;
589                 }
590                 cfg->virtual = val;
591         }
592         else if (STR_EQ(str, "vdev")) {
593                 prox_strncpy(cfg->vdev, pkey, MAX_NAME_SIZE);
594         }
595 #if RTE_VERSION >= RTE_VERSION_NUM(18,8,0,1)
596         else if (STR_EQ(str, "disable tx offload")) {
597                 uint32_t val;
598                 if (parse_int(&val, pkey)) {
599                         return -1;
600                 }
601                 if (val)
602                         cfg->disabled_tx_offload = val;
603         }
604 #endif
605         else if (STR_EQ(str, "strip crc")) {
606                 uint32_t val;
607                 if (parse_bool(&val, pkey)) {
608                         return -1;
609                 }
610 #if defined(DEV_RX_OFFLOAD_CRC_STRIP)
611                 if (val)
612                         cfg->requested_rx_offload |= DEV_RX_OFFLOAD_CRC_STRIP;
613                 else
614                         cfg->requested_rx_offload &= ~DEV_RX_OFFLOAD_CRC_STRIP;
615 #else
616 #if defined (DEV_RX_OFFLOAD_KEEP_CRC)
617                 if (val)
618                         cfg->requested_rx_offload &= ~DEV_RX_OFFLOAD_KEEP_CRC;
619                 else
620 #endif
621                         cfg->requested_rx_offload |= DEV_RX_OFFLOAD_KEEP_CRC;
622 #endif
623
624         }
625         else if (STR_EQ(str, "vlan tag")) {
626                 return parse_int_set(cfg->vlan_tags, pkey, sizeof(cfg->vlan_tags) / sizeof(cfg->vlan_tags[0]));
627         }
628         else if (STR_EQ(str, "vlan")) {
629 #if RTE_VERSION >= RTE_VERSION_NUM(18,8,0,1)
630                 uint32_t val;
631                 if (parse_bool(&val, pkey)) {
632                         return -1;
633                 }
634                 if (val) {
635                         cfg->requested_rx_offload |= DEV_RX_OFFLOAD_VLAN_STRIP;
636                         cfg->requested_tx_offload |= DEV_TX_OFFLOAD_VLAN_INSERT;
637                 } else {
638                         cfg->requested_rx_offload &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
639                         cfg->requested_tx_offload &= ~DEV_TX_OFFLOAD_VLAN_INSERT;
640                 }
641 #else
642                 plog_warn("vlan option not supported : update DPDK at least to 18.08 to support this option\n");
643 #endif
644         }
645         else if (STR_EQ(str, "mtu size")) {
646                 uint32_t val;
647                 if (parse_int(&val, pkey)) {
648                         return -1;
649                 }
650                 if (val) {
651                         cfg->mtu = val;
652                         // A frame of 1526 bytes (1500 bytes mtu, 14 bytes hdr, 4 bytes crc and 8 bytes vlan)
653                         // should not be considered as a jumbo frame. However rte_ethdev.c considers that
654                         // the max_rx_pkt_len for a non jumbo frame is 1518
655                         cfg->port_conf.rxmode.max_rx_pkt_len = cfg->mtu + PROX_RTE_ETHER_HDR_LEN + PROX_RTE_ETHER_CRC_LEN;
656                         if (cfg->port_conf.rxmode.max_rx_pkt_len > PROX_RTE_ETHER_MAX_LEN) {
657                                 cfg->requested_rx_offload |= DEV_RX_OFFLOAD_JUMBO_FRAME;
658                         }
659                 }
660         }
661
662         else if (STR_EQ(str, "rss")) {
663                 uint32_t val;
664                 if (parse_bool(&val, pkey)) {
665                         return -1;
666                 }
667                 if (val) {
668                         cfg->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
669                         cfg->port_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IPV4;
670                 }
671         }
672         else if (STR_EQ(str, "rx_ring")) {
673                 parse_str(cfg->rx_ring, pkey, sizeof(cfg->rx_ring));
674         }
675         else if (STR_EQ(str, "tx_ring")) {
676                 parse_str(cfg->tx_ring, pkey, sizeof(cfg->tx_ring));
677         }
678
679         return 0;
680 }
681
682 static enum police_action str_to_color(const char *str)
683 {
684         if (STR_EQ(str, "green"))
685                 return ACT_GREEN;
686         if (STR_EQ(str, "yellow"))
687                 return ACT_YELLOW;
688         if (STR_EQ(str, "red"))
689                 return ACT_RED;
690         if (STR_EQ(str, "drop"))
691                 return ACT_DROP;
692         return ACT_INVALID;
693 }
694
695 struct cfg_depr task_cfg_depr[] = {
696         {"sig", ""},
697 };
698
699 struct cfg_depr core_cfg_depr[] = {
700         {"do sig", ""},
701         {"lat", ""},
702         {"network side", ""},
703 };
704
705 /* [core] parser */
706 static int get_core_cfg(unsigned sindex, char *str, void *data)
707 {
708         char *pkey;
709         struct lcore_cfg *lconf = (struct lcore_cfg *)data;
710
711         if (str == NULL || lconf == NULL || !(sindex & CFG_INDEXED)) {
712                 return -1;
713         }
714
715         pkey = get_cfg_key(str);
716         if (pkey == NULL) {
717                 set_errf("Missing key after option");
718                 return -1;
719         }
720
721         uint32_t ncore = sindex & ~CFG_INDEXED;
722         if (ncore >= RTE_MAX_LCORE) {
723                 set_errf("Core index too high (max allowed %d)", RTE_MAX_LCORE - 1);
724                 return -1;
725         }
726
727         lconf = &lconf[ncore];
728
729         for (uint32_t i = 0; i < RTE_DIM(core_cfg_depr); ++i) {
730                 if (STR_EQ(str, core_cfg_depr[i].opt)) {
731                         set_errf("Option '%s' is deprecated%s%s",
732                                  core_cfg_depr[i].opt, strlen(core_cfg_depr[i].info)? ": ": "", core_cfg_depr[i].info);
733                         return -1;
734                 }
735         }
736
737         char buff[128];
738         lcore_to_socket_core_ht(ncore, buff, sizeof(buff));
739         set_self_var(buff);
740         if (STR_EQ(str, "task")) {
741
742                 uint32_t val;
743                 if (parse_int(&val, pkey)) {
744                         return -1;
745                 }
746                 if (val >= MAX_TASKS_PER_CORE) {
747                         set_errf("Too many tasks for core (max allowed %d)", MAX_TASKS_PER_CORE - 1);
748                         return -1;
749                 }
750                 if (val != lconf->n_tasks_all) {
751                         set_errf("Task ID skipped or defined twice");
752                         return -1;
753                 }
754
755                 lconf->active_task = val;
756
757                 lconf->targs[lconf->active_task].task = lconf->active_task;
758
759                 if (lconf->n_tasks_all < lconf->active_task + 1) {
760                         lconf->n_tasks_all = lconf->active_task + 1;
761                 }
762                 return 0;
763         }
764
765         struct task_args *targ = &lconf->targs[lconf->active_task];
766         if (STR_EQ(str, "tx ports from routing table")) {
767                 uint32_t vals[PROX_MAX_PORTS];
768                 uint32_t n_if;
769                 if (!(targ->task_init->flag_features & TASK_FEATURE_ROUTING)) {
770                         set_errf("tx port form route not supported mode %s",  targ->task_init->mode_str);
771                         return -1;
772                 }
773
774                 if (parse_port_name_list(vals, &n_if, PROX_MAX_PORTS, pkey)) {
775                         return -1;
776                 }
777
778                 for (uint8_t i = 0; i < n_if; ++i) {
779                         targ->tx_port_queue[i].port = vals[i];
780                         targ->nb_txports++;
781                 }
782                 targ->runtime_flags |= TASK_ROUTING;
783                 return 0;
784         }
785         if (STR_EQ(str, "tx ports from cpe table")) {
786                 uint32_t vals[PROX_MAX_PORTS];
787                 int n_remap = -1;
788                 uint32_t ret;
789                 uint32_t val;
790                 char* mapping_str = strstr(pkey, " remap=");
791
792                 if (mapping_str != NULL) {
793                         *mapping_str = 0;
794                         mapping_str += strlen(" remap=");
795                         n_remap = parse_remap(targ->mapping, mapping_str);
796                 }
797
798                 if (parse_port_name_list(vals, &ret, PROX_MAX_PORTS, pkey)) {
799                         return -1;
800                 }
801
802                 if (n_remap != -1 && ret != (uint32_t)n_remap) {
803                         set_errf("Expected %d remap elements but had %d", n_remap, ret);
804                         return -1;
805                 }
806
807                 for (uint8_t i = 0; i < ret; ++i) {
808                         targ->tx_port_queue[i].port = vals[i];
809
810                         /* default mapping this case is port0 -> port0 */
811                         if (n_remap == -1) {
812                                 targ->mapping[vals[i]] = i;
813                         }
814                 }
815
816                 targ->nb_txports = ret;
817
818                 return 0;
819         }
820         if (STR_EQ(str, "tx cores from routing table")) {
821                 if (!(targ->task_init->flag_features & TASK_FEATURE_ROUTING)) {
822                         set_errf("tx port form route not supported mode %s",  targ->task_init->mode_str);
823                         return -1;
824                 }
825
826                 struct core_task_set *cts = &targ->core_task_set[0];
827
828                 if (parse_task_set(cts, pkey))
829                         return -1;
830
831                 if (cts->n_elems > MAX_WT_PER_LB) {
832                         set_errf("Maximum worker threads allowed is %u but have %u", MAX_WT_PER_LB, cts->n_elems);
833                         return -1;
834                 }
835
836                 targ->nb_worker_threads = cts->n_elems;
837                 targ->nb_txrings = cts->n_elems;
838
839                 if (targ->nb_txrings > MAX_RINGS_PER_TASK) {
840                         set_errf("Maximum allowed TX rings is %u but have %u", MAX_RINGS_PER_TASK, targ->nb_txrings);
841                         return -1;
842                 }
843
844                 targ->runtime_flags |= TASK_ROUTING;
845                 return 0;
846         }
847         if (STR_EQ(str, "tx cores from cpe table")) {
848                 struct core_task_set *core_task_set =  &targ->core_task_set[0];
849                 int ret, ret2;
850                 char *mapping_str;
851
852                 mapping_str = strstr(pkey, " remap=");
853                 if (mapping_str == NULL) {
854                         set_errf("There is no default mapping for tx cores from cpe table. Please specify it through remap=");
855                         return -1;
856                 }
857                 *mapping_str = 0;
858                 mapping_str += strlen(" remap=");
859                 ret = parse_remap(targ->mapping, mapping_str);
860                 if (ret <= 0) {
861                         return -1;
862                 }
863
864                 struct core_task_set *cts = &targ->core_task_set[0];
865
866                 if (parse_task_set(cts, pkey))
867                         return -1;
868                 if (cts->n_elems > MAX_RINGS_PER_TASK) {
869                         set_errf("Maximum cores to route to is %u\n", MAX_RINGS_PER_TASK);
870                         return -1;
871                 }
872
873                 targ->nb_txrings = cts->n_elems;
874
875                 if (ret != targ->nb_txrings) {
876                         set_errf("Expecting same number of remaps as cores\n", str);
877                         return -1;
878                 }
879                 return 0;
880         }
881
882         if (STR_EQ(str, "delay ms")) {
883                 if (targ->delay_us) {
884                         set_errf("delay ms and delay us are mutually exclusive\n", str);
885                         return -1;
886                 }
887                 uint32_t delay_ms;
888                 int rc = parse_int(&delay_ms, pkey);
889                 targ->delay_us = delay_ms * 1000;
890                 return rc;
891         }
892         if (STR_EQ(str, "delay us")) {
893                 if (targ->delay_us) {
894                         set_errf("delay ms and delay us are mutually exclusive\n", str);
895                         return -1;
896                 }
897                 return parse_int(&targ->delay_us, pkey);
898         }
899         if (STR_EQ(str, "random delay us")) {
900                 return parse_int(&targ->random_delay_us, pkey);
901         }
902         if (STR_EQ(str, "cpe table timeout ms")) {
903                 return parse_int(&targ->cpe_table_timeout_ms, pkey);
904         }
905         if (STR_EQ(str, "ctrl path polling frequency")) {
906                 int rc = parse_int(&targ->ctrl_freq, pkey);
907                 if (rc == 0) {
908                         if (targ->ctrl_freq == 0) {
909                                 set_errf("ctrl frequency must be non null.");
910                                 return -1;
911                         }
912                 }
913                 return rc;
914         }
915
916         if (STR_EQ(str, "handle arp")) {
917                 return parse_flag(&targ->runtime_flags, TASK_CTRL_HANDLE_ARP, pkey);
918         }
919         if (STR_EQ(str, "fast path handle arp")) {
920                 return parse_flag(&targ->runtime_flags, TASK_FP_HANDLE_ARP, pkey);
921         }
922
923         if (STR_EQ(str, "do not forward geneve")) {
924                 return parse_flag(&targ->runtime_flags, TASK_DO_NOT_FWD_GENEVE, pkey);
925         }
926         /* Using tx port name, only a _single_ port can be assigned to a task. */
927         if (STR_EQ(str, "tx port")) {
928                 if (targ->nb_txports > 0) {
929                         set_errf("Only one tx port can be defined per task. Use a LB task or routing instead.");
930                         return -1;
931                 }
932
933                 uint32_t n_if = 0;
934                 uint32_t ports[PROX_MAX_PORTS];
935
936                 if(parse_port_name_list(ports, &n_if, PROX_MAX_PORTS, pkey)) {
937                         // Port name not found, but could be a virtual device of a secondary process
938                         // As DPDK not started yet, we can only check the config file to see whether we are a secondary process
939                         if (rte_cfg.eal &&
940                                         (strstr(rte_cfg.eal, "secondary") || strstr(rte_cfg.eal, "auto")) &&
941                                         (n_deferred_ports < PROX_MAX_PORTS)) {
942                                 prox_strncpy(deferred_port[n_deferred_ports].name, pkey, sizeof(deferred_port[n_deferred_ports].name));
943                                 deferred_port[n_deferred_ports].is_rx_port = 0;
944                                 deferred_port[n_deferred_ports++].targ = targ;
945                                 return 0;
946                         } else
947                                 return -1;
948                 }
949
950                 PROX_ASSERT(n_if-1 < PROX_MAX_PORTS);
951
952                 for (uint8_t i = 0; i < n_if; ++i) {
953                         targ->tx_port_queue[i].port = ports[i];
954                         targ->nb_txports++;
955                 }
956
957                 if (n_if > 1) {
958                         targ->nb_worker_threads = targ->nb_txports;
959                 }
960
961                 return 0;
962         }
963         if (STR_EQ(str, "rx ring")) {
964                 uint32_t val;
965                 int err = parse_bool(&val, pkey);
966                 if (!err && val && targ->rx_port_queue[0].port != OUT_DISCARD) {
967                         set_errf("Can't read both from internal ring and external port from the same task. Use multiple tasks instead.");
968                         return -1;
969                 }
970
971                 return parse_flag(&targ->flags, TASK_ARG_RX_RING, pkey);
972         }
973         if (STR_EQ(str, "private")) {
974                 return parse_bool(&targ->use_src, pkey);
975         }
976         if (STR_EQ(str, "use src ip")) {
977                 return parse_bool(&targ->use_src, pkey);
978         }
979         if (STR_EQ(str, "nat table")) {
980                 return parse_str(targ->nat_table, pkey, sizeof(targ->nat_table));
981         }
982         if (STR_EQ(str, "rules")) {
983                 return parse_str(targ->rules, pkey, sizeof(targ->rules));
984         }
985         if (STR_EQ(str, "route table")) {
986                 return parse_str(targ->route_table, pkey, sizeof(targ->route_table));
987         }
988         if (STR_EQ(str, "dscp")) {
989                 return parse_str(targ->dscp, pkey, sizeof(targ->dscp));
990         }
991         if (STR_EQ(str, "tun_bindings")) {
992                 return parse_str(targ->tun_bindings, pkey, sizeof(targ->tun_bindings));
993         }
994         if (STR_EQ(str, "cpe table")) {
995                 return parse_str(targ->cpe_table_name, pkey, sizeof(targ->cpe_table_name));
996         }
997         if (STR_EQ(str, "user table")) {
998                 return parse_str(targ->user_table, pkey, sizeof(targ->user_table));
999         }
1000         if (STR_EQ(str, "streams")) {
1001                 return parse_str(targ->streams, pkey, sizeof(targ->streams));
1002         }
1003         if (STR_EQ(str, "Unsollicited NA")) {
1004                 return parse_flag(&targ->flags, TASK_ARG_SEND_NA_AT_STARTUP, pkey);
1005         }
1006         if (STR_EQ(str, "local lpm")) {
1007                 return parse_flag(&targ->flags, TASK_ARG_LOCAL_LPM, pkey);
1008         }
1009         if (STR_EQ(str, "drop")) {
1010                 return parse_flag(&targ->flags, TASK_ARG_DROP, pkey);
1011         }
1012         if (STR_EQ(str, "loop")) {
1013                 parse_flag(&targ->loop, 1, pkey);
1014                 return parse_flag(&targ->loop, 1, pkey);
1015         }
1016         if (STR_EQ(str, "qinq")) {
1017                 return parse_flag(&targ->flags, TASK_ARG_QINQ_ACL, pkey);
1018         }
1019         if (STR_EQ(str, "bps")) {
1020                 return parse_u64(&targ->rate_bps, pkey);
1021         }
1022         if (STR_EQ(str, "random")) {
1023                 return parse_str(targ->rand_str[targ->n_rand_str++], pkey, sizeof(targ->rand_str[0]));
1024         }
1025         if (STR_EQ(str, "range")) {
1026                 int rc = parse_range(&targ->range[targ->n_ranges].min, &targ->range[targ->n_ranges].max, pkey);
1027                 targ->n_ranges++;
1028                 return rc;
1029         }
1030         if (STR_EQ(str, "range_offset")) {
1031                 if (targ->n_ranges == 0) {
1032                         set_errf("No range defined previously (use range=...)");
1033                         return -1;
1034                 }
1035                 return parse_int(&targ->range[targ->n_ranges - 1].offset, pkey);
1036         }
1037         if (STR_EQ(str, "rand_offset")) {
1038                 if (targ->n_rand_str == 0) {
1039                         set_errf("No random defined previously (use random=...)");
1040                         return -1;
1041                 }
1042
1043                 return parse_int(&targ->rand_offset[targ->n_rand_str - 1], pkey);
1044         }
1045         if (STR_EQ(str, "keep src mac")) {
1046                 return parse_flag(&targ->flags, DSF_KEEP_SRC_MAC, pkey);
1047         }
1048         if (STR_EQ(str, "pcap file")) {
1049                 return parse_str(targ->pcap_file, pkey, sizeof(targ->pcap_file));
1050         }
1051         if (STR_EQ(str, "imix")) {
1052                 char pkey2[MAX_CFG_STRING_LEN], *ptr;
1053                 if (parse_str(pkey2, pkey, sizeof(pkey2)) != 0) {
1054                         set_errf("Error while parsing imix, too long\n");
1055                         return -1;
1056                 }
1057                 const size_t pkey_len = strlen(pkey2);
1058                 targ->imix_nb_pkts = 0;
1059                 ptr = pkey2;
1060                 while (targ->imix_nb_pkts < MAX_IMIX_PKTS) {
1061                         if (parse_int(&targ->imix_pkt_sizes[targ->imix_nb_pkts], ptr) != 0)
1062                                 break;
1063                         targ->imix_nb_pkts++;
1064                         if ((ptr = strchr(ptr, ',')) == NULL)
1065                                 break;
1066                         ptr++;
1067                         if (targ->imix_nb_pkts == MAX_IMIX_PKTS) {
1068                                 set_errf("Too many packet sizes specified");
1069                                 return -1;
1070                         }
1071                 }
1072                 plog_info("%d IMIX packets:", targ->imix_nb_pkts);
1073                 for (size_t i = 0; i < targ->imix_nb_pkts; ++i) {
1074                         plog_info("%d ", targ->imix_pkt_sizes[i]);
1075                 }
1076                 plog_info("\n");
1077                 return 0;
1078         }
1079         if (STR_EQ(str, "pkt inline")) {
1080                 char pkey2[MAX_CFG_STRING_LEN];
1081                 if (parse_str(pkey2, pkey, sizeof(pkey2)) != 0) {
1082                         set_errf("Error while parsing pkt line, too long\n");
1083                         return -1;
1084                 }
1085
1086                 const size_t pkey_len = strlen(pkey2);
1087                 targ->pkt_size = 0;
1088
1089                 for (size_t i = 0; i < pkey_len; ++i) {
1090                         if (pkey2[i] == ' ')
1091                                 continue;
1092
1093                         if (i + 1 == pkey_len) {
1094                                 set_errf("Incomplete byte at character %z", i);
1095                                 return -1;
1096                         }
1097
1098                         uint8_t byte = 0;
1099
1100                         if (pkey2[i] >= '0' && pkey2[i] <= '9') {
1101                                 byte = (pkey2[i] - '0') << 4;
1102                         }
1103                         else if (pkey2[i] >= 'a' && pkey2[i] <= 'f') {
1104                                 byte = (pkey2[i] - 'a' + 10) << 4;
1105                         }
1106                         else if (pkey2[i] >= 'A' && pkey2[i] <= 'F') {
1107                                 byte = (pkey2[i] - 'A' + 10) << 4;
1108                         }
1109                         else {
1110                                 set_errf("Invalid character in pkt inline at byte %d (%c)", i, pkey2[i]);
1111                                 return -1;
1112                         }
1113
1114                         if (pkey2[i + 1] >= '0' && pkey2[i + 1] <= '9') {
1115                                 byte |= (pkey2[i + 1] - '0');
1116                         }
1117                         else if (pkey2[i + 1] >= 'a' && pkey2[i + 1] <= 'f') {
1118                                 byte |= (pkey2[i + 1] - 'a' + 10);
1119                         }
1120                         else if (pkey2[i + 1] >= 'A' && pkey2[i + 1] <= 'F') {
1121                                 byte |= (pkey2[i + 1] - 'A' + 10);
1122                         }
1123                         else {
1124                                 set_errf("Invalid character in pkt inline at byte %d (%c)", i, pkey2[i + 1]);
1125                                 return -1;
1126                         }
1127                         if (targ->pkt_size == sizeof(targ->pkt_inline)) {
1128                                 set_errf("Inline packet definition can't be longer than %u", sizeof(targ->pkt_inline));
1129                                 return -1;
1130                         }
1131
1132                         targ->pkt_inline[targ->pkt_size++] = byte;
1133                         i += 1;
1134                 }
1135
1136                 return 0;
1137         }
1138         if (STR_EQ(str, "accuracy limit nsec")) {
1139                 return parse_int(&targ->accuracy_limit_nsec, pkey);
1140         }
1141         if (STR_EQ(str, "latency bucket size")) {
1142                 return parse_int(&targ->bucket_size, pkey);
1143         }
1144         if (STR_EQ(str, "latency buffer size")) {
1145                 return parse_int(&targ->latency_buffer_size, pkey);
1146         }
1147         if (STR_EQ(str, "loss buffer size")) {
1148                 return parse_int(&targ->loss_buffer_size, pkey);
1149         }
1150         if (STR_EQ(str, "accuracy pos")) {
1151                 return parse_int(&targ->accur_pos, pkey);
1152         }
1153         if (STR_EQ(str, "signature")) {
1154                 return parse_int(&targ->sig, pkey);
1155         }
1156         if (STR_EQ(str, "signature pos")) {
1157                 return parse_int(&targ->sig_pos, pkey);
1158         }
1159         if (STR_EQ(str, "lat pos")) {
1160                 targ->lat_enabled = 1;
1161                 return parse_int(&targ->lat_pos, pkey);
1162         }
1163         if (STR_EQ(str, "packet id pos")) {
1164                 return parse_int(&targ->packet_id_pos, pkey);
1165         }
1166         if (STR_EQ(str, "flow id pos")) {
1167                 return parse_int(&targ->flow_id_pos, pkey);
1168         }
1169         if (STR_EQ(str, "packet id in flow pos")) {
1170                 return parse_int(&targ->packet_id_in_flow_pos, pkey);
1171         }
1172         if (STR_EQ(str, "flow count")) {
1173                 return parse_int(&targ->flow_count, pkey);
1174         }
1175         if (STR_EQ(str, "probability")) { // old - use "probability no drop" instead
1176                 float probability;
1177                 int rc = parse_float(&probability, pkey);
1178                 if (probability == 0) {
1179                         set_errf("Probability must be != 0\n");
1180                         return -1;
1181                 } else if (probability > 100.0) {
1182                         set_errf("Probability must be < 100\n");
1183                         return -1;
1184                 }
1185                 targ->probability_no_drop = probability * 10000;
1186                 return rc;
1187         }
1188         if (STR_EQ(str, "proba no drop")) {
1189                 float probability;
1190                 int rc = parse_float(&probability, pkey);
1191                 if (probability == 0) {
1192                         set_errf("probability no drop must be != 0\n");
1193                         return -1;
1194                 } else if (probability > 100.0) {
1195                         set_errf("Probability must be < 100\n");
1196                         return -1;
1197                 }
1198                 targ->probability_no_drop = probability * 10000;
1199                 return rc;
1200         }
1201         if (STR_EQ(str, "proba delay")) {
1202                 float probability;
1203                 int rc = parse_float(&probability, pkey);
1204                 if (probability > 100.0) {
1205                         set_errf("Probability must be < 100\n");
1206                         return -1;
1207                 }
1208                 targ->probability_delay = probability * 10000;
1209                 return rc;
1210         }
1211 #if RTE_VERSION >= RTE_VERSION_NUM(19,11,0,0)
1212         if (STR_EQ(str, "proba duplicate")) {
1213                 float probability;
1214                 int rc = parse_float(&probability, pkey);
1215                 if (probability > 100.0) {
1216                         set_errf("probability duplicate must be < 100\n");
1217                         return -1;
1218                 }
1219                 targ->probability_duplicate = probability * 10000;
1220                 return rc;
1221         }
1222 #endif
1223         if (STR_EQ(str, "concur conn")) {
1224                 return parse_int(&targ->n_concur_conn, pkey);
1225         }
1226         if (STR_EQ(str, "max setup rate")) {
1227                 return parse_int(&targ->max_setup_rate, pkey);
1228         }
1229         if (STR_EQ(str, "pkt size")) {
1230                 return parse_int(&targ->pkt_size, pkey);
1231         }
1232         if (STR_EQ(str, "min bulk size")) {
1233                 return parse_int(&targ->min_bulk_size, pkey);
1234         }
1235         if (STR_EQ(str, "max bulk size")) {
1236                 return parse_int(&targ->max_bulk_size, pkey);
1237         }
1238         if (STR_EQ(str, "rx port")) {
1239                 if (targ->flags & TASK_ARG_RX_RING) {
1240                         set_errf("Can't read both from internal ring and external port from the same task. Use multiple tasks instead.");
1241                         return -1;
1242                 }
1243                 uint32_t vals[PROX_MAX_PORTS];
1244                 uint32_t n_if;
1245
1246                 if (parse_port_name_list(vals, &n_if, PROX_MAX_PORTS, pkey)) {
1247                         // Port name not found, but could be a virtual device of a secondary process
1248                         // As DPDK not started yet, we can only check the config file to see whether we are a secondary process
1249                         if (rte_cfg.eal &&
1250                                         (strstr(rte_cfg.eal, "secondary") || strstr(rte_cfg.eal, "auto")) &&
1251                                         (n_deferred_ports < PROX_MAX_PORTS)) {
1252                                 prox_strncpy(deferred_port[n_deferred_ports].name, pkey, sizeof(deferred_port[n_deferred_ports].name));
1253                                 deferred_port[n_deferred_ports].is_rx_port = 1;
1254                                 deferred_port[n_deferred_ports++].targ = targ;
1255                                 return 0;
1256                         } else
1257                                 return -1;
1258                 }
1259
1260                 for (uint8_t i = 0; i < n_if; ++i) {
1261                         PROX_ASSERT(vals[i] < PROX_MAX_PORTS);
1262                         targ->rx_port_queue[i].port = vals[i];
1263                         targ->nb_rxports++;
1264                 }
1265                 return 0;
1266         }
1267
1268         if (STR_EQ(str, "mode")) {
1269                 /* Check deprecated task modes */
1270                 char mode[255];
1271                 int ret = parse_str(mode, pkey, sizeof(mode));
1272                 if (ret)
1273                         return ret;
1274
1275                 for (uint32_t i = 0; i < RTE_DIM(task_cfg_depr); ++i) {
1276                         if (STR_EQ(mode, task_cfg_depr[i].opt)) {
1277                                 set_errf("Task mode '%s' is deprecated%s%s",
1278                                          task_cfg_depr[i].opt, strlen(task_cfg_depr[i].info)? ": ": "", task_cfg_depr[i].info);
1279                                 return -1;
1280                         }
1281                 }
1282
1283                 /* master is a special mode that is always needed (cannot be turned off) */
1284                 if (STR_EQ(mode, "master")) {
1285                         prox_cfg.master = ncore;
1286                         targ->mode = MASTER;
1287                         if (lconf->n_tasks_all > 1 || targ->task != 0) {
1288                                 set_errf("Master core can only have one task\n");
1289                                 return -1;
1290                         }
1291                         // Initialize number of tasks to 1 for master, even if no task specified
1292                         lconf->n_tasks_all = 1;
1293                         lconf->active_task = 0;
1294                         lconf->targs[lconf->active_task].task = 0;
1295                         struct task_init* task_init = to_task_init(mode, "");
1296                         if (task_init) {
1297                                 targ->mode = task_init->mode;
1298                         }
1299                         targ->task_init = task_init;
1300                         return 0;
1301                 }
1302
1303                 struct task_init* task_init = to_task_init(mode, "");
1304                 if (task_init) {
1305                         targ->mode = task_init->mode;
1306                 }
1307                 else {
1308                         set_errf("Task mode '%s' is invalid", mode);
1309                         tasks_list();
1310                         return -1;
1311                 }
1312                 targ->task_init = task_init;
1313                 return 0;
1314         }
1315         if (STR_EQ(str, "users")) {
1316                 return parse_int(&targ->n_flows, pkey);
1317         }
1318
1319         if (STR_EQ(str, "mark")) {
1320                 return parse_flag(&targ->runtime_flags, TASK_MARK, pkey);
1321         }
1322
1323         if (STR_EQ(str, "mark green")) {
1324                 return parse_int(&targ->marking[0], pkey);
1325         }
1326
1327         if (STR_EQ(str, "mark yellow")) {
1328                 return parse_int(&targ->marking[1], pkey);
1329         }
1330
1331         if (STR_EQ(str, "mark red")) {
1332                 return parse_int(&targ->marking[2], pkey);
1333         }
1334
1335         if (STR_EQ(str, "tx cores")) {
1336                 uint8_t dest_task = 0;
1337                 /* if user did not specify, dest_port is left at default (first type) */
1338                 uint8_t dest_proto = 0;
1339                 uint8_t ctrl = CTRL_TYPE_DP;
1340                 char *task_str = strstr(pkey, "proto=");
1341                 if (task_str) {
1342                         task_str += strlen("proto=");
1343
1344                         if (STR_EQ(task_str, "ipv4")) {
1345                                 dest_proto = IPV4;
1346                         }
1347                         else if (STR_EQ(task_str, "arp")) {
1348                                 dest_proto = ARP;
1349                         }
1350                         else if (STR_EQ(task_str, "ipv6")) {
1351                                 dest_proto = IPV6;
1352                         }
1353                         else {
1354                                 set_errf("proto needs to be either ipv4, arp or ipv6");
1355                                 return -1;
1356                         }
1357
1358                 }
1359
1360                 task_str = strstr(pkey, "task=");
1361
1362                 if (task_str) {
1363                         --task_str;
1364                         *task_str = 0;
1365                         task_str++;
1366                         task_str += strlen("task=");
1367                         char *task_str_end = strstr(task_str, " ");
1368                         if (task_str_end) {
1369                                 *task_str_end = 0;
1370                         }
1371                         if (0 == strlen(task_str)) {
1372                                 set_errf("Invalid task= syntax");
1373                                 return -1;
1374                         }
1375
1376                         switch (task_str[strlen(task_str) - 1]) {
1377                         case 'p':
1378                                 ctrl = CTRL_TYPE_PKT;
1379                                 break;
1380                         case 'm':
1381                                 ctrl = CTRL_TYPE_MSG;
1382                                 break;
1383                         case '\n':
1384                         case 0:
1385                                 break;
1386                         default:
1387                                 if (task_str[strlen(task_str) -1] < '0' ||
1388                                     task_str[strlen(task_str) -1] > '9') {
1389                                         set_errf("Unknown ring type %c.\n",
1390                                                  task_str[strlen(task_str) - 1]);
1391                                         return -1;
1392                                 }
1393                         }
1394
1395                         dest_task = atoi(task_str);
1396                         if (dest_task >= MAX_TASKS_PER_CORE) {
1397                                 set_errf("Destination task too high (max allowed %d)", MAX_TASKS_PER_CORE - 1);
1398                                 return -1;
1399                         }
1400                 }
1401                 else {
1402                         dest_task = 0;
1403                 }
1404
1405                 struct core_task_set *cts = &targ->core_task_set[dest_proto];
1406
1407                 if (parse_task_set(cts, pkey))
1408                         return -1;
1409
1410                 if (cts->n_elems > MAX_WT_PER_LB) {
1411                         set_errf("Too many worker threads (max allowed %d)", MAX_WT_PER_LB - 1);
1412                         return -1;
1413                 }
1414
1415                 targ->nb_worker_threads = cts->n_elems;
1416                 targ->nb_txrings += cts->n_elems;
1417
1418                 return 0;
1419         }
1420         if (STR_EQ(str, "tx crc")) {
1421                 return parse_flag(&targ->runtime_flags, TASK_TX_CRC, pkey);
1422         }
1423         if (STR_EQ(str, "ring size")) {
1424                 return parse_int(&targ->ring_size, pkey);
1425         }
1426         if (STR_EQ(str, "mempool size")) {
1427                 return parse_kmg(&targ->nb_mbuf, pkey);
1428         }
1429
1430         else if (STR_EQ(str, "mbuf size")) {
1431                 return parse_int(&targ->mbuf_size, pkey);
1432         }
1433         if (STR_EQ(str, "memcache size")) {
1434                 return parse_kmg(&targ->nb_cache_mbuf, pkey);
1435         }
1436
1437         if (STR_EQ(str, "byte offset")) {
1438                 return parse_int(&targ->byte_offset, pkey);
1439         }
1440
1441         if (STR_EQ(str, "realtime scheduling")) {
1442                 return parse_flag(&lconf->flags, LCONF_FLAG_SCHED_RR, pkey);
1443         }
1444         if (STR_EQ(str, "name")) {
1445                 return parse_str(lconf->name, pkey, sizeof(lconf->name));
1446         }
1447         /* MPLS configuration */
1448         if (STR_EQ(str, "untag mpls")) {
1449                 return parse_flag(&targ->runtime_flags, TASK_MPLS_TAGGING, pkey);
1450         }
1451
1452         if (STR_EQ(str, "add mpls")) {
1453                 return parse_flag(&targ->runtime_flags, TASK_MPLS_TAGGING, pkey);
1454         }
1455
1456         if (STR_EQ(str, "ether type")) {
1457                 return parse_int(&targ->etype, pkey);
1458         }
1459
1460         if (STR_EQ(str, "cache set")) {
1461                 return parse_int(&lconf->cache_set, pkey);
1462         }
1463
1464         if (STR_EQ(str, "sub mode")) {
1465                 const char* mode_str = targ->task_init->mode_str;
1466                 const char *sub_mode_str = pkey;
1467
1468                 targ->task_init = to_task_init(mode_str, sub_mode_str);
1469                 if (!targ->task_init) {
1470                         if ((strcmp(sub_mode_str, "l3") != 0) && (strcmp(sub_mode_str, "ndp") != 0)) {
1471                                 set_errf("sub mode %s not supported for mode %s", sub_mode_str, mode_str);
1472                                 return -1;
1473                         }
1474                         targ->task_init = to_task_init(mode_str, "");
1475                         if (!targ->task_init) {
1476                                 set_errf("sub mode %s not supported for mode %s", sub_mode_str, mode_str);
1477                                 return -1;
1478                         }
1479                 }
1480                 if (strcmp(sub_mode_str, "l3") == 0) {
1481                         prox_cfg.flags |= DSF_L3_ENABLED;
1482                         targ->flags |= TASK_ARG_L3;
1483                         strcpy(targ->sub_mode_str, "l3");
1484                 } else if (strcmp(sub_mode_str, "ndp") == 0) {
1485                         prox_cfg.flags |= DSF_NDP_ENABLED;
1486                         targ->flags |= TASK_ARG_NDP;
1487                         strcpy(targ->sub_mode_str, "ndp");
1488                 } else {
1489                         strcpy(targ->sub_mode_str, targ->task_init->sub_mode_str);
1490                 }
1491                 return 0;
1492         }
1493
1494         if (STR_EQ(str, "mempool name")) {
1495                 return parse_str(targ->pool_name, pkey, sizeof(targ->pool_name));
1496         }
1497         if (STR_EQ(str, "dpi engine")) {
1498                 return parse_str(targ->dpi_engine_path, pkey, sizeof(targ->dpi_engine_path));
1499         }
1500         if (STR_EQ(str, "dpi engine arg")) {
1501                 return parse_str(targ->dpi_engine_args[targ->n_dpi_engine_args++], pkey,
1502                                  sizeof(targ->dpi_engine_args[0]));
1503         }
1504         if (STR_EQ(str, "dst mac")) { /* destination MAC address to be used for packets */
1505                 if (parse_mac(&targ->edaddr, pkey)) {
1506                         if (STR_EQ(pkey, "no")) {
1507                                 targ->flags |= TASK_ARG_DO_NOT_SET_DST_MAC;
1508                                 return 0;
1509                         }
1510                         if (STR_EQ(pkey, "packet") == 0)
1511                                 return -1;
1512                         else
1513                                 return 0;
1514                 }
1515                 targ->flags |= TASK_ARG_DST_MAC_SET;
1516                 return 0;
1517         }
1518         if (STR_EQ(str, "src mac")) {
1519                 if (parse_mac(&targ->esaddr, pkey)) {
1520                         if (STR_EQ(pkey, "no")) {
1521                                 targ->flags |= TASK_ARG_DO_NOT_SET_SRC_MAC;
1522                                 return 0;
1523                         }
1524                         else if (STR_EQ(pkey, "packet"))
1525                                 return 0;
1526                         else if (STR_EQ(pkey, "hw")) {
1527                                 targ->flags |= TASK_ARG_HW_SRC_MAC;
1528                                 return 0;
1529                         } else {
1530                                 return -1;
1531                         }
1532                 }
1533                 targ->flags |= TASK_ARG_SRC_MAC_SET;
1534                 return 0;
1535         }
1536         if (STR_EQ(str, "igmp ipv4")) { /* IGMP Group */
1537                 return parse_ip(&targ->igmp_address, pkey);
1538         }
1539         if (STR_EQ(str, "gateway ipv4")) { /* Gateway IP address used when generating */
1540                 if ((targ->flags & TASK_ARG_L3) == 0)
1541                         plog_warn("gateway ipv4 configured but L3 sub mode not enabled\n");
1542                 if (targ->local_ipv4)
1543                         targ->local_prefix = 32;
1544                 return parse_ip(&targ->gateway_ipv4, pkey);
1545         }
1546         if (STR_EQ(str, "ipv6 router")) { /* we simulate an IPV6 router */
1547                 int rc = parse_flag(&targ->ipv6_router, 1, pkey);
1548                 if (!rc && targ->ipv6_router) {
1549                         plog_info("\tipv6 router configured => NDP enabled\n");
1550                         prox_cfg.flags |= DSF_NDP_ENABLED;
1551                         targ->flags |= TASK_ARG_NDP;
1552                         strcpy(targ->sub_mode_str, "ndp");
1553                 }
1554                 return 0;
1555         }
1556         if (STR_EQ(str, "gateway ipv6")) { /* Gateway IP address used when generating */
1557                 if ((targ->flags & TASK_ARG_NDP) == 0)
1558                         plog_warn("gateway ipv6 configured but NDP sub mode not enabled\n");
1559                 return parse_ip6(&targ->gateway_ipv6, pkey);
1560         }
1561         if (STR_EQ(str, "local ipv4")) { /* source IP address to be used for packets */
1562                 struct ip4_subnet cidr;
1563                 if (parse_ip4_and_prefix(&cidr, pkey) != 0) {
1564                         if (targ->gateway_ipv4)
1565                                 targ->local_prefix = 32;
1566                         else
1567                                 targ->local_prefix = 0;
1568                         return parse_ip(&targ->local_ipv4, pkey);
1569                 } else {
1570                         targ->local_ipv4 = cidr.ip;
1571                         targ->local_prefix = cidr.prefix;
1572                         return 0;
1573                 }
1574         }
1575         if (STR_EQ(str, "remote ipv4")) { /* source IP address to be used for packets */
1576                 return parse_ip(&targ->remote_ipv4, pkey);
1577         }
1578         if (STR_EQ(str, "global ipv6")) {
1579                 if (parse_ip6(&targ->global_ipv6, pkey) == 0) {
1580                         plog_info("\tglobal ipv6 configured => NDP enabled\n");
1581                         targ->flags |= TASK_ARG_NDP;
1582                         prox_cfg.flags |= DSF_NDP_ENABLED;
1583                         strcpy(targ->sub_mode_str, "ndp");
1584                 } else {
1585                         plog_err("Unable to parse content of local ipv6: %s\n", pkey);
1586                         return -1;
1587                 }
1588                 return 0;
1589         }
1590         if (STR_EQ(str, "local ipv6")) { /* source IPv6 address to be used for packets */
1591                 if (parse_ip6(&targ->local_ipv6, pkey) == 0) {
1592                         plog_info("\tlocal ipv6 configured => NDP enabled\n");
1593                         targ->flags |= TASK_ARG_NDP;
1594                         prox_cfg.flags |= DSF_NDP_ENABLED;
1595                         strcpy(targ->sub_mode_str, "ndp");
1596                 } else {
1597                         plog_err("Unable to parse content of local ipv6: %s\n", pkey);
1598                         return -1;
1599                 }
1600                 return 0;
1601         }
1602         if (STR_EQ(str, "router prefix")) {
1603                 if (parse_ip6(&targ->router_prefix, pkey) == 0) {
1604                         plog_info("\trouter prefix set to "IPv6_BYTES_FMT" (%s)\n", IPv6_BYTES(targ->router_prefix.bytes), IP6_Canonical(&targ->router_prefix));
1605                 } else {
1606                         plog_err("Unable to parse content of router prefix: %s\n", pkey);
1607                         return -1;
1608                 }
1609                 return 0;
1610         }
1611         if (STR_EQ(str, "arp timeout"))
1612                 return parse_int(&targ->reachable_timeout, pkey);
1613         if (STR_EQ(str, "arp update time"))
1614                 return parse_int(&targ->arp_ndp_retransmit_timeout, pkey);
1615         if (STR_EQ(str, "number of packets"))
1616                 return parse_int(&targ->n_pkts, pkey);
1617         if (STR_EQ(str, "store size"))
1618                 return parse_int(&targ->store_max, pkey);
1619         if (STR_EQ(str, "pipes")) {
1620                 uint32_t val;
1621                 int err = parse_int(&val, pkey);
1622                 if (err)
1623                         return -1;
1624                 if (!val || !rte_is_power_of_2(val)) {
1625                         set_errf("Number of pipes has to be power of 2 and not zero");
1626                         return -1;
1627                 }
1628
1629                 targ->qos_conf.port_params.n_pipes_per_subport = val;
1630                 return 0;
1631         }
1632         if (STR_EQ(str, "queue size")) {
1633                 uint32_t val;
1634                 int err = parse_int(&val, pkey);
1635                 if (err) {
1636                         return -1;
1637                 }
1638 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1639                 targ->qos_conf.subport_params[0].qsize[0] = val;
1640                 targ->qos_conf.subport_params[0].qsize[1] = val;
1641                 targ->qos_conf.subport_params[0].qsize[2] = val;
1642                 targ->qos_conf.subport_params[0].qsize[3] = val;
1643 #else
1644                 targ->qos_conf.port_params.qsize[0] = val;
1645                 targ->qos_conf.port_params.qsize[1] = val;
1646                 targ->qos_conf.port_params.qsize[2] = val;
1647                 targ->qos_conf.port_params.qsize[3] = val;
1648 #endif
1649                 return 0;
1650         }
1651         if (STR_EQ(str, "subport tb rate")) {
1652 #if RTE_VERSION >= RTE_VERSION_NUM(20,11,0,0)
1653                 return parse_u64(&targ->qos_conf.port_params.subport_profiles->tb_rate, pkey);
1654 #else
1655 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1656                 return parse_u64(&targ->qos_conf.subport_params[0].tb_rate, pkey);
1657 #else
1658                 return parse_int(&targ->qos_conf.subport_params[0].tb_rate, pkey);
1659 #endif
1660 #endif
1661         }
1662         if (STR_EQ(str, "subport tb size")) {
1663 #if RTE_VERSION >= RTE_VERSION_NUM(20,11,0,0)
1664                 return parse_u64(&targ->qos_conf.port_params.subport_profiles->tb_size, pkey);
1665 #else
1666 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1667                 return parse_u64(&targ->qos_conf.subport_params[0].tb_size, pkey);
1668 #else
1669                 return parse_int(&targ->qos_conf.subport_params[0].tb_size, pkey);
1670 #endif
1671 #endif
1672         }
1673         if (STR_EQ(str, "subport tc 0 rate")) {
1674 #if RTE_VERSION >= RTE_VERSION_NUM(20,11,0,0)
1675                 return parse_u64(&targ->qos_conf.port_params.subport_profiles->tc_rate[0], pkey);
1676 #else
1677 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1678                 return parse_u64(&targ->qos_conf.subport_params[0].tc_rate[0], pkey);
1679 #else
1680                 return parse_int(&targ->qos_conf.subport_params[0].tc_rate[0], pkey);
1681 #endif
1682 #endif
1683         }
1684         if (STR_EQ(str, "subport tc 1 rate")) {
1685 #if RTE_VERSION >= RTE_VERSION_NUM(20,11,0,0)
1686                 return parse_u64(&targ->qos_conf.port_params.subport_profiles->tc_rate[1], pkey);
1687 #else
1688 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1689                 return parse_u64(&targ->qos_conf.subport_params[0].tc_rate[1], pkey);
1690 #else
1691                 return parse_int(&targ->qos_conf.subport_params[0].tc_rate[1], pkey);
1692 #endif
1693 #endif
1694         }
1695         if (STR_EQ(str, "subport tc 2 rate")) {
1696 #if RTE_VERSION >= RTE_VERSION_NUM(20,11,0,0)
1697                 return parse_u64(&targ->qos_conf.port_params.subport_profiles->tc_rate[2], pkey);
1698 #else
1699 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1700                 return parse_u64(&targ->qos_conf.subport_params[0].tc_rate[2], pkey);
1701 #else
1702                 return parse_int(&targ->qos_conf.subport_params[0].tc_rate[2], pkey);
1703 #endif
1704 #endif
1705         }
1706         if (STR_EQ(str, "subport tc 3 rate")) {
1707 #if RTE_VERSION >= RTE_VERSION_NUM(20,11,0,0)
1708                 return parse_u64(&targ->qos_conf.port_params.subport_profiles->tc_rate[3], pkey);
1709 #else
1710 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1711                 return parse_u64(&targ->qos_conf.subport_params[0].tc_rate[3], pkey);
1712 #else
1713                 return parse_int(&targ->qos_conf.subport_params[0].tc_rate[3], pkey);
1714 #endif
1715 #endif
1716         }
1717
1718         if (STR_EQ(str, "subport tc rate")) {
1719                 uint32_t val;
1720                 int err = parse_int(&val, pkey);
1721                 if (err) {
1722                         return -1;
1723                 }
1724
1725 #if RTE_VERSION >= RTE_VERSION_NUM(20,11,0,0)
1726                 targ->qos_conf.port_params.subport_profiles->tc_rate[0] = val;
1727                 targ->qos_conf.port_params.subport_profiles->tc_rate[1] = val;
1728                 targ->qos_conf.port_params.subport_profiles->tc_rate[2] = val;
1729                 targ->qos_conf.port_params.subport_profiles->tc_rate[3] = val;
1730 #else
1731                 targ->qos_conf.subport_params[0].tc_rate[0] = val;
1732                 targ->qos_conf.subport_params[0].tc_rate[1] = val;
1733                 targ->qos_conf.subport_params[0].tc_rate[2] = val;
1734                 targ->qos_conf.subport_params[0].tc_rate[3] = val;
1735 #endif
1736
1737                 return 0;
1738         }
1739         if (STR_EQ(str, "subport tc period")) {
1740 #if RTE_VERSION >= RTE_VERSION_NUM(20,11,0,0)
1741                 return parse_u64(&targ->qos_conf.port_params.subport_profiles->tc_period, pkey);
1742 #else
1743 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1744                 return parse_u64(&targ->qos_conf.subport_params[0].tc_period, pkey);
1745 #else
1746                 return parse_int(&targ->qos_conf.subport_params[0].tc_period, pkey);
1747 #endif
1748 #endif
1749         }
1750         if (STR_EQ(str, "pipe tb rate")) {
1751 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1752                 return parse_u64(&targ->qos_conf.pipe_params[0].tb_rate, pkey);
1753 #else
1754                 return parse_int(&targ->qos_conf.pipe_params[0].tb_rate, pkey);
1755 #endif
1756         }
1757         if (STR_EQ(str, "pipe tb size")) {
1758 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1759                 return parse_u64(&targ->qos_conf.pipe_params[0].tb_size, pkey);
1760 #else
1761                 return parse_int(&targ->qos_conf.pipe_params[0].tb_size, pkey);
1762 #endif
1763         }
1764         if (STR_EQ(str, "pipe tc rate")) {
1765                 uint32_t val;
1766                 int err = parse_int(&val, pkey);
1767                 if (err) {
1768                         return -1;
1769                 }
1770
1771                 targ->qos_conf.pipe_params[0].tc_rate[0] = val;
1772                 targ->qos_conf.pipe_params[0].tc_rate[1] = val;
1773                 targ->qos_conf.pipe_params[0].tc_rate[2] = val;
1774                 targ->qos_conf.pipe_params[0].tc_rate[3] = val;
1775                 return 0;
1776         }
1777         if (STR_EQ(str, "pipe tc 0 rate")) {
1778 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1779                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_rate[0], pkey);
1780 #else
1781                 return parse_int(&targ->qos_conf.pipe_params[0].tc_rate[0], pkey);
1782 #endif
1783         }
1784         if (STR_EQ(str, "pipe tc 1 rate")) {
1785 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1786                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_rate[1], pkey);
1787 #else
1788                 return parse_int(&targ->qos_conf.pipe_params[0].tc_rate[1], pkey);
1789 #endif
1790         }
1791         if (STR_EQ(str, "pipe tc 2 rate")) {
1792 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1793                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_rate[2], pkey);
1794 #else
1795                 return parse_int(&targ->qos_conf.pipe_params[0].tc_rate[2], pkey);
1796 #endif
1797         }
1798         if (STR_EQ(str, "pipe tc 3 rate")) {
1799 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1800                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_rate[3], pkey);
1801 #else
1802                 return parse_int(&targ->qos_conf.pipe_params[0].tc_rate[3], pkey);
1803 #endif
1804         }
1805         if (STR_EQ(str, "pipe tc period")) {
1806 #if RTE_VERSION > RTE_VERSION_NUM(19,11,0,0)
1807                 return parse_u64(&targ->qos_conf.pipe_params[0].tc_period, pkey);
1808 #else
1809                 return parse_int(&targ->qos_conf.pipe_params[0].tc_period, pkey);
1810 #endif
1811         }
1812         if (STR_EQ(str, "police action")) {
1813                 char *in = strstr(pkey, " io=");
1814                 if (in == NULL) {
1815                         set_errf("Need to specify io colors using io=in_color,out_color\n");
1816                         return -1;
1817                 }
1818                 *in = 0;
1819                 in += strlen(" io=");
1820
1821                 char *out = strstr(in, ",");
1822                 if (out == NULL) {
1823                         set_errf("Output color not specified\n");
1824                 }
1825                 *out = 0;
1826                 out++;
1827
1828                 enum police_action in_color = str_to_color(in);
1829                 enum police_action out_color = str_to_color(out);
1830
1831                 if (in_color == ACT_INVALID) {
1832                         set_errf("Invalid input color %s. Expected green, yellow or red", in);
1833                         return -1;
1834                 }
1835                 if (out_color == ACT_INVALID) {
1836                         set_errf("Invalid output color %s. Expected green, yellow or red", out);
1837                         return -1;
1838                 }
1839                 enum police_action action = str_to_color(pkey);
1840                 if (action == ACT_INVALID) {
1841                         set_errf("Error action %s. Expected green, yellow, red or drop", pkey);
1842                         return -1;
1843                 }
1844                 targ->police_act[in_color][out_color] = action;
1845
1846                 return 0;
1847         }
1848         if (STR_EQ(str, "qinq tag")) {
1849                 return parse_int(&targ->qinq_tag, pkey);
1850         }
1851         if (STR_EQ(str, "cir")) {
1852                 return parse_int(&targ->cir, pkey);
1853         }
1854         if (STR_EQ(str, "cbs")) {
1855                 return parse_int(&targ->cbs, pkey);
1856         }
1857         if (STR_EQ(str, "pir")) {
1858                 return parse_int(&targ->pir, pkey);
1859         }
1860         if (STR_EQ(str, "pbs")) {
1861                 return parse_int(&targ->pbs, pkey);
1862         }
1863         if (STR_EQ(str, "ebs")) {
1864                 return parse_int(&targ->ebs, pkey);
1865         }
1866         uint32_t queue_id = 0;
1867         if (sscanf(str, "queue %d weight", &queue_id) == 1) {
1868                 uint32_t val;
1869                 int err = parse_int(&val, pkey);
1870                 if (err) {
1871                         return -1;
1872                 }
1873                 if (queue_id >= RTE_SCHED_BE_QUEUES_PER_PIPE) {
1874                         set_errf("queue_id must be < %d", RTE_SCHED_BE_QUEUES_PER_PIPE);
1875                         return -1;
1876                 }
1877                 targ->qos_conf.pipe_params[0].wrr_weights[queue_id] = val;
1878                 return 0;
1879         }
1880         if (STR_EQ(str, "classify")) {
1881                 if (!(targ->task_init->flag_features & TASK_FEATURE_CLASSIFY)) {
1882                         set_errf("Classify is not supported in '%s' mode", targ->task_init->mode_str);
1883                         return -1;
1884                 }
1885
1886                 return parse_flag(&targ->runtime_flags, TASK_CLASSIFY, pkey);
1887         }
1888         if (STR_EQ(str, "flow table size")) {
1889                 return parse_int(&targ->flow_table_size, pkey);
1890         }
1891 #ifdef GRE_TP
1892         if (STR_EQ(str, "tbf rate")) {
1893                 return parse_int(&targ->tb_rate, pkey);
1894         }
1895         if (STR_EQ(str, "tbf size")) {
1896                 return parse_int(&targ->tb_size, pkey);
1897         }
1898 #endif
1899         if (STR_EQ(str, "max rules")) {
1900                 return parse_int(&targ->n_max_rules, pkey);
1901         }
1902
1903         if (STR_EQ(str, "tunnel hop limit")) {
1904                 uint32_t val;
1905                 int err = parse_int(&val, pkey);
1906                 if (err) {
1907                         return -1;
1908                 }
1909                 targ->tunnel_hop_limit = val;
1910                 return 0;
1911         }
1912
1913         if (STR_EQ(str, "lookup port mask")) {
1914                 uint32_t val;
1915                 int err = parse_int(&val, pkey);
1916                 if (err) {
1917                         return -1;
1918                 }
1919                 targ->lookup_port_mask = val;
1920                 return 0;
1921         }
1922
1923         if (STR_EQ(str, "irq debug")) {
1924                 parse_int(&targ->irq_debug, pkey);
1925                 return 0;
1926         }
1927
1928         if (STR_EQ(str, "multiplier")) {
1929                 parse_int(&targ->multiplier, pkey);
1930                 return 0;
1931         }
1932
1933         if (STR_EQ(str, "mirror size")) {
1934                 parse_int(&targ->mirror_size, pkey);
1935                 return 0;
1936         }
1937
1938         set_errf("Option '%s' is not known", str);
1939         /* fail on unknown keys */
1940         return -1;
1941 }
1942
1943 static int str_is_number(const char *in)
1944 {
1945         int dot_once = 0;
1946
1947         for (size_t i = 0; i < strlen(in); ++i) {
1948                 if (!dot_once && in[i] == '.') {
1949                         dot_once = 1;
1950                         continue;
1951                 }
1952
1953                 if (in[i] < '0' || in[i] > '9')
1954                         return 0;
1955         }
1956
1957         return 1;
1958 }
1959
1960 /* command line parameters parsing procedure */
1961 int prox_parse_args(int argc, char **argv)
1962 {
1963         int i, opt, ret;
1964         char *tmp, *tmp2;
1965         char tmp3[64];
1966
1967         /* Default settings */
1968         prox_cfg.flags |= DSF_AUTOSTART | DSF_WAIT_ON_QUIT;
1969         prox_cfg.ui = PROX_UI_CURSES;
1970
1971         plog_info("\tCommand line:");
1972         for (i = 0; i < argc; ++i) {
1973                 plog_info(" %s", argv[i]);
1974         }
1975         plog_info("\n");
1976
1977         while ((opt = getopt(argc, argv, "f:dnzpo:tkuar:emsiw:l:v:q:")) != EOF) {
1978                 switch (opt) {
1979                 case 'f':
1980                         /* path to config file */
1981                         cfg_file = optarg;
1982                         size_t offset = 0;
1983                         for (size_t i = 0; i < strlen(cfg_file); ++i) {
1984                                 if (cfg_file[i] == '/') {
1985                                         offset = i + 1;
1986                                 }
1987                         }
1988
1989                         prox_strncpy(prox_cfg.name, cfg_file + offset, MAX_NAME_SIZE);
1990                         break;
1991                 case 'v':
1992                         plog_set_lvl(atoi(optarg));
1993                         break;
1994                 case 'l':
1995                         prox_cfg.log_name_pid = 0;
1996                         prox_strncpy(prox_cfg.log_name, optarg, MAX_NAME_SIZE);
1997                         break;
1998                 case 'p':
1999                         prox_cfg.log_name_pid = 1;
2000                         break;
2001                 case 'k':
2002                         prox_cfg.use_stats_logger = 1;
2003                         break;
2004                 case 'd':
2005                         prox_cfg.flags |= DSF_DAEMON;
2006                         prox_cfg.ui = PROX_UI_NONE;
2007                         break;
2008                 case 'z':
2009                         prox_cfg.flags |= DSF_USE_DUMMY_CPU_TOPO;
2010                         prox_cfg.flags |= DSF_CHECK_INIT;
2011                         break;
2012                 case 'n':
2013                         prox_cfg.flags |= DSF_USE_DUMMY_DEVICES;
2014                         break;
2015                 case 'r':
2016                         if (!str_is_number(optarg) || strlen(optarg) > 11)
2017                                 return -1;
2018                         prox_strncpy(prox_cfg.update_interval_str, optarg, sizeof(prox_cfg.update_interval_str));
2019                         break;
2020                 case 'o':
2021                         if (prox_cfg.flags & DSF_DAEMON)
2022                                 break;
2023
2024                         if (!strcmp(optarg, "curses")) {
2025                                 prox_cfg.ui = PROX_UI_CURSES;
2026                         }
2027                         else if (!strcmp(optarg, "cli")) {
2028                                 prox_cfg.ui = PROX_UI_CLI;
2029                         }
2030                         else if (!strcmp(optarg, "none")) {
2031                                 prox_cfg.ui = PROX_UI_NONE;
2032                         }
2033                         else {
2034                                 plog_err("Invalid local UI '%s', local UI can be 'curses', 'cli' or 'none'.", optarg);
2035                                 return -1;
2036                         }
2037                         break;
2038                 case 'q':
2039                         if (luaL_loadstring(prox_lua(), optarg)) {
2040                                 set_errf("Lua error: '%s'\n", lua_tostring(prox_lua(), -1));
2041                                 return -1;
2042                         }
2043
2044                         if (lua_pcall(prox_lua(), 0, LUA_MULTRET, 0)) {
2045                                 set_errf("Lua error: '%s'\n", lua_tostring(prox_lua(), -1));
2046                                 return -1;
2047                         }
2048
2049                         break;
2050                 case 'a':
2051                         /* autostart all cores */
2052                         prox_cfg.flags |= DSF_AUTOSTART;
2053                         break;
2054                 case 'e':
2055                         /* don't autostart */
2056                         prox_cfg.flags &= ~DSF_AUTOSTART;
2057                         break;
2058                 case 't':
2059                         prox_cfg.flags |= DSF_LISTEN_TCP;
2060                         break;
2061                 case 'u':
2062                         prox_cfg.flags |= DSF_LISTEN_UDS;
2063                         break;
2064                 case 'm':
2065                         /* list supported task modes and exit */
2066                         prox_cfg.flags |= DSF_LIST_TASK_MODES;
2067                         break;
2068                 case 's':
2069                         /* check configuration file syntax and exit */
2070                         prox_cfg.flags |= DSF_CHECK_SYNTAX;
2071                         break;
2072                 case 'i':
2073                         /* check initialization sequence and exit */
2074                         prox_cfg.flags |= DSF_CHECK_INIT;
2075                         break;
2076                 case 'w':
2077                         tmp = optarg;
2078                         tmp2 = 0;
2079                         if (strlen(tmp) >= 3 &&
2080                             (tmp2 = strchr(tmp, '='))) {
2081                                 *tmp2 = 0;
2082                                 tmp3[0] = '$';
2083                                 prox_strncpy(tmp3 + 1, tmp, 63);
2084                                 plog_info("\tAdding variable: %s = %s\n", tmp3, tmp2 + 1);
2085                                 ret = add_var(tmp3, tmp2 + 1, 1);
2086                                 if (ret == -2) {
2087                                         plog_err("\tFailed to add variable, too many variables defines\n");
2088                                         return -1;
2089                                 }
2090                                 else if(ret == -3) {
2091                                         plog_err("\tFailed to add variable, already defined\n");
2092                                         return -1;
2093                                 }
2094                                 break;
2095                         }
2096                         /* fall-through */
2097                 default:
2098                         plog_err("\tUnknown option\n");
2099                         return -1;
2100                 }
2101         }
2102
2103         /* reset getopt lib for DPDK */
2104         optind = 0;
2105
2106         return 0;
2107 }
2108
2109 static int check_cfg(void)
2110 {
2111         /* Sanity check */
2112 #define RETURN_IF(cond, err)                    \
2113         if (cond) {                             \
2114                 plog_err(err);                  \
2115                 return -1;                      \
2116         };
2117
2118         RETURN_IF(rte_cfg.force_nchannel == 0, "\tError: number of memory channels not specified in [eal options] section\n");
2119         RETURN_IF(prox_cfg.master >= RTE_MAX_LCORE, "\tError: No master core specified (one core needs to have mode=master)\n");
2120
2121 #undef RETURN_IF
2122
2123         return 0;
2124 }
2125
2126 static int calc_tot_rxrings(void)
2127 {
2128         struct lcore_cfg *slconf, *dlconf;
2129         struct task_args *starg, *dtarg;
2130         uint32_t dlcore_id;
2131         uint8_t dtask_id;
2132         struct core_task ct;
2133
2134         dlconf = NULL;
2135         while (core_targ_next_early(&dlconf, &dtarg, 1) == 0) {
2136                 dtarg->tot_rxrings = 0;
2137         }
2138
2139         slconf = NULL;
2140         while (core_targ_next_early(&slconf, &starg, 1) == 0) {
2141                 for (uint8_t idx = 0; idx < MAX_PROTOCOLS; ++idx) {
2142                         for (uint8_t ring_idx = 0; ring_idx < starg->core_task_set[idx].n_elems; ++ring_idx) {
2143                                 ct = starg->core_task_set[idx].core_task[ring_idx];
2144                                 if (!prox_core_active(ct.core, 0)) {
2145                                         set_errf("Core %u is disabled but Core %u task %u is sending to it\n",
2146                                                  ct.core, slconf->id, starg->id);
2147                                         return -1;
2148                                 }
2149
2150                                 dlconf = &lcore_cfg_init[ct.core];
2151
2152                                 if (ct.task >= dlconf->n_tasks_all) {
2153                                         set_errf("Core %u task %u not enabled\n", ct.core, ct.task);
2154                                         return -1;
2155                                 }
2156
2157                                 dtarg = &dlconf->targs[ct.task];
2158
2159                                 /* Control rings are not relevant at this point. */
2160                                 if (ct.type)
2161                                         continue;
2162
2163                                 if (!(dtarg->flags & TASK_ARG_RX_RING)) {
2164                                         set_errf("Core %u task %u is not expecting to receive through a ring\n",
2165                                                  ct.core, ct.task);
2166                                         return -1;
2167                                 }
2168
2169                                 dtarg->tot_rxrings++;
2170                                 if (dtarg->tot_rxrings > MAX_RINGS_PER_TASK) {
2171                                         set_errf("Core %u task %u is receiving from too many tasks",
2172                                                  ct.core, ct.task);
2173                                         return -1;
2174                                 }
2175                         }
2176                 }
2177         }
2178
2179         return 0;
2180 }
2181
2182 static void prox_set_core_mask(void)
2183 {
2184         struct lcore_cfg *lconf;
2185
2186         prox_core_clr();
2187         for (uint8_t lcore_id = 0; lcore_id < RTE_MAX_LCORE; ++lcore_id) {
2188                 lconf = &lcore_cfg_init[lcore_id];
2189                 if (lconf->n_tasks_all > 0 && lconf->targs[0].mode != MASTER) {
2190                         prox_core_set_active(lcore_id);
2191                 }
2192         }
2193 }
2194
2195 static int is_using_no_drop(void)
2196 {
2197         uint32_t lcore_id;
2198         struct lcore_cfg *lconf;
2199         struct task_args *targs;
2200
2201         lcore_id = -1;
2202         while(prox_core_next(&lcore_id, 1) == 0) {
2203                 lconf = &lcore_cfg_init[lcore_id];
2204                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
2205                         targs = &lconf->targs[task_id];
2206                         if (!(targs->flags & TASK_ARG_DROP))
2207                                 return 1;
2208                 }
2209         }
2210         return 0;
2211 }
2212
2213 int prox_read_config_file(void)
2214 {
2215         set_global_defaults(&prox_cfg);
2216         set_task_defaults(&prox_cfg, lcore_cfg_init);
2217         set_port_defaults();
2218         plog_info("=== Parsing configuration file '%s' ===\n", cfg_file);
2219         struct cfg_file *pcfg = cfg_open(cfg_file);
2220         if (pcfg == NULL) {
2221                 return -1;
2222         }
2223
2224         struct cfg_section* config_sections[] = {
2225                 &lua_cfg          ,
2226                 &var_cfg          ,
2227                 &eal_default_cfg  ,
2228                 &cache_set_cfg    ,
2229                 &port_cfg         ,
2230                 &defaults_cfg     ,
2231                 &settings_cfg     ,
2232                 &core_cfg         ,
2233                 NULL
2234         };
2235
2236         for (struct cfg_section** section = config_sections; *section != NULL; ++section) {
2237                 const char* name = (*section)->name;
2238                 size_t len = strlen(name);
2239                 plog_info("\t*** Reading [%s] section%s ***\n", name, name[len - 1] == '#'? "s": "");
2240                 cfg_parse(pcfg, *section);
2241
2242                 if ((*section)->error) {
2243                         plog_err("At line %u, section [%s], entry %u: '%s'\n\t%s\n"
2244                                  , pcfg->err_line, pcfg->err_section, pcfg->err_entry + 1, pcfg->cur_line,
2245                                  strlen(get_parse_err())? get_parse_err() : err_str);
2246                         cfg_close(pcfg); /* cannot close before printing error, print uses internal buffer */
2247                         return -1;
2248                 }
2249         }
2250
2251         cfg_close(pcfg);
2252
2253         prox_set_core_mask();
2254
2255         if (is_using_no_drop()) {
2256                 prox_cfg.flags &= ~DSF_WAIT_ON_QUIT;
2257         }
2258
2259         if (calc_tot_rxrings()) {
2260                 plog_err("Error in configuration: %s\n", err_str);
2261                 return -1;
2262         }
2263
2264         return check_cfg();
2265 }
2266
2267 static void failed_rte_eal_init(__attribute__((unused))const char *prog_name)
2268 {
2269         plog_err("\tError in rte_eal_init()\n");
2270 }
2271
2272 int prox_setup_rte(const char *prog_name)
2273 {
2274         char *rte_argv[MAX_RTE_ARGV];
2275         char  rte_arg[MAX_RTE_ARGV][MAX_ARG_LEN];
2276         char tmp[PROX_CM_STR_LEN];
2277         /* create mask of used cores */
2278         plog_info("=== Setting up RTE EAL ===\n");
2279
2280         if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO) {
2281                 plog_info("Using dummy cpu topology\n");
2282                 snprintf(tmp, sizeof(tmp), "0x1");
2283         } else {
2284                 prox_core_to_hex(tmp, sizeof(tmp), 0);
2285                 plog_info("\tWorker threads core mask is %s\n", tmp);
2286                 prox_core_to_hex(tmp, sizeof(tmp), 1);
2287                 plog_info("\tWith master core index %u, full core mask is %s\n", prox_cfg.master, tmp);
2288         }
2289
2290         /* fake command line parameters for rte_eal_init() */
2291         int argc = 0;
2292         rte_argv[argc] = strdup(prog_name);
2293         sprintf(rte_arg[++argc], "-c%s", tmp);
2294         rte_argv[argc] = rte_arg[argc];
2295 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
2296         if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO)
2297                 sprintf(rte_arg[++argc], "--master-lcore=%u", 0);
2298         else
2299                 sprintf(rte_arg[++argc], "--master-lcore=%u", prox_cfg.master);
2300         rte_argv[argc] = rte_arg[argc];
2301 #else
2302         /* For old DPDK versions, the master core had to be the first
2303            core. */
2304         uint32_t first_core = -1;
2305
2306         if (prox_core_next(&first_core, 1) == -1) {
2307                 plog_err("Can't core ID of first core in use\n");
2308                 return -1;
2309         }
2310         if (first_core != prox_cfg.master) {
2311                 plog_err("The master core needs to be the first core (master core = %u, first core = %u).\n", first_core, prox_cfg.master);
2312                 return -1;
2313         }
2314 #endif
2315
2316         if (rte_cfg.memory) {
2317                 sprintf(rte_arg[++argc], "-m%u", rte_cfg.memory);
2318                 rte_argv[argc] = rte_arg[argc];
2319         }
2320
2321         if (rte_cfg.force_nchannel) {
2322                 sprintf(rte_arg[++argc], "-n%u", rte_cfg.force_nchannel);
2323                 rte_argv[argc] = rte_arg[argc];
2324         }
2325
2326         if (rte_cfg.force_nrank) {
2327                 sprintf(rte_arg[++argc], "-r%u", rte_cfg.force_nrank);
2328                 rte_argv[argc] = rte_arg[argc];
2329         }
2330
2331         if (rte_cfg.no_hugetlbfs) {
2332                 strcpy(rte_arg[++argc], "--no-huge");
2333                 rte_argv[argc] = rte_arg[argc];
2334         }
2335
2336         if (rte_cfg.no_pci) {
2337                 strcpy(rte_arg[++argc], "--no-pci");
2338                 rte_argv[argc] = rte_arg[argc];
2339         }
2340
2341         if (rte_cfg.no_hpet) {
2342                 strcpy(rte_arg[++argc], "--no-hpet");
2343                 rte_argv[argc] = rte_arg[argc];
2344         }
2345
2346         if (rte_cfg.no_shconf) {
2347                 strcpy(rte_arg[++argc], "--no-shconf");
2348                 rte_argv[argc] = rte_arg[argc];
2349         }
2350
2351         if (rte_cfg.eal != NULL) {
2352                 char *ptr = rte_cfg.eal;
2353                 char *ptr2;
2354                 while (ptr != NULL) {
2355                         while (isspace(*ptr))
2356                                 ptr++;
2357                         ptr2 = ptr;
2358                         ptr = strchr(ptr, ' ');
2359                         if (ptr) {
2360                                 *ptr++ = '\0';
2361                         }
2362                         prox_strncpy(rte_arg[++argc], ptr2, MAX_ARG_LEN);
2363                         rte_argv[argc] = rte_arg[argc];
2364                 }
2365         }
2366
2367         if (rte_cfg.hugedir != NULL) {
2368                 strcpy(rte_arg[++argc], "--huge-dir");
2369                 rte_argv[argc] = rte_arg[argc];
2370                 rte_argv[++argc] = rte_cfg.hugedir;
2371         }
2372
2373         if (rte_cfg.no_output) {
2374                 rte_log_set_global_level(0);
2375         }
2376         /* init EAL */
2377         plog_info("\tEAL command line:");
2378         if (argc >= MAX_RTE_ARGV) {
2379                 plog_err("too many arguments for EAL\n");
2380                 return -1;
2381         }
2382
2383         for (int h = 0; h <= argc; ++h) {
2384                 plog_info(" %s", rte_argv[h]);
2385         }
2386         plog_info("\n");
2387
2388         rte_set_application_usage_hook(failed_rte_eal_init);
2389         if (rte_eal_init(++argc, rte_argv) < 0) {
2390                 plog_err("\tError in rte_eal_init()\n");
2391                 return -1;
2392         }
2393         plog_info("\tEAL Initialized\n");
2394
2395         if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO)
2396                 return 0;
2397
2398         /* check if all active cores are in enabled in DPDK */
2399         for (uint32_t lcore_id = 0; lcore_id < RTE_MAX_LCORE; ++lcore_id) {
2400                 if (lcore_id == prox_cfg.master) {
2401                         if (!rte_lcore_is_enabled(lcore_id))
2402                                 return -1;
2403                 }
2404                 else if (rte_lcore_is_enabled(lcore_id) != prox_core_active(lcore_id, 0)) {
2405                         plog_err("\tFailed to enable lcore %u\n", lcore_id);
2406                         return -1;
2407                 }
2408                 else if (lcore_cfg_init[lcore_id].n_tasks_all != 0 && !rte_lcore_is_enabled(lcore_id)) {
2409                         plog_err("\tFailed to enable lcore %u\n", lcore_id);
2410                         return -1;
2411                 }
2412         }
2413         uint16_t port_id;
2414         for (int i = 0; i < n_deferred_ports; i++) {
2415                 if (prox_rte_eth_dev_get_port_by_name(deferred_port[i].name, &port_id) != 0) {
2416                         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");
2417                         return -1;
2418                 }
2419                 plog_info("\tport %s is port id %d\n", deferred_port[i].name, port_id);
2420                 if (deferred_port[i].is_rx_port) {
2421                         deferred_port[i].targ->rx_port_queue[0].port = port_id;
2422                         deferred_port[i].targ->nb_rxports = 1;
2423                 } else {
2424                         deferred_port[i].targ->tx_port_queue[0].port = port_id;
2425                         deferred_port[i].targ->nb_txports = 1;
2426                 }
2427         }
2428         return 0;
2429 }