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