1
2
3
4
5#ifndef _RTE_CRYPTO_H_
6#define _RTE_CRYPTO_H_
7
8
9
10
11
12
13
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19
20#include <rte_mbuf.h>
21#include <rte_memory.h>
22#include <rte_mempool.h>
23#include <rte_common.h>
24
25#include "rte_crypto_sym.h"
26#include "rte_crypto_asym.h"
27
28
29enum rte_crypto_op_type {
30 RTE_CRYPTO_OP_TYPE_UNDEFINED,
31
32 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
33
34 RTE_CRYPTO_OP_TYPE_ASYMMETRIC
35
36};
37
38
39enum rte_crypto_op_status {
40 RTE_CRYPTO_OP_STATUS_SUCCESS,
41
42 RTE_CRYPTO_OP_STATUS_NOT_PROCESSED,
43
44 RTE_CRYPTO_OP_STATUS_AUTH_FAILED,
45
46 RTE_CRYPTO_OP_STATUS_INVALID_SESSION,
47
48
49
50
51 RTE_CRYPTO_OP_STATUS_INVALID_ARGS,
52
53 RTE_CRYPTO_OP_STATUS_ERROR,
54
55};
56
57
58
59
60
61
62enum rte_crypto_op_sess_type {
63 RTE_CRYPTO_OP_WITH_SESSION,
64 RTE_CRYPTO_OP_SESSIONLESS,
65 RTE_CRYPTO_OP_SECURITY_SESSION
66};
67
68
69
70
71
72
73
74
75
76
77
78struct rte_crypto_op {
79 __extension__
80 union {
81 uint64_t raw;
82 __extension__
83 struct {
84 uint8_t type;
85
86 uint8_t status;
87
88
89
90
91
92
93
94 uint8_t sess_type;
95
96 uint8_t reserved[3];
97
98
99
100 uint16_t private_data_offset;
101
102
103
104
105
106
107
108 };
109 };
110 struct rte_mempool *mempool;
111
112
113 rte_iova_t phys_addr;
114
115
116 __extension__
117 union {
118 struct rte_crypto_sym_op sym[0];
119
120
121 struct rte_crypto_asym_op asym[0];
122
123
124 };
125};
126
127
128
129
130
131
132
133static inline void
134__rte_crypto_op_reset(struct rte_crypto_op *op, enum rte_crypto_op_type type)
135{
136 op->type = type;
137 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
138 op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
139
140 switch (type) {
141 case RTE_CRYPTO_OP_TYPE_SYMMETRIC:
142 __rte_crypto_sym_op_reset(op->sym);
143 break;
144 case RTE_CRYPTO_OP_TYPE_ASYMMETRIC:
145 memset(op->asym, 0, sizeof(struct rte_crypto_asym_op));
146 break;
147 case RTE_CRYPTO_OP_TYPE_UNDEFINED:
148 default:
149 break;
150 }
151}
152
153
154
155
156struct rte_crypto_op_pool_private {
157 enum rte_crypto_op_type type;
158
159 uint16_t priv_size;
160
161};
162
163
164
165
166
167
168
169
170
171
172static inline uint16_t
173__rte_crypto_op_get_priv_data_size(struct rte_mempool *mempool)
174{
175 struct rte_crypto_op_pool_private *priv =
176 (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
177
178 return priv->priv_size;
179}
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201extern struct rte_mempool *
202rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
203 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
204 int socket_id);
205
206
207
208
209
210
211
212
213
214
215
216
217static inline int
218__rte_crypto_op_raw_bulk_alloc(struct rte_mempool *mempool,
219 enum rte_crypto_op_type type,
220 struct rte_crypto_op **ops, uint16_t nb_ops)
221{
222 struct rte_crypto_op_pool_private *priv;
223
224 priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
225 if (unlikely(priv->type != type &&
226 priv->type != RTE_CRYPTO_OP_TYPE_UNDEFINED))
227 return -EINVAL;
228
229 if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
230 return nb_ops;
231
232 return 0;
233}
234
235
236
237
238
239
240
241
242
243
244
245static inline struct rte_crypto_op *
246rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
247{
248 struct rte_crypto_op *op = NULL;
249 int retval;
250
251 retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
252 if (unlikely(retval != 1))
253 return NULL;
254
255 __rte_crypto_op_reset(op, type);
256
257 return op;
258}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275static inline unsigned
276rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
277 enum rte_crypto_op_type type,
278 struct rte_crypto_op **ops, uint16_t nb_ops)
279{
280 int i;
281
282 if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
283 != nb_ops))
284 return 0;
285
286 for (i = 0; i < nb_ops; i++)
287 __rte_crypto_op_reset(ops[i], type);
288
289 return nb_ops;
290}
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305static inline void *
306__rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
307{
308 uint32_t priv_size;
309
310 if (likely(op->mempool != NULL)) {
311 priv_size = __rte_crypto_op_get_priv_data_size(op->mempool);
312
313 if (likely(priv_size >= size)) {
314 if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
315 return (void *)((uint8_t *)(op + 1) +
316 sizeof(struct rte_crypto_sym_op));
317 if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
318 return (void *)((uint8_t *)(op + 1) +
319 sizeof(struct rte_crypto_asym_op));
320 }
321 }
322
323 return NULL;
324}
325
326
327
328
329
330
331
332
333static inline void
334rte_crypto_op_free(struct rte_crypto_op *op)
335{
336 if (op != NULL && op->mempool != NULL)
337 rte_mempool_put(op->mempool, op);
338}
339
340
341
342
343
344
345
346
347
348
349
350
351static inline struct rte_crypto_op *
352rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
353{
354 if (unlikely(m == NULL))
355 return NULL;
356
357
358
359
360
361 if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
362 sizeof(struct rte_crypto_sym_op))))
363 return NULL;
364
365
366 struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
367
368 __rte_crypto_op_reset(op, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
369
370 op->mempool = NULL;
371 op->sym->m_src = m;
372
373 return op;
374}
375
376
377
378
379
380
381
382
383
384
385static inline struct rte_crypto_sym_xform *
386rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
387{
388 void *priv_data;
389 uint32_t size;
390
391 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
392 return NULL;
393
394 size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
395
396 priv_data = __rte_crypto_op_get_priv_data(op, size);
397 if (priv_data == NULL)
398 return NULL;
399
400 return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
401 nb_xforms);
402}
403
404
405
406
407
408
409
410
411static inline int
412rte_crypto_op_attach_sym_session(struct rte_crypto_op *op,
413 struct rte_cryptodev_sym_session *sess)
414{
415 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
416 return -1;
417
418 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
419
420 return __rte_crypto_sym_op_attach_sym_session(op->sym, sess);
421}
422
423
424
425
426
427
428
429static inline int
430rte_crypto_op_attach_asym_session(struct rte_crypto_op *op,
431 struct rte_cryptodev_asym_session *sess)
432{
433 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_ASYMMETRIC))
434 return -1;
435
436 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
437 op->asym->session = sess;
438 return 0;
439}
440
441#ifdef __cplusplus
442}
443#endif
444
445#endif
446