1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
11 * RTE Cryptography Common Definitions
21 #include <rte_memory.h>
22 #include <rte_mempool.h>
23 #include <rte_common.h>
25 #include "rte_crypto_sym.h"
27 /** Crypto operation types */
28 enum rte_crypto_op_type {
29 RTE_CRYPTO_OP_TYPE_UNDEFINED,
30 /**< Undefined operation type */
31 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
32 /**< Symmetric operation */
35 /** Status of crypto operation */
36 enum rte_crypto_op_status {
37 RTE_CRYPTO_OP_STATUS_SUCCESS,
38 /**< Operation completed successfully */
39 RTE_CRYPTO_OP_STATUS_NOT_PROCESSED,
40 /**< Operation has not yet been processed by a crypto device */
41 RTE_CRYPTO_OP_STATUS_AUTH_FAILED,
42 /**< Authentication verification failed */
43 RTE_CRYPTO_OP_STATUS_INVALID_SESSION,
45 * Symmetric operation failed due to invalid session arguments, or if
46 * in session-less mode, failed to allocate private operation material.
48 RTE_CRYPTO_OP_STATUS_INVALID_ARGS,
49 /**< Operation failed due to invalid arguments in request */
50 RTE_CRYPTO_OP_STATUS_ERROR,
51 /**< Error handling operation */
55 * Crypto operation session type. This is used to specify whether a crypto
56 * operation has session structure attached for immutable parameters or if all
57 * operation information is included in the operation data structure.
59 enum rte_crypto_op_sess_type {
60 RTE_CRYPTO_OP_WITH_SESSION, /**< Session based crypto operation */
61 RTE_CRYPTO_OP_SESSIONLESS, /**< Session-less crypto operation */
62 RTE_CRYPTO_OP_SECURITY_SESSION /**< Security session crypto operation */
66 * Cryptographic Operation.
68 * This structure contains data relating to performing cryptographic
69 * operations. This operation structure is used to contain any operation which
70 * is supported by the cryptodev API, PMDs should check the type parameter to
71 * verify that the operation is a support function of the device. Crypto
72 * operations are enqueued and dequeued in crypto PMDs using the
73 * rte_cryptodev_enqueue_burst() / rte_cryptodev_dequeue_burst() .
75 struct rte_crypto_op {
77 /**< operation type */
80 * operation status - this is reset to
81 * RTE_CRYPTO_OP_STATUS_NOT_PROCESSED on allocation from mempool and
82 * will be set to RTE_CRYPTO_OP_STATUS_SUCCESS after crypto operation
83 * is successfully processed by a crypto PMD
86 /**< operation session type */
89 /**< Reserved bytes to fill 64 bits for future additions */
90 struct rte_mempool *mempool;
91 /**< crypto operation mempool which operation is allocated from */
94 /**< physical address of crypto operation */
98 struct rte_crypto_sym_op sym[0];
99 /**< Symmetric operation parameters */
100 }; /**< operation specific parameters */
104 * Reset the fields of a crypto operation to their default values.
106 * @param op The crypto operation to be reset.
107 * @param type The crypto operation type.
110 __rte_crypto_op_reset(struct rte_crypto_op *op, enum rte_crypto_op_type type)
113 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
114 op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
117 case RTE_CRYPTO_OP_TYPE_SYMMETRIC:
118 __rte_crypto_sym_op_reset(op->sym);
120 case RTE_CRYPTO_OP_TYPE_UNDEFINED:
127 * Private data structure belonging to a crypto symmetric operation pool.
129 struct rte_crypto_op_pool_private {
130 enum rte_crypto_op_type type;
131 /**< Crypto op pool type operation. */
133 /**< Size of private area in each crypto operation. */
138 * Returns the size of private data allocated with each rte_crypto_op object by
141 * @param mempool rte_crypto_op mempool
143 * @return private data size
145 static inline uint16_t
146 __rte_crypto_op_get_priv_data_size(struct rte_mempool *mempool)
148 struct rte_crypto_op_pool_private *priv =
149 (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
151 return priv->priv_size;
156 * Creates a crypto operation pool
158 * @param name pool name
159 * @param type crypto operation type, use
160 * RTE_CRYPTO_OP_TYPE_UNDEFINED for a pool which
161 * supports all operation types
162 * @param nb_elts number of elements in pool
163 * @param cache_size Number of elements to cache on lcore, see
164 * *rte_mempool_create* for further details about
166 * @param priv_size Size of private data to allocate with each
168 * @param socket_id Socket to allocate memory on
171 * - On success pointer to mempool
174 extern struct rte_mempool *
175 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
176 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
180 * Bulk allocate raw element from mempool and return as crypto operations
182 * @param mempool crypto operation mempool.
183 * @param type crypto operation type.
184 * @param ops Array to place allocated crypto operations
185 * @param nb_ops Number of crypto operations to allocate
188 * - On success returns number of ops allocated
191 __rte_crypto_op_raw_bulk_alloc(struct rte_mempool *mempool,
192 enum rte_crypto_op_type type,
193 struct rte_crypto_op **ops, uint16_t nb_ops)
195 struct rte_crypto_op_pool_private *priv;
197 priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
198 if (unlikely(priv->type != type &&
199 priv->type != RTE_CRYPTO_OP_TYPE_UNDEFINED))
202 if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
209 * Allocate a crypto operation from a mempool with default parameters set
211 * @param mempool crypto operation mempool
212 * @param type operation type to allocate
215 * - On success returns a valid rte_crypto_op structure
216 * - On failure returns NULL
218 static inline struct rte_crypto_op *
219 rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
221 struct rte_crypto_op *op = NULL;
224 retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
225 if (unlikely(retval != 1))
228 __rte_crypto_op_reset(op, type);
235 * Bulk allocate crypto operations from a mempool with default parameters set
237 * @param mempool crypto operation mempool
238 * @param type operation type to allocate
239 * @param ops Array to place allocated crypto operations
240 * @param nb_ops Number of crypto operations to allocate
243 * - nb_ops if the number of operations requested were allocated.
244 * - 0 if the requested number of ops are not available.
245 * None are allocated in this case.
248 static inline unsigned
249 rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
250 enum rte_crypto_op_type type,
251 struct rte_crypto_op **ops, uint16_t nb_ops)
255 if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
259 for (i = 0; i < nb_ops; i++)
260 __rte_crypto_op_reset(ops[i], type);
268 * Returns a pointer to the private data of a crypto operation if
269 * that operation has enough capacity for requested size.
271 * @param op crypto operation.
272 * @param size size of space requested in private data.
275 * - if sufficient space available returns pointer to start of private data
276 * - if insufficient space returns NULL
279 __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
283 if (likely(op->mempool != NULL)) {
284 priv_size = __rte_crypto_op_get_priv_data_size(op->mempool);
286 if (likely(priv_size >= size))
287 return (void *)((uint8_t *)(op + 1) +
288 sizeof(struct rte_crypto_sym_op));
295 * free crypto operation structure
296 * If operation has been allocate from a rte_mempool, then the operation will
297 * be returned to the mempool.
299 * @param op symmetric crypto operation
302 rte_crypto_op_free(struct rte_crypto_op *op)
304 if (op != NULL && op->mempool != NULL)
305 rte_mempool_put(op->mempool, op);
309 * Allocate a symmetric crypto operation in the private data of an mbuf.
311 * @param m mbuf which is associated with the crypto operation, the
312 * operation will be allocated in the private data of that
316 * - On success returns a pointer to the crypto operation.
317 * - On failure returns NULL.
319 static inline struct rte_crypto_op *
320 rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
322 if (unlikely(m == NULL))
326 * check that the mbuf's private data size is sufficient to contain a
329 if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
330 sizeof(struct rte_crypto_sym_op))))
333 /* private data starts immediately after the mbuf header in the mbuf. */
334 struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
336 __rte_crypto_op_reset(op, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
345 * Allocate space for symmetric crypto xforms in the private data space of the
346 * crypto operation. This also defaults the crypto xform type and configures
347 * the chaining of the xforms in the crypto operation
350 * - On success returns pointer to first crypto xform in crypto operations chain
351 * - On failure returns NULL
353 static inline struct rte_crypto_sym_xform *
354 rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
359 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
362 size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
364 priv_data = __rte_crypto_op_get_priv_data(op, size);
365 if (priv_data == NULL)
368 return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
374 * Attach a session to a crypto operation
376 * @param op crypto operation, must be of type symmetric
377 * @param sess cryptodev session
380 rte_crypto_op_attach_sym_session(struct rte_crypto_op *op,
381 struct rte_cryptodev_sym_session *sess)
383 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
386 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
388 return __rte_crypto_sym_op_attach_sym_session(op->sym, sess);
395 #endif /* _RTE_CRYPTO_H_ */