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