add FreeBSD support
[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 <unistd.h>
35
36 #include <rte_malloc.h>
37 #include <rte_log.h>
38 #include <rte_string_fns.h>
39
40 #include "rte_eth_pcap_arg_parser.h"
41
42 /*
43  * Initializes a non NULL dictionary reference to be used later on.
44  */
45 inline int
46 rte_eth_pcap_init_args_dict(struct args_dict *dict)
47 {
48         dict->index = 0;
49         dict->size = RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS;
50         memset(dict->pairs, 0, dict->size);
51         return 0;
52 }
53
54 /*
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.
58  */
59 inline int
60 rte_eth_pcap_add_pair_to_dict(struct args_dict *dict,
61                 char *key,
62                 char *val)
63 {
64         unsigned i;
65         struct key_value* entry;
66
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);
70                 return -1;
71         }
72
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);
78                         return -1;
79                 }
80         }
81
82         entry = &dict->pairs[dict->index];
83         entry->key = key;
84         entry->value = val;
85         dict->index++;
86         return 0;
87
88 }
89
90 #define RTE_ETH_PCAP_PAIRS_DELIM                ';'
91 #define RTE_ETH_PCAP_KEY_VALUE_DELIM    '='
92 /*
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.
96  */
97 inline int
98 rte_eth_pcap_tokenize_args(struct args_dict *dict,
99                 const char *name,
100                 const char *params)
101 {
102         int i;
103         char *args;
104         char *pairs[RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS];
105         char *pair[2];
106         int num_of_pairs;
107
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);
111                 return -1;
112         }
113
114         /* Copy the const char *params to a modifiable string
115          * to pass to rte_strsplit
116          */
117         args = strdup(params);
118         if(args == NULL){
119                 RTE_LOG(ERR, PMD, "Couldn't parse %s device \n", name);
120                 return -1;
121         }
122
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);
125
126         for (i = 0; i < num_of_pairs; i++) {
127                 pair[0] = NULL;
128                 pair[1] = NULL;
129
130                 rte_strsplit(pairs[i], strnlen(pairs[i], sysconf(_SC_ARG_MAX)), pair, 2,
131                                 RTE_ETH_PCAP_KEY_VALUE_DELIM);
132
133                 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
134                                 || pair[1][0] == 0) {
135                         RTE_LOG(ERR, PMD,
136                                         "Couldn't parse %s device, wrong key or value \n", name);
137                         goto error;
138                 }
139
140                 if (rte_eth_pcap_add_pair_to_dict(dict, pair[0], pair[1]) < 0)
141                         goto error;
142         }
143         return 0;
144
145 error:
146         rte_free(args);
147         return -1;
148 }
149
150 /*
151  * Determines whether a key is valid or not by looking
152  * into a list of valid keys.
153  */
154 static inline int
155 is_valid_key(const char *valid[],
156                 struct key_value *pair)
157 {
158         const char **valid_ptr;
159
160         for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
161                 if (strstr(pair->key, *valid_ptr) != NULL)
162                         return 1;
163         return 0;
164 }
165
166 /*
167  * Determines whether all keys are valid or not by looking
168  * into a list of valid keys.
169  */
170 static inline int
171 check_for_valid_keys(struct args_dict *dict,
172                 const char *valid[])
173 {
174         unsigned k_index, ret;
175         struct key_value *pair;
176
177         for (k_index = 0; k_index < dict->index; k_index++) {
178                 pair = &dict->pairs[k_index];
179                 ret = is_valid_key(valid, pair);
180                 if (!ret) {
181                         RTE_LOG(ERR, PMD,
182                                         "Error parsing device, invalid key %s\n", pair->key);
183                         return -1;
184                 }
185         }
186         return 0;
187 }
188
189 /*
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.
193  */
194 inline unsigned
195 rte_eth_pcap_num_of_args(struct args_dict *dict, const char *arg_name)
196 {
197         unsigned k_index;
198         struct key_value *pair;
199         unsigned num_of_keys;
200
201         num_of_keys = 0;
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)
205                         num_of_keys++;
206         }
207
208         return num_of_keys;
209 }
210
211 /*
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.
214  */
215 inline int
216 rte_eth_pcap_post_process_arguments(struct args_dict *dict,
217                 const char *arg_name,
218                 arg_handler_t handler,
219                 void *extra_args)
220 {
221         unsigned k_index;
222         struct key_value *pair;
223
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)
228                                 return -1;
229                 }
230         }
231         return 0;
232 }
233
234 /*
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.
238  */
239 inline int
240 rte_eth_pcap_parse_args(struct args_dict *dict,
241                 const char *name,
242                 const char *args,
243                 const char *valids[])
244 {
245
246         int ret;
247
248         ret = rte_eth_pcap_tokenize_args(dict, name, args);
249         if (ret < 0)
250                 return ret;
251
252         return check_for_valid_keys(dict, valids);
253 }
254