1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2016-2017 Intel Corporation. 3 4L2 Forwarding with Crypto Sample Application 5============================================ 6 7The L2 Forwarding with Crypto (l2fwd-crypto) sample application 8is a simple example of packet processing 9using the Data Plane Development Kit (DPDK) 10in conjunction with the cryptodev library. 11 12Overview 13-------- 14 15The L2 Forwarding with Crypto sample application performs a crypto operation (cipher/hash) 16specified by the user from the command line (or using the default values), 17with a crypto device capable of doing that operation, 18for each packet that is received on an RX_PORT and performs L2 forwarding. 19 20The destination port is the adjacent port from the enabled portmask. 21If the first four ports are enabled (portmask 0xf), 22ports 0 and 1 forward into each other, and ports 2 and 3 forward into each other. 23If the MAC addresses updating is enabled, the MAC addresses are affected as follows: 24 25* The source MAC address is replaced by the TX_PORT MAC address 26 27* The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID 28 29Compiling the Application 30------------------------- 31 32To compile the sample application, see :doc:`compiling`. 33 34The application is located in the ``l2fwd-crypto`` sub-directory. 35 36Running the Application 37----------------------- 38 39The application requires a number of command line options: 40 41.. code-block:: console 42 43 ./<build_dir>/examples/dpdk-l2fwd-crypto [EAL options] -- [-p PORTMASK] [-q NQ] [-s] [-T PERIOD] / 44 [--cdev_type HW/SW/ANY] [--chain HASH_CIPHER/CIPHER_HASH/CIPHER_ONLY/HASH_ONLY/AEAD] / 45 [--cipher_algo ALGO] [--cipher_op ENCRYPT/DECRYPT] [--cipher_dataunit_len SIZE] / 46 [--cipher_key KEY] [--cipher_key_random_size SIZE] [--cipher_iv IV] / 47 [--cipher_iv_random_size SIZE] / 48 [--auth_algo ALGO] [--auth_op GENERATE/VERIFY] [--auth_key KEY] / 49 [--auth_key_random_size SIZE] [--auth_iv IV] [--auth_iv_random_size SIZE] / 50 [--aead_algo ALGO] [--aead_op ENCRYPT/DECRYPT] [--aead_key KEY] / 51 [--aead_key_random_size SIZE] [--aead_iv] [--aead_iv_random_size SIZE] / 52 [--aad AAD] [--aad_random_size SIZE] / 53 [--digest size SIZE] [--sessionless] [--cryptodev_mask MASK] / 54 [--mac-updating] [--no-mac-updating] 55 56where, 57 58* p PORTMASK: A hexadecimal bitmask of the ports to configure. (Default is all the ports.) 59 60* q NQ: Maximum number of queues per lcore (default is 1) 61 62* s: manage all ports from a single core. 63 64* T PERIOD: statistics will be refreshed each PERIOD seconds. 65 66 (0 to disable, 10 default, 86400 maximum.) 67 68* cdev_type: select preferred crypto device type: HW, SW or anything (ANY). 69 70 (Default is ANY.) 71 72* chain: select the operation chaining to perform: Cipher->Hash (CIPHER_HASH), 73 74 Hash->Cipher (HASH_CIPHER), Cipher (CIPHER_ONLY), Hash (HASH_ONLY) 75 76 or AEAD (AEAD). 77 78 (Default is Cipher->Hash.) 79 80* cipher_algo: select the ciphering algorithm. (Default is aes-cbc.) 81 82* cipher_op: select the ciphering operation to perform: ENCRYPT or DECRYPT. 83 84 (Default is ENCRYPT.) 85 86* cipher_dataunit_len: set the length of the cipher data-unit. 87 88* cipher_key: set the ciphering key to be used. Bytes have to be separated with ":". 89 90* cipher_key_random_size: set the size of the ciphering key, 91 92 which will be generated randomly. 93 94 Note that if --cipher_key is used, this will be ignored. 95 96* cipher_iv: set the cipher IV to be used. Bytes have to be separated with ":". 97 98* cipher_iv_random_size: set the size of the cipher IV, which will be generated randomly. 99 100 Note that if --cipher_iv is used, this will be ignored. 101 102* auth_algo: select the authentication algorithm. (Default is sha1-hmac.) 103 104* auth_op: select the authentication operation to perform: GENERATE or VERIFY. 105 106 (Default is GENERATE.) 107 108* auth_key: set the authentication key to be used. Bytes have to be separated with ":". 109 110* auth_key_random_size: set the size of the authentication key, 111 112 which will be generated randomly. 113 114 Note that if --auth_key is used, this will be ignored. 115 116* auth_iv: set the auth IV to be used. Bytes have to be separated with ":". 117 118* auth_iv_random_size: set the size of the auth IV, which will be generated randomly. 119 120 Note that if --auth_iv is used, this will be ignored. 121 122* aead_algo: select the AEAD algorithm. (Default is aes-gcm.) 123 124* aead_op: select the AEAD operation to perform: ENCRYPT or DECRYPT. 125 126 (Default is ENCRYPT.) 127 128* aead_key: set the AEAD key to be used. Bytes have to be separated with ":". 129 130* aead_key_random_size: set the size of the AEAD key, 131 132 which will be generated randomly. 133 134 Note that if --aead_key is used, this will be ignored. 135 136* aead_iv: set the AEAD IV to be used. Bytes have to be separated with ":". 137 138* aead_iv_random_size: set the size of the AEAD IV, which will be generated randomly. 139 140 Note that if --aead_iv is used, this will be ignored. 141 142* aad: set the AAD to be used. Bytes have to be separated with ":". 143 144* aad_random_size: set the size of the AAD, which will be generated randomly. 145 146 Note that if --aad is used, this will be ignored. 147 148* digest_size: set the size of the digest to be generated/verified. 149 150* sessionless: no crypto session will be created. 151 152* cryptodev_mask: A hexadecimal bitmask of the cryptodevs to be used by the 153 application. 154 155 (Default is all cryptodevs.) 156 157* [no-]mac-updating: Enable or disable MAC addresses updating. (Enabled by default.) 158 159 160The application requires that crypto devices capable of performing 161the specified crypto operation are available on application initialization. 162This means that HW crypto device/s must be bound to a DPDK driver or 163a SW crypto device/s (virtual crypto PMD) must be created (using --vdev). 164 165To run the application in Linux environment with 2 lcores, 2 ports and 2 crypto devices, issue the command: 166 167.. code-block:: console 168 169 $ ./<build_dir>/examples/dpdk-l2fwd-crypto -l 0-1 --vdev "crypto_aesni_mb0" \ 170 --vdev "crypto_aesni_mb1" -- -p 0x3 --chain CIPHER_HASH \ 171 --cipher_op ENCRYPT --cipher_algo aes-cbc \ 172 --cipher_key 00:01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f \ 173 --auth_op GENERATE --auth_algo aes-xcbc-mac \ 174 --auth_key 10:11:12:13:14:15:16:17:18:19:1a:1b:1c:1d:1e:1f 175 176Refer to the *DPDK Getting Started Guide* for general information on running applications 177and the Environment Abstraction Layer (EAL) options. 178 179.. Note:: 180 181 * The ``l2fwd-crypto`` sample application requires IPv4 packets for crypto operation. 182 183 * If multiple Ethernet ports are passed, then equal number of crypto devices are to be passed. 184 185 * All crypto devices shall use the same session. 186 187Explanation 188----------- 189 190The L2 forward with Crypto application demonstrates the performance of a crypto operation 191on a packet received on an RX PORT before forwarding it to a TX PORT. 192 193The following figure illustrates a sample flow of a packet in the application, 194from reception until transmission. 195 196.. _figure_l2_fwd_encrypt_flow: 197 198.. figure:: img/l2_fwd_encrypt_flow.* 199 200 Encryption flow through the L2 Forwarding with Crypto Application 201 202 203The following sections provide explanation of the application. 204 205Crypto operation specification 206~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 207 208All the packets received in all the ports get transformed by the crypto devices 209(ciphering and/or authentication). 210The crypto operation to be performed on the packet is parsed from the command line. 211(Go to "Running the Application" section for all the options.) 212 213If no parameter is passed, the default crypto operation is: 214 215* Encryption with AES-CBC with 128 bit key. 216 217* Authentication with SHA1-HMAC (generation). 218 219* Keys, IV and AAD are generated randomly. 220 221There are two methods to pass keys, IV and ADD from the command line: 222 223* Passing the full key, separated bytes by ":":: 224 225 --cipher_key 00:11:22:33:44 226 227* Passing the size, so key is generated randomly:: 228 229 --cipher_key_random_size 16 230 231**Note**: 232 If the full key is passed (first method) and the size is passed as well (second method), 233 the latter will be ignored. 234 235The size of these keys are checked (regardless of the method) before starting the app, 236to make sure that it is supported by the crypto devices. 237 238Crypto device initialization 239~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 240 241Once the encryption operation is defined, the crypto devices are initialized. 242The crypto devices must be either bound to a DPDK driver (if they are physical devices) 243or created using the EAL option --vdev (if they are virtual devices), 244when running the application. 245 246The initialize_cryptodevs() function performs the device initialization. 247It iterates through the list of the available crypto devices and 248checks which ones are capable of performing the operation. 249Each device has a set of capabilities associated with it, 250which are stored in the device info structure, so the function checks if the operation 251is within the structure of each device. 252 253The following code checks if the device supports the specified cipher algorithm 254(similar for the authentication algorithm): 255 256.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 257 :language: c 258 :start-after: Check if device supports cipher algo. 8< 259 :end-before: >8 End of check if device supports cipher algo. 260 :dedent: 2 261 262If a capable crypto device is found, key sizes are checked to see if they are supported 263(cipher key and IV for the ciphering): 264 265.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 266 :language: c 267 :start-after: Check if capable cipher is supported. 8< 268 :end-before: >8 End of checking if cipher is supported. 269 :dedent: 2 270 271After all the checks, the device is configured and it is added to the 272crypto device list. 273 274**Note**: 275 The number of crypto devices that supports the specified crypto operation 276 must be at least the number of ports to be used. 277 278Session creation 279~~~~~~~~~~~~~~~~ 280 281The crypto operation has a crypto session associated to it, which contains 282information such as the transform chain to perform (e.g. ciphering then hashing) 283pointers to the keys, lengths... etc. 284 285This session is created and is later attached to the crypto operation: 286 287.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 288 :language: c 289 :start-after: Session is created and is later attached to the crypto operation. 8< 290 :end-before: >8 End of creation of session. 291 292Crypto operation creation 293~~~~~~~~~~~~~~~~~~~~~~~~~ 294 295Given N packets received from an RX PORT, N crypto operations are allocated 296and filled: 297 298.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 299 :language: c 300 :start-after: Allocate and fillcrypto operations. 8< 301 :end-before: >8 End of crypto operation allocated and filled. 302 :dedent: 3 303 304After filling the crypto operation (including session attachment), 305the mbuf which will be transformed is attached to it:: 306 307 op->sym->m_src = m; 308 309Since no destination mbuf is set, the source mbuf will be overwritten 310after the operation is done (in-place). 311 312Crypto operation enqueuing/dequeuing 313~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 314 315Once the operation has been created, it has to be enqueued in one of the crypto devices. 316Before doing so, for performance reasons, the operation stays in a buffer. 317When the buffer has enough operations (MAX_PKT_BURST), they are enqueued in the device, 318which will perform the operation at that moment: 319 320.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 321 :language: c 322 :start-after: Crypto enqueue. 8< 323 :end-before: >8 End of crypto enqueue. 324 325.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 326 :language: c 327 :start-after: l2fwd_crypto_send_burst 8< 328 :end-before: >8 End of l2fwd_crypto_send_burst. 329 330After this, the operations are dequeued from the device, and the transformed mbuf 331is extracted from the operation. Then, the operation is freed and the mbuf is 332forwarded as it is done in the L2 forwarding application. 333 334.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 335 :language: c 336 :start-after: Dequeue packets from Crypto device. 8< 337 :end-before: >8 End of dequeue packets from crypto device. 338 :dedent: 3 339