4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/binfmts.h>
37 #include <rte_malloc.h>
39 #include <rte_string_fns.h>
41 #include "rte_eth_pcap_arg_parser.h"
44 * Initializes a non NULL dictionary reference to be used later on.
47 rte_eth_pcap_init_args_dict(struct args_dict *dict)
50 dict->size = RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS;
51 memset(dict->pairs, 0, dict->size);
56 * Adds a key-value pair to a given non-NULL dictionary reference.
57 * The final key will be the name+key.
58 * Returns error in case the dictionary is full or if the key is duplicated.
61 rte_eth_pcap_add_pair_to_dict(struct args_dict *dict,
66 struct key_value* entry;
68 /* is the dictionary full? */
69 if (dict->index >= dict->size) {
70 RTE_LOG(ERR, PMD, "Couldn't add %s, dictionary is full\n", key);
74 /* Check if the key is duplicated */
75 for (i = 0; i < dict->index; i++) {
76 entry = &dict->pairs[i];
77 if (strcmp(entry->key, key) == 0) {
78 RTE_LOG(ERR, PMD, "Couldn't add %s, duplicated key\n", key);
83 entry = &dict->pairs[dict->index];
91 #define RTE_ETH_PCAP_PAIRS_DELIM ';'
92 #define RTE_ETH_PCAP_KEY_VALUE_DELIM '='
94 * Receives a string with a list of arguments following the pattern
95 * key=value;key=value;... and inserts them into the non NULL dictionary.
96 * strtok is used so the params string will be copied to be modified.
99 rte_eth_pcap_tokenize_args(struct args_dict *dict,
105 char *pairs[RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS];
109 /* If params are empty, nothing to do */
110 if (params == NULL || params[0] == 0) {
111 RTE_LOG(ERR, PMD, "Couldn't parse %s device, empty arguments\n", name);
115 /* Copy the const char *params to a modifiable string
116 * to pass to rte_strsplit
118 args = strdup(params);
120 RTE_LOG(ERR, PMD, "Couldn't parse %s device \n", name);
124 num_of_pairs = rte_strsplit(args, strnlen(args, MAX_ARG_STRLEN), pairs,
125 RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS, RTE_ETH_PCAP_PAIRS_DELIM);
127 for (i = 0; i < num_of_pairs; i++) {
131 rte_strsplit(pairs[i], strnlen(pairs[i], MAX_ARG_STRLEN), pair, 2,
132 RTE_ETH_PCAP_KEY_VALUE_DELIM);
134 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
135 || pair[1][0] == 0) {
137 "Couldn't parse %s device, wrong key or value \n", name);
141 if (rte_eth_pcap_add_pair_to_dict(dict, pair[0], pair[1]) < 0)
152 * Determines whether a key is valid or not by looking
153 * into a list of valid keys.
156 is_valid_key(const char *valid[],
157 struct key_value *pair)
159 const char **valid_ptr;
161 for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
162 if (strstr(pair->key, *valid_ptr) != NULL)
168 * Determines whether all keys are valid or not by looking
169 * into a list of valid keys.
172 check_for_valid_keys(struct args_dict *dict,
175 unsigned k_index, ret;
176 struct key_value *pair;
178 for (k_index = 0; k_index < dict->index; k_index++) {
179 pair = &dict->pairs[k_index];
180 ret = is_valid_key(valid, pair);
183 "Error parsing device, invalid key %s\n", pair->key);
191 * Returns the number of times a given arg_name exists on a dictionary.
192 * E.g. given a dict = { rx0 = 0, rx1 = 1, tx0 = 2 } the number of args for
193 * arg "rx" will be 2.
196 rte_eth_pcap_num_of_args(struct args_dict *dict, const char *arg_name)
199 struct key_value *pair;
200 unsigned num_of_keys;
203 for (k_index = 0; k_index < dict->index; k_index++) {
204 pair = &dict->pairs[k_index];
205 if (strcmp(pair->key, arg_name) == 0)
213 * Calls the handler function for a given arg_name passing the
214 * value on the dictionary for that key and a given extra argument.
217 rte_eth_pcap_post_process_arguments(struct args_dict *dict,
218 const char *arg_name,
219 arg_handler_t handler,
223 struct key_value *pair;
225 for (k_index = 0; k_index < dict->index; k_index++) {
226 pair = &dict->pairs[k_index];
227 if (strstr(pair->key, arg_name) != NULL) {
228 if ((*handler)(pair->value, extra_args) < 0)
236 * Parses the arguments "key=value;key=value;..." string and returns
237 * a simple dictionary implementation containing these pairs. It also
238 * checks if only valid keys were used.
241 rte_eth_pcap_parse_args(struct args_dict *dict,
244 const char *valids[])
249 ret = rte_eth_pcap_tokenize_args(dict, name, args);
253 return check_for_valid_keys(dict, valids);