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