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"
24 #define SESS_MEMPOOL_CACHE_SIZE 64
26 const char *cperf_test_type_strs[] = {
27 [CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
28 [CPERF_TEST_TYPE_LATENCY] = "latency",
29 [CPERF_TEST_TYPE_VERIFY] = "verify",
30 [CPERF_TEST_TYPE_PMDCC] = "pmd-cyclecount"
33 const char *cperf_op_type_strs[] = {
34 [CPERF_CIPHER_ONLY] = "cipher-only",
35 [CPERF_AUTH_ONLY] = "auth-only",
36 [CPERF_CIPHER_THEN_AUTH] = "cipher-then-auth",
37 [CPERF_AUTH_THEN_CIPHER] = "auth-then-cipher",
41 const struct cperf_test cperf_testmap[] = {
42 [CPERF_TEST_TYPE_THROUGHPUT] = {
43 cperf_throughput_test_constructor,
44 cperf_throughput_test_runner,
45 cperf_throughput_test_destructor
47 [CPERF_TEST_TYPE_LATENCY] = {
48 cperf_latency_test_constructor,
49 cperf_latency_test_runner,
50 cperf_latency_test_destructor
52 [CPERF_TEST_TYPE_VERIFY] = {
53 cperf_verify_test_constructor,
54 cperf_verify_test_runner,
55 cperf_verify_test_destructor
57 [CPERF_TEST_TYPE_PMDCC] = {
58 cperf_pmd_cyclecount_test_constructor,
59 cperf_pmd_cyclecount_test_runner,
60 cperf_pmd_cyclecount_test_destructor
65 cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs,
66 struct rte_mempool *session_pool_socket[])
68 uint8_t enabled_cdev_count = 0, nb_lcores, cdev_id;
69 uint32_t sessions_needed = 0;
73 enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
74 enabled_cdevs, RTE_CRYPTO_MAX_DEVS);
75 if (enabled_cdev_count == 0) {
76 printf("No crypto devices type %s available\n",
81 nb_lcores = rte_lcore_count() - 1;
85 "Number of enabled cores need to be higher than 1\n");
90 * Use less number of devices,
91 * if there are more available than cores.
93 if (enabled_cdev_count > nb_lcores)
94 enabled_cdev_count = nb_lcores;
96 /* Create a mempool shared by all the devices */
97 uint32_t max_sess_size = 0, sess_size;
99 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
100 sess_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
101 if (sess_size > max_sess_size)
102 max_sess_size = sess_size;
106 * Calculate number of needed queue pairs, based on the amount
107 * of available number of logical cores and crypto devices.
108 * For instance, if there are 4 cores and 2 crypto devices,
109 * 2 queue pairs will be set up per device.
111 opts->nb_qps = (nb_lcores % enabled_cdev_count) ?
112 (nb_lcores / enabled_cdev_count) + 1 :
113 nb_lcores / enabled_cdev_count;
115 for (i = 0; i < enabled_cdev_count &&
116 i < RTE_CRYPTO_MAX_DEVS; i++) {
117 cdev_id = enabled_cdevs[i];
118 #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
120 * If multi-core scheduler is used, limit the number
121 * of queue pairs to 1, as there is no way to know
122 * how many cores are being used by the PMD, and
123 * how many will be available for the application.
125 if (!strcmp((const char *)opts->device_type, "crypto_scheduler") &&
126 rte_cryptodev_scheduler_mode_get(cdev_id) ==
127 CDEV_SCHED_MODE_MULTICORE)
131 struct rte_cryptodev_info cdev_info;
132 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
134 rte_cryptodev_info_get(cdev_id, &cdev_info);
135 if (opts->nb_qps > cdev_info.max_nb_queue_pairs) {
136 printf("Number of needed queue pairs is higher "
137 "than the maximum number of queue pairs "
139 printf("Lower the number of cores or increase "
140 "the number of crypto devices\n");
143 struct rte_cryptodev_config conf = {
144 .nb_queue_pairs = opts->nb_qps,
145 .socket_id = socket_id
148 struct rte_cryptodev_qp_conf qp_conf = {
149 .nb_descriptors = opts->nb_descriptors
152 uint32_t dev_max_nb_sess = cdev_info.sym.max_nb_sessions;
154 * Two sessions objects are required for each session
155 * (one for the header, one for the private data)
157 if (!strcmp((const char *)opts->device_type,
158 "crypto_scheduler")) {
159 #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
161 rte_cryptodev_scheduler_slaves_get(cdev_id,
164 sessions_needed = 2 * enabled_cdev_count *
165 opts->nb_qps * nb_slaves;
168 sessions_needed = 2 * enabled_cdev_count *
172 * A single session is required per queue pair
175 if (dev_max_nb_sess < opts->nb_qps) {
177 "Device does not support at least "
178 "%u sessions\n", opts->nb_qps);
181 if (session_pool_socket[socket_id] == NULL) {
182 char mp_name[RTE_MEMPOOL_NAMESIZE];
183 struct rte_mempool *sess_mp;
185 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
186 "sess_mp_%u", socket_id);
187 sess_mp = rte_mempool_create(mp_name,
190 SESS_MEMPOOL_CACHE_SIZE,
195 if (sess_mp == NULL) {
196 printf("Cannot create session pool on socket %d\n",
201 printf("Allocated session pool on socket %d\n", socket_id);
202 session_pool_socket[socket_id] = sess_mp;
205 ret = rte_cryptodev_configure(cdev_id, &conf);
207 printf("Failed to configure cryptodev %u", cdev_id);
211 for (j = 0; j < opts->nb_qps; j++) {
212 ret = rte_cryptodev_queue_pair_setup(cdev_id, j,
214 session_pool_socket[socket_id]);
216 printf("Failed to setup queue pair %u on "
217 "cryptodev %u", j, cdev_id);
222 ret = rte_cryptodev_start(cdev_id);
224 printf("Failed to start device %u: error %d\n",
230 return enabled_cdev_count;
234 cperf_verify_devices_capabilities(struct cperf_options *opts,
235 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
237 struct rte_cryptodev_sym_capability_idx cap_idx;
238 const struct rte_cryptodev_symmetric_capability *capability;
243 for (i = 0; i < nb_cryptodevs; i++) {
245 cdev_id = enabled_cdevs[i];
247 if (opts->op_type == CPERF_AUTH_ONLY ||
248 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
249 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
251 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
252 cap_idx.algo.auth = opts->auth_algo;
254 capability = rte_cryptodev_sym_capability_get(cdev_id,
256 if (capability == NULL)
259 ret = rte_cryptodev_sym_capability_check_auth(
268 if (opts->op_type == CPERF_CIPHER_ONLY ||
269 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
270 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
272 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
273 cap_idx.algo.cipher = opts->cipher_algo;
275 capability = rte_cryptodev_sym_capability_get(cdev_id,
277 if (capability == NULL)
280 ret = rte_cryptodev_sym_capability_check_cipher(
288 if (opts->op_type == CPERF_AEAD) {
290 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
291 cap_idx.algo.aead = opts->aead_algo;
293 capability = rte_cryptodev_sym_capability_get(cdev_id,
295 if (capability == NULL)
298 ret = rte_cryptodev_sym_capability_check_aead(
313 cperf_check_test_vector(struct cperf_options *opts,
314 struct cperf_test_vector *test_vec)
316 if (opts->op_type == CPERF_CIPHER_ONLY) {
317 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
318 if (test_vec->plaintext.data == NULL)
320 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
321 if (test_vec->plaintext.data == NULL)
323 if (test_vec->plaintext.length < opts->max_buffer_size)
325 if (test_vec->ciphertext.data == NULL)
327 if (test_vec->ciphertext.length < opts->max_buffer_size)
329 if (test_vec->cipher_iv.data == NULL)
331 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
333 if (test_vec->cipher_key.data == NULL)
335 if (test_vec->cipher_key.length != opts->cipher_key_sz)
338 } else if (opts->op_type == CPERF_AUTH_ONLY) {
339 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
340 if (test_vec->plaintext.data == NULL)
342 if (test_vec->plaintext.length < opts->max_buffer_size)
344 if (test_vec->auth_key.data == NULL)
346 if (test_vec->auth_key.length != opts->auth_key_sz)
348 if (test_vec->auth_iv.length != opts->auth_iv_sz)
350 /* Auth IV is only required for some algorithms */
351 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
353 if (test_vec->digest.data == NULL)
355 if (test_vec->digest.length < opts->digest_sz)
359 } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
360 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
361 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
362 if (test_vec->plaintext.data == NULL)
364 if (test_vec->plaintext.length < opts->max_buffer_size)
366 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
367 if (test_vec->plaintext.data == NULL)
369 if (test_vec->plaintext.length < opts->max_buffer_size)
371 if (test_vec->ciphertext.data == NULL)
373 if (test_vec->ciphertext.length < opts->max_buffer_size)
375 if (test_vec->cipher_iv.data == NULL)
377 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
379 if (test_vec->cipher_key.data == NULL)
381 if (test_vec->cipher_key.length != opts->cipher_key_sz)
384 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
385 if (test_vec->auth_key.data == NULL)
387 if (test_vec->auth_key.length != opts->auth_key_sz)
389 if (test_vec->auth_iv.length != opts->auth_iv_sz)
391 /* Auth IV is only required for some algorithms */
392 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
394 if (test_vec->digest.data == NULL)
396 if (test_vec->digest.length < opts->digest_sz)
399 } else if (opts->op_type == CPERF_AEAD) {
400 if (test_vec->plaintext.data == NULL)
402 if (test_vec->plaintext.length < opts->max_buffer_size)
404 if (test_vec->ciphertext.data == NULL)
406 if (test_vec->ciphertext.length < opts->max_buffer_size)
408 if (test_vec->aead_iv.data == NULL)
410 if (test_vec->aead_iv.length != opts->aead_iv_sz)
412 if (test_vec->aad.data == NULL)
414 if (test_vec->aad.length != opts->aead_aad_sz)
416 if (test_vec->digest.data == NULL)
418 if (test_vec->digest.length < opts->digest_sz)
425 main(int argc, char **argv)
427 struct cperf_options opts = {0};
428 struct cperf_test_vector *t_vec = NULL;
429 struct cperf_op_fns op_fns;
431 void *ctx[RTE_MAX_LCORE] = { };
432 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
434 int nb_cryptodevs = 0;
435 uint16_t total_nb_qps = 0;
437 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
439 uint8_t buffer_size_idx = 0;
444 /* Initialise DPDK EAL */
445 ret = rte_eal_init(argc, argv);
447 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
451 cperf_options_default(&opts);
453 ret = cperf_options_parse(&opts, argc, argv);
455 RTE_LOG(ERR, USER1, "Parsing on or more user options failed\n");
459 ret = cperf_options_check(&opts);
462 "Checking on or more user options failed\n");
466 nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs,
467 session_pool_socket);
470 cperf_options_dump(&opts);
472 if (nb_cryptodevs < 1) {
473 RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
479 ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
482 RTE_LOG(ERR, USER1, "Crypto device type does not support "
483 "capabilities requested\n");
487 if (opts.test_file != NULL) {
488 t_vec = cperf_test_vector_get_from_file(&opts);
491 "Failed to create test vector for"
492 " specified file\n");
496 if (cperf_check_test_vector(&opts, t_vec)) {
497 RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
502 t_vec = cperf_test_vector_get_dummy(&opts);
505 "Failed to create test vector for"
506 " specified algorithms\n");
511 ret = cperf_get_op_functions(&opts, &op_fns);
513 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
514 "specified algorithms combination\n");
519 show_test_vector(t_vec);
521 total_nb_qps = nb_cryptodevs * opts.nb_qps;
524 uint8_t qp_id = 0, cdev_index = 0;
525 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
527 if (i == total_nb_qps)
530 cdev_id = enabled_cdevs[cdev_index];
532 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
534 ctx[i] = cperf_testmap[opts.test].constructor(
535 session_pool_socket[socket_id], cdev_id, qp_id,
536 &opts, t_vec, &op_fns);
537 if (ctx[i] == NULL) {
538 RTE_LOG(ERR, USER1, "Test run constructor failed\n");
541 qp_id = (qp_id + 1) % opts.nb_qps;
547 if (opts.imix_distribution_count != 0) {
548 uint8_t buffer_size_count = opts.buffer_size_count;
549 uint16_t distribution_total[buffer_size_count];
551 uint32_t test_average_size = 0;
552 const uint32_t *buffer_size_list = opts.buffer_size_list;
553 const uint32_t *imix_distribution_list = opts.imix_distribution_list;
555 opts.imix_buffer_sizes = rte_malloc(NULL,
556 sizeof(uint32_t) * opts.pool_sz,
559 * Calculate accumulated distribution of
560 * probabilities per packet size
562 distribution_total[0] = imix_distribution_list[0];
563 for (i = 1; i < buffer_size_count; i++)
564 distribution_total[i] = imix_distribution_list[i] +
565 distribution_total[i-1];
567 /* Calculate a random sequence of packet sizes, based on distribution */
568 for (op_idx = 0; op_idx < opts.pool_sz; op_idx++) {
569 uint16_t random_number = rte_rand() %
570 distribution_total[buffer_size_count - 1];
571 for (i = 0; i < buffer_size_count; i++)
572 if (random_number < distribution_total[i])
575 opts.imix_buffer_sizes[op_idx] = buffer_size_list[i];
578 /* Calculate average buffer size for the IMIX distribution */
579 for (i = 0; i < buffer_size_count; i++)
580 test_average_size += buffer_size_list[i] *
581 imix_distribution_list[i];
583 opts.test_buffer_size = test_average_size /
584 distribution_total[buffer_size_count - 1];
587 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
589 if (i == total_nb_qps)
592 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
597 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
599 if (i == total_nb_qps)
601 rte_eal_wait_lcore(lcore_id);
606 /* Get next size from range or list */
607 if (opts.inc_buffer_size != 0)
608 opts.test_buffer_size = opts.min_buffer_size;
610 opts.test_buffer_size = opts.buffer_size_list[0];
612 while (opts.test_buffer_size <= opts.max_buffer_size) {
614 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
616 if (i == total_nb_qps)
619 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
624 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
626 if (i == total_nb_qps)
628 rte_eal_wait_lcore(lcore_id);
632 /* Get next size from range or list */
633 if (opts.inc_buffer_size != 0)
634 opts.test_buffer_size += opts.inc_buffer_size;
636 if (++buffer_size_idx == opts.buffer_size_count)
638 opts.test_buffer_size =
639 opts.buffer_size_list[buffer_size_idx];
645 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
647 if (i == total_nb_qps)
650 cperf_testmap[opts.test].destructor(ctx[i]);
654 for (i = 0; i < nb_cryptodevs &&
655 i < RTE_CRYPTO_MAX_DEVS; i++)
656 rte_cryptodev_stop(enabled_cdevs[i]);
658 free_test_vector(t_vec, &opts);
665 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
666 if (i == total_nb_qps)
669 if (ctx[i] && cperf_testmap[opts.test].destructor)
670 cperf_testmap[opts.test].destructor(ctx[i]);
674 for (i = 0; i < nb_cryptodevs &&
675 i < RTE_CRYPTO_MAX_DEVS; i++)
676 rte_cryptodev_stop(enabled_cdevs[i]);
677 rte_free(opts.imix_buffer_sizes);
678 free_test_vector(t_vec, &opts);