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