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