xref: /dpdk/drivers/crypto/null/null_crypto_pmd_ops.c (revision 94b0ad8e0aa556230183f4c4d06b68bfd145dce3)
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 <string.h>
34 
35 #include <rte_common.h>
36 #include <rte_malloc.h>
37 #include <rte_cryptodev_pmd.h>
38 
39 #include "null_crypto_pmd_private.h"
40 
41 /** Configure device */
42 static int
43 null_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev)
44 {
45 	return 0;
46 }
47 
48 /** Start device */
49 static int
50 null_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
51 {
52 	return 0;
53 }
54 
55 /** Stop device */
56 static void
57 null_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
58 {
59 }
60 
61 /** Close device */
62 static int
63 null_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
64 {
65 	return 0;
66 }
67 
68 /** Get device statistics */
69 static void
70 null_crypto_pmd_stats_get(struct rte_cryptodev *dev,
71 		struct rte_cryptodev_stats *stats)
72 {
73 	int qp_id;
74 
75 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
76 		struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
77 
78 		stats->enqueued_count += qp->qp_stats.enqueued_count;
79 		stats->dequeued_count += qp->qp_stats.dequeued_count;
80 
81 		stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
82 		stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
83 	}
84 }
85 
86 /** Reset device statistics */
87 static void
88 null_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
89 {
90 	int qp_id;
91 
92 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
93 		struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
94 
95 		memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
96 	}
97 }
98 
99 
100 /** Get device info */
101 static void
102 null_crypto_pmd_info_get(struct rte_cryptodev *dev,
103 		struct rte_cryptodev_info *dev_info)
104 {
105 	struct null_crypto_private *internals = dev->data->dev_private;
106 
107 	if (dev_info != NULL) {
108 		dev_info->dev_type = dev->dev_type;
109 		dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
110 		dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
111 	}
112 }
113 
114 /** Release queue pair */
115 static int
116 null_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
117 {
118 	if (dev->data->queue_pairs[qp_id] != NULL) {
119 		rte_free(dev->data->queue_pairs[qp_id]);
120 		dev->data->queue_pairs[qp_id] = NULL;
121 	}
122 	return 0;
123 }
124 
125 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
126 static int
127 null_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
128 		struct null_crypto_qp *qp)
129 {
130 	unsigned n = snprintf(qp->name, sizeof(qp->name),
131 			"null_crypto_pmd_%u_qp_%u",
132 			dev->data->dev_id, qp->id);
133 
134 	if (n > sizeof(qp->name))
135 		return -1;
136 
137 	return 0;
138 }
139 
140 /** Create a ring to place process packets on */
141 static struct rte_ring *
142 null_crypto_pmd_qp_create_processed_pkts_ring(struct null_crypto_qp *qp,
143 		unsigned ring_size, int socket_id)
144 {
145 	struct rte_ring *r;
146 
147 	r = rte_ring_lookup(qp->name);
148 	if (r) {
149 		if (r->prod.size >= ring_size) {
150 			NULL_CRYPTO_LOG_INFO(
151 				"Reusing existing ring %s for processed packets",
152 				qp->name);
153 			return r;
154 		}
155 
156 		NULL_CRYPTO_LOG_INFO(
157 			"Unable to reuse existing ring %s for processed packets",
158 			 qp->name);
159 		return NULL;
160 	}
161 
162 	return rte_ring_create(qp->name, ring_size, socket_id,
163 			RING_F_SP_ENQ | RING_F_SC_DEQ);
164 }
165 
166 /** Setup a queue pair */
167 static int
168 null_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
169 		const struct rte_cryptodev_qp_conf *qp_conf,
170 		 int socket_id)
171 {
172 	struct null_crypto_private *internals = dev->data->dev_private;
173 	struct null_crypto_qp *qp;
174 	int retval;
175 
176 	if (qp_id >= internals->max_nb_qpairs) {
177 		NULL_CRYPTO_LOG_ERR("Invalid qp_id %u, greater than maximum "
178 				"number of queue pairs supported (%u).",
179 				qp_id, internals->max_nb_qpairs);
180 		return (-EINVAL);
181 	}
182 
183 	/* Free memory prior to re-allocation if needed. */
184 	if (dev->data->queue_pairs[qp_id] != NULL)
185 		null_crypto_pmd_qp_release(dev, qp_id);
186 
187 	/* Allocate the queue pair data structure. */
188 	qp = rte_zmalloc_socket("Null Crypto PMD Queue Pair", sizeof(*qp),
189 					RTE_CACHE_LINE_SIZE, socket_id);
190 	if (qp == NULL) {
191 		NULL_CRYPTO_LOG_ERR("Failed to allocate queue pair memory");
192 		return (-ENOMEM);
193 	}
194 
195 	qp->id = qp_id;
196 	dev->data->queue_pairs[qp_id] = qp;
197 
198 	retval = null_crypto_pmd_qp_set_unique_name(dev, qp);
199 	if (retval) {
200 		NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
201 				"crypto device");
202 		goto qp_setup_cleanup;
203 	}
204 
205 	qp->processed_pkts = null_crypto_pmd_qp_create_processed_pkts_ring(qp,
206 			qp_conf->nb_descriptors, socket_id);
207 	if (qp->processed_pkts == NULL) {
208 		NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
209 				"crypto device");
210 		goto qp_setup_cleanup;
211 	}
212 
213 	qp->sess_mp = dev->data->session_pool;
214 
215 	memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
216 
217 	return 0;
218 
219 qp_setup_cleanup:
220 	if (qp)
221 		rte_free(qp);
222 
223 	return -1;
224 }
225 
226 /** Start queue pair */
227 static int
228 null_crypto_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
229 		__rte_unused uint16_t queue_pair_id)
230 {
231 	return -ENOTSUP;
232 }
233 
234 /** Stop queue pair */
235 static int
236 null_crypto_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
237 		__rte_unused uint16_t queue_pair_id)
238 {
239 	return -ENOTSUP;
240 }
241 
242 /** Return the number of allocated queue pairs */
243 static uint32_t
244 null_crypto_pmd_qp_count(struct rte_cryptodev *dev)
245 {
246 	return dev->data->nb_queue_pairs;
247 }
248 
249 /** Returns the size of the NULL crypto session structure */
250 static unsigned
251 null_crypto_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
252 {
253 	return sizeof(struct null_crypto_session);
254 }
255 
256 /** Configure a null crypto session from a crypto xform chain */
257 static void *
258 null_crypto_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
259 		struct rte_crypto_sym_xform *xform, void *sess)
260 {
261 	int retval;
262 
263 	if (unlikely(sess == NULL)) {
264 		NULL_CRYPTO_LOG_ERR("invalid session struct");
265 		return NULL;
266 	}
267 	retval = null_crypto_set_session_parameters(
268 			(struct null_crypto_session *)sess, xform);
269 	if (retval != 0) {
270 		NULL_CRYPTO_LOG_ERR("failed configure session parameters");
271 		return NULL;
272 	}
273 
274 	return sess;
275 }
276 
277 /** Clear the memory of session so it doesn't leave key material behind */
278 static void
279 null_crypto_pmd_session_clear(struct rte_cryptodev *dev __rte_unused,
280 		void *sess)
281 {
282 	if (sess)
283 		memset(sess, 0, sizeof(struct null_crypto_session));
284 }
285 
286 struct rte_cryptodev_ops pmd_ops = {
287 		.dev_configure		= null_crypto_pmd_config,
288 		.dev_start		= null_crypto_pmd_start,
289 		.dev_stop		= null_crypto_pmd_stop,
290 		.dev_close		= null_crypto_pmd_close,
291 
292 		.stats_get		= null_crypto_pmd_stats_get,
293 		.stats_reset		= null_crypto_pmd_stats_reset,
294 
295 		.dev_infos_get		= null_crypto_pmd_info_get,
296 
297 		.queue_pair_setup	= null_crypto_pmd_qp_setup,
298 		.queue_pair_release	= null_crypto_pmd_qp_release,
299 		.queue_pair_start	= null_crypto_pmd_qp_start,
300 		.queue_pair_stop	= null_crypto_pmd_qp_stop,
301 		.queue_pair_count	= null_crypto_pmd_qp_count,
302 
303 		.session_get_size	= null_crypto_pmd_session_get_size,
304 		.session_configure	= null_crypto_pmd_session_configure,
305 		.session_clear		= null_crypto_pmd_session_clear
306 };
307 
308 struct rte_cryptodev_ops *null_crypto_pmd_ops = &pmd_ops;
309