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,
66 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
68 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
70 .algo = RTE_CRYPTO_AUTH_MD5,
88 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
90 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
92 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
110 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
112 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
114 .algo = RTE_CRYPTO_AUTH_SHA1,
132 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
134 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
136 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
154 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
156 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
158 .algo = RTE_CRYPTO_AUTH_SHA224,
176 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
178 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
180 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
198 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
200 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
202 .algo = RTE_CRYPTO_AUTH_SHA256,
220 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
222 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
224 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
242 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
244 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
246 .algo = RTE_CRYPTO_AUTH_SHA384,
264 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
266 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
268 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
286 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
288 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
290 .algo = RTE_CRYPTO_AUTH_SHA512,
308 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
310 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
312 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
328 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
330 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
332 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
348 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
350 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
352 .algo = RTE_CRYPTO_AEAD_AES_GCM,
377 { /* AES GMAC (AUTH) */
378 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
380 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
382 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
403 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
405 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
407 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
423 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
425 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
427 .algo = RTE_CRYPTO_CIPHER_3DES_CTR,
442 { /* DES DOCSIS BPI */
443 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
445 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
447 .algo = RTE_CRYPTO_CIPHER_DES_DOCSISBPI,
463 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
467 /** Configure device */
469 openssl_pmd_config(__rte_unused struct rte_cryptodev *dev,
470 __rte_unused struct rte_cryptodev_config *config)
477 openssl_pmd_start(__rte_unused struct rte_cryptodev *dev)
484 openssl_pmd_stop(__rte_unused struct rte_cryptodev *dev)
490 openssl_pmd_close(__rte_unused struct rte_cryptodev *dev)
496 /** Get device statistics */
498 openssl_pmd_stats_get(struct rte_cryptodev *dev,
499 struct rte_cryptodev_stats *stats)
503 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
504 struct openssl_qp *qp = dev->data->queue_pairs[qp_id];
506 stats->enqueued_count += qp->stats.enqueued_count;
507 stats->dequeued_count += qp->stats.dequeued_count;
509 stats->enqueue_err_count += qp->stats.enqueue_err_count;
510 stats->dequeue_err_count += qp->stats.dequeue_err_count;
514 /** Reset device statistics */
516 openssl_pmd_stats_reset(struct rte_cryptodev *dev)
520 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
521 struct openssl_qp *qp = dev->data->queue_pairs[qp_id];
523 memset(&qp->stats, 0, sizeof(qp->stats));
528 /** Get device info */
530 openssl_pmd_info_get(struct rte_cryptodev *dev,
531 struct rte_cryptodev_info *dev_info)
533 struct openssl_private *internals = dev->data->dev_private;
535 if (dev_info != NULL) {
536 dev_info->driver_id = dev->driver_id;
537 dev_info->feature_flags = dev->feature_flags;
538 dev_info->capabilities = openssl_pmd_capabilities;
539 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
540 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
544 /** Release queue pair */
546 openssl_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
548 if (dev->data->queue_pairs[qp_id] != NULL) {
549 rte_free(dev->data->queue_pairs[qp_id]);
550 dev->data->queue_pairs[qp_id] = NULL;
555 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
557 openssl_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
558 struct openssl_qp *qp)
560 unsigned int n = snprintf(qp->name, sizeof(qp->name),
561 "openssl_pmd_%u_qp_%u",
562 dev->data->dev_id, qp->id);
564 if (n > sizeof(qp->name))
571 /** Create a ring to place processed operations on */
572 static struct rte_ring *
573 openssl_pmd_qp_create_processed_ops_ring(struct openssl_qp *qp,
574 unsigned int ring_size, int socket_id)
578 r = rte_ring_lookup(qp->name);
580 if (rte_ring_get_size(r) >= ring_size) {
582 "Reusing existing ring %s for processed ops",
588 "Unable to reuse existing ring %s for processed ops",
593 return rte_ring_create(qp->name, ring_size, socket_id,
594 RING_F_SP_ENQ | RING_F_SC_DEQ);
598 /** Setup a queue pair */
600 openssl_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
601 const struct rte_cryptodev_qp_conf *qp_conf,
604 struct openssl_qp *qp = NULL;
606 /* Free memory prior to re-allocation if needed. */
607 if (dev->data->queue_pairs[qp_id] != NULL)
608 openssl_pmd_qp_release(dev, qp_id);
610 /* Allocate the queue pair data structure. */
611 qp = rte_zmalloc_socket("OPENSSL PMD Queue Pair", sizeof(*qp),
612 RTE_CACHE_LINE_SIZE, socket_id);
617 dev->data->queue_pairs[qp_id] = qp;
619 if (openssl_pmd_qp_set_unique_name(dev, qp))
620 goto qp_setup_cleanup;
622 qp->processed_ops = openssl_pmd_qp_create_processed_ops_ring(qp,
623 qp_conf->nb_descriptors, socket_id);
624 if (qp->processed_ops == NULL)
625 goto qp_setup_cleanup;
627 qp->sess_mp = dev->data->session_pool;
629 memset(&qp->stats, 0, sizeof(qp->stats));
640 /** Start queue pair */
642 openssl_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
643 __rte_unused uint16_t queue_pair_id)
648 /** Stop queue pair */
650 openssl_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
651 __rte_unused uint16_t queue_pair_id)
656 /** Return the number of allocated queue pairs */
658 openssl_pmd_qp_count(struct rte_cryptodev *dev)
660 return dev->data->nb_queue_pairs;
663 /** Returns the size of the session structure */
665 openssl_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
667 return sizeof(struct openssl_session);
670 /** Configure the session from a crypto xform chain */
672 openssl_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
673 struct rte_crypto_sym_xform *xform,
674 struct rte_cryptodev_sym_session *sess,
675 struct rte_mempool *mempool)
677 void *sess_private_data;
679 if (unlikely(sess == NULL)) {
680 OPENSSL_LOG_ERR("invalid session struct");
684 if (rte_mempool_get(mempool, &sess_private_data)) {
686 "Couldn't get object from session mempool");
690 if (openssl_set_session_parameters(
691 sess_private_data, xform) != 0) {
692 OPENSSL_LOG_ERR("failed configure session parameters");
694 /* Return session to mempool */
695 rte_mempool_put(mempool, sess_private_data);
699 set_session_private_data(sess, dev->driver_id,
706 /** Clear the memory of session so it doesn't leave key material behind */
708 openssl_pmd_session_clear(struct rte_cryptodev *dev,
709 struct rte_cryptodev_sym_session *sess)
711 uint8_t index = dev->driver_id;
712 void *sess_priv = get_session_private_data(sess, index);
714 /* Zero out the whole structure */
716 openssl_reset_session(sess_priv);
717 memset(sess_priv, 0, sizeof(struct openssl_session));
718 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
719 set_session_private_data(sess, index, NULL);
720 rte_mempool_put(sess_mp, sess_priv);
724 struct rte_cryptodev_ops openssl_pmd_ops = {
725 .dev_configure = openssl_pmd_config,
726 .dev_start = openssl_pmd_start,
727 .dev_stop = openssl_pmd_stop,
728 .dev_close = openssl_pmd_close,
730 .stats_get = openssl_pmd_stats_get,
731 .stats_reset = openssl_pmd_stats_reset,
733 .dev_infos_get = openssl_pmd_info_get,
735 .queue_pair_setup = openssl_pmd_qp_setup,
736 .queue_pair_release = openssl_pmd_qp_release,
737 .queue_pair_start = openssl_pmd_qp_start,
738 .queue_pair_stop = openssl_pmd_qp_stop,
739 .queue_pair_count = openssl_pmd_qp_count,
741 .session_get_size = openssl_pmd_session_get_size,
742 .session_configure = openssl_pmd_session_configure,
743 .session_clear = openssl_pmd_session_clear
746 struct rte_cryptodev_ops *rte_openssl_pmd_ops = &openssl_pmd_ops;