1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2013 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
10 #include <rte_string_fns.h>
12 #include "rte_kvargs.h"
15 * Receive a string with a list of arguments following the pattern
16 * key=value,key=value,... and insert them into the list.
17 * strtok() is used so the params string will be copied to be modified.
20 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
27 /* Copy the const char *params to a modifiable string
28 * to pass to rte_strsplit
30 kvlist->str = strdup(params);
31 if (kvlist->str == NULL) {
32 RTE_LOG(ERR, PMD, "Cannot parse arguments: not enough memory\n");
36 /* browse each key/value pair and add it in kvlist */
38 while ((str = strtok_r(str, RTE_KVARGS_PAIRS_DELIM, &ctx1)) != NULL) {
41 if (i >= RTE_KVARGS_MAX) {
42 RTE_LOG(ERR, PMD, "Cannot parse arguments: list full\n");
46 kvlist->pairs[i].key = strtok_r(str, RTE_KVARGS_KV_DELIM, &ctx2);
47 kvlist->pairs[i].value = strtok_r(NULL, RTE_KVARGS_KV_DELIM, &ctx2);
48 if (kvlist->pairs[i].key == NULL || kvlist->pairs[i].value == NULL) {
50 "Cannot parse arguments: wrong key or value\n"
51 "params=<%s>\n", params);
63 * Determine whether a key is valid or not by looking
64 * into a list of valid keys.
67 is_valid_key(const char * const valid[], const char *key_match)
69 const char * const *valid_ptr;
71 for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++) {
72 if (strcmp(key_match, *valid_ptr) == 0)
79 * Determine whether all keys are valid or not by looking
80 * into a list of valid keys.
83 check_for_valid_keys(struct rte_kvargs *kvlist,
84 const char * const valid[])
87 struct rte_kvargs_pair *pair;
89 for (i = 0; i < kvlist->count; i++) {
90 pair = &kvlist->pairs[i];
91 ret = is_valid_key(valid, pair->key);
94 "Error parsing device, invalid key <%s>\n",
103 * Return the number of times a given arg_name exists in the key/value list.
104 * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
105 * arg "rx" will be 2.
108 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
110 const struct rte_kvargs_pair *pair;
114 for (i = 0; i < kvlist->count; i++) {
115 pair = &kvlist->pairs[i];
116 if (key_match == NULL || strcmp(pair->key, key_match) == 0)
124 * For each matching key, call the given handler function.
127 rte_kvargs_process(const struct rte_kvargs *kvlist,
128 const char *key_match,
129 arg_handler_t handler,
132 const struct rte_kvargs_pair *pair;
135 for (i = 0; i < kvlist->count; i++) {
136 pair = &kvlist->pairs[i];
137 if (key_match == NULL || strcmp(pair->key, key_match) == 0) {
138 if ((*handler)(pair->key, pair->value, opaque_arg) < 0)
145 /* free the rte_kvargs structure */
147 rte_kvargs_free(struct rte_kvargs *kvlist)
157 * Parse the arguments "key=value,key=value,..." string and return
158 * an allocated structure that contains a key/value list. Also
159 * check if only valid keys were used.
162 rte_kvargs_parse(const char *args, const char * const valid_keys[])
164 struct rte_kvargs *kvlist;
166 kvlist = malloc(sizeof(*kvlist));
169 memset(kvlist, 0, sizeof(*kvlist));
171 if (rte_kvargs_tokenize(kvlist, args) < 0) {
172 rte_kvargs_free(kvlist);
176 if (valid_keys != NULL && check_for_valid_keys(kvlist, valid_keys) < 0) {
177 rte_kvargs_free(kvlist);