DPDK  24.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 
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 crypto operation */
68 #define RTE_CRYPTO_OP_AUX_FLAGS_SESS_SOFT_EXPIRY (1 << 0)
69 
76 /* Auxiliary flags related to IPsec offload with RTE_SECURITY */
77 
78 #define RTE_CRYPTO_OP_AUX_FLAGS_IPSEC_SOFT_EXPIRY RTE_CRYPTO_OP_AUX_FLAGS_SESS_SOFT_EXPIRY
79 
91 struct rte_crypto_op {
92  __extension__
93  union {
94  uint64_t raw;
95  __extension__
96  struct {
97  uint8_t type;
99  uint8_t status;
107  uint8_t sess_type;
109  uint8_t aux_flags;
126  union {
127  struct {
128  uint8_t content_type;
145  } tls_record;
147  } param1;
149  uint8_t reserved[1];
161  };
162  };
169 /* empty structures do not have zero size in C++ leading to compilation errors
170  * with clang about structure/union having different sizes in C and C++.
171  * While things are clearer with an explicit union, since each field is
172  * zero-sized it's not actually needed, so omit it for C++
173  */
174 #ifndef __cplusplus
175  __extension__
176  union {
177 #endif
184 #ifndef __cplusplus
185  };
186 #endif
187 };
188 
195 static inline void
197 {
198  op->type = type;
201 
202  switch (type) {
205  break;
207  memset(op->asym, 0, sizeof(struct rte_crypto_asym_op));
208  break;
210  default:
211  break;
212  }
213 }
214 
221  uint16_t priv_size;
223 };
224 
225 
234 static inline uint16_t
236 {
237  struct rte_crypto_op_pool_private *priv =
239 
240  return priv->priv_size;
241 }
242 
243 
263 struct rte_mempool *
265  unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
266  int socket_id);
267 
279 static inline int
281  enum rte_crypto_op_type type,
282  struct rte_crypto_op **ops, uint16_t nb_ops)
283 {
284  struct rte_crypto_op_pool_private *priv;
285 
286  priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
287  if (unlikely(priv->type != type &&
289  return -EINVAL;
290 
291  if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
292  return nb_ops;
293 
294  return 0;
295 }
296 
307 static inline struct rte_crypto_op *
309 {
310  struct rte_crypto_op *op = NULL;
311  int retval;
312 
313  retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
314  if (unlikely(retval != 1))
315  return NULL;
316 
317  __rte_crypto_op_reset(op, type);
318 
319  return op;
320 }
321 
322 
337 static inline unsigned
339  enum rte_crypto_op_type type,
340  struct rte_crypto_op **ops, uint16_t nb_ops)
341 {
342  int i;
343 
344  if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
345  != nb_ops))
346  return 0;
347 
348  for (i = 0; i < nb_ops; i++)
349  __rte_crypto_op_reset(ops[i], type);
350 
351  return nb_ops;
352 }
353 
354 
355 
367 static inline void *
369 {
370  uint32_t priv_size;
371 
372  if (likely(op->mempool != NULL)) {
374 
375  if (likely(priv_size >= size)) {
377  return (void *)((uint8_t *)(op + 1) +
378  sizeof(struct rte_crypto_sym_op));
380  return (void *)((uint8_t *)(op + 1) +
381  sizeof(struct rte_crypto_asym_op));
382  }
383  }
384 
385  return NULL;
386 }
387 
397 static inline void
399 {
400  if (op != NULL && op->mempool != NULL)
401  rte_mempool_put(op->mempool, op);
402 }
403 
415 static inline struct rte_crypto_op *
417 {
418  if (unlikely(m == NULL))
419  return NULL;
420 
421  /*
422  * check that the mbuf's private data size is sufficient to contain a
423  * crypto operation
424  */
425  if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
426  sizeof(struct rte_crypto_sym_op))))
427  return NULL;
428 
429  /* private data starts immediately after the mbuf header in the mbuf. */
430  struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
431 
433 
434  op->mempool = NULL;
435  op->sym->m_src = m;
436 
437  return op;
438 }
439 
449 static inline struct rte_crypto_sym_xform *
450 rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
451 {
452  void *priv_data;
453  uint32_t size;
454 
456  return NULL;
457 
458  size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
459 
460  priv_data = __rte_crypto_op_get_priv_data(op, size);
461  if (priv_data == NULL)
462  return NULL;
463 
464  return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
465  nb_xforms);
466 }
467 
468 
475 static inline int
477 {
479  return -1;
480 
482 
484 }
485 
492 static inline int
494  struct rte_cryptodev_asym_session *sess)
495 {
497  return -1;
498 
500  op->asym->session = sess;
501  return 0;
502 }
503 
504 #ifdef __cplusplus
505 }
506 #endif
507 
508 #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:196
enum rte_crypto_op_type type
Definition: rte_crypto.h:219
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:493
#define likely(x)
static void * __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
Definition: rte_crypto.h:368
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:99
union rte_crypto_op::@79::@83::@85 param1
uint8_t type
Definition: rte_crypto.h:97
uint64_t rte_iova_t
Definition: rte_common.h:599
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:280
rte_iova_t phys_addr
Definition: rte_crypto.h:166
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:109
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:338
uint8_t reserved[1]
Definition: rte_crypto.h:149
uint32_t cache_size
Definition: rte_mempool.h:241
char name[RTE_MEMPOOL_NAMESIZE]
Definition: rte_mempool.h:231
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:450
#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:235
rte_crypto_op_type
Definition: rte_crypto.h:28
static void rte_crypto_op_free(struct rte_crypto_op *op)
Definition: rte_crypto.h:398
static __rte_always_inline int rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned int n)
Definition: rte_mempool.h:1679
uint16_t private_data_offset
Definition: rte_crypto.h:153
static struct rte_crypto_op * rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
Definition: rte_crypto.h:416
struct rte_crypto_asym_op asym[0]
Definition: rte_crypto.h:181
rte_crypto_op_sess_type
Definition: rte_crypto.h:61
struct rte_cryptodev_asym_session * session
static int rte_crypto_op_attach_sym_session(struct rte_crypto_op *op, void *sess)
Definition: rte_crypto.h:476
static struct rte_crypto_op * rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
Definition: rte_crypto.h:308
static __rte_always_inline void rte_mempool_put(struct rte_mempool *mp, void *obj)
Definition: rte_mempool.h:1487
uint8_t sess_type
Definition: rte_crypto.h:107
uint8_t content_type
Definition: rte_crypto.h:128
static void * rte_mempool_get_priv(struct rte_mempool *mp)
Definition: rte_mempool.h:1861
struct rte_crypto_op::@79::@83::@85::@86 tls_record
rte_crypto_op_status
Definition: rte_crypto.h:38
struct rte_mempool * mempool
Definition: rte_crypto.h:163
struct rte_crypto_sym_op sym[0]
Definition: rte_crypto.h:178