xref: /dpdk/drivers/crypto/null/null_crypto_pmd.c (revision 4e30ead5e7ca886535e2b30632b2948d2aac1681)
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_vdev.h>
37 #include <rte_malloc.h>
38 
39 #include "null_crypto_pmd_private.h"
40 
41 /** verify and set session parameters */
42 int
43 null_crypto_set_session_parameters(
44 		struct null_crypto_session *sess __rte_unused,
45 		const struct rte_crypto_sym_xform *xform)
46 {
47 	if (xform == NULL) {
48 		return -1;
49 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
50 			xform->next == NULL) {
51 		/* Authentication Only */
52 		if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
53 			return 0;
54 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
55 			xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
56 		/* Authentication then Cipher */
57 		if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
58 			xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
59 			return 0;
60 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
61 			xform->next == NULL) {
62 		/* Cipher Only */
63 		if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
64 			return 0;
65 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
66 			xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
67 		/* Cipher then Authentication */
68 		if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL &&
69 			xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
70 			return 0;
71 	}
72 
73 	return -1;
74 }
75 
76 /** Process crypto operation for mbuf */
77 static int
78 process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op,
79 		struct null_crypto_session *sess __rte_unused)
80 {
81 	/* set status as successful by default */
82 	op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
83 
84 	/*
85 	 * if crypto session and operation are valid just enqueue the packet
86 	 * in the processed ring
87 	 */
88 	return rte_ring_enqueue(qp->processed_pkts, (void *)op);
89 }
90 
91 static struct null_crypto_session *
92 get_session(struct null_crypto_qp *qp, struct rte_crypto_sym_op *op)
93 {
94 	struct null_crypto_session *sess;
95 
96 	if (op->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
97 		if (unlikely(op->session == NULL ||
98 			     op->session->dev_type != RTE_CRYPTODEV_NULL_PMD))
99 			return NULL;
100 
101 		sess = (struct null_crypto_session *)op->session->_private;
102 	} else  {
103 		struct rte_cryptodev_session *c_sess = NULL;
104 
105 		if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
106 			return NULL;
107 
108 		sess = (struct null_crypto_session *)c_sess->_private;
109 
110 		if (null_crypto_set_session_parameters(sess, op->xform)	!= 0)
111 			return NULL;
112 	}
113 
114 	return sess;
115 }
116 
117 /** Enqueue burst */
118 static uint16_t
119 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
120 		uint16_t nb_ops)
121 {
122 	struct null_crypto_session *sess;
123 	struct null_crypto_qp *qp = queue_pair;
124 
125 	int i, retval;
126 
127 	for (i = 0; i < nb_ops; i++) {
128 		sess = get_session(qp, ops[i]->sym);
129 		if (unlikely(sess == NULL))
130 			goto enqueue_err;
131 
132 		retval = process_op(qp, ops[i], sess);
133 		if (unlikely(retval < 0))
134 			goto enqueue_err;
135 	}
136 
137 	qp->qp_stats.enqueued_count += i;
138 	return i;
139 
140 enqueue_err:
141 	if (ops[i])
142 		ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
143 
144 	qp->qp_stats.enqueue_err_count++;
145 	return i;
146 }
147 
148 /** Dequeue burst */
149 static uint16_t
150 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
151 		uint16_t nb_ops)
152 {
153 	struct null_crypto_qp *qp = queue_pair;
154 
155 	unsigned nb_dequeued;
156 
157 	nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
158 			(void **)ops, nb_ops, NULL);
159 	qp->qp_stats.dequeued_count += nb_dequeued;
160 
161 	return nb_dequeued;
162 }
163 
164 static int cryptodev_null_remove(const char *name);
165 
166 /** Create crypto device */
167 static int
168 cryptodev_null_create(struct rte_crypto_vdev_init_params *init_params)
169 {
170 	struct rte_cryptodev *dev;
171 	struct null_crypto_private *internals;
172 
173 	if (init_params->name[0] == '\0') {
174 		int ret = rte_cryptodev_pmd_create_dev_name(
175 				init_params->name,
176 				RTE_STR(CRYPTODEV_NAME_NULL_PMD));
177 
178 		if (ret < 0) {
179 			NULL_CRYPTO_LOG_ERR("failed to create unique "
180 					"name");
181 			return ret;
182 		}
183 	}
184 
185 	dev = rte_cryptodev_pmd_virtual_dev_init(init_params->name,
186 			sizeof(struct null_crypto_private),
187 			init_params->socket_id);
188 	if (dev == NULL) {
189 		NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
190 		goto init_error;
191 	}
192 
193 	dev->dev_type = RTE_CRYPTODEV_NULL_PMD;
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 
230 	rte_cryptodev_parse_vdev_init_params(&init_params,
231 		rte_vdev_device_args(dev));
232 
233 	RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n",
234 		rte_vdev_device_name(dev), init_params.socket_id);
235 	if (init_params.name[0] != '\0')
236 		RTE_LOG(INFO, PMD, "  User defined name = %s\n",
237 			init_params.name);
238 	RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
239 			init_params.max_nb_queue_pairs);
240 	RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
241 			init_params.max_nb_sessions);
242 
243 	return cryptodev_null_create(&init_params);
244 }
245 
246 /** Uninitialise null crypto device */
247 static int
248 cryptodev_null_remove(const char *name)
249 {
250 	if (name == NULL)
251 		return -EINVAL;
252 
253 	RTE_LOG(INFO, PMD, "Closing null crypto device %s on numa socket %u\n",
254 			name, rte_socket_id());
255 
256 	return 0;
257 }
258 
259 static int
260 cryptodev_null_remove_dev(struct rte_vdev_device *dev)
261 {
262 	return cryptodev_null_remove(rte_vdev_device_name(dev));
263 }
264 
265 static struct rte_vdev_driver cryptodev_null_pmd_drv = {
266 	.probe = cryptodev_null_probe,
267 	.remove = cryptodev_null_remove_dev,
268 };
269 
270 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd_drv);
271 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd);
272 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD,
273 	"max_nb_queue_pairs=<int> "
274 	"max_nb_sessions=<int> "
275 	"socket_id=<int>");
276