887de80304329a532c43d80acab216e09464b53d
[samplevnf.git] / VNFs / DPPD-PROX / parse_utils.c
1 /*
2 // Copyright (c) 2010-2017 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 <ctype.h>
18 #include <stdio.h>
19 #include <float.h>
20 #include <math.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24
25 #include <rte_ether.h>
26 #include <rte_string_fns.h>
27
28 #include "quit.h"
29 #include "cfgfile.h"
30 #include "ip6_addr.h"
31 #include "parse_utils.h"
32 #include "prox_globals.h"
33 #include "prox_cfg.h"
34 #include "log.h"
35 #include "prox_lua.h"
36 #include "prox_lua_types.h"
37 #include "prox_compat.h"
38
39 #define MAX_NB_PORT_NAMES PROX_MAX_PORTS
40 #define MAX_LEN_PORT_NAME 24
41 #define MAX_LEN_VAR_NAME  24
42 #define MAX_LEN_VAL       512
43 #define MAX_NB_VARS       32
44
45 #if MAX_WT_PER_LB > MAX_INDEX
46 #error MAX_WT_PER_LB > MAX_INDEX
47 #endif
48
49 /* The CPU topology of the system is used to parse "socket
50    notation". This notation allows to refer to cores on specific
51    sockets and the hyper-thread of those cores. The CPU topology is
52    loaded only if the socket notation is used at least once. */
53
54 struct cpu_topology {
55         int socket[MAX_SOCKETS][RTE_MAX_LCORE][2];
56         uint32_t n_cores[MAX_SOCKETS];
57         uint32_t n_sockets;
58 };
59
60 struct cpu_topology cpu_topo;
61
62 struct port_name {
63         uint32_t id;
64         char     name[MAX_LEN_PORT_NAME];
65 };
66
67 static struct port_name port_names[MAX_NB_PORT_NAMES];
68 static uint8_t nb_port_names;
69
70 struct var {
71         uint8_t  cli;
72         char     name[MAX_LEN_VAR_NAME];
73         char     val[MAX_LEN_VAL];
74 };
75
76 static struct var vars[MAX_NB_VARS];
77 static uint8_t nb_vars;
78
79 static char format_err_str[256];
80 static const char *err_str = "";
81
82 const char *get_parse_err(void)
83 {
84         return err_str;
85 }
86
87 static int read_cpu_topology(void);
88
89 static int parse_core(int *socket, int *core, int *ht, const char* str);
90
91 static void set_errf(const char *format, ...)
92 {
93         va_list ap;
94         va_start(ap, format);
95         vsnprintf(format_err_str, sizeof(format_err_str), format, ap);
96         va_end(ap);
97         err_str = format_err_str;
98 }
99
100 static struct var *var_lookup(const char *name)
101 {
102         for (uint8_t i = 0; i < nb_vars; ++i) {
103                 if (!strcmp(name, vars[i].name)) {
104                         return &vars[i];
105                 }
106         }
107         return NULL;
108 }
109
110 int parse_single_var(char *val, size_t len, const char *name)
111 {
112         struct var *match;
113
114         match = var_lookup(name);
115         if (match) {
116                 if (strlen(match->val) + 1 > len) {
117                         set_errf("Variables '%s' with value '%s' is too long\n",
118                                  match->name, match->val);
119                         return -1;
120                 }
121                 prox_strncpy(val, match->val, len);
122                 return 0;
123         }
124         else {
125                 /* name + 1 to skip leading '$' */
126                 if (lua_to_string(prox_lua(), GLOBAL, name + 1, val, len) >= 0)
127                         return 0;
128         }
129
130         set_errf("Variable '%s' not defined!", name);
131         return 1;
132 }
133
134 /* Replace $... and each occurrence of ${...} with variable values */
135 int parse_vars(char *val, size_t len, const char *name)
136 {
137         static char result[MAX_CFG_STRING_LEN];
138         static char cur_var[MAX_CFG_STRING_LEN];
139         char parsed[MAX_CFG_STRING_LEN];
140         size_t name_len = strlen(name);
141         enum parse_vars_state {NO_VAR, WHOLE_VAR, INLINE_VAR} state = NO_VAR;
142         size_t result_len = 0;
143         size_t start_var = 0;
144
145         memset(result, 0, sizeof(result));
146         PROX_PANIC(name_len > sizeof(result), "\tUnable to parse var %s: too long\n", name);
147
148         for (size_t i = 0; i < name_len; ++i) {
149                 switch (state) {
150                 case NO_VAR:
151                         if (name[i] == '$') {
152                                 if (i != name_len - 1 && name[i + 1] == '{') {
153                                         start_var = i + 2;
154                                         state = INLINE_VAR;
155                                         i = i + 1;
156                                 }
157                                 else if (i == 0 && i != name_len - 1) {
158                                         state = WHOLE_VAR;
159                                 }
160                                 else {
161                                         set_errf("Invalid variable syntax");
162                                         return -1;
163                                 }
164                         }
165                         else {
166                                 result[result_len++] = name[i];
167                         }
168                         break;
169                 case INLINE_VAR:
170                         if (name[i] == '}') {
171                                 cur_var[0] = '$';
172                                 size_t var_len = i - start_var;
173                                 if (var_len == 0) {
174                                         set_errf("Empty variable are not allowed");
175                                         return -1;
176                                 }
177
178                                 strncpy(&cur_var[1], &name[start_var], var_len);
179                                 cur_var[1 + var_len] = 0;
180                                 if (parse_single_var(parsed, sizeof(parsed), cur_var)) {
181                                         return -1;
182                                 }
183                                 strcpy(&result[result_len], parsed);
184                                 result_len += strlen(parsed);
185                                 state = NO_VAR;
186                         }
187                         else if (i == name_len - 1) {
188                                 set_errf("Invalid variable syntax, expected '}'.");
189                                 return -1;
190                         }
191                         break;
192                 case WHOLE_VAR:
193                         if (i == name_len - 1) {
194                                 return parse_single_var(val, len, name);
195                         }
196                         break;
197                 }
198         }
199         prox_strncpy(val, result, len);
200
201         return 0;
202 }
203
204 int parse_int_mask(uint32_t *val, uint32_t *mask, const char *str2)
205 {
206         char str[MAX_STR_LEN_PROC];
207         char *mask_str;
208
209         if (parse_vars(str, sizeof(str), str2))
210                 return -1;
211
212         mask_str = strchr(str, '&');
213
214         if (mask_str == NULL) {
215                 set_errf("Missing '&' when parsing mask");
216                 return -2;
217         }
218
219         *mask_str = 0;
220
221         if (parse_int(val, str))
222                 return -1;
223         if (parse_int(mask, mask_str + 1))
224                 return -1;
225
226         return 0;
227 }
228
229 int parse_range(uint32_t* lo, uint32_t* hi, const char *str2)
230 {
231         char str[MAX_STR_LEN_PROC];
232         char *dash;
233
234         if (parse_vars(str, sizeof(str), str2))
235                 return -1;
236
237         dash = strstr(str, "-");
238
239         if (dash == NULL) {
240                 set_errf("Missing '-' when parsing mask");
241                 return -2;
242         }
243
244         *dash = 0;
245
246         if (parse_int(lo, str))
247                 return -1;
248         if (parse_int(hi, dash + 1))
249                 return -1;
250
251         int64_t tmp = strtol(str, 0, 0);
252         if (tmp > UINT32_MAX) {
253                 set_errf("Integer is bigger than %u", UINT32_MAX);
254                 return -1;
255         }
256         if (tmp < 0) {
257                 set_errf("Integer is negative");
258                 return -2;
259         }
260
261         *lo = tmp;
262
263         tmp = strtol(dash + 1, 0, 0);
264         if (tmp > UINT32_MAX) {
265                 set_errf("Integer is bigger than %u", UINT32_MAX);
266                 return -1;
267         }
268         if (tmp < 0) {
269                 set_errf("Integer is negative");
270                 return -2;
271         }
272
273         *hi = tmp;
274
275         if (*lo > *hi) {
276                 set_errf("Low boundary is above high boundary in range");
277                 return -2;
278         }
279
280         return 0;
281 }
282
283 int parse_ip(uint32_t *addr, const char *str2)
284 {
285         char str[MAX_STR_LEN_PROC];
286
287         if (parse_vars(str, sizeof(str), str2))
288                 return -1;
289
290         char *ip_parts[5];
291
292         if (strlen(str) > MAX_STR_LEN_PROC) {
293                 set_errf("String too long (max supported: %d)", MAX_STR_LEN_PROC);
294                 return -2;
295         }
296
297         if (4 != rte_strsplit(str, strlen(str), ip_parts, 5, '.')) {
298                 set_errf("Expecting 4 octets in ip.");
299                 return -1;
300         }
301
302         uint32_t val;
303         for (uint8_t i = 0; i < 4; ++i) {
304                 val = atoi(ip_parts[i]);
305                 if (val > 255) {
306                         set_errf("Maximum value for octet is 255 but octet %u is %u", i, val);
307                         return -1;
308                 }
309                 *addr = *addr << 8 | val;
310         }
311         return 0;
312 }
313
314 int parse_ip4_cidr(struct ip4_subnet *val, const char *str2)
315 {
316         char str[MAX_STR_LEN_PROC];
317         char *slash;
318         int prefix;
319
320         if (parse_vars(str, sizeof(str), str2))
321                 return -1;
322
323         slash = strstr(str, "/");
324
325         if (slash == NULL) {
326                 set_errf("Missing '/' when parsing CIDR notation");
327                 return -2;
328         }
329
330         *slash = 0;
331         prefix = atoi(slash + 1);
332         val->prefix = prefix;
333
334         if (prefix > 32) {
335                 set_errf("Prefix %d is too big", prefix);
336                 return -2;
337         }
338         if (prefix < 1) {
339                 set_errf("Prefix %d is too small", prefix);
340         }
341         if (parse_ip(&val->ip, str))
342                 return -2;
343
344         /* Apply mask making all bits outside the prefix zero */
345         val->ip &= ((int)(1 << 31)) >> (prefix - 1);
346
347         return 0;
348 }
349
350 int parse_ip6_cidr(struct ip6_subnet *val, const char *str2)
351 {
352         char str[MAX_STR_LEN_PROC];
353         char *slash;
354         int prefix;
355
356         if (parse_vars(str, sizeof(str), str2))
357                 return -1;
358
359         slash = strstr(str, "/");
360
361         if (slash == NULL) {
362                 set_errf("Missing '/' when parsing CIDR notation");
363                 return -2;
364         }
365
366         *slash = 0;
367         prefix = atoi(slash + 1);
368         val->prefix = prefix;
369
370         parse_ip6((struct ipv6_addr *)&val->ip, str);
371
372         /* Apply mask making all bits outside the prefix zero */
373
374         int p = 120;
375         int cnt = 0;
376
377         while (p >= prefix) {
378                 val->ip[15-cnt] = 0;
379                 p -= 8;
380                 cnt++;
381         }
382
383         if (prefix % 8 != 0) {
384                 val->ip[15-cnt] &= ((int8_t)(1 << 7)) >> ((prefix %8) - 1);
385         }
386
387         return 0;
388 }
389
390 int parse_ip6(struct ipv6_addr *addr, const char *str2)
391 {
392         char str[MAX_STR_LEN_PROC];
393         char *addr_parts[9];
394
395         if (parse_vars(str, sizeof(str), str2))
396                 return -1;
397
398         uint8_t ret = rte_strsplit(str, strlen(str), addr_parts, 9, ':');
399
400         if (ret == 9) {
401                 set_errf("Invalid IPv6 address");
402                 return -1;
403         }
404
405         uint8_t omitted = 0;
406
407         for (uint8_t i = 0, j = 0; i < ret; ++i, ++j) {
408                 if (*addr_parts[i] == 0) {
409                         if (omitted == 0) {
410                                 set_errf("Can only omit zeros once");
411                                 return -1;
412                         }
413                         omitted = 1;
414                         j += 8 - ret;
415                 }
416                 else {
417                         uint16_t w = strtoll(addr_parts[i], NULL, 16);
418                         addr->bytes[j++] = (w >> 8) & 0xff;
419                         addr->bytes[j] = w & 0xff;
420                 }
421         }
422         return 0;
423 }
424
425 int parse_mac(prox_rte_ether_addr *ether_addr, const char *str2)
426 {
427         char str[MAX_STR_LEN_PROC];
428         char *addr_parts[7];
429
430         if (parse_vars(str, sizeof(str), str2))
431                 return -1;
432
433         uint8_t ret = rte_strsplit(str, strlen(str), addr_parts, 7, ':');
434         if (ret != 6)
435                 ret = rte_strsplit(str, strlen(str), addr_parts, 7, ' ');
436
437         if (ret != 6) {
438                 set_errf("Invalid MAC address format");
439                 return -1;
440         }
441
442         for (uint8_t i = 0; i < 6; ++i) {
443                 if (2 != strlen(addr_parts[i])) {
444                         set_errf("Invalid MAC address format");
445                         return -1;
446                 }
447                 ether_addr->addr_bytes[i] = strtol(addr_parts[i], NULL, 16);
448         }
449
450         return 0;
451 }
452
453 char* get_cfg_key(char *str)
454 {
455         char *pkey = strchr(str, '=');
456
457         if (pkey == NULL) {
458                 return NULL;
459         }
460         *pkey++ = '\0';
461
462         /* remove leading spaces */
463         while (isspace(*pkey)) {
464                 pkey++;
465         }
466         if (*pkey == '\0') { /* an empty key */
467                 return NULL;
468         }
469
470         return pkey;
471 }
472
473 void strip_spaces(char *strings[], const uint32_t count)
474 {
475         for (uint32_t i = 0; i < count; ++i) {
476                 while (isspace(strings[i][0])) {
477                         ++strings[i];
478                 }
479                 size_t len = strlen(strings[i]);
480
481                 while (len && isspace(strings[i][len - 1])) {
482                         strings[i][len - 1] = '\0';
483                         --len;
484                 }
485         }
486 }
487
488 int is_virtualized(void)
489 {
490         char buf[1024]= "/proc/cpuinfo";
491         int virtualized = 0;
492         FILE* fd = fopen(buf, "r");
493         if (fd == NULL) {
494                 set_errf("Could not open %s", buf);
495                 return -1;
496         }
497         while (fgets(buf, sizeof(buf), fd) != NULL) {
498                 if ((strstr(buf, "flags") != NULL) && (strstr(buf, "hypervisor") != NULL))
499                         virtualized = 1;
500         }
501         fclose(fd);
502         return virtualized;
503 }
504
505 static int get_phys_core(uint32_t *dst, int lcore_id)
506 {
507         uint32_t ret;
508         char buf[1024];
509         snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcore_id);
510         FILE* ht_fd = fopen(buf, "r");
511
512         if (ht_fd == NULL) {
513                 set_errf("Could not open cpu topology %s", buf);
514                 return -1;
515         }
516
517         if (fgets(buf, sizeof(buf), ht_fd) == NULL) {
518                 set_errf("Could not read cpu topology");
519                 return -1;
520         }
521         fclose(ht_fd);
522
523         uint32_t list[2] = {-1,-1};
524         parse_list_set(list, buf, 2);
525
526         *dst = list[0];
527
528         return 0;
529 }
530
531 static int get_socket(uint32_t core_id, uint32_t *socket)
532 {
533         int ret = -1;
534         char buf[1024];
535         snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu%u/topology/physical_package_id", core_id);
536         FILE* fd = fopen(buf, "r");
537
538         if (fd == NULL) {
539                 set_errf("%s", buf);
540                 return -1;
541         }
542
543         if (fgets(buf, sizeof(buf), fd) != NULL) {
544                 ret = atoi(buf);
545         }
546         fclose(fd);
547
548         if (socket)
549                 *socket = (ret == -1 ? 0 : ret);
550
551         return 0;
552 }
553
554 int lcore_to_socket_core_ht(uint32_t lcore_id, char *dst, size_t len)
555 {
556         if (cpu_topo.n_sockets == 0) {
557                 if (read_cpu_topology() == -1) {
558                         return -1;
559                 }
560         }
561
562         for (uint32_t s = 0; s < cpu_topo.n_sockets; s++) {
563                 for (uint32_t i = 0; i < cpu_topo.n_cores[s]; ++i) {
564                         if ((uint32_t)cpu_topo.socket[s][i][0] == lcore_id) {
565                                 snprintf(dst, len, "%us%u", i, s);
566                                 return 0;
567                         } else if ((uint32_t)cpu_topo.socket[s][i][1] == lcore_id) {
568                                 snprintf(dst, len, "%us%uh", i, s);
569                                 return 0;
570                         }
571                 }
572         }
573
574         return -1;
575 }
576
577 static int get_lcore_id(uint32_t socket_id, uint32_t core_id, int ht)
578 {
579         if (cpu_topo.n_sockets == 0) {
580                 if (read_cpu_topology() == -1) {
581                         return -1;
582                 }
583         }
584
585         if (socket_id == UINT32_MAX)
586                 socket_id = 0;
587
588         if (socket_id >= MAX_SOCKETS) {
589                 set_errf("Socket id %d too high (max allowed is %d)", MAX_SOCKETS);
590                 return -1;
591         }
592         if (core_id >= RTE_MAX_LCORE) {
593                 set_errf("Core id %d too high (max allowed is %d)", RTE_MAX_LCORE);
594                 return -1;
595         }
596         if (socket_id >= cpu_topo.n_sockets) {
597                 set_errf("Current CPU topology reported that there are %u CPU sockets, CPU topology = %u socket(s), %u physical cores per socket, %u thread(s) per physical core",
598                          cpu_topo.n_sockets, cpu_topo.n_sockets, cpu_topo.n_cores[0], cpu_topo.socket[0][0][1] == -1? 1: 2);
599                 return -1;
600         }
601         if (core_id >= cpu_topo.n_cores[socket_id]) {
602                 set_errf("Core %u on socket %u does not exist, CPU topology = %u socket(s), %u physical cores per socket, %u thread(s) per physical core",
603                          core_id, socket_id, cpu_topo.n_sockets, cpu_topo.n_cores[0], cpu_topo.socket[socket_id][0][1] == -1? 1: 2);
604                 return -1;
605         }
606         if (cpu_topo.socket[socket_id][core_id][!!ht] == -1) {
607                 set_errf("Core %u %son socket %u has no hyper-thread, CPU topology = %u socket(s), %u physical cores per socket, %u thread(s) per physical core",
608                          core_id, ht ? "(hyper-thread) " : "", socket_id, cpu_topo.n_sockets, cpu_topo.n_cores[0], cpu_topo.socket[socket_id][core_id][1] == -1? 1: 2);
609
610                 return -1;
611         }
612         return cpu_topo.socket[socket_id][core_id][!!ht];
613 }
614
615 /* Returns 0 on success, negative on error. Parses the syntax XsYh
616    where sYh is optional. If sY is specified, Y is stored in the
617    socket argument. If, in addition, h is specified, *ht is set to
618    1. In case the input is only a number, socket and ht are set to
619    -1.*/
620 static int parse_core(int *socket, int *core, int *ht, const char* str)
621 {
622         *socket = -1;
623         *core = -1;
624         *ht = -1;
625
626         char* end;
627
628         *core = strtol(str, &end, 10);
629
630         if (*end == 's') {
631                 *socket = 0;
632                 *ht = 0;
633
634                 if (cpu_topo.n_sockets == 0) {
635                         if (read_cpu_topology() == -1) {
636                                 return -1;
637                         }
638                 }
639
640                 ++end;
641                 *socket = strtol(end, &end, 10);
642                 if (*socket >= MAX_SOCKETS) {
643                         set_errf("Socket id %d too high (max allowed is %d)", *socket, MAX_SOCKETS - 1);
644                         return -1;
645                 }
646
647                 if (*end == 'h') {
648                         ++end;
649                         *ht = 1;
650                 }
651
652                 return 0;
653         }
654
655         if (*end == 'h') {
656                 set_errf("Can't find hyper-thread since socket has not been specified");
657                 return -1;
658         }
659
660         return 0;
661 }
662
663 static int parse_task(const char *str, uint32_t *socket, uint32_t *core, uint32_t *task, uint32_t *ht, enum ctrl_type *type)
664 {
665         const char *str_beg = str;
666         char *end;
667
668         *core = strtol(str, &end, 10);
669         if (str == end) {
670                 set_errf("Expected number to in core-task definition:\n"
671                          "\t(i.e. 5s1t0 for task 0 on core 5 on socket 1)\n"
672                          "\tHave: '%s'.", end);
673                 return -1;
674         }
675
676         *task = 0;
677         *socket = -1;
678         *ht = -1;
679         *type = 0;
680
681         str = end;
682
683         if (*str == 's') {
684                 str++;
685                 *socket = 0;
686                 *ht = 0;
687
688                 *socket = strtol(str, &end, 10);
689                 str = end;
690
691                 if (*str == 'h') {
692                         str++;
693                         *ht = 1;
694                 }
695                 if (*str == 't') {
696                         str++;
697                         *task = strtol(str, &end, 10);
698                         str = end;
699                         if (*str == 'p') {
700                                 *type = CTRL_TYPE_PKT;
701                                 str += 1;
702                         }
703                         else if (*str == 'm') {
704                                 *type = CTRL_TYPE_MSG;
705                                 str += 1;
706                         }
707                 }
708         } else {
709                 if (*str == 'h') {
710                         set_errf("Can't find hyper-thread since socket has not been specified");
711                         return -1;
712                 }
713                 if (*str == 't') {
714                         str++;
715                         *task = strtol(str, &end, 10);
716                         str = end;
717                         if (*str == 'p') {
718                                 *type = CTRL_TYPE_PKT;
719                                 str += 1;
720                         }
721                         else if (*str == 'm') {
722                                 *type = CTRL_TYPE_MSG;
723                                 str += 1;
724                         }
725                 }
726         }
727         return str - str_beg;
728 }
729
730 static int core_task_set_add(struct core_task_set *val, uint32_t core, uint32_t task, enum ctrl_type type)
731 {
732         if (val->n_elems == sizeof(val->core_task)/sizeof(val->core_task[0]))
733                 return -1;
734
735         val->core_task[val->n_elems].core = core;
736         val->core_task[val->n_elems].task = task;
737         val->core_task[val->n_elems].type = type;
738         val->n_elems++;
739
740         return 0;
741 }
742
743 int parse_task_set(struct core_task_set *cts, const char *str2)
744 {
745         char str[MAX_STR_LEN_PROC];
746
747         if (parse_vars(str, sizeof(str), str2))
748                 return -1;
749         cts->n_elems = 0;
750
751         char *str3 = str;
752         int ret;
753
754         uint32_t socket_beg, core_beg, task_beg, ht_beg,
755                 socket_end, core_end, task_end, ht_end;
756         enum ctrl_type type_beg, type_end;
757         uint32_t task_group_start = -1;
758
759         while (*str3 && *str3 != ' ') {
760                 if (*str3 == '(') {
761                         task_group_start = cts->n_elems;
762                         str3 += 1;
763                         continue;
764                 }
765                 if (*str3 == ')' && *(str3 + 1) == 't') {
766                         str3 += 2;
767                         char *end;
768                         uint32_t t = strtol(str3, &end, 10);
769                         enum ctrl_type type = 0;
770                         str3 = end;
771
772                         if (*str3 == 'p') {
773                                 type = CTRL_TYPE_PKT;
774                                 str3 += 1;
775                         }
776                         else if (*str3 == 'm') {
777                                 type = CTRL_TYPE_MSG;
778                                 str3 += 1;
779                         }
780
781                         for (uint32_t i = task_group_start; i < cts->n_elems; ++i) {
782                                 cts->core_task[i].task = t;
783                                 cts->core_task[i].type = type;
784                         }
785                         continue;
786                 }
787                 ret = parse_task(str3, &socket_beg, &core_beg, &task_beg, &ht_beg, &type_beg);
788                 if (ret < 0)
789                         return -1;
790                 str3 += ret;
791                 socket_end = socket_beg;
792                 core_end = core_beg;
793                 task_end = task_beg;
794                 ht_end = ht_beg;
795                 type_end = type_beg;
796
797                 if (*str3 == '-') {
798                         str3 += 1;
799                         ret = parse_task(str3, &socket_end, &core_end, &task_end, &ht_end, &type_end);
800                         if (ret < 0)
801                                 return -1;
802                         str3 += ret;
803                 }
804
805                 if (*str3 == ',')
806                         str3 += 1;
807
808                 if (socket_end != socket_beg) {
809                         set_errf("Same socket must be used in range syntax.");
810                         return -1;
811                 } else if (ht_beg != ht_end) {
812                         set_errf("If 'h' syntax is in range, it must be specified everywhere.\n");
813                         return -1;
814                 } else if (task_end != task_beg && core_end != core_beg) {
815                         set_errf("Same task must be used in range syntax when cores are different.\n");
816                         return -1;
817                 } else if (task_end < task_beg) {
818                         set_errf("Task for end of range must be higher than task for beginning of range.\n");
819                         return -1;
820                 } else if (type_end != type_beg) {
821                         set_errf("Task type for end of range must be the same as  task type for beginning.\n");
822                         return -1;
823                 } else if (core_end < core_beg) {
824                         set_errf("Core for end of range must be higher than core for beginning of range.\n");
825                         return -1;
826                 }
827
828                 for (uint32_t j = core_beg; j <= core_end; ++j) {
829                         if (socket_beg != UINT32_MAX && ht_beg != UINT32_MAX)
830                                 ret = get_lcore_id(socket_beg, j, ht_beg);
831                         else
832                                 ret = j;
833                         if (ret < 0)
834                                 return -1;
835                         for (uint32_t k = task_beg; k <= task_end; ++k) {
836                                 core_task_set_add(cts, ret, k, type_beg);
837                         }
838                 }
839         }
840         return 0;
841 }
842
843 int parse_list_set(uint32_t *list, const char *str2, uint32_t max_list)
844 {
845         char str[MAX_STR_LEN_PROC];
846         char *parts[MAX_STR_LEN_PROC];
847
848         if (parse_vars(str, sizeof(str), str2))
849                 return -1;
850
851         int n_parts = rte_strsplit(str, strlen(str), parts, MAX_STR_LEN_PROC, ',');
852         size_t list_count = 0;
853
854         for (int i = 0; i < n_parts; ++i) {
855                 char *cur_part = parts[i];
856                 char *sub_parts[3];
857                 int n_sub_parts = rte_strsplit(cur_part, strlen(cur_part), sub_parts, 3, '-');
858                 int socket1, socket2;
859                 int ht1, ht2;
860                 int core1, core2;
861                 int ret = 0;
862
863                 if (n_sub_parts == 1) {
864                         if (parse_core(&socket1, &core1, &ht1, sub_parts[0]))
865                                 return -1;
866
867                         socket2 = socket1;
868                         core2 = core1;
869                         ht2 = ht1;
870                 } else if (n_sub_parts == 2) {
871                         if (parse_core(&socket1, &core1, &ht1, sub_parts[0]))
872                                 return -1;
873                         if (parse_core(&socket2, &core2, &ht2, sub_parts[1]))
874                                 return -1;
875                 } else if (n_sub_parts >= 3) {
876                         set_errf("Multiple '-' characters in range syntax found");
877                         return -1;
878                 } else {
879                         set_errf("Invalid list syntax");
880                         return -1;
881                 }
882
883                 if (socket1 != socket2) {
884                         set_errf("Same socket must be used in range syntax");
885                         return -1;
886                 }
887                 else if (ht1 != ht2) {
888                         set_errf("If 'h' syntax is in range, it must be specified everywhere.");
889                         return -1;
890                 }
891
892                 for (int cur_core = core1; cur_core <= core2; ++cur_core) {
893                         int effective_core;
894
895                         if (socket1 != -1)
896                                 effective_core = get_lcore_id(socket1, cur_core, ht1);
897                         else
898                                 effective_core = cur_core;
899
900                         if (list_count >= max_list) {
901                                 set_errf("Too many elements in list");
902                                 return -1;
903                         }
904                         list[list_count++] = effective_core;
905                 }
906         }
907
908         return list_count;
909 }
910
911 int parse_kmg(uint32_t* val, const char *str2)
912 {
913         char str[MAX_STR_LEN_PROC];
914
915         if (parse_vars(str, sizeof(str), str2))
916                 return -1;
917
918         char c = str[strlen(str) - 1];
919         *val = atoi(str);
920
921         switch (c) {
922         case 'G':
923                 if (*val >> 22)
924                         return -2;
925                 *val <<= 10;
926                 // __attribute__ ((fallthrough));
927         case 'M':
928                 if (*val >> 22)
929                         return -2;
930                 *val <<= 10;
931                 // __attribute__ ((fallthrough));
932         case 'K':
933                 if (*val >> 22)
934                         return -2;
935                 *val <<= 10;
936                 break;
937         default:
938                 /* only support optional KMG suffix */
939                 if (c < '0' || c > '9') {
940                         set_errf("Unknown syntax for KMG suffix '%c' (expected K, M or G)", c);
941                         return -1;
942                 }
943         }
944
945         return 0;
946 }
947
948 int parse_bool(uint32_t* val, const char *str2)
949 {
950         char str[MAX_STR_LEN_PROC];
951
952         if (parse_vars(str, sizeof(str), str2))
953                 return -1;
954
955         if (!strcmp(str, "yes")) {
956                 *val = 1;
957                 return 0;
958         }
959         else if (!strcmp(str, "no")) {
960                 *val = 0;
961                 return 0;
962         }
963         set_errf("Unknown syntax for bool '%s' (expected yes or no)", str);
964         return -1;
965 }
966
967 int parse_flag(uint32_t* val, uint32_t flag, const char *str2)
968 {
969         char str[MAX_STR_LEN_PROC];
970
971         if (parse_vars(str, sizeof(str), str2))
972                 return -1;
973
974         uint32_t tmp;
975         if (parse_bool(&tmp, str))
976                 return -1;
977
978         if (tmp)
979                 *val |= flag;
980         else
981                 *val &= ~flag;
982
983         return 0;
984 }
985
986 int parse_int(uint32_t* val, const char *str2)
987 {
988         char str[MAX_STR_LEN_PROC];
989
990         if (parse_vars(str, sizeof(str), str2))
991                 return -1;
992
993         int64_t tmp = strtol(str, 0, 0);
994         if (tmp > UINT32_MAX) {
995                 set_errf("Integer is bigger than %u", UINT32_MAX);
996                 return -1;
997         }
998         if (tmp < 0) {
999                 set_errf("Integer is negative");
1000                 return -2;
1001         }
1002         *val = tmp;
1003
1004         return 0;
1005 }
1006
1007 int parse_float(float* val, const char *str2)
1008 {
1009         char str[MAX_STR_LEN_PROC];
1010
1011         if (parse_vars(str, sizeof(str), str2))
1012                 return -1;
1013
1014         float tmp = strtof(str, 0);
1015         if ((tmp >= HUGE_VALF) || (tmp <= -HUGE_VALF)) {
1016                 set_errf("Unable to parse float\n");
1017                 return -1;
1018         }
1019         *val = tmp;
1020
1021         return 0;
1022 }
1023
1024 int parse_u64(uint64_t* val, const char *str2)
1025 {
1026         char str[MAX_STR_LEN_PROC];
1027
1028         if (parse_vars(str, sizeof(str), str2))
1029                 return -1;
1030
1031         errno = 0;
1032         uint64_t tmp = strtoul(str, NULL, 0);
1033         if (errno != 0) {
1034                 set_errf("Invalid u64 '%s' (%s)", str, strerror(errno));
1035                 return -2;
1036         }
1037         *val = tmp;
1038
1039         return 0;
1040 }
1041
1042 int parse_str(char* dst, const char *str2, size_t max_len)
1043 {
1044         char str[MAX_STR_LEN_PROC];
1045
1046         if (parse_vars(str, sizeof(str), str2))
1047                 return -1;
1048
1049         if (strlen(str) > max_len - 1) {
1050                 set_errf("String too long (%u > %u)", strlen(str), max_len - 1);
1051                 return -2;
1052         }
1053
1054         prox_strncpy(dst, str, max_len);
1055         return 0;
1056 }
1057
1058 int parse_path(char *dst, const char *str, size_t max_len)
1059 {
1060         if (parse_str(dst, str, max_len))
1061                 return -1;
1062         if (access(dst, F_OK)) {
1063                 set_errf("Invalid file '%s' (%s)", dst, strerror(errno));
1064                 return -1;
1065         }
1066         return 0;
1067 }
1068
1069 int parse_port_name(uint32_t *val, const char *str2)
1070 {
1071         char str[MAX_STR_LEN_PROC];
1072
1073         if (parse_vars(str, sizeof(str), str2))
1074                 return -1;
1075
1076         for (uint8_t i = 0; i < nb_port_names; ++i) {
1077                 if (!strcmp(str, port_names[i].name)) {
1078                         *val = port_names[i].id;
1079                         return 0;
1080                 }
1081         }
1082         set_errf("Port with name %s not defined", str);
1083         return 1;
1084 }
1085
1086 int parse_port_name_list(uint32_t *val, uint32_t* tot, uint8_t max_vals, const char *str2)
1087 {
1088         char *elements[PROX_MAX_PORTS + 1];
1089         char str[MAX_STR_LEN_PROC];
1090         uint32_t cur;
1091         int ret;
1092
1093         if (parse_str(str, str2, sizeof(str)))
1094                 return -1;
1095
1096         ret = rte_strsplit(str, strlen(str), elements, PROX_MAX_PORTS + 1, ',');
1097
1098         if (ret == PROX_MAX_PORTS + 1 || ret > max_vals) {
1099                 set_errf("Too many ports in port list");
1100                 return -1;
1101         }
1102
1103         strip_spaces(elements, ret);
1104         for (uint8_t i = 0; i < ret; ++i) {
1105                 if (parse_port_name(&cur, elements[i])) {
1106                         return -1;
1107                 }
1108                 val[i] = cur;
1109         }
1110         if (tot) {
1111                 *tot = ret;
1112         }
1113         return 0;
1114 }
1115
1116 int parse_remap(uint8_t *mapping, const char *str)
1117 {
1118         char *elements[PROX_MAX_PORTS + 1];
1119         char *elements2[PROX_MAX_PORTS + 1];
1120         char str_cpy[MAX_STR_LEN_PROC];
1121         uint32_t val;
1122         int ret, ret2;
1123
1124         if (strlen(str) > MAX_STR_LEN_PROC) {
1125                 set_errf("String too long (max supported: %d)", MAX_STR_LEN_PROC);
1126                 return -2;
1127         }
1128         prox_strncpy(str_cpy, str, MAX_STR_LEN_PROC);
1129
1130         ret = rte_strsplit(str_cpy, strlen(str_cpy), elements, PROX_MAX_PORTS + 1, ',');
1131         if (ret <= 0) {
1132                 set_errf("Invalid remap syntax");
1133                 return -1;
1134         }
1135         else if (ret > PROX_MAX_PORTS) {
1136                 set_errf("Too many remaps");
1137                 return -2;
1138         }
1139
1140         strip_spaces(elements, ret);
1141         for (uint8_t i = 0; i < ret; ++i) {
1142                 ret2 = rte_strsplit(elements[i], strlen(elements[i]), elements2, PROX_MAX_PORTS + 1, '|');
1143                 strip_spaces(elements2, ret2);
1144                 if (ret2 > PROX_MAX_PORTS) {
1145                         set_errf("Too many remaps");
1146                         return -2;
1147                 }
1148                 for (uint8_t j = 0; j < ret2; ++j) {
1149                         if (parse_port_name(&val, elements2[j])) {
1150                                 return -1;
1151                         }
1152
1153                         /* This port will be mapped to the i'th
1154                            element specified before remap=. */
1155                         mapping[val] = i;
1156                 }
1157         }
1158
1159         return ret;
1160 }
1161
1162 int add_port_name(uint32_t val, const char *str2)
1163 {
1164         char str[MAX_STR_LEN_PROC];
1165
1166         if (parse_vars(str, sizeof(str), str2))
1167                 return -1;
1168
1169         struct port_name* pn;
1170
1171         if (nb_port_names == MAX_NB_PORT_NAMES) {
1172                 set_errf("Too many ports defined (can define %d)", MAX_NB_PORT_NAMES);
1173                 return -1;
1174         }
1175
1176         for (uint8_t i = 0; i < nb_port_names; ++i) {
1177                 /* each port has to have a unique name*/
1178                 if (!strcmp(str, port_names[i].name)) {
1179                         set_errf("Port with name %s is already defined", str);
1180                         return -2;
1181                 }
1182         }
1183
1184         pn = &port_names[nb_port_names];
1185         prox_strncpy(pn->name, str, sizeof(pn->name));
1186         pn->id = val;
1187
1188         ++nb_port_names;
1189         return 0;
1190 }
1191
1192 int set_self_var(const char *str)
1193 {
1194         for (uint8_t i = 0; i < nb_vars; ++i) {
1195                 if (!strcmp("$self", vars[i].name)) {
1196                         sprintf(vars[i].val, "%s", str);
1197                         return 0;
1198                 }
1199         }
1200
1201         struct var *v = &vars[nb_vars];
1202
1203         prox_strncpy(v->name, "$self", strlen("$self") + 1);
1204         sprintf(v->val, "%s", str);
1205         nb_vars++;
1206
1207         return 0;
1208 }
1209
1210 int add_var(const char* name, const char *str2, uint8_t cli)
1211 {
1212         struct var* v;
1213
1214         char str[MAX_STR_LEN_PROC];
1215
1216         if (parse_vars(str, sizeof(str), str2))
1217                 return -1;
1218
1219         if (strlen(name) == 0 || strlen(name) == 1) {
1220                 set_errf("Can't define variables with empty name");
1221                 return -1;
1222         }
1223
1224         if (name[0] != '$') {
1225                 set_errf("Each variable should start with the $ character");
1226                 return -1;
1227         }
1228
1229         if (nb_vars == MAX_NB_VARS) {
1230                 set_errf("Too many variables defined (can define %d)", MAX_NB_VARS);
1231                 return -2;
1232         }
1233
1234         for (uint8_t i = 0; i < nb_vars; ++i) {
1235                 if (!strcmp(name, vars[i].name)) {
1236
1237                         /* Variables defined through program arguments
1238                            take precedence. */
1239                         if (!cli && vars[i].cli) {
1240                                 return 0;
1241                         }
1242
1243                         set_errf("Variable with name %s is already defined", name);
1244                         return -3;
1245                 }
1246         }
1247
1248         v = &vars[nb_vars];
1249         PROX_PANIC(strlen(name) > sizeof(v->name), "\tUnable to parse var %s: too long\n", name);
1250         PROX_PANIC(strlen(str) > sizeof(v->val), "\tUnable to parse var %s=%s: too long\n", name,str);
1251         prox_strncpy(v->name, name, sizeof(v->name));
1252         prox_strncpy(v->val, str, sizeof(v->val));
1253         v->cli = cli;
1254
1255         ++nb_vars;
1256         return 0;
1257 }
1258
1259 static int read_cores_present(uint32_t *cores, int max_cores, int *res)
1260 {
1261         FILE* fd = fopen("/sys/devices/system/cpu/present", "r");
1262         char buf[1024];
1263
1264         if (fd == NULL) {
1265                 set_errf("Could not opening file /sys/devices/system/cpu/present");
1266                 return -1;
1267         }
1268
1269         if (fgets(buf, sizeof(buf), fd) == NULL) {
1270                 set_errf("Could not read cores range");
1271                 return -1;
1272         }
1273
1274         fclose(fd);
1275
1276         int ret = parse_list_set(cores, buf, max_cores);
1277
1278         if (ret < 0)
1279                 return -1;
1280
1281         *res = ret;
1282         return 0;
1283 }
1284
1285 static int set_dummy_topology(void)
1286 {
1287         int core_count = 0;
1288
1289         for (int s = 0; s < MAX_SOCKETS; s++) {
1290                 for (int i = 0; i < 32; ++i) {
1291                         cpu_topo.socket[s][i][0] = core_count++;
1292                         cpu_topo.socket[s][i][1] = core_count++;
1293                         cpu_topo.n_cores[s]++;
1294                 }
1295         }
1296         cpu_topo.n_sockets = MAX_SOCKETS;
1297         return 0;
1298 }
1299
1300 static int read_cpu_topology(void)
1301 {
1302         if (cpu_topo.n_sockets != 0)
1303                 return 0;
1304         if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO)
1305                 return set_dummy_topology();
1306
1307         uint32_t cores[RTE_MAX_LCORE];
1308         int n_cores = 0;
1309
1310         if (read_cores_present(cores, sizeof(cores)/sizeof(cores[0]), &n_cores) != 0)
1311                 return -1;
1312
1313         for (int s = 0; s < MAX_SOCKETS; s++) {
1314                 for (int i = 0; i < RTE_MAX_LCORE; ++i) {
1315                         cpu_topo.socket[s][i][0] = -1;
1316                         cpu_topo.socket[s][i][1] = -1;
1317                 }
1318         }
1319
1320         for (int i = 0; i < n_cores; ++i) {
1321                 uint32_t socket_id, lcore_id, phys;
1322
1323                 lcore_id = cores[i];
1324                 if (get_socket(lcore_id, &socket_id) != 0)
1325                         return -1;
1326                 if (socket_id >= MAX_SOCKETS) {
1327                         set_errf("Can't read CPU topology due too high socket ID (max allowed is %d)",
1328                                  MAX_SOCKETS);
1329                         return -1;
1330                 }
1331                 if (socket_id >= cpu_topo.n_sockets) {
1332                         cpu_topo.n_sockets = socket_id + 1;
1333                 }
1334                 if (get_phys_core(&phys, lcore_id) != 0)
1335                         return -1;
1336                 if (phys >= RTE_MAX_LCORE) {
1337                         set_errf("Core ID %u too high", phys);
1338                         return -1;
1339                 }
1340
1341                 if (cpu_topo.socket[socket_id][phys][0] == -1) {
1342                         cpu_topo.socket[socket_id][phys][0] = lcore_id;
1343                         cpu_topo.n_cores[socket_id]++;
1344                 }
1345                 else if (cpu_topo.socket[socket_id][phys][1] == -1) {
1346                         cpu_topo.socket[socket_id][phys][1] = lcore_id;
1347                 }
1348                 else {
1349                         set_errf("Too many core siblings");
1350                         return -1;
1351                 }
1352         }
1353
1354         /* There can be holes in the cpu_topo description at this
1355            point. An example for this is a CPU topology where the
1356            lowest core ID of 2 hyper-threads is always an even
1357            number. Before finished up this phase, compact all the
1358            cores to make the numbers consecutive. */
1359
1360         for (uint32_t i = 0; i < cpu_topo.n_sockets; ++i) {
1361                 int spread = 0, compact = 0;
1362                 while (cpu_topo.socket[i][spread][0] == -1)
1363                         spread++;
1364
1365                 for (uint32_t c = 0; c < cpu_topo.n_cores[i]; ++c) {
1366                         cpu_topo.socket[i][compact][0] = cpu_topo.socket[i][spread][0];
1367                         cpu_topo.socket[i][compact][1] = cpu_topo.socket[i][spread][1];
1368                         compact++;
1369                         spread++;
1370                         /* Skip gaps */
1371                         while (cpu_topo.socket[i][spread][0] == -1)
1372                                 spread++;
1373                 }
1374         }
1375
1376         return 0;
1377 }
1378
1379 static int bit_len_valid(uint32_t len, const char *str)
1380 {
1381         if (len > 32) {
1382                 set_errf("Maximum random length is 32, but length of '%s' is %zu\n", str, len);
1383                 return 0;
1384         }
1385         if (len % 8) {
1386                 plog_err("Random should be multiple of 8 long\n");
1387                 return 0;
1388         }
1389         if (len == 0) {
1390                 plog_err("Random should be at least 1 byte long\n");
1391                 return 0;
1392         }
1393         return -1;
1394 }
1395
1396 int parse_random_str(uint32_t *mask, uint32_t *fixed, uint32_t *len, const char *str)
1397 {
1398         const size_t len_bits = strlen(str);
1399
1400         if (!bit_len_valid(len_bits, str))
1401                 return -1;
1402
1403         *mask = 0;
1404         *fixed = 0;
1405         *len = len_bits / 8;
1406
1407         for (uint32_t j = 0; j < len_bits; ++j) {
1408                 /* Store in the lower bits the value of the rand string (note
1409                    that these are the higher bits in LE). */
1410                 switch (str[j]) {
1411                 case 'X':
1412                         *mask |= 1 << (len_bits - 1 - j);
1413                         break;
1414                 case '1':
1415                         *fixed |= 1 << (len_bits - 1 - j);
1416                         break;
1417                 case '0':
1418                         break;
1419                 default:
1420                         set_errf("Unexpected %c\n", str[j]);
1421                         return -1;
1422                 }
1423         }
1424         return 0;
1425 }