1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020 Intel Corporation
15 #include <netinet/in.h>
17 #include <rte_byteorder.h>
19 #include <rte_debug.h>
21 #include <rte_interrupts.h>
22 #include <rte_memory.h>
23 #include <rte_memcpy.h>
24 #include <rte_memzone.h>
25 #include <rte_launch.h>
26 #include <rte_tailq.h>
28 #include <rte_per_lcore.h>
29 #include <rte_lcore.h>
30 #include <rte_atomic.h>
31 #include <rte_branch_prediction.h>
32 #include <rte_common.h>
33 #include <rte_mempool.h>
34 #include <rte_malloc.h>
36 #include <rte_errno.h>
37 #include <rte_spinlock.h>
38 #include <rte_string_fns.h>
40 #include "rte_crypto.h"
41 #include "rte_cryptodev.h"
42 #include "rte_cryptodev_pmd.h"
43 #include "rte_cryptodev_trace.h"
45 static uint8_t nb_drivers;
47 static struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
49 struct rte_cryptodev *rte_cryptodevs = rte_crypto_devices;
51 static struct rte_cryptodev_global cryptodev_globals = {
52 .devs = rte_crypto_devices,
57 /* spinlock for crypto device callbacks */
58 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
61 * The user application callback description.
63 * It contains callback address to be registered by user application,
64 * the pointer to the parameters for callback, and the event type.
66 struct rte_cryptodev_callback {
67 TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
68 rte_cryptodev_cb_fn cb_fn; /**< Callback address */
69 void *cb_arg; /**< Parameter for callback */
70 enum rte_cryptodev_event_type event; /**< Interrupt event type */
71 uint32_t active; /**< Callback is executing */
75 * The crypto cipher algorithm strings identifiers.
76 * It could be used in application command line.
79 rte_crypto_cipher_algorithm_strings[] = {
80 [RTE_CRYPTO_CIPHER_3DES_CBC] = "3des-cbc",
81 [RTE_CRYPTO_CIPHER_3DES_ECB] = "3des-ecb",
82 [RTE_CRYPTO_CIPHER_3DES_CTR] = "3des-ctr",
84 [RTE_CRYPTO_CIPHER_AES_CBC] = "aes-cbc",
85 [RTE_CRYPTO_CIPHER_AES_CTR] = "aes-ctr",
86 [RTE_CRYPTO_CIPHER_AES_DOCSISBPI] = "aes-docsisbpi",
87 [RTE_CRYPTO_CIPHER_AES_ECB] = "aes-ecb",
88 [RTE_CRYPTO_CIPHER_AES_F8] = "aes-f8",
89 [RTE_CRYPTO_CIPHER_AES_XTS] = "aes-xts",
91 [RTE_CRYPTO_CIPHER_ARC4] = "arc4",
93 [RTE_CRYPTO_CIPHER_DES_CBC] = "des-cbc",
94 [RTE_CRYPTO_CIPHER_DES_DOCSISBPI] = "des-docsisbpi",
96 [RTE_CRYPTO_CIPHER_NULL] = "null",
98 [RTE_CRYPTO_CIPHER_KASUMI_F8] = "kasumi-f8",
99 [RTE_CRYPTO_CIPHER_SNOW3G_UEA2] = "snow3g-uea2",
100 [RTE_CRYPTO_CIPHER_ZUC_EEA3] = "zuc-eea3"
104 * The crypto cipher operation strings identifiers.
105 * It could be used in application command line.
108 rte_crypto_cipher_operation_strings[] = {
109 [RTE_CRYPTO_CIPHER_OP_ENCRYPT] = "encrypt",
110 [RTE_CRYPTO_CIPHER_OP_DECRYPT] = "decrypt"
114 * The crypto auth algorithm strings identifiers.
115 * It could be used in application command line.
118 rte_crypto_auth_algorithm_strings[] = {
119 [RTE_CRYPTO_AUTH_AES_CBC_MAC] = "aes-cbc-mac",
120 [RTE_CRYPTO_AUTH_AES_CMAC] = "aes-cmac",
121 [RTE_CRYPTO_AUTH_AES_GMAC] = "aes-gmac",
122 [RTE_CRYPTO_AUTH_AES_XCBC_MAC] = "aes-xcbc-mac",
124 [RTE_CRYPTO_AUTH_MD5] = "md5",
125 [RTE_CRYPTO_AUTH_MD5_HMAC] = "md5-hmac",
127 [RTE_CRYPTO_AUTH_NULL] = "null",
129 [RTE_CRYPTO_AUTH_SHA1] = "sha1",
130 [RTE_CRYPTO_AUTH_SHA1_HMAC] = "sha1-hmac",
132 [RTE_CRYPTO_AUTH_SHA224] = "sha2-224",
133 [RTE_CRYPTO_AUTH_SHA224_HMAC] = "sha2-224-hmac",
134 [RTE_CRYPTO_AUTH_SHA256] = "sha2-256",
135 [RTE_CRYPTO_AUTH_SHA256_HMAC] = "sha2-256-hmac",
136 [RTE_CRYPTO_AUTH_SHA384] = "sha2-384",
137 [RTE_CRYPTO_AUTH_SHA384_HMAC] = "sha2-384-hmac",
138 [RTE_CRYPTO_AUTH_SHA512] = "sha2-512",
139 [RTE_CRYPTO_AUTH_SHA512_HMAC] = "sha2-512-hmac",
141 [RTE_CRYPTO_AUTH_KASUMI_F9] = "kasumi-f9",
142 [RTE_CRYPTO_AUTH_SNOW3G_UIA2] = "snow3g-uia2",
143 [RTE_CRYPTO_AUTH_ZUC_EIA3] = "zuc-eia3"
147 * The crypto AEAD algorithm strings identifiers.
148 * It could be used in application command line.
151 rte_crypto_aead_algorithm_strings[] = {
152 [RTE_CRYPTO_AEAD_AES_CCM] = "aes-ccm",
153 [RTE_CRYPTO_AEAD_AES_GCM] = "aes-gcm",
154 [RTE_CRYPTO_AEAD_CHACHA20_POLY1305] = "chacha20-poly1305"
158 * The crypto AEAD operation strings identifiers.
159 * It could be used in application command line.
162 rte_crypto_aead_operation_strings[] = {
163 [RTE_CRYPTO_AEAD_OP_ENCRYPT] = "encrypt",
164 [RTE_CRYPTO_AEAD_OP_DECRYPT] = "decrypt"
168 * Asymmetric crypto transform operation strings identifiers.
170 const char *rte_crypto_asym_xform_strings[] = {
171 [RTE_CRYPTO_ASYM_XFORM_NONE] = "none",
172 [RTE_CRYPTO_ASYM_XFORM_RSA] = "rsa",
173 [RTE_CRYPTO_ASYM_XFORM_MODEX] = "modexp",
174 [RTE_CRYPTO_ASYM_XFORM_MODINV] = "modinv",
175 [RTE_CRYPTO_ASYM_XFORM_DH] = "dh",
176 [RTE_CRYPTO_ASYM_XFORM_DSA] = "dsa",
177 [RTE_CRYPTO_ASYM_XFORM_ECDSA] = "ecdsa",
178 [RTE_CRYPTO_ASYM_XFORM_ECPM] = "ecpm",
182 * Asymmetric crypto operation strings identifiers.
184 const char *rte_crypto_asym_op_strings[] = {
185 [RTE_CRYPTO_ASYM_OP_ENCRYPT] = "encrypt",
186 [RTE_CRYPTO_ASYM_OP_DECRYPT] = "decrypt",
187 [RTE_CRYPTO_ASYM_OP_SIGN] = "sign",
188 [RTE_CRYPTO_ASYM_OP_VERIFY] = "verify",
189 [RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE] = "priv_key_generate",
190 [RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE] = "pub_key_generate",
191 [RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE] = "sharedsecret_compute",
195 * The private data structure stored in the session mempool private data.
197 struct rte_cryptodev_sym_session_pool_private_data {
199 /**< number of elements in sess_data array */
200 uint16_t user_data_sz;
201 /**< session user data will be placed after sess_data */
205 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
206 const char *algo_string)
210 for (i = 1; i < RTE_DIM(rte_crypto_cipher_algorithm_strings); i++) {
211 if (strcmp(algo_string, rte_crypto_cipher_algorithm_strings[i]) == 0) {
212 *algo_enum = (enum rte_crypto_cipher_algorithm) i;
222 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
223 const char *algo_string)
227 for (i = 1; i < RTE_DIM(rte_crypto_auth_algorithm_strings); i++) {
228 if (strcmp(algo_string, rte_crypto_auth_algorithm_strings[i]) == 0) {
229 *algo_enum = (enum rte_crypto_auth_algorithm) i;
239 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
240 const char *algo_string)
244 for (i = 1; i < RTE_DIM(rte_crypto_aead_algorithm_strings); i++) {
245 if (strcmp(algo_string, rte_crypto_aead_algorithm_strings[i]) == 0) {
246 *algo_enum = (enum rte_crypto_aead_algorithm) i;
256 rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type *xform_enum,
257 const char *xform_string)
261 for (i = 1; i < RTE_DIM(rte_crypto_asym_xform_strings); i++) {
262 if (strcmp(xform_string,
263 rte_crypto_asym_xform_strings[i]) == 0) {
264 *xform_enum = (enum rte_crypto_asym_xform_type) i;
274 * The crypto auth operation strings identifiers.
275 * It could be used in application command line.
278 rte_crypto_auth_operation_strings[] = {
279 [RTE_CRYPTO_AUTH_OP_VERIFY] = "verify",
280 [RTE_CRYPTO_AUTH_OP_GENERATE] = "generate"
283 const struct rte_cryptodev_symmetric_capability *
284 rte_cryptodev_sym_capability_get(uint8_t dev_id,
285 const struct rte_cryptodev_sym_capability_idx *idx)
287 const struct rte_cryptodev_capabilities *capability;
288 struct rte_cryptodev_info dev_info;
291 rte_cryptodev_info_get(dev_id, &dev_info);
293 while ((capability = &dev_info.capabilities[i++])->op !=
294 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
295 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
298 if (capability->sym.xform_type != idx->type)
301 if (idx->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
302 capability->sym.auth.algo == idx->algo.auth)
303 return &capability->sym;
305 if (idx->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
306 capability->sym.cipher.algo == idx->algo.cipher)
307 return &capability->sym;
309 if (idx->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
310 capability->sym.aead.algo == idx->algo.aead)
311 return &capability->sym;
318 param_range_check(uint16_t size, const struct rte_crypto_param_range *range)
320 unsigned int next_size;
322 /* Check lower/upper bounds */
323 if (size < range->min)
326 if (size > range->max)
329 /* If range is actually only one value, size is correct */
330 if (range->increment == 0)
333 /* Check if value is one of the supported sizes */
334 for (next_size = range->min; next_size <= range->max;
335 next_size += range->increment)
336 if (size == next_size)
342 const struct rte_cryptodev_asymmetric_xform_capability *
343 rte_cryptodev_asym_capability_get(uint8_t dev_id,
344 const struct rte_cryptodev_asym_capability_idx *idx)
346 const struct rte_cryptodev_capabilities *capability;
347 struct rte_cryptodev_info dev_info;
350 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
351 rte_cryptodev_info_get(dev_id, &dev_info);
353 while ((capability = &dev_info.capabilities[i++])->op !=
354 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
355 if (capability->op != RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
358 if (capability->asym.xform_capa.xform_type == idx->type)
359 return &capability->asym.xform_capa;
365 rte_cryptodev_sym_capability_check_cipher(
366 const struct rte_cryptodev_symmetric_capability *capability,
367 uint16_t key_size, uint16_t iv_size)
369 if (param_range_check(key_size, &capability->cipher.key_size) != 0)
372 if (param_range_check(iv_size, &capability->cipher.iv_size) != 0)
379 rte_cryptodev_sym_capability_check_auth(
380 const struct rte_cryptodev_symmetric_capability *capability,
381 uint16_t key_size, uint16_t digest_size, uint16_t iv_size)
383 if (param_range_check(key_size, &capability->auth.key_size) != 0)
386 if (param_range_check(digest_size, &capability->auth.digest_size) != 0)
389 if (param_range_check(iv_size, &capability->auth.iv_size) != 0)
396 rte_cryptodev_sym_capability_check_aead(
397 const struct rte_cryptodev_symmetric_capability *capability,
398 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
401 if (param_range_check(key_size, &capability->aead.key_size) != 0)
404 if (param_range_check(digest_size, &capability->aead.digest_size) != 0)
407 if (param_range_check(aad_size, &capability->aead.aad_size) != 0)
410 if (param_range_check(iv_size, &capability->aead.iv_size) != 0)
416 rte_cryptodev_asym_xform_capability_check_optype(
417 const struct rte_cryptodev_asymmetric_xform_capability *capability,
418 enum rte_crypto_asym_op_type op_type)
420 if (capability->op_types & (1 << op_type))
427 rte_cryptodev_asym_xform_capability_check_modlen(
428 const struct rte_cryptodev_asymmetric_xform_capability *capability,
431 /* no need to check for limits, if min or max = 0 */
432 if (capability->modlen.min != 0) {
433 if (modlen < capability->modlen.min)
437 if (capability->modlen.max != 0) {
438 if (modlen > capability->modlen.max)
442 /* in any case, check if given modlen is module increment */
443 if (capability->modlen.increment != 0) {
444 if (modlen % (capability->modlen.increment))
453 rte_cryptodev_get_feature_name(uint64_t flag)
456 case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
457 return "SYMMETRIC_CRYPTO";
458 case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
459 return "ASYMMETRIC_CRYPTO";
460 case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
461 return "SYM_OPERATION_CHAINING";
462 case RTE_CRYPTODEV_FF_CPU_SSE:
464 case RTE_CRYPTODEV_FF_CPU_AVX:
466 case RTE_CRYPTODEV_FF_CPU_AVX2:
468 case RTE_CRYPTODEV_FF_CPU_AVX512:
470 case RTE_CRYPTODEV_FF_CPU_AESNI:
472 case RTE_CRYPTODEV_FF_HW_ACCELERATED:
473 return "HW_ACCELERATED";
474 case RTE_CRYPTODEV_FF_IN_PLACE_SGL:
475 return "IN_PLACE_SGL";
476 case RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT:
477 return "OOP_SGL_IN_SGL_OUT";
478 case RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT:
479 return "OOP_SGL_IN_LB_OUT";
480 case RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT:
481 return "OOP_LB_IN_SGL_OUT";
482 case RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT:
483 return "OOP_LB_IN_LB_OUT";
484 case RTE_CRYPTODEV_FF_CPU_NEON:
486 case RTE_CRYPTODEV_FF_CPU_ARM_CE:
488 case RTE_CRYPTODEV_FF_SECURITY:
489 return "SECURITY_PROTOCOL";
490 case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP:
491 return "RSA_PRIV_OP_KEY_EXP";
492 case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT:
493 return "RSA_PRIV_OP_KEY_QT";
494 case RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED:
495 return "DIGEST_ENCRYPTED";
496 case RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO:
497 return "SYM_CPU_CRYPTO";
498 case RTE_CRYPTODEV_FF_ASYM_SESSIONLESS:
499 return "ASYM_SESSIONLESS";
500 case RTE_CRYPTODEV_FF_SYM_SESSIONLESS:
501 return "SYM_SESSIONLESS";
502 case RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA:
503 return "NON_BYTE_ALIGNED_DATA";
509 struct rte_cryptodev *
510 rte_cryptodev_pmd_get_dev(uint8_t dev_id)
512 return &cryptodev_globals.devs[dev_id];
515 struct rte_cryptodev *
516 rte_cryptodev_pmd_get_named_dev(const char *name)
518 struct rte_cryptodev *dev;
524 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
525 dev = &cryptodev_globals.devs[i];
527 if ((dev->attached == RTE_CRYPTODEV_ATTACHED) &&
528 (strcmp(dev->data->name, name) == 0))
535 static inline uint8_t
536 rte_cryptodev_is_valid_device_data(uint8_t dev_id)
538 if (dev_id >= RTE_CRYPTO_MAX_DEVS ||
539 rte_crypto_devices[dev_id].data == NULL)
546 rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id)
548 struct rte_cryptodev *dev = NULL;
550 if (!rte_cryptodev_is_valid_device_data(dev_id))
553 dev = rte_cryptodev_pmd_get_dev(dev_id);
554 if (dev->attached != RTE_CRYPTODEV_ATTACHED)
562 rte_cryptodev_get_dev_id(const char *name)
569 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
570 if (!rte_cryptodev_is_valid_device_data(i))
572 if ((strcmp(cryptodev_globals.devs[i].data->name, name)
574 (cryptodev_globals.devs[i].attached ==
575 RTE_CRYPTODEV_ATTACHED))
583 rte_cryptodev_count(void)
585 return cryptodev_globals.nb_devs;
589 rte_cryptodev_device_count_by_driver(uint8_t driver_id)
591 uint8_t i, dev_count = 0;
593 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++)
594 if (cryptodev_globals.devs[i].driver_id == driver_id &&
595 cryptodev_globals.devs[i].attached ==
596 RTE_CRYPTODEV_ATTACHED)
603 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
606 uint8_t i, count = 0;
607 struct rte_cryptodev *devs = cryptodev_globals.devs;
609 for (i = 0; i < RTE_CRYPTO_MAX_DEVS && count < nb_devices; i++) {
610 if (!rte_cryptodev_is_valid_device_data(i))
613 if (devs[i].attached == RTE_CRYPTODEV_ATTACHED) {
616 cmp = strncmp(devs[i].device->driver->name,
618 strlen(driver_name) + 1);
621 devices[count++] = devs[i].data->dev_id;
629 rte_cryptodev_get_sec_ctx(uint8_t dev_id)
631 if (dev_id < RTE_CRYPTO_MAX_DEVS &&
632 (rte_crypto_devices[dev_id].feature_flags &
633 RTE_CRYPTODEV_FF_SECURITY))
634 return rte_crypto_devices[dev_id].security_ctx;
640 rte_cryptodev_socket_id(uint8_t dev_id)
642 struct rte_cryptodev *dev;
644 if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
647 dev = rte_cryptodev_pmd_get_dev(dev_id);
649 return dev->data->socket_id;
653 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
656 char mz_name[RTE_MEMZONE_NAMESIZE];
657 const struct rte_memzone *mz;
660 /* generate memzone name */
661 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
662 if (n >= (int)sizeof(mz_name))
665 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
666 mz = rte_memzone_reserve(mz_name,
667 sizeof(struct rte_cryptodev_data),
669 CDEV_LOG_DEBUG("PRIMARY:reserved memzone for %s (%p)",
672 mz = rte_memzone_lookup(mz_name);
673 CDEV_LOG_DEBUG("SECONDARY:looked up memzone for %s (%p)",
681 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
682 memset(*data, 0, sizeof(struct rte_cryptodev_data));
688 rte_cryptodev_data_free(uint8_t dev_id, struct rte_cryptodev_data **data)
690 char mz_name[RTE_MEMZONE_NAMESIZE];
691 const struct rte_memzone *mz;
694 /* generate memzone name */
695 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
696 if (n >= (int)sizeof(mz_name))
699 mz = rte_memzone_lookup(mz_name);
703 RTE_ASSERT(*data == mz->addr);
706 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
707 CDEV_LOG_DEBUG("PRIMARY:free memzone of %s (%p)",
709 return rte_memzone_free(mz);
711 CDEV_LOG_DEBUG("SECONDARY:don't free memzone of %s (%p)",
719 rte_cryptodev_find_free_device_index(void)
723 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
724 if (rte_crypto_devices[dev_id].attached ==
725 RTE_CRYPTODEV_DETACHED)
728 return RTE_CRYPTO_MAX_DEVS;
731 struct rte_cryptodev *
732 rte_cryptodev_pmd_allocate(const char *name, int socket_id)
734 struct rte_cryptodev *cryptodev;
737 if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
738 CDEV_LOG_ERR("Crypto device with name %s already "
743 dev_id = rte_cryptodev_find_free_device_index();
744 if (dev_id == RTE_CRYPTO_MAX_DEVS) {
745 CDEV_LOG_ERR("Reached maximum number of crypto devices");
749 cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
751 if (cryptodev->data == NULL) {
752 struct rte_cryptodev_data **cryptodev_data =
753 &cryptodev_globals.data[dev_id];
755 int retval = rte_cryptodev_data_alloc(dev_id, cryptodev_data,
758 if (retval < 0 || *cryptodev_data == NULL)
761 cryptodev->data = *cryptodev_data;
763 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
764 strlcpy(cryptodev->data->name, name,
765 RTE_CRYPTODEV_NAME_MAX_LEN);
767 cryptodev->data->dev_id = dev_id;
768 cryptodev->data->socket_id = socket_id;
769 cryptodev->data->dev_started = 0;
770 CDEV_LOG_DEBUG("PRIMARY:init data");
773 CDEV_LOG_DEBUG("Data for %s: dev_id %d, socket %d, started %d",
774 cryptodev->data->name,
775 cryptodev->data->dev_id,
776 cryptodev->data->socket_id,
777 cryptodev->data->dev_started);
779 /* init user callbacks */
780 TAILQ_INIT(&(cryptodev->link_intr_cbs));
782 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
784 cryptodev_globals.nb_devs++;
791 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
796 if (cryptodev == NULL)
799 dev_id = cryptodev->data->dev_id;
801 /* Close device only if device operations have been set */
802 if (cryptodev->dev_ops) {
803 ret = rte_cryptodev_close(dev_id);
808 ret = rte_cryptodev_data_free(dev_id, &cryptodev_globals.data[dev_id]);
812 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
813 cryptodev_globals.nb_devs--;
818 rte_cryptodev_queue_pair_count(uint8_t dev_id)
820 struct rte_cryptodev *dev;
822 if (!rte_cryptodev_is_valid_device_data(dev_id)) {
823 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
827 dev = &rte_crypto_devices[dev_id];
828 return dev->data->nb_queue_pairs;
832 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
835 struct rte_cryptodev_info dev_info;
839 if ((dev == NULL) || (nb_qpairs < 1)) {
840 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
845 CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
846 nb_qpairs, dev->data->dev_id);
848 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
850 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
851 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
853 if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
854 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
855 nb_qpairs, dev->data->dev_id);
859 if (dev->data->queue_pairs == NULL) { /* first time configuration */
860 dev->data->queue_pairs = rte_zmalloc_socket(
861 "cryptodev->queue_pairs",
862 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
863 RTE_CACHE_LINE_SIZE, socket_id);
865 if (dev->data->queue_pairs == NULL) {
866 dev->data->nb_queue_pairs = 0;
867 CDEV_LOG_ERR("failed to get memory for qp meta data, "
872 } else { /* re-configure */
874 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
876 qp = dev->data->queue_pairs;
878 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
881 for (i = nb_qpairs; i < old_nb_queues; i++) {
882 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
887 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
888 RTE_CACHE_LINE_SIZE);
890 CDEV_LOG_ERR("failed to realloc qp meta data,"
891 " nb_queues %u", nb_qpairs);
895 if (nb_qpairs > old_nb_queues) {
896 uint16_t new_qs = nb_qpairs - old_nb_queues;
898 memset(qp + old_nb_queues, 0,
899 sizeof(qp[0]) * new_qs);
902 dev->data->queue_pairs = qp;
905 dev->data->nb_queue_pairs = nb_qpairs;
910 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
912 struct rte_cryptodev *dev;
915 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
916 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
920 dev = &rte_crypto_devices[dev_id];
922 if (dev->data->dev_started) {
924 "device %d must be stopped to allow configuration", dev_id);
928 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
930 /* Setup new number of queue pairs and reconfigure device. */
931 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
934 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
939 rte_cryptodev_trace_configure(dev_id, config);
940 return (*dev->dev_ops->dev_configure)(dev, config);
945 rte_cryptodev_start(uint8_t dev_id)
947 struct rte_cryptodev *dev;
950 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
952 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
953 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
957 dev = &rte_crypto_devices[dev_id];
959 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
961 if (dev->data->dev_started != 0) {
962 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
967 diag = (*dev->dev_ops->dev_start)(dev);
968 rte_cryptodev_trace_start(dev_id, diag);
970 dev->data->dev_started = 1;
978 rte_cryptodev_stop(uint8_t dev_id)
980 struct rte_cryptodev *dev;
982 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
983 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
987 dev = &rte_crypto_devices[dev_id];
989 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
991 if (dev->data->dev_started == 0) {
992 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
997 (*dev->dev_ops->dev_stop)(dev);
998 rte_cryptodev_trace_stop(dev_id);
999 dev->data->dev_started = 0;
1003 rte_cryptodev_close(uint8_t dev_id)
1005 struct rte_cryptodev *dev;
1008 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1009 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1013 dev = &rte_crypto_devices[dev_id];
1015 /* Device must be stopped before it can be closed */
1016 if (dev->data->dev_started == 1) {
1017 CDEV_LOG_ERR("Device %u must be stopped before closing",
1022 /* We can't close the device if there are outstanding sessions in use */
1023 if (dev->data->session_pool != NULL) {
1024 if (!rte_mempool_full(dev->data->session_pool)) {
1025 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
1026 "has sessions still in use, free "
1027 "all sessions before calling close",
1033 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
1034 retval = (*dev->dev_ops->dev_close)(dev);
1035 rte_cryptodev_trace_close(dev_id, retval);
1044 rte_cryptodev_get_qp_status(uint8_t dev_id, uint16_t queue_pair_id)
1046 struct rte_cryptodev *dev;
1048 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1049 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1053 dev = &rte_crypto_devices[dev_id];
1054 if (queue_pair_id >= dev->data->nb_queue_pairs) {
1055 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1058 void **qps = dev->data->queue_pairs;
1060 if (qps[queue_pair_id]) {
1061 CDEV_LOG_DEBUG("qp %d on dev %d is initialised",
1062 queue_pair_id, dev_id);
1066 CDEV_LOG_DEBUG("qp %d on dev %d is not initialised",
1067 queue_pair_id, dev_id);
1073 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
1074 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
1077 struct rte_cryptodev *dev;
1079 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1080 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1084 dev = &rte_crypto_devices[dev_id];
1085 if (queue_pair_id >= dev->data->nb_queue_pairs) {
1086 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1091 CDEV_LOG_ERR("qp_conf cannot be NULL\n");
1095 if ((qp_conf->mp_session && !qp_conf->mp_session_private) ||
1096 (!qp_conf->mp_session && qp_conf->mp_session_private)) {
1097 CDEV_LOG_ERR("Invalid mempools\n");
1101 if (qp_conf->mp_session) {
1102 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1103 uint32_t obj_size = qp_conf->mp_session->elt_size;
1104 uint32_t obj_priv_size = qp_conf->mp_session_private->elt_size;
1105 struct rte_cryptodev_sym_session s = {0};
1107 pool_priv = rte_mempool_get_priv(qp_conf->mp_session);
1108 if (!pool_priv || qp_conf->mp_session->private_data_size <
1109 sizeof(*pool_priv)) {
1110 CDEV_LOG_ERR("Invalid mempool\n");
1114 s.nb_drivers = pool_priv->nb_drivers;
1115 s.user_data_sz = pool_priv->user_data_sz;
1117 if ((rte_cryptodev_sym_get_existing_header_session_size(&s) >
1118 obj_size) || (s.nb_drivers <= dev->driver_id) ||
1119 rte_cryptodev_sym_get_private_session_size(dev_id) >
1121 CDEV_LOG_ERR("Invalid mempool\n");
1126 if (dev->data->dev_started) {
1128 "device %d must be stopped to allow configuration", dev_id);
1132 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
1134 rte_cryptodev_trace_queue_pair_setup(dev_id, queue_pair_id, qp_conf);
1135 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
1141 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
1143 struct rte_cryptodev *dev;
1145 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1146 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1150 if (stats == NULL) {
1151 CDEV_LOG_ERR("Invalid stats ptr");
1155 dev = &rte_crypto_devices[dev_id];
1156 memset(stats, 0, sizeof(*stats));
1158 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1159 (*dev->dev_ops->stats_get)(dev, stats);
1164 rte_cryptodev_stats_reset(uint8_t dev_id)
1166 struct rte_cryptodev *dev;
1168 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1169 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1173 dev = &rte_crypto_devices[dev_id];
1175 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
1176 (*dev->dev_ops->stats_reset)(dev);
1180 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
1182 struct rte_cryptodev *dev;
1184 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1185 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1189 dev = &rte_crypto_devices[dev_id];
1191 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
1193 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1194 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1196 dev_info->driver_name = dev->device->driver->name;
1197 dev_info->device = dev->device;
1201 rte_cryptodev_callback_register(uint8_t dev_id,
1202 enum rte_cryptodev_event_type event,
1203 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1205 struct rte_cryptodev *dev;
1206 struct rte_cryptodev_callback *user_cb;
1211 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1212 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1216 dev = &rte_crypto_devices[dev_id];
1217 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1219 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
1220 if (user_cb->cb_fn == cb_fn &&
1221 user_cb->cb_arg == cb_arg &&
1222 user_cb->event == event) {
1227 /* create a new callback. */
1228 if (user_cb == NULL) {
1229 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
1230 sizeof(struct rte_cryptodev_callback), 0);
1231 if (user_cb != NULL) {
1232 user_cb->cb_fn = cb_fn;
1233 user_cb->cb_arg = cb_arg;
1234 user_cb->event = event;
1235 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
1239 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1240 return (user_cb == NULL) ? -ENOMEM : 0;
1244 rte_cryptodev_callback_unregister(uint8_t dev_id,
1245 enum rte_cryptodev_event_type event,
1246 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1249 struct rte_cryptodev *dev;
1250 struct rte_cryptodev_callback *cb, *next;
1255 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1256 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1260 dev = &rte_crypto_devices[dev_id];
1261 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1264 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
1266 next = TAILQ_NEXT(cb, next);
1268 if (cb->cb_fn != cb_fn || cb->event != event ||
1269 (cb->cb_arg != (void *)-1 &&
1270 cb->cb_arg != cb_arg))
1274 * if this callback is not executing right now,
1277 if (cb->active == 0) {
1278 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1285 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1290 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1291 enum rte_cryptodev_event_type event)
1293 struct rte_cryptodev_callback *cb_lst;
1294 struct rte_cryptodev_callback dev_cb;
1296 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1297 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1298 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1302 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1303 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1305 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1308 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1312 rte_cryptodev_sym_session_init(uint8_t dev_id,
1313 struct rte_cryptodev_sym_session *sess,
1314 struct rte_crypto_sym_xform *xforms,
1315 struct rte_mempool *mp)
1317 struct rte_cryptodev *dev;
1318 uint32_t sess_priv_sz = rte_cryptodev_sym_get_private_session_size(
1323 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1324 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1328 dev = rte_cryptodev_pmd_get_dev(dev_id);
1330 if (sess == NULL || xforms == NULL || dev == NULL || mp == NULL)
1333 if (mp->elt_size < sess_priv_sz)
1336 index = dev->driver_id;
1337 if (index >= sess->nb_drivers)
1340 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_configure, -ENOTSUP);
1342 if (sess->sess_data[index].refcnt == 0) {
1343 ret = dev->dev_ops->sym_session_configure(dev, xforms,
1347 "dev_id %d failed to configure session details",
1353 rte_cryptodev_trace_sym_session_init(dev_id, sess, xforms, mp);
1354 sess->sess_data[index].refcnt++;
1359 rte_cryptodev_asym_session_init(uint8_t dev_id,
1360 struct rte_cryptodev_asym_session *sess,
1361 struct rte_crypto_asym_xform *xforms,
1362 struct rte_mempool *mp)
1364 struct rte_cryptodev *dev;
1368 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1369 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1373 dev = rte_cryptodev_pmd_get_dev(dev_id);
1375 if (sess == NULL || xforms == NULL || dev == NULL)
1378 index = dev->driver_id;
1380 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_configure,
1383 if (sess->sess_private_data[index] == NULL) {
1384 ret = dev->dev_ops->asym_session_configure(dev,
1389 "dev_id %d failed to configure session details",
1395 rte_cryptodev_trace_asym_session_init(dev_id, sess, xforms, mp);
1399 struct rte_mempool *
1400 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1401 uint32_t elt_size, uint32_t cache_size, uint16_t user_data_size,
1404 struct rte_mempool *mp;
1405 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1408 obj_sz = rte_cryptodev_sym_get_header_session_size() + user_data_size;
1409 if (obj_sz > elt_size)
1410 CDEV_LOG_INFO("elt_size %u is expanded to %u\n", elt_size,
1415 mp = rte_mempool_create(name, nb_elts, obj_sz, cache_size,
1416 (uint32_t)(sizeof(*pool_priv)),
1417 NULL, NULL, NULL, NULL,
1420 CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
1421 __func__, name, rte_errno);
1425 pool_priv = rte_mempool_get_priv(mp);
1427 CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
1429 rte_mempool_free(mp);
1433 pool_priv->nb_drivers = nb_drivers;
1434 pool_priv->user_data_sz = user_data_size;
1436 rte_cryptodev_trace_sym_session_pool_create(name, nb_elts,
1437 elt_size, cache_size, user_data_size, mp);
1442 rte_cryptodev_sym_session_data_size(struct rte_cryptodev_sym_session *sess)
1444 return (sizeof(sess->sess_data[0]) * sess->nb_drivers) +
1449 rte_cryptodev_sym_is_valid_session_pool(struct rte_mempool *mp)
1451 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1456 pool_priv = rte_mempool_get_priv(mp);
1458 if (!pool_priv || mp->private_data_size < sizeof(*pool_priv) ||
1459 pool_priv->nb_drivers != nb_drivers ||
1461 rte_cryptodev_sym_get_header_session_size()
1462 + pool_priv->user_data_sz)
1468 struct rte_cryptodev_sym_session *
1469 rte_cryptodev_sym_session_create(struct rte_mempool *mp)
1471 struct rte_cryptodev_sym_session *sess;
1472 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1474 if (!rte_cryptodev_sym_is_valid_session_pool(mp)) {
1475 CDEV_LOG_ERR("Invalid mempool\n");
1479 pool_priv = rte_mempool_get_priv(mp);
1481 /* Allocate a session structure from the session pool */
1482 if (rte_mempool_get(mp, (void **)&sess)) {
1483 CDEV_LOG_ERR("couldn't get object from session mempool");
1487 sess->nb_drivers = pool_priv->nb_drivers;
1488 sess->user_data_sz = pool_priv->user_data_sz;
1489 sess->opaque_data = 0;
1491 /* Clear device session pointer.
1492 * Include the flag indicating presence of user data
1494 memset(sess->sess_data, 0,
1495 rte_cryptodev_sym_session_data_size(sess));
1497 rte_cryptodev_trace_sym_session_create(mp, sess);
1501 struct rte_cryptodev_asym_session *
1502 rte_cryptodev_asym_session_create(struct rte_mempool *mp)
1504 struct rte_cryptodev_asym_session *sess;
1505 unsigned int session_size =
1506 rte_cryptodev_asym_get_header_session_size();
1509 CDEV_LOG_ERR("invalid mempool\n");
1513 /* Verify if provided mempool can hold elements big enough. */
1514 if (mp->elt_size < session_size) {
1516 "mempool elements too small to hold session objects");
1520 /* Allocate a session structure from the session pool */
1521 if (rte_mempool_get(mp, (void **)&sess)) {
1522 CDEV_LOG_ERR("couldn't get object from session mempool");
1526 /* Clear device session pointer.
1527 * Include the flag indicating presence of private data
1529 memset(sess, 0, session_size);
1531 rte_cryptodev_trace_asym_session_create(mp, sess);
1536 rte_cryptodev_sym_session_clear(uint8_t dev_id,
1537 struct rte_cryptodev_sym_session *sess)
1539 struct rte_cryptodev *dev;
1542 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1543 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1547 dev = rte_cryptodev_pmd_get_dev(dev_id);
1549 if (dev == NULL || sess == NULL)
1552 driver_id = dev->driver_id;
1553 if (sess->sess_data[driver_id].refcnt == 0)
1555 if (--sess->sess_data[driver_id].refcnt != 0)
1558 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_clear, -ENOTSUP);
1560 dev->dev_ops->sym_session_clear(dev, sess);
1562 rte_cryptodev_trace_sym_session_clear(dev_id, sess);
1567 rte_cryptodev_asym_session_clear(uint8_t dev_id,
1568 struct rte_cryptodev_asym_session *sess)
1570 struct rte_cryptodev *dev;
1572 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1573 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1577 dev = rte_cryptodev_pmd_get_dev(dev_id);
1579 if (dev == NULL || sess == NULL)
1582 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_clear, -ENOTSUP);
1584 dev->dev_ops->asym_session_clear(dev, sess);
1586 rte_cryptodev_trace_sym_session_clear(dev_id, sess);
1591 rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess)
1594 struct rte_mempool *sess_mp;
1599 /* Check that all device private data has been freed */
1600 for (i = 0; i < sess->nb_drivers; i++) {
1601 if (sess->sess_data[i].refcnt != 0)
1605 /* Return session to mempool */
1606 sess_mp = rte_mempool_from_obj(sess);
1607 rte_mempool_put(sess_mp, sess);
1609 rte_cryptodev_trace_sym_session_free(sess);
1614 rte_cryptodev_asym_session_free(struct rte_cryptodev_asym_session *sess)
1618 struct rte_mempool *sess_mp;
1623 /* Check that all device private data has been freed */
1624 for (i = 0; i < nb_drivers; i++) {
1625 sess_priv = get_asym_session_private_data(sess, i);
1626 if (sess_priv != NULL)
1630 /* Return session to mempool */
1631 sess_mp = rte_mempool_from_obj(sess);
1632 rte_mempool_put(sess_mp, sess);
1634 rte_cryptodev_trace_asym_session_free(sess);
1639 rte_cryptodev_sym_get_header_session_size(void)
1642 * Header contains pointers to the private data of all registered
1643 * drivers and all necessary information to ensure safely clear
1644 * or free al session.
1646 struct rte_cryptodev_sym_session s = {0};
1648 s.nb_drivers = nb_drivers;
1650 return (unsigned int)(sizeof(s) +
1651 rte_cryptodev_sym_session_data_size(&s));
1655 rte_cryptodev_sym_get_existing_header_session_size(
1656 struct rte_cryptodev_sym_session *sess)
1661 return (unsigned int)(sizeof(*sess) +
1662 rte_cryptodev_sym_session_data_size(sess));
1666 rte_cryptodev_asym_get_header_session_size(void)
1669 * Header contains pointers to the private data
1670 * of all registered drivers, and a flag which
1671 * indicates presence of private data
1673 return ((sizeof(void *) * nb_drivers) + sizeof(uint8_t));
1677 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)
1679 struct rte_cryptodev *dev;
1680 unsigned int priv_sess_size;
1682 if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
1685 dev = rte_cryptodev_pmd_get_dev(dev_id);
1687 if (*dev->dev_ops->sym_session_get_size == NULL)
1690 priv_sess_size = (*dev->dev_ops->sym_session_get_size)(dev);
1692 return priv_sess_size;
1696 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)
1698 struct rte_cryptodev *dev;
1699 unsigned int header_size = sizeof(void *) * nb_drivers;
1700 unsigned int priv_sess_size;
1702 if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
1705 dev = rte_cryptodev_pmd_get_dev(dev_id);
1707 if (*dev->dev_ops->asym_session_get_size == NULL)
1710 priv_sess_size = (*dev->dev_ops->asym_session_get_size)(dev);
1711 if (priv_sess_size < header_size)
1714 return priv_sess_size;
1719 rte_cryptodev_sym_session_set_user_data(
1720 struct rte_cryptodev_sym_session *sess,
1727 if (sess->user_data_sz < size)
1730 rte_memcpy(sess->sess_data + sess->nb_drivers, data, size);
1735 rte_cryptodev_sym_session_get_user_data(
1736 struct rte_cryptodev_sym_session *sess)
1738 if (sess == NULL || sess->user_data_sz == 0)
1741 return (void *)(sess->sess_data + sess->nb_drivers);
1745 sym_crypto_fill_status(struct rte_crypto_sym_vec *vec, int32_t errnum)
1748 for (i = 0; i < vec->num; i++)
1749 vec->status[i] = errnum;
1753 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
1754 struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs ofs,
1755 struct rte_crypto_sym_vec *vec)
1757 struct rte_cryptodev *dev;
1759 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1760 sym_crypto_fill_status(vec, EINVAL);
1764 dev = rte_cryptodev_pmd_get_dev(dev_id);
1766 if (*dev->dev_ops->sym_cpu_process == NULL ||
1767 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) {
1768 sym_crypto_fill_status(vec, ENOTSUP);
1772 return dev->dev_ops->sym_cpu_process(dev, sess, ofs, vec);
1776 rte_cryptodev_get_raw_dp_ctx_size(uint8_t dev_id)
1778 struct rte_cryptodev *dev;
1779 int32_t size = sizeof(struct rte_crypto_raw_dp_ctx);
1782 if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
1785 dev = rte_cryptodev_pmd_get_dev(dev_id);
1787 if (*dev->dev_ops->sym_get_raw_dp_ctx_size == NULL ||
1788 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)) {
1792 priv_size = (*dev->dev_ops->sym_get_raw_dp_ctx_size)(dev);
1796 return RTE_ALIGN_CEIL((size + priv_size), 8);
1800 rte_cryptodev_configure_raw_dp_ctx(uint8_t dev_id, uint16_t qp_id,
1801 struct rte_crypto_raw_dp_ctx *ctx,
1802 enum rte_crypto_op_sess_type sess_type,
1803 union rte_cryptodev_session_ctx session_ctx,
1806 struct rte_cryptodev *dev;
1808 if (!rte_cryptodev_get_qp_status(dev_id, qp_id))
1811 dev = rte_cryptodev_pmd_get_dev(dev_id);
1812 if (!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)
1813 || dev->dev_ops->sym_configure_raw_dp_ctx == NULL)
1816 return (*dev->dev_ops->sym_configure_raw_dp_ctx)(dev, qp_id, ctx,
1817 sess_type, session_ctx, is_update);
1821 rte_cryptodev_raw_enqueue_burst(struct rte_crypto_raw_dp_ctx *ctx,
1822 struct rte_crypto_sym_vec *vec, union rte_crypto_sym_ofs ofs,
1823 void **user_data, int *enqueue_status)
1825 return (*ctx->enqueue_burst)(ctx->qp_data, ctx->drv_ctx_data, vec,
1826 ofs, user_data, enqueue_status);
1830 rte_cryptodev_raw_enqueue_done(struct rte_crypto_raw_dp_ctx *ctx,
1833 return (*ctx->enqueue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
1837 rte_cryptodev_raw_dequeue_burst(struct rte_crypto_raw_dp_ctx *ctx,
1838 rte_cryptodev_raw_get_dequeue_count_t get_dequeue_count,
1839 rte_cryptodev_raw_post_dequeue_t post_dequeue,
1840 void **out_user_data, uint8_t is_user_data_array,
1841 uint32_t *n_success_jobs, int *status)
1843 return (*ctx->dequeue_burst)(ctx->qp_data, ctx->drv_ctx_data,
1844 get_dequeue_count, post_dequeue, out_user_data,
1845 is_user_data_array, n_success_jobs, status);
1849 rte_cryptodev_raw_dequeue_done(struct rte_crypto_raw_dp_ctx *ctx,
1852 return (*ctx->dequeue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
1855 /** Initialise rte_crypto_op mempool element */
1857 rte_crypto_op_init(struct rte_mempool *mempool,
1860 __rte_unused unsigned i)
1862 struct rte_crypto_op *op = _op_data;
1863 enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1865 memset(_op_data, 0, mempool->elt_size);
1867 __rte_crypto_op_reset(op, type);
1869 op->phys_addr = rte_mem_virt2iova(_op_data);
1870 op->mempool = mempool;
1874 struct rte_mempool *
1875 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1876 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1879 struct rte_crypto_op_pool_private *priv;
1881 unsigned elt_size = sizeof(struct rte_crypto_op) +
1884 if (type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
1885 elt_size += sizeof(struct rte_crypto_sym_op);
1886 } else if (type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
1887 elt_size += sizeof(struct rte_crypto_asym_op);
1888 } else if (type == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1889 elt_size += RTE_MAX(sizeof(struct rte_crypto_sym_op),
1890 sizeof(struct rte_crypto_asym_op));
1892 CDEV_LOG_ERR("Invalid op_type\n");
1896 /* lookup mempool in case already allocated */
1897 struct rte_mempool *mp = rte_mempool_lookup(name);
1900 priv = (struct rte_crypto_op_pool_private *)
1901 rte_mempool_get_priv(mp);
1903 if (mp->elt_size != elt_size ||
1904 mp->cache_size < cache_size ||
1905 mp->size < nb_elts ||
1906 priv->priv_size < priv_size) {
1908 CDEV_LOG_ERR("Mempool %s already exists but with "
1909 "incompatible parameters", name);
1915 mp = rte_mempool_create(
1920 sizeof(struct rte_crypto_op_pool_private),
1929 CDEV_LOG_ERR("Failed to create mempool %s", name);
1933 priv = (struct rte_crypto_op_pool_private *)
1934 rte_mempool_get_priv(mp);
1936 priv->priv_size = priv_size;
1943 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix)
1945 struct rte_cryptodev *dev = NULL;
1951 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
1952 int ret = snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
1953 "%s_%u", dev_name_prefix, i);
1958 dev = rte_cryptodev_pmd_get_named_dev(name);
1966 TAILQ_HEAD(cryptodev_driver_list, cryptodev_driver);
1968 static struct cryptodev_driver_list cryptodev_driver_list =
1969 TAILQ_HEAD_INITIALIZER(cryptodev_driver_list);
1972 rte_cryptodev_driver_id_get(const char *name)
1974 struct cryptodev_driver *driver;
1975 const char *driver_name;
1978 RTE_LOG(DEBUG, CRYPTODEV, "name pointer NULL");
1982 TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
1983 driver_name = driver->driver->name;
1984 if (strncmp(driver_name, name, strlen(driver_name) + 1) == 0)
1991 rte_cryptodev_name_get(uint8_t dev_id)
1993 struct rte_cryptodev *dev;
1995 if (!rte_cryptodev_is_valid_device_data(dev_id)) {
1996 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2000 dev = rte_cryptodev_pmd_get_dev(dev_id);
2004 return dev->data->name;
2008 rte_cryptodev_driver_name_get(uint8_t driver_id)
2010 struct cryptodev_driver *driver;
2012 TAILQ_FOREACH(driver, &cryptodev_driver_list, next)
2013 if (driver->id == driver_id)
2014 return driver->driver->name;
2019 rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
2020 const struct rte_driver *drv)
2022 crypto_drv->driver = drv;
2023 crypto_drv->id = nb_drivers;
2025 TAILQ_INSERT_TAIL(&cryptodev_driver_list, crypto_drv, next);
2027 return nb_drivers++;