4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <rte_common.h>
36 #include <rte_malloc.h>
37 #include <rte_cryptodev_pmd.h>
39 #include "aesni_gcm_pmd_private.h"
41 static const struct rte_cryptodev_capabilities aesni_gcm_pmd_capabilities[] = {
42 { /* AES GMAC (AUTH) */
43 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
45 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
47 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
67 { /* AES GCM (AUTH) */
68 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
70 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
72 .algo = RTE_CRYPTO_AUTH_AES_GCM,
92 { /* AES GCM (CIPHER) */
93 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
95 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
97 .algo = RTE_CRYPTO_CIPHER_AES_GCM,
112 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
115 /** Configure device */
117 aesni_gcm_pmd_config(__rte_unused struct rte_cryptodev *dev)
124 aesni_gcm_pmd_start(__rte_unused struct rte_cryptodev *dev)
131 aesni_gcm_pmd_stop(__rte_unused struct rte_cryptodev *dev)
137 aesni_gcm_pmd_close(__rte_unused struct rte_cryptodev *dev)
143 /** Get device statistics */
145 aesni_gcm_pmd_stats_get(struct rte_cryptodev *dev,
146 struct rte_cryptodev_stats *stats)
150 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
151 struct aesni_gcm_qp *qp = dev->data->queue_pairs[qp_id];
153 stats->enqueued_count += qp->qp_stats.enqueued_count;
154 stats->dequeued_count += qp->qp_stats.dequeued_count;
156 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
157 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
161 /** Reset device statistics */
163 aesni_gcm_pmd_stats_reset(struct rte_cryptodev *dev)
167 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
168 struct aesni_gcm_qp *qp = dev->data->queue_pairs[qp_id];
170 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
175 /** Get device info */
177 aesni_gcm_pmd_info_get(struct rte_cryptodev *dev,
178 struct rte_cryptodev_info *dev_info)
180 struct aesni_gcm_private *internals = dev->data->dev_private;
182 if (dev_info != NULL) {
183 dev_info->dev_type = dev->dev_type;
184 dev_info->feature_flags = dev->feature_flags;
185 dev_info->capabilities = aesni_gcm_pmd_capabilities;
187 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
188 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
192 /** Release queue pair */
194 aesni_gcm_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
196 if (dev->data->queue_pairs[qp_id] != NULL) {
197 rte_free(dev->data->queue_pairs[qp_id]);
198 dev->data->queue_pairs[qp_id] = NULL;
203 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
205 aesni_gcm_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
206 struct aesni_gcm_qp *qp)
208 unsigned n = snprintf(qp->name, sizeof(qp->name),
209 "aesni_gcm_pmd_%u_qp_%u",
210 dev->data->dev_id, qp->id);
212 if (n > sizeof(qp->name))
218 /** Create a ring to place process packets on */
219 static struct rte_ring *
220 aesni_gcm_pmd_qp_create_processed_pkts_ring(struct aesni_gcm_qp *qp,
221 unsigned ring_size, int socket_id)
225 r = rte_ring_lookup(qp->name);
227 if (rte_ring_get_size(r) >= ring_size) {
228 GCM_LOG_INFO("Reusing existing ring %s for processed"
229 " packets", qp->name);
233 GCM_LOG_ERR("Unable to reuse existing ring %s for processed"
234 " packets", qp->name);
238 return rte_ring_create(qp->name, ring_size, socket_id,
239 RING_F_SP_ENQ | RING_F_SC_DEQ);
242 /** Setup a queue pair */
244 aesni_gcm_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
245 const struct rte_cryptodev_qp_conf *qp_conf,
248 struct aesni_gcm_qp *qp = NULL;
250 /* Free memory prior to re-allocation if needed. */
251 if (dev->data->queue_pairs[qp_id] != NULL)
252 aesni_gcm_pmd_qp_release(dev, qp_id);
254 /* Allocate the queue pair data structure. */
255 qp = rte_zmalloc_socket("AES-NI PMD Queue Pair", sizeof(*qp),
256 RTE_CACHE_LINE_SIZE, socket_id);
261 dev->data->queue_pairs[qp_id] = qp;
263 if (aesni_gcm_pmd_qp_set_unique_name(dev, qp))
264 goto qp_setup_cleanup;
266 qp->processed_pkts = aesni_gcm_pmd_qp_create_processed_pkts_ring(qp,
267 qp_conf->nb_descriptors, socket_id);
268 if (qp->processed_pkts == NULL)
269 goto qp_setup_cleanup;
271 qp->sess_mp = dev->data->session_pool;
273 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
284 /** Start queue pair */
286 aesni_gcm_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
287 __rte_unused uint16_t queue_pair_id)
292 /** Stop queue pair */
294 aesni_gcm_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
295 __rte_unused uint16_t queue_pair_id)
300 /** Return the number of allocated queue pairs */
302 aesni_gcm_pmd_qp_count(struct rte_cryptodev *dev)
304 return dev->data->nb_queue_pairs;
307 /** Returns the size of the aesni gcm session structure */
309 aesni_gcm_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
311 return sizeof(struct aesni_gcm_session);
314 /** Configure a aesni gcm session from a crypto xform chain */
316 aesni_gcm_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
317 struct rte_crypto_sym_xform *xform, void *sess)
319 if (unlikely(sess == NULL)) {
320 GCM_LOG_ERR("invalid session struct");
324 if (aesni_gcm_set_session_parameters(sess, xform) != 0) {
325 GCM_LOG_ERR("failed configure session parameters");
332 /** Clear the memory of session so it doesn't leave key material behind */
334 aesni_gcm_pmd_session_clear(struct rte_cryptodev *dev __rte_unused, void *sess)
337 memset(sess, 0, sizeof(struct aesni_gcm_session));
340 struct rte_cryptodev_ops aesni_gcm_pmd_ops = {
341 .dev_configure = aesni_gcm_pmd_config,
342 .dev_start = aesni_gcm_pmd_start,
343 .dev_stop = aesni_gcm_pmd_stop,
344 .dev_close = aesni_gcm_pmd_close,
346 .stats_get = aesni_gcm_pmd_stats_get,
347 .stats_reset = aesni_gcm_pmd_stats_reset,
349 .dev_infos_get = aesni_gcm_pmd_info_get,
351 .queue_pair_setup = aesni_gcm_pmd_qp_setup,
352 .queue_pair_release = aesni_gcm_pmd_qp_release,
353 .queue_pair_start = aesni_gcm_pmd_qp_start,
354 .queue_pair_stop = aesni_gcm_pmd_qp_stop,
355 .queue_pair_count = aesni_gcm_pmd_qp_count,
357 .session_get_size = aesni_gcm_pmd_session_get_size,
358 .session_configure = aesni_gcm_pmd_session_configure,
359 .session_clear = aesni_gcm_pmd_session_clear
362 struct rte_cryptodev_ops *rte_aesni_gcm_pmd_ops = &aesni_gcm_pmd_ops;