aab8cffe22b86435f242301be7a5b5073efcceef
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev.h
1 /*-
2  *
3  *   Copyright(c) 2015-2016 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  * @b EXPERIMENTAL: this API may change without prior notice
44  *
45  */
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #include "rte_crypto.h"
52 #include "rte_dev.h"
53
54 #define CRYPTODEV_NAME_NULL_PMD         ("cryptodev_null_pmd")
55 /**< Null crypto PMD device name */
56 #define CRYPTODEV_NAME_AESNI_MB_PMD     ("cryptodev_aesni_mb_pmd")
57 /**< AES-NI Multi buffer PMD device name */
58 #define CRYPTODEV_NAME_QAT_SYM_PMD      ("cryptodev_qat_sym_pmd")
59 /**< Intel QAT Symmetric Crypto PMD device name */
60
61 /** Crypto device type */
62 enum rte_cryptodev_type {
63         RTE_CRYPTODEV_NULL_PMD = 1,     /**< Null crypto PMD */
64         RTE_CRYPTODEV_AESNI_MB_PMD,     /**< AES-NI multi buffer PMD */
65         RTE_CRYPTODEV_QAT_SYM_PMD,      /**< QAT PMD Symmetric Crypto */
66 };
67
68
69 extern const char **rte_cyptodev_names;
70
71 /* Logging Macros */
72
73 #define CDEV_LOG_ERR(fmt, args...)                                      \
74                 RTE_LOG(ERR, CRYPTODEV, "%s() line %u: " fmt "\n",      \
75                                 __func__, __LINE__, ## args)
76
77 #define CDEV_PMD_LOG_ERR(dev, fmt, args...)                             \
78                 RTE_LOG(ERR, CRYPTODEV, "[%s] %s() line %u: " fmt "\n", \
79                                 dev, __func__, __LINE__, ## args)
80
81 #ifdef RTE_LIBRTE_CRYPTODEV_DEBUG
82 #define CDEV_LOG_DEBUG(fmt, args...)                                    \
83                 RTE_LOG(DEBUG, CRYPTODEV, "%s() line %u: " fmt "\n",    \
84                                 __func__, __LINE__, ## args)            \
85
86 #define CDEV_PMD_TRACE(fmt, args...)                                    \
87                 RTE_LOG(DEBUG, CRYPTODEV, "[%s] %s: " fmt "\n",         \
88                                 dev, __func__, ## args)
89
90 #else
91 #define CDEV_LOG_DEBUG(fmt, args...)
92 #define CDEV_PMD_TRACE(fmt, args...)
93 #endif
94
95 /**  Crypto device information */
96 struct rte_cryptodev_info {
97         const char *driver_name;                /**< Driver name. */
98         enum rte_cryptodev_type dev_type;       /**< Device type */
99         struct rte_pci_device *pci_dev;         /**< PCI information. */
100
101         unsigned max_nb_queue_pairs;
102         /**< Maximum number of queues pairs supported by device. */
103
104         struct {
105                 unsigned max_nb_sessions;
106                 /**< Maximum number of sessions supported by device. */
107         } sym;
108 };
109
110 #define RTE_CRYPTODEV_DETACHED  (0)
111 #define RTE_CRYPTODEV_ATTACHED  (1)
112
113 /** Definitions of Crypto device event types */
114 enum rte_cryptodev_event_type {
115         RTE_CRYPTODEV_EVENT_UNKNOWN,    /**< unknown event type */
116         RTE_CRYPTODEV_EVENT_ERROR,      /**< error interrupt event */
117         RTE_CRYPTODEV_EVENT_MAX         /**< max value of this enum */
118 };
119
120 /** Crypto device queue pair configuration structure. */
121 struct rte_cryptodev_qp_conf {
122         uint32_t nb_descriptors; /**< Number of descriptors per queue pair */
123 };
124
125 /**
126  * Typedef for application callback function to be registered by application
127  * software for notification of device events
128  *
129  * @param       dev_id  Crypto device identifier
130  * @param       event   Crypto device event to register for notification of.
131  * @param       cb_arg  User specified parameter to be passed as to passed to
132  *                      users callback function.
133  */
134 typedef void (*rte_cryptodev_cb_fn)(uint8_t dev_id,
135                 enum rte_cryptodev_event_type event, void *cb_arg);
136
137
138 /** Crypto Device statistics */
139 struct rte_cryptodev_stats {
140         uint64_t enqueued_count;
141         /**< Count of all operations enqueued */
142         uint64_t dequeued_count;
143         /**< Count of all operations dequeued */
144
145         uint64_t enqueue_err_count;
146         /**< Total error count on operations enqueued */
147         uint64_t dequeue_err_count;
148         /**< Total error count on operations dequeued */
149 };
150
151
152 /**
153  * Create a virtual crypto device
154  *
155  * @param       name    Cryptodev PMD name of device to be created.
156  * @param       args    Options arguments for device.
157  *
158  * @return
159  * - On successful creation of the cryptodev the device index is returned,
160  *   which will be between 0 and rte_cryptodev_count().
161  * - In the case of a failure, returns -1.
162  */
163 extern int
164 rte_cryptodev_create_vdev(const char *name, const char *args);
165
166 /**
167  * Get the device identifier for the named crypto device.
168  *
169  * @param       name    device name to select the device structure.
170  *
171  * @return
172  *   - Returns crypto device identifier on success.
173  *   - Return -1 on failure to find named crypto device.
174  */
175 extern int
176 rte_cryptodev_get_dev_id(const char *name);
177
178 /**
179  * Get the total number of crypto devices that have been successfully
180  * initialised.
181  *
182  * @return
183  *   - The total number of usable crypto devices.
184  */
185 extern uint8_t
186 rte_cryptodev_count(void);
187
188 extern uint8_t
189 rte_cryptodev_count_devtype(enum rte_cryptodev_type type);
190 /*
191  * Return the NUMA socket to which a device is connected
192  *
193  * @param dev_id
194  *   The identifier of the device
195  * @return
196  *   The NUMA socket id to which the device is connected or
197  *   a default of zero if the socket could not be determined.
198  *   -1 if returned is the dev_id value is out of range.
199  */
200 extern int
201 rte_cryptodev_socket_id(uint8_t dev_id);
202
203 /** Crypto device configuration structure */
204 struct rte_cryptodev_config {
205         int socket_id;                  /**< Socket to allocate resources on */
206         uint16_t nb_queue_pairs;
207         /**< Number of queue pairs to configure on device */
208
209         struct {
210                 uint32_t nb_objs;       /**< Number of objects in mempool */
211                 uint32_t cache_size;    /**< l-core object cache size */
212         } session_mp;           /**< Session mempool configuration */
213 };
214
215 /**
216  * Configure a device.
217  *
218  * This function must be invoked first before any other function in the
219  * API. This function can also be re-invoked when a device is in the
220  * stopped state.
221  *
222  * @param       dev_id          The identifier of the device to configure.
223  * @param       config          The crypto device configuration structure.
224  *
225  * @return
226  *   - 0: Success, device configured.
227  *   - <0: Error code returned by the driver configuration function.
228  */
229 extern int
230 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config);
231
232 /**
233  * Start an device.
234  *
235  * The device start step is the last one and consists of setting the configured
236  * offload features and in starting the transmit and the receive units of the
237  * device.
238  * On success, all basic functions exported by the API (link status,
239  * receive/transmit, and so on) can be invoked.
240  *
241  * @param dev_id
242  *   The identifier of the device.
243  * @return
244  *   - 0: Success, device started.
245  *   - <0: Error code of the driver device start function.
246  */
247 extern int
248 rte_cryptodev_start(uint8_t dev_id);
249
250 /**
251  * Stop an device. The device can be restarted with a call to
252  * rte_cryptodev_start()
253  *
254  * @param       dev_id          The identifier of the device.
255  */
256 extern void
257 rte_cryptodev_stop(uint8_t dev_id);
258
259 /**
260  * Close an device. The device cannot be restarted!
261  *
262  * @param       dev_id          The identifier of the device.
263  *
264  * @return
265  *  - 0 on successfully closing device
266  *  - <0 on failure to close device
267  */
268 extern int
269 rte_cryptodev_close(uint8_t dev_id);
270
271 /**
272  * Allocate and set up a receive queue pair for a device.
273  *
274  *
275  * @param       dev_id          The identifier of the device.
276  * @param       queue_pair_id   The index of the queue pairs to set up. The
277  *                              value must be in the range [0, nb_queue_pair
278  *                              - 1] previously supplied to
279  *                              rte_cryptodev_configure().
280  * @param       qp_conf         The pointer to the configuration data to be
281  *                              used for the queue pair. NULL value is
282  *                              allowed, in which case default configuration
283  *                              will be used.
284  * @param       socket_id       The *socket_id* argument is the socket
285  *                              identifier in case of NUMA. The value can be
286  *                              *SOCKET_ID_ANY* if there is no NUMA constraint
287  *                              for the DMA memory allocated for the receive
288  *                              queue pair.
289  *
290  * @return
291  *   - 0: Success, queue pair correctly set up.
292  *   - <0: Queue pair configuration failed
293  */
294 extern int
295 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
296                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id);
297
298 /**
299  * Start a specified queue pair of a device. It is used
300  * when deferred_start flag of the specified queue is true.
301  *
302  * @param       dev_id          The identifier of the device
303  * @param       queue_pair_id   The index of the queue pair to start. The value
304  *                              must be in the range [0, nb_queue_pair - 1]
305  *                              previously supplied to
306  *                              rte_crypto_dev_configure().
307  * @return
308  *   - 0: Success, the transmit queue is correctly set up.
309  *   - -EINVAL: The dev_id or the queue_id out of range.
310  *   - -ENOTSUP: The function not supported in PMD driver.
311  */
312 extern int
313 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id);
314
315 /**
316  * Stop specified queue pair of a device
317  *
318  * @param       dev_id          The identifier of the device
319  * @param       queue_pair_id   The index of the queue pair to stop. The value
320  *                              must be in the range [0, nb_queue_pair - 1]
321  *                              previously supplied to
322  *                              rte_cryptodev_configure().
323  * @return
324  *   - 0: Success, the transmit queue is correctly set up.
325  *   - -EINVAL: The dev_id or the queue_id out of range.
326  *   - -ENOTSUP: The function not supported in PMD driver.
327  */
328 extern int
329 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id);
330
331 /**
332  * Get the number of queue pairs on a specific crypto device
333  *
334  * @param       dev_id          Crypto device identifier.
335  * @return
336  *   - The number of configured queue pairs.
337  */
338 extern uint16_t
339 rte_cryptodev_queue_pair_count(uint8_t dev_id);
340
341
342 /**
343  * Retrieve the general I/O statistics of a device.
344  *
345  * @param       dev_id          The identifier of the device.
346  * @param       stats           A pointer to a structure of type
347  *                              *rte_cryptodev_stats* to be filled with the
348  *                              values of device counters.
349  * @return
350  *   - Zero if successful.
351  *   - Non-zero otherwise.
352  */
353 extern int
354 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats);
355
356 /**
357  * Reset the general I/O statistics of a device.
358  *
359  * @param       dev_id          The identifier of the device.
360  */
361 extern void
362 rte_cryptodev_stats_reset(uint8_t dev_id);
363
364 /**
365  * Retrieve the contextual information of a device.
366  *
367  * @param       dev_id          The identifier of the device.
368  * @param       dev_info        A pointer to a structure of type
369  *                              *rte_cryptodev_info* to be filled with the
370  *                              contextual information of the device.
371  */
372 extern void
373 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info);
374
375
376 /**
377  * Register a callback function for specific device id.
378  *
379  * @param       dev_id          Device id.
380  * @param       event           Event interested.
381  * @param       cb_fn           User supplied callback function to be called.
382  * @param       cb_arg          Pointer to the parameters for the registered
383  *                              callback.
384  *
385  * @return
386  *  - On success, zero.
387  *  - On failure, a negative value.
388  */
389 extern int
390 rte_cryptodev_callback_register(uint8_t dev_id,
391                 enum rte_cryptodev_event_type event,
392                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
393
394 /**
395  * Unregister a callback function for specific device id.
396  *
397  * @param       dev_id          The device identifier.
398  * @param       event           Event interested.
399  * @param       cb_fn           User supplied callback function to be called.
400  * @param       cb_arg          Pointer to the parameters for the registered
401  *                              callback.
402  *
403  * @return
404  *  - On success, zero.
405  *  - On failure, a negative value.
406  */
407 extern int
408 rte_cryptodev_callback_unregister(uint8_t dev_id,
409                 enum rte_cryptodev_event_type event,
410                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
411
412
413 typedef uint16_t (*dequeue_pkt_burst_t)(void *qp,
414                 struct rte_crypto_op **ops,     uint16_t nb_ops);
415 /**< Dequeue processed packets from queue pair of a device. */
416
417 typedef uint16_t (*enqueue_pkt_burst_t)(void *qp,
418                 struct rte_crypto_op **ops,     uint16_t nb_ops);
419 /**< Enqueue packets for processing on queue pair of a device. */
420
421
422 struct rte_cryptodev_callback;
423
424 /** Structure to keep track of registered callbacks */
425 TAILQ_HEAD(rte_cryptodev_cb_list, rte_cryptodev_callback);
426
427 /** The data structure associated with each crypto device. */
428 struct rte_cryptodev {
429         dequeue_pkt_burst_t dequeue_burst;
430         /**< Pointer to PMD receive function. */
431         enqueue_pkt_burst_t enqueue_burst;
432         /**< Pointer to PMD transmit function. */
433
434         const struct rte_cryptodev_driver *driver;
435         /**< Driver for this device */
436         struct rte_cryptodev_data *data;
437         /**< Pointer to device data */
438         struct rte_cryptodev_ops *dev_ops;
439         /**< Functions exported by PMD */
440         struct rte_pci_device *pci_dev;
441         /**< PCI info. supplied by probing */
442
443         enum rte_cryptodev_type dev_type;
444         /**< Crypto device type */
445         enum pmd_type pmd_type;
446         /**< PMD type - PDEV / VDEV */
447
448         struct rte_cryptodev_cb_list link_intr_cbs;
449         /**< User application callback for interrupts if present */
450
451         uint8_t attached : 1;
452         /**< Flag indicating the device is attached */
453 } __rte_cache_aligned;
454
455
456 #define RTE_CRYPTODEV_NAME_MAX_LEN      (64)
457 /**< Max length of name of crypto PMD */
458
459 /**
460  *
461  * The data part, with no function pointers, associated with each device.
462  *
463  * This structure is safe to place in shared memory to be common among
464  * different processes in a multi-process configuration.
465  */
466 struct rte_cryptodev_data {
467         uint8_t dev_id;
468         /**< Device ID for this instance */
469         uint8_t socket_id;
470         /**< Socket ID where memory is allocated */
471         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
472         /**< Unique identifier name */
473
474         uint8_t dev_started : 1;
475         /**< Device state: STARTED(1)/STOPPED(0) */
476
477         struct rte_mempool *session_pool;
478         /**< Session memory pool */
479         void **queue_pairs;
480         /**< Array of pointers to queue pairs. */
481         uint16_t nb_queue_pairs;
482         /**< Number of device queue pairs. */
483
484         void *dev_private;
485         /**< PMD-specific private data */
486 } __rte_cache_aligned;
487
488 extern struct rte_cryptodev *rte_cryptodevs;
489 /**
490  *
491  * Dequeue a burst of processed crypto operations from a queue on the crypto
492  * device. The dequeued operation are stored in *rte_crypto_op* structures
493  * whose pointers are supplied in the *ops* array.
494  *
495  * The rte_cryptodev_dequeue_burst() function returns the number of ops
496  * actually dequeued, which is the number of *rte_crypto_op* data structures
497  * effectively supplied into the *ops* array.
498  *
499  * A return value equal to *nb_ops* indicates that the queue contained
500  * at least *nb_ops* operations, and this is likely to signify that other
501  * processed operations remain in the devices output queue. Applications
502  * implementing a "retrieve as many processed operations as possible" policy
503  * can check this specific case and keep invoking the
504  * rte_cryptodev_dequeue_burst() function until a value less than
505  * *nb_ops* is returned.
506  *
507  * The rte_cryptodev_dequeue_burst() function does not provide any error
508  * notification to avoid the corresponding overhead.
509  *
510  * @param       dev_id          The symmetric crypto device identifier
511  * @param       qp_id           The index of the queue pair from which to
512  *                              retrieve processed packets. The value must be
513  *                              in the range [0, nb_queue_pair - 1] previously
514  *                              supplied to rte_cryptodev_configure().
515  * @param       ops             The address of an array of pointers to
516  *                              *rte_crypto_op* structures that must be
517  *                              large enough to store *nb_ops* pointers in it.
518  * @param       nb_ops          The maximum number of operations to dequeue.
519  *
520  * @return
521  *   - The number of operations actually dequeued, which is the number
522  *   of pointers to *rte_crypto_op* structures effectively supplied to the
523  *   *ops* array.
524  */
525 static inline uint16_t
526 rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
527                 struct rte_crypto_op **ops, uint16_t nb_ops)
528 {
529         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
530
531         nb_ops = (*dev->dequeue_burst)
532                         (dev->data->queue_pairs[qp_id], ops, nb_ops);
533
534         return nb_ops;
535 }
536
537 /**
538  * Enqueue a burst of operations for processing on a crypto device.
539  *
540  * The rte_cryptodev_enqueue_burst() function is invoked to place
541  * crypto operations on the queue *qp_id* of the device designated by
542  * its *dev_id*.
543  *
544  * The *nb_ops* parameter is the number of operations to process which are
545  * supplied in the *ops* array of *rte_crypto_op* structures.
546  *
547  * The rte_cryptodev_enqueue_burst() function returns the number of
548  * operations it actually enqueued for processing. A return value equal to
549  * *nb_ops* means that all packets have been enqueued.
550  *
551  * @param       dev_id          The identifier of the device.
552  * @param       qp_id           The index of the queue pair which packets are
553  *                              to be enqueued for processing. The value
554  *                              must be in the range [0, nb_queue_pairs - 1]
555  *                              previously supplied to
556  *                               *rte_cryptodev_configure*.
557  * @param       ops             The address of an array of *nb_ops* pointers
558  *                              to *rte_crypto_op* structures which contain
559  *                              the crypto operations to be processed.
560  * @param       nb_ops          The number of operations to process.
561  *
562  * @return
563  * The number of operations actually enqueued on the crypto device. The return
564  * value can be less than the value of the *nb_ops* parameter when the
565  * crypto devices queue is full or if invalid parameters are specified in
566  * a *rte_crypto_op*.
567  */
568 static inline uint16_t
569 rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
570                 struct rte_crypto_op **ops, uint16_t nb_ops)
571 {
572         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
573
574         return (*dev->enqueue_burst)(
575                         dev->data->queue_pairs[qp_id], ops, nb_ops);
576 }
577
578
579 /** Cryptodev symmetric crypto session */
580 struct rte_cryptodev_sym_session {
581         struct {
582                 uint8_t dev_id;
583                 /**< Device Id */
584                 enum rte_cryptodev_type type;
585                 /** Crypto Device type session created on */
586                 struct rte_mempool *mp;
587                 /**< Mempool session allocated from */
588         } __rte_aligned(8);
589         /**< Public symmetric session details */
590
591         char _private[0];
592         /**< Private session material */
593 };
594
595
596 /**
597  * Initialise a session for symmetric cryptographic operations.
598  *
599  * This function is used by the client to initialize immutable
600  * parameters of symmetric cryptographic operation.
601  * To perform the operation the rte_cryptodev_enqueue_burst function is
602  * used.  Each mbuf should contain a reference to the session
603  * pointer returned from this function contained within it's crypto_op if a
604  * session-based operation is being provisioned. Memory to contain the session
605  * information is allocated from within mempool managed by the cryptodev.
606  *
607  * The rte_cryptodev_session_free must be called to free allocated
608  * memory when the session is no longer required.
609  *
610  * @param       dev_id          The device identifier.
611  * @param       xform           Crypto transform chain.
612
613  *
614  * @return
615  *  Pointer to the created session or NULL
616  */
617 extern struct rte_cryptodev_sym_session *
618 rte_cryptodev_sym_session_create(uint8_t dev_id,
619                 struct rte_crypto_sym_xform *xform);
620
621 /**
622  * Free the memory associated with a previously allocated session.
623  *
624  * @param       dev_id          The device identifier.
625  * @param       session         Session pointer previously allocated by
626  *                              *rte_cryptodev_sym_session_create*.
627  *
628  * @return
629  *   NULL on successful freeing of session.
630  *   Session pointer on failure to free session.
631  */
632 extern struct rte_cryptodev_sym_session *
633 rte_cryptodev_sym_session_free(uint8_t dev_id,
634                 struct rte_cryptodev_sym_session *session);
635
636
637 #ifdef __cplusplus
638 }
639 #endif
640
641 #endif /* _RTE_CRYPTODEV_H_ */