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