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