xref: /dpdk/drivers/crypto/null/null_crypto_pmd_ops.c (revision 8b9bd0efe0b6920a08e28eebacf2bb916bdf5653)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4 
5 #include <string.h>
6 
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_cryptodev_pmd.h>
10 
11 #include "null_crypto_pmd_private.h"
12 
13 static const struct rte_cryptodev_capabilities null_crypto_pmd_capabilities[] = {
14 	{	/* NULL (AUTH) */
15 		.op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
16 		{.sym = {
17 			.xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
18 			{.auth = {
19 				.algo = RTE_CRYPTO_AUTH_NULL,
20 				.block_size = 1,
21 				.key_size = {
22 					.min = 0,
23 					.max = 0,
24 					.increment = 0
25 				},
26 				.digest_size = {
27 					.min = 0,
28 					.max = 0,
29 					.increment = 0
30 				},
31 				.iv_size = { 0 }
32 			}, },
33 		}, },
34 	},
35 	{	/* NULL (CIPHER) */
36 		.op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
37 		{.sym = {
38 			.xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
39 			{.cipher = {
40 				.algo = RTE_CRYPTO_CIPHER_NULL,
41 				.block_size = 1,
42 				.key_size = {
43 					.min = 0,
44 					.max = 0,
45 					.increment = 0
46 				},
47 				.iv_size = { 0 }
48 			}, },
49 		}, }
50 	},
51 	RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
52 };
53 
54 /** Configure device */
55 static int
56 null_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
57 		__rte_unused struct rte_cryptodev_config *config)
58 {
59 	return 0;
60 }
61 
62 /** Start device */
63 static int
64 null_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
65 {
66 	return 0;
67 }
68 
69 /** Stop device */
70 static void
71 null_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
72 {
73 }
74 
75 /** Close device */
76 static int
77 null_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
78 {
79 	return 0;
80 }
81 
82 /** Get device statistics */
83 static void
84 null_crypto_pmd_stats_get(struct rte_cryptodev *dev,
85 		struct rte_cryptodev_stats *stats)
86 {
87 	int qp_id;
88 
89 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
90 		struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
91 
92 		stats->enqueued_count += qp->qp_stats.enqueued_count;
93 		stats->dequeued_count += qp->qp_stats.dequeued_count;
94 
95 		stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
96 		stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
97 	}
98 }
99 
100 /** Reset device statistics */
101 static void
102 null_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
103 {
104 	int qp_id;
105 
106 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
107 		struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
108 
109 		memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
110 	}
111 }
112 
113 
114 /** Get device info */
115 static void
116 null_crypto_pmd_info_get(struct rte_cryptodev *dev,
117 		struct rte_cryptodev_info *dev_info)
118 {
119 	struct null_crypto_private *internals = dev->data->dev_private;
120 
121 	if (dev_info != NULL) {
122 		dev_info->driver_id = dev->driver_id;
123 		dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
124 		dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
125 		dev_info->feature_flags = dev->feature_flags;
126 		dev_info->capabilities = null_crypto_pmd_capabilities;
127 	}
128 }
129 
130 /** Release queue pair */
131 static int
132 null_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
133 {
134 	if (dev->data->queue_pairs[qp_id] != NULL) {
135 		rte_free(dev->data->queue_pairs[qp_id]);
136 		dev->data->queue_pairs[qp_id] = NULL;
137 	}
138 	return 0;
139 }
140 
141 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
142 static int
143 null_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
144 		struct null_crypto_qp *qp)
145 {
146 	unsigned n = snprintf(qp->name, sizeof(qp->name),
147 			"null_crypto_pmd_%u_qp_%u",
148 			dev->data->dev_id, qp->id);
149 
150 	if (n >= sizeof(qp->name))
151 		return -1;
152 
153 	return 0;
154 }
155 
156 /** Create a ring to place process packets on */
157 static struct rte_ring *
158 null_crypto_pmd_qp_create_processed_pkts_ring(struct null_crypto_qp *qp,
159 		unsigned ring_size, int socket_id)
160 {
161 	struct rte_ring *r;
162 
163 	r = rte_ring_lookup(qp->name);
164 	if (r) {
165 		if (rte_ring_get_size(r) >= ring_size) {
166 			NULL_CRYPTO_LOG_INFO(
167 				"Reusing existing ring %s for processed packets",
168 				qp->name);
169 			return r;
170 		}
171 
172 		NULL_CRYPTO_LOG_INFO(
173 			"Unable to reuse existing ring %s for processed packets",
174 			 qp->name);
175 		return NULL;
176 	}
177 
178 	return rte_ring_create(qp->name, ring_size, socket_id,
179 			RING_F_SP_ENQ | RING_F_SC_DEQ);
180 }
181 
182 /** Setup a queue pair */
183 static int
184 null_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
185 		const struct rte_cryptodev_qp_conf *qp_conf,
186 		int socket_id, struct rte_mempool *session_pool)
187 {
188 	struct null_crypto_private *internals = dev->data->dev_private;
189 	struct null_crypto_qp *qp;
190 	int retval;
191 
192 	if (qp_id >= internals->max_nb_qpairs) {
193 		NULL_CRYPTO_LOG_ERR("Invalid qp_id %u, greater than maximum "
194 				"number of queue pairs supported (%u).",
195 				qp_id, internals->max_nb_qpairs);
196 		return (-EINVAL);
197 	}
198 
199 	/* Free memory prior to re-allocation if needed. */
200 	if (dev->data->queue_pairs[qp_id] != NULL)
201 		null_crypto_pmd_qp_release(dev, qp_id);
202 
203 	/* Allocate the queue pair data structure. */
204 	qp = rte_zmalloc_socket("Null Crypto PMD Queue Pair", sizeof(*qp),
205 					RTE_CACHE_LINE_SIZE, socket_id);
206 	if (qp == NULL) {
207 		NULL_CRYPTO_LOG_ERR("Failed to allocate queue pair memory");
208 		return (-ENOMEM);
209 	}
210 
211 	qp->id = qp_id;
212 	dev->data->queue_pairs[qp_id] = qp;
213 
214 	retval = null_crypto_pmd_qp_set_unique_name(dev, qp);
215 	if (retval) {
216 		NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
217 				"crypto device");
218 		goto qp_setup_cleanup;
219 	}
220 
221 	qp->processed_pkts = null_crypto_pmd_qp_create_processed_pkts_ring(qp,
222 			qp_conf->nb_descriptors, socket_id);
223 	if (qp->processed_pkts == NULL) {
224 		NULL_CRYPTO_LOG_ERR("Failed to create unique name for null "
225 				"crypto device");
226 		goto qp_setup_cleanup;
227 	}
228 
229 	qp->sess_mp = session_pool;
230 
231 	memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
232 
233 	return 0;
234 
235 qp_setup_cleanup:
236 	if (qp)
237 		rte_free(qp);
238 
239 	return -1;
240 }
241 
242 /** Start queue pair */
243 static int
244 null_crypto_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
245 		__rte_unused uint16_t queue_pair_id)
246 {
247 	return -ENOTSUP;
248 }
249 
250 /** Stop queue pair */
251 static int
252 null_crypto_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
253 		__rte_unused uint16_t queue_pair_id)
254 {
255 	return -ENOTSUP;
256 }
257 
258 /** Return the number of allocated queue pairs */
259 static uint32_t
260 null_crypto_pmd_qp_count(struct rte_cryptodev *dev)
261 {
262 	return dev->data->nb_queue_pairs;
263 }
264 
265 /** Returns the size of the NULL crypto session structure */
266 static unsigned
267 null_crypto_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
268 {
269 	return sizeof(struct null_crypto_session);
270 }
271 
272 /** Configure a null crypto session from a crypto xform chain */
273 static int
274 null_crypto_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
275 		struct rte_crypto_sym_xform *xform,
276 		struct rte_cryptodev_sym_session *sess,
277 		struct rte_mempool *mp)
278 {
279 	void *sess_private_data;
280 	int ret;
281 
282 	if (unlikely(sess == NULL)) {
283 		NULL_CRYPTO_LOG_ERR("invalid session struct");
284 		return -EINVAL;
285 	}
286 
287 	if (rte_mempool_get(mp, &sess_private_data)) {
288 		CDEV_LOG_ERR(
289 			"Couldn't get object from session mempool");
290 		return -ENOMEM;
291 	}
292 
293 	ret = null_crypto_set_session_parameters(sess_private_data, xform);
294 	if (ret != 0) {
295 		NULL_CRYPTO_LOG_ERR("failed configure session parameters");
296 
297 		/* Return session to mempool */
298 		rte_mempool_put(mp, sess_private_data);
299 		return ret;
300 	}
301 
302 	set_session_private_data(sess, dev->driver_id,
303 		sess_private_data);
304 
305 	return 0;
306 }
307 
308 /** Clear the memory of session so it doesn't leave key material behind */
309 static void
310 null_crypto_pmd_session_clear(struct rte_cryptodev *dev,
311 		struct rte_cryptodev_sym_session *sess)
312 {
313 	uint8_t index = dev->driver_id;
314 	void *sess_priv = get_session_private_data(sess, index);
315 
316 	/* Zero out the whole structure */
317 	if (sess_priv) {
318 		memset(sess_priv, 0, sizeof(struct null_crypto_session));
319 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
320 		set_session_private_data(sess, index, NULL);
321 		rte_mempool_put(sess_mp, sess_priv);
322 	}
323 }
324 
325 struct rte_cryptodev_ops pmd_ops = {
326 		.dev_configure		= null_crypto_pmd_config,
327 		.dev_start		= null_crypto_pmd_start,
328 		.dev_stop		= null_crypto_pmd_stop,
329 		.dev_close		= null_crypto_pmd_close,
330 
331 		.stats_get		= null_crypto_pmd_stats_get,
332 		.stats_reset		= null_crypto_pmd_stats_reset,
333 
334 		.dev_infos_get		= null_crypto_pmd_info_get,
335 
336 		.queue_pair_setup	= null_crypto_pmd_qp_setup,
337 		.queue_pair_release	= null_crypto_pmd_qp_release,
338 		.queue_pair_start	= null_crypto_pmd_qp_start,
339 		.queue_pair_stop	= null_crypto_pmd_qp_stop,
340 		.queue_pair_count	= null_crypto_pmd_qp_count,
341 
342 		.session_get_size	= null_crypto_pmd_session_get_size,
343 		.session_configure	= null_crypto_pmd_session_configure,
344 		.session_clear		= null_crypto_pmd_session_clear
345 };
346 
347 struct rte_cryptodev_ops *null_crypto_pmd_ops = &pmd_ops;
348