DPDK  20.11.6
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 
78 struct rte_crypto_op {
79  __extension__
80  union {
81  uint64_t raw;
82  __extension__
83  struct {
84  uint8_t type;
86  uint8_t status;
94  uint8_t sess_type;
96  uint8_t reserved[3];
108  };
109  };
116 /* empty structures do not have zero size in C++ leading to compilation errors
117  * with clang about structure/union having different sizes in C and C++.
118  * While things are clearer with an explicit union, since each field is
119  * zero-sized it's not actually needed, so omit it for C++
120  */
121 #ifndef __cplusplus
122  __extension__
123  union {
124 #endif
131 #ifndef __cplusplus
132  };
133 #endif
134 };
135 
142 static inline void
144 {
145  op->type = type;
148 
149  switch (type) {
152  break;
154  memset(op->asym, 0, sizeof(struct rte_crypto_asym_op));
155  break;
157  default:
158  break;
159  }
160 }
161 
168  uint16_t priv_size;
170 };
171 
172 
181 static inline uint16_t
183 {
184  struct rte_crypto_op_pool_private *priv =
186 
187  return priv->priv_size;
188 }
189 
190 
210 extern struct rte_mempool *
212  unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
213  int socket_id);
214 
226 static inline int
228  enum rte_crypto_op_type type,
229  struct rte_crypto_op **ops, uint16_t nb_ops)
230 {
231  struct rte_crypto_op_pool_private *priv;
232 
233  priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
234  if (unlikely(priv->type != type &&
236  return -EINVAL;
237 
238  if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
239  return nb_ops;
240 
241  return 0;
242 }
243 
254 static inline struct rte_crypto_op *
256 {
257  struct rte_crypto_op *op = NULL;
258  int retval;
259 
260  retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
261  if (unlikely(retval != 1))
262  return NULL;
263 
264  __rte_crypto_op_reset(op, type);
265 
266  return op;
267 }
268 
269 
284 static inline unsigned
286  enum rte_crypto_op_type type,
287  struct rte_crypto_op **ops, uint16_t nb_ops)
288 {
289  int i;
290 
291  if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
292  != nb_ops))
293  return 0;
294 
295  for (i = 0; i < nb_ops; i++)
296  __rte_crypto_op_reset(ops[i], type);
297 
298  return nb_ops;
299 }
300 
301 
302 
314 static inline void *
316 {
317  uint32_t priv_size;
318 
319  if (likely(op->mempool != NULL)) {
321 
322  if (likely(priv_size >= size)) {
324  return (void *)((uint8_t *)(op + 1) +
325  sizeof(struct rte_crypto_sym_op));
327  return (void *)((uint8_t *)(op + 1) +
328  sizeof(struct rte_crypto_asym_op));
329  }
330  }
331 
332  return NULL;
333 }
334 
342 static inline void
344 {
345  if (op != NULL && op->mempool != NULL)
346  rte_mempool_put(op->mempool, op);
347 }
348 
360 static inline struct rte_crypto_op *
362 {
363  if (unlikely(m == NULL))
364  return NULL;
365 
366  /*
367  * check that the mbuf's private data size is sufficient to contain a
368  * crypto operation
369  */
370  if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
371  sizeof(struct rte_crypto_sym_op))))
372  return NULL;
373 
374  /* private data starts immediately after the mbuf header in the mbuf. */
375  struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
376 
378 
379  op->mempool = NULL;
380  op->sym->m_src = m;
381 
382  return op;
383 }
384 
394 static inline struct rte_crypto_sym_xform *
395 rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
396 {
397  void *priv_data;
398  uint32_t size;
399 
401  return NULL;
402 
403  size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
404 
405  priv_data = __rte_crypto_op_get_priv_data(op, size);
406  if (priv_data == NULL)
407  return NULL;
408 
409  return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
410  nb_xforms);
411 }
412 
413 
420 static inline int
422  struct rte_cryptodev_sym_session *sess)
423 {
425  return -1;
426 
428 
430 }
431 
438 static inline int
440  struct rte_cryptodev_asym_session *sess)
441 {
443  return -1;
444 
446  op->asym->session = sess;
447  return 0;
448 }
449 
450 #ifdef __cplusplus
451 }
452 #endif
453 
454 #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:143
enum rte_crypto_op_type type
Definition: rte_crypto.h:166
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:439
#define likely(x)
static void * __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
Definition: rte_crypto.h:315
uint8_t status
Definition: rte_crypto.h:86
uint8_t type
Definition: rte_crypto.h:84
uint64_t rte_iova_t
Definition: rte_common.h:423
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:227
rte_iova_t phys_addr
Definition: rte_crypto.h:113
static void __rte_crypto_sym_op_reset(struct rte_crypto_sym_op *op)
struct rte_mbuf * m_src
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:285
uint32_t cache_size
Definition: rte_mempool.h:225
static int rte_crypto_op_attach_sym_session(struct rte_crypto_op *op, struct rte_cryptodev_sym_session *sess)
Definition: rte_crypto.h:421
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:395
#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:182
rte_crypto_op_type
Definition: rte_crypto.h:29
static void rte_crypto_op_free(struct rte_crypto_op *op)
Definition: rte_crypto.h:343
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:1522
uint16_t private_data_offset
Definition: rte_crypto.h:100
static struct rte_crypto_op * rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
Definition: rte_crypto.h:361
struct rte_crypto_asym_op asym[0]
Definition: rte_crypto.h:128
rte_crypto_op_sess_type
Definition: rte_crypto.h:62
struct rte_cryptodev_asym_session * session
static struct rte_crypto_op * rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
Definition: rte_crypto.h:255
static __rte_always_inline void rte_mempool_put(struct rte_mempool *mp, void *obj)
Definition: rte_mempool.h:1390
uint8_t sess_type
Definition: rte_crypto.h:94
static void * rte_mempool_get_priv(struct rte_mempool *mp)
Definition: rte_mempool.h:1702
char name[RTE_MEMZONE_NAMESIZE]
Definition: rte_mempool.h:214
uint8_t reserved[3]
Definition: rte_crypto.h:96
rte_crypto_op_status
Definition: rte_crypto.h:39
struct rte_mempool * mempool
Definition: rte_crypto.h:110
struct rte_crypto_sym_op sym[0]
Definition: rte_crypto.h:125