1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_cryptodev_pmd.h>
11 #include "rte_openssl_pmd_private.h"
14 static const struct rte_cryptodev_capabilities openssl_pmd_capabilities[] = {
16 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
18 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
20 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
37 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
39 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
41 .algo = RTE_CRYPTO_AUTH_MD5,
58 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
60 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
62 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
79 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
81 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
83 .algo = RTE_CRYPTO_AUTH_SHA1,
100 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
102 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
104 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
121 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
123 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
125 .algo = RTE_CRYPTO_AUTH_SHA224,
142 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
144 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
146 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
163 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
165 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
167 .algo = RTE_CRYPTO_AUTH_SHA256,
184 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
186 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
188 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
205 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
207 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
209 .algo = RTE_CRYPTO_AUTH_SHA384,
226 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
228 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
230 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
247 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
249 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
251 .algo = RTE_CRYPTO_AUTH_SHA512,
268 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
270 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
272 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
288 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
290 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
292 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
308 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
310 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
312 .algo = RTE_CRYPTO_AEAD_AES_GCM,
338 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
340 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
342 .algo = RTE_CRYPTO_AEAD_AES_CCM,
367 { /* AES GMAC (AUTH) */
368 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
370 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
372 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
393 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
395 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
397 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
413 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
415 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
417 .algo = RTE_CRYPTO_CIPHER_3DES_CTR,
433 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
435 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
437 .algo = RTE_CRYPTO_CIPHER_DES_CBC,
452 { /* DES DOCSIS BPI */
453 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
455 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
457 .algo = RTE_CRYPTO_CIPHER_DES_DOCSISBPI,
473 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
477 /** Configure device */
479 openssl_pmd_config(__rte_unused struct rte_cryptodev *dev,
480 __rte_unused struct rte_cryptodev_config *config)
487 openssl_pmd_start(__rte_unused struct rte_cryptodev *dev)
494 openssl_pmd_stop(__rte_unused struct rte_cryptodev *dev)
500 openssl_pmd_close(__rte_unused struct rte_cryptodev *dev)
506 /** Get device statistics */
508 openssl_pmd_stats_get(struct rte_cryptodev *dev,
509 struct rte_cryptodev_stats *stats)
513 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
514 struct openssl_qp *qp = dev->data->queue_pairs[qp_id];
516 stats->enqueued_count += qp->stats.enqueued_count;
517 stats->dequeued_count += qp->stats.dequeued_count;
519 stats->enqueue_err_count += qp->stats.enqueue_err_count;
520 stats->dequeue_err_count += qp->stats.dequeue_err_count;
524 /** Reset device statistics */
526 openssl_pmd_stats_reset(struct rte_cryptodev *dev)
530 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
531 struct openssl_qp *qp = dev->data->queue_pairs[qp_id];
533 memset(&qp->stats, 0, sizeof(qp->stats));
538 /** Get device info */
540 openssl_pmd_info_get(struct rte_cryptodev *dev,
541 struct rte_cryptodev_info *dev_info)
543 struct openssl_private *internals = dev->data->dev_private;
545 if (dev_info != NULL) {
546 dev_info->driver_id = dev->driver_id;
547 dev_info->feature_flags = dev->feature_flags;
548 dev_info->capabilities = openssl_pmd_capabilities;
549 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
550 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
554 /** Release queue pair */
556 openssl_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
558 if (dev->data->queue_pairs[qp_id] != NULL) {
559 rte_free(dev->data->queue_pairs[qp_id]);
560 dev->data->queue_pairs[qp_id] = NULL;
565 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
567 openssl_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
568 struct openssl_qp *qp)
570 unsigned int n = snprintf(qp->name, sizeof(qp->name),
571 "openssl_pmd_%u_qp_%u",
572 dev->data->dev_id, qp->id);
574 if (n >= sizeof(qp->name))
581 /** Create a ring to place processed operations on */
582 static struct rte_ring *
583 openssl_pmd_qp_create_processed_ops_ring(struct openssl_qp *qp,
584 unsigned int ring_size, int socket_id)
588 r = rte_ring_lookup(qp->name);
590 if (rte_ring_get_size(r) >= ring_size) {
592 "Reusing existing ring %s for processed ops",
598 "Unable to reuse existing ring %s for processed ops",
603 return rte_ring_create(qp->name, ring_size, socket_id,
604 RING_F_SP_ENQ | RING_F_SC_DEQ);
608 /** Setup a queue pair */
610 openssl_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
611 const struct rte_cryptodev_qp_conf *qp_conf,
612 int socket_id, struct rte_mempool *session_pool)
614 struct openssl_qp *qp = NULL;
616 /* Free memory prior to re-allocation if needed. */
617 if (dev->data->queue_pairs[qp_id] != NULL)
618 openssl_pmd_qp_release(dev, qp_id);
620 /* Allocate the queue pair data structure. */
621 qp = rte_zmalloc_socket("OPENSSL PMD Queue Pair", sizeof(*qp),
622 RTE_CACHE_LINE_SIZE, socket_id);
627 dev->data->queue_pairs[qp_id] = qp;
629 if (openssl_pmd_qp_set_unique_name(dev, qp))
630 goto qp_setup_cleanup;
632 qp->processed_ops = openssl_pmd_qp_create_processed_ops_ring(qp,
633 qp_conf->nb_descriptors, socket_id);
634 if (qp->processed_ops == NULL)
635 goto qp_setup_cleanup;
637 qp->sess_mp = session_pool;
639 memset(&qp->stats, 0, sizeof(qp->stats));
650 /** Start queue pair */
652 openssl_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
653 __rte_unused uint16_t queue_pair_id)
658 /** Stop queue pair */
660 openssl_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
661 __rte_unused uint16_t queue_pair_id)
666 /** Return the number of allocated queue pairs */
668 openssl_pmd_qp_count(struct rte_cryptodev *dev)
670 return dev->data->nb_queue_pairs;
673 /** Returns the size of the session structure */
675 openssl_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
677 return sizeof(struct openssl_session);
680 /** Configure the session from a crypto xform chain */
682 openssl_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
683 struct rte_crypto_sym_xform *xform,
684 struct rte_cryptodev_sym_session *sess,
685 struct rte_mempool *mempool)
687 void *sess_private_data;
690 if (unlikely(sess == NULL)) {
691 OPENSSL_LOG_ERR("invalid session struct");
695 if (rte_mempool_get(mempool, &sess_private_data)) {
697 "Couldn't get object from session mempool");
701 ret = openssl_set_session_parameters(sess_private_data, xform);
703 OPENSSL_LOG_ERR("failed configure session parameters");
705 /* Return session to mempool */
706 rte_mempool_put(mempool, sess_private_data);
710 set_session_private_data(sess, dev->driver_id,
717 /** Clear the memory of session so it doesn't leave key material behind */
719 openssl_pmd_session_clear(struct rte_cryptodev *dev,
720 struct rte_cryptodev_sym_session *sess)
722 uint8_t index = dev->driver_id;
723 void *sess_priv = get_session_private_data(sess, index);
725 /* Zero out the whole structure */
727 openssl_reset_session(sess_priv);
728 memset(sess_priv, 0, sizeof(struct openssl_session));
729 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
730 set_session_private_data(sess, index, NULL);
731 rte_mempool_put(sess_mp, sess_priv);
735 struct rte_cryptodev_ops openssl_pmd_ops = {
736 .dev_configure = openssl_pmd_config,
737 .dev_start = openssl_pmd_start,
738 .dev_stop = openssl_pmd_stop,
739 .dev_close = openssl_pmd_close,
741 .stats_get = openssl_pmd_stats_get,
742 .stats_reset = openssl_pmd_stats_reset,
744 .dev_infos_get = openssl_pmd_info_get,
746 .queue_pair_setup = openssl_pmd_qp_setup,
747 .queue_pair_release = openssl_pmd_qp_release,
748 .queue_pair_start = openssl_pmd_qp_start,
749 .queue_pair_stop = openssl_pmd_qp_stop,
750 .queue_pair_count = openssl_pmd_qp_count,
752 .session_get_size = openssl_pmd_session_get_size,
753 .session_configure = openssl_pmd_session_configure,
754 .session_clear = openssl_pmd_session_clear
757 struct rte_cryptodev_ops *rte_openssl_pmd_ops = &openssl_pmd_ops;