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_cryptodev_pmd.h>
35 #include <rte_bus_vdev.h>
36 #include <rte_malloc.h>
38 #include "null_crypto_pmd_private.h"
40 static uint8_t cryptodev_driver_id;
42 /** verify and set session parameters */
44 null_crypto_set_session_parameters(
45 struct null_crypto_session *sess __rte_unused,
46 const struct rte_crypto_sym_xform *xform)
50 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
51 xform->next == NULL) {
52 /* Authentication Only */
53 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
55 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
56 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
57 /* Authentication then Cipher */
58 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
59 xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
61 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
62 xform->next == NULL) {
64 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
66 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
67 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
68 /* Cipher then Authentication */
69 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL &&
70 xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
77 /** Process crypto operation for mbuf */
79 process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op,
80 struct null_crypto_session *sess __rte_unused)
82 /* set status as successful by default */
83 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
85 /* Free session if a session-less crypto op. */
86 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
87 memset(op->sym->session, 0,
88 sizeof(struct null_crypto_session));
89 rte_cryptodev_sym_session_free(op->sym->session);
90 op->sym->session = NULL;
94 * if crypto session and operation are valid just enqueue the packet
95 * in the processed ring
97 return rte_ring_enqueue(qp->processed_pkts, (void *)op);
100 static struct null_crypto_session *
101 get_session(struct null_crypto_qp *qp, struct rte_crypto_op *op)
103 struct null_crypto_session *sess = NULL;
104 struct rte_crypto_sym_op *sym_op = op->sym;
106 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
107 if (likely(sym_op->session != NULL))
108 sess = (struct null_crypto_session *)
109 get_session_private_data(
110 sym_op->session, cryptodev_driver_id);
113 void *_sess_private_data = NULL;
115 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
118 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
121 sess = (struct null_crypto_session *)_sess_private_data;
123 if (unlikely(null_crypto_set_session_parameters(sess,
124 sym_op->xform) != 0)) {
125 rte_mempool_put(qp->sess_mp, _sess);
126 rte_mempool_put(qp->sess_mp, _sess_private_data);
129 sym_op->session = (struct rte_cryptodev_sym_session *)_sess;
130 set_session_private_data(sym_op->session, cryptodev_driver_id,
139 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
142 struct null_crypto_session *sess;
143 struct null_crypto_qp *qp = queue_pair;
147 for (i = 0; i < nb_ops; i++) {
148 sess = get_session(qp, ops[i]);
149 if (unlikely(sess == NULL))
152 retval = process_op(qp, ops[i], sess);
153 if (unlikely(retval < 0))
157 qp->qp_stats.enqueued_count += i;
162 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
164 qp->qp_stats.enqueue_err_count++;
170 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
173 struct null_crypto_qp *qp = queue_pair;
175 unsigned nb_dequeued;
177 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
178 (void **)ops, nb_ops, NULL);
179 qp->qp_stats.dequeued_count += nb_dequeued;
184 /** Create crypto device */
186 cryptodev_null_create(const char *name,
187 struct rte_vdev_device *vdev,
188 struct rte_cryptodev_pmd_init_params *init_params)
190 struct rte_cryptodev *dev;
191 struct null_crypto_private *internals;
193 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
195 NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
199 dev->driver_id = cryptodev_driver_id;
200 dev->dev_ops = null_crypto_pmd_ops;
202 /* register rx/tx burst functions for data path */
203 dev->dequeue_burst = null_crypto_pmd_dequeue_burst;
204 dev->enqueue_burst = null_crypto_pmd_enqueue_burst;
206 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
207 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
208 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
210 internals = dev->data->dev_private;
212 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
213 internals->max_nb_sessions = init_params->max_nb_sessions;
218 /** Initialise null crypto device */
220 cryptodev_null_probe(struct rte_vdev_device *dev)
222 struct rte_cryptodev_pmd_init_params init_params = {
224 sizeof(struct null_crypto_private),
226 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
227 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
229 const char *name, *args;
232 name = rte_vdev_device_name(dev);
236 args = rte_vdev_device_args(dev);
238 retval = rte_cryptodev_pmd_parse_input_args(&init_params, args);
241 "Failed to parse initialisation arguments[%s]\n", args);
245 return cryptodev_null_create(name, dev, &init_params);
249 cryptodev_null_remove_dev(struct rte_vdev_device *vdev)
251 struct rte_cryptodev *cryptodev;
254 name = rte_vdev_device_name(vdev);
258 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
259 if (cryptodev == NULL)
262 return rte_cryptodev_pmd_destroy(cryptodev);
265 static struct rte_vdev_driver cryptodev_null_pmd_drv = {
266 .probe = cryptodev_null_probe,
267 .remove = cryptodev_null_remove_dev,
270 static struct cryptodev_driver null_crypto_drv;
272 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd_drv);
273 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd);
274 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD,
275 "max_nb_queue_pairs=<int> "
276 "max_nb_sessions=<int> "
278 RTE_PMD_REGISTER_CRYPTO_DRIVER(null_crypto_drv, cryptodev_null_pmd_drv,
279 cryptodev_driver_id);