10fe0804b2edb84ce174afdf035b96af8ca3809e
[deb_dpdk.git] / lib / librte_cryptodev / rte_crypto.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32
33 #ifndef _RTE_CRYPTO_H_
34 #define _RTE_CRYPTO_H_
35
36 /**
37  * @file rte_crypto.h
38  *
39  * RTE Cryptography Common Definitions
40  *
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48 #include <rte_mbuf.h>
49 #include <rte_memory.h>
50 #include <rte_mempool.h>
51 #include <rte_common.h>
52
53 #include "rte_crypto_sym.h"
54
55 /** Crypto operation types */
56 enum rte_crypto_op_type {
57         RTE_CRYPTO_OP_TYPE_UNDEFINED,
58         /**< Undefined operation type */
59         RTE_CRYPTO_OP_TYPE_SYMMETRIC,
60         /**< Symmetric operation */
61 };
62
63 /** Status of crypto operation */
64 enum rte_crypto_op_status {
65         RTE_CRYPTO_OP_STATUS_SUCCESS,
66         /**< Operation completed successfully */
67         RTE_CRYPTO_OP_STATUS_NOT_PROCESSED,
68         /**< Operation has not yet been processed by a crypto device */
69         RTE_CRYPTO_OP_STATUS_AUTH_FAILED,
70         /**< Authentication verification failed */
71         RTE_CRYPTO_OP_STATUS_INVALID_SESSION,
72         /**<
73          * Symmetric operation failed due to invalid session arguments, or if
74          * in session-less mode, failed to allocate private operation material.
75          */
76         RTE_CRYPTO_OP_STATUS_INVALID_ARGS,
77         /**< Operation failed due to invalid arguments in request */
78         RTE_CRYPTO_OP_STATUS_ERROR,
79         /**< Error handling operation */
80 };
81
82 /**
83  * Crypto operation session type. This is used to specify whether a crypto
84  * operation has session structure attached for immutable parameters or if all
85  * operation information is included in the operation data structure.
86  */
87 enum rte_crypto_op_sess_type {
88         RTE_CRYPTO_OP_WITH_SESSION,     /**< Session based crypto operation */
89         RTE_CRYPTO_OP_SESSIONLESS       /**< Session-less crypto operation */
90 };
91
92 /**
93  * Cryptographic Operation.
94  *
95  * This structure contains data relating to performing cryptographic
96  * operations. This operation structure is used to contain any operation which
97  * is supported by the cryptodev API, PMDs should check the type parameter to
98  * verify that the operation is a support function of the device. Crypto
99  * operations are enqueued and dequeued in crypto PMDs using the
100  * rte_cryptodev_enqueue_burst() / rte_cryptodev_dequeue_burst() .
101  */
102 struct rte_crypto_op {
103         uint8_t type;
104         /**< operation type */
105         uint8_t status;
106         /**<
107          * operation status - this is reset to
108          * RTE_CRYPTO_OP_STATUS_NOT_PROCESSED on allocation from mempool and
109          * will be set to RTE_CRYPTO_OP_STATUS_SUCCESS after crypto operation
110          * is successfully processed by a crypto PMD
111          */
112         uint8_t sess_type;
113         /**< operation session type */
114
115         uint8_t reserved[5];
116         /**< Reserved bytes to fill 64 bits for future additions */
117         struct rte_mempool *mempool;
118         /**< crypto operation mempool which operation is allocated from */
119
120         phys_addr_t phys_addr;
121         /**< physical address of crypto operation */
122
123         RTE_STD_C11
124         union {
125                 struct rte_crypto_sym_op sym[0];
126                 /**< Symmetric operation parameters */
127         }; /**< operation specific parameters */
128 };
129
130 /**
131  * Reset the fields of a crypto operation to their default values.
132  *
133  * @param       op      The crypto operation to be reset.
134  * @param       type    The crypto operation type.
135  */
136 static inline void
137 __rte_crypto_op_reset(struct rte_crypto_op *op, enum rte_crypto_op_type type)
138 {
139         op->type = type;
140         op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
141         op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
142
143         switch (type) {
144         case RTE_CRYPTO_OP_TYPE_SYMMETRIC:
145                 __rte_crypto_sym_op_reset(op->sym);
146                 break;
147         default:
148                 break;
149         }
150 }
151
152 /**
153  * Private data structure belonging to a crypto symmetric operation pool.
154  */
155 struct rte_crypto_op_pool_private {
156         enum rte_crypto_op_type type;
157         /**< Crypto op pool type operation. */
158         uint16_t priv_size;
159         /**< Size of private area in each crypto operation. */
160 };
161
162
163 /**
164  * Returns the size of private data allocated with each rte_crypto_op object by
165  * the mempool
166  *
167  * @param       mempool rte_crypto_op mempool
168  *
169  * @return      private data size
170  */
171 static inline uint16_t
172 __rte_crypto_op_get_priv_data_size(struct rte_mempool *mempool)
173 {
174         struct rte_crypto_op_pool_private *priv =
175                 (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
176
177         return priv->priv_size;
178 }
179
180
181 /**
182  * Creates a crypto operation pool
183  *
184  * @param       name            pool name
185  * @param       type            crypto operation type, use
186  *                              RTE_CRYPTO_OP_TYPE_UNDEFINED for a pool which
187  *                              supports all operation types
188  * @param       nb_elts         number of elements in pool
189  * @param       cache_size      Number of elements to cache on lcore, see
190  *                              *rte_mempool_create* for further details about
191  *                              cache size
192  * @param       priv_size       Size of private data to allocate with each
193  *                              operation
194  * @param       socket_id       Socket to allocate memory on
195  *
196  * @return
197  *  - On success pointer to mempool
198  *  - On failure NULL
199  */
200 extern struct rte_mempool *
201 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
202                 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
203                 int socket_id);
204
205 /**
206  * Bulk allocate raw element from mempool and return as crypto operations
207  *
208  * @param       mempool         crypto operation mempool.
209  * @param       type            crypto operation type.
210  * @param       ops             Array to place allocated crypto operations
211  * @param       nb_ops          Number of crypto operations to allocate
212  *
213  * @returns
214  * - On success returns  number of ops allocated
215  */
216 static inline int
217 __rte_crypto_op_raw_bulk_alloc(struct rte_mempool *mempool,
218                 enum rte_crypto_op_type type,
219                 struct rte_crypto_op **ops, uint16_t nb_ops)
220 {
221         struct rte_crypto_op_pool_private *priv;
222
223         priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
224         if (unlikely(priv->type != type &&
225                         priv->type != RTE_CRYPTO_OP_TYPE_UNDEFINED))
226                 return -EINVAL;
227
228         if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
229                 return nb_ops;
230
231         return 0;
232 }
233
234 /**
235  * Allocate a crypto operation from a mempool with default parameters set
236  *
237  * @param       mempool crypto operation mempool
238  * @param       type    operation type to allocate
239  *
240  * @returns
241  * - On success returns a valid rte_crypto_op structure
242  * - On failure returns NULL
243  */
244 static inline struct rte_crypto_op *
245 rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
246 {
247         struct rte_crypto_op *op = NULL;
248         int retval;
249
250         retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
251         if (unlikely(retval != 1))
252                 return NULL;
253
254         __rte_crypto_op_reset(op, type);
255
256         return op;
257 }
258
259
260 /**
261  * Bulk allocate crypto operations from a mempool with default parameters set
262  *
263  * @param       mempool crypto operation mempool
264  * @param       type    operation type to allocate
265  * @param       ops     Array to place allocated crypto operations
266  * @param       nb_ops  Number of crypto operations to allocate
267  *
268  * @returns
269  * - nb_ops if the number of operations requested were allocated.
270  * - 0 if the requested number of ops are not available.
271  *   None are allocated in this case.
272  */
273
274 static inline unsigned
275 rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
276                 enum rte_crypto_op_type type,
277                 struct rte_crypto_op **ops, uint16_t nb_ops)
278 {
279         int i;
280
281         if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
282                         != nb_ops))
283                 return 0;
284
285         for (i = 0; i < nb_ops; i++)
286                 __rte_crypto_op_reset(ops[i], type);
287
288         return nb_ops;
289 }
290
291
292
293 /**
294  * Returns a pointer to the private data of a crypto operation if
295  * that operation has enough capacity for requested size.
296  *
297  * @param       op      crypto operation.
298  * @param       size    size of space requested in private data.
299  *
300  * @returns
301  * - if sufficient space available returns pointer to start of private data
302  * - if insufficient space returns NULL
303  */
304 static inline void *
305 __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
306 {
307         uint32_t priv_size;
308
309         if (likely(op->mempool != NULL)) {
310                 priv_size = __rte_crypto_op_get_priv_data_size(op->mempool);
311
312                 if (likely(priv_size >= size))
313                         return (void *)((uint8_t *)(op + 1) +
314                                         sizeof(struct rte_crypto_sym_op));
315         }
316
317         return NULL;
318 }
319
320 /**
321  * free crypto operation structure
322  * If operation has been allocate from a rte_mempool, then the operation will
323  * be returned to the mempool.
324  *
325  * @param       op      symmetric crypto operation
326  */
327 static inline void
328 rte_crypto_op_free(struct rte_crypto_op *op)
329 {
330         if (op != NULL && op->mempool != NULL)
331                 rte_mempool_put(op->mempool, op);
332 }
333
334 /**
335  * Allocate a symmetric crypto operation in the private data of an mbuf.
336  *
337  * @param       m       mbuf which is associated with the crypto operation, the
338  *                      operation will be allocated in the private data of that
339  *                      mbuf.
340  *
341  * @returns
342  * - On success returns a pointer to the crypto operation.
343  * - On failure returns NULL.
344  */
345 static inline struct rte_crypto_op *
346 rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
347 {
348         if (unlikely(m == NULL))
349                 return NULL;
350
351         /*
352          * check that the mbuf's private data size is sufficient to contain a
353          * crypto operation
354          */
355         if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
356                         sizeof(struct rte_crypto_sym_op))))
357                 return NULL;
358
359         /* private data starts immediately after the mbuf header in the mbuf. */
360         struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
361
362         __rte_crypto_op_reset(op, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
363
364         op->mempool = NULL;
365         op->sym->m_src = m;
366
367         return op;
368 }
369
370 /**
371  * Allocate space for symmetric crypto xforms in the private data space of the
372  * crypto operation. This also defaults the crypto xform type and configures
373  * the chaining of the xforms in the crypto operation
374  *
375  * @return
376  * - On success returns pointer to first crypto xform in crypto operations chain
377  * - On failure returns NULL
378  */
379 static inline struct rte_crypto_sym_xform *
380 rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
381 {
382         void *priv_data;
383         uint32_t size;
384
385         if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
386                 return NULL;
387
388         size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
389
390         priv_data = __rte_crypto_op_get_priv_data(op, size);
391         if (priv_data == NULL)
392                 return NULL;
393
394         return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
395                         nb_xforms);
396 }
397
398
399 /**
400  * Attach a session to a crypto operation
401  *
402  * @param       op      crypto operation, must be of type symmetric
403  * @param       sess    cryptodev session
404  */
405 static inline int
406 rte_crypto_op_attach_sym_session(struct rte_crypto_op *op,
407                 struct rte_cryptodev_sym_session *sess)
408 {
409         if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
410                 return -1;
411
412         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
413
414         return __rte_crypto_sym_op_attach_sym_session(op->sym, sess);
415 }
416
417 #ifdef __cplusplus
418 }
419 #endif
420
421 #endif /* _RTE_CRYPTO_H_ */