Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation.
3 : : * Copyright (c) 2010, Keith Wiles <keith.wiles@windriver.com>
4 : : * All rights reserved.
5 : : */
6 : :
7 : : #include <stdio.h>
8 : : #include <stdlib.h>
9 : : #include <string.h>
10 : : #include <errno.h>
11 : :
12 : : #include <rte_string_fns.h>
13 : : #include "cmdline_parse.h"
14 : : #include "cmdline_parse_portlist.h"
15 : :
16 : : struct cmdline_token_ops cmdline_token_portlist_ops = {
17 : : .parse = cmdline_parse_portlist,
18 : : .complete_get_nb = NULL,
19 : : .complete_get_elt = NULL,
20 : : .get_help = cmdline_get_help_portlist,
21 : : };
22 : :
23 : : static void
24 : : parse_set_list(cmdline_portlist_t *pl, size_t low, size_t high)
25 : : {
26 : : do {
27 : 447 : pl->map |= (1 << low++);
28 [ + + ]: 447 : } while (low <= high);
29 : : }
30 : :
31 : : static int
32 : 39 : parse_ports(cmdline_portlist_t *pl, const char *str)
33 : : {
34 : : size_t ps, pe;
35 : : const char *first, *last;
36 : : char *end;
37 : :
38 : 39 : for (first = str, last = first;
39 [ + + ]: 98 : first != NULL && last != NULL;
40 : 59 : first = last + 1) {
41 : :
42 : 76 : last = strchr(first, ',');
43 : :
44 : 76 : errno = 0;
45 : 76 : ps = strtoul(first, &end, 10);
46 [ + - + + ]: 76 : if (errno != 0 || end == first ||
47 [ + + + - ]: 65 : (end[0] != '-' && end[0] != 0 && end != last))
48 : : return -1;
49 : :
50 : : /* Support for N-M portlist format */
51 [ + + ]: 65 : if (end[0] == '-') {
52 : 21 : errno = 0;
53 : 21 : first = end + 1;
54 : 21 : pe = strtoul(first, &end, 10);
55 [ + - + + ]: 21 : if (errno != 0 || end == first ||
56 [ + + + + ]: 18 : (end[0] != 0 && end != last))
57 : : return -1;
58 : : } else {
59 : : pe = ps;
60 : : }
61 : :
62 [ + + ]: 61 : if (ps > pe || pe >= sizeof (pl->map) * 8)
63 : : return -1;
64 : :
65 : : parse_set_list(pl, ps, pe);
66 : : }
67 : :
68 : : return 0;
69 : : }
70 : :
71 : : int
72 : 52 : cmdline_parse_portlist(__rte_unused cmdline_parse_token_hdr_t *tk,
73 : : const char *buf, void *res, unsigned ressize)
74 : : {
75 : : unsigned int token_len = 0;
76 : : char portlist_str[PORTLIST_TOKEN_SIZE+1];
77 : : cmdline_portlist_t *pl;
78 : :
79 [ + + + + ]: 52 : if (!buf || ! *buf)
80 : : return -1;
81 : :
82 [ + - ]: 48 : if (res && ressize < sizeof(cmdline_portlist_t))
83 : : return -1;
84 : :
85 : : pl = res;
86 : :
87 [ + + + + ]: 403 : while (!cmdline_isendoftoken(buf[token_len]) &&
88 : : (token_len < PORTLIST_TOKEN_SIZE))
89 : 355 : token_len++;
90 : :
91 [ + + ]: 48 : if (token_len >= PORTLIST_TOKEN_SIZE)
92 : : return -1;
93 : :
94 [ + + ]: 47 : strlcpy(portlist_str, buf, token_len + 1);
95 : :
96 [ + + ]: 47 : if (pl) {
97 : 46 : pl->map = 0;
98 [ + + ]: 46 : if (strcmp("all", portlist_str) == 0)
99 : 7 : pl->map = UINT32_MAX;
100 [ + + ]: 39 : else if (parse_ports(pl, portlist_str) != 0)
101 : : return -1;
102 : : }
103 : :
104 : 30 : return token_len;
105 : : }
106 : :
107 : : int
108 : 1 : cmdline_get_help_portlist(__rte_unused cmdline_parse_token_hdr_t *tk,
109 : : char *dstbuf, unsigned int size)
110 : : {
111 : : int ret;
112 [ - + ]: 1 : ret = snprintf(dstbuf, size, "range of ports as 3,4-6,8-19,20");
113 [ - + ]: 1 : if (ret < 0)
114 : 0 : return -1;
115 : : return 0;
116 : : }
|