1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_cryptodev_pmd.h>
11 #include "null_crypto_pmd_private.h"
13 static const struct rte_cryptodev_capabilities null_crypto_pmd_capabilities[] = {
15 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
17 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
19 .algo = RTE_CRYPTO_AUTH_NULL,
36 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
38 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
40 .algo = RTE_CRYPTO_CIPHER_NULL,
51 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
54 /** Configure device */
56 null_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
57 __rte_unused struct rte_cryptodev_config *config)
64 null_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
71 null_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
77 null_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
82 /** Get device statistics */
84 null_crypto_pmd_stats_get(struct rte_cryptodev *dev,
85 struct rte_cryptodev_stats *stats)
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];
92 stats->enqueued_count += qp->qp_stats.enqueued_count;
93 stats->dequeued_count += qp->qp_stats.dequeued_count;
95 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
96 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
100 /** Reset device statistics */
102 null_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
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];
109 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
114 /** Get device info */
116 null_crypto_pmd_info_get(struct rte_cryptodev *dev,
117 struct rte_cryptodev_info *dev_info)
119 struct null_crypto_private *internals = dev->data->dev_private;
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 /* No limit of number of sessions */
125 dev_info->sym.max_nb_sessions = 0;
126 dev_info->feature_flags = dev->feature_flags;
127 dev_info->capabilities = null_crypto_pmd_capabilities;
131 /** Release queue pair */
133 null_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
135 if (dev->data->queue_pairs[qp_id] != NULL) {
136 struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id];
138 if (qp->processed_pkts)
139 rte_ring_free(qp->processed_pkts);
141 rte_free(dev->data->queue_pairs[qp_id]);
142 dev->data->queue_pairs[qp_id] = NULL;
147 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
149 null_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
150 struct null_crypto_qp *qp)
152 unsigned n = snprintf(qp->name, sizeof(qp->name),
153 "null_crypto_pmd_%u_qp_%u",
154 dev->data->dev_id, qp->id);
156 if (n >= sizeof(qp->name))
162 /** Create a ring to place process packets on */
163 static struct rte_ring *
164 null_crypto_pmd_qp_create_processed_pkts_ring(struct null_crypto_qp *qp,
165 unsigned ring_size, int socket_id)
169 r = rte_ring_lookup(qp->name);
171 if (rte_ring_get_size(r) >= ring_size) {
173 "Reusing existing ring %s for "
174 " processed packets", qp->name);
179 "Unable to reuse existing ring %s for "
180 " processed packets", qp->name);
184 return rte_ring_create(qp->name, ring_size, socket_id,
185 RING_F_SP_ENQ | RING_F_SC_DEQ);
188 /** Setup a queue pair */
190 null_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
191 const struct rte_cryptodev_qp_conf *qp_conf,
194 struct null_crypto_private *internals = dev->data->dev_private;
195 struct null_crypto_qp *qp;
198 if (qp_id >= internals->max_nb_qpairs) {
199 NULL_LOG(ERR, "Invalid qp_id %u, greater than maximum "
200 "number of queue pairs supported (%u).",
201 qp_id, internals->max_nb_qpairs);
205 /* Free memory prior to re-allocation if needed. */
206 if (dev->data->queue_pairs[qp_id] != NULL)
207 null_crypto_pmd_qp_release(dev, qp_id);
209 /* Allocate the queue pair data structure. */
210 qp = rte_zmalloc_socket("Null Crypto PMD Queue Pair", sizeof(*qp),
211 RTE_CACHE_LINE_SIZE, socket_id);
213 NULL_LOG(ERR, "Failed to allocate queue pair memory");
218 dev->data->queue_pairs[qp_id] = qp;
220 retval = null_crypto_pmd_qp_set_unique_name(dev, qp);
222 NULL_LOG(ERR, "Failed to create unique name for null "
225 goto qp_setup_cleanup;
228 qp->processed_pkts = null_crypto_pmd_qp_create_processed_pkts_ring(qp,
229 qp_conf->nb_descriptors, socket_id);
230 if (qp->processed_pkts == NULL) {
231 NULL_LOG(ERR, "Failed to create unique name for null "
233 goto qp_setup_cleanup;
236 qp->sess_mp = qp_conf->mp_session;
237 qp->sess_mp_priv = qp_conf->mp_session_private;
239 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
250 /** Return the number of allocated queue pairs */
252 null_crypto_pmd_qp_count(struct rte_cryptodev *dev)
254 return dev->data->nb_queue_pairs;
257 /** Returns the size of the NULL crypto session structure */
259 null_crypto_pmd_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
261 return sizeof(struct null_crypto_session);
264 /** Configure a null crypto session from a crypto xform chain */
266 null_crypto_pmd_sym_session_configure(struct rte_cryptodev *dev __rte_unused,
267 struct rte_crypto_sym_xform *xform,
268 struct rte_cryptodev_sym_session *sess,
269 struct rte_mempool *mp)
271 void *sess_private_data;
274 if (unlikely(sess == NULL)) {
275 NULL_LOG(ERR, "invalid session struct");
279 if (rte_mempool_get(mp, &sess_private_data)) {
281 "Couldn't get object from session mempool");
285 ret = null_crypto_set_session_parameters(sess_private_data, xform);
287 NULL_LOG(ERR, "failed configure session parameters");
289 /* Return session to mempool */
290 rte_mempool_put(mp, sess_private_data);
294 set_sym_session_private_data(sess, dev->driver_id,
300 /** Clear the memory of session so it doesn't leave key material behind */
302 null_crypto_pmd_sym_session_clear(struct rte_cryptodev *dev,
303 struct rte_cryptodev_sym_session *sess)
305 uint8_t index = dev->driver_id;
306 void *sess_priv = get_sym_session_private_data(sess, index);
308 /* Zero out the whole structure */
310 memset(sess_priv, 0, sizeof(struct null_crypto_session));
311 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
312 set_sym_session_private_data(sess, index, NULL);
313 rte_mempool_put(sess_mp, sess_priv);
317 static struct rte_cryptodev_ops pmd_ops = {
318 .dev_configure = null_crypto_pmd_config,
319 .dev_start = null_crypto_pmd_start,
320 .dev_stop = null_crypto_pmd_stop,
321 .dev_close = null_crypto_pmd_close,
323 .stats_get = null_crypto_pmd_stats_get,
324 .stats_reset = null_crypto_pmd_stats_reset,
326 .dev_infos_get = null_crypto_pmd_info_get,
328 .queue_pair_setup = null_crypto_pmd_qp_setup,
329 .queue_pair_release = null_crypto_pmd_qp_release,
330 .queue_pair_count = null_crypto_pmd_qp_count,
332 .sym_session_get_size = null_crypto_pmd_sym_session_get_size,
333 .sym_session_configure = null_crypto_pmd_sym_session_configure,
334 .sym_session_clear = null_crypto_pmd_sym_session_clear
337 struct rte_cryptodev_ops *null_crypto_pmd_ops = &pmd_ops;