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.
36 #include <rte_malloc.h>
38 #include <rte_string_fns.h>
40 #include "rte_eth_pcap_arg_parser.h"
43 * Initializes a non NULL dictionary reference to be used later on.
46 rte_eth_pcap_init_args_dict(struct args_dict *dict)
49 dict->size = RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS;
50 memset(dict->pairs, 0, dict->size);
55 * Adds a key-value pair to a given non-NULL dictionary reference.
56 * The final key will be the name+key.
57 * Returns error in case the dictionary is full or if the key is duplicated.
60 rte_eth_pcap_add_pair_to_dict(struct args_dict *dict,
65 struct key_value* entry;
67 /* is the dictionary full? */
68 if (dict->index >= dict->size) {
69 RTE_LOG(ERR, PMD, "Couldn't add %s, dictionary is full\n", key);
73 /* Check if the key is duplicated */
74 for (i = 0; i < dict->index; i++) {
75 entry = &dict->pairs[i];
76 if (strcmp(entry->key, key) == 0) {
77 RTE_LOG(ERR, PMD, "Couldn't add %s, duplicated key\n", key);
82 entry = &dict->pairs[dict->index];
90 #define RTE_ETH_PCAP_PAIRS_DELIM ';'
91 #define RTE_ETH_PCAP_KEY_VALUE_DELIM '='
93 * Receives a string with a list of arguments following the pattern
94 * key=value;key=value;... and inserts them into the non NULL dictionary.
95 * strtok is used so the params string will be copied to be modified.
98 rte_eth_pcap_tokenize_args(struct args_dict *dict,
104 char *pairs[RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS];
108 /* If params are empty, nothing to do */
109 if (params == NULL || params[0] == 0) {
110 RTE_LOG(ERR, PMD, "Couldn't parse %s device, empty arguments\n", name);
114 /* Copy the const char *params to a modifiable string
115 * to pass to rte_strsplit
117 args = strdup(params);
119 RTE_LOG(ERR, PMD, "Couldn't parse %s device \n", name);
123 num_of_pairs = rte_strsplit(args, strnlen(args, sysconf(_SC_ARG_MAX)), pairs,
124 RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS, RTE_ETH_PCAP_PAIRS_DELIM);
126 for (i = 0; i < num_of_pairs; i++) {
130 rte_strsplit(pairs[i], strnlen(pairs[i], sysconf(_SC_ARG_MAX)), pair, 2,
131 RTE_ETH_PCAP_KEY_VALUE_DELIM);
133 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
134 || pair[1][0] == 0) {
136 "Couldn't parse %s device, wrong key or value \n", name);
140 if (rte_eth_pcap_add_pair_to_dict(dict, pair[0], pair[1]) < 0)
151 * Determines whether a key is valid or not by looking
152 * into a list of valid keys.
155 is_valid_key(const char *valid[],
156 struct key_value *pair)
158 const char **valid_ptr;
160 for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
161 if (strstr(pair->key, *valid_ptr) != NULL)
167 * Determines whether all keys are valid or not by looking
168 * into a list of valid keys.
171 check_for_valid_keys(struct args_dict *dict,
174 unsigned k_index, ret;
175 struct key_value *pair;
177 for (k_index = 0; k_index < dict->index; k_index++) {
178 pair = &dict->pairs[k_index];
179 ret = is_valid_key(valid, pair);
182 "Error parsing device, invalid key %s\n", pair->key);
190 * Returns the number of times a given arg_name exists on a dictionary.
191 * E.g. given a dict = { rx0 = 0, rx1 = 1, tx0 = 2 } the number of args for
192 * arg "rx" will be 2.
195 rte_eth_pcap_num_of_args(struct args_dict *dict, const char *arg_name)
198 struct key_value *pair;
199 unsigned num_of_keys;
202 for (k_index = 0; k_index < dict->index; k_index++) {
203 pair = &dict->pairs[k_index];
204 if (strcmp(pair->key, arg_name) == 0)
212 * Calls the handler function for a given arg_name passing the
213 * value on the dictionary for that key and a given extra argument.
216 rte_eth_pcap_post_process_arguments(struct args_dict *dict,
217 const char *arg_name,
218 arg_handler_t handler,
222 struct key_value *pair;
224 for (k_index = 0; k_index < dict->index; k_index++) {
225 pair = &dict->pairs[k_index];
226 if (strstr(pair->key, arg_name) != NULL) {
227 if ((*handler)(pair->value, extra_args) < 0)
235 * Parses the arguments "key=value;key=value;..." string and returns
236 * a simple dictionary implementation containing these pairs. It also
237 * checks if only valid keys were used.
240 rte_eth_pcap_parse_args(struct args_dict *dict,
243 const char *valids[])
248 ret = rte_eth_pcap_tokenize_args(dict, name, args);
252 return check_for_valid_keys(dict, valids);