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_kvargs.h"
52 #include "rte_crypto.h"
55 #define CRYPTODEV_NAME_NULL_PMD ("cryptodev_null_pmd")
56 /**< Null crypto PMD device name */
57 #define CRYPTODEV_NAME_AESNI_MB_PMD ("cryptodev_aesni_mb_pmd")
58 /**< AES-NI Multi buffer PMD device name */
59 #define CRYPTODEV_NAME_QAT_SYM_PMD ("cryptodev_qat_sym_pmd")
60 /**< Intel QAT Symmetric Crypto PMD device name */
61 #define CRYPTODEV_NAME_SNOW3G_PMD ("cryptodev_snow3g_pmd")
62 /**< SNOW 3G PMD device name */
64 /** Crypto device type */
65 enum rte_cryptodev_type {
66 RTE_CRYPTODEV_NULL_PMD = 1, /**< Null crypto PMD */
67 RTE_CRYPTODEV_AESNI_MB_PMD, /**< AES-NI multi buffer PMD */
68 RTE_CRYPTODEV_QAT_SYM_PMD, /**< QAT PMD Symmetric Crypto */
69 RTE_CRYPTODEV_SNOW3G_PMD, /**< SNOW 3G PMD */
72 extern const char **rte_cyptodev_names;
76 #define CDEV_LOG_ERR(fmt, args...) \
77 RTE_LOG(ERR, CRYPTODEV, "%s() line %u: " fmt "\n", \
78 __func__, __LINE__, ## args)
80 #define CDEV_PMD_LOG_ERR(dev, fmt, args...) \
81 RTE_LOG(ERR, CRYPTODEV, "[%s] %s() line %u: " fmt "\n", \
82 dev, __func__, __LINE__, ## args)
84 #ifdef RTE_LIBRTE_CRYPTODEV_DEBUG
85 #define CDEV_LOG_DEBUG(fmt, args...) \
86 RTE_LOG(DEBUG, CRYPTODEV, "%s() line %u: " fmt "\n", \
87 __func__, __LINE__, ## args) \
89 #define CDEV_PMD_TRACE(fmt, args...) \
90 RTE_LOG(DEBUG, CRYPTODEV, "[%s] %s: " fmt "\n", \
91 dev, __func__, ## args)
94 #define CDEV_LOG_DEBUG(fmt, args...)
95 #define CDEV_PMD_TRACE(fmt, args...)
98 /** Crypto device information */
99 struct rte_cryptodev_info {
100 const char *driver_name; /**< Driver name. */
101 enum rte_cryptodev_type dev_type; /**< Device type */
102 struct rte_pci_device *pci_dev; /**< PCI information. */
104 unsigned max_nb_queue_pairs;
105 /**< Maximum number of queues pairs supported by device. */
108 unsigned max_nb_sessions;
109 /**< Maximum number of sessions supported by device. */
113 #define RTE_CRYPTODEV_DETACHED (0)
114 #define RTE_CRYPTODEV_ATTACHED (1)
116 /** Definitions of Crypto device event types */
117 enum rte_cryptodev_event_type {
118 RTE_CRYPTODEV_EVENT_UNKNOWN, /**< unknown event type */
119 RTE_CRYPTODEV_EVENT_ERROR, /**< error interrupt event */
120 RTE_CRYPTODEV_EVENT_MAX /**< max value of this enum */
123 /** Crypto device queue pair configuration structure. */
124 struct rte_cryptodev_qp_conf {
125 uint32_t nb_descriptors; /**< Number of descriptors per queue pair */
129 * Typedef for application callback function to be registered by application
130 * software for notification of device events
132 * @param dev_id Crypto device identifier
133 * @param event Crypto device event to register for notification of.
134 * @param cb_arg User specified parameter to be passed as to passed to
135 * users callback function.
137 typedef void (*rte_cryptodev_cb_fn)(uint8_t dev_id,
138 enum rte_cryptodev_event_type event, void *cb_arg);
141 /** Crypto Device statistics */
142 struct rte_cryptodev_stats {
143 uint64_t enqueued_count;
144 /**< Count of all operations enqueued */
145 uint64_t dequeued_count;
146 /**< Count of all operations dequeued */
148 uint64_t enqueue_err_count;
149 /**< Total error count on operations enqueued */
150 uint64_t dequeue_err_count;
151 /**< Total error count on operations dequeued */
154 #define RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS 8
155 #define RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS 2048
159 * Initialisation parameters for virtual crypto devices
161 struct rte_crypto_vdev_init_params {
162 unsigned max_nb_queue_pairs;
163 unsigned max_nb_sessions;
167 #define RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG ("max_nb_queue_pairs")
168 #define RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG ("max_nb_sessions")
169 #define RTE_CRYPTODEV_VDEV_SOCKET_ID ("socket_id")
171 static const char *cryptodev_vdev_valid_params[] = {
172 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
173 RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
174 RTE_CRYPTODEV_VDEV_SOCKET_ID
177 static inline uint8_t
178 number_of_sockets(void)
182 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
184 for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
185 if (sockets < ms[i].socket_id)
186 sockets = ms[i].socket_id;
189 /* Number of sockets = maximum socket_id + 1 */
193 /** Parse integer from integer argument */
195 __rte_cryptodev_parse_integer_arg(const char *key __rte_unused,
196 const char *value, void *extra_args)
198 int *i = (int *) extra_args;
202 CDEV_LOG_ERR("Argument has to be positive.");
210 * Parse virtual device initialisation parameters input arguments
213 * @params params Initialisation parameters with defaults set.
214 * @params input_args Command line arguments
217 * 0 on successful parse
218 * <0 on failure to parse
221 rte_cryptodev_parse_vdev_init_params(struct rte_crypto_vdev_init_params *params,
222 const char *input_args)
224 struct rte_kvargs *kvlist;
231 kvlist = rte_kvargs_parse(input_args,
232 cryptodev_vdev_valid_params);
236 ret = rte_kvargs_process(kvlist,
237 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
238 &__rte_cryptodev_parse_integer_arg,
239 ¶ms->max_nb_queue_pairs);
243 ret = rte_kvargs_process(kvlist,
244 RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
245 &__rte_cryptodev_parse_integer_arg,
246 ¶ms->max_nb_sessions);
250 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
251 &__rte_cryptodev_parse_integer_arg,
256 if (params->socket_id >= number_of_sockets()) {
257 CDEV_LOG_ERR("Invalid socket id specified to create "
258 "the virtual crypto device on");
266 rte_kvargs_free(kvlist);
271 * Create a virtual crypto device
273 * @param name Cryptodev PMD name of device to be created.
274 * @param args Options arguments for device.
277 * - On successful creation of the cryptodev the device index is returned,
278 * which will be between 0 and rte_cryptodev_count().
279 * - In the case of a failure, returns -1.
282 rte_cryptodev_create_vdev(const char *name, const char *args);
285 * Get the device identifier for the named crypto device.
287 * @param name device name to select the device structure.
290 * - Returns crypto device identifier on success.
291 * - Return -1 on failure to find named crypto device.
294 rte_cryptodev_get_dev_id(const char *name);
297 * Get the total number of crypto devices that have been successfully
301 * - The total number of usable crypto devices.
304 rte_cryptodev_count(void);
307 rte_cryptodev_count_devtype(enum rte_cryptodev_type type);
309 * Return the NUMA socket to which a device is connected
312 * The identifier of the device
314 * The NUMA socket id to which the device is connected or
315 * a default of zero if the socket could not be determined.
316 * -1 if returned is the dev_id value is out of range.
319 rte_cryptodev_socket_id(uint8_t dev_id);
321 /** Crypto device configuration structure */
322 struct rte_cryptodev_config {
323 int socket_id; /**< Socket to allocate resources on */
324 uint16_t nb_queue_pairs;
325 /**< Number of queue pairs to configure on device */
328 uint32_t nb_objs; /**< Number of objects in mempool */
329 uint32_t cache_size; /**< l-core object cache size */
330 } session_mp; /**< Session mempool configuration */
334 * Configure a device.
336 * This function must be invoked first before any other function in the
337 * API. This function can also be re-invoked when a device is in the
340 * @param dev_id The identifier of the device to configure.
341 * @param config The crypto device configuration structure.
344 * - 0: Success, device configured.
345 * - <0: Error code returned by the driver configuration function.
348 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config);
353 * The device start step is the last one and consists of setting the configured
354 * offload features and in starting the transmit and the receive units of the
356 * On success, all basic functions exported by the API (link status,
357 * receive/transmit, and so on) can be invoked.
360 * The identifier of the device.
362 * - 0: Success, device started.
363 * - <0: Error code of the driver device start function.
366 rte_cryptodev_start(uint8_t dev_id);
369 * Stop an device. The device can be restarted with a call to
370 * rte_cryptodev_start()
372 * @param dev_id The identifier of the device.
375 rte_cryptodev_stop(uint8_t dev_id);
378 * Close an device. The device cannot be restarted!
380 * @param dev_id The identifier of the device.
383 * - 0 on successfully closing device
384 * - <0 on failure to close device
387 rte_cryptodev_close(uint8_t dev_id);
390 * Allocate and set up a receive queue pair for a device.
393 * @param dev_id The identifier of the device.
394 * @param queue_pair_id The index of the queue pairs to set up. The
395 * value must be in the range [0, nb_queue_pair
396 * - 1] previously supplied to
397 * rte_cryptodev_configure().
398 * @param qp_conf The pointer to the configuration data to be
399 * used for the queue pair. NULL value is
400 * allowed, in which case default configuration
402 * @param socket_id The *socket_id* argument is the socket
403 * identifier in case of NUMA. The value can be
404 * *SOCKET_ID_ANY* if there is no NUMA constraint
405 * for the DMA memory allocated for the receive
409 * - 0: Success, queue pair correctly set up.
410 * - <0: Queue pair configuration failed
413 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
414 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id);
417 * Start a specified queue pair of a device. It is used
418 * when deferred_start flag of the specified queue is true.
420 * @param dev_id The identifier of the device
421 * @param queue_pair_id The index of the queue pair to start. The value
422 * must be in the range [0, nb_queue_pair - 1]
423 * previously supplied to
424 * rte_crypto_dev_configure().
426 * - 0: Success, the transmit queue is correctly set up.
427 * - -EINVAL: The dev_id or the queue_id out of range.
428 * - -ENOTSUP: The function not supported in PMD driver.
431 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id);
434 * Stop specified queue pair of a device
436 * @param dev_id The identifier of the device
437 * @param queue_pair_id The index of the queue pair to stop. The value
438 * must be in the range [0, nb_queue_pair - 1]
439 * previously supplied to
440 * rte_cryptodev_configure().
442 * - 0: Success, the transmit queue is correctly set up.
443 * - -EINVAL: The dev_id or the queue_id out of range.
444 * - -ENOTSUP: The function not supported in PMD driver.
447 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id);
450 * Get the number of queue pairs on a specific crypto device
452 * @param dev_id Crypto device identifier.
454 * - The number of configured queue pairs.
457 rte_cryptodev_queue_pair_count(uint8_t dev_id);
461 * Retrieve the general I/O statistics of a device.
463 * @param dev_id The identifier of the device.
464 * @param stats A pointer to a structure of type
465 * *rte_cryptodev_stats* to be filled with the
466 * values of device counters.
468 * - Zero if successful.
469 * - Non-zero otherwise.
472 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats);
475 * Reset the general I/O statistics of a device.
477 * @param dev_id The identifier of the device.
480 rte_cryptodev_stats_reset(uint8_t dev_id);
483 * Retrieve the contextual information of a device.
485 * @param dev_id The identifier of the device.
486 * @param dev_info A pointer to a structure of type
487 * *rte_cryptodev_info* to be filled with the
488 * contextual information of the device.
491 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info);
495 * Register a callback function for specific device id.
497 * @param dev_id Device id.
498 * @param event Event interested.
499 * @param cb_fn User supplied callback function to be called.
500 * @param cb_arg Pointer to the parameters for the registered
504 * - On success, zero.
505 * - On failure, a negative value.
508 rte_cryptodev_callback_register(uint8_t dev_id,
509 enum rte_cryptodev_event_type event,
510 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
513 * Unregister a callback function for specific device id.
515 * @param dev_id The device identifier.
516 * @param event Event interested.
517 * @param cb_fn User supplied callback function to be called.
518 * @param cb_arg Pointer to the parameters for the registered
522 * - On success, zero.
523 * - On failure, a negative value.
526 rte_cryptodev_callback_unregister(uint8_t dev_id,
527 enum rte_cryptodev_event_type event,
528 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
531 typedef uint16_t (*dequeue_pkt_burst_t)(void *qp,
532 struct rte_crypto_op **ops, uint16_t nb_ops);
533 /**< Dequeue processed packets from queue pair of a device. */
535 typedef uint16_t (*enqueue_pkt_burst_t)(void *qp,
536 struct rte_crypto_op **ops, uint16_t nb_ops);
537 /**< Enqueue packets for processing on queue pair of a device. */
540 struct rte_cryptodev_callback;
542 /** Structure to keep track of registered callbacks */
543 TAILQ_HEAD(rte_cryptodev_cb_list, rte_cryptodev_callback);
545 /** The data structure associated with each crypto device. */
546 struct rte_cryptodev {
547 dequeue_pkt_burst_t dequeue_burst;
548 /**< Pointer to PMD receive function. */
549 enqueue_pkt_burst_t enqueue_burst;
550 /**< Pointer to PMD transmit function. */
552 const struct rte_cryptodev_driver *driver;
553 /**< Driver for this device */
554 struct rte_cryptodev_data *data;
555 /**< Pointer to device data */
556 struct rte_cryptodev_ops *dev_ops;
557 /**< Functions exported by PMD */
558 struct rte_pci_device *pci_dev;
559 /**< PCI info. supplied by probing */
561 enum rte_cryptodev_type dev_type;
562 /**< Crypto device type */
563 enum pmd_type pmd_type;
564 /**< PMD type - PDEV / VDEV */
566 struct rte_cryptodev_cb_list link_intr_cbs;
567 /**< User application callback for interrupts if present */
569 uint8_t attached : 1;
570 /**< Flag indicating the device is attached */
571 } __rte_cache_aligned;
574 #define RTE_CRYPTODEV_NAME_MAX_LEN (64)
575 /**< Max length of name of crypto PMD */
579 * The data part, with no function pointers, associated with each device.
581 * This structure is safe to place in shared memory to be common among
582 * different processes in a multi-process configuration.
584 struct rte_cryptodev_data {
586 /**< Device ID for this instance */
588 /**< Socket ID where memory is allocated */
589 char name[RTE_CRYPTODEV_NAME_MAX_LEN];
590 /**< Unique identifier name */
592 uint8_t dev_started : 1;
593 /**< Device state: STARTED(1)/STOPPED(0) */
595 struct rte_mempool *session_pool;
596 /**< Session memory pool */
598 /**< Array of pointers to queue pairs. */
599 uint16_t nb_queue_pairs;
600 /**< Number of device queue pairs. */
603 /**< PMD-specific private data */
604 } __rte_cache_aligned;
606 extern struct rte_cryptodev *rte_cryptodevs;
609 * Dequeue a burst of processed crypto operations from a queue on the crypto
610 * device. The dequeued operation are stored in *rte_crypto_op* structures
611 * whose pointers are supplied in the *ops* array.
613 * The rte_cryptodev_dequeue_burst() function returns the number of ops
614 * actually dequeued, which is the number of *rte_crypto_op* data structures
615 * effectively supplied into the *ops* array.
617 * A return value equal to *nb_ops* indicates that the queue contained
618 * at least *nb_ops* operations, and this is likely to signify that other
619 * processed operations remain in the devices output queue. Applications
620 * implementing a "retrieve as many processed operations as possible" policy
621 * can check this specific case and keep invoking the
622 * rte_cryptodev_dequeue_burst() function until a value less than
623 * *nb_ops* is returned.
625 * The rte_cryptodev_dequeue_burst() function does not provide any error
626 * notification to avoid the corresponding overhead.
628 * @param dev_id The symmetric crypto device identifier
629 * @param qp_id The index of the queue pair from which to
630 * retrieve processed packets. The value must be
631 * in the range [0, nb_queue_pair - 1] previously
632 * supplied to rte_cryptodev_configure().
633 * @param ops The address of an array of pointers to
634 * *rte_crypto_op* structures that must be
635 * large enough to store *nb_ops* pointers in it.
636 * @param nb_ops The maximum number of operations to dequeue.
639 * - The number of operations actually dequeued, which is the number
640 * of pointers to *rte_crypto_op* structures effectively supplied to the
643 static inline uint16_t
644 rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
645 struct rte_crypto_op **ops, uint16_t nb_ops)
647 struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
649 nb_ops = (*dev->dequeue_burst)
650 (dev->data->queue_pairs[qp_id], ops, nb_ops);
656 * Enqueue a burst of operations for processing on a crypto device.
658 * The rte_cryptodev_enqueue_burst() function is invoked to place
659 * crypto operations on the queue *qp_id* of the device designated by
662 * The *nb_ops* parameter is the number of operations to process which are
663 * supplied in the *ops* array of *rte_crypto_op* structures.
665 * The rte_cryptodev_enqueue_burst() function returns the number of
666 * operations it actually enqueued for processing. A return value equal to
667 * *nb_ops* means that all packets have been enqueued.
669 * @param dev_id The identifier of the device.
670 * @param qp_id The index of the queue pair which packets are
671 * to be enqueued for processing. The value
672 * must be in the range [0, nb_queue_pairs - 1]
673 * previously supplied to
674 * *rte_cryptodev_configure*.
675 * @param ops The address of an array of *nb_ops* pointers
676 * to *rte_crypto_op* structures which contain
677 * the crypto operations to be processed.
678 * @param nb_ops The number of operations to process.
681 * The number of operations actually enqueued on the crypto device. The return
682 * value can be less than the value of the *nb_ops* parameter when the
683 * crypto devices queue is full or if invalid parameters are specified in
686 static inline uint16_t
687 rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
688 struct rte_crypto_op **ops, uint16_t nb_ops)
690 struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
692 return (*dev->enqueue_burst)(
693 dev->data->queue_pairs[qp_id], ops, nb_ops);
697 /** Cryptodev symmetric crypto session */
698 struct rte_cryptodev_sym_session {
702 enum rte_cryptodev_type type;
703 /** Crypto Device type session created on */
704 struct rte_mempool *mp;
705 /**< Mempool session allocated from */
707 /**< Public symmetric session details */
710 /**< Private session material */
715 * Initialise a session for symmetric cryptographic operations.
717 * This function is used by the client to initialize immutable
718 * parameters of symmetric cryptographic operation.
719 * To perform the operation the rte_cryptodev_enqueue_burst function is
720 * used. Each mbuf should contain a reference to the session
721 * pointer returned from this function contained within it's crypto_op if a
722 * session-based operation is being provisioned. Memory to contain the session
723 * information is allocated from within mempool managed by the cryptodev.
725 * The rte_cryptodev_session_free must be called to free allocated
726 * memory when the session is no longer required.
728 * @param dev_id The device identifier.
729 * @param xform Crypto transform chain.
733 * Pointer to the created session or NULL
735 extern struct rte_cryptodev_sym_session *
736 rte_cryptodev_sym_session_create(uint8_t dev_id,
737 struct rte_crypto_sym_xform *xform);
740 * Free the memory associated with a previously allocated session.
742 * @param dev_id The device identifier.
743 * @param session Session pointer previously allocated by
744 * *rte_cryptodev_sym_session_create*.
747 * NULL on successful freeing of session.
748 * Session pointer on failure to free session.
750 extern struct rte_cryptodev_sym_session *
751 rte_cryptodev_sym_session_free(uint8_t dev_id,
752 struct rte_cryptodev_sym_session *session);
759 #endif /* _RTE_CRYPTODEV_H_ */