1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
18 #include <arpa/inet.h>
19 #include <sys/socket.h>
21 #include <rte_errno.h>
29 case '0': case '1': case '2': case '3': case '4': case '5':
30 case '6': case '7': case '8': case '9':
32 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
34 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
42 softnic_parser_read_arg_bool(const char *p)
44 p = skip_white_spaces(p);
47 if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) ||
48 ((p[0] == 'Y') && (p[1] == 'E') && (p[2] == 'S'))) {
53 if (((p[0] == 'o') && (p[1] == 'n')) ||
54 ((p[0] == 'O') && (p[1] == 'N'))) {
59 if (((p[0] == 'n') && (p[1] == 'o')) ||
60 ((p[0] == 'N') && (p[1] == 'O'))) {
65 if (((p[0] == 'o') && (p[1] == 'f') && (p[2] == 'f')) ||
66 ((p[0] == 'O') && (p[1] == 'F') && (p[2] == 'F'))) {
71 p = skip_white_spaces(p);
80 softnic_parser_read_int32(int32_t *value, const char *p)
85 p = skip_white_spaces(p);
89 val = strtol(p, &next, 10);
98 softnic_parser_read_uint64(uint64_t *value, const char *p)
103 p = skip_white_spaces(p);
107 val = strtoul(p, &next, 10);
129 p = skip_white_spaces(p);
138 softnic_parser_read_uint64_hex(uint64_t *value, const char *p)
143 p = skip_white_spaces(p);
145 val = strtoul(p, &next, 16);
149 p = skip_white_spaces(next);
158 softnic_parser_read_uint32(uint32_t *value, const char *p)
161 int ret = softnic_parser_read_uint64(&val, p);
166 if (val > UINT32_MAX)
174 softnic_parser_read_uint32_hex(uint32_t *value, const char *p)
177 int ret = softnic_parser_read_uint64_hex(&val, p);
182 if (val > UINT32_MAX)
190 softnic_parser_read_uint16(uint16_t *value, const char *p)
193 int ret = softnic_parser_read_uint64(&val, p);
198 if (val > UINT16_MAX)
206 softnic_parser_read_uint16_hex(uint16_t *value, const char *p)
209 int ret = softnic_parser_read_uint64_hex(&val, p);
214 if (val > UINT16_MAX)
222 softnic_parser_read_uint8(uint8_t *value, const char *p)
225 int ret = softnic_parser_read_uint64(&val, p);
238 softnic_parser_read_uint8_hex(uint8_t *value, const char *p)
241 int ret = softnic_parser_read_uint64_hex(&val, p);
254 softnic_parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
258 if (string == NULL ||
263 for (i = 0; i < *n_tokens; i++) {
264 tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
265 if (tokens[i] == NULL)
269 if (i == *n_tokens &&
270 strtok_r(string, PARSE_DELIMITER, &string) != NULL)
278 softnic_parse_hex_string(char *src, uint8_t *dst, uint32_t *size)
283 /* Check input parameters */
291 if (((len & 3) != 0) ||
296 for (c = src; *c != 0; c++) {
297 if ((((*c) >= '0') && ((*c) <= '9')) ||
298 (((*c) >= 'A') && ((*c) <= 'F')) ||
299 (((*c) >= 'a') && ((*c) <= 'f')))
305 /* Convert chars to bytes */
306 for (i = 0; i < *size; i++)
307 dst[i] = get_hex_val(src[2 * i]) * 16 +
308 get_hex_val(src[2 * i + 1]);
314 softnic_parse_mpls_labels(char *string, uint32_t *labels, uint32_t *n_labels)
316 uint32_t n_max_labels = *n_labels, count = 0;
318 /* Check for void list of labels */
319 if (strcmp(string, "<void>") == 0) {
324 /* At least one label should be present */
325 for ( ; (*string != '\0'); ) {
329 if (count >= n_max_labels)
333 if (string[0] != ':')
339 value = strtol(string, &next, 10);
344 labels[count++] = (uint32_t)value;
351 static struct rte_ether_addr *
352 my_ether_aton(const char *a)
356 unsigned long o[RTE_ETHER_ADDR_LEN];
357 static struct rte_ether_addr ether_addr;
362 o[i] = strtoul(a, &end, 16);
363 if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
366 } while (++i != sizeof(o) / sizeof(o[0]) && end[0] != 0);
368 /* Junk at the end of line */
372 /* Support the format XX:XX:XX:XX:XX:XX */
373 if (i == RTE_ETHER_ADDR_LEN) {
375 if (o[i] > UINT8_MAX)
377 ether_addr.addr_bytes[i] = (uint8_t)o[i];
379 /* Support the format XXXX:XXXX:XXXX */
380 } else if (i == RTE_ETHER_ADDR_LEN / 2) {
382 if (o[i] > UINT16_MAX)
384 ether_addr.addr_bytes[i * 2] = (uint8_t)(o[i] >> 8);
385 ether_addr.addr_bytes[i * 2 + 1] = (uint8_t)(o[i] & 0xff);
391 return (struct rte_ether_addr *)ðer_addr;
395 softnic_parse_ipv4_addr(const char *token, struct in_addr *ipv4)
397 if (strlen(token) >= INET_ADDRSTRLEN)
400 if (inet_pton(AF_INET, token, ipv4) != 1)
407 softnic_parse_ipv6_addr(const char *token, struct in6_addr *ipv6)
409 if (strlen(token) >= INET6_ADDRSTRLEN)
412 if (inet_pton(AF_INET6, token, ipv6) != 1)
419 softnic_parse_mac_addr(const char *token, struct rte_ether_addr *addr)
421 struct rte_ether_addr *tmp;
423 tmp = my_ether_aton(token);
427 memcpy(addr, tmp, sizeof(struct rte_ether_addr));
432 softnic_parse_cpu_core(const char *entry,
433 struct softnic_cpu_core_params *p)
438 uint32_t s = 0, c = 0, h = 0, val;
439 uint8_t s_parsed = 0, c_parsed = 0, h_parsed = 0;
440 const char *next = skip_white_spaces(entry);
446 /* Expect <CORE> or [sX][cY][h]. At least one parameter is required. */
447 while (*next != '\0') {
448 /* If everything parsed nothing should left */
449 if (s_parsed && c_parsed && h_parsed)
456 if (s_parsed || c_parsed || h_parsed)
463 if (c_parsed || h_parsed)
476 /* If it start from digit it must be only core id. */
477 if (!isdigit(*next) || s_parsed || c_parsed || h_parsed)
483 for (num_len = 0; *next != '\0'; next++, num_len++) {
484 if (num_len == RTE_DIM(num))
490 num[num_len] = *next;
493 if (num_len == 0 && type != 'h' && type != 'H')
496 if (num_len != 0 && (type == 'h' || type == 'H'))
500 val = strtol(num, NULL, 10);