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>
40 #include <rte_string_fns.h>
42 #include "rte_kvargs.h"
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.
49 rte_kvargs_add_pair(struct rte_kvargs *kvlist, char *key, char *val)
52 struct rte_kvargs_pair* entry;
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);
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);
69 entry = &kvlist->pairs[kvlist->count];
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.
82 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
85 char *pairs[RTE_KVARGS_MAX];
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");
94 /* Copy the const char *params to a modifiable string
95 * to pass to rte_strsplit
97 kvlist->str = strdup(params);
98 if (kvlist->str == NULL) {
99 RTE_LOG(ERR, PMD, "Cannot parse arguments: not enough memory\n");
103 count = rte_strsplit(kvlist->str, strnlen(kvlist->str, MAX_ARG_STRLEN), pairs,
104 RTE_KVARGS_MAX, RTE_KVARGS_PAIRS_DELIM);
106 for (i = 0; i < count; i++) {
110 rte_strsplit(pairs[i], strnlen(pairs[i], MAX_ARG_STRLEN), pair, 2,
111 RTE_KVARGS_KV_DELIM);
113 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
114 || pair[1][0] == 0) {
116 "Cannot parse arguments: wrong key or value\n"
117 "params=<%s>\n", params);
121 if (rte_kvargs_add_pair(kvlist, pair[0], pair[1]) < 0)
128 * Determine whether a key is valid or not by looking
129 * into a list of valid keys.
132 is_valid_key(const char *valid[], const char *key_match)
134 const char **valid_ptr;
136 for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
137 if (strstr(key_match, *valid_ptr) != NULL)
143 * Determine whether all keys are valid or not by looking
144 * into a list of valid keys.
147 check_for_valid_keys(struct rte_kvargs *kvlist,
151 struct rte_kvargs_pair *pair;
153 for (i = 0; i < kvlist->count; i++) {
154 pair = &kvlist->pairs[i];
155 ret = is_valid_key(valid, pair->key);
158 "Error parsing device, invalid key <%s>\n",
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.
172 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
174 const struct rte_kvargs_pair *pair;
178 for (i = 0; i < kvlist->count; i++) {
179 pair = &kvlist->pairs[i];
180 if (strcmp(pair->key, key_match) == 0)
188 * For each matching key, call the given handler function.
191 rte_kvargs_process(const struct rte_kvargs *kvlist,
192 const char *key_match,
193 arg_handler_t handler,
196 const struct rte_kvargs_pair *pair;
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)
209 /* free the rte_kvargs structure */
211 rte_kvargs_free(struct rte_kvargs *kvlist)
213 if (kvlist->str != NULL)
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.
224 rte_kvargs_parse(const char *args, const char *valid_keys[])
226 struct rte_kvargs *kvlist;
228 kvlist = malloc(sizeof(*kvlist));
231 memset(kvlist, 0, sizeof(*kvlist));
233 if (rte_kvargs_tokenize(kvlist, args) < 0) {
234 rte_kvargs_free(kvlist);
238 if (valid_keys != NULL && check_for_valid_keys(kvlist, valid_keys) < 0) {
239 rte_kvargs_free(kvlist);