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.
33 #include <rte_common.h>
34 #include <rte_config.h>
35 #include <rte_cryptodev_pmd.h>
36 #include <rte_cryptodev_vdev.h>
38 #include <rte_malloc.h>
40 #include "null_crypto_pmd_private.h"
42 static uint8_t cryptodev_driver_id;
44 /** verify and set session parameters */
46 null_crypto_set_session_parameters(
47 struct null_crypto_session *sess __rte_unused,
48 const struct rte_crypto_sym_xform *xform)
52 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
53 xform->next == NULL) {
54 /* Authentication Only */
55 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
57 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
58 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
59 /* Authentication then Cipher */
60 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
61 xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
63 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
64 xform->next == NULL) {
66 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
68 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
69 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
70 /* Cipher then Authentication */
71 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL &&
72 xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
79 /** Process crypto operation for mbuf */
81 process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op,
82 struct null_crypto_session *sess __rte_unused)
84 /* set status as successful by default */
85 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
87 /* Free session if a session-less crypto op. */
88 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
89 memset(op->sym->session, 0,
90 sizeof(struct null_crypto_session));
91 rte_cryptodev_sym_session_free(op->sym->session);
92 op->sym->session = NULL;
96 * if crypto session and operation are valid just enqueue the packet
97 * in the processed ring
99 return rte_ring_enqueue(qp->processed_pkts, (void *)op);
102 static struct null_crypto_session *
103 get_session(struct null_crypto_qp *qp, struct rte_crypto_op *op)
105 struct null_crypto_session *sess = NULL;
106 struct rte_crypto_sym_op *sym_op = op->sym;
108 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
109 if (likely(sym_op->session != NULL))
110 sess = (struct null_crypto_session *)
111 get_session_private_data(
112 sym_op->session, cryptodev_driver_id);
115 void *_sess_private_data = NULL;
117 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
120 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
123 sess = (struct null_crypto_session *)_sess_private_data;
125 if (unlikely(null_crypto_set_session_parameters(sess,
126 sym_op->xform) != 0)) {
127 rte_mempool_put(qp->sess_mp, _sess);
128 rte_mempool_put(qp->sess_mp, _sess_private_data);
131 sym_op->session = (struct rte_cryptodev_sym_session *)_sess;
132 set_session_private_data(sym_op->session, cryptodev_driver_id,
141 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
144 struct null_crypto_session *sess;
145 struct null_crypto_qp *qp = queue_pair;
149 for (i = 0; i < nb_ops; i++) {
150 sess = get_session(qp, ops[i]);
151 if (unlikely(sess == NULL))
154 retval = process_op(qp, ops[i], sess);
155 if (unlikely(retval < 0))
159 qp->qp_stats.enqueued_count += i;
164 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
166 qp->qp_stats.enqueue_err_count++;
172 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
175 struct null_crypto_qp *qp = queue_pair;
177 unsigned nb_dequeued;
179 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
180 (void **)ops, nb_ops, NULL);
181 qp->qp_stats.dequeued_count += nb_dequeued;
186 static int cryptodev_null_remove(const char *name);
188 /** Create crypto device */
190 cryptodev_null_create(const char *name,
191 struct rte_vdev_device *vdev,
192 struct rte_crypto_vdev_init_params *init_params)
194 struct rte_cryptodev *dev;
195 struct null_crypto_private *internals;
197 if (init_params->name[0] == '\0')
198 snprintf(init_params->name, sizeof(init_params->name),
201 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
202 sizeof(struct null_crypto_private),
203 init_params->socket_id,
206 NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
210 dev->driver_id = cryptodev_driver_id;
211 dev->dev_ops = null_crypto_pmd_ops;
213 /* register rx/tx burst functions for data path */
214 dev->dequeue_burst = null_crypto_pmd_dequeue_burst;
215 dev->enqueue_burst = null_crypto_pmd_enqueue_burst;
217 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
218 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
219 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
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",
231 cryptodev_null_remove(init_params->name);
236 /** Initialise null crypto device */
238 cryptodev_null_probe(struct rte_vdev_device *dev)
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,
248 name = rte_vdev_device_name(dev);
252 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n",
253 name, init_params.socket_id);
254 if (init_params.name[0] != '\0')
255 RTE_LOG(INFO, PMD, " User defined name = %s\n",
257 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
258 init_params.max_nb_queue_pairs);
259 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
260 init_params.max_nb_sessions);
262 return cryptodev_null_create(name, dev, &init_params);
265 /** Uninitialise null crypto device */
267 cryptodev_null_remove(const char *name)
272 RTE_LOG(INFO, PMD, "Closing null crypto device %s on numa socket %u\n",
273 name, rte_socket_id());
279 cryptodev_null_remove_dev(struct rte_vdev_device *dev)
281 return cryptodev_null_remove(rte_vdev_device_name(dev));
284 static struct rte_vdev_driver cryptodev_null_pmd_drv = {
285 .probe = cryptodev_null_probe,
286 .remove = cryptodev_null_remove_dev,
289 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd_drv);
290 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd);
291 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD,
292 "max_nb_queue_pairs=<int> "
293 "max_nb_sessions=<int> "
295 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_null_pmd_drv, cryptodev_driver_id);