kvargs: add a new library to parse key/value arguments
[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         kvlist->size = RTE_KVARGS_MAX;
52         memset(kvlist->pairs, 0, kvlist->size);
53         return 0;
54 }
55
56 /*
57  * Add a key-value pair at the end of a given key/value list.
58  * Return an error if the list is full or if the key is duplicated.
59  */
60 static int
61 rte_kvargs_add_pair(struct rte_kvargs *kvlist, char *key, char *val)
62 {
63         unsigned i;
64         struct rte_kvargs_pair* entry;
65
66         /* is the list full? */
67         if (kvlist->count >= kvlist->size) {
68                 RTE_LOG(ERR, PMD, "Couldn't add %s, key/value list is full\n", key);
69                 return -1;
70         }
71
72         /* Check if the key is duplicated */
73         for (i = 0; i < kvlist->count; i++) {
74                 entry = &kvlist->pairs[i];
75                 if (strcmp(entry->key, key) == 0) {
76                         RTE_LOG(ERR, PMD, "Couldn't add %s, duplicated key\n", key);
77                         return -1;
78                 }
79         }
80
81         entry = &kvlist->pairs[kvlist->count];
82         entry->key = key;
83         entry->value = val;
84         kvlist->count++;
85         return 0;
86 }
87
88 /*
89  * Receive a string with a list of arguments following the pattern
90  * key=value;key=value;... and insert them into the list.
91  * strtok() is used so the params string will be copied to be modified.
92  */
93 static int
94 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *name,
95         const char *params)
96 {
97         unsigned i, count;
98         char *args;
99         char *pairs[RTE_KVARGS_MAX];
100         char *pair[2];
101
102         /* If params are empty, nothing to do */
103         if (params == NULL || params[0] == 0) {
104                 RTE_LOG(ERR, PMD, "Couldn't parse %s device, empty arguments\n", name);
105                 return -1;
106         }
107
108         /* Copy the const char *params to a modifiable string
109          * to pass to rte_strsplit
110          */
111         args = strdup(params);
112         if(args == NULL){
113                 RTE_LOG(ERR, PMD, "Couldn't parse %s device \n", name);
114                 return -1;
115         }
116
117         count = rte_strsplit(args, strnlen(args, MAX_ARG_STRLEN), pairs,
118                         RTE_KVARGS_MAX, RTE_KVARGS_PAIRS_DELIM);
119
120         for (i = 0; i < count; i++) {
121                 pair[0] = NULL;
122                 pair[1] = NULL;
123
124                 rte_strsplit(pairs[i], strnlen(pairs[i], MAX_ARG_STRLEN), pair, 2,
125                                 RTE_KVARGS_KV_DELIM);
126
127                 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
128                                 || pair[1][0] == 0) {
129                         RTE_LOG(ERR, PMD,
130                                         "Couldn't parse %s device, wrong key or value \n", name);
131                         goto error;
132                 }
133
134                 if (rte_kvargs_add_pair(kvlist, pair[0], pair[1]) < 0)
135                         goto error;
136         }
137         return 0;
138
139 error:
140         rte_free(args);
141         return -1;
142 }
143
144 /*
145  * Determine whether a key is valid or not by looking
146  * into a list of valid keys.
147  */
148 static int
149 is_valid_key(const char *valid[], const char *key_match)
150 {
151         const char **valid_ptr;
152
153         for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
154                 if (strstr(key_match, *valid_ptr) != NULL)
155                         return 1;
156         return 0;
157 }
158
159 /*
160  * Determine whether all keys are valid or not by looking
161  * into a list of valid keys.
162  */
163 static int
164 check_for_valid_keys(struct rte_kvargs *kvlist,
165                 const char *valid[])
166 {
167         unsigned i, ret;
168         struct rte_kvargs_pair *pair;
169
170         for (i = 0; i < kvlist->count; i++) {
171                 pair = &kvlist->pairs[i];
172                 ret = is_valid_key(valid, pair->key);
173                 if (!ret) {
174                         RTE_LOG(ERR, PMD,
175                                 "Error parsing device, invalid key <%s>\n",
176                                 pair->key);
177                         return -1;
178                 }
179         }
180         return 0;
181 }
182
183 /*
184  * Return the number of times a given arg_name exists in the key/value list.
185  * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
186  * arg "rx" will be 2.
187  */
188 unsigned
189 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
190 {
191         const struct rte_kvargs_pair *pair;
192         unsigned i, ret;
193
194         ret = 0;
195         for (i = 0; i < kvlist->count; i++) {
196                 pair = &kvlist->pairs[i];
197                 if (strcmp(pair->key, key_match) == 0)
198                         ret++;
199         }
200
201         return ret;
202 }
203
204 /*
205  * For each matching key, call the given handler function.
206  */
207 int
208 rte_kvargs_process(const struct rte_kvargs *kvlist,
209                 const char *key_match,
210                 arg_handler_t handler,
211                 void *opaque_arg)
212 {
213         const struct rte_kvargs_pair *pair;
214         unsigned i;
215
216         for (i = 0; i < kvlist->count; i++) {
217                 pair = &kvlist->pairs[i];
218                 if (strstr(pair->key, key_match) != NULL) {
219                         if ((*handler)(pair->value, opaque_arg) < 0)
220                                 return -1;
221                 }
222         }
223         return 0;
224 }
225
226 /*
227  * Parse the arguments "key=value;key=value;..." string and return
228  * an allocated structure that contains a key/value list. Also
229  * check if only valid keys were used.
230  */
231 int
232 rte_kvargs_parse(struct rte_kvargs *kvlist,
233                 const char *name,
234                 const char *args,
235                 const char *valid_keys[])
236 {
237
238         int ret;
239
240         ret = rte_kvargs_tokenize(kvlist, name, args);
241         if (ret < 0)
242                 return ret;
243
244         if (valid_keys == NULL)
245                 return 0;
246
247         return check_for_valid_keys(kvlist, valid_keys);
248 }