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,
430 { /* DES DOCSIS BPI */
431 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
433 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
435 .algo = RTE_CRYPTO_CIPHER_DES_DOCSISBPI,
451 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
455 /** Configure device */
457 openssl_pmd_config(__rte_unused struct rte_cryptodev *dev,
458 __rte_unused struct rte_cryptodev_config *config)
465 openssl_pmd_start(__rte_unused struct rte_cryptodev *dev)
472 openssl_pmd_stop(__rte_unused struct rte_cryptodev *dev)
478 openssl_pmd_close(__rte_unused struct rte_cryptodev *dev)
484 /** Get device statistics */
486 openssl_pmd_stats_get(struct rte_cryptodev *dev,
487 struct rte_cryptodev_stats *stats)
491 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
492 struct openssl_qp *qp = dev->data->queue_pairs[qp_id];
494 stats->enqueued_count += qp->stats.enqueued_count;
495 stats->dequeued_count += qp->stats.dequeued_count;
497 stats->enqueue_err_count += qp->stats.enqueue_err_count;
498 stats->dequeue_err_count += qp->stats.dequeue_err_count;
502 /** Reset device statistics */
504 openssl_pmd_stats_reset(struct rte_cryptodev *dev)
508 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
509 struct openssl_qp *qp = dev->data->queue_pairs[qp_id];
511 memset(&qp->stats, 0, sizeof(qp->stats));
516 /** Get device info */
518 openssl_pmd_info_get(struct rte_cryptodev *dev,
519 struct rte_cryptodev_info *dev_info)
521 struct openssl_private *internals = dev->data->dev_private;
523 if (dev_info != NULL) {
524 dev_info->driver_id = dev->driver_id;
525 dev_info->feature_flags = dev->feature_flags;
526 dev_info->capabilities = openssl_pmd_capabilities;
527 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
528 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
532 /** Release queue pair */
534 openssl_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
536 if (dev->data->queue_pairs[qp_id] != NULL) {
537 rte_free(dev->data->queue_pairs[qp_id]);
538 dev->data->queue_pairs[qp_id] = NULL;
543 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
545 openssl_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
546 struct openssl_qp *qp)
548 unsigned int n = snprintf(qp->name, sizeof(qp->name),
549 "openssl_pmd_%u_qp_%u",
550 dev->data->dev_id, qp->id);
552 if (n > sizeof(qp->name))
559 /** Create a ring to place processed operations on */
560 static struct rte_ring *
561 openssl_pmd_qp_create_processed_ops_ring(struct openssl_qp *qp,
562 unsigned int ring_size, int socket_id)
566 r = rte_ring_lookup(qp->name);
568 if (rte_ring_get_size(r) >= ring_size) {
570 "Reusing existing ring %s for processed ops",
576 "Unable to reuse existing ring %s for processed ops",
581 return rte_ring_create(qp->name, ring_size, socket_id,
582 RING_F_SP_ENQ | RING_F_SC_DEQ);
586 /** Setup a queue pair */
588 openssl_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
589 const struct rte_cryptodev_qp_conf *qp_conf,
590 int socket_id, struct rte_mempool *session_pool)
592 struct openssl_qp *qp = NULL;
594 /* Free memory prior to re-allocation if needed. */
595 if (dev->data->queue_pairs[qp_id] != NULL)
596 openssl_pmd_qp_release(dev, qp_id);
598 /* Allocate the queue pair data structure. */
599 qp = rte_zmalloc_socket("OPENSSL PMD Queue Pair", sizeof(*qp),
600 RTE_CACHE_LINE_SIZE, socket_id);
605 dev->data->queue_pairs[qp_id] = qp;
607 if (openssl_pmd_qp_set_unique_name(dev, qp))
608 goto qp_setup_cleanup;
610 qp->processed_ops = openssl_pmd_qp_create_processed_ops_ring(qp,
611 qp_conf->nb_descriptors, socket_id);
612 if (qp->processed_ops == NULL)
613 goto qp_setup_cleanup;
615 qp->sess_mp = session_pool;
617 memset(&qp->stats, 0, sizeof(qp->stats));
628 /** Start queue pair */
630 openssl_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
631 __rte_unused uint16_t queue_pair_id)
636 /** Stop queue pair */
638 openssl_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
639 __rte_unused uint16_t queue_pair_id)
644 /** Return the number of allocated queue pairs */
646 openssl_pmd_qp_count(struct rte_cryptodev *dev)
648 return dev->data->nb_queue_pairs;
651 /** Returns the size of the session structure */
653 openssl_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
655 return sizeof(struct openssl_session);
658 /** Configure the session from a crypto xform chain */
660 openssl_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
661 struct rte_crypto_sym_xform *xform,
662 struct rte_cryptodev_sym_session *sess,
663 struct rte_mempool *mempool)
665 void *sess_private_data;
668 if (unlikely(sess == NULL)) {
669 OPENSSL_LOG_ERR("invalid session struct");
673 if (rte_mempool_get(mempool, &sess_private_data)) {
675 "Couldn't get object from session mempool");
679 ret = openssl_set_session_parameters(sess_private_data, xform);
681 OPENSSL_LOG_ERR("failed configure session parameters");
683 /* Return session to mempool */
684 rte_mempool_put(mempool, sess_private_data);
688 set_session_private_data(sess, dev->driver_id,
695 /** Clear the memory of session so it doesn't leave key material behind */
697 openssl_pmd_session_clear(struct rte_cryptodev *dev,
698 struct rte_cryptodev_sym_session *sess)
700 uint8_t index = dev->driver_id;
701 void *sess_priv = get_session_private_data(sess, index);
703 /* Zero out the whole structure */
705 openssl_reset_session(sess_priv);
706 memset(sess_priv, 0, sizeof(struct openssl_session));
707 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
708 set_session_private_data(sess, index, NULL);
709 rte_mempool_put(sess_mp, sess_priv);
713 struct rte_cryptodev_ops openssl_pmd_ops = {
714 .dev_configure = openssl_pmd_config,
715 .dev_start = openssl_pmd_start,
716 .dev_stop = openssl_pmd_stop,
717 .dev_close = openssl_pmd_close,
719 .stats_get = openssl_pmd_stats_get,
720 .stats_reset = openssl_pmd_stats_reset,
722 .dev_infos_get = openssl_pmd_info_get,
724 .queue_pair_setup = openssl_pmd_qp_setup,
725 .queue_pair_release = openssl_pmd_qp_release,
726 .queue_pair_start = openssl_pmd_qp_start,
727 .queue_pair_stop = openssl_pmd_qp_stop,
728 .queue_pair_count = openssl_pmd_qp_count,
730 .session_get_size = openssl_pmd_session_get_size,
731 .session_configure = openssl_pmd_session_configure,
732 .session_clear = openssl_pmd_session_clear
735 struct rte_cryptodev_ops *rte_openssl_pmd_ops = &openssl_pmd_ops;