kvargs: remove useless size field
[dpdk.git] / lib / librte_kvargs / rte_kvargs.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <string.h>
35 #include <sys/user.h>
36 #include <linux/binfmts.h>
37
38 #include <rte_malloc.h>
39 #include <rte_log.h>
40 #include <rte_string_fns.h>
41
42 #include "rte_kvargs.h"
43
44 /*
45  * Initialize a rte_kvargs structure to an empty key/value list.
46  */
47 int
48 rte_kvargs_init(struct rte_kvargs *kvlist)
49 {
50         kvlist->count = 0;
51         memset(kvlist->pairs, 0, sizeof(kvlist->pairs));
52         return 0;
53 }
54
55 /*
56  * Add a key-value pair at the end of a given key/value list.
57  * Return an error if the list is full or if the key is duplicated.
58  */
59 static int
60 rte_kvargs_add_pair(struct rte_kvargs *kvlist, char *key, char *val)
61 {
62         unsigned i;
63         struct rte_kvargs_pair* entry;
64
65         /* is the list full? */
66         if (kvlist->count >= RTE_KVARGS_MAX) {
67                 RTE_LOG(ERR, PMD, "Couldn't add %s, key/value list is full\n", key);
68                 return -1;
69         }
70
71         /* Check if the key is duplicated */
72         for (i = 0; i < kvlist->count; i++) {
73                 entry = &kvlist->pairs[i];
74                 if (strcmp(entry->key, key) == 0) {
75                         RTE_LOG(ERR, PMD, "Couldn't add %s, duplicated key\n", key);
76                         return -1;
77                 }
78         }
79
80         entry = &kvlist->pairs[kvlist->count];
81         entry->key = key;
82         entry->value = val;
83         kvlist->count++;
84         return 0;
85 }
86
87 /*
88  * Receive a string with a list of arguments following the pattern
89  * key=value;key=value;... and insert them into the list.
90  * strtok() is used so the params string will be copied to be modified.
91  */
92 static int
93 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
94 {
95         unsigned i, count;
96         char *args;
97         char *pairs[RTE_KVARGS_MAX];
98         char *pair[2];
99
100         /* If params are empty, nothing to do */
101         if (params == NULL || params[0] == 0) {
102                 RTE_LOG(ERR, PMD, "Cannot parse empty arguments\n");
103                 return -1;
104         }
105
106         /* Copy the const char *params to a modifiable string
107          * to pass to rte_strsplit
108          */
109         args = strdup(params);
110         if(args == NULL){
111                 RTE_LOG(ERR, PMD, "Cannot parse arguments: not enough memory\n");
112                 return -1;
113         }
114
115         count = rte_strsplit(args, strnlen(args, MAX_ARG_STRLEN), pairs,
116                         RTE_KVARGS_MAX, RTE_KVARGS_PAIRS_DELIM);
117
118         for (i = 0; i < count; i++) {
119                 pair[0] = NULL;
120                 pair[1] = NULL;
121
122                 rte_strsplit(pairs[i], strnlen(pairs[i], MAX_ARG_STRLEN), pair, 2,
123                                 RTE_KVARGS_KV_DELIM);
124
125                 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
126                                 || pair[1][0] == 0) {
127                         RTE_LOG(ERR, PMD,
128                                 "Cannot parse arguments: wrong key or value\n"
129                                 "params=<%s>\n", params);
130                         goto error;
131                 }
132
133                 if (rte_kvargs_add_pair(kvlist, pair[0], pair[1]) < 0)
134                         goto error;
135         }
136         return 0;
137
138 error:
139         rte_free(args);
140         return -1;
141 }
142
143 /*
144  * Determine whether a key is valid or not by looking
145  * into a list of valid keys.
146  */
147 static int
148 is_valid_key(const char *valid[], const char *key_match)
149 {
150         const char **valid_ptr;
151
152         for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
153                 if (strstr(key_match, *valid_ptr) != NULL)
154                         return 1;
155         return 0;
156 }
157
158 /*
159  * Determine whether all keys are valid or not by looking
160  * into a list of valid keys.
161  */
162 static int
163 check_for_valid_keys(struct rte_kvargs *kvlist,
164                 const char *valid[])
165 {
166         unsigned i, ret;
167         struct rte_kvargs_pair *pair;
168
169         for (i = 0; i < kvlist->count; i++) {
170                 pair = &kvlist->pairs[i];
171                 ret = is_valid_key(valid, pair->key);
172                 if (!ret) {
173                         RTE_LOG(ERR, PMD,
174                                 "Error parsing device, invalid key <%s>\n",
175                                 pair->key);
176                         return -1;
177                 }
178         }
179         return 0;
180 }
181
182 /*
183  * Return the number of times a given arg_name exists in the key/value list.
184  * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
185  * arg "rx" will be 2.
186  */
187 unsigned
188 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
189 {
190         const struct rte_kvargs_pair *pair;
191         unsigned i, ret;
192
193         ret = 0;
194         for (i = 0; i < kvlist->count; i++) {
195                 pair = &kvlist->pairs[i];
196                 if (strcmp(pair->key, key_match) == 0)
197                         ret++;
198         }
199
200         return ret;
201 }
202
203 /*
204  * For each matching key, call the given handler function.
205  */
206 int
207 rte_kvargs_process(const struct rte_kvargs *kvlist,
208                 const char *key_match,
209                 arg_handler_t handler,
210                 void *opaque_arg)
211 {
212         const struct rte_kvargs_pair *pair;
213         unsigned i;
214
215         for (i = 0; i < kvlist->count; i++) {
216                 pair = &kvlist->pairs[i];
217                 if (strstr(pair->key, key_match) != NULL) {
218                         if ((*handler)(pair->value, opaque_arg) < 0)
219                                 return -1;
220                 }
221         }
222         return 0;
223 }
224
225 /*
226  * Parse the arguments "key=value;key=value;..." string and return
227  * an allocated structure that contains a key/value list. Also
228  * check if only valid keys were used.
229  */
230 int
231 rte_kvargs_parse(struct rte_kvargs *kvlist,
232                 const char *args,
233                 const char *valid_keys[])
234 {
235
236         int ret;
237
238         ret = rte_kvargs_tokenize(kvlist, args);
239         if (ret < 0)
240                 return ret;
241
242         if (valid_keys == NULL)
243                 return 0;
244
245         return check_for_valid_keys(kvlist, valid_keys);
246 }