Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016-2017 Intel Corporation
3 : : */
4 : :
5 : : #include <stdio.h>
6 : : #include <stdlib.h>
7 : : #include <unistd.h>
8 : :
9 : : #include <rte_malloc.h>
10 : : #include <rte_random.h>
11 : : #include <rte_eal.h>
12 : : #include <rte_errno.h>
13 : : #include <rte_cryptodev.h>
14 : : #ifdef RTE_CRYPTO_SCHEDULER
15 : : #include <rte_cryptodev_scheduler.h>
16 : : #endif
17 : :
18 : : #include "cperf.h"
19 : : #include "cperf_options.h"
20 : : #include "cperf_test_vector_parsing.h"
21 : : #include "cperf_test_common.h"
22 : : #include "cperf_test_throughput.h"
23 : : #include "cperf_test_latency.h"
24 : : #include "cperf_test_verify.h"
25 : : #include "cperf_test_pmd_cyclecount.h"
26 : :
27 : : static struct {
28 : : struct rte_mempool *sess_mp;
29 : : } session_pool_socket[RTE_MAX_NUMA_NODES];
30 : :
31 : : const char *cperf_test_type_strs[] = {
32 : : [CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
33 : : [CPERF_TEST_TYPE_LATENCY] = "latency",
34 : : [CPERF_TEST_TYPE_VERIFY] = "verify",
35 : : [CPERF_TEST_TYPE_PMDCC] = "pmd-cyclecount"
36 : : };
37 : :
38 : : const char *cperf_op_type_strs[] = {
39 : : [CPERF_CIPHER_ONLY] = "cipher-only",
40 : : [CPERF_AUTH_ONLY] = "auth-only",
41 : : [CPERF_CIPHER_THEN_AUTH] = "cipher-then-auth",
42 : : [CPERF_AUTH_THEN_CIPHER] = "auth-then-cipher",
43 : : [CPERF_AEAD] = "aead",
44 : : [CPERF_PDCP] = "pdcp",
45 : : [CPERF_DOCSIS] = "docsis",
46 : : [CPERF_IPSEC] = "ipsec",
47 : : [CPERF_ASYM_MODEX] = "modex",
48 : : [CPERF_ASYM_RSA] = "rsa",
49 : : [CPERF_ASYM_SECP192R1] = "ecdsa_p192r1",
50 : : [CPERF_ASYM_SECP224R1] = "ecdsa_p224r1",
51 : : [CPERF_ASYM_SECP256R1] = "ecdsa_p256r1",
52 : : [CPERF_ASYM_SECP384R1] = "ecdsa_p384r1",
53 : : [CPERF_ASYM_SECP521R1] = "ecdsa_p521r1",
54 : : [CPERF_ASYM_ED25519] = "eddsa_25519",
55 : : [CPERF_ASYM_SM2] = "sm2",
56 : : [CPERF_TLS] = "tls-record",
57 : : [CPERF_ASYM_MLKEM512] = "mlkem_512",
58 : : [CPERF_ASYM_MLDSA44] = "mldsa_44",
59 : : };
60 : :
61 : : const char *cperf_rsa_priv_keytype_strs[] = {
62 : : [RTE_RSA_KEY_TYPE_EXP] = "exp",
63 : : [RTE_RSA_KEY_TYPE_QT] = "qt",
64 : : };
65 : :
66 : : const struct cperf_test cperf_testmap[] = {
67 : : [CPERF_TEST_TYPE_THROUGHPUT] = {
68 : : cperf_throughput_test_constructor,
69 : : cperf_throughput_test_runner,
70 : : cperf_throughput_test_destructor
71 : : },
72 : : [CPERF_TEST_TYPE_LATENCY] = {
73 : : cperf_latency_test_constructor,
74 : : cperf_latency_test_runner,
75 : : cperf_latency_test_destructor
76 : : },
77 : : [CPERF_TEST_TYPE_VERIFY] = {
78 : : cperf_verify_test_constructor,
79 : : cperf_verify_test_runner,
80 : : cperf_verify_test_destructor
81 : : },
82 : : [CPERF_TEST_TYPE_PMDCC] = {
83 : : cperf_pmd_cyclecount_test_constructor,
84 : : cperf_pmd_cyclecount_test_runner,
85 : : cperf_pmd_cyclecount_test_destructor
86 : : }
87 : : };
88 : :
89 : : static int
90 : 0 : create_asym_op_pool_socket(int32_t socket_id, uint32_t nb_sessions)
91 : : {
92 : : char mp_name[RTE_MEMPOOL_NAMESIZE];
93 : : struct rte_mempool *mpool = NULL;
94 : :
95 : 0 : if (session_pool_socket[socket_id].sess_mp == NULL) {
96 : : snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "perf_asym_sess_pool%u",
97 : : socket_id);
98 : 0 : mpool = rte_cryptodev_asym_session_pool_create(mp_name,
99 : : nb_sessions, 0, 0, socket_id);
100 : 0 : if (mpool == NULL) {
101 : : printf("Cannot create pool \"%s\" on socket %d\n",
102 : : mp_name, socket_id);
103 : 0 : return -ENOMEM;
104 : : }
105 : 0 : session_pool_socket[socket_id].sess_mp = mpool;
106 : : }
107 : : return 0;
108 : : }
109 : :
110 : : static int
111 : 0 : fill_session_pool_socket(int32_t socket_id, uint32_t session_priv_size,
112 : : uint32_t nb_sessions)
113 : : {
114 : : char mp_name[RTE_MEMPOOL_NAMESIZE];
115 : : struct rte_mempool *sess_mp;
116 : :
117 : 0 : if (session_pool_socket[socket_id].sess_mp == NULL) {
118 : :
119 : : snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
120 : : "sess_mp_%u", socket_id);
121 : :
122 : 0 : sess_mp = rte_cryptodev_sym_session_pool_create(mp_name,
123 : : nb_sessions, session_priv_size, 0, 0,
124 : : socket_id);
125 : :
126 : 0 : if (sess_mp == NULL) {
127 : : printf("Cannot create pool \"%s\" on socket %d\n",
128 : : mp_name, socket_id);
129 : 0 : return -ENOMEM;
130 : : }
131 : :
132 : : printf("Allocated pool \"%s\" on socket %d\n",
133 : : mp_name, socket_id);
134 : 0 : session_pool_socket[socket_id].sess_mp = sess_mp;
135 : : }
136 : :
137 : : return 0;
138 : : }
139 : :
140 : : static int
141 : 0 : cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs)
142 : : {
143 : : uint8_t enabled_cdev_count = 0, nb_lcores, cdev_id;
144 : : uint32_t sessions_needed = 0;
145 : : unsigned int i, j;
146 : : int ret;
147 : :
148 : 0 : enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
149 : : enabled_cdevs, RTE_CRYPTO_MAX_DEVS);
150 : 0 : if (enabled_cdev_count == 0) {
151 : : printf("No crypto devices type %s available\n",
152 : : opts->device_type);
153 : 0 : return -EINVAL;
154 : : }
155 : :
156 : 0 : nb_lcores = rte_lcore_count() - 1;
157 : :
158 : 0 : if (nb_lcores < 1) {
159 : 0 : RTE_LOG(ERR, USER1,
160 : : "Number of enabled cores need to be higher than 1\n");
161 : 0 : return -EINVAL;
162 : : }
163 : :
164 : : /*
165 : : * Use less number of devices,
166 : : * if there are more available than cores.
167 : : */
168 : : if (enabled_cdev_count > nb_lcores)
169 : : enabled_cdev_count = nb_lcores;
170 : :
171 : : /* Create a mempool shared by all the devices */
172 : : uint32_t max_sess_size = 0, sess_size;
173 : :
174 : 0 : for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
175 : 0 : sess_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
176 : : if (sess_size > max_sess_size)
177 : : max_sess_size = sess_size;
178 : : }
179 : : #ifdef RTE_LIB_SECURITY
180 : 0 : for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
181 : 0 : sess_size = rte_security_session_get_size(
182 : : rte_cryptodev_get_sec_ctx(cdev_id));
183 : : if (sess_size > max_sess_size)
184 : : max_sess_size = sess_size;
185 : : }
186 : : #endif
187 : : /*
188 : : * Calculate number of needed queue pairs, based on the amount
189 : : * of available number of logical cores and crypto devices.
190 : : * For instance, if there are 4 cores and 2 crypto devices,
191 : : * 2 queue pairs will be set up per device.
192 : : */
193 : 0 : opts->nb_qps = (nb_lcores % enabled_cdev_count) ?
194 : 0 : (nb_lcores / enabled_cdev_count) + 1 :
195 : : nb_lcores / enabled_cdev_count;
196 : :
197 : 0 : for (i = 0; i < enabled_cdev_count &&
198 : 0 : i < RTE_CRYPTO_MAX_DEVS; i++) {
199 : 0 : cdev_id = enabled_cdevs[i];
200 : : #ifdef RTE_CRYPTO_SCHEDULER
201 : : /*
202 : : * If multi-core scheduler is used, limit the number
203 : : * of queue pairs to 1, as there is no way to know
204 : : * how many cores are being used by the PMD, and
205 : : * how many will be available for the application.
206 : : */
207 : 0 : if (!strcmp((const char *)opts->device_type, "crypto_scheduler") &&
208 : 0 : rte_cryptodev_scheduler_mode_get(cdev_id) ==
209 : : CDEV_SCHED_MODE_MULTICORE)
210 : 0 : opts->nb_qps = 1;
211 : : #endif
212 : :
213 : : struct rte_cryptodev_info cdev_info;
214 : 0 : int socket_id = rte_cryptodev_socket_id(cdev_id);
215 : :
216 : : /* Use the first socket if SOCKET_ID_ANY is returned. */
217 : 0 : if (socket_id == SOCKET_ID_ANY)
218 : : socket_id = 0;
219 : :
220 : 0 : rte_cryptodev_info_get(cdev_id, &cdev_info);
221 : :
222 : 0 : if (cperf_is_asym_test(opts)) {
223 : 0 : if ((cdev_info.feature_flags &
224 : : RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) == 0)
225 : 0 : continue;
226 : : }
227 : :
228 : 0 : if (opts->nb_qps > cdev_info.max_nb_queue_pairs) {
229 : : printf("Number of needed queue pairs is higher "
230 : : "than the maximum number of queue pairs "
231 : : "per device.\n");
232 : : printf("Lower the number of cores or increase "
233 : : "the number of crypto devices\n");
234 : 0 : return -EINVAL;
235 : : }
236 : 0 : struct rte_cryptodev_config conf = {
237 : : .nb_queue_pairs = opts->nb_qps,
238 : : .socket_id = socket_id,
239 : : };
240 : :
241 : 0 : switch (opts->op_type) {
242 : 0 : case CPERF_ASYM_SECP192R1:
243 : : case CPERF_ASYM_SECP224R1:
244 : : case CPERF_ASYM_SECP256R1:
245 : : case CPERF_ASYM_SECP384R1:
246 : : case CPERF_ASYM_SECP521R1:
247 : : case CPERF_ASYM_ED25519:
248 : : case CPERF_ASYM_SM2:
249 : : case CPERF_ASYM_RSA:
250 : : case CPERF_ASYM_MLKEM512:
251 : : case CPERF_ASYM_MLDSA44:
252 : : case CPERF_ASYM_MODEX:
253 : 0 : conf.ff_disable |= (RTE_CRYPTODEV_FF_SECURITY |
254 : : RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO);
255 : 0 : break;
256 : 0 : case CPERF_CIPHER_ONLY:
257 : : case CPERF_AUTH_ONLY:
258 : : case CPERF_CIPHER_THEN_AUTH:
259 : : case CPERF_AUTH_THEN_CIPHER:
260 : : case CPERF_AEAD:
261 : 0 : conf.ff_disable |= RTE_CRYPTODEV_FF_SECURITY;
262 : : /* Fall through */
263 : 0 : case CPERF_PDCP:
264 : : case CPERF_DOCSIS:
265 : : case CPERF_IPSEC:
266 : : case CPERF_TLS:
267 : : /* Fall through */
268 : : default:
269 : 0 : conf.ff_disable |= RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
270 : : }
271 : :
272 : 0 : struct rte_cryptodev_qp_conf qp_conf = {
273 : 0 : .nb_descriptors = opts->nb_descriptors,
274 : : .priority = RTE_CRYPTODEV_QP_PRIORITY_HIGHEST
275 : : };
276 : :
277 : : /**
278 : : * Device info specifies the min headroom and tailroom
279 : : * requirement for the crypto PMD. This need to be honoured
280 : : * by the application, while creating mbuf.
281 : : */
282 : 0 : if (opts->headroom_sz < cdev_info.min_mbuf_headroom_req) {
283 : : /* Update headroom */
284 : 0 : opts->headroom_sz = cdev_info.min_mbuf_headroom_req;
285 : : }
286 : 0 : if (opts->tailroom_sz < cdev_info.min_mbuf_tailroom_req) {
287 : : /* Update tailroom */
288 : 0 : opts->tailroom_sz = cdev_info.min_mbuf_tailroom_req;
289 : : }
290 : :
291 : : /* Update segment size to include headroom & tailroom */
292 : 0 : opts->segment_sz += (opts->headroom_sz + opts->tailroom_sz);
293 : :
294 : 0 : uint32_t dev_max_nb_sess = cdev_info.sym.max_nb_sessions;
295 : 0 : if (!strcmp((const char *)opts->device_type,
296 : : "crypto_scheduler")) {
297 : : #ifdef RTE_CRYPTO_SCHEDULER
298 : 0 : uint32_t nb_workers =
299 : 0 : rte_cryptodev_scheduler_workers_get(cdev_id,
300 : : NULL);
301 : : /* scheduler session header per lcore + 1 session per worker qp */
302 : 0 : sessions_needed = nb_lcores + enabled_cdev_count *
303 : 0 : opts->nb_qps * nb_workers;
304 : : #endif
305 : : } else
306 : 0 : sessions_needed = enabled_cdev_count * opts->nb_qps;
307 : :
308 : : /*
309 : : * A single session is required per queue pair
310 : : * in each device
311 : : */
312 : 0 : if (dev_max_nb_sess != 0 && dev_max_nb_sess < opts->nb_qps) {
313 : 0 : RTE_LOG(ERR, USER1,
314 : : "Device does not support at least "
315 : : "%u sessions\n", opts->nb_qps);
316 : 0 : return -ENOTSUP;
317 : : }
318 : :
319 : 0 : if (cperf_is_asym_test(opts))
320 : 0 : ret = create_asym_op_pool_socket(socket_id,
321 : : sessions_needed);
322 : : else
323 : 0 : ret = fill_session_pool_socket(socket_id, max_sess_size,
324 : : sessions_needed);
325 : 0 : if (ret < 0)
326 : 0 : return ret;
327 : :
328 : 0 : qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
329 : :
330 : 0 : if (cperf_is_asym_test(opts))
331 : 0 : qp_conf.mp_session = NULL;
332 : :
333 : 0 : ret = rte_cryptodev_configure(cdev_id, &conf);
334 : 0 : if (ret < 0) {
335 : : printf("Failed to configure cryptodev %u", cdev_id);
336 : 0 : return -EINVAL;
337 : : }
338 : :
339 : 0 : for (j = 0; j < opts->nb_qps; j++) {
340 : 0 : if ((1 << j) & opts->low_prio_qp_mask)
341 : 0 : qp_conf.priority = RTE_CRYPTODEV_QP_PRIORITY_LOWEST;
342 : :
343 : 0 : ret = rte_cryptodev_queue_pair_setup(cdev_id, j,
344 : : &qp_conf, socket_id);
345 : 0 : if (ret < 0) {
346 : : printf("Failed to setup queue pair %u on "
347 : : "cryptodev %u", j, cdev_id);
348 : 0 : return -EINVAL;
349 : : }
350 : : }
351 : :
352 : 0 : ret = rte_cryptodev_start(cdev_id);
353 : 0 : if (ret < 0) {
354 : : printf("Failed to start device %u: error %d\n",
355 : : cdev_id, ret);
356 : 0 : return -EPERM;
357 : : }
358 : : }
359 : :
360 : 0 : return enabled_cdev_count;
361 : : }
362 : :
363 : : static void
364 : : set_ecdsa_key_null(struct cperf_ecdsa_test_data *curve_data)
365 : : {
366 : 0 : if (curve_data != NULL) {
367 : 0 : curve_data->k.data = NULL;
368 : 0 : curve_data->k.length = 0;
369 : : }
370 : : }
371 : :
372 : : static int
373 : 0 : cperf_verify_devices_capabilities(struct cperf_options *opts,
374 : : uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
375 : : {
376 : : struct rte_cryptodev_sym_capability_idx cap_idx;
377 : : const struct rte_cryptodev_symmetric_capability *capability;
378 : : struct rte_cryptodev_asym_capability_idx asym_cap_idx;
379 : : const struct rte_cryptodev_asymmetric_xform_capability *asym_capability;
380 : :
381 : :
382 : : uint8_t i, cdev_id;
383 : : int ret;
384 : :
385 : 0 : for (i = 0; i < nb_cryptodevs; i++) {
386 : :
387 : 0 : cdev_id = enabled_cdevs[i];
388 : :
389 : 0 : if (opts->op_type == CPERF_ASYM_MODEX) {
390 : 0 : asym_cap_idx.type = RTE_CRYPTO_ASYM_XFORM_MODEX;
391 : 0 : asym_capability = rte_cryptodev_asym_capability_get(
392 : : cdev_id, &asym_cap_idx);
393 : 0 : if (asym_capability == NULL)
394 : : return -1;
395 : :
396 : 0 : ret = rte_cryptodev_asym_xform_capability_check_modlen(
397 : 0 : asym_capability, opts->modex_data->modulus.len);
398 : 0 : if (ret != 0)
399 : 0 : return ret;
400 : :
401 : : }
402 : :
403 : 0 : if (opts->op_type == CPERF_ASYM_RSA) {
404 : 0 : asym_cap_idx.type = RTE_CRYPTO_ASYM_XFORM_RSA;
405 : 0 : asym_capability = rte_cryptodev_asym_capability_get(cdev_id, &asym_cap_idx);
406 : 0 : if (asym_capability == NULL)
407 : : return -1;
408 : :
409 : 0 : if (!rte_cryptodev_asym_xform_capability_check_optype(asym_capability,
410 : : opts->asym_op_type))
411 : : return -1;
412 : :
413 : : }
414 : :
415 : 0 : if ((opts->op_type == CPERF_ASYM_SECP192R1) ||
416 : : (opts->op_type == CPERF_ASYM_SECP224R1) ||
417 : : (opts->op_type == CPERF_ASYM_SECP256R1) ||
418 : 0 : (opts->op_type == CPERF_ASYM_SECP384R1) ||
419 : : (opts->op_type == CPERF_ASYM_SECP521R1)) {
420 : 0 : asym_cap_idx.type = RTE_CRYPTO_ASYM_XFORM_ECDSA;
421 : 0 : asym_capability = rte_cryptodev_asym_capability_get(cdev_id, &asym_cap_idx);
422 : 0 : if (asym_capability == NULL)
423 : : return -1;
424 : :
425 : 0 : if (!rte_cryptodev_asym_xform_capability_check_optype(asym_capability,
426 : : opts->asym_op_type))
427 : : return -1;
428 : :
429 : 0 : if (asym_capability->internal_rng != 0) {
430 : 0 : switch (opts->op_type) {
431 : 0 : case CPERF_ASYM_SECP192R1:
432 : 0 : set_ecdsa_key_null(opts->secp192r1_data);
433 : : break;
434 : 0 : case CPERF_ASYM_SECP224R1:
435 : 0 : set_ecdsa_key_null(opts->secp224r1_data);
436 : : break;
437 : 0 : case CPERF_ASYM_SECP256R1:
438 : 0 : set_ecdsa_key_null(opts->secp256r1_data);
439 : : break;
440 : 0 : case CPERF_ASYM_SECP384R1:
441 : 0 : set_ecdsa_key_null(opts->secp384r1_data);
442 : : break;
443 : 0 : case CPERF_ASYM_SECP521R1:
444 : 0 : set_ecdsa_key_null(opts->secp521r1_data);
445 : : break;
446 : : default:
447 : : break;
448 : : }
449 : : }
450 : : }
451 : :
452 : 0 : if (opts->op_type == CPERF_ASYM_ED25519) {
453 : 0 : asym_cap_idx.type = RTE_CRYPTO_ASYM_XFORM_EDDSA;
454 : 0 : asym_capability = rte_cryptodev_asym_capability_get(cdev_id, &asym_cap_idx);
455 : 0 : if (asym_capability == NULL)
456 : : return -1;
457 : :
458 : 0 : if (!rte_cryptodev_asym_xform_capability_check_optype(asym_capability,
459 : : opts->asym_op_type))
460 : : return -1;
461 : : }
462 : :
463 : 0 : if (opts->op_type == CPERF_ASYM_SM2) {
464 : 0 : asym_cap_idx.type = RTE_CRYPTO_ASYM_XFORM_SM2;
465 : 0 : asym_capability = rte_cryptodev_asym_capability_get(cdev_id, &asym_cap_idx);
466 : 0 : if (asym_capability == NULL)
467 : : return -1;
468 : :
469 : 0 : if (!rte_cryptodev_asym_xform_capability_check_optype(asym_capability,
470 : : opts->asym_op_type))
471 : : return -1;
472 : :
473 : 0 : if (rte_cryptodev_asym_xform_capability_check_hash(asym_capability,
474 : : RTE_CRYPTO_AUTH_SM3)) {
475 : 0 : opts->asym_hash_alg = RTE_CRYPTO_AUTH_SM3;
476 : 0 : if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_SIGN ||
477 : : opts->asym_op_type == RTE_CRYPTO_ASYM_OP_VERIFY) {
478 : 0 : opts->sm2_data->message.data = sm2_perf_data.message.data;
479 : 0 : opts->sm2_data->message.length =
480 : 0 : sm2_perf_data.message.length;
481 : 0 : opts->sm2_data->id.data = sm2_perf_data.id.data;
482 : 0 : opts->sm2_data->id.length = sm2_perf_data.id.length;
483 : : }
484 : : } else {
485 : 0 : opts->asym_hash_alg = RTE_CRYPTO_AUTH_NULL;
486 : 0 : if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_SIGN ||
487 : : opts->asym_op_type == RTE_CRYPTO_ASYM_OP_VERIFY) {
488 : 0 : opts->sm2_data->message.data = sm2_perf_data.digest.data;
489 : 0 : opts->sm2_data->message.length =
490 : 0 : sm2_perf_data.digest.length;
491 : 0 : opts->sm2_data->id.data = NULL;
492 : 0 : opts->sm2_data->id.length = 0;
493 : : }
494 : : }
495 : 0 : if (asym_capability->internal_rng != 0) {
496 : 0 : opts->sm2_data->k.data = NULL;
497 : 0 : opts->sm2_data->k.length = 0;
498 : : }
499 : 0 : if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_ENCRYPT) {
500 : 0 : opts->sm2_data->message.data = sm2_perf_data.message.data;
501 : 0 : opts->sm2_data->message.length = sm2_perf_data.message.length;
502 : 0 : opts->sm2_data->cipher.data = sm2_perf_data.cipher.data;
503 : 0 : opts->sm2_data->cipher.length = sm2_perf_data.cipher.length;
504 : 0 : } else if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_DECRYPT) {
505 : 0 : opts->sm2_data->cipher.data = sm2_perf_data.cipher.data;
506 : 0 : opts->sm2_data->cipher.length = sm2_perf_data.cipher.length;
507 : 0 : opts->sm2_data->message.data = sm2_perf_data.message.data;
508 : 0 : opts->sm2_data->message.length = sm2_perf_data.message.length;
509 : 0 : } else if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_SIGN) {
510 : 0 : opts->sm2_data->sign_r.data = sm2_perf_data.sign_r.data;
511 : 0 : opts->sm2_data->sign_r.length = sm2_perf_data.sign_r.length;
512 : 0 : opts->sm2_data->sign_s.data = sm2_perf_data.sign_s.data;
513 : 0 : opts->sm2_data->sign_s.length = sm2_perf_data.sign_s.length;
514 : 0 : } else if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_VERIFY) {
515 : 0 : opts->sm2_data->sign_r.data = sm2_perf_data.sign_r.data;
516 : 0 : opts->sm2_data->sign_r.length = sm2_perf_data.sign_r.length;
517 : 0 : opts->sm2_data->sign_s.data = sm2_perf_data.sign_s.data;
518 : 0 : opts->sm2_data->sign_s.length = sm2_perf_data.sign_s.length;
519 : : }
520 : : }
521 : 0 : if (opts->op_type == CPERF_ASYM_MLDSA44) {
522 : 0 : asym_cap_idx.type = RTE_CRYPTO_ASYM_XFORM_ML_DSA;
523 : 0 : asym_capability = rte_cryptodev_asym_capability_get(cdev_id, &asym_cap_idx);
524 : 0 : if (asym_capability == NULL)
525 : : return -1;
526 : :
527 : 0 : if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_SIGN)
528 : 0 : opts->mldsa_data = &mldsa_sign_perf_data[0];
529 : 0 : else if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
530 : 0 : opts->mldsa_data = &mldsa_verify_perf_data[0];
531 : : else {
532 : 0 : RTE_LOG(ERR, USER1, "Unsupported MLDSA operation type\n");
533 : 0 : return -ENOTSUP;
534 : : }
535 : : }
536 : :
537 : 0 : if (opts->op_type == CPERF_ASYM_MLKEM512) {
538 : 0 : asym_cap_idx.type = RTE_CRYPTO_ASYM_XFORM_ML_KEM;
539 : 0 : asym_capability = rte_cryptodev_asym_capability_get(cdev_id, &asym_cap_idx);
540 : 0 : if (asym_capability == NULL)
541 : : return -1;
542 : :
543 : 0 : if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_ENCRYPT)
544 : 0 : opts->mlkem_data = &mlkem_encap_perf_data[0];
545 : 0 : else if (opts->asym_op_type == RTE_CRYPTO_ASYM_OP_DECRYPT)
546 : 0 : opts->mlkem_data = &mlkem_decap_perf_data[0];
547 : : else {
548 : 0 : RTE_LOG(ERR, USER1, "Unsupported MLKEM operation type\n");
549 : 0 : return -ENOTSUP;
550 : : }
551 : :
552 : : }
553 : :
554 : 0 : if (opts->op_type == CPERF_AUTH_ONLY ||
555 : 0 : opts->op_type == CPERF_CIPHER_THEN_AUTH ||
556 : : opts->op_type == CPERF_AUTH_THEN_CIPHER) {
557 : :
558 : 0 : cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
559 : 0 : cap_idx.algo.auth = opts->auth_algo;
560 : :
561 : 0 : capability = rte_cryptodev_sym_capability_get(cdev_id,
562 : : &cap_idx);
563 : 0 : if (capability == NULL)
564 : : return -1;
565 : :
566 : 0 : ret = rte_cryptodev_sym_capability_check_auth(
567 : : capability,
568 : 0 : opts->auth_key_sz,
569 : 0 : opts->digest_sz,
570 : 0 : opts->auth_iv_sz);
571 : 0 : if (ret != 0)
572 : 0 : return ret;
573 : : }
574 : :
575 : 0 : if (opts->op_type == CPERF_CIPHER_ONLY ||
576 : 0 : opts->op_type == CPERF_CIPHER_THEN_AUTH ||
577 : : opts->op_type == CPERF_AUTH_THEN_CIPHER) {
578 : :
579 : 0 : cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
580 : 0 : cap_idx.algo.cipher = opts->cipher_algo;
581 : :
582 : 0 : capability = rte_cryptodev_sym_capability_get(cdev_id,
583 : : &cap_idx);
584 : 0 : if (capability == NULL)
585 : : return -1;
586 : :
587 : 0 : ret = rte_cryptodev_sym_capability_check_cipher(
588 : : capability,
589 : 0 : opts->cipher_key_sz,
590 : 0 : opts->cipher_iv_sz);
591 : 0 : if (ret != 0)
592 : 0 : return ret;
593 : : }
594 : :
595 : 0 : if (opts->op_type == CPERF_AEAD) {
596 : :
597 : 0 : cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
598 : 0 : cap_idx.algo.aead = opts->aead_algo;
599 : :
600 : 0 : capability = rte_cryptodev_sym_capability_get(cdev_id,
601 : : &cap_idx);
602 : 0 : if (capability == NULL)
603 : : return -1;
604 : :
605 : 0 : ret = rte_cryptodev_sym_capability_check_aead(
606 : : capability,
607 : 0 : opts->aead_key_sz,
608 : 0 : opts->digest_sz,
609 : 0 : opts->aead_aad_sz,
610 : 0 : opts->aead_iv_sz);
611 : 0 : if (ret != 0)
612 : 0 : return ret;
613 : : }
614 : : }
615 : :
616 : : return 0;
617 : : }
618 : :
619 : : static int
620 : 0 : cperf_check_test_vector(struct cperf_options *opts,
621 : : struct cperf_test_vector *test_vec)
622 : : {
623 : 0 : if (opts->op_type == CPERF_CIPHER_ONLY) {
624 : 0 : if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
625 : 0 : if (test_vec->plaintext.data == NULL)
626 : 0 : return -1;
627 : : } else {
628 : 0 : if (test_vec->plaintext.data == NULL)
629 : : return -1;
630 : 0 : if (test_vec->plaintext.length < opts->max_buffer_size)
631 : : return -1;
632 : 0 : if (test_vec->ciphertext.data == NULL)
633 : : return -1;
634 : 0 : if (test_vec->ciphertext.length < opts->max_buffer_size)
635 : : return -1;
636 : : /* Cipher IV is only required for some algorithms */
637 : 0 : if (opts->cipher_iv_sz &&
638 : 0 : test_vec->cipher_iv.data == NULL)
639 : : return -1;
640 : 0 : if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
641 : : return -1;
642 : 0 : if (test_vec->cipher_key.data == NULL)
643 : : return -1;
644 : 0 : if (test_vec->cipher_key.length != opts->cipher_key_sz)
645 : 0 : return -1;
646 : : }
647 : 0 : } else if (opts->op_type == CPERF_AUTH_ONLY) {
648 : 0 : if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
649 : 0 : if (test_vec->plaintext.data == NULL)
650 : : return -1;
651 : 0 : if (test_vec->plaintext.length < opts->max_buffer_size)
652 : : return -1;
653 : : /* Auth key is only required for some algorithms */
654 : 0 : if (opts->auth_key_sz &&
655 : 0 : test_vec->auth_key.data == NULL)
656 : : return -1;
657 : 0 : if (test_vec->auth_key.length != opts->auth_key_sz)
658 : : return -1;
659 : 0 : if (test_vec->auth_iv.length != opts->auth_iv_sz)
660 : : return -1;
661 : : /* Auth IV is only required for some algorithms */
662 : 0 : if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
663 : : return -1;
664 : 0 : if (test_vec->digest.data == NULL)
665 : : return -1;
666 : 0 : if (test_vec->digest.length < opts->digest_sz)
667 : 0 : return -1;
668 : : }
669 : :
670 : 0 : } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
671 : : opts->op_type == CPERF_AUTH_THEN_CIPHER) {
672 : 0 : if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
673 : 0 : if (test_vec->plaintext.data == NULL)
674 : : return -1;
675 : 0 : if (test_vec->plaintext.length < opts->max_buffer_size)
676 : : return -1;
677 : : } else {
678 : 0 : if (test_vec->plaintext.data == NULL)
679 : : return -1;
680 : 0 : if (test_vec->plaintext.length < opts->max_buffer_size)
681 : : return -1;
682 : 0 : if (test_vec->ciphertext.data == NULL)
683 : : return -1;
684 : 0 : if (test_vec->ciphertext.length < opts->max_buffer_size)
685 : : return -1;
686 : 0 : if (test_vec->cipher_iv.data == NULL)
687 : : return -1;
688 : 0 : if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
689 : : return -1;
690 : 0 : if (test_vec->cipher_key.data == NULL)
691 : : return -1;
692 : 0 : if (test_vec->cipher_key.length != opts->cipher_key_sz)
693 : : return -1;
694 : : }
695 : 0 : if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
696 : 0 : if (test_vec->auth_key.data == NULL)
697 : : return -1;
698 : 0 : if (test_vec->auth_key.length != opts->auth_key_sz)
699 : : return -1;
700 : 0 : if (test_vec->auth_iv.length != opts->auth_iv_sz)
701 : : return -1;
702 : : /* Auth IV is only required for some algorithms */
703 : 0 : if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
704 : : return -1;
705 : 0 : if (test_vec->digest.data == NULL)
706 : : return -1;
707 : 0 : if (test_vec->digest.length < opts->digest_sz)
708 : 0 : return -1;
709 : : }
710 : 0 : } else if (opts->op_type == CPERF_AEAD) {
711 : 0 : if (test_vec->plaintext.data == NULL)
712 : : return -1;
713 : 0 : if (test_vec->plaintext.length < opts->max_buffer_size)
714 : : return -1;
715 : 0 : if (test_vec->ciphertext.data == NULL)
716 : : return -1;
717 : 0 : if (test_vec->ciphertext.length < opts->max_buffer_size)
718 : : return -1;
719 : 0 : if (test_vec->aead_key.data == NULL)
720 : : return -1;
721 : 0 : if (test_vec->aead_key.length != opts->aead_key_sz)
722 : : return -1;
723 : 0 : if (test_vec->aead_iv.data == NULL)
724 : : return -1;
725 : 0 : if (test_vec->aead_iv.length != opts->aead_iv_sz)
726 : : return -1;
727 : 0 : if (test_vec->aad.data == NULL)
728 : : return -1;
729 : 0 : if (test_vec->aad.length != opts->aead_aad_sz)
730 : : return -1;
731 : 0 : if (test_vec->digest.data == NULL)
732 : : return -1;
733 : 0 : if (test_vec->digest.length < opts->digest_sz)
734 : 0 : return -1;
735 : : }
736 : : return 0;
737 : : }
738 : :
739 : : int
740 : 0 : main(int argc, char **argv)
741 : : {
742 : 0 : struct cperf_options opts = {0};
743 : : struct cperf_test_vector *t_vec = NULL;
744 : : struct cperf_op_fns op_fns;
745 : 0 : void *ctx[RTE_MAX_LCORE] = { };
746 : : int nb_cryptodevs = 0;
747 : : uint16_t total_nb_qps = 0;
748 : : uint8_t cdev_id, i;
749 : 0 : uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
750 : :
751 : : uint8_t buffer_size_idx = 0;
752 : :
753 : : int ret;
754 : : uint32_t lcore_id;
755 : : bool cap_unsupported = false;
756 : :
757 : : /* Initialise DPDK EAL */
758 : 0 : ret = rte_eal_init(argc, argv);
759 : 0 : if (ret < 0)
760 : 0 : rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
761 : 0 : argc -= ret;
762 : 0 : argv += ret;
763 : :
764 : 0 : cperf_options_default(&opts);
765 : :
766 : 0 : ret = cperf_options_parse(&opts, argc, argv);
767 : 0 : if (ret) {
768 : 0 : RTE_LOG(ERR, USER1, "Parsing one or more user options failed\n");
769 : 0 : goto err;
770 : : }
771 : :
772 : 0 : ret = cperf_options_check(&opts);
773 : 0 : if (ret) {
774 : 0 : RTE_LOG(ERR, USER1,
775 : : "Checking one or more user options failed\n");
776 : 0 : goto err;
777 : : }
778 : :
779 : 0 : nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs);
780 : :
781 : 0 : if (!opts.silent)
782 : 0 : cperf_options_dump(&opts);
783 : :
784 : 0 : if (nb_cryptodevs < 1) {
785 : 0 : RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
786 : : "device type\n");
787 : : nb_cryptodevs = 0;
788 : 0 : goto err;
789 : : }
790 : :
791 : 0 : ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
792 : : nb_cryptodevs);
793 : 0 : if (ret) {
794 : 0 : RTE_LOG(ERR, USER1, "Crypto device type does not support "
795 : : "capabilities requested\n");
796 : : cap_unsupported = true;
797 : 0 : goto err;
798 : : }
799 : :
800 : 0 : if (opts.test_file != NULL) {
801 : 0 : t_vec = cperf_test_vector_get_from_file(&opts);
802 : 0 : if (t_vec == NULL) {
803 : 0 : RTE_LOG(ERR, USER1,
804 : : "Failed to create test vector for"
805 : : " specified file\n");
806 : 0 : goto err;
807 : : }
808 : :
809 : 0 : if (cperf_check_test_vector(&opts, t_vec)) {
810 : 0 : RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
811 : : "\n");
812 : 0 : goto err;
813 : : }
814 : : } else {
815 : 0 : t_vec = cperf_test_vector_get_dummy(&opts);
816 : 0 : if (t_vec == NULL) {
817 : 0 : RTE_LOG(ERR, USER1,
818 : : "Failed to create test vector for"
819 : : " specified algorithms\n");
820 : 0 : goto err;
821 : : }
822 : : }
823 : :
824 : 0 : ret = cperf_get_op_functions(&opts, &op_fns);
825 : 0 : if (ret) {
826 : 0 : RTE_LOG(ERR, USER1, "Failed to find function ops set for "
827 : : "specified algorithms combination\n");
828 : 0 : goto err;
829 : : }
830 : :
831 : 0 : if (!opts.silent && opts.test != CPERF_TEST_TYPE_THROUGHPUT &&
832 : : opts.test != CPERF_TEST_TYPE_LATENCY)
833 : 0 : show_test_vector(t_vec);
834 : :
835 : 0 : total_nb_qps = nb_cryptodevs * opts.nb_qps;
836 : :
837 : : i = 0;
838 : : uint8_t qp_id = 0, cdev_index = 0;
839 : :
840 : 0 : void *sess = NULL;
841 : :
842 : 0 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
843 : :
844 : 0 : if (i == total_nb_qps)
845 : : break;
846 : :
847 : 0 : cdev_id = enabled_cdevs[cdev_index];
848 : :
849 : 0 : int socket_id = rte_cryptodev_socket_id(cdev_id);
850 : :
851 : : /* Use the first socket if SOCKET_ID_ANY is returned. */
852 : 0 : if (socket_id == SOCKET_ID_ANY)
853 : : socket_id = 0;
854 : :
855 : 0 : ctx[i] = cperf_testmap[opts.test].constructor(
856 : : session_pool_socket[socket_id].sess_mp,
857 : : cdev_id, qp_id,
858 : : &opts, t_vec, &op_fns, &sess);
859 : :
860 : : /*
861 : : * If sess was NULL, the constructor will have set it to a newly
862 : : * created session. This means future calls to constructors will
863 : : * provide this session, sharing it across all qps. If session
864 : : * sharing is not enabled, re-set sess to NULL, to prevent this.
865 : : */
866 : 0 : if (!opts.shared_session)
867 : 0 : sess = NULL;
868 : :
869 : 0 : if (ctx[i] == NULL) {
870 : 0 : RTE_LOG(ERR, USER1, "Test run constructor failed\n");
871 : 0 : goto err;
872 : : }
873 : :
874 : 0 : qp_id = (qp_id + 1) % opts.nb_qps;
875 : 0 : if (qp_id == 0) {
876 : 0 : cdev_index++;
877 : : /* If next qp is on a new cdev, don't share the session
878 : : * - it shouldn't be shared across different cdevs.
879 : : */
880 : 0 : sess = NULL;
881 : : }
882 : 0 : i++;
883 : : }
884 : :
885 : 0 : if (opts.imix_distribution_count != 0) {
886 : 0 : uint8_t buffer_size_count = opts.buffer_size_count;
887 : 0 : uint16_t distribution_total[buffer_size_count];
888 : : uint32_t op_idx;
889 : : uint32_t test_average_size = 0;
890 : : const uint32_t *buffer_size_list = opts.buffer_size_list;
891 : : const uint32_t *imix_distribution_list = opts.imix_distribution_list;
892 : :
893 : 0 : opts.imix_buffer_sizes = rte_malloc(NULL,
894 : 0 : sizeof(uint32_t) * opts.pool_sz,
895 : : 0);
896 : : /*
897 : : * Calculate accumulated distribution of
898 : : * probabilities per packet size
899 : : */
900 : 0 : distribution_total[0] = imix_distribution_list[0];
901 : 0 : for (i = 1; i < buffer_size_count; i++)
902 : 0 : distribution_total[i] = imix_distribution_list[i] +
903 : 0 : distribution_total[i-1];
904 : :
905 : : /* Calculate a random sequence of packet sizes, based on distribution */
906 : 0 : for (op_idx = 0; op_idx < opts.pool_sz; op_idx++) {
907 : 0 : uint16_t random_number = rte_rand() %
908 : 0 : distribution_total[buffer_size_count - 1];
909 : 0 : for (i = 0; i < buffer_size_count; i++)
910 : 0 : if (random_number < distribution_total[i])
911 : : break;
912 : :
913 : 0 : opts.imix_buffer_sizes[op_idx] = buffer_size_list[i];
914 : : }
915 : :
916 : : /* Calculate average buffer size for the IMIX distribution */
917 : 0 : for (i = 0; i < buffer_size_count; i++)
918 : 0 : test_average_size += buffer_size_list[i] *
919 : 0 : imix_distribution_list[i];
920 : :
921 : 0 : opts.test_buffer_size = test_average_size /
922 : 0 : distribution_total[buffer_size_count - 1];
923 : :
924 : : i = 0;
925 : 0 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
926 : :
927 : 0 : if (i == total_nb_qps)
928 : : break;
929 : :
930 : 0 : rte_eal_remote_launch(cperf_testmap[opts.test].runner,
931 : : ctx[i], lcore_id);
932 : 0 : i++;
933 : : }
934 : : i = 0;
935 : 0 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
936 : :
937 : 0 : if (i == total_nb_qps)
938 : : break;
939 : 0 : ret |= rte_eal_wait_lcore(lcore_id);
940 : 0 : i++;
941 : : }
942 : :
943 : 0 : if (ret != EXIT_SUCCESS)
944 : 0 : goto err;
945 : : } else {
946 : :
947 : : /* Get next size from range or list */
948 : 0 : if (opts.inc_buffer_size != 0)
949 : 0 : opts.test_buffer_size = opts.min_buffer_size;
950 : : else
951 : 0 : opts.test_buffer_size = opts.buffer_size_list[0];
952 : :
953 : 0 : while (opts.test_buffer_size <= opts.max_buffer_size) {
954 : : i = 0;
955 : 0 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
956 : :
957 : 0 : if (i == total_nb_qps)
958 : : break;
959 : :
960 : 0 : rte_eal_remote_launch(cperf_testmap[opts.test].runner,
961 : : ctx[i], lcore_id);
962 : 0 : i++;
963 : : }
964 : : i = 0;
965 : 0 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
966 : :
967 : 0 : if (i == total_nb_qps)
968 : : break;
969 : 0 : ret |= rte_eal_wait_lcore(lcore_id);
970 : 0 : i++;
971 : : }
972 : :
973 : 0 : if (ret != EXIT_SUCCESS)
974 : 0 : goto err;
975 : :
976 : : /* Get next size from range or list */
977 : 0 : if (opts.inc_buffer_size != 0)
978 : 0 : opts.test_buffer_size += opts.inc_buffer_size;
979 : : else {
980 : 0 : if (++buffer_size_idx == opts.buffer_size_count)
981 : : break;
982 : 0 : opts.test_buffer_size =
983 : 0 : opts.buffer_size_list[buffer_size_idx];
984 : : }
985 : : }
986 : : }
987 : :
988 : : i = 0;
989 : 0 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
990 : :
991 : 0 : if (i == total_nb_qps)
992 : : break;
993 : :
994 : 0 : cperf_testmap[opts.test].destructor(ctx[i]);
995 : 0 : i++;
996 : : }
997 : :
998 : 0 : for (i = 0; i < nb_cryptodevs &&
999 : 0 : i < RTE_CRYPTO_MAX_DEVS; i++) {
1000 : 0 : rte_cryptodev_stop(enabled_cdevs[i]);
1001 : 0 : ret = rte_cryptodev_close(enabled_cdevs[i]);
1002 : 0 : if (ret)
1003 : 0 : RTE_LOG(ERR, USER1,
1004 : : "Crypto device close error %d\n", ret);
1005 : : }
1006 : :
1007 : 0 : free_test_vector(t_vec, &opts);
1008 : :
1009 : : printf("\n");
1010 : 0 : return EXIT_SUCCESS;
1011 : :
1012 : 0 : err:
1013 : : i = 0;
1014 : 0 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
1015 : 0 : if (i == total_nb_qps)
1016 : : break;
1017 : :
1018 : 0 : if (ctx[i] && cperf_testmap[opts.test].destructor)
1019 : 0 : cperf_testmap[opts.test].destructor(ctx[i]);
1020 : 0 : i++;
1021 : : }
1022 : :
1023 : 0 : for (i = 0; i < nb_cryptodevs &&
1024 : 0 : i < RTE_CRYPTO_MAX_DEVS; i++) {
1025 : 0 : rte_cryptodev_stop(enabled_cdevs[i]);
1026 : 0 : ret = rte_cryptodev_close(enabled_cdevs[i]);
1027 : 0 : if (ret)
1028 : 0 : RTE_LOG(ERR, USER1,
1029 : : "Crypto device close error %d\n", ret);
1030 : :
1031 : : }
1032 : 0 : rte_free(opts.imix_buffer_sizes);
1033 : 0 : free_test_vector(t_vec, &opts);
1034 : :
1035 : 0 : if (rte_errno == ENOTSUP || cap_unsupported) {
1036 : 0 : RTE_LOG(ERR, USER1, "Unsupported case: errno: %u\n", rte_errno);
1037 : 0 : return -ENOTSUP;
1038 : : }
1039 : : printf("\n");
1040 : 0 : return EXIT_FAILURE;
1041 : : }
|