4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_common.h>
36 #include <rte_malloc.h>
37 #include <rte_cryptodev_pmd.h>
39 #include "rte_openssl_pmd_private.h"
42 static const struct rte_cryptodev_capabilities openssl_pmd_capabilities[] = {
44 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
46 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
48 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
65 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
67 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
69 .algo = RTE_CRYPTO_AUTH_MD5,
86 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
88 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
90 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
107 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
109 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
111 .algo = RTE_CRYPTO_AUTH_SHA1,
128 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
130 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
132 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
149 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
151 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
153 .algo = RTE_CRYPTO_AUTH_SHA224,
170 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
172 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
174 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
191 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
193 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
195 .algo = RTE_CRYPTO_AUTH_SHA256,
212 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
214 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
216 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
233 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
235 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
237 .algo = RTE_CRYPTO_AUTH_SHA384,
254 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
256 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
258 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
275 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
277 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
279 .algo = RTE_CRYPTO_AUTH_SHA512,
296 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
298 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
300 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
316 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
318 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
320 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
336 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
338 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
340 .algo = RTE_CRYPTO_AEAD_AES_GCM,
365 { /* AES GMAC (AUTH) */
366 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
368 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
370 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
391 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
393 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
395 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
411 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
413 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
415 .algo = RTE_CRYPTO_CIPHER_3DES_CTR,
431 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
433 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
435 .algo = RTE_CRYPTO_CIPHER_DES_CBC,
450 { /* DES DOCSIS BPI */
451 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
453 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
455 .algo = RTE_CRYPTO_CIPHER_DES_DOCSISBPI,
471 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
475 /** Configure device */
477 openssl_pmd_config(__rte_unused struct rte_cryptodev *dev,
478 __rte_unused struct rte_cryptodev_config *config)
485 openssl_pmd_start(__rte_unused struct rte_cryptodev *dev)
492 openssl_pmd_stop(__rte_unused struct rte_cryptodev *dev)
498 openssl_pmd_close(__rte_unused struct rte_cryptodev *dev)
504 /** Get device statistics */
506 openssl_pmd_stats_get(struct rte_cryptodev *dev,
507 struct rte_cryptodev_stats *stats)
511 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
512 struct openssl_qp *qp = dev->data->queue_pairs[qp_id];
514 stats->enqueued_count += qp->stats.enqueued_count;
515 stats->dequeued_count += qp->stats.dequeued_count;
517 stats->enqueue_err_count += qp->stats.enqueue_err_count;
518 stats->dequeue_err_count += qp->stats.dequeue_err_count;
522 /** Reset device statistics */
524 openssl_pmd_stats_reset(struct rte_cryptodev *dev)
528 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
529 struct openssl_qp *qp = dev->data->queue_pairs[qp_id];
531 memset(&qp->stats, 0, sizeof(qp->stats));
536 /** Get device info */
538 openssl_pmd_info_get(struct rte_cryptodev *dev,
539 struct rte_cryptodev_info *dev_info)
541 struct openssl_private *internals = dev->data->dev_private;
543 if (dev_info != NULL) {
544 dev_info->driver_id = dev->driver_id;
545 dev_info->feature_flags = dev->feature_flags;
546 dev_info->capabilities = openssl_pmd_capabilities;
547 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
548 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
552 /** Release queue pair */
554 openssl_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
556 if (dev->data->queue_pairs[qp_id] != NULL) {
557 rte_free(dev->data->queue_pairs[qp_id]);
558 dev->data->queue_pairs[qp_id] = NULL;
563 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
565 openssl_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
566 struct openssl_qp *qp)
568 unsigned int n = snprintf(qp->name, sizeof(qp->name),
569 "openssl_pmd_%u_qp_%u",
570 dev->data->dev_id, qp->id);
572 if (n > sizeof(qp->name))
579 /** Create a ring to place processed operations on */
580 static struct rte_ring *
581 openssl_pmd_qp_create_processed_ops_ring(struct openssl_qp *qp,
582 unsigned int ring_size, int socket_id)
586 r = rte_ring_lookup(qp->name);
588 if (rte_ring_get_size(r) >= ring_size) {
590 "Reusing existing ring %s for processed ops",
596 "Unable to reuse existing ring %s for processed ops",
601 return rte_ring_create(qp->name, ring_size, socket_id,
602 RING_F_SP_ENQ | RING_F_SC_DEQ);
606 /** Setup a queue pair */
608 openssl_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
609 const struct rte_cryptodev_qp_conf *qp_conf,
610 int socket_id, struct rte_mempool *session_pool)
612 struct openssl_qp *qp = NULL;
614 /* Free memory prior to re-allocation if needed. */
615 if (dev->data->queue_pairs[qp_id] != NULL)
616 openssl_pmd_qp_release(dev, qp_id);
618 /* Allocate the queue pair data structure. */
619 qp = rte_zmalloc_socket("OPENSSL PMD Queue Pair", sizeof(*qp),
620 RTE_CACHE_LINE_SIZE, socket_id);
625 dev->data->queue_pairs[qp_id] = qp;
627 if (openssl_pmd_qp_set_unique_name(dev, qp))
628 goto qp_setup_cleanup;
630 qp->processed_ops = openssl_pmd_qp_create_processed_ops_ring(qp,
631 qp_conf->nb_descriptors, socket_id);
632 if (qp->processed_ops == NULL)
633 goto qp_setup_cleanup;
635 qp->sess_mp = session_pool;
637 memset(&qp->stats, 0, sizeof(qp->stats));
648 /** Start queue pair */
650 openssl_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
651 __rte_unused uint16_t queue_pair_id)
656 /** Stop queue pair */
658 openssl_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
659 __rte_unused uint16_t queue_pair_id)
664 /** Return the number of allocated queue pairs */
666 openssl_pmd_qp_count(struct rte_cryptodev *dev)
668 return dev->data->nb_queue_pairs;
671 /** Returns the size of the session structure */
673 openssl_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
675 return sizeof(struct openssl_session);
678 /** Configure the session from a crypto xform chain */
680 openssl_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
681 struct rte_crypto_sym_xform *xform,
682 struct rte_cryptodev_sym_session *sess,
683 struct rte_mempool *mempool)
685 void *sess_private_data;
688 if (unlikely(sess == NULL)) {
689 OPENSSL_LOG_ERR("invalid session struct");
693 if (rte_mempool_get(mempool, &sess_private_data)) {
695 "Couldn't get object from session mempool");
699 ret = openssl_set_session_parameters(sess_private_data, xform);
701 OPENSSL_LOG_ERR("failed configure session parameters");
703 /* Return session to mempool */
704 rte_mempool_put(mempool, sess_private_data);
708 set_session_private_data(sess, dev->driver_id,
715 /** Clear the memory of session so it doesn't leave key material behind */
717 openssl_pmd_session_clear(struct rte_cryptodev *dev,
718 struct rte_cryptodev_sym_session *sess)
720 uint8_t index = dev->driver_id;
721 void *sess_priv = get_session_private_data(sess, index);
723 /* Zero out the whole structure */
725 openssl_reset_session(sess_priv);
726 memset(sess_priv, 0, sizeof(struct openssl_session));
727 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
728 set_session_private_data(sess, index, NULL);
729 rte_mempool_put(sess_mp, sess_priv);
733 struct rte_cryptodev_ops openssl_pmd_ops = {
734 .dev_configure = openssl_pmd_config,
735 .dev_start = openssl_pmd_start,
736 .dev_stop = openssl_pmd_stop,
737 .dev_close = openssl_pmd_close,
739 .stats_get = openssl_pmd_stats_get,
740 .stats_reset = openssl_pmd_stats_reset,
742 .dev_infos_get = openssl_pmd_info_get,
744 .queue_pair_setup = openssl_pmd_qp_setup,
745 .queue_pair_release = openssl_pmd_qp_release,
746 .queue_pair_start = openssl_pmd_qp_start,
747 .queue_pair_stop = openssl_pmd_qp_stop,
748 .queue_pair_count = openssl_pmd_qp_count,
750 .session_get_size = openssl_pmd_session_get_size,
751 .session_configure = openssl_pmd_session_configure,
752 .session_clear = openssl_pmd_session_clear
755 struct rte_cryptodev_ops *rte_openssl_pmd_ops = &openssl_pmd_ops;