2 // Copyright (c) 2010-2017 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <rte_ether.h>
26 #include <rte_string_fns.h>
31 #include "parse_utils.h"
32 #include "prox_globals.h"
36 #include "prox_lua_types.h"
38 #define MAX_NB_PORT_NAMES PROX_MAX_PORTS
39 #define MAX_LEN_PORT_NAME 24
40 #define MAX_LEN_VAR_NAME 24
41 #define MAX_LEN_VAL 512
42 #define MAX_NB_VARS 32
44 #if MAX_WT_PER_LB > MAX_INDEX
45 #error MAX_WT_PER_LB > MAX_INDEX
48 /* The CPU topology of the system is used to parse "socket
49 notation". This notation allows to refer to cores on specific
50 sockets and the hyper-thread of those cores. The CPU topology is
51 loaded only if the socket notation is used at least once. */
54 int socket[MAX_SOCKETS][RTE_MAX_LCORE][2];
55 uint32_t n_cores[MAX_SOCKETS];
59 struct cpu_topology cpu_topo;
63 char name[MAX_LEN_PORT_NAME];
66 static struct port_name port_names[MAX_NB_PORT_NAMES];
67 static uint8_t nb_port_names;
71 char name[MAX_LEN_VAR_NAME];
72 char val[MAX_LEN_VAL];
75 static struct var vars[MAX_NB_VARS];
76 static uint8_t nb_vars;
78 static char format_err_str[256];
79 static const char *err_str = "";
81 const char *get_parse_err(void)
86 static int read_cpu_topology(void);
88 static int parse_core(int *socket, int *core, int *ht, const char* str);
90 static void set_errf(const char *format, ...)
94 vsnprintf(format_err_str, sizeof(format_err_str), format, ap);
96 err_str = format_err_str;
99 static struct var *var_lookup(const char *name)
101 for (uint8_t i = 0; i < nb_vars; ++i) {
102 if (!strcmp(name, vars[i].name)) {
109 int parse_single_var(char *val, size_t len, const char *name)
113 match = var_lookup(name);
115 if (strlen(match->val) + 1 > len) {
116 set_errf("Variables '%s' with value '%s' is too long\n",
117 match->name, match->val);
120 strncpy(val, match->val, len);
124 /* name + 1 to skip leading '$' */
125 if (lua_to_string(prox_lua(), GLOBAL, name + 1, val, len) >= 0)
129 set_errf("Variable '%s' not defined!", name);
133 /* Replace $... and each occurrence of ${...} with variable values */
134 int parse_vars(char *val, size_t len, const char *name)
136 static char result[MAX_CFG_STRING_LEN];
137 static char cur_var[MAX_CFG_STRING_LEN];
138 char parsed[MAX_CFG_STRING_LEN];
139 size_t name_len = strlen(name);
140 enum parse_vars_state {NO_VAR, WHOLE_VAR, INLINE_VAR} state = NO_VAR;
141 size_t result_len = 0;
142 size_t start_var = 0;
144 memset(result, 0, sizeof(result));
145 PROX_PANIC(name_len > sizeof(result), "\tUnable to parse var %s: too long\n", name);
147 for (size_t i = 0; i < name_len; ++i) {
150 if (name[i] == '$') {
151 if (i != name_len - 1 && name[i + 1] == '{') {
156 else if (i == 0 && i != name_len - 1) {
160 set_errf("Invalid variable syntax");
165 result[result_len++] = name[i];
169 if (name[i] == '}') {
171 size_t var_len = i - start_var;
173 set_errf("Empty variable are not allowed");
177 strncpy(&cur_var[1], &name[start_var], var_len);
178 cur_var[1 + var_len] = 0;
179 if (parse_single_var(parsed, sizeof(parsed), cur_var)) {
182 strcpy(&result[result_len], parsed);
183 result_len += strlen(parsed);
186 else if (i == name_len - 1) {
187 set_errf("Invalid variable syntax, expected '}'.");
192 if (i == name_len - 1) {
193 return parse_single_var(val, len, name);
198 strncpy(val, result, len);
203 int parse_int_mask(uint32_t *val, uint32_t *mask, const char *str2)
205 char str[MAX_STR_LEN_PROC];
208 if (parse_vars(str, sizeof(str), str2))
211 mask_str = strchr(str, '&');
213 if (mask_str == NULL) {
214 set_errf("Missing '&' when parsing mask");
220 if (parse_int(val, str))
222 if (parse_int(mask, mask_str + 1))
228 int parse_range(uint32_t* lo, uint32_t* hi, const char *str2)
230 char str[MAX_STR_LEN_PROC];
233 if (parse_vars(str, sizeof(str), str2))
236 dash = strstr(str, "-");
239 set_errf("Missing '-' when parsing mask");
245 if (parse_int(lo, str))
247 if (parse_int(hi, dash + 1))
250 int64_t tmp = strtol(str, 0, 0);
251 if (tmp > UINT32_MAX) {
252 set_errf("Integer is bigger than %u", UINT32_MAX);
256 set_errf("Integer is negative");
262 tmp = strtol(dash + 1, 0, 0);
263 if (tmp > UINT32_MAX) {
264 set_errf("Integer is bigger than %u", UINT32_MAX);
268 set_errf("Integer is negative");
275 set_errf("Low boundary is above high boundary in range");
282 int parse_ip(uint32_t *addr, const char *str2)
284 char str[MAX_STR_LEN_PROC];
286 if (parse_vars(str, sizeof(str), str2))
291 if (strlen(str) > MAX_STR_LEN_PROC) {
292 set_errf("String too long (max supported: %d)", MAX_STR_LEN_PROC);
296 if (4 != rte_strsplit(str, strlen(str), ip_parts, 5, '.')) {
297 set_errf("Expecting 4 octets in ip.");
302 for (uint8_t i = 0; i < 4; ++i) {
303 val = atoi(ip_parts[i]);
305 set_errf("Maximum value for octet is 255 but octet %u is %u", i, val);
308 *addr = *addr << 8 | val;
313 int parse_ip4_cidr(struct ip4_subnet *val, const char *str2)
315 char str[MAX_STR_LEN_PROC];
319 if (parse_vars(str, sizeof(str), str2))
322 slash = strstr(str, "/");
325 set_errf("Missing '/' when parsing CIDR notation");
330 prefix = atoi(slash + 1);
331 val->prefix = prefix;
334 set_errf("Prefix %d is too big", prefix);
338 set_errf("Prefix %d is too small", prefix);
340 if (parse_ip(&val->ip, str))
343 /* Apply mask making all bits outside the prefix zero */
344 val->ip &= ((int)(1 << 31)) >> (prefix - 1);
349 int parse_ip6_cidr(struct ip6_subnet *val, const char *str2)
351 char str[MAX_STR_LEN_PROC];
355 if (parse_vars(str, sizeof(str), str2))
358 slash = strstr(str, "/");
361 set_errf("Missing '/' when parsing CIDR notation");
366 prefix = atoi(slash + 1);
367 val->prefix = prefix;
369 parse_ip6((struct ipv6_addr *)&val->ip, str);
371 /* Apply mask making all bits outside the prefix zero */
376 while (p >= prefix) {
382 if (prefix % 8 != 0) {
383 val->ip[15-cnt] &= ((int8_t)(1 << 7)) >> ((prefix %8) - 1);
389 int parse_ip6(struct ipv6_addr *addr, const char *str2)
391 char str[MAX_STR_LEN_PROC];
394 if (parse_vars(str, sizeof(str), str2))
397 uint8_t ret = rte_strsplit(str, strlen(str), addr_parts, 9, ':');
400 set_errf("Invalid IPv6 address");
406 for (uint8_t i = 0, j = 0; i < ret; ++i, ++j) {
407 if (*addr_parts[i] == 0) {
409 set_errf("Can only omit zeros once");
416 uint16_t w = strtoll(addr_parts[i], NULL, 16);
417 addr->bytes[j++] = (w >> 8) & 0xff;
418 addr->bytes[j] = w & 0xff;
424 int parse_mac(struct ether_addr *ether_addr, const char *str2)
426 char str[MAX_STR_LEN_PROC];
429 if (parse_vars(str, sizeof(str), str2))
432 uint8_t ret = rte_strsplit(str, strlen(str), addr_parts, 7, ':');
434 ret = rte_strsplit(str, strlen(str), addr_parts, 7, ' ');
437 set_errf("Invalid MAC address format");
441 for (uint8_t i = 0; i < 6; ++i) {
442 if (2 != strlen(addr_parts[i])) {
443 set_errf("Invalid MAC address format");
446 ether_addr->addr_bytes[i] = strtol(addr_parts[i], NULL, 16);
452 char* get_cfg_key(char *str)
454 char *pkey = strchr(str, '=');
461 /* remove leading spaces */
462 while (isspace(*pkey)) {
465 if (*pkey == '\0') { /* an empty key */
472 void strip_spaces(char *strings[], const uint32_t count)
474 for (uint32_t i = 0; i < count; ++i) {
475 while (isspace(strings[i][0])) {
478 size_t len = strlen(strings[i]);
480 while (len && isspace(strings[i][len - 1])) {
481 strings[i][len - 1] = '\0';
487 int is_virtualized(void)
489 char buf[1024]= "/proc/cpuinfo";
491 FILE* fd = fopen(buf, "r");
493 set_errf("Could not open %s", buf);
496 while (fgets(buf, sizeof(buf), fd) != NULL) {
497 if ((strstr(buf, "flags") != NULL) && (strstr(buf, "hypervisor") != NULL))
504 static int get_phys_core(uint32_t *dst, int lcore_id)
508 snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcore_id);
509 FILE* ht_fd = fopen(buf, "r");
512 set_errf("Could not open cpu topology %s", buf);
516 if (fgets(buf, sizeof(buf), ht_fd) == NULL) {
517 set_errf("Could not read cpu topology");
522 uint32_t list[2] = {-1,-1};
523 parse_list_set(list, buf, 2);
530 static int get_socket(uint32_t core_id, uint32_t *socket)
534 snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu%u/topology/physical_package_id", core_id);
535 FILE* fd = fopen(buf, "r");
542 if (fgets(buf, sizeof(buf), fd) != NULL) {
548 *socket = (ret == -1 ? 0 : ret);
553 int lcore_to_socket_core_ht(uint32_t lcore_id, char *dst, size_t len)
555 if (cpu_topo.n_sockets == 0) {
556 if (read_cpu_topology() == -1) {
561 for (uint32_t s = 0; s < cpu_topo.n_sockets; s++) {
562 for (uint32_t i = 0; i < cpu_topo.n_cores[s]; ++i) {
563 if ((uint32_t)cpu_topo.socket[s][i][0] == lcore_id) {
564 snprintf(dst, len, "%us%u", i, s);
566 } else if ((uint32_t)cpu_topo.socket[s][i][1] == lcore_id) {
567 snprintf(dst, len, "%us%uh", i, s);
576 static int get_lcore_id(uint32_t socket_id, uint32_t core_id, int ht)
578 if (cpu_topo.n_sockets == 0) {
579 if (read_cpu_topology() == -1) {
584 if (socket_id == UINT32_MAX)
587 if (socket_id >= MAX_SOCKETS) {
588 set_errf("Socket id %d too high (max allowed is %d)", MAX_SOCKETS);
591 if (core_id >= RTE_MAX_LCORE) {
592 set_errf("Core id %d too high (max allowed is %d)", RTE_MAX_LCORE);
595 if (socket_id >= cpu_topo.n_sockets) {
596 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",
597 cpu_topo.n_sockets, cpu_topo.n_sockets, cpu_topo.n_cores[0], cpu_topo.socket[0][0][1] == -1? 1: 2);
600 if (core_id >= cpu_topo.n_cores[socket_id]) {
601 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",
602 core_id, socket_id, cpu_topo.n_sockets, cpu_topo.n_cores[0], cpu_topo.socket[socket_id][0][1] == -1? 1: 2);
605 if (cpu_topo.socket[socket_id][core_id][!!ht] == -1) {
606 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",
607 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);
611 return cpu_topo.socket[socket_id][core_id][!!ht];
614 /* Returns 0 on success, negative on error. Parses the syntax XsYh
615 where sYh is optional. If sY is specified, Y is stored in the
616 socket argument. If, in addition, h is specified, *ht is set to
617 1. In case the input is only a number, socket and ht are set to
619 static int parse_core(int *socket, int *core, int *ht, const char* str)
627 *core = strtol(str, &end, 10);
633 if (cpu_topo.n_sockets == 0) {
634 if (read_cpu_topology() == -1) {
640 *socket = strtol(end, &end, 10);
641 if (*socket >= MAX_SOCKETS) {
642 set_errf("Socket id %d too high (max allowed is %d)", *socket, MAX_SOCKETS - 1);
655 set_errf("Can't find hyper-thread since socket has not been specified");
662 static int parse_task(const char *str, uint32_t *socket, uint32_t *core, uint32_t *task, uint32_t *ht, enum ctrl_type *type)
664 const char *str_beg = str;
667 *core = strtol(str, &end, 10);
669 set_errf("Expected number to in core-task definition:\n"
670 "\t(i.e. 5s1t0 for task 0 on core 5 on socket 1)\n"
671 "\tHave: '%s'.", end);
687 *socket = strtol(str, &end, 10);
696 *task = strtol(str, &end, 10);
699 *type = CTRL_TYPE_PKT;
702 else if (*str == 'm') {
703 *type = CTRL_TYPE_MSG;
709 set_errf("Can't find hyper-thread since socket has not been specified");
714 *task = strtol(str, &end, 10);
717 *type = CTRL_TYPE_PKT;
720 else if (*str == 'm') {
721 *type = CTRL_TYPE_MSG;
726 return str - str_beg;
729 static int core_task_set_add(struct core_task_set *val, uint32_t core, uint32_t task, enum ctrl_type type)
731 if (val->n_elems == sizeof(val->core_task)/sizeof(val->core_task[0]))
734 val->core_task[val->n_elems].core = core;
735 val->core_task[val->n_elems].task = task;
736 val->core_task[val->n_elems].type = type;
742 int parse_task_set(struct core_task_set *cts, const char *str2)
744 char str[MAX_STR_LEN_PROC];
746 if (parse_vars(str, sizeof(str), str2))
753 uint32_t socket_beg, core_beg, task_beg, ht_beg,
754 socket_end, core_end, task_end, ht_end;
755 enum ctrl_type type_beg, type_end;
756 uint32_t task_group_start = -1;
758 while (*str3 && *str3 != ' ') {
760 task_group_start = cts->n_elems;
764 if (*str3 == ')' && *(str3 + 1) == 't') {
767 uint32_t t = strtol(str3, &end, 10);
768 enum ctrl_type type = 0;
772 type = CTRL_TYPE_PKT;
775 else if (*str3 == 'm') {
776 type = CTRL_TYPE_MSG;
780 for (uint32_t i = task_group_start; i < cts->n_elems; ++i) {
781 cts->core_task[i].task = t;
782 cts->core_task[i].type = type;
786 ret = parse_task(str3, &socket_beg, &core_beg, &task_beg, &ht_beg, &type_beg);
790 socket_end = socket_beg;
798 ret = parse_task(str3, &socket_end, &core_end, &task_end, &ht_end, &type_end);
807 if (socket_end != socket_beg) {
808 set_errf("Same socket must be used in range syntax.");
810 } else if (ht_beg != ht_end) {
811 set_errf("If 'h' syntax is in range, it must be specified everywhere.\n");
813 } else if (task_end != task_beg && core_end != core_beg) {
814 set_errf("Same task must be used in range syntax when cores are different.\n");
816 } else if (task_end < task_beg) {
817 set_errf("Task for end of range must be higher than task for beginning of range.\n");
819 } else if (type_end != type_beg) {
820 set_errf("Task type for end of range must be the same as task type for beginning.\n");
822 } else if (core_end < core_beg) {
823 set_errf("Core for end of range must be higher than core for beginning of range.\n");
827 for (uint32_t j = core_beg; j <= core_end; ++j) {
828 if (socket_beg != UINT32_MAX && ht_beg != UINT32_MAX)
829 ret = get_lcore_id(socket_beg, j, ht_beg);
834 for (uint32_t k = task_beg; k <= task_end; ++k) {
835 core_task_set_add(cts, ret, k, type_beg);
842 int parse_list_set(uint32_t *list, const char *str2, uint32_t max_list)
844 char str[MAX_STR_LEN_PROC];
845 char *parts[MAX_STR_LEN_PROC];
847 if (parse_vars(str, sizeof(str), str2))
850 int n_parts = rte_strsplit(str, strlen(str), parts, MAX_STR_LEN_PROC, ',');
851 size_t list_count = 0;
853 for (int i = 0; i < n_parts; ++i) {
854 char *cur_part = parts[i];
856 int n_sub_parts = rte_strsplit(cur_part, strlen(cur_part), sub_parts, 3, '-');
857 int socket1, socket2;
862 if (n_sub_parts == 1) {
863 if (parse_core(&socket1, &core1, &ht1, sub_parts[0]))
869 } else if (n_sub_parts == 2) {
870 if (parse_core(&socket1, &core1, &ht1, sub_parts[0]))
872 if (parse_core(&socket2, &core2, &ht2, sub_parts[1]))
874 } else if (n_sub_parts >= 3) {
875 set_errf("Multiple '-' characters in range syntax found");
878 set_errf("Invalid list syntax");
882 if (socket1 != socket2) {
883 set_errf("Same socket must be used in range syntax");
886 else if (ht1 != ht2) {
887 set_errf("If 'h' syntax is in range, it must be specified everywhere.");
891 for (int cur_core = core1; cur_core <= core2; ++cur_core) {
895 effective_core = get_lcore_id(socket1, cur_core, ht1);
897 effective_core = cur_core;
899 if (list_count >= max_list) {
900 set_errf("Too many elements in list");
903 list[list_count++] = effective_core;
910 int parse_kmg(uint32_t* val, const char *str2)
912 char str[MAX_STR_LEN_PROC];
914 if (parse_vars(str, sizeof(str), str2))
917 char c = str[strlen(str) - 1];
935 /* only support optional KMG suffix */
936 if (c < '0' || c > '9') {
937 set_errf("Unknown syntax for KMG suffix '%c' (expected K, M or G)", c);
945 int parse_bool(uint32_t* val, const char *str2)
947 char str[MAX_STR_LEN_PROC];
949 if (parse_vars(str, sizeof(str), str2))
952 if (!strcmp(str, "yes")) {
956 else if (!strcmp(str, "no")) {
960 set_errf("Unknown syntax for bool '%s' (expected yes or no)", str);
964 int parse_flag(uint32_t* val, uint32_t flag, const char *str2)
966 char str[MAX_STR_LEN_PROC];
968 if (parse_vars(str, sizeof(str), str2))
972 if (parse_bool(&tmp, str))
983 int parse_int(uint32_t* val, const char *str2)
985 char str[MAX_STR_LEN_PROC];
987 if (parse_vars(str, sizeof(str), str2))
990 int64_t tmp = strtol(str, 0, 0);
991 if (tmp > UINT32_MAX) {
992 set_errf("Integer is bigger than %u", UINT32_MAX);
996 set_errf("Integer is negative");
1004 int parse_float(float* val, const char *str2)
1006 char str[MAX_STR_LEN_PROC];
1008 if (parse_vars(str, sizeof(str), str2))
1011 float tmp = strtof(str, 0);
1012 if ((tmp >= HUGE_VALF) || (tmp <= -HUGE_VALF)) {
1013 set_errf("Unable to parse float\n");
1021 int parse_u64(uint64_t* val, const char *str2)
1023 char str[MAX_STR_LEN_PROC];
1025 if (parse_vars(str, sizeof(str), str2))
1029 uint64_t tmp = strtoul(str, NULL, 0);
1031 set_errf("Invalid u64 '%s' (%s)", str, strerror(errno));
1039 int parse_str(char* dst, const char *str2, size_t max_len)
1041 char str[MAX_STR_LEN_PROC];
1043 if (parse_vars(str, sizeof(str), str2))
1046 if (strlen(str) > max_len - 1) {
1047 set_errf("String too long (%u > %u)", strlen(str), max_len - 1);
1051 strncpy(dst, str, max_len);
1055 int parse_path(char *dst, const char *str, size_t max_len)
1057 if (parse_str(dst, str, max_len))
1059 if (access(dst, F_OK)) {
1060 set_errf("Invalid file '%s' (%s)", dst, strerror(errno));
1066 int parse_port_name(uint32_t *val, const char *str2)
1068 char str[MAX_STR_LEN_PROC];
1070 if (parse_vars(str, sizeof(str), str2))
1073 for (uint8_t i = 0; i < nb_port_names; ++i) {
1074 if (!strcmp(str, port_names[i].name)) {
1075 *val = port_names[i].id;
1079 set_errf("Port with name %s not defined", str);
1083 int parse_port_name_list(uint32_t *val, uint32_t* tot, uint8_t max_vals, const char *str2)
1085 char *elements[PROX_MAX_PORTS + 1];
1086 char str[MAX_STR_LEN_PROC];
1090 if (parse_str(str, str2, sizeof(str)))
1093 ret = rte_strsplit(str, strlen(str), elements, PROX_MAX_PORTS + 1, ',');
1095 if (ret == PROX_MAX_PORTS + 1 || ret > max_vals) {
1096 set_errf("Too many ports in port list");
1100 strip_spaces(elements, ret);
1101 for (uint8_t i = 0; i < ret; ++i) {
1102 if (parse_port_name(&cur, elements[i])) {
1113 int parse_remap(uint8_t *mapping, const char *str)
1115 char *elements[PROX_MAX_PORTS + 1];
1116 char *elements2[PROX_MAX_PORTS + 1];
1117 char str_cpy[MAX_STR_LEN_PROC];
1121 if (strlen(str) > MAX_STR_LEN_PROC) {
1122 set_errf("String too long (max supported: %d)", MAX_STR_LEN_PROC);
1125 strncpy(str_cpy, str, MAX_STR_LEN_PROC);
1127 ret = rte_strsplit(str_cpy, strlen(str_cpy), elements, PROX_MAX_PORTS + 1, ',');
1129 set_errf("Invalid remap syntax");
1132 else if (ret > PROX_MAX_PORTS) {
1133 set_errf("Too many remaps");
1137 strip_spaces(elements, ret);
1138 for (uint8_t i = 0; i < ret; ++i) {
1139 ret2 = rte_strsplit(elements[i], strlen(elements[i]), elements2, PROX_MAX_PORTS + 1, '|');
1140 strip_spaces(elements2, ret2);
1141 if (ret2 > PROX_MAX_PORTS) {
1142 set_errf("Too many remaps");
1145 for (uint8_t j = 0; j < ret2; ++j) {
1146 if (parse_port_name(&val, elements2[j])) {
1150 /* This port will be mapped to the i'th
1151 element specified before remap=. */
1159 int add_port_name(uint32_t val, const char *str2)
1161 char str[MAX_STR_LEN_PROC];
1163 if (parse_vars(str, sizeof(str), str2))
1166 struct port_name* pn;
1168 if (nb_port_names == MAX_NB_PORT_NAMES) {
1169 set_errf("Too many ports defined (can define %d)", MAX_NB_PORT_NAMES);
1173 for (uint8_t i = 0; i < nb_port_names; ++i) {
1174 /* each port has to have a unique name*/
1175 if (!strcmp(str, port_names[i].name)) {
1176 set_errf("Port with name %s is already defined", str);
1181 pn = &port_names[nb_port_names];
1182 strncpy(pn->name, str, sizeof(pn->name));
1189 int set_self_var(const char *str)
1191 for (uint8_t i = 0; i < nb_vars; ++i) {
1192 if (!strcmp("$self", vars[i].name)) {
1193 sprintf(vars[i].val, "%s", str);
1198 struct var *v = &vars[nb_vars];
1200 strncpy(v->name, "$self", strlen("$self"));
1201 sprintf(v->val, "%s", str);
1207 int add_var(const char* name, const char *str2, uint8_t cli)
1211 char str[MAX_STR_LEN_PROC];
1213 if (parse_vars(str, sizeof(str), str2))
1216 if (strlen(name) == 0 || strlen(name) == 1) {
1217 set_errf("Can't define variables with empty name");
1221 if (name[0] != '$') {
1222 set_errf("Each variable should start with the $ character");
1226 if (nb_vars == MAX_NB_VARS) {
1227 set_errf("Too many variables defined (can define %d)", MAX_NB_VARS);
1231 for (uint8_t i = 0; i < nb_vars; ++i) {
1232 if (!strcmp(name, vars[i].name)) {
1234 /* Variables defined through program arguments
1236 if (!cli && vars[i].cli) {
1240 set_errf("Variable with name %s is already defined", name);
1246 PROX_PANIC(strlen(name) > sizeof(v->name), "\tUnable to parse var %s: too long\n", name);
1247 PROX_PANIC(strlen(str) > sizeof(v->val), "\tUnable to parse var %s=%s: too long\n", name,str);
1248 strncpy(v->name, name, sizeof(v->name));
1249 strncpy(v->val, str, sizeof(v->val));
1256 static int read_cores_present(uint32_t *cores, int max_cores, int *res)
1258 FILE* fd = fopen("/sys/devices/system/cpu/present", "r");
1262 set_errf("Could not opening file /sys/devices/system/cpu/present");
1266 if (fgets(buf, sizeof(buf), fd) == NULL) {
1267 set_errf("Could not read cores range");
1273 int ret = parse_list_set(cores, buf, max_cores);
1282 static int set_dummy_topology(void)
1286 for (int s = 0; s < MAX_SOCKETS; s++) {
1287 for (int i = 0; i < 32; ++i) {
1288 cpu_topo.socket[s][i][0] = core_count++;
1289 cpu_topo.socket[s][i][1] = core_count++;
1290 cpu_topo.n_cores[s]++;
1293 cpu_topo.n_sockets = MAX_SOCKETS;
1297 static int read_cpu_topology(void)
1299 if (cpu_topo.n_sockets != 0)
1301 if (prox_cfg.flags & DSF_USE_DUMMY_CPU_TOPO)
1302 return set_dummy_topology();
1304 uint32_t cores[RTE_MAX_LCORE];
1307 if (read_cores_present(cores, sizeof(cores)/sizeof(cores[0]), &n_cores) != 0)
1310 for (int s = 0; s < MAX_SOCKETS; s++) {
1311 for (int i = 0; i < RTE_MAX_LCORE; ++i) {
1312 cpu_topo.socket[s][i][0] = -1;
1313 cpu_topo.socket[s][i][1] = -1;
1317 for (int i = 0; i < n_cores; ++i) {
1318 uint32_t socket_id, lcore_id, phys;
1320 lcore_id = cores[i];
1321 if (get_socket(lcore_id, &socket_id) != 0)
1323 if (socket_id >= MAX_SOCKETS) {
1324 set_errf("Can't read CPU topology due too high socket ID (max allowed is %d)",
1328 if (socket_id >= cpu_topo.n_sockets) {
1329 cpu_topo.n_sockets = socket_id + 1;
1331 if (get_phys_core(&phys, lcore_id) != 0)
1333 if (phys >= RTE_MAX_LCORE) {
1334 set_errf("Core ID %u too high", phys);
1338 if (cpu_topo.socket[socket_id][phys][0] == -1) {
1339 cpu_topo.socket[socket_id][phys][0] = lcore_id;
1340 cpu_topo.n_cores[socket_id]++;
1342 else if (cpu_topo.socket[socket_id][phys][1] == -1) {
1343 cpu_topo.socket[socket_id][phys][1] = lcore_id;
1346 set_errf("Too many core siblings");
1351 /* There can be holes in the cpu_topo description at this
1352 point. An example for this is a CPU topology where the
1353 lowest core ID of 2 hyper-threads is always an even
1354 number. Before finished up this phase, compact all the
1355 cores to make the numbers consecutive. */
1357 for (uint32_t i = 0; i < cpu_topo.n_sockets; ++i) {
1358 int spread = 0, compact = 0;
1359 while (cpu_topo.socket[i][spread][0] == -1)
1362 for (uint32_t c = 0; c < cpu_topo.n_cores[i]; ++c) {
1363 cpu_topo.socket[i][compact][0] = cpu_topo.socket[i][spread][0];
1364 cpu_topo.socket[i][compact][1] = cpu_topo.socket[i][spread][1];
1368 while (cpu_topo.socket[i][spread][0] == -1)
1376 static int bit_len_valid(uint32_t len, const char *str)
1379 set_errf("Maximum random length is 32, but length of '%s' is %zu\n", str, len);
1383 plog_err("Random should be multiple of 8 long\n");
1387 plog_err("Random should be at least 1 byte long\n");
1393 int parse_random_str(uint32_t *mask, uint32_t *fixed, uint32_t *len, const char *str)
1395 const size_t len_bits = strlen(str);
1397 if (!bit_len_valid(len_bits, str))
1402 *len = len_bits / 8;
1404 for (uint32_t j = 0; j < len_bits; ++j) {
1405 /* Store in the lower bits the value of the rand string (note
1406 that these are the higher bits in LE). */
1409 *mask |= 1 << (len_bits - 1 - j);
1412 *fixed |= 1 << (len_bits - 1 - j);
1417 set_errf("Unexpected %c\n", str[j]);