DPDK  19.08.2
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  __extension__
117  union {
124  };
125 };
126 
133 static inline void
135 {
136  op->type = type;
139 
140  switch (type) {
143  break;
145  memset(op->asym, 0, sizeof(struct rte_crypto_asym_op));
146  break;
148  default:
149  break;
150  }
151 }
152 
159  uint16_t priv_size;
161 };
162 
163 
172 static inline uint16_t
174 {
175  struct rte_crypto_op_pool_private *priv =
177 
178  return priv->priv_size;
179 }
180 
181 
201 extern struct rte_mempool *
202 rte_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 
217 static inline int
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 &&
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 
245 static inline struct rte_crypto_op *
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 
275 static inline unsigned
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 
305 static inline void *
307 {
308  uint32_t priv_size;
309 
310  if (likely(op->mempool != NULL)) {
312 
313  if (likely(priv_size >= size)) {
315  return (void *)((uint8_t *)(op + 1) +
316  sizeof(struct rte_crypto_sym_op));
318  return (void *)((uint8_t *)(op + 1) +
319  sizeof(struct rte_crypto_asym_op));
320  }
321  }
322 
323  return NULL;
324 }
325 
333 static inline void
335 {
336  if (op != NULL && op->mempool != NULL)
337  rte_mempool_put(op->mempool, op);
338 }
339 
351 static inline struct rte_crypto_op *
353 {
354  if (unlikely(m == NULL))
355  return NULL;
356 
357  /*
358  * check that the mbuf's private data size is sufficient to contain a
359  * crypto operation
360  */
361  if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
362  sizeof(struct rte_crypto_sym_op))))
363  return NULL;
364 
365  /* private data starts immediately after the mbuf header in the mbuf. */
366  struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
367 
369 
370  op->mempool = NULL;
371  op->sym->m_src = m;
372 
373  return op;
374 }
375 
385 static inline struct rte_crypto_sym_xform *
386 rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
387 {
388  void *priv_data;
389  uint32_t size;
390 
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 
411 static inline int
413  struct rte_cryptodev_sym_session *sess)
414 {
416  return -1;
417 
419 
421 }
422 
429 static inline int
431  struct rte_cryptodev_asym_session *sess)
432 {
434  return -1;
435 
437  op->asym->session = sess;
438  return 0;
439 }
440 
441 #ifdef __cplusplus
442 }
443 #endif
444 
445 #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:134
enum rte_crypto_op_type type
Definition: rte_crypto.h:157
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:430
#define likely(x)
static void * __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
Definition: rte_crypto.h:306
uint8_t status
Definition: rte_crypto.h:86
uint8_t type
Definition: rte_crypto.h:84
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:218
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:276
uint32_t cache_size
Definition: rte_mempool.h:230
static int rte_crypto_op_attach_sym_session(struct rte_crypto_op *op, struct rte_cryptodev_sym_session *sess)
Definition: rte_crypto.h:412
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:386
#define unlikely(x)
uint16_t priv_size
Definition: rte_mbuf.h:728
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:173
rte_crypto_op_type
Definition: rte_crypto.h:29
static void rte_crypto_op_free(struct rte_crypto_op *op)
Definition: rte_crypto.h:334
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:1454
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:352
struct rte_crypto_asym_op asym[0]
Definition: rte_crypto.h:121
uint64_t rte_iova_t
Definition: rte_memory.h:80
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:246
static __rte_always_inline void rte_mempool_put(struct rte_mempool *mp, void *obj)
Definition: rte_mempool.h:1323
uint8_t sess_type
Definition: rte_crypto.h:94
static void * rte_mempool_get_priv(struct rte_mempool *mp)
Definition: rte_mempool.h:1636
char name[RTE_MEMZONE_NAMESIZE]
Definition: rte_mempool.h:219
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:118