test mbuf attach
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev_pmd.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020 Intel Corporation.
3  */
4
5 #ifndef _RTE_CRYPTODEV_PMD_H_
6 #define _RTE_CRYPTODEV_PMD_H_
7
8 /** @file
9  * RTE Crypto PMD APIs
10  *
11  * @note
12  * These API are from crypto PMD only and user applications should not call
13  * them directly.
14  */
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <string.h>
21
22 #include <rte_config.h>
23 #include <rte_dev.h>
24 #include <rte_malloc.h>
25 #include <rte_mbuf.h>
26 #include <rte_mempool.h>
27 #include <rte_log.h>
28 #include <rte_common.h>
29
30 #include "rte_crypto.h"
31 #include "rte_cryptodev.h"
32
33
34 #define RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS    8
35
36 #define RTE_CRYPTODEV_PMD_NAME_ARG                      ("name")
37 #define RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG                 ("max_nb_queue_pairs")
38 #define RTE_CRYPTODEV_PMD_SOCKET_ID_ARG                 ("socket_id")
39
40
41 static const char * const cryptodev_pmd_valid_params[] = {
42         RTE_CRYPTODEV_PMD_NAME_ARG,
43         RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
44         RTE_CRYPTODEV_PMD_SOCKET_ID_ARG
45 };
46
47 /**
48  * @internal
49  * Initialisation parameters for crypto devices
50  */
51 struct rte_cryptodev_pmd_init_params {
52         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
53         size_t private_data_size;
54         int socket_id;
55         unsigned int max_nb_queue_pairs;
56 };
57
58 /** Global structure used for maintaining state of allocated crypto devices */
59 struct rte_cryptodev_global {
60         struct rte_cryptodev *devs;     /**< Device information array */
61         struct rte_cryptodev_data *data[RTE_CRYPTO_MAX_DEVS];
62         /**< Device private data */
63         uint8_t nb_devs;                /**< Number of devices found */
64 };
65
66 /* Cryptodev driver, containing the driver ID */
67 struct cryptodev_driver {
68         TAILQ_ENTRY(cryptodev_driver) next; /**< Next in list. */
69         const struct rte_driver *driver;
70         uint8_t id;
71 };
72
73 /**
74  * Get the rte_cryptodev structure device pointer for the device. Assumes a
75  * valid device index.
76  *
77  * @param       dev_id  Device ID value to select the device structure.
78  *
79  * @return
80  *   - The rte_cryptodev structure pointer for the given device ID.
81  */
82 struct rte_cryptodev *
83 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
84
85 /**
86  * Get the rte_cryptodev structure device pointer for the named device.
87  *
88  * @param       name    device name to select the device structure.
89  *
90  * @return
91  *   - The rte_cryptodev structure pointer for the given device ID.
92  */
93 struct rte_cryptodev *
94 rte_cryptodev_pmd_get_named_dev(const char *name);
95
96 /**
97  * Validate if the crypto device index is valid attached crypto device.
98  *
99  * @param       dev_id  Crypto device index.
100  *
101  * @return
102  *   - If the device index is valid (1) or not (0).
103  */
104 unsigned int
105 rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id);
106
107 /**
108  * The pool of rte_cryptodev structures.
109  */
110 extern struct rte_cryptodev *rte_cryptodevs;
111
112
113 /**
114  * Definitions of all functions exported by a driver through the
115  * the generic structure of type *crypto_dev_ops* supplied in the
116  * *rte_cryptodev* structure associated with a device.
117  */
118
119 /**
120  *      Function used to configure device.
121  *
122  * @param       dev     Crypto device pointer
123  *              config  Crypto device configurations
124  *
125  * @return      Returns 0 on success
126  */
127 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
128                 struct rte_cryptodev_config *config);
129
130 /**
131  * Function used to start a configured device.
132  *
133  * @param       dev     Crypto device pointer
134  *
135  * @return      Returns 0 on success
136  */
137 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
138
139 /**
140  * Function used to stop a configured device.
141  *
142  * @param       dev     Crypto device pointer
143  */
144 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
145
146 /**
147  * Function used to close a configured device.
148  *
149  * @param       dev     Crypto device pointer
150  * @return
151  * - 0 on success.
152  * - EAGAIN if can't close as device is busy
153  */
154 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
155
156
157 /**
158  * Function used to get statistics of a device.
159  *
160  * @param       dev     Crypto device pointer
161  * @param       stats   Pointer to crypto device stats structure to populate
162  */
163 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
164                                 struct rte_cryptodev_stats *stats);
165
166
167 /**
168  * Function used to reset statistics of a device.
169  *
170  * @param       dev     Crypto device pointer
171  */
172 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
173
174
175 /**
176  * Function used to get specific information of a device.
177  *
178  * @param       dev     Crypto device pointer
179  */
180 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
181                                 struct rte_cryptodev_info *dev_info);
182
183 /**
184  * Setup a queue pair for a device.
185  *
186  * @param       dev             Crypto device pointer
187  * @param       qp_id           Queue Pair Index
188  * @param       qp_conf         Queue configuration structure
189  * @param       socket_id       Socket Index
190  *
191  * @return      Returns 0 on success.
192  */
193 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
194                 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
195                 int socket_id);
196
197 /**
198  * Release memory resources allocated by given queue pair.
199  *
200  * @param       dev     Crypto device pointer
201  * @param       qp_id   Queue Pair Index
202  *
203  * @return
204  * - 0 on success.
205  * - EAGAIN if can't close as device is busy
206  */
207 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
208                 uint16_t qp_id);
209
210 /**
211  * Create a session mempool to allocate sessions from
212  *
213  * @param       dev             Crypto device pointer
214  * @param       nb_objs         number of sessions objects in mempool
215  * @param       obj_cache       l-core object cache size, see *rte_ring_create*
216  * @param       socket_id       Socket Id to allocate  mempool on.
217  *
218  * @return
219  * - On success returns a pointer to a rte_mempool
220  * - On failure returns a NULL pointer
221  */
222 typedef int (*cryptodev_sym_create_session_pool_t)(
223                 struct rte_cryptodev *dev, unsigned nb_objs,
224                 unsigned obj_cache_size, int socket_id);
225
226
227 /**
228  * Get the size of a cryptodev session
229  *
230  * @param       dev             Crypto device pointer
231  *
232  * @return
233  *  - On success returns the size of the session structure for device
234  *  - On failure returns 0
235  */
236 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
237                 struct rte_cryptodev *dev);
238 /**
239  * Get the size of a asymmetric cryptodev session
240  *
241  * @param       dev             Crypto device pointer
242  *
243  * @return
244  *  - On success returns the size of the session structure for device
245  *  - On failure returns 0
246  */
247 typedef unsigned int (*cryptodev_asym_get_session_private_size_t)(
248                 struct rte_cryptodev *dev);
249
250 /**
251  * Configure a Crypto session on a device.
252  *
253  * @param       dev             Crypto device pointer
254  * @param       xform           Single or chain of crypto xforms
255  * @param       priv_sess       Pointer to cryptodev's private session structure
256  * @param       mp              Mempool where the private session is allocated
257  *
258  * @return
259  *  - Returns 0 if private session structure have been created successfully.
260  *  - Returns -EINVAL if input parameters are invalid.
261  *  - Returns -ENOTSUP if crypto device does not support the crypto transform.
262  *  - Returns -ENOMEM if the private session could not be allocated.
263  */
264 typedef int (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
265                 struct rte_crypto_sym_xform *xform,
266                 struct rte_cryptodev_sym_session *session,
267                 struct rte_mempool *mp);
268 /**
269  * Configure a Crypto asymmetric session on a device.
270  *
271  * @param       dev             Crypto device pointer
272  * @param       xform           Single or chain of crypto xforms
273  * @param       priv_sess       Pointer to cryptodev's private session structure
274  * @param       mp              Mempool where the private session is allocated
275  *
276  * @return
277  *  - Returns 0 if private session structure have been created successfully.
278  *  - Returns -EINVAL if input parameters are invalid.
279  *  - Returns -ENOTSUP if crypto device does not support the crypto transform.
280  *  - Returns -ENOMEM if the private session could not be allocated.
281  */
282 typedef int (*cryptodev_asym_configure_session_t)(struct rte_cryptodev *dev,
283                 struct rte_crypto_asym_xform *xform,
284                 struct rte_cryptodev_asym_session *session,
285                 struct rte_mempool *mp);
286 /**
287  * Free driver private session data.
288  *
289  * @param       dev             Crypto device pointer
290  * @param       sess            Cryptodev session structure
291  */
292 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
293                 struct rte_cryptodev_sym_session *sess);
294 /**
295  * Free asymmetric session private data.
296  *
297  * @param       dev             Crypto device pointer
298  * @param       sess            Cryptodev session structure
299  */
300 typedef void (*cryptodev_asym_free_session_t)(struct rte_cryptodev *dev,
301                 struct rte_cryptodev_asym_session *sess);
302 /**
303  * Perform actual crypto processing (encrypt/digest or auth/decrypt)
304  * on user provided data.
305  *
306  * @param       dev     Crypto device pointer
307  * @param       sess    Cryptodev session structure
308  * @param       ofs     Start and stop offsets for auth and cipher operations
309  * @param       vec     Vectorized operation descriptor
310  *
311  * @return
312  *  - Returns number of successfully processed packets.
313  *
314  */
315 typedef uint32_t (*cryptodev_sym_cpu_crypto_process_t)
316         (struct rte_cryptodev *dev, struct rte_cryptodev_sym_session *sess,
317         union rte_crypto_sym_ofs ofs, struct rte_crypto_sym_vec *vec);
318
319
320 /** Crypto device operations function pointer table */
321 struct rte_cryptodev_ops {
322         cryptodev_configure_t dev_configure;    /**< Configure device. */
323         cryptodev_start_t dev_start;            /**< Start device. */
324         cryptodev_stop_t dev_stop;              /**< Stop device. */
325         cryptodev_close_t dev_close;            /**< Close device. */
326
327         cryptodev_info_get_t dev_infos_get;     /**< Get device info. */
328
329         cryptodev_stats_get_t stats_get;
330         /**< Get device statistics. */
331         cryptodev_stats_reset_t stats_reset;
332         /**< Reset device statistics. */
333
334         cryptodev_queue_pair_setup_t queue_pair_setup;
335         /**< Set up a device queue pair. */
336         cryptodev_queue_pair_release_t queue_pair_release;
337         /**< Release a queue pair. */
338
339         cryptodev_sym_get_session_private_size_t sym_session_get_size;
340         /**< Return private session. */
341         cryptodev_asym_get_session_private_size_t asym_session_get_size;
342         /**< Return asym session private size. */
343         cryptodev_sym_configure_session_t sym_session_configure;
344         /**< Configure a Crypto session. */
345         cryptodev_asym_configure_session_t asym_session_configure;
346         /**< Configure asymmetric Crypto session. */
347         cryptodev_sym_free_session_t sym_session_clear;
348         /**< Clear a Crypto sessions private data. */
349         cryptodev_asym_free_session_t asym_session_clear;
350         /**< Clear a Crypto sessions private data. */
351         cryptodev_sym_cpu_crypto_process_t sym_cpu_process;
352         /**< process input data synchronously (cpu-crypto). */
353 };
354
355
356 /**
357  * Function for internal use by dummy drivers primarily, e.g. ring-based
358  * driver.
359  * Allocates a new cryptodev slot for an crypto device and returns the pointer
360  * to that slot for the driver to use.
361  *
362  * @param       name            Unique identifier name for each device
363  * @param       socket_id       Socket to allocate resources on.
364  * @return
365  *   - Slot in the rte_dev_devices array for a new device;
366  */
367 struct rte_cryptodev *
368 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
369
370 /**
371  * Function for internal use by dummy drivers primarily, e.g. ring-based
372  * driver.
373  * Release the specified cryptodev device.
374  *
375  * @param cryptodev
376  * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
377  * @return
378  *   - 0 on success, negative on error
379  */
380 extern int
381 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
382
383
384 /**
385  * @internal
386  *
387  * PMD assist function to parse initialisation arguments for crypto driver
388  * when creating a new crypto PMD device instance.
389  *
390  * PMD driver should set default values for that PMD before calling function,
391  * these default values will be over-written with successfully parsed values
392  * from args string.
393  *
394  * @param       params  parsed PMD initialisation parameters
395  * @param       args    input argument string to parse
396  *
397  * @return
398  *  - 0 on success
399  *  - errno on failure
400  */
401 int
402 rte_cryptodev_pmd_parse_input_args(
403                 struct rte_cryptodev_pmd_init_params *params,
404                 const char *args);
405
406 /**
407  * @internal
408  *
409  * PMD assist function to provide boiler plate code for crypto driver to create
410  * and allocate resources for a new crypto PMD device instance.
411  *
412  * @param       name    crypto device name.
413  * @param       device  base device instance
414  * @param       params  PMD initialisation parameters
415  *
416  * @return
417  *  - crypto device instance on success
418  *  - NULL on creation failure
419  */
420 struct rte_cryptodev *
421 rte_cryptodev_pmd_create(const char *name,
422                 struct rte_device *device,
423                 struct rte_cryptodev_pmd_init_params *params);
424
425 /**
426  * @internal
427  *
428  * PMD assist function to provide boiler plate code for crypto driver to
429  * destroy and free resources associated with a crypto PMD device instance.
430  *
431  * @param       cryptodev       crypto device handle.
432  *
433  * @return
434  *  - 0 on success
435  *  - errno on failure
436  */
437 int
438 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev);
439
440 /**
441  * Executes all the user application registered callbacks for the specific
442  * device.
443  *  *
444  * @param       dev     Pointer to cryptodev struct
445  * @param       event   Crypto device interrupt event type.
446  *
447  * @return
448  *  void
449  */
450 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
451                                 enum rte_cryptodev_event_type event);
452
453 /**
454  * @internal
455  * Create unique device name
456  */
457 int
458 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
459
460 /**
461  * @internal
462  * Allocate Cryptodev driver.
463  *
464  * @param crypto_drv
465  *   Pointer to cryptodev_driver.
466  * @param drv
467  *   Pointer to rte_driver.
468  *
469  * @return
470  *  The driver type identifier
471  */
472 uint8_t rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
473                 const struct rte_driver *drv);
474
475
476 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(crypto_drv, drv, driver_id)\
477 RTE_INIT(init_ ##driver_id)\
478 {\
479         driver_id = rte_cryptodev_allocate_driver(&crypto_drv, &(drv));\
480 }
481
482 static inline void *
483 get_sym_session_private_data(const struct rte_cryptodev_sym_session *sess,
484                 uint8_t driver_id) {
485         if (unlikely(sess->nb_drivers <= driver_id))
486                 return NULL;
487
488         return sess->sess_data[driver_id].data;
489 }
490
491 static inline void
492 set_sym_session_private_data(struct rte_cryptodev_sym_session *sess,
493                 uint8_t driver_id, void *private_data)
494 {
495         if (unlikely(sess->nb_drivers <= driver_id)) {
496                 CDEV_LOG_ERR("Set private data for driver %u not allowed\n",
497                                 driver_id);
498                 return;
499         }
500
501         sess->sess_data[driver_id].data = private_data;
502 }
503
504 static inline void *
505 get_asym_session_private_data(const struct rte_cryptodev_asym_session *sess,
506                 uint8_t driver_id) {
507         return sess->sess_private_data[driver_id];
508 }
509
510 static inline void
511 set_asym_session_private_data(struct rte_cryptodev_asym_session *sess,
512                 uint8_t driver_id, void *private_data)
513 {
514         sess->sess_private_data[driver_id] = private_data;
515 }
516
517 #ifdef __cplusplus
518 }
519 #endif
520
521 #endif /* _RTE_CRYPTODEV_PMD_H_ */