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