1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
8 #include <rte_malloc.h>
9 #include <rte_random.h>
11 #include <rte_cryptodev.h>
12 #ifdef RTE_CRYPTO_SCHEDULER
13 #include <rte_cryptodev_scheduler.h>
17 #include "cperf_options.h"
18 #include "cperf_test_vector_parsing.h"
19 #include "cperf_test_throughput.h"
20 #include "cperf_test_latency.h"
21 #include "cperf_test_verify.h"
22 #include "cperf_test_pmd_cyclecount.h"
25 struct rte_mempool *sess_mp;
26 struct rte_mempool *priv_mp;
27 } session_pool_socket[RTE_MAX_NUMA_NODES];
29 const char *cperf_test_type_strs[] = {
30 [CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
31 [CPERF_TEST_TYPE_LATENCY] = "latency",
32 [CPERF_TEST_TYPE_VERIFY] = "verify",
33 [CPERF_TEST_TYPE_PMDCC] = "pmd-cyclecount"
36 const char *cperf_op_type_strs[] = {
37 [CPERF_CIPHER_ONLY] = "cipher-only",
38 [CPERF_AUTH_ONLY] = "auth-only",
39 [CPERF_CIPHER_THEN_AUTH] = "cipher-then-auth",
40 [CPERF_AUTH_THEN_CIPHER] = "auth-then-cipher",
41 [CPERF_AEAD] = "aead",
42 [CPERF_PDCP] = "pdcp",
43 [CPERF_DOCSIS] = "docsis",
44 [CPERF_IPSEC] = "ipsec",
45 [CPERF_ASYM_MODEX] = "modex"
48 const struct cperf_test cperf_testmap[] = {
49 [CPERF_TEST_TYPE_THROUGHPUT] = {
50 cperf_throughput_test_constructor,
51 cperf_throughput_test_runner,
52 cperf_throughput_test_destructor
54 [CPERF_TEST_TYPE_LATENCY] = {
55 cperf_latency_test_constructor,
56 cperf_latency_test_runner,
57 cperf_latency_test_destructor
59 [CPERF_TEST_TYPE_VERIFY] = {
60 cperf_verify_test_constructor,
61 cperf_verify_test_runner,
62 cperf_verify_test_destructor
64 [CPERF_TEST_TYPE_PMDCC] = {
65 cperf_pmd_cyclecount_test_constructor,
66 cperf_pmd_cyclecount_test_runner,
67 cperf_pmd_cyclecount_test_destructor
72 create_asym_op_pool_socket(int32_t socket_id, uint32_t nb_sessions)
74 char mp_name[RTE_MEMPOOL_NAMESIZE];
75 struct rte_mempool *mpool = NULL;
77 if (session_pool_socket[socket_id].sess_mp == NULL) {
78 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "perf_asym_sess_pool%u",
80 mpool = rte_cryptodev_asym_session_pool_create(mp_name,
81 nb_sessions, 0, 0, socket_id);
83 printf("Cannot create pool \"%s\" on socket %d\n",
87 session_pool_socket[socket_id].sess_mp = mpool;
93 fill_session_pool_socket(int32_t socket_id, uint32_t session_priv_size,
96 char mp_name[RTE_MEMPOOL_NAMESIZE];
97 struct rte_mempool *sess_mp;
99 if (session_pool_socket[socket_id].priv_mp == NULL) {
100 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
101 "priv_sess_mp_%u", socket_id);
103 sess_mp = rte_mempool_create(mp_name,
106 0, 0, NULL, NULL, NULL,
110 if (sess_mp == NULL) {
111 printf("Cannot create pool \"%s\" on socket %d\n",
116 printf("Allocated pool \"%s\" on socket %d\n",
118 session_pool_socket[socket_id].priv_mp = sess_mp;
121 if (session_pool_socket[socket_id].sess_mp == NULL) {
123 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
124 "sess_mp_%u", socket_id);
126 sess_mp = rte_cryptodev_sym_session_pool_create(mp_name,
127 nb_sessions, 0, 0, 0, socket_id);
129 if (sess_mp == NULL) {
130 printf("Cannot create pool \"%s\" on socket %d\n",
135 printf("Allocated pool \"%s\" on socket %d\n",
137 session_pool_socket[socket_id].sess_mp = sess_mp;
144 cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs)
146 uint8_t enabled_cdev_count = 0, nb_lcores, cdev_id;
147 uint32_t sessions_needed = 0;
151 enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
152 enabled_cdevs, RTE_CRYPTO_MAX_DEVS);
153 if (enabled_cdev_count == 0) {
154 printf("No crypto devices type %s available\n",
159 nb_lcores = rte_lcore_count() - 1;
163 "Number of enabled cores need to be higher than 1\n");
168 * Use less number of devices,
169 * if there are more available than cores.
171 if (enabled_cdev_count > nb_lcores)
172 enabled_cdev_count = nb_lcores;
174 /* Create a mempool shared by all the devices */
175 uint32_t max_sess_size = 0, sess_size;
177 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
178 sess_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
179 if (sess_size > max_sess_size)
180 max_sess_size = sess_size;
182 #ifdef RTE_LIB_SECURITY
183 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
184 sess_size = rte_security_session_get_size(
185 rte_cryptodev_get_sec_ctx(cdev_id));
186 if (sess_size > max_sess_size)
187 max_sess_size = sess_size;
191 * Calculate number of needed queue pairs, based on the amount
192 * of available number of logical cores and crypto devices.
193 * For instance, if there are 4 cores and 2 crypto devices,
194 * 2 queue pairs will be set up per device.
196 opts->nb_qps = (nb_lcores % enabled_cdev_count) ?
197 (nb_lcores / enabled_cdev_count) + 1 :
198 nb_lcores / enabled_cdev_count;
200 for (i = 0; i < enabled_cdev_count &&
201 i < RTE_CRYPTO_MAX_DEVS; i++) {
202 cdev_id = enabled_cdevs[i];
203 #ifdef RTE_CRYPTO_SCHEDULER
205 * If multi-core scheduler is used, limit the number
206 * of queue pairs to 1, as there is no way to know
207 * how many cores are being used by the PMD, and
208 * how many will be available for the application.
210 if (!strcmp((const char *)opts->device_type, "crypto_scheduler") &&
211 rte_cryptodev_scheduler_mode_get(cdev_id) ==
212 CDEV_SCHED_MODE_MULTICORE)
216 struct rte_cryptodev_info cdev_info;
217 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
218 /* range check the socket_id - negative values become big
219 * positive ones due to use of unsigned value
221 if (socket_id >= RTE_MAX_NUMA_NODES)
224 rte_cryptodev_info_get(cdev_id, &cdev_info);
226 if (opts->op_type == CPERF_ASYM_MODEX) {
227 if ((cdev_info.feature_flags &
228 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) == 0)
232 if (opts->nb_qps > cdev_info.max_nb_queue_pairs) {
233 printf("Number of needed queue pairs is higher "
234 "than the maximum number of queue pairs "
236 printf("Lower the number of cores or increase "
237 "the number of crypto devices\n");
240 struct rte_cryptodev_config conf = {
241 .nb_queue_pairs = opts->nb_qps,
242 .socket_id = socket_id,
245 switch (opts->op_type) {
246 case CPERF_ASYM_MODEX:
247 conf.ff_disable |= (RTE_CRYPTODEV_FF_SECURITY |
248 RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO);
250 case CPERF_CIPHER_ONLY:
251 case CPERF_AUTH_ONLY:
252 case CPERF_CIPHER_THEN_AUTH:
253 case CPERF_AUTH_THEN_CIPHER:
255 conf.ff_disable |= RTE_CRYPTODEV_FF_SECURITY;
262 conf.ff_disable |= RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
265 struct rte_cryptodev_qp_conf qp_conf = {
266 .nb_descriptors = opts->nb_descriptors
270 * Device info specifies the min headroom and tailroom
271 * requirement for the crypto PMD. This need to be honoured
272 * by the application, while creating mbuf.
274 if (opts->headroom_sz < cdev_info.min_mbuf_headroom_req) {
275 /* Update headroom */
276 opts->headroom_sz = cdev_info.min_mbuf_headroom_req;
278 if (opts->tailroom_sz < cdev_info.min_mbuf_tailroom_req) {
279 /* Update tailroom */
280 opts->tailroom_sz = cdev_info.min_mbuf_tailroom_req;
283 /* Update segment size to include headroom & tailroom */
284 opts->segment_sz += (opts->headroom_sz + opts->tailroom_sz);
286 uint32_t dev_max_nb_sess = cdev_info.sym.max_nb_sessions;
288 * Two sessions objects are required for each session
289 * (one for the header, one for the private data)
291 if (!strcmp((const char *)opts->device_type,
292 "crypto_scheduler")) {
293 #ifdef RTE_CRYPTO_SCHEDULER
295 rte_cryptodev_scheduler_workers_get(cdev_id,
298 sessions_needed = enabled_cdev_count *
299 opts->nb_qps * nb_slaves;
302 sessions_needed = enabled_cdev_count * opts->nb_qps;
305 * A single session is required per queue pair
308 if (dev_max_nb_sess != 0 && dev_max_nb_sess < opts->nb_qps) {
310 "Device does not support at least "
311 "%u sessions\n", opts->nb_qps);
315 if (opts->op_type == CPERF_ASYM_MODEX)
316 ret = create_asym_op_pool_socket(socket_id,
319 ret = fill_session_pool_socket(socket_id, max_sess_size,
324 qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
325 qp_conf.mp_session_private =
326 session_pool_socket[socket_id].priv_mp;
328 if (opts->op_type == CPERF_ASYM_MODEX) {
329 qp_conf.mp_session = NULL;
330 qp_conf.mp_session_private = NULL;
333 ret = rte_cryptodev_configure(cdev_id, &conf);
335 printf("Failed to configure cryptodev %u", cdev_id);
339 for (j = 0; j < opts->nb_qps; j++) {
340 ret = rte_cryptodev_queue_pair_setup(cdev_id, j,
341 &qp_conf, socket_id);
343 printf("Failed to setup queue pair %u on "
344 "cryptodev %u", j, cdev_id);
349 ret = rte_cryptodev_start(cdev_id);
351 printf("Failed to start device %u: error %d\n",
357 return enabled_cdev_count;
361 cperf_verify_devices_capabilities(struct cperf_options *opts,
362 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
364 struct rte_cryptodev_sym_capability_idx cap_idx;
365 const struct rte_cryptodev_symmetric_capability *capability;
366 struct rte_cryptodev_asym_capability_idx asym_cap_idx;
367 const struct rte_cryptodev_asymmetric_xform_capability *asym_capability;
373 for (i = 0; i < nb_cryptodevs; i++) {
375 cdev_id = enabled_cdevs[i];
377 if (opts->op_type == CPERF_ASYM_MODEX) {
378 asym_cap_idx.type = RTE_CRYPTO_ASYM_XFORM_MODEX;
379 asym_capability = rte_cryptodev_asym_capability_get(
380 cdev_id, &asym_cap_idx);
381 if (asym_capability == NULL)
384 ret = rte_cryptodev_asym_xform_capability_check_modlen(
385 asym_capability, opts->modex_data->modulus.len);
391 if (opts->op_type == CPERF_AUTH_ONLY ||
392 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
393 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
395 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
396 cap_idx.algo.auth = opts->auth_algo;
398 capability = rte_cryptodev_sym_capability_get(cdev_id,
400 if (capability == NULL)
403 ret = rte_cryptodev_sym_capability_check_auth(
412 if (opts->op_type == CPERF_CIPHER_ONLY ||
413 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
414 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
416 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
417 cap_idx.algo.cipher = opts->cipher_algo;
419 capability = rte_cryptodev_sym_capability_get(cdev_id,
421 if (capability == NULL)
424 ret = rte_cryptodev_sym_capability_check_cipher(
432 if (opts->op_type == CPERF_AEAD) {
434 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
435 cap_idx.algo.aead = opts->aead_algo;
437 capability = rte_cryptodev_sym_capability_get(cdev_id,
439 if (capability == NULL)
442 ret = rte_cryptodev_sym_capability_check_aead(
457 cperf_check_test_vector(struct cperf_options *opts,
458 struct cperf_test_vector *test_vec)
460 if (opts->op_type == CPERF_CIPHER_ONLY) {
461 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
462 if (test_vec->plaintext.data == NULL)
465 if (test_vec->plaintext.data == NULL)
467 if (test_vec->plaintext.length < opts->max_buffer_size)
469 if (test_vec->ciphertext.data == NULL)
471 if (test_vec->ciphertext.length < opts->max_buffer_size)
473 /* Cipher IV is only required for some algorithms */
474 if (opts->cipher_iv_sz &&
475 test_vec->cipher_iv.data == NULL)
477 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
479 if (test_vec->cipher_key.data == NULL)
481 if (test_vec->cipher_key.length != opts->cipher_key_sz)
484 } else if (opts->op_type == CPERF_AUTH_ONLY) {
485 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
486 if (test_vec->plaintext.data == NULL)
488 if (test_vec->plaintext.length < opts->max_buffer_size)
490 /* Auth key is only required for some algorithms */
491 if (opts->auth_key_sz &&
492 test_vec->auth_key.data == NULL)
494 if (test_vec->auth_key.length != opts->auth_key_sz)
496 if (test_vec->auth_iv.length != opts->auth_iv_sz)
498 /* Auth IV is only required for some algorithms */
499 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
501 if (test_vec->digest.data == NULL)
503 if (test_vec->digest.length < opts->digest_sz)
507 } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
508 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
509 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
510 if (test_vec->plaintext.data == NULL)
512 if (test_vec->plaintext.length < opts->max_buffer_size)
515 if (test_vec->plaintext.data == NULL)
517 if (test_vec->plaintext.length < opts->max_buffer_size)
519 if (test_vec->ciphertext.data == NULL)
521 if (test_vec->ciphertext.length < opts->max_buffer_size)
523 if (test_vec->cipher_iv.data == NULL)
525 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
527 if (test_vec->cipher_key.data == NULL)
529 if (test_vec->cipher_key.length != opts->cipher_key_sz)
532 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
533 if (test_vec->auth_key.data == NULL)
535 if (test_vec->auth_key.length != opts->auth_key_sz)
537 if (test_vec->auth_iv.length != opts->auth_iv_sz)
539 /* Auth IV is only required for some algorithms */
540 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
542 if (test_vec->digest.data == NULL)
544 if (test_vec->digest.length < opts->digest_sz)
547 } else if (opts->op_type == CPERF_AEAD) {
548 if (test_vec->plaintext.data == NULL)
550 if (test_vec->plaintext.length < opts->max_buffer_size)
552 if (test_vec->ciphertext.data == NULL)
554 if (test_vec->ciphertext.length < opts->max_buffer_size)
556 if (test_vec->aead_key.data == NULL)
558 if (test_vec->aead_key.length != opts->aead_key_sz)
560 if (test_vec->aead_iv.data == NULL)
562 if (test_vec->aead_iv.length != opts->aead_iv_sz)
564 if (test_vec->aad.data == NULL)
566 if (test_vec->aad.length != opts->aead_aad_sz)
568 if (test_vec->digest.data == NULL)
570 if (test_vec->digest.length < opts->digest_sz)
577 main(int argc, char **argv)
579 struct cperf_options opts = {0};
580 struct cperf_test_vector *t_vec = NULL;
581 struct cperf_op_fns op_fns;
582 void *ctx[RTE_MAX_LCORE] = { };
583 int nb_cryptodevs = 0;
584 uint16_t total_nb_qps = 0;
586 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
588 uint8_t buffer_size_idx = 0;
593 /* Initialise DPDK EAL */
594 ret = rte_eal_init(argc, argv);
596 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
600 cperf_options_default(&opts);
602 ret = cperf_options_parse(&opts, argc, argv);
604 RTE_LOG(ERR, USER1, "Parsing one or more user options failed\n");
608 ret = cperf_options_check(&opts);
611 "Checking one or more user options failed\n");
615 nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs);
618 cperf_options_dump(&opts);
620 if (nb_cryptodevs < 1) {
621 RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
627 ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
630 RTE_LOG(ERR, USER1, "Crypto device type does not support "
631 "capabilities requested\n");
635 if (opts.test_file != NULL) {
636 t_vec = cperf_test_vector_get_from_file(&opts);
639 "Failed to create test vector for"
640 " specified file\n");
644 if (cperf_check_test_vector(&opts, t_vec)) {
645 RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
650 t_vec = cperf_test_vector_get_dummy(&opts);
653 "Failed to create test vector for"
654 " specified algorithms\n");
659 ret = cperf_get_op_functions(&opts, &op_fns);
661 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
662 "specified algorithms combination\n");
666 if (!opts.silent && opts.test != CPERF_TEST_TYPE_THROUGHPUT &&
667 opts.test != CPERF_TEST_TYPE_LATENCY)
668 show_test_vector(t_vec);
670 total_nb_qps = nb_cryptodevs * opts.nb_qps;
673 uint8_t qp_id = 0, cdev_index = 0;
674 RTE_LCORE_FOREACH_WORKER(lcore_id) {
676 if (i == total_nb_qps)
679 cdev_id = enabled_cdevs[cdev_index];
681 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
683 ctx[i] = cperf_testmap[opts.test].constructor(
684 session_pool_socket[socket_id].sess_mp,
685 session_pool_socket[socket_id].priv_mp,
687 &opts, t_vec, &op_fns);
688 if (ctx[i] == NULL) {
689 RTE_LOG(ERR, USER1, "Test run constructor failed\n");
692 qp_id = (qp_id + 1) % opts.nb_qps;
698 if (opts.imix_distribution_count != 0) {
699 uint8_t buffer_size_count = opts.buffer_size_count;
700 uint16_t distribution_total[buffer_size_count];
702 uint32_t test_average_size = 0;
703 const uint32_t *buffer_size_list = opts.buffer_size_list;
704 const uint32_t *imix_distribution_list = opts.imix_distribution_list;
706 opts.imix_buffer_sizes = rte_malloc(NULL,
707 sizeof(uint32_t) * opts.pool_sz,
710 * Calculate accumulated distribution of
711 * probabilities per packet size
713 distribution_total[0] = imix_distribution_list[0];
714 for (i = 1; i < buffer_size_count; i++)
715 distribution_total[i] = imix_distribution_list[i] +
716 distribution_total[i-1];
718 /* Calculate a random sequence of packet sizes, based on distribution */
719 for (op_idx = 0; op_idx < opts.pool_sz; op_idx++) {
720 uint16_t random_number = rte_rand() %
721 distribution_total[buffer_size_count - 1];
722 for (i = 0; i < buffer_size_count; i++)
723 if (random_number < distribution_total[i])
726 opts.imix_buffer_sizes[op_idx] = buffer_size_list[i];
729 /* Calculate average buffer size for the IMIX distribution */
730 for (i = 0; i < buffer_size_count; i++)
731 test_average_size += buffer_size_list[i] *
732 imix_distribution_list[i];
734 opts.test_buffer_size = test_average_size /
735 distribution_total[buffer_size_count - 1];
738 RTE_LCORE_FOREACH_WORKER(lcore_id) {
740 if (i == total_nb_qps)
743 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
748 RTE_LCORE_FOREACH_WORKER(lcore_id) {
750 if (i == total_nb_qps)
752 ret |= rte_eal_wait_lcore(lcore_id);
756 if (ret != EXIT_SUCCESS)
760 /* Get next size from range or list */
761 if (opts.inc_buffer_size != 0)
762 opts.test_buffer_size = opts.min_buffer_size;
764 opts.test_buffer_size = opts.buffer_size_list[0];
766 while (opts.test_buffer_size <= opts.max_buffer_size) {
768 RTE_LCORE_FOREACH_WORKER(lcore_id) {
770 if (i == total_nb_qps)
773 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
778 RTE_LCORE_FOREACH_WORKER(lcore_id) {
780 if (i == total_nb_qps)
782 ret |= rte_eal_wait_lcore(lcore_id);
786 if (ret != EXIT_SUCCESS)
789 /* Get next size from range or list */
790 if (opts.inc_buffer_size != 0)
791 opts.test_buffer_size += opts.inc_buffer_size;
793 if (++buffer_size_idx == opts.buffer_size_count)
795 opts.test_buffer_size =
796 opts.buffer_size_list[buffer_size_idx];
802 RTE_LCORE_FOREACH_WORKER(lcore_id) {
804 if (i == total_nb_qps)
807 cperf_testmap[opts.test].destructor(ctx[i]);
811 for (i = 0; i < nb_cryptodevs &&
812 i < RTE_CRYPTO_MAX_DEVS; i++) {
813 rte_cryptodev_stop(enabled_cdevs[i]);
814 ret = rte_cryptodev_close(enabled_cdevs[i]);
817 "Crypto device close error %d\n", ret);
820 free_test_vector(t_vec, &opts);
827 RTE_LCORE_FOREACH_WORKER(lcore_id) {
828 if (i == total_nb_qps)
831 if (ctx[i] && cperf_testmap[opts.test].destructor)
832 cperf_testmap[opts.test].destructor(ctx[i]);
836 for (i = 0; i < nb_cryptodevs &&
837 i < RTE_CRYPTO_MAX_DEVS; i++) {
838 rte_cryptodev_stop(enabled_cdevs[i]);
839 ret = rte_cryptodev_close(enabled_cdevs[i]);
842 "Crypto device close error %d\n", ret);
845 rte_free(opts.imix_buffer_sizes);
846 free_test_vector(t_vec, &opts);