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