1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc 2017.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium, Inc nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <inttypes.h>
36 #include <getopt.h>
37 
38 #include <rte_common.h>
39 #include <rte_eventdev.h>
40 #include <rte_lcore.h>
41 
42 #include "evt_options.h"
43 #include "evt_test.h"
44 #include "parser.h"
45 
46 void
47 evt_options_default(struct evt_options *opt)
48 {
49 	memset(opt, 0, sizeof(*opt));
50 	opt->verbose_level = 1; /* Enable minimal prints */
51 	opt->dev_id = 0;
52 	strncpy(opt->test_name, "order_queue", EVT_TEST_NAME_MAX_LEN);
53 	opt->nb_flows = 1024;
54 	opt->socket_id = SOCKET_ID_ANY;
55 	opt->pool_sz = 16 * 1024;
56 	opt->wkr_deq_dep = 16;
57 	opt->nb_pkts = (1ULL << 26); /* do ~64M packets */
58 }
59 
60 typedef int (*option_parser_t)(struct evt_options *opt,
61 		const char *arg);
62 
63 struct long_opt_parser {
64 	const char *lgopt_name;
65 	option_parser_t parser_fn;
66 };
67 
68 static int
69 evt_parse_nb_flows(struct evt_options *opt, const char *arg)
70 {
71 	int ret;
72 
73 	ret = parser_read_uint32(&(opt->nb_flows), arg);
74 
75 	return ret;
76 }
77 
78 static int
79 evt_parse_dev_id(struct evt_options *opt, const char *arg)
80 {
81 	int ret;
82 
83 	ret = parser_read_uint8(&(opt->dev_id), arg);
84 
85 	return ret;
86 }
87 
88 static int
89 evt_parse_verbose(struct evt_options *opt, const char *arg __rte_unused)
90 {
91 	opt->verbose_level = atoi(arg);
92 	return 0;
93 }
94 
95 static int
96 evt_parse_fwd_latency(struct evt_options *opt, const char *arg __rte_unused)
97 {
98 	opt->fwd_latency = 1;
99 	return 0;
100 }
101 
102 static int
103 evt_parse_queue_priority(struct evt_options *opt, const char *arg __rte_unused)
104 {
105 	opt->q_priority = 1;
106 	return 0;
107 }
108 
109 static int
110 evt_parse_test_name(struct evt_options *opt, const char *arg)
111 {
112 	snprintf(opt->test_name, EVT_TEST_NAME_MAX_LEN, "%s", arg);
113 	return 0;
114 }
115 
116 static int
117 evt_parse_socket_id(struct evt_options *opt, const char *arg)
118 {
119 	opt->socket_id = atoi(arg);
120 	return 0;
121 }
122 
123 static int
124 evt_parse_wkr_deq_dep(struct evt_options *opt, const char *arg)
125 {
126 	int ret;
127 
128 	ret = parser_read_uint16(&(opt->wkr_deq_dep), arg);
129 	return ret;
130 }
131 
132 static int
133 evt_parse_nb_pkts(struct evt_options *opt, const char *arg)
134 {
135 	int ret;
136 
137 	ret = parser_read_uint64(&(opt->nb_pkts), arg);
138 
139 	return ret;
140 }
141 
142 static int
143 evt_parse_pool_sz(struct evt_options *opt, const char *arg)
144 {
145 	opt->pool_sz = atoi(arg);
146 
147 	return 0;
148 }
149 
150 static int
151 evt_parse_plcores(struct evt_options *opt, const char *corelist)
152 {
153 	int ret;
154 
155 	ret = parse_lcores_list(opt->plcores, corelist);
156 	if (ret == -E2BIG)
157 		evt_err("duplicate lcores in plcores");
158 
159 	return ret;
160 }
161 
162 static int
163 evt_parse_work_lcores(struct evt_options *opt, const char *corelist)
164 {
165 	int ret;
166 
167 	ret = parse_lcores_list(opt->wlcores, corelist);
168 	if (ret == -E2BIG)
169 		evt_err("duplicate lcores in wlcores");
170 
171 	return ret;
172 }
173 
174 static void
175 usage(char *program)
176 {
177 	printf("usage : %s [EAL options] -- [application options]\n", program);
178 	printf("application options:\n");
179 	printf("\t--verbose          : verbose level\n"
180 		"\t--dev              : device id of the event device\n"
181 		"\t--test             : name of the test application to run\n"
182 		"\t--socket_id        : socket_id of application resources\n"
183 		"\t--pool_sz          : pool size of the mempool\n"
184 		"\t--plcores          : list of lcore ids for producers\n"
185 		"\t--wlcores          : list of lcore ids for workers\n"
186 		"\t--stlist           : list of scheduled types of the stages\n"
187 		"\t--nb_flows         : number of flows to produce\n"
188 		"\t--nb_pkts          : number of packets to produce\n"
189 		"\t--worker_deq_depth : dequeue depth of the worker\n"
190 		"\t--fwd_latency      : perform fwd_latency measurement\n"
191 		"\t--queue_priority   : enable queue priority\n"
192 		);
193 	printf("available tests:\n");
194 	evt_test_dump_names();
195 }
196 
197 static int
198 evt_parse_sched_type_list(struct evt_options *opt, const char *arg)
199 {
200 	char c;
201 	int i = 0, j = -1;
202 
203 	for (i = 0; i < EVT_MAX_STAGES; i++)
204 		opt->sched_type_list[i] = (uint8_t)-1;
205 
206 	i = 0;
207 
208 	do {
209 		c = arg[++j];
210 
211 		switch (c) {
212 		case 'o':
213 		case 'O':
214 			opt->sched_type_list[i++] = RTE_SCHED_TYPE_ORDERED;
215 			break;
216 		case 'a':
217 		case 'A':
218 			opt->sched_type_list[i++] = RTE_SCHED_TYPE_ATOMIC;
219 			break;
220 		case 'p':
221 		case 'P':
222 			opt->sched_type_list[i++] = RTE_SCHED_TYPE_PARALLEL;
223 			break;
224 		case ',':
225 			break;
226 		default:
227 			if (c != '\0') {
228 				evt_err("invalid sched_type %c", c);
229 				return -EINVAL;
230 			}
231 		}
232 	} while (c != '\0');
233 
234 	opt->nb_stages = i;
235 	return 0;
236 }
237 
238 static struct option lgopts[] = {
239 	{ EVT_NB_FLOWS,         1, 0, 0 },
240 	{ EVT_DEVICE,           1, 0, 0 },
241 	{ EVT_VERBOSE,          1, 0, 0 },
242 	{ EVT_TEST,             1, 0, 0 },
243 	{ EVT_PROD_LCORES,      1, 0, 0 },
244 	{ EVT_WORK_LCORES,      1, 0, 0 },
245 	{ EVT_SOCKET_ID,        1, 0, 0 },
246 	{ EVT_POOL_SZ,          1, 0, 0 },
247 	{ EVT_NB_PKTS,          1, 0, 0 },
248 	{ EVT_WKR_DEQ_DEP,      1, 0, 0 },
249 	{ EVT_SCHED_TYPE_LIST,  1, 0, 0 },
250 	{ EVT_FWD_LATENCY,      0, 0, 0 },
251 	{ EVT_QUEUE_PRIORITY,   0, 0, 0 },
252 	{ EVT_HELP,             0, 0, 0 },
253 	{ NULL,                 0, 0, 0 }
254 };
255 
256 static int
257 evt_opts_parse_long(int opt_idx, struct evt_options *opt)
258 {
259 	unsigned int i;
260 
261 	struct long_opt_parser parsermap[] = {
262 		{ EVT_NB_FLOWS, evt_parse_nb_flows},
263 		{ EVT_DEVICE, evt_parse_dev_id},
264 		{ EVT_VERBOSE, evt_parse_verbose},
265 		{ EVT_TEST, evt_parse_test_name},
266 		{ EVT_PROD_LCORES, evt_parse_plcores},
267 		{ EVT_WORK_LCORES, evt_parse_work_lcores},
268 		{ EVT_SOCKET_ID, evt_parse_socket_id},
269 		{ EVT_POOL_SZ, evt_parse_pool_sz},
270 		{ EVT_NB_PKTS, evt_parse_nb_pkts},
271 		{ EVT_WKR_DEQ_DEP, evt_parse_wkr_deq_dep},
272 		{ EVT_SCHED_TYPE_LIST, evt_parse_sched_type_list},
273 		{ EVT_FWD_LATENCY, evt_parse_fwd_latency},
274 		{ EVT_QUEUE_PRIORITY, evt_parse_queue_priority},
275 	};
276 
277 	for (i = 0; i < RTE_DIM(parsermap); i++) {
278 		if (strncmp(lgopts[opt_idx].name, parsermap[i].lgopt_name,
279 				strlen(parsermap[i].lgopt_name)) == 0)
280 			return parsermap[i].parser_fn(opt, optarg);
281 	}
282 
283 	return -EINVAL;
284 }
285 
286 int
287 evt_options_parse(struct evt_options *opt, int argc, char **argv)
288 {
289 	int opts, retval, opt_idx;
290 
291 	while ((opts = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
292 		switch (opts) {
293 		case 0: /* long options */
294 			if (!strcmp(lgopts[opt_idx].name, "help")) {
295 				usage(argv[0]);
296 				exit(EXIT_SUCCESS);
297 			}
298 
299 			retval = evt_opts_parse_long(opt_idx, opt);
300 			if (retval != 0)
301 				return retval;
302 			break;
303 		default:
304 			return -EINVAL;
305 		}
306 	}
307 	return 0;
308 }
309 
310 void
311 evt_options_dump(struct evt_options *opt)
312 {
313 	int lcore_id;
314 	struct rte_event_dev_info dev_info;
315 
316 	rte_event_dev_info_get(opt->dev_id, &dev_info);
317 	evt_dump("driver", "%s", dev_info.driver_name);
318 	evt_dump("test", "%s", opt->test_name);
319 	evt_dump("dev", "%d", opt->dev_id);
320 	evt_dump("verbose_level", "%d", opt->verbose_level);
321 	evt_dump("socket_id", "%d", opt->socket_id);
322 	evt_dump("pool_sz", "%d", opt->pool_sz);
323 	evt_dump("master lcore", "%d", rte_get_master_lcore());
324 	evt_dump("nb_pkts", "%"PRIu64, opt->nb_pkts);
325 	evt_dump_begin("available lcores");
326 	RTE_LCORE_FOREACH(lcore_id)
327 		printf("%d ", lcore_id);
328 	evt_dump_end;
329 	evt_dump_nb_flows(opt);
330 	evt_dump_worker_dequeue_depth(opt);
331 }
332