xref: /dpdk/doc/guides/prog_guide/cryptodev_lib.rst (revision 8711af290f353f727989684de2e75c7c41d2779a)
15630257fSFerruh Yigit..  SPDX-License-Identifier: BSD-3-Clause
27adf992fSMarcin Smoczynski    Copyright(c) 2016-2020 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
13*8711af29SNandini PersadThe usages in security protocols are discussed in the :doc:`../howto/security` guide.
140318c02bSDeclan Doherty
150318c02bSDeclan DohertyDesign Principles
160318c02bSDeclan Doherty-----------------
170318c02bSDeclan Doherty
18d629b7b5SJohn McNamaraThe cryptodev library follows the same basic principles as those used in DPDK's
190318c02bSDeclan DohertyEthernet Device framework. The Crypto framework provides a generic Crypto device
200318c02bSDeclan Dohertyframework which supports both physical (hardware) and virtual (software) Crypto
210318c02bSDeclan Dohertydevices as well as a generic Crypto API which allows Crypto devices to be
220318c02bSDeclan Dohertymanaged and configured and supports Crypto operations to be provisioned on
230318c02bSDeclan DohertyCrypto poll mode driver.
240318c02bSDeclan Doherty
250318c02bSDeclan Doherty
260318c02bSDeclan DohertyDevice Management
270318c02bSDeclan Doherty-----------------
280318c02bSDeclan Doherty
290318c02bSDeclan DohertyDevice Creation
300318c02bSDeclan Doherty~~~~~~~~~~~~~~~
310318c02bSDeclan Doherty
320318c02bSDeclan DohertyPhysical Crypto devices are discovered during the PCI probe/enumeration of the
330318c02bSDeclan DohertyEAL function which is executed at DPDK initialization, based on
340318c02bSDeclan Dohertytheir PCI device identifier, each unique PCI BDF (bus/bridge, device,
350318c02bSDeclan Dohertyfunction). Specific physical Crypto devices, like other physical devices in DPDK
36db27370bSStephen Hemmingercan be listed using the EAL command line options.
370318c02bSDeclan Doherty
380318c02bSDeclan DohertyVirtual devices can be created by two mechanisms, either using the EAL command
390318c02bSDeclan Dohertyline options or from within the application using an EAL API directly.
400318c02bSDeclan Doherty
410318c02bSDeclan DohertyFrom the command line using the --vdev EAL option
420318c02bSDeclan Doherty
430318c02bSDeclan Doherty.. code-block:: console
440318c02bSDeclan Doherty
45e1fc5b76SPablo de Lara   --vdev  'crypto_aesni_mb0,max_nb_queue_pairs=2,socket_id=0'
460318c02bSDeclan Doherty
47c149818bSVipin Varghese.. Note::
48c149818bSVipin Varghese
49c149818bSVipin Varghese   * If DPDK application requires multiple software crypto PMD devices then required
50c149818bSVipin Varghese     number of ``--vdev`` with appropriate libraries are to be added.
51c149818bSVipin Varghese
52d629b7b5SJohn McNamara   * An Application with crypto PMD instances sharing the same library requires unique ID.
53c149818bSVipin Varghese
54c149818bSVipin Varghese   Example: ``--vdev  'crypto_aesni_mb0' --vdev  'crypto_aesni_mb1'``
55c149818bSVipin Varghese
568b283e90SThierry HerbelotOr using the rte_vdev_init API within the application code.
570318c02bSDeclan Doherty
580318c02bSDeclan Doherty.. code-block:: c
590318c02bSDeclan Doherty
6030883f3eSPablo de Lara   rte_vdev_init("crypto_aesni_mb",
61e1fc5b76SPablo de Lara                     "max_nb_queue_pairs=2,socket_id=0")
620318c02bSDeclan Doherty
630318c02bSDeclan DohertyAll virtual Crypto devices support the following initialization parameters:
640318c02bSDeclan Doherty
650318c02bSDeclan Doherty* ``max_nb_queue_pairs`` - maximum number of queue pairs supported by the device.
660318c02bSDeclan Doherty* ``socket_id`` - socket on which to allocate the device resources on.
670318c02bSDeclan Doherty
680318c02bSDeclan Doherty
690318c02bSDeclan DohertyDevice Identification
700318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~
710318c02bSDeclan Doherty
720318c02bSDeclan DohertyEach device, whether virtual or physical is uniquely designated by two
730318c02bSDeclan Dohertyidentifiers:
740318c02bSDeclan Doherty
750318c02bSDeclan Doherty- A unique device index used to designate the Crypto device in all functions
760318c02bSDeclan Doherty  exported by the cryptodev API.
770318c02bSDeclan Doherty
780318c02bSDeclan Doherty- A device name used to designate the Crypto device in console messages, for
790318c02bSDeclan Doherty  administration or debugging purposes. For ease of use, the port name includes
800318c02bSDeclan Doherty  the port index.
810318c02bSDeclan Doherty
820318c02bSDeclan Doherty
830318c02bSDeclan DohertyDevice Configuration
840318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~
850318c02bSDeclan Doherty
860318c02bSDeclan DohertyThe configuration of each Crypto device includes the following operations:
870318c02bSDeclan Doherty
880318c02bSDeclan Doherty- Allocation of resources, including hardware resources if a physical device.
890318c02bSDeclan Doherty- Resetting the device into a well-known default state.
900318c02bSDeclan Doherty- Initialization of statistics counters.
910318c02bSDeclan Doherty
920318c02bSDeclan DohertyThe rte_cryptodev_configure API is used to configure a Crypto device.
930318c02bSDeclan Doherty
940318c02bSDeclan Doherty.. code-block:: c
950318c02bSDeclan Doherty
960318c02bSDeclan Doherty   int rte_cryptodev_configure(uint8_t dev_id,
970318c02bSDeclan Doherty                               struct rte_cryptodev_config *config)
980318c02bSDeclan Doherty
99bb59dac7SPablo de LaraThe ``rte_cryptodev_config`` structure is used to pass the configuration
100bb59dac7SPablo de Laraparameters for socket selection and number of queue pairs.
1010318c02bSDeclan Doherty
102d2d7f019SAkhil Goyal.. literalinclude:: ../../../lib/cryptodev/rte_cryptodev.h
103d2d7f019SAkhil Goyal   :language: c
104d2d7f019SAkhil Goyal   :start-after: Structure rte_cryptodev_config 8<
105d2d7f019SAkhil Goyal   :end-before: >8 End of structure rte_cryptodev_config.
1060318c02bSDeclan Doherty
1070318c02bSDeclan Doherty
1080318c02bSDeclan DohertyConfiguration of Queue Pairs
1090318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1100318c02bSDeclan Doherty
1110318c02bSDeclan DohertyEach Crypto devices queue pair is individually configured through the
1120318c02bSDeclan Doherty``rte_cryptodev_queue_pair_setup`` API.
1130318c02bSDeclan DohertyEach queue pairs resources may be allocated on a specified socket.
1140318c02bSDeclan Doherty
1150318c02bSDeclan Doherty.. code-block:: c
1160318c02bSDeclan Doherty
1170318c02bSDeclan Doherty    int rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
1180318c02bSDeclan Doherty                const struct rte_cryptodev_qp_conf *qp_conf,
1190318c02bSDeclan Doherty                int socket_id)
1200318c02bSDeclan Doherty
121d2d7f019SAkhil Goyal
122d2d7f019SAkhil Goyal.. literalinclude:: ../../../lib/cryptodev/rte_cryptodev.h
123d2d7f019SAkhil Goyal   :language: c
124d2d7f019SAkhil Goyal   :start-after: Structure rte_cryptodev_qp_conf 8<
125d2d7f019SAkhil Goyal   :end-before: >8 End of structure rte_cryptodev_qp_conf.
1260318c02bSDeclan Doherty
1270318c02bSDeclan Doherty
1282a440d6aSAkhil GoyalThe field ``mp_session`` is used for creating temporary session to process
1292a440d6aSAkhil Goyalthe crypto operations in the session-less mode.
130725d2a7fSFan ZhangThey can be the same other different mempools. Please note not all Cryptodev
131725d2a7fSFan ZhangPMDs supports session-less mode.
132725d2a7fSFan Zhang
133725d2a7fSFan Zhang
1340318c02bSDeclan DohertyLogical Cores, Memory and Queues Pair Relationships
1350318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1360318c02bSDeclan Doherty
1370318c02bSDeclan DohertyThe Crypto device Library as the Poll Mode Driver library support NUMA for when
1380318c02bSDeclan Dohertya processor’s logical cores and interfaces utilize its local memory. Therefore
1390318c02bSDeclan DohertyCrypto operations, and in the case of symmetric Crypto operations, the session
1400318c02bSDeclan Dohertyand the mbuf being operated on, should be allocated from memory pools created
1410318c02bSDeclan Dohertyin the local memory. The buffers should, if possible, remain on the local
1420318c02bSDeclan Dohertyprocessor to obtain the best performance results and buffer descriptors should
1430318c02bSDeclan Dohertybe populated with mbufs allocated from a mempool allocated from local memory.
1440318c02bSDeclan Doherty
1450318c02bSDeclan DohertyThe run-to-completion model also performs better, especially in the case of
1460318c02bSDeclan Dohertyvirtual Crypto devices, if the Crypto operation and session and data buffer is
1470318c02bSDeclan Dohertyin local memory instead of a remote processor's memory. This is also true for
1480318c02bSDeclan Dohertythe pipe-line model provided all logical cores used are located on the same
1490318c02bSDeclan Dohertyprocessor.
1500318c02bSDeclan Doherty
1510318c02bSDeclan DohertyMultiple logical cores should never share the same queue pair for enqueuing
1520318c02bSDeclan Dohertyoperations or dequeuing operations on the same Crypto device since this would
1530318c02bSDeclan Dohertyrequire global locks and hinder performance. It is however possible to use a
1540318c02bSDeclan Dohertydifferent logical core to dequeue an operation on a queue pair from the logical
1550318c02bSDeclan Dohertycore which it was enqueued on. This means that a crypto burst enqueue/dequeue
1560318c02bSDeclan DohertyAPIs are a logical place to transition from one logical core to another in a
1570318c02bSDeclan Dohertypacket processing pipeline.
1580318c02bSDeclan Doherty
1590318c02bSDeclan Doherty
1600318c02bSDeclan DohertyDevice Features and Capabilities
1610318c02bSDeclan Doherty---------------------------------
1620318c02bSDeclan Doherty
1630318c02bSDeclan DohertyCrypto devices define their functionality through two mechanisms, global device
1640318c02bSDeclan Dohertyfeatures and algorithm capabilities. Global devices features identify device
1650318c02bSDeclan Dohertywide level features which are applicable to the whole device such as
166b9209dc2SShally Vermathe device having hardware acceleration or supporting symmetric and/or asymmetric
167b9209dc2SShally VermaCrypto operations.
1680318c02bSDeclan Doherty
1690318c02bSDeclan DohertyThe capabilities mechanism defines the individual algorithms/functions which
17083984b7fSPablo de Larathe device supports, such as a specific symmetric Crypto cipher,
17183984b7fSPablo de Laraauthentication operation or Authenticated Encryption with Associated Data
17283984b7fSPablo de Lara(AEAD) operation.
1730318c02bSDeclan Doherty
1740318c02bSDeclan Doherty
1750318c02bSDeclan DohertyDevice Features
1760318c02bSDeclan Doherty~~~~~~~~~~~~~~~
1770318c02bSDeclan Doherty
1780318c02bSDeclan DohertyCurrently the following Crypto device features are defined:
1790318c02bSDeclan Doherty
1800318c02bSDeclan Doherty* Symmetric Crypto operations
1810318c02bSDeclan Doherty* Asymmetric Crypto operations
1820318c02bSDeclan Doherty* Chaining of symmetric Crypto operations
1830318c02bSDeclan Doherty* SSE accelerated SIMD vector operations
1840318c02bSDeclan Doherty* AVX accelerated SIMD vector operations
1850318c02bSDeclan Doherty* AVX2 accelerated SIMD vector operations
1860318c02bSDeclan Doherty* AESNI accelerated instructions
1870318c02bSDeclan Doherty* Hardware off-load processing
1880318c02bSDeclan Doherty
1890318c02bSDeclan Doherty
1900318c02bSDeclan DohertyDevice Operation Capabilities
1910318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1920318c02bSDeclan Doherty
1930318c02bSDeclan DohertyCrypto capabilities which identify particular algorithm which the Crypto PMD
1940318c02bSDeclan Dohertysupports are  defined by the operation type, the operation transform, the
1950318c02bSDeclan Dohertytransform identifier and then the particulars of the transform. For the full
1960318c02bSDeclan Dohertyscope of the Crypto capability see the definition of the structure in the
1970318c02bSDeclan Doherty*DPDK API Reference*.
1980318c02bSDeclan Doherty
1990318c02bSDeclan Doherty.. code-block:: c
2000318c02bSDeclan Doherty
2010318c02bSDeclan Doherty   struct rte_cryptodev_capabilities;
2020318c02bSDeclan Doherty
2030318c02bSDeclan DohertyEach Crypto poll mode driver defines its own private array of capabilities
2040318c02bSDeclan Dohertyfor the operations it supports. Below is an example of the capabilities for a
2050318c02bSDeclan DohertyPMD which supports the authentication algorithm SHA1_HMAC and the cipher
2060318c02bSDeclan Dohertyalgorithm AES_CBC.
2070318c02bSDeclan Doherty
2080318c02bSDeclan Doherty.. code-block:: c
2090318c02bSDeclan Doherty
2100318c02bSDeclan Doherty    static const struct rte_cryptodev_capabilities pmd_capabilities[] = {
2110318c02bSDeclan Doherty        {    /* SHA1 HMAC */
2120318c02bSDeclan Doherty            .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
2130318c02bSDeclan Doherty            .sym = {
2140318c02bSDeclan Doherty                .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
2150318c02bSDeclan Doherty                .auth = {
2160318c02bSDeclan Doherty                    .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
2170318c02bSDeclan Doherty                    .block_size = 64,
2180318c02bSDeclan Doherty                    .key_size = {
2190318c02bSDeclan Doherty                        .min = 64,
2200318c02bSDeclan Doherty                        .max = 64,
2210318c02bSDeclan Doherty                        .increment = 0
2220318c02bSDeclan Doherty                    },
2230318c02bSDeclan Doherty                    .digest_size = {
2240318c02bSDeclan Doherty                        .min = 12,
2250318c02bSDeclan Doherty                        .max = 12,
2260318c02bSDeclan Doherty                        .increment = 0
2270318c02bSDeclan Doherty                    },
228acf86169SPablo de Lara                    .aad_size = { 0 },
229acf86169SPablo de Lara                    .iv_size = { 0 }
2300318c02bSDeclan Doherty                }
2310318c02bSDeclan Doherty            }
2320318c02bSDeclan Doherty        },
2330318c02bSDeclan Doherty        {    /* AES CBC */
2340318c02bSDeclan Doherty            .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
2350318c02bSDeclan Doherty            .sym = {
2360318c02bSDeclan Doherty                .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
2370318c02bSDeclan Doherty                .cipher = {
2380318c02bSDeclan Doherty                    .algo = RTE_CRYPTO_CIPHER_AES_CBC,
2390318c02bSDeclan Doherty                    .block_size = 16,
2400318c02bSDeclan Doherty                    .key_size = {
2410318c02bSDeclan Doherty                        .min = 16,
2420318c02bSDeclan Doherty                        .max = 32,
2430318c02bSDeclan Doherty                        .increment = 8
2440318c02bSDeclan Doherty                    },
2450318c02bSDeclan Doherty                    .iv_size = {
2460318c02bSDeclan Doherty                        .min = 16,
2470318c02bSDeclan Doherty                        .max = 16,
2480318c02bSDeclan Doherty                        .increment = 0
2490318c02bSDeclan Doherty                    }
2500318c02bSDeclan Doherty                }
2510318c02bSDeclan Doherty            }
2520318c02bSDeclan Doherty        }
2530318c02bSDeclan Doherty    }
2540318c02bSDeclan Doherty
2550318c02bSDeclan Doherty
2560318c02bSDeclan DohertyCapabilities Discovery
2570318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~
2580318c02bSDeclan Doherty
2590318c02bSDeclan DohertyDiscovering the features and capabilities of a Crypto device poll mode driver
2600318c02bSDeclan Dohertyis achieved through the ``rte_cryptodev_info_get`` function.
2610318c02bSDeclan Doherty
2620318c02bSDeclan Doherty.. code-block:: c
2630318c02bSDeclan Doherty
2640318c02bSDeclan Doherty   void rte_cryptodev_info_get(uint8_t dev_id,
2650318c02bSDeclan Doherty                               struct rte_cryptodev_info *dev_info);
2660318c02bSDeclan Doherty
2670318c02bSDeclan DohertyThis allows the user to query a specific Crypto PMD and get all the device
2680318c02bSDeclan Dohertyfeatures and capabilities. The ``rte_cryptodev_info`` structure contains all the
2690318c02bSDeclan Dohertyrelevant information for the device.
2700318c02bSDeclan Doherty
271d2d7f019SAkhil Goyal.. literalinclude:: ../../../lib/cryptodev/rte_cryptodev.h
272d2d7f019SAkhil Goyal   :language: c
273d2d7f019SAkhil Goyal   :start-after: Structure rte_cryptodev_info 8<
274d2d7f019SAkhil Goyal   :end-before: >8 End of structure rte_cryptodev_info.
2750318c02bSDeclan Doherty
2760318c02bSDeclan Doherty
2770318c02bSDeclan DohertyOperation Processing
2780318c02bSDeclan Doherty--------------------
2790318c02bSDeclan Doherty
2800318c02bSDeclan DohertyScheduling of Crypto operations on DPDK's application data path is
2810318c02bSDeclan Dohertyperformed using a burst oriented asynchronous API set. A queue pair on a Crypto
2820318c02bSDeclan Dohertydevice accepts a burst of Crypto operations using enqueue burst API. On physical
2830318c02bSDeclan DohertyCrypto devices the enqueue burst API will place the operations to be processed
2840318c02bSDeclan Dohertyon the devices hardware input queue, for virtual devices the processing of the
2850318c02bSDeclan DohertyCrypto operations is usually completed during the enqueue call to the Crypto
2860318c02bSDeclan Dohertydevice. The dequeue burst API will retrieve any processed operations available
2870318c02bSDeclan Dohertyfrom the queue pair on the Crypto device, from physical devices this is usually
2880318c02bSDeclan Dohertydirectly from the devices processed queue, and for virtual device's from a
2896b1a74efSThierry Herbelot``rte_ring`` where processed operations are placed after being processed on the
2900318c02bSDeclan Dohertyenqueue call.
2910318c02bSDeclan Doherty
2920318c02bSDeclan Doherty
293fe84aaeeSAbhinandan GujjarPrivate data
294fe84aaeeSAbhinandan Gujjar~~~~~~~~~~~~
295fe84aaeeSAbhinandan GujjarFor session-based operations, the set and get API provides a mechanism for an
2962d349f60SFiona Traheapplication to store and retrieve the private user data information stored along
2972d349f60SFiona Trahewith the crypto session.
298fe84aaeeSAbhinandan Gujjar
299fe84aaeeSAbhinandan GujjarFor example, suppose an application is submitting a crypto operation with a session
3002d349f60SFiona Traheassociated and wants to indicate private user data information which is required to be
301fe84aaeeSAbhinandan Gujjarused after completion of the crypto operation. In this case, the application can use
3022d349f60SFiona Trahethe set API to set the user data and retrieve it using get API.
303fe84aaeeSAbhinandan Gujjar
304fe84aaeeSAbhinandan Gujjar.. code-block:: c
305fe84aaeeSAbhinandan Gujjar
3062d349f60SFiona Trahe	int rte_cryptodev_sym_session_set_user_data(
307fe84aaeeSAbhinandan Gujjar		struct rte_cryptodev_sym_session *sess,	void *data, uint16_t size);
308fe84aaeeSAbhinandan Gujjar
3092d349f60SFiona Trahe	void * rte_cryptodev_sym_session_get_user_data(
310fe84aaeeSAbhinandan Gujjar		struct rte_cryptodev_sym_session *sess);
311fe84aaeeSAbhinandan Gujjar
3129e5f5ecbSFan ZhangPlease note the ``size`` passed to set API cannot be bigger than the predefined
3139e5f5ecbSFan Zhang``user_data_sz`` when creating the session header mempool, otherwise the
3149e5f5ecbSFan Zhangfunction will return error. Also when ``user_data_sz`` was defined as ``0`` when
3159e5f5ecbSFan Zhangcreating the session header mempool, the get API will always return ``NULL``.
316fe84aaeeSAbhinandan Gujjar
3172d349f60SFiona TraheFor session-less mode, the private user data information can be placed along with the
318fe84aaeeSAbhinandan Gujjar``struct rte_crypto_op``. The ``rte_crypto_op::private_data_offset`` indicates the
319fe84aaeeSAbhinandan Gujjarstart of private data information. The offset is counted from the start of the
320fe84aaeeSAbhinandan Gujjarrte_crypto_op including other crypto information such as the IVs (since there can
321fe84aaeeSAbhinandan Gujjarbe an IV also for authentication).
322fe84aaeeSAbhinandan Gujjar
3231c3ffb95SAbhinandan GujjarUser callback APIs
3241c3ffb95SAbhinandan Gujjar~~~~~~~~~~~~~~~~~~
3251c3ffb95SAbhinandan GujjarThe add APIs configures a user callback function to be called for each burst of crypto
3261c3ffb95SAbhinandan Gujjarops received/sent on a given crypto device queue pair. The return value is a pointer
3271c3ffb95SAbhinandan Gujjarthat can be used later to remove the callback using remove API. Application is expected
3281c3ffb95SAbhinandan Gujjarto register a callback function of type ``rte_cryptodev_callback_fn``. Multiple callback
3291c3ffb95SAbhinandan Gujjarfunctions can be added for a given queue pair. API does not restrict on maximum number of
3301c3ffb95SAbhinandan Gujjarcallbacks.
3311c3ffb95SAbhinandan Gujjar
3321c3ffb95SAbhinandan GujjarCallbacks registered by application would not survive ``rte_cryptodev_configure`` as it
3331c3ffb95SAbhinandan Gujjarreinitializes the callback list. It is user responsibility to remove all installed
3341c3ffb95SAbhinandan Gujjarcallbacks before calling ``rte_cryptodev_configure`` to avoid possible memory leakage.
3351c3ffb95SAbhinandan Gujjar
3361c3ffb95SAbhinandan GujjarSo, the application is expected to add user callback after ``rte_cryptodev_configure``.
3371c3ffb95SAbhinandan GujjarThe callbacks can also be added at the runtime. These callbacks get executed when
3381c3ffb95SAbhinandan Gujjar``rte_cryptodev_enqueue_burst``/``rte_cryptodev_dequeue_burst`` is called.
3391c3ffb95SAbhinandan Gujjar
3401c3ffb95SAbhinandan Gujjar.. code-block:: c
3411c3ffb95SAbhinandan Gujjar
3421c3ffb95SAbhinandan Gujjar	struct rte_cryptodev_cb *
3431c3ffb95SAbhinandan Gujjar		rte_cryptodev_add_enq_callback(uint8_t dev_id, uint16_t qp_id,
3441c3ffb95SAbhinandan Gujjar					       rte_cryptodev_callback_fn cb_fn,
3451c3ffb95SAbhinandan Gujjar					       void *cb_arg);
3461c3ffb95SAbhinandan Gujjar
3471c3ffb95SAbhinandan Gujjar	struct rte_cryptodev_cb *
3481c3ffb95SAbhinandan Gujjar		rte_cryptodev_add_deq_callback(uint8_t dev_id, uint16_t qp_id,
3491c3ffb95SAbhinandan Gujjar					       rte_cryptodev_callback_fn cb_fn,
3501c3ffb95SAbhinandan Gujjar					       void *cb_arg);
3511c3ffb95SAbhinandan Gujjar
3521c3ffb95SAbhinandan Gujjar	uint16_t (* rte_cryptodev_callback_fn)(uint16_t dev_id, uint16_t qp_id,
3531c3ffb95SAbhinandan Gujjar					       struct rte_crypto_op **ops,
3541c3ffb95SAbhinandan Gujjar					       uint16_t nb_ops, void *user_param);
3551c3ffb95SAbhinandan Gujjar
3561c3ffb95SAbhinandan GujjarThe remove API removes a callback function added by
3571c3ffb95SAbhinandan Gujjar``rte_cryptodev_add_enq_callback``/``rte_cryptodev_add_deq_callback``.
3581c3ffb95SAbhinandan Gujjar
3591c3ffb95SAbhinandan Gujjar.. code-block:: c
3601c3ffb95SAbhinandan Gujjar
3611c3ffb95SAbhinandan Gujjar	int rte_cryptodev_remove_enq_callback(uint8_t dev_id, uint16_t qp_id,
3621c3ffb95SAbhinandan Gujjar					      struct rte_cryptodev_cb *cb);
3631c3ffb95SAbhinandan Gujjar
3641c3ffb95SAbhinandan Gujjar	int rte_cryptodev_remove_deq_callback(uint8_t dev_id, uint16_t qp_id,
3651c3ffb95SAbhinandan Gujjar					      struct rte_cryptodev_cb *cb);
3661c3ffb95SAbhinandan Gujjar
367fe84aaeeSAbhinandan Gujjar
3680318c02bSDeclan DohertyEnqueue / Dequeue Burst APIs
3690318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3700318c02bSDeclan Doherty
3710318c02bSDeclan DohertyThe burst enqueue API uses a Crypto device identifier and a queue pair
3720318c02bSDeclan Dohertyidentifier to specify the Crypto device queue pair to schedule the processing on.
3730318c02bSDeclan DohertyThe ``nb_ops`` parameter is the number of operations to process which are
3740318c02bSDeclan Dohertysupplied in the ``ops`` array of ``rte_crypto_op`` structures.
3750318c02bSDeclan DohertyThe enqueue function returns the number of operations it actually enqueued for
3760318c02bSDeclan Dohertyprocessing, a return value equal to ``nb_ops`` means that all packets have been
3770318c02bSDeclan Dohertyenqueued.
3780318c02bSDeclan Doherty
3790318c02bSDeclan Doherty.. code-block:: c
3800318c02bSDeclan Doherty
3810318c02bSDeclan Doherty   uint16_t rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
3820318c02bSDeclan Doherty                                        struct rte_crypto_op **ops, uint16_t nb_ops)
3830318c02bSDeclan Doherty
3840318c02bSDeclan DohertyThe dequeue API uses the same format as the enqueue API of processed but
3850318c02bSDeclan Dohertythe ``nb_ops`` and ``ops`` parameters are now used to specify the max processed
3860318c02bSDeclan Dohertyoperations the user wishes to retrieve and the location in which to store them.
3870318c02bSDeclan DohertyThe API call returns the actual number of processed operations returned, this
3880318c02bSDeclan Dohertycan never be larger than ``nb_ops``.
3890318c02bSDeclan Doherty
3900318c02bSDeclan Doherty.. code-block:: c
3910318c02bSDeclan Doherty
3920318c02bSDeclan Doherty   uint16_t rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
3930318c02bSDeclan Doherty                                        struct rte_crypto_op **ops, uint16_t nb_ops)
3940318c02bSDeclan Doherty
3950318c02bSDeclan Doherty
3960318c02bSDeclan DohertyOperation Representation
3970318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~
3980318c02bSDeclan Doherty
3990318c02bSDeclan DohertyAn Crypto operation is represented by an rte_crypto_op structure, which is a
4000318c02bSDeclan Dohertygeneric metadata container for all necessary information required for the
4010318c02bSDeclan DohertyCrypto operation to be processed on a particular Crypto device poll mode driver.
4020318c02bSDeclan Doherty
4030318c02bSDeclan Doherty.. figure:: img/crypto_op.*
4040318c02bSDeclan Doherty
4055209df0dSPablo de LaraThe operation structure includes the operation type, the operation status
4065209df0dSPablo de Laraand the session type (session-based/less), a reference to the operation
4075209df0dSPablo de Laraspecific data, which can vary in size and content depending on the operation
4085209df0dSPablo de Larabeing provisioned. It also contains the source mempool for the operation,
409b1f6192bSPablo de Laraif it allocated from a mempool.
4100318c02bSDeclan Doherty
4110318c02bSDeclan DohertyIf Crypto operations are allocated from a Crypto operation mempool, see next
4120318c02bSDeclan Dohertysection, there is also the ability to allocate private memory with the
4130318c02bSDeclan Dohertyoperation for applications purposes.
4140318c02bSDeclan Doherty
4150318c02bSDeclan DohertyApplication software is responsible for specifying all the operation specific
4160318c02bSDeclan Dohertyfields in the ``rte_crypto_op`` structure which are then used by the Crypto PMD
4170318c02bSDeclan Dohertyto process the requested operation.
4180318c02bSDeclan Doherty
4190318c02bSDeclan Doherty
4200318c02bSDeclan DohertyOperation Management and Allocation
4210318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4220318c02bSDeclan Doherty
4230318c02bSDeclan DohertyThe cryptodev library provides an API set for managing Crypto operations which
4240318c02bSDeclan Dohertyutilize the Mempool Library to allocate operation buffers. Therefore, it ensures
425d629b7b5SJohn McNamarathat the crypto operation is interleaved optimally across the channels and
4260318c02bSDeclan Dohertyranks for optimal processing.
4270318c02bSDeclan DohertyA ``rte_crypto_op`` contains a field indicating the pool that it originated from.
4280318c02bSDeclan DohertyWhen calling ``rte_crypto_op_free(op)``, the operation returns to its original pool.
4290318c02bSDeclan Doherty
4300318c02bSDeclan Doherty.. code-block:: c
4310318c02bSDeclan Doherty
4320318c02bSDeclan Doherty   extern struct rte_mempool *
4330318c02bSDeclan Doherty   rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
4340318c02bSDeclan Doherty                             unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
4350318c02bSDeclan Doherty                             int socket_id);
4360318c02bSDeclan Doherty
4370318c02bSDeclan DohertyDuring pool creation ``rte_crypto_op_init()`` is called as a constructor to
4380318c02bSDeclan Dohertyinitialize each Crypto operation which subsequently calls
4390318c02bSDeclan Doherty``__rte_crypto_op_reset()`` to configure any operation type specific fields based
4400318c02bSDeclan Dohertyon the type parameter.
4410318c02bSDeclan Doherty
4420318c02bSDeclan Doherty
4430318c02bSDeclan Doherty``rte_crypto_op_alloc()`` and ``rte_crypto_op_bulk_alloc()`` are used to allocate
4440318c02bSDeclan DohertyCrypto operations of a specific type from a given Crypto operation mempool.
4450318c02bSDeclan Doherty``__rte_crypto_op_reset()`` is called on each operation before being returned to
4460318c02bSDeclan Dohertyallocate to a user so the operation is always in a good known state before use
4470318c02bSDeclan Dohertyby the application.
4480318c02bSDeclan Doherty
4490318c02bSDeclan Doherty.. code-block:: c
4500318c02bSDeclan Doherty
4510318c02bSDeclan Doherty   struct rte_crypto_op *rte_crypto_op_alloc(struct rte_mempool *mempool,
4520318c02bSDeclan Doherty                                             enum rte_crypto_op_type type)
4530318c02bSDeclan Doherty
4540318c02bSDeclan Doherty   unsigned rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
4550318c02bSDeclan Doherty                                     enum rte_crypto_op_type type,
4560318c02bSDeclan Doherty                                     struct rte_crypto_op **ops, uint16_t nb_ops)
4570318c02bSDeclan Doherty
4580318c02bSDeclan Doherty``rte_crypto_op_free()`` is called by the application to return an operation to
4590318c02bSDeclan Dohertyits allocating pool.
4600318c02bSDeclan Doherty
4610318c02bSDeclan Doherty.. code-block:: c
4620318c02bSDeclan Doherty
4630318c02bSDeclan Doherty   void rte_crypto_op_free(struct rte_crypto_op *op)
4640318c02bSDeclan Doherty
4650318c02bSDeclan Doherty
4660318c02bSDeclan DohertySymmetric Cryptography Support
4670318c02bSDeclan Doherty------------------------------
4680318c02bSDeclan Doherty
4690318c02bSDeclan DohertyThe cryptodev library currently provides support for the following symmetric
4700318c02bSDeclan DohertyCrypto operations; cipher, authentication, including chaining of these
4710318c02bSDeclan Dohertyoperations, as well as also supporting AEAD operations.
4720318c02bSDeclan Doherty
4730318c02bSDeclan Doherty
4740318c02bSDeclan DohertySession and Session Management
475e3346dfcSPablo de Lara~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4760318c02bSDeclan Doherty
477bb59dac7SPablo de LaraSessions are used in symmetric cryptographic processing to store the immutable
4780318c02bSDeclan Dohertydata defined in a cryptographic transform which is used in the operation
4790318c02bSDeclan Dohertyprocessing of a packet flow. Sessions are used to manage information such as
4800318c02bSDeclan Dohertyexpand cipher keys and HMAC IPADs and OPADs, which need to be calculated for a
4810318c02bSDeclan Dohertyparticular Crypto operation, but are immutable on a packet to packet basis for
4820318c02bSDeclan Dohertya flow. Crypto sessions cache this immutable data in a optimal way for the
4830318c02bSDeclan Dohertyunderlying PMD and this allows further acceleration of the offload of
4840318c02bSDeclan DohertyCrypto workloads.
4850318c02bSDeclan Doherty
4861d6f8988SFan ZhangThe Crypto device framework provides APIs to create session mempool and allocate
4871d6f8988SFan Zhangand initialize sessions for crypto devices, where sessions are mempool objects.
4881d6f8988SFan ZhangThe application has to use ``rte_cryptodev_sym_session_pool_create()`` to
4899a8569acSFan Zhangcreate the session mempool header and the private data with the size specified
4909a8569acSFan Zhangby the user through the ``elt_size`` parameter in the function.
4919a8569acSFan ZhangThe session private data is for the driver to initialize and access
4929a8569acSFan Zhangduring crypto operations, hence the ``elt_size`` should be big enough
4939a8569acSFan Zhangfor all drivers that will share this mempool.
4949a8569acSFan ZhangTo obtain the proper session private data size of a crypto device,
4959a8569acSFan Zhangthe user can call ``rte_cryptodev_sym_get_private_session_size()`` function.
4969a8569acSFan ZhangIn case of heterogeneous crypto devices which will share the same session mempool,
4979a8569acSFan Zhangthe maximum session private data size of them should be passed.
4980318c02bSDeclan Doherty
499bb59dac7SPablo de LaraOnce the session mempools have been created, ``rte_cryptodev_sym_session_create()``
5009a8569acSFan Zhangis used to allocate and initialize the session from the given mempool.
5019a8569acSFan ZhangThe created session can ONLY be used by the crypto devices sharing the same driver ID
5029a8569acSFan Zhangas the device ID passed into the function as the parameter.
5039a8569acSFan ZhangIn addition, a symmetric transform chain is used to specify the operation and its parameters.
5049a8569acSFan ZhangSee the section below for details on transforms.
5050318c02bSDeclan Doherty
5069a8569acSFan ZhangWhen a session is no longer used, user must call ``rte_cryptodev_sym_session_free()``
5079a8569acSFan Zhangto uninitialize the session data and return the session
5089a8569acSFan Zhangback to the mempool it belongs.
5090318c02bSDeclan Doherty
5100318c02bSDeclan Doherty
5110318c02bSDeclan DohertyTransforms and Transform Chaining
5120318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5130318c02bSDeclan Doherty
5140318c02bSDeclan DohertySymmetric Crypto transforms (``rte_crypto_sym_xform``) are the mechanism used
5150318c02bSDeclan Dohertyto specify the details of the Crypto operation. For chaining of symmetric
5160318c02bSDeclan Dohertyoperations such as cipher encrypt and authentication generate, the next pointer
5170318c02bSDeclan Dohertyallows transform to be chained together. Crypto devices which support chaining
51848903a79SFiona Trahemust publish the chaining of symmetric Crypto operations feature flag. Allocation of the
519f43d3dbbSDavid Marchandxform structure is in the application domain. To allow future API extensions in a
52048903a79SFiona Trahebackwardly compatible manner, e.g. addition of a new parameter, the application should
52148903a79SFiona Trahezero the full xform struct before populating it.
5220318c02bSDeclan Doherty
52383984b7fSPablo de LaraCurrently there are three transforms types cipher, authentication and AEAD.
52483984b7fSPablo de LaraAlso it is important to note that the order in which the
5250318c02bSDeclan Dohertytransforms are passed indicates the order of the chaining.
5260318c02bSDeclan Doherty
527d2d7f019SAkhil Goyal.. literalinclude:: ../../../lib/cryptodev/rte_crypto_sym.h
528d2d7f019SAkhil Goyal   :language: c
529d2d7f019SAkhil Goyal   :start-after: Structure rte_crypto_sym_xform 8<
530d2d7f019SAkhil Goyal   :end-before: >8 End of structure rte_crypto_sym_xform.
5310318c02bSDeclan Doherty
5320318c02bSDeclan DohertyThe API does not place a limit on the number of transforms that can be chained
5330318c02bSDeclan Dohertytogether but this will be limited by the underlying Crypto device poll mode
5340318c02bSDeclan Dohertydriver which is processing the operation.
5350318c02bSDeclan Doherty
5360318c02bSDeclan Doherty.. figure:: img/crypto_xform_chain.*
5370318c02bSDeclan Doherty
5380318c02bSDeclan Doherty
5390318c02bSDeclan DohertySymmetric Operations
5400318c02bSDeclan Doherty~~~~~~~~~~~~~~~~~~~~
5410318c02bSDeclan Doherty
5420318c02bSDeclan DohertyThe symmetric Crypto operation structure contains all the mutable data relating
5430318c02bSDeclan Dohertyto performing symmetric cryptographic processing on a referenced mbuf data
5440318c02bSDeclan Dohertybuffer. It is used for either cipher, authentication, AEAD and chained
5450318c02bSDeclan Dohertyoperations.
5460318c02bSDeclan Doherty
5470318c02bSDeclan DohertyAs a minimum the symmetric operation must have a source data buffer (``m_src``),
5485209df0dSPablo de Laraa valid session (or transform chain if in session-less mode) and the minimum
54983984b7fSPablo de Laraauthentication/ cipher/ AEAD parameters required depending on the type of operation
5505209df0dSPablo de Laraspecified in the session or the transform
5510318c02bSDeclan Dohertychain.
5520318c02bSDeclan Doherty
553d2d7f019SAkhil Goyal.. literalinclude:: ../../../lib/cryptodev/rte_crypto_sym.h
554d2d7f019SAkhil Goyal   :language: c
555d2d7f019SAkhil Goyal   :start-after: Structure rte_crypto_sym_op 8<
556d2d7f019SAkhil Goyal   :end-before: >8 End of structure rte_crypto_sym_op.
5570318c02bSDeclan Doherty
5580318c02bSDeclan Doherty
5597adf992fSMarcin SmoczynskiSynchronous mode
5607adf992fSMarcin Smoczynski----------------
5617adf992fSMarcin Smoczynski
5627adf992fSMarcin SmoczynskiSome cryptodevs support synchronous mode alongside with a standard asynchronous
5637adf992fSMarcin Smoczynskimode. In that case operations are performed directly when calling
5647adf992fSMarcin Smoczynski``rte_cryptodev_sym_cpu_crypto_process`` method instead of enqueuing and
5657adf992fSMarcin Smoczynskidequeuing an operation before. This mode of operation allows cryptodevs which
5667adf992fSMarcin Smoczynskiutilize CPU cryptographic acceleration to have significant performance boost
5677adf992fSMarcin Smoczynskicomparing to standard asynchronous approach. Cryptodevs supporting synchronous
5687adf992fSMarcin Smoczynskimode have ``RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO`` feature flag set.
5697adf992fSMarcin Smoczynski
5707adf992fSMarcin SmoczynskiTo perform a synchronous operation a call to
5717adf992fSMarcin Smoczynski``rte_cryptodev_sym_cpu_crypto_process`` has to be made with vectorized
5727adf992fSMarcin Smoczynskioperation descriptor (``struct rte_crypto_sym_vec``) containing:
5737adf992fSMarcin Smoczynski
5747adf992fSMarcin Smoczynski- ``num`` - number of operations to perform,
5757adf992fSMarcin Smoczynski- pointer to an array of size ``num`` containing a scatter-gather list
5767adf992fSMarcin Smoczynski  descriptors of performed operations (``struct rte_crypto_sgl``). Each instance
5777adf992fSMarcin Smoczynski  of ``struct rte_crypto_sgl`` consists of a number of segments and a pointer to
5787adf992fSMarcin Smoczynski  an array of segment descriptors ``struct rte_crypto_vec``;
5798d928d47SFan Zhang- pointers to arrays of size ``num`` containing IV, AAD and digest information
5808d928d47SFan Zhang  in the ``cpu_crypto`` sub-structure,
5817adf992fSMarcin Smoczynski- pointer to an array of size ``num`` where status information will be stored
5827adf992fSMarcin Smoczynski  for each operation.
5837adf992fSMarcin Smoczynski
5847adf992fSMarcin SmoczynskiFunction returns a number of successfully completed operations and sets
5857adf992fSMarcin Smoczynskiappropriate status number for each operation in the status array provided as
5867adf992fSMarcin Smoczynskia call argument. Status different than zero must be treated as error.
5877adf992fSMarcin Smoczynski
5887adf992fSMarcin SmoczynskiFor more details, e.g. how to convert an mbuf to an SGL, please refer to an
5897adf992fSMarcin Smoczynskiexample usage in the IPsec library implementation.
5907adf992fSMarcin Smoczynski
591eb7eed34SFan ZhangCryptodev Raw Data-path APIs
592eb7eed34SFan Zhang~~~~~~~~~~~~~~~~~~~~~~~~~~~~
593eb7eed34SFan Zhang
594eb7eed34SFan ZhangThe Crypto Raw data-path APIs are a set of APIs designed to enable external
595eb7eed34SFan Zhanglibraries/applications to leverage the cryptographic processing provided by
596eb7eed34SFan ZhangDPDK crypto PMDs through the cryptodev API but in a manner that is not
597eb7eed34SFan Zhangdependent on native DPDK data structures (eg. rte_mbuf, rte_crypto_op, ... etc)
598eb7eed34SFan Zhangin their data-path implementation.
599eb7eed34SFan Zhang
600eb7eed34SFan ZhangThe raw data-path APIs have the following advantages:
601eb7eed34SFan Zhang
602eb7eed34SFan Zhang- External data structure friendly design. The new APIs uses the operation
603eb7eed34SFan Zhang  descriptor ``struct rte_crypto_sym_vec`` that supports raw data pointer and
604eb7eed34SFan Zhang  IOVA addresses as input. Moreover, the APIs does not require the user to
605eb7eed34SFan Zhang  allocate the descriptor from mempool, nor requiring mbufs to describe input
606eb7eed34SFan Zhang  data's virtual and IOVA addresses. All these features made the translation
607eb7eed34SFan Zhang  from user's own data structure into the descriptor easier and more efficient.
608eb7eed34SFan Zhang
609eb7eed34SFan Zhang- Flexible enqueue and dequeue operation. The raw data-path APIs gives the
610eb7eed34SFan Zhang  user more control to the enqueue and dequeue operations, including the
611eb7eed34SFan Zhang  capability of precious enqueue/dequeue count, abandoning enqueue or dequeue
612eb7eed34SFan Zhang  at any time, and operation status translation and set on the fly.
613eb7eed34SFan Zhang
614eb7eed34SFan ZhangCryptodev PMDs which support the raw data-path APIs will have
615eb7eed34SFan Zhang``RTE_CRYPTODEV_FF_SYM_RAW_DP`` feature flag presented. To use this feature,
616eb7eed34SFan Zhangthe user shall create a local ``struct rte_crypto_raw_dp_ctx`` buffer and
617eb7eed34SFan Zhangextend to at least the length returned by ``rte_cryptodev_get_raw_dp_ctx_size``
618eb7eed34SFan Zhangfunction call. The created buffer is then initialized using
619eb7eed34SFan Zhang``rte_cryptodev_configure_raw_dp_ctx`` function with the ``is_update``
620eb7eed34SFan Zhangparameter as 0. The library and the crypto device driver will then set the
621eb7eed34SFan Zhangbuffer and attach either the cryptodev sym session, the rte_security session,
622eb7eed34SFan Zhangor the cryptodev xform for session-less operation into the ctx buffer, and
623eb7eed34SFan Zhangset the corresponding enqueue and dequeue function handlers based on the
624eb7eed34SFan Zhangalgorithm information stored in the session or xform. When the ``is_update``
625eb7eed34SFan Zhangparameter passed into ``rte_cryptodev_configure_raw_dp_ctx`` is 1, the driver
626eb7eed34SFan Zhangwill not initialize the buffer but only update the session or xform and
627eb7eed34SFan Zhangthe function handlers accordingly.
628eb7eed34SFan Zhang
629eb7eed34SFan ZhangAfter the ``struct rte_crypto_raw_dp_ctx`` buffer is initialized, it is now
630eb7eed34SFan Zhangready for enqueue and dequeue operation. There are two different enqueue
631eb7eed34SFan Zhangfunctions: ``rte_cryptodev_raw_enqueue`` to enqueue single raw data
632eb7eed34SFan Zhangoperation, and ``rte_cryptodev_raw_enqueue_burst`` to enqueue a descriptor
633eb7eed34SFan Zhangwith multiple operations. In case of the application uses similar approach to
634eb7eed34SFan Zhang``struct rte_crypto_sym_vec`` to manage its data burst but with different
635eb7eed34SFan Zhangdata structure, using the ``rte_cryptodev_raw_enqueue_burst`` function may be
636eb7eed34SFan Zhangless efficient as this is a situation where the application has to loop over
637eb7eed34SFan Zhangall crypto operations to assemble the ``struct rte_crypto_sym_vec`` descriptor
638eb7eed34SFan Zhangfrom its own data structure, and then the driver will loop over them again to
639eb7eed34SFan Zhangtranslate every operation in the descriptor to the driver's specific queue data.
640eb7eed34SFan ZhangThe ``rte_cryptodev_raw_enqueue`` should be used to save one loop for each data
641eb7eed34SFan Zhangburst instead.
642eb7eed34SFan Zhang
643eb7eed34SFan ZhangThe ``rte_cryptodev_raw_enqueue`` and ``rte_cryptodev_raw_enqueue_burst``
644eb7eed34SFan Zhangfunctions will return or set the enqueue status. ``rte_cryptodev_raw_enqueue``
645eb7eed34SFan Zhangwill return the status directly, ``rte_cryptodev_raw_enqueue_burst`` will
646eb7eed34SFan Zhangreturn the number of operations enqueued or stored (explained as follows) and
647eb7eed34SFan Zhangset the ``enqueue_status`` buffer provided by the user. The possible
648eb7eed34SFan Zhangenqueue status values are:
649eb7eed34SFan Zhang
650eb7eed34SFan Zhang- ``1``: the operation(s) is/are enqueued successfully.
651eb7eed34SFan Zhang- ``0``: the operation(s) is/are cached successfully in the crypto device queue
652eb7eed34SFan Zhang  but is not actually enqueued. The user shall call
653eb7eed34SFan Zhang  ``rte_cryptodev_raw_enqueue_done`` function after the expected operations
654eb7eed34SFan Zhang  are stored. The crypto device will then start enqueuing all of them at
655eb7eed34SFan Zhang  once.
656eb7eed34SFan Zhang- The negative integer: error occurred during enqueue.
657eb7eed34SFan Zhang
658eb7eed34SFan ZhangCalling ``rte_cryptodev_configure_raw_dp_ctx`` with the parameter ``is_update``
659eb7eed34SFan Zhangset as 0 twice without the enqueue function returning or setting enqueue status
660eb7eed34SFan Zhangto 1 or ``rte_cryptodev_raw_enqueue_done`` function being called in between will
661eb7eed34SFan Zhanginvalidate any operation stored in the device queue but not enqueued. This
662eb7eed34SFan Zhangfeature is useful when the user wants to abandon partially enqueued operations
663eb7eed34SFan Zhangfor a failed enqueue burst operation and try enqueuing in a whole later.
664eb7eed34SFan Zhang
665eb7eed34SFan ZhangSimilar as enqueue, there are two dequeue functions:
6667be78d02SJosh Soref``rte_cryptodev_raw_dequeue`` for dequeuing single operation, and
667eb7eed34SFan Zhang``rte_cryptodev_raw_dequeue_burst`` for dequeuing a burst of operations (e.g.
668eb7eed34SFan Zhangall operations in a ``struct rte_crypto_sym_vec`` descriptor). The
669eb7eed34SFan Zhang``rte_cryptodev_raw_dequeue_burst`` function allows the user to provide callback
670eb7eed34SFan Zhangfunctions to retrieve dequeue count from the enqueued user data and write the
671eb7eed34SFan Zhangexpected status value to the user data on the fly. The dequeue functions also
672eb7eed34SFan Zhangset the dequeue status:
673eb7eed34SFan Zhang
674eb7eed34SFan Zhang- ``1``: the operation(s) is/are dequeued successfully.
675eb7eed34SFan Zhang- ``0``: the operation(s) is/are completed but is not actually dequeued (hence
676eb7eed34SFan Zhang  still kept in the device queue). The user shall call the
677eb7eed34SFan Zhang  ``rte_cryptodev_raw_dequeue_done`` function after the expected number of
678eb7eed34SFan Zhang  operations (e.g. all operations in a descriptor) are dequeued. The crypto
679eb7eed34SFan Zhang  device driver will then free them from the queue at once.
680eb7eed34SFan Zhang- The negative integer: error occurred during dequeue.
681eb7eed34SFan Zhang
682eb7eed34SFan ZhangCalling ``rte_cryptodev_configure_raw_dp_ctx`` with the parameter ``is_update``
683eb7eed34SFan Zhangset as 0 twice without the dequeue functions execution changed dequeue_status
684eb7eed34SFan Zhangto 1 or ``rte_cryptodev_raw_dequeue_done`` function being called in between will
685eb7eed34SFan Zhangrevert the crypto device queue's dequeue effort to the moment when the
686eb7eed34SFan Zhang``struct rte_crypto_raw_dp_ctx`` buffer is initialized. This feature is useful
687eb7eed34SFan Zhangwhen the user wants to abandon partially dequeued data and try dequeuing again
688eb7eed34SFan Zhanglater in a whole.
689eb7eed34SFan Zhang
690eb7eed34SFan ZhangThere are a few limitations to the raw data path APIs:
691eb7eed34SFan Zhang
692eb7eed34SFan Zhang* Only support in-place operations.
693eb7eed34SFan Zhang* APIs are NOT thread-safe.
694eb7eed34SFan Zhang* CANNOT mix the raw data-path API's enqueue with rte_cryptodev_enqueue_burst,
695eb7eed34SFan Zhang  or vice versa.
696eb7eed34SFan Zhang
697eb7eed34SFan ZhangSee *DPDK API Reference* for details on each API definitions.
698eb7eed34SFan Zhang
69931850d26SPablo de LaraSample code
70031850d26SPablo de Lara-----------
70131850d26SPablo de Lara
70231850d26SPablo de LaraThere are various sample applications that show how to use the cryptodev library,
70331850d26SPablo de Larasuch as the L2fwd with Crypto sample application (L2fwd-crypto) and
704d629b7b5SJohn McNamarathe IPsec Security Gateway application (ipsec-secgw).
70531850d26SPablo de Lara
70631850d26SPablo de LaraWhile these applications demonstrate how an application can be created to perform
70731850d26SPablo de Larageneric crypto operation, the required complexity hides the basic steps of
70831850d26SPablo de Larahow to use the cryptodev APIs.
70931850d26SPablo de Lara
71031850d26SPablo de LaraThe following sample code shows the basic steps to encrypt several buffers
71131850d26SPablo de Larawith AES-CBC (although performing other crypto operations is similar),
71231850d26SPablo de Larausing one of the crypto PMDs available in DPDK.
71331850d26SPablo de Lara
71431850d26SPablo de Lara.. code-block:: c
71531850d26SPablo de Lara
71631850d26SPablo de Lara    /*
71731850d26SPablo de Lara     * Simple example to encrypt several buffers with AES-CBC using
71831850d26SPablo de Lara     * the Cryptodev APIs.
71931850d26SPablo de Lara     */
72031850d26SPablo de Lara
72131850d26SPablo de Lara    #define MAX_SESSIONS         1024
72231850d26SPablo de Lara    #define NUM_MBUFS            1024
72331850d26SPablo de Lara    #define POOL_CACHE_SIZE      128
72431850d26SPablo de Lara    #define BURST_SIZE           32
72531850d26SPablo de Lara    #define BUFFER_SIZE          1024
72631850d26SPablo de Lara    #define AES_CBC_IV_LENGTH    16
72731850d26SPablo de Lara    #define AES_CBC_KEY_LENGTH   16
72831850d26SPablo de Lara    #define IV_OFFSET            (sizeof(struct rte_crypto_op) + \
72931850d26SPablo de Lara                                 sizeof(struct rte_crypto_sym_op))
73031850d26SPablo de Lara
7311d6f8988SFan Zhang    struct rte_mempool *mbuf_pool, *crypto_op_pool;
7321d6f8988SFan Zhang    struct rte_mempool *session_pool, *session_priv_pool;
73331850d26SPablo de Lara    unsigned int session_size;
73431850d26SPablo de Lara    int ret;
73531850d26SPablo de Lara
73631850d26SPablo de Lara    /* Initialize EAL. */
73731850d26SPablo de Lara    ret = rte_eal_init(argc, argv);
73831850d26SPablo de Lara    if (ret < 0)
73931850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
74031850d26SPablo de Lara
74131850d26SPablo de Lara    uint8_t socket_id = rte_socket_id();
74231850d26SPablo de Lara
74331850d26SPablo de Lara    /* Create the mbuf pool. */
74431850d26SPablo de Lara    mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool",
74531850d26SPablo de Lara                                    NUM_MBUFS,
74631850d26SPablo de Lara                                    POOL_CACHE_SIZE,
74731850d26SPablo de Lara                                    0,
74831850d26SPablo de Lara                                    RTE_MBUF_DEFAULT_BUF_SIZE,
74931850d26SPablo de Lara                                    socket_id);
75031850d26SPablo de Lara    if (mbuf_pool == NULL)
75131850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
75231850d26SPablo de Lara
75331850d26SPablo de Lara    /*
75431850d26SPablo de Lara     * The IV is always placed after the crypto operation,
75531850d26SPablo de Lara     * so some private data is required to be reserved.
75631850d26SPablo de Lara     */
75731850d26SPablo de Lara    unsigned int crypto_op_private_data = AES_CBC_IV_LENGTH;
75831850d26SPablo de Lara
75931850d26SPablo de Lara    /* Create crypto operation pool. */
76031850d26SPablo de Lara    crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
76131850d26SPablo de Lara                                            RTE_CRYPTO_OP_TYPE_SYMMETRIC,
76231850d26SPablo de Lara                                            NUM_MBUFS,
76331850d26SPablo de Lara                                            POOL_CACHE_SIZE,
76431850d26SPablo de Lara                                            crypto_op_private_data,
76531850d26SPablo de Lara                                            socket_id);
76631850d26SPablo de Lara    if (crypto_op_pool == NULL)
76731850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
76831850d26SPablo de Lara
76931850d26SPablo de Lara    /* Create the virtual crypto device. */
77031850d26SPablo de Lara    char args[128];
77131850d26SPablo de Lara    const char *crypto_name = "crypto_aesni_mb0";
77231850d26SPablo de Lara    snprintf(args, sizeof(args), "socket_id=%d", socket_id);
77331850d26SPablo de Lara    ret = rte_vdev_init(crypto_name, args);
77431850d26SPablo de Lara    if (ret != 0)
77531850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Cannot create virtual device");
77631850d26SPablo de Lara
77731850d26SPablo de Lara    uint8_t cdev_id = rte_cryptodev_get_dev_id(crypto_name);
77831850d26SPablo de Lara
77931850d26SPablo de Lara    /* Get private session data size. */
780a106fcceSPablo de Lara    session_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
78131850d26SPablo de Lara
7821d6f8988SFan Zhang    #ifdef USE_TWO_MEMPOOLS
7831d6f8988SFan Zhang    /* Create session mempool for the session header. */
7841d6f8988SFan Zhang    session_pool = rte_cryptodev_sym_session_pool_create("session_pool",
7851d6f8988SFan Zhang                                    MAX_SESSIONS,
7861d6f8988SFan Zhang                                    0,
7871d6f8988SFan Zhang                                    POOL_CACHE_SIZE,
7881d6f8988SFan Zhang                                    0,
7891d6f8988SFan Zhang                                    socket_id);
7901d6f8988SFan Zhang
79131850d26SPablo de Lara    /*
7921d6f8988SFan Zhang     * Create session private data mempool for the
79331850d26SPablo de Lara     * private session data for the crypto device.
79431850d26SPablo de Lara     */
7951d6f8988SFan Zhang    session_priv_pool = rte_mempool_create("session_pool",
7961d6f8988SFan Zhang                                    MAX_SESSIONS,
79731850d26SPablo de Lara                                    session_size,
79831850d26SPablo de Lara                                    POOL_CACHE_SIZE,
79931850d26SPablo de Lara                                    0, NULL, NULL, NULL,
80031850d26SPablo de Lara                                    NULL, socket_id,
80131850d26SPablo de Lara                                    0);
80231850d26SPablo de Lara
8031d6f8988SFan Zhang    #else
8041d6f8988SFan Zhang    /* Use of the same mempool for session header and private data */
8051d6f8988SFan Zhang	session_pool = rte_cryptodev_sym_session_pool_create("session_pool",
8061d6f8988SFan Zhang                                    MAX_SESSIONS * 2,
8071d6f8988SFan Zhang                                    session_size,
8081d6f8988SFan Zhang                                    POOL_CACHE_SIZE,
8091d6f8988SFan Zhang                                    0,
8101d6f8988SFan Zhang                                    socket_id);
8111d6f8988SFan Zhang
8121d6f8988SFan Zhang	session_priv_pool = session_pool;
8131d6f8988SFan Zhang
8141d6f8988SFan Zhang    #endif
8151d6f8988SFan Zhang
81631850d26SPablo de Lara    /* Configure the crypto device. */
81731850d26SPablo de Lara    struct rte_cryptodev_config conf = {
81831850d26SPablo de Lara        .nb_queue_pairs = 1,
81931850d26SPablo de Lara        .socket_id = socket_id
82031850d26SPablo de Lara    };
8211d6f8988SFan Zhang
82231850d26SPablo de Lara    struct rte_cryptodev_qp_conf qp_conf = {
823725d2a7fSFan Zhang        .nb_descriptors = 2048,
824725d2a7fSFan Zhang        .mp_session = session_pool,
8251d6f8988SFan Zhang        .mp_session_private = session_priv_pool
82631850d26SPablo de Lara    };
82731850d26SPablo de Lara
82831850d26SPablo de Lara    if (rte_cryptodev_configure(cdev_id, &conf) < 0)
82931850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Failed to configure cryptodev %u", cdev_id);
83031850d26SPablo de Lara
831725d2a7fSFan Zhang    if (rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf, socket_id) < 0)
83231850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Failed to setup queue pair\n");
83331850d26SPablo de Lara
83431850d26SPablo de Lara    if (rte_cryptodev_start(cdev_id) < 0)
83531850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Failed to start device\n");
83631850d26SPablo de Lara
83731850d26SPablo de Lara    /* Create the crypto transform. */
83831850d26SPablo de Lara    uint8_t cipher_key[16] = {0};
83931850d26SPablo de Lara    struct rte_crypto_sym_xform cipher_xform = {
84031850d26SPablo de Lara        .next = NULL,
84131850d26SPablo de Lara        .type = RTE_CRYPTO_SYM_XFORM_CIPHER,
84231850d26SPablo de Lara        .cipher = {
84331850d26SPablo de Lara            .op = RTE_CRYPTO_CIPHER_OP_ENCRYPT,
84431850d26SPablo de Lara            .algo = RTE_CRYPTO_CIPHER_AES_CBC,
84531850d26SPablo de Lara            .key = {
84631850d26SPablo de Lara                .data = cipher_key,
84731850d26SPablo de Lara                .length = AES_CBC_KEY_LENGTH
84831850d26SPablo de Lara            },
84931850d26SPablo de Lara            .iv = {
85031850d26SPablo de Lara                .offset = IV_OFFSET,
85131850d26SPablo de Lara                .length = AES_CBC_IV_LENGTH
85231850d26SPablo de Lara            }
85331850d26SPablo de Lara        }
85431850d26SPablo de Lara    };
85531850d26SPablo de Lara
85631850d26SPablo de Lara    /* Create crypto session and initialize it for the crypto device. */
85731850d26SPablo de Lara    struct rte_cryptodev_sym_session *session;
8582a440d6aSAkhil Goyal    session = rte_cryptodev_sym_session_create(cdev_id, &cipher_xform,
8592a440d6aSAkhil Goyal                    session_pool);
86031850d26SPablo de Lara    if (session == NULL)
86131850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Session could not be created\n");
86231850d26SPablo de Lara
86331850d26SPablo de Lara    /* Get a burst of crypto operations. */
86431850d26SPablo de Lara    struct rte_crypto_op *crypto_ops[BURST_SIZE];
86531850d26SPablo de Lara    if (rte_crypto_op_bulk_alloc(crypto_op_pool,
86631850d26SPablo de Lara                            RTE_CRYPTO_OP_TYPE_SYMMETRIC,
86731850d26SPablo de Lara                            crypto_ops, BURST_SIZE) == 0)
86831850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Not enough crypto operations available\n");
86931850d26SPablo de Lara
87031850d26SPablo de Lara    /* Get a burst of mbufs. */
87131850d26SPablo de Lara    struct rte_mbuf *mbufs[BURST_SIZE];
87231850d26SPablo de Lara    if (rte_pktmbuf_alloc_bulk(mbuf_pool, mbufs, BURST_SIZE) < 0)
87331850d26SPablo de Lara        rte_exit(EXIT_FAILURE, "Not enough mbufs available");
87431850d26SPablo de Lara
87531850d26SPablo de Lara    /* Initialize the mbufs and append them to the crypto operations. */
87631850d26SPablo de Lara    unsigned int i;
87731850d26SPablo de Lara    for (i = 0; i < BURST_SIZE; i++) {
87831850d26SPablo de Lara        if (rte_pktmbuf_append(mbufs[i], BUFFER_SIZE) == NULL)
87931850d26SPablo de Lara            rte_exit(EXIT_FAILURE, "Not enough room in the mbuf\n");
88031850d26SPablo de Lara        crypto_ops[i]->sym->m_src = mbufs[i];
88131850d26SPablo de Lara    }
88231850d26SPablo de Lara
88331850d26SPablo de Lara    /* Set up the crypto operations. */
88431850d26SPablo de Lara    for (i = 0; i < BURST_SIZE; i++) {
88531850d26SPablo de Lara        struct rte_crypto_op *op = crypto_ops[i];
88631850d26SPablo de Lara        /* Modify bytes of the IV at the end of the crypto operation */
88731850d26SPablo de Lara        uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
88831850d26SPablo de Lara                                                IV_OFFSET);
88931850d26SPablo de Lara
89031850d26SPablo de Lara        generate_random_bytes(iv_ptr, AES_CBC_IV_LENGTH);
89131850d26SPablo de Lara
89231850d26SPablo de Lara        op->sym->cipher.data.offset = 0;
89331850d26SPablo de Lara        op->sym->cipher.data.length = BUFFER_SIZE;
89431850d26SPablo de Lara
89531850d26SPablo de Lara        /* Attach the crypto session to the operation */
89631850d26SPablo de Lara        rte_crypto_op_attach_sym_session(op, session);
89731850d26SPablo de Lara    }
89831850d26SPablo de Lara
89931850d26SPablo de Lara    /* Enqueue the crypto operations in the crypto device. */
90031850d26SPablo de Lara    uint16_t num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, 0,
90131850d26SPablo de Lara                                            crypto_ops, BURST_SIZE);
90231850d26SPablo de Lara
90331850d26SPablo de Lara    /*
90431850d26SPablo de Lara     * Dequeue the crypto operations until all the operations
905d629b7b5SJohn McNamara     * are processed in the crypto device.
90631850d26SPablo de Lara     */
90731850d26SPablo de Lara    uint16_t num_dequeued_ops, total_num_dequeued_ops = 0;
90831850d26SPablo de Lara    do {
90931850d26SPablo de Lara        struct rte_crypto_op *dequeued_ops[BURST_SIZE];
91031850d26SPablo de Lara        num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, 0,
91131850d26SPablo de Lara                                        dequeued_ops, BURST_SIZE);
91231850d26SPablo de Lara        total_num_dequeued_ops += num_dequeued_ops;
91331850d26SPablo de Lara
91431850d26SPablo de Lara        /* Check if operation was processed successfully */
91531850d26SPablo de Lara        for (i = 0; i < num_dequeued_ops; i++) {
91631850d26SPablo de Lara            if (dequeued_ops[i]->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
91731850d26SPablo de Lara                rte_exit(EXIT_FAILURE,
91831850d26SPablo de Lara                        "Some operations were not processed correctly");
91931850d26SPablo de Lara        }
92031850d26SPablo de Lara
92131850d26SPablo de Lara        rte_mempool_put_bulk(crypto_op_pool, (void **)dequeued_ops,
92231850d26SPablo de Lara                                            num_dequeued_ops);
92331850d26SPablo de Lara    } while (total_num_dequeued_ops < num_enqueued_ops);
92431850d26SPablo de Lara
9250318c02bSDeclan DohertyAsymmetric Cryptography
9260318c02bSDeclan Doherty-----------------------
9270318c02bSDeclan Doherty
928b9209dc2SShally VermaThe cryptodev library currently provides support for the following asymmetric
92996db98dbSArek KusztalCrypto operations; RSA, Modular exponentiation and inversion, Diffie-Hellman and
93096db98dbSArek KusztalElliptic Curve Diffie-Hellman public and/or private key generation and shared
9318bd4315cSGowrishankar Muthukrishnansecret compute, DSA and EdDSA signature generation and verification.
932b9209dc2SShally Verma
933b9209dc2SShally VermaSession and Session Management
934b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
935b9209dc2SShally Verma
936b9209dc2SShally VermaSessions are used in asymmetric cryptographic processing to store the immutable
937b9209dc2SShally Vermadata defined in asymmetric cryptographic transform which is further used in the
938b9209dc2SShally Vermaoperation processing. Sessions typically stores information, such as, public
939b9209dc2SShally Vermaand private key information or domain params or prime modulus data i.e. immutable
940b9209dc2SShally Vermaacross data sets. Crypto sessions cache this immutable data in a optimal way for the
941b9209dc2SShally Vermaunderlying PMD and this allows further acceleration of the offload of Crypto workloads.
942b9209dc2SShally Verma
943b9209dc2SShally VermaLike symmetric, the Crypto device framework provides APIs to allocate and initialize
944b9209dc2SShally Vermaasymmetric sessions for crypto devices, where sessions are mempool objects.
945b9209dc2SShally VermaIt is the application's responsibility to create and manage the session mempools.
946b9209dc2SShally VermaApplication using both symmetric and asymmetric sessions should allocate and maintain
947b9209dc2SShally Vermadifferent sessions pools for each type.
948b9209dc2SShally Verma
9491f1e4b7cSCiara PowerAn application can use ``rte_cryptodev_asym_session_pool_create()`` to create a mempool
9501f1e4b7cSCiara Powerwith a specified number of elements. The element size will allow for the session header,
9511f1e4b7cSCiara Powerand the max private session size.
9521f1e4b7cSCiara PowerThe max private session size is chosen based on available crypto devices,
9531f1e4b7cSCiara Powerthe biggest private session size is used. This means any of those devices can be used,
9541f1e4b7cSCiara Powerand the mempool element will have available space for its private session data.
955b9209dc2SShally Verma
956b9209dc2SShally VermaOnce the session mempools have been created, ``rte_cryptodev_asym_session_create()``
9571f1e4b7cSCiara Poweris used to allocate and initialize an asymmetric session from the given mempool.
9581f1e4b7cSCiara PowerAn asymmetric transform chain is used to specify the operation and its parameters.
9591f1e4b7cSCiara PowerSee the section below for details on transforms.
960b9209dc2SShally Verma
961b9209dc2SShally VermaWhen a session is no longer used, user must call ``rte_cryptodev_asym_session_clear()``
962b9209dc2SShally Vermafor each of the crypto devices that are using the session, to free all driver
963b9209dc2SShally Vermaprivate asymmetric session data. Once this is done, session should be freed using
964b9209dc2SShally Verma``rte_cryptodev_asym_session_free()`` which returns them to their mempool.
965b9209dc2SShally Verma
966b9209dc2SShally VermaAsymmetric Sessionless Support
967b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
968f2b2a449SArek Kusztal
969f2b2a449SArek KusztalAsymmetric crypto framework supports session-less operations as well.
970f2b2a449SArek Kusztal
971f2b2a449SArek KusztalFields that should be set by user are:
972f2b2a449SArek Kusztal
973f2b2a449SArek KusztalMember xform of struct rte_crypto_asym_op should point to the user created rte_crypto_asym_xform.
974f2b2a449SArek KusztalNote that rte_crypto_asym_xform should be immutable for the lifetime of associated crypto_op.
975f2b2a449SArek Kusztal
976f2b2a449SArek KusztalMember sess_type of rte_crypto_op should also be set to RTE_CRYPTO_OP_SESSIONLESS.
977b9209dc2SShally Verma
978b9209dc2SShally VermaTransforms and Transform Chaining
979b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
980b9209dc2SShally Verma
981b9209dc2SShally VermaAsymmetric Crypto transforms (``rte_crypto_asym_xform``) are the mechanism used
982b9209dc2SShally Vermato specify the details of the asymmetric Crypto operation. Next pointer within
983b9209dc2SShally Vermaxform allows transform to be chained together. Also it is important to note that
98448903a79SFiona Trahethe order in which the transforms are passed indicates the order of the chaining. Allocation
985f43d3dbbSDavid Marchandof the xform structure is in the application domain. To allow future API extensions in a
98648903a79SFiona Trahebackwardly compatible manner, e.g. addition of a new parameter, the application should
98748903a79SFiona Trahezero the full xform struct before populating it.
988b9209dc2SShally Verma
989b9209dc2SShally VermaNot all asymmetric crypto xforms are supported for chaining. Currently supported
990b9209dc2SShally Vermaasymmetric crypto chaining is Diffie-Hellman private key generation followed by
991b9209dc2SShally Vermapublic generation. Also, currently API does not support chaining of symmetric and
992d629b7b5SJohn McNamaraasymmetric crypto xforms.
993b9209dc2SShally Verma
994b9209dc2SShally VermaEach xform defines specific asymmetric crypto algo. Currently supported are:
995b9209dc2SShally Verma* RSA
996b9209dc2SShally Verma* Modular operations (Exponentiation and Inverse)
997b9209dc2SShally Verma* Diffie-Hellman
998b9209dc2SShally Verma* DSA
99996db98dbSArek Kusztal* Elliptic Curve Diffie-Hellman
1000b9209dc2SShally Verma* None - special case where PMD may support a passthrough mode. More for diagnostic purpose
1001b9209dc2SShally Verma
1002b9209dc2SShally VermaSee *DPDK API Reference* for details on each rte_crypto_xxx_xform struct
1003b9209dc2SShally Verma
1004b9209dc2SShally VermaAsymmetric Operations
1005b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~
1006b9209dc2SShally Verma
1007b9209dc2SShally VermaThe asymmetric Crypto operation structure contains all the mutable data relating
1008b9209dc2SShally Vermato asymmetric cryptographic processing on an input data buffer. It uses either
1009b9209dc2SShally VermaRSA, Modular, Diffie-Hellman or DSA operations depending upon session it is attached
1010b9209dc2SShally Vermato.
1011b9209dc2SShally Verma
1012b9209dc2SShally VermaEvery operation must carry a valid session handle which further carries information
1013b9209dc2SShally Vermaon xform or xform-chain to be performed on op. Every xform type defines its own set
1014b9209dc2SShally Vermaof operational params in their respective rte_crypto_xxx_op_param struct. Depending
1015b9209dc2SShally Vermaon xform information within session, PMD picks up and process respective op_param
1016b9209dc2SShally Vermastruct.
1017b9209dc2SShally VermaUnlike symmetric, asymmetric operations do not use mbufs for input/output.
1018b9209dc2SShally VermaThey operate on data buffer of type ``rte_crypto_param``.
1019b9209dc2SShally Verma
1020b9209dc2SShally VermaSee *DPDK API Reference* for details on each rte_crypto_xxx_op_param struct
1021b9209dc2SShally Verma
102292d55afeSCiara PowerPrivate user data
102392d55afeSCiara Power~~~~~~~~~~~~~~~~~
102492d55afeSCiara Power
102592d55afeSCiara PowerSimilar to symmetric above, asymmetric also has a set and get API that provides a
102692d55afeSCiara Powermechanism for an application to store and retrieve the private user data information
102792d55afeSCiara Powerstored along with the crypto session.
102892d55afeSCiara Power
102992d55afeSCiara Power.. code-block:: c
103092d55afeSCiara Power
103192d55afeSCiara Power	int rte_cryptodev_asym_session_set_user_data(void *sess,
103292d55afeSCiara Power		void *data, uint16_t size);
103392d55afeSCiara Power
103492d55afeSCiara Power	void * rte_cryptodev_asym_session_get_user_data(void *sess);
103592d55afeSCiara Power
103692d55afeSCiara PowerPlease note the ``size`` passed to set API cannot be bigger than the predefined
103792d55afeSCiara Power``user_data_sz`` when creating the session mempool, otherwise the function will
103892d55afeSCiara Powerreturn an error. Also when ``user_data_sz`` was defined as ``0`` when
103992d55afeSCiara Powercreating the session mempool, the get API will always return ``NULL``.
104092d55afeSCiara Power
1041b9209dc2SShally VermaAsymmetric crypto Sample code
1042b9209dc2SShally Verma-----------------------------
1043b9209dc2SShally Verma
1044b9209dc2SShally VermaThere's a unit test application test_cryptodev_asym.c inside unit test framework that
1045b9209dc2SShally Vermashow how to setup and process asymmetric operations using cryptodev library.
1046b9209dc2SShally Verma
10470438b7dfSCiara PowerThe following code samples are taken from the test application mentioned above,
10480438b7dfSCiara Powerand show basic steps to compute modular exponentiation using an openssl PMD
10490438b7dfSCiara Poweravailable in DPDK (performing other crypto operations is similar except change
10500438b7dfSCiara Powerto respective op and xform setup).
1051b9209dc2SShally Verma
10520438b7dfSCiara Power.. note::
10530438b7dfSCiara Power   The following code snippets are taken from multiple functions, so variable
10540438b7dfSCiara Power   names may differ slightly between sections.
1055b9209dc2SShally Verma
10560438b7dfSCiara PowerConfigure the virtual device, queue pairs, crypto op pool and session mempool.
1057b9209dc2SShally Verma
10580438b7dfSCiara Power.. literalinclude:: ../../../app/test/test_cryptodev_asym.c
10590438b7dfSCiara Power   :language: c
10600438b7dfSCiara Power   :start-after: Device, op pool and session configuration for asymmetric crypto. 8<
10610438b7dfSCiara Power   :end-before: >8 End of device, op pool and session configuration for asymmetric crypto section.
10620438b7dfSCiara Power   :dedent: 1
1063b9209dc2SShally Verma
10640438b7dfSCiara PowerCreate MODEX data vectors.
1065b9209dc2SShally Verma
10660438b7dfSCiara Power.. literalinclude:: ../../../app/test/test_cryptodev_mod_test_vectors.h
10670438b7dfSCiara Power   :language: c
10680438b7dfSCiara Power   :start-after: MODEX data. 8<
10690438b7dfSCiara Power   :end-before: >8 End of MODEX data.
1070b9209dc2SShally Verma
10710438b7dfSCiara PowerSetup crypto xform to do modular exponentiation using data vectors.
1072b9209dc2SShally Verma
10730438b7dfSCiara Power.. literalinclude:: ../../../app/test/test_cryptodev_mod_test_vectors.h
10740438b7dfSCiara Power   :language: c
10750438b7dfSCiara Power   :start-after: MODEX vector. 8<
10760438b7dfSCiara Power   :end-before: >8 End of MODEX vector.
1077b9209dc2SShally Verma
10780438b7dfSCiara PowerGenerate crypto op, create and attach a session, then process packets.
1079b9209dc2SShally Verma
10800438b7dfSCiara Power.. literalinclude:: ../../../app/test/test_cryptodev_asym.c
10810438b7dfSCiara Power   :language: c
10820438b7dfSCiara Power   :start-after: Create op, create session, and process packets. 8<
10830438b7dfSCiara Power   :end-before: >8 End of create op, create session, and process packets section.
10840438b7dfSCiara Power   :dedent: 1
10850318c02bSDeclan Doherty
1086a29bb248SCiara Power.. note::
1087a29bb248SCiara Power   The ``rte_cryptodev_asym_session`` struct is hidden from the application.
1088a29bb248SCiara Power   The ``sess`` pointer used above is a void pointer.
1089a29bb248SCiara Power
10900318c02bSDeclan Doherty
1091b9209dc2SShally VermaAsymmetric Crypto Device API
1092b9209dc2SShally Verma~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10930318c02bSDeclan Doherty
1094b9209dc2SShally VermaThe cryptodev Library API is described in the
10953d4b2afbSDavid Marchand`DPDK API Reference <https://doc.dpdk.org/api/>`_
1096d3d98f5cSRebecca Troy
1097d3d98f5cSRebecca Troy
1098d3d98f5cSRebecca TroyDevice Statistics
1099d3d98f5cSRebecca Troy-----------------
1100d3d98f5cSRebecca Troy
1101d3d98f5cSRebecca TroyThe Cryptodev library has support for displaying Crypto device information
1102d3d98f5cSRebecca Troythrough the Telemetry interface. Telemetry commands that can be used
1103d3d98f5cSRebecca Troyare shown below.
1104d3d98f5cSRebecca Troy
1105d3d98f5cSRebecca Troy#. Get the list of available Crypto devices by ID::
1106d3d98f5cSRebecca Troy
1107d3d98f5cSRebecca Troy     --> /cryptodev/list
1108d3d98f5cSRebecca Troy     {"/cryptodev/list": [0, 1, 2, 3]}
1109d3d98f5cSRebecca Troy
1110d3d98f5cSRebecca Troy#. Get general information from a Crypto device::
1111d3d98f5cSRebecca Troy
1112d3d98f5cSRebecca Troy     --> /cryptodev/info,0
1113d3d98f5cSRebecca Troy     {"/cryptodev/info": {"device_name": "0000:1c:01.0_qat_sym",
1114d3d98f5cSRebecca Troy     "max_nb_queue_pairs": 2}}
1115d3d98f5cSRebecca Troy
1116d3d98f5cSRebecca Troy#. Get the statistics for a particular Crypto device::
1117d3d98f5cSRebecca Troy
1118d3d98f5cSRebecca Troy     --> /cryptodev/stats,0
1119d3d98f5cSRebecca Troy     {"/cryptodev/stats": {"enqueued_count": 0, "dequeued_count": 0,
1120d3d98f5cSRebecca Troy     "enqueue_err_count": 0, "dequeue_err_count": 0}}
1121d3d98f5cSRebecca Troy
11221c559ee8SGowrishankar Muthukrishnan#. Get the capabilities of a particular Crypto device::
11234ac7359bSSean Morrissey
11241c559ee8SGowrishankar Muthukrishnan     --> /cryptodev/caps,0
11251c559ee8SGowrishankar Muthukrishnan     {"/cryptodev/caps": {"crypto_caps": [<array of serialized bytes of
11261c559ee8SGowrishankar Muthukrishnan     capabilities>], "crypto_caps_n": <number of capabilities>}}
11271c559ee8SGowrishankar Muthukrishnan
1128d3d98f5cSRebecca TroyFor more information on how to use the Telemetry interface, see
1129d3d98f5cSRebecca Troythe :doc:`../howto/telemetry`.
1130