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.
38 #include <rte_string_fns.h>
40 #include "rte_kvargs.h"
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.
48 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
51 char *str, *ctx1, *ctx2;
53 /* Copy the const char *params to a modifiable string
54 * to pass to rte_strsplit
56 kvlist->str = strdup(params);
57 if (kvlist->str == NULL) {
58 RTE_LOG(ERR, PMD, "Cannot parse arguments: not enough memory\n");
62 /* browse each key/value pair and add it in kvlist */
64 while ((str = strtok_r(str, RTE_KVARGS_PAIRS_DELIM, &ctx1)) != NULL) {
67 if (i >= RTE_KVARGS_MAX) {
68 RTE_LOG(ERR, PMD, "Cannot parse arguments: list full\n");
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) {
76 "Cannot parse arguments: wrong key or value\n"
77 "params=<%s>\n", params);
89 * Determine whether a key is valid or not by looking
90 * into a list of valid keys.
93 is_valid_key(const char *valid[], const char *key_match)
95 const char **valid_ptr;
97 for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++) {
98 if (strcmp(key_match, *valid_ptr) == 0)
105 * Determine whether all keys are valid or not by looking
106 * into a list of valid keys.
109 check_for_valid_keys(struct rte_kvargs *kvlist,
113 struct rte_kvargs_pair *pair;
115 for (i = 0; i < kvlist->count; i++) {
116 pair = &kvlist->pairs[i];
117 ret = is_valid_key(valid, pair->key);
120 "Error parsing device, invalid key <%s>\n",
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.
134 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
136 const struct rte_kvargs_pair *pair;
140 for (i = 0; i < kvlist->count; i++) {
141 pair = &kvlist->pairs[i];
142 if (strcmp(pair->key, key_match) == 0)
150 * For each matching key, call the given handler function.
153 rte_kvargs_process(const struct rte_kvargs *kvlist,
154 const char *key_match,
155 arg_handler_t handler,
158 const struct rte_kvargs_pair *pair;
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->value, opaque_arg) < 0)
171 /* free the rte_kvargs structure */
173 rte_kvargs_free(struct rte_kvargs *kvlist)
175 if (kvlist->str != NULL)
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.
186 rte_kvargs_parse(const char *args, const char *valid_keys[])
188 struct rte_kvargs *kvlist;
190 kvlist = malloc(sizeof(*kvlist));
193 memset(kvlist, 0, sizeof(*kvlist));
195 if (rte_kvargs_tokenize(kvlist, args) < 0) {
196 rte_kvargs_free(kvlist);
200 if (valid_keys != NULL && check_for_valid_keys(kvlist, valid_keys) < 0) {
201 rte_kvargs_free(kvlist);