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