4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5 * Copyright(c) 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <linux/binfmts.h>
38 #include <rte_malloc.h>
40 #include <rte_string_fns.h>
42 #include "rte_kvargs.h"
45 * Initialize a rte_kvargs structure to an empty key/value list.
48 rte_kvargs_init(struct rte_kvargs *kvlist)
51 kvlist->size = RTE_KVARGS_MAX;
52 memset(kvlist->pairs, 0, kvlist->size);
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.
61 rte_kvargs_add_pair(struct rte_kvargs *kvlist, char *key, char *val)
64 struct rte_kvargs_pair* entry;
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);
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);
81 entry = &kvlist->pairs[kvlist->count];
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.
94 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
98 char *pairs[RTE_KVARGS_MAX];
101 /* If params are empty, nothing to do */
102 if (params == NULL || params[0] == 0) {
103 RTE_LOG(ERR, PMD, "Cannot parse empty arguments\n");
107 /* Copy the const char *params to a modifiable string
108 * to pass to rte_strsplit
110 args = strdup(params);
112 RTE_LOG(ERR, PMD, "Cannot parse arguments: not enough memory\n");
116 count = rte_strsplit(args, strnlen(args, MAX_ARG_STRLEN), pairs,
117 RTE_KVARGS_MAX, RTE_KVARGS_PAIRS_DELIM);
119 for (i = 0; i < count; i++) {
123 rte_strsplit(pairs[i], strnlen(pairs[i], MAX_ARG_STRLEN), pair, 2,
124 RTE_KVARGS_KV_DELIM);
126 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
127 || pair[1][0] == 0) {
129 "Cannot parse arguments: wrong key or value\n"
130 "params=<%s>\n", params);
134 if (rte_kvargs_add_pair(kvlist, pair[0], pair[1]) < 0)
145 * Determine whether a key is valid or not by looking
146 * into a list of valid keys.
149 is_valid_key(const char *valid[], const char *key_match)
151 const char **valid_ptr;
153 for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
154 if (strstr(key_match, *valid_ptr) != NULL)
160 * Determine whether all keys are valid or not by looking
161 * into a list of valid keys.
164 check_for_valid_keys(struct rte_kvargs *kvlist,
168 struct rte_kvargs_pair *pair;
170 for (i = 0; i < kvlist->count; i++) {
171 pair = &kvlist->pairs[i];
172 ret = is_valid_key(valid, pair->key);
175 "Error parsing device, invalid key <%s>\n",
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.
189 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
191 const struct rte_kvargs_pair *pair;
195 for (i = 0; i < kvlist->count; i++) {
196 pair = &kvlist->pairs[i];
197 if (strcmp(pair->key, key_match) == 0)
205 * For each matching key, call the given handler function.
208 rte_kvargs_process(const struct rte_kvargs *kvlist,
209 const char *key_match,
210 arg_handler_t handler,
213 const struct rte_kvargs_pair *pair;
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)
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.
232 rte_kvargs_parse(struct rte_kvargs *kvlist,
234 const char *valid_keys[])
239 ret = rte_kvargs_tokenize(kvlist, args);
243 if (valid_keys == NULL)
246 return check_for_valid_keys(kvlist, valid_keys);