New upstream version 17.08
[deb_dpdk.git] / test / test-pipeline / config.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_memzone.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_atomic.h>
55 #include <rte_cycles.h>
56 #include <rte_prefetch.h>
57 #include <rte_lcore.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_interrupts.h>
60 #include <rte_pci.h>
61 #include <rte_random.h>
62 #include <rte_debug.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_mempool.h>
66 #include <rte_mbuf.h>
67 #include <rte_ip.h>
68 #include <rte_tcp.h>
69 #include <rte_lpm.h>
70 #include <rte_lpm6.h>
71 #include <rte_string_fns.h>
72
73 #include "main.h"
74
75 struct app_params app;
76
77 static const char usage[] = "\n";
78
79 void
80 app_print_usage(void)
81 {
82         printf(usage);
83 }
84
85 static int
86 app_parse_port_mask(const char *arg)
87 {
88         char *end = NULL;
89         uint64_t port_mask;
90         uint32_t i;
91
92         if (arg[0] == '\0')
93                 return -1;
94
95         port_mask = strtoul(arg, &end, 16);
96         if ((end == NULL) || (*end != '\0'))
97                 return -2;
98
99         if (port_mask == 0)
100                 return -3;
101
102         app.n_ports = 0;
103         for (i = 0; i < 64; i++) {
104                 if ((port_mask & (1LLU << i)) == 0)
105                         continue;
106
107                 if (app.n_ports >= APP_MAX_PORTS)
108                         return -4;
109
110                 app.ports[app.n_ports] = i;
111                 app.n_ports++;
112         }
113
114         if (!rte_is_power_of_2(app.n_ports))
115                 return -5;
116
117         return 0;
118 }
119
120 struct {
121         const char *name;
122         uint32_t value;
123 } app_args_table[] = {
124         {"none", e_APP_PIPELINE_NONE},
125         {"stub", e_APP_PIPELINE_STUB},
126         {"hash-8-ext", e_APP_PIPELINE_HASH_KEY8_EXT},
127         {"hash-8-lru", e_APP_PIPELINE_HASH_KEY8_LRU},
128         {"hash-16-ext", e_APP_PIPELINE_HASH_KEY16_EXT},
129         {"hash-16-lru", e_APP_PIPELINE_HASH_KEY16_LRU},
130         {"hash-32-ext", e_APP_PIPELINE_HASH_KEY32_EXT},
131         {"hash-32-lru", e_APP_PIPELINE_HASH_KEY32_LRU},
132         {"hash-spec-8-ext", e_APP_PIPELINE_HASH_SPEC_KEY8_EXT},
133         {"hash-spec-8-lru", e_APP_PIPELINE_HASH_SPEC_KEY8_LRU},
134         {"hash-spec-16-ext", e_APP_PIPELINE_HASH_SPEC_KEY16_EXT},
135         {"hash-spec-16-lru", e_APP_PIPELINE_HASH_SPEC_KEY16_LRU},
136         {"hash-spec-32-ext", e_APP_PIPELINE_HASH_SPEC_KEY32_EXT},
137         {"hash-spec-32-lru", e_APP_PIPELINE_HASH_SPEC_KEY32_LRU},
138         {"acl", e_APP_PIPELINE_ACL},
139         {"lpm", e_APP_PIPELINE_LPM},
140         {"lpm-ipv6", e_APP_PIPELINE_LPM_IPV6},
141         {"hash-cuckoo-8", e_APP_PIPELINE_HASH_CUCKOO_KEY8},
142         {"hash-cuckoo-16", e_APP_PIPELINE_HASH_CUCKOO_KEY16},
143         {"hash-cuckoo-32", e_APP_PIPELINE_HASH_CUCKOO_KEY32},
144         {"hash-cuckoo-48", e_APP_PIPELINE_HASH_CUCKOO_KEY48},
145         {"hash-cuckoo-64", e_APP_PIPELINE_HASH_CUCKOO_KEY64},
146         {"hash-cuckoo-80", e_APP_PIPELINE_HASH_CUCKOO_KEY80},
147         {"hash-cuckoo-96", e_APP_PIPELINE_HASH_CUCKOO_KEY96},
148         {"hash-cuckoo-112", e_APP_PIPELINE_HASH_CUCKOO_KEY112},
149         {"hash-cuckoo-128", e_APP_PIPELINE_HASH_CUCKOO_KEY128},
150 };
151
152 int
153 app_parse_args(int argc, char **argv)
154 {
155         int opt, ret;
156         char **argvopt;
157         int option_index;
158         char *prgname = argv[0];
159         static struct option lgopts[] = {
160                 {"none", 0, 0, 0},
161                 {"stub", 0, 0, 0},
162                 {"hash-8-ext", 0, 0, 0},
163                 {"hash-8-lru", 0, 0, 0},
164                 {"hash-16-ext", 0, 0, 0},
165                 {"hash-16-lru", 0, 0, 0},
166                 {"hash-32-ext", 0, 0, 0},
167                 {"hash-32-lru", 0, 0, 0},
168                 {"hash-spec-8-ext", 0, 0, 0},
169                 {"hash-spec-8-lru", 0, 0, 0},
170                 {"hash-spec-16-ext", 0, 0, 0},
171                 {"hash-spec-16-lru", 0, 0, 0},
172                 {"hash-spec-32-ext", 0, 0, 0},
173                 {"hash-spec-32-lru", 0, 0, 0},
174                 {"acl", 0, 0, 0},
175                 {"lpm", 0, 0, 0},
176                 {"lpm-ipv6", 0, 0, 0},
177                 {"hash-cuckoo-8", 0, 0, 0},
178                 {"hash-cuckoo-16", 0, 0, 0},
179                 {"hash-cuckoo-32", 0, 0, 0},
180                 {"hash-cuckoo-48", 0, 0, 0},
181                 {"hash-cuckoo-64", 0, 0, 0},
182                 {"hash-cuckoo-80", 0, 0, 0},
183                 {"hash-cuckoo-96", 0, 0, 0},
184                 {"hash-cuckoo-112", 0, 0, 0},
185                 {"hash-cuckoo-128", 0, 0, 0},
186                 {NULL, 0, 0, 0}
187         };
188         uint32_t lcores[3], n_lcores, lcore_id, pipeline_type_provided;
189
190         /* EAL args */
191         n_lcores = 0;
192         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
193                 if (rte_lcore_is_enabled(lcore_id) == 0)
194                         continue;
195
196                 if (n_lcores >= 3) {
197                         RTE_LOG(ERR, USER1, "Number of cores must be 3\n");
198                         app_print_usage();
199                         return -1;
200                 }
201
202                 lcores[n_lcores] = lcore_id;
203                 n_lcores++;
204         }
205
206         if (n_lcores != 3) {
207                 RTE_LOG(ERR, USER1, "Number of cores must be 3\n");
208                 app_print_usage();
209                 return -1;
210         }
211
212         app.core_rx = lcores[0];
213         app.core_worker = lcores[1];
214         app.core_tx = lcores[2];
215
216         /* Non-EAL args */
217         argvopt = argv;
218
219         app.pipeline_type = e_APP_PIPELINE_HASH_KEY16_LRU;
220         pipeline_type_provided = 0;
221
222         while ((opt = getopt_long(argc, argvopt, "p:",
223                         lgopts, &option_index)) != EOF) {
224                 switch (opt) {
225                 case 'p':
226                         if (app_parse_port_mask(optarg) < 0) {
227                                 app_print_usage();
228                                 return -1;
229                         }
230                         break;
231
232                 case 0: /* long options */
233                         if (!pipeline_type_provided) {
234                                 uint32_t i;
235
236                                 for (i = 0; i < e_APP_PIPELINES; i++) {
237                                         if (!strcmp(lgopts[option_index].name,
238                                                 app_args_table[i].name)) {
239                                                 app.pipeline_type =
240                                                         app_args_table[i].value;
241                                                 pipeline_type_provided = 1;
242                                                 break;
243                                         }
244                                 }
245
246                                 break;
247                         }
248
249                         app_print_usage();
250                         return -1;
251
252                 default:
253                         return -1;
254                 }
255         }
256
257         if (optind >= 0)
258                 argv[optind - 1] = prgname;
259
260         ret = optind - 1;
261         optind = 1; /* reset getopt lib */
262         return ret;
263 }