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 const char *cperf_test_type_strs[] = {
26 [CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
27 [CPERF_TEST_TYPE_LATENCY] = "latency",
28 [CPERF_TEST_TYPE_VERIFY] = "verify",
29 [CPERF_TEST_TYPE_PMDCC] = "pmd-cyclecount"
32 const char *cperf_op_type_strs[] = {
33 [CPERF_CIPHER_ONLY] = "cipher-only",
34 [CPERF_AUTH_ONLY] = "auth-only",
35 [CPERF_CIPHER_THEN_AUTH] = "cipher-then-auth",
36 [CPERF_AUTH_THEN_CIPHER] = "auth-then-cipher",
40 const struct cperf_test cperf_testmap[] = {
41 [CPERF_TEST_TYPE_THROUGHPUT] = {
42 cperf_throughput_test_constructor,
43 cperf_throughput_test_runner,
44 cperf_throughput_test_destructor
46 [CPERF_TEST_TYPE_LATENCY] = {
47 cperf_latency_test_constructor,
48 cperf_latency_test_runner,
49 cperf_latency_test_destructor
51 [CPERF_TEST_TYPE_VERIFY] = {
52 cperf_verify_test_constructor,
53 cperf_verify_test_runner,
54 cperf_verify_test_destructor
56 [CPERF_TEST_TYPE_PMDCC] = {
57 cperf_pmd_cyclecount_test_constructor,
58 cperf_pmd_cyclecount_test_runner,
59 cperf_pmd_cyclecount_test_destructor
64 cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs,
65 struct rte_mempool *session_pool_socket[])
67 uint8_t enabled_cdev_count = 0, nb_lcores, cdev_id;
68 uint32_t sessions_needed = 0;
72 enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
73 enabled_cdevs, RTE_CRYPTO_MAX_DEVS);
74 if (enabled_cdev_count == 0) {
75 printf("No crypto devices type %s available\n",
80 nb_lcores = rte_lcore_count() - 1;
84 "Number of enabled cores need to be higher than 1\n");
89 * Use less number of devices,
90 * if there are more available than cores.
92 if (enabled_cdev_count > nb_lcores)
93 enabled_cdev_count = nb_lcores;
95 /* Create a mempool shared by all the devices */
96 uint32_t max_sess_size = 0, sess_size;
98 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
99 sess_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
100 if (sess_size > max_sess_size)
101 max_sess_size = sess_size;
105 * Calculate number of needed queue pairs, based on the amount
106 * of available number of logical cores and crypto devices.
107 * For instance, if there are 4 cores and 2 crypto devices,
108 * 2 queue pairs will be set up per device.
110 opts->nb_qps = (nb_lcores % enabled_cdev_count) ?
111 (nb_lcores / enabled_cdev_count) + 1 :
112 nb_lcores / enabled_cdev_count;
114 for (i = 0; i < enabled_cdev_count &&
115 i < RTE_CRYPTO_MAX_DEVS; i++) {
116 cdev_id = enabled_cdevs[i];
117 #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
119 * If multi-core scheduler is used, limit the number
120 * of queue pairs to 1, as there is no way to know
121 * how many cores are being used by the PMD, and
122 * how many will be available for the application.
124 if (!strcmp((const char *)opts->device_type, "crypto_scheduler") &&
125 rte_cryptodev_scheduler_mode_get(cdev_id) ==
126 CDEV_SCHED_MODE_MULTICORE)
130 struct rte_cryptodev_info cdev_info;
131 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
132 /* range check the socket_id - negative values become big
133 * positive ones due to use of unsigned value
135 if (socket_id >= RTE_MAX_NUMA_NODES)
138 rte_cryptodev_info_get(cdev_id, &cdev_info);
139 if (opts->nb_qps > cdev_info.max_nb_queue_pairs) {
140 printf("Number of needed queue pairs is higher "
141 "than the maximum number of queue pairs "
143 printf("Lower the number of cores or increase "
144 "the number of crypto devices\n");
147 struct rte_cryptodev_config conf = {
148 .nb_queue_pairs = opts->nb_qps,
149 .socket_id = socket_id
152 struct rte_cryptodev_qp_conf qp_conf = {
153 .nb_descriptors = opts->nb_descriptors
157 * Device info specifies the min headroom and tailroom
158 * requirement for the crypto PMD. This need to be honoured
159 * by the application, while creating mbuf.
161 if (opts->headroom_sz < cdev_info.min_mbuf_headroom_req) {
162 /* Update headroom */
163 opts->headroom_sz = cdev_info.min_mbuf_headroom_req;
165 if (opts->tailroom_sz < cdev_info.min_mbuf_tailroom_req) {
166 /* Update tailroom */
167 opts->tailroom_sz = cdev_info.min_mbuf_tailroom_req;
170 /* Update segment size to include headroom & tailroom */
171 opts->segment_sz += (opts->headroom_sz + opts->tailroom_sz);
173 uint32_t dev_max_nb_sess = cdev_info.sym.max_nb_sessions;
175 * Two sessions objects are required for each session
176 * (one for the header, one for the private data)
178 if (!strcmp((const char *)opts->device_type,
179 "crypto_scheduler")) {
180 #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
182 rte_cryptodev_scheduler_slaves_get(cdev_id,
185 sessions_needed = 2 * enabled_cdev_count *
186 opts->nb_qps * nb_slaves;
189 sessions_needed = 2 * enabled_cdev_count *
193 * A single session is required per queue pair
196 if (dev_max_nb_sess != 0 && dev_max_nb_sess < opts->nb_qps) {
198 "Device does not support at least "
199 "%u sessions\n", opts->nb_qps);
202 if (session_pool_socket[socket_id] == NULL) {
203 char mp_name[RTE_MEMPOOL_NAMESIZE];
204 struct rte_mempool *sess_mp;
206 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
207 "sess_mp_%u", socket_id);
208 sess_mp = rte_mempool_create(mp_name,
216 if (sess_mp == NULL) {
217 printf("Cannot create session pool on socket %d\n",
222 printf("Allocated session pool on socket %d\n", socket_id);
223 session_pool_socket[socket_id] = sess_mp;
226 ret = rte_cryptodev_configure(cdev_id, &conf);
228 printf("Failed to configure cryptodev %u", cdev_id);
232 for (j = 0; j < opts->nb_qps; j++) {
233 ret = rte_cryptodev_queue_pair_setup(cdev_id, j,
235 session_pool_socket[socket_id]);
237 printf("Failed to setup queue pair %u on "
238 "cryptodev %u", j, cdev_id);
243 ret = rte_cryptodev_start(cdev_id);
245 printf("Failed to start device %u: error %d\n",
251 return enabled_cdev_count;
255 cperf_verify_devices_capabilities(struct cperf_options *opts,
256 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
258 struct rte_cryptodev_sym_capability_idx cap_idx;
259 const struct rte_cryptodev_symmetric_capability *capability;
264 for (i = 0; i < nb_cryptodevs; i++) {
266 cdev_id = enabled_cdevs[i];
268 if (opts->op_type == CPERF_AUTH_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_AUTH;
273 cap_idx.algo.auth = opts->auth_algo;
275 capability = rte_cryptodev_sym_capability_get(cdev_id,
277 if (capability == NULL)
280 ret = rte_cryptodev_sym_capability_check_auth(
289 if (opts->op_type == CPERF_CIPHER_ONLY ||
290 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
291 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
293 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
294 cap_idx.algo.cipher = opts->cipher_algo;
296 capability = rte_cryptodev_sym_capability_get(cdev_id,
298 if (capability == NULL)
301 ret = rte_cryptodev_sym_capability_check_cipher(
309 if (opts->op_type == CPERF_AEAD) {
311 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
312 cap_idx.algo.aead = opts->aead_algo;
314 capability = rte_cryptodev_sym_capability_get(cdev_id,
316 if (capability == NULL)
319 ret = rte_cryptodev_sym_capability_check_aead(
334 cperf_check_test_vector(struct cperf_options *opts,
335 struct cperf_test_vector *test_vec)
337 if (opts->op_type == CPERF_CIPHER_ONLY) {
338 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
339 if (test_vec->plaintext.data == NULL)
341 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
342 if (test_vec->plaintext.data == NULL)
344 if (test_vec->plaintext.length < opts->max_buffer_size)
346 if (test_vec->ciphertext.data == NULL)
348 if (test_vec->ciphertext.length < opts->max_buffer_size)
350 /* Cipher IV is only required for some algorithms */
351 if (opts->cipher_iv_sz &&
352 test_vec->cipher_iv.data == NULL)
354 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
356 if (test_vec->cipher_key.data == NULL)
358 if (test_vec->cipher_key.length != opts->cipher_key_sz)
361 } else if (opts->op_type == CPERF_AUTH_ONLY) {
362 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
363 if (test_vec->plaintext.data == NULL)
365 if (test_vec->plaintext.length < opts->max_buffer_size)
367 /* Auth key is only required for some algorithms */
368 if (opts->auth_key_sz &&
369 test_vec->auth_key.data == NULL)
371 if (test_vec->auth_key.length != opts->auth_key_sz)
373 if (test_vec->auth_iv.length != opts->auth_iv_sz)
375 /* Auth IV is only required for some algorithms */
376 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
378 if (test_vec->digest.data == NULL)
380 if (test_vec->digest.length < opts->digest_sz)
384 } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
385 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
386 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
387 if (test_vec->plaintext.data == NULL)
389 if (test_vec->plaintext.length < opts->max_buffer_size)
391 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
392 if (test_vec->plaintext.data == NULL)
394 if (test_vec->plaintext.length < opts->max_buffer_size)
396 if (test_vec->ciphertext.data == NULL)
398 if (test_vec->ciphertext.length < opts->max_buffer_size)
400 if (test_vec->cipher_iv.data == NULL)
402 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
404 if (test_vec->cipher_key.data == NULL)
406 if (test_vec->cipher_key.length != opts->cipher_key_sz)
409 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
410 if (test_vec->auth_key.data == NULL)
412 if (test_vec->auth_key.length != opts->auth_key_sz)
414 if (test_vec->auth_iv.length != opts->auth_iv_sz)
416 /* Auth IV is only required for some algorithms */
417 if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
419 if (test_vec->digest.data == NULL)
421 if (test_vec->digest.length < opts->digest_sz)
424 } else if (opts->op_type == CPERF_AEAD) {
425 if (test_vec->plaintext.data == NULL)
427 if (test_vec->plaintext.length < opts->max_buffer_size)
429 if (test_vec->ciphertext.data == NULL)
431 if (test_vec->ciphertext.length < opts->max_buffer_size)
433 if (test_vec->aead_key.data == NULL)
435 if (test_vec->aead_key.length != opts->aead_key_sz)
437 if (test_vec->aead_iv.data == NULL)
439 if (test_vec->aead_iv.length != opts->aead_iv_sz)
441 if (test_vec->aad.data == NULL)
443 if (test_vec->aad.length != opts->aead_aad_sz)
445 if (test_vec->digest.data == NULL)
447 if (test_vec->digest.length < opts->digest_sz)
454 main(int argc, char **argv)
456 struct cperf_options opts = {0};
457 struct cperf_test_vector *t_vec = NULL;
458 struct cperf_op_fns op_fns;
460 void *ctx[RTE_MAX_LCORE] = { };
461 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
463 int nb_cryptodevs = 0;
464 uint16_t total_nb_qps = 0;
466 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
468 uint8_t buffer_size_idx = 0;
473 /* Initialise DPDK EAL */
474 ret = rte_eal_init(argc, argv);
476 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
480 cperf_options_default(&opts);
482 ret = cperf_options_parse(&opts, argc, argv);
484 RTE_LOG(ERR, USER1, "Parsing on or more user options failed\n");
488 ret = cperf_options_check(&opts);
491 "Checking on or more user options failed\n");
495 nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs,
496 session_pool_socket);
499 cperf_options_dump(&opts);
501 if (nb_cryptodevs < 1) {
502 RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
508 ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
511 RTE_LOG(ERR, USER1, "Crypto device type does not support "
512 "capabilities requested\n");
516 if (opts.test_file != NULL) {
517 t_vec = cperf_test_vector_get_from_file(&opts);
520 "Failed to create test vector for"
521 " specified file\n");
525 if (cperf_check_test_vector(&opts, t_vec)) {
526 RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
531 t_vec = cperf_test_vector_get_dummy(&opts);
534 "Failed to create test vector for"
535 " specified algorithms\n");
540 ret = cperf_get_op_functions(&opts, &op_fns);
542 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
543 "specified algorithms combination\n");
548 show_test_vector(t_vec);
550 total_nb_qps = nb_cryptodevs * opts.nb_qps;
553 uint8_t qp_id = 0, cdev_index = 0;
554 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
556 if (i == total_nb_qps)
559 cdev_id = enabled_cdevs[cdev_index];
561 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
563 ctx[i] = cperf_testmap[opts.test].constructor(
564 session_pool_socket[socket_id], cdev_id, qp_id,
565 &opts, t_vec, &op_fns);
566 if (ctx[i] == NULL) {
567 RTE_LOG(ERR, USER1, "Test run constructor failed\n");
570 qp_id = (qp_id + 1) % opts.nb_qps;
576 if (opts.imix_distribution_count != 0) {
577 uint8_t buffer_size_count = opts.buffer_size_count;
578 uint16_t distribution_total[buffer_size_count];
580 uint32_t test_average_size = 0;
581 const uint32_t *buffer_size_list = opts.buffer_size_list;
582 const uint32_t *imix_distribution_list = opts.imix_distribution_list;
584 opts.imix_buffer_sizes = rte_malloc(NULL,
585 sizeof(uint32_t) * opts.pool_sz,
588 * Calculate accumulated distribution of
589 * probabilities per packet size
591 distribution_total[0] = imix_distribution_list[0];
592 for (i = 1; i < buffer_size_count; i++)
593 distribution_total[i] = imix_distribution_list[i] +
594 distribution_total[i-1];
596 /* Calculate a random sequence of packet sizes, based on distribution */
597 for (op_idx = 0; op_idx < opts.pool_sz; op_idx++) {
598 uint16_t random_number = rte_rand() %
599 distribution_total[buffer_size_count - 1];
600 for (i = 0; i < buffer_size_count; i++)
601 if (random_number < distribution_total[i])
604 opts.imix_buffer_sizes[op_idx] = buffer_size_list[i];
607 /* Calculate average buffer size for the IMIX distribution */
608 for (i = 0; i < buffer_size_count; i++)
609 test_average_size += buffer_size_list[i] *
610 imix_distribution_list[i];
612 opts.test_buffer_size = test_average_size /
613 distribution_total[buffer_size_count - 1];
616 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
618 if (i == total_nb_qps)
621 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
626 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
628 if (i == total_nb_qps)
630 rte_eal_wait_lcore(lcore_id);
635 /* Get next size from range or list */
636 if (opts.inc_buffer_size != 0)
637 opts.test_buffer_size = opts.min_buffer_size;
639 opts.test_buffer_size = opts.buffer_size_list[0];
641 while (opts.test_buffer_size <= opts.max_buffer_size) {
643 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
645 if (i == total_nb_qps)
648 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
653 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
655 if (i == total_nb_qps)
657 rte_eal_wait_lcore(lcore_id);
661 /* Get next size from range or list */
662 if (opts.inc_buffer_size != 0)
663 opts.test_buffer_size += opts.inc_buffer_size;
665 if (++buffer_size_idx == opts.buffer_size_count)
667 opts.test_buffer_size =
668 opts.buffer_size_list[buffer_size_idx];
674 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
676 if (i == total_nb_qps)
679 cperf_testmap[opts.test].destructor(ctx[i]);
683 for (i = 0; i < nb_cryptodevs &&
684 i < RTE_CRYPTO_MAX_DEVS; i++)
685 rte_cryptodev_stop(enabled_cdevs[i]);
687 free_test_vector(t_vec, &opts);
694 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
695 if (i == total_nb_qps)
698 if (ctx[i] && cperf_testmap[opts.test].destructor)
699 cperf_testmap[opts.test].destructor(ctx[i]);
703 for (i = 0; i < nb_cryptodevs &&
704 i < RTE_CRYPTO_MAX_DEVS; i++)
705 rte_cryptodev_stop(enabled_cdevs[i]);
706 rte_free(opts.imix_buffer_sizes);
707 free_test_vector(t_vec, &opts);