194b0ad8eSDeclan Doherty /*- 294b0ad8eSDeclan Doherty * BSD LICENSE 394b0ad8eSDeclan Doherty * 494b0ad8eSDeclan Doherty * Copyright(c) 2016 Intel Corporation. All rights reserved. 594b0ad8eSDeclan Doherty * 694b0ad8eSDeclan Doherty * Redistribution and use in source and binary forms, with or without 794b0ad8eSDeclan Doherty * modification, are permitted provided that the following conditions 894b0ad8eSDeclan Doherty * are met: 994b0ad8eSDeclan Doherty * 1094b0ad8eSDeclan Doherty * * Redistributions of source code must retain the above copyright 1194b0ad8eSDeclan Doherty * notice, this list of conditions and the following disclaimer. 1294b0ad8eSDeclan Doherty * * Redistributions in binary form must reproduce the above copyright 1394b0ad8eSDeclan Doherty * notice, this list of conditions and the following disclaimer in 1494b0ad8eSDeclan Doherty * the documentation and/or other materials provided with the 1594b0ad8eSDeclan Doherty * distribution. 1694b0ad8eSDeclan Doherty * * Neither the name of Intel Corporation nor the names of its 1794b0ad8eSDeclan Doherty * contributors may be used to endorse or promote products derived 1894b0ad8eSDeclan Doherty * from this software without specific prior written permission. 1994b0ad8eSDeclan Doherty * 2094b0ad8eSDeclan Doherty * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2194b0ad8eSDeclan Doherty * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2294b0ad8eSDeclan Doherty * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2394b0ad8eSDeclan Doherty * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2494b0ad8eSDeclan Doherty * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2594b0ad8eSDeclan Doherty * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2694b0ad8eSDeclan Doherty * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2794b0ad8eSDeclan Doherty * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2894b0ad8eSDeclan Doherty * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2994b0ad8eSDeclan Doherty * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3094b0ad8eSDeclan Doherty * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3194b0ad8eSDeclan Doherty */ 3294b0ad8eSDeclan Doherty 3394b0ad8eSDeclan Doherty #include <rte_common.h> 3494b0ad8eSDeclan Doherty #include <rte_config.h> 3594b0ad8eSDeclan Doherty #include <rte_cryptodev_pmd.h> 3694b0ad8eSDeclan Doherty #include <rte_dev.h> 3794b0ad8eSDeclan Doherty #include <rte_malloc.h> 3894b0ad8eSDeclan Doherty 3994b0ad8eSDeclan Doherty #include "null_crypto_pmd_private.h" 4094b0ad8eSDeclan Doherty 4194b0ad8eSDeclan Doherty /** 4294b0ad8eSDeclan Doherty * Global static parameter used to create a unique name for each crypto device. 4394b0ad8eSDeclan Doherty */ 4494b0ad8eSDeclan Doherty static unsigned unique_name_id; 4594b0ad8eSDeclan Doherty 4694b0ad8eSDeclan Doherty static inline int 4794b0ad8eSDeclan Doherty create_unique_device_name(char *name, size_t size) 4894b0ad8eSDeclan Doherty { 4994b0ad8eSDeclan Doherty int ret; 5094b0ad8eSDeclan Doherty 5194b0ad8eSDeclan Doherty if (name == NULL) 5294b0ad8eSDeclan Doherty return -EINVAL; 5394b0ad8eSDeclan Doherty 5494b0ad8eSDeclan Doherty ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_NULL_PMD, 5594b0ad8eSDeclan Doherty unique_name_id++); 5694b0ad8eSDeclan Doherty if (ret < 0) 5794b0ad8eSDeclan Doherty return ret; 5894b0ad8eSDeclan Doherty return 0; 5994b0ad8eSDeclan Doherty } 6094b0ad8eSDeclan Doherty 6194b0ad8eSDeclan Doherty 6294b0ad8eSDeclan Doherty /** verify and set session parameters */ 6394b0ad8eSDeclan Doherty int 6494b0ad8eSDeclan Doherty null_crypto_set_session_parameters( 6594b0ad8eSDeclan Doherty struct null_crypto_session *sess __rte_unused, 6694b0ad8eSDeclan Doherty const struct rte_crypto_sym_xform *xform) 6794b0ad8eSDeclan Doherty { 6894b0ad8eSDeclan Doherty if (xform == NULL) { 6994b0ad8eSDeclan Doherty return -1; 7094b0ad8eSDeclan Doherty } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && 7194b0ad8eSDeclan Doherty xform->next == NULL) { 7294b0ad8eSDeclan Doherty /* Authentication Only */ 7394b0ad8eSDeclan Doherty if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL) 7494b0ad8eSDeclan Doherty return 0; 7594b0ad8eSDeclan Doherty } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && 7694b0ad8eSDeclan Doherty xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) { 7794b0ad8eSDeclan Doherty /* Authentication then Cipher */ 7894b0ad8eSDeclan Doherty if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL && 7994b0ad8eSDeclan Doherty xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL) 8094b0ad8eSDeclan Doherty return 0; 8194b0ad8eSDeclan Doherty } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && 8294b0ad8eSDeclan Doherty xform->next == NULL) { 8394b0ad8eSDeclan Doherty /* Cipher Only */ 8494b0ad8eSDeclan Doherty if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL) 8594b0ad8eSDeclan Doherty return 0; 8694b0ad8eSDeclan Doherty } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && 8794b0ad8eSDeclan Doherty xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) { 8894b0ad8eSDeclan Doherty /* Cipher then Authentication */ 8994b0ad8eSDeclan Doherty if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL && 9094b0ad8eSDeclan Doherty xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL) 9194b0ad8eSDeclan Doherty return 0; 9294b0ad8eSDeclan Doherty } 9394b0ad8eSDeclan Doherty 9494b0ad8eSDeclan Doherty return -1; 9594b0ad8eSDeclan Doherty } 9694b0ad8eSDeclan Doherty 9794b0ad8eSDeclan Doherty /** Process crypto operation for mbuf */ 9894b0ad8eSDeclan Doherty static int 9994b0ad8eSDeclan Doherty process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op, 10094b0ad8eSDeclan Doherty struct null_crypto_session *sess __rte_unused) 10194b0ad8eSDeclan Doherty { 10294b0ad8eSDeclan Doherty /* set status as successful by default */ 10394b0ad8eSDeclan Doherty op->status = RTE_CRYPTO_OP_STATUS_SUCCESS; 10494b0ad8eSDeclan Doherty 10594b0ad8eSDeclan Doherty /* 10694b0ad8eSDeclan Doherty * if crypto session and operation are valid just enqueue the packet 10794b0ad8eSDeclan Doherty * in the processed ring 10894b0ad8eSDeclan Doherty */ 10994b0ad8eSDeclan Doherty return rte_ring_enqueue(qp->processed_pkts, (void *)op); 11094b0ad8eSDeclan Doherty } 11194b0ad8eSDeclan Doherty 11294b0ad8eSDeclan Doherty static struct null_crypto_session * 11394b0ad8eSDeclan Doherty get_session(struct null_crypto_qp *qp, struct rte_crypto_sym_op *op) 11494b0ad8eSDeclan Doherty { 11594b0ad8eSDeclan Doherty struct null_crypto_session *sess; 11694b0ad8eSDeclan Doherty 11794b0ad8eSDeclan Doherty if (op->type == RTE_CRYPTO_SYM_OP_WITH_SESSION) { 11894b0ad8eSDeclan Doherty if (unlikely(op->session == NULL || 11994b0ad8eSDeclan Doherty op->session->type != RTE_CRYPTODEV_NULL_PMD)) 12094b0ad8eSDeclan Doherty return NULL; 12194b0ad8eSDeclan Doherty 12294b0ad8eSDeclan Doherty sess = (struct null_crypto_session *)op->session->_private; 12394b0ad8eSDeclan Doherty } else { 12494b0ad8eSDeclan Doherty struct rte_cryptodev_session *c_sess = NULL; 12594b0ad8eSDeclan Doherty 12694b0ad8eSDeclan Doherty if (rte_mempool_get(qp->sess_mp, (void **)&c_sess)) 12794b0ad8eSDeclan Doherty return NULL; 12894b0ad8eSDeclan Doherty 12994b0ad8eSDeclan Doherty sess = (struct null_crypto_session *)c_sess->_private; 13094b0ad8eSDeclan Doherty 13194b0ad8eSDeclan Doherty if (null_crypto_set_session_parameters(sess, op->xform) != 0) 13294b0ad8eSDeclan Doherty return NULL; 13394b0ad8eSDeclan Doherty } 13494b0ad8eSDeclan Doherty 13594b0ad8eSDeclan Doherty return sess; 13694b0ad8eSDeclan Doherty } 13794b0ad8eSDeclan Doherty 13894b0ad8eSDeclan Doherty /** Enqueue burst */ 13994b0ad8eSDeclan Doherty static uint16_t 14094b0ad8eSDeclan Doherty null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops, 14194b0ad8eSDeclan Doherty uint16_t nb_ops) 14294b0ad8eSDeclan Doherty { 14394b0ad8eSDeclan Doherty struct null_crypto_session *sess; 14494b0ad8eSDeclan Doherty struct null_crypto_qp *qp = queue_pair; 14594b0ad8eSDeclan Doherty 14694b0ad8eSDeclan Doherty int i, retval; 14794b0ad8eSDeclan Doherty 14894b0ad8eSDeclan Doherty for (i = 0; i < nb_ops; i++) { 14994b0ad8eSDeclan Doherty sess = get_session(qp, ops[i]->sym); 15094b0ad8eSDeclan Doherty if (unlikely(sess == NULL)) 15194b0ad8eSDeclan Doherty goto enqueue_err; 15294b0ad8eSDeclan Doherty 15394b0ad8eSDeclan Doherty retval = process_op(qp, ops[i], sess); 15494b0ad8eSDeclan Doherty if (unlikely(retval < 0)) 15594b0ad8eSDeclan Doherty goto enqueue_err; 15694b0ad8eSDeclan Doherty } 15794b0ad8eSDeclan Doherty 15894b0ad8eSDeclan Doherty qp->qp_stats.enqueued_count += i; 15994b0ad8eSDeclan Doherty return i; 16094b0ad8eSDeclan Doherty 16194b0ad8eSDeclan Doherty enqueue_err: 16294b0ad8eSDeclan Doherty if (ops[i]) 16394b0ad8eSDeclan Doherty ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS; 16494b0ad8eSDeclan Doherty 16594b0ad8eSDeclan Doherty qp->qp_stats.enqueue_err_count++; 16694b0ad8eSDeclan Doherty return i; 16794b0ad8eSDeclan Doherty } 16894b0ad8eSDeclan Doherty 16994b0ad8eSDeclan Doherty /** Dequeue burst */ 17094b0ad8eSDeclan Doherty static uint16_t 17194b0ad8eSDeclan Doherty null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops, 17294b0ad8eSDeclan Doherty uint16_t nb_ops) 17394b0ad8eSDeclan Doherty { 17494b0ad8eSDeclan Doherty struct null_crypto_qp *qp = queue_pair; 17594b0ad8eSDeclan Doherty 17694b0ad8eSDeclan Doherty unsigned nb_dequeued; 17794b0ad8eSDeclan Doherty 17894b0ad8eSDeclan Doherty nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts, 17994b0ad8eSDeclan Doherty (void **)ops, nb_ops); 18094b0ad8eSDeclan Doherty qp->qp_stats.dequeued_count += nb_dequeued; 18194b0ad8eSDeclan Doherty 18294b0ad8eSDeclan Doherty return nb_dequeued; 18394b0ad8eSDeclan Doherty } 18494b0ad8eSDeclan Doherty 18594b0ad8eSDeclan Doherty static int cryptodev_null_uninit(const char *name); 18694b0ad8eSDeclan Doherty 18794b0ad8eSDeclan Doherty /** Create crypto device */ 18894b0ad8eSDeclan Doherty static int 18994b0ad8eSDeclan Doherty cryptodev_null_create(const char *name, 19094b0ad8eSDeclan Doherty struct rte_crypto_vdev_init_params *init_params) 19194b0ad8eSDeclan Doherty { 19294b0ad8eSDeclan Doherty struct rte_cryptodev *dev; 19394b0ad8eSDeclan Doherty char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN]; 19494b0ad8eSDeclan Doherty struct null_crypto_private *internals; 19594b0ad8eSDeclan Doherty 19694b0ad8eSDeclan Doherty /* create a unique device name */ 19794b0ad8eSDeclan Doherty if (create_unique_device_name(crypto_dev_name, 19894b0ad8eSDeclan Doherty RTE_CRYPTODEV_NAME_MAX_LEN) != 0) { 19994b0ad8eSDeclan Doherty NULL_CRYPTO_LOG_ERR("failed to create unique cryptodev name"); 20094b0ad8eSDeclan Doherty return -EINVAL; 20194b0ad8eSDeclan Doherty } 20294b0ad8eSDeclan Doherty 20394b0ad8eSDeclan Doherty dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name, 20494b0ad8eSDeclan Doherty sizeof(struct null_crypto_private), 20594b0ad8eSDeclan Doherty init_params->socket_id); 20694b0ad8eSDeclan Doherty if (dev == NULL) { 20794b0ad8eSDeclan Doherty NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev"); 20894b0ad8eSDeclan Doherty goto init_error; 20994b0ad8eSDeclan Doherty } 21094b0ad8eSDeclan Doherty 21194b0ad8eSDeclan Doherty dev->dev_type = RTE_CRYPTODEV_NULL_PMD; 21294b0ad8eSDeclan Doherty dev->dev_ops = null_crypto_pmd_ops; 21394b0ad8eSDeclan Doherty 21494b0ad8eSDeclan Doherty /* register rx/tx burst functions for data path */ 21594b0ad8eSDeclan Doherty dev->dequeue_burst = null_crypto_pmd_dequeue_burst; 21694b0ad8eSDeclan Doherty dev->enqueue_burst = null_crypto_pmd_enqueue_burst; 21794b0ad8eSDeclan Doherty 218*26c2e4adSDeclan Doherty dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO | 219*26c2e4adSDeclan Doherty RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING; 220*26c2e4adSDeclan Doherty 22194b0ad8eSDeclan Doherty internals = dev->data->dev_private; 22294b0ad8eSDeclan Doherty 22394b0ad8eSDeclan Doherty internals->max_nb_qpairs = init_params->max_nb_queue_pairs; 22494b0ad8eSDeclan Doherty internals->max_nb_sessions = init_params->max_nb_sessions; 22594b0ad8eSDeclan Doherty 22694b0ad8eSDeclan Doherty return 0; 22794b0ad8eSDeclan Doherty 22894b0ad8eSDeclan Doherty init_error: 22994b0ad8eSDeclan Doherty NULL_CRYPTO_LOG_ERR("driver %s: cryptodev_null_create failed", name); 23094b0ad8eSDeclan Doherty cryptodev_null_uninit(crypto_dev_name); 23194b0ad8eSDeclan Doherty 23294b0ad8eSDeclan Doherty return -EFAULT; 23394b0ad8eSDeclan Doherty } 23494b0ad8eSDeclan Doherty 23594b0ad8eSDeclan Doherty /** Initialise null crypto device */ 23694b0ad8eSDeclan Doherty static int 23794b0ad8eSDeclan Doherty cryptodev_null_init(const char *name, 23894b0ad8eSDeclan Doherty const char *input_args) 23994b0ad8eSDeclan Doherty { 24094b0ad8eSDeclan Doherty struct rte_crypto_vdev_init_params init_params = { 24194b0ad8eSDeclan Doherty RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS, 24294b0ad8eSDeclan Doherty RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS, 24394b0ad8eSDeclan Doherty rte_socket_id() 24494b0ad8eSDeclan Doherty }; 24594b0ad8eSDeclan Doherty 24694b0ad8eSDeclan Doherty rte_cryptodev_parse_vdev_init_params(&init_params, input_args); 24794b0ad8eSDeclan Doherty 24894b0ad8eSDeclan Doherty RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name, 24994b0ad8eSDeclan Doherty init_params.socket_id); 25094b0ad8eSDeclan Doherty RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n", 25194b0ad8eSDeclan Doherty init_params.max_nb_queue_pairs); 25294b0ad8eSDeclan Doherty RTE_LOG(INFO, PMD, " Max number of sessions = %d\n", 25394b0ad8eSDeclan Doherty init_params.max_nb_sessions); 25494b0ad8eSDeclan Doherty 25594b0ad8eSDeclan Doherty return cryptodev_null_create(name, &init_params); 25694b0ad8eSDeclan Doherty } 25794b0ad8eSDeclan Doherty 25894b0ad8eSDeclan Doherty /** Uninitialise null crypto device */ 25994b0ad8eSDeclan Doherty static int 26094b0ad8eSDeclan Doherty cryptodev_null_uninit(const char *name) 26194b0ad8eSDeclan Doherty { 26294b0ad8eSDeclan Doherty if (name == NULL) 26394b0ad8eSDeclan Doherty return -EINVAL; 26494b0ad8eSDeclan Doherty 26594b0ad8eSDeclan Doherty RTE_LOG(INFO, PMD, "Closing null crypto device %s on numa socket %u\n", 26694b0ad8eSDeclan Doherty name, rte_socket_id()); 26794b0ad8eSDeclan Doherty 26894b0ad8eSDeclan Doherty return 0; 26994b0ad8eSDeclan Doherty } 27094b0ad8eSDeclan Doherty 27194b0ad8eSDeclan Doherty static struct rte_driver cryptodev_null_pmd_drv = { 27294b0ad8eSDeclan Doherty .name = CRYPTODEV_NAME_NULL_PMD, 27394b0ad8eSDeclan Doherty .type = PMD_VDEV, 27494b0ad8eSDeclan Doherty .init = cryptodev_null_init, 27594b0ad8eSDeclan Doherty .uninit = cryptodev_null_uninit 27694b0ad8eSDeclan Doherty }; 27794b0ad8eSDeclan Doherty 27894b0ad8eSDeclan Doherty PMD_REGISTER_DRIVER(cryptodev_null_pmd_drv); 279