update Intel copyright years to 2014
[dpdk.git] / lib / librte_pmd_pcap / rte_eth_pcap_arg_parser.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33 #include <string.h>
34 #include <sys/user.h>
35 #include <linux/binfmts.h>
36
37 #include <rte_malloc.h>
38 #include <rte_log.h>
39 #include <rte_string_fns.h>
40
41 #include "rte_eth_pcap_arg_parser.h"
42
43 /*
44  * Initializes a non NULL dictionary reference to be used later on.
45  */
46 inline int
47 rte_eth_pcap_init_args_dict(struct args_dict *dict)
48 {
49         dict->index = 0;
50         dict->size = RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS;
51         memset(dict->pairs, 0, dict->size);
52         return 0;
53 }
54
55 /*
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.
59  */
60 inline int
61 rte_eth_pcap_add_pair_to_dict(struct args_dict *dict,
62                 char *key,
63                 char *val)
64 {
65         unsigned i;
66         struct key_value* entry;
67
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);
71                 return -1;
72         }
73
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);
79                         return -1;
80                 }
81         }
82
83         entry = &dict->pairs[dict->index];
84         entry->key = key;
85         entry->value = val;
86         dict->index++;
87         return 0;
88
89 }
90
91 #define RTE_ETH_PCAP_PAIRS_DELIM                ';'
92 #define RTE_ETH_PCAP_KEY_VALUE_DELIM    '='
93 /*
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.
97  */
98 inline int
99 rte_eth_pcap_tokenize_args(struct args_dict *dict,
100                 const char *name,
101                 const char *params)
102 {
103         int i;
104         char *args;
105         char *pairs[RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS];
106         char *pair[2];
107         int num_of_pairs;
108
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);
112                 return -1;
113         }
114
115         /* Copy the const char *params to a modifiable string
116          * to pass to rte_strsplit
117          */
118         args = strdup(params);
119         if(args == NULL){
120                 RTE_LOG(ERR, PMD, "Couldn't parse %s device \n", name);
121                 return -1;
122         }
123
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);
126
127         for (i = 0; i < num_of_pairs; i++) {
128                 pair[0] = NULL;
129                 pair[1] = NULL;
130
131                 rte_strsplit(pairs[i], strnlen(pairs[i], MAX_ARG_STRLEN), pair, 2,
132                                 RTE_ETH_PCAP_KEY_VALUE_DELIM);
133
134                 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
135                                 || pair[1][0] == 0) {
136                         RTE_LOG(ERR, PMD,
137                                         "Couldn't parse %s device, wrong key or value \n", name);
138                         goto error;
139                 }
140
141                 if (rte_eth_pcap_add_pair_to_dict(dict, pair[0], pair[1]) < 0)
142                         goto error;
143         }
144         return 0;
145
146 error:
147         rte_free(args);
148         return -1;
149 }
150
151 /*
152  * Determines whether a key is valid or not by looking
153  * into a list of valid keys.
154  */
155 static inline int
156 is_valid_key(const char *valid[],
157                 struct key_value *pair)
158 {
159         const char **valid_ptr;
160
161         for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
162                 if (strstr(pair->key, *valid_ptr) != NULL)
163                         return 1;
164         return 0;
165 }
166
167 /*
168  * Determines whether all keys are valid or not by looking
169  * into a list of valid keys.
170  */
171 static inline int
172 check_for_valid_keys(struct args_dict *dict,
173                 const char *valid[])
174 {
175         unsigned k_index, ret;
176         struct key_value *pair;
177
178         for (k_index = 0; k_index < dict->index; k_index++) {
179                 pair = &dict->pairs[k_index];
180                 ret = is_valid_key(valid, pair);
181                 if (!ret) {
182                         RTE_LOG(ERR, PMD,
183                                         "Error parsing device, invalid key %s\n", pair->key);
184                         return -1;
185                 }
186         }
187         return 0;
188 }
189
190 /*
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.
194  */
195 inline unsigned
196 rte_eth_pcap_num_of_args(struct args_dict *dict, const char *arg_name)
197 {
198         unsigned k_index;
199         struct key_value *pair;
200         unsigned num_of_keys;
201
202         num_of_keys = 0;
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)
206                         num_of_keys++;
207         }
208
209         return num_of_keys;
210 }
211
212 /*
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.
215  */
216 inline int
217 rte_eth_pcap_post_process_arguments(struct args_dict *dict,
218                 const char *arg_name,
219                 arg_handler_t handler,
220                 void *extra_args)
221 {
222         unsigned k_index;
223         struct key_value *pair;
224
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)
229                                 return -1;
230                 }
231         }
232         return 0;
233 }
234
235 /*
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.
239  */
240 inline int
241 rte_eth_pcap_parse_args(struct args_dict *dict,
242                 const char *name,
243                 const char *args,
244                 const char *valids[])
245 {
246
247         int ret;
248
249         ret = rte_eth_pcap_tokenize_args(dict, name, args);
250         if (ret < 0)
251                 return ret;
252
253         return check_for_valid_keys(dict, valids);
254 }
255