]>
git.proxmox.com Git - ceph.git/blob - ceph/src/dpdk/lib/librte_kvargs/rte_kvargs.c
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
)
55 /* Copy the const char *params to a modifiable string
56 * to pass to rte_strsplit
58 kvlist
->str
= strdup(params
);
59 if (kvlist
->str
== NULL
) {
60 RTE_LOG(ERR
, PMD
, "Cannot parse arguments: not enough memory\n");
64 /* browse each key/value pair and add it in kvlist */
66 while ((str
= strtok_r(str
, RTE_KVARGS_PAIRS_DELIM
, &ctx1
)) != NULL
) {
69 if (i
>= RTE_KVARGS_MAX
) {
70 RTE_LOG(ERR
, PMD
, "Cannot parse arguments: list full\n");
74 kvlist
->pairs
[i
].key
= strtok_r(str
, RTE_KVARGS_KV_DELIM
, &ctx2
);
75 kvlist
->pairs
[i
].value
= strtok_r(NULL
, RTE_KVARGS_KV_DELIM
, &ctx2
);
76 if (kvlist
->pairs
[i
].key
== NULL
|| kvlist
->pairs
[i
].value
== NULL
) {
78 "Cannot parse arguments: wrong key or value\n"
79 "params=<%s>\n", params
);
91 * Determine whether a key is valid or not by looking
92 * into a list of valid keys.
95 is_valid_key(const char *valid
[], const char *key_match
)
97 const char **valid_ptr
;
99 for (valid_ptr
= valid
; *valid_ptr
!= NULL
; valid_ptr
++) {
100 if (strcmp(key_match
, *valid_ptr
) == 0)
107 * Determine whether all keys are valid or not by looking
108 * into a list of valid keys.
111 check_for_valid_keys(struct rte_kvargs
*kvlist
,
115 struct rte_kvargs_pair
*pair
;
117 for (i
= 0; i
< kvlist
->count
; i
++) {
118 pair
= &kvlist
->pairs
[i
];
119 ret
= is_valid_key(valid
, pair
->key
);
122 "Error parsing device, invalid key <%s>\n",
131 * Return the number of times a given arg_name exists in the key/value list.
132 * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
133 * arg "rx" will be 2.
136 rte_kvargs_count(const struct rte_kvargs
*kvlist
, const char *key_match
)
138 const struct rte_kvargs_pair
*pair
;
142 for (i
= 0; i
< kvlist
->count
; i
++) {
143 pair
= &kvlist
->pairs
[i
];
144 if (key_match
== NULL
|| strcmp(pair
->key
, key_match
) == 0)
152 * For each matching key, call the given handler function.
155 rte_kvargs_process(const struct rte_kvargs
*kvlist
,
156 const char *key_match
,
157 arg_handler_t handler
,
160 const struct rte_kvargs_pair
*pair
;
163 for (i
= 0; i
< kvlist
->count
; i
++) {
164 pair
= &kvlist
->pairs
[i
];
165 if (key_match
== NULL
|| strcmp(pair
->key
, key_match
) == 0) {
166 if ((*handler
)(pair
->key
, pair
->value
, opaque_arg
) < 0)
173 /* free the rte_kvargs structure */
175 rte_kvargs_free(struct rte_kvargs
*kvlist
)
185 * Parse the arguments "key=value;key=value;..." string and return
186 * an allocated structure that contains a key/value list. Also
187 * check if only valid keys were used.
190 rte_kvargs_parse(const char *args
, const char *valid_keys
[])
192 struct rte_kvargs
*kvlist
;
194 kvlist
= malloc(sizeof(*kvlist
));
197 memset(kvlist
, 0, sizeof(*kvlist
));
199 if (rte_kvargs_tokenize(kvlist
, args
) < 0) {
200 rte_kvargs_free(kvlist
);
204 if (valid_keys
!= NULL
&& check_for_valid_keys(kvlist
, valid_keys
) < 0) {
205 rte_kvargs_free(kvlist
);