xref: /dpdk/doc/guides/prog_guide/cryptodev_lib.rst (revision 1d6f89885e1af581834a9c1d14b03e0defc785a4)
15630257fSFerruh Yigit..  SPDX-License-Identifier: BSD-3-Clause
25630257fSFerruh Yigit    Copyright(c) 2016-2017 Intel Corporation.
30318c02bSDeclan Doherty
40318c02bSDeclan DohertyCryptography Device Library
50318c02bSDeclan Doherty===========================
60318c02bSDeclan Doherty
70318c02bSDeclan DohertyThe cryptodev library provides a Crypto device framework for management and
80318c02bSDeclan Dohertyprovisioning of hardware and software Crypto poll mode drivers, defining generic
90318c02bSDeclan DohertyAPIs which support a number of different Crypto operations. The framework
100318c02bSDeclan Dohertycurrently only supports cipher, authentication, chained cipher/authentication
11b9209dc2SShally Vermaand AEAD symmetric and asymmetric Crypto operations.
120318c02bSDeclan Doherty
130318c02bSDeclan Doherty
140318c02bSDeclan DohertyDesign Principles
150318c02bSDeclan Doherty-----------------
160318c02bSDeclan Doherty
170318c02bSDeclan DohertyThe cryptodev library follows the same basic principles as those used in DPDKs
180318c02bSDeclan DohertyEthernet Device framework. The Crypto framework provides a generic Crypto device
190318c02bSDeclan Dohertyframework which supports both physical (hardware) and virtual (software) Crypto
200318c02bSDeclan Dohertydevices as well as a generic Crypto API which allows Crypto devices to be
210318c02bSDeclan Dohertymanaged and configured and supports Crypto operations to be provisioned on
220318c02bSDeclan DohertyCrypto poll mode driver.
230318c02bSDeclan Doherty
240318c02bSDeclan Doherty
250318c02bSDeclan DohertyDevice Management
260318c02bSDeclan Doherty-----------------
270318c02bSDeclan Doherty
280318c02bSDeclan DohertyDevice Creation
290318c02bSDeclan Doherty~~~~~~~~~~~~~~~
300318c02bSDeclan Doherty
310318c02bSDeclan DohertyPhysical Crypto devices are discovered during the PCI probe/enumeration of the
320318c02bSDeclan DohertyEAL function which is executed at DPDK initialization, based on
330318c02bSDeclan Dohertytheir PCI device identifier, each unique PCI BDF (bus/bridge, device,
340318c02bSDeclan Dohertyfunction). Specific physical Crypto devices, like other physical devices in DPDK
350318c02bSDeclan Dohertycan be white-listed or black-listed using the EAL command line options.
360318c02bSDeclan Doherty
370318c02bSDeclan DohertyVirtual devices can be created by two mechanisms, either using the EAL command
380318c02bSDeclan Dohertyline options or from within the application using an EAL API directly.
390318c02bSDeclan Doherty
400318c02bSDeclan DohertyFrom the command line using the --vdev EAL option
410318c02bSDeclan Doherty
420318c02bSDeclan Doherty.. code-block:: console
430318c02bSDeclan Doherty
44e1fc5b76SPablo de Lara   --vdev  'crypto_aesni_mb0,max_nb_queue_pairs=2,socket_id=0'
450318c02bSDeclan Doherty
46c149818bSVipin Varghese.. Note::
47c149818bSVipin Varghese
48c149818bSVipin Varghese   * If DPDK application requires multiple software crypto PMD devices then required
49c149818bSVipin Varghese     number of ``--vdev`` with appropriate libraries are to be added.
50c149818bSVipin Varghese
51c149818bSVipin Varghese   * An Application with crypto PMD instaces sharing the same library requires unique ID.
52c149818bSVipin Varghese
53c149818bSVipin Varghese   Example: ``--vdev  'crypto_aesni_mb0' --vdev  'crypto_aesni_mb1'``
54c149818bSVipin Varghese
552f6fec53SThomas MonjalonOur using the rte_vdev_init API within the application code.
560318c02bSDeclan Doherty
570318c02bSDeclan Doherty.. code-block:: c
580318c02bSDeclan Doherty
5930883f3eSPablo de Lara   rte_vdev_init("crypto_aesni_mb",
60e1fc5b76SPablo de Lara                     "max_nb_queue_pairs=2,socket_id=0")
610318c02bSDeclan Doherty
620318c02bSDeclan DohertyAll virtual Crypto devices support the following initialization parameters:
630318c02bSDeclan Doherty
640318c02bSDeclan Doherty* ``max_nb_queue_pairs`` - maximum number of queue pairs supported by the device.
650318c02bSDeclan Doherty* ``socket_id`` - socket on which to allocate the device resources on.
660318c02bSDeclan Doherty
670318c02bSDeclan Doherty
680318c02bSDeclan DohertyDevice Identification
690318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~
700318c02bSDeclan Doherty
710318c02bSDeclan DohertyEach device, whether virtual or physical is uniquely designated by two
720318c02bSDeclan Dohertyidentifiers:
730318c02bSDeclan Doherty
740318c02bSDeclan Doherty- A unique device index used to designate the Crypto device in all functions
750318c02bSDeclan Doherty  exported by the cryptodev API.
760318c02bSDeclan Doherty
770318c02bSDeclan Doherty- A device name used to designate the Crypto device in console messages, for
780318c02bSDeclan Doherty  administration or debugging purposes. For ease of use, the port name includes
790318c02bSDeclan Doherty  the port index.
800318c02bSDeclan Doherty
810318c02bSDeclan Doherty
820318c02bSDeclan DohertyDevice Configuration
830318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~
840318c02bSDeclan Doherty
850318c02bSDeclan DohertyThe configuration of each Crypto device includes the following operations:
860318c02bSDeclan Doherty
870318c02bSDeclan Doherty- Allocation of resources, including hardware resources if a physical device.
880318c02bSDeclan Doherty- Resetting the device into a well-known default state.
890318c02bSDeclan Doherty- Initialization of statistics counters.
900318c02bSDeclan Doherty
910318c02bSDeclan DohertyThe rte_cryptodev_configure API is used to configure a Crypto device.
920318c02bSDeclan Doherty
930318c02bSDeclan Doherty.. code-block:: c
940318c02bSDeclan Doherty
950318c02bSDeclan Doherty   int rte_cryptodev_configure(uint8_t dev_id,
960318c02bSDeclan Doherty                               struct rte_cryptodev_config *config)
970318c02bSDeclan Doherty
98bb59dac7SPablo de LaraThe ``rte_cryptodev_config`` structure is used to pass the configuration
99bb59dac7SPablo de Laraparameters for socket selection and number of queue pairs.
1000318c02bSDeclan Doherty
1010318c02bSDeclan Doherty.. code-block:: c
1020318c02bSDeclan Doherty
1030318c02bSDeclan Doherty    struct rte_cryptodev_config {
1040318c02bSDeclan Doherty        int socket_id;
1050318c02bSDeclan Doherty        /**< Socket to allocate resources on */
1060318c02bSDeclan Doherty        uint16_t nb_queue_pairs;
1070318c02bSDeclan Doherty        /**< Number of queue pairs to configure on device */
1080318c02bSDeclan Doherty    };
1090318c02bSDeclan Doherty
1100318c02bSDeclan Doherty
1110318c02bSDeclan DohertyConfiguration of Queue Pairs
1120318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1130318c02bSDeclan Doherty
1140318c02bSDeclan DohertyEach Crypto devices queue pair is individually configured through the
1150318c02bSDeclan Doherty``rte_cryptodev_queue_pair_setup`` API.
1160318c02bSDeclan DohertyEach queue pairs resources may be allocated on a specified socket.
1170318c02bSDeclan Doherty
1180318c02bSDeclan Doherty.. code-block:: c
1190318c02bSDeclan Doherty
1200318c02bSDeclan Doherty    int rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
1210318c02bSDeclan Doherty                const struct rte_cryptodev_qp_conf *qp_conf,
1220318c02bSDeclan Doherty                int socket_id)
1230318c02bSDeclan Doherty
1240318c02bSDeclan Doherty   struct rte_cryptodev_qp_conf {
1250318c02bSDeclan Doherty        uint32_t nb_descriptors; /**< Number of descriptors per queue pair */
126725d2a7fSFan Zhang        struct rte_mempool *mp_session;
127725d2a7fSFan Zhang        /**< The mempool for creating session in sessionless mode */
128725d2a7fSFan Zhang        struct rte_mempool *mp_session_private;
129725d2a7fSFan Zhang        /**< The mempool for creating sess private data in sessionless mode */
1300318c02bSDeclan Doherty    };
1310318c02bSDeclan Doherty
1320318c02bSDeclan Doherty
133725d2a7fSFan ZhangThe fields ``mp_session`` and ``mp_session_private`` are used for creating
134725d2a7fSFan Zhangtemporary session to process the crypto operations in the session-less mode.
135725d2a7fSFan ZhangThey can be the same other different mempools. Please note not all Cryptodev
136725d2a7fSFan ZhangPMDs supports session-less mode.
137725d2a7fSFan Zhang
138725d2a7fSFan Zhang
1390318c02bSDeclan DohertyLogical Cores, Memory and Queues Pair Relationships
1400318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1410318c02bSDeclan Doherty
1420318c02bSDeclan DohertyThe Crypto device Library as the Poll Mode Driver library support NUMA for when
1430318c02bSDeclan Dohertya processor’s logical cores and interfaces utilize its local memory. Therefore
1440318c02bSDeclan DohertyCrypto operations, and in the case of symmetric Crypto operations, the session
1450318c02bSDeclan Dohertyand the mbuf being operated on, should be allocated from memory pools created
1460318c02bSDeclan Dohertyin the local memory. The buffers should, if possible, remain on the local
1470318c02bSDeclan Dohertyprocessor to obtain the best performance results and buffer descriptors should
1480318c02bSDeclan Dohertybe populated with mbufs allocated from a mempool allocated from local memory.
1490318c02bSDeclan Doherty
1500318c02bSDeclan DohertyThe run-to-completion model also performs better, especially in the case of
1510318c02bSDeclan Dohertyvirtual Crypto devices, if the Crypto operation and session and data buffer is
1520318c02bSDeclan Dohertyin local memory instead of a remote processor's memory. This is also true for
1530318c02bSDeclan Dohertythe pipe-line model provided all logical cores used are located on the same
1540318c02bSDeclan Dohertyprocessor.
1550318c02bSDeclan Doherty
1560318c02bSDeclan DohertyMultiple logical cores should never share the same queue pair for enqueuing
1570318c02bSDeclan Dohertyoperations or dequeuing operations on the same Crypto device since this would
1580318c02bSDeclan Dohertyrequire global locks and hinder performance. It is however possible to use a
1590318c02bSDeclan Dohertydifferent logical core to dequeue an operation on a queue pair from the logical
1600318c02bSDeclan Dohertycore which it was enqueued on. This means that a crypto burst enqueue/dequeue
1610318c02bSDeclan DohertyAPIs are a logical place to transition from one logical core to another in a
1620318c02bSDeclan Dohertypacket processing pipeline.
1630318c02bSDeclan Doherty
1640318c02bSDeclan Doherty
1650318c02bSDeclan DohertyDevice Features and Capabilities
1660318c02bSDeclan Doherty---------------------------------
1670318c02bSDeclan Doherty
1680318c02bSDeclan DohertyCrypto devices define their functionality through two mechanisms, global device
1690318c02bSDeclan Dohertyfeatures and algorithm capabilities. Global devices features identify device
1700318c02bSDeclan Dohertywide level features which are applicable to the whole device such as
171b9209dc2SShally Vermathe device having hardware acceleration or supporting symmetric and/or asymmetric
172b9209dc2SShally VermaCrypto operations.
1730318c02bSDeclan Doherty
1740318c02bSDeclan DohertyThe capabilities mechanism defines the individual algorithms/functions which
17583984b7fSPablo de Larathe device supports, such as a specific symmetric Crypto cipher,
17683984b7fSPablo de Laraauthentication operation or Authenticated Encryption with Associated Data
17783984b7fSPablo de Lara(AEAD) operation.
1780318c02bSDeclan Doherty
1790318c02bSDeclan Doherty
1800318c02bSDeclan DohertyDevice Features
1810318c02bSDeclan Doherty~~~~~~~~~~~~~~~
1820318c02bSDeclan Doherty
1830318c02bSDeclan DohertyCurrently the following Crypto device features are defined:
1840318c02bSDeclan Doherty
1850318c02bSDeclan Doherty* Symmetric Crypto operations
1860318c02bSDeclan Doherty* Asymmetric Crypto operations
1870318c02bSDeclan Doherty* Chaining of symmetric Crypto operations
1880318c02bSDeclan Doherty* SSE accelerated SIMD vector operations
1890318c02bSDeclan Doherty* AVX accelerated SIMD vector operations
1900318c02bSDeclan Doherty* AVX2 accelerated SIMD vector operations
1910318c02bSDeclan Doherty* AESNI accelerated instructions
1920318c02bSDeclan Doherty* Hardware off-load processing
1930318c02bSDeclan Doherty
1940318c02bSDeclan Doherty
1950318c02bSDeclan DohertyDevice Operation Capabilities
1960318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1970318c02bSDeclan Doherty
1980318c02bSDeclan DohertyCrypto capabilities which identify particular algorithm which the Crypto PMD
1990318c02bSDeclan Dohertysupports are  defined by the operation type, the operation transform, the
2000318c02bSDeclan Dohertytransform identifier and then the particulars of the transform. For the full
2010318c02bSDeclan Dohertyscope of the Crypto capability see the definition of the structure in the
2020318c02bSDeclan Doherty*DPDK API Reference*.
2030318c02bSDeclan Doherty
2040318c02bSDeclan Doherty.. code-block:: c
2050318c02bSDeclan Doherty
2060318c02bSDeclan Doherty   struct rte_cryptodev_capabilities;
2070318c02bSDeclan Doherty
2080318c02bSDeclan DohertyEach Crypto poll mode driver defines its own private array of capabilities
2090318c02bSDeclan Dohertyfor the operations it supports. Below is an example of the capabilities for a
2100318c02bSDeclan DohertyPMD which supports the authentication algorithm SHA1_HMAC and the cipher
2110318c02bSDeclan Dohertyalgorithm AES_CBC.
2120318c02bSDeclan Doherty
2130318c02bSDeclan Doherty.. code-block:: c
2140318c02bSDeclan Doherty
2150318c02bSDeclan Doherty    static const struct rte_cryptodev_capabilities pmd_capabilities[] = {
2160318c02bSDeclan Doherty        {    /* SHA1 HMAC */
2170318c02bSDeclan Doherty            .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
2180318c02bSDeclan Doherty            .sym = {
2190318c02bSDeclan Doherty                .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
2200318c02bSDeclan Doherty                .auth = {
2210318c02bSDeclan Doherty                    .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
2220318c02bSDeclan Doherty                    .block_size = 64,
2230318c02bSDeclan Doherty                    .key_size = {
2240318c02bSDeclan Doherty                        .min = 64,
2250318c02bSDeclan Doherty                        .max = 64,
2260318c02bSDeclan Doherty                        .increment = 0
2270318c02bSDeclan Doherty                    },
2280318c02bSDeclan Doherty                    .digest_size = {
2290318c02bSDeclan Doherty                        .min = 12,
2300318c02bSDeclan Doherty                        .max = 12,
2310318c02bSDeclan Doherty                        .increment = 0
2320318c02bSDeclan Doherty                    },
233acf86169SPablo de Lara                    .aad_size = { 0 },
234acf86169SPablo de Lara                    .iv_size = { 0 }
2350318c02bSDeclan Doherty                }
2360318c02bSDeclan Doherty            }
2370318c02bSDeclan Doherty        },
2380318c02bSDeclan Doherty        {    /* AES CBC */
2390318c02bSDeclan Doherty            .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
2400318c02bSDeclan Doherty            .sym = {
2410318c02bSDeclan Doherty                .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
2420318c02bSDeclan Doherty                .cipher = {
2430318c02bSDeclan Doherty                    .algo = RTE_CRYPTO_CIPHER_AES_CBC,
2440318c02bSDeclan Doherty                    .block_size = 16,
2450318c02bSDeclan Doherty                    .key_size = {
2460318c02bSDeclan Doherty                        .min = 16,
2470318c02bSDeclan Doherty                        .max = 32,
2480318c02bSDeclan Doherty                        .increment = 8
2490318c02bSDeclan Doherty                    },
2500318c02bSDeclan Doherty                    .iv_size = {
2510318c02bSDeclan Doherty                        .min = 16,
2520318c02bSDeclan Doherty                        .max = 16,
2530318c02bSDeclan Doherty                        .increment = 0
2540318c02bSDeclan Doherty                    }
2550318c02bSDeclan Doherty                }
2560318c02bSDeclan Doherty            }
2570318c02bSDeclan Doherty        }
2580318c02bSDeclan Doherty    }
2590318c02bSDeclan Doherty
2600318c02bSDeclan Doherty
2610318c02bSDeclan DohertyCapabilities Discovery
2620318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~
2630318c02bSDeclan Doherty
2640318c02bSDeclan DohertyDiscovering the features and capabilities of a Crypto device poll mode driver
2650318c02bSDeclan Dohertyis achieved through the ``rte_cryptodev_info_get`` function.
2660318c02bSDeclan Doherty
2670318c02bSDeclan Doherty.. code-block:: c
2680318c02bSDeclan Doherty
2690318c02bSDeclan Doherty   void rte_cryptodev_info_get(uint8_t dev_id,
2700318c02bSDeclan Doherty                               struct rte_cryptodev_info *dev_info);
2710318c02bSDeclan Doherty
2720318c02bSDeclan DohertyThis allows the user to query a specific Crypto PMD and get all the device
2730318c02bSDeclan Dohertyfeatures and capabilities. The ``rte_cryptodev_info`` structure contains all the
2740318c02bSDeclan Dohertyrelevant information for the device.
2750318c02bSDeclan Doherty
2760318c02bSDeclan Doherty.. code-block:: c
2770318c02bSDeclan Doherty
2780318c02bSDeclan Doherty    struct rte_cryptodev_info {
2790318c02bSDeclan Doherty        const char *driver_name;
2807a364faeSSlawomir Mrozowicz        uint8_t driver_id;
281a4493be5SPablo de Lara        struct rte_device *device;
2820318c02bSDeclan Doherty
2830318c02bSDeclan Doherty        uint64_t feature_flags;
2840318c02bSDeclan Doherty
2850318c02bSDeclan Doherty        const struct rte_cryptodev_capabilities *capabilities;
2860318c02bSDeclan Doherty
2870318c02bSDeclan Doherty        unsigned max_nb_queue_pairs;
2880318c02bSDeclan Doherty
2890318c02bSDeclan Doherty        struct {
2900318c02bSDeclan Doherty            unsigned max_nb_sessions;
2910318c02bSDeclan Doherty        } sym;
2920318c02bSDeclan Doherty    };
2930318c02bSDeclan Doherty
2940318c02bSDeclan Doherty
2950318c02bSDeclan DohertyOperation Processing
2960318c02bSDeclan Doherty--------------------
2970318c02bSDeclan Doherty
2980318c02bSDeclan DohertyScheduling of Crypto operations on DPDK's application data path is
2990318c02bSDeclan Dohertyperformed using a burst oriented asynchronous API set. A queue pair on a Crypto
3000318c02bSDeclan Dohertydevice accepts a burst of Crypto operations using enqueue burst API. On physical
3010318c02bSDeclan DohertyCrypto devices the enqueue burst API will place the operations to be processed
3020318c02bSDeclan Dohertyon the devices hardware input queue, for virtual devices the processing of the
3030318c02bSDeclan DohertyCrypto operations is usually completed during the enqueue call to the Crypto
3040318c02bSDeclan Dohertydevice. The dequeue burst API will retrieve any processed operations available
3050318c02bSDeclan Dohertyfrom the queue pair on the Crypto device, from physical devices this is usually
3060318c02bSDeclan Dohertydirectly from the devices processed queue, and for virtual device's from a
3070318c02bSDeclan Doherty``rte_ring`` where processed operations are place after being processed on the
3080318c02bSDeclan Dohertyenqueue call.
3090318c02bSDeclan Doherty
3100318c02bSDeclan Doherty
311fe84aaeeSAbhinandan GujjarPrivate data
312fe84aaeeSAbhinandan Gujjar~~~~~~~~~~~~
313fe84aaeeSAbhinandan GujjarFor session-based operations, the set and get API provides a mechanism for an
3142d349f60SFiona Traheapplication to store and retrieve the private user data information stored along
3152d349f60SFiona Trahewith the crypto session.
316fe84aaeeSAbhinandan Gujjar
317fe84aaeeSAbhinandan GujjarFor example, suppose an application is submitting a crypto operation with a session
3182d349f60SFiona Traheassociated and wants to indicate private user data information which is required to be
319fe84aaeeSAbhinandan Gujjarused after completion of the crypto operation. In this case, the application can use
3202d349f60SFiona Trahethe set API to set the user data and retrieve it using get API.
321fe84aaeeSAbhinandan Gujjar
322fe84aaeeSAbhinandan Gujjar.. code-block:: c
323fe84aaeeSAbhinandan Gujjar
3242d349f60SFiona Trahe	int rte_cryptodev_sym_session_set_user_data(
325fe84aaeeSAbhinandan Gujjar		struct rte_cryptodev_sym_session *sess,	void *data, uint16_t size);
326fe84aaeeSAbhinandan Gujjar
3272d349f60SFiona Trahe	void * rte_cryptodev_sym_session_get_user_data(
328fe84aaeeSAbhinandan Gujjar		struct rte_cryptodev_sym_session *sess);
329fe84aaeeSAbhinandan Gujjar
330fe84aaeeSAbhinandan Gujjar
3312d349f60SFiona TraheFor session-less mode, the private user data information can be placed along with the
332fe84aaeeSAbhinandan Gujjar``struct rte_crypto_op``. The ``rte_crypto_op::private_data_offset`` indicates the
333fe84aaeeSAbhinandan Gujjarstart of private data information. The offset is counted from the start of the
334fe84aaeeSAbhinandan Gujjarrte_crypto_op including other crypto information such as the IVs (since there can
335fe84aaeeSAbhinandan Gujjarbe an IV also for authentication).
336fe84aaeeSAbhinandan Gujjar
337fe84aaeeSAbhinandan Gujjar
3380318c02bSDeclan DohertyEnqueue / Dequeue Burst APIs
3390318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3400318c02bSDeclan Doherty
3410318c02bSDeclan DohertyThe burst enqueue API uses a Crypto device identifier and a queue pair
3420318c02bSDeclan Dohertyidentifier to specify the Crypto device queue pair to schedule the processing on.
3430318c02bSDeclan DohertyThe ``nb_ops`` parameter is the number of operations to process which are
3440318c02bSDeclan Dohertysupplied in the ``ops`` array of ``rte_crypto_op`` structures.
3450318c02bSDeclan DohertyThe enqueue function returns the number of operations it actually enqueued for
3460318c02bSDeclan Dohertyprocessing, a return value equal to ``nb_ops`` means that all packets have been
3470318c02bSDeclan Dohertyenqueued.
3480318c02bSDeclan Doherty
3490318c02bSDeclan Doherty.. code-block:: c
3500318c02bSDeclan Doherty
3510318c02bSDeclan Doherty   uint16_t rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
3520318c02bSDeclan Doherty                                        struct rte_crypto_op **ops, uint16_t nb_ops)
3530318c02bSDeclan Doherty
3540318c02bSDeclan DohertyThe dequeue API uses the same format as the enqueue API of processed but
3550318c02bSDeclan Dohertythe ``nb_ops`` and ``ops`` parameters are now used to specify the max processed
3560318c02bSDeclan Dohertyoperations the user wishes to retrieve and the location in which to store them.
3570318c02bSDeclan DohertyThe API call returns the actual number of processed operations returned, this
3580318c02bSDeclan Dohertycan never be larger than ``nb_ops``.
3590318c02bSDeclan Doherty
3600318c02bSDeclan Doherty.. code-block:: c
3610318c02bSDeclan Doherty
3620318c02bSDeclan Doherty   uint16_t rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
3630318c02bSDeclan Doherty                                        struct rte_crypto_op **ops, uint16_t nb_ops)
3640318c02bSDeclan Doherty
3650318c02bSDeclan Doherty
3660318c02bSDeclan DohertyOperation Representation
3670318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~
3680318c02bSDeclan Doherty
3690318c02bSDeclan DohertyAn Crypto operation is represented by an rte_crypto_op structure, which is a
3700318c02bSDeclan Dohertygeneric metadata container for all necessary information required for the
3710318c02bSDeclan DohertyCrypto operation to be processed on a particular Crypto device poll mode driver.
3720318c02bSDeclan Doherty
3730318c02bSDeclan Doherty.. figure:: img/crypto_op.*
3740318c02bSDeclan Doherty
3755209df0dSPablo de LaraThe operation structure includes the operation type, the operation status
3765209df0dSPablo de Laraand the session type (session-based/less), a reference to the operation
3775209df0dSPablo de Laraspecific data, which can vary in size and content depending on the operation
3785209df0dSPablo de Larabeing provisioned. It also contains the source mempool for the operation,
379b1f6192bSPablo de Laraif it allocated from a mempool.
3800318c02bSDeclan Doherty
3810318c02bSDeclan DohertyIf Crypto operations are allocated from a Crypto operation mempool, see next
3820318c02bSDeclan Dohertysection, there is also the ability to allocate private memory with the
3830318c02bSDeclan Dohertyoperation for applications purposes.
3840318c02bSDeclan Doherty
3850318c02bSDeclan DohertyApplication software is responsible for specifying all the operation specific
3860318c02bSDeclan Dohertyfields in the ``rte_crypto_op`` structure which are then used by the Crypto PMD
3870318c02bSDeclan Dohertyto process the requested operation.
3880318c02bSDeclan Doherty
3890318c02bSDeclan Doherty
3900318c02bSDeclan DohertyOperation Management and Allocation
3910318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3920318c02bSDeclan Doherty
3930318c02bSDeclan DohertyThe cryptodev library provides an API set for managing Crypto operations which
3940318c02bSDeclan Dohertyutilize the Mempool Library to allocate operation buffers. Therefore, it ensures
3950318c02bSDeclan Dohertythat the crytpo operation is interleaved optimally across the channels and
3960318c02bSDeclan Dohertyranks for optimal processing.
3970318c02bSDeclan DohertyA ``rte_crypto_op`` contains a field indicating the pool that it originated from.
3980318c02bSDeclan DohertyWhen calling ``rte_crypto_op_free(op)``, the operation returns to its original pool.
3990318c02bSDeclan Doherty
4000318c02bSDeclan Doherty.. code-block:: c
4010318c02bSDeclan Doherty
4020318c02bSDeclan Doherty   extern struct rte_mempool *
4030318c02bSDeclan Doherty   rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
4040318c02bSDeclan Doherty                             unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
4050318c02bSDeclan Doherty                             int socket_id);
4060318c02bSDeclan Doherty
4070318c02bSDeclan DohertyDuring pool creation ``rte_crypto_op_init()`` is called as a constructor to
4080318c02bSDeclan Dohertyinitialize each Crypto operation which subsequently calls
4090318c02bSDeclan Doherty``__rte_crypto_op_reset()`` to configure any operation type specific fields based
4100318c02bSDeclan Dohertyon the type parameter.
4110318c02bSDeclan Doherty
4120318c02bSDeclan Doherty
4130318c02bSDeclan Doherty``rte_crypto_op_alloc()`` and ``rte_crypto_op_bulk_alloc()`` are used to allocate
4140318c02bSDeclan DohertyCrypto operations of a specific type from a given Crypto operation mempool.
4150318c02bSDeclan Doherty``__rte_crypto_op_reset()`` is called on each operation before being returned to
4160318c02bSDeclan Dohertyallocate to a user so the operation is always in a good known state before use
4170318c02bSDeclan Dohertyby the application.
4180318c02bSDeclan Doherty
4190318c02bSDeclan Doherty.. code-block:: c
4200318c02bSDeclan Doherty
4210318c02bSDeclan Doherty   struct rte_crypto_op *rte_crypto_op_alloc(struct rte_mempool *mempool,
4220318c02bSDeclan Doherty                                             enum rte_crypto_op_type type)
4230318c02bSDeclan Doherty
4240318c02bSDeclan Doherty   unsigned rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
4250318c02bSDeclan Doherty                                     enum rte_crypto_op_type type,
4260318c02bSDeclan Doherty                                     struct rte_crypto_op **ops, uint16_t nb_ops)
4270318c02bSDeclan Doherty
4280318c02bSDeclan Doherty``rte_crypto_op_free()`` is called by the application to return an operation to
4290318c02bSDeclan Dohertyits allocating pool.
4300318c02bSDeclan Doherty
4310318c02bSDeclan Doherty.. code-block:: c
4320318c02bSDeclan Doherty
4330318c02bSDeclan Doherty   void rte_crypto_op_free(struct rte_crypto_op *op)
4340318c02bSDeclan Doherty
4350318c02bSDeclan Doherty
4360318c02bSDeclan DohertySymmetric Cryptography Support
4370318c02bSDeclan Doherty------------------------------
4380318c02bSDeclan Doherty
4390318c02bSDeclan DohertyThe cryptodev library currently provides support for the following symmetric
4400318c02bSDeclan DohertyCrypto operations; cipher, authentication, including chaining of these
4410318c02bSDeclan Dohertyoperations, as well as also supporting AEAD operations.
4420318c02bSDeclan Doherty
4430318c02bSDeclan Doherty
4440318c02bSDeclan DohertySession and Session Management
445e3346dfcSPablo de Lara~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4460318c02bSDeclan Doherty
447bb59dac7SPablo de LaraSessions are used in symmetric cryptographic processing to store the immutable
4480318c02bSDeclan Dohertydata defined in a cryptographic transform which is used in the operation
4490318c02bSDeclan Dohertyprocessing of a packet flow. Sessions are used to manage information such as
4500318c02bSDeclan Dohertyexpand cipher keys and HMAC IPADs and OPADs, which need to be calculated for a
4510318c02bSDeclan Dohertyparticular Crypto operation, but are immutable on a packet to packet basis for
4520318c02bSDeclan Dohertya flow. Crypto sessions cache this immutable data in a optimal way for the
4530318c02bSDeclan Dohertyunderlying PMD and this allows further acceleration of the offload of
4540318c02bSDeclan DohertyCrypto workloads.
4550318c02bSDeclan Doherty
4560318c02bSDeclan Doherty.. figure:: img/cryptodev_sym_sess.*
4570318c02bSDeclan Doherty
458*1d6f8988SFan ZhangThe Crypto device framework provides APIs to create session mempool and allocate
459*1d6f8988SFan Zhangand initialize sessions for crypto devices, where sessions are mempool objects.
460*1d6f8988SFan ZhangThe application has to use ``rte_cryptodev_sym_session_pool_create()`` to
461*1d6f8988SFan Zhangcreate the session header mempool that creates a mempool with proper element
462*1d6f8988SFan Zhangsize automatically and stores necessary information for safely accessing the
463*1d6f8988SFan Zhangsession in the mempool's private data field.
4640318c02bSDeclan Doherty
465*1d6f8988SFan ZhangTo create a mempool for storing session private data, the application has two
466*1d6f8988SFan Zhangoptions. The first is to create another mempool with elt size equal to or
467*1d6f8988SFan Zhangbigger than the maximum session private data size of all crypto devices that
468*1d6f8988SFan Zhangwill share the same session header. The creation of the mempool shall use the
469*1d6f8988SFan Zhangtraditional ``rte_mempool_create()`` with the correct ``elt_size``. The other
470*1d6f8988SFan Zhangoption is to change the ``elt_size`` parameter in
471*1d6f8988SFan Zhang``rte_cryptodev_sym_session_pool_create()`` to the correct value. The first
472*1d6f8988SFan Zhangoption is more complex to implement but may result in better memory usage as
473*1d6f8988SFan Zhanga session header normally takes smaller memory footprint as the session private
474*1d6f8988SFan Zhangdata.
4750318c02bSDeclan Doherty
476bb59dac7SPablo de LaraOnce the session mempools have been created, ``rte_cryptodev_sym_session_create()``
477bb59dac7SPablo de Larais used to allocate an uninitialized session from the given mempool.
478bb59dac7SPablo de LaraThe session then must be initialized using ``rte_cryptodev_sym_session_init()``
479bb59dac7SPablo de Larafor each of the required crypto devices. A symmetric transform chain
480bb59dac7SPablo de Larais used to specify the operation and its parameters. See the section below for
481bb59dac7SPablo de Laradetails on transforms.
4820318c02bSDeclan Doherty
483bb59dac7SPablo de LaraWhen a session is no longer used, user must call ``rte_cryptodev_sym_session_clear()``
484bb59dac7SPablo de Larafor each of the crypto devices that are using the session, to free all driver
485bb59dac7SPablo de Laraprivate session data. Once this is done, session should be freed using
486bb59dac7SPablo de Lara``rte_cryptodev_sym_session_free`` which returns them to their mempool.
4870318c02bSDeclan Doherty
4880318c02bSDeclan Doherty
4890318c02bSDeclan DohertyTransforms and Transform Chaining
4900318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4910318c02bSDeclan Doherty
4920318c02bSDeclan DohertySymmetric Crypto transforms (``rte_crypto_sym_xform``) are the mechanism used
4930318c02bSDeclan Dohertyto specify the details of the Crypto operation. For chaining of symmetric
4940318c02bSDeclan Dohertyoperations such as cipher encrypt and authentication generate, the next pointer
4950318c02bSDeclan Dohertyallows transform to be chained together. Crypto devices which support chaining
4960318c02bSDeclan Dohertymust publish the chaining of symmetric Crypto operations feature flag.
4970318c02bSDeclan Doherty
49883984b7fSPablo de LaraCurrently there are three transforms types cipher, authentication and AEAD.
49983984b7fSPablo de LaraAlso it is important to note that the order in which the
5000318c02bSDeclan Dohertytransforms are passed indicates the order of the chaining.
5010318c02bSDeclan Doherty
5020318c02bSDeclan Doherty.. code-block:: c
5030318c02bSDeclan Doherty
5040318c02bSDeclan Doherty    struct rte_crypto_sym_xform {
5050318c02bSDeclan Doherty        struct rte_crypto_sym_xform *next;
5060318c02bSDeclan Doherty        /**< next xform in chain */
5070318c02bSDeclan Doherty        enum rte_crypto_sym_xform_type type;
5080318c02bSDeclan Doherty        /**< xform type */
5090318c02bSDeclan Doherty        union {
5100318c02bSDeclan Doherty            struct rte_crypto_auth_xform auth;
5110318c02bSDeclan Doherty            /**< Authentication / hash xform */
5120318c02bSDeclan Doherty            struct rte_crypto_cipher_xform cipher;
5130318c02bSDeclan Doherty            /**< Cipher xform */
51483984b7fSPablo de Lara            struct rte_crypto_aead_xform aead;
51583984b7fSPablo de Lara            /**< AEAD xform */
5160318c02bSDeclan Doherty        };
5170318c02bSDeclan Doherty    };
5180318c02bSDeclan Doherty
5190318c02bSDeclan DohertyThe API does not place a limit on the number of transforms that can be chained
5200318c02bSDeclan Dohertytogether but this will be limited by the underlying Crypto device poll mode
5210318c02bSDeclan Dohertydriver which is processing the operation.
5220318c02bSDeclan Doherty
5230318c02bSDeclan Doherty.. figure:: img/crypto_xform_chain.*
5240318c02bSDeclan Doherty
5250318c02bSDeclan Doherty
5260318c02bSDeclan DohertySymmetric Operations
5270318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~
5280318c02bSDeclan Doherty
5290318c02bSDeclan DohertyThe symmetric Crypto operation structure contains all the mutable data relating
5300318c02bSDeclan Dohertyto performing symmetric cryptographic processing on a referenced mbuf data
5310318c02bSDeclan Dohertybuffer. It is used for either cipher, authentication, AEAD and chained
5320318c02bSDeclan Dohertyoperations.
5330318c02bSDeclan Doherty
5340318c02bSDeclan DohertyAs a minimum the symmetric operation must have a source data buffer (``m_src``),
5355209df0dSPablo de Laraa valid session (or transform chain if in session-less mode) and the minimum
53683984b7fSPablo de Laraauthentication/ cipher/ AEAD parameters required depending on the type of operation
5375209df0dSPablo de Laraspecified in the session or the transform
5380318c02bSDeclan Dohertychain.
5390318c02bSDeclan Doherty
5400318c02bSDeclan Doherty.. code-block:: c
5410318c02bSDeclan Doherty
5420318c02bSDeclan Doherty    struct rte_crypto_sym_op {
5430318c02bSDeclan Doherty        struct rte_mbuf *m_src;
5440318c02bSDeclan Doherty        struct rte_mbuf *m_dst;
5450318c02bSDeclan Doherty
5460318c02bSDeclan Doherty        union {
5470318c02bSDeclan Doherty            struct rte_cryptodev_sym_session *session;
5480318c02bSDeclan Doherty            /**< Handle for the initialised session context */
5490318c02bSDeclan Doherty            struct rte_crypto_sym_xform *xform;
5500318c02bSDeclan Doherty            /**< Session-less API Crypto operation parameters */
5510318c02bSDeclan Doherty        };
5520318c02bSDeclan Doherty
553b59502a5SPablo de Lara        union {
554b59502a5SPablo de Lara            struct {
555b59502a5SPablo de Lara                struct {
556b59502a5SPablo de Lara                    uint32_t offset;
557b59502a5SPablo de Lara                    uint32_t length;
558b59502a5SPablo de Lara                } data; /**< Data offsets and length for AEAD */
559b59502a5SPablo de Lara
560b59502a5SPablo de Lara                struct {
561b59502a5SPablo de Lara                    uint8_t *data;
562c4509373SSantosh Shukla                    rte_iova_t phys_addr;
563b59502a5SPablo de Lara                } digest; /**< Digest parameters */
564b59502a5SPablo de Lara
565b59502a5SPablo de Lara                struct {
566b59502a5SPablo de Lara                    uint8_t *data;
567c4509373SSantosh Shukla                    rte_iova_t phys_addr;
568b59502a5SPablo de Lara                } aad;
569b59502a5SPablo de Lara                /**< Additional authentication parameters */
570b59502a5SPablo de Lara            } aead;
571b59502a5SPablo de Lara
572b59502a5SPablo de Lara            struct {
5730318c02bSDeclan Doherty                struct {
5740318c02bSDeclan Doherty                    struct {
5750318c02bSDeclan Doherty                        uint32_t offset;
5760318c02bSDeclan Doherty                        uint32_t length;
5770318c02bSDeclan Doherty                    } data; /**< Data offsets and length for ciphering */
5780318c02bSDeclan Doherty                } cipher;
5790318c02bSDeclan Doherty
5800318c02bSDeclan Doherty                struct {
5810318c02bSDeclan Doherty                    struct {
5820318c02bSDeclan Doherty                        uint32_t offset;
5830318c02bSDeclan Doherty                        uint32_t length;
584b59502a5SPablo de Lara                    } data;
585b59502a5SPablo de Lara                    /**< Data offsets and length for authentication */
5860318c02bSDeclan Doherty
5870318c02bSDeclan Doherty                    struct {
5880318c02bSDeclan Doherty                        uint8_t *data;
589c4509373SSantosh Shukla                        rte_iova_t phys_addr;
5900318c02bSDeclan Doherty                    } digest; /**< Digest parameters */
5910318c02bSDeclan Doherty                } auth;
592b59502a5SPablo de Lara            };
593b59502a5SPablo de Lara        };
594b59502a5SPablo de Lara    };
5950318c02bSDeclan Doherty
59631850d26SPablo de LaraSample code
59731850d26SPablo de Lara-----------
59831850d26SPablo de Lara
59931850d26SPablo de LaraThere are various sample applications that show how to use the cryptodev library,
60031850d26SPablo de Larasuch as the L2fwd with Crypto sample application (L2fwd-crypto) and
60131850d26SPablo de Larathe IPSec Security Gateway application (ipsec-secgw).
60231850d26SPablo de Lara
60331850d26SPablo de LaraWhile these applications demonstrate how an application can be created to perform
60431850d26SPablo de Larageneric crypto operation, the required complexity hides the basic steps of
60531850d26SPablo de Larahow to use the cryptodev APIs.
60631850d26SPablo de Lara
60731850d26SPablo de LaraThe following sample code shows the basic steps to encrypt several buffers
60831850d26SPablo de Larawith AES-CBC (although performing other crypto operations is similar),
60931850d26SPablo de Larausing one of the crypto PMDs available in DPDK.
61031850d26SPablo de Lara
61131850d26SPablo de Lara.. code-block:: c
61231850d26SPablo de Lara
61331850d26SPablo de Lara    /*
61431850d26SPablo de Lara     * Simple example to encrypt several buffers with AES-CBC using
61531850d26SPablo de Lara     * the Cryptodev APIs.
61631850d26SPablo de Lara     */
61731850d26SPablo de Lara
61831850d26SPablo de Lara    #define MAX_SESSIONS         1024
61931850d26SPablo de Lara    #define NUM_MBUFS            1024
62031850d26SPablo de Lara    #define POOL_CACHE_SIZE      128
62131850d26SPablo de Lara    #define BURST_SIZE           32
62231850d26SPablo de Lara    #define BUFFER_SIZE          1024
62331850d26SPablo de Lara    #define AES_CBC_IV_LENGTH    16
62431850d26SPablo de Lara    #define AES_CBC_KEY_LENGTH   16
62531850d26SPablo de Lara    #define IV_OFFSET            (sizeof(struct rte_crypto_op) + \
62631850d26SPablo de Lara                                 sizeof(struct rte_crypto_sym_op))
62731850d26SPablo de Lara
628*1d6f8988SFan Zhang    struct rte_mempool *mbuf_pool, *crypto_op_pool;
629*1d6f8988SFan Zhang    struct rte_mempool *session_pool, *session_priv_pool;
63031850d26SPablo de Lara    unsigned int session_size;
63131850d26SPablo de Lara    int ret;
63231850d26SPablo de Lara
63331850d26SPablo de Lara    /* Initialize EAL. */
63431850d26SPablo de Lara    ret = rte_eal_init(argc, argv);
63531850d26SPablo de Lara    if (ret < 0)
63631850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
63731850d26SPablo de Lara
63831850d26SPablo de Lara    uint8_t socket_id = rte_socket_id();
63931850d26SPablo de Lara
64031850d26SPablo de Lara    /* Create the mbuf pool. */
64131850d26SPablo de Lara    mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool",
64231850d26SPablo de Lara                                    NUM_MBUFS,
64331850d26SPablo de Lara                                    POOL_CACHE_SIZE,
64431850d26SPablo de Lara                                    0,
64531850d26SPablo de Lara                                    RTE_MBUF_DEFAULT_BUF_SIZE,
64631850d26SPablo de Lara                                    socket_id);
64731850d26SPablo de Lara    if (mbuf_pool == NULL)
64831850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
64931850d26SPablo de Lara
65031850d26SPablo de Lara    /*
65131850d26SPablo de Lara     * The IV is always placed after the crypto operation,
65231850d26SPablo de Lara     * so some private data is required to be reserved.
65331850d26SPablo de Lara     */
65431850d26SPablo de Lara    unsigned int crypto_op_private_data = AES_CBC_IV_LENGTH;
65531850d26SPablo de Lara
65631850d26SPablo de Lara    /* Create crypto operation pool. */
65731850d26SPablo de Lara    crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
65831850d26SPablo de Lara                                            RTE_CRYPTO_OP_TYPE_SYMMETRIC,
65931850d26SPablo de Lara                                            NUM_MBUFS,
66031850d26SPablo de Lara                                            POOL_CACHE_SIZE,
66131850d26SPablo de Lara                                            crypto_op_private_data,
66231850d26SPablo de Lara                                            socket_id);
66331850d26SPablo de Lara    if (crypto_op_pool == NULL)
66431850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
66531850d26SPablo de Lara
66631850d26SPablo de Lara    /* Create the virtual crypto device. */
66731850d26SPablo de Lara    char args[128];
66831850d26SPablo de Lara    const char *crypto_name = "crypto_aesni_mb0";
66931850d26SPablo de Lara    snprintf(args, sizeof(args), "socket_id=%d", socket_id);
67031850d26SPablo de Lara    ret = rte_vdev_init(crypto_name, args);
67131850d26SPablo de Lara    if (ret != 0)
67231850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Cannot create virtual device");
67331850d26SPablo de Lara
67431850d26SPablo de Lara    uint8_t cdev_id = rte_cryptodev_get_dev_id(crypto_name);
67531850d26SPablo de Lara
67631850d26SPablo de Lara    /* Get private session data size. */
677a106fcceSPablo de Lara    session_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
67831850d26SPablo de Lara
679*1d6f8988SFan Zhang    #ifdef USE_TWO_MEMPOOLS
680*1d6f8988SFan Zhang    /* Create session mempool for the session header. */
681*1d6f8988SFan Zhang    session_pool = rte_cryptodev_sym_session_pool_create("session_pool",
682*1d6f8988SFan Zhang                                    MAX_SESSIONS,
683*1d6f8988SFan Zhang                                    0,
684*1d6f8988SFan Zhang                                    POOL_CACHE_SIZE,
685*1d6f8988SFan Zhang                                    0,
686*1d6f8988SFan Zhang                                    socket_id);
687*1d6f8988SFan Zhang
68831850d26SPablo de Lara    /*
689*1d6f8988SFan Zhang     * Create session private data mempool for the
69031850d26SPablo de Lara     * private session data for the crypto device.
69131850d26SPablo de Lara     */
692*1d6f8988SFan Zhang    session_priv_pool = rte_mempool_create("session_pool",
693*1d6f8988SFan Zhang                                    MAX_SESSIONS,
69431850d26SPablo de Lara                                    session_size,
69531850d26SPablo de Lara                                    POOL_CACHE_SIZE,
69631850d26SPablo de Lara                                    0, NULL, NULL, NULL,
69731850d26SPablo de Lara                                    NULL, socket_id,
69831850d26SPablo de Lara                                    0);
69931850d26SPablo de Lara
700*1d6f8988SFan Zhang    #else
701*1d6f8988SFan Zhang    /* Use of the same mempool for session header and private data */
702*1d6f8988SFan Zhang	session_pool = rte_cryptodev_sym_session_pool_create("session_pool",
703*1d6f8988SFan Zhang                                    MAX_SESSIONS * 2,
704*1d6f8988SFan Zhang                                    session_size,
705*1d6f8988SFan Zhang                                    POOL_CACHE_SIZE,
706*1d6f8988SFan Zhang                                    0,
707*1d6f8988SFan Zhang                                    socket_id);
708*1d6f8988SFan Zhang
709*1d6f8988SFan Zhang	session_priv_pool = session_pool;
710*1d6f8988SFan Zhang
711*1d6f8988SFan Zhang    #endif
712*1d6f8988SFan Zhang
71331850d26SPablo de Lara    /* Configure the crypto device. */
71431850d26SPablo de Lara    struct rte_cryptodev_config conf = {
71531850d26SPablo de Lara        .nb_queue_pairs = 1,
71631850d26SPablo de Lara        .socket_id = socket_id
71731850d26SPablo de Lara    };
718*1d6f8988SFan Zhang
71931850d26SPablo de Lara    struct rte_cryptodev_qp_conf qp_conf = {
720725d2a7fSFan Zhang        .nb_descriptors = 2048,
721725d2a7fSFan Zhang        .mp_session = session_pool,
722*1d6f8988SFan Zhang        .mp_session_private = session_priv_pool
72331850d26SPablo de Lara    };
72431850d26SPablo de Lara
72531850d26SPablo de Lara    if (rte_cryptodev_configure(cdev_id, &conf) < 0)
72631850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Failed to configure cryptodev %u", cdev_id);
72731850d26SPablo de Lara
728725d2a7fSFan Zhang    if (rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf, socket_id) < 0)
72931850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Failed to setup queue pair\n");
73031850d26SPablo de Lara
73131850d26SPablo de Lara    if (rte_cryptodev_start(cdev_id) < 0)
73231850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Failed to start device\n");
73331850d26SPablo de Lara
73431850d26SPablo de Lara    /* Create the crypto transform. */
73531850d26SPablo de Lara    uint8_t cipher_key[16] = {0};
73631850d26SPablo de Lara    struct rte_crypto_sym_xform cipher_xform = {
73731850d26SPablo de Lara        .next = NULL,
73831850d26SPablo de Lara        .type = RTE_CRYPTO_SYM_XFORM_CIPHER,
73931850d26SPablo de Lara        .cipher = {
74031850d26SPablo de Lara            .op = RTE_CRYPTO_CIPHER_OP_ENCRYPT,
74131850d26SPablo de Lara            .algo = RTE_CRYPTO_CIPHER_AES_CBC,
74231850d26SPablo de Lara            .key = {
74331850d26SPablo de Lara                .data = cipher_key,
74431850d26SPablo de Lara                .length = AES_CBC_KEY_LENGTH
74531850d26SPablo de Lara            },
74631850d26SPablo de Lara            .iv = {
74731850d26SPablo de Lara                .offset = IV_OFFSET,
74831850d26SPablo de Lara                .length = AES_CBC_IV_LENGTH
74931850d26SPablo de Lara            }
75031850d26SPablo de Lara        }
75131850d26SPablo de Lara    };
75231850d26SPablo de Lara
75331850d26SPablo de Lara    /* Create crypto session and initialize it for the crypto device. */
75431850d26SPablo de Lara    struct rte_cryptodev_sym_session *session;
75531850d26SPablo de Lara    session = rte_cryptodev_sym_session_create(session_pool);
75631850d26SPablo de Lara    if (session == NULL)
75731850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Session could not be created\n");
75831850d26SPablo de Lara
75931850d26SPablo de Lara    if (rte_cryptodev_sym_session_init(cdev_id, session,
760*1d6f8988SFan Zhang                    &cipher_xform, session_priv_pool) < 0)
76131850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Session could not be initialized "
76231850d26SPablo de Lara                    "for the crypto device\n");
76331850d26SPablo de Lara
76431850d26SPablo de Lara    /* Get a burst of crypto operations. */
76531850d26SPablo de Lara    struct rte_crypto_op *crypto_ops[BURST_SIZE];
76631850d26SPablo de Lara    if (rte_crypto_op_bulk_alloc(crypto_op_pool,
76731850d26SPablo de Lara                            RTE_CRYPTO_OP_TYPE_SYMMETRIC,
76831850d26SPablo de Lara                            crypto_ops, BURST_SIZE) == 0)
76931850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Not enough crypto operations available\n");
77031850d26SPablo de Lara
77131850d26SPablo de Lara    /* Get a burst of mbufs. */
77231850d26SPablo de Lara    struct rte_mbuf *mbufs[BURST_SIZE];
77331850d26SPablo de Lara    if (rte_pktmbuf_alloc_bulk(mbuf_pool, mbufs, BURST_SIZE) < 0)
77431850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Not enough mbufs available");
77531850d26SPablo de Lara
77631850d26SPablo de Lara    /* Initialize the mbufs and append them to the crypto operations. */
77731850d26SPablo de Lara    unsigned int i;
77831850d26SPablo de Lara    for (i = 0; i < BURST_SIZE; i++) {
77931850d26SPablo de Lara        if (rte_pktmbuf_append(mbufs[i], BUFFER_SIZE) == NULL)
78031850d26SPablo de Lara            rte_exit(EXIT_FAILURE, "Not enough room in the mbuf\n");
78131850d26SPablo de Lara        crypto_ops[i]->sym->m_src = mbufs[i];
78231850d26SPablo de Lara    }
78331850d26SPablo de Lara
78431850d26SPablo de Lara    /* Set up the crypto operations. */
78531850d26SPablo de Lara    for (i = 0; i < BURST_SIZE; i++) {
78631850d26SPablo de Lara        struct rte_crypto_op *op = crypto_ops[i];
78731850d26SPablo de Lara        /* Modify bytes of the IV at the end of the crypto operation */
78831850d26SPablo de Lara        uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
78931850d26SPablo de Lara                                                IV_OFFSET);
79031850d26SPablo de Lara
79131850d26SPablo de Lara        generate_random_bytes(iv_ptr, AES_CBC_IV_LENGTH);
79231850d26SPablo de Lara
79331850d26SPablo de Lara        op->sym->cipher.data.offset = 0;
79431850d26SPablo de Lara        op->sym->cipher.data.length = BUFFER_SIZE;
79531850d26SPablo de Lara
79631850d26SPablo de Lara        /* Attach the crypto session to the operation */
79731850d26SPablo de Lara        rte_crypto_op_attach_sym_session(op, session);
79831850d26SPablo de Lara    }
79931850d26SPablo de Lara
80031850d26SPablo de Lara    /* Enqueue the crypto operations in the crypto device. */
80131850d26SPablo de Lara    uint16_t num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, 0,
80231850d26SPablo de Lara                                            crypto_ops, BURST_SIZE);
80331850d26SPablo de Lara
80431850d26SPablo de Lara    /*
80531850d26SPablo de Lara     * Dequeue the crypto operations until all the operations
80631850d26SPablo de Lara     * are proccessed in the crypto device.
80731850d26SPablo de Lara     */
80831850d26SPablo de Lara    uint16_t num_dequeued_ops, total_num_dequeued_ops = 0;
80931850d26SPablo de Lara    do {
81031850d26SPablo de Lara        struct rte_crypto_op *dequeued_ops[BURST_SIZE];
81131850d26SPablo de Lara        num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, 0,
81231850d26SPablo de Lara                                        dequeued_ops, BURST_SIZE);
81331850d26SPablo de Lara        total_num_dequeued_ops += num_dequeued_ops;
81431850d26SPablo de Lara
81531850d26SPablo de Lara        /* Check if operation was processed successfully */
81631850d26SPablo de Lara        for (i = 0; i < num_dequeued_ops; i++) {
81731850d26SPablo de Lara            if (dequeued_ops[i]->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
81831850d26SPablo de Lara                rte_exit(EXIT_FAILURE,
81931850d26SPablo de Lara                        "Some operations were not processed correctly");
82031850d26SPablo de Lara        }
82131850d26SPablo de Lara
82231850d26SPablo de Lara        rte_mempool_put_bulk(crypto_op_pool, (void **)dequeued_ops,
82331850d26SPablo de Lara                                            num_dequeued_ops);
82431850d26SPablo de Lara    } while (total_num_dequeued_ops < num_enqueued_ops);
82531850d26SPablo de Lara
8260318c02bSDeclan DohertyAsymmetric Cryptography
8270318c02bSDeclan Doherty-----------------------
8280318c02bSDeclan Doherty
829b9209dc2SShally VermaThe cryptodev library currently provides support for the following asymmetric
830b9209dc2SShally VermaCrypto operations; RSA, Modular exponentiation and inversion, Diffie-Hellman
831b9209dc2SShally Vermapublic and/or private key generation and shared secret compute, DSA Signature
832b9209dc2SShally Vermageneration and verification.
833b9209dc2SShally Verma
834b9209dc2SShally VermaSession and Session Management
835b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
836b9209dc2SShally Verma
837b9209dc2SShally VermaSessions are used in asymmetric cryptographic processing to store the immutable
838b9209dc2SShally Vermadata defined in asymmetric cryptographic transform which is further used in the
839b9209dc2SShally Vermaoperation processing. Sessions typically stores information, such as, public
840b9209dc2SShally Vermaand private key information or domain params or prime modulus data i.e. immutable
841b9209dc2SShally Vermaacross data sets. Crypto sessions cache this immutable data in a optimal way for the
842b9209dc2SShally Vermaunderlying PMD and this allows further acceleration of the offload of Crypto workloads.
843b9209dc2SShally Verma
844b9209dc2SShally VermaLike symmetric, the Crypto device framework provides APIs to allocate and initialize
845b9209dc2SShally Vermaasymmetric sessions for crypto devices, where sessions are mempool objects.
846b9209dc2SShally VermaIt is the application's responsibility to create and manage the session mempools.
847b9209dc2SShally VermaApplication using both symmetric and asymmetric sessions should allocate and maintain
848b9209dc2SShally Vermadifferent sessions pools for each type.
849b9209dc2SShally Verma
850b9209dc2SShally VermaAn application can use ``rte_cryptodev_get_asym_session_private_size()`` to
851b9209dc2SShally Vermaget the private size of asymmetric session on a given crypto device. This
852b9209dc2SShally Vermafunction would allow an application to calculate the max device asymmetric
853b9209dc2SShally Vermasession size of all crypto devices to create a single session mempool.
854b9209dc2SShally VermaIf instead an application creates multiple asymmetric session mempools,
855b9209dc2SShally Vermathe Crypto device framework also provides ``rte_cryptodev_asym_get_header_session_size()`` to get
856b9209dc2SShally Vermathe size of an uninitialized session.
857b9209dc2SShally Verma
858b9209dc2SShally VermaOnce the session mempools have been created, ``rte_cryptodev_asym_session_create()``
859b9209dc2SShally Vermais used to allocate an uninitialized asymmetric session from the given mempool.
860b9209dc2SShally VermaThe session then must be initialized using ``rte_cryptodev_asym_session_init()``
861b9209dc2SShally Vermafor each of the required crypto devices. An asymmetric transform chain
862b9209dc2SShally Vermais used to specify the operation and its parameters. See the section below for
863b9209dc2SShally Vermadetails on transforms.
864b9209dc2SShally Verma
865b9209dc2SShally VermaWhen a session is no longer used, user must call ``rte_cryptodev_asym_session_clear()``
866b9209dc2SShally Vermafor each of the crypto devices that are using the session, to free all driver
867b9209dc2SShally Vermaprivate asymmetric session data. Once this is done, session should be freed using
868b9209dc2SShally Verma``rte_cryptodev_asym_session_free()`` which returns them to their mempool.
869b9209dc2SShally Verma
870b9209dc2SShally VermaAsymmetric Sessionless Support
871b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
872b9209dc2SShally VermaCurrently asymmetric crypto framework does not support sessionless.
873b9209dc2SShally Verma
874b9209dc2SShally VermaTransforms and Transform Chaining
875b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
876b9209dc2SShally Verma
877b9209dc2SShally VermaAsymmetric Crypto transforms (``rte_crypto_asym_xform``) are the mechanism used
878b9209dc2SShally Vermato specify the details of the asymmetric Crypto operation. Next pointer within
879b9209dc2SShally Vermaxform allows transform to be chained together. Also it is important to note that
880b9209dc2SShally Vermathe order in which the transforms are passed indicates the order of the chaining.
881b9209dc2SShally Verma
882b9209dc2SShally VermaNot all asymmetric crypto xforms are supported for chaining. Currently supported
883b9209dc2SShally Vermaasymmetric crypto chaining is Diffie-Hellman private key generation followed by
884b9209dc2SShally Vermapublic generation. Also, currently API does not support chaining of symmetric and
885b9209dc2SShally Vermaasymmetric crypto xfroms.
886b9209dc2SShally Verma
887b9209dc2SShally VermaEach xform defines specific asymmetric crypto algo. Currently supported are:
888b9209dc2SShally Verma* RSA
889b9209dc2SShally Verma* Modular operations (Exponentiation and Inverse)
890b9209dc2SShally Verma* Diffie-Hellman
891b9209dc2SShally Verma* DSA
892b9209dc2SShally Verma* None - special case where PMD may support a passthrough mode. More for diagnostic purpose
893b9209dc2SShally Verma
894b9209dc2SShally VermaSee *DPDK API Reference* for details on each rte_crypto_xxx_xform struct
895b9209dc2SShally Verma
896b9209dc2SShally VermaAsymmetric Operations
897b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~
898b9209dc2SShally Verma
899b9209dc2SShally VermaThe asymmetric Crypto operation structure contains all the mutable data relating
900b9209dc2SShally Vermato asymmetric cryptographic processing on an input data buffer. It uses either
901b9209dc2SShally VermaRSA, Modular, Diffie-Hellman or DSA operations depending upon session it is attached
902b9209dc2SShally Vermato.
903b9209dc2SShally Verma
904b9209dc2SShally VermaEvery operation must carry a valid session handle which further carries information
905b9209dc2SShally Vermaon xform or xform-chain to be performed on op. Every xform type defines its own set
906b9209dc2SShally Vermaof operational params in their respective rte_crypto_xxx_op_param struct. Depending
907b9209dc2SShally Vermaon xform information within session, PMD picks up and process respective op_param
908b9209dc2SShally Vermastruct.
909b9209dc2SShally VermaUnlike symmetric, asymmetric operations do not use mbufs for input/output.
910b9209dc2SShally VermaThey operate on data buffer of type ``rte_crypto_param``.
911b9209dc2SShally Verma
912b9209dc2SShally VermaSee *DPDK API Reference* for details on each rte_crypto_xxx_op_param struct
913b9209dc2SShally Verma
914b9209dc2SShally VermaAsymmetric crypto Sample code
915b9209dc2SShally Verma-----------------------------
916b9209dc2SShally Verma
917b9209dc2SShally VermaThere's a unit test application test_cryptodev_asym.c inside unit test framework that
918b9209dc2SShally Vermashow how to setup and process asymmetric operations using cryptodev library.
919b9209dc2SShally Verma
920b9209dc2SShally VermaThe following sample code shows the basic steps to compute modular exponentiation
921b9209dc2SShally Vermausing 1024-bit modulus length using openssl PMD available in DPDK (performing other
922b9209dc2SShally Vermacrypto operations is similar except change to respective op and xform setup).
923b9209dc2SShally Verma
924b9209dc2SShally Verma.. code-block:: c
925b9209dc2SShally Verma
926b9209dc2SShally Verma    /*
927b9209dc2SShally Verma     * Simple example to compute modular exponentiation with 1024-bit key
928b9209dc2SShally Verma     *
929b9209dc2SShally Verma     */
930b9209dc2SShally Verma    #define MAX_ASYM_SESSIONS	10
931b9209dc2SShally Verma    #define NUM_ASYM_BUFS	10
932b9209dc2SShally Verma
933b9209dc2SShally Verma    struct rte_mempool *crypto_op_pool, *asym_session_pool;
934b9209dc2SShally Verma    unsigned int asym_session_size;
935b9209dc2SShally Verma    int ret;
936b9209dc2SShally Verma
937b9209dc2SShally Verma    /* Initialize EAL. */
938b9209dc2SShally Verma    ret = rte_eal_init(argc, argv);
939b9209dc2SShally Verma    if (ret < 0)
940b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
941b9209dc2SShally Verma
942b9209dc2SShally Verma    uint8_t socket_id = rte_socket_id();
943b9209dc2SShally Verma
944b9209dc2SShally Verma    /* Create crypto operation pool. */
945b9209dc2SShally Verma    crypto_op_pool = rte_crypto_op_pool_create(
946b9209dc2SShally Verma                                    "crypto_op_pool",
947b9209dc2SShally Verma                                    RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
948b9209dc2SShally Verma                                    NUM_ASYM_BUFS, 0, 0,
949b9209dc2SShally Verma                                    socket_id);
950b9209dc2SShally Verma    if (crypto_op_pool == NULL)
951b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
952b9209dc2SShally Verma
953b9209dc2SShally Verma    /* Create the virtual crypto device. */
954b9209dc2SShally Verma    char args[128];
955b9209dc2SShally Verma    const char *crypto_name = "crypto_openssl";
956b9209dc2SShally Verma    snprintf(args, sizeof(args), "socket_id=%d", socket_id);
957b9209dc2SShally Verma    ret = rte_vdev_init(crypto_name, args);
958b9209dc2SShally Verma    if (ret != 0)
959b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Cannot create virtual device");
960b9209dc2SShally Verma
961b9209dc2SShally Verma    uint8_t cdev_id = rte_cryptodev_get_dev_id(crypto_name);
962b9209dc2SShally Verma
963b9209dc2SShally Verma    /* Get private asym session data size. */
964b9209dc2SShally Verma    asym_session_size = rte_cryptodev_get_asym_private_session_size(cdev_id);
965b9209dc2SShally Verma
966b9209dc2SShally Verma    /*
967b9209dc2SShally Verma     * Create session mempool, with two objects per session,
968b9209dc2SShally Verma     * one for the session header and another one for the
969b9209dc2SShally Verma     * private asym session data for the crypto device.
970b9209dc2SShally Verma     */
971b9209dc2SShally Verma    asym_session_pool = rte_mempool_create("asym_session_pool",
972b9209dc2SShally Verma                                    MAX_ASYM_SESSIONS * 2,
973b9209dc2SShally Verma                                    asym_session_size,
974b9209dc2SShally Verma                                    0,
975b9209dc2SShally Verma                                    0, NULL, NULL, NULL,
976b9209dc2SShally Verma                                    NULL, socket_id,
977b9209dc2SShally Verma                                    0);
978b9209dc2SShally Verma
979b9209dc2SShally Verma    /* Configure the crypto device. */
980b9209dc2SShally Verma    struct rte_cryptodev_config conf = {
981b9209dc2SShally Verma        .nb_queue_pairs = 1,
982b9209dc2SShally Verma        .socket_id = socket_id
983b9209dc2SShally Verma    };
984b9209dc2SShally Verma    struct rte_cryptodev_qp_conf qp_conf = {
985b9209dc2SShally Verma        .nb_descriptors = 2048
986b9209dc2SShally Verma    };
987b9209dc2SShally Verma
988b9209dc2SShally Verma    if (rte_cryptodev_configure(cdev_id, &conf) < 0)
989b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Failed to configure cryptodev %u", cdev_id);
990b9209dc2SShally Verma
991b9209dc2SShally Verma    if (rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
992b9209dc2SShally Verma                            socket_id, asym_session_pool) < 0)
993b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Failed to setup queue pair\n");
994b9209dc2SShally Verma
995b9209dc2SShally Verma    if (rte_cryptodev_start(cdev_id) < 0)
996b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Failed to start device\n");
997b9209dc2SShally Verma
998b9209dc2SShally Verma    /* Setup crypto xform to do modular exponentiation with 1024 bit
999b9209dc2SShally Verma	 * length modulus
1000b9209dc2SShally Verma	 */
1001b9209dc2SShally Verma    struct rte_crypto_asym_xform modex_xform = {
1002b9209dc2SShally Verma		.next = NULL,
1003b9209dc2SShally Verma		.xform_type = RTE_CRYPTO_ASYM_XFORM_MODEX,
1004b9209dc2SShally Verma		.modex = {
1005b9209dc2SShally Verma			.modulus = {
1006b9209dc2SShally Verma				.data =
1007b9209dc2SShally Verma				(uint8_t *)
1008b9209dc2SShally Verma				("\xb3\xa1\xaf\xb7\x13\x08\x00\x0a\x35\xdc\x2b\x20\x8d"
1009b9209dc2SShally Verma				"\xa1\xb5\xce\x47\x8a\xc3\x80\xf4\x7d\x4a\xa2\x62\xfd\x61\x7f"
1010b9209dc2SShally Verma				"\xb5\xa8\xde\x0a\x17\x97\xa0\xbf\xdf\x56\x5a\x3d\x51\x56\x4f"
1011b9209dc2SShally Verma				"\x70\x70\x3f\x63\x6a\x44\x5b\xad\x84\x0d\x3f\x27\x6e\x3b\x34"
1012b9209dc2SShally Verma				"\x91\x60\x14\xb9\xaa\x72\xfd\xa3\x64\xd2\x03\xa7\x53\x87\x9e"
1013b9209dc2SShally Verma				"\x88\x0b\xc1\x14\x93\x1a\x62\xff\xb1\x5d\x74\xcd\x59\x63\x18"
1014b9209dc2SShally Verma				"\x11\x3d\x4f\xba\x75\xd4\x33\x4e\x23\x6b\x7b\x57\x44\xe1\xd3"
1015b9209dc2SShally Verma				"\x03\x13\xa6\xf0\x8b\x60\xb0\x9e\xee\x75\x08\x9d\x71\x63\x13"
1016b9209dc2SShally Verma				"\xcb\xa6\x81\x92\x14\x03\x22\x2d\xde\x55"),
1017b9209dc2SShally Verma				.length = 128
1018b9209dc2SShally Verma			},
1019b9209dc2SShally Verma			.exponent = {
1020b9209dc2SShally Verma				.data = (uint8_t *)("\x01\x00\x01"),
1021b9209dc2SShally Verma				.length = 3
1022b9209dc2SShally Verma			}
1023b9209dc2SShally Verma		}
1024b9209dc2SShally Verma    };
1025b9209dc2SShally Verma    /* Create asym crypto session and initialize it for the crypto device. */
1026b9209dc2SShally Verma    struct rte_cryptodev_asym_session *asym_session;
1027b9209dc2SShally Verma    asym_session = rte_cryptodev_asym_session_create(asym_session_pool);
1028b9209dc2SShally Verma    if (asym_session == NULL)
1029b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Session could not be created\n");
1030b9209dc2SShally Verma
1031b9209dc2SShally Verma    if (rte_cryptodev_asym_session_init(cdev_id, asym_session,
1032b9209dc2SShally Verma                    &modex_xform, asym_session_pool) < 0)
1033b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Session could not be initialized "
1034b9209dc2SShally Verma                    "for the crypto device\n");
1035b9209dc2SShally Verma
1036b9209dc2SShally Verma    /* Get a burst of crypto operations. */
1037b9209dc2SShally Verma    struct rte_crypto_op *crypto_ops[1];
1038b9209dc2SShally Verma    if (rte_crypto_op_bulk_alloc(crypto_op_pool,
1039b9209dc2SShally Verma                            RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
1040b9209dc2SShally Verma                            crypto_ops, 1) == 0)
1041b9209dc2SShally Verma        rte_exit(EXIT_FAILURE, "Not enough crypto operations available\n");
1042b9209dc2SShally Verma
1043b9209dc2SShally Verma    /* Set up the crypto operations. */
1044b9209dc2SShally Verma    struct rte_crypto_asym_op *asym_op = crypto_ops[0]->asym;
1045b9209dc2SShally Verma
1046b9209dc2SShally Verma	/* calculate mod exp of value 0xf8 */
1047b9209dc2SShally Verma    static unsigned char base[] = {0xF8};
1048b9209dc2SShally Verma    asym_op->modex.base.data = base;
1049b9209dc2SShally Verma    asym_op->modex.base.length = sizeof(base);
1050b9209dc2SShally Verma	asym_op->modex.base.iova = base;
1051b9209dc2SShally Verma
1052b9209dc2SShally Verma    /* Attach the asym crypto session to the operation */
1053b9209dc2SShally Verma    rte_crypto_op_attach_asym_session(op, asym_session);
1054b9209dc2SShally Verma
1055b9209dc2SShally Verma    /* Enqueue the crypto operations in the crypto device. */
1056b9209dc2SShally Verma    uint16_t num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, 0,
1057b9209dc2SShally Verma                                            crypto_ops, 1);
1058b9209dc2SShally Verma
1059b9209dc2SShally Verma    /*
1060b9209dc2SShally Verma     * Dequeue the crypto operations until all the operations
1061b9209dc2SShally Verma     * are processed in the crypto device.
1062b9209dc2SShally Verma     */
1063b9209dc2SShally Verma    uint16_t num_dequeued_ops, total_num_dequeued_ops = 0;
1064b9209dc2SShally Verma    do {
1065b9209dc2SShally Verma        struct rte_crypto_op *dequeued_ops[1];
1066b9209dc2SShally Verma        num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, 0,
1067b9209dc2SShally Verma                                        dequeued_ops, 1);
1068b9209dc2SShally Verma        total_num_dequeued_ops += num_dequeued_ops;
1069b9209dc2SShally Verma
1070b9209dc2SShally Verma        /* Check if operation was processed successfully */
1071b9209dc2SShally Verma        if (dequeued_ops[0]->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
1072b9209dc2SShally Verma                rte_exit(EXIT_FAILURE,
1073b9209dc2SShally Verma                        "Some operations were not processed correctly");
1074b9209dc2SShally Verma
1075b9209dc2SShally Verma    } while (total_num_dequeued_ops < num_enqueued_ops);
10760318c02bSDeclan Doherty
10770318c02bSDeclan Doherty
1078b9209dc2SShally VermaAsymmetric Crypto Device API
1079b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10800318c02bSDeclan Doherty
1081b9209dc2SShally VermaThe cryptodev Library API is described in the
108243d162bcSThomas Monjalon`DPDK API Reference <http://doc.dpdk.org/api/>`_
1083