4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <rte_cryptodev.h>
37 #include <rte_malloc.h>
39 #include "cperf_options.h"
47 get_str_key_id_mapping(struct name_id_map *map, unsigned int map_len,
52 for (i = 0; i < map_len; i++) {
54 if (strcmp(str_key, map[i].name) == 0)
62 parse_cperf_test_type(struct cperf_options *opts, const char *arg)
64 struct name_id_map cperftest_namemap[] = {
66 cperf_test_type_strs[CPERF_TEST_TYPE_THROUGHPUT],
67 CPERF_TEST_TYPE_THROUGHPUT
70 cperf_test_type_strs[CPERF_TEST_TYPE_VERIFY],
71 CPERF_TEST_TYPE_VERIFY
74 cperf_test_type_strs[CPERF_TEST_TYPE_LATENCY],
75 CPERF_TEST_TYPE_LATENCY
79 int id = get_str_key_id_mapping(
80 (struct name_id_map *)cperftest_namemap,
81 RTE_DIM(cperftest_namemap), arg);
83 RTE_LOG(ERR, USER1, "failed to parse test type");
87 opts->test = (enum cperf_perf_test_type)id;
93 parse_uint32_t(uint32_t *value, const char *arg)
96 unsigned long n = strtoul(arg, &end, 10);
98 if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
104 *value = (uint32_t) n;
110 parse_uint16_t(uint16_t *value, const char *arg)
113 int ret = parse_uint32_t(&val, arg);
118 if (val > UINT16_MAX)
121 *value = (uint16_t) val;
127 parse_total_ops(struct cperf_options *opts, const char *arg)
129 int ret = parse_uint32_t(&opts->total_ops, arg);
132 RTE_LOG(ERR, USER1, "failed to parse total operations count\n");
134 if (opts->total_ops == 0) {
136 "invalid total operations count number specified\n");
144 parse_pool_sz(struct cperf_options *opts, const char *arg)
146 int ret = parse_uint32_t(&opts->pool_sz, arg);
149 RTE_LOG(ERR, USER1, "failed to parse pool size");
154 parse_burst_sz(struct cperf_options *opts, const char *arg)
156 int ret = parse_uint32_t(&opts->burst_sz, arg);
159 RTE_LOG(ERR, USER1, "failed to parse burst size");
164 parse_buffer_sz(struct cperf_options *opts, const char *arg)
166 uint32_t i, valid_buf_sz[] = {
167 32, 64, 128, 256, 384, 512, 768, 1024, 1280, 1536, 1792,
171 if (parse_uint32_t(&opts->buffer_sz, arg)) {
172 RTE_LOG(ERR, USER1, "failed to parse buffer size");
176 for (i = 0; i < RTE_DIM(valid_buf_sz); i++)
177 if (valid_buf_sz[i] == opts->buffer_sz)
180 RTE_LOG(ERR, USER1, "invalid buffer size specified");
185 parse_segments_nb(struct cperf_options *opts, const char *arg)
187 int ret = parse_uint32_t(&opts->segments_nb, arg);
190 RTE_LOG(ERR, USER1, "failed to parse segments number\n");
194 if ((opts->segments_nb == 0) || (opts->segments_nb > 255)) {
195 RTE_LOG(ERR, USER1, "invalid segments number specified\n");
203 parse_device_type(struct cperf_options *opts, const char *arg)
205 if (strlen(arg) > (sizeof(opts->device_type) - 1))
208 strncpy(opts->device_type, arg, sizeof(opts->device_type) - 1);
209 *(opts->device_type + sizeof(opts->device_type) - 1) = '\0';
215 parse_op_type(struct cperf_options *opts, const char *arg)
217 struct name_id_map optype_namemap[] = {
219 cperf_op_type_strs[CPERF_CIPHER_ONLY],
223 cperf_op_type_strs[CPERF_AUTH_ONLY],
227 cperf_op_type_strs[CPERF_CIPHER_THEN_AUTH],
228 CPERF_CIPHER_THEN_AUTH
231 cperf_op_type_strs[CPERF_AUTH_THEN_CIPHER],
232 CPERF_AUTH_THEN_CIPHER
235 cperf_op_type_strs[CPERF_AEAD],
240 int id = get_str_key_id_mapping(optype_namemap,
241 RTE_DIM(optype_namemap), arg);
243 RTE_LOG(ERR, USER1, "invalid opt type specified\n");
247 opts->op_type = (enum cperf_op_type)id;
253 parse_sessionless(struct cperf_options *opts,
254 const char *arg __rte_unused)
256 opts->sessionless = 1;
261 parse_out_of_place(struct cperf_options *opts,
262 const char *arg __rte_unused)
264 opts->out_of_place = 1;
269 parse_test_file(struct cperf_options *opts,
272 opts->test_file = strdup(arg);
273 if (access(opts->test_file, F_OK) != -1)
275 RTE_LOG(ERR, USER1, "Test vector file doesn't exist\n");
281 parse_test_name(struct cperf_options *opts,
284 char *test_name = (char *) rte_zmalloc(NULL,
285 sizeof(char) * (strlen(arg) + 3), 0);
286 snprintf(test_name, strlen(arg) + 3, "[%s]", arg);
287 opts->test_name = test_name;
293 parse_silent(struct cperf_options *opts,
294 const char *arg __rte_unused)
302 parse_cipher_algo(struct cperf_options *opts, const char *arg)
305 enum rte_crypto_cipher_algorithm cipher_algo;
307 if (rte_cryptodev_get_cipher_algo_enum(&cipher_algo, arg) < 0) {
308 RTE_LOG(ERR, USER1, "Invalid cipher algorithm specified\n");
312 opts->cipher_algo = cipher_algo;
318 parse_cipher_op(struct cperf_options *opts, const char *arg)
320 struct name_id_map cipher_op_namemap[] = {
322 rte_crypto_cipher_operation_strings
323 [RTE_CRYPTO_CIPHER_OP_ENCRYPT],
324 RTE_CRYPTO_CIPHER_OP_ENCRYPT },
326 rte_crypto_cipher_operation_strings
327 [RTE_CRYPTO_CIPHER_OP_DECRYPT],
328 RTE_CRYPTO_CIPHER_OP_DECRYPT
332 int id = get_str_key_id_mapping(cipher_op_namemap,
333 RTE_DIM(cipher_op_namemap), arg);
335 RTE_LOG(ERR, USER1, "Invalid cipher operation specified\n");
339 opts->cipher_op = (enum rte_crypto_cipher_operation)id;
345 parse_cipher_key_sz(struct cperf_options *opts, const char *arg)
347 return parse_uint16_t(&opts->cipher_key_sz, arg);
351 parse_cipher_iv_sz(struct cperf_options *opts, const char *arg)
353 return parse_uint16_t(&opts->cipher_iv_sz, arg);
357 parse_auth_algo(struct cperf_options *opts, const char *arg)
359 enum rte_crypto_auth_algorithm auth_algo;
361 if (rte_cryptodev_get_auth_algo_enum(&auth_algo, arg) < 0) {
362 RTE_LOG(ERR, USER1, "Invalid authentication algorithm specified\n");
366 opts->auth_algo = auth_algo;
372 parse_auth_op(struct cperf_options *opts, const char *arg)
374 struct name_id_map auth_op_namemap[] = {
376 rte_crypto_auth_operation_strings
377 [RTE_CRYPTO_AUTH_OP_GENERATE],
378 RTE_CRYPTO_AUTH_OP_GENERATE },
380 rte_crypto_auth_operation_strings
381 [RTE_CRYPTO_AUTH_OP_VERIFY],
382 RTE_CRYPTO_AUTH_OP_VERIFY
386 int id = get_str_key_id_mapping(auth_op_namemap,
387 RTE_DIM(auth_op_namemap), arg);
389 RTE_LOG(ERR, USER1, "invalid authentication operation specified"
394 opts->auth_op = (enum rte_crypto_auth_operation)id;
400 parse_auth_key_sz(struct cperf_options *opts, const char *arg)
402 return parse_uint16_t(&opts->auth_key_sz, arg);
406 parse_auth_digest_sz(struct cperf_options *opts, const char *arg)
408 return parse_uint16_t(&opts->auth_digest_sz, arg);
412 parse_auth_aad_sz(struct cperf_options *opts, const char *arg)
414 return parse_uint16_t(&opts->auth_aad_sz, arg);
418 parse_csv_friendly(struct cperf_options *opts, const char *arg __rte_unused)
425 typedef int (*option_parser_t)(struct cperf_options *opts,
428 struct long_opt_parser {
429 const char *lgopt_name;
430 option_parser_t parser_fn;
434 static struct option lgopts[] = {
436 { CPERF_PTEST_TYPE, required_argument, 0, 0 },
438 { CPERF_POOL_SIZE, required_argument, 0, 0 },
439 { CPERF_TOTAL_OPS, required_argument, 0, 0 },
440 { CPERF_BURST_SIZE, required_argument, 0, 0 },
441 { CPERF_BUFFER_SIZE, required_argument, 0, 0 },
442 { CPERF_SEGMENTS_NB, required_argument, 0, 0 },
444 { CPERF_DEVTYPE, required_argument, 0, 0 },
445 { CPERF_OPTYPE, required_argument, 0, 0 },
447 { CPERF_SILENT, no_argument, 0, 0 },
448 { CPERF_SESSIONLESS, no_argument, 0, 0 },
449 { CPERF_OUT_OF_PLACE, no_argument, 0, 0 },
450 { CPERF_TEST_FILE, required_argument, 0, 0 },
451 { CPERF_TEST_NAME, required_argument, 0, 0 },
453 { CPERF_CIPHER_ALGO, required_argument, 0, 0 },
454 { CPERF_CIPHER_OP, required_argument, 0, 0 },
456 { CPERF_CIPHER_KEY_SZ, required_argument, 0, 0 },
457 { CPERF_CIPHER_IV_SZ, required_argument, 0, 0 },
459 { CPERF_AUTH_ALGO, required_argument, 0, 0 },
460 { CPERF_AUTH_OP, required_argument, 0, 0 },
462 { CPERF_AUTH_KEY_SZ, required_argument, 0, 0 },
463 { CPERF_AUTH_DIGEST_SZ, required_argument, 0, 0 },
464 { CPERF_AUTH_AAD_SZ, required_argument, 0, 0 },
465 { CPERF_CSV, no_argument, 0, 0},
471 cperf_options_default(struct cperf_options *opts)
473 opts->test = CPERF_TEST_TYPE_THROUGHPUT;
475 opts->pool_sz = 8192;
476 opts->total_ops = 10000000;
478 opts->buffer_sz = 64;
479 opts->segments_nb = 1;
481 strncpy(opts->device_type, "crypto_aesni_mb",
482 sizeof(opts->device_type));
484 opts->op_type = CPERF_CIPHER_THEN_AUTH;
487 opts->test_file = NULL;
488 opts->test_name = NULL;
489 opts->sessionless = 0;
490 opts->out_of_place = 0;
493 opts->cipher_algo = RTE_CRYPTO_CIPHER_AES_CBC;
494 opts->cipher_op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
495 opts->cipher_key_sz = 16;
496 opts->cipher_iv_sz = 16;
498 opts->auth_algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
499 opts->auth_op = RTE_CRYPTO_AUTH_OP_GENERATE;
501 opts->auth_key_sz = 64;
502 opts->auth_digest_sz = 12;
503 opts->auth_aad_sz = 0;
507 cperf_opts_parse_long(int opt_idx, struct cperf_options *opts)
509 struct long_opt_parser parsermap[] = {
510 { CPERF_PTEST_TYPE, parse_cperf_test_type },
511 { CPERF_SILENT, parse_silent },
512 { CPERF_POOL_SIZE, parse_pool_sz },
513 { CPERF_TOTAL_OPS, parse_total_ops },
514 { CPERF_BURST_SIZE, parse_burst_sz },
515 { CPERF_BUFFER_SIZE, parse_buffer_sz },
516 { CPERF_SEGMENTS_NB, parse_segments_nb },
517 { CPERF_DEVTYPE, parse_device_type },
518 { CPERF_OPTYPE, parse_op_type },
519 { CPERF_SESSIONLESS, parse_sessionless },
520 { CPERF_OUT_OF_PLACE, parse_out_of_place },
521 { CPERF_TEST_FILE, parse_test_file },
522 { CPERF_TEST_NAME, parse_test_name },
523 { CPERF_CIPHER_ALGO, parse_cipher_algo },
524 { CPERF_CIPHER_OP, parse_cipher_op },
525 { CPERF_CIPHER_KEY_SZ, parse_cipher_key_sz },
526 { CPERF_CIPHER_IV_SZ, parse_cipher_iv_sz },
527 { CPERF_AUTH_ALGO, parse_auth_algo },
528 { CPERF_AUTH_OP, parse_auth_op },
529 { CPERF_AUTH_KEY_SZ, parse_auth_key_sz },
530 { CPERF_AUTH_DIGEST_SZ, parse_auth_digest_sz },
531 { CPERF_AUTH_AAD_SZ, parse_auth_aad_sz },
532 { CPERF_CSV, parse_csv_friendly},
536 for (i = 0; i < RTE_DIM(parsermap); i++) {
537 if (strncmp(lgopts[opt_idx].name, parsermap[i].lgopt_name,
538 strlen(lgopts[opt_idx].name)) == 0)
539 return parsermap[i].parser_fn(opts, optarg);
546 cperf_options_parse(struct cperf_options *options, int argc, char **argv)
548 int opt, retval, opt_idx;
550 while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
555 retval = cperf_opts_parse_long(opt_idx, options);
570 cperf_options_check(struct cperf_options *options)
572 if (options->segments_nb > options->buffer_sz) {
574 "Segments number greater than buffer size.\n");
578 if (options->test == CPERF_TEST_TYPE_VERIFY &&
579 options->test_file == NULL) {
580 RTE_LOG(ERR, USER1, "Define path to the file with test"
585 if (options->test_name != NULL && options->test_file == NULL) {
586 RTE_LOG(ERR, USER1, "Define path to the file with test"
591 if (options->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY &&
592 options->test_file == NULL) {
593 RTE_LOG(ERR, USER1, "Define path to the file with test"
598 if (options->test == CPERF_TEST_TYPE_VERIFY &&
599 options->total_ops > options->pool_sz) {
600 RTE_LOG(ERR, USER1, "Total number of ops must be less than or"
601 " equal to the pool size.\n");
605 if (options->op_type == CPERF_CIPHER_THEN_AUTH) {
606 if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
608 RTE_CRYPTO_AUTH_OP_GENERATE) {
609 RTE_LOG(ERR, USER1, "Option cipher then auth must use"
610 " options: encrypt and generate.\n");
613 } else if (options->op_type == CPERF_AUTH_THEN_CIPHER) {
614 if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_DECRYPT &&
616 RTE_CRYPTO_AUTH_OP_VERIFY) {
617 RTE_LOG(ERR, USER1, "Option auth then cipher must use"
618 " options: decrypt and verify.\n");
621 } else if (options->op_type == CPERF_AEAD) {
622 if (!(options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
624 RTE_CRYPTO_AUTH_OP_GENERATE) &&
625 !(options->cipher_op ==
626 RTE_CRYPTO_CIPHER_OP_DECRYPT &&
628 RTE_CRYPTO_AUTH_OP_VERIFY)) {
629 RTE_LOG(ERR, USER1, "Use together options: encrypt and"
630 " generate or decrypt and verify.\n");
635 if (options->cipher_algo == RTE_CRYPTO_CIPHER_AES_GCM ||
636 options->cipher_algo == RTE_CRYPTO_CIPHER_AES_CCM ||
637 options->auth_algo == RTE_CRYPTO_AUTH_AES_GCM ||
638 options->auth_algo == RTE_CRYPTO_AUTH_AES_CCM ||
639 options->auth_algo == RTE_CRYPTO_AUTH_AES_GMAC) {
640 if (options->op_type != CPERF_AEAD) {
641 RTE_LOG(ERR, USER1, "Use --optype aead\n");
650 cperf_options_dump(struct cperf_options *opts)
652 printf("# Crypto Performance Application Options:\n");
654 printf("# cperf test: %s\n", cperf_test_type_strs[opts->test]);
656 printf("# size of crypto op / mbuf pool: %u\n", opts->pool_sz);
657 printf("# total number of ops: %u\n", opts->total_ops);
658 printf("# burst size: %u\n", opts->burst_sz);
659 printf("# buffer size: %u\n", opts->buffer_sz);
660 printf("# segments per buffer: %u\n", opts->segments_nb);
662 printf("# cryptodev type: %s\n", opts->device_type);
664 printf("# crypto operation: %s\n", cperf_op_type_strs[opts->op_type]);
665 printf("# sessionless: %s\n", opts->sessionless ? "yes" : "no");
666 printf("# out of place: %s\n", opts->out_of_place ? "yes" : "no");
670 if (opts->op_type == CPERF_AUTH_ONLY ||
671 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
672 opts->op_type == CPERF_AUTH_THEN_CIPHER ||
673 opts->op_type == CPERF_AEAD) {
674 printf("# auth algorithm: %s\n",
675 rte_crypto_auth_algorithm_strings[opts->auth_algo]);
676 printf("# auth operation: %s\n",
677 rte_crypto_auth_operation_strings[opts->auth_op]);
678 printf("# auth key size: %u\n", opts->auth_key_sz);
679 printf("# auth digest size: %u\n", opts->auth_digest_sz);
680 printf("# auth aad size: %u\n", opts->auth_aad_sz);
684 if (opts->op_type == CPERF_CIPHER_ONLY ||
685 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
686 opts->op_type == CPERF_AUTH_THEN_CIPHER ||
687 opts->op_type == CPERF_AEAD) {
688 printf("# cipher algorithm: %s\n",
689 rte_crypto_cipher_algorithm_strings[opts->cipher_algo]);
690 printf("# cipher operation: %s\n",
691 rte_crypto_cipher_operation_strings[opts->cipher_op]);
692 printf("# cipher key size: %u\n", opts->cipher_key_sz);
693 printf("# cipher iv size: %u\n", opts->cipher_iv_sz);