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 */
87 uint16_t private_data_offset;
88 /**< Offset to indicate start of private data (if any). The offset
89 * is counted from the start of the rte_crypto_op including IV.
90 * The private data may be used by the application to store
91 * information which should remain untouched in the library/driver
95 /**< Reserved bytes to fill 64 bits for future additions */
96 struct rte_mempool *mempool;
97 /**< crypto operation mempool which operation is allocated from */
100 /**< physical address of crypto operation */
104 struct rte_crypto_sym_op sym[0];
105 /**< Symmetric operation parameters */
106 }; /**< operation specific parameters */
110 * Reset the fields of a crypto operation to their default values.
112 * @param op The crypto operation to be reset.
113 * @param type The crypto operation type.
116 __rte_crypto_op_reset(struct rte_crypto_op *op, enum rte_crypto_op_type type)
119 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
120 op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
123 case RTE_CRYPTO_OP_TYPE_SYMMETRIC:
124 __rte_crypto_sym_op_reset(op->sym);
126 case RTE_CRYPTO_OP_TYPE_UNDEFINED:
133 * Private data structure belonging to a crypto symmetric operation pool.
135 struct rte_crypto_op_pool_private {
136 enum rte_crypto_op_type type;
137 /**< Crypto op pool type operation. */
139 /**< Size of private area in each crypto operation. */
144 * Returns the size of private data allocated with each rte_crypto_op object by
147 * @param mempool rte_crypto_op mempool
149 * @return private data size
151 static inline uint16_t
152 __rte_crypto_op_get_priv_data_size(struct rte_mempool *mempool)
154 struct rte_crypto_op_pool_private *priv =
155 (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
157 return priv->priv_size;
162 * Creates a crypto operation pool
164 * @param name pool name
165 * @param type crypto operation type, use
166 * RTE_CRYPTO_OP_TYPE_UNDEFINED for a pool which
167 * supports all operation types
168 * @param nb_elts number of elements in pool
169 * @param cache_size Number of elements to cache on lcore, see
170 * *rte_mempool_create* for further details about
172 * @param priv_size Size of private data to allocate with each
174 * @param socket_id Socket to allocate memory on
177 * - On success pointer to mempool
180 extern struct rte_mempool *
181 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
182 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
186 * Bulk allocate raw element from mempool and return as crypto operations
188 * @param mempool crypto operation mempool.
189 * @param type crypto operation type.
190 * @param ops Array to place allocated crypto operations
191 * @param nb_ops Number of crypto operations to allocate
194 * - On success returns number of ops allocated
197 __rte_crypto_op_raw_bulk_alloc(struct rte_mempool *mempool,
198 enum rte_crypto_op_type type,
199 struct rte_crypto_op **ops, uint16_t nb_ops)
201 struct rte_crypto_op_pool_private *priv;
203 priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
204 if (unlikely(priv->type != type &&
205 priv->type != RTE_CRYPTO_OP_TYPE_UNDEFINED))
208 if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
215 * Allocate a crypto operation from a mempool with default parameters set
217 * @param mempool crypto operation mempool
218 * @param type operation type to allocate
221 * - On success returns a valid rte_crypto_op structure
222 * - On failure returns NULL
224 static inline struct rte_crypto_op *
225 rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
227 struct rte_crypto_op *op = NULL;
230 retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
231 if (unlikely(retval != 1))
234 __rte_crypto_op_reset(op, type);
241 * Bulk allocate crypto operations from a mempool with default parameters set
243 * @param mempool crypto operation mempool
244 * @param type operation type to allocate
245 * @param ops Array to place allocated crypto operations
246 * @param nb_ops Number of crypto operations to allocate
249 * - nb_ops if the number of operations requested were allocated.
250 * - 0 if the requested number of ops are not available.
251 * None are allocated in this case.
254 static inline unsigned
255 rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
256 enum rte_crypto_op_type type,
257 struct rte_crypto_op **ops, uint16_t nb_ops)
261 if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
265 for (i = 0; i < nb_ops; i++)
266 __rte_crypto_op_reset(ops[i], type);
274 * Returns a pointer to the private data of a crypto operation if
275 * that operation has enough capacity for requested size.
277 * @param op crypto operation.
278 * @param size size of space requested in private data.
281 * - if sufficient space available returns pointer to start of private data
282 * - if insufficient space returns NULL
285 __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
289 if (likely(op->mempool != NULL)) {
290 priv_size = __rte_crypto_op_get_priv_data_size(op->mempool);
292 if (likely(priv_size >= size))
293 return (void *)((uint8_t *)(op + 1) +
294 sizeof(struct rte_crypto_sym_op));
301 * free crypto operation structure
302 * If operation has been allocate from a rte_mempool, then the operation will
303 * be returned to the mempool.
305 * @param op symmetric crypto operation
308 rte_crypto_op_free(struct rte_crypto_op *op)
310 if (op != NULL && op->mempool != NULL)
311 rte_mempool_put(op->mempool, op);
315 * Allocate a symmetric crypto operation in the private data of an mbuf.
317 * @param m mbuf which is associated with the crypto operation, the
318 * operation will be allocated in the private data of that
322 * - On success returns a pointer to the crypto operation.
323 * - On failure returns NULL.
325 static inline struct rte_crypto_op *
326 rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
328 if (unlikely(m == NULL))
332 * check that the mbuf's private data size is sufficient to contain a
335 if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
336 sizeof(struct rte_crypto_sym_op))))
339 /* private data starts immediately after the mbuf header in the mbuf. */
340 struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
342 __rte_crypto_op_reset(op, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
351 * Allocate space for symmetric crypto xforms in the private data space of the
352 * crypto operation. This also defaults the crypto xform type and configures
353 * the chaining of the xforms in the crypto operation
356 * - On success returns pointer to first crypto xform in crypto operations chain
357 * - On failure returns NULL
359 static inline struct rte_crypto_sym_xform *
360 rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
365 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
368 size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
370 priv_data = __rte_crypto_op_get_priv_data(op, size);
371 if (priv_data == NULL)
374 return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
380 * Attach a session to a crypto operation
382 * @param op crypto operation, must be of type symmetric
383 * @param sess cryptodev session
386 rte_crypto_op_attach_sym_session(struct rte_crypto_op *op,
387 struct rte_cryptodev_sym_session *sess)
389 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
392 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
394 return __rte_crypto_sym_op_attach_sym_session(op->sym, sess);
401 #endif /* _RTE_CRYPTO_H_ */