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