1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020 Intel Corporation
16 #include <rte_byteorder.h>
18 #include <rte_debug.h>
20 #include <rte_interrupts.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_memzone.h>
24 #include <rte_launch.h>
25 #include <rte_tailq.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_atomic.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_common.h>
32 #include <rte_mempool.h>
33 #include <rte_malloc.h>
35 #include <rte_errno.h>
36 #include <rte_spinlock.h>
37 #include <rte_string_fns.h>
38 #include <rte_telemetry.h>
40 #include "rte_crypto.h"
41 #include "rte_cryptodev.h"
42 #include "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 /* Public fastpath APIs. */
58 struct rte_crypto_fp_ops rte_crypto_fp_ops[RTE_CRYPTO_MAX_DEVS];
60 /* spinlock for crypto device callbacks */
61 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
64 * The user application callback description.
66 * It contains callback address to be registered by user application,
67 * the pointer to the parameters for callback, and the event type.
69 struct rte_cryptodev_callback {
70 TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
71 rte_cryptodev_cb_fn cb_fn; /**< Callback address */
72 void *cb_arg; /**< Parameter for callback */
73 enum rte_cryptodev_event_type event; /**< Interrupt event type */
74 uint32_t active; /**< Callback is executing */
78 * The crypto cipher algorithm strings identifiers.
79 * It could be used in application command line.
82 rte_crypto_cipher_algorithm_strings[] = {
83 [RTE_CRYPTO_CIPHER_3DES_CBC] = "3des-cbc",
84 [RTE_CRYPTO_CIPHER_3DES_ECB] = "3des-ecb",
85 [RTE_CRYPTO_CIPHER_3DES_CTR] = "3des-ctr",
87 [RTE_CRYPTO_CIPHER_AES_CBC] = "aes-cbc",
88 [RTE_CRYPTO_CIPHER_AES_CTR] = "aes-ctr",
89 [RTE_CRYPTO_CIPHER_AES_DOCSISBPI] = "aes-docsisbpi",
90 [RTE_CRYPTO_CIPHER_AES_ECB] = "aes-ecb",
91 [RTE_CRYPTO_CIPHER_AES_F8] = "aes-f8",
92 [RTE_CRYPTO_CIPHER_AES_XTS] = "aes-xts",
94 [RTE_CRYPTO_CIPHER_ARC4] = "arc4",
96 [RTE_CRYPTO_CIPHER_DES_CBC] = "des-cbc",
97 [RTE_CRYPTO_CIPHER_DES_DOCSISBPI] = "des-docsisbpi",
99 [RTE_CRYPTO_CIPHER_NULL] = "null",
101 [RTE_CRYPTO_CIPHER_KASUMI_F8] = "kasumi-f8",
102 [RTE_CRYPTO_CIPHER_SNOW3G_UEA2] = "snow3g-uea2",
103 [RTE_CRYPTO_CIPHER_ZUC_EEA3] = "zuc-eea3"
107 * The crypto cipher operation strings identifiers.
108 * It could be used in application command line.
111 rte_crypto_cipher_operation_strings[] = {
112 [RTE_CRYPTO_CIPHER_OP_ENCRYPT] = "encrypt",
113 [RTE_CRYPTO_CIPHER_OP_DECRYPT] = "decrypt"
117 * The crypto auth algorithm strings identifiers.
118 * It could be used in application command line.
121 rte_crypto_auth_algorithm_strings[] = {
122 [RTE_CRYPTO_AUTH_AES_CBC_MAC] = "aes-cbc-mac",
123 [RTE_CRYPTO_AUTH_AES_CMAC] = "aes-cmac",
124 [RTE_CRYPTO_AUTH_AES_GMAC] = "aes-gmac",
125 [RTE_CRYPTO_AUTH_AES_XCBC_MAC] = "aes-xcbc-mac",
127 [RTE_CRYPTO_AUTH_MD5] = "md5",
128 [RTE_CRYPTO_AUTH_MD5_HMAC] = "md5-hmac",
130 [RTE_CRYPTO_AUTH_NULL] = "null",
132 [RTE_CRYPTO_AUTH_SHA1] = "sha1",
133 [RTE_CRYPTO_AUTH_SHA1_HMAC] = "sha1-hmac",
135 [RTE_CRYPTO_AUTH_SHA224] = "sha2-224",
136 [RTE_CRYPTO_AUTH_SHA224_HMAC] = "sha2-224-hmac",
137 [RTE_CRYPTO_AUTH_SHA256] = "sha2-256",
138 [RTE_CRYPTO_AUTH_SHA256_HMAC] = "sha2-256-hmac",
139 [RTE_CRYPTO_AUTH_SHA384] = "sha2-384",
140 [RTE_CRYPTO_AUTH_SHA384_HMAC] = "sha2-384-hmac",
141 [RTE_CRYPTO_AUTH_SHA512] = "sha2-512",
142 [RTE_CRYPTO_AUTH_SHA512_HMAC] = "sha2-512-hmac",
144 [RTE_CRYPTO_AUTH_KASUMI_F9] = "kasumi-f9",
145 [RTE_CRYPTO_AUTH_SNOW3G_UIA2] = "snow3g-uia2",
146 [RTE_CRYPTO_AUTH_ZUC_EIA3] = "zuc-eia3"
150 * The crypto AEAD algorithm strings identifiers.
151 * It could be used in application command line.
154 rte_crypto_aead_algorithm_strings[] = {
155 [RTE_CRYPTO_AEAD_AES_CCM] = "aes-ccm",
156 [RTE_CRYPTO_AEAD_AES_GCM] = "aes-gcm",
157 [RTE_CRYPTO_AEAD_CHACHA20_POLY1305] = "chacha20-poly1305"
161 * The crypto AEAD operation strings identifiers.
162 * It could be used in application command line.
165 rte_crypto_aead_operation_strings[] = {
166 [RTE_CRYPTO_AEAD_OP_ENCRYPT] = "encrypt",
167 [RTE_CRYPTO_AEAD_OP_DECRYPT] = "decrypt"
171 * Asymmetric crypto transform operation strings identifiers.
173 const char *rte_crypto_asym_xform_strings[] = {
174 [RTE_CRYPTO_ASYM_XFORM_NONE] = "none",
175 [RTE_CRYPTO_ASYM_XFORM_RSA] = "rsa",
176 [RTE_CRYPTO_ASYM_XFORM_MODEX] = "modexp",
177 [RTE_CRYPTO_ASYM_XFORM_MODINV] = "modinv",
178 [RTE_CRYPTO_ASYM_XFORM_DH] = "dh",
179 [RTE_CRYPTO_ASYM_XFORM_DSA] = "dsa",
180 [RTE_CRYPTO_ASYM_XFORM_ECDSA] = "ecdsa",
181 [RTE_CRYPTO_ASYM_XFORM_ECPM] = "ecpm",
185 * Asymmetric crypto operation strings identifiers.
187 const char *rte_crypto_asym_op_strings[] = {
188 [RTE_CRYPTO_ASYM_OP_ENCRYPT] = "encrypt",
189 [RTE_CRYPTO_ASYM_OP_DECRYPT] = "decrypt",
190 [RTE_CRYPTO_ASYM_OP_SIGN] = "sign",
191 [RTE_CRYPTO_ASYM_OP_VERIFY] = "verify",
192 [RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE] = "priv_key_generate",
193 [RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE] = "pub_key_generate",
194 [RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE] = "sharedsecret_compute",
198 * The private data structure stored in the sym session mempool private data.
200 struct rte_cryptodev_sym_session_pool_private_data {
202 /**< number of elements in sess_data array */
203 uint16_t user_data_sz;
204 /**< session user data will be placed after sess_data */
208 * The private data structure stored in the asym session mempool private data.
210 struct rte_cryptodev_asym_session_pool_private_data {
211 uint16_t max_priv_session_sz;
212 /**< Size of private session data used when creating mempool */
213 uint16_t user_data_sz;
214 /**< Session user data will be placed after sess_private_data */
218 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
219 const char *algo_string)
223 for (i = 1; i < RTE_DIM(rte_crypto_cipher_algorithm_strings); i++) {
224 if (strcmp(algo_string, rte_crypto_cipher_algorithm_strings[i]) == 0) {
225 *algo_enum = (enum rte_crypto_cipher_algorithm) i;
235 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
236 const char *algo_string)
240 for (i = 1; i < RTE_DIM(rte_crypto_auth_algorithm_strings); i++) {
241 if (strcmp(algo_string, rte_crypto_auth_algorithm_strings[i]) == 0) {
242 *algo_enum = (enum rte_crypto_auth_algorithm) i;
252 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
253 const char *algo_string)
257 for (i = 1; i < RTE_DIM(rte_crypto_aead_algorithm_strings); i++) {
258 if (strcmp(algo_string, rte_crypto_aead_algorithm_strings[i]) == 0) {
259 *algo_enum = (enum rte_crypto_aead_algorithm) i;
269 rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type *xform_enum,
270 const char *xform_string)
274 for (i = 1; i < RTE_DIM(rte_crypto_asym_xform_strings); i++) {
275 if (strcmp(xform_string,
276 rte_crypto_asym_xform_strings[i]) == 0) {
277 *xform_enum = (enum rte_crypto_asym_xform_type) i;
287 * The crypto auth operation strings identifiers.
288 * It could be used in application command line.
291 rte_crypto_auth_operation_strings[] = {
292 [RTE_CRYPTO_AUTH_OP_VERIFY] = "verify",
293 [RTE_CRYPTO_AUTH_OP_GENERATE] = "generate"
296 const struct rte_cryptodev_symmetric_capability *
297 rte_cryptodev_sym_capability_get(uint8_t dev_id,
298 const struct rte_cryptodev_sym_capability_idx *idx)
300 const struct rte_cryptodev_capabilities *capability;
301 struct rte_cryptodev_info dev_info;
304 rte_cryptodev_info_get(dev_id, &dev_info);
306 while ((capability = &dev_info.capabilities[i++])->op !=
307 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
308 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
311 if (capability->sym.xform_type != idx->type)
314 if (idx->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
315 capability->sym.auth.algo == idx->algo.auth)
316 return &capability->sym;
318 if (idx->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
319 capability->sym.cipher.algo == idx->algo.cipher)
320 return &capability->sym;
322 if (idx->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
323 capability->sym.aead.algo == idx->algo.aead)
324 return &capability->sym;
331 param_range_check(uint16_t size, const struct rte_crypto_param_range *range)
333 unsigned int next_size;
335 /* Check lower/upper bounds */
336 if (size < range->min)
339 if (size > range->max)
342 /* If range is actually only one value, size is correct */
343 if (range->increment == 0)
346 /* Check if value is one of the supported sizes */
347 for (next_size = range->min; next_size <= range->max;
348 next_size += range->increment)
349 if (size == next_size)
355 const struct rte_cryptodev_asymmetric_xform_capability *
356 rte_cryptodev_asym_capability_get(uint8_t dev_id,
357 const struct rte_cryptodev_asym_capability_idx *idx)
359 const struct rte_cryptodev_capabilities *capability;
360 struct rte_cryptodev_info dev_info;
363 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
364 rte_cryptodev_info_get(dev_id, &dev_info);
366 while ((capability = &dev_info.capabilities[i++])->op !=
367 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
368 if (capability->op != RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
371 if (capability->asym.xform_capa.xform_type == idx->type)
372 return &capability->asym.xform_capa;
378 rte_cryptodev_sym_capability_check_cipher(
379 const struct rte_cryptodev_symmetric_capability *capability,
380 uint16_t key_size, uint16_t iv_size)
382 if (param_range_check(key_size, &capability->cipher.key_size) != 0)
385 if (param_range_check(iv_size, &capability->cipher.iv_size) != 0)
392 rte_cryptodev_sym_capability_check_auth(
393 const struct rte_cryptodev_symmetric_capability *capability,
394 uint16_t key_size, uint16_t digest_size, uint16_t iv_size)
396 if (param_range_check(key_size, &capability->auth.key_size) != 0)
399 if (param_range_check(digest_size, &capability->auth.digest_size) != 0)
402 if (param_range_check(iv_size, &capability->auth.iv_size) != 0)
409 rte_cryptodev_sym_capability_check_aead(
410 const struct rte_cryptodev_symmetric_capability *capability,
411 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
414 if (param_range_check(key_size, &capability->aead.key_size) != 0)
417 if (param_range_check(digest_size, &capability->aead.digest_size) != 0)
420 if (param_range_check(aad_size, &capability->aead.aad_size) != 0)
423 if (param_range_check(iv_size, &capability->aead.iv_size) != 0)
429 rte_cryptodev_asym_xform_capability_check_optype(
430 const struct rte_cryptodev_asymmetric_xform_capability *capability,
431 enum rte_crypto_asym_op_type op_type)
433 if (capability->op_types & (1 << op_type))
440 rte_cryptodev_asym_xform_capability_check_modlen(
441 const struct rte_cryptodev_asymmetric_xform_capability *capability,
444 /* no need to check for limits, if min or max = 0 */
445 if (capability->modlen.min != 0) {
446 if (modlen < capability->modlen.min)
450 if (capability->modlen.max != 0) {
451 if (modlen > capability->modlen.max)
455 /* in any case, check if given modlen is module increment */
456 if (capability->modlen.increment != 0) {
457 if (modlen % (capability->modlen.increment))
464 /* spinlock for crypto device enq callbacks */
465 static rte_spinlock_t rte_cryptodev_callback_lock = RTE_SPINLOCK_INITIALIZER;
468 cryptodev_cb_cleanup(struct rte_cryptodev *dev)
470 struct rte_cryptodev_cb_rcu *list;
471 struct rte_cryptodev_cb *cb, *next;
474 if (dev->enq_cbs == NULL && dev->deq_cbs == NULL)
477 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
478 list = &dev->enq_cbs[qp_id];
486 rte_free(list->qsbr);
489 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
490 list = &dev->deq_cbs[qp_id];
498 rte_free(list->qsbr);
501 rte_free(dev->enq_cbs);
503 rte_free(dev->deq_cbs);
508 cryptodev_cb_init(struct rte_cryptodev *dev)
510 struct rte_cryptodev_cb_rcu *list;
511 struct rte_rcu_qsbr *qsbr;
515 /* Max thread set to 1, as one DP thread accessing a queue-pair */
516 const uint32_t max_threads = 1;
518 dev->enq_cbs = rte_zmalloc(NULL,
519 sizeof(struct rte_cryptodev_cb_rcu) *
520 dev->data->nb_queue_pairs, 0);
521 if (dev->enq_cbs == NULL) {
522 CDEV_LOG_ERR("Failed to allocate memory for enq callbacks");
526 dev->deq_cbs = rte_zmalloc(NULL,
527 sizeof(struct rte_cryptodev_cb_rcu) *
528 dev->data->nb_queue_pairs, 0);
529 if (dev->deq_cbs == NULL) {
530 CDEV_LOG_ERR("Failed to allocate memory for deq callbacks");
531 rte_free(dev->enq_cbs);
535 /* Create RCU QSBR variable */
536 size = rte_rcu_qsbr_get_memsize(max_threads);
538 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
539 list = &dev->enq_cbs[qp_id];
540 qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
542 CDEV_LOG_ERR("Failed to allocate memory for RCU on "
543 "queue_pair_id=%d", qp_id);
547 if (rte_rcu_qsbr_init(qsbr, max_threads)) {
548 CDEV_LOG_ERR("Failed to initialize for RCU on "
549 "queue_pair_id=%d", qp_id);
556 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
557 list = &dev->deq_cbs[qp_id];
558 qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
560 CDEV_LOG_ERR("Failed to allocate memory for RCU on "
561 "queue_pair_id=%d", qp_id);
565 if (rte_rcu_qsbr_init(qsbr, max_threads)) {
566 CDEV_LOG_ERR("Failed to initialize for RCU on "
567 "queue_pair_id=%d", qp_id);
577 cryptodev_cb_cleanup(dev);
582 rte_cryptodev_get_feature_name(uint64_t flag)
585 case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
586 return "SYMMETRIC_CRYPTO";
587 case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
588 return "ASYMMETRIC_CRYPTO";
589 case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
590 return "SYM_OPERATION_CHAINING";
591 case RTE_CRYPTODEV_FF_CPU_SSE:
593 case RTE_CRYPTODEV_FF_CPU_AVX:
595 case RTE_CRYPTODEV_FF_CPU_AVX2:
597 case RTE_CRYPTODEV_FF_CPU_AVX512:
599 case RTE_CRYPTODEV_FF_CPU_AESNI:
601 case RTE_CRYPTODEV_FF_HW_ACCELERATED:
602 return "HW_ACCELERATED";
603 case RTE_CRYPTODEV_FF_IN_PLACE_SGL:
604 return "IN_PLACE_SGL";
605 case RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT:
606 return "OOP_SGL_IN_SGL_OUT";
607 case RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT:
608 return "OOP_SGL_IN_LB_OUT";
609 case RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT:
610 return "OOP_LB_IN_SGL_OUT";
611 case RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT:
612 return "OOP_LB_IN_LB_OUT";
613 case RTE_CRYPTODEV_FF_CPU_NEON:
615 case RTE_CRYPTODEV_FF_CPU_ARM_CE:
617 case RTE_CRYPTODEV_FF_SECURITY:
618 return "SECURITY_PROTOCOL";
619 case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP:
620 return "RSA_PRIV_OP_KEY_EXP";
621 case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT:
622 return "RSA_PRIV_OP_KEY_QT";
623 case RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED:
624 return "DIGEST_ENCRYPTED";
625 case RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO:
626 return "SYM_CPU_CRYPTO";
627 case RTE_CRYPTODEV_FF_ASYM_SESSIONLESS:
628 return "ASYM_SESSIONLESS";
629 case RTE_CRYPTODEV_FF_SYM_SESSIONLESS:
630 return "SYM_SESSIONLESS";
631 case RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA:
632 return "NON_BYTE_ALIGNED_DATA";
633 case RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS:
634 return "CIPHER_MULTIPLE_DATA_UNITS";
635 case RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY:
636 return "CIPHER_WRAPPED_KEY";
642 struct rte_cryptodev *
643 rte_cryptodev_pmd_get_dev(uint8_t dev_id)
645 return &cryptodev_globals.devs[dev_id];
648 struct rte_cryptodev *
649 rte_cryptodev_pmd_get_named_dev(const char *name)
651 struct rte_cryptodev *dev;
657 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
658 dev = &cryptodev_globals.devs[i];
660 if ((dev->attached == RTE_CRYPTODEV_ATTACHED) &&
661 (strcmp(dev->data->name, name) == 0))
668 static inline uint8_t
669 rte_cryptodev_is_valid_device_data(uint8_t dev_id)
671 if (dev_id >= RTE_CRYPTO_MAX_DEVS ||
672 rte_crypto_devices[dev_id].data == NULL)
679 rte_cryptodev_is_valid_dev(uint8_t dev_id)
681 struct rte_cryptodev *dev = NULL;
683 if (!rte_cryptodev_is_valid_device_data(dev_id))
686 dev = rte_cryptodev_pmd_get_dev(dev_id);
687 if (dev->attached != RTE_CRYPTODEV_ATTACHED)
695 rte_cryptodev_get_dev_id(const char *name)
702 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
703 if (!rte_cryptodev_is_valid_device_data(i))
705 if ((strcmp(cryptodev_globals.devs[i].data->name, name)
707 (cryptodev_globals.devs[i].attached ==
708 RTE_CRYPTODEV_ATTACHED))
716 rte_cryptodev_count(void)
718 return cryptodev_globals.nb_devs;
722 rte_cryptodev_device_count_by_driver(uint8_t driver_id)
724 uint8_t i, dev_count = 0;
726 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++)
727 if (cryptodev_globals.devs[i].driver_id == driver_id &&
728 cryptodev_globals.devs[i].attached ==
729 RTE_CRYPTODEV_ATTACHED)
736 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
739 uint8_t i, count = 0;
740 struct rte_cryptodev *devs = cryptodev_globals.devs;
742 for (i = 0; i < RTE_CRYPTO_MAX_DEVS && count < nb_devices; i++) {
743 if (!rte_cryptodev_is_valid_device_data(i))
746 if (devs[i].attached == RTE_CRYPTODEV_ATTACHED) {
749 cmp = strncmp(devs[i].device->driver->name,
751 strlen(driver_name) + 1);
754 devices[count++] = devs[i].data->dev_id;
762 rte_cryptodev_get_sec_ctx(uint8_t dev_id)
764 if (dev_id < RTE_CRYPTO_MAX_DEVS &&
765 (rte_crypto_devices[dev_id].feature_flags &
766 RTE_CRYPTODEV_FF_SECURITY))
767 return rte_crypto_devices[dev_id].security_ctx;
773 rte_cryptodev_socket_id(uint8_t dev_id)
775 struct rte_cryptodev *dev;
777 if (!rte_cryptodev_is_valid_dev(dev_id))
780 dev = rte_cryptodev_pmd_get_dev(dev_id);
782 return dev->data->socket_id;
786 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
789 char mz_name[RTE_MEMZONE_NAMESIZE];
790 const struct rte_memzone *mz;
793 /* generate memzone name */
794 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
795 if (n >= (int)sizeof(mz_name))
798 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
799 mz = rte_memzone_reserve(mz_name,
800 sizeof(struct rte_cryptodev_data),
802 CDEV_LOG_DEBUG("PRIMARY:reserved memzone for %s (%p)",
805 mz = rte_memzone_lookup(mz_name);
806 CDEV_LOG_DEBUG("SECONDARY:looked up memzone for %s (%p)",
814 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
815 memset(*data, 0, sizeof(struct rte_cryptodev_data));
821 rte_cryptodev_data_free(uint8_t dev_id, struct rte_cryptodev_data **data)
823 char mz_name[RTE_MEMZONE_NAMESIZE];
824 const struct rte_memzone *mz;
827 /* generate memzone name */
828 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
829 if (n >= (int)sizeof(mz_name))
832 mz = rte_memzone_lookup(mz_name);
836 RTE_ASSERT(*data == mz->addr);
839 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
840 CDEV_LOG_DEBUG("PRIMARY:free memzone of %s (%p)",
842 return rte_memzone_free(mz);
844 CDEV_LOG_DEBUG("SECONDARY:don't free memzone of %s (%p)",
852 rte_cryptodev_find_free_device_index(void)
856 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
857 if (rte_crypto_devices[dev_id].attached ==
858 RTE_CRYPTODEV_DETACHED)
861 return RTE_CRYPTO_MAX_DEVS;
864 struct rte_cryptodev *
865 rte_cryptodev_pmd_allocate(const char *name, int socket_id)
867 struct rte_cryptodev *cryptodev;
870 if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
871 CDEV_LOG_ERR("Crypto device with name %s already "
876 dev_id = rte_cryptodev_find_free_device_index();
877 if (dev_id == RTE_CRYPTO_MAX_DEVS) {
878 CDEV_LOG_ERR("Reached maximum number of crypto devices");
882 cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
884 if (cryptodev->data == NULL) {
885 struct rte_cryptodev_data **cryptodev_data =
886 &cryptodev_globals.data[dev_id];
888 int retval = rte_cryptodev_data_alloc(dev_id, cryptodev_data,
891 if (retval < 0 || *cryptodev_data == NULL)
894 cryptodev->data = *cryptodev_data;
896 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
897 strlcpy(cryptodev->data->name, name,
898 RTE_CRYPTODEV_NAME_MAX_LEN);
900 cryptodev->data->dev_id = dev_id;
901 cryptodev->data->socket_id = socket_id;
902 cryptodev->data->dev_started = 0;
903 CDEV_LOG_DEBUG("PRIMARY:init data");
906 CDEV_LOG_DEBUG("Data for %s: dev_id %d, socket %d, started %d",
907 cryptodev->data->name,
908 cryptodev->data->dev_id,
909 cryptodev->data->socket_id,
910 cryptodev->data->dev_started);
912 /* init user callbacks */
913 TAILQ_INIT(&(cryptodev->link_intr_cbs));
915 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
917 cryptodev_globals.nb_devs++;
924 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
929 if (cryptodev == NULL)
932 dev_id = cryptodev->data->dev_id;
934 cryptodev_fp_ops_reset(rte_crypto_fp_ops + dev_id);
936 /* Close device only if device operations have been set */
937 if (cryptodev->dev_ops) {
938 ret = rte_cryptodev_close(dev_id);
943 ret = rte_cryptodev_data_free(dev_id, &cryptodev_globals.data[dev_id]);
947 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
948 cryptodev_globals.nb_devs--;
953 rte_cryptodev_queue_pair_count(uint8_t dev_id)
955 struct rte_cryptodev *dev;
957 if (!rte_cryptodev_is_valid_device_data(dev_id)) {
958 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
962 dev = &rte_crypto_devices[dev_id];
963 return dev->data->nb_queue_pairs;
967 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
970 struct rte_cryptodev_info dev_info;
974 if ((dev == NULL) || (nb_qpairs < 1)) {
975 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
980 CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
981 nb_qpairs, dev->data->dev_id);
983 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
985 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
986 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
988 if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
989 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
990 nb_qpairs, dev->data->dev_id);
994 if (dev->data->queue_pairs == NULL) { /* first time configuration */
995 dev->data->queue_pairs = rte_zmalloc_socket(
996 "cryptodev->queue_pairs",
997 sizeof(dev->data->queue_pairs[0]) *
998 dev_info.max_nb_queue_pairs,
999 RTE_CACHE_LINE_SIZE, socket_id);
1001 if (dev->data->queue_pairs == NULL) {
1002 dev->data->nb_queue_pairs = 0;
1003 CDEV_LOG_ERR("failed to get memory for qp meta data, "
1008 } else { /* re-configure */
1010 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
1012 qp = dev->data->queue_pairs;
1014 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
1017 for (i = nb_qpairs; i < old_nb_queues; i++) {
1018 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
1025 dev->data->nb_queue_pairs = nb_qpairs;
1030 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
1032 struct rte_cryptodev *dev;
1035 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1036 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1040 dev = &rte_crypto_devices[dev_id];
1042 if (dev->data->dev_started) {
1044 "device %d must be stopped to allow configuration", dev_id);
1048 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
1050 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1051 cryptodev_cb_cleanup(dev);
1052 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1054 /* Setup new number of queue pairs and reconfigure device. */
1055 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
1058 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
1063 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1064 diag = cryptodev_cb_init(dev);
1065 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1067 CDEV_LOG_ERR("Callback init failed for dev_id=%d", dev_id);
1071 rte_cryptodev_trace_configure(dev_id, config);
1072 return (*dev->dev_ops->dev_configure)(dev, config);
1076 rte_cryptodev_start(uint8_t dev_id)
1078 struct rte_cryptodev *dev;
1081 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
1083 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1084 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1088 dev = &rte_crypto_devices[dev_id];
1090 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1092 if (dev->data->dev_started != 0) {
1093 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
1098 diag = (*dev->dev_ops->dev_start)(dev);
1099 /* expose selection of PMD fast-path functions */
1100 cryptodev_fp_ops_set(rte_crypto_fp_ops + dev_id, dev);
1102 rte_cryptodev_trace_start(dev_id, diag);
1104 dev->data->dev_started = 1;
1112 rte_cryptodev_stop(uint8_t dev_id)
1114 struct rte_cryptodev *dev;
1116 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1117 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1121 dev = &rte_crypto_devices[dev_id];
1123 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1125 if (dev->data->dev_started == 0) {
1126 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
1131 /* point fast-path functions to dummy ones */
1132 cryptodev_fp_ops_reset(rte_crypto_fp_ops + dev_id);
1134 (*dev->dev_ops->dev_stop)(dev);
1135 rte_cryptodev_trace_stop(dev_id);
1136 dev->data->dev_started = 0;
1140 rte_cryptodev_close(uint8_t dev_id)
1142 struct rte_cryptodev *dev;
1145 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1146 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1150 dev = &rte_crypto_devices[dev_id];
1152 /* Device must be stopped before it can be closed */
1153 if (dev->data->dev_started == 1) {
1154 CDEV_LOG_ERR("Device %u must be stopped before closing",
1159 /* We can't close the device if there are outstanding sessions in use */
1160 if (dev->data->session_pool != NULL) {
1161 if (!rte_mempool_full(dev->data->session_pool)) {
1162 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
1163 "has sessions still in use, free "
1164 "all sessions before calling close",
1170 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
1171 retval = (*dev->dev_ops->dev_close)(dev);
1172 rte_cryptodev_trace_close(dev_id, retval);
1181 rte_cryptodev_get_qp_status(uint8_t dev_id, uint16_t queue_pair_id)
1183 struct rte_cryptodev *dev;
1185 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1186 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1190 dev = &rte_crypto_devices[dev_id];
1191 if (queue_pair_id >= dev->data->nb_queue_pairs) {
1192 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1195 void **qps = dev->data->queue_pairs;
1197 if (qps[queue_pair_id]) {
1198 CDEV_LOG_DEBUG("qp %d on dev %d is initialised",
1199 queue_pair_id, dev_id);
1203 CDEV_LOG_DEBUG("qp %d on dev %d is not initialised",
1204 queue_pair_id, dev_id);
1210 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
1211 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
1214 struct rte_cryptodev *dev;
1216 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1217 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1221 dev = &rte_crypto_devices[dev_id];
1222 if (queue_pair_id >= dev->data->nb_queue_pairs) {
1223 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1228 CDEV_LOG_ERR("qp_conf cannot be NULL\n");
1232 if ((qp_conf->mp_session && !qp_conf->mp_session_private) ||
1233 (!qp_conf->mp_session && qp_conf->mp_session_private)) {
1234 CDEV_LOG_ERR("Invalid mempools\n");
1238 if (qp_conf->mp_session) {
1239 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1240 uint32_t obj_size = qp_conf->mp_session->elt_size;
1241 uint32_t obj_priv_size = qp_conf->mp_session_private->elt_size;
1242 struct rte_cryptodev_sym_session s = {0};
1244 pool_priv = rte_mempool_get_priv(qp_conf->mp_session);
1245 if (!pool_priv || qp_conf->mp_session->private_data_size <
1246 sizeof(*pool_priv)) {
1247 CDEV_LOG_ERR("Invalid mempool\n");
1251 s.nb_drivers = pool_priv->nb_drivers;
1252 s.user_data_sz = pool_priv->user_data_sz;
1254 if ((rte_cryptodev_sym_get_existing_header_session_size(&s) >
1255 obj_size) || (s.nb_drivers <= dev->driver_id) ||
1256 rte_cryptodev_sym_get_private_session_size(dev_id) >
1258 CDEV_LOG_ERR("Invalid mempool\n");
1263 if (dev->data->dev_started) {
1265 "device %d must be stopped to allow configuration", dev_id);
1269 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
1271 rte_cryptodev_trace_queue_pair_setup(dev_id, queue_pair_id, qp_conf);
1272 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
1276 struct rte_cryptodev_cb *
1277 rte_cryptodev_add_enq_callback(uint8_t dev_id,
1279 rte_cryptodev_callback_fn cb_fn,
1282 struct rte_cryptodev *dev;
1283 struct rte_cryptodev_cb_rcu *list;
1284 struct rte_cryptodev_cb *cb, *tail;
1287 CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1292 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1293 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1298 dev = &rte_crypto_devices[dev_id];
1299 if (qp_id >= dev->data->nb_queue_pairs) {
1300 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1305 cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1307 CDEV_LOG_ERR("Failed to allocate memory for callback on "
1308 "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1313 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1318 /* Add the callbacks in fifo order. */
1319 list = &dev->enq_cbs[qp_id];
1325 /* Stores to cb->fn and cb->param should complete before
1326 * cb is visible to data plane.
1328 __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
1330 /* Stores to cb->fn and cb->param should complete before
1331 * cb is visible to data plane.
1333 __atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
1336 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1342 rte_cryptodev_remove_enq_callback(uint8_t dev_id,
1344 struct rte_cryptodev_cb *cb)
1346 struct rte_cryptodev *dev;
1347 struct rte_cryptodev_cb **prev_cb, *curr_cb;
1348 struct rte_cryptodev_cb_rcu *list;
1354 CDEV_LOG_ERR("Callback is NULL");
1358 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1359 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1363 dev = &rte_crypto_devices[dev_id];
1364 if (qp_id >= dev->data->nb_queue_pairs) {
1365 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1369 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1370 if (dev->enq_cbs == NULL) {
1371 CDEV_LOG_ERR("Callback not initialized");
1375 list = &dev->enq_cbs[qp_id];
1377 CDEV_LOG_ERR("Callback list is NULL");
1381 if (list->qsbr == NULL) {
1382 CDEV_LOG_ERR("Rcu qsbr is NULL");
1386 prev_cb = &list->next;
1387 for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1389 if (curr_cb == cb) {
1390 /* Remove the user cb from the callback list. */
1391 __atomic_store_n(prev_cb, curr_cb->next,
1399 /* Call sync with invalid thread id as this is part of
1402 rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1407 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1411 struct rte_cryptodev_cb *
1412 rte_cryptodev_add_deq_callback(uint8_t dev_id,
1414 rte_cryptodev_callback_fn cb_fn,
1417 struct rte_cryptodev *dev;
1418 struct rte_cryptodev_cb_rcu *list;
1419 struct rte_cryptodev_cb *cb, *tail;
1422 CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1427 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1428 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1433 dev = &rte_crypto_devices[dev_id];
1434 if (qp_id >= dev->data->nb_queue_pairs) {
1435 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1440 cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1442 CDEV_LOG_ERR("Failed to allocate memory for callback on "
1443 "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1448 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1453 /* Add the callbacks in fifo order. */
1454 list = &dev->deq_cbs[qp_id];
1460 /* Stores to cb->fn and cb->param should complete before
1461 * cb is visible to data plane.
1463 __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
1465 /* Stores to cb->fn and cb->param should complete before
1466 * cb is visible to data plane.
1468 __atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
1471 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1477 rte_cryptodev_remove_deq_callback(uint8_t dev_id,
1479 struct rte_cryptodev_cb *cb)
1481 struct rte_cryptodev *dev;
1482 struct rte_cryptodev_cb **prev_cb, *curr_cb;
1483 struct rte_cryptodev_cb_rcu *list;
1489 CDEV_LOG_ERR("Callback is NULL");
1493 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1494 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1498 dev = &rte_crypto_devices[dev_id];
1499 if (qp_id >= dev->data->nb_queue_pairs) {
1500 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1504 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1505 if (dev->enq_cbs == NULL) {
1506 CDEV_LOG_ERR("Callback not initialized");
1510 list = &dev->deq_cbs[qp_id];
1512 CDEV_LOG_ERR("Callback list is NULL");
1516 if (list->qsbr == NULL) {
1517 CDEV_LOG_ERR("Rcu qsbr is NULL");
1521 prev_cb = &list->next;
1522 for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1524 if (curr_cb == cb) {
1525 /* Remove the user cb from the callback list. */
1526 __atomic_store_n(prev_cb, curr_cb->next,
1534 /* Call sync with invalid thread id as this is part of
1537 rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1542 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1547 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
1549 struct rte_cryptodev *dev;
1551 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1552 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1556 if (stats == NULL) {
1557 CDEV_LOG_ERR("Invalid stats ptr");
1561 dev = &rte_crypto_devices[dev_id];
1562 memset(stats, 0, sizeof(*stats));
1564 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1565 (*dev->dev_ops->stats_get)(dev, stats);
1570 rte_cryptodev_stats_reset(uint8_t dev_id)
1572 struct rte_cryptodev *dev;
1574 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1575 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1579 dev = &rte_crypto_devices[dev_id];
1581 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
1582 (*dev->dev_ops->stats_reset)(dev);
1586 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
1588 struct rte_cryptodev *dev;
1590 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1591 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1595 dev = &rte_crypto_devices[dev_id];
1597 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
1599 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1600 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1602 dev_info->driver_name = dev->device->driver->name;
1603 dev_info->device = dev->device;
1607 rte_cryptodev_callback_register(uint8_t dev_id,
1608 enum rte_cryptodev_event_type event,
1609 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1611 struct rte_cryptodev *dev;
1612 struct rte_cryptodev_callback *user_cb;
1617 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1618 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1622 dev = &rte_crypto_devices[dev_id];
1623 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1625 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
1626 if (user_cb->cb_fn == cb_fn &&
1627 user_cb->cb_arg == cb_arg &&
1628 user_cb->event == event) {
1633 /* create a new callback. */
1634 if (user_cb == NULL) {
1635 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
1636 sizeof(struct rte_cryptodev_callback), 0);
1637 if (user_cb != NULL) {
1638 user_cb->cb_fn = cb_fn;
1639 user_cb->cb_arg = cb_arg;
1640 user_cb->event = event;
1641 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
1645 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1646 return (user_cb == NULL) ? -ENOMEM : 0;
1650 rte_cryptodev_callback_unregister(uint8_t dev_id,
1651 enum rte_cryptodev_event_type event,
1652 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1655 struct rte_cryptodev *dev;
1656 struct rte_cryptodev_callback *cb, *next;
1661 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1662 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1666 dev = &rte_crypto_devices[dev_id];
1667 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1670 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
1672 next = TAILQ_NEXT(cb, next);
1674 if (cb->cb_fn != cb_fn || cb->event != event ||
1675 (cb->cb_arg != (void *)-1 &&
1676 cb->cb_arg != cb_arg))
1680 * if this callback is not executing right now,
1683 if (cb->active == 0) {
1684 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1691 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1696 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1697 enum rte_cryptodev_event_type event)
1699 struct rte_cryptodev_callback *cb_lst;
1700 struct rte_cryptodev_callback dev_cb;
1702 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1703 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1704 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1708 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1709 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1711 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1714 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1718 rte_cryptodev_sym_session_init(uint8_t dev_id,
1719 struct rte_cryptodev_sym_session *sess,
1720 struct rte_crypto_sym_xform *xforms,
1721 struct rte_mempool *mp)
1723 struct rte_cryptodev *dev;
1724 uint32_t sess_priv_sz = rte_cryptodev_sym_get_private_session_size(
1729 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1730 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1734 dev = rte_cryptodev_pmd_get_dev(dev_id);
1736 if (sess == NULL || xforms == NULL || dev == NULL || mp == NULL)
1739 if (mp->elt_size < sess_priv_sz)
1742 index = dev->driver_id;
1743 if (index >= sess->nb_drivers)
1746 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_configure, -ENOTSUP);
1748 if (sess->sess_data[index].refcnt == 0) {
1749 ret = dev->dev_ops->sym_session_configure(dev, xforms,
1753 "dev_id %d failed to configure session details",
1759 rte_cryptodev_trace_sym_session_init(dev_id, sess, xforms, mp);
1760 sess->sess_data[index].refcnt++;
1764 struct rte_mempool *
1765 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1766 uint32_t elt_size, uint32_t cache_size, uint16_t user_data_size,
1769 struct rte_mempool *mp;
1770 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1773 obj_sz = rte_cryptodev_sym_get_header_session_size() + user_data_size;
1774 if (obj_sz > elt_size)
1775 CDEV_LOG_INFO("elt_size %u is expanded to %u\n", elt_size,
1780 mp = rte_mempool_create(name, nb_elts, obj_sz, cache_size,
1781 (uint32_t)(sizeof(*pool_priv)),
1782 NULL, NULL, NULL, NULL,
1785 CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
1786 __func__, name, rte_errno);
1790 pool_priv = rte_mempool_get_priv(mp);
1792 CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
1794 rte_mempool_free(mp);
1798 pool_priv->nb_drivers = nb_drivers;
1799 pool_priv->user_data_sz = user_data_size;
1801 rte_cryptodev_trace_sym_session_pool_create(name, nb_elts,
1802 elt_size, cache_size, user_data_size, mp);
1806 struct rte_mempool *
1807 rte_cryptodev_asym_session_pool_create(const char *name, uint32_t nb_elts,
1808 uint32_t cache_size, uint16_t user_data_size, int socket_id)
1810 struct rte_mempool *mp;
1811 struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
1812 uint32_t obj_sz, obj_sz_aligned;
1813 uint8_t dev_id, priv_sz, max_priv_sz = 0;
1815 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
1816 if (rte_cryptodev_is_valid_dev(dev_id)) {
1817 priv_sz = rte_cryptodev_asym_get_private_session_size(dev_id);
1818 if (priv_sz > max_priv_sz)
1819 max_priv_sz = priv_sz;
1821 if (max_priv_sz == 0) {
1822 CDEV_LOG_INFO("Could not set max private session size\n");
1826 obj_sz = rte_cryptodev_asym_get_header_session_size() + max_priv_sz +
1828 obj_sz_aligned = RTE_ALIGN_CEIL(obj_sz, RTE_CACHE_LINE_SIZE);
1830 mp = rte_mempool_create(name, nb_elts, obj_sz_aligned, cache_size,
1831 (uint32_t)(sizeof(*pool_priv)),
1832 NULL, NULL, NULL, NULL,
1835 CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
1836 __func__, name, rte_errno);
1840 pool_priv = rte_mempool_get_priv(mp);
1842 CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
1844 rte_mempool_free(mp);
1847 pool_priv->max_priv_session_sz = max_priv_sz;
1848 pool_priv->user_data_sz = user_data_size;
1850 rte_cryptodev_trace_asym_session_pool_create(name, nb_elts,
1851 user_data_size, cache_size, mp);
1856 rte_cryptodev_sym_session_data_size(struct rte_cryptodev_sym_session *sess)
1858 return (sizeof(sess->sess_data[0]) * sess->nb_drivers) +
1863 rte_cryptodev_sym_is_valid_session_pool(struct rte_mempool *mp)
1865 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1870 pool_priv = rte_mempool_get_priv(mp);
1872 if (!pool_priv || mp->private_data_size < sizeof(*pool_priv) ||
1873 pool_priv->nb_drivers != nb_drivers ||
1875 rte_cryptodev_sym_get_header_session_size()
1876 + pool_priv->user_data_sz)
1882 struct rte_cryptodev_sym_session *
1883 rte_cryptodev_sym_session_create(struct rte_mempool *mp)
1885 struct rte_cryptodev_sym_session *sess;
1886 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1888 if (!rte_cryptodev_sym_is_valid_session_pool(mp)) {
1889 CDEV_LOG_ERR("Invalid mempool\n");
1893 pool_priv = rte_mempool_get_priv(mp);
1895 /* Allocate a session structure from the session pool */
1896 if (rte_mempool_get(mp, (void **)&sess)) {
1897 CDEV_LOG_ERR("couldn't get object from session mempool");
1901 sess->nb_drivers = pool_priv->nb_drivers;
1902 sess->user_data_sz = pool_priv->user_data_sz;
1903 sess->opaque_data = 0;
1905 /* Clear device session pointer.
1906 * Include the flag indicating presence of user data
1908 memset(sess->sess_data, 0,
1909 rte_cryptodev_sym_session_data_size(sess));
1911 rte_cryptodev_trace_sym_session_create(mp, sess);
1916 rte_cryptodev_asym_session_create(uint8_t dev_id,
1917 struct rte_crypto_asym_xform *xforms, struct rte_mempool *mp,
1920 struct rte_cryptodev_asym_session *sess;
1921 uint32_t session_priv_data_sz;
1922 struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
1923 unsigned int session_header_size =
1924 rte_cryptodev_asym_get_header_session_size();
1925 struct rte_cryptodev *dev;
1928 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1929 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1933 dev = rte_cryptodev_pmd_get_dev(dev_id);
1939 CDEV_LOG_ERR("invalid mempool\n");
1943 session_priv_data_sz = rte_cryptodev_asym_get_private_session_size(
1945 pool_priv = rte_mempool_get_priv(mp);
1947 if (pool_priv->max_priv_session_sz < session_priv_data_sz) {
1949 "The private session data size used when creating the mempool is smaller than this device's private session data.");
1953 /* Verify if provided mempool can hold elements big enough. */
1954 if (mp->elt_size < session_header_size + session_priv_data_sz) {
1956 "mempool elements too small to hold session objects");
1960 /* Allocate a session structure from the session pool */
1961 if (rte_mempool_get(mp, session)) {
1962 CDEV_LOG_ERR("couldn't get object from session mempool");
1967 sess->driver_id = dev->driver_id;
1968 sess->user_data_sz = pool_priv->user_data_sz;
1969 sess->max_priv_data_sz = pool_priv->max_priv_session_sz;
1971 /* Clear device session pointer.*/
1972 memset(sess->sess_private_data, 0, session_priv_data_sz + sess->user_data_sz);
1974 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_configure, -ENOTSUP);
1976 if (sess->sess_private_data[0] == 0) {
1977 ret = dev->dev_ops->asym_session_configure(dev, xforms, sess);
1980 "dev_id %d failed to configure session details",
1986 rte_cryptodev_trace_asym_session_create(dev_id, xforms, mp, sess);
1991 rte_cryptodev_sym_session_clear(uint8_t dev_id,
1992 struct rte_cryptodev_sym_session *sess)
1994 struct rte_cryptodev *dev;
1997 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1998 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2002 dev = rte_cryptodev_pmd_get_dev(dev_id);
2004 if (dev == NULL || sess == NULL)
2007 driver_id = dev->driver_id;
2008 if (sess->sess_data[driver_id].refcnt == 0)
2010 if (--sess->sess_data[driver_id].refcnt != 0)
2013 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_clear, -ENOTSUP);
2015 dev->dev_ops->sym_session_clear(dev, sess);
2017 rte_cryptodev_trace_sym_session_clear(dev_id, sess);
2022 rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess)
2025 struct rte_mempool *sess_mp;
2030 /* Check that all device private data has been freed */
2031 for (i = 0; i < sess->nb_drivers; i++) {
2032 if (sess->sess_data[i].refcnt != 0)
2036 /* Return session to mempool */
2037 sess_mp = rte_mempool_from_obj(sess);
2038 rte_mempool_put(sess_mp, sess);
2040 rte_cryptodev_trace_sym_session_free(sess);
2045 rte_cryptodev_asym_session_free(uint8_t dev_id, void *sess)
2047 struct rte_mempool *sess_mp;
2048 struct rte_cryptodev *dev;
2050 if (!rte_cryptodev_is_valid_dev(dev_id)) {
2051 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2055 dev = rte_cryptodev_pmd_get_dev(dev_id);
2057 if (dev == NULL || sess == NULL)
2060 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_clear, -ENOTSUP);
2062 dev->dev_ops->asym_session_clear(dev, sess);
2064 /* Return session to mempool */
2065 sess_mp = rte_mempool_from_obj(sess);
2066 rte_mempool_put(sess_mp, sess);
2068 rte_cryptodev_trace_asym_session_free(dev_id, sess);
2073 rte_cryptodev_sym_get_header_session_size(void)
2076 * Header contains pointers to the private data of all registered
2077 * drivers and all necessary information to ensure safely clear
2078 * or free al session.
2080 struct rte_cryptodev_sym_session s = {0};
2082 s.nb_drivers = nb_drivers;
2084 return (unsigned int)(sizeof(s) +
2085 rte_cryptodev_sym_session_data_size(&s));
2089 rte_cryptodev_sym_get_existing_header_session_size(
2090 struct rte_cryptodev_sym_session *sess)
2095 return (unsigned int)(sizeof(*sess) +
2096 rte_cryptodev_sym_session_data_size(sess));
2100 rte_cryptodev_asym_get_header_session_size(void)
2102 return sizeof(struct rte_cryptodev_asym_session);
2106 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)
2108 struct rte_cryptodev *dev;
2109 unsigned int priv_sess_size;
2111 if (!rte_cryptodev_is_valid_dev(dev_id))
2114 dev = rte_cryptodev_pmd_get_dev(dev_id);
2116 if (*dev->dev_ops->sym_session_get_size == NULL)
2119 priv_sess_size = (*dev->dev_ops->sym_session_get_size)(dev);
2121 return priv_sess_size;
2125 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)
2127 struct rte_cryptodev *dev;
2128 unsigned int priv_sess_size;
2130 if (!rte_cryptodev_is_valid_dev(dev_id))
2133 dev = rte_cryptodev_pmd_get_dev(dev_id);
2135 if (*dev->dev_ops->asym_session_get_size == NULL)
2138 priv_sess_size = (*dev->dev_ops->asym_session_get_size)(dev);
2140 return priv_sess_size;
2144 rte_cryptodev_sym_session_set_user_data(
2145 struct rte_cryptodev_sym_session *sess,
2152 if (sess->user_data_sz < size)
2155 rte_memcpy(sess->sess_data + sess->nb_drivers, data, size);
2160 rte_cryptodev_sym_session_get_user_data(
2161 struct rte_cryptodev_sym_session *sess)
2163 if (sess == NULL || sess->user_data_sz == 0)
2166 return (void *)(sess->sess_data + sess->nb_drivers);
2170 rte_cryptodev_asym_session_set_user_data(void *session, void *data, uint16_t size)
2172 struct rte_cryptodev_asym_session *sess = session;
2176 if (sess->user_data_sz < size)
2179 rte_memcpy(sess->sess_private_data +
2180 sess->max_priv_data_sz,
2186 rte_cryptodev_asym_session_get_user_data(void *session)
2188 struct rte_cryptodev_asym_session *sess = session;
2189 if (sess == NULL || sess->user_data_sz == 0)
2192 return (void *)(sess->sess_private_data +
2193 sess->max_priv_data_sz);
2197 sym_crypto_fill_status(struct rte_crypto_sym_vec *vec, int32_t errnum)
2200 for (i = 0; i < vec->num; i++)
2201 vec->status[i] = errnum;
2205 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
2206 struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs ofs,
2207 struct rte_crypto_sym_vec *vec)
2209 struct rte_cryptodev *dev;
2211 if (!rte_cryptodev_is_valid_dev(dev_id)) {
2212 sym_crypto_fill_status(vec, EINVAL);
2216 dev = rte_cryptodev_pmd_get_dev(dev_id);
2218 if (*dev->dev_ops->sym_cpu_process == NULL ||
2219 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) {
2220 sym_crypto_fill_status(vec, ENOTSUP);
2224 return dev->dev_ops->sym_cpu_process(dev, sess, ofs, vec);
2228 rte_cryptodev_get_raw_dp_ctx_size(uint8_t dev_id)
2230 struct rte_cryptodev *dev;
2231 int32_t size = sizeof(struct rte_crypto_raw_dp_ctx);
2234 if (!rte_cryptodev_is_valid_dev(dev_id))
2237 dev = rte_cryptodev_pmd_get_dev(dev_id);
2239 if (*dev->dev_ops->sym_get_raw_dp_ctx_size == NULL ||
2240 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)) {
2244 priv_size = (*dev->dev_ops->sym_get_raw_dp_ctx_size)(dev);
2248 return RTE_ALIGN_CEIL((size + priv_size), 8);
2252 rte_cryptodev_configure_raw_dp_ctx(uint8_t dev_id, uint16_t qp_id,
2253 struct rte_crypto_raw_dp_ctx *ctx,
2254 enum rte_crypto_op_sess_type sess_type,
2255 union rte_cryptodev_session_ctx session_ctx,
2258 struct rte_cryptodev *dev;
2260 if (!rte_cryptodev_get_qp_status(dev_id, qp_id))
2263 dev = rte_cryptodev_pmd_get_dev(dev_id);
2264 if (!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)
2265 || dev->dev_ops->sym_configure_raw_dp_ctx == NULL)
2268 return (*dev->dev_ops->sym_configure_raw_dp_ctx)(dev, qp_id, ctx,
2269 sess_type, session_ctx, is_update);
2273 rte_cryptodev_raw_enqueue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2274 struct rte_crypto_sym_vec *vec, union rte_crypto_sym_ofs ofs,
2275 void **user_data, int *enqueue_status)
2277 return (*ctx->enqueue_burst)(ctx->qp_data, ctx->drv_ctx_data, vec,
2278 ofs, user_data, enqueue_status);
2282 rte_cryptodev_raw_enqueue_done(struct rte_crypto_raw_dp_ctx *ctx,
2285 return (*ctx->enqueue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2289 rte_cryptodev_raw_dequeue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2290 rte_cryptodev_raw_get_dequeue_count_t get_dequeue_count,
2291 uint32_t max_nb_to_dequeue,
2292 rte_cryptodev_raw_post_dequeue_t post_dequeue,
2293 void **out_user_data, uint8_t is_user_data_array,
2294 uint32_t *n_success_jobs, int *status)
2296 return (*ctx->dequeue_burst)(ctx->qp_data, ctx->drv_ctx_data,
2297 get_dequeue_count, max_nb_to_dequeue, post_dequeue,
2298 out_user_data, is_user_data_array, n_success_jobs, status);
2302 rte_cryptodev_raw_dequeue_done(struct rte_crypto_raw_dp_ctx *ctx,
2305 return (*ctx->dequeue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2308 /** Initialise rte_crypto_op mempool element */
2310 rte_crypto_op_init(struct rte_mempool *mempool,
2313 __rte_unused unsigned i)
2315 struct rte_crypto_op *op = _op_data;
2316 enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
2318 memset(_op_data, 0, mempool->elt_size);
2320 __rte_crypto_op_reset(op, type);
2322 op->phys_addr = rte_mem_virt2iova(_op_data);
2323 op->mempool = mempool;
2327 struct rte_mempool *
2328 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
2329 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
2332 struct rte_crypto_op_pool_private *priv;
2334 unsigned elt_size = sizeof(struct rte_crypto_op) +
2337 if (type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
2338 elt_size += sizeof(struct rte_crypto_sym_op);
2339 } else if (type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
2340 elt_size += sizeof(struct rte_crypto_asym_op);
2341 } else if (type == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2342 elt_size += RTE_MAX(sizeof(struct rte_crypto_sym_op),
2343 sizeof(struct rte_crypto_asym_op));
2345 CDEV_LOG_ERR("Invalid op_type\n");
2349 /* lookup mempool in case already allocated */
2350 struct rte_mempool *mp = rte_mempool_lookup(name);
2353 priv = (struct rte_crypto_op_pool_private *)
2354 rte_mempool_get_priv(mp);
2356 if (mp->elt_size != elt_size ||
2357 mp->cache_size < cache_size ||
2358 mp->size < nb_elts ||
2359 priv->priv_size < priv_size) {
2361 CDEV_LOG_ERR("Mempool %s already exists but with "
2362 "incompatible parameters", name);
2368 mp = rte_mempool_create(
2373 sizeof(struct rte_crypto_op_pool_private),
2382 CDEV_LOG_ERR("Failed to create mempool %s", name);
2386 priv = (struct rte_crypto_op_pool_private *)
2387 rte_mempool_get_priv(mp);
2389 priv->priv_size = priv_size;
2396 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix)
2398 struct rte_cryptodev *dev = NULL;
2404 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
2405 int ret = snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
2406 "%s_%u", dev_name_prefix, i);
2411 dev = rte_cryptodev_pmd_get_named_dev(name);
2419 TAILQ_HEAD(cryptodev_driver_list, cryptodev_driver);
2421 static struct cryptodev_driver_list cryptodev_driver_list =
2422 TAILQ_HEAD_INITIALIZER(cryptodev_driver_list);
2425 rte_cryptodev_driver_id_get(const char *name)
2427 struct cryptodev_driver *driver;
2428 const char *driver_name;
2431 RTE_LOG(DEBUG, CRYPTODEV, "name pointer NULL");
2435 TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
2436 driver_name = driver->driver->name;
2437 if (strncmp(driver_name, name, strlen(driver_name) + 1) == 0)
2444 rte_cryptodev_name_get(uint8_t dev_id)
2446 struct rte_cryptodev *dev;
2448 if (!rte_cryptodev_is_valid_device_data(dev_id)) {
2449 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2453 dev = rte_cryptodev_pmd_get_dev(dev_id);
2457 return dev->data->name;
2461 rte_cryptodev_driver_name_get(uint8_t driver_id)
2463 struct cryptodev_driver *driver;
2465 TAILQ_FOREACH(driver, &cryptodev_driver_list, next)
2466 if (driver->id == driver_id)
2467 return driver->driver->name;
2472 rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
2473 const struct rte_driver *drv)
2475 crypto_drv->driver = drv;
2476 crypto_drv->id = nb_drivers;
2478 TAILQ_INSERT_TAIL(&cryptodev_driver_list, crypto_drv, next);
2480 return nb_drivers++;
2483 RTE_INIT(cryptodev_init_fp_ops)
2487 for (i = 0; i != RTE_DIM(rte_crypto_fp_ops); i++)
2488 cryptodev_fp_ops_reset(rte_crypto_fp_ops + i);
2492 cryptodev_handle_dev_list(const char *cmd __rte_unused,
2493 const char *params __rte_unused,
2494 struct rte_tel_data *d)
2498 if (rte_cryptodev_count() < 1)
2501 rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
2502 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
2503 if (rte_cryptodev_is_valid_dev(dev_id))
2504 rte_tel_data_add_array_int(d, dev_id);
2510 cryptodev_handle_dev_info(const char *cmd __rte_unused,
2511 const char *params, struct rte_tel_data *d)
2513 struct rte_cryptodev_info cryptodev_info;
2517 if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2520 dev_id = strtoul(params, &end_param, 0);
2521 if (*end_param != '\0')
2522 CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2523 if (!rte_cryptodev_is_valid_dev(dev_id))
2526 rte_cryptodev_info_get(dev_id, &cryptodev_info);
2528 rte_tel_data_start_dict(d);
2529 rte_tel_data_add_dict_string(d, "device_name",
2530 cryptodev_info.device->name);
2531 rte_tel_data_add_dict_int(d, "max_nb_queue_pairs",
2532 cryptodev_info.max_nb_queue_pairs);
2537 #define ADD_DICT_STAT(s) rte_tel_data_add_dict_u64(d, #s, cryptodev_stats.s)
2540 cryptodev_handle_dev_stats(const char *cmd __rte_unused,
2542 struct rte_tel_data *d)
2544 struct rte_cryptodev_stats cryptodev_stats;
2548 if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2551 dev_id = strtoul(params, &end_param, 0);
2552 if (*end_param != '\0')
2553 CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2554 if (!rte_cryptodev_is_valid_dev(dev_id))
2557 ret = rte_cryptodev_stats_get(dev_id, &cryptodev_stats);
2561 rte_tel_data_start_dict(d);
2562 ADD_DICT_STAT(enqueued_count);
2563 ADD_DICT_STAT(dequeued_count);
2564 ADD_DICT_STAT(enqueue_err_count);
2565 ADD_DICT_STAT(dequeue_err_count);
2570 #define CRYPTO_CAPS_SZ \
2571 (RTE_ALIGN_CEIL(sizeof(struct rte_cryptodev_capabilities), \
2572 sizeof(uint64_t)) / \
2576 crypto_caps_array(struct rte_tel_data *d,
2577 const struct rte_cryptodev_capabilities *capabilities)
2579 const struct rte_cryptodev_capabilities *dev_caps;
2580 uint64_t caps_val[CRYPTO_CAPS_SZ];
2581 unsigned int i = 0, j;
2583 rte_tel_data_start_array(d, RTE_TEL_U64_VAL);
2585 while ((dev_caps = &capabilities[i++])->op !=
2586 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2587 memset(&caps_val, 0, CRYPTO_CAPS_SZ * sizeof(caps_val[0]));
2588 rte_memcpy(caps_val, dev_caps, sizeof(capabilities[0]));
2589 for (j = 0; j < CRYPTO_CAPS_SZ; j++)
2590 rte_tel_data_add_array_u64(d, caps_val[j]);
2597 cryptodev_handle_dev_caps(const char *cmd __rte_unused, const char *params,
2598 struct rte_tel_data *d)
2600 struct rte_cryptodev_info dev_info;
2601 struct rte_tel_data *crypto_caps;
2606 if (!params || strlen(params) == 0 || !isdigit(*params))
2609 dev_id = strtoul(params, &end_param, 0);
2610 if (*end_param != '\0')
2611 CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2612 if (!rte_cryptodev_is_valid_dev(dev_id))
2615 rte_tel_data_start_dict(d);
2616 crypto_caps = rte_tel_data_alloc();
2620 rte_cryptodev_info_get(dev_id, &dev_info);
2621 crypto_caps_n = crypto_caps_array(crypto_caps, dev_info.capabilities);
2622 rte_tel_data_add_dict_container(d, "crypto_caps", crypto_caps, 0);
2623 rte_tel_data_add_dict_int(d, "crypto_caps_n", crypto_caps_n);
2628 RTE_INIT(cryptodev_init_telemetry)
2630 rte_telemetry_register_cmd("/cryptodev/info", cryptodev_handle_dev_info,
2631 "Returns information for a cryptodev. Parameters: int dev_id");
2632 rte_telemetry_register_cmd("/cryptodev/list",
2633 cryptodev_handle_dev_list,
2634 "Returns list of available crypto devices by IDs. No parameters.");
2635 rte_telemetry_register_cmd("/cryptodev/stats",
2636 cryptodev_handle_dev_stats,
2637 "Returns the stats for a cryptodev. Parameters: int dev_id");
2638 rte_telemetry_register_cmd("/cryptodev/caps",
2639 cryptodev_handle_dev_caps,
2640 "Returns the capabilities for a cryptodev. Parameters: int dev_id");