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