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 */
216 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
217 const char *algo_string)
221 for (i = 1; i < RTE_DIM(rte_crypto_cipher_algorithm_strings); i++) {
222 if (strcmp(algo_string, rte_crypto_cipher_algorithm_strings[i]) == 0) {
223 *algo_enum = (enum rte_crypto_cipher_algorithm) i;
233 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
234 const char *algo_string)
238 for (i = 1; i < RTE_DIM(rte_crypto_auth_algorithm_strings); i++) {
239 if (strcmp(algo_string, rte_crypto_auth_algorithm_strings[i]) == 0) {
240 *algo_enum = (enum rte_crypto_auth_algorithm) i;
250 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
251 const char *algo_string)
255 for (i = 1; i < RTE_DIM(rte_crypto_aead_algorithm_strings); i++) {
256 if (strcmp(algo_string, rte_crypto_aead_algorithm_strings[i]) == 0) {
257 *algo_enum = (enum rte_crypto_aead_algorithm) i;
267 rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type *xform_enum,
268 const char *xform_string)
272 for (i = 1; i < RTE_DIM(rte_crypto_asym_xform_strings); i++) {
273 if (strcmp(xform_string,
274 rte_crypto_asym_xform_strings[i]) == 0) {
275 *xform_enum = (enum rte_crypto_asym_xform_type) i;
285 * The crypto auth operation strings identifiers.
286 * It could be used in application command line.
289 rte_crypto_auth_operation_strings[] = {
290 [RTE_CRYPTO_AUTH_OP_VERIFY] = "verify",
291 [RTE_CRYPTO_AUTH_OP_GENERATE] = "generate"
294 const struct rte_cryptodev_symmetric_capability *
295 rte_cryptodev_sym_capability_get(uint8_t dev_id,
296 const struct rte_cryptodev_sym_capability_idx *idx)
298 const struct rte_cryptodev_capabilities *capability;
299 struct rte_cryptodev_info dev_info;
302 rte_cryptodev_info_get(dev_id, &dev_info);
304 while ((capability = &dev_info.capabilities[i++])->op !=
305 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
306 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
309 if (capability->sym.xform_type != idx->type)
312 if (idx->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
313 capability->sym.auth.algo == idx->algo.auth)
314 return &capability->sym;
316 if (idx->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
317 capability->sym.cipher.algo == idx->algo.cipher)
318 return &capability->sym;
320 if (idx->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
321 capability->sym.aead.algo == idx->algo.aead)
322 return &capability->sym;
329 param_range_check(uint16_t size, const struct rte_crypto_param_range *range)
331 unsigned int next_size;
333 /* Check lower/upper bounds */
334 if (size < range->min)
337 if (size > range->max)
340 /* If range is actually only one value, size is correct */
341 if (range->increment == 0)
344 /* Check if value is one of the supported sizes */
345 for (next_size = range->min; next_size <= range->max;
346 next_size += range->increment)
347 if (size == next_size)
353 const struct rte_cryptodev_asymmetric_xform_capability *
354 rte_cryptodev_asym_capability_get(uint8_t dev_id,
355 const struct rte_cryptodev_asym_capability_idx *idx)
357 const struct rte_cryptodev_capabilities *capability;
358 struct rte_cryptodev_info dev_info;
361 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
362 rte_cryptodev_info_get(dev_id, &dev_info);
364 while ((capability = &dev_info.capabilities[i++])->op !=
365 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
366 if (capability->op != RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
369 if (capability->asym.xform_capa.xform_type == idx->type)
370 return &capability->asym.xform_capa;
376 rte_cryptodev_sym_capability_check_cipher(
377 const struct rte_cryptodev_symmetric_capability *capability,
378 uint16_t key_size, uint16_t iv_size)
380 if (param_range_check(key_size, &capability->cipher.key_size) != 0)
383 if (param_range_check(iv_size, &capability->cipher.iv_size) != 0)
390 rte_cryptodev_sym_capability_check_auth(
391 const struct rte_cryptodev_symmetric_capability *capability,
392 uint16_t key_size, uint16_t digest_size, uint16_t iv_size)
394 if (param_range_check(key_size, &capability->auth.key_size) != 0)
397 if (param_range_check(digest_size, &capability->auth.digest_size) != 0)
400 if (param_range_check(iv_size, &capability->auth.iv_size) != 0)
407 rte_cryptodev_sym_capability_check_aead(
408 const struct rte_cryptodev_symmetric_capability *capability,
409 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
412 if (param_range_check(key_size, &capability->aead.key_size) != 0)
415 if (param_range_check(digest_size, &capability->aead.digest_size) != 0)
418 if (param_range_check(aad_size, &capability->aead.aad_size) != 0)
421 if (param_range_check(iv_size, &capability->aead.iv_size) != 0)
427 rte_cryptodev_asym_xform_capability_check_optype(
428 const struct rte_cryptodev_asymmetric_xform_capability *capability,
429 enum rte_crypto_asym_op_type op_type)
431 if (capability->op_types & (1 << op_type))
438 rte_cryptodev_asym_xform_capability_check_modlen(
439 const struct rte_cryptodev_asymmetric_xform_capability *capability,
442 /* no need to check for limits, if min or max = 0 */
443 if (capability->modlen.min != 0) {
444 if (modlen < capability->modlen.min)
448 if (capability->modlen.max != 0) {
449 if (modlen > capability->modlen.max)
453 /* in any case, check if given modlen is module increment */
454 if (capability->modlen.increment != 0) {
455 if (modlen % (capability->modlen.increment))
462 /* spinlock for crypto device enq callbacks */
463 static rte_spinlock_t rte_cryptodev_callback_lock = RTE_SPINLOCK_INITIALIZER;
466 cryptodev_cb_cleanup(struct rte_cryptodev *dev)
468 struct rte_cryptodev_cb_rcu *list;
469 struct rte_cryptodev_cb *cb, *next;
472 if (dev->enq_cbs == NULL && dev->deq_cbs == NULL)
475 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
476 list = &dev->enq_cbs[qp_id];
484 rte_free(list->qsbr);
487 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
488 list = &dev->deq_cbs[qp_id];
496 rte_free(list->qsbr);
499 rte_free(dev->enq_cbs);
501 rte_free(dev->deq_cbs);
506 cryptodev_cb_init(struct rte_cryptodev *dev)
508 struct rte_cryptodev_cb_rcu *list;
509 struct rte_rcu_qsbr *qsbr;
513 /* Max thread set to 1, as one DP thread accessing a queue-pair */
514 const uint32_t max_threads = 1;
516 dev->enq_cbs = rte_zmalloc(NULL,
517 sizeof(struct rte_cryptodev_cb_rcu) *
518 dev->data->nb_queue_pairs, 0);
519 if (dev->enq_cbs == NULL) {
520 CDEV_LOG_ERR("Failed to allocate memory for enq callbacks");
524 dev->deq_cbs = rte_zmalloc(NULL,
525 sizeof(struct rte_cryptodev_cb_rcu) *
526 dev->data->nb_queue_pairs, 0);
527 if (dev->deq_cbs == NULL) {
528 CDEV_LOG_ERR("Failed to allocate memory for deq callbacks");
529 rte_free(dev->enq_cbs);
533 /* Create RCU QSBR variable */
534 size = rte_rcu_qsbr_get_memsize(max_threads);
536 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
537 list = &dev->enq_cbs[qp_id];
538 qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
540 CDEV_LOG_ERR("Failed to allocate memory for RCU on "
541 "queue_pair_id=%d", qp_id);
545 if (rte_rcu_qsbr_init(qsbr, max_threads)) {
546 CDEV_LOG_ERR("Failed to initialize for RCU on "
547 "queue_pair_id=%d", qp_id);
554 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
555 list = &dev->deq_cbs[qp_id];
556 qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
558 CDEV_LOG_ERR("Failed to allocate memory for RCU on "
559 "queue_pair_id=%d", qp_id);
563 if (rte_rcu_qsbr_init(qsbr, max_threads)) {
564 CDEV_LOG_ERR("Failed to initialize for RCU on "
565 "queue_pair_id=%d", qp_id);
575 cryptodev_cb_cleanup(dev);
580 rte_cryptodev_get_feature_name(uint64_t flag)
583 case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
584 return "SYMMETRIC_CRYPTO";
585 case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
586 return "ASYMMETRIC_CRYPTO";
587 case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
588 return "SYM_OPERATION_CHAINING";
589 case RTE_CRYPTODEV_FF_CPU_SSE:
591 case RTE_CRYPTODEV_FF_CPU_AVX:
593 case RTE_CRYPTODEV_FF_CPU_AVX2:
595 case RTE_CRYPTODEV_FF_CPU_AVX512:
597 case RTE_CRYPTODEV_FF_CPU_AESNI:
599 case RTE_CRYPTODEV_FF_HW_ACCELERATED:
600 return "HW_ACCELERATED";
601 case RTE_CRYPTODEV_FF_IN_PLACE_SGL:
602 return "IN_PLACE_SGL";
603 case RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT:
604 return "OOP_SGL_IN_SGL_OUT";
605 case RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT:
606 return "OOP_SGL_IN_LB_OUT";
607 case RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT:
608 return "OOP_LB_IN_SGL_OUT";
609 case RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT:
610 return "OOP_LB_IN_LB_OUT";
611 case RTE_CRYPTODEV_FF_CPU_NEON:
613 case RTE_CRYPTODEV_FF_CPU_ARM_CE:
615 case RTE_CRYPTODEV_FF_SECURITY:
616 return "SECURITY_PROTOCOL";
617 case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP:
618 return "RSA_PRIV_OP_KEY_EXP";
619 case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT:
620 return "RSA_PRIV_OP_KEY_QT";
621 case RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED:
622 return "DIGEST_ENCRYPTED";
623 case RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO:
624 return "SYM_CPU_CRYPTO";
625 case RTE_CRYPTODEV_FF_ASYM_SESSIONLESS:
626 return "ASYM_SESSIONLESS";
627 case RTE_CRYPTODEV_FF_SYM_SESSIONLESS:
628 return "SYM_SESSIONLESS";
629 case RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA:
630 return "NON_BYTE_ALIGNED_DATA";
631 case RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS:
632 return "CIPHER_MULTIPLE_DATA_UNITS";
633 case RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY:
634 return "CIPHER_WRAPPED_KEY";
640 struct rte_cryptodev *
641 rte_cryptodev_pmd_get_dev(uint8_t dev_id)
643 return &cryptodev_globals.devs[dev_id];
646 struct rte_cryptodev *
647 rte_cryptodev_pmd_get_named_dev(const char *name)
649 struct rte_cryptodev *dev;
655 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
656 dev = &cryptodev_globals.devs[i];
658 if ((dev->attached == RTE_CRYPTODEV_ATTACHED) &&
659 (strcmp(dev->data->name, name) == 0))
666 static inline uint8_t
667 rte_cryptodev_is_valid_device_data(uint8_t dev_id)
669 if (dev_id >= RTE_CRYPTO_MAX_DEVS ||
670 rte_crypto_devices[dev_id].data == NULL)
677 rte_cryptodev_is_valid_dev(uint8_t dev_id)
679 struct rte_cryptodev *dev = NULL;
681 if (!rte_cryptodev_is_valid_device_data(dev_id))
684 dev = rte_cryptodev_pmd_get_dev(dev_id);
685 if (dev->attached != RTE_CRYPTODEV_ATTACHED)
693 rte_cryptodev_get_dev_id(const char *name)
700 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
701 if (!rte_cryptodev_is_valid_device_data(i))
703 if ((strcmp(cryptodev_globals.devs[i].data->name, name)
705 (cryptodev_globals.devs[i].attached ==
706 RTE_CRYPTODEV_ATTACHED))
714 rte_cryptodev_count(void)
716 return cryptodev_globals.nb_devs;
720 rte_cryptodev_device_count_by_driver(uint8_t driver_id)
722 uint8_t i, dev_count = 0;
724 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++)
725 if (cryptodev_globals.devs[i].driver_id == driver_id &&
726 cryptodev_globals.devs[i].attached ==
727 RTE_CRYPTODEV_ATTACHED)
734 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
737 uint8_t i, count = 0;
738 struct rte_cryptodev *devs = cryptodev_globals.devs;
740 for (i = 0; i < RTE_CRYPTO_MAX_DEVS && count < nb_devices; i++) {
741 if (!rte_cryptodev_is_valid_device_data(i))
744 if (devs[i].attached == RTE_CRYPTODEV_ATTACHED) {
747 cmp = strncmp(devs[i].device->driver->name,
749 strlen(driver_name) + 1);
752 devices[count++] = devs[i].data->dev_id;
760 rte_cryptodev_get_sec_ctx(uint8_t dev_id)
762 if (dev_id < RTE_CRYPTO_MAX_DEVS &&
763 (rte_crypto_devices[dev_id].feature_flags &
764 RTE_CRYPTODEV_FF_SECURITY))
765 return rte_crypto_devices[dev_id].security_ctx;
771 rte_cryptodev_socket_id(uint8_t dev_id)
773 struct rte_cryptodev *dev;
775 if (!rte_cryptodev_is_valid_dev(dev_id))
778 dev = rte_cryptodev_pmd_get_dev(dev_id);
780 return dev->data->socket_id;
784 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
787 char mz_name[RTE_MEMZONE_NAMESIZE];
788 const struct rte_memzone *mz;
791 /* generate memzone name */
792 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
793 if (n >= (int)sizeof(mz_name))
796 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
797 mz = rte_memzone_reserve(mz_name,
798 sizeof(struct rte_cryptodev_data),
800 CDEV_LOG_DEBUG("PRIMARY:reserved memzone for %s (%p)",
803 mz = rte_memzone_lookup(mz_name);
804 CDEV_LOG_DEBUG("SECONDARY:looked up memzone for %s (%p)",
812 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
813 memset(*data, 0, sizeof(struct rte_cryptodev_data));
819 rte_cryptodev_data_free(uint8_t dev_id, struct rte_cryptodev_data **data)
821 char mz_name[RTE_MEMZONE_NAMESIZE];
822 const struct rte_memzone *mz;
825 /* generate memzone name */
826 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
827 if (n >= (int)sizeof(mz_name))
830 mz = rte_memzone_lookup(mz_name);
834 RTE_ASSERT(*data == mz->addr);
837 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
838 CDEV_LOG_DEBUG("PRIMARY:free memzone of %s (%p)",
840 return rte_memzone_free(mz);
842 CDEV_LOG_DEBUG("SECONDARY:don't free memzone of %s (%p)",
850 rte_cryptodev_find_free_device_index(void)
854 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
855 if (rte_crypto_devices[dev_id].attached ==
856 RTE_CRYPTODEV_DETACHED)
859 return RTE_CRYPTO_MAX_DEVS;
862 struct rte_cryptodev *
863 rte_cryptodev_pmd_allocate(const char *name, int socket_id)
865 struct rte_cryptodev *cryptodev;
868 if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
869 CDEV_LOG_ERR("Crypto device with name %s already "
874 dev_id = rte_cryptodev_find_free_device_index();
875 if (dev_id == RTE_CRYPTO_MAX_DEVS) {
876 CDEV_LOG_ERR("Reached maximum number of crypto devices");
880 cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
882 if (cryptodev->data == NULL) {
883 struct rte_cryptodev_data **cryptodev_data =
884 &cryptodev_globals.data[dev_id];
886 int retval = rte_cryptodev_data_alloc(dev_id, cryptodev_data,
889 if (retval < 0 || *cryptodev_data == NULL)
892 cryptodev->data = *cryptodev_data;
894 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
895 strlcpy(cryptodev->data->name, name,
896 RTE_CRYPTODEV_NAME_MAX_LEN);
898 cryptodev->data->dev_id = dev_id;
899 cryptodev->data->socket_id = socket_id;
900 cryptodev->data->dev_started = 0;
901 CDEV_LOG_DEBUG("PRIMARY:init data");
904 CDEV_LOG_DEBUG("Data for %s: dev_id %d, socket %d, started %d",
905 cryptodev->data->name,
906 cryptodev->data->dev_id,
907 cryptodev->data->socket_id,
908 cryptodev->data->dev_started);
910 /* init user callbacks */
911 TAILQ_INIT(&(cryptodev->link_intr_cbs));
913 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
915 cryptodev_globals.nb_devs++;
922 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
927 if (cryptodev == NULL)
930 dev_id = cryptodev->data->dev_id;
932 cryptodev_fp_ops_reset(rte_crypto_fp_ops + dev_id);
934 /* Close device only if device operations have been set */
935 if (cryptodev->dev_ops) {
936 ret = rte_cryptodev_close(dev_id);
941 ret = rte_cryptodev_data_free(dev_id, &cryptodev_globals.data[dev_id]);
945 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
946 cryptodev_globals.nb_devs--;
951 rte_cryptodev_queue_pair_count(uint8_t dev_id)
953 struct rte_cryptodev *dev;
955 if (!rte_cryptodev_is_valid_device_data(dev_id)) {
956 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
960 dev = &rte_crypto_devices[dev_id];
961 return dev->data->nb_queue_pairs;
965 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
968 struct rte_cryptodev_info dev_info;
972 if ((dev == NULL) || (nb_qpairs < 1)) {
973 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
978 CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
979 nb_qpairs, dev->data->dev_id);
981 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
983 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
984 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
986 if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
987 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
988 nb_qpairs, dev->data->dev_id);
992 if (dev->data->queue_pairs == NULL) { /* first time configuration */
993 dev->data->queue_pairs = rte_zmalloc_socket(
994 "cryptodev->queue_pairs",
995 sizeof(dev->data->queue_pairs[0]) *
996 dev_info.max_nb_queue_pairs,
997 RTE_CACHE_LINE_SIZE, socket_id);
999 if (dev->data->queue_pairs == NULL) {
1000 dev->data->nb_queue_pairs = 0;
1001 CDEV_LOG_ERR("failed to get memory for qp meta data, "
1006 } else { /* re-configure */
1008 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
1010 qp = dev->data->queue_pairs;
1012 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
1015 for (i = nb_qpairs; i < old_nb_queues; i++) {
1016 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
1023 dev->data->nb_queue_pairs = nb_qpairs;
1028 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
1030 struct rte_cryptodev *dev;
1033 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1034 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1038 dev = &rte_crypto_devices[dev_id];
1040 if (dev->data->dev_started) {
1042 "device %d must be stopped to allow configuration", dev_id);
1046 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
1048 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1049 cryptodev_cb_cleanup(dev);
1050 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1052 /* Setup new number of queue pairs and reconfigure device. */
1053 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
1056 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
1061 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1062 diag = cryptodev_cb_init(dev);
1063 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1065 CDEV_LOG_ERR("Callback init failed for dev_id=%d", dev_id);
1069 rte_cryptodev_trace_configure(dev_id, config);
1070 return (*dev->dev_ops->dev_configure)(dev, config);
1074 rte_cryptodev_start(uint8_t dev_id)
1076 struct rte_cryptodev *dev;
1079 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
1081 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1082 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1086 dev = &rte_crypto_devices[dev_id];
1088 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1090 if (dev->data->dev_started != 0) {
1091 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
1096 diag = (*dev->dev_ops->dev_start)(dev);
1097 /* expose selection of PMD fast-path functions */
1098 cryptodev_fp_ops_set(rte_crypto_fp_ops + dev_id, dev);
1100 rte_cryptodev_trace_start(dev_id, diag);
1102 dev->data->dev_started = 1;
1110 rte_cryptodev_stop(uint8_t dev_id)
1112 struct rte_cryptodev *dev;
1114 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1115 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1119 dev = &rte_crypto_devices[dev_id];
1121 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1123 if (dev->data->dev_started == 0) {
1124 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
1129 /* point fast-path functions to dummy ones */
1130 cryptodev_fp_ops_reset(rte_crypto_fp_ops + dev_id);
1132 (*dev->dev_ops->dev_stop)(dev);
1133 rte_cryptodev_trace_stop(dev_id);
1134 dev->data->dev_started = 0;
1138 rte_cryptodev_close(uint8_t dev_id)
1140 struct rte_cryptodev *dev;
1143 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1144 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1148 dev = &rte_crypto_devices[dev_id];
1150 /* Device must be stopped before it can be closed */
1151 if (dev->data->dev_started == 1) {
1152 CDEV_LOG_ERR("Device %u must be stopped before closing",
1157 /* We can't close the device if there are outstanding sessions in use */
1158 if (dev->data->session_pool != NULL) {
1159 if (!rte_mempool_full(dev->data->session_pool)) {
1160 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
1161 "has sessions still in use, free "
1162 "all sessions before calling close",
1168 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
1169 retval = (*dev->dev_ops->dev_close)(dev);
1170 rte_cryptodev_trace_close(dev_id, retval);
1179 rte_cryptodev_get_qp_status(uint8_t dev_id, uint16_t queue_pair_id)
1181 struct rte_cryptodev *dev;
1183 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1184 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1188 dev = &rte_crypto_devices[dev_id];
1189 if (queue_pair_id >= dev->data->nb_queue_pairs) {
1190 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1193 void **qps = dev->data->queue_pairs;
1195 if (qps[queue_pair_id]) {
1196 CDEV_LOG_DEBUG("qp %d on dev %d is initialised",
1197 queue_pair_id, dev_id);
1201 CDEV_LOG_DEBUG("qp %d on dev %d is not initialised",
1202 queue_pair_id, dev_id);
1208 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
1209 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
1212 struct rte_cryptodev *dev;
1214 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1215 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1219 dev = &rte_crypto_devices[dev_id];
1220 if (queue_pair_id >= dev->data->nb_queue_pairs) {
1221 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1226 CDEV_LOG_ERR("qp_conf cannot be NULL\n");
1230 if ((qp_conf->mp_session && !qp_conf->mp_session_private) ||
1231 (!qp_conf->mp_session && qp_conf->mp_session_private)) {
1232 CDEV_LOG_ERR("Invalid mempools\n");
1236 if (qp_conf->mp_session) {
1237 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1238 uint32_t obj_size = qp_conf->mp_session->elt_size;
1239 uint32_t obj_priv_size = qp_conf->mp_session_private->elt_size;
1240 struct rte_cryptodev_sym_session s = {0};
1242 pool_priv = rte_mempool_get_priv(qp_conf->mp_session);
1243 if (!pool_priv || qp_conf->mp_session->private_data_size <
1244 sizeof(*pool_priv)) {
1245 CDEV_LOG_ERR("Invalid mempool\n");
1249 s.nb_drivers = pool_priv->nb_drivers;
1250 s.user_data_sz = pool_priv->user_data_sz;
1252 if ((rte_cryptodev_sym_get_existing_header_session_size(&s) >
1253 obj_size) || (s.nb_drivers <= dev->driver_id) ||
1254 rte_cryptodev_sym_get_private_session_size(dev_id) >
1256 CDEV_LOG_ERR("Invalid mempool\n");
1261 if (dev->data->dev_started) {
1263 "device %d must be stopped to allow configuration", dev_id);
1267 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
1269 rte_cryptodev_trace_queue_pair_setup(dev_id, queue_pair_id, qp_conf);
1270 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
1274 struct rte_cryptodev_cb *
1275 rte_cryptodev_add_enq_callback(uint8_t dev_id,
1277 rte_cryptodev_callback_fn cb_fn,
1280 struct rte_cryptodev *dev;
1281 struct rte_cryptodev_cb_rcu *list;
1282 struct rte_cryptodev_cb *cb, *tail;
1285 CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1290 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1291 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1296 dev = &rte_crypto_devices[dev_id];
1297 if (qp_id >= dev->data->nb_queue_pairs) {
1298 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1303 cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1305 CDEV_LOG_ERR("Failed to allocate memory for callback on "
1306 "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1311 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1316 /* Add the callbacks in fifo order. */
1317 list = &dev->enq_cbs[qp_id];
1323 /* Stores to cb->fn and cb->param should complete before
1324 * cb is visible to data plane.
1326 __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
1328 /* Stores to cb->fn and cb->param should complete before
1329 * cb is visible to data plane.
1331 __atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
1334 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1340 rte_cryptodev_remove_enq_callback(uint8_t dev_id,
1342 struct rte_cryptodev_cb *cb)
1344 struct rte_cryptodev *dev;
1345 struct rte_cryptodev_cb **prev_cb, *curr_cb;
1346 struct rte_cryptodev_cb_rcu *list;
1352 CDEV_LOG_ERR("Callback is NULL");
1356 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1357 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1361 dev = &rte_crypto_devices[dev_id];
1362 if (qp_id >= dev->data->nb_queue_pairs) {
1363 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1367 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1368 if (dev->enq_cbs == NULL) {
1369 CDEV_LOG_ERR("Callback not initialized");
1373 list = &dev->enq_cbs[qp_id];
1375 CDEV_LOG_ERR("Callback list is NULL");
1379 if (list->qsbr == NULL) {
1380 CDEV_LOG_ERR("Rcu qsbr is NULL");
1384 prev_cb = &list->next;
1385 for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1387 if (curr_cb == cb) {
1388 /* Remove the user cb from the callback list. */
1389 __atomic_store_n(prev_cb, curr_cb->next,
1397 /* Call sync with invalid thread id as this is part of
1400 rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1405 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1409 struct rte_cryptodev_cb *
1410 rte_cryptodev_add_deq_callback(uint8_t dev_id,
1412 rte_cryptodev_callback_fn cb_fn,
1415 struct rte_cryptodev *dev;
1416 struct rte_cryptodev_cb_rcu *list;
1417 struct rte_cryptodev_cb *cb, *tail;
1420 CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1425 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1426 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1431 dev = &rte_crypto_devices[dev_id];
1432 if (qp_id >= dev->data->nb_queue_pairs) {
1433 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1438 cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1440 CDEV_LOG_ERR("Failed to allocate memory for callback on "
1441 "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1446 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1451 /* Add the callbacks in fifo order. */
1452 list = &dev->deq_cbs[qp_id];
1458 /* Stores to cb->fn and cb->param should complete before
1459 * cb is visible to data plane.
1461 __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
1463 /* Stores to cb->fn and cb->param should complete before
1464 * cb is visible to data plane.
1466 __atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
1469 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1475 rte_cryptodev_remove_deq_callback(uint8_t dev_id,
1477 struct rte_cryptodev_cb *cb)
1479 struct rte_cryptodev *dev;
1480 struct rte_cryptodev_cb **prev_cb, *curr_cb;
1481 struct rte_cryptodev_cb_rcu *list;
1487 CDEV_LOG_ERR("Callback is NULL");
1491 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1492 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1496 dev = &rte_crypto_devices[dev_id];
1497 if (qp_id >= dev->data->nb_queue_pairs) {
1498 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1502 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1503 if (dev->enq_cbs == NULL) {
1504 CDEV_LOG_ERR("Callback not initialized");
1508 list = &dev->deq_cbs[qp_id];
1510 CDEV_LOG_ERR("Callback list is NULL");
1514 if (list->qsbr == NULL) {
1515 CDEV_LOG_ERR("Rcu qsbr is NULL");
1519 prev_cb = &list->next;
1520 for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1522 if (curr_cb == cb) {
1523 /* Remove the user cb from the callback list. */
1524 __atomic_store_n(prev_cb, curr_cb->next,
1532 /* Call sync with invalid thread id as this is part of
1535 rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1540 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1545 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
1547 struct rte_cryptodev *dev;
1549 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1550 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1554 if (stats == NULL) {
1555 CDEV_LOG_ERR("Invalid stats ptr");
1559 dev = &rte_crypto_devices[dev_id];
1560 memset(stats, 0, sizeof(*stats));
1562 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1563 (*dev->dev_ops->stats_get)(dev, stats);
1568 rte_cryptodev_stats_reset(uint8_t dev_id)
1570 struct rte_cryptodev *dev;
1572 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1573 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1577 dev = &rte_crypto_devices[dev_id];
1579 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
1580 (*dev->dev_ops->stats_reset)(dev);
1584 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
1586 struct rte_cryptodev *dev;
1588 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1589 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1593 dev = &rte_crypto_devices[dev_id];
1595 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
1597 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1598 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1600 dev_info->driver_name = dev->device->driver->name;
1601 dev_info->device = dev->device;
1605 rte_cryptodev_callback_register(uint8_t dev_id,
1606 enum rte_cryptodev_event_type event,
1607 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1609 struct rte_cryptodev *dev;
1610 struct rte_cryptodev_callback *user_cb;
1615 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1616 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1620 dev = &rte_crypto_devices[dev_id];
1621 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1623 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
1624 if (user_cb->cb_fn == cb_fn &&
1625 user_cb->cb_arg == cb_arg &&
1626 user_cb->event == event) {
1631 /* create a new callback. */
1632 if (user_cb == NULL) {
1633 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
1634 sizeof(struct rte_cryptodev_callback), 0);
1635 if (user_cb != NULL) {
1636 user_cb->cb_fn = cb_fn;
1637 user_cb->cb_arg = cb_arg;
1638 user_cb->event = event;
1639 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
1643 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1644 return (user_cb == NULL) ? -ENOMEM : 0;
1648 rte_cryptodev_callback_unregister(uint8_t dev_id,
1649 enum rte_cryptodev_event_type event,
1650 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1653 struct rte_cryptodev *dev;
1654 struct rte_cryptodev_callback *cb, *next;
1659 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1660 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1664 dev = &rte_crypto_devices[dev_id];
1665 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1668 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
1670 next = TAILQ_NEXT(cb, next);
1672 if (cb->cb_fn != cb_fn || cb->event != event ||
1673 (cb->cb_arg != (void *)-1 &&
1674 cb->cb_arg != cb_arg))
1678 * if this callback is not executing right now,
1681 if (cb->active == 0) {
1682 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1689 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1694 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1695 enum rte_cryptodev_event_type event)
1697 struct rte_cryptodev_callback *cb_lst;
1698 struct rte_cryptodev_callback dev_cb;
1700 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1701 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1702 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1706 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1707 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1709 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1712 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1716 rte_cryptodev_sym_session_init(uint8_t dev_id,
1717 struct rte_cryptodev_sym_session *sess,
1718 struct rte_crypto_sym_xform *xforms,
1719 struct rte_mempool *mp)
1721 struct rte_cryptodev *dev;
1722 uint32_t sess_priv_sz = rte_cryptodev_sym_get_private_session_size(
1727 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1728 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1732 dev = rte_cryptodev_pmd_get_dev(dev_id);
1734 if (sess == NULL || xforms == NULL || dev == NULL || mp == NULL)
1737 if (mp->elt_size < sess_priv_sz)
1740 index = dev->driver_id;
1741 if (index >= sess->nb_drivers)
1744 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_configure, -ENOTSUP);
1746 if (sess->sess_data[index].refcnt == 0) {
1747 ret = dev->dev_ops->sym_session_configure(dev, xforms,
1751 "dev_id %d failed to configure session details",
1757 rte_cryptodev_trace_sym_session_init(dev_id, sess, xforms, mp);
1758 sess->sess_data[index].refcnt++;
1762 struct rte_mempool *
1763 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1764 uint32_t elt_size, uint32_t cache_size, uint16_t user_data_size,
1767 struct rte_mempool *mp;
1768 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1771 obj_sz = rte_cryptodev_sym_get_header_session_size() + user_data_size;
1772 if (obj_sz > elt_size)
1773 CDEV_LOG_INFO("elt_size %u is expanded to %u\n", elt_size,
1778 mp = rte_mempool_create(name, nb_elts, obj_sz, cache_size,
1779 (uint32_t)(sizeof(*pool_priv)),
1780 NULL, NULL, NULL, NULL,
1783 CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
1784 __func__, name, rte_errno);
1788 pool_priv = rte_mempool_get_priv(mp);
1790 CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
1792 rte_mempool_free(mp);
1796 pool_priv->nb_drivers = nb_drivers;
1797 pool_priv->user_data_sz = user_data_size;
1799 rte_cryptodev_trace_sym_session_pool_create(name, nb_elts,
1800 elt_size, cache_size, user_data_size, mp);
1804 struct rte_mempool *
1805 rte_cryptodev_asym_session_pool_create(const char *name, uint32_t nb_elts,
1806 uint32_t cache_size, int socket_id)
1808 struct rte_mempool *mp;
1809 struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
1810 uint32_t obj_sz, obj_sz_aligned;
1811 uint8_t dev_id, priv_sz, max_priv_sz = 0;
1813 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
1814 if (rte_cryptodev_is_valid_dev(dev_id)) {
1815 priv_sz = rte_cryptodev_asym_get_private_session_size(dev_id);
1816 if (priv_sz > max_priv_sz)
1817 max_priv_sz = priv_sz;
1819 if (max_priv_sz == 0) {
1820 CDEV_LOG_INFO("Could not set max private session size\n");
1824 obj_sz = rte_cryptodev_asym_get_header_session_size() + max_priv_sz;
1825 obj_sz_aligned = RTE_ALIGN_CEIL(obj_sz, RTE_CACHE_LINE_SIZE);
1827 mp = rte_mempool_create(name, nb_elts, obj_sz_aligned, cache_size,
1828 (uint32_t)(sizeof(*pool_priv)),
1829 NULL, NULL, NULL, NULL,
1832 CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
1833 __func__, name, rte_errno);
1837 pool_priv = rte_mempool_get_priv(mp);
1839 CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
1841 rte_mempool_free(mp);
1844 pool_priv->max_priv_session_sz = max_priv_sz;
1846 rte_cryptodev_trace_asym_session_pool_create(name, nb_elts,
1852 rte_cryptodev_sym_session_data_size(struct rte_cryptodev_sym_session *sess)
1854 return (sizeof(sess->sess_data[0]) * sess->nb_drivers) +
1859 rte_cryptodev_sym_is_valid_session_pool(struct rte_mempool *mp)
1861 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1866 pool_priv = rte_mempool_get_priv(mp);
1868 if (!pool_priv || mp->private_data_size < sizeof(*pool_priv) ||
1869 pool_priv->nb_drivers != nb_drivers ||
1871 rte_cryptodev_sym_get_header_session_size()
1872 + pool_priv->user_data_sz)
1878 struct rte_cryptodev_sym_session *
1879 rte_cryptodev_sym_session_create(struct rte_mempool *mp)
1881 struct rte_cryptodev_sym_session *sess;
1882 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1884 if (!rte_cryptodev_sym_is_valid_session_pool(mp)) {
1885 CDEV_LOG_ERR("Invalid mempool\n");
1889 pool_priv = rte_mempool_get_priv(mp);
1891 /* Allocate a session structure from the session pool */
1892 if (rte_mempool_get(mp, (void **)&sess)) {
1893 CDEV_LOG_ERR("couldn't get object from session mempool");
1897 sess->nb_drivers = pool_priv->nb_drivers;
1898 sess->user_data_sz = pool_priv->user_data_sz;
1899 sess->opaque_data = 0;
1901 /* Clear device session pointer.
1902 * Include the flag indicating presence of user data
1904 memset(sess->sess_data, 0,
1905 rte_cryptodev_sym_session_data_size(sess));
1907 rte_cryptodev_trace_sym_session_create(mp, sess);
1911 struct rte_cryptodev_asym_session *
1912 rte_cryptodev_asym_session_create(uint8_t dev_id,
1913 struct rte_crypto_asym_xform *xforms, struct rte_mempool *mp)
1915 struct rte_cryptodev_asym_session *sess;
1916 uint32_t session_priv_data_sz;
1917 struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
1918 unsigned int session_header_size =
1919 rte_cryptodev_asym_get_header_session_size();
1920 struct rte_cryptodev *dev;
1923 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1924 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1928 dev = rte_cryptodev_pmd_get_dev(dev_id);
1934 CDEV_LOG_ERR("invalid mempool\n");
1938 session_priv_data_sz = rte_cryptodev_asym_get_private_session_size(
1940 pool_priv = rte_mempool_get_priv(mp);
1942 if (pool_priv->max_priv_session_sz < session_priv_data_sz) {
1944 "The private session data size used when creating the mempool is smaller than this device's private session data.");
1948 /* Verify if provided mempool can hold elements big enough. */
1949 if (mp->elt_size < session_header_size + session_priv_data_sz) {
1951 "mempool elements too small to hold session objects");
1955 /* Allocate a session structure from the session pool */
1956 if (rte_mempool_get(mp, (void **)&sess)) {
1957 CDEV_LOG_ERR("couldn't get object from session mempool");
1961 sess->driver_id = dev->driver_id;
1962 sess->max_priv_data_sz = pool_priv->max_priv_session_sz;
1964 /* Clear device session pointer.*/
1965 memset(sess->sess_private_data, 0, session_priv_data_sz);
1967 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_configure, NULL);
1969 if (sess->sess_private_data[0] == 0) {
1970 ret = dev->dev_ops->asym_session_configure(dev, xforms, sess);
1973 "dev_id %d failed to configure session details",
1979 rte_cryptodev_trace_asym_session_create(dev_id, xforms, mp);
1984 rte_cryptodev_sym_session_clear(uint8_t dev_id,
1985 struct rte_cryptodev_sym_session *sess)
1987 struct rte_cryptodev *dev;
1990 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1991 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1995 dev = rte_cryptodev_pmd_get_dev(dev_id);
1997 if (dev == NULL || sess == NULL)
2000 driver_id = dev->driver_id;
2001 if (sess->sess_data[driver_id].refcnt == 0)
2003 if (--sess->sess_data[driver_id].refcnt != 0)
2006 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_clear, -ENOTSUP);
2008 dev->dev_ops->sym_session_clear(dev, sess);
2010 rte_cryptodev_trace_sym_session_clear(dev_id, sess);
2015 rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess)
2018 struct rte_mempool *sess_mp;
2023 /* Check that all device private data has been freed */
2024 for (i = 0; i < sess->nb_drivers; i++) {
2025 if (sess->sess_data[i].refcnt != 0)
2029 /* Return session to mempool */
2030 sess_mp = rte_mempool_from_obj(sess);
2031 rte_mempool_put(sess_mp, sess);
2033 rte_cryptodev_trace_sym_session_free(sess);
2038 rte_cryptodev_asym_session_free(uint8_t dev_id,
2039 struct rte_cryptodev_asym_session *sess)
2041 struct rte_mempool *sess_mp;
2042 struct rte_cryptodev *dev;
2044 if (!rte_cryptodev_is_valid_dev(dev_id)) {
2045 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2049 dev = rte_cryptodev_pmd_get_dev(dev_id);
2051 if (dev == NULL || sess == NULL)
2054 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_clear, -ENOTSUP);
2056 dev->dev_ops->asym_session_clear(dev, sess);
2058 /* Return session to mempool */
2059 sess_mp = rte_mempool_from_obj(sess);
2060 rte_mempool_put(sess_mp, sess);
2062 rte_cryptodev_trace_asym_session_free(dev_id, sess);
2067 rte_cryptodev_sym_get_header_session_size(void)
2070 * Header contains pointers to the private data of all registered
2071 * drivers and all necessary information to ensure safely clear
2072 * or free al session.
2074 struct rte_cryptodev_sym_session s = {0};
2076 s.nb_drivers = nb_drivers;
2078 return (unsigned int)(sizeof(s) +
2079 rte_cryptodev_sym_session_data_size(&s));
2083 rte_cryptodev_sym_get_existing_header_session_size(
2084 struct rte_cryptodev_sym_session *sess)
2089 return (unsigned int)(sizeof(*sess) +
2090 rte_cryptodev_sym_session_data_size(sess));
2094 rte_cryptodev_asym_get_header_session_size(void)
2096 return sizeof(struct rte_cryptodev_asym_session);
2100 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)
2102 struct rte_cryptodev *dev;
2103 unsigned int priv_sess_size;
2105 if (!rte_cryptodev_is_valid_dev(dev_id))
2108 dev = rte_cryptodev_pmd_get_dev(dev_id);
2110 if (*dev->dev_ops->sym_session_get_size == NULL)
2113 priv_sess_size = (*dev->dev_ops->sym_session_get_size)(dev);
2115 return priv_sess_size;
2119 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)
2121 struct rte_cryptodev *dev;
2122 unsigned int priv_sess_size;
2124 if (!rte_cryptodev_is_valid_dev(dev_id))
2127 dev = rte_cryptodev_pmd_get_dev(dev_id);
2129 if (*dev->dev_ops->asym_session_get_size == NULL)
2132 priv_sess_size = (*dev->dev_ops->asym_session_get_size)(dev);
2134 return priv_sess_size;
2138 rte_cryptodev_sym_session_set_user_data(
2139 struct rte_cryptodev_sym_session *sess,
2146 if (sess->user_data_sz < size)
2149 rte_memcpy(sess->sess_data + sess->nb_drivers, data, size);
2154 rte_cryptodev_sym_session_get_user_data(
2155 struct rte_cryptodev_sym_session *sess)
2157 if (sess == NULL || sess->user_data_sz == 0)
2160 return (void *)(sess->sess_data + sess->nb_drivers);
2164 sym_crypto_fill_status(struct rte_crypto_sym_vec *vec, int32_t errnum)
2167 for (i = 0; i < vec->num; i++)
2168 vec->status[i] = errnum;
2172 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
2173 struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs ofs,
2174 struct rte_crypto_sym_vec *vec)
2176 struct rte_cryptodev *dev;
2178 if (!rte_cryptodev_is_valid_dev(dev_id)) {
2179 sym_crypto_fill_status(vec, EINVAL);
2183 dev = rte_cryptodev_pmd_get_dev(dev_id);
2185 if (*dev->dev_ops->sym_cpu_process == NULL ||
2186 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) {
2187 sym_crypto_fill_status(vec, ENOTSUP);
2191 return dev->dev_ops->sym_cpu_process(dev, sess, ofs, vec);
2195 rte_cryptodev_get_raw_dp_ctx_size(uint8_t dev_id)
2197 struct rte_cryptodev *dev;
2198 int32_t size = sizeof(struct rte_crypto_raw_dp_ctx);
2201 if (!rte_cryptodev_is_valid_dev(dev_id))
2204 dev = rte_cryptodev_pmd_get_dev(dev_id);
2206 if (*dev->dev_ops->sym_get_raw_dp_ctx_size == NULL ||
2207 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)) {
2211 priv_size = (*dev->dev_ops->sym_get_raw_dp_ctx_size)(dev);
2215 return RTE_ALIGN_CEIL((size + priv_size), 8);
2219 rte_cryptodev_configure_raw_dp_ctx(uint8_t dev_id, uint16_t qp_id,
2220 struct rte_crypto_raw_dp_ctx *ctx,
2221 enum rte_crypto_op_sess_type sess_type,
2222 union rte_cryptodev_session_ctx session_ctx,
2225 struct rte_cryptodev *dev;
2227 if (!rte_cryptodev_get_qp_status(dev_id, qp_id))
2230 dev = rte_cryptodev_pmd_get_dev(dev_id);
2231 if (!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)
2232 || dev->dev_ops->sym_configure_raw_dp_ctx == NULL)
2235 return (*dev->dev_ops->sym_configure_raw_dp_ctx)(dev, qp_id, ctx,
2236 sess_type, session_ctx, is_update);
2240 rte_cryptodev_raw_enqueue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2241 struct rte_crypto_sym_vec *vec, union rte_crypto_sym_ofs ofs,
2242 void **user_data, int *enqueue_status)
2244 return (*ctx->enqueue_burst)(ctx->qp_data, ctx->drv_ctx_data, vec,
2245 ofs, user_data, enqueue_status);
2249 rte_cryptodev_raw_enqueue_done(struct rte_crypto_raw_dp_ctx *ctx,
2252 return (*ctx->enqueue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2256 rte_cryptodev_raw_dequeue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2257 rte_cryptodev_raw_get_dequeue_count_t get_dequeue_count,
2258 uint32_t max_nb_to_dequeue,
2259 rte_cryptodev_raw_post_dequeue_t post_dequeue,
2260 void **out_user_data, uint8_t is_user_data_array,
2261 uint32_t *n_success_jobs, int *status)
2263 return (*ctx->dequeue_burst)(ctx->qp_data, ctx->drv_ctx_data,
2264 get_dequeue_count, max_nb_to_dequeue, post_dequeue,
2265 out_user_data, is_user_data_array, n_success_jobs, status);
2269 rte_cryptodev_raw_dequeue_done(struct rte_crypto_raw_dp_ctx *ctx,
2272 return (*ctx->dequeue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2275 /** Initialise rte_crypto_op mempool element */
2277 rte_crypto_op_init(struct rte_mempool *mempool,
2280 __rte_unused unsigned i)
2282 struct rte_crypto_op *op = _op_data;
2283 enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
2285 memset(_op_data, 0, mempool->elt_size);
2287 __rte_crypto_op_reset(op, type);
2289 op->phys_addr = rte_mem_virt2iova(_op_data);
2290 op->mempool = mempool;
2294 struct rte_mempool *
2295 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
2296 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
2299 struct rte_crypto_op_pool_private *priv;
2301 unsigned elt_size = sizeof(struct rte_crypto_op) +
2304 if (type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
2305 elt_size += sizeof(struct rte_crypto_sym_op);
2306 } else if (type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
2307 elt_size += sizeof(struct rte_crypto_asym_op);
2308 } else if (type == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2309 elt_size += RTE_MAX(sizeof(struct rte_crypto_sym_op),
2310 sizeof(struct rte_crypto_asym_op));
2312 CDEV_LOG_ERR("Invalid op_type\n");
2316 /* lookup mempool in case already allocated */
2317 struct rte_mempool *mp = rte_mempool_lookup(name);
2320 priv = (struct rte_crypto_op_pool_private *)
2321 rte_mempool_get_priv(mp);
2323 if (mp->elt_size != elt_size ||
2324 mp->cache_size < cache_size ||
2325 mp->size < nb_elts ||
2326 priv->priv_size < priv_size) {
2328 CDEV_LOG_ERR("Mempool %s already exists but with "
2329 "incompatible parameters", name);
2335 mp = rte_mempool_create(
2340 sizeof(struct rte_crypto_op_pool_private),
2349 CDEV_LOG_ERR("Failed to create mempool %s", name);
2353 priv = (struct rte_crypto_op_pool_private *)
2354 rte_mempool_get_priv(mp);
2356 priv->priv_size = priv_size;
2363 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix)
2365 struct rte_cryptodev *dev = NULL;
2371 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
2372 int ret = snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
2373 "%s_%u", dev_name_prefix, i);
2378 dev = rte_cryptodev_pmd_get_named_dev(name);
2386 TAILQ_HEAD(cryptodev_driver_list, cryptodev_driver);
2388 static struct cryptodev_driver_list cryptodev_driver_list =
2389 TAILQ_HEAD_INITIALIZER(cryptodev_driver_list);
2392 rte_cryptodev_driver_id_get(const char *name)
2394 struct cryptodev_driver *driver;
2395 const char *driver_name;
2398 RTE_LOG(DEBUG, CRYPTODEV, "name pointer NULL");
2402 TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
2403 driver_name = driver->driver->name;
2404 if (strncmp(driver_name, name, strlen(driver_name) + 1) == 0)
2411 rte_cryptodev_name_get(uint8_t dev_id)
2413 struct rte_cryptodev *dev;
2415 if (!rte_cryptodev_is_valid_device_data(dev_id)) {
2416 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2420 dev = rte_cryptodev_pmd_get_dev(dev_id);
2424 return dev->data->name;
2428 rte_cryptodev_driver_name_get(uint8_t driver_id)
2430 struct cryptodev_driver *driver;
2432 TAILQ_FOREACH(driver, &cryptodev_driver_list, next)
2433 if (driver->id == driver_id)
2434 return driver->driver->name;
2439 rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
2440 const struct rte_driver *drv)
2442 crypto_drv->driver = drv;
2443 crypto_drv->id = nb_drivers;
2445 TAILQ_INSERT_TAIL(&cryptodev_driver_list, crypto_drv, next);
2447 return nb_drivers++;
2450 RTE_INIT(cryptodev_init_fp_ops)
2454 for (i = 0; i != RTE_DIM(rte_crypto_fp_ops); i++)
2455 cryptodev_fp_ops_reset(rte_crypto_fp_ops + i);
2459 cryptodev_handle_dev_list(const char *cmd __rte_unused,
2460 const char *params __rte_unused,
2461 struct rte_tel_data *d)
2465 if (rte_cryptodev_count() < 1)
2468 rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
2469 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
2470 if (rte_cryptodev_is_valid_dev(dev_id))
2471 rte_tel_data_add_array_int(d, dev_id);
2477 cryptodev_handle_dev_info(const char *cmd __rte_unused,
2478 const char *params, struct rte_tel_data *d)
2480 struct rte_cryptodev_info cryptodev_info;
2484 if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2487 dev_id = strtoul(params, &end_param, 0);
2488 if (*end_param != '\0')
2489 CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2490 if (!rte_cryptodev_is_valid_dev(dev_id))
2493 rte_cryptodev_info_get(dev_id, &cryptodev_info);
2495 rte_tel_data_start_dict(d);
2496 rte_tel_data_add_dict_string(d, "device_name",
2497 cryptodev_info.device->name);
2498 rte_tel_data_add_dict_int(d, "max_nb_queue_pairs",
2499 cryptodev_info.max_nb_queue_pairs);
2504 #define ADD_DICT_STAT(s) rte_tel_data_add_dict_u64(d, #s, cryptodev_stats.s)
2507 cryptodev_handle_dev_stats(const char *cmd __rte_unused,
2509 struct rte_tel_data *d)
2511 struct rte_cryptodev_stats cryptodev_stats;
2515 if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2518 dev_id = strtoul(params, &end_param, 0);
2519 if (*end_param != '\0')
2520 CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2521 if (!rte_cryptodev_is_valid_dev(dev_id))
2524 ret = rte_cryptodev_stats_get(dev_id, &cryptodev_stats);
2528 rte_tel_data_start_dict(d);
2529 ADD_DICT_STAT(enqueued_count);
2530 ADD_DICT_STAT(dequeued_count);
2531 ADD_DICT_STAT(enqueue_err_count);
2532 ADD_DICT_STAT(dequeue_err_count);
2537 #define CRYPTO_CAPS_SZ \
2538 (RTE_ALIGN_CEIL(sizeof(struct rte_cryptodev_capabilities), \
2539 sizeof(uint64_t)) / \
2543 crypto_caps_array(struct rte_tel_data *d,
2544 const struct rte_cryptodev_capabilities *capabilities)
2546 const struct rte_cryptodev_capabilities *dev_caps;
2547 uint64_t caps_val[CRYPTO_CAPS_SZ];
2548 unsigned int i = 0, j;
2550 rte_tel_data_start_array(d, RTE_TEL_U64_VAL);
2552 while ((dev_caps = &capabilities[i++])->op !=
2553 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2554 memset(&caps_val, 0, CRYPTO_CAPS_SZ * sizeof(caps_val[0]));
2555 rte_memcpy(caps_val, dev_caps, sizeof(capabilities[0]));
2556 for (j = 0; j < CRYPTO_CAPS_SZ; j++)
2557 rte_tel_data_add_array_u64(d, caps_val[j]);
2564 cryptodev_handle_dev_caps(const char *cmd __rte_unused, const char *params,
2565 struct rte_tel_data *d)
2567 struct rte_cryptodev_info dev_info;
2568 struct rte_tel_data *crypto_caps;
2573 if (!params || strlen(params) == 0 || !isdigit(*params))
2576 dev_id = strtoul(params, &end_param, 0);
2577 if (*end_param != '\0')
2578 CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2579 if (!rte_cryptodev_is_valid_dev(dev_id))
2582 rte_tel_data_start_dict(d);
2583 crypto_caps = rte_tel_data_alloc();
2587 rte_cryptodev_info_get(dev_id, &dev_info);
2588 crypto_caps_n = crypto_caps_array(crypto_caps, dev_info.capabilities);
2589 rte_tel_data_add_dict_container(d, "crypto_caps", crypto_caps, 0);
2590 rte_tel_data_add_dict_int(d, "crypto_caps_n", crypto_caps_n);
2595 RTE_INIT(cryptodev_init_telemetry)
2597 rte_telemetry_register_cmd("/cryptodev/info", cryptodev_handle_dev_info,
2598 "Returns information for a cryptodev. Parameters: int dev_id");
2599 rte_telemetry_register_cmd("/cryptodev/list",
2600 cryptodev_handle_dev_list,
2601 "Returns list of available crypto devices by IDs. No parameters.");
2602 rte_telemetry_register_cmd("/cryptodev/stats",
2603 cryptodev_handle_dev_stats,
2604 "Returns the stats for a cryptodev. Parameters: int dev_id");
2605 rte_telemetry_register_cmd("/cryptodev/caps",
2606 cryptodev_handle_dev_caps,
2607 "Returns the capabilities for a cryptodev. Parameters: int dev_id");