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_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
132 char *copy_arg = strdup(arg);
134 if (copy_arg == NULL)
137 token = strtok(copy_arg, ":");
139 /* Parse minimum value */
141 number = strtoul(token, NULL, 10);
143 if (errno == EINVAL || errno == ERANGE ||
151 token = strtok(NULL, ":");
153 /* Parse increment value */
155 number = strtoul(token, NULL, 10);
157 if (errno == EINVAL || errno == ERANGE ||
165 token = strtok(NULL, ":");
167 /* Parse maximum value */
169 number = strtoul(token, NULL, 10);
171 if (errno == EINVAL || errno == ERANGE ||
180 if (strtok(NULL, ":") != NULL)
192 parse_list(const char *arg, uint32_t *list, uint32_t *min, uint32_t *max)
198 char *copy_arg = strdup(arg);
200 if (copy_arg == NULL)
203 token = strtok(copy_arg, ",");
205 /* Parse first value */
207 number = strtoul(token, NULL, 10);
209 if (errno == EINVAL || errno == ERANGE ||
213 list[count++] = number;
219 token = strtok(NULL, ",");
221 while (token != NULL) {
222 if (count == MAX_LIST) {
223 RTE_LOG(WARNING, USER1, "Using only the first %u sizes\n",
228 number = strtoul(token, NULL, 10);
230 if (errno == EINVAL || errno == ERANGE ||
234 list[count++] = number;
241 token = strtok(NULL, ",");
253 parse_total_ops(struct cperf_options *opts, const char *arg)
255 int ret = parse_uint32_t(&opts->total_ops, arg);
258 RTE_LOG(ERR, USER1, "failed to parse total operations count\n");
260 if (opts->total_ops == 0) {
262 "invalid total operations count number specified\n");
270 parse_pool_sz(struct cperf_options *opts, const char *arg)
272 int ret = parse_uint32_t(&opts->pool_sz, arg);
275 RTE_LOG(ERR, USER1, "failed to parse pool size");
280 parse_burst_sz(struct cperf_options *opts, const char *arg)
284 /* Try parsing the argument as a range, if it fails, parse it as a list */
285 if (parse_range(arg, &opts->min_burst_size, &opts->max_burst_size,
286 &opts->inc_burst_size) < 0) {
287 ret = parse_list(arg, opts->burst_size_list,
288 &opts->min_burst_size,
289 &opts->max_burst_size);
291 RTE_LOG(ERR, USER1, "failed to parse burst size/s\n");
294 opts->burst_size_count = ret;
301 parse_buffer_sz(struct cperf_options *opts, const char *arg)
305 /* Try parsing the argument as a range, if it fails, parse it as a list */
306 if (parse_range(arg, &opts->min_buffer_size, &opts->max_buffer_size,
307 &opts->inc_buffer_size) < 0) {
308 ret = parse_list(arg, opts->buffer_size_list,
309 &opts->min_buffer_size,
310 &opts->max_buffer_size);
312 RTE_LOG(ERR, USER1, "failed to parse burst size/s\n");
315 opts->buffer_size_count = ret;
322 parse_segments_nb(struct cperf_options *opts, const char *arg)
324 int ret = parse_uint32_t(&opts->segments_nb, arg);
327 RTE_LOG(ERR, USER1, "failed to parse segments number\n");
331 if ((opts->segments_nb == 0) || (opts->segments_nb > 255)) {
332 RTE_LOG(ERR, USER1, "invalid segments number specified\n");
340 parse_device_type(struct cperf_options *opts, const char *arg)
342 if (strlen(arg) > (sizeof(opts->device_type) - 1))
345 strncpy(opts->device_type, arg, sizeof(opts->device_type) - 1);
346 *(opts->device_type + sizeof(opts->device_type) - 1) = '\0';
352 parse_op_type(struct cperf_options *opts, const char *arg)
354 struct name_id_map optype_namemap[] = {
356 cperf_op_type_strs[CPERF_CIPHER_ONLY],
360 cperf_op_type_strs[CPERF_AUTH_ONLY],
364 cperf_op_type_strs[CPERF_CIPHER_THEN_AUTH],
365 CPERF_CIPHER_THEN_AUTH
368 cperf_op_type_strs[CPERF_AUTH_THEN_CIPHER],
369 CPERF_AUTH_THEN_CIPHER
372 cperf_op_type_strs[CPERF_AEAD],
377 int id = get_str_key_id_mapping(optype_namemap,
378 RTE_DIM(optype_namemap), arg);
380 RTE_LOG(ERR, USER1, "invalid opt type specified\n");
384 opts->op_type = (enum cperf_op_type)id;
390 parse_sessionless(struct cperf_options *opts,
391 const char *arg __rte_unused)
393 opts->sessionless = 1;
398 parse_out_of_place(struct cperf_options *opts,
399 const char *arg __rte_unused)
401 opts->out_of_place = 1;
406 parse_test_file(struct cperf_options *opts,
409 opts->test_file = strdup(arg);
410 if (access(opts->test_file, F_OK) != -1)
412 RTE_LOG(ERR, USER1, "Test vector file doesn't exist\n");
418 parse_test_name(struct cperf_options *opts,
421 char *test_name = (char *) rte_zmalloc(NULL,
422 sizeof(char) * (strlen(arg) + 3), 0);
423 snprintf(test_name, strlen(arg) + 3, "[%s]", arg);
424 opts->test_name = test_name;
430 parse_silent(struct cperf_options *opts,
431 const char *arg __rte_unused)
439 parse_cipher_algo(struct cperf_options *opts, const char *arg)
442 enum rte_crypto_cipher_algorithm cipher_algo;
444 if (rte_cryptodev_get_cipher_algo_enum(&cipher_algo, arg) < 0) {
445 RTE_LOG(ERR, USER1, "Invalid cipher algorithm specified\n");
449 opts->cipher_algo = cipher_algo;
455 parse_cipher_op(struct cperf_options *opts, const char *arg)
457 struct name_id_map cipher_op_namemap[] = {
459 rte_crypto_cipher_operation_strings
460 [RTE_CRYPTO_CIPHER_OP_ENCRYPT],
461 RTE_CRYPTO_CIPHER_OP_ENCRYPT },
463 rte_crypto_cipher_operation_strings
464 [RTE_CRYPTO_CIPHER_OP_DECRYPT],
465 RTE_CRYPTO_CIPHER_OP_DECRYPT
469 int id = get_str_key_id_mapping(cipher_op_namemap,
470 RTE_DIM(cipher_op_namemap), arg);
472 RTE_LOG(ERR, USER1, "Invalid cipher operation specified\n");
476 opts->cipher_op = (enum rte_crypto_cipher_operation)id;
482 parse_cipher_key_sz(struct cperf_options *opts, const char *arg)
484 return parse_uint16_t(&opts->cipher_key_sz, arg);
488 parse_cipher_iv_sz(struct cperf_options *opts, const char *arg)
490 return parse_uint16_t(&opts->cipher_iv_sz, arg);
494 parse_auth_algo(struct cperf_options *opts, const char *arg)
496 enum rte_crypto_auth_algorithm auth_algo;
498 if (rte_cryptodev_get_auth_algo_enum(&auth_algo, arg) < 0) {
499 RTE_LOG(ERR, USER1, "Invalid authentication algorithm specified\n");
503 opts->auth_algo = auth_algo;
509 parse_auth_op(struct cperf_options *opts, const char *arg)
511 struct name_id_map auth_op_namemap[] = {
513 rte_crypto_auth_operation_strings
514 [RTE_CRYPTO_AUTH_OP_GENERATE],
515 RTE_CRYPTO_AUTH_OP_GENERATE },
517 rte_crypto_auth_operation_strings
518 [RTE_CRYPTO_AUTH_OP_VERIFY],
519 RTE_CRYPTO_AUTH_OP_VERIFY
523 int id = get_str_key_id_mapping(auth_op_namemap,
524 RTE_DIM(auth_op_namemap), arg);
526 RTE_LOG(ERR, USER1, "invalid authentication operation specified"
531 opts->auth_op = (enum rte_crypto_auth_operation)id;
537 parse_auth_key_sz(struct cperf_options *opts, const char *arg)
539 return parse_uint16_t(&opts->auth_key_sz, arg);
543 parse_auth_digest_sz(struct cperf_options *opts, const char *arg)
545 return parse_uint16_t(&opts->auth_digest_sz, arg);
549 parse_auth_aad_sz(struct cperf_options *opts, const char *arg)
551 return parse_uint16_t(&opts->auth_aad_sz, arg);
555 parse_csv_friendly(struct cperf_options *opts, const char *arg __rte_unused)
562 typedef int (*option_parser_t)(struct cperf_options *opts,
565 struct long_opt_parser {
566 const char *lgopt_name;
567 option_parser_t parser_fn;
571 static struct option lgopts[] = {
573 { CPERF_PTEST_TYPE, required_argument, 0, 0 },
575 { CPERF_POOL_SIZE, required_argument, 0, 0 },
576 { CPERF_TOTAL_OPS, required_argument, 0, 0 },
577 { CPERF_BURST_SIZE, required_argument, 0, 0 },
578 { CPERF_BUFFER_SIZE, required_argument, 0, 0 },
579 { CPERF_SEGMENTS_NB, required_argument, 0, 0 },
581 { CPERF_DEVTYPE, required_argument, 0, 0 },
582 { CPERF_OPTYPE, required_argument, 0, 0 },
584 { CPERF_SILENT, no_argument, 0, 0 },
585 { CPERF_SESSIONLESS, no_argument, 0, 0 },
586 { CPERF_OUT_OF_PLACE, no_argument, 0, 0 },
587 { CPERF_TEST_FILE, required_argument, 0, 0 },
588 { CPERF_TEST_NAME, required_argument, 0, 0 },
590 { CPERF_CIPHER_ALGO, required_argument, 0, 0 },
591 { CPERF_CIPHER_OP, required_argument, 0, 0 },
593 { CPERF_CIPHER_KEY_SZ, required_argument, 0, 0 },
594 { CPERF_CIPHER_IV_SZ, required_argument, 0, 0 },
596 { CPERF_AUTH_ALGO, required_argument, 0, 0 },
597 { CPERF_AUTH_OP, required_argument, 0, 0 },
599 { CPERF_AUTH_KEY_SZ, required_argument, 0, 0 },
600 { CPERF_AUTH_DIGEST_SZ, required_argument, 0, 0 },
601 { CPERF_AUTH_AAD_SZ, required_argument, 0, 0 },
602 { CPERF_CSV, no_argument, 0, 0},
608 cperf_options_default(struct cperf_options *opts)
610 opts->test = CPERF_TEST_TYPE_THROUGHPUT;
612 opts->pool_sz = 8192;
613 opts->total_ops = 10000000;
615 opts->buffer_size_list[0] = 64;
616 opts->buffer_size_count = 1;
617 opts->max_buffer_size = 64;
618 opts->min_buffer_size = 64;
619 opts->inc_buffer_size = 0;
621 opts->burst_size_list[0] = 32;
622 opts->burst_size_count = 1;
623 opts->max_burst_size = 32;
624 opts->min_burst_size = 32;
625 opts->inc_burst_size = 0;
627 opts->segments_nb = 1;
629 strncpy(opts->device_type, "crypto_aesni_mb",
630 sizeof(opts->device_type));
632 opts->op_type = CPERF_CIPHER_THEN_AUTH;
635 opts->test_file = NULL;
636 opts->test_name = NULL;
637 opts->sessionless = 0;
638 opts->out_of_place = 0;
641 opts->cipher_algo = RTE_CRYPTO_CIPHER_AES_CBC;
642 opts->cipher_op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
643 opts->cipher_key_sz = 16;
644 opts->cipher_iv_sz = 16;
646 opts->auth_algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
647 opts->auth_op = RTE_CRYPTO_AUTH_OP_GENERATE;
649 opts->auth_key_sz = 64;
650 opts->auth_digest_sz = 12;
651 opts->auth_aad_sz = 0;
655 cperf_opts_parse_long(int opt_idx, struct cperf_options *opts)
657 struct long_opt_parser parsermap[] = {
658 { CPERF_PTEST_TYPE, parse_cperf_test_type },
659 { CPERF_SILENT, parse_silent },
660 { CPERF_POOL_SIZE, parse_pool_sz },
661 { CPERF_TOTAL_OPS, parse_total_ops },
662 { CPERF_BURST_SIZE, parse_burst_sz },
663 { CPERF_BUFFER_SIZE, parse_buffer_sz },
664 { CPERF_SEGMENTS_NB, parse_segments_nb },
665 { CPERF_DEVTYPE, parse_device_type },
666 { CPERF_OPTYPE, parse_op_type },
667 { CPERF_SESSIONLESS, parse_sessionless },
668 { CPERF_OUT_OF_PLACE, parse_out_of_place },
669 { CPERF_TEST_FILE, parse_test_file },
670 { CPERF_TEST_NAME, parse_test_name },
671 { CPERF_CIPHER_ALGO, parse_cipher_algo },
672 { CPERF_CIPHER_OP, parse_cipher_op },
673 { CPERF_CIPHER_KEY_SZ, parse_cipher_key_sz },
674 { CPERF_CIPHER_IV_SZ, parse_cipher_iv_sz },
675 { CPERF_AUTH_ALGO, parse_auth_algo },
676 { CPERF_AUTH_OP, parse_auth_op },
677 { CPERF_AUTH_KEY_SZ, parse_auth_key_sz },
678 { CPERF_AUTH_DIGEST_SZ, parse_auth_digest_sz },
679 { CPERF_AUTH_AAD_SZ, parse_auth_aad_sz },
680 { CPERF_CSV, parse_csv_friendly},
684 for (i = 0; i < RTE_DIM(parsermap); i++) {
685 if (strncmp(lgopts[opt_idx].name, parsermap[i].lgopt_name,
686 strlen(lgopts[opt_idx].name)) == 0)
687 return parsermap[i].parser_fn(opts, optarg);
694 cperf_options_parse(struct cperf_options *options, int argc, char **argv)
696 int opt, retval, opt_idx;
698 while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
703 retval = cperf_opts_parse_long(opt_idx, options);
718 cperf_options_check(struct cperf_options *options)
720 if (options->segments_nb > options->min_buffer_size) {
722 "Segments number greater than buffer size.\n");
726 if (options->test == CPERF_TEST_TYPE_VERIFY &&
727 options->test_file == NULL) {
728 RTE_LOG(ERR, USER1, "Define path to the file with test"
733 if (options->test_name != NULL && options->test_file == NULL) {
734 RTE_LOG(ERR, USER1, "Define path to the file with test"
739 if (options->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY &&
740 options->test_file == NULL) {
741 RTE_LOG(ERR, USER1, "Define path to the file with test"
746 if (options->test == CPERF_TEST_TYPE_VERIFY &&
747 options->total_ops > options->pool_sz) {
748 RTE_LOG(ERR, USER1, "Total number of ops must be less than or"
749 " equal to the pool size.\n");
753 if (options->test == CPERF_TEST_TYPE_VERIFY &&
754 (options->inc_buffer_size != 0 ||
755 options->buffer_size_count > 1)) {
756 RTE_LOG(ERR, USER1, "Only one buffer size is allowed when "
757 "using the verify test.\n");
761 if (options->test == CPERF_TEST_TYPE_VERIFY &&
762 (options->inc_burst_size != 0 ||
763 options->burst_size_count > 1)) {
764 RTE_LOG(ERR, USER1, "Only one burst size is allowed when "
765 "using the verify test.\n");
769 if (options->op_type == CPERF_CIPHER_THEN_AUTH) {
770 if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
772 RTE_CRYPTO_AUTH_OP_GENERATE) {
773 RTE_LOG(ERR, USER1, "Option cipher then auth must use"
774 " options: encrypt and generate.\n");
777 } else if (options->op_type == CPERF_AUTH_THEN_CIPHER) {
778 if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_DECRYPT &&
780 RTE_CRYPTO_AUTH_OP_VERIFY) {
781 RTE_LOG(ERR, USER1, "Option auth then cipher must use"
782 " options: decrypt and verify.\n");
785 } else if (options->op_type == CPERF_AEAD) {
786 if (!(options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
788 RTE_CRYPTO_AUTH_OP_GENERATE) &&
789 !(options->cipher_op ==
790 RTE_CRYPTO_CIPHER_OP_DECRYPT &&
792 RTE_CRYPTO_AUTH_OP_VERIFY)) {
793 RTE_LOG(ERR, USER1, "Use together options: encrypt and"
794 " generate or decrypt and verify.\n");
799 if (options->cipher_algo == RTE_CRYPTO_CIPHER_AES_GCM ||
800 options->cipher_algo == RTE_CRYPTO_CIPHER_AES_CCM ||
801 options->auth_algo == RTE_CRYPTO_AUTH_AES_GCM ||
802 options->auth_algo == RTE_CRYPTO_AUTH_AES_CCM ||
803 options->auth_algo == RTE_CRYPTO_AUTH_AES_GMAC) {
804 if (options->op_type != CPERF_AEAD) {
805 RTE_LOG(ERR, USER1, "Use --optype aead\n");
814 cperf_options_dump(struct cperf_options *opts)
818 printf("# Crypto Performance Application Options:\n");
820 printf("# cperf test: %s\n", cperf_test_type_strs[opts->test]);
822 printf("# size of crypto op / mbuf pool: %u\n", opts->pool_sz);
823 printf("# total number of ops: %u\n", opts->total_ops);
824 if (opts->inc_buffer_size != 0) {
825 printf("# buffer size:\n");
826 printf("#\t min: %u\n", opts->min_buffer_size);
827 printf("#\t max: %u\n", opts->max_buffer_size);
828 printf("#\t inc: %u\n", opts->inc_buffer_size);
830 printf("# buffer sizes: ");
831 for (size_idx = 0; size_idx < opts->buffer_size_count; size_idx++)
832 printf("%u ", opts->buffer_size_list[size_idx]);
835 if (opts->inc_burst_size != 0) {
836 printf("# burst size:\n");
837 printf("#\t min: %u\n", opts->min_burst_size);
838 printf("#\t max: %u\n", opts->max_burst_size);
839 printf("#\t inc: %u\n", opts->inc_burst_size);
841 printf("# burst sizes: ");
842 for (size_idx = 0; size_idx < opts->burst_size_count; size_idx++)
843 printf("%u ", opts->burst_size_list[size_idx]);
846 printf("\n# segments per buffer: %u\n", opts->segments_nb);
848 printf("# cryptodev type: %s\n", opts->device_type);
850 printf("# crypto operation: %s\n", cperf_op_type_strs[opts->op_type]);
851 printf("# sessionless: %s\n", opts->sessionless ? "yes" : "no");
852 printf("# out of place: %s\n", opts->out_of_place ? "yes" : "no");
856 if (opts->op_type == CPERF_AUTH_ONLY ||
857 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
858 opts->op_type == CPERF_AUTH_THEN_CIPHER ||
859 opts->op_type == CPERF_AEAD) {
860 printf("# auth algorithm: %s\n",
861 rte_crypto_auth_algorithm_strings[opts->auth_algo]);
862 printf("# auth operation: %s\n",
863 rte_crypto_auth_operation_strings[opts->auth_op]);
864 printf("# auth key size: %u\n", opts->auth_key_sz);
865 printf("# auth digest size: %u\n", opts->auth_digest_sz);
866 printf("# auth aad size: %u\n", opts->auth_aad_sz);
870 if (opts->op_type == CPERF_CIPHER_ONLY ||
871 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
872 opts->op_type == CPERF_AUTH_THEN_CIPHER ||
873 opts->op_type == CPERF_AEAD) {
874 printf("# cipher algorithm: %s\n",
875 rte_crypto_cipher_algorithm_strings[opts->cipher_algo]);
876 printf("# cipher operation: %s\n",
877 rte_crypto_cipher_operation_strings[opts->cipher_op]);
878 printf("# cipher key size: %u\n", opts->cipher_key_sz);
879 printf("# cipher iv size: %u\n", opts->cipher_iv_sz);