cryptodev: support security APIs
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev.h
1 /*-
2  *
3  *   Copyright(c) 2015-2017 Intel Corporation. All rights reserved.
4  *
5  *   Redistribution and use in source and binary forms, with or without
6  *   modification, are permitted provided that the following conditions
7  *   are met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above copyright
12  *       notice, this list of conditions and the following disclaimer in
13  *       the documentation and/or other materials provided with the
14  *       distribution.
15  *     * Neither the name of Intel Corporation nor the names of its
16  *       contributors may be used to endorse or promote products derived
17  *       from this software without specific prior written permission.
18  *
19  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef _RTE_CRYPTODEV_H_
33 #define _RTE_CRYPTODEV_H_
34
35 /**
36  * @file rte_cryptodev.h
37  *
38  * RTE Cryptographic Device APIs
39  *
40  * Defines RTE Crypto Device APIs for the provisioning of cipher and
41  * authentication operations.
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include "rte_kvargs.h"
49 #include "rte_crypto.h"
50 #include "rte_dev.h"
51 #include <rte_common.h>
52
53 extern const char **rte_cyptodev_names;
54
55 /* Logging Macros */
56
57 #define CDEV_LOG_ERR(...) \
58         RTE_LOG(ERR, CRYPTODEV, \
59                 RTE_FMT("%s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
60                         __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,)))
61
62 #define CDEV_LOG_INFO(...) \
63         RTE_LOG(INFO, CRYPTODEV, \
64                 RTE_FMT(RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
65                         RTE_FMT_TAIL(__VA_ARGS__,)))
66
67 #ifdef RTE_LIBRTE_CRYPTODEV_DEBUG
68 #define CDEV_LOG_DEBUG(...) \
69         RTE_LOG(DEBUG, CRYPTODEV, \
70                 RTE_FMT("%s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
71                         __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,)))
72
73 #define CDEV_PMD_TRACE(...) \
74         RTE_LOG(DEBUG, CRYPTODEV, \
75                 RTE_FMT("[%s] %s: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
76                         dev, __func__, RTE_FMT_TAIL(__VA_ARGS__,)))
77
78 #else
79 #define CDEV_LOG_DEBUG(...) (void)0
80 #define CDEV_PMD_TRACE(...) (void)0
81 #endif
82
83
84
85 /**
86  * A macro that points to an offset from the start
87  * of the crypto operation structure (rte_crypto_op)
88  *
89  * The returned pointer is cast to type t.
90  *
91  * @param c
92  *   The crypto operation.
93  * @param o
94  *   The offset from the start of the crypto operation.
95  * @param t
96  *   The type to cast the result into.
97  */
98 #define rte_crypto_op_ctod_offset(c, t, o)      \
99         ((t)((char *)(c) + (o)))
100
101 /**
102  * A macro that returns the physical address that points
103  * to an offset from the start of the crypto operation
104  * (rte_crypto_op)
105  *
106  * @param c
107  *   The crypto operation.
108  * @param o
109  *   The offset from the start of the crypto operation
110  *   to calculate address from.
111  */
112 #define rte_crypto_op_ctophys_offset(c, o)      \
113         (phys_addr_t)((c)->phys_addr + (o))
114
115 /**
116  * Crypto parameters range description
117  */
118 struct rte_crypto_param_range {
119         uint16_t min;   /**< minimum size */
120         uint16_t max;   /**< maximum size */
121         uint16_t increment;
122         /**< if a range of sizes are supported,
123          * this parameter is used to indicate
124          * increments in byte size that are supported
125          * between the minimum and maximum
126          */
127 };
128
129 /**
130  * Symmetric Crypto Capability
131  */
132 struct rte_cryptodev_symmetric_capability {
133         enum rte_crypto_sym_xform_type xform_type;
134         /**< Transform type : Authentication / Cipher / AEAD */
135         RTE_STD_C11
136         union {
137                 struct {
138                         enum rte_crypto_auth_algorithm algo;
139                         /**< authentication algorithm */
140                         uint16_t block_size;
141                         /**< algorithm block size */
142                         struct rte_crypto_param_range key_size;
143                         /**< auth key size range */
144                         struct rte_crypto_param_range digest_size;
145                         /**< digest size range */
146                         struct rte_crypto_param_range aad_size;
147                         /**< Additional authentication data size range */
148                         struct rte_crypto_param_range iv_size;
149                         /**< Initialisation vector data size range */
150                 } auth;
151                 /**< Symmetric Authentication transform capabilities */
152                 struct {
153                         enum rte_crypto_cipher_algorithm algo;
154                         /**< cipher algorithm */
155                         uint16_t block_size;
156                         /**< algorithm block size */
157                         struct rte_crypto_param_range key_size;
158                         /**< cipher key size range */
159                         struct rte_crypto_param_range iv_size;
160                         /**< Initialisation vector data size range */
161                 } cipher;
162                 /**< Symmetric Cipher transform capabilities */
163                 struct {
164                         enum rte_crypto_aead_algorithm algo;
165                         /**< AEAD algorithm */
166                         uint16_t block_size;
167                         /**< algorithm block size */
168                         struct rte_crypto_param_range key_size;
169                         /**< AEAD key size range */
170                         struct rte_crypto_param_range digest_size;
171                         /**< digest size range */
172                         struct rte_crypto_param_range aad_size;
173                         /**< Additional authentication data size range */
174                         struct rte_crypto_param_range iv_size;
175                         /**< Initialisation vector data size range */
176                 } aead;
177         };
178 };
179
180 /** Structure used to capture a capability of a crypto device */
181 struct rte_cryptodev_capabilities {
182         enum rte_crypto_op_type op;
183         /**< Operation type */
184
185         RTE_STD_C11
186         union {
187                 struct rte_cryptodev_symmetric_capability sym;
188                 /**< Symmetric operation capability parameters */
189         };
190 };
191
192 /** Structure used to describe crypto algorithms */
193 struct rte_cryptodev_sym_capability_idx {
194         enum rte_crypto_sym_xform_type type;
195         union {
196                 enum rte_crypto_cipher_algorithm cipher;
197                 enum rte_crypto_auth_algorithm auth;
198                 enum rte_crypto_aead_algorithm aead;
199         } algo;
200 };
201
202 /**
203  *  Provide capabilities available for defined device and algorithm
204  *
205  * @param       dev_id          The identifier of the device.
206  * @param       idx             Description of crypto algorithms.
207  *
208  * @return
209  *   - Return description of the symmetric crypto capability if exist.
210  *   - Return NULL if the capability not exist.
211  */
212 const struct rte_cryptodev_symmetric_capability *
213 rte_cryptodev_sym_capability_get(uint8_t dev_id,
214                 const struct rte_cryptodev_sym_capability_idx *idx);
215
216 /**
217  * Check if key size and initial vector are supported
218  * in crypto cipher capability
219  *
220  * @param       capability      Description of the symmetric crypto capability.
221  * @param       key_size        Cipher key size.
222  * @param       iv_size         Cipher initial vector size.
223  *
224  * @return
225  *   - Return 0 if the parameters are in range of the capability.
226  *   - Return -1 if the parameters are out of range of the capability.
227  */
228 int
229 rte_cryptodev_sym_capability_check_cipher(
230                 const struct rte_cryptodev_symmetric_capability *capability,
231                 uint16_t key_size, uint16_t iv_size);
232
233 /**
234  * Check if key size and initial vector are supported
235  * in crypto auth capability
236  *
237  * @param       capability      Description of the symmetric crypto capability.
238  * @param       key_size        Auth key size.
239  * @param       digest_size     Auth digest size.
240  * @param       iv_size         Auth initial vector size.
241  *
242  * @return
243  *   - Return 0 if the parameters are in range of the capability.
244  *   - Return -1 if the parameters are out of range of the capability.
245  */
246 int
247 rte_cryptodev_sym_capability_check_auth(
248                 const struct rte_cryptodev_symmetric_capability *capability,
249                 uint16_t key_size, uint16_t digest_size, uint16_t iv_size);
250
251 /**
252  * Check if key, digest, AAD and initial vector sizes are supported
253  * in crypto AEAD capability
254  *
255  * @param       capability      Description of the symmetric crypto capability.
256  * @param       key_size        AEAD key size.
257  * @param       digest_size     AEAD digest size.
258  * @param       aad_size        AEAD AAD size.
259  * @param       iv_size         AEAD IV size.
260  *
261  * @return
262  *   - Return 0 if the parameters are in range of the capability.
263  *   - Return -1 if the parameters are out of range of the capability.
264  */
265 int
266 rte_cryptodev_sym_capability_check_aead(
267                 const struct rte_cryptodev_symmetric_capability *capability,
268                 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
269                 uint16_t iv_size);
270
271 /**
272  * Provide the cipher algorithm enum, given an algorithm string
273  *
274  * @param       algo_enum       A pointer to the cipher algorithm
275  *                              enum to be filled
276  * @param       algo_string     Authentication algo string
277  *
278  * @return
279  * - Return -1 if string is not valid
280  * - Return 0 is the string is valid
281  */
282 int
283 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
284                 const char *algo_string);
285
286 /**
287  * Provide the authentication algorithm enum, given an algorithm string
288  *
289  * @param       algo_enum       A pointer to the authentication algorithm
290  *                              enum to be filled
291  * @param       algo_string     Authentication algo string
292  *
293  * @return
294  * - Return -1 if string is not valid
295  * - Return 0 is the string is valid
296  */
297 int
298 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
299                 const char *algo_string);
300
301 /**
302  * Provide the AEAD algorithm enum, given an algorithm string
303  *
304  * @param       algo_enum       A pointer to the AEAD algorithm
305  *                              enum to be filled
306  * @param       algo_string     AEAD algorithm string
307  *
308  * @return
309  * - Return -1 if string is not valid
310  * - Return 0 is the string is valid
311  */
312 int
313 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
314                 const char *algo_string);
315
316 /** Macro used at end of crypto PMD list */
317 #define RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST() \
318         { RTE_CRYPTO_OP_TYPE_UNDEFINED }
319
320
321 /**
322  * Crypto device supported feature flags
323  *
324  * Note:
325  * New features flags should be added to the end of the list
326  *
327  * Keep these flags synchronised with rte_cryptodev_get_feature_name()
328  */
329 #define RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO       (1ULL << 0)
330 /**< Symmetric crypto operations are supported */
331 #define RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO      (1ULL << 1)
332 /**< Asymmetric crypto operations are supported */
333 #define RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING (1ULL << 2)
334 /**< Chaining symmetric crypto operations are supported */
335 #define RTE_CRYPTODEV_FF_CPU_SSE                (1ULL << 3)
336 /**< Utilises CPU SIMD SSE instructions */
337 #define RTE_CRYPTODEV_FF_CPU_AVX                (1ULL << 4)
338 /**< Utilises CPU SIMD AVX instructions */
339 #define RTE_CRYPTODEV_FF_CPU_AVX2               (1ULL << 5)
340 /**< Utilises CPU SIMD AVX2 instructions */
341 #define RTE_CRYPTODEV_FF_CPU_AESNI              (1ULL << 6)
342 /**< Utilises CPU AES-NI instructions */
343 #define RTE_CRYPTODEV_FF_HW_ACCELERATED         (1ULL << 7)
344 /**< Operations are off-loaded to an external hardware accelerator */
345 #define RTE_CRYPTODEV_FF_CPU_AVX512             (1ULL << 8)
346 /**< Utilises CPU SIMD AVX512 instructions */
347 #define RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER    (1ULL << 9)
348 /**< Scatter-gather mbufs are supported */
349 #define RTE_CRYPTODEV_FF_CPU_NEON               (1ULL << 10)
350 /**< Utilises CPU NEON instructions */
351 #define RTE_CRYPTODEV_FF_CPU_ARM_CE             (1ULL << 11)
352 /**< Utilises ARM CPU Cryptographic Extensions */
353 #define RTE_CRYPTODEV_FF_SECURITY               (1ULL << 12)
354 /**< Support Security Protocol Processing */
355
356
357 /**
358  * Get the name of a crypto device feature flag
359  *
360  * @param       flag    The mask describing the flag.
361  *
362  * @return
363  *   The name of this flag, or NULL if it's not a valid feature flag.
364  */
365
366 extern const char *
367 rte_cryptodev_get_feature_name(uint64_t flag);
368
369 /**  Crypto device information */
370 struct rte_cryptodev_info {
371         const char *driver_name;                /**< Driver name. */
372         uint8_t driver_id;                      /**< Driver identifier */
373         struct rte_pci_device *pci_dev;         /**< PCI information. */
374
375         uint64_t feature_flags;                 /**< Feature flags */
376
377         const struct rte_cryptodev_capabilities *capabilities;
378         /**< Array of devices supported capabilities */
379
380         unsigned max_nb_queue_pairs;
381         /**< Maximum number of queues pairs supported by device. */
382
383         struct {
384                 unsigned max_nb_sessions;
385                 /**< Maximum number of sessions supported by device. */
386                 unsigned int max_nb_sessions_per_qp;
387                 /**< Maximum number of sessions per queue pair.
388                  * Default 0 for infinite sessions
389                  */
390         } sym;
391 };
392
393 #define RTE_CRYPTODEV_DETACHED  (0)
394 #define RTE_CRYPTODEV_ATTACHED  (1)
395
396 /** Definitions of Crypto device event types */
397 enum rte_cryptodev_event_type {
398         RTE_CRYPTODEV_EVENT_UNKNOWN,    /**< unknown event type */
399         RTE_CRYPTODEV_EVENT_ERROR,      /**< error interrupt event */
400         RTE_CRYPTODEV_EVENT_MAX         /**< max value of this enum */
401 };
402
403 /** Crypto device queue pair configuration structure. */
404 struct rte_cryptodev_qp_conf {
405         uint32_t nb_descriptors; /**< Number of descriptors per queue pair */
406 };
407
408 /**
409  * Typedef for application callback function to be registered by application
410  * software for notification of device events
411  *
412  * @param       dev_id  Crypto device identifier
413  * @param       event   Crypto device event to register for notification of.
414  * @param       cb_arg  User specified parameter to be passed as to passed to
415  *                      users callback function.
416  */
417 typedef void (*rte_cryptodev_cb_fn)(uint8_t dev_id,
418                 enum rte_cryptodev_event_type event, void *cb_arg);
419
420
421 /** Crypto Device statistics */
422 struct rte_cryptodev_stats {
423         uint64_t enqueued_count;
424         /**< Count of all operations enqueued */
425         uint64_t dequeued_count;
426         /**< Count of all operations dequeued */
427
428         uint64_t enqueue_err_count;
429         /**< Total error count on operations enqueued */
430         uint64_t dequeue_err_count;
431         /**< Total error count on operations dequeued */
432 };
433
434 #define RTE_CRYPTODEV_NAME_MAX_LEN      (64)
435 /**< Max length of name of crypto PMD */
436
437 /**
438  * @deprecated
439  *
440  * Create a virtual crypto device
441  *
442  * @param       name    Cryptodev PMD name of device to be created.
443  * @param       args    Options arguments for device.
444  *
445  * @return
446  * - On successful creation of the cryptodev the device index is returned,
447  *   which will be between 0 and rte_cryptodev_count().
448  * - In the case of a failure, returns -1.
449  */
450 __rte_deprecated
451 extern int
452 rte_cryptodev_create_vdev(const char *name, const char *args);
453
454 /**
455  * Get the device identifier for the named crypto device.
456  *
457  * @param       name    device name to select the device structure.
458  *
459  * @return
460  *   - Returns crypto device identifier on success.
461  *   - Return -1 on failure to find named crypto device.
462  */
463 extern int
464 rte_cryptodev_get_dev_id(const char *name);
465
466 /**
467  * Get the crypto device name given a device identifier.
468  *
469  * @param dev_id
470  *   The identifier of the device
471  *
472  * @return
473  *   - Returns crypto device name.
474  *   - Returns NULL if crypto device is not present.
475  */
476 extern const char *
477 rte_cryptodev_name_get(uint8_t dev_id);
478
479 /**
480  * Get the total number of crypto devices that have been successfully
481  * initialised.
482  *
483  * @return
484  *   - The total number of usable crypto devices.
485  */
486 extern uint8_t
487 rte_cryptodev_count(void);
488
489 /**
490  * Get number of crypto device defined type.
491  *
492  * @param       driver_id       driver identifier.
493  *
494  * @return
495  *   Returns number of crypto device.
496  */
497 extern uint8_t
498 rte_cryptodev_device_count_by_driver(uint8_t driver_id);
499
500 /**
501  * Get number and identifiers of attached crypto devices that
502  * use the same crypto driver.
503  *
504  * @param       driver_name     driver name.
505  * @param       devices         output devices identifiers.
506  * @param       nb_devices      maximal number of devices.
507  *
508  * @return
509  *   Returns number of attached crypto device.
510  */
511 uint8_t
512 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
513                 uint8_t nb_devices);
514 /*
515  * Return the NUMA socket to which a device is connected
516  *
517  * @param dev_id
518  *   The identifier of the device
519  * @return
520  *   The NUMA socket id to which the device is connected or
521  *   a default of zero if the socket could not be determined.
522  *   -1 if returned is the dev_id value is out of range.
523  */
524 extern int
525 rte_cryptodev_socket_id(uint8_t dev_id);
526
527 /** Crypto device configuration structure */
528 struct rte_cryptodev_config {
529         int socket_id;                  /**< Socket to allocate resources on */
530         uint16_t nb_queue_pairs;
531         /**< Number of queue pairs to configure on device */
532 };
533
534 /**
535  * Configure a device.
536  *
537  * This function must be invoked first before any other function in the
538  * API. This function can also be re-invoked when a device is in the
539  * stopped state.
540  *
541  * @param       dev_id          The identifier of the device to configure.
542  * @param       config          The crypto device configuration structure.
543  *
544  * @return
545  *   - 0: Success, device configured.
546  *   - <0: Error code returned by the driver configuration function.
547  */
548 extern int
549 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config);
550
551 /**
552  * Start an device.
553  *
554  * The device start step is the last one and consists of setting the configured
555  * offload features and in starting the transmit and the receive units of the
556  * device.
557  * On success, all basic functions exported by the API (link status,
558  * receive/transmit, and so on) can be invoked.
559  *
560  * @param dev_id
561  *   The identifier of the device.
562  * @return
563  *   - 0: Success, device started.
564  *   - <0: Error code of the driver device start function.
565  */
566 extern int
567 rte_cryptodev_start(uint8_t dev_id);
568
569 /**
570  * Stop an device. The device can be restarted with a call to
571  * rte_cryptodev_start()
572  *
573  * @param       dev_id          The identifier of the device.
574  */
575 extern void
576 rte_cryptodev_stop(uint8_t dev_id);
577
578 /**
579  * Close an device. The device cannot be restarted!
580  *
581  * @param       dev_id          The identifier of the device.
582  *
583  * @return
584  *  - 0 on successfully closing device
585  *  - <0 on failure to close device
586  */
587 extern int
588 rte_cryptodev_close(uint8_t dev_id);
589
590 /**
591  * Allocate and set up a receive queue pair for a device.
592  *
593  *
594  * @param       dev_id          The identifier of the device.
595  * @param       queue_pair_id   The index of the queue pairs to set up. The
596  *                              value must be in the range [0, nb_queue_pair
597  *                              - 1] previously supplied to
598  *                              rte_cryptodev_configure().
599  * @param       qp_conf         The pointer to the configuration data to be
600  *                              used for the queue pair. NULL value is
601  *                              allowed, in which case default configuration
602  *                              will be used.
603  * @param       socket_id       The *socket_id* argument is the socket
604  *                              identifier in case of NUMA. The value can be
605  *                              *SOCKET_ID_ANY* if there is no NUMA constraint
606  *                              for the DMA memory allocated for the receive
607  *                              queue pair.
608  * @param       session_pool    Pointer to device session mempool, used
609  *                              for session-less operations.
610  *
611  * @return
612  *   - 0: Success, queue pair correctly set up.
613  *   - <0: Queue pair configuration failed
614  */
615 extern int
616 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
617                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id,
618                 struct rte_mempool *session_pool);
619
620 /**
621  * Start a specified queue pair of a device. It is used
622  * when deferred_start flag of the specified queue is true.
623  *
624  * @param       dev_id          The identifier of the device
625  * @param       queue_pair_id   The index of the queue pair to start. The value
626  *                              must be in the range [0, nb_queue_pair - 1]
627  *                              previously supplied to
628  *                              rte_crypto_dev_configure().
629  * @return
630  *   - 0: Success, the transmit queue is correctly set up.
631  *   - -EINVAL: The dev_id or the queue_id out of range.
632  *   - -ENOTSUP: The function not supported in PMD driver.
633  */
634 extern int
635 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id);
636
637 /**
638  * Stop specified queue pair of a device
639  *
640  * @param       dev_id          The identifier of the device
641  * @param       queue_pair_id   The index of the queue pair to stop. The value
642  *                              must be in the range [0, nb_queue_pair - 1]
643  *                              previously supplied to
644  *                              rte_cryptodev_configure().
645  * @return
646  *   - 0: Success, the transmit queue is correctly set up.
647  *   - -EINVAL: The dev_id or the queue_id out of range.
648  *   - -ENOTSUP: The function not supported in PMD driver.
649  */
650 extern int
651 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id);
652
653 /**
654  * Get the number of queue pairs on a specific crypto device
655  *
656  * @param       dev_id          Crypto device identifier.
657  * @return
658  *   - The number of configured queue pairs.
659  */
660 extern uint16_t
661 rte_cryptodev_queue_pair_count(uint8_t dev_id);
662
663
664 /**
665  * Retrieve the general I/O statistics of a device.
666  *
667  * @param       dev_id          The identifier of the device.
668  * @param       stats           A pointer to a structure of type
669  *                              *rte_cryptodev_stats* to be filled with the
670  *                              values of device counters.
671  * @return
672  *   - Zero if successful.
673  *   - Non-zero otherwise.
674  */
675 extern int
676 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats);
677
678 /**
679  * Reset the general I/O statistics of a device.
680  *
681  * @param       dev_id          The identifier of the device.
682  */
683 extern void
684 rte_cryptodev_stats_reset(uint8_t dev_id);
685
686 /**
687  * Retrieve the contextual information of a device.
688  *
689  * @param       dev_id          The identifier of the device.
690  * @param       dev_info        A pointer to a structure of type
691  *                              *rte_cryptodev_info* to be filled with the
692  *                              contextual information of the device.
693  *
694  * @note The capabilities field of dev_info is set to point to the first
695  * element of an array of struct rte_cryptodev_capabilities. The element after
696  * the last valid element has it's op field set to
697  * RTE_CRYPTO_OP_TYPE_UNDEFINED.
698  */
699 extern void
700 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info);
701
702
703 /**
704  * Register a callback function for specific device id.
705  *
706  * @param       dev_id          Device id.
707  * @param       event           Event interested.
708  * @param       cb_fn           User supplied callback function to be called.
709  * @param       cb_arg          Pointer to the parameters for the registered
710  *                              callback.
711  *
712  * @return
713  *  - On success, zero.
714  *  - On failure, a negative value.
715  */
716 extern int
717 rte_cryptodev_callback_register(uint8_t dev_id,
718                 enum rte_cryptodev_event_type event,
719                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
720
721 /**
722  * Unregister a callback function for specific device id.
723  *
724  * @param       dev_id          The device identifier.
725  * @param       event           Event interested.
726  * @param       cb_fn           User supplied callback function to be called.
727  * @param       cb_arg          Pointer to the parameters for the registered
728  *                              callback.
729  *
730  * @return
731  *  - On success, zero.
732  *  - On failure, a negative value.
733  */
734 extern int
735 rte_cryptodev_callback_unregister(uint8_t dev_id,
736                 enum rte_cryptodev_event_type event,
737                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
738
739
740 typedef uint16_t (*dequeue_pkt_burst_t)(void *qp,
741                 struct rte_crypto_op **ops,     uint16_t nb_ops);
742 /**< Dequeue processed packets from queue pair of a device. */
743
744 typedef uint16_t (*enqueue_pkt_burst_t)(void *qp,
745                 struct rte_crypto_op **ops,     uint16_t nb_ops);
746 /**< Enqueue packets for processing on queue pair of a device. */
747
748
749
750
751 struct rte_cryptodev_callback;
752
753 /** Structure to keep track of registered callbacks */
754 TAILQ_HEAD(rte_cryptodev_cb_list, rte_cryptodev_callback);
755
756 /** The data structure associated with each crypto device. */
757 struct rte_cryptodev {
758         dequeue_pkt_burst_t dequeue_burst;
759         /**< Pointer to PMD receive function. */
760         enqueue_pkt_burst_t enqueue_burst;
761         /**< Pointer to PMD transmit function. */
762
763         struct rte_cryptodev_data *data;
764         /**< Pointer to device data */
765         struct rte_cryptodev_ops *dev_ops;
766         /**< Functions exported by PMD */
767         uint64_t feature_flags;
768         /**< Supported features */
769         struct rte_device *device;
770         /**< Backing device */
771
772         uint8_t driver_id;
773         /**< Crypto driver identifier*/
774
775         struct rte_cryptodev_cb_list link_intr_cbs;
776         /**< User application callback for interrupts if present */
777
778         void *security_ctx;
779         /**< Context for security ops */
780
781         __extension__
782         uint8_t attached : 1;
783         /**< Flag indicating the device is attached */
784 } __rte_cache_aligned;
785
786 void *
787 rte_cryptodev_get_sec_ctx(uint8_t dev_id);
788
789 /**
790  *
791  * The data part, with no function pointers, associated with each device.
792  *
793  * This structure is safe to place in shared memory to be common among
794  * different processes in a multi-process configuration.
795  */
796 struct rte_cryptodev_data {
797         uint8_t dev_id;
798         /**< Device ID for this instance */
799         uint8_t socket_id;
800         /**< Socket ID where memory is allocated */
801         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
802         /**< Unique identifier name */
803
804         __extension__
805         uint8_t dev_started : 1;
806         /**< Device state: STARTED(1)/STOPPED(0) */
807
808         struct rte_mempool *session_pool;
809         /**< Session memory pool */
810         void **queue_pairs;
811         /**< Array of pointers to queue pairs. */
812         uint16_t nb_queue_pairs;
813         /**< Number of device queue pairs. */
814
815         void *dev_private;
816         /**< PMD-specific private data */
817 } __rte_cache_aligned;
818
819 extern struct rte_cryptodev *rte_cryptodevs;
820 /**
821  *
822  * Dequeue a burst of processed crypto operations from a queue on the crypto
823  * device. The dequeued operation are stored in *rte_crypto_op* structures
824  * whose pointers are supplied in the *ops* array.
825  *
826  * The rte_cryptodev_dequeue_burst() function returns the number of ops
827  * actually dequeued, which is the number of *rte_crypto_op* data structures
828  * effectively supplied into the *ops* array.
829  *
830  * A return value equal to *nb_ops* indicates that the queue contained
831  * at least *nb_ops* operations, and this is likely to signify that other
832  * processed operations remain in the devices output queue. Applications
833  * implementing a "retrieve as many processed operations as possible" policy
834  * can check this specific case and keep invoking the
835  * rte_cryptodev_dequeue_burst() function until a value less than
836  * *nb_ops* is returned.
837  *
838  * The rte_cryptodev_dequeue_burst() function does not provide any error
839  * notification to avoid the corresponding overhead.
840  *
841  * @param       dev_id          The symmetric crypto device identifier
842  * @param       qp_id           The index of the queue pair from which to
843  *                              retrieve processed packets. The value must be
844  *                              in the range [0, nb_queue_pair - 1] previously
845  *                              supplied to rte_cryptodev_configure().
846  * @param       ops             The address of an array of pointers to
847  *                              *rte_crypto_op* structures that must be
848  *                              large enough to store *nb_ops* pointers in it.
849  * @param       nb_ops          The maximum number of operations to dequeue.
850  *
851  * @return
852  *   - The number of operations actually dequeued, which is the number
853  *   of pointers to *rte_crypto_op* structures effectively supplied to the
854  *   *ops* array.
855  */
856 static inline uint16_t
857 rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
858                 struct rte_crypto_op **ops, uint16_t nb_ops)
859 {
860         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
861
862         nb_ops = (*dev->dequeue_burst)
863                         (dev->data->queue_pairs[qp_id], ops, nb_ops);
864
865         return nb_ops;
866 }
867
868 /**
869  * Enqueue a burst of operations for processing on a crypto device.
870  *
871  * The rte_cryptodev_enqueue_burst() function is invoked to place
872  * crypto operations on the queue *qp_id* of the device designated by
873  * its *dev_id*.
874  *
875  * The *nb_ops* parameter is the number of operations to process which are
876  * supplied in the *ops* array of *rte_crypto_op* structures.
877  *
878  * The rte_cryptodev_enqueue_burst() function returns the number of
879  * operations it actually enqueued for processing. A return value equal to
880  * *nb_ops* means that all packets have been enqueued.
881  *
882  * @param       dev_id          The identifier of the device.
883  * @param       qp_id           The index of the queue pair which packets are
884  *                              to be enqueued for processing. The value
885  *                              must be in the range [0, nb_queue_pairs - 1]
886  *                              previously supplied to
887  *                               *rte_cryptodev_configure*.
888  * @param       ops             The address of an array of *nb_ops* pointers
889  *                              to *rte_crypto_op* structures which contain
890  *                              the crypto operations to be processed.
891  * @param       nb_ops          The number of operations to process.
892  *
893  * @return
894  * The number of operations actually enqueued on the crypto device. The return
895  * value can be less than the value of the *nb_ops* parameter when the
896  * crypto devices queue is full or if invalid parameters are specified in
897  * a *rte_crypto_op*.
898  */
899 static inline uint16_t
900 rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
901                 struct rte_crypto_op **ops, uint16_t nb_ops)
902 {
903         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
904
905         return (*dev->enqueue_burst)(
906                         dev->data->queue_pairs[qp_id], ops, nb_ops);
907 }
908
909
910 /** Cryptodev symmetric crypto session */
911 struct rte_cryptodev_sym_session {
912         __extension__ void *sess_private_data[0];
913         /**< Private session material */
914 };
915
916
917 /**
918  * Create symmetric crypto session header (generic with no private data)
919  *
920  * @param   mempool    Symmetric session mempool to allocate session
921  *                     objects from
922  * @return
923  *  - On success return pointer to sym-session
924  *  - On failure returns NULL
925  */
926 struct rte_cryptodev_sym_session *
927 rte_cryptodev_sym_session_create(struct rte_mempool *mempool);
928
929 /**
930  * Frees symmetric crypto session header, after checking that all
931  * the device private data has been freed, returning it
932  * to its original mempool.
933  *
934  * @param   sess     Session header to be freed.
935  *
936  * @return
937  *  - 0 if successful.
938  *  - -EINVAL if session is NULL.
939  *  - -EBUSY if not all device private data has been freed.
940  */
941 int
942 rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess);
943
944 /**
945  * Fill out private data for the device id, based on its device type.
946  *
947  * @param   dev_id   ID of device that we want the session to be used on
948  * @param   sess     Session where the private data will be attached to
949  * @param   xforms   Symmetric crypto transform operations to apply on flow
950  *                   processed with this session
951  * @param   mempool  Mempool where the private data is allocated.
952  *
953  * @return
954  *  - On success, zero.
955  *  - -EINVAL if input parameters are invalid.
956  *  - -ENOTSUP if crypto device does not support the crypto transform.
957  *  - -ENOMEM if the private session could not be allocated.
958  */
959 int
960 rte_cryptodev_sym_session_init(uint8_t dev_id,
961                         struct rte_cryptodev_sym_session *sess,
962                         struct rte_crypto_sym_xform *xforms,
963                         struct rte_mempool *mempool);
964
965 /**
966  * Frees private data for the device id, based on its device type,
967  * returning it to its mempool.
968  *
969  * @param   dev_id   ID of device that uses the session.
970  * @param   sess     Session containing the reference to the private data
971  *
972  * @return
973  *  - 0 if successful.
974  *  - -EINVAL if device is invalid or session is NULL.
975  */
976 int
977 rte_cryptodev_sym_session_clear(uint8_t dev_id,
978                         struct rte_cryptodev_sym_session *sess);
979
980 /**
981  * Get the size of the header session, for all registered drivers.
982  *
983  * @return
984  *   Size of the header session.
985  */
986 unsigned int
987 rte_cryptodev_get_header_session_size(void);
988
989 /**
990  * Get the size of the private session data for a device.
991  *
992  * @param       dev_id          The device identifier.
993  *
994  * @return
995  *   - Size of the private data, if successful
996  *   - 0 if device is invalid or does not have private session
997  */
998 unsigned int
999 rte_cryptodev_get_private_session_size(uint8_t dev_id);
1000
1001 /**
1002  * Attach queue pair with sym session.
1003  *
1004  * @param       dev_id          Device to which the session will be attached.
1005  * @param       qp_id           Queue pair to which the session will be attached.
1006  * @param       session         Session pointer previously allocated by
1007  *                              *rte_cryptodev_sym_session_create*.
1008  *
1009  * @return
1010  *  - On success, zero.
1011  *  - On failure, a negative value.
1012  */
1013 int
1014 rte_cryptodev_queue_pair_attach_sym_session(uint8_t dev_id, uint16_t qp_id,
1015                 struct rte_cryptodev_sym_session *session);
1016
1017 /**
1018  * Detach queue pair with sym session.
1019  *
1020  * @param       dev_id          Device to which the session is attached.
1021  * @param       qp_id           Queue pair to which the session is attached.
1022  * @param       session         Session pointer previously allocated by
1023  *                              *rte_cryptodev_sym_session_create*.
1024  *
1025  * @return
1026  *  - On success, zero.
1027  *  - On failure, a negative value.
1028  */
1029 int
1030 rte_cryptodev_queue_pair_detach_sym_session(uint8_t dev_id, uint16_t qp_id,
1031                 struct rte_cryptodev_sym_session *session);
1032
1033 /**
1034  * Provide driver identifier.
1035  *
1036  * @param name
1037  *   The pointer to a driver name.
1038  * @return
1039  *  The driver type identifier or -1 if no driver found
1040  */
1041 int rte_cryptodev_driver_id_get(const char *name);
1042
1043 /**
1044  * Provide driver name.
1045  *
1046  * @param driver_id
1047  *   The driver identifier.
1048  * @return
1049  *  The driver name or null if no driver found
1050  */
1051 const char *rte_cryptodev_driver_name_get(uint8_t driver_id);
1052
1053 #ifdef __cplusplus
1054 }
1055 #endif
1056
1057 #endif /* _RTE_CRYPTODEV_H_ */