cryptodev: change queue pair configure structure
[dpdk.git] / doc / guides / prog_guide / cryptodev_lib.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2016-2017 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 DPDKs
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 white-listed or black-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 instaces sharing the same library requires unique ID.
52
53    Example: ``--vdev  'crypto_aesni_mb0' --vdev  'crypto_aesni_mb1'``
54
55 Our 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 place 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
331 For session-less mode, the private user data information can be placed along with the
332 ``struct rte_crypto_op``. The ``rte_crypto_op::private_data_offset`` indicates the
333 start of private data information. The offset is counted from the start of the
334 rte_crypto_op including other crypto information such as the IVs (since there can
335 be an IV also for authentication).
336
337
338 Enqueue / Dequeue Burst APIs
339 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340
341 The burst enqueue API uses a Crypto device identifier and a queue pair
342 identifier to specify the Crypto device queue pair to schedule the processing on.
343 The ``nb_ops`` parameter is the number of operations to process which are
344 supplied in the ``ops`` array of ``rte_crypto_op`` structures.
345 The enqueue function returns the number of operations it actually enqueued for
346 processing, a return value equal to ``nb_ops`` means that all packets have been
347 enqueued.
348
349 .. code-block:: c
350
351    uint16_t rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
352                                         struct rte_crypto_op **ops, uint16_t nb_ops)
353
354 The dequeue API uses the same format as the enqueue API of processed but
355 the ``nb_ops`` and ``ops`` parameters are now used to specify the max processed
356 operations the user wishes to retrieve and the location in which to store them.
357 The API call returns the actual number of processed operations returned, this
358 can never be larger than ``nb_ops``.
359
360 .. code-block:: c
361
362    uint16_t rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
363                                         struct rte_crypto_op **ops, uint16_t nb_ops)
364
365
366 Operation Representation
367 ~~~~~~~~~~~~~~~~~~~~~~~~
368
369 An Crypto operation is represented by an rte_crypto_op structure, which is a
370 generic metadata container for all necessary information required for the
371 Crypto operation to be processed on a particular Crypto device poll mode driver.
372
373 .. figure:: img/crypto_op.*
374
375 The operation structure includes the operation type, the operation status
376 and the session type (session-based/less), a reference to the operation
377 specific data, which can vary in size and content depending on the operation
378 being provisioned. It also contains the source mempool for the operation,
379 if it allocated from a mempool.
380
381 If Crypto operations are allocated from a Crypto operation mempool, see next
382 section, there is also the ability to allocate private memory with the
383 operation for applications purposes.
384
385 Application software is responsible for specifying all the operation specific
386 fields in the ``rte_crypto_op`` structure which are then used by the Crypto PMD
387 to process the requested operation.
388
389
390 Operation Management and Allocation
391 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
392
393 The cryptodev library provides an API set for managing Crypto operations which
394 utilize the Mempool Library to allocate operation buffers. Therefore, it ensures
395 that the crytpo operation is interleaved optimally across the channels and
396 ranks for optimal processing.
397 A ``rte_crypto_op`` contains a field indicating the pool that it originated from.
398 When calling ``rte_crypto_op_free(op)``, the operation returns to its original pool.
399
400 .. code-block:: c
401
402    extern struct rte_mempool *
403    rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
404                              unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
405                              int socket_id);
406
407 During pool creation ``rte_crypto_op_init()`` is called as a constructor to
408 initialize each Crypto operation which subsequently calls
409 ``__rte_crypto_op_reset()`` to configure any operation type specific fields based
410 on the type parameter.
411
412
413 ``rte_crypto_op_alloc()`` and ``rte_crypto_op_bulk_alloc()`` are used to allocate
414 Crypto operations of a specific type from a given Crypto operation mempool.
415 ``__rte_crypto_op_reset()`` is called on each operation before being returned to
416 allocate to a user so the operation is always in a good known state before use
417 by the application.
418
419 .. code-block:: c
420
421    struct rte_crypto_op *rte_crypto_op_alloc(struct rte_mempool *mempool,
422                                              enum rte_crypto_op_type type)
423
424    unsigned rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
425                                      enum rte_crypto_op_type type,
426                                      struct rte_crypto_op **ops, uint16_t nb_ops)
427
428 ``rte_crypto_op_free()`` is called by the application to return an operation to
429 its allocating pool.
430
431 .. code-block:: c
432
433    void rte_crypto_op_free(struct rte_crypto_op *op)
434
435
436 Symmetric Cryptography Support
437 ------------------------------
438
439 The cryptodev library currently provides support for the following symmetric
440 Crypto operations; cipher, authentication, including chaining of these
441 operations, as well as also supporting AEAD operations.
442
443
444 Session and Session Management
445 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
446
447 Sessions are used in symmetric cryptographic processing to store the immutable
448 data defined in a cryptographic transform which is used in the operation
449 processing of a packet flow. Sessions are used to manage information such as
450 expand cipher keys and HMAC IPADs and OPADs, which need to be calculated for a
451 particular Crypto operation, but are immutable on a packet to packet basis for
452 a flow. Crypto sessions cache this immutable data in a optimal way for the
453 underlying PMD and this allows further acceleration of the offload of
454 Crypto workloads.
455
456 .. figure:: img/cryptodev_sym_sess.*
457
458 The Crypto device framework provides APIs to allocate and initialize sessions
459 for crypto devices, where sessions are mempool objects.
460 It is the application's responsibility to create and manage the session mempools.
461 This approach allows for different scenarios such as having a single session
462 mempool for all crypto devices (where the mempool object size is big
463 enough to hold the private session of any crypto device), as well as having
464 multiple session mempools of different sizes for better memory usage.
465
466 An application can use ``rte_cryptodev_sym_get_private_session_size()`` to
467 get the private session size of given crypto device. This function would allow
468 an application to calculate the max device session size of all crypto devices
469 to create a single session mempool.
470 If instead an application creates multiple session mempools, the Crypto device
471 framework also provides ``rte_cryptodev_sym_get_header_session_size`` to get
472 the size of an uninitialized session.
473
474 Once the session mempools have been created, ``rte_cryptodev_sym_session_create()``
475 is used to allocate an uninitialized session from the given mempool.
476 The session then must be initialized using ``rte_cryptodev_sym_session_init()``
477 for each of the required crypto devices. A symmetric transform chain
478 is used to specify the operation and its parameters. See the section below for
479 details on transforms.
480
481 When a session is no longer used, user must call ``rte_cryptodev_sym_session_clear()``
482 for each of the crypto devices that are using the session, to free all driver
483 private session data. Once this is done, session should be freed using
484 ``rte_cryptodev_sym_session_free`` which returns them to their mempool.
485
486
487 Transforms and Transform Chaining
488 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
489
490 Symmetric Crypto transforms (``rte_crypto_sym_xform``) are the mechanism used
491 to specify the details of the Crypto operation. For chaining of symmetric
492 operations such as cipher encrypt and authentication generate, the next pointer
493 allows transform to be chained together. Crypto devices which support chaining
494 must publish the chaining of symmetric Crypto operations feature flag.
495
496 Currently there are three transforms types cipher, authentication and AEAD.
497 Also it is important to note that the order in which the
498 transforms are passed indicates the order of the chaining.
499
500 .. code-block:: c
501
502     struct rte_crypto_sym_xform {
503         struct rte_crypto_sym_xform *next;
504         /**< next xform in chain */
505         enum rte_crypto_sym_xform_type type;
506         /**< xform type */
507         union {
508             struct rte_crypto_auth_xform auth;
509             /**< Authentication / hash xform */
510             struct rte_crypto_cipher_xform cipher;
511             /**< Cipher xform */
512             struct rte_crypto_aead_xform aead;
513             /**< AEAD xform */
514         };
515     };
516
517 The API does not place a limit on the number of transforms that can be chained
518 together but this will be limited by the underlying Crypto device poll mode
519 driver which is processing the operation.
520
521 .. figure:: img/crypto_xform_chain.*
522
523
524 Symmetric Operations
525 ~~~~~~~~~~~~~~~~~~~~
526
527 The symmetric Crypto operation structure contains all the mutable data relating
528 to performing symmetric cryptographic processing on a referenced mbuf data
529 buffer. It is used for either cipher, authentication, AEAD and chained
530 operations.
531
532 As a minimum the symmetric operation must have a source data buffer (``m_src``),
533 a valid session (or transform chain if in session-less mode) and the minimum
534 authentication/ cipher/ AEAD parameters required depending on the type of operation
535 specified in the session or the transform
536 chain.
537
538 .. code-block:: c
539
540     struct rte_crypto_sym_op {
541         struct rte_mbuf *m_src;
542         struct rte_mbuf *m_dst;
543
544         union {
545             struct rte_cryptodev_sym_session *session;
546             /**< Handle for the initialised session context */
547             struct rte_crypto_sym_xform *xform;
548             /**< Session-less API Crypto operation parameters */
549         };
550
551         union {
552             struct {
553                 struct {
554                     uint32_t offset;
555                     uint32_t length;
556                 } data; /**< Data offsets and length for AEAD */
557
558                 struct {
559                     uint8_t *data;
560                     rte_iova_t phys_addr;
561                 } digest; /**< Digest parameters */
562
563                 struct {
564                     uint8_t *data;
565                     rte_iova_t phys_addr;
566                 } aad;
567                 /**< Additional authentication parameters */
568             } aead;
569
570             struct {
571                 struct {
572                     struct {
573                         uint32_t offset;
574                         uint32_t length;
575                     } data; /**< Data offsets and length for ciphering */
576                 } cipher;
577
578                 struct {
579                     struct {
580                         uint32_t offset;
581                         uint32_t length;
582                     } data;
583                     /**< Data offsets and length for authentication */
584
585                     struct {
586                         uint8_t *data;
587                         rte_iova_t phys_addr;
588                     } digest; /**< Digest parameters */
589                 } auth;
590             };
591         };
592     };
593
594 Sample code
595 -----------
596
597 There are various sample applications that show how to use the cryptodev library,
598 such as the L2fwd with Crypto sample application (L2fwd-crypto) and
599 the IPSec Security Gateway application (ipsec-secgw).
600
601 While these applications demonstrate how an application can be created to perform
602 generic crypto operation, the required complexity hides the basic steps of
603 how to use the cryptodev APIs.
604
605 The following sample code shows the basic steps to encrypt several buffers
606 with AES-CBC (although performing other crypto operations is similar),
607 using one of the crypto PMDs available in DPDK.
608
609 .. code-block:: c
610
611     /*
612      * Simple example to encrypt several buffers with AES-CBC using
613      * the Cryptodev APIs.
614      */
615
616     #define MAX_SESSIONS         1024
617     #define NUM_MBUFS            1024
618     #define POOL_CACHE_SIZE      128
619     #define BURST_SIZE           32
620     #define BUFFER_SIZE          1024
621     #define AES_CBC_IV_LENGTH    16
622     #define AES_CBC_KEY_LENGTH   16
623     #define IV_OFFSET            (sizeof(struct rte_crypto_op) + \
624                                  sizeof(struct rte_crypto_sym_op))
625
626     struct rte_mempool *mbuf_pool, *crypto_op_pool, *session_pool;
627     unsigned int session_size;
628     int ret;
629
630     /* Initialize EAL. */
631     ret = rte_eal_init(argc, argv);
632     if (ret < 0)
633         rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
634
635     uint8_t socket_id = rte_socket_id();
636
637     /* Create the mbuf pool. */
638     mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool",
639                                     NUM_MBUFS,
640                                     POOL_CACHE_SIZE,
641                                     0,
642                                     RTE_MBUF_DEFAULT_BUF_SIZE,
643                                     socket_id);
644     if (mbuf_pool == NULL)
645         rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
646
647     /*
648      * The IV is always placed after the crypto operation,
649      * so some private data is required to be reserved.
650      */
651     unsigned int crypto_op_private_data = AES_CBC_IV_LENGTH;
652
653     /* Create crypto operation pool. */
654     crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
655                                             RTE_CRYPTO_OP_TYPE_SYMMETRIC,
656                                             NUM_MBUFS,
657                                             POOL_CACHE_SIZE,
658                                             crypto_op_private_data,
659                                             socket_id);
660     if (crypto_op_pool == NULL)
661         rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
662
663     /* Create the virtual crypto device. */
664     char args[128];
665     const char *crypto_name = "crypto_aesni_mb0";
666     snprintf(args, sizeof(args), "socket_id=%d", socket_id);
667     ret = rte_vdev_init(crypto_name, args);
668     if (ret != 0)
669         rte_exit(EXIT_FAILURE, "Cannot create virtual device");
670
671     uint8_t cdev_id = rte_cryptodev_get_dev_id(crypto_name);
672
673     /* Get private session data size. */
674     session_size = rte_cryptodev_sym_get_private_session_size(cdev_id);
675
676     /*
677      * Create session mempool, with two objects per session,
678      * one for the session header and another one for the
679      * private session data for the crypto device.
680      */
681     session_pool = rte_mempool_create("session_pool",
682                                     MAX_SESSIONS * 2,
683                                     session_size,
684                                     POOL_CACHE_SIZE,
685                                     0, NULL, NULL, NULL,
686                                     NULL, socket_id,
687                                     0);
688
689     /* Configure the crypto device. */
690     struct rte_cryptodev_config conf = {
691         .nb_queue_pairs = 1,
692         .socket_id = socket_id
693     };
694     struct rte_cryptodev_qp_conf qp_conf = {
695         .nb_descriptors = 2048,
696         .mp_session = session_pool,
697         .mp_session_private = session_pool
698     };
699
700     if (rte_cryptodev_configure(cdev_id, &conf) < 0)
701         rte_exit(EXIT_FAILURE, "Failed to configure cryptodev %u", cdev_id);
702
703     if (rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf, socket_id) < 0)
704         rte_exit(EXIT_FAILURE, "Failed to setup queue pair\n");
705
706     if (rte_cryptodev_start(cdev_id) < 0)
707         rte_exit(EXIT_FAILURE, "Failed to start device\n");
708
709     /* Create the crypto transform. */
710     uint8_t cipher_key[16] = {0};
711     struct rte_crypto_sym_xform cipher_xform = {
712         .next = NULL,
713         .type = RTE_CRYPTO_SYM_XFORM_CIPHER,
714         .cipher = {
715             .op = RTE_CRYPTO_CIPHER_OP_ENCRYPT,
716             .algo = RTE_CRYPTO_CIPHER_AES_CBC,
717             .key = {
718                 .data = cipher_key,
719                 .length = AES_CBC_KEY_LENGTH
720             },
721             .iv = {
722                 .offset = IV_OFFSET,
723                 .length = AES_CBC_IV_LENGTH
724             }
725         }
726     };
727
728     /* Create crypto session and initialize it for the crypto device. */
729     struct rte_cryptodev_sym_session *session;
730     session = rte_cryptodev_sym_session_create(session_pool);
731     if (session == NULL)
732         rte_exit(EXIT_FAILURE, "Session could not be created\n");
733
734     if (rte_cryptodev_sym_session_init(cdev_id, session,
735                     &cipher_xform, session_pool) < 0)
736         rte_exit(EXIT_FAILURE, "Session could not be initialized "
737                     "for the crypto device\n");
738
739     /* Get a burst of crypto operations. */
740     struct rte_crypto_op *crypto_ops[BURST_SIZE];
741     if (rte_crypto_op_bulk_alloc(crypto_op_pool,
742                             RTE_CRYPTO_OP_TYPE_SYMMETRIC,
743                             crypto_ops, BURST_SIZE) == 0)
744         rte_exit(EXIT_FAILURE, "Not enough crypto operations available\n");
745
746     /* Get a burst of mbufs. */
747     struct rte_mbuf *mbufs[BURST_SIZE];
748     if (rte_pktmbuf_alloc_bulk(mbuf_pool, mbufs, BURST_SIZE) < 0)
749         rte_exit(EXIT_FAILURE, "Not enough mbufs available");
750
751     /* Initialize the mbufs and append them to the crypto operations. */
752     unsigned int i;
753     for (i = 0; i < BURST_SIZE; i++) {
754         if (rte_pktmbuf_append(mbufs[i], BUFFER_SIZE) == NULL)
755             rte_exit(EXIT_FAILURE, "Not enough room in the mbuf\n");
756         crypto_ops[i]->sym->m_src = mbufs[i];
757     }
758
759     /* Set up the crypto operations. */
760     for (i = 0; i < BURST_SIZE; i++) {
761         struct rte_crypto_op *op = crypto_ops[i];
762         /* Modify bytes of the IV at the end of the crypto operation */
763         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
764                                                 IV_OFFSET);
765
766         generate_random_bytes(iv_ptr, AES_CBC_IV_LENGTH);
767
768         op->sym->cipher.data.offset = 0;
769         op->sym->cipher.data.length = BUFFER_SIZE;
770
771         /* Attach the crypto session to the operation */
772         rte_crypto_op_attach_sym_session(op, session);
773     }
774
775     /* Enqueue the crypto operations in the crypto device. */
776     uint16_t num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, 0,
777                                             crypto_ops, BURST_SIZE);
778
779     /*
780      * Dequeue the crypto operations until all the operations
781      * are proccessed in the crypto device.
782      */
783     uint16_t num_dequeued_ops, total_num_dequeued_ops = 0;
784     do {
785         struct rte_crypto_op *dequeued_ops[BURST_SIZE];
786         num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, 0,
787                                         dequeued_ops, BURST_SIZE);
788         total_num_dequeued_ops += num_dequeued_ops;
789
790         /* Check if operation was processed successfully */
791         for (i = 0; i < num_dequeued_ops; i++) {
792             if (dequeued_ops[i]->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
793                 rte_exit(EXIT_FAILURE,
794                         "Some operations were not processed correctly");
795         }
796
797         rte_mempool_put_bulk(crypto_op_pool, (void **)dequeued_ops,
798                                             num_dequeued_ops);
799     } while (total_num_dequeued_ops < num_enqueued_ops);
800
801 Asymmetric Cryptography
802 -----------------------
803
804 The cryptodev library currently provides support for the following asymmetric
805 Crypto operations; RSA, Modular exponentiation and inversion, Diffie-Hellman
806 public and/or private key generation and shared secret compute, DSA Signature
807 generation and verification.
808
809 Session and Session Management
810 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
811
812 Sessions are used in asymmetric cryptographic processing to store the immutable
813 data defined in asymmetric cryptographic transform which is further used in the
814 operation processing. Sessions typically stores information, such as, public
815 and private key information or domain params or prime modulus data i.e. immutable
816 across data sets. Crypto sessions cache this immutable data in a optimal way for the
817 underlying PMD and this allows further acceleration of the offload of Crypto workloads.
818
819 Like symmetric, the Crypto device framework provides APIs to allocate and initialize
820 asymmetric sessions for crypto devices, where sessions are mempool objects.
821 It is the application's responsibility to create and manage the session mempools.
822 Application using both symmetric and asymmetric sessions should allocate and maintain
823 different sessions pools for each type.
824
825 An application can use ``rte_cryptodev_get_asym_session_private_size()`` to
826 get the private size of asymmetric session on a given crypto device. This
827 function would allow an application to calculate the max device asymmetric
828 session size of all crypto devices to create a single session mempool.
829 If instead an application creates multiple asymmetric session mempools,
830 the Crypto device framework also provides ``rte_cryptodev_asym_get_header_session_size()`` to get
831 the size of an uninitialized session.
832
833 Once the session mempools have been created, ``rte_cryptodev_asym_session_create()``
834 is used to allocate an uninitialized asymmetric session from the given mempool.
835 The session then must be initialized using ``rte_cryptodev_asym_session_init()``
836 for each of the required crypto devices. An asymmetric transform chain
837 is used to specify the operation and its parameters. See the section below for
838 details on transforms.
839
840 When a session is no longer used, user must call ``rte_cryptodev_asym_session_clear()``
841 for each of the crypto devices that are using the session, to free all driver
842 private asymmetric session data. Once this is done, session should be freed using
843 ``rte_cryptodev_asym_session_free()`` which returns them to their mempool.
844
845 Asymmetric Sessionless Support
846 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
847 Currently asymmetric crypto framework does not support sessionless.
848
849 Transforms and Transform Chaining
850 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
851
852 Asymmetric Crypto transforms (``rte_crypto_asym_xform``) are the mechanism used
853 to specify the details of the asymmetric Crypto operation. Next pointer within
854 xform allows transform to be chained together. Also it is important to note that
855 the order in which the transforms are passed indicates the order of the chaining.
856
857 Not all asymmetric crypto xforms are supported for chaining. Currently supported
858 asymmetric crypto chaining is Diffie-Hellman private key generation followed by
859 public generation. Also, currently API does not support chaining of symmetric and
860 asymmetric crypto xfroms.
861
862 Each xform defines specific asymmetric crypto algo. Currently supported are:
863 * RSA
864 * Modular operations (Exponentiation and Inverse)
865 * Diffie-Hellman
866 * DSA
867 * None - special case where PMD may support a passthrough mode. More for diagnostic purpose
868
869 See *DPDK API Reference* for details on each rte_crypto_xxx_xform struct
870
871 Asymmetric Operations
872 ~~~~~~~~~~~~~~~~~~~~~
873
874 The asymmetric Crypto operation structure contains all the mutable data relating
875 to asymmetric cryptographic processing on an input data buffer. It uses either
876 RSA, Modular, Diffie-Hellman or DSA operations depending upon session it is attached
877 to.
878
879 Every operation must carry a valid session handle which further carries information
880 on xform or xform-chain to be performed on op. Every xform type defines its own set
881 of operational params in their respective rte_crypto_xxx_op_param struct. Depending
882 on xform information within session, PMD picks up and process respective op_param
883 struct.
884 Unlike symmetric, asymmetric operations do not use mbufs for input/output.
885 They operate on data buffer of type ``rte_crypto_param``.
886
887 See *DPDK API Reference* for details on each rte_crypto_xxx_op_param struct
888
889 Asymmetric crypto Sample code
890 -----------------------------
891
892 There's a unit test application test_cryptodev_asym.c inside unit test framework that
893 show how to setup and process asymmetric operations using cryptodev library.
894
895 The following sample code shows the basic steps to compute modular exponentiation
896 using 1024-bit modulus length using openssl PMD available in DPDK (performing other
897 crypto operations is similar except change to respective op and xform setup).
898
899 .. code-block:: c
900
901     /*
902      * Simple example to compute modular exponentiation with 1024-bit key
903      *
904      */
905     #define MAX_ASYM_SESSIONS   10
906     #define NUM_ASYM_BUFS       10
907
908     struct rte_mempool *crypto_op_pool, *asym_session_pool;
909     unsigned int asym_session_size;
910     int ret;
911
912     /* Initialize EAL. */
913     ret = rte_eal_init(argc, argv);
914     if (ret < 0)
915         rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
916
917     uint8_t socket_id = rte_socket_id();
918
919     /* Create crypto operation pool. */
920     crypto_op_pool = rte_crypto_op_pool_create(
921                                     "crypto_op_pool",
922                                     RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
923                                     NUM_ASYM_BUFS, 0, 0,
924                                     socket_id);
925     if (crypto_op_pool == NULL)
926         rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
927
928     /* Create the virtual crypto device. */
929     char args[128];
930     const char *crypto_name = "crypto_openssl";
931     snprintf(args, sizeof(args), "socket_id=%d", socket_id);
932     ret = rte_vdev_init(crypto_name, args);
933     if (ret != 0)
934         rte_exit(EXIT_FAILURE, "Cannot create virtual device");
935
936     uint8_t cdev_id = rte_cryptodev_get_dev_id(crypto_name);
937
938     /* Get private asym session data size. */
939     asym_session_size = rte_cryptodev_get_asym_private_session_size(cdev_id);
940
941     /*
942      * Create session mempool, with two objects per session,
943      * one for the session header and another one for the
944      * private asym session data for the crypto device.
945      */
946     asym_session_pool = rte_mempool_create("asym_session_pool",
947                                     MAX_ASYM_SESSIONS * 2,
948                                     asym_session_size,
949                                     0,
950                                     0, NULL, NULL, NULL,
951                                     NULL, socket_id,
952                                     0);
953
954     /* Configure the crypto device. */
955     struct rte_cryptodev_config conf = {
956         .nb_queue_pairs = 1,
957         .socket_id = socket_id
958     };
959     struct rte_cryptodev_qp_conf qp_conf = {
960         .nb_descriptors = 2048
961     };
962
963     if (rte_cryptodev_configure(cdev_id, &conf) < 0)
964         rte_exit(EXIT_FAILURE, "Failed to configure cryptodev %u", cdev_id);
965
966     if (rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
967                             socket_id, asym_session_pool) < 0)
968         rte_exit(EXIT_FAILURE, "Failed to setup queue pair\n");
969
970     if (rte_cryptodev_start(cdev_id) < 0)
971         rte_exit(EXIT_FAILURE, "Failed to start device\n");
972
973     /* Setup crypto xform to do modular exponentiation with 1024 bit
974          * length modulus
975          */
976     struct rte_crypto_asym_xform modex_xform = {
977                 .next = NULL,
978                 .xform_type = RTE_CRYPTO_ASYM_XFORM_MODEX,
979                 .modex = {
980                         .modulus = {
981                                 .data =
982                                 (uint8_t *)
983                                 ("\xb3\xa1\xaf\xb7\x13\x08\x00\x0a\x35\xdc\x2b\x20\x8d"
984                                 "\xa1\xb5\xce\x47\x8a\xc3\x80\xf4\x7d\x4a\xa2\x62\xfd\x61\x7f"
985                                 "\xb5\xa8\xde\x0a\x17\x97\xa0\xbf\xdf\x56\x5a\x3d\x51\x56\x4f"
986                                 "\x70\x70\x3f\x63\x6a\x44\x5b\xad\x84\x0d\x3f\x27\x6e\x3b\x34"
987                                 "\x91\x60\x14\xb9\xaa\x72\xfd\xa3\x64\xd2\x03\xa7\x53\x87\x9e"
988                                 "\x88\x0b\xc1\x14\x93\x1a\x62\xff\xb1\x5d\x74\xcd\x59\x63\x18"
989                                 "\x11\x3d\x4f\xba\x75\xd4\x33\x4e\x23\x6b\x7b\x57\x44\xe1\xd3"
990                                 "\x03\x13\xa6\xf0\x8b\x60\xb0\x9e\xee\x75\x08\x9d\x71\x63\x13"
991                                 "\xcb\xa6\x81\x92\x14\x03\x22\x2d\xde\x55"),
992                                 .length = 128
993                         },
994                         .exponent = {
995                                 .data = (uint8_t *)("\x01\x00\x01"),
996                                 .length = 3
997                         }
998                 }
999     };
1000     /* Create asym crypto session and initialize it for the crypto device. */
1001     struct rte_cryptodev_asym_session *asym_session;
1002     asym_session = rte_cryptodev_asym_session_create(asym_session_pool);
1003     if (asym_session == NULL)
1004         rte_exit(EXIT_FAILURE, "Session could not be created\n");
1005
1006     if (rte_cryptodev_asym_session_init(cdev_id, asym_session,
1007                     &modex_xform, asym_session_pool) < 0)
1008         rte_exit(EXIT_FAILURE, "Session could not be initialized "
1009                     "for the crypto device\n");
1010
1011     /* Get a burst of crypto operations. */
1012     struct rte_crypto_op *crypto_ops[1];
1013     if (rte_crypto_op_bulk_alloc(crypto_op_pool,
1014                             RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
1015                             crypto_ops, 1) == 0)
1016         rte_exit(EXIT_FAILURE, "Not enough crypto operations available\n");
1017
1018     /* Set up the crypto operations. */
1019     struct rte_crypto_asym_op *asym_op = crypto_ops[0]->asym;
1020
1021         /* calculate mod exp of value 0xf8 */
1022     static unsigned char base[] = {0xF8};
1023     asym_op->modex.base.data = base;
1024     asym_op->modex.base.length = sizeof(base);
1025         asym_op->modex.base.iova = base;
1026
1027     /* Attach the asym crypto session to the operation */
1028     rte_crypto_op_attach_asym_session(op, asym_session);
1029
1030     /* Enqueue the crypto operations in the crypto device. */
1031     uint16_t num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, 0,
1032                                             crypto_ops, 1);
1033
1034     /*
1035      * Dequeue the crypto operations until all the operations
1036      * are processed in the crypto device.
1037      */
1038     uint16_t num_dequeued_ops, total_num_dequeued_ops = 0;
1039     do {
1040         struct rte_crypto_op *dequeued_ops[1];
1041         num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, 0,
1042                                         dequeued_ops, 1);
1043         total_num_dequeued_ops += num_dequeued_ops;
1044
1045         /* Check if operation was processed successfully */
1046         if (dequeued_ops[0]->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
1047                 rte_exit(EXIT_FAILURE,
1048                         "Some operations were not processed correctly");
1049
1050     } while (total_num_dequeued_ops < num_enqueued_ops);
1051
1052
1053 Asymmetric Crypto Device API
1054 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1055
1056 The cryptodev Library API is described in the
1057 `DPDK API Reference <http://doc.dpdk.org/api/>`_