xref: /dpdk/drivers/crypto/null/null_crypto_pmd.c (revision 0abf98589d9402c6fd9b8866639f1535ceb302ce)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32 
33 #include <rte_common.h>
34 #include <rte_config.h>
35 #include <rte_cryptodev_pmd.h>
36 #include <rte_cryptodev_vdev.h>
37 #include <rte_vdev.h>
38 #include <rte_malloc.h>
39 
40 #include "null_crypto_pmd_private.h"
41 
42 /** verify and set session parameters */
43 int
44 null_crypto_set_session_parameters(
45 		struct null_crypto_session *sess __rte_unused,
46 		const struct rte_crypto_sym_xform *xform)
47 {
48 	if (xform == NULL) {
49 		return -1;
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)
54 			return 0;
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)
60 			return 0;
61 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
62 			xform->next == NULL) {
63 		/* Cipher Only */
64 		if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
65 			return 0;
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)
71 			return 0;
72 	}
73 
74 	return -1;
75 }
76 
77 /** Process crypto operation for mbuf */
78 static int
79 process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op,
80 		struct null_crypto_session *sess __rte_unused)
81 {
82 	/* set status as successful by default */
83 	op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
84 
85 	/*
86 	 * if crypto session and operation are valid just enqueue the packet
87 	 * in the processed ring
88 	 */
89 	return rte_ring_enqueue(qp->processed_pkts, (void *)op);
90 }
91 
92 static struct null_crypto_session *
93 get_session(struct null_crypto_qp *qp, struct rte_crypto_sym_op *op)
94 {
95 	struct null_crypto_session *sess;
96 
97 	if (op->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
98 		if (unlikely(op->session == NULL ||
99 			     op->session->dev_type != RTE_CRYPTODEV_NULL_PMD))
100 			return NULL;
101 
102 		sess = (struct null_crypto_session *)op->session->_private;
103 	} else  {
104 		struct rte_cryptodev_session *c_sess = NULL;
105 
106 		if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
107 			return NULL;
108 
109 		sess = (struct null_crypto_session *)c_sess->_private;
110 
111 		if (null_crypto_set_session_parameters(sess, op->xform)	!= 0)
112 			return NULL;
113 	}
114 
115 	return sess;
116 }
117 
118 /** Enqueue burst */
119 static uint16_t
120 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
121 		uint16_t nb_ops)
122 {
123 	struct null_crypto_session *sess;
124 	struct null_crypto_qp *qp = queue_pair;
125 
126 	int i, retval;
127 
128 	for (i = 0; i < nb_ops; i++) {
129 		sess = get_session(qp, ops[i]->sym);
130 		if (unlikely(sess == NULL))
131 			goto enqueue_err;
132 
133 		retval = process_op(qp, ops[i], sess);
134 		if (unlikely(retval < 0))
135 			goto enqueue_err;
136 	}
137 
138 	qp->qp_stats.enqueued_count += i;
139 	return i;
140 
141 enqueue_err:
142 	if (ops[i])
143 		ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
144 
145 	qp->qp_stats.enqueue_err_count++;
146 	return i;
147 }
148 
149 /** Dequeue burst */
150 static uint16_t
151 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
152 		uint16_t nb_ops)
153 {
154 	struct null_crypto_qp *qp = queue_pair;
155 
156 	unsigned nb_dequeued;
157 
158 	nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
159 			(void **)ops, nb_ops, NULL);
160 	qp->qp_stats.dequeued_count += nb_dequeued;
161 
162 	return nb_dequeued;
163 }
164 
165 static int cryptodev_null_remove(const char *name);
166 
167 /** Create crypto device */
168 static int
169 cryptodev_null_create(const char *name,
170 		struct rte_vdev_device *vdev,
171 		struct rte_crypto_vdev_init_params *init_params)
172 {
173 	struct rte_cryptodev *dev;
174 	struct null_crypto_private *internals;
175 
176 	if (init_params->name[0] == '\0')
177 		snprintf(init_params->name, sizeof(init_params->name),
178 				"%s", name);
179 
180 	dev = rte_cryptodev_vdev_pmd_init(init_params->name,
181 			sizeof(struct null_crypto_private),
182 			init_params->socket_id,
183 			vdev);
184 	if (dev == NULL) {
185 		NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
186 		goto init_error;
187 	}
188 
189 	dev->dev_type = RTE_CRYPTODEV_NULL_PMD;
190 	dev->dev_ops = null_crypto_pmd_ops;
191 
192 	/* register rx/tx burst functions for data path */
193 	dev->dequeue_burst = null_crypto_pmd_dequeue_burst;
194 	dev->enqueue_burst = null_crypto_pmd_enqueue_burst;
195 
196 	dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
197 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
198 			RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
199 
200 	internals = dev->data->dev_private;
201 
202 	internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
203 	internals->max_nb_sessions = init_params->max_nb_sessions;
204 
205 	return 0;
206 
207 init_error:
208 	NULL_CRYPTO_LOG_ERR("driver %s: cryptodev_null_create failed",
209 			init_params->name);
210 	cryptodev_null_remove(init_params->name);
211 
212 	return -EFAULT;
213 }
214 
215 /** Initialise null crypto device */
216 static int
217 cryptodev_null_probe(struct rte_vdev_device *dev)
218 {
219 	struct rte_crypto_vdev_init_params init_params = {
220 		RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
221 		RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
222 		rte_socket_id(),
223 		{0}
224 	};
225 	const char *name;
226 
227 	name = rte_vdev_device_name(dev);
228 	if (name == NULL)
229 		return -EINVAL;
230 
231 	RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n",
232 		name, init_params.socket_id);
233 	if (init_params.name[0] != '\0')
234 		RTE_LOG(INFO, PMD, "  User defined name = %s\n",
235 			init_params.name);
236 	RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
237 			init_params.max_nb_queue_pairs);
238 	RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
239 			init_params.max_nb_sessions);
240 
241 	return cryptodev_null_create(name, dev, &init_params);
242 }
243 
244 /** Uninitialise null crypto device */
245 static int
246 cryptodev_null_remove(const char *name)
247 {
248 	if (name == NULL)
249 		return -EINVAL;
250 
251 	RTE_LOG(INFO, PMD, "Closing null crypto device %s on numa socket %u\n",
252 			name, rte_socket_id());
253 
254 	return 0;
255 }
256 
257 static int
258 cryptodev_null_remove_dev(struct rte_vdev_device *dev)
259 {
260 	return cryptodev_null_remove(rte_vdev_device_name(dev));
261 }
262 
263 static struct rte_vdev_driver cryptodev_null_pmd_drv = {
264 	.probe = cryptodev_null_probe,
265 	.remove = cryptodev_null_remove_dev,
266 };
267 
268 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd_drv);
269 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd);
270 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD,
271 	"max_nb_queue_pairs=<int> "
272 	"max_nb_sessions=<int> "
273 	"socket_id=<int>");
274