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_LIBRTE_PMD_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;
161 * Calculate number of needed queue pairs, based on the amount
162 * of available number of logical cores and crypto devices.
163 * For instance, if there are 4 cores and 2 crypto devices,
164 * 2 queue pairs will be set up per device.
166 opts->nb_qps = (nb_lcores % enabled_cdev_count) ?
167 (nb_lcores / enabled_cdev_count) + 1 :
168 nb_lcores / enabled_cdev_count;
170 for (i = 0; i < enabled_cdev_count &&
171 i < RTE_CRYPTO_MAX_DEVS; i++) {
172 cdev_id = enabled_cdevs[i];
173 #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
175 * If multi-core scheduler is used, limit the number
176 * of queue pairs to 1, as there is no way to know
177 * how many cores are being used by the PMD, and
178 * how many will be available for the application.
180 if (!strcmp((const char *)opts->device_type, "crypto_scheduler") &&
181 rte_cryptodev_scheduler_mode_get(cdev_id) ==
182 CDEV_SCHED_MODE_MULTICORE)
186 struct rte_cryptodev_info cdev_info;
187 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
188 /* range check the socket_id - negative values become big
189 * positive ones due to use of unsigned value
191 if (socket_id >= RTE_MAX_NUMA_NODES)
194 rte_cryptodev_info_get(cdev_id, &cdev_info);
195 if (opts->nb_qps > cdev_info.max_nb_queue_pairs) {
196 printf("Number of needed queue pairs is higher "
197 "than the maximum number of queue pairs "
199 printf("Lower the number of cores or increase "
200 "the number of crypto devices\n");
203 struct rte_cryptodev_config conf = {
204 .nb_queue_pairs = opts->nb_qps,
205 .socket_id = socket_id,
206 .ff_disable = RTE_CRYPTODEV_FF_SECURITY |
207 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO,
210 struct rte_cryptodev_qp_conf qp_conf = {
211 .nb_descriptors = opts->nb_descriptors
215 * Device info specifies the min headroom and tailroom
216 * requirement for the crypto PMD. This need to be honoured
217 * by the application, while creating mbuf.
219 if (opts->headroom_sz < cdev_info.min_mbuf_headroom_req) {
220 /* Update headroom */
221 opts->headroom_sz = cdev_info.min_mbuf_headroom_req;
223 if (opts->tailroom_sz < cdev_info.min_mbuf_tailroom_req) {
224 /* Update tailroom */
225 opts->tailroom_sz = cdev_info.min_mbuf_tailroom_req;
228 /* Update segment size to include headroom & tailroom */
229 opts->segment_sz += (opts->headroom_sz + opts->tailroom_sz);
231 uint32_t dev_max_nb_sess = cdev_info.sym.max_nb_sessions;
233 * Two sessions objects are required for each session
234 * (one for the header, one for the private data)
236 if (!strcmp((const char *)opts->device_type,
237 "crypto_scheduler")) {
238 #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
240 rte_cryptodev_scheduler_slaves_get(cdev_id,
243 sessions_needed = enabled_cdev_count *
244 opts->nb_qps * nb_slaves;
247 sessions_needed = enabled_cdev_count *
251 * A single session is required per queue pair
254 if (dev_max_nb_sess != 0 && dev_max_nb_sess < opts->nb_qps) {
256 "Device does not support at least "
257 "%u sessions\n", opts->nb_qps);
261 ret = fill_session_pool_socket(socket_id, max_sess_size,
266 qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
267 qp_conf.mp_session_private =
268 session_pool_socket[socket_id].priv_mp;
270 ret = rte_cryptodev_configure(cdev_id, &conf);
272 printf("Failed to configure cryptodev %u", cdev_id);
276 for (j = 0; j < opts->nb_qps; j++) {
277 ret = rte_cryptodev_queue_pair_setup(cdev_id, j,
278 &qp_conf, socket_id);
280 printf("Failed to setup queue pair %u on "
281 "cryptodev %u", j, cdev_id);
286 ret = rte_cryptodev_start(cdev_id);
288 printf("Failed to start device %u: error %d\n",
294 return enabled_cdev_count;
298 cperf_verify_devices_capabilities(struct cperf_options *opts,
299 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
301 struct rte_cryptodev_sym_capability_idx cap_idx;
302 const struct rte_cryptodev_symmetric_capability *capability;
307 for (i = 0; i < nb_cryptodevs; i++) {
309 cdev_id = enabled_cdevs[i];
311 if (opts->op_type == CPERF_AUTH_ONLY ||
312 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
313 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
315 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
316 cap_idx.algo.auth = opts->auth_algo;
318 capability = rte_cryptodev_sym_capability_get(cdev_id,
320 if (capability == NULL)
323 ret = rte_cryptodev_sym_capability_check_auth(
332 if (opts->op_type == CPERF_CIPHER_ONLY ||
333 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
334 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
336 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
337 cap_idx.algo.cipher = opts->cipher_algo;
339 capability = rte_cryptodev_sym_capability_get(cdev_id,
341 if (capability == NULL)
344 ret = rte_cryptodev_sym_capability_check_cipher(
352 if (opts->op_type == CPERF_AEAD) {
354 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
355 cap_idx.algo.aead = opts->aead_algo;
357 capability = rte_cryptodev_sym_capability_get(cdev_id,
359 if (capability == NULL)
362 ret = rte_cryptodev_sym_capability_check_aead(
377 cperf_check_test_vector(struct cperf_options *opts,
378 struct cperf_test_vector *test_vec)
380 if (opts->op_type == CPERF_CIPHER_ONLY) {
381 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
382 if (test_vec->plaintext.data == NULL)
384 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
385 if (test_vec->plaintext.data == NULL)
387 if (test_vec->plaintext.length < opts->max_buffer_size)
389 if (test_vec->ciphertext.data == NULL)
391 if (test_vec->ciphertext.length < opts->max_buffer_size)
393 /* Cipher IV is only required for some algorithms */
394 if (opts->cipher_iv_sz &&
395 test_vec->cipher_iv.data == NULL)
397 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
399 if (test_vec->cipher_key.data == NULL)
401 if (test_vec->cipher_key.length != opts->cipher_key_sz)
404 } else if (opts->op_type == CPERF_AUTH_ONLY) {
405 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
406 if (test_vec->plaintext.data == NULL)
408 if (test_vec->plaintext.length < opts->max_buffer_size)
410 /* Auth key is only required for some algorithms */
411 if (opts->auth_key_sz &&
412 test_vec->auth_key.data == NULL)
414 if (test_vec->auth_key.length != opts->auth_key_sz)
416 if (test_vec->auth_iv.length != opts->auth_iv_sz)
418 /* Auth IV is only required for some algorithms */
419 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
421 if (test_vec->digest.data == NULL)
423 if (test_vec->digest.length < opts->digest_sz)
427 } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
428 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
429 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
430 if (test_vec->plaintext.data == NULL)
432 if (test_vec->plaintext.length < opts->max_buffer_size)
434 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
435 if (test_vec->plaintext.data == NULL)
437 if (test_vec->plaintext.length < opts->max_buffer_size)
439 if (test_vec->ciphertext.data == NULL)
441 if (test_vec->ciphertext.length < opts->max_buffer_size)
443 if (test_vec->cipher_iv.data == NULL)
445 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
447 if (test_vec->cipher_key.data == NULL)
449 if (test_vec->cipher_key.length != opts->cipher_key_sz)
452 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
453 if (test_vec->auth_key.data == NULL)
455 if (test_vec->auth_key.length != opts->auth_key_sz)
457 if (test_vec->auth_iv.length != opts->auth_iv_sz)
459 /* Auth IV is only required for some algorithms */
460 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
462 if (test_vec->digest.data == NULL)
464 if (test_vec->digest.length < opts->digest_sz)
467 } else if (opts->op_type == CPERF_AEAD) {
468 if (test_vec->plaintext.data == NULL)
470 if (test_vec->plaintext.length < opts->max_buffer_size)
472 if (test_vec->ciphertext.data == NULL)
474 if (test_vec->ciphertext.length < opts->max_buffer_size)
476 if (test_vec->aead_key.data == NULL)
478 if (test_vec->aead_key.length != opts->aead_key_sz)
480 if (test_vec->aead_iv.data == NULL)
482 if (test_vec->aead_iv.length != opts->aead_iv_sz)
484 if (test_vec->aad.data == NULL)
486 if (test_vec->aad.length != opts->aead_aad_sz)
488 if (test_vec->digest.data == NULL)
490 if (test_vec->digest.length < opts->digest_sz)
497 main(int argc, char **argv)
499 struct cperf_options opts = {0};
500 struct cperf_test_vector *t_vec = NULL;
501 struct cperf_op_fns op_fns;
502 void *ctx[RTE_MAX_LCORE] = { };
503 int nb_cryptodevs = 0;
504 uint16_t total_nb_qps = 0;
506 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
508 uint8_t buffer_size_idx = 0;
513 /* Initialise DPDK EAL */
514 ret = rte_eal_init(argc, argv);
516 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
520 cperf_options_default(&opts);
522 ret = cperf_options_parse(&opts, argc, argv);
524 RTE_LOG(ERR, USER1, "Parsing on or more user options failed\n");
528 ret = cperf_options_check(&opts);
531 "Checking on or more user options failed\n");
535 nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs);
538 cperf_options_dump(&opts);
540 if (nb_cryptodevs < 1) {
541 RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
547 ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
550 RTE_LOG(ERR, USER1, "Crypto device type does not support "
551 "capabilities requested\n");
555 if (opts.test_file != NULL) {
556 t_vec = cperf_test_vector_get_from_file(&opts);
559 "Failed to create test vector for"
560 " specified file\n");
564 if (cperf_check_test_vector(&opts, t_vec)) {
565 RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
570 t_vec = cperf_test_vector_get_dummy(&opts);
573 "Failed to create test vector for"
574 " specified algorithms\n");
579 ret = cperf_get_op_functions(&opts, &op_fns);
581 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
582 "specified algorithms combination\n");
586 if (!opts.silent && opts.test != CPERF_TEST_TYPE_THROUGHPUT &&
587 opts.test != CPERF_TEST_TYPE_LATENCY)
588 show_test_vector(t_vec);
590 total_nb_qps = nb_cryptodevs * opts.nb_qps;
593 uint8_t qp_id = 0, cdev_index = 0;
594 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
596 if (i == total_nb_qps)
599 cdev_id = enabled_cdevs[cdev_index];
601 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
603 ctx[i] = cperf_testmap[opts.test].constructor(
604 session_pool_socket[socket_id].sess_mp,
605 session_pool_socket[socket_id].priv_mp,
607 &opts, t_vec, &op_fns);
608 if (ctx[i] == NULL) {
609 RTE_LOG(ERR, USER1, "Test run constructor failed\n");
612 qp_id = (qp_id + 1) % opts.nb_qps;
618 if (opts.imix_distribution_count != 0) {
619 uint8_t buffer_size_count = opts.buffer_size_count;
620 uint16_t distribution_total[buffer_size_count];
622 uint32_t test_average_size = 0;
623 const uint32_t *buffer_size_list = opts.buffer_size_list;
624 const uint32_t *imix_distribution_list = opts.imix_distribution_list;
626 opts.imix_buffer_sizes = rte_malloc(NULL,
627 sizeof(uint32_t) * opts.pool_sz,
630 * Calculate accumulated distribution of
631 * probabilities per packet size
633 distribution_total[0] = imix_distribution_list[0];
634 for (i = 1; i < buffer_size_count; i++)
635 distribution_total[i] = imix_distribution_list[i] +
636 distribution_total[i-1];
638 /* Calculate a random sequence of packet sizes, based on distribution */
639 for (op_idx = 0; op_idx < opts.pool_sz; op_idx++) {
640 uint16_t random_number = rte_rand() %
641 distribution_total[buffer_size_count - 1];
642 for (i = 0; i < buffer_size_count; i++)
643 if (random_number < distribution_total[i])
646 opts.imix_buffer_sizes[op_idx] = buffer_size_list[i];
649 /* Calculate average buffer size for the IMIX distribution */
650 for (i = 0; i < buffer_size_count; i++)
651 test_average_size += buffer_size_list[i] *
652 imix_distribution_list[i];
654 opts.test_buffer_size = test_average_size /
655 distribution_total[buffer_size_count - 1];
658 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
660 if (i == total_nb_qps)
663 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
668 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
670 if (i == total_nb_qps)
672 ret |= rte_eal_wait_lcore(lcore_id);
676 if (ret != EXIT_SUCCESS)
680 /* Get next size from range or list */
681 if (opts.inc_buffer_size != 0)
682 opts.test_buffer_size = opts.min_buffer_size;
684 opts.test_buffer_size = opts.buffer_size_list[0];
686 while (opts.test_buffer_size <= opts.max_buffer_size) {
688 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
690 if (i == total_nb_qps)
693 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
698 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
700 if (i == total_nb_qps)
702 ret |= rte_eal_wait_lcore(lcore_id);
706 if (ret != EXIT_SUCCESS)
709 /* Get next size from range or list */
710 if (opts.inc_buffer_size != 0)
711 opts.test_buffer_size += opts.inc_buffer_size;
713 if (++buffer_size_idx == opts.buffer_size_count)
715 opts.test_buffer_size =
716 opts.buffer_size_list[buffer_size_idx];
722 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
724 if (i == total_nb_qps)
727 cperf_testmap[opts.test].destructor(ctx[i]);
731 for (i = 0; i < nb_cryptodevs &&
732 i < RTE_CRYPTO_MAX_DEVS; i++)
733 rte_cryptodev_stop(enabled_cdevs[i]);
735 free_test_vector(t_vec, &opts);
742 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
743 if (i == total_nb_qps)
746 if (ctx[i] && cperf_testmap[opts.test].destructor)
747 cperf_testmap[opts.test].destructor(ctx[i]);
751 for (i = 0; i < nb_cryptodevs &&
752 i < RTE_CRYPTO_MAX_DEVS; i++)
753 rte_cryptodev_stop(enabled_cdevs[i]);
754 rte_free(opts.imix_buffer_sizes);
755 free_test_vector(t_vec, &opts);