DPDK  23.07.0
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 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 
19 #include <rte_mbuf.h>
20 #include <rte_memory.h>
21 #include <rte_mempool.h>
22 #include <rte_common.h>
23 
24 #include "rte_crypto_sym.h"
25 #include "rte_crypto_asym.h"
26 
35 };
36 
54 };
55 
65 };
66 
67 /* Auxiliary flags related to IPsec offload with RTE_SECURITY */
68 
69 #define RTE_CRYPTO_OP_AUX_FLAGS_IPSEC_SOFT_EXPIRY (1 << 0)
70 
82 struct rte_crypto_op {
83  __extension__
84  union {
85  uint64_t raw;
86  __extension__
87  struct {
88  uint8_t type;
90  uint8_t status;
98  uint8_t sess_type;
100  uint8_t aux_flags;
105  uint8_t reserved[2];
117  };
118  };
125 /* empty structures do not have zero size in C++ leading to compilation errors
126  * with clang about structure/union having different sizes in C and C++.
127  * While things are clearer with an explicit union, since each field is
128  * zero-sized it's not actually needed, so omit it for C++
129  */
130 #ifndef __cplusplus
131  __extension__
132  union {
133 #endif
140 #ifndef __cplusplus
141  };
142 #endif
143 };
144 
151 static inline void
153 {
154  op->type = type;
157 
158  switch (type) {
161  break;
163  memset(op->asym, 0, sizeof(struct rte_crypto_asym_op));
164  break;
166  default:
167  break;
168  }
169 }
170 
177  uint16_t priv_size;
179 };
180 
181 
190 static inline uint16_t
192 {
193  struct rte_crypto_op_pool_private *priv =
195 
196  return priv->priv_size;
197 }
198 
199 
219 struct rte_mempool *
221  unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
222  int socket_id);
223 
235 static inline int
237  enum rte_crypto_op_type type,
238  struct rte_crypto_op **ops, uint16_t nb_ops)
239 {
240  struct rte_crypto_op_pool_private *priv;
241 
242  priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
243  if (unlikely(priv->type != type &&
245  return -EINVAL;
246 
247  if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
248  return nb_ops;
249 
250  return 0;
251 }
252 
263 static inline struct rte_crypto_op *
265 {
266  struct rte_crypto_op *op = NULL;
267  int retval;
268 
269  retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
270  if (unlikely(retval != 1))
271  return NULL;
272 
273  __rte_crypto_op_reset(op, type);
274 
275  return op;
276 }
277 
278 
293 static inline unsigned
295  enum rte_crypto_op_type type,
296  struct rte_crypto_op **ops, uint16_t nb_ops)
297 {
298  int i;
299 
300  if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
301  != nb_ops))
302  return 0;
303 
304  for (i = 0; i < nb_ops; i++)
305  __rte_crypto_op_reset(ops[i], type);
306 
307  return nb_ops;
308 }
309 
310 
311 
323 static inline void *
325 {
326  uint32_t priv_size;
327 
328  if (likely(op->mempool != NULL)) {
330 
331  if (likely(priv_size >= size)) {
333  return (void *)((uint8_t *)(op + 1) +
334  sizeof(struct rte_crypto_sym_op));
336  return (void *)((uint8_t *)(op + 1) +
337  sizeof(struct rte_crypto_asym_op));
338  }
339  }
340 
341  return NULL;
342 }
343 
353 static inline void
355 {
356  if (op != NULL && op->mempool != NULL)
357  rte_mempool_put(op->mempool, op);
358 }
359 
371 static inline struct rte_crypto_op *
373 {
374  if (unlikely(m == NULL))
375  return NULL;
376 
377  /*
378  * check that the mbuf's private data size is sufficient to contain a
379  * crypto operation
380  */
381  if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
382  sizeof(struct rte_crypto_sym_op))))
383  return NULL;
384 
385  /* private data starts immediately after the mbuf header in the mbuf. */
386  struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
387 
389 
390  op->mempool = NULL;
391  op->sym->m_src = m;
392 
393  return op;
394 }
395 
405 static inline struct rte_crypto_sym_xform *
406 rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
407 {
408  void *priv_data;
409  uint32_t size;
410 
412  return NULL;
413 
414  size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
415 
416  priv_data = __rte_crypto_op_get_priv_data(op, size);
417  if (priv_data == NULL)
418  return NULL;
419 
420  return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
421  nb_xforms);
422 }
423 
424 
431 static inline int
433 {
435  return -1;
436 
438 
440 }
441 
448 static inline int
450  struct rte_cryptodev_asym_session *sess)
451 {
453  return -1;
454 
456  op->asym->session = sess;
457  return 0;
458 }
459 
460 #ifdef __cplusplus
461 }
462 #endif
463 
464 #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:152
enum rte_crypto_op_type type
Definition: rte_crypto.h:175
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:449
#define likely(x)
static void * __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
Definition: rte_crypto.h:324
static int __rte_crypto_sym_op_attach_sym_session(struct rte_crypto_sym_op *sym_op, void *sess)
uint8_t status
Definition: rte_crypto.h:90
uint8_t type
Definition: rte_crypto.h:88
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:236
rte_iova_t phys_addr
Definition: rte_crypto.h:122
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:100
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:294
uint32_t cache_size
Definition: rte_mempool.h:231
char name[RTE_MEMPOOL_NAMESIZE]
Definition: rte_mempool.h:220
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:406
#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:191
rte_crypto_op_type
Definition: rte_crypto.h:28
static void rte_crypto_op_free(struct rte_crypto_op *op)
Definition: rte_crypto.h:354
static __rte_always_inline int rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned int n)
Definition: rte_mempool.h:1664
uint16_t private_data_offset
Definition: rte_crypto.h:109
static struct rte_crypto_op * rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
Definition: rte_crypto.h:372
struct rte_crypto_asym_op asym[0]
Definition: rte_crypto.h:137
rte_crypto_op_sess_type
Definition: rte_crypto.h:61
struct rte_cryptodev_asym_session * session
uint8_t reserved[2]
Definition: rte_crypto.h:105
static int rte_crypto_op_attach_sym_session(struct rte_crypto_op *op, void *sess)
Definition: rte_crypto.h:432
static struct rte_crypto_op * rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
Definition: rte_crypto.h:264
static __rte_always_inline void rte_mempool_put(struct rte_mempool *mp, void *obj)
Definition: rte_mempool.h:1471
uint8_t sess_type
Definition: rte_crypto.h:98
static void * rte_mempool_get_priv(struct rte_mempool *mp)
Definition: rte_mempool.h:1846
rte_crypto_op_status
Definition: rte_crypto.h:38
struct rte_mempool * mempool
Definition: rte_crypto.h:119
struct rte_crypto_sym_op sym[0]
Definition: rte_crypto.h:134