3 * Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
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.
32 #ifndef _RTE_CRYPTODEV_H_
33 #define _RTE_CRYPTODEV_H_
36 * @file rte_cryptodev.h
38 * RTE Cryptographic Device APIs
40 * Defines RTE Crypto Device APIs for the provisioning of cipher and
41 * authentication operations.
43 * @b EXPERIMENTAL: this API may change without prior notice
51 #include "rte_crypto.h"
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 */
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 */
69 extern const char **rte_cyptodev_names;
73 #define CDEV_LOG_ERR(fmt, args...) \
74 RTE_LOG(ERR, CRYPTODEV, "%s() line %u: " fmt "\n", \
75 __func__, __LINE__, ## args)
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)
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) \
86 #define CDEV_PMD_TRACE(fmt, args...) \
87 RTE_LOG(DEBUG, CRYPTODEV, "[%s] %s: " fmt "\n", \
88 dev, __func__, ## args)
91 #define CDEV_LOG_DEBUG(fmt, args...)
92 #define CDEV_PMD_TRACE(fmt, args...)
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. */
101 unsigned max_nb_queue_pairs;
102 /**< Maximum number of queues pairs supported by device. */
105 unsigned max_nb_sessions;
106 /**< Maximum number of sessions supported by device. */
110 #define RTE_CRYPTODEV_DETACHED (0)
111 #define RTE_CRYPTODEV_ATTACHED (1)
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 */
120 /** Crypto device queue pair configuration structure. */
121 struct rte_cryptodev_qp_conf {
122 uint32_t nb_descriptors; /**< Number of descriptors per queue pair */
126 * Typedef for application callback function to be registered by application
127 * software for notification of device events
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.
134 typedef void (*rte_cryptodev_cb_fn)(uint8_t dev_id,
135 enum rte_cryptodev_event_type event, void *cb_arg);
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 */
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 */
153 * Create a virtual crypto device
155 * @param name Cryptodev PMD name of device to be created.
156 * @param args Options arguments for device.
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.
164 rte_cryptodev_create_vdev(const char *name, const char *args);
167 * Get the device identifier for the named crypto device.
169 * @param name device name to select the device structure.
172 * - Returns crypto device identifier on success.
173 * - Return -1 on failure to find named crypto device.
176 rte_cryptodev_get_dev_id(const char *name);
179 * Get the total number of crypto devices that have been successfully
183 * - The total number of usable crypto devices.
186 rte_cryptodev_count(void);
189 rte_cryptodev_count_devtype(enum rte_cryptodev_type type);
191 * Return the NUMA socket to which a device is connected
194 * The identifier of the device
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.
201 rte_cryptodev_socket_id(uint8_t dev_id);
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 */
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 */
216 * Configure a device.
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
222 * @param dev_id The identifier of the device to configure.
223 * @param config The crypto device configuration structure.
226 * - 0: Success, device configured.
227 * - <0: Error code returned by the driver configuration function.
230 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config);
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
238 * On success, all basic functions exported by the API (link status,
239 * receive/transmit, and so on) can be invoked.
242 * The identifier of the device.
244 * - 0: Success, device started.
245 * - <0: Error code of the driver device start function.
248 rte_cryptodev_start(uint8_t dev_id);
251 * Stop an device. The device can be restarted with a call to
252 * rte_cryptodev_start()
254 * @param dev_id The identifier of the device.
257 rte_cryptodev_stop(uint8_t dev_id);
260 * Close an device. The device cannot be restarted!
262 * @param dev_id The identifier of the device.
265 * - 0 on successfully closing device
266 * - <0 on failure to close device
269 rte_cryptodev_close(uint8_t dev_id);
272 * Allocate and set up a receive queue pair for a device.
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
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
291 * - 0: Success, queue pair correctly set up.
292 * - <0: Queue pair configuration failed
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);
299 * Start a specified queue pair of a device. It is used
300 * when deferred_start flag of the specified queue is true.
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().
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.
313 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id);
316 * Stop specified queue pair of a device
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().
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.
329 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id);
332 * Get the number of queue pairs on a specific crypto device
334 * @param dev_id Crypto device identifier.
336 * - The number of configured queue pairs.
339 rte_cryptodev_queue_pair_count(uint8_t dev_id);
343 * Retrieve the general I/O statistics of a device.
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.
350 * - Zero if successful.
351 * - Non-zero otherwise.
354 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats);
357 * Reset the general I/O statistics of a device.
359 * @param dev_id The identifier of the device.
362 rte_cryptodev_stats_reset(uint8_t dev_id);
365 * Retrieve the contextual information of a device.
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.
373 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info);
377 * Register a callback function for specific device id.
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
386 * - On success, zero.
387 * - On failure, a negative value.
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);
395 * Unregister a callback function for specific device id.
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
404 * - On success, zero.
405 * - On failure, a negative value.
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);
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. */
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. */
422 struct rte_cryptodev_callback;
424 /** Structure to keep track of registered callbacks */
425 TAILQ_HEAD(rte_cryptodev_cb_list, rte_cryptodev_callback);
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. */
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 */
443 enum rte_cryptodev_type dev_type;
444 /**< Crypto device type */
445 enum pmd_type pmd_type;
446 /**< PMD type - PDEV / VDEV */
448 struct rte_cryptodev_cb_list link_intr_cbs;
449 /**< User application callback for interrupts if present */
451 uint8_t attached : 1;
452 /**< Flag indicating the device is attached */
453 } __rte_cache_aligned;
456 #define RTE_CRYPTODEV_NAME_MAX_LEN (64)
457 /**< Max length of name of crypto PMD */
461 * The data part, with no function pointers, associated with each device.
463 * This structure is safe to place in shared memory to be common among
464 * different processes in a multi-process configuration.
466 struct rte_cryptodev_data {
468 /**< Device ID for this instance */
470 /**< Socket ID where memory is allocated */
471 char name[RTE_CRYPTODEV_NAME_MAX_LEN];
472 /**< Unique identifier name */
474 uint8_t dev_started : 1;
475 /**< Device state: STARTED(1)/STOPPED(0) */
477 struct rte_mempool *session_pool;
478 /**< Session memory pool */
480 /**< Array of pointers to queue pairs. */
481 uint16_t nb_queue_pairs;
482 /**< Number of device queue pairs. */
485 /**< PMD-specific private data */
486 } __rte_cache_aligned;
488 extern struct rte_cryptodev *rte_cryptodevs;
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.
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.
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.
507 * The rte_cryptodev_dequeue_burst() function does not provide any error
508 * notification to avoid the corresponding overhead.
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.
521 * - The number of operations actually dequeued, which is the number
522 * of pointers to *rte_crypto_op* structures effectively supplied to the
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)
529 struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
531 nb_ops = (*dev->dequeue_burst)
532 (dev->data->queue_pairs[qp_id], ops, nb_ops);
538 * Enqueue a burst of operations for processing on a crypto device.
540 * The rte_cryptodev_enqueue_burst() function is invoked to place
541 * crypto operations on the queue *qp_id* of the device designated by
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.
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.
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.
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
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)
572 struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
574 return (*dev->enqueue_burst)(
575 dev->data->queue_pairs[qp_id], ops, nb_ops);
579 /** Cryptodev symmetric crypto session */
580 struct rte_cryptodev_sym_session {
584 enum rte_cryptodev_type type;
585 /** Crypto Device type session created on */
586 struct rte_mempool *mp;
587 /**< Mempool session allocated from */
589 /**< Public symmetric session details */
592 /**< Private session material */
597 * Initialise a session for symmetric cryptographic operations.
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.
607 * The rte_cryptodev_session_free must be called to free allocated
608 * memory when the session is no longer required.
610 * @param dev_id The device identifier.
611 * @param xform Crypto transform chain.
615 * Pointer to the created session or NULL
617 extern struct rte_cryptodev_sym_session *
618 rte_cryptodev_sym_session_create(uint8_t dev_id,
619 struct rte_crypto_sym_xform *xform);
622 * Free the memory associated with a previously allocated session.
624 * @param dev_id The device identifier.
625 * @param session Session pointer previously allocated by
626 * *rte_cryptodev_sym_session_create*.
629 * NULL on successful freeing of session.
630 * Session pointer on failure to free session.
632 extern struct rte_cryptodev_sym_session *
633 rte_cryptodev_sym_session_free(uint8_t dev_id,
634 struct rte_cryptodev_sym_session *session);
641 #endif /* _RTE_CRYPTODEV_H_ */