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>
22 #include <rte_string_fns.h>
30 case '0': case '1': case '2': case '3': case '4': case '5':
31 case '6': case '7': case '8': case '9':
33 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
35 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
43 parser_read_arg_bool(const char *p)
45 p = skip_white_spaces(p);
48 if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) ||
49 ((p[0] == 'Y') && (p[1] == 'E') && (p[2] == 'S'))) {
54 if (((p[0] == 'o') && (p[1] == 'n')) ||
55 ((p[0] == 'O') && (p[1] == 'N'))) {
60 if (((p[0] == 'n') && (p[1] == 'o')) ||
61 ((p[0] == 'N') && (p[1] == 'O'))) {
66 if (((p[0] == 'o') && (p[1] == 'f') && (p[2] == 'f')) ||
67 ((p[0] == 'O') && (p[1] == 'F') && (p[2] == 'F'))) {
72 p = skip_white_spaces(p);
81 parser_read_uint64(uint64_t *value, const char *p)
86 p = skip_white_spaces(p);
90 val = strtoul(p, &next, 10);
112 p = skip_white_spaces(p);
121 parser_read_uint64_hex(uint64_t *value, const char *p)
126 p = skip_white_spaces(p);
128 val = strtoul(p, &next, 16);
132 p = skip_white_spaces(next);
141 parser_read_uint32(uint32_t *value, const char *p)
144 int ret = parser_read_uint64(&val, p);
149 if (val > UINT32_MAX)
157 parser_read_uint32_hex(uint32_t *value, const char *p)
160 int ret = parser_read_uint64_hex(&val, p);
165 if (val > UINT32_MAX)
173 parser_read_uint16(uint16_t *value, const char *p)
176 int ret = parser_read_uint64(&val, p);
181 if (val > UINT16_MAX)
189 parser_read_uint16_hex(uint16_t *value, const char *p)
192 int ret = parser_read_uint64_hex(&val, p);
197 if (val > UINT16_MAX)
205 parser_read_uint8(uint8_t *value, const char *p)
208 int ret = parser_read_uint64(&val, p);
221 parser_read_uint8_hex(uint8_t *value, const char *p)
224 int ret = parser_read_uint64_hex(&val, p);
237 parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
241 if ((string == NULL) ||
246 for (i = 0; i < *n_tokens; i++) {
247 tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
248 if (tokens[i] == NULL)
252 if ((i == *n_tokens) &&
253 (NULL != strtok_r(string, PARSE_DELIMITER, &string)))
261 parse_hex_string(char *src, uint8_t *dst, uint32_t *size)
266 /* Check input parameters */
274 if (((len & 3) != 0) ||
279 for (c = src; *c != 0; c++) {
280 if ((((*c) >= '0') && ((*c) <= '9')) ||
281 (((*c) >= 'A') && ((*c) <= 'F')) ||
282 (((*c) >= 'a') && ((*c) <= 'f')))
288 /* Convert chars to bytes */
289 for (i = 0; i < *size; i++)
290 dst[i] = get_hex_val(src[2 * i]) * 16 +
291 get_hex_val(src[2 * i + 1]);
297 parse_mpls_labels(char *string, uint32_t *labels, uint32_t *n_labels)
299 uint32_t n_max_labels = *n_labels, count = 0;
301 /* Check for void list of labels */
302 if (strcmp(string, "<void>") == 0) {
307 /* At least one label should be present */
308 for ( ; (*string != '\0'); ) {
312 if (count >= n_max_labels)
316 if (string[0] != ':')
322 value = strtol(string, &next, 10);
327 labels[count++] = (uint32_t) value;
334 static struct rte_ether_addr *
335 my_ether_aton(const char *a)
339 unsigned long o[RTE_ETHER_ADDR_LEN];
340 static struct rte_ether_addr ether_addr;
345 o[i] = strtoul(a, &end, 16);
346 if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
349 } while (++i != RTE_DIM(o) && end[0] != 0);
351 /* Junk at the end of line */
355 /* Support the format XX:XX:XX:XX:XX:XX */
356 if (i == RTE_ETHER_ADDR_LEN) {
358 if (o[i] > UINT8_MAX)
360 ether_addr.addr_bytes[i] = (uint8_t)o[i];
362 /* Support the format XXXX:XXXX:XXXX */
363 } else if (i == RTE_ETHER_ADDR_LEN / 2) {
365 if (o[i] > UINT16_MAX)
367 ether_addr.addr_bytes[i * 2] = (uint8_t)(o[i] >> 8);
368 ether_addr.addr_bytes[i * 2 + 1] = (uint8_t)(o[i] & 0xff);
374 return (struct rte_ether_addr *)ðer_addr;
378 parse_ipv4_addr(const char *token, struct in_addr *ipv4)
380 if (strlen(token) >= INET_ADDRSTRLEN)
383 if (inet_pton(AF_INET, token, ipv4) != 1)
390 parse_ipv6_addr(const char *token, struct in6_addr *ipv6)
392 if (strlen(token) >= INET6_ADDRSTRLEN)
395 if (inet_pton(AF_INET6, token, ipv6) != 1)
402 parse_mac_addr(const char *token, struct rte_ether_addr *addr)
404 struct rte_ether_addr *tmp;
406 tmp = my_ether_aton(token);
410 memcpy(addr, tmp, sizeof(struct rte_ether_addr));
415 parse_cpu_core(const char *entry,
416 struct cpu_core_params *p)
421 uint32_t s = 0, c = 0, h = 0, val;
422 uint8_t s_parsed = 0, c_parsed = 0, h_parsed = 0;
423 const char *next = skip_white_spaces(entry);
429 /* Expect <CORE> or [sX][cY][h]. At least one parameter is required. */
430 while (*next != '\0') {
431 /* If everything parsed nothing should left */
432 if (s_parsed && c_parsed && h_parsed)
439 if (s_parsed || c_parsed || h_parsed)
446 if (c_parsed || h_parsed)
459 /* If it start from digit it must be only core id. */
460 if (!isdigit(*next) || s_parsed || c_parsed || h_parsed)
466 for (num_len = 0; *next != '\0'; next++, num_len++) {
467 if (num_len == RTE_DIM(num))
473 num[num_len] = *next;
476 if (num_len == 0 && type != 'h' && type != 'H')
479 if (num_len != 0 && (type == 'h' || type == 'H'))
483 val = strtol(num, NULL, 10);