131ce8d88SJasvinder Singh /* SPDX-License-Identifier: BSD-3-Clause
231ce8d88SJasvinder Singh * Copyright(c) 2016 Intel Corporation.
331ce8d88SJasvinder Singh * Copyright (c) 2009, Olivier MATZ <[email protected]>
431ce8d88SJasvinder Singh * All rights reserved.
531ce8d88SJasvinder Singh */
631ce8d88SJasvinder Singh
731ce8d88SJasvinder Singh #include <stdint.h>
831ce8d88SJasvinder Singh #include <stdlib.h>
931ce8d88SJasvinder Singh #include <stdio.h>
1031ce8d88SJasvinder Singh #include <ctype.h>
1131ce8d88SJasvinder Singh #include <getopt.h>
1231ce8d88SJasvinder Singh #include <errno.h>
1331ce8d88SJasvinder Singh #include <stdarg.h>
1431ce8d88SJasvinder Singh #include <string.h>
1531ce8d88SJasvinder Singh #include <libgen.h>
1631ce8d88SJasvinder Singh #include <unistd.h>
1731ce8d88SJasvinder Singh #include <sys/wait.h>
18*4a91344bSIbtisam Tariq #include <arpa/inet.h>
19*4a91344bSIbtisam Tariq #include <sys/socket.h>
2031ce8d88SJasvinder Singh
2131ce8d88SJasvinder Singh #include <rte_errno.h>
2231ce8d88SJasvinder Singh
2331ce8d88SJasvinder Singh #include "parser.h"
2431ce8d88SJasvinder Singh
2531ce8d88SJasvinder Singh static uint32_t
get_hex_val(char c)2631ce8d88SJasvinder Singh get_hex_val(char c)
2731ce8d88SJasvinder Singh {
2831ce8d88SJasvinder Singh switch (c) {
2931ce8d88SJasvinder Singh case '0': case '1': case '2': case '3': case '4': case '5':
3031ce8d88SJasvinder Singh case '6': case '7': case '8': case '9':
3131ce8d88SJasvinder Singh return c - '0';
3231ce8d88SJasvinder Singh case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3331ce8d88SJasvinder Singh return c - 'A' + 10;
3431ce8d88SJasvinder Singh case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3531ce8d88SJasvinder Singh return c - 'a' + 10;
3631ce8d88SJasvinder Singh default:
3731ce8d88SJasvinder Singh return 0;
3831ce8d88SJasvinder Singh }
3931ce8d88SJasvinder Singh }
4031ce8d88SJasvinder Singh
4131ce8d88SJasvinder Singh int
softnic_parser_read_arg_bool(const char * p)4231ce8d88SJasvinder Singh softnic_parser_read_arg_bool(const char *p)
4331ce8d88SJasvinder Singh {
4431ce8d88SJasvinder Singh p = skip_white_spaces(p);
4531ce8d88SJasvinder Singh int result = -EINVAL;
4631ce8d88SJasvinder Singh
4731ce8d88SJasvinder Singh if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) ||
4831ce8d88SJasvinder Singh ((p[0] == 'Y') && (p[1] == 'E') && (p[2] == 'S'))) {
4931ce8d88SJasvinder Singh p += 3;
5031ce8d88SJasvinder Singh result = 1;
5131ce8d88SJasvinder Singh }
5231ce8d88SJasvinder Singh
5331ce8d88SJasvinder Singh if (((p[0] == 'o') && (p[1] == 'n')) ||
5431ce8d88SJasvinder Singh ((p[0] == 'O') && (p[1] == 'N'))) {
5531ce8d88SJasvinder Singh p += 2;
5631ce8d88SJasvinder Singh result = 1;
5731ce8d88SJasvinder Singh }
5831ce8d88SJasvinder Singh
5931ce8d88SJasvinder Singh if (((p[0] == 'n') && (p[1] == 'o')) ||
6031ce8d88SJasvinder Singh ((p[0] == 'N') && (p[1] == 'O'))) {
6131ce8d88SJasvinder Singh p += 2;
6231ce8d88SJasvinder Singh result = 0;
6331ce8d88SJasvinder Singh }
6431ce8d88SJasvinder Singh
6531ce8d88SJasvinder Singh if (((p[0] == 'o') && (p[1] == 'f') && (p[2] == 'f')) ||
6631ce8d88SJasvinder Singh ((p[0] == 'O') && (p[1] == 'F') && (p[2] == 'F'))) {
6731ce8d88SJasvinder Singh p += 3;
6831ce8d88SJasvinder Singh result = 0;
6931ce8d88SJasvinder Singh }
7031ce8d88SJasvinder Singh
7131ce8d88SJasvinder Singh p = skip_white_spaces(p);
7231ce8d88SJasvinder Singh
7331ce8d88SJasvinder Singh if (p[0] != '\0')
7431ce8d88SJasvinder Singh return -EINVAL;
7531ce8d88SJasvinder Singh
7631ce8d88SJasvinder Singh return result;
7731ce8d88SJasvinder Singh }
7831ce8d88SJasvinder Singh
7931ce8d88SJasvinder Singh int
softnic_parser_read_int32(int32_t * value,const char * p)80552e3f72SCristian Dumitrescu softnic_parser_read_int32(int32_t *value, const char *p)
81552e3f72SCristian Dumitrescu {
82552e3f72SCristian Dumitrescu char *next;
83552e3f72SCristian Dumitrescu int32_t val;
84552e3f72SCristian Dumitrescu
85552e3f72SCristian Dumitrescu p = skip_white_spaces(p);
86552e3f72SCristian Dumitrescu if (!isdigit(*p))
87552e3f72SCristian Dumitrescu return -EINVAL;
88552e3f72SCristian Dumitrescu
89552e3f72SCristian Dumitrescu val = strtol(p, &next, 10);
90552e3f72SCristian Dumitrescu if (p == next)
91552e3f72SCristian Dumitrescu return -EINVAL;
92552e3f72SCristian Dumitrescu
93552e3f72SCristian Dumitrescu *value = val;
94552e3f72SCristian Dumitrescu return 0;
95552e3f72SCristian Dumitrescu }
96552e3f72SCristian Dumitrescu
97552e3f72SCristian Dumitrescu int
softnic_parser_read_uint64(uint64_t * value,const char * p)9831ce8d88SJasvinder Singh softnic_parser_read_uint64(uint64_t *value, const char *p)
9931ce8d88SJasvinder Singh {
10031ce8d88SJasvinder Singh char *next;
10131ce8d88SJasvinder Singh uint64_t val;
10231ce8d88SJasvinder Singh
10331ce8d88SJasvinder Singh p = skip_white_spaces(p);
10431ce8d88SJasvinder Singh if (!isdigit(*p))
10531ce8d88SJasvinder Singh return -EINVAL;
10631ce8d88SJasvinder Singh
10731ce8d88SJasvinder Singh val = strtoul(p, &next, 10);
10831ce8d88SJasvinder Singh if (p == next)
10931ce8d88SJasvinder Singh return -EINVAL;
11031ce8d88SJasvinder Singh
11131ce8d88SJasvinder Singh p = next;
11231ce8d88SJasvinder Singh switch (*p) {
11331ce8d88SJasvinder Singh case 'T':
11431ce8d88SJasvinder Singh val *= 1024ULL;
11531ce8d88SJasvinder Singh /* fall through */
11631ce8d88SJasvinder Singh case 'G':
11731ce8d88SJasvinder Singh val *= 1024ULL;
11831ce8d88SJasvinder Singh /* fall through */
11931ce8d88SJasvinder Singh case 'M':
12031ce8d88SJasvinder Singh val *= 1024ULL;
12131ce8d88SJasvinder Singh /* fall through */
12231ce8d88SJasvinder Singh case 'k':
12331ce8d88SJasvinder Singh case 'K':
12431ce8d88SJasvinder Singh val *= 1024ULL;
12531ce8d88SJasvinder Singh p++;
12631ce8d88SJasvinder Singh break;
12731ce8d88SJasvinder Singh }
12831ce8d88SJasvinder Singh
12931ce8d88SJasvinder Singh p = skip_white_spaces(p);
13031ce8d88SJasvinder Singh if (*p != '\0')
13131ce8d88SJasvinder Singh return -EINVAL;
13231ce8d88SJasvinder Singh
13331ce8d88SJasvinder Singh *value = val;
13431ce8d88SJasvinder Singh return 0;
13531ce8d88SJasvinder Singh }
13631ce8d88SJasvinder Singh
13731ce8d88SJasvinder Singh int
softnic_parser_read_uint64_hex(uint64_t * value,const char * p)13831ce8d88SJasvinder Singh softnic_parser_read_uint64_hex(uint64_t *value, const char *p)
13931ce8d88SJasvinder Singh {
14031ce8d88SJasvinder Singh char *next;
14131ce8d88SJasvinder Singh uint64_t val;
14231ce8d88SJasvinder Singh
14331ce8d88SJasvinder Singh p = skip_white_spaces(p);
14431ce8d88SJasvinder Singh
14531ce8d88SJasvinder Singh val = strtoul(p, &next, 16);
14631ce8d88SJasvinder Singh if (p == next)
14731ce8d88SJasvinder Singh return -EINVAL;
14831ce8d88SJasvinder Singh
14931ce8d88SJasvinder Singh p = skip_white_spaces(next);
15031ce8d88SJasvinder Singh if (*p != '\0')
15131ce8d88SJasvinder Singh return -EINVAL;
15231ce8d88SJasvinder Singh
15331ce8d88SJasvinder Singh *value = val;
15431ce8d88SJasvinder Singh return 0;
15531ce8d88SJasvinder Singh }
15631ce8d88SJasvinder Singh
15731ce8d88SJasvinder Singh int
softnic_parser_read_uint32(uint32_t * value,const char * p)15831ce8d88SJasvinder Singh softnic_parser_read_uint32(uint32_t *value, const char *p)
15931ce8d88SJasvinder Singh {
16031ce8d88SJasvinder Singh uint64_t val = 0;
16131ce8d88SJasvinder Singh int ret = softnic_parser_read_uint64(&val, p);
16231ce8d88SJasvinder Singh
16331ce8d88SJasvinder Singh if (ret < 0)
16431ce8d88SJasvinder Singh return ret;
16531ce8d88SJasvinder Singh
16631ce8d88SJasvinder Singh if (val > UINT32_MAX)
16731ce8d88SJasvinder Singh return -ERANGE;
16831ce8d88SJasvinder Singh
16931ce8d88SJasvinder Singh *value = val;
17031ce8d88SJasvinder Singh return 0;
17131ce8d88SJasvinder Singh }
17231ce8d88SJasvinder Singh
17331ce8d88SJasvinder Singh int
softnic_parser_read_uint32_hex(uint32_t * value,const char * p)17431ce8d88SJasvinder Singh softnic_parser_read_uint32_hex(uint32_t *value, const char *p)
17531ce8d88SJasvinder Singh {
17631ce8d88SJasvinder Singh uint64_t val = 0;
17731ce8d88SJasvinder Singh int ret = softnic_parser_read_uint64_hex(&val, p);
17831ce8d88SJasvinder Singh
17931ce8d88SJasvinder Singh if (ret < 0)
18031ce8d88SJasvinder Singh return ret;
18131ce8d88SJasvinder Singh
18231ce8d88SJasvinder Singh if (val > UINT32_MAX)
18331ce8d88SJasvinder Singh return -ERANGE;
18431ce8d88SJasvinder Singh
18531ce8d88SJasvinder Singh *value = val;
18631ce8d88SJasvinder Singh return 0;
18731ce8d88SJasvinder Singh }
18831ce8d88SJasvinder Singh
18931ce8d88SJasvinder Singh int
softnic_parser_read_uint16(uint16_t * value,const char * p)19031ce8d88SJasvinder Singh softnic_parser_read_uint16(uint16_t *value, const char *p)
19131ce8d88SJasvinder Singh {
19231ce8d88SJasvinder Singh uint64_t val = 0;
19331ce8d88SJasvinder Singh int ret = softnic_parser_read_uint64(&val, p);
19431ce8d88SJasvinder Singh
19531ce8d88SJasvinder Singh if (ret < 0)
19631ce8d88SJasvinder Singh return ret;
19731ce8d88SJasvinder Singh
19831ce8d88SJasvinder Singh if (val > UINT16_MAX)
19931ce8d88SJasvinder Singh return -ERANGE;
20031ce8d88SJasvinder Singh
20131ce8d88SJasvinder Singh *value = val;
20231ce8d88SJasvinder Singh return 0;
20331ce8d88SJasvinder Singh }
20431ce8d88SJasvinder Singh
20531ce8d88SJasvinder Singh int
softnic_parser_read_uint16_hex(uint16_t * value,const char * p)20631ce8d88SJasvinder Singh softnic_parser_read_uint16_hex(uint16_t *value, const char *p)
20731ce8d88SJasvinder Singh {
20831ce8d88SJasvinder Singh uint64_t val = 0;
20931ce8d88SJasvinder Singh int ret = softnic_parser_read_uint64_hex(&val, p);
21031ce8d88SJasvinder Singh
21131ce8d88SJasvinder Singh if (ret < 0)
21231ce8d88SJasvinder Singh return ret;
21331ce8d88SJasvinder Singh
21431ce8d88SJasvinder Singh if (val > UINT16_MAX)
21531ce8d88SJasvinder Singh return -ERANGE;
21631ce8d88SJasvinder Singh
21731ce8d88SJasvinder Singh *value = val;
21831ce8d88SJasvinder Singh return 0;
21931ce8d88SJasvinder Singh }
22031ce8d88SJasvinder Singh
22131ce8d88SJasvinder Singh int
softnic_parser_read_uint8(uint8_t * value,const char * p)22231ce8d88SJasvinder Singh softnic_parser_read_uint8(uint8_t *value, const char *p)
22331ce8d88SJasvinder Singh {
22431ce8d88SJasvinder Singh uint64_t val = 0;
22531ce8d88SJasvinder Singh int ret = softnic_parser_read_uint64(&val, p);
22631ce8d88SJasvinder Singh
22731ce8d88SJasvinder Singh if (ret < 0)
22831ce8d88SJasvinder Singh return ret;
22931ce8d88SJasvinder Singh
23031ce8d88SJasvinder Singh if (val > UINT8_MAX)
23131ce8d88SJasvinder Singh return -ERANGE;
23231ce8d88SJasvinder Singh
23331ce8d88SJasvinder Singh *value = val;
23431ce8d88SJasvinder Singh return 0;
23531ce8d88SJasvinder Singh }
23631ce8d88SJasvinder Singh
23731ce8d88SJasvinder Singh int
softnic_parser_read_uint8_hex(uint8_t * value,const char * p)23831ce8d88SJasvinder Singh softnic_parser_read_uint8_hex(uint8_t *value, const char *p)
23931ce8d88SJasvinder Singh {
24031ce8d88SJasvinder Singh uint64_t val = 0;
24131ce8d88SJasvinder Singh int ret = softnic_parser_read_uint64_hex(&val, p);
24231ce8d88SJasvinder Singh
24331ce8d88SJasvinder Singh if (ret < 0)
24431ce8d88SJasvinder Singh return ret;
24531ce8d88SJasvinder Singh
24631ce8d88SJasvinder Singh if (val > UINT8_MAX)
24731ce8d88SJasvinder Singh return -ERANGE;
24831ce8d88SJasvinder Singh
24931ce8d88SJasvinder Singh *value = val;
25031ce8d88SJasvinder Singh return 0;
25131ce8d88SJasvinder Singh }
25231ce8d88SJasvinder Singh
25331ce8d88SJasvinder Singh int
softnic_parse_tokenize_string(char * string,char * tokens[],uint32_t * n_tokens)25431ce8d88SJasvinder Singh softnic_parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
25531ce8d88SJasvinder Singh {
25631ce8d88SJasvinder Singh uint32_t i;
25731ce8d88SJasvinder Singh
25831ce8d88SJasvinder Singh if (string == NULL ||
25931ce8d88SJasvinder Singh tokens == NULL ||
26031ce8d88SJasvinder Singh (*n_tokens < 1))
26131ce8d88SJasvinder Singh return -EINVAL;
26231ce8d88SJasvinder Singh
26331ce8d88SJasvinder Singh for (i = 0; i < *n_tokens; i++) {
26431ce8d88SJasvinder Singh tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
26531ce8d88SJasvinder Singh if (tokens[i] == NULL)
26631ce8d88SJasvinder Singh break;
26731ce8d88SJasvinder Singh }
26831ce8d88SJasvinder Singh
26931ce8d88SJasvinder Singh if (i == *n_tokens &&
27031ce8d88SJasvinder Singh strtok_r(string, PARSE_DELIMITER, &string) != NULL)
27131ce8d88SJasvinder Singh return -E2BIG;
27231ce8d88SJasvinder Singh
27331ce8d88SJasvinder Singh *n_tokens = i;
27431ce8d88SJasvinder Singh return 0;
27531ce8d88SJasvinder Singh }
27631ce8d88SJasvinder Singh
27731ce8d88SJasvinder Singh int
softnic_parse_hex_string(char * src,uint8_t * dst,uint32_t * size)27831ce8d88SJasvinder Singh softnic_parse_hex_string(char *src, uint8_t *dst, uint32_t *size)
27931ce8d88SJasvinder Singh {
28031ce8d88SJasvinder Singh char *c;
28131ce8d88SJasvinder Singh uint32_t len, i;
28231ce8d88SJasvinder Singh
28331ce8d88SJasvinder Singh /* Check input parameters */
28431ce8d88SJasvinder Singh if (src == NULL ||
28531ce8d88SJasvinder Singh dst == NULL ||
28631ce8d88SJasvinder Singh size == NULL ||
28731ce8d88SJasvinder Singh (*size == 0))
28831ce8d88SJasvinder Singh return -1;
28931ce8d88SJasvinder Singh
29031ce8d88SJasvinder Singh len = strlen(src);
29131ce8d88SJasvinder Singh if (((len & 3) != 0) ||
29231ce8d88SJasvinder Singh (len > (*size) * 2))
29331ce8d88SJasvinder Singh return -1;
29431ce8d88SJasvinder Singh *size = len / 2;
29531ce8d88SJasvinder Singh
29631ce8d88SJasvinder Singh for (c = src; *c != 0; c++) {
29731ce8d88SJasvinder Singh if ((((*c) >= '0') && ((*c) <= '9')) ||
29831ce8d88SJasvinder Singh (((*c) >= 'A') && ((*c) <= 'F')) ||
29931ce8d88SJasvinder Singh (((*c) >= 'a') && ((*c) <= 'f')))
30031ce8d88SJasvinder Singh continue;
30131ce8d88SJasvinder Singh
30231ce8d88SJasvinder Singh return -1;
30331ce8d88SJasvinder Singh }
30431ce8d88SJasvinder Singh
30531ce8d88SJasvinder Singh /* Convert chars to bytes */
30631ce8d88SJasvinder Singh for (i = 0; i < *size; i++)
30731ce8d88SJasvinder Singh dst[i] = get_hex_val(src[2 * i]) * 16 +
30831ce8d88SJasvinder Singh get_hex_val(src[2 * i + 1]);
30931ce8d88SJasvinder Singh
31031ce8d88SJasvinder Singh return 0;
31131ce8d88SJasvinder Singh }
31231ce8d88SJasvinder Singh
31331ce8d88SJasvinder Singh int
softnic_parse_mpls_labels(char * string,uint32_t * labels,uint32_t * n_labels)31431ce8d88SJasvinder Singh softnic_parse_mpls_labels(char *string, uint32_t *labels, uint32_t *n_labels)
31531ce8d88SJasvinder Singh {
31631ce8d88SJasvinder Singh uint32_t n_max_labels = *n_labels, count = 0;
31731ce8d88SJasvinder Singh
31831ce8d88SJasvinder Singh /* Check for void list of labels */
31931ce8d88SJasvinder Singh if (strcmp(string, "<void>") == 0) {
32031ce8d88SJasvinder Singh *n_labels = 0;
32131ce8d88SJasvinder Singh return 0;
32231ce8d88SJasvinder Singh }
32331ce8d88SJasvinder Singh
32431ce8d88SJasvinder Singh /* At least one label should be present */
32531ce8d88SJasvinder Singh for ( ; (*string != '\0'); ) {
32631ce8d88SJasvinder Singh char *next;
32731ce8d88SJasvinder Singh int value;
32831ce8d88SJasvinder Singh
32931ce8d88SJasvinder Singh if (count >= n_max_labels)
33031ce8d88SJasvinder Singh return -1;
33131ce8d88SJasvinder Singh
33231ce8d88SJasvinder Singh if (count > 0) {
33331ce8d88SJasvinder Singh if (string[0] != ':')
33431ce8d88SJasvinder Singh return -1;
33531ce8d88SJasvinder Singh
33631ce8d88SJasvinder Singh string++;
33731ce8d88SJasvinder Singh }
33831ce8d88SJasvinder Singh
33931ce8d88SJasvinder Singh value = strtol(string, &next, 10);
34031ce8d88SJasvinder Singh if (next == string)
34131ce8d88SJasvinder Singh return -1;
34231ce8d88SJasvinder Singh string = next;
34331ce8d88SJasvinder Singh
34431ce8d88SJasvinder Singh labels[count++] = (uint32_t)value;
34531ce8d88SJasvinder Singh }
34631ce8d88SJasvinder Singh
34731ce8d88SJasvinder Singh *n_labels = count;
34831ce8d88SJasvinder Singh return 0;
34931ce8d88SJasvinder Singh }
35031ce8d88SJasvinder Singh
3516d13ea8eSOlivier Matz static struct rte_ether_addr *
my_ether_aton(const char * a)35231ce8d88SJasvinder Singh my_ether_aton(const char *a)
35331ce8d88SJasvinder Singh {
35431ce8d88SJasvinder Singh int i;
35531ce8d88SJasvinder Singh char *end;
35635b2d13fSOlivier Matz unsigned long o[RTE_ETHER_ADDR_LEN];
3576d13ea8eSOlivier Matz static struct rte_ether_addr ether_addr;
35831ce8d88SJasvinder Singh
35931ce8d88SJasvinder Singh i = 0;
36031ce8d88SJasvinder Singh do {
36131ce8d88SJasvinder Singh errno = 0;
36231ce8d88SJasvinder Singh o[i] = strtoul(a, &end, 16);
36331ce8d88SJasvinder Singh if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
36431ce8d88SJasvinder Singh return NULL;
36531ce8d88SJasvinder Singh a = end + 1;
36631ce8d88SJasvinder Singh } while (++i != sizeof(o) / sizeof(o[0]) && end[0] != 0);
36731ce8d88SJasvinder Singh
36831ce8d88SJasvinder Singh /* Junk at the end of line */
36931ce8d88SJasvinder Singh if (end[0] != 0)
37031ce8d88SJasvinder Singh return NULL;
37131ce8d88SJasvinder Singh
37231ce8d88SJasvinder Singh /* Support the format XX:XX:XX:XX:XX:XX */
37335b2d13fSOlivier Matz if (i == RTE_ETHER_ADDR_LEN) {
37431ce8d88SJasvinder Singh while (i-- != 0) {
37531ce8d88SJasvinder Singh if (o[i] > UINT8_MAX)
37631ce8d88SJasvinder Singh return NULL;
37731ce8d88SJasvinder Singh ether_addr.addr_bytes[i] = (uint8_t)o[i];
37831ce8d88SJasvinder Singh }
37931ce8d88SJasvinder Singh /* Support the format XXXX:XXXX:XXXX */
38035b2d13fSOlivier Matz } else if (i == RTE_ETHER_ADDR_LEN / 2) {
38131ce8d88SJasvinder Singh while (i-- != 0) {
38231ce8d88SJasvinder Singh if (o[i] > UINT16_MAX)
38331ce8d88SJasvinder Singh return NULL;
38431ce8d88SJasvinder Singh ether_addr.addr_bytes[i * 2] = (uint8_t)(o[i] >> 8);
38531ce8d88SJasvinder Singh ether_addr.addr_bytes[i * 2 + 1] = (uint8_t)(o[i] & 0xff);
38631ce8d88SJasvinder Singh }
38731ce8d88SJasvinder Singh /* unknown format */
38831ce8d88SJasvinder Singh } else
38931ce8d88SJasvinder Singh return NULL;
39031ce8d88SJasvinder Singh
3916d13ea8eSOlivier Matz return (struct rte_ether_addr *)ðer_addr;
39231ce8d88SJasvinder Singh }
39331ce8d88SJasvinder Singh
39431ce8d88SJasvinder Singh int
softnic_parse_ipv4_addr(const char * token,struct in_addr * ipv4)39531ce8d88SJasvinder Singh softnic_parse_ipv4_addr(const char *token, struct in_addr *ipv4)
39631ce8d88SJasvinder Singh {
39731ce8d88SJasvinder Singh if (strlen(token) >= INET_ADDRSTRLEN)
39831ce8d88SJasvinder Singh return -EINVAL;
39931ce8d88SJasvinder Singh
400*4a91344bSIbtisam Tariq if (inet_pton(AF_INET, token, ipv4) != 1)
40131ce8d88SJasvinder Singh return -EINVAL;
40231ce8d88SJasvinder Singh
40331ce8d88SJasvinder Singh return 0;
40431ce8d88SJasvinder Singh }
40531ce8d88SJasvinder Singh
40631ce8d88SJasvinder Singh int
softnic_parse_ipv6_addr(const char * token,struct in6_addr * ipv6)40731ce8d88SJasvinder Singh softnic_parse_ipv6_addr(const char *token, struct in6_addr *ipv6)
40831ce8d88SJasvinder Singh {
40931ce8d88SJasvinder Singh if (strlen(token) >= INET6_ADDRSTRLEN)
41031ce8d88SJasvinder Singh return -EINVAL;
41131ce8d88SJasvinder Singh
412*4a91344bSIbtisam Tariq if (inet_pton(AF_INET6, token, ipv6) != 1)
41331ce8d88SJasvinder Singh return -EINVAL;
41431ce8d88SJasvinder Singh
41531ce8d88SJasvinder Singh return 0;
41631ce8d88SJasvinder Singh }
41731ce8d88SJasvinder Singh
41831ce8d88SJasvinder Singh int
softnic_parse_mac_addr(const char * token,struct rte_ether_addr * addr)4196d13ea8eSOlivier Matz softnic_parse_mac_addr(const char *token, struct rte_ether_addr *addr)
42031ce8d88SJasvinder Singh {
4216d13ea8eSOlivier Matz struct rte_ether_addr *tmp;
42231ce8d88SJasvinder Singh
42331ce8d88SJasvinder Singh tmp = my_ether_aton(token);
42431ce8d88SJasvinder Singh if (tmp == NULL)
42531ce8d88SJasvinder Singh return -1;
42631ce8d88SJasvinder Singh
4276d13ea8eSOlivier Matz memcpy(addr, tmp, sizeof(struct rte_ether_addr));
42831ce8d88SJasvinder Singh return 0;
42931ce8d88SJasvinder Singh }
43031ce8d88SJasvinder Singh
43131ce8d88SJasvinder Singh int
softnic_parse_cpu_core(const char * entry,struct softnic_cpu_core_params * p)43231ce8d88SJasvinder Singh softnic_parse_cpu_core(const char *entry,
43331ce8d88SJasvinder Singh struct softnic_cpu_core_params *p)
43431ce8d88SJasvinder Singh {
43531ce8d88SJasvinder Singh size_t num_len;
43631ce8d88SJasvinder Singh char num[8];
43731ce8d88SJasvinder Singh
43831ce8d88SJasvinder Singh uint32_t s = 0, c = 0, h = 0, val;
43931ce8d88SJasvinder Singh uint8_t s_parsed = 0, c_parsed = 0, h_parsed = 0;
44031ce8d88SJasvinder Singh const char *next = skip_white_spaces(entry);
44131ce8d88SJasvinder Singh char type;
44231ce8d88SJasvinder Singh
44331ce8d88SJasvinder Singh if (p == NULL)
44431ce8d88SJasvinder Singh return -EINVAL;
44531ce8d88SJasvinder Singh
44631ce8d88SJasvinder Singh /* Expect <CORE> or [sX][cY][h]. At least one parameter is required. */
44731ce8d88SJasvinder Singh while (*next != '\0') {
44831ce8d88SJasvinder Singh /* If everything parsed nothing should left */
44931ce8d88SJasvinder Singh if (s_parsed && c_parsed && h_parsed)
45031ce8d88SJasvinder Singh return -EINVAL;
45131ce8d88SJasvinder Singh
45231ce8d88SJasvinder Singh type = *next;
45331ce8d88SJasvinder Singh switch (type) {
45431ce8d88SJasvinder Singh case 's':
45531ce8d88SJasvinder Singh case 'S':
45631ce8d88SJasvinder Singh if (s_parsed || c_parsed || h_parsed)
45731ce8d88SJasvinder Singh return -EINVAL;
45831ce8d88SJasvinder Singh s_parsed = 1;
45931ce8d88SJasvinder Singh next++;
46031ce8d88SJasvinder Singh break;
46131ce8d88SJasvinder Singh case 'c':
46231ce8d88SJasvinder Singh case 'C':
46331ce8d88SJasvinder Singh if (c_parsed || h_parsed)
46431ce8d88SJasvinder Singh return -EINVAL;
46531ce8d88SJasvinder Singh c_parsed = 1;
46631ce8d88SJasvinder Singh next++;
46731ce8d88SJasvinder Singh break;
46831ce8d88SJasvinder Singh case 'h':
46931ce8d88SJasvinder Singh case 'H':
47031ce8d88SJasvinder Singh if (h_parsed)
47131ce8d88SJasvinder Singh return -EINVAL;
47231ce8d88SJasvinder Singh h_parsed = 1;
47331ce8d88SJasvinder Singh next++;
47431ce8d88SJasvinder Singh break;
47531ce8d88SJasvinder Singh default:
47631ce8d88SJasvinder Singh /* If it start from digit it must be only core id. */
47731ce8d88SJasvinder Singh if (!isdigit(*next) || s_parsed || c_parsed || h_parsed)
47831ce8d88SJasvinder Singh return -EINVAL;
47931ce8d88SJasvinder Singh
48031ce8d88SJasvinder Singh type = 'C';
48131ce8d88SJasvinder Singh }
48231ce8d88SJasvinder Singh
48331ce8d88SJasvinder Singh for (num_len = 0; *next != '\0'; next++, num_len++) {
48431ce8d88SJasvinder Singh if (num_len == RTE_DIM(num))
48531ce8d88SJasvinder Singh return -EINVAL;
48631ce8d88SJasvinder Singh
48731ce8d88SJasvinder Singh if (!isdigit(*next))
48831ce8d88SJasvinder Singh break;
48931ce8d88SJasvinder Singh
49031ce8d88SJasvinder Singh num[num_len] = *next;
49131ce8d88SJasvinder Singh }
49231ce8d88SJasvinder Singh
49331ce8d88SJasvinder Singh if (num_len == 0 && type != 'h' && type != 'H')
49431ce8d88SJasvinder Singh return -EINVAL;
49531ce8d88SJasvinder Singh
49631ce8d88SJasvinder Singh if (num_len != 0 && (type == 'h' || type == 'H'))
49731ce8d88SJasvinder Singh return -EINVAL;
49831ce8d88SJasvinder Singh
49931ce8d88SJasvinder Singh num[num_len] = '\0';
50031ce8d88SJasvinder Singh val = strtol(num, NULL, 10);
50131ce8d88SJasvinder Singh
50231ce8d88SJasvinder Singh h = 0;
50331ce8d88SJasvinder Singh switch (type) {
50431ce8d88SJasvinder Singh case 's':
50531ce8d88SJasvinder Singh case 'S':
50631ce8d88SJasvinder Singh s = val;
50731ce8d88SJasvinder Singh break;
50831ce8d88SJasvinder Singh case 'c':
50931ce8d88SJasvinder Singh case 'C':
51031ce8d88SJasvinder Singh c = val;
51131ce8d88SJasvinder Singh break;
51231ce8d88SJasvinder Singh case 'h':
51331ce8d88SJasvinder Singh case 'H':
51431ce8d88SJasvinder Singh h = 1;
51531ce8d88SJasvinder Singh break;
51631ce8d88SJasvinder Singh }
51731ce8d88SJasvinder Singh }
51831ce8d88SJasvinder Singh
51931ce8d88SJasvinder Singh p->socket_id = s;
52031ce8d88SJasvinder Singh p->core_id = c;
52131ce8d88SJasvinder Singh p->thread_id = h;
52231ce8d88SJasvinder Singh return 0;
52331ce8d88SJasvinder Singh }
524