4 * Copyright(c) 2016 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.
33 #include <rte_common.h>
34 #include <rte_config.h>
35 #include <rte_cryptodev_pmd.h>
37 #include <rte_malloc.h>
39 #include "null_crypto_pmd_private.h"
42 * Global static parameter used to create a unique name for each crypto device.
44 static unsigned unique_name_id;
47 create_unique_device_name(char *name, size_t size)
54 ret = snprintf(name, size, "%s_%u", RTE_STR(CRYPTODEV_NAME_NULL_PMD),
62 /** verify and set session parameters */
64 null_crypto_set_session_parameters(
65 struct null_crypto_session *sess __rte_unused,
66 const struct rte_crypto_sym_xform *xform)
70 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
71 xform->next == NULL) {
72 /* Authentication Only */
73 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
75 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
76 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
77 /* Authentication then Cipher */
78 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
79 xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
81 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
82 xform->next == NULL) {
84 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
86 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
87 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
88 /* Cipher then Authentication */
89 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL &&
90 xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
97 /** Process crypto operation for mbuf */
99 process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op,
100 struct null_crypto_session *sess __rte_unused)
102 /* set status as successful by default */
103 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
106 * if crypto session and operation are valid just enqueue the packet
107 * in the processed ring
109 return rte_ring_enqueue(qp->processed_pkts, (void *)op);
112 static struct null_crypto_session *
113 get_session(struct null_crypto_qp *qp, struct rte_crypto_sym_op *op)
115 struct null_crypto_session *sess;
117 if (op->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
118 if (unlikely(op->session == NULL ||
119 op->session->dev_type != RTE_CRYPTODEV_NULL_PMD))
122 sess = (struct null_crypto_session *)op->session->_private;
124 struct rte_cryptodev_session *c_sess = NULL;
126 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
129 sess = (struct null_crypto_session *)c_sess->_private;
131 if (null_crypto_set_session_parameters(sess, op->xform) != 0)
140 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
143 struct null_crypto_session *sess;
144 struct null_crypto_qp *qp = queue_pair;
148 for (i = 0; i < nb_ops; i++) {
149 sess = get_session(qp, ops[i]->sym);
150 if (unlikely(sess == NULL))
153 retval = process_op(qp, ops[i], sess);
154 if (unlikely(retval < 0))
158 qp->qp_stats.enqueued_count += i;
163 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
165 qp->qp_stats.enqueue_err_count++;
171 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
174 struct null_crypto_qp *qp = queue_pair;
176 unsigned nb_dequeued;
178 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
179 (void **)ops, nb_ops);
180 qp->qp_stats.dequeued_count += nb_dequeued;
185 static int cryptodev_null_uninit(const char *name);
187 /** Create crypto device */
189 cryptodev_null_create(const char *name,
190 struct rte_crypto_vdev_init_params *init_params)
192 struct rte_cryptodev *dev;
193 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
194 struct null_crypto_private *internals;
196 /* create a unique device name */
197 if (create_unique_device_name(crypto_dev_name,
198 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
199 NULL_CRYPTO_LOG_ERR("failed to create unique cryptodev name");
203 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
204 sizeof(struct null_crypto_private),
205 init_params->socket_id);
207 NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
211 dev->dev_type = RTE_CRYPTODEV_NULL_PMD;
212 dev->dev_ops = null_crypto_pmd_ops;
214 /* register rx/tx burst functions for data path */
215 dev->dequeue_burst = null_crypto_pmd_dequeue_burst;
216 dev->enqueue_burst = null_crypto_pmd_enqueue_burst;
218 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
219 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
221 internals = dev->data->dev_private;
223 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
224 internals->max_nb_sessions = init_params->max_nb_sessions;
229 NULL_CRYPTO_LOG_ERR("driver %s: cryptodev_null_create failed", name);
230 cryptodev_null_uninit(crypto_dev_name);
235 /** Initialise null crypto device */
237 cryptodev_null_init(const char *name,
238 const char *input_args)
240 struct rte_crypto_vdev_init_params init_params = {
241 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
242 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
246 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
248 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
249 init_params.socket_id);
250 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
251 init_params.max_nb_queue_pairs);
252 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
253 init_params.max_nb_sessions);
255 return cryptodev_null_create(name, &init_params);
258 /** Uninitialise null crypto device */
260 cryptodev_null_uninit(const char *name)
265 RTE_LOG(INFO, PMD, "Closing null crypto device %s on numa socket %u\n",
266 name, rte_socket_id());
271 static struct rte_driver cryptodev_null_pmd_drv = {
273 .init = cryptodev_null_init,
274 .uninit = cryptodev_null_uninit
277 PMD_REGISTER_DRIVER(cryptodev_null_pmd_drv, CRYPTODEV_NAME_NULL_PMD);
278 DRIVER_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD,
279 "max_nb_queue_pairs=<int> "
280 "max_nb_sessions=<int> "