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"
46 const struct cperf_test cperf_testmap[] = {
47 [CPERF_TEST_TYPE_THROUGHPUT] = {
48 cperf_throughput_test_constructor,
49 cperf_throughput_test_runner,
50 cperf_throughput_test_destructor
52 [CPERF_TEST_TYPE_LATENCY] = {
53 cperf_latency_test_constructor,
54 cperf_latency_test_runner,
55 cperf_latency_test_destructor
57 [CPERF_TEST_TYPE_VERIFY] = {
58 cperf_verify_test_constructor,
59 cperf_verify_test_runner,
60 cperf_verify_test_destructor
62 [CPERF_TEST_TYPE_PMDCC] = {
63 cperf_pmd_cyclecount_test_constructor,
64 cperf_pmd_cyclecount_test_runner,
65 cperf_pmd_cyclecount_test_destructor
70 fill_session_pool_socket(int32_t socket_id, uint32_t session_priv_size,
73 char mp_name[RTE_MEMPOOL_NAMESIZE];
74 struct rte_mempool *sess_mp;
76 if (session_pool_socket[socket_id].priv_mp == NULL) {
77 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
78 "priv_sess_mp_%u", socket_id);
80 sess_mp = rte_mempool_create(mp_name,
83 0, 0, NULL, NULL, NULL,
87 if (sess_mp == NULL) {
88 printf("Cannot create pool \"%s\" on socket %d\n",
93 printf("Allocated pool \"%s\" on socket %d\n",
95 session_pool_socket[socket_id].priv_mp = sess_mp;
98 if (session_pool_socket[socket_id].sess_mp == NULL) {
100 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
101 "sess_mp_%u", socket_id);
103 sess_mp = rte_cryptodev_sym_session_pool_create(mp_name,
104 nb_sessions, 0, 0, 0, socket_id);
106 if (sess_mp == NULL) {
107 printf("Cannot create pool \"%s\" on socket %d\n",
112 printf("Allocated pool \"%s\" on socket %d\n",
114 session_pool_socket[socket_id].sess_mp = sess_mp;
121 cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs)
123 uint8_t enabled_cdev_count = 0, nb_lcores, cdev_id;
124 uint32_t sessions_needed = 0;
128 enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
129 enabled_cdevs, RTE_CRYPTO_MAX_DEVS);
130 if (enabled_cdev_count == 0) {
131 printf("No crypto devices type %s available\n",
136 nb_lcores = rte_lcore_count() - 1;
140 "Number of enabled cores need to be higher than 1\n");
145 * Use less number of devices,
146 * if there are more available than cores.
148 if (enabled_cdev_count > nb_lcores)
149 enabled_cdev_count = nb_lcores;
151 /* Create a mempool shared by all the devices */
152 uint32_t max_sess_size = 0, sess_size;
154 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
155 sess_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
156 if (sess_size > max_sess_size)
157 max_sess_size = sess_size;
159 #ifdef RTE_LIB_SECURITY
160 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
161 sess_size = rte_security_session_get_size(
162 rte_cryptodev_get_sec_ctx(cdev_id));
163 if (sess_size > max_sess_size)
164 max_sess_size = sess_size;
168 * Calculate number of needed queue pairs, based on the amount
169 * of available number of logical cores and crypto devices.
170 * For instance, if there are 4 cores and 2 crypto devices,
171 * 2 queue pairs will be set up per device.
173 opts->nb_qps = (nb_lcores % enabled_cdev_count) ?
174 (nb_lcores / enabled_cdev_count) + 1 :
175 nb_lcores / enabled_cdev_count;
177 for (i = 0; i < enabled_cdev_count &&
178 i < RTE_CRYPTO_MAX_DEVS; i++) {
179 cdev_id = enabled_cdevs[i];
180 #ifdef RTE_CRYPTO_SCHEDULER
182 * If multi-core scheduler is used, limit the number
183 * of queue pairs to 1, as there is no way to know
184 * how many cores are being used by the PMD, and
185 * how many will be available for the application.
187 if (!strcmp((const char *)opts->device_type, "crypto_scheduler") &&
188 rte_cryptodev_scheduler_mode_get(cdev_id) ==
189 CDEV_SCHED_MODE_MULTICORE)
193 struct rte_cryptodev_info cdev_info;
194 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
195 /* range check the socket_id - negative values become big
196 * positive ones due to use of unsigned value
198 if (socket_id >= RTE_MAX_NUMA_NODES)
201 rte_cryptodev_info_get(cdev_id, &cdev_info);
202 if (opts->nb_qps > cdev_info.max_nb_queue_pairs) {
203 printf("Number of needed queue pairs is higher "
204 "than the maximum number of queue pairs "
206 printf("Lower the number of cores or increase "
207 "the number of crypto devices\n");
210 struct rte_cryptodev_config conf = {
211 .nb_queue_pairs = opts->nb_qps,
212 .socket_id = socket_id,
213 .ff_disable = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO,
216 if (opts->op_type != CPERF_PDCP &&
217 opts->op_type != CPERF_DOCSIS)
218 conf.ff_disable |= RTE_CRYPTODEV_FF_SECURITY;
220 struct rte_cryptodev_qp_conf qp_conf = {
221 .nb_descriptors = opts->nb_descriptors
225 * Device info specifies the min headroom and tailroom
226 * requirement for the crypto PMD. This need to be honoured
227 * by the application, while creating mbuf.
229 if (opts->headroom_sz < cdev_info.min_mbuf_headroom_req) {
230 /* Update headroom */
231 opts->headroom_sz = cdev_info.min_mbuf_headroom_req;
233 if (opts->tailroom_sz < cdev_info.min_mbuf_tailroom_req) {
234 /* Update tailroom */
235 opts->tailroom_sz = cdev_info.min_mbuf_tailroom_req;
238 /* Update segment size to include headroom & tailroom */
239 opts->segment_sz += (opts->headroom_sz + opts->tailroom_sz);
241 uint32_t dev_max_nb_sess = cdev_info.sym.max_nb_sessions;
243 * Two sessions objects are required for each session
244 * (one for the header, one for the private data)
246 if (!strcmp((const char *)opts->device_type,
247 "crypto_scheduler")) {
248 #ifdef RTE_CRYPTO_SCHEDULER
250 rte_cryptodev_scheduler_workers_get(cdev_id,
253 sessions_needed = enabled_cdev_count *
254 opts->nb_qps * nb_slaves;
257 sessions_needed = enabled_cdev_count * opts->nb_qps;
260 * A single session is required per queue pair
263 if (dev_max_nb_sess != 0 && dev_max_nb_sess < opts->nb_qps) {
265 "Device does not support at least "
266 "%u sessions\n", opts->nb_qps);
270 ret = fill_session_pool_socket(socket_id, max_sess_size,
275 qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
276 qp_conf.mp_session_private =
277 session_pool_socket[socket_id].priv_mp;
279 ret = rte_cryptodev_configure(cdev_id, &conf);
281 printf("Failed to configure cryptodev %u", cdev_id);
285 for (j = 0; j < opts->nb_qps; j++) {
286 ret = rte_cryptodev_queue_pair_setup(cdev_id, j,
287 &qp_conf, socket_id);
289 printf("Failed to setup queue pair %u on "
290 "cryptodev %u", j, cdev_id);
295 ret = rte_cryptodev_start(cdev_id);
297 printf("Failed to start device %u: error %d\n",
303 return enabled_cdev_count;
307 cperf_verify_devices_capabilities(struct cperf_options *opts,
308 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
310 struct rte_cryptodev_sym_capability_idx cap_idx;
311 const struct rte_cryptodev_symmetric_capability *capability;
316 for (i = 0; i < nb_cryptodevs; i++) {
318 cdev_id = enabled_cdevs[i];
320 if (opts->op_type == CPERF_AUTH_ONLY ||
321 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
322 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
324 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
325 cap_idx.algo.auth = opts->auth_algo;
327 capability = rte_cryptodev_sym_capability_get(cdev_id,
329 if (capability == NULL)
332 ret = rte_cryptodev_sym_capability_check_auth(
341 if (opts->op_type == CPERF_CIPHER_ONLY ||
342 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
343 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
345 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
346 cap_idx.algo.cipher = opts->cipher_algo;
348 capability = rte_cryptodev_sym_capability_get(cdev_id,
350 if (capability == NULL)
353 ret = rte_cryptodev_sym_capability_check_cipher(
361 if (opts->op_type == CPERF_AEAD) {
363 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
364 cap_idx.algo.aead = opts->aead_algo;
366 capability = rte_cryptodev_sym_capability_get(cdev_id,
368 if (capability == NULL)
371 ret = rte_cryptodev_sym_capability_check_aead(
386 cperf_check_test_vector(struct cperf_options *opts,
387 struct cperf_test_vector *test_vec)
389 if (opts->op_type == CPERF_CIPHER_ONLY) {
390 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
391 if (test_vec->plaintext.data == NULL)
394 if (test_vec->plaintext.data == NULL)
396 if (test_vec->plaintext.length < opts->max_buffer_size)
398 if (test_vec->ciphertext.data == NULL)
400 if (test_vec->ciphertext.length < opts->max_buffer_size)
402 /* Cipher IV is only required for some algorithms */
403 if (opts->cipher_iv_sz &&
404 test_vec->cipher_iv.data == NULL)
406 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
408 if (test_vec->cipher_key.data == NULL)
410 if (test_vec->cipher_key.length != opts->cipher_key_sz)
413 } else if (opts->op_type == CPERF_AUTH_ONLY) {
414 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
415 if (test_vec->plaintext.data == NULL)
417 if (test_vec->plaintext.length < opts->max_buffer_size)
419 /* Auth key is only required for some algorithms */
420 if (opts->auth_key_sz &&
421 test_vec->auth_key.data == NULL)
423 if (test_vec->auth_key.length != opts->auth_key_sz)
425 if (test_vec->auth_iv.length != opts->auth_iv_sz)
427 /* Auth IV is only required for some algorithms */
428 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
430 if (test_vec->digest.data == NULL)
432 if (test_vec->digest.length < opts->digest_sz)
436 } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
437 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
438 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
439 if (test_vec->plaintext.data == NULL)
441 if (test_vec->plaintext.length < opts->max_buffer_size)
444 if (test_vec->plaintext.data == NULL)
446 if (test_vec->plaintext.length < opts->max_buffer_size)
448 if (test_vec->ciphertext.data == NULL)
450 if (test_vec->ciphertext.length < opts->max_buffer_size)
452 if (test_vec->cipher_iv.data == NULL)
454 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
456 if (test_vec->cipher_key.data == NULL)
458 if (test_vec->cipher_key.length != opts->cipher_key_sz)
461 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
462 if (test_vec->auth_key.data == NULL)
464 if (test_vec->auth_key.length != opts->auth_key_sz)
466 if (test_vec->auth_iv.length != opts->auth_iv_sz)
468 /* Auth IV is only required for some algorithms */
469 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
471 if (test_vec->digest.data == NULL)
473 if (test_vec->digest.length < opts->digest_sz)
476 } else if (opts->op_type == CPERF_AEAD) {
477 if (test_vec->plaintext.data == NULL)
479 if (test_vec->plaintext.length < opts->max_buffer_size)
481 if (test_vec->ciphertext.data == NULL)
483 if (test_vec->ciphertext.length < opts->max_buffer_size)
485 if (test_vec->aead_key.data == NULL)
487 if (test_vec->aead_key.length != opts->aead_key_sz)
489 if (test_vec->aead_iv.data == NULL)
491 if (test_vec->aead_iv.length != opts->aead_iv_sz)
493 if (test_vec->aad.data == NULL)
495 if (test_vec->aad.length != opts->aead_aad_sz)
497 if (test_vec->digest.data == NULL)
499 if (test_vec->digest.length < opts->digest_sz)
506 main(int argc, char **argv)
508 struct cperf_options opts = {0};
509 struct cperf_test_vector *t_vec = NULL;
510 struct cperf_op_fns op_fns;
511 void *ctx[RTE_MAX_LCORE] = { };
512 int nb_cryptodevs = 0;
513 uint16_t total_nb_qps = 0;
515 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
517 uint8_t buffer_size_idx = 0;
522 /* Initialise DPDK EAL */
523 ret = rte_eal_init(argc, argv);
525 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
529 cperf_options_default(&opts);
531 ret = cperf_options_parse(&opts, argc, argv);
533 RTE_LOG(ERR, USER1, "Parsing one or more user options failed\n");
537 ret = cperf_options_check(&opts);
540 "Checking one or more user options failed\n");
544 nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs);
547 cperf_options_dump(&opts);
549 if (nb_cryptodevs < 1) {
550 RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
556 ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
559 RTE_LOG(ERR, USER1, "Crypto device type does not support "
560 "capabilities requested\n");
564 if (opts.test_file != NULL) {
565 t_vec = cperf_test_vector_get_from_file(&opts);
568 "Failed to create test vector for"
569 " specified file\n");
573 if (cperf_check_test_vector(&opts, t_vec)) {
574 RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
579 t_vec = cperf_test_vector_get_dummy(&opts);
582 "Failed to create test vector for"
583 " specified algorithms\n");
588 ret = cperf_get_op_functions(&opts, &op_fns);
590 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
591 "specified algorithms combination\n");
595 if (!opts.silent && opts.test != CPERF_TEST_TYPE_THROUGHPUT &&
596 opts.test != CPERF_TEST_TYPE_LATENCY)
597 show_test_vector(t_vec);
599 total_nb_qps = nb_cryptodevs * opts.nb_qps;
602 uint8_t qp_id = 0, cdev_index = 0;
603 RTE_LCORE_FOREACH_WORKER(lcore_id) {
605 if (i == total_nb_qps)
608 cdev_id = enabled_cdevs[cdev_index];
610 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
612 ctx[i] = cperf_testmap[opts.test].constructor(
613 session_pool_socket[socket_id].sess_mp,
614 session_pool_socket[socket_id].priv_mp,
616 &opts, t_vec, &op_fns);
617 if (ctx[i] == NULL) {
618 RTE_LOG(ERR, USER1, "Test run constructor failed\n");
621 qp_id = (qp_id + 1) % opts.nb_qps;
627 if (opts.imix_distribution_count != 0) {
628 uint8_t buffer_size_count = opts.buffer_size_count;
629 uint16_t distribution_total[buffer_size_count];
631 uint32_t test_average_size = 0;
632 const uint32_t *buffer_size_list = opts.buffer_size_list;
633 const uint32_t *imix_distribution_list = opts.imix_distribution_list;
635 opts.imix_buffer_sizes = rte_malloc(NULL,
636 sizeof(uint32_t) * opts.pool_sz,
639 * Calculate accumulated distribution of
640 * probabilities per packet size
642 distribution_total[0] = imix_distribution_list[0];
643 for (i = 1; i < buffer_size_count; i++)
644 distribution_total[i] = imix_distribution_list[i] +
645 distribution_total[i-1];
647 /* Calculate a random sequence of packet sizes, based on distribution */
648 for (op_idx = 0; op_idx < opts.pool_sz; op_idx++) {
649 uint16_t random_number = rte_rand() %
650 distribution_total[buffer_size_count - 1];
651 for (i = 0; i < buffer_size_count; i++)
652 if (random_number < distribution_total[i])
655 opts.imix_buffer_sizes[op_idx] = buffer_size_list[i];
658 /* Calculate average buffer size for the IMIX distribution */
659 for (i = 0; i < buffer_size_count; i++)
660 test_average_size += buffer_size_list[i] *
661 imix_distribution_list[i];
663 opts.test_buffer_size = test_average_size /
664 distribution_total[buffer_size_count - 1];
667 RTE_LCORE_FOREACH_WORKER(lcore_id) {
669 if (i == total_nb_qps)
672 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
677 RTE_LCORE_FOREACH_WORKER(lcore_id) {
679 if (i == total_nb_qps)
681 ret |= rte_eal_wait_lcore(lcore_id);
685 if (ret != EXIT_SUCCESS)
689 /* Get next size from range or list */
690 if (opts.inc_buffer_size != 0)
691 opts.test_buffer_size = opts.min_buffer_size;
693 opts.test_buffer_size = opts.buffer_size_list[0];
695 while (opts.test_buffer_size <= opts.max_buffer_size) {
697 RTE_LCORE_FOREACH_WORKER(lcore_id) {
699 if (i == total_nb_qps)
702 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
707 RTE_LCORE_FOREACH_WORKER(lcore_id) {
709 if (i == total_nb_qps)
711 ret |= rte_eal_wait_lcore(lcore_id);
715 if (ret != EXIT_SUCCESS)
718 /* Get next size from range or list */
719 if (opts.inc_buffer_size != 0)
720 opts.test_buffer_size += opts.inc_buffer_size;
722 if (++buffer_size_idx == opts.buffer_size_count)
724 opts.test_buffer_size =
725 opts.buffer_size_list[buffer_size_idx];
731 RTE_LCORE_FOREACH_WORKER(lcore_id) {
733 if (i == total_nb_qps)
736 cperf_testmap[opts.test].destructor(ctx[i]);
740 for (i = 0; i < nb_cryptodevs &&
741 i < RTE_CRYPTO_MAX_DEVS; i++) {
742 rte_cryptodev_stop(enabled_cdevs[i]);
743 ret = rte_cryptodev_close(enabled_cdevs[i]);
746 "Crypto device close error %d\n", ret);
749 free_test_vector(t_vec, &opts);
756 RTE_LCORE_FOREACH_WORKER(lcore_id) {
757 if (i == total_nb_qps)
760 if (ctx[i] && cperf_testmap[opts.test].destructor)
761 cperf_testmap[opts.test].destructor(ctx[i]);
765 for (i = 0; i < nb_cryptodevs &&
766 i < RTE_CRYPTO_MAX_DEVS; i++) {
767 rte_cryptodev_stop(enabled_cdevs[i]);
768 ret = rte_cryptodev_close(enabled_cdevs[i]);
771 "Crypto device close error %d\n", ret);
774 rte_free(opts.imix_buffer_sizes);
775 free_test_vector(t_vec, &opts);