xref: /dpdk/app/test-crypto-perf/cperf_options_parsing.c (revision f8be1786b1b8de697f68e9ffe2addff864783b06)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
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 Intel Corporation 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 <getopt.h>
34 #include <unistd.h>
35 
36 #include <rte_malloc.h>
37 
38 #include "cperf_options.h"
39 
40 struct name_id_map {
41 	const char *name;
42 	uint32_t id;
43 };
44 
45 static int
46 get_str_key_id_mapping(struct name_id_map *map, unsigned int map_len,
47 		const char *str_key)
48 {
49 	unsigned int i;
50 
51 	for (i = 0; i < map_len; i++) {
52 
53 		if (strcmp(str_key, map[i].name) == 0)
54 			return map[i].id;
55 	}
56 
57 	return -1;
58 }
59 
60 static int
61 parse_cperf_test_type(struct cperf_options *opts, const char *arg)
62 {
63 	struct name_id_map cperftest_namemap[] = {
64 		{
65 			cperf_test_type_strs[CPERF_TEST_TYPE_THROUGHPUT],
66 			CPERF_TEST_TYPE_THROUGHPUT
67 		},
68 		{
69 			cperf_test_type_strs[CPERF_TEST_TYPE_CYCLECOUNT],
70 			CPERF_TEST_TYPE_CYCLECOUNT
71 		},
72 		{
73 			cperf_test_type_strs[CPERF_TEST_TYPE_LATENCY],
74 			CPERF_TEST_TYPE_LATENCY
75 		}
76 	};
77 
78 	int id = get_str_key_id_mapping(
79 			(struct name_id_map *)cperftest_namemap,
80 			RTE_DIM(cperftest_namemap), arg);
81 	if (id < 0) {
82 		RTE_LOG(ERR, USER1, "failed to parse test type");
83 		return -1;
84 	}
85 
86 	opts->test = (enum cperf_perf_test_type)id;
87 
88 	return 0;
89 }
90 
91 static int
92 parse_uint32_t(uint32_t *value, const char *arg)
93 {
94 	char *end = NULL;
95 	unsigned long n = strtoul(arg, &end, 10);
96 
97 	if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
98 		return -1;
99 
100 	if (n > UINT32_MAX)
101 		return -ERANGE;
102 
103 	*value = (uint32_t) n;
104 
105 	return 0;
106 }
107 
108 static int
109 parse_uint16_t(uint16_t *value, const char *arg)
110 {
111 	uint32_t val = 0;
112 	int ret = parse_uint32_t(&val, arg);
113 
114 	if (ret < 0)
115 		return ret;
116 
117 	if (val > UINT16_MAX)
118 		return -ERANGE;
119 
120 	*value = (uint16_t) val;
121 
122 	return 0;
123 }
124 
125 static int
126 parse_total_ops(struct cperf_options *opts, const char *arg)
127 {
128 	int ret = parse_uint32_t(&opts->total_ops, arg);
129 
130 	if (ret)
131 		RTE_LOG(ERR, USER1, "failed to parse total operations count");
132 
133 	return ret;
134 }
135 
136 static int
137 parse_pool_sz(struct cperf_options *opts, const char *arg)
138 {
139 	int ret =  parse_uint32_t(&opts->pool_sz, arg);
140 
141 	if (ret)
142 		RTE_LOG(ERR, USER1, "failed to parse pool size");
143 	return ret;
144 }
145 
146 static int
147 parse_burst_sz(struct cperf_options *opts, const char *arg)
148 {
149 	int ret = parse_uint32_t(&opts->burst_sz, arg);
150 
151 	if (ret)
152 		RTE_LOG(ERR, USER1, "failed to parse burst size");
153 	return ret;
154 }
155 
156 static int
157 parse_buffer_sz(struct cperf_options *opts, const char *arg)
158 {
159 	uint32_t i, valid_buf_sz[] = {
160 			32, 64, 128, 256, 384, 512, 768, 1024, 1280, 1536, 1792,
161 			2048
162 	};
163 
164 	if (parse_uint32_t(&opts->buffer_sz, arg)) {
165 		RTE_LOG(ERR, USER1, "failed to parse buffer size");
166 		return -1;
167 	}
168 
169 	for (i = 0; i < RTE_DIM(valid_buf_sz); i++)
170 		if (valid_buf_sz[i] == opts->buffer_sz)
171 			return 0;
172 
173 	RTE_LOG(ERR, USER1, "invalid buffer size specified");
174 	return -1;
175 }
176 
177 static int
178 parse_segments_nb(struct cperf_options *opts, const char *arg)
179 {
180 	int ret = parse_uint32_t(&opts->segments_nb, arg);
181 
182 	if (ret) {
183 		RTE_LOG(ERR, USER1, "failed to parse segments number\n");
184 		return -1;
185 	}
186 
187 	if ((opts->segments_nb == 0) || (opts->segments_nb > 255)) {
188 		RTE_LOG(ERR, USER1, "invalid segments number specified\n");
189 		return -1;
190 	}
191 
192 	return 0;
193 }
194 
195 static int
196 parse_device_type(struct cperf_options *opts, const char *arg)
197 {
198 	if (strlen(arg) > (sizeof(opts->device_type) - 1))
199 		return -1;
200 
201 	strncpy(opts->device_type, arg, sizeof(opts->device_type));
202 
203 	return 0;
204 }
205 
206 static int
207 parse_op_type(struct cperf_options *opts, const char *arg)
208 {
209 	struct name_id_map optype_namemap[] = {
210 		{
211 			cperf_op_type_strs[CPERF_CIPHER_ONLY],
212 			CPERF_CIPHER_ONLY
213 		},
214 		{
215 			cperf_op_type_strs[CPERF_AUTH_ONLY],
216 			CPERF_AUTH_ONLY
217 		},
218 		{
219 			cperf_op_type_strs[CPERF_CIPHER_THEN_AUTH],
220 			CPERF_CIPHER_THEN_AUTH
221 		},
222 		{
223 			cperf_op_type_strs[CPERF_AUTH_THEN_CIPHER],
224 			CPERF_AUTH_THEN_CIPHER
225 		},
226 		{
227 			cperf_op_type_strs[CPERF_AEAD],
228 			CPERF_AEAD
229 		}
230 	};
231 
232 	int id = get_str_key_id_mapping(optype_namemap,
233 			RTE_DIM(optype_namemap), arg);
234 	if (id < 0) {
235 		RTE_LOG(ERR, USER1, "invalid opt type specified\n");
236 		return -1;
237 	}
238 
239 	opts->op_type = (enum cperf_op_type)id;
240 
241 	return 0;
242 }
243 
244 static int
245 parse_sessionless(struct cperf_options *opts,
246 		const char *arg __rte_unused)
247 {
248 	opts->sessionless = 1;
249 	return 0;
250 }
251 
252 static int
253 parse_out_of_place(struct cperf_options *opts,
254 		const char *arg __rte_unused)
255 {
256 	opts->out_of_place = 1;
257 	return 0;
258 }
259 
260 static int
261 parse_verify(struct cperf_options *opts,
262 		const char *arg __rte_unused)
263 {
264 	opts->verify = 1;
265 
266 	return 0;
267 }
268 
269 static int
270 parse_test_file(struct cperf_options *opts,
271 		const char *arg)
272 {
273 	opts->test_file = strdup(arg);
274 	if (access(opts->test_file, F_OK) != -1)
275 		return 0;
276 	RTE_LOG(ERR, USER1, "Test vector file doesn't exist\n");
277 
278 	return -1;
279 }
280 
281 static int
282 parse_test_name(struct cperf_options *opts,
283 		const char *arg)
284 {
285 	char *test_name = (char *) rte_zmalloc(NULL,
286 		sizeof(char) * (strlen(arg) + 3), 0);
287 	snprintf(test_name, strlen(arg) + 3, "[%s]", arg);
288 	opts->test_name = test_name;
289 
290 	return 0;
291 }
292 
293 static int
294 parse_silent(struct cperf_options *opts,
295 		const char *arg __rte_unused)
296 {
297 	opts->silent = 1;
298 
299 	return 0;
300 }
301 
302 static int
303 parse_cipher_algo(struct cperf_options *opts, const char *arg)
304 {
305 	struct name_id_map cipher_algo_namemap[] = {
306 		{
307 			rte_crypto_cipher_algorithm_strings
308 			[RTE_CRYPTO_CIPHER_3DES_CBC],
309 			RTE_CRYPTO_CIPHER_3DES_CBC
310 		},
311 		{
312 			rte_crypto_cipher_algorithm_strings
313 			[RTE_CRYPTO_CIPHER_3DES_ECB],
314 			RTE_CRYPTO_CIPHER_3DES_ECB
315 		},
316 		{
317 			rte_crypto_cipher_algorithm_strings
318 			[RTE_CRYPTO_CIPHER_3DES_CTR],
319 			RTE_CRYPTO_CIPHER_3DES_CTR
320 		},
321 		{
322 			rte_crypto_cipher_algorithm_strings
323 			[RTE_CRYPTO_CIPHER_AES_CBC],
324 			RTE_CRYPTO_CIPHER_AES_CBC
325 		},
326 		{
327 			rte_crypto_cipher_algorithm_strings
328 			[RTE_CRYPTO_CIPHER_AES_CCM],
329 			RTE_CRYPTO_CIPHER_AES_CCM
330 		},
331 		{
332 			rte_crypto_cipher_algorithm_strings
333 			[RTE_CRYPTO_CIPHER_AES_CTR],
334 			RTE_CRYPTO_CIPHER_AES_CTR
335 		},
336 		{
337 			rte_crypto_cipher_algorithm_strings
338 			[RTE_CRYPTO_CIPHER_AES_ECB],
339 			RTE_CRYPTO_CIPHER_AES_ECB
340 		},
341 		{
342 			rte_crypto_cipher_algorithm_strings
343 			[RTE_CRYPTO_CIPHER_AES_GCM],
344 			RTE_CRYPTO_CIPHER_AES_GCM
345 		},
346 		{
347 			rte_crypto_cipher_algorithm_strings
348 			[RTE_CRYPTO_CIPHER_AES_F8],
349 			RTE_CRYPTO_CIPHER_AES_F8
350 		},
351 		{
352 			rte_crypto_cipher_algorithm_strings
353 			[RTE_CRYPTO_CIPHER_AES_XTS],
354 			RTE_CRYPTO_CIPHER_AES_XTS
355 		},
356 		{
357 			rte_crypto_cipher_algorithm_strings
358 			[RTE_CRYPTO_CIPHER_ARC4],
359 			RTE_CRYPTO_CIPHER_ARC4
360 		},
361 		{
362 			rte_crypto_cipher_algorithm_strings
363 			[RTE_CRYPTO_CIPHER_NULL],
364 			RTE_CRYPTO_CIPHER_NULL
365 		},
366 		{
367 			rte_crypto_cipher_algorithm_strings
368 			[RTE_CRYPTO_CIPHER_KASUMI_F8],
369 			RTE_CRYPTO_CIPHER_KASUMI_F8
370 		},
371 		{
372 			rte_crypto_cipher_algorithm_strings
373 			[RTE_CRYPTO_CIPHER_SNOW3G_UEA2],
374 			RTE_CRYPTO_CIPHER_SNOW3G_UEA2
375 		},
376 		{
377 			rte_crypto_cipher_algorithm_strings
378 			[RTE_CRYPTO_CIPHER_ZUC_EEA3],
379 			RTE_CRYPTO_CIPHER_ZUC_EEA3
380 		},
381 	};
382 
383 
384 	int id = get_str_key_id_mapping(cipher_algo_namemap,
385 			RTE_DIM(cipher_algo_namemap), arg);
386 	if (id < 0) {
387 		RTE_LOG(ERR, USER1, "Invalid cipher algorithm specified\n");
388 		return -1;
389 	}
390 
391 	opts->cipher_algo = (enum rte_crypto_cipher_algorithm)id;
392 
393 	return 0;
394 }
395 
396 static int
397 parse_cipher_op(struct cperf_options *opts, const char *arg)
398 {
399 	struct name_id_map cipher_op_namemap[] = {
400 		{
401 			rte_crypto_cipher_operation_strings
402 			[RTE_CRYPTO_CIPHER_OP_ENCRYPT],
403 			RTE_CRYPTO_CIPHER_OP_ENCRYPT },
404 		{
405 			rte_crypto_cipher_operation_strings
406 			[RTE_CRYPTO_CIPHER_OP_DECRYPT],
407 			RTE_CRYPTO_CIPHER_OP_DECRYPT
408 		}
409 	};
410 
411 	int id = get_str_key_id_mapping(cipher_op_namemap,
412 			RTE_DIM(cipher_op_namemap), arg);
413 	if (id < 0) {
414 		RTE_LOG(ERR, USER1, "Invalid cipher operation specified\n");
415 		return -1;
416 	}
417 
418 	opts->cipher_op = (enum rte_crypto_cipher_operation)id;
419 
420 	return 0;
421 }
422 
423 static int
424 parse_cipher_key_sz(struct cperf_options *opts, const char *arg)
425 {
426 	return parse_uint16_t(&opts->cipher_key_sz, arg);
427 }
428 
429 static int
430 parse_cipher_iv_sz(struct cperf_options *opts, const char *arg)
431 {
432 	return parse_uint16_t(&opts->cipher_iv_sz, arg);
433 }
434 
435 static int
436 parse_auth_algo(struct cperf_options *opts, const char *arg) {
437 	struct name_id_map cipher_auth_namemap[] = {
438 		{
439 			rte_crypto_auth_algorithm_strings
440 			[RTE_CRYPTO_AUTH_AES_CBC_MAC],
441 			RTE_CRYPTO_AUTH_AES_CBC_MAC
442 		},
443 		{
444 			rte_crypto_auth_algorithm_strings
445 			[RTE_CRYPTO_AUTH_AES_CCM],
446 			RTE_CRYPTO_AUTH_AES_CCM
447 		},
448 		{
449 			rte_crypto_auth_algorithm_strings
450 			[RTE_CRYPTO_AUTH_AES_CMAC],
451 			RTE_CRYPTO_AUTH_AES_CMAC
452 		},
453 		{
454 			rte_crypto_auth_algorithm_strings
455 			[RTE_CRYPTO_AUTH_AES_GCM],
456 			RTE_CRYPTO_AUTH_AES_GCM
457 		},
458 		{
459 			rte_crypto_auth_algorithm_strings
460 			[RTE_CRYPTO_AUTH_AES_GMAC],
461 			RTE_CRYPTO_AUTH_AES_GMAC
462 		},
463 		{
464 			rte_crypto_auth_algorithm_strings
465 			[RTE_CRYPTO_AUTH_AES_XCBC_MAC],
466 			RTE_CRYPTO_AUTH_AES_XCBC_MAC
467 		},
468 		{
469 			rte_crypto_auth_algorithm_strings
470 			[RTE_CRYPTO_AUTH_MD5],
471 			RTE_CRYPTO_AUTH_MD5
472 		},
473 		{
474 			rte_crypto_auth_algorithm_strings
475 			[RTE_CRYPTO_AUTH_MD5_HMAC],
476 			RTE_CRYPTO_AUTH_MD5_HMAC
477 		},
478 		{
479 			rte_crypto_auth_algorithm_strings
480 			[RTE_CRYPTO_AUTH_SHA1],
481 			RTE_CRYPTO_AUTH_SHA1
482 		},
483 		{
484 			rte_crypto_auth_algorithm_strings
485 			[RTE_CRYPTO_AUTH_SHA1_HMAC],
486 			RTE_CRYPTO_AUTH_SHA1_HMAC
487 		},
488 		{
489 			rte_crypto_auth_algorithm_strings
490 			[RTE_CRYPTO_AUTH_SHA224],
491 			RTE_CRYPTO_AUTH_SHA224
492 		},
493 		{
494 			rte_crypto_auth_algorithm_strings
495 			[RTE_CRYPTO_AUTH_SHA224_HMAC],
496 			RTE_CRYPTO_AUTH_SHA224_HMAC
497 		},
498 		{
499 			rte_crypto_auth_algorithm_strings
500 			[RTE_CRYPTO_AUTH_SHA256],
501 			RTE_CRYPTO_AUTH_SHA256
502 		},
503 		{
504 			rte_crypto_auth_algorithm_strings
505 			[RTE_CRYPTO_AUTH_SHA256_HMAC],
506 			RTE_CRYPTO_AUTH_SHA256_HMAC
507 		},
508 		{
509 			rte_crypto_auth_algorithm_strings
510 			[RTE_CRYPTO_AUTH_SHA384],
511 			RTE_CRYPTO_AUTH_SHA384
512 		},
513 		{
514 			rte_crypto_auth_algorithm_strings
515 			[RTE_CRYPTO_AUTH_SHA384_HMAC],
516 			RTE_CRYPTO_AUTH_SHA384_HMAC
517 		},
518 		{
519 			rte_crypto_auth_algorithm_strings
520 			[RTE_CRYPTO_AUTH_SHA512],
521 			RTE_CRYPTO_AUTH_SHA512
522 		},
523 		{
524 			rte_crypto_auth_algorithm_strings
525 			[RTE_CRYPTO_AUTH_SHA512_HMAC],
526 			RTE_CRYPTO_AUTH_SHA512_HMAC
527 		},
528 		{
529 			rte_crypto_auth_algorithm_strings
530 			[RTE_CRYPTO_AUTH_KASUMI_F9],
531 			RTE_CRYPTO_AUTH_KASUMI_F9
532 		},
533 		{
534 			rte_crypto_auth_algorithm_strings
535 			[RTE_CRYPTO_AUTH_SNOW3G_UIA2],
536 			RTE_CRYPTO_AUTH_SNOW3G_UIA2
537 		},
538 		{
539 			rte_crypto_auth_algorithm_strings
540 			[RTE_CRYPTO_AUTH_ZUC_EIA3],
541 			RTE_CRYPTO_AUTH_ZUC_EIA3
542 		},
543 	};
544 
545 
546 	int id = get_str_key_id_mapping(cipher_auth_namemap,
547 			RTE_DIM(cipher_auth_namemap), arg);
548 	if (id < 0) {
549 		RTE_LOG(ERR, USER1, "invalid authentication algorithm specified"
550 				"\n");
551 		return -1;
552 	}
553 
554 	opts->auth_algo = (enum rte_crypto_auth_algorithm)id;
555 
556 	return 0;
557 }
558 
559 static int
560 parse_auth_op(struct cperf_options *opts, const char *arg)
561 {
562 	struct name_id_map auth_op_namemap[] = {
563 		{
564 			rte_crypto_auth_operation_strings
565 			[RTE_CRYPTO_AUTH_OP_GENERATE],
566 			RTE_CRYPTO_AUTH_OP_GENERATE },
567 		{
568 			rte_crypto_auth_operation_strings
569 			[RTE_CRYPTO_AUTH_OP_VERIFY],
570 			RTE_CRYPTO_AUTH_OP_VERIFY
571 		}
572 	};
573 
574 	int id = get_str_key_id_mapping(auth_op_namemap,
575 			RTE_DIM(auth_op_namemap), arg);
576 	if (id < 0) {
577 		RTE_LOG(ERR, USER1, "invalid authentication operation specified"
578 				"\n");
579 		return -1;
580 	}
581 
582 	opts->auth_op = (enum rte_crypto_auth_operation)id;
583 
584 	return 0;
585 }
586 
587 static int
588 parse_auth_key_sz(struct cperf_options *opts, const char *arg)
589 {
590 	return parse_uint16_t(&opts->auth_key_sz, arg);
591 }
592 
593 static int
594 parse_auth_digest_sz(struct cperf_options *opts, const char *arg)
595 {
596 	return parse_uint16_t(&opts->auth_digest_sz, arg);
597 }
598 
599 static int
600 parse_auth_aad_sz(struct cperf_options *opts, const char *arg)
601 {
602 	return parse_uint16_t(&opts->auth_aad_sz, arg);
603 }
604 
605 static int
606 parse_csv_friendly(struct cperf_options *opts, const char *arg __rte_unused)
607 {
608 	opts->csv = 1;
609 	opts->silent = 1;
610 	return 0;
611 }
612 
613 typedef int (*option_parser_t)(struct cperf_options *opts,
614 		const char *arg);
615 
616 struct long_opt_parser {
617 	const char *lgopt_name;
618 	option_parser_t parser_fn;
619 
620 };
621 
622 static struct option lgopts[] = {
623 
624 	{ CPERF_PTEST_TYPE, required_argument, 0, 0 },
625 
626 	{ CPERF_POOL_SIZE, required_argument, 0, 0 },
627 	{ CPERF_TOTAL_OPS, required_argument, 0, 0 },
628 	{ CPERF_BURST_SIZE, required_argument, 0, 0 },
629 	{ CPERF_BUFFER_SIZE, required_argument, 0, 0 },
630 	{ CPERF_SEGMENTS_NB, required_argument, 0, 0 },
631 
632 	{ CPERF_DEVTYPE, required_argument, 0, 0 },
633 	{ CPERF_OPTYPE, required_argument, 0, 0 },
634 
635 	{ CPERF_SILENT, no_argument, 0, 0 },
636 	{ CPERF_SESSIONLESS, no_argument, 0, 0 },
637 	{ CPERF_OUT_OF_PLACE, no_argument, 0, 0 },
638 	{ CPERF_VERIFY, no_argument, 0, 0 },
639 	{ CPERF_TEST_FILE, required_argument, 0, 0 },
640 	{ CPERF_TEST_NAME, required_argument, 0, 0 },
641 
642 	{ CPERF_CIPHER_ALGO, required_argument, 0, 0 },
643 	{ CPERF_CIPHER_OP, required_argument, 0, 0 },
644 
645 	{ CPERF_CIPHER_KEY_SZ, required_argument, 0, 0 },
646 	{ CPERF_CIPHER_IV_SZ, required_argument, 0, 0 },
647 
648 	{ CPERF_AUTH_ALGO, required_argument, 0, 0 },
649 	{ CPERF_AUTH_OP, required_argument, 0, 0 },
650 
651 	{ CPERF_AUTH_KEY_SZ, required_argument, 0, 0 },
652 	{ CPERF_AUTH_DIGEST_SZ, required_argument, 0, 0 },
653 	{ CPERF_AUTH_AAD_SZ, required_argument, 0, 0 },
654 	{ CPERF_CSV, no_argument, 0, 0},
655 
656 	{ NULL, 0, 0, 0 }
657 };
658 
659 void
660 cperf_options_default(struct cperf_options *opts)
661 {
662 	opts->test = CPERF_TEST_TYPE_THROUGHPUT;
663 
664 	opts->pool_sz = 8192;
665 	opts->total_ops = 10000000;
666 	opts->burst_sz = 32;
667 	opts->buffer_sz = 64;
668 	opts->segments_nb = 1;
669 
670 	strncpy(opts->device_type, "crypto_aesni_mb",
671 			sizeof(opts->device_type));
672 
673 	opts->op_type = CPERF_CIPHER_THEN_AUTH;
674 
675 	opts->silent = 0;
676 	opts->verify = 0;
677 	opts->test_file = NULL;
678 	opts->test_name = NULL;
679 	opts->sessionless = 0;
680 	opts->out_of_place = 0;
681 	opts->csv = 0;
682 
683 	opts->cipher_algo = RTE_CRYPTO_CIPHER_AES_CBC;
684 	opts->cipher_op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
685 	opts->cipher_key_sz = 16;
686 	opts->cipher_iv_sz = 16;
687 
688 	opts->auth_algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
689 	opts->auth_op = RTE_CRYPTO_AUTH_OP_GENERATE;
690 
691 	opts->auth_key_sz = 64;
692 	opts->auth_digest_sz = 12;
693 	opts->auth_aad_sz = 0;
694 }
695 
696 static int
697 cperf_opts_parse_long(int opt_idx, struct cperf_options *opts)
698 {
699 	struct long_opt_parser parsermap[] = {
700 		{ CPERF_PTEST_TYPE,	parse_cperf_test_type },
701 		{ CPERF_SILENT,		parse_silent },
702 		{ CPERF_POOL_SIZE,	parse_pool_sz },
703 		{ CPERF_TOTAL_OPS,	parse_total_ops },
704 		{ CPERF_BURST_SIZE,	parse_burst_sz },
705 		{ CPERF_BUFFER_SIZE,	parse_buffer_sz },
706 		{ CPERF_SEGMENTS_NB,	parse_segments_nb },
707 		{ CPERF_DEVTYPE,	parse_device_type },
708 		{ CPERF_OPTYPE,		parse_op_type },
709 		{ CPERF_SESSIONLESS,	parse_sessionless },
710 		{ CPERF_OUT_OF_PLACE,	parse_out_of_place },
711 		{ CPERF_VERIFY,		parse_verify },
712 		{ CPERF_TEST_FILE,	parse_test_file },
713 		{ CPERF_TEST_NAME,	parse_test_name },
714 		{ CPERF_CIPHER_ALGO,	parse_cipher_algo },
715 		{ CPERF_CIPHER_OP,	parse_cipher_op },
716 		{ CPERF_CIPHER_KEY_SZ,	parse_cipher_key_sz },
717 		{ CPERF_CIPHER_IV_SZ,	parse_cipher_iv_sz },
718 		{ CPERF_AUTH_ALGO,	parse_auth_algo },
719 		{ CPERF_AUTH_OP,	parse_auth_op },
720 		{ CPERF_AUTH_KEY_SZ,	parse_auth_key_sz },
721 		{ CPERF_AUTH_DIGEST_SZ,	parse_auth_digest_sz },
722 		{ CPERF_AUTH_AAD_SZ,	parse_auth_aad_sz },
723 		{ CPERF_CSV,	parse_csv_friendly},
724 	};
725 	unsigned int i;
726 
727 	for (i = 0; i < RTE_DIM(parsermap); i++) {
728 		if (strncmp(lgopts[opt_idx].name, parsermap[i].lgopt_name,
729 				strlen(lgopts[opt_idx].name)) == 0)
730 			return parsermap[i].parser_fn(opts, optarg);
731 	}
732 
733 	return -EINVAL;
734 }
735 
736 int
737 cperf_options_parse(struct cperf_options *options, int argc, char **argv)
738 {
739 	int opt, retval, opt_idx;
740 
741 	while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
742 		switch (opt) {
743 		/* long options */
744 		case 0:
745 
746 			retval = cperf_opts_parse_long(opt_idx, options);
747 			if (retval != 0)
748 				return retval;
749 
750 			break;
751 
752 		default:
753 			return -EINVAL;
754 		}
755 	}
756 
757 	return 0;
758 }
759 
760 int
761 cperf_options_check(struct cperf_options *options)
762 {
763 	if (options->segments_nb > options->buffer_sz) {
764 		RTE_LOG(ERR, USER1,
765 				"Segments number greater than buffer size.\n");
766 		return -EINVAL;
767 	}
768 
769 	if (options->verify && options->test_file == NULL) {
770 		RTE_LOG(ERR, USER1, "Define path to the file with test"
771 				" vectors.\n");
772 		return -EINVAL;
773 	}
774 
775 	if (options->test_name != NULL && options->test_file == NULL) {
776 		RTE_LOG(ERR, USER1, "Define path to the file with test"
777 				" vectors.\n");
778 		return -EINVAL;
779 	}
780 
781 	if (options->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY &&
782 			options->test_file == NULL) {
783 		RTE_LOG(ERR, USER1, "Define path to the file with test"
784 				" vectors.\n");
785 		return -EINVAL;
786 	}
787 
788 	if (options->verify &&
789 			options->total_ops > options->pool_sz) {
790 		RTE_LOG(ERR, USER1, "Total number of ops must be less than or"
791 				" equal to the pool size.\n");
792 		return -EINVAL;
793 	}
794 
795 	if (options->op_type == CPERF_CIPHER_THEN_AUTH) {
796 		if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
797 				options->auth_op !=
798 				RTE_CRYPTO_AUTH_OP_GENERATE) {
799 			RTE_LOG(ERR, USER1, "Option cipher then auth must use"
800 					" options: encrypt and generate.\n");
801 			return -EINVAL;
802 		}
803 	} else if (options->op_type == CPERF_AUTH_THEN_CIPHER) {
804 		if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_DECRYPT &&
805 				options->auth_op !=
806 				RTE_CRYPTO_AUTH_OP_VERIFY) {
807 			RTE_LOG(ERR, USER1, "Option auth then cipher must use"
808 					" options: decrypt and verify.\n");
809 			return -EINVAL;
810 		}
811 	} else if (options->op_type == CPERF_AEAD) {
812 		if (!(options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
813 				options->auth_op ==
814 				RTE_CRYPTO_AUTH_OP_GENERATE) &&
815 				!(options->cipher_op ==
816 				RTE_CRYPTO_CIPHER_OP_DECRYPT &&
817 				options->auth_op ==
818 				RTE_CRYPTO_AUTH_OP_VERIFY)) {
819 			RTE_LOG(ERR, USER1, "Use together options: encrypt and"
820 					" generate or decrypt and verify.\n");
821 			return -EINVAL;
822 		}
823 	}
824 
825 	return 0;
826 }
827 
828 void
829 cperf_options_dump(struct cperf_options *opts)
830 {
831 	printf("# Crypto Performance Application Options:\n");
832 	printf("#\n");
833 	printf("# cperf test: %s\n", cperf_test_type_strs[opts->test]);
834 	printf("#\n");
835 	printf("# size of crypto op / mbuf pool: %u\n", opts->pool_sz);
836 	printf("# total number of ops: %u\n", opts->total_ops);
837 	printf("# burst size: %u\n", opts->burst_sz);
838 	printf("# buffer size: %u\n", opts->buffer_sz);
839 	printf("# segments per buffer: %u\n", opts->segments_nb);
840 	printf("#\n");
841 	printf("# cryptodev type: %s\n", opts->device_type);
842 	printf("#\n");
843 	printf("# crypto operation: %s\n", cperf_op_type_strs[opts->op_type]);
844 	printf("# verify operation: %s\n", opts->verify ? "yes" : "no");
845 	printf("# sessionless: %s\n", opts->sessionless ? "yes" : "no");
846 	printf("# out of place: %s\n", opts->out_of_place ? "yes" : "no");
847 
848 	printf("#\n");
849 
850 	if (opts->op_type == CPERF_AUTH_ONLY ||
851 			opts->op_type == CPERF_CIPHER_THEN_AUTH ||
852 			opts->op_type == CPERF_AUTH_THEN_CIPHER ||
853 			opts->op_type == CPERF_AEAD) {
854 		printf("# auth algorithm: %s\n",
855 			rte_crypto_auth_algorithm_strings[opts->auth_algo]);
856 		printf("# auth operation: %s\n",
857 			rte_crypto_auth_operation_strings[opts->auth_op]);
858 		printf("# auth key size: %u\n", opts->auth_key_sz);
859 		printf("# auth digest size: %u\n", opts->auth_digest_sz);
860 		printf("# auth aad size: %u\n", opts->auth_aad_sz);
861 		printf("#\n");
862 	}
863 
864 	if (opts->op_type == CPERF_CIPHER_ONLY ||
865 			opts->op_type == CPERF_CIPHER_THEN_AUTH ||
866 			opts->op_type == CPERF_AUTH_THEN_CIPHER ||
867 			opts->op_type == CPERF_AEAD) {
868 		printf("# cipher algorithm: %s\n",
869 			rte_crypto_cipher_algorithm_strings[opts->cipher_algo]);
870 		printf("# cipher operation: %s\n",
871 			rte_crypto_cipher_operation_strings[opts->cipher_op]);
872 		printf("# cipher key size: %u\n", opts->cipher_key_sz);
873 		printf("# cipher iv size: %u\n", opts->cipher_iv_sz);
874 		printf("#\n");
875 	}
876 }
877