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