9ceb1c5979e29100253ff48a13536195e690d17a
[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 "parse_utils.h"
31 #include "prox_globals.h"
32 #include "prox_cfg.h"
33 #include "log.h"
34 #include "prox_lua.h"
35 #include "prox_lua_types.h"
36 #include "prox_ipv6.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_and_prefix(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         return 0;
345 }
346
347 int parse_ip4_cidr(struct ip4_subnet *val, const char *str2)
348 {
349         int rc = parse_ip4_and_prefix(val, str2);
350         /* Apply mask making all bits outside the prefix zero */
351         val->ip &= ((int)(1 << 31)) >> (val->prefix - 1);
352
353         return rc;
354 }
355
356 int parse_ip6_cidr(struct ip6_subnet *val, const char *str2)
357 {
358         char str[MAX_STR_LEN_PROC];
359         char *slash;
360         int prefix;
361
362         if (parse_vars(str, sizeof(str), str2))
363                 return -1;
364
365         slash = strstr(str, "/");
366
367         if (slash == NULL) {
368                 set_errf("Missing '/' when parsing CIDR notation");
369                 return -2;
370         }
371
372         *slash = 0;
373         prefix = atoi(slash + 1);
374         val->prefix = prefix;
375
376         parse_ip6((struct ipv6_addr *)&val->ip, str);
377
378         /* Apply mask making all bits outside the prefix zero */
379
380         int p = 120;
381         int cnt = 0;
382
383         while (p >= prefix) {
384                 val->ip[15-cnt] = 0;
385                 p -= 8;
386                 cnt++;
387         }
388
389         if (prefix % 8 != 0) {
390                 val->ip[15-cnt] &= ((int8_t)(1 << 7)) >> ((prefix %8) - 1);
391         }
392
393         return 0;
394 }
395
396 int parse_ip6(struct ipv6_addr *addr, const char *str2)
397 {
398         char str[MAX_STR_LEN_PROC];
399         char *addr_parts[9];
400
401         if (parse_vars(str, sizeof(str), str2))
402                 return -1;
403
404         uint8_t ret = rte_strsplit(str, strlen(str), addr_parts, 9, ':');
405
406         if (ret == 9) {
407                 set_errf("Invalid IPv6 address");
408                 return -1;
409         }
410
411         uint8_t omitted = 0;
412
413         for (uint8_t i = 0, j = 0; i < ret; ++i, ++j) {
414                 if (*addr_parts[i] == 0) {
415                         if (omitted) {
416                                 set_errf("Can only omit zeros once");
417                                 return -1;
418                         }
419                         omitted = 1;
420                         j += 2 * (8 - ret) + 1;
421                 }
422                 else {
423                         uint16_t w = strtoll(addr_parts[i], NULL, 16);
424                         addr->bytes[j++] = (w >> 8) & 0xff;
425                         addr->bytes[j] = w & 0xff;
426                 }
427         }
428         return 0;
429 }
430
431 int parse_mac(prox_rte_ether_addr *ether_addr, const char *str2)
432 {
433         char str[MAX_STR_LEN_PROC];
434         char *addr_parts[7];
435
436         if (parse_vars(str, sizeof(str), str2))
437                 return -1;
438
439         uint8_t ret = rte_strsplit(str, strlen(str), addr_parts, 7, ':');
440         if (ret != 6)
441                 ret = rte_strsplit(str, strlen(str), addr_parts, 7, ' ');
442
443         if (ret != 6) {
444                 set_errf("Invalid MAC address format");
445                 return -1;
446         }
447
448         for (uint8_t i = 0; i < 6; ++i) {
449                 if (2 != strlen(addr_parts[i])) {
450                         set_errf("Invalid MAC address format");
451                         return -1;
452                 }
453                 ether_addr->addr_bytes[i] = strtol(addr_parts[i], NULL, 16);
454         }
455
456         return 0;
457 }
458
459 char* get_cfg_key(char *str)
460 {
461         char *pkey = strchr(str, '=');
462
463         if (pkey == NULL) {
464                 return NULL;
465         }
466         *pkey++ = '\0';
467
468         /* remove leading spaces */
469         while (isspace(*pkey)) {
470                 pkey++;
471         }
472         if (*pkey == '\0') { /* an empty key */
473                 return NULL;
474         }
475
476         return pkey;
477 }
478
479 void strip_spaces(char *strings[], const uint32_t count)
480 {
481         for (uint32_t i = 0; i < count; ++i) {
482                 while (isspace(strings[i][0])) {
483                         ++strings[i];
484                 }
485                 size_t len = strlen(strings[i]);
486
487                 while (len && isspace(strings[i][len - 1])) {
488                         strings[i][len - 1] = '\0';
489                         --len;
490                 }
491         }
492 }
493
494 int is_virtualized(void)
495 {
496         char buf[1024]= "/proc/cpuinfo";
497         int virtualized = 0;
498         FILE* fd = fopen(buf, "r");
499         if (fd == NULL) {
500                 set_errf("Could not open %s", buf);
501                 return -1;
502         }
503         while (fgets(buf, sizeof(buf), fd) != NULL) {
504                 if ((strstr(buf, "flags") != NULL) && (strstr(buf, "hypervisor") != NULL))
505                         virtualized = 1;
506         }
507         fclose(fd);
508         return virtualized;
509 }
510
511 static int get_phys_core(uint32_t *dst, int lcore_id)
512 {
513         uint32_t ret;
514         char buf[1024];
515         snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcore_id);
516         FILE* ht_fd = fopen(buf, "r");
517
518         if (ht_fd == NULL) {
519                 set_errf("Could not open cpu topology %s", buf);
520                 return -1;
521         }
522
523         if (fgets(buf, sizeof(buf), ht_fd) == NULL) {
524                 set_errf("Could not read cpu topology");
525                 return -1;
526         }
527         fclose(ht_fd);
528
529         uint32_t list[2] = {-1,-1};
530         parse_list_set(list, buf, 2);
531
532         *dst = list[0];
533
534         return 0;
535 }
536
537 static int get_socket(uint32_t core_id, uint32_t *socket)
538 {
539         int ret = -1;
540         char buf[1024];
541         snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu%u/topology/physical_package_id", core_id);
542         FILE* fd = fopen(buf, "r");
543
544         if (fd == NULL) {
545                 set_errf("%s", buf);
546                 return -1;
547         }
548
549         if (fgets(buf, sizeof(buf), fd) != NULL) {
550                 ret = atoi(buf);
551         }
552         fclose(fd);
553
554         if (socket)
555                 *socket = (ret == -1 ? 0 : ret);
556
557         return 0;
558 }
559
560 int lcore_to_socket_core_ht(uint32_t lcore_id, char *dst, size_t len)
561 {
562         if (cpu_topo.n_sockets == 0) {
563                 if (read_cpu_topology() == -1) {
564                         return -1;
565                 }
566         }
567
568         for (uint32_t s = 0; s < cpu_topo.n_sockets; s++) {
569                 for (uint32_t i = 0; i < cpu_topo.n_cores[s]; ++i) {
570                         if ((uint32_t)cpu_topo.socket[s][i][0] == lcore_id) {
571                                 snprintf(dst, len, "%us%u", i, s);
572                                 return 0;
573                         } else if ((uint32_t)cpu_topo.socket[s][i][1] == lcore_id) {
574                                 snprintf(dst, len, "%us%uh", i, s);
575                                 return 0;
576                         }
577                 }
578         }
579
580         return -1;
581 }
582
583 static int get_lcore_id(uint32_t socket_id, uint32_t core_id, int ht)
584 {
585         if (cpu_topo.n_sockets == 0) {
586                 if (read_cpu_topology() == -1) {
587                         return -1;
588                 }
589         }
590
591         if (socket_id == UINT32_MAX)
592                 socket_id = 0;
593
594         if (socket_id >= MAX_SOCKETS) {
595                 set_errf("Socket id %d too high (max allowed is %d)", MAX_SOCKETS);
596                 return -1;
597         }
598         if (core_id >= RTE_MAX_LCORE) {
599                 set_errf("Core id %d too high (max allowed is %d)", RTE_MAX_LCORE);
600                 return -1;
601         }
602         if (socket_id >= cpu_topo.n_sockets) {
603                 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",
604                          cpu_topo.n_sockets, cpu_topo.n_sockets, cpu_topo.n_cores[0], cpu_topo.socket[0][0][1] == -1? 1: 2);
605                 return -1;
606         }
607         if (core_id >= cpu_topo.n_cores[socket_id]) {
608                 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",
609                          core_id, socket_id, cpu_topo.n_sockets, cpu_topo.n_cores[0], cpu_topo.socket[socket_id][0][1] == -1? 1: 2);
610                 return -1;
611         }
612         if (cpu_topo.socket[socket_id][core_id][!!ht] == -1) {
613                 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",
614                          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);
615
616                 return -1;
617         }
618         return cpu_topo.socket[socket_id][core_id][!!ht];
619 }
620
621 /* Returns 0 on success, negative on error. Parses the syntax XsYh
622    where sYh is optional. If sY is specified, Y is stored in the
623    socket argument. If, in addition, h is specified, *ht is set to
624    1. In case the input is only a number, socket and ht are set to
625    -1.*/
626 static int parse_core(int *socket, int *core, int *ht, const char* str)
627 {
628         *socket = -1;
629         *core = -1;
630         *ht = -1;
631
632         char* end;
633
634         *core = strtol(str, &end, 10);
635
636         if (*end == 's') {
637                 *socket = 0;
638                 *ht = 0;
639
640                 if (cpu_topo.n_sockets == 0) {
641                         if (read_cpu_topology() == -1) {
642                                 return -1;
643                         }
644                 }
645
646                 ++end;
647                 *socket = strtol(end, &end, 10);
648                 if (*socket >= MAX_SOCKETS) {
649                         set_errf("Socket id %d too high (max allowed is %d)", *socket, MAX_SOCKETS - 1);
650                         return -1;
651                 }
652
653                 if (*end == 'h') {
654                         ++end;
655                         *ht = 1;
656                 }
657
658                 return 0;
659         }
660
661         if (*end == 'h') {
662                 set_errf("Can't find hyper-thread since socket has not been specified");
663                 return -1;
664         }
665
666         return 0;
667 }
668
669 static int parse_task(const char *str, uint32_t *socket, uint32_t *core, uint32_t *task, uint32_t *ht, enum ctrl_type *type)
670 {
671         const char *str_beg = str;
672         char *end;
673
674         *core = strtol(str, &end, 10);
675         if (str == end) {
676                 set_errf("Expected number to in core-task definition:\n"
677                          "\t(i.e. 5s1t0 for task 0 on core 5 on socket 1)\n"
678                          "\tHave: '%s'.", end);
679                 return -1;
680         }
681
682         *task = 0;
683         *socket = -1;
684         *ht = -1;
685         *type = 0;
686
687         str = end;
688
689         if (*str == 's') {
690                 str++;
691                 *socket = 0;
692                 *ht = 0;
693
694                 *socket = strtol(str, &end, 10);
695                 str = end;
696
697                 if (*str == 'h') {
698                         str++;
699                         *ht = 1;
700                 }
701                 if (*str == 't') {
702                         str++;
703                         *task = strtol(str, &end, 10);
704                         str = end;
705                         if (*str == 'p') {
706                                 *type = CTRL_TYPE_PKT;
707                                 str += 1;
708                         }
709                         else if (*str == 'm') {
710                                 *type = CTRL_TYPE_MSG;
711                                 str += 1;
712                         }
713                 }
714         } else {
715                 if (*str == 'h') {
716                         set_errf("Can't find hyper-thread since socket has not been specified");
717                         return -1;
718                 }
719                 if (*str == 't') {
720                         str++;
721                         *task = strtol(str, &end, 10);
722                         str = end;
723                         if (*str == 'p') {
724                                 *type = CTRL_TYPE_PKT;
725                                 str += 1;
726                         }
727                         else if (*str == 'm') {
728                                 *type = CTRL_TYPE_MSG;
729                                 str += 1;
730                         }
731                 }
732         }
733         return str - str_beg;
734 }
735
736 static int core_task_set_add(struct core_task_set *val, uint32_t core, uint32_t task, enum ctrl_type type)
737 {
738         if (val->n_elems == sizeof(val->core_task)/sizeof(val->core_task[0]))
739                 return -1;
740
741         val->core_task[val->n_elems].core = core;
742         val->core_task[val->n_elems].task = task;
743         val->core_task[val->n_elems].type = type;
744         val->n_elems++;
745
746         return 0;
747 }
748
749 int parse_task_set(struct core_task_set *cts, const char *str2)
750 {
751         char str[MAX_STR_LEN_PROC];
752
753         if (parse_vars(str, sizeof(str), str2))
754                 return -1;
755         cts->n_elems = 0;
756
757         char *str3 = str;
758         int ret;
759
760         uint32_t socket_beg, core_beg, task_beg, ht_beg,
761                 socket_end, core_end, task_end, ht_end;
762         enum ctrl_type type_beg, type_end;
763         uint32_t task_group_start = -1;
764
765         while (*str3 && *str3 != ' ') {
766                 if (*str3 == '(') {
767                         task_group_start = cts->n_elems;
768                         str3 += 1;
769                         continue;
770                 }
771                 if (*str3 == ')' && *(str3 + 1) == 't') {
772                         str3 += 2;
773                         char *end;
774                         uint32_t t = strtol(str3, &end, 10);
775                         enum ctrl_type type = 0;
776                         str3 = end;
777
778                         if (*str3 == 'p') {
779                                 type = CTRL_TYPE_PKT;
780                                 str3 += 1;
781                         }
782                         else if (*str3 == 'm') {
783                                 type = CTRL_TYPE_MSG;
784                                 str3 += 1;
785                         }
786
787                         for (uint32_t i = task_group_start; i < cts->n_elems; ++i) {
788                                 cts->core_task[i].task = t;
789                                 cts->core_task[i].type = type;
790                         }
791                         continue;
792                 }
793                 ret = parse_task(str3, &socket_beg, &core_beg, &task_beg, &ht_beg, &type_beg);
794                 if (ret < 0)
795                         return -1;
796                 str3 += ret;
797                 socket_end = socket_beg;
798                 core_end = core_beg;
799                 task_end = task_beg;
800                 ht_end = ht_beg;
801                 type_end = type_beg;
802
803                 if (*str3 == '-') {
804                         str3 += 1;
805                         ret = parse_task(str3, &socket_end, &core_end, &task_end, &ht_end, &type_end);
806                         if (ret < 0)
807                                 return -1;
808                         str3 += ret;
809                 }
810
811                 if (*str3 == ',')
812                         str3 += 1;
813
814                 if (socket_end != socket_beg) {
815                         set_errf("Same socket must be used in range syntax.");
816                         return -1;
817                 } else if (ht_beg != ht_end) {
818                         set_errf("If 'h' syntax is in range, it must be specified everywhere.\n");
819                         return -1;
820                 } else if (task_end != task_beg && core_end != core_beg) {
821                         set_errf("Same task must be used in range syntax when cores are different.\n");
822                         return -1;
823                 } else if (task_end < task_beg) {
824                         set_errf("Task for end of range must be higher than task for beginning of range.\n");
825                         return -1;
826                 } else if (type_end != type_beg) {
827                         set_errf("Task type for end of range must be the same as  task type for beginning.\n");
828                         return -1;
829                 } else if (core_end < core_beg) {
830                         set_errf("Core for end of range must be higher than core for beginning of range.\n");
831                         return -1;
832                 }
833
834                 for (uint32_t j = core_beg; j <= core_end; ++j) {
835                         if (socket_beg != UINT32_MAX && ht_beg != UINT32_MAX)
836                                 ret = get_lcore_id(socket_beg, j, ht_beg);
837                         else
838                                 ret = j;
839                         if (ret < 0)
840                                 return -1;
841                         for (uint32_t k = task_beg; k <= task_end; ++k) {
842                                 core_task_set_add(cts, ret, k, type_beg);
843                         }
844                 }
845         }
846         return 0;
847 }
848
849 int parse_list_set(uint32_t *list, const char *str2, uint32_t max_list)
850 {
851         char str[MAX_STR_LEN_PROC];
852         char *parts[MAX_STR_LEN_PROC];
853
854         if (parse_vars(str, sizeof(str), str2))
855                 return -1;
856
857         int n_parts = rte_strsplit(str, strlen(str), parts, MAX_STR_LEN_PROC, ',');
858         size_t list_count = 0;
859
860         for (int i = 0; i < n_parts; ++i) {
861                 char *cur_part = parts[i];
862                 char *sub_parts[3];
863                 int n_sub_parts = rte_strsplit(cur_part, strlen(cur_part), sub_parts, 3, '-');
864                 int socket1, socket2;
865                 int ht1, ht2;
866                 int core1, core2;
867                 int ret = 0;
868
869                 if (n_sub_parts == 1) {
870                         if (parse_core(&socket1, &core1, &ht1, sub_parts[0]))
871                                 return -1;
872
873                         socket2 = socket1;
874                         core2 = core1;
875                         ht2 = ht1;
876                 } else if (n_sub_parts == 2) {
877                         if (parse_core(&socket1, &core1, &ht1, sub_parts[0]))
878                                 return -1;
879                         if (parse_core(&socket2, &core2, &ht2, sub_parts[1]))
880                                 return -1;
881                 } else if (n_sub_parts >= 3) {
882                         set_errf("Multiple '-' characters in range syntax found");
883                         return -1;
884                 } else {
885                         set_errf("Invalid list syntax");
886                         return -1;
887                 }
888
889                 if (socket1 != socket2) {
890                         set_errf("Same socket must be used in range syntax");
891                         return -1;
892                 }
893                 else if (ht1 != ht2) {
894                         set_errf("If 'h' syntax is in range, it must be specified everywhere.");
895                         return -1;
896                 }
897
898                 for (int cur_core = core1; cur_core <= core2; ++cur_core) {
899                         int effective_core;
900
901                         if (socket1 != -1)
902                                 effective_core = get_lcore_id(socket1, cur_core, ht1);
903                         else
904                                 effective_core = cur_core;
905
906                         if (list_count >= max_list) {
907                                 set_errf("Too many elements in list");
908                                 return -1;
909                         }
910                         list[list_count++] = effective_core;
911                 }
912         }
913
914         return list_count;
915 }
916
917 int parse_kmg(uint32_t* val, const char *str2)
918 {
919         char str[MAX_STR_LEN_PROC];
920
921         if (parse_vars(str, sizeof(str), str2))
922                 return -1;
923
924         char c = str[strlen(str) - 1];
925         *val = atoi(str);
926
927         switch (c) {
928         case 'G':
929                 if (*val >> 22)
930                         return -2;
931                 *val <<= 10;
932                 // __attribute__ ((fallthrough));
933         case 'M':
934                 if (*val >> 22)
935                         return -2;
936                 *val <<= 10;
937                 // __attribute__ ((fallthrough));
938         case 'K':
939                 if (*val >> 22)
940                         return -2;
941                 *val <<= 10;
942                 break;
943         default:
944                 /* only support optional KMG suffix */
945                 if (c < '0' || c > '9') {
946                         set_errf("Unknown syntax for KMG suffix '%c' (expected K, M or G)", c);
947                         return -1;
948                 }
949         }
950
951         return 0;
952 }
953
954 int parse_bool(uint32_t* val, const char *str2)
955 {
956         char str[MAX_STR_LEN_PROC];
957
958         if (parse_vars(str, sizeof(str), str2))
959                 return -1;
960
961         if (!strcmp(str, "yes")) {
962                 *val = 1;
963                 return 0;
964         }
965         else if (!strcmp(str, "no")) {
966                 *val = 0;
967                 return 0;
968         }
969         set_errf("Unknown syntax for bool '%s' (expected yes or no)", str);
970         return -1;
971 }
972
973 int parse_flag(uint32_t* val, uint32_t flag, const char *str2)
974 {
975         char str[MAX_STR_LEN_PROC];
976
977         if (parse_vars(str, sizeof(str), str2))
978                 return -1;
979
980         uint32_t tmp;
981         if (parse_bool(&tmp, str))
982                 return -1;
983
984         if (tmp)
985                 *val |= flag;
986         else
987                 *val &= ~flag;
988
989         return 0;
990 }
991
992 int parse_int(uint32_t* val, const char *str2)
993 {
994         char str[MAX_STR_LEN_PROC];
995
996         if (parse_vars(str, sizeof(str), str2))
997                 return -1;
998
999         int64_t tmp = strtol(str, 0, 0);
1000         if (tmp > UINT32_MAX) {
1001                 set_errf("Integer is bigger than %u", UINT32_MAX);
1002                 return -1;
1003         }
1004         if (tmp < 0) {
1005                 set_errf("Integer is negative");
1006                 return -2;
1007         }
1008         *val = tmp;
1009
1010         return 0;
1011 }
1012
1013 int parse_float(float* val, const char *str2)
1014 {
1015         char str[MAX_STR_LEN_PROC];
1016
1017         if (parse_vars(str, sizeof(str), str2))
1018                 return -1;
1019
1020         float tmp = strtof(str, 0);
1021         if ((tmp >= HUGE_VALF) || (tmp <= -HUGE_VALF)) {
1022                 set_errf("Unable to parse float\n");
1023                 return -1;
1024         }
1025         *val = tmp;
1026
1027         return 0;
1028 }
1029
1030 int parse_u64(uint64_t* val, const char *str2)
1031 {
1032         char str[MAX_STR_LEN_PROC];
1033
1034         if (parse_vars(str, sizeof(str), str2))
1035                 return -1;
1036
1037         errno = 0;
1038         uint64_t tmp = strtoul(str, NULL, 0);
1039         if (errno != 0) {
1040                 set_errf("Invalid u64 '%s' (%s)", str, strerror(errno));
1041                 return -2;
1042         }
1043         *val = tmp;
1044
1045         return 0;
1046 }
1047
1048 int parse_str(char* dst, const char *str2, size_t max_len)
1049 {
1050         char str[MAX_STR_LEN_PROC];
1051
1052         if (parse_vars(str, sizeof(str), str2))
1053                 return -1;
1054
1055         if (strlen(str) > max_len - 1) {
1056                 set_errf("String too long (%u > %u)", strlen(str), max_len - 1);
1057                 return -2;
1058         }
1059
1060         prox_strncpy(dst, str, max_len);
1061         return 0;
1062 }
1063
1064 int parse_path(char *dst, const char *str, size_t max_len)
1065 {
1066         if (parse_str(dst, str, max_len))
1067                 return -1;
1068         if (access(dst, F_OK)) {
1069                 set_errf("Invalid file '%s' (%s)", dst, strerror(errno));
1070                 return -1;
1071         }
1072         return 0;
1073 }
1074
1075 int parse_port_name(uint32_t *val, const char *str2)
1076 {
1077         char str[MAX_STR_LEN_PROC];
1078
1079         if (parse_vars(str, sizeof(str), str2))
1080                 return -1;
1081
1082         for (uint8_t i = 0; i < nb_port_names; ++i) {
1083                 if (!strcmp(str, port_names[i].name)) {
1084                         *val = port_names[i].id;
1085                         return 0;
1086                 }
1087         }
1088         set_errf("Port with name %s not defined", str);
1089         return 1;
1090 }
1091
1092 int parse_port_name_list(uint32_t *val, uint32_t* tot, uint8_t max_vals, const char *str2)
1093 {
1094         char *elements[PROX_MAX_PORTS + 1];
1095         char str[MAX_STR_LEN_PROC];
1096         uint32_t cur;
1097         int ret;
1098
1099         if (parse_str(str, str2, sizeof(str)))
1100                 return -1;
1101
1102         ret = rte_strsplit(str, strlen(str), elements, PROX_MAX_PORTS + 1, ',');
1103
1104         if (ret == PROX_MAX_PORTS + 1 || ret > max_vals) {
1105                 set_errf("Too many ports in port list");
1106                 return -1;
1107         }
1108
1109         strip_spaces(elements, ret);
1110         for (uint8_t i = 0; i < ret; ++i) {
1111                 if (parse_port_name(&cur, elements[i])) {
1112                         return -1;
1113                 }
1114                 val[i] = cur;
1115         }
1116         if (tot) {
1117                 *tot = ret;
1118         }
1119         return 0;
1120 }
1121
1122 int parse_remap(uint8_t *mapping, const char *str)
1123 {
1124         char *elements[PROX_MAX_PORTS + 1];
1125         char *elements2[PROX_MAX_PORTS + 1];
1126         char str_cpy[MAX_STR_LEN_PROC];
1127         uint32_t val;
1128         int ret, ret2;
1129
1130         if (strlen(str) > MAX_STR_LEN_PROC) {
1131                 set_errf("String too long (max supported: %d)", MAX_STR_LEN_PROC);
1132                 return -2;
1133         }
1134         prox_strncpy(str_cpy, str, MAX_STR_LEN_PROC);
1135
1136         ret = rte_strsplit(str_cpy, strlen(str_cpy), elements, PROX_MAX_PORTS + 1, ',');
1137         if (ret <= 0) {
1138                 set_errf("Invalid remap syntax");
1139                 return -1;
1140         }
1141         else if (ret > PROX_MAX_PORTS) {
1142                 set_errf("Too many remaps");
1143                 return -2;
1144         }
1145
1146         strip_spaces(elements, ret);
1147         for (uint8_t i = 0; i < ret; ++i) {
1148                 ret2 = rte_strsplit(elements[i], strlen(elements[i]), elements2, PROX_MAX_PORTS + 1, '|');
1149                 strip_spaces(elements2, ret2);
1150                 if (ret2 > PROX_MAX_PORTS) {
1151                         set_errf("Too many remaps");
1152                         return -2;
1153                 }
1154                 for (uint8_t j = 0; j < ret2; ++j) {
1155                         if (parse_port_name(&val, elements2[j])) {
1156                                 return -1;
1157                         }
1158
1159                         /* This port will be mapped to the i'th
1160                            element specified before remap=. */
1161                         mapping[val] = i;
1162                 }
1163         }
1164
1165         return ret;
1166 }
1167
1168 int add_port_name(uint32_t val, const char *str2)
1169 {
1170         char str[MAX_STR_LEN_PROC];
1171
1172         if (parse_vars(str, sizeof(str), str2))
1173                 return -1;
1174
1175         struct port_name* pn;
1176
1177         if (nb_port_names == MAX_NB_PORT_NAMES) {
1178                 set_errf("Too many ports defined (can define %d)", MAX_NB_PORT_NAMES);
1179                 return -1;
1180         }
1181
1182         for (uint8_t i = 0; i < nb_port_names; ++i) {
1183                 /* each port has to have a unique name*/
1184                 if (!strcmp(str, port_names[i].name)) {
1185                         set_errf("Port with name %s is already defined", str);
1186                         return -2;
1187                 }
1188         }
1189
1190         pn = &port_names[nb_port_names];
1191         prox_strncpy(pn->name, str, sizeof(pn->name));
1192         pn->id = val;
1193
1194         ++nb_port_names;
1195         return 0;
1196 }
1197
1198 int set_self_var(const char *str)
1199 {
1200         for (uint8_t i = 0; i < nb_vars; ++i) {
1201                 if (!strcmp("$self", vars[i].name)) {
1202                         sprintf(vars[i].val, "%s", str);
1203                         return 0;
1204                 }
1205         }
1206
1207         struct var *v = &vars[nb_vars];
1208
1209         prox_strncpy(v->name, "$self", strlen("$self") + 1);
1210         sprintf(v->val, "%s", str);
1211         nb_vars++;
1212
1213         return 0;
1214 }
1215
1216 int add_var(const char* name, const char *str2, uint8_t cli)
1217 {
1218         struct var* v;
1219
1220         char str[MAX_STR_LEN_PROC];
1221
1222         if (parse_vars(str, sizeof(str), str2))
1223                 return -1;
1224
1225         if (strlen(name) == 0 || strlen(name) == 1) {
1226                 set_errf("Can't define variables with empty name");
1227                 return -1;
1228         }
1229
1230         if (name[0] != '$') {
1231                 set_errf("Each variable should start with the $ character");
1232                 return -1;
1233         }
1234
1235         if (nb_vars == MAX_NB_VARS) {
1236                 set_errf("Too many variables defined (can define %d)", MAX_NB_VARS);
1237                 return -2;
1238         }
1239
1240         for (uint8_t i = 0; i < nb_vars; ++i) {
1241                 if (!strcmp(name, vars[i].name)) {
1242
1243                         /* Variables defined through program arguments
1244                            take precedence. */
1245                         if (!cli && vars[i].cli) {
1246                                 return 0;
1247                         }
1248
1249                         set_errf("Variable with name %s is already defined", name);
1250                         return -3;
1251                 }
1252         }
1253
1254         v = &vars[nb_vars];
1255         PROX_PANIC(strlen(name) > sizeof(v->name), "\tUnable to parse var %s: too long\n", name);
1256         PROX_PANIC(strlen(str) > sizeof(v->val), "\tUnable to parse var %s=%s: too long\n", name,str);
1257         prox_strncpy(v->name, name, sizeof(v->name));
1258         prox_strncpy(v->val, str, sizeof(v->val));
1259         v->cli = cli;
1260
1261         ++nb_vars;
1262         return 0;
1263 }
1264
1265 static int read_cores_present(uint32_t *cores, int max_cores, int *res)
1266 {
1267         FILE* fd = fopen("/sys/devices/system/cpu/present", "r");
1268         char buf[1024];
1269
1270         if (fd == NULL) {
1271                 set_errf("Could not opening file /sys/devices/system/cpu/present");
1272                 return -1;
1273         }
1274
1275         if (fgets(buf, sizeof(buf), fd) == NULL) {
1276                 set_errf("Could not read cores range");
1277                 return -1;
1278         }
1279
1280         fclose(fd);
1281
1282         int ret = parse_list_set(cores, buf, max_cores);
1283
1284         if (ret < 0)
1285                 return -1;
1286
1287         *res = ret;
1288         return 0;
1289 }
1290
1291 static int set_dummy_topology(void)
1292 {
1293         int core_count = 0;
1294
1295         for (int s = 0; s < MAX_SOCKETS; s++) {
1296                 for (int i = 0; i < 32; ++i) {
1297                         cpu_topo.socket[s][i][0] = core_count++;
1298                         cpu_topo.socket[s][i][1] = core_count++;
1299                         cpu_topo.n_cores[s]++;
1300                 }
1301         }
1302         cpu_topo.n_sockets = MAX_SOCKETS;
1303         return 0;
1304 }
1305
1306 static int read_cpu_topology(void)
1307 {
1308         if (cpu_topo.n_sockets != 0)
1309                 return 0;
1310         if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO)
1311                 return set_dummy_topology();
1312
1313         uint32_t cores[RTE_MAX_LCORE];
1314         int n_cores = 0;
1315
1316         if (read_cores_present(cores, sizeof(cores)/sizeof(cores[0]), &n_cores) != 0)
1317                 return -1;
1318
1319         for (int s = 0; s < MAX_SOCKETS; s++) {
1320                 for (int i = 0; i < RTE_MAX_LCORE; ++i) {
1321                         cpu_topo.socket[s][i][0] = -1;
1322                         cpu_topo.socket[s][i][1] = -1;
1323                 }
1324         }
1325
1326         for (int i = 0; i < n_cores; ++i) {
1327                 uint32_t socket_id, lcore_id, phys;
1328
1329                 lcore_id = cores[i];
1330                 if (get_socket(lcore_id, &socket_id) != 0)
1331                         return -1;
1332                 if (socket_id >= MAX_SOCKETS) {
1333                         set_errf("Can't read CPU topology due too high socket ID (max allowed is %d)",
1334                                  MAX_SOCKETS);
1335                         return -1;
1336                 }
1337                 if (socket_id >= cpu_topo.n_sockets) {
1338                         cpu_topo.n_sockets = socket_id + 1;
1339                 }
1340                 if (get_phys_core(&phys, lcore_id) != 0)
1341                         return -1;
1342                 if (phys >= RTE_MAX_LCORE) {
1343                         set_errf("Core ID %u too high", phys);
1344                         return -1;
1345                 }
1346
1347                 if (cpu_topo.socket[socket_id][phys][0] == -1) {
1348                         cpu_topo.socket[socket_id][phys][0] = lcore_id;
1349                         cpu_topo.n_cores[socket_id]++;
1350                 }
1351                 else if (cpu_topo.socket[socket_id][phys][1] == -1) {
1352                         cpu_topo.socket[socket_id][phys][1] = lcore_id;
1353                 }
1354                 else {
1355                         set_errf("Too many core siblings");
1356                         return -1;
1357                 }
1358         }
1359
1360         /* There can be holes in the cpu_topo description at this
1361            point. An example for this is a CPU topology where the
1362            lowest core ID of 2 hyper-threads is always an even
1363            number. Before finished up this phase, compact all the
1364            cores to make the numbers consecutive. */
1365
1366         for (uint32_t i = 0; i < cpu_topo.n_sockets; ++i) {
1367                 int spread = 0, compact = 0;
1368                 while (cpu_topo.socket[i][spread][0] == -1)
1369                         spread++;
1370
1371                 for (uint32_t c = 0; c < cpu_topo.n_cores[i]; ++c) {
1372                         cpu_topo.socket[i][compact][0] = cpu_topo.socket[i][spread][0];
1373                         cpu_topo.socket[i][compact][1] = cpu_topo.socket[i][spread][1];
1374                         compact++;
1375                         spread++;
1376                         /* Skip gaps */
1377                         while (cpu_topo.socket[i][spread][0] == -1)
1378                                 spread++;
1379                 }
1380         }
1381
1382         return 0;
1383 }
1384
1385 static int bit_len_valid(uint32_t len, const char *str)
1386 {
1387         if (len > 32) {
1388                 set_errf("Maximum random length is 32, but length of '%s' is %zu\n", str, len);
1389                 return 0;
1390         }
1391         if (len % 8) {
1392                 plog_err("Random should be multiple of 8 long\n");
1393                 return 0;
1394         }
1395         if (len == 0) {
1396                 plog_err("Random should be at least 1 byte long\n");
1397                 return 0;
1398         }
1399         return -1;
1400 }
1401
1402 int parse_random_str(uint32_t *mask, uint32_t *fixed, uint32_t *len, const char *str)
1403 {
1404         const size_t len_bits = strlen(str);
1405
1406         if (!bit_len_valid(len_bits, str))
1407                 return -1;
1408
1409         *mask = 0;
1410         *fixed = 0;
1411         *len = len_bits / 8;
1412
1413         for (uint32_t j = 0; j < len_bits; ++j) {
1414                 /* Store in the lower bits the value of the rand string (note
1415                    that these are the higher bits in LE). */
1416                 switch (str[j]) {
1417                 case 'X':
1418                         *mask |= 1 << (len_bits - 1 - j);
1419                         break;
1420                 case '1':
1421                         *fixed |= 1 << (len_bits - 1 - j);
1422                         break;
1423                 case '0':
1424                         break;
1425                 default:
1426                         set_errf("Unexpected %c\n", str[j]);
1427                         return -1;
1428                 }
1429         }
1430         return 0;
1431 }