DPDK  22.11.0-rc0
rte_crypto.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4 
5 #ifndef _RTE_CRYPTO_H_
6 #define _RTE_CRYPTO_H_
7 
15 #ifdef __cplusplus
16 extern "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 
36 };
37 
55 };
56 
66 };
67 
68 /* Auxiliary flags related to IPsec offload with RTE_SECURITY */
69 
70 #define RTE_CRYPTO_OP_AUX_FLAGS_IPSEC_SOFT_EXPIRY (1 << 0)
71 
83 struct rte_crypto_op {
84  __extension__
85  union {
86  uint64_t raw;
87  __extension__
88  struct {
89  uint8_t type;
91  uint8_t status;
99  uint8_t sess_type;
101  uint8_t aux_flags;
106  uint8_t reserved[2];
118  };
119  };
126 /* empty structures do not have zero size in C++ leading to compilation errors
127  * with clang about structure/union having different sizes in C and C++.
128  * While things are clearer with an explicit union, since each field is
129  * zero-sized it's not actually needed, so omit it for C++
130  */
131 #ifndef __cplusplus
132  __extension__
133  union {
134 #endif
141 #ifndef __cplusplus
142  };
143 #endif
144 };
145 
152 static inline void
154 {
155  op->type = type;
158 
159  switch (type) {
162  break;
164  memset(op->asym, 0, sizeof(struct rte_crypto_asym_op));
165  break;
167  default:
168  break;
169  }
170 }
171 
178  uint16_t priv_size;
180 };
181 
182 
191 static inline uint16_t
193 {
194  struct rte_crypto_op_pool_private *priv =
196 
197  return priv->priv_size;
198 }
199 
200 
220 extern struct rte_mempool *
222  unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
223  int socket_id);
224 
236 static inline int
238  enum rte_crypto_op_type type,
239  struct rte_crypto_op **ops, uint16_t nb_ops)
240 {
241  struct rte_crypto_op_pool_private *priv;
242 
243  priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
244  if (unlikely(priv->type != type &&
246  return -EINVAL;
247 
248  if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
249  return nb_ops;
250 
251  return 0;
252 }
253 
264 static inline struct rte_crypto_op *
266 {
267  struct rte_crypto_op *op = NULL;
268  int retval;
269 
270  retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
271  if (unlikely(retval != 1))
272  return NULL;
273 
274  __rte_crypto_op_reset(op, type);
275 
276  return op;
277 }
278 
279 
294 static inline unsigned
296  enum rte_crypto_op_type type,
297  struct rte_crypto_op **ops, uint16_t nb_ops)
298 {
299  int i;
300 
301  if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
302  != nb_ops))
303  return 0;
304 
305  for (i = 0; i < nb_ops; i++)
306  __rte_crypto_op_reset(ops[i], type);
307 
308  return nb_ops;
309 }
310 
311 
312 
324 static inline void *
326 {
327  uint32_t priv_size;
328 
329  if (likely(op->mempool != NULL)) {
331 
332  if (likely(priv_size >= size)) {
334  return (void *)((uint8_t *)(op + 1) +
335  sizeof(struct rte_crypto_sym_op));
337  return (void *)((uint8_t *)(op + 1) +
338  sizeof(struct rte_crypto_asym_op));
339  }
340  }
341 
342  return NULL;
343 }
344 
354 static inline void
356 {
357  if (op != NULL && op->mempool != NULL)
358  rte_mempool_put(op->mempool, op);
359 }
360 
372 static inline struct rte_crypto_op *
374 {
375  if (unlikely(m == NULL))
376  return NULL;
377 
378  /*
379  * check that the mbuf's private data size is sufficient to contain a
380  * crypto operation
381  */
382  if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
383  sizeof(struct rte_crypto_sym_op))))
384  return NULL;
385 
386  /* private data starts immediately after the mbuf header in the mbuf. */
387  struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
388 
390 
391  op->mempool = NULL;
392  op->sym->m_src = m;
393 
394  return op;
395 }
396 
406 static inline struct rte_crypto_sym_xform *
407 rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
408 {
409  void *priv_data;
410  uint32_t size;
411 
413  return NULL;
414 
415  size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
416 
417  priv_data = __rte_crypto_op_get_priv_data(op, size);
418  if (priv_data == NULL)
419  return NULL;
420 
421  return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
422  nb_xforms);
423 }
424 
425 
432 static inline int
434  struct rte_cryptodev_sym_session *sess)
435 {
437  return -1;
438 
440 
442 }
443 
450 static inline int
452  struct rte_cryptodev_asym_session *sess)
453 {
455  return -1;
456 
458  op->asym->session = sess;
459  return 0;
460 }
461 
462 #ifdef __cplusplus
463 }
464 #endif
465 
466 #endif /* _RTE_CRYPTO_H_ */
static void __rte_crypto_op_reset(struct rte_crypto_op *op, enum rte_crypto_op_type type)
Definition: rte_crypto.h:153
enum rte_crypto_op_type type
Definition: rte_crypto.h:176
struct rte_mempool * rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type, unsigned nb_elts, unsigned cache_size, uint16_t priv_size, int socket_id)
static int rte_crypto_op_attach_asym_session(struct rte_crypto_op *op, struct rte_cryptodev_asym_session *sess)
Definition: rte_crypto.h:451
#define likely(x)
static void * __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
Definition: rte_crypto.h:325
uint8_t status
Definition: rte_crypto.h:91
uint8_t type
Definition: rte_crypto.h:89
uint64_t rte_iova_t
Definition: rte_common.h:458
static int __rte_crypto_op_raw_bulk_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type, struct rte_crypto_op **ops, uint16_t nb_ops)
Definition: rte_crypto.h:237
rte_iova_t phys_addr
Definition: rte_crypto.h:123
static void __rte_crypto_sym_op_reset(struct rte_crypto_sym_op *op)
struct rte_mbuf * m_src
uint8_t aux_flags
Definition: rte_crypto.h:101
static unsigned rte_crypto_op_bulk_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type, struct rte_crypto_op **ops, uint16_t nb_ops)
Definition: rte_crypto.h:295
uint32_t cache_size
Definition: rte_mempool.h:214
char name[RTE_MEMPOOL_NAMESIZE]
Definition: rte_mempool.h:203
static int rte_crypto_op_attach_sym_session(struct rte_crypto_op *op, struct rte_cryptodev_sym_session *sess)
Definition: rte_crypto.h:433
static struct rte_crypto_sym_xform * rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
Definition: rte_crypto.h:407
#define unlikely(x)
uint16_t priv_size
static struct rte_crypto_sym_xform * __rte_crypto_sym_op_sym_xforms_alloc(struct rte_crypto_sym_op *sym_op, void *priv_data, uint8_t nb_xforms)
static uint16_t __rte_crypto_op_get_priv_data_size(struct rte_mempool *mempool)
Definition: rte_crypto.h:192
rte_crypto_op_type
Definition: rte_crypto.h:29
static void rte_crypto_op_free(struct rte_crypto_op *op)
Definition: rte_crypto.h:355
static int __rte_crypto_sym_op_attach_sym_session(struct rte_crypto_sym_op *sym_op, struct rte_cryptodev_sym_session *sess)
static __rte_always_inline int rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned int n)
Definition: rte_mempool.h:1558
uint16_t private_data_offset
Definition: rte_crypto.h:110
static struct rte_crypto_op * rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
Definition: rte_crypto.h:373
struct rte_crypto_asym_op asym[0]
Definition: rte_crypto.h:138
rte_crypto_op_sess_type
Definition: rte_crypto.h:62
struct rte_cryptodev_asym_session * session
uint8_t reserved[2]
Definition: rte_crypto.h:106
static struct rte_crypto_op * rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
Definition: rte_crypto.h:265
static __rte_always_inline void rte_mempool_put(struct rte_mempool *mp, void *obj)
Definition: rte_mempool.h:1422
uint8_t sess_type
Definition: rte_crypto.h:99
static void * rte_mempool_get_priv(struct rte_mempool *mp)
Definition: rte_mempool.h:1740
rte_crypto_op_status
Definition: rte_crypto.h:39
struct rte_mempool * mempool
Definition: rte_crypto.h:120
struct rte_crypto_sym_op sym[0]
Definition: rte_crypto.h:135