xref: /dpdk/app/test-crypto-perf/cperf_options_parsing.c (revision c36432166982e745ee79cbb00e54522138922bc6)
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_cryptodev.h>
37 #include <rte_malloc.h>
38 
39 #include "cperf_options.h"
40 
41 #define AES_BLOCK_SIZE 16
42 #define DES_BLOCK_SIZE 8
43 
44 struct name_id_map {
45 	const char *name;
46 	uint32_t id;
47 };
48 
49 static int
50 get_str_key_id_mapping(struct name_id_map *map, unsigned int map_len,
51 		const char *str_key)
52 {
53 	unsigned int i;
54 
55 	for (i = 0; i < map_len; i++) {
56 
57 		if (strcmp(str_key, map[i].name) == 0)
58 			return map[i].id;
59 	}
60 
61 	return -1;
62 }
63 
64 static int
65 parse_cperf_test_type(struct cperf_options *opts, const char *arg)
66 {
67 	struct name_id_map cperftest_namemap[] = {
68 		{
69 			cperf_test_type_strs[CPERF_TEST_TYPE_THROUGHPUT],
70 			CPERF_TEST_TYPE_THROUGHPUT
71 		},
72 		{
73 			cperf_test_type_strs[CPERF_TEST_TYPE_VERIFY],
74 			CPERF_TEST_TYPE_VERIFY
75 		},
76 		{
77 			cperf_test_type_strs[CPERF_TEST_TYPE_LATENCY],
78 			CPERF_TEST_TYPE_LATENCY
79 		}
80 	};
81 
82 	int id = get_str_key_id_mapping(
83 			(struct name_id_map *)cperftest_namemap,
84 			RTE_DIM(cperftest_namemap), arg);
85 	if (id < 0) {
86 		RTE_LOG(ERR, USER1, "failed to parse test type");
87 		return -1;
88 	}
89 
90 	opts->test = (enum cperf_perf_test_type)id;
91 
92 	return 0;
93 }
94 
95 static int
96 parse_uint32_t(uint32_t *value, const char *arg)
97 {
98 	char *end = NULL;
99 	unsigned long n = strtoul(arg, &end, 10);
100 
101 	if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
102 		return -1;
103 
104 	if (n > UINT32_MAX)
105 		return -ERANGE;
106 
107 	*value = (uint32_t) n;
108 
109 	return 0;
110 }
111 
112 static int
113 parse_uint16_t(uint16_t *value, const char *arg)
114 {
115 	uint32_t val = 0;
116 	int ret = parse_uint32_t(&val, arg);
117 
118 	if (ret < 0)
119 		return ret;
120 
121 	if (val > UINT16_MAX)
122 		return -ERANGE;
123 
124 	*value = (uint16_t) val;
125 
126 	return 0;
127 }
128 
129 static int
130 parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
131 {
132 	char *token;
133 	uint32_t number;
134 
135 	char *copy_arg = strdup(arg);
136 
137 	if (copy_arg == NULL)
138 		return -1;
139 
140 	errno = 0;
141 	token = strtok(copy_arg, ":");
142 
143 	/* Parse minimum value */
144 	if (token != NULL) {
145 		number = strtoul(token, NULL, 10);
146 
147 		if (errno == EINVAL || errno == ERANGE ||
148 				number == 0)
149 			goto err_range;
150 
151 		*min = number;
152 	} else
153 		goto err_range;
154 
155 	token = strtok(NULL, ":");
156 
157 	/* Parse increment value */
158 	if (token != NULL) {
159 		number = strtoul(token, NULL, 10);
160 
161 		if (errno == EINVAL || errno == ERANGE ||
162 				number == 0)
163 			goto err_range;
164 
165 		*inc = number;
166 	} else
167 		goto err_range;
168 
169 	token = strtok(NULL, ":");
170 
171 	/* Parse maximum value */
172 	if (token != NULL) {
173 		number = strtoul(token, NULL, 10);
174 
175 		if (errno == EINVAL || errno == ERANGE ||
176 				number == 0 ||
177 				number < *min)
178 			goto err_range;
179 
180 		*max = number;
181 	} else
182 		goto err_range;
183 
184 	if (strtok(NULL, ":") != NULL)
185 		goto err_range;
186 
187 	free(copy_arg);
188 	return 0;
189 
190 err_range:
191 	free(copy_arg);
192 	return -1;
193 }
194 
195 static int
196 parse_list(const char *arg, uint32_t *list, uint32_t *min, uint32_t *max)
197 {
198 	char *token;
199 	uint32_t number;
200 	uint8_t count = 0;
201 
202 	char *copy_arg = strdup(arg);
203 
204 	if (copy_arg == NULL)
205 		return -1;
206 
207 	errno = 0;
208 	token = strtok(copy_arg, ",");
209 
210 	/* Parse first value */
211 	if (token != NULL) {
212 		number = strtoul(token, NULL, 10);
213 
214 		if (errno == EINVAL || errno == ERANGE ||
215 				number == 0)
216 			goto err_list;
217 
218 		list[count++] = number;
219 		*min = number;
220 		*max = number;
221 	} else
222 		goto err_list;
223 
224 	token = strtok(NULL, ",");
225 
226 	while (token != NULL) {
227 		if (count == MAX_LIST) {
228 			RTE_LOG(WARNING, USER1, "Using only the first %u sizes\n",
229 					MAX_LIST);
230 			break;
231 		}
232 
233 		number = strtoul(token, NULL, 10);
234 
235 		if (errno == EINVAL || errno == ERANGE ||
236 				number == 0)
237 			goto err_list;
238 
239 		list[count++] = number;
240 
241 		if (number < *min)
242 			*min = number;
243 		if (number > *max)
244 			*max = number;
245 
246 		token = strtok(NULL, ",");
247 	}
248 
249 	free(copy_arg);
250 	return count;
251 
252 err_list:
253 	free(copy_arg);
254 	return -1;
255 }
256 
257 static int
258 parse_total_ops(struct cperf_options *opts, const char *arg)
259 {
260 	int ret = parse_uint32_t(&opts->total_ops, arg);
261 
262 	if (ret)
263 		RTE_LOG(ERR, USER1, "failed to parse total operations count\n");
264 
265 	if (opts->total_ops == 0) {
266 		RTE_LOG(ERR, USER1,
267 				"invalid total operations count number specified\n");
268 		return -1;
269 	}
270 
271 	return ret;
272 }
273 
274 static int
275 parse_pool_sz(struct cperf_options *opts, const char *arg)
276 {
277 	int ret =  parse_uint32_t(&opts->pool_sz, arg);
278 
279 	if (ret)
280 		RTE_LOG(ERR, USER1, "failed to parse pool size");
281 	return ret;
282 }
283 
284 static int
285 parse_burst_sz(struct cperf_options *opts, const char *arg)
286 {
287 	int ret;
288 
289 	/* Try parsing the argument as a range, if it fails, parse it as a list */
290 	if (parse_range(arg, &opts->min_burst_size, &opts->max_burst_size,
291 			&opts->inc_burst_size) < 0) {
292 		ret = parse_list(arg, opts->burst_size_list,
293 					&opts->min_burst_size,
294 					&opts->max_burst_size);
295 		if (ret < 0) {
296 			RTE_LOG(ERR, USER1, "failed to parse burst size/s\n");
297 			return -1;
298 		}
299 		opts->burst_size_count = ret;
300 	}
301 
302 	return 0;
303 }
304 
305 static int
306 parse_buffer_sz(struct cperf_options *opts, const char *arg)
307 {
308 	int ret;
309 
310 	/* Try parsing the argument as a range, if it fails, parse it as a list */
311 	if (parse_range(arg, &opts->min_buffer_size, &opts->max_buffer_size,
312 			&opts->inc_buffer_size) < 0) {
313 		ret = parse_list(arg, opts->buffer_size_list,
314 					&opts->min_buffer_size,
315 					&opts->max_buffer_size);
316 		if (ret < 0) {
317 			RTE_LOG(ERR, USER1, "failed to parse buffer size/s\n");
318 			return -1;
319 		}
320 		opts->buffer_size_count = ret;
321 	}
322 
323 	return 0;
324 }
325 
326 static int
327 parse_segments_nb(struct cperf_options *opts, const char *arg)
328 {
329 	int ret = parse_uint32_t(&opts->segments_nb, arg);
330 
331 	if (ret) {
332 		RTE_LOG(ERR, USER1, "failed to parse segments number\n");
333 		return -1;
334 	}
335 
336 	if ((opts->segments_nb == 0) || (opts->segments_nb > 255)) {
337 		RTE_LOG(ERR, USER1, "invalid segments number specified\n");
338 		return -1;
339 	}
340 
341 	return 0;
342 }
343 
344 static int
345 parse_desc_nb(struct cperf_options *opts, const char *arg)
346 {
347 	int ret = parse_uint32_t(&opts->nb_descriptors, arg);
348 
349 	if (ret) {
350 		RTE_LOG(ERR, USER1, "failed to parse descriptors number\n");
351 		return -1;
352 	}
353 
354 	if (opts->nb_descriptors == 0) {
355 		RTE_LOG(ERR, USER1, "invalid descriptors number specified\n");
356 		return -1;
357 	}
358 
359 	return 0;
360 }
361 
362 static int
363 parse_device_type(struct cperf_options *opts, const char *arg)
364 {
365 	if (strlen(arg) > (sizeof(opts->device_type) - 1))
366 		return -1;
367 
368 	strncpy(opts->device_type, arg, sizeof(opts->device_type) - 1);
369 	*(opts->device_type + sizeof(opts->device_type) - 1) = '\0';
370 
371 	return 0;
372 }
373 
374 static int
375 parse_op_type(struct cperf_options *opts, const char *arg)
376 {
377 	struct name_id_map optype_namemap[] = {
378 		{
379 			cperf_op_type_strs[CPERF_CIPHER_ONLY],
380 			CPERF_CIPHER_ONLY
381 		},
382 		{
383 			cperf_op_type_strs[CPERF_AUTH_ONLY],
384 			CPERF_AUTH_ONLY
385 		},
386 		{
387 			cperf_op_type_strs[CPERF_CIPHER_THEN_AUTH],
388 			CPERF_CIPHER_THEN_AUTH
389 		},
390 		{
391 			cperf_op_type_strs[CPERF_AUTH_THEN_CIPHER],
392 			CPERF_AUTH_THEN_CIPHER
393 		},
394 		{
395 			cperf_op_type_strs[CPERF_AEAD],
396 			CPERF_AEAD
397 		}
398 	};
399 
400 	int id = get_str_key_id_mapping(optype_namemap,
401 			RTE_DIM(optype_namemap), arg);
402 	if (id < 0) {
403 		RTE_LOG(ERR, USER1, "invalid opt type specified\n");
404 		return -1;
405 	}
406 
407 	opts->op_type = (enum cperf_op_type)id;
408 
409 	return 0;
410 }
411 
412 static int
413 parse_sessionless(struct cperf_options *opts,
414 		const char *arg __rte_unused)
415 {
416 	opts->sessionless = 1;
417 	return 0;
418 }
419 
420 static int
421 parse_out_of_place(struct cperf_options *opts,
422 		const char *arg __rte_unused)
423 {
424 	opts->out_of_place = 1;
425 	return 0;
426 }
427 
428 static int
429 parse_test_file(struct cperf_options *opts,
430 		const char *arg)
431 {
432 	opts->test_file = strdup(arg);
433 	if (access(opts->test_file, F_OK) != -1)
434 		return 0;
435 	RTE_LOG(ERR, USER1, "Test vector file doesn't exist\n");
436 
437 	return -1;
438 }
439 
440 static int
441 parse_test_name(struct cperf_options *opts,
442 		const char *arg)
443 {
444 	char *test_name = (char *) rte_zmalloc(NULL,
445 		sizeof(char) * (strlen(arg) + 3), 0);
446 	snprintf(test_name, strlen(arg) + 3, "[%s]", arg);
447 	opts->test_name = test_name;
448 
449 	return 0;
450 }
451 
452 static int
453 parse_silent(struct cperf_options *opts,
454 		const char *arg __rte_unused)
455 {
456 	opts->silent = 1;
457 
458 	return 0;
459 }
460 
461 static int
462 parse_cipher_algo(struct cperf_options *opts, const char *arg)
463 {
464 
465 	enum rte_crypto_cipher_algorithm cipher_algo;
466 
467 	if (rte_cryptodev_get_cipher_algo_enum(&cipher_algo, arg) < 0) {
468 		RTE_LOG(ERR, USER1, "Invalid cipher algorithm specified\n");
469 		return -1;
470 	}
471 
472 	opts->cipher_algo = cipher_algo;
473 
474 	return 0;
475 }
476 
477 static int
478 parse_cipher_op(struct cperf_options *opts, const char *arg)
479 {
480 	struct name_id_map cipher_op_namemap[] = {
481 		{
482 			rte_crypto_cipher_operation_strings
483 			[RTE_CRYPTO_CIPHER_OP_ENCRYPT],
484 			RTE_CRYPTO_CIPHER_OP_ENCRYPT },
485 		{
486 			rte_crypto_cipher_operation_strings
487 			[RTE_CRYPTO_CIPHER_OP_DECRYPT],
488 			RTE_CRYPTO_CIPHER_OP_DECRYPT
489 		}
490 	};
491 
492 	int id = get_str_key_id_mapping(cipher_op_namemap,
493 			RTE_DIM(cipher_op_namemap), arg);
494 	if (id < 0) {
495 		RTE_LOG(ERR, USER1, "Invalid cipher operation specified\n");
496 		return -1;
497 	}
498 
499 	opts->cipher_op = (enum rte_crypto_cipher_operation)id;
500 
501 	return 0;
502 }
503 
504 static int
505 parse_cipher_key_sz(struct cperf_options *opts, const char *arg)
506 {
507 	return parse_uint16_t(&opts->cipher_key_sz, arg);
508 }
509 
510 static int
511 parse_cipher_iv_sz(struct cperf_options *opts, const char *arg)
512 {
513 	return parse_uint16_t(&opts->cipher_iv_sz, arg);
514 }
515 
516 static int
517 parse_auth_algo(struct cperf_options *opts, const char *arg)
518 {
519 	enum rte_crypto_auth_algorithm auth_algo;
520 
521 	if (rte_cryptodev_get_auth_algo_enum(&auth_algo, arg) < 0) {
522 		RTE_LOG(ERR, USER1, "Invalid authentication algorithm specified\n");
523 		return -1;
524 	}
525 
526 	opts->auth_algo = auth_algo;
527 
528 	return 0;
529 }
530 
531 static int
532 parse_auth_op(struct cperf_options *opts, const char *arg)
533 {
534 	struct name_id_map auth_op_namemap[] = {
535 		{
536 			rte_crypto_auth_operation_strings
537 			[RTE_CRYPTO_AUTH_OP_GENERATE],
538 			RTE_CRYPTO_AUTH_OP_GENERATE },
539 		{
540 			rte_crypto_auth_operation_strings
541 			[RTE_CRYPTO_AUTH_OP_VERIFY],
542 			RTE_CRYPTO_AUTH_OP_VERIFY
543 		}
544 	};
545 
546 	int id = get_str_key_id_mapping(auth_op_namemap,
547 			RTE_DIM(auth_op_namemap), arg);
548 	if (id < 0) {
549 		RTE_LOG(ERR, USER1, "invalid authentication operation specified"
550 				"\n");
551 		return -1;
552 	}
553 
554 	opts->auth_op = (enum rte_crypto_auth_operation)id;
555 
556 	return 0;
557 }
558 
559 static int
560 parse_auth_key_sz(struct cperf_options *opts, const char *arg)
561 {
562 	return parse_uint16_t(&opts->auth_key_sz, arg);
563 }
564 
565 static int
566 parse_digest_sz(struct cperf_options *opts, const char *arg)
567 {
568 	return parse_uint16_t(&opts->digest_sz, arg);
569 }
570 
571 static int
572 parse_auth_iv_sz(struct cperf_options *opts, const char *arg)
573 {
574 	return parse_uint16_t(&opts->auth_iv_sz, arg);
575 }
576 
577 static int
578 parse_aead_algo(struct cperf_options *opts, const char *arg)
579 {
580 	enum rte_crypto_aead_algorithm aead_algo;
581 
582 	if (rte_cryptodev_get_aead_algo_enum(&aead_algo, arg) < 0) {
583 		RTE_LOG(ERR, USER1, "Invalid AEAD algorithm specified\n");
584 		return -1;
585 	}
586 
587 	opts->aead_algo = aead_algo;
588 
589 	return 0;
590 }
591 
592 static int
593 parse_aead_op(struct cperf_options *opts, const char *arg)
594 {
595 	struct name_id_map aead_op_namemap[] = {
596 		{
597 			rte_crypto_aead_operation_strings
598 			[RTE_CRYPTO_AEAD_OP_ENCRYPT],
599 			RTE_CRYPTO_AEAD_OP_ENCRYPT },
600 		{
601 			rte_crypto_aead_operation_strings
602 			[RTE_CRYPTO_AEAD_OP_DECRYPT],
603 			RTE_CRYPTO_AEAD_OP_DECRYPT
604 		}
605 	};
606 
607 	int id = get_str_key_id_mapping(aead_op_namemap,
608 			RTE_DIM(aead_op_namemap), arg);
609 	if (id < 0) {
610 		RTE_LOG(ERR, USER1, "invalid AEAD operation specified"
611 				"\n");
612 		return -1;
613 	}
614 
615 	opts->aead_op = (enum rte_crypto_aead_operation)id;
616 
617 	return 0;
618 }
619 
620 static int
621 parse_aead_key_sz(struct cperf_options *opts, const char *arg)
622 {
623 	return parse_uint16_t(&opts->aead_key_sz, arg);
624 }
625 
626 static int
627 parse_aead_iv_sz(struct cperf_options *opts, const char *arg)
628 {
629 	return parse_uint16_t(&opts->aead_iv_sz, arg);
630 }
631 
632 static int
633 parse_aead_aad_sz(struct cperf_options *opts, const char *arg)
634 {
635 	return parse_uint16_t(&opts->aead_aad_sz, arg);
636 }
637 
638 static int
639 parse_csv_friendly(struct cperf_options *opts, const char *arg __rte_unused)
640 {
641 	opts->csv = 1;
642 	opts->silent = 1;
643 	return 0;
644 }
645 
646 typedef int (*option_parser_t)(struct cperf_options *opts,
647 		const char *arg);
648 
649 struct long_opt_parser {
650 	const char *lgopt_name;
651 	option_parser_t parser_fn;
652 
653 };
654 
655 static struct option lgopts[] = {
656 
657 	{ CPERF_PTEST_TYPE, required_argument, 0, 0 },
658 
659 	{ CPERF_POOL_SIZE, required_argument, 0, 0 },
660 	{ CPERF_TOTAL_OPS, required_argument, 0, 0 },
661 	{ CPERF_BURST_SIZE, required_argument, 0, 0 },
662 	{ CPERF_BUFFER_SIZE, required_argument, 0, 0 },
663 	{ CPERF_SEGMENTS_NB, required_argument, 0, 0 },
664 	{ CPERF_DESC_NB, required_argument, 0, 0 },
665 
666 	{ CPERF_DEVTYPE, required_argument, 0, 0 },
667 	{ CPERF_OPTYPE, required_argument, 0, 0 },
668 
669 	{ CPERF_SILENT, no_argument, 0, 0 },
670 	{ CPERF_SESSIONLESS, no_argument, 0, 0 },
671 	{ CPERF_OUT_OF_PLACE, no_argument, 0, 0 },
672 	{ CPERF_TEST_FILE, required_argument, 0, 0 },
673 	{ CPERF_TEST_NAME, required_argument, 0, 0 },
674 
675 	{ CPERF_CIPHER_ALGO, required_argument, 0, 0 },
676 	{ CPERF_CIPHER_OP, required_argument, 0, 0 },
677 
678 	{ CPERF_CIPHER_KEY_SZ, required_argument, 0, 0 },
679 	{ CPERF_CIPHER_IV_SZ, required_argument, 0, 0 },
680 
681 	{ CPERF_AUTH_ALGO, required_argument, 0, 0 },
682 	{ CPERF_AUTH_OP, required_argument, 0, 0 },
683 
684 	{ CPERF_AUTH_KEY_SZ, required_argument, 0, 0 },
685 	{ CPERF_AUTH_IV_SZ, required_argument, 0, 0 },
686 
687 	{ CPERF_AEAD_ALGO, required_argument, 0, 0 },
688 	{ CPERF_AEAD_OP, required_argument, 0, 0 },
689 
690 	{ CPERF_AEAD_KEY_SZ, required_argument, 0, 0 },
691 	{ CPERF_AEAD_AAD_SZ, required_argument, 0, 0 },
692 	{ CPERF_AEAD_IV_SZ, required_argument, 0, 0 },
693 
694 	{ CPERF_DIGEST_SZ, required_argument, 0, 0 },
695 
696 	{ CPERF_CSV, no_argument, 0, 0},
697 
698 	{ NULL, 0, 0, 0 }
699 };
700 
701 void
702 cperf_options_default(struct cperf_options *opts)
703 {
704 	opts->test = CPERF_TEST_TYPE_THROUGHPUT;
705 
706 	opts->pool_sz = 8192;
707 	opts->total_ops = 10000000;
708 	opts->nb_descriptors = 2048;
709 
710 	opts->buffer_size_list[0] = 64;
711 	opts->buffer_size_count = 1;
712 	opts->max_buffer_size = 64;
713 	opts->min_buffer_size = 64;
714 	opts->inc_buffer_size = 0;
715 
716 	opts->burst_size_list[0] = 32;
717 	opts->burst_size_count = 1;
718 	opts->max_burst_size = 32;
719 	opts->min_burst_size = 32;
720 	opts->inc_burst_size = 0;
721 
722 	opts->segments_nb = 1;
723 
724 	strncpy(opts->device_type, "crypto_aesni_mb",
725 			sizeof(opts->device_type));
726 
727 	opts->op_type = CPERF_CIPHER_THEN_AUTH;
728 
729 	opts->silent = 0;
730 	opts->test_file = NULL;
731 	opts->test_name = NULL;
732 	opts->sessionless = 0;
733 	opts->out_of_place = 0;
734 	opts->csv = 0;
735 
736 	opts->cipher_algo = RTE_CRYPTO_CIPHER_AES_CBC;
737 	opts->cipher_op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
738 	opts->cipher_key_sz = 16;
739 	opts->cipher_iv_sz = 16;
740 
741 	opts->auth_algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
742 	opts->auth_op = RTE_CRYPTO_AUTH_OP_GENERATE;
743 
744 	opts->auth_key_sz = 64;
745 	opts->auth_iv_sz = 0;
746 
747 	opts->aead_key_sz = 0;
748 	opts->aead_iv_sz = 0;
749 	opts->aead_aad_sz = 0;
750 
751 	opts->digest_sz = 12;
752 }
753 
754 static int
755 cperf_opts_parse_long(int opt_idx, struct cperf_options *opts)
756 {
757 	struct long_opt_parser parsermap[] = {
758 		{ CPERF_PTEST_TYPE,	parse_cperf_test_type },
759 		{ CPERF_SILENT,		parse_silent },
760 		{ CPERF_POOL_SIZE,	parse_pool_sz },
761 		{ CPERF_TOTAL_OPS,	parse_total_ops },
762 		{ CPERF_BURST_SIZE,	parse_burst_sz },
763 		{ CPERF_BUFFER_SIZE,	parse_buffer_sz },
764 		{ CPERF_SEGMENTS_NB,	parse_segments_nb },
765 		{ CPERF_DESC_NB,	parse_desc_nb },
766 		{ CPERF_DEVTYPE,	parse_device_type },
767 		{ CPERF_OPTYPE,		parse_op_type },
768 		{ CPERF_SESSIONLESS,	parse_sessionless },
769 		{ CPERF_OUT_OF_PLACE,	parse_out_of_place },
770 		{ CPERF_TEST_FILE,	parse_test_file },
771 		{ CPERF_TEST_NAME,	parse_test_name },
772 		{ CPERF_CIPHER_ALGO,	parse_cipher_algo },
773 		{ CPERF_CIPHER_OP,	parse_cipher_op },
774 		{ CPERF_CIPHER_KEY_SZ,	parse_cipher_key_sz },
775 		{ CPERF_CIPHER_IV_SZ,	parse_cipher_iv_sz },
776 		{ CPERF_AUTH_ALGO,	parse_auth_algo },
777 		{ CPERF_AUTH_OP,	parse_auth_op },
778 		{ CPERF_AUTH_KEY_SZ,	parse_auth_key_sz },
779 		{ CPERF_AUTH_IV_SZ,	parse_auth_iv_sz },
780 		{ CPERF_AEAD_ALGO,	parse_aead_algo },
781 		{ CPERF_AEAD_OP,	parse_aead_op },
782 		{ CPERF_AEAD_KEY_SZ,	parse_aead_key_sz },
783 		{ CPERF_AEAD_IV_SZ,	parse_aead_iv_sz },
784 		{ CPERF_AEAD_AAD_SZ,	parse_aead_aad_sz },
785 		{ CPERF_DIGEST_SZ,	parse_digest_sz },
786 		{ CPERF_CSV,		parse_csv_friendly},
787 	};
788 	unsigned int i;
789 
790 	for (i = 0; i < RTE_DIM(parsermap); i++) {
791 		if (strncmp(lgopts[opt_idx].name, parsermap[i].lgopt_name,
792 				strlen(lgopts[opt_idx].name)) == 0)
793 			return parsermap[i].parser_fn(opts, optarg);
794 	}
795 
796 	return -EINVAL;
797 }
798 
799 int
800 cperf_options_parse(struct cperf_options *options, int argc, char **argv)
801 {
802 	int opt, retval, opt_idx;
803 
804 	while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
805 		switch (opt) {
806 		/* long options */
807 		case 0:
808 
809 			retval = cperf_opts_parse_long(opt_idx, options);
810 			if (retval != 0)
811 				return retval;
812 
813 			break;
814 
815 		default:
816 			return -EINVAL;
817 		}
818 	}
819 
820 	return 0;
821 }
822 
823 static int
824 check_cipher_buffer_length(struct cperf_options *options)
825 {
826 	uint32_t buffer_size, buffer_size_idx = 0;
827 
828 	if (options->cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC ||
829 			options->cipher_algo == RTE_CRYPTO_CIPHER_AES_ECB) {
830 		if (options->inc_buffer_size != 0)
831 			buffer_size = options->min_buffer_size;
832 		else
833 			buffer_size = options->buffer_size_list[0];
834 
835 		while (buffer_size <= options->max_buffer_size) {
836 			if ((buffer_size % AES_BLOCK_SIZE) != 0) {
837 				RTE_LOG(ERR, USER1, "Some of the buffer sizes are "
838 					"not suitable for the algorithm selected\n");
839 				return -EINVAL;
840 			}
841 
842 			if (options->inc_buffer_size != 0)
843 				buffer_size += options->inc_buffer_size;
844 			else {
845 				if (++buffer_size_idx == options->buffer_size_count)
846 					break;
847 				buffer_size = options->buffer_size_list[buffer_size_idx];
848 			}
849 
850 		}
851 	}
852 
853 	if (options->cipher_algo == RTE_CRYPTO_CIPHER_DES_CBC ||
854 			options->cipher_algo == RTE_CRYPTO_CIPHER_3DES_CBC ||
855 			options->cipher_algo == RTE_CRYPTO_CIPHER_3DES_ECB) {
856 		for (buffer_size = options->min_buffer_size;
857 				buffer_size < options->max_buffer_size;
858 				buffer_size += options->inc_buffer_size) {
859 			if ((buffer_size % DES_BLOCK_SIZE) != 0) {
860 				RTE_LOG(ERR, USER1, "Some of the buffer sizes are "
861 					"not suitable for the algorithm selected\n");
862 				return -EINVAL;
863 			}
864 		}
865 	}
866 
867 	return 0;
868 }
869 
870 int
871 cperf_options_check(struct cperf_options *options)
872 {
873 	if (options->segments_nb > options->min_buffer_size) {
874 		RTE_LOG(ERR, USER1,
875 				"Segments number greater than buffer size.\n");
876 		return -EINVAL;
877 	}
878 
879 	if (options->test == CPERF_TEST_TYPE_VERIFY &&
880 			options->test_file == NULL) {
881 		RTE_LOG(ERR, USER1, "Define path to the file with test"
882 				" vectors.\n");
883 		return -EINVAL;
884 	}
885 
886 	if (options->test == CPERF_TEST_TYPE_VERIFY &&
887 			options->op_type != CPERF_CIPHER_ONLY &&
888 			options->test_name == NULL) {
889 		RTE_LOG(ERR, USER1, "Define test name to get the correct digest"
890 				" from the test vectors.\n");
891 		return -EINVAL;
892 	}
893 
894 	if (options->test_name != NULL && options->test_file == NULL) {
895 		RTE_LOG(ERR, USER1, "Define path to the file with test"
896 				" vectors.\n");
897 		return -EINVAL;
898 	}
899 
900 	if (options->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY &&
901 			options->test_file == NULL) {
902 		RTE_LOG(ERR, USER1, "Define path to the file with test"
903 				" vectors.\n");
904 		return -EINVAL;
905 	}
906 
907 	if (options->test == CPERF_TEST_TYPE_VERIFY &&
908 			options->total_ops > options->pool_sz) {
909 		RTE_LOG(ERR, USER1, "Total number of ops must be less than or"
910 				" equal to the pool size.\n");
911 		return -EINVAL;
912 	}
913 
914 	if (options->test == CPERF_TEST_TYPE_VERIFY &&
915 			(options->inc_buffer_size != 0 ||
916 			options->buffer_size_count > 1)) {
917 		RTE_LOG(ERR, USER1, "Only one buffer size is allowed when "
918 				"using the verify test.\n");
919 		return -EINVAL;
920 	}
921 
922 	if (options->test == CPERF_TEST_TYPE_VERIFY &&
923 			(options->inc_burst_size != 0 ||
924 			options->burst_size_count > 1)) {
925 		RTE_LOG(ERR, USER1, "Only one burst size is allowed when "
926 				"using the verify test.\n");
927 		return -EINVAL;
928 	}
929 
930 	if (options->op_type == CPERF_CIPHER_THEN_AUTH) {
931 		if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
932 				options->auth_op !=
933 				RTE_CRYPTO_AUTH_OP_GENERATE) {
934 			RTE_LOG(ERR, USER1, "Option cipher then auth must use"
935 					" options: encrypt and generate.\n");
936 			return -EINVAL;
937 		}
938 	} else if (options->op_type == CPERF_AUTH_THEN_CIPHER) {
939 		if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_DECRYPT &&
940 				options->auth_op !=
941 				RTE_CRYPTO_AUTH_OP_VERIFY) {
942 			RTE_LOG(ERR, USER1, "Option auth then cipher must use"
943 					" options: decrypt and verify.\n");
944 			return -EINVAL;
945 		}
946 	}
947 
948 	if (options->op_type == CPERF_CIPHER_ONLY ||
949 			options->op_type == CPERF_CIPHER_THEN_AUTH ||
950 			options->op_type == CPERF_AUTH_THEN_CIPHER) {
951 		if (check_cipher_buffer_length(options) < 0)
952 			return -EINVAL;
953 	}
954 
955 	return 0;
956 }
957 
958 void
959 cperf_options_dump(struct cperf_options *opts)
960 {
961 	uint8_t size_idx;
962 
963 	printf("# Crypto Performance Application Options:\n");
964 	printf("#\n");
965 	printf("# cperf test: %s\n", cperf_test_type_strs[opts->test]);
966 	printf("#\n");
967 	printf("# size of crypto op / mbuf pool: %u\n", opts->pool_sz);
968 	printf("# total number of ops: %u\n", opts->total_ops);
969 	if (opts->inc_buffer_size != 0) {
970 		printf("# buffer size:\n");
971 		printf("#\t min: %u\n", opts->min_buffer_size);
972 		printf("#\t max: %u\n", opts->max_buffer_size);
973 		printf("#\t inc: %u\n", opts->inc_buffer_size);
974 	} else {
975 		printf("# buffer sizes: ");
976 		for (size_idx = 0; size_idx < opts->buffer_size_count; size_idx++)
977 			printf("%u ", opts->buffer_size_list[size_idx]);
978 		printf("\n");
979 	}
980 	if (opts->inc_burst_size != 0) {
981 		printf("# burst size:\n");
982 		printf("#\t min: %u\n", opts->min_burst_size);
983 		printf("#\t max: %u\n", opts->max_burst_size);
984 		printf("#\t inc: %u\n", opts->inc_burst_size);
985 	} else {
986 		printf("# burst sizes: ");
987 		for (size_idx = 0; size_idx < opts->burst_size_count; size_idx++)
988 			printf("%u ", opts->burst_size_list[size_idx]);
989 		printf("\n");
990 	}
991 	printf("\n# segments per buffer: %u\n", opts->segments_nb);
992 	printf("#\n");
993 	printf("# cryptodev type: %s\n", opts->device_type);
994 	printf("#\n");
995 	printf("# crypto operation: %s\n", cperf_op_type_strs[opts->op_type]);
996 	printf("# sessionless: %s\n", opts->sessionless ? "yes" : "no");
997 	printf("# out of place: %s\n", opts->out_of_place ? "yes" : "no");
998 
999 	printf("#\n");
1000 
1001 	if (opts->op_type == CPERF_AUTH_ONLY ||
1002 			opts->op_type == CPERF_CIPHER_THEN_AUTH ||
1003 			opts->op_type == CPERF_AUTH_THEN_CIPHER) {
1004 		printf("# auth algorithm: %s\n",
1005 			rte_crypto_auth_algorithm_strings[opts->auth_algo]);
1006 		printf("# auth operation: %s\n",
1007 			rte_crypto_auth_operation_strings[opts->auth_op]);
1008 		printf("# auth key size: %u\n", opts->auth_key_sz);
1009 		printf("# auth iv size: %u\n", opts->auth_iv_sz);
1010 		printf("# auth digest size: %u\n", opts->digest_sz);
1011 		printf("#\n");
1012 	}
1013 
1014 	if (opts->op_type == CPERF_CIPHER_ONLY ||
1015 			opts->op_type == CPERF_CIPHER_THEN_AUTH ||
1016 			opts->op_type == CPERF_AUTH_THEN_CIPHER) {
1017 		printf("# cipher algorithm: %s\n",
1018 			rte_crypto_cipher_algorithm_strings[opts->cipher_algo]);
1019 		printf("# cipher operation: %s\n",
1020 			rte_crypto_cipher_operation_strings[opts->cipher_op]);
1021 		printf("# cipher key size: %u\n", opts->cipher_key_sz);
1022 		printf("# cipher iv size: %u\n", opts->cipher_iv_sz);
1023 		printf("#\n");
1024 	}
1025 
1026 	if (opts->op_type == CPERF_AEAD) {
1027 		printf("# aead algorithm: %s\n",
1028 			rte_crypto_aead_algorithm_strings[opts->aead_algo]);
1029 		printf("# aead operation: %s\n",
1030 			rte_crypto_aead_operation_strings[opts->aead_op]);
1031 		printf("# aead key size: %u\n", opts->aead_key_sz);
1032 		printf("# aead iv size: %u\n", opts->aead_iv_sz);
1033 		printf("# aead digest size: %u\n", opts->digest_sz);
1034 		printf("# aead aad size: %u\n", opts->aead_aad_sz);
1035 		printf("#\n");
1036 	}
1037 }
1038