1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
8 * For inet_pton4() and inet_pton6() functions:
10 * Copyright (c) 1996 by Internet Software Consortium.
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
17 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
19 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
21 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
22 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
38 #include <rte_errno.h>
39 #include <rte_string_fns.h>
47 case '0': case '1': case '2': case '3': case '4': case '5':
48 case '6': case '7': case '8': case '9':
50 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
52 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
60 parser_read_arg_bool(const char *p)
62 p = skip_white_spaces(p);
65 if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) ||
66 ((p[0] == 'Y') && (p[1] == 'E') && (p[2] == 'S'))) {
71 if (((p[0] == 'o') && (p[1] == 'n')) ||
72 ((p[0] == 'O') && (p[1] == 'N'))) {
77 if (((p[0] == 'n') && (p[1] == 'o')) ||
78 ((p[0] == 'N') && (p[1] == 'O'))) {
83 if (((p[0] == 'o') && (p[1] == 'f') && (p[2] == 'f')) ||
84 ((p[0] == 'O') && (p[1] == 'F') && (p[2] == 'F'))) {
89 p = skip_white_spaces(p);
98 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 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 parser_read_uint32(uint32_t *value, const char *p)
161 int ret = parser_read_uint64(&val, p);
166 if (val > UINT32_MAX)
174 parser_read_uint32_hex(uint32_t *value, const char *p)
177 int ret = parser_read_uint64_hex(&val, p);
182 if (val > UINT32_MAX)
190 parser_read_uint16(uint16_t *value, const char *p)
193 int ret = parser_read_uint64(&val, p);
198 if (val > UINT16_MAX)
206 parser_read_uint16_hex(uint16_t *value, const char *p)
209 int ret = parser_read_uint64_hex(&val, p);
214 if (val > UINT16_MAX)
222 parser_read_uint8(uint8_t *value, const char *p)
225 int ret = parser_read_uint64(&val, p);
238 parser_read_uint8_hex(uint8_t *value, const char *p)
241 int ret = parser_read_uint64_hex(&val, p);
254 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 (NULL != strtok_r(string, PARSE_DELIMITER, &string)))
278 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 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;
355 * inet_pton4(src, dst)
356 * like inet_aton() but without all the hexadecimal and shorthand.
358 * 1 if `src' is a valid dotted quad, else 0.
360 * does not touch `dst' unless it's returning 1.
365 inet_pton4(const char *src, unsigned char *dst)
367 static const char digits[] = "0123456789";
368 int saw_digit, octets, ch;
369 unsigned char tmp[INADDRSZ], *tp;
374 while ((ch = *src++) != '\0') {
377 pch = strchr(digits, ch);
379 unsigned int new = *tp * 10 + (pch - digits);
388 *tp = (unsigned char)new;
389 } else if (ch == '.' && saw_digit) {
400 memcpy(dst, tmp, INADDRSZ);
405 * inet_pton6(src, dst)
406 * convert presentation level address to network order binary form.
408 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
410 * (1) does not touch `dst' unless it's returning 1.
411 * (2) :: in a full address is silently ignored.
413 * inspired by Mark Andrews.
418 inet_pton6(const char *src, unsigned char *dst)
420 static const char xdigits_l[] = "0123456789abcdef",
421 xdigits_u[] = "0123456789ABCDEF";
422 unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
423 const char *xdigits = 0, *curtok = 0;
424 int ch = 0, saw_xdigit = 0, count_xdigit = 0;
425 unsigned int val = 0;
426 unsigned dbloct_count = 0;
428 memset((tp = tmp), '\0', IN6ADDRSZ);
429 endp = tp + IN6ADDRSZ;
431 /* Leading :: requires some special handling. */
436 saw_xdigit = count_xdigit = 0;
439 while ((ch = *src++) != '\0') {
442 pch = strchr((xdigits = xdigits_l), ch);
444 pch = strchr((xdigits = xdigits_u), ch);
446 if (count_xdigit >= 4)
449 val |= (pch - xdigits);
463 } else if (*src == '\0') {
466 if (tp + sizeof(int16_t) > endp)
468 *tp++ = (unsigned char) ((val >> 8) & 0xff);
469 *tp++ = (unsigned char) (val & 0xff);
476 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
477 inet_pton4(curtok, tp) > 0) {
481 break; /* '\0' was seen by inet_pton4(). */
486 if (tp + sizeof(int16_t) > endp)
488 *tp++ = (unsigned char) ((val >> 8) & 0xff);
489 *tp++ = (unsigned char) (val & 0xff);
492 if (colonp != NULL) {
493 /* if we already have 8 double octets, having a colon means error */
494 if (dbloct_count == 8)
498 * Since some memmove()'s erroneously fail to handle
499 * overlapping regions, we'll do the shift by hand.
501 const int n = tp - colonp;
504 for (i = 1; i <= n; i++) {
505 endp[-i] = colonp[n - i];
512 memcpy(dst, tmp, IN6ADDRSZ);
516 static struct rte_ether_addr *
517 my_ether_aton(const char *a)
521 unsigned long o[RTE_ETHER_ADDR_LEN];
522 static struct rte_ether_addr ether_addr;
527 o[i] = strtoul(a, &end, 16);
528 if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
531 } while (++i != RTE_DIM(o) && end[0] != 0);
533 /* Junk at the end of line */
537 /* Support the format XX:XX:XX:XX:XX:XX */
538 if (i == RTE_ETHER_ADDR_LEN) {
540 if (o[i] > UINT8_MAX)
542 ether_addr.addr_bytes[i] = (uint8_t)o[i];
544 /* Support the format XXXX:XXXX:XXXX */
545 } else if (i == RTE_ETHER_ADDR_LEN / 2) {
547 if (o[i] > UINT16_MAX)
549 ether_addr.addr_bytes[i * 2] = (uint8_t)(o[i] >> 8);
550 ether_addr.addr_bytes[i * 2 + 1] = (uint8_t)(o[i] & 0xff);
556 return (struct rte_ether_addr *)ðer_addr;
560 parse_ipv4_addr(const char *token, struct in_addr *ipv4)
562 if (strlen(token) >= INET_ADDRSTRLEN)
565 if (inet_pton4(token, (unsigned char *)ipv4) != 1)
572 parse_ipv6_addr(const char *token, struct in6_addr *ipv6)
574 if (strlen(token) >= INET6_ADDRSTRLEN)
577 if (inet_pton6(token, (unsigned char *)ipv6) != 1)
584 parse_mac_addr(const char *token, struct rte_ether_addr *addr)
586 struct rte_ether_addr *tmp;
588 tmp = my_ether_aton(token);
592 memcpy(addr, tmp, sizeof(struct rte_ether_addr));
597 parse_cpu_core(const char *entry,
598 struct cpu_core_params *p)
603 uint32_t s = 0, c = 0, h = 0, val;
604 uint8_t s_parsed = 0, c_parsed = 0, h_parsed = 0;
605 const char *next = skip_white_spaces(entry);
611 /* Expect <CORE> or [sX][cY][h]. At least one parameter is required. */
612 while (*next != '\0') {
613 /* If everything parsed nothing should left */
614 if (s_parsed && c_parsed && h_parsed)
621 if (s_parsed || c_parsed || h_parsed)
628 if (c_parsed || h_parsed)
641 /* If it start from digit it must be only core id. */
642 if (!isdigit(*next) || s_parsed || c_parsed || h_parsed)
648 for (num_len = 0; *next != '\0'; next++, num_len++) {
649 if (num_len == RTE_DIM(num))
655 num[num_len] = *next;
658 if (num_len == 0 && type != 'h' && type != 'H')
661 if (num_len != 0 && (type == 'h' || type == 'H'))
665 val = strtol(num, NULL, 10);