Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef included_vnet_crypto_crypto_h |
| 17 | #define included_vnet_crypto_crypto_h |
| 18 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 19 | #include <vlib/vlib.h> |
| 20 | |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 21 | #define VNET_CRYPTO_FRAME_SIZE 64 |
Fan Zhang | e4db945 | 2021-03-30 17:31:38 +0100 | [diff] [blame] | 22 | #define VNET_CRYPTO_FRAME_POOL_SIZE 1024 |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 23 | |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 24 | /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 25 | #define foreach_crypto_cipher_alg \ |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 26 | _(DES_CBC, "des-cbc", 7) \ |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 27 | _(3DES_CBC, "3des-cbc", 24) \ |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 28 | _(AES_128_CBC, "aes-128-cbc", 16) \ |
| 29 | _(AES_192_CBC, "aes-192-cbc", 24) \ |
| 30 | _(AES_256_CBC, "aes-256-cbc", 32) \ |
| 31 | _(AES_128_CTR, "aes-128-ctr", 16) \ |
| 32 | _(AES_192_CTR, "aes-192-ctr", 24) \ |
| 33 | _(AES_256_CTR, "aes-256-ctr", 32) |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 34 | |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 35 | /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 36 | #define foreach_crypto_aead_alg \ |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 37 | _(AES_128_GCM, "aes-128-gcm", 16) \ |
| 38 | _(AES_192_GCM, "aes-192-gcm", 24) \ |
Artem Glazychev | 61f49aa | 2020-08-31 15:37:39 +0700 | [diff] [blame] | 39 | _(AES_256_GCM, "aes-256-gcm", 32) \ |
| 40 | _(CHACHA20_POLY1305, "chacha20-poly1305", 32) |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 41 | |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 42 | #define foreach_crypto_hash_alg \ |
| 43 | _ (SHA1, "sha-1") \ |
| 44 | _ (SHA224, "sha-224") \ |
| 45 | _ (SHA256, "sha-256") \ |
| 46 | _ (SHA384, "sha-384") \ |
| 47 | _ (SHA512, "sha-512") |
| 48 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 49 | #define foreach_crypto_hmac_alg \ |
Filip Tehlar | e225f71 | 2019-03-19 10:37:06 -0700 | [diff] [blame] | 50 | _(MD5, "md5") \ |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 51 | _(SHA1, "sha-1") \ |
| 52 | _(SHA224, "sha-224") \ |
| 53 | _(SHA256, "sha-256") \ |
| 54 | _(SHA384, "sha-384") \ |
| 55 | _(SHA512, "sha-512") |
| 56 | |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 57 | #define foreach_crypto_op_type \ |
| 58 | _ (ENCRYPT, "encrypt") \ |
| 59 | _ (DECRYPT, "decrypt") \ |
| 60 | _ (AEAD_ENCRYPT, "aead-encrypt") \ |
| 61 | _ (AEAD_DECRYPT, "aead-decrypt") \ |
| 62 | _ (HMAC, "hmac") \ |
| 63 | _ (HASH, "hash") |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 64 | |
| 65 | typedef enum |
| 66 | { |
| 67 | #define _(n, s) VNET_CRYPTO_OP_TYPE_##n, |
| 68 | foreach_crypto_op_type |
| 69 | #undef _ |
| 70 | VNET_CRYPTO_OP_N_TYPES, |
| 71 | } vnet_crypto_op_type_t; |
| 72 | |
| 73 | #define foreach_crypto_op_status \ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 74 | _(IDLE, "idle") \ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 75 | _(PENDING, "pending") \ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 76 | _(WORK_IN_PROGRESS, "work-in-progress") \ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 77 | _(COMPLETED, "completed") \ |
| 78 | _(FAIL_NO_HANDLER, "no-handler") \ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 79 | _(FAIL_BAD_HMAC, "bad-hmac") \ |
| 80 | _(FAIL_ENGINE_ERR, "engine-error") |
| 81 | |
| 82 | /** async crypto **/ |
| 83 | |
| 84 | /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, TAG_LEN, AAD_LEN */ |
Gabriel Oginski | 492d779 | 2021-11-10 07:59:56 +0000 | [diff] [blame] | 85 | #define foreach_crypto_aead_async_alg \ |
| 86 | _ (AES_128_GCM, "aes-128-gcm-aad8", 16, 16, 8) \ |
| 87 | _ (AES_128_GCM, "aes-128-gcm-aad12", 16, 16, 12) \ |
| 88 | _ (AES_192_GCM, "aes-192-gcm-aad8", 24, 16, 8) \ |
| 89 | _ (AES_192_GCM, "aes-192-gcm-aad12", 24, 16, 12) \ |
| 90 | _ (AES_256_GCM, "aes-256-gcm-aad8", 32, 16, 8) \ |
| 91 | _ (AES_256_GCM, "aes-256-gcm-aad12", 32, 16, 12) \ |
| 92 | _ (CHACHA20_POLY1305, "chacha20-poly1305-aad8", 32, 16, 8) \ |
| 93 | _ (CHACHA20_POLY1305, "chacha20-poly1305-aad12", 32, 16, 12) \ |
| 94 | _ (CHACHA20_POLY1305, "chacha20-poly1305", 32, 16, 0) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 95 | |
| 96 | /* CRYPTO_ID, INTEG_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, DIGEST_LEN */ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 97 | #define foreach_crypto_link_async_alg \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 98 | _ (3DES_CBC, MD5, "3des-cbc-hmac-md5", 24, 12) \ |
| 99 | _ (AES_128_CBC, MD5, "aes-128-cbc-hmac-md5", 16, 12) \ |
| 100 | _ (AES_192_CBC, MD5, "aes-192-cbc-hmac-md5", 24, 12) \ |
| 101 | _ (AES_256_CBC, MD5, "aes-256-cbc-hmac-md5", 32, 12) \ |
| 102 | _ (3DES_CBC, SHA1, "3des-cbc-hmac-sha-1", 24, 12) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 103 | _ (AES_128_CBC, SHA1, "aes-128-cbc-hmac-sha-1", 16, 12) \ |
| 104 | _ (AES_192_CBC, SHA1, "aes-192-cbc-hmac-sha-1", 24, 12) \ |
| 105 | _ (AES_256_CBC, SHA1, "aes-256-cbc-hmac-sha-1", 32, 12) \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 106 | _ (3DES_CBC, SHA224, "3des-cbc-hmac-sha-224", 24, 14) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 107 | _ (AES_128_CBC, SHA224, "aes-128-cbc-hmac-sha-224", 16, 14) \ |
| 108 | _ (AES_192_CBC, SHA224, "aes-192-cbc-hmac-sha-224", 24, 14) \ |
| 109 | _ (AES_256_CBC, SHA224, "aes-256-cbc-hmac-sha-224", 32, 14) \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 110 | _ (3DES_CBC, SHA256, "3des-cbc-hmac-sha-256", 24, 16) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 111 | _ (AES_128_CBC, SHA256, "aes-128-cbc-hmac-sha-256", 16, 16) \ |
| 112 | _ (AES_192_CBC, SHA256, "aes-192-cbc-hmac-sha-256", 24, 16) \ |
| 113 | _ (AES_256_CBC, SHA256, "aes-256-cbc-hmac-sha-256", 32, 16) \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 114 | _ (3DES_CBC, SHA384, "3des-cbc-hmac-sha-384", 24, 24) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 115 | _ (AES_128_CBC, SHA384, "aes-128-cbc-hmac-sha-384", 16, 24) \ |
| 116 | _ (AES_192_CBC, SHA384, "aes-192-cbc-hmac-sha-384", 24, 24) \ |
| 117 | _ (AES_256_CBC, SHA384, "aes-256-cbc-hmac-sha-384", 32, 24) \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 118 | _ (3DES_CBC, SHA512, "3des-cbc-hmac-sha-512", 24, 32) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 119 | _ (AES_128_CBC, SHA512, "aes-128-cbc-hmac-sha-512", 16, 32) \ |
| 120 | _ (AES_192_CBC, SHA512, "aes-192-cbc-hmac-sha-512", 24, 32) \ |
| 121 | _ (AES_256_CBC, SHA512, "aes-256-cbc-hmac-sha-512", 32, 32) \ |
| 122 | _ (AES_128_CTR, SHA1, "aes-128-ctr-hmac-sha-1", 16, 12) \ |
| 123 | _ (AES_192_CTR, SHA1, "aes-192-ctr-hmac-sha-1", 24, 12) \ |
| 124 | _ (AES_256_CTR, SHA1, "aes-256-ctr-hmac-sha-1", 32, 12) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 125 | |
| 126 | #define foreach_crypto_async_op_type \ |
| 127 | _(ENCRYPT, "async-encrypt") \ |
| 128 | _(DECRYPT, "async-decrypt") |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 129 | |
| 130 | typedef enum |
| 131 | { |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 132 | VNET_CRYPTO_KEY_OP_ADD, |
| 133 | VNET_CRYPTO_KEY_OP_DEL, |
| 134 | VNET_CRYPTO_KEY_OP_MODIFY, |
| 135 | } vnet_crypto_key_op_t; |
| 136 | |
| 137 | typedef enum |
| 138 | { |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 139 | #define _(n, s) VNET_CRYPTO_OP_STATUS_##n, |
| 140 | foreach_crypto_op_status |
| 141 | #undef _ |
| 142 | VNET_CRYPTO_OP_N_STATUS, |
| 143 | } vnet_crypto_op_status_t; |
| 144 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 145 | /* *INDENT-OFF* */ |
| 146 | typedef enum |
| 147 | { |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 148 | VNET_CRYPTO_ALG_NONE = 0, |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 149 | #define _(n, s, l) VNET_CRYPTO_ALG_##n, |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 150 | foreach_crypto_cipher_alg foreach_crypto_aead_alg |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 151 | #undef _ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 152 | #define _(n, s) VNET_CRYPTO_ALG_HMAC_##n, |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 153 | foreach_crypto_hmac_alg |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 154 | #undef _ |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 155 | #define _(n, s) VNET_CRYPTO_ALG_HASH_##n, |
| 156 | foreach_crypto_hash_alg |
| 157 | #undef _ |
| 158 | VNET_CRYPTO_N_ALGS, |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 159 | } vnet_crypto_alg_t; |
| 160 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 161 | typedef enum |
| 162 | { |
| 163 | #define _(n, s) VNET_CRYPTO_ASYNC_OP_TYPE_##n, |
| 164 | foreach_crypto_async_op_type |
| 165 | #undef _ |
| 166 | VNET_CRYPTO_ASYNC_OP_N_TYPES, |
| 167 | } vnet_crypto_async_op_type_t; |
| 168 | |
| 169 | typedef enum |
| 170 | { |
| 171 | VNET_CRYPTO_ASYNC_ALG_NONE = 0, |
| 172 | #define _(n, s, k, t, a) \ |
| 173 | VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a, |
| 174 | foreach_crypto_aead_async_alg |
| 175 | #undef _ |
| 176 | #define _(c, h, s, k ,d) \ |
| 177 | VNET_CRYPTO_ALG_##c##_##h##_TAG##d, |
| 178 | foreach_crypto_link_async_alg |
| 179 | #undef _ |
| 180 | VNET_CRYPTO_N_ASYNC_ALGS, |
| 181 | } vnet_crypto_async_alg_t; |
| 182 | |
| 183 | typedef enum |
| 184 | { |
| 185 | VNET_CRYPTO_ASYNC_OP_NONE = 0, |
| 186 | #define _(n, s, k, t, a) \ |
| 187 | VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \ |
| 188 | VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC, |
| 189 | foreach_crypto_aead_async_alg |
| 190 | #undef _ |
| 191 | #define _(c, h, s, k ,d) \ |
| 192 | VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \ |
| 193 | VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC, |
| 194 | foreach_crypto_link_async_alg |
| 195 | #undef _ |
| 196 | VNET_CRYPTO_ASYNC_OP_N_IDS, |
| 197 | } vnet_crypto_async_op_id_t; |
| 198 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 199 | typedef struct |
| 200 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 201 | union |
| 202 | { |
| 203 | struct |
| 204 | { |
| 205 | u8 *data; |
| 206 | vnet_crypto_alg_t alg:8; |
| 207 | }; |
| 208 | struct |
| 209 | { |
| 210 | u32 index_crypto; |
| 211 | u32 index_integ; |
| 212 | vnet_crypto_async_alg_t async_alg:8; |
| 213 | }; |
| 214 | }; |
| 215 | #define VNET_CRYPTO_KEY_TYPE_DATA 0 |
| 216 | #define VNET_CRYPTO_KEY_TYPE_LINK 1 |
| 217 | u8 type; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 218 | } vnet_crypto_key_t; |
| 219 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 220 | typedef enum |
| 221 | { |
| 222 | VNET_CRYPTO_OP_NONE = 0, |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 223 | #define _(n, s, l) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC, |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 224 | foreach_crypto_cipher_alg foreach_crypto_aead_alg |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 225 | #undef _ |
| 226 | #define _(n, s) VNET_CRYPTO_OP_##n##_HMAC, |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 227 | foreach_crypto_hmac_alg |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 228 | #undef _ |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 229 | #define _(n, s) VNET_CRYPTO_OP_##n##_HASH, |
| 230 | foreach_crypto_hash_alg |
| 231 | #undef _ |
| 232 | VNET_CRYPTO_N_OP_IDS, |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 233 | } vnet_crypto_op_id_t; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 234 | /* *INDENT-ON* */ |
| 235 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 236 | typedef enum |
| 237 | { |
| 238 | CRYPTO_OP_SIMPLE, |
| 239 | CRYPTO_OP_CHAINED, |
| 240 | CRYPTO_OP_BOTH, |
| 241 | } crypto_op_class_type_t; |
| 242 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 243 | typedef struct |
| 244 | { |
| 245 | char *name; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 246 | vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES]; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 247 | } vnet_crypto_alg_data_t; |
| 248 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 249 | typedef struct |
| 250 | { |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 251 | u8 *src; |
| 252 | u8 *dst; |
| 253 | u32 len; |
| 254 | } vnet_crypto_op_chunk_t; |
| 255 | |
| 256 | typedef struct |
| 257 | { |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 258 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 259 | uword user_data; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 260 | vnet_crypto_op_id_t op:16; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 261 | vnet_crypto_op_status_t status:8; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 262 | u8 flags; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 263 | #define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0) |
| 264 | #define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1) |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 265 | #define VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS (1 << 2) |
| 266 | |
| 267 | union |
| 268 | { |
| 269 | u8 digest_len; |
| 270 | u8 tag_len; |
| 271 | }; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 272 | u16 aad_len; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 273 | |
| 274 | union |
| 275 | { |
| 276 | struct |
| 277 | { |
| 278 | u8 *src; |
| 279 | u8 *dst; |
| 280 | }; |
| 281 | |
| 282 | /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */ |
| 283 | u16 n_chunks; |
| 284 | }; |
| 285 | |
| 286 | union |
| 287 | { |
| 288 | u32 len; |
| 289 | /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */ |
| 290 | u32 chunk_index; |
| 291 | }; |
| 292 | |
| 293 | u32 key_index; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 294 | u8 *iv; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 295 | u8 *aad; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 296 | |
| 297 | union |
| 298 | { |
| 299 | u8 *tag; |
| 300 | u8 *digest; |
| 301 | }; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 302 | } vnet_crypto_op_t; |
| 303 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 304 | STATIC_ASSERT_SIZEOF (vnet_crypto_op_t, CLIB_CACHE_LINE_BYTES); |
| 305 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 306 | typedef struct |
| 307 | { |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 308 | vnet_crypto_op_type_t type; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 309 | vnet_crypto_alg_t alg; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 310 | u32 active_engine_index_simple; |
| 311 | u32 active_engine_index_chained; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 312 | } vnet_crypto_op_data_t; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 313 | |
| 314 | typedef struct |
| 315 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 316 | vnet_crypto_async_op_type_t type; |
| 317 | vnet_crypto_async_alg_t alg; |
| 318 | u32 active_engine_index_async; |
| 319 | } vnet_crypto_async_op_data_t; |
| 320 | |
| 321 | typedef struct |
| 322 | { |
| 323 | char *name; |
| 324 | vnet_crypto_async_op_id_t op_by_type[VNET_CRYPTO_ASYNC_OP_N_TYPES]; |
| 325 | } vnet_crypto_async_alg_data_t; |
| 326 | |
| 327 | typedef struct |
| 328 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 329 | u8 *iv; |
| 330 | union |
| 331 | { |
| 332 | u8 *digest; |
| 333 | u8 *tag; |
| 334 | }; |
| 335 | u8 *aad; |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 336 | u32 key_index; |
| 337 | u32 crypto_total_length; |
| 338 | i16 crypto_start_offset; /* first buffer offset */ |
| 339 | i16 integ_start_offset; |
| 340 | /* adj total_length for integ, e.g.4 bytes for IPSec ESN */ |
| 341 | u16 integ_length_adj; |
| 342 | vnet_crypto_op_status_t status : 8; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 343 | u8 flags; /**< share same VNET_CRYPTO_OP_FLAG_* values */ |
| 344 | } vnet_crypto_async_frame_elt_t; |
| 345 | |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 346 | /* Assert the size so the compiler will warn us when it changes */ |
| 347 | STATIC_ASSERT_SIZEOF (vnet_crypto_async_frame_elt_t, 5 * sizeof (u64)); |
| 348 | |
| 349 | typedef enum vnet_crypto_async_frame_state_t_ |
| 350 | { |
| 351 | VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED, |
| 352 | /* frame waiting to be processed */ |
| 353 | VNET_CRYPTO_FRAME_STATE_PENDING, |
| 354 | VNET_CRYPTO_FRAME_STATE_WORK_IN_PROGRESS, |
| 355 | VNET_CRYPTO_FRAME_STATE_SUCCESS, |
| 356 | VNET_CRYPTO_FRAME_STATE_ELT_ERROR |
| 357 | } __clib_packed vnet_crypto_async_frame_state_t; |
| 358 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 359 | typedef struct |
| 360 | { |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 361 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 362 | vnet_crypto_async_frame_state_t state; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 363 | vnet_crypto_async_op_id_t op:8; |
| 364 | u16 n_elts; |
| 365 | vnet_crypto_async_frame_elt_t elts[VNET_CRYPTO_FRAME_SIZE]; |
| 366 | u32 buffer_indices[VNET_CRYPTO_FRAME_SIZE]; |
| 367 | u16 next_node_index[VNET_CRYPTO_FRAME_SIZE]; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 368 | u32 enqueue_thread_index; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 369 | } vnet_crypto_async_frame_t; |
| 370 | |
| 371 | typedef struct |
| 372 | { |
| 373 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 374 | vnet_crypto_async_frame_t *frame_pool; |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 375 | u32 *buffer_indices; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 376 | u16 *nexts; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 377 | } vnet_crypto_thread_t; |
| 378 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 379 | typedef u32 vnet_crypto_key_index_t; |
| 380 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 381 | typedef u32 (vnet_crypto_chained_ops_handler_t) (vlib_main_t * vm, |
| 382 | vnet_crypto_op_t * ops[], |
| 383 | vnet_crypto_op_chunk_t * |
| 384 | chunks, u32 n_ops); |
| 385 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 386 | typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm, |
| 387 | vnet_crypto_op_t * ops[], u32 n_ops); |
| 388 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 389 | typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm, |
| 390 | vnet_crypto_key_op_t kop, |
| 391 | vnet_crypto_key_index_t idx); |
| 392 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 393 | /** async crypto function handlers **/ |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 394 | typedef int |
| 395 | (vnet_crypto_frame_enqueue_t) (vlib_main_t * vm, |
| 396 | vnet_crypto_async_frame_t * frame); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 397 | typedef vnet_crypto_async_frame_t * |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 398 | (vnet_crypto_frame_dequeue_t) (vlib_main_t * vm, u32 * nb_elts_processed, |
| 399 | u32 * enqueue_thread_idx); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 400 | |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 401 | u32 |
| 402 | vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, |
| 403 | char *desc); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 404 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 405 | void vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index, |
| 406 | vnet_crypto_op_id_t opt, |
| 407 | vnet_crypto_ops_handler_t * oph); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 408 | |
| 409 | void vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, |
| 410 | u32 engine_index, |
| 411 | vnet_crypto_op_id_t opt, |
| 412 | vnet_crypto_chained_ops_handler_t |
| 413 | * oph); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 414 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 415 | void vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index, |
| 416 | vnet_crypto_op_id_t opt, |
| 417 | vnet_crypto_ops_handler_t * fn, |
| 418 | vnet_crypto_chained_ops_handler_t * |
| 419 | cfn); |
| 420 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 421 | void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index, |
| 422 | vnet_crypto_key_handler_t * keyh); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 423 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 424 | /** async crypto register functions */ |
| 425 | u32 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name); |
Jakub Wysocki | 83b2bb8 | 2021-11-30 10:53:03 +0000 | [diff] [blame] | 426 | |
| 427 | void |
| 428 | vnet_crypto_register_enqueue_handler (vlib_main_t *vm, u32 engine_index, |
| 429 | vnet_crypto_async_op_id_t opt, |
| 430 | vnet_crypto_frame_enqueue_t *enq_fn); |
| 431 | |
| 432 | void |
| 433 | vnet_crypto_register_dequeue_handler (vlib_main_t *vm, u32 engine_index, |
| 434 | vnet_crypto_frame_dequeue_t *deq_fn); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 435 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 436 | typedef struct |
| 437 | { |
| 438 | char *name; |
| 439 | char *desc; |
| 440 | int priority; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 441 | vnet_crypto_key_handler_t *key_op_handler; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 442 | vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS]; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 443 | vnet_crypto_chained_ops_handler_t |
| 444 | * chained_ops_handlers[VNET_CRYPTO_N_OP_IDS]; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 445 | vnet_crypto_frame_enqueue_t *enqueue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS]; |
Jakub Wysocki | 83b2bb8 | 2021-11-30 10:53:03 +0000 | [diff] [blame] | 446 | vnet_crypto_frame_dequeue_t *dequeue_handler; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 447 | } vnet_crypto_engine_t; |
| 448 | |
| 449 | typedef struct |
| 450 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 451 | u32 node_idx; |
| 452 | u32 next_idx; |
| 453 | } vnet_crypto_async_next_node_t; |
| 454 | |
| 455 | typedef struct |
| 456 | { |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 457 | vnet_crypto_alg_data_t *algs; |
| 458 | vnet_crypto_thread_t *threads; |
| 459 | vnet_crypto_ops_handler_t **ops_handlers; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 460 | vnet_crypto_chained_ops_handler_t **chained_ops_handlers; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 461 | vnet_crypto_frame_enqueue_t **enqueue_handlers; |
| 462 | vnet_crypto_frame_dequeue_t **dequeue_handlers; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 463 | vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS]; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 464 | vnet_crypto_async_op_data_t async_opt_data[VNET_CRYPTO_ASYNC_OP_N_IDS]; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 465 | vnet_crypto_engine_t *engines; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 466 | vnet_crypto_key_t *keys; |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 467 | uword *engine_index_by_name; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 468 | uword *alg_index_by_name; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 469 | uword *async_alg_index_by_name; |
| 470 | vnet_crypto_async_alg_data_t *async_algs; |
| 471 | u32 async_refcnt; |
| 472 | vnet_crypto_async_next_node_t *next_nodes; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 473 | u32 crypto_node_index; |
| 474 | #define VNET_CRYPTO_ASYNC_DISPATCH_POLLING 0 |
| 475 | #define VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT 1 |
| 476 | u8 dispatch_mode; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 477 | } vnet_crypto_main_t; |
| 478 | |
| 479 | extern vnet_crypto_main_t crypto_main; |
| 480 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 481 | u32 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], |
| 482 | vnet_crypto_op_chunk_t * chunks, |
| 483 | u32 n_ops); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 484 | u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], |
| 485 | u32 n_ops); |
| 486 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 487 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 488 | int vnet_crypto_set_handler2 (char *ops_handler_name, char *engine, |
| 489 | crypto_op_class_type_t oct); |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 490 | int vnet_crypto_is_set_handler (vnet_crypto_alg_t alg); |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 491 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 492 | u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, |
| 493 | u8 * data, u16 length); |
| 494 | void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index); |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 495 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 496 | /** |
| 497 | * Use 2 created keys to generate new key for linked algs (cipher + integ) |
| 498 | * The returned key index is to be used for linked alg only. |
| 499 | **/ |
| 500 | u32 vnet_crypto_key_add_linked (vlib_main_t * vm, |
| 501 | vnet_crypto_key_index_t index_crypto, |
| 502 | vnet_crypto_key_index_t index_integ); |
| 503 | |
| 504 | clib_error_t *crypto_dispatch_enable_disable (int is_enable); |
| 505 | |
| 506 | int vnet_crypto_set_async_handler2 (char *alg_name, char *engine); |
| 507 | |
| 508 | int vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t opt); |
| 509 | |
| 510 | void vnet_crypto_request_async_mode (int is_enable); |
| 511 | |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 512 | void vnet_crypto_set_async_dispatch_mode (u8 mode); |
| 513 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 514 | vnet_crypto_async_alg_t vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg, |
| 515 | vnet_crypto_alg_t integ_alg); |
| 516 | |
| 517 | clib_error_t *crypto_dispatch_enable_disable (int is_enable); |
| 518 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 519 | format_function_t format_vnet_crypto_alg; |
| 520 | format_function_t format_vnet_crypto_engine; |
| 521 | format_function_t format_vnet_crypto_op; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 522 | format_function_t format_vnet_crypto_op_type; |
| 523 | format_function_t format_vnet_crypto_op_status; |
Damjan Marion | 61c0a3d | 2019-04-05 19:42:21 +0200 | [diff] [blame] | 524 | unformat_function_t unformat_vnet_crypto_alg; |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame] | 525 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 526 | format_function_t format_vnet_crypto_async_op; |
| 527 | format_function_t format_vnet_crypto_async_alg; |
| 528 | format_function_t format_vnet_crypto_async_op_type; |
| 529 | |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame] | 530 | static_always_inline void |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 531 | vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type) |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame] | 532 | { |
| 533 | if (CLIB_DEBUG > 0) |
| 534 | clib_memset (op, 0xfe, sizeof (*op)); |
| 535 | op->op = type; |
| 536 | op->flags = 0; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 537 | op->key_index = ~0; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 538 | op->n_chunks = 0; |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 541 | static_always_inline vnet_crypto_op_type_t |
| 542 | vnet_crypto_get_op_type (vnet_crypto_op_id_t id) |
| 543 | { |
| 544 | vnet_crypto_main_t *cm = &crypto_main; |
Lijian Zhang | b15d796 | 2019-09-27 16:25:35 +0800 | [diff] [blame] | 545 | ASSERT (id < VNET_CRYPTO_N_OP_IDS); |
| 546 | vnet_crypto_op_data_t *od = cm->opt_data + id; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 547 | return od->type; |
| 548 | } |
| 549 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 550 | static_always_inline vnet_crypto_key_t * |
| 551 | vnet_crypto_get_key (vnet_crypto_key_index_t index) |
| 552 | { |
| 553 | vnet_crypto_main_t *cm = &crypto_main; |
| 554 | return vec_elt_at_index (cm->keys, index); |
| 555 | } |
| 556 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 557 | static_always_inline int |
| 558 | vnet_crypto_set_handler (char *alg_name, char *engine) |
| 559 | { |
| 560 | return vnet_crypto_set_handler2 (alg_name, engine, CRYPTO_OP_BOTH); |
| 561 | } |
| 562 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 563 | /** async crypto inline functions **/ |
| 564 | |
| 565 | static_always_inline vnet_crypto_async_frame_t * |
| 566 | vnet_crypto_async_get_frame (vlib_main_t * vm, vnet_crypto_async_op_id_t opt) |
| 567 | { |
| 568 | vnet_crypto_main_t *cm = &crypto_main; |
| 569 | vnet_crypto_thread_t *ct = cm->threads + vm->thread_index; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 570 | vnet_crypto_async_frame_t *f = NULL; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 571 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 572 | pool_get_aligned (ct->frame_pool, f, CLIB_CACHE_LINE_BYTES); |
| 573 | if (CLIB_DEBUG > 0) |
| 574 | clib_memset (f, 0xfe, sizeof (*f)); |
| 575 | f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED; |
| 576 | f->op = opt; |
| 577 | f->n_elts = 0; |
| 578 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 579 | return f; |
| 580 | } |
| 581 | |
| 582 | static_always_inline void |
| 583 | vnet_crypto_async_free_frame (vlib_main_t * vm, |
| 584 | vnet_crypto_async_frame_t * frame) |
| 585 | { |
| 586 | vnet_crypto_main_t *cm = &crypto_main; |
| 587 | vnet_crypto_thread_t *ct = cm->threads + vm->thread_index; |
| 588 | pool_put (ct->frame_pool, frame); |
| 589 | } |
| 590 | |
| 591 | static_always_inline int |
| 592 | vnet_crypto_async_submit_open_frame (vlib_main_t * vm, |
| 593 | vnet_crypto_async_frame_t * frame) |
| 594 | { |
| 595 | vnet_crypto_main_t *cm = &crypto_main; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 596 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 597 | u32 i = vlib_num_workers () > 0; |
| 598 | |
PiotrX Kleski | ef69b51 | 2020-11-24 08:26:26 +0000 | [diff] [blame] | 599 | frame->state = VNET_CRYPTO_FRAME_STATE_PENDING; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 600 | frame->enqueue_thread_index = vm->thread_index; |
PiotrX Kleski | ef69b51 | 2020-11-24 08:26:26 +0000 | [diff] [blame] | 601 | |
Dastin Wilski | 8a4a7c2 | 2022-03-31 11:55:09 +0200 | [diff] [blame] | 602 | if (PREDICT_FALSE (cm->enqueue_handlers == NULL)) |
| 603 | { |
| 604 | frame->state = VNET_CRYPTO_FRAME_STATE_ELT_ERROR; |
| 605 | return -1; |
| 606 | } |
| 607 | |
PiotrX Kleski | ef69b51 | 2020-11-24 08:26:26 +0000 | [diff] [blame] | 608 | int ret = (cm->enqueue_handlers[frame->op]) (vm, frame); |
| 609 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 610 | if (PREDICT_TRUE (ret == 0)) |
| 611 | { |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 612 | if (cm->dispatch_mode == VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT) |
| 613 | { |
| 614 | for (; i < tm->n_vlib_mains; i++) |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 615 | vlib_node_set_interrupt_pending (vlib_get_main_by_index (i), |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 616 | cm->crypto_node_index); |
| 617 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 618 | } |
PiotrX Kleski | ef69b51 | 2020-11-24 08:26:26 +0000 | [diff] [blame] | 619 | else |
| 620 | { |
| 621 | frame->state = VNET_CRYPTO_FRAME_STATE_ELT_ERROR; |
| 622 | } |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 623 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 624 | return ret; |
| 625 | } |
| 626 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 627 | static_always_inline void |
| 628 | vnet_crypto_async_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f, |
| 629 | u32 key_index, u32 crypto_len, |
| 630 | i16 integ_len_adj, i16 crypto_start_offset, |
| 631 | u16 integ_start_offset, u32 buffer_index, |
| 632 | u16 next_node, u8 *iv, u8 *tag, u8 *aad, |
| 633 | u8 flags) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 634 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 635 | vnet_crypto_async_frame_elt_t *fe; |
| 636 | u16 index; |
| 637 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 638 | ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 639 | |
| 640 | index = f->n_elts; |
| 641 | fe = &f->elts[index]; |
| 642 | f->n_elts++; |
| 643 | fe->key_index = key_index; |
| 644 | fe->crypto_total_length = crypto_len; |
| 645 | fe->crypto_start_offset = crypto_start_offset; |
| 646 | fe->integ_start_offset = integ_start_offset; |
| 647 | fe->integ_length_adj = integ_len_adj; |
| 648 | fe->iv = iv; |
| 649 | fe->tag = tag; |
| 650 | fe->aad = aad; |
| 651 | fe->flags = flags; |
| 652 | f->buffer_indices[index] = buffer_index; |
| 653 | f->next_node_index[index] = next_node; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static_always_inline void |
| 657 | vnet_crypto_async_reset_frame (vnet_crypto_async_frame_t * f) |
| 658 | { |
| 659 | vnet_crypto_async_op_id_t opt; |
| 660 | ASSERT (f != 0); |
PiotrX Kleski | 8ed06c0 | 2020-11-24 07:34:09 +0000 | [diff] [blame] | 661 | ASSERT ((f->state == VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED |
| 662 | || f->state == VNET_CRYPTO_FRAME_STATE_ELT_ERROR)); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 663 | opt = f->op; |
| 664 | if (CLIB_DEBUG > 0) |
| 665 | clib_memset (f, 0xfe, sizeof (*f)); |
| 666 | f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED; |
| 667 | f->op = opt; |
| 668 | f->n_elts = 0; |
| 669 | } |
| 670 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 671 | static_always_inline u8 |
| 672 | vnet_crypto_async_frame_is_full (const vnet_crypto_async_frame_t *f) |
| 673 | { |
| 674 | return (f->n_elts == VNET_CRYPTO_FRAME_SIZE); |
| 675 | } |
| 676 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 677 | #endif /* included_vnet_crypto_crypto_h */ |
| 678 | |
| 679 | /* |
| 680 | * fd.io coding-style-patch-verification: ON |
| 681 | * |
| 682 | * Local Variables: |
| 683 | * eval: (c-set-style "gnu") |
| 684 | * End: |
| 685 | */ |