xref: /dpdk/doc/guides/prog_guide/cryptodev_lib.rst (revision 4ed890495594a71e618d62fd975c5868915eb4a1)
1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright(c) 2016-2020 Intel Corporation.
3
4Cryptography Device Library
5===========================
6
7The cryptodev library provides a Crypto device framework for management and
8provisioning of hardware and software Crypto poll mode drivers, defining generic
9APIs which support a number of different Crypto operations. The framework
10currently only supports cipher, authentication, chained cipher/authentication
11and AEAD symmetric and asymmetric Crypto operations.
12
13
14Design Principles
15-----------------
16
17The cryptodev library follows the same basic principles as those used in DPDK's
18Ethernet Device framework. The Crypto framework provides a generic Crypto device
19framework which supports both physical (hardware) and virtual (software) Crypto
20devices as well as a generic Crypto API which allows Crypto devices to be
21managed and configured and supports Crypto operations to be provisioned on
22Crypto poll mode driver.
23
24
25Device Management
26-----------------
27
28Device Creation
29~~~~~~~~~~~~~~~
30
31Physical Crypto devices are discovered during the PCI probe/enumeration of the
32EAL function which is executed at DPDK initialization, based on
33their PCI device identifier, each unique PCI BDF (bus/bridge, device,
34function). Specific physical Crypto devices, like other physical devices in DPDK
35can be listed using the EAL command line options.
36
37Virtual devices can be created by two mechanisms, either using the EAL command
38line options or from within the application using an EAL API directly.
39
40From the command line using the --vdev EAL option
41
42.. code-block:: console
43
44   --vdev  'crypto_aesni_mb0,max_nb_queue_pairs=2,socket_id=0'
45
46.. Note::
47
48   * If DPDK application requires multiple software crypto PMD devices then required
49     number of ``--vdev`` with appropriate libraries are to be added.
50
51   * An Application with crypto PMD instances sharing the same library requires unique ID.
52
53   Example: ``--vdev  'crypto_aesni_mb0' --vdev  'crypto_aesni_mb1'``
54
55Or using the rte_vdev_init API within the application code.
56
57.. code-block:: c
58
59   rte_vdev_init("crypto_aesni_mb",
60                     "max_nb_queue_pairs=2,socket_id=0")
61
62All virtual Crypto devices support the following initialization parameters:
63
64* ``max_nb_queue_pairs`` - maximum number of queue pairs supported by the device.
65* ``socket_id`` - socket on which to allocate the device resources on.
66
67
68Device Identification
69~~~~~~~~~~~~~~~~~~~~~
70
71Each device, whether virtual or physical is uniquely designated by two
72identifiers:
73
74- A unique device index used to designate the Crypto device in all functions
75  exported by the cryptodev API.
76
77- A device name used to designate the Crypto device in console messages, for
78  administration or debugging purposes. For ease of use, the port name includes
79  the port index.
80
81
82Device Configuration
83~~~~~~~~~~~~~~~~~~~~
84
85The configuration of each Crypto device includes the following operations:
86
87- Allocation of resources, including hardware resources if a physical device.
88- Resetting the device into a well-known default state.
89- Initialization of statistics counters.
90
91The rte_cryptodev_configure API is used to configure a Crypto device.
92
93.. code-block:: c
94
95   int rte_cryptodev_configure(uint8_t dev_id,
96                               struct rte_cryptodev_config *config)
97
98The ``rte_cryptodev_config`` structure is used to pass the configuration
99parameters for socket selection and number of queue pairs.
100
101.. code-block:: c
102
103    struct rte_cryptodev_config {
104        int socket_id;
105        /**< Socket to allocate resources on */
106        uint16_t nb_queue_pairs;
107        /**< Number of queue pairs to configure on device */
108    };
109
110
111Configuration of Queue Pairs
112~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113
114Each Crypto devices queue pair is individually configured through the
115``rte_cryptodev_queue_pair_setup`` API.
116Each queue pairs resources may be allocated on a specified socket.
117
118.. code-block:: c
119
120    int rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
121                const struct rte_cryptodev_qp_conf *qp_conf,
122                int socket_id)
123
124   struct rte_cryptodev_qp_conf {
125        uint32_t nb_descriptors; /**< Number of descriptors per queue pair */
126        struct rte_mempool *mp_session;
127        /**< The mempool for creating session in sessionless mode */
128    };
129
130
131The field ``mp_session`` is used for creating temporary session to process
132the crypto operations in the session-less mode.
133They can be the same other different mempools. Please note not all Cryptodev
134PMDs supports session-less mode.
135
136
137Logical Cores, Memory and Queues Pair Relationships
138~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139
140The Crypto device Library as the Poll Mode Driver library support NUMA for when
141a processor’s logical cores and interfaces utilize its local memory. Therefore
142Crypto operations, and in the case of symmetric Crypto operations, the session
143and the mbuf being operated on, should be allocated from memory pools created
144in the local memory. The buffers should, if possible, remain on the local
145processor to obtain the best performance results and buffer descriptors should
146be populated with mbufs allocated from a mempool allocated from local memory.
147
148The run-to-completion model also performs better, especially in the case of
149virtual Crypto devices, if the Crypto operation and session and data buffer is
150in local memory instead of a remote processor's memory. This is also true for
151the pipe-line model provided all logical cores used are located on the same
152processor.
153
154Multiple logical cores should never share the same queue pair for enqueuing
155operations or dequeuing operations on the same Crypto device since this would
156require global locks and hinder performance. It is however possible to use a
157different logical core to dequeue an operation on a queue pair from the logical
158core which it was enqueued on. This means that a crypto burst enqueue/dequeue
159APIs are a logical place to transition from one logical core to another in a
160packet processing pipeline.
161
162
163Device Features and Capabilities
164---------------------------------
165
166Crypto devices define their functionality through two mechanisms, global device
167features and algorithm capabilities. Global devices features identify device
168wide level features which are applicable to the whole device such as
169the device having hardware acceleration or supporting symmetric and/or asymmetric
170Crypto operations.
171
172The capabilities mechanism defines the individual algorithms/functions which
173the device supports, such as a specific symmetric Crypto cipher,
174authentication operation or Authenticated Encryption with Associated Data
175(AEAD) operation.
176
177
178Device Features
179~~~~~~~~~~~~~~~
180
181Currently the following Crypto device features are defined:
182
183* Symmetric Crypto operations
184* Asymmetric Crypto operations
185* Chaining of symmetric Crypto operations
186* SSE accelerated SIMD vector operations
187* AVX accelerated SIMD vector operations
188* AVX2 accelerated SIMD vector operations
189* AESNI accelerated instructions
190* Hardware off-load processing
191
192
193Device Operation Capabilities
194~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
195
196Crypto capabilities which identify particular algorithm which the Crypto PMD
197supports are  defined by the operation type, the operation transform, the
198transform identifier and then the particulars of the transform. For the full
199scope of the Crypto capability see the definition of the structure in the
200*DPDK API Reference*.
201
202.. code-block:: c
203
204   struct rte_cryptodev_capabilities;
205
206Each Crypto poll mode driver defines its own private array of capabilities
207for the operations it supports. Below is an example of the capabilities for a
208PMD which supports the authentication algorithm SHA1_HMAC and the cipher
209algorithm AES_CBC.
210
211.. code-block:: c
212
213    static const struct rte_cryptodev_capabilities pmd_capabilities[] = {
214        {    /* SHA1 HMAC */
215            .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
216            .sym = {
217                .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
218                .auth = {
219                    .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
220                    .block_size = 64,
221                    .key_size = {
222                        .min = 64,
223                        .max = 64,
224                        .increment = 0
225                    },
226                    .digest_size = {
227                        .min = 12,
228                        .max = 12,
229                        .increment = 0
230                    },
231                    .aad_size = { 0 },
232                    .iv_size = { 0 }
233                }
234            }
235        },
236        {    /* AES CBC */
237            .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
238            .sym = {
239                .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
240                .cipher = {
241                    .algo = RTE_CRYPTO_CIPHER_AES_CBC,
242                    .block_size = 16,
243                    .key_size = {
244                        .min = 16,
245                        .max = 32,
246                        .increment = 8
247                    },
248                    .iv_size = {
249                        .min = 16,
250                        .max = 16,
251                        .increment = 0
252                    }
253                }
254            }
255        }
256    }
257
258
259Capabilities Discovery
260~~~~~~~~~~~~~~~~~~~~~~
261
262Discovering the features and capabilities of a Crypto device poll mode driver
263is achieved through the ``rte_cryptodev_info_get`` function.
264
265.. code-block:: c
266
267   void rte_cryptodev_info_get(uint8_t dev_id,
268                               struct rte_cryptodev_info *dev_info);
269
270This allows the user to query a specific Crypto PMD and get all the device
271features and capabilities. The ``rte_cryptodev_info`` structure contains all the
272relevant information for the device.
273
274.. code-block:: c
275
276    struct rte_cryptodev_info {
277        const char *driver_name;
278        uint8_t driver_id;
279        struct rte_device *device;
280
281        uint64_t feature_flags;
282
283        const struct rte_cryptodev_capabilities *capabilities;
284
285        unsigned max_nb_queue_pairs;
286
287        struct {
288            unsigned max_nb_sessions;
289        } sym;
290    };
291
292
293Operation Processing
294--------------------
295
296Scheduling of Crypto operations on DPDK's application data path is
297performed using a burst oriented asynchronous API set. A queue pair on a Crypto
298device accepts a burst of Crypto operations using enqueue burst API. On physical
299Crypto devices the enqueue burst API will place the operations to be processed
300on the devices hardware input queue, for virtual devices the processing of the
301Crypto operations is usually completed during the enqueue call to the Crypto
302device. The dequeue burst API will retrieve any processed operations available
303from the queue pair on the Crypto device, from physical devices this is usually
304directly from the devices processed queue, and for virtual device's from a
305``rte_ring`` where processed operations are placed after being processed on the
306enqueue call.
307
308
309Private data
310~~~~~~~~~~~~
311For session-based operations, the set and get API provides a mechanism for an
312application to store and retrieve the private user data information stored along
313with the crypto session.
314
315For example, suppose an application is submitting a crypto operation with a session
316associated and wants to indicate private user data information which is required to be
317used after completion of the crypto operation. In this case, the application can use
318the set API to set the user data and retrieve it using get API.
319
320.. code-block:: c
321
322	int rte_cryptodev_sym_session_set_user_data(
323		struct rte_cryptodev_sym_session *sess,	void *data, uint16_t size);
324
325	void * rte_cryptodev_sym_session_get_user_data(
326		struct rte_cryptodev_sym_session *sess);
327
328Please note the ``size`` passed to set API cannot be bigger than the predefined
329``user_data_sz`` when creating the session header mempool, otherwise the
330function will return error. Also when ``user_data_sz`` was defined as ``0`` when
331creating the session header mempool, the get API will always return ``NULL``.
332
333For session-less mode, the private user data information can be placed along with the
334``struct rte_crypto_op``. The ``rte_crypto_op::private_data_offset`` indicates the
335start of private data information. The offset is counted from the start of the
336rte_crypto_op including other crypto information such as the IVs (since there can
337be an IV also for authentication).
338
339User callback APIs
340~~~~~~~~~~~~~~~~~~
341The add APIs configures a user callback function to be called for each burst of crypto
342ops received/sent on a given crypto device queue pair. The return value is a pointer
343that can be used later to remove the callback using remove API. Application is expected
344to register a callback function of type ``rte_cryptodev_callback_fn``. Multiple callback
345functions can be added for a given queue pair. API does not restrict on maximum number of
346callbacks.
347
348Callbacks registered by application would not survive ``rte_cryptodev_configure`` as it
349reinitializes the callback list. It is user responsibility to remove all installed
350callbacks before calling ``rte_cryptodev_configure`` to avoid possible memory leakage.
351
352So, the application is expected to add user callback after ``rte_cryptodev_configure``.
353The callbacks can also be added at the runtime. These callbacks get executed when
354``rte_cryptodev_enqueue_burst``/``rte_cryptodev_dequeue_burst`` is called.
355
356.. code-block:: c
357
358	struct rte_cryptodev_cb *
359		rte_cryptodev_add_enq_callback(uint8_t dev_id, uint16_t qp_id,
360					       rte_cryptodev_callback_fn cb_fn,
361					       void *cb_arg);
362
363	struct rte_cryptodev_cb *
364		rte_cryptodev_add_deq_callback(uint8_t dev_id, uint16_t qp_id,
365					       rte_cryptodev_callback_fn cb_fn,
366					       void *cb_arg);
367
368	uint16_t (* rte_cryptodev_callback_fn)(uint16_t dev_id, uint16_t qp_id,
369					       struct rte_crypto_op **ops,
370					       uint16_t nb_ops, void *user_param);
371
372The remove API removes a callback function added by
373``rte_cryptodev_add_enq_callback``/``rte_cryptodev_add_deq_callback``.
374
375.. code-block:: c
376
377	int rte_cryptodev_remove_enq_callback(uint8_t dev_id, uint16_t qp_id,
378					      struct rte_cryptodev_cb *cb);
379
380	int rte_cryptodev_remove_deq_callback(uint8_t dev_id, uint16_t qp_id,
381					      struct rte_cryptodev_cb *cb);
382
383
384Enqueue / Dequeue Burst APIs
385~~~~~~~~~~~~~~~~~~~~~~~~~~~~
386
387The burst enqueue API uses a Crypto device identifier and a queue pair
388identifier to specify the Crypto device queue pair to schedule the processing on.
389The ``nb_ops`` parameter is the number of operations to process which are
390supplied in the ``ops`` array of ``rte_crypto_op`` structures.
391The enqueue function returns the number of operations it actually enqueued for
392processing, a return value equal to ``nb_ops`` means that all packets have been
393enqueued.
394
395.. code-block:: c
396
397   uint16_t rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
398                                        struct rte_crypto_op **ops, uint16_t nb_ops)
399
400The dequeue API uses the same format as the enqueue API of processed but
401the ``nb_ops`` and ``ops`` parameters are now used to specify the max processed
402operations the user wishes to retrieve and the location in which to store them.
403The API call returns the actual number of processed operations returned, this
404can never be larger than ``nb_ops``.
405
406.. code-block:: c
407
408   uint16_t rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
409                                        struct rte_crypto_op **ops, uint16_t nb_ops)
410
411
412Operation Representation
413~~~~~~~~~~~~~~~~~~~~~~~~
414
415An Crypto operation is represented by an rte_crypto_op structure, which is a
416generic metadata container for all necessary information required for the
417Crypto operation to be processed on a particular Crypto device poll mode driver.
418
419.. figure:: img/crypto_op.*
420
421The operation structure includes the operation type, the operation status
422and the session type (session-based/less), a reference to the operation
423specific data, which can vary in size and content depending on the operation
424being provisioned. It also contains the source mempool for the operation,
425if it allocated from a mempool.
426
427If Crypto operations are allocated from a Crypto operation mempool, see next
428section, there is also the ability to allocate private memory with the
429operation for applications purposes.
430
431Application software is responsible for specifying all the operation specific
432fields in the ``rte_crypto_op`` structure which are then used by the Crypto PMD
433to process the requested operation.
434
435
436Operation Management and Allocation
437~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
438
439The cryptodev library provides an API set for managing Crypto operations which
440utilize the Mempool Library to allocate operation buffers. Therefore, it ensures
441that the crypto operation is interleaved optimally across the channels and
442ranks for optimal processing.
443A ``rte_crypto_op`` contains a field indicating the pool that it originated from.
444When calling ``rte_crypto_op_free(op)``, the operation returns to its original pool.
445
446.. code-block:: c
447
448   extern struct rte_mempool *
449   rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
450                             unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
451                             int socket_id);
452
453During pool creation ``rte_crypto_op_init()`` is called as a constructor to
454initialize each Crypto operation which subsequently calls
455``__rte_crypto_op_reset()`` to configure any operation type specific fields based
456on the type parameter.
457
458
459``rte_crypto_op_alloc()`` and ``rte_crypto_op_bulk_alloc()`` are used to allocate
460Crypto operations of a specific type from a given Crypto operation mempool.
461``__rte_crypto_op_reset()`` is called on each operation before being returned to
462allocate to a user so the operation is always in a good known state before use
463by the application.
464
465.. code-block:: c
466
467   struct rte_crypto_op *rte_crypto_op_alloc(struct rte_mempool *mempool,
468                                             enum rte_crypto_op_type type)
469
470   unsigned rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
471                                     enum rte_crypto_op_type type,
472                                     struct rte_crypto_op **ops, uint16_t nb_ops)
473
474``rte_crypto_op_free()`` is called by the application to return an operation to
475its allocating pool.
476
477.. code-block:: c
478
479   void rte_crypto_op_free(struct rte_crypto_op *op)
480
481
482Symmetric Cryptography Support
483------------------------------
484
485The cryptodev library currently provides support for the following symmetric
486Crypto operations; cipher, authentication, including chaining of these
487operations, as well as also supporting AEAD operations.
488
489
490Session and Session Management
491~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
492
493Sessions are used in symmetric cryptographic processing to store the immutable
494data defined in a cryptographic transform which is used in the operation
495processing of a packet flow. Sessions are used to manage information such as
496expand cipher keys and HMAC IPADs and OPADs, which need to be calculated for a
497particular Crypto operation, but are immutable on a packet to packet basis for
498a flow. Crypto sessions cache this immutable data in a optimal way for the
499underlying PMD and this allows further acceleration of the offload of
500Crypto workloads.
501
502.. figure:: img/cryptodev_sym_sess.*
503
504The Crypto device framework provides APIs to create session mempool and allocate
505and initialize sessions for crypto devices, where sessions are mempool objects.
506The application has to use ``rte_cryptodev_sym_session_pool_create()`` to
507create the session header mempool that creates a mempool with proper element
508size automatically and stores necessary information for safely accessing the
509session in the mempool's private data field.
510
511To create a mempool for storing session private data, the application has two
512options. The first is to create another mempool with elt size equal to or
513bigger than the maximum session private data size of all crypto devices that
514will share the same session header. The creation of the mempool shall use the
515traditional ``rte_mempool_create()`` with the correct ``elt_size``. The other
516option is to change the ``elt_size`` parameter in
517``rte_cryptodev_sym_session_pool_create()`` to the correct value. The first
518option is more complex to implement but may result in better memory usage as
519a session header normally takes smaller memory footprint as the session private
520data.
521
522Once the session mempools have been created, ``rte_cryptodev_sym_session_create()``
523is used to allocate an uninitialized session from the given mempool.
524The session then must be initialized using ``rte_cryptodev_sym_session_init()``
525for each of the required crypto devices. A symmetric transform chain
526is used to specify the operation and its parameters. See the section below for
527details on transforms.
528
529When a session is no longer used, user must call ``rte_cryptodev_sym_session_clear()``
530for each of the crypto devices that are using the session, to free all driver
531private session data. Once this is done, session should be freed using
532``rte_cryptodev_sym_session_free`` which returns them to their mempool.
533
534
535Transforms and Transform Chaining
536~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
537
538Symmetric Crypto transforms (``rte_crypto_sym_xform``) are the mechanism used
539to specify the details of the Crypto operation. For chaining of symmetric
540operations such as cipher encrypt and authentication generate, the next pointer
541allows transform to be chained together. Crypto devices which support chaining
542must publish the chaining of symmetric Crypto operations feature flag. Allocation of the
543xform structure is in the application domain. To allow future API extensions in a
544backwardly compatible manner, e.g. addition of a new parameter, the application should
545zero the full xform struct before populating it.
546
547Currently there are three transforms types cipher, authentication and AEAD.
548Also it is important to note that the order in which the
549transforms are passed indicates the order of the chaining.
550
551.. code-block:: c
552
553    struct rte_crypto_sym_xform {
554        struct rte_crypto_sym_xform *next;
555        /**< next xform in chain */
556        enum rte_crypto_sym_xform_type type;
557        /**< xform type */
558        union {
559            struct rte_crypto_auth_xform auth;
560            /**< Authentication / hash xform */
561            struct rte_crypto_cipher_xform cipher;
562            /**< Cipher xform */
563            struct rte_crypto_aead_xform aead;
564            /**< AEAD xform */
565        };
566    };
567
568The API does not place a limit on the number of transforms that can be chained
569together but this will be limited by the underlying Crypto device poll mode
570driver which is processing the operation.
571
572.. figure:: img/crypto_xform_chain.*
573
574
575Symmetric Operations
576~~~~~~~~~~~~~~~~~~~~
577
578The symmetric Crypto operation structure contains all the mutable data relating
579to performing symmetric cryptographic processing on a referenced mbuf data
580buffer. It is used for either cipher, authentication, AEAD and chained
581operations.
582
583As a minimum the symmetric operation must have a source data buffer (``m_src``),
584a valid session (or transform chain if in session-less mode) and the minimum
585authentication/ cipher/ AEAD parameters required depending on the type of operation
586specified in the session or the transform
587chain.
588
589.. code-block:: c
590
591    struct rte_crypto_sym_op {
592        struct rte_mbuf *m_src;
593        struct rte_mbuf *m_dst;
594
595        union {
596            void *session;
597            /**< Handle for the initialised session context */
598            struct rte_crypto_sym_xform *xform;
599            /**< Session-less API Crypto operation parameters */
600        };
601
602        union {
603            struct {
604                struct {
605                    uint32_t offset;
606                    uint32_t length;
607                } data; /**< Data offsets and length for AEAD */
608
609                struct {
610                    uint8_t *data;
611                    rte_iova_t phys_addr;
612                } digest; /**< Digest parameters */
613
614                struct {
615                    uint8_t *data;
616                    rte_iova_t phys_addr;
617                } aad;
618                /**< Additional authentication parameters */
619            } aead;
620
621            struct {
622                struct {
623                    struct {
624                        uint32_t offset;
625                        uint32_t length;
626                    } data; /**< Data offsets and length for ciphering */
627                } cipher;
628
629                struct {
630                    struct {
631                        uint32_t offset;
632                        uint32_t length;
633                    } data;
634                    /**< Data offsets and length for authentication */
635
636                    struct {
637                        uint8_t *data;
638                        rte_iova_t phys_addr;
639                    } digest; /**< Digest parameters */
640                } auth;
641            };
642        };
643    };
644
645Synchronous mode
646----------------
647
648Some cryptodevs support synchronous mode alongside with a standard asynchronous
649mode. In that case operations are performed directly when calling
650``rte_cryptodev_sym_cpu_crypto_process`` method instead of enqueuing and
651dequeuing an operation before. This mode of operation allows cryptodevs which
652utilize CPU cryptographic acceleration to have significant performance boost
653comparing to standard asynchronous approach. Cryptodevs supporting synchronous
654mode have ``RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO`` feature flag set.
655
656To perform a synchronous operation a call to
657``rte_cryptodev_sym_cpu_crypto_process`` has to be made with vectorized
658operation descriptor (``struct rte_crypto_sym_vec``) containing:
659
660- ``num`` - number of operations to perform,
661- pointer to an array of size ``num`` containing a scatter-gather list
662  descriptors of performed operations (``struct rte_crypto_sgl``). Each instance
663  of ``struct rte_crypto_sgl`` consists of a number of segments and a pointer to
664  an array of segment descriptors ``struct rte_crypto_vec``;
665- pointers to arrays of size ``num`` containing IV, AAD and digest information
666  in the ``cpu_crypto`` sub-structure,
667- pointer to an array of size ``num`` where status information will be stored
668  for each operation.
669
670Function returns a number of successfully completed operations and sets
671appropriate status number for each operation in the status array provided as
672a call argument. Status different than zero must be treated as error.
673
674For more details, e.g. how to convert an mbuf to an SGL, please refer to an
675example usage in the IPsec library implementation.
676
677Cryptodev Raw Data-path APIs
678~~~~~~~~~~~~~~~~~~~~~~~~~~~~
679
680The Crypto Raw data-path APIs are a set of APIs designed to enable external
681libraries/applications to leverage the cryptographic processing provided by
682DPDK crypto PMDs through the cryptodev API but in a manner that is not
683dependent on native DPDK data structures (eg. rte_mbuf, rte_crypto_op, ... etc)
684in their data-path implementation.
685
686The raw data-path APIs have the following advantages:
687
688- External data structure friendly design. The new APIs uses the operation
689  descriptor ``struct rte_crypto_sym_vec`` that supports raw data pointer and
690  IOVA addresses as input. Moreover, the APIs does not require the user to
691  allocate the descriptor from mempool, nor requiring mbufs to describe input
692  data's virtual and IOVA addresses. All these features made the translation
693  from user's own data structure into the descriptor easier and more efficient.
694
695- Flexible enqueue and dequeue operation. The raw data-path APIs gives the
696  user more control to the enqueue and dequeue operations, including the
697  capability of precious enqueue/dequeue count, abandoning enqueue or dequeue
698  at any time, and operation status translation and set on the fly.
699
700Cryptodev PMDs which support the raw data-path APIs will have
701``RTE_CRYPTODEV_FF_SYM_RAW_DP`` feature flag presented. To use this feature,
702the user shall create a local ``struct rte_crypto_raw_dp_ctx`` buffer and
703extend to at least the length returned by ``rte_cryptodev_get_raw_dp_ctx_size``
704function call. The created buffer is then initialized using
705``rte_cryptodev_configure_raw_dp_ctx`` function with the ``is_update``
706parameter as 0. The library and the crypto device driver will then set the
707buffer and attach either the cryptodev sym session, the rte_security session,
708or the cryptodev xform for session-less operation into the ctx buffer, and
709set the corresponding enqueue and dequeue function handlers based on the
710algorithm information stored in the session or xform. When the ``is_update``
711parameter passed into ``rte_cryptodev_configure_raw_dp_ctx`` is 1, the driver
712will not initialize the buffer but only update the session or xform and
713the function handlers accordingly.
714
715After the ``struct rte_crypto_raw_dp_ctx`` buffer is initialized, it is now
716ready for enqueue and dequeue operation. There are two different enqueue
717functions: ``rte_cryptodev_raw_enqueue`` to enqueue single raw data
718operation, and ``rte_cryptodev_raw_enqueue_burst`` to enqueue a descriptor
719with multiple operations. In case of the application uses similar approach to
720``struct rte_crypto_sym_vec`` to manage its data burst but with different
721data structure, using the ``rte_cryptodev_raw_enqueue_burst`` function may be
722less efficient as this is a situation where the application has to loop over
723all crypto operations to assemble the ``struct rte_crypto_sym_vec`` descriptor
724from its own data structure, and then the driver will loop over them again to
725translate every operation in the descriptor to the driver's specific queue data.
726The ``rte_cryptodev_raw_enqueue`` should be used to save one loop for each data
727burst instead.
728
729The ``rte_cryptodev_raw_enqueue`` and ``rte_cryptodev_raw_enqueue_burst``
730functions will return or set the enqueue status. ``rte_cryptodev_raw_enqueue``
731will return the status directly, ``rte_cryptodev_raw_enqueue_burst`` will
732return the number of operations enqueued or stored (explained as follows) and
733set the ``enqueue_status`` buffer provided by the user. The possible
734enqueue status values are:
735
736- ``1``: the operation(s) is/are enqueued successfully.
737- ``0``: the operation(s) is/are cached successfully in the crypto device queue
738  but is not actually enqueued. The user shall call
739  ``rte_cryptodev_raw_enqueue_done`` function after the expected operations
740  are stored. The crypto device will then start enqueuing all of them at
741  once.
742- The negative integer: error occurred during enqueue.
743
744Calling ``rte_cryptodev_configure_raw_dp_ctx`` with the parameter ``is_update``
745set as 0 twice without the enqueue function returning or setting enqueue status
746to 1 or ``rte_cryptodev_raw_enqueue_done`` function being called in between will
747invalidate any operation stored in the device queue but not enqueued. This
748feature is useful when the user wants to abandon partially enqueued operations
749for a failed enqueue burst operation and try enqueuing in a whole later.
750
751Similar as enqueue, there are two dequeue functions:
752``rte_cryptodev_raw_dequeue`` for dequeuing single operation, and
753``rte_cryptodev_raw_dequeue_burst`` for dequeuing a burst of operations (e.g.
754all operations in a ``struct rte_crypto_sym_vec`` descriptor). The
755``rte_cryptodev_raw_dequeue_burst`` function allows the user to provide callback
756functions to retrieve dequeue count from the enqueued user data and write the
757expected status value to the user data on the fly. The dequeue functions also
758set the dequeue status:
759
760- ``1``: the operation(s) is/are dequeued successfully.
761- ``0``: the operation(s) is/are completed but is not actually dequeued (hence
762  still kept in the device queue). The user shall call the
763  ``rte_cryptodev_raw_dequeue_done`` function after the expected number of
764  operations (e.g. all operations in a descriptor) are dequeued. The crypto
765  device driver will then free them from the queue at once.
766- The negative integer: error occurred during dequeue.
767
768Calling ``rte_cryptodev_configure_raw_dp_ctx`` with the parameter ``is_update``
769set as 0 twice without the dequeue functions execution changed dequeue_status
770to 1 or ``rte_cryptodev_raw_dequeue_done`` function being called in between will
771revert the crypto device queue's dequeue effort to the moment when the
772``struct rte_crypto_raw_dp_ctx`` buffer is initialized. This feature is useful
773when the user wants to abandon partially dequeued data and try dequeuing again
774later in a whole.
775
776There are a few limitations to the raw data path APIs:
777
778* Only support in-place operations.
779* APIs are NOT thread-safe.
780* CANNOT mix the raw data-path API's enqueue with rte_cryptodev_enqueue_burst,
781  or vice versa.
782
783See *DPDK API Reference* for details on each API definitions.
784
785Sample code
786-----------
787
788There are various sample applications that show how to use the cryptodev library,
789such as the L2fwd with Crypto sample application (L2fwd-crypto) and
790the IPsec Security Gateway application (ipsec-secgw).
791
792While these applications demonstrate how an application can be created to perform
793generic crypto operation, the required complexity hides the basic steps of
794how to use the cryptodev APIs.
795
796The following sample code shows the basic steps to encrypt several buffers
797with AES-CBC (although performing other crypto operations is similar),
798using one of the crypto PMDs available in DPDK.
799
800.. code-block:: c
801
802    /*
803     * Simple example to encrypt several buffers with AES-CBC using
804     * the Cryptodev APIs.
805     */
806
807    #define MAX_SESSIONS         1024
808    #define NUM_MBUFS            1024
809    #define POOL_CACHE_SIZE      128
810    #define BURST_SIZE           32
811    #define BUFFER_SIZE          1024
812    #define AES_CBC_IV_LENGTH    16
813    #define AES_CBC_KEY_LENGTH   16
814    #define IV_OFFSET            (sizeof(struct rte_crypto_op) + \
815                                 sizeof(struct rte_crypto_sym_op))
816
817    struct rte_mempool *mbuf_pool, *crypto_op_pool;
818    struct rte_mempool *session_pool, *session_priv_pool;
819    unsigned int session_size;
820    int ret;
821
822    /* Initialize EAL. */
823    ret = rte_eal_init(argc, argv);
824    if (ret < 0)
825        rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
826
827    uint8_t socket_id = rte_socket_id();
828
829    /* Create the mbuf pool. */
830    mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool",
831                                    NUM_MBUFS,
832                                    POOL_CACHE_SIZE,
833                                    0,
834                                    RTE_MBUF_DEFAULT_BUF_SIZE,
835                                    socket_id);
836    if (mbuf_pool == NULL)
837        rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
838
839    /*
840     * The IV is always placed after the crypto operation,
841     * so some private data is required to be reserved.
842     */
843    unsigned int crypto_op_private_data = AES_CBC_IV_LENGTH;
844
845    /* Create crypto operation pool. */
846    crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
847                                            RTE_CRYPTO_OP_TYPE_SYMMETRIC,
848                                            NUM_MBUFS,
849                                            POOL_CACHE_SIZE,
850                                            crypto_op_private_data,
851                                            socket_id);
852    if (crypto_op_pool == NULL)
853        rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
854
855    /* Create the virtual crypto device. */
856    char args[128];
857    const char *crypto_name = "crypto_aesni_mb0";
858    snprintf(args, sizeof(args), "socket_id=%d", socket_id);
859    ret = rte_vdev_init(crypto_name, args);
860    if (ret != 0)
861        rte_exit(EXIT_FAILURE, "Cannot create virtual device");
862
863    uint8_t cdev_id = rte_cryptodev_get_dev_id(crypto_name);
864
865    /* Get private session data size. */
866    session_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
867
868    #ifdef USE_TWO_MEMPOOLS
869    /* Create session mempool for the session header. */
870    session_pool = rte_cryptodev_sym_session_pool_create("session_pool",
871                                    MAX_SESSIONS,
872                                    0,
873                                    POOL_CACHE_SIZE,
874                                    0,
875                                    socket_id);
876
877    /*
878     * Create session private data mempool for the
879     * private session data for the crypto device.
880     */
881    session_priv_pool = rte_mempool_create("session_pool",
882                                    MAX_SESSIONS,
883                                    session_size,
884                                    POOL_CACHE_SIZE,
885                                    0, NULL, NULL, NULL,
886                                    NULL, socket_id,
887                                    0);
888
889    #else
890    /* Use of the same mempool for session header and private data */
891	session_pool = rte_cryptodev_sym_session_pool_create("session_pool",
892                                    MAX_SESSIONS * 2,
893                                    session_size,
894                                    POOL_CACHE_SIZE,
895                                    0,
896                                    socket_id);
897
898	session_priv_pool = session_pool;
899
900    #endif
901
902    /* Configure the crypto device. */
903    struct rte_cryptodev_config conf = {
904        .nb_queue_pairs = 1,
905        .socket_id = socket_id
906    };
907
908    struct rte_cryptodev_qp_conf qp_conf = {
909        .nb_descriptors = 2048,
910        .mp_session = session_pool,
911        .mp_session_private = session_priv_pool
912    };
913
914    if (rte_cryptodev_configure(cdev_id, &conf) < 0)
915        rte_exit(EXIT_FAILURE, "Failed to configure cryptodev %u", cdev_id);
916
917    if (rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf, socket_id) < 0)
918        rte_exit(EXIT_FAILURE, "Failed to setup queue pair\n");
919
920    if (rte_cryptodev_start(cdev_id) < 0)
921        rte_exit(EXIT_FAILURE, "Failed to start device\n");
922
923    /* Create the crypto transform. */
924    uint8_t cipher_key[16] = {0};
925    struct rte_crypto_sym_xform cipher_xform = {
926        .next = NULL,
927        .type = RTE_CRYPTO_SYM_XFORM_CIPHER,
928        .cipher = {
929            .op = RTE_CRYPTO_CIPHER_OP_ENCRYPT,
930            .algo = RTE_CRYPTO_CIPHER_AES_CBC,
931            .key = {
932                .data = cipher_key,
933                .length = AES_CBC_KEY_LENGTH
934            },
935            .iv = {
936                .offset = IV_OFFSET,
937                .length = AES_CBC_IV_LENGTH
938            }
939        }
940    };
941
942    /* Create crypto session and initialize it for the crypto device. */
943    struct rte_cryptodev_sym_session *session;
944    session = rte_cryptodev_sym_session_create(cdev_id, &cipher_xform,
945                    session_pool);
946    if (session == NULL)
947        rte_exit(EXIT_FAILURE, "Session could not be created\n");
948
949    /* Get a burst of crypto operations. */
950    struct rte_crypto_op *crypto_ops[BURST_SIZE];
951    if (rte_crypto_op_bulk_alloc(crypto_op_pool,
952                            RTE_CRYPTO_OP_TYPE_SYMMETRIC,
953                            crypto_ops, BURST_SIZE) == 0)
954        rte_exit(EXIT_FAILURE, "Not enough crypto operations available\n");
955
956    /* Get a burst of mbufs. */
957    struct rte_mbuf *mbufs[BURST_SIZE];
958    if (rte_pktmbuf_alloc_bulk(mbuf_pool, mbufs, BURST_SIZE) < 0)
959        rte_exit(EXIT_FAILURE, "Not enough mbufs available");
960
961    /* Initialize the mbufs and append them to the crypto operations. */
962    unsigned int i;
963    for (i = 0; i < BURST_SIZE; i++) {
964        if (rte_pktmbuf_append(mbufs[i], BUFFER_SIZE) == NULL)
965            rte_exit(EXIT_FAILURE, "Not enough room in the mbuf\n");
966        crypto_ops[i]->sym->m_src = mbufs[i];
967    }
968
969    /* Set up the crypto operations. */
970    for (i = 0; i < BURST_SIZE; i++) {
971        struct rte_crypto_op *op = crypto_ops[i];
972        /* Modify bytes of the IV at the end of the crypto operation */
973        uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
974                                                IV_OFFSET);
975
976        generate_random_bytes(iv_ptr, AES_CBC_IV_LENGTH);
977
978        op->sym->cipher.data.offset = 0;
979        op->sym->cipher.data.length = BUFFER_SIZE;
980
981        /* Attach the crypto session to the operation */
982        rte_crypto_op_attach_sym_session(op, session);
983    }
984
985    /* Enqueue the crypto operations in the crypto device. */
986    uint16_t num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, 0,
987                                            crypto_ops, BURST_SIZE);
988
989    /*
990     * Dequeue the crypto operations until all the operations
991     * are processed in the crypto device.
992     */
993    uint16_t num_dequeued_ops, total_num_dequeued_ops = 0;
994    do {
995        struct rte_crypto_op *dequeued_ops[BURST_SIZE];
996        num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, 0,
997                                        dequeued_ops, BURST_SIZE);
998        total_num_dequeued_ops += num_dequeued_ops;
999
1000        /* Check if operation was processed successfully */
1001        for (i = 0; i < num_dequeued_ops; i++) {
1002            if (dequeued_ops[i]->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
1003                rte_exit(EXIT_FAILURE,
1004                        "Some operations were not processed correctly");
1005        }
1006
1007        rte_mempool_put_bulk(crypto_op_pool, (void **)dequeued_ops,
1008                                            num_dequeued_ops);
1009    } while (total_num_dequeued_ops < num_enqueued_ops);
1010
1011Asymmetric Cryptography
1012-----------------------
1013
1014The cryptodev library currently provides support for the following asymmetric
1015Crypto operations; RSA, Modular exponentiation and inversion, Diffie-Hellman and
1016Elliptic Curve Diffie-Hellman public and/or private key generation and shared
1017secret compute, DSA Signature generation and verification.
1018
1019Session and Session Management
1020~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1021
1022Sessions are used in asymmetric cryptographic processing to store the immutable
1023data defined in asymmetric cryptographic transform which is further used in the
1024operation processing. Sessions typically stores information, such as, public
1025and private key information or domain params or prime modulus data i.e. immutable
1026across data sets. Crypto sessions cache this immutable data in a optimal way for the
1027underlying PMD and this allows further acceleration of the offload of Crypto workloads.
1028
1029Like symmetric, the Crypto device framework provides APIs to allocate and initialize
1030asymmetric sessions for crypto devices, where sessions are mempool objects.
1031It is the application's responsibility to create and manage the session mempools.
1032Application using both symmetric and asymmetric sessions should allocate and maintain
1033different sessions pools for each type.
1034
1035An application can use ``rte_cryptodev_asym_session_pool_create()`` to create a mempool
1036with a specified number of elements. The element size will allow for the session header,
1037and the max private session size.
1038The max private session size is chosen based on available crypto devices,
1039the biggest private session size is used. This means any of those devices can be used,
1040and the mempool element will have available space for its private session data.
1041
1042Once the session mempools have been created, ``rte_cryptodev_asym_session_create()``
1043is used to allocate and initialize an asymmetric session from the given mempool.
1044An asymmetric transform chain is used to specify the operation and its parameters.
1045See the section below for details on transforms.
1046
1047When a session is no longer used, user must call ``rte_cryptodev_asym_session_clear()``
1048for each of the crypto devices that are using the session, to free all driver
1049private asymmetric session data. Once this is done, session should be freed using
1050``rte_cryptodev_asym_session_free()`` which returns them to their mempool.
1051
1052Asymmetric Sessionless Support
1053~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1054
1055Asymmetric crypto framework supports session-less operations as well.
1056
1057Fields that should be set by user are:
1058
1059Member xform of struct rte_crypto_asym_op should point to the user created rte_crypto_asym_xform.
1060Note that rte_crypto_asym_xform should be immutable for the lifetime of associated crypto_op.
1061
1062Member sess_type of rte_crypto_op should also be set to RTE_CRYPTO_OP_SESSIONLESS.
1063
1064Transforms and Transform Chaining
1065~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1066
1067Asymmetric Crypto transforms (``rte_crypto_asym_xform``) are the mechanism used
1068to specify the details of the asymmetric Crypto operation. Next pointer within
1069xform allows transform to be chained together. Also it is important to note that
1070the order in which the transforms are passed indicates the order of the chaining. Allocation
1071of the xform structure is in the application domain. To allow future API extensions in a
1072backwardly compatible manner, e.g. addition of a new parameter, the application should
1073zero the full xform struct before populating it.
1074
1075Not all asymmetric crypto xforms are supported for chaining. Currently supported
1076asymmetric crypto chaining is Diffie-Hellman private key generation followed by
1077public generation. Also, currently API does not support chaining of symmetric and
1078asymmetric crypto xforms.
1079
1080Each xform defines specific asymmetric crypto algo. Currently supported are:
1081* RSA
1082* Modular operations (Exponentiation and Inverse)
1083* Diffie-Hellman
1084* DSA
1085* Elliptic Curve Diffie-Hellman
1086* None - special case where PMD may support a passthrough mode. More for diagnostic purpose
1087
1088See *DPDK API Reference* for details on each rte_crypto_xxx_xform struct
1089
1090Asymmetric Operations
1091~~~~~~~~~~~~~~~~~~~~~
1092
1093The asymmetric Crypto operation structure contains all the mutable data relating
1094to asymmetric cryptographic processing on an input data buffer. It uses either
1095RSA, Modular, Diffie-Hellman or DSA operations depending upon session it is attached
1096to.
1097
1098Every operation must carry a valid session handle which further carries information
1099on xform or xform-chain to be performed on op. Every xform type defines its own set
1100of operational params in their respective rte_crypto_xxx_op_param struct. Depending
1101on xform information within session, PMD picks up and process respective op_param
1102struct.
1103Unlike symmetric, asymmetric operations do not use mbufs for input/output.
1104They operate on data buffer of type ``rte_crypto_param``.
1105
1106See *DPDK API Reference* for details on each rte_crypto_xxx_op_param struct
1107
1108Private user data
1109~~~~~~~~~~~~~~~~~
1110
1111Similar to symmetric above, asymmetric also has a set and get API that provides a
1112mechanism for an application to store and retrieve the private user data information
1113stored along with the crypto session.
1114
1115.. code-block:: c
1116
1117	int rte_cryptodev_asym_session_set_user_data(void *sess,
1118		void *data, uint16_t size);
1119
1120	void * rte_cryptodev_asym_session_get_user_data(void *sess);
1121
1122Please note the ``size`` passed to set API cannot be bigger than the predefined
1123``user_data_sz`` when creating the session mempool, otherwise the function will
1124return an error. Also when ``user_data_sz`` was defined as ``0`` when
1125creating the session mempool, the get API will always return ``NULL``.
1126
1127Asymmetric crypto Sample code
1128-----------------------------
1129
1130There's a unit test application test_cryptodev_asym.c inside unit test framework that
1131show how to setup and process asymmetric operations using cryptodev library.
1132
1133The following code samples are taken from the test application mentioned above,
1134and show basic steps to compute modular exponentiation using an openssl PMD
1135available in DPDK (performing other crypto operations is similar except change
1136to respective op and xform setup).
1137
1138.. note::
1139   The following code snippets are taken from multiple functions, so variable
1140   names may differ slightly between sections.
1141
1142Configure the virtual device, queue pairs, crypto op pool and session mempool.
1143
1144.. literalinclude:: ../../../app/test/test_cryptodev_asym.c
1145   :language: c
1146   :start-after: Device, op pool and session configuration for asymmetric crypto. 8<
1147   :end-before: >8 End of device, op pool and session configuration for asymmetric crypto section.
1148   :dedent: 1
1149
1150Create MODEX data vectors.
1151
1152.. literalinclude:: ../../../app/test/test_cryptodev_mod_test_vectors.h
1153   :language: c
1154   :start-after: MODEX data. 8<
1155   :end-before: >8 End of MODEX data.
1156
1157Setup crypto xform to do modular exponentiation using data vectors.
1158
1159.. literalinclude:: ../../../app/test/test_cryptodev_mod_test_vectors.h
1160   :language: c
1161   :start-after: MODEX vector. 8<
1162   :end-before: >8 End of MODEX vector.
1163
1164Generate crypto op, create and attach a session, then process packets.
1165
1166.. literalinclude:: ../../../app/test/test_cryptodev_asym.c
1167   :language: c
1168   :start-after: Create op, create session, and process packets. 8<
1169   :end-before: >8 End of create op, create session, and process packets section.
1170   :dedent: 1
1171
1172.. note::
1173   The ``rte_cryptodev_asym_session`` struct is hidden from the application.
1174   The ``sess`` pointer used above is a void pointer.
1175
1176
1177Asymmetric Crypto Device API
1178~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1179
1180The cryptodev Library API is described in the
1181`DPDK API Reference <https://doc.dpdk.org/api/>`_
1182
1183
1184Device Statistics
1185-----------------
1186
1187The Cryptodev library has support for displaying Crypto device information
1188through the Telemetry interface. Telemetry commands that can be used
1189are shown below.
1190
1191#. Get the list of available Crypto devices by ID::
1192
1193     --> /cryptodev/list
1194     {"/cryptodev/list": [0, 1, 2, 3]}
1195
1196#. Get general information from a Crypto device::
1197
1198     --> /cryptodev/info,0
1199     {"/cryptodev/info": {"device_name": "0000:1c:01.0_qat_sym",
1200     "max_nb_queue_pairs": 2}}
1201
1202#. Get the statistics for a particular Crypto device::
1203
1204     --> /cryptodev/stats,0
1205     {"/cryptodev/stats": {"enqueued_count": 0, "dequeued_count": 0,
1206     "enqueue_err_count": 0, "dequeue_err_count": 0}}
1207
1208#. Get the capabilities of a particular Crypto device::
1209
1210     --> /cryptodev/caps,0
1211     {"/cryptodev/caps": {"crypto_caps": [<array of serialized bytes of
1212     capabilities>], "crypto_caps_n": <number of capabilities>}}
1213
1214For more information on how to use the Telemetry interface, see
1215the :doc:`../howto/telemetry`.
1216