xref: /dpdk/examples/qos_sched/args.c (revision b79e4c00af0e7cfb8601ab0208659d226b82bd10)
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 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <locale.h>
39 #include <unistd.h>
40 #include <limits.h>
41 #include <getopt.h>
42 
43 #include <rte_log.h>
44 #include <rte_eal.h>
45 #include <rte_lcore.h>
46 #include <rte_string_fns.h>
47 
48 #include "main.h"
49 
50 #define APP_NAME "qos_sched"
51 #define MAX_OPT_VALUES 8
52 #define SYS_CPU_DIR "/sys/devices/system/cpu/cpu%u/topology/"
53 
54 static uint32_t app_master_core = 1;
55 static uint32_t app_numa_mask;
56 static uint64_t app_used_core_mask = 0;
57 static uint64_t app_used_port_mask = 0;
58 static uint64_t app_used_rx_port_mask = 0;
59 static uint64_t app_used_tx_port_mask = 0;
60 
61 
62 static const char usage[] =
63 	"                                                                               \n"
64 	"    %s <APP PARAMS>                                                            \n"
65 	"                                                                               \n"
66 	"Application mandatory parameters:                                              \n"
67 	"    --pfc \"RX PORT, TX PORT, RX LCORE, WT LCORE\" : Packet flow configuration \n"
68 	"           multiple pfc can be configured in command line                      \n"
69 	"                                                                               \n"
70 	"Application optional parameters:                                               \n"
71         "    --i     : run in interactive mode (default value is %u)                    \n"
72 	"    --mst I : master core index (default value is %u)                          \n"
73 	"    --rsz \"A, B, C\" :   Ring sizes                                           \n"
74 	"           A = Size (in number of buffer descriptors) of each of the NIC RX    \n"
75 	"               rings read by the I/O RX lcores (default value is %u)           \n"
76 	"           B = Size (in number of elements) of each of the SW rings used by the\n"
77 	"               I/O RX lcores to send packets to worker lcores (default value is\n"
78 	"               %u)                                                             \n"
79 	"           C = Size (in number of buffer descriptors) of each of the NIC TX    \n"
80 	"               rings written by worker lcores (default value is %u)            \n"
81 	"    --bsz \"A, B, C, D\": Burst sizes                                          \n"
82 	"           A = I/O RX lcore read burst size from NIC RX (default value is %u)  \n"
83 	"           B = I/O RX lcore write burst size to output SW rings,               \n"
84 	"               Worker lcore read burst size from input SW rings,               \n"
85 	"               QoS enqueue size (default value is %u)                          \n"
86 	"           C = QoS dequeue size (default value is %u)                          \n"
87 	"           D = Worker lcore write burst size to NIC TX (default value is %u)   \n"
88 	"    --msz M : Mempool size (in number of mbufs) for each pfc (default %u)      \n"
89 	"    --rth \"A, B, C\" :   RX queue threshold parameters                        \n"
90 	"           A = RX prefetch threshold (default value is %u)                     \n"
91 	"           B = RX host threshold (default value is %u)                         \n"
92 	"           C = RX write-back threshold (default value is %u)                   \n"
93 	"    --tth \"A, B, C\" :   TX queue threshold parameters                        \n"
94 	"           A = TX prefetch threshold (default value is %u)                     \n"
95 	"           B = TX host threshold (default value is %u)                         \n"
96 	"           C = TX write-back threshold (default value is %u)                   \n"
97 	"    --cfg FILE : profile configuration to load                                 \n"
98 ;
99 
100 /* display usage */
101 static void
102 app_usage(const char *prgname)
103 {
104 	printf(usage, prgname, APP_INTERACTIVE_DEFAULT, app_master_core,
105 		APP_RX_DESC_DEFAULT, APP_RING_SIZE, APP_TX_DESC_DEFAULT,
106 		MAX_PKT_RX_BURST, PKT_ENQUEUE, PKT_DEQUEUE,
107 		MAX_PKT_TX_BURST, NB_MBUF,
108 		RX_PTHRESH, RX_HTHRESH, RX_WTHRESH,
109 		TX_PTHRESH, TX_HTHRESH, TX_WTHRESH
110 		);
111 }
112 
113 static inline int str_is(const char *str, const char *is)
114 {
115 	return strcmp(str, is) == 0;
116 }
117 
118 /* returns core mask used by DPDK */
119 static uint64_t
120 app_eal_core_mask(void)
121 {
122 	uint32_t i;
123 	uint64_t cm = 0;
124 	struct rte_config *cfg = rte_eal_get_configuration();
125 
126 	for (i = 0; i < APP_MAX_LCORE; i++) {
127 		if (cfg->lcore_role[i] == ROLE_RTE)
128 			cm |= (1ULL << i);
129 	}
130 
131 	cm |= (1ULL << cfg->master_lcore);
132 
133 	return cm;
134 }
135 
136 
137 /* returns total number of cores presented in a system */
138 static uint32_t
139 app_cpu_core_count(void)
140 {
141 	int i, len;
142 	char path[PATH_MAX];
143 	uint32_t ncores = 0;
144 
145 	for (i = 0; i < APP_MAX_LCORE; i++) {
146 		len = snprintf(path, sizeof(path), SYS_CPU_DIR, i);
147 		if (len <= 0 || (unsigned)len >= sizeof(path))
148 			continue;
149 
150 		if (access(path, F_OK) == 0)
151 			ncores++;
152 	}
153 
154 	return ncores;
155 }
156 
157 /* returns:
158 	 number of values parsed
159 	-1 in case of error
160 */
161 static int
162 app_parse_opt_vals(const char *conf_str, char separator, uint32_t n_vals, uint32_t *opt_vals)
163 {
164 	char *string;
165 	int i, n_tokens;
166 	char *tokens[MAX_OPT_VALUES];
167 
168 	if (conf_str == NULL || opt_vals == NULL || n_vals == 0 || n_vals > MAX_OPT_VALUES)
169 		return -1;
170 
171 	/* duplicate configuration string before splitting it to tokens */
172 	string = strdup(conf_str);
173 	if (string == NULL)
174 		return -1;
175 
176 	n_tokens = rte_strsplit(string, strnlen(string, 32), tokens, n_vals, separator);
177 
178 	if (n_tokens > MAX_OPT_VALUES)
179 		return -1;
180 
181 	for (i = 0; i < n_tokens; i++)
182 		opt_vals[i] = (uint32_t)atol(tokens[i]);
183 
184 	free(string);
185 
186 	return n_tokens;
187 }
188 
189 static int
190 app_parse_ring_conf(const char *conf_str)
191 {
192 	int ret;
193 	uint32_t vals[3];
194 
195 	ret = app_parse_opt_vals(conf_str, ',', 3, vals);
196 	if (ret != 3)
197 		return ret;
198 
199 	ring_conf.rx_size = vals[0];
200 	ring_conf.ring_size = vals[1];
201 	ring_conf.tx_size = vals[2];
202 
203 	return 0;
204 }
205 
206 static int
207 app_parse_rth_conf(const char *conf_str)
208 {
209 	int ret;
210 	uint32_t vals[3];
211 
212 	ret = app_parse_opt_vals(conf_str, ',', 3, vals);
213 	if (ret != 3)
214 		return ret;
215 
216 	rx_thresh.pthresh = (uint8_t)vals[0];
217 	rx_thresh.hthresh = (uint8_t)vals[1];
218 	rx_thresh.wthresh = (uint8_t)vals[2];
219 
220 	return 0;
221 }
222 
223 static int
224 app_parse_tth_conf(const char *conf_str)
225 {
226 	int ret;
227 	uint32_t vals[3];
228 
229 	ret = app_parse_opt_vals(conf_str, ',', 3, vals);
230 	if (ret != 3)
231 		return ret;
232 
233 	tx_thresh.pthresh = (uint8_t)vals[0];
234 	tx_thresh.hthresh = (uint8_t)vals[1];
235 	tx_thresh.wthresh = (uint8_t)vals[2];
236 
237 	return 0;
238 }
239 
240 static int
241 app_parse_flow_conf(const char *conf_str)
242 {
243 	int ret;
244 	uint32_t vals[5];
245 	struct flow_conf *pconf;
246 	uint64_t mask;
247 
248 	memset(vals, 0, sizeof(vals));
249 	ret = app_parse_opt_vals(conf_str, ',', 6, vals);
250 	if (ret < 4 || ret > 5)
251 		return ret;
252 
253 	pconf = &qos_conf[nb_pfc];
254 
255 	pconf->rx_port = (uint8_t)vals[0];
256 	pconf->tx_port = (uint8_t)vals[1];
257 	pconf->rx_core = (uint8_t)vals[2];
258 	pconf->wt_core = (uint8_t)vals[3];
259 	if (ret == 5)
260 		pconf->tx_core = (uint8_t)vals[4];
261 	else
262 		pconf->tx_core = pconf->wt_core;
263 
264 	if (pconf->rx_core == pconf->wt_core) {
265 		RTE_LOG(ERR, APP, "pfc %u: rx thread and worker thread cannot share same core\n", nb_pfc);
266 		return -1;
267 	}
268 
269 	if (pconf->rx_port >= RTE_MAX_ETHPORTS) {
270 		RTE_LOG(ERR, APP, "pfc %u: invalid rx port %"PRIu8" index\n",
271 				nb_pfc, pconf->rx_port);
272 		return -1;
273 	}
274 	if (pconf->tx_port >= RTE_MAX_ETHPORTS) {
275 		RTE_LOG(ERR, APP, "pfc %u: invalid tx port %"PRIu8" index\n",
276 				nb_pfc, pconf->tx_port);
277 		return -1;
278 	}
279 
280 	mask = 1lu << pconf->rx_port;
281 	if (app_used_rx_port_mask & mask) {
282 		RTE_LOG(ERR, APP, "pfc %u: rx port %"PRIu8" is used already\n",
283 				nb_pfc, pconf->rx_port);
284 		return -1;
285 	}
286 	app_used_rx_port_mask |= mask;
287 	app_used_port_mask |= mask;
288 
289 	mask = 1lu << pconf->tx_port;
290 	if (app_used_tx_port_mask & mask) {
291 		RTE_LOG(ERR, APP, "pfc %u: port %"PRIu8" is used already\n",
292 				nb_pfc, pconf->tx_port);
293 		return -1;
294 	}
295 	app_used_tx_port_mask |= mask;
296 	app_used_port_mask |= mask;
297 
298 	mask = 1lu << pconf->rx_core;
299 	app_used_core_mask |= mask;
300 
301 	mask = 1lu << pconf->wt_core;
302 	app_used_core_mask |= mask;
303 
304 	mask = 1lu << pconf->tx_core;
305 	app_used_core_mask |= mask;
306 
307 	nb_pfc++;
308 
309 	return 0;
310 }
311 
312 static int
313 app_parse_burst_conf(const char *conf_str)
314 {
315 	int ret;
316 	uint32_t vals[4];
317 
318 	ret = app_parse_opt_vals(conf_str, ',', 4, vals);
319 	if (ret != 4)
320 		return ret;
321 
322 	burst_conf.rx_burst    = (uint16_t)vals[0];
323 	burst_conf.ring_burst  = (uint16_t)vals[1];
324 	burst_conf.qos_dequeue = (uint16_t)vals[2];
325 	burst_conf.tx_burst    = (uint16_t)vals[3];
326 
327 	return 0;
328 }
329 
330 /*
331  * Parses the argument given in the command line of the application,
332  * calculates mask for used cores and initializes EAL with calculated core mask
333  */
334 int
335 app_parse_args(int argc, char **argv)
336 {
337 	int opt, ret;
338 	int option_index;
339 	const char *optname;
340 	char *prgname = argv[0];
341 	uint32_t i, nb_lcores;
342 
343 	static struct option lgopts[] = {
344 		{ "pfc", 1, 0, 0 },
345 		{ "mst", 1, 0, 0 },
346 		{ "rsz", 1, 0, 0 },
347 		{ "bsz", 1, 0, 0 },
348 		{ "msz", 1, 0, 0 },
349 		{ "rth", 1, 0, 0 },
350 		{ "tth", 1, 0, 0 },
351 		{ "cfg", 1, 0, 0 },
352 		{ NULL,  0, 0, 0 }
353 	};
354 
355 	/* initialize EAL first */
356 	ret = rte_eal_init(argc, argv);
357 	if (ret < 0)
358 		return -1;
359 
360 	argc -= ret;
361 	argv += ret;
362 
363 	/* set en_US locale to print big numbers with ',' */
364 	setlocale(LC_NUMERIC, "en_US.utf-8");
365 
366 	while ((opt = getopt_long(argc, argv, "i",
367 		lgopts, &option_index)) != EOF) {
368 
369 			switch (opt) {
370 			case 'i':
371 				printf("Interactive-mode selected\n");
372 				interactive = 1;
373 				break;
374 			/* long options */
375 			case 0:
376 				optname = lgopts[option_index].name;
377 				if (str_is(optname, "pfc")) {
378 					ret = app_parse_flow_conf(optarg);
379 					if (ret) {
380 						RTE_LOG(ERR, APP, "Invalid pipe configuration %s\n", optarg);
381 						return -1;
382 					}
383 					break;
384 				}
385 				if (str_is(optname, "mst")) {
386 					app_master_core = (uint32_t)atoi(optarg);
387 					break;
388 				}
389 				if (str_is(optname, "rsz")) {
390 					ret = app_parse_ring_conf(optarg);
391 					if (ret) {
392 						RTE_LOG(ERR, APP, "Invalid ring configuration %s\n", optarg);
393 						return -1;
394 					}
395 					break;
396 				}
397 				if (str_is(optname, "bsz")) {
398 					ret = app_parse_burst_conf(optarg);
399 					if (ret) {
400 						RTE_LOG(ERR, APP, "Invalid burst configuration %s\n", optarg);
401 						return -1;
402 					}
403 					break;
404 				}
405 				if (str_is(optname, "msz")) {
406 					mp_size = atoi(optarg);
407 					if (mp_size <= 0) {
408 						RTE_LOG(ERR, APP, "Invalid mempool size %s\n", optarg);
409 						return -1;
410 					}
411 					break;
412 				}
413 				if (str_is(optname, "rth")) {
414 					ret = app_parse_rth_conf(optarg);
415 					if (ret) {
416 						RTE_LOG(ERR, APP, "Invalid RX threshold configuration %s\n", optarg);
417 						return -1;
418 					}
419 					break;
420 				}
421 				if (str_is(optname, "tth")) {
422 					ret = app_parse_tth_conf(optarg);
423 					if (ret) {
424 						RTE_LOG(ERR, APP, "Invalid TX threshold configuration %s\n", optarg);
425 						return -1;
426 					}
427 					break;
428 				}
429 				if (str_is(optname, "cfg")) {
430 					cfg_profile = optarg;
431 					break;
432 				}
433 				break;
434 
435 			default:
436 				app_usage(prgname);
437 				return -1;
438 			}
439 	}
440 
441 	/* check master core index validity */
442 	for(i = 0; i <= app_master_core; i++) {
443 		if (app_used_core_mask & (1u << app_master_core)) {
444 			RTE_LOG(ERR, APP, "Master core index is not configured properly\n");
445 			app_usage(prgname);
446 			return -1;
447 		}
448 	}
449 	app_used_core_mask |= 1u << app_master_core;
450 
451 	if ((app_used_core_mask != app_eal_core_mask()) ||
452 			(app_master_core != rte_get_master_lcore())) {
453 		RTE_LOG(ERR, APP, "EAL core mask not configured properly, must be %" PRIx64
454 				" instead of %" PRIx64 "\n" , app_used_core_mask, app_eal_core_mask());
455 		return -1;
456 	}
457 
458 	if (nb_pfc == 0) {
459 		RTE_LOG(ERR, APP, "Packet flow not configured!\n");
460 		app_usage(prgname);
461 		return -1;
462 	}
463 
464 	/* sanity check for cores assignment */
465 	nb_lcores = app_cpu_core_count();
466 
467 	for(i = 0; i < nb_pfc; i++) {
468 		if (qos_conf[i].rx_core >= nb_lcores) {
469 			RTE_LOG(ERR, APP, "pfc %u: invalid RX lcore index %u\n", i + 1,
470 					qos_conf[i].rx_core);
471 			return -1;
472 		}
473 		if (qos_conf[i].wt_core >= nb_lcores) {
474 			RTE_LOG(ERR, APP, "pfc %u: invalid WT lcore index %u\n", i + 1,
475 					qos_conf[i].wt_core);
476 			return -1;
477 		}
478 		uint32_t rx_sock = rte_lcore_to_socket_id(qos_conf[i].rx_core);
479 		uint32_t wt_sock = rte_lcore_to_socket_id(qos_conf[i].wt_core);
480 		if (rx_sock != wt_sock) {
481 			RTE_LOG(ERR, APP, "pfc %u: RX and WT must be on the same socket\n", i + 1);
482 			return -1;
483 		}
484 		app_numa_mask |= 1 << rte_lcore_to_socket_id(qos_conf[i].rx_core);
485 	}
486 
487 	return 0;
488 }
489