dpdk: code preparation for bumping to DPDK 22.11
[vpp.git] / src / plugins / dpdk / cryptodev / cryptodev.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2019 - 2021 Intel and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 #ifndef included_cryptodev_h
18 #define included_cryptodev_h
19
20 #include <vnet/crypto/crypto.h>
21 #undef always_inline
22 #include <rte_cryptodev.h>
23
24 #define CRYPTODEV_NB_CRYPTO_OPS    1024
25 #define CRYPTODEV_CACHE_QUEUE_SIZE VNET_CRYPTO_FRAME_POOL_SIZE
26 #define CRYPTODEV_CACHE_QUEUE_MASK (VNET_CRYPTO_FRAME_POOL_SIZE - 1)
27 #define CRYPTODEV_MAX_INFLIGHT     (CRYPTODEV_NB_CRYPTO_OPS - 1)
28 #define CRYPTODEV_AAD_MASK         (CRYPTODEV_NB_CRYPTO_OPS - 1)
29 #define CRYPTODEV_DEQ_CACHE_SZ     32
30 #define CRYPTODEV_NB_SESSION       4096
31 #define CRYPTODEV_MAX_IV_SIZE      16
32 #define CRYPTODEV_MAX_AAD_SIZE     16
33 #define CRYPTODEV_MAX_N_SGL        8 /**< maximum number of segments */
34
35 #define CRYPTODEV_IV_OFFSET  (offsetof (cryptodev_op_t, iv))
36 #define CRYPTODEV_AAD_OFFSET (offsetof (cryptodev_op_t, aad))
37
38 /* VNET_CRYPTO_ALGO, TYPE, DPDK_CRYPTO_ALGO, IV_LEN, TAG_LEN, AAD_LEN, KEY_LEN
39  */
40 #define foreach_vnet_aead_crypto_conversion                                   \
41   _ (AES_128_GCM, AEAD, AES_GCM, 12, 16, 8, 16)                               \
42   _ (AES_128_GCM, AEAD, AES_GCM, 12, 16, 12, 16)                              \
43   _ (AES_192_GCM, AEAD, AES_GCM, 12, 16, 8, 24)                               \
44   _ (AES_192_GCM, AEAD, AES_GCM, 12, 16, 12, 24)                              \
45   _ (AES_256_GCM, AEAD, AES_GCM, 12, 16, 8, 32)                               \
46   _ (AES_256_GCM, AEAD, AES_GCM, 12, 16, 12, 32)                              \
47   _ (CHACHA20_POLY1305, AEAD, CHACHA20_POLY1305, 12, 16, 0, 32)               \
48   _ (CHACHA20_POLY1305, AEAD, CHACHA20_POLY1305, 12, 16, 8, 32)               \
49   _ (CHACHA20_POLY1305, AEAD, CHACHA20_POLY1305, 12, 16, 12, 32)
50
51 /**
52  * crypto (alg, cryptodev_alg, key_size), hash (alg, digest-size)
53  **/
54 #define foreach_cryptodev_link_async_alg                                      \
55   _ (AES_128_CBC, AES_CBC, 16, MD5, 12)                                       \
56   _ (AES_192_CBC, AES_CBC, 24, MD5, 12)                                       \
57   _ (AES_256_CBC, AES_CBC, 32, MD5, 12)                                       \
58   _ (AES_128_CBC, AES_CBC, 16, SHA1, 12)                                      \
59   _ (AES_192_CBC, AES_CBC, 24, SHA1, 12)                                      \
60   _ (AES_256_CBC, AES_CBC, 32, SHA1, 12)                                      \
61   _ (AES_128_CBC, AES_CBC, 16, SHA224, 14)                                    \
62   _ (AES_192_CBC, AES_CBC, 24, SHA224, 14)                                    \
63   _ (AES_256_CBC, AES_CBC, 32, SHA224, 14)                                    \
64   _ (AES_128_CBC, AES_CBC, 16, SHA256, 16)                                    \
65   _ (AES_192_CBC, AES_CBC, 24, SHA256, 16)                                    \
66   _ (AES_256_CBC, AES_CBC, 32, SHA256, 16)                                    \
67   _ (AES_128_CBC, AES_CBC, 16, SHA384, 24)                                    \
68   _ (AES_192_CBC, AES_CBC, 24, SHA384, 24)                                    \
69   _ (AES_256_CBC, AES_CBC, 32, SHA384, 24)                                    \
70   _ (AES_128_CBC, AES_CBC, 16, SHA512, 32)                                    \
71   _ (AES_192_CBC, AES_CBC, 24, SHA512, 32)                                    \
72   _ (AES_256_CBC, AES_CBC, 32, SHA512, 32)                                    \
73   _ (AES_128_CTR, AES_CTR, 16, SHA1, 12)                                      \
74   _ (AES_192_CTR, AES_CTR, 24, SHA1, 12)                                      \
75   _ (AES_256_CTR, AES_CTR, 32, SHA1, 12)
76
77 typedef enum
78 {
79   CRYPTODEV_OP_TYPE_ENCRYPT = 0,
80   CRYPTODEV_OP_TYPE_DECRYPT,
81   CRYPTODEV_N_OP_TYPES,
82 } cryptodev_op_type_t;
83
84 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
85 typedef void cryptodev_session_t;
86 #else
87 typedef struct rte_cryptodev_sym_session cryptodev_session_t;
88 #endif
89
90 /* Cryptodev session data, one data per direction per numa */
91 typedef struct
92 {
93   cryptodev_session_t ***keys;
94 } cryptodev_key_t;
95
96 /* Replicate DPDK rte_cryptodev_sym_capability structure with key size ranges
97  * in favor of vpp vector */
98 typedef struct
99 {
100   enum rte_crypto_sym_xform_type xform_type;
101   union
102   {
103     struct
104     {
105       enum rte_crypto_auth_algorithm algo; /*auth algo */
106       u32 *digest_sizes;                   /* vector of auth digest sizes */
107     } auth;
108     struct
109     {
110       enum rte_crypto_cipher_algorithm algo; /* cipher algo */
111       u32 *key_sizes;                        /* vector of cipher key sizes */
112     } cipher;
113     struct
114     {
115       enum rte_crypto_aead_algorithm algo; /* aead algo */
116       u32 *key_sizes;                      /*vector of aead key sizes */
117       u32 *aad_sizes;                      /*vector of aad sizes */
118       u32 *digest_sizes;                   /* vector of aead digest sizes */
119     } aead;
120   };
121 } cryptodev_capability_t;
122
123 /* Cryptodev instance data */
124 typedef struct
125 {
126   u32 dev_id;
127   u32 q_id;
128   char *desc;
129 } cryptodev_inst_t;
130
131 typedef struct
132 {
133   struct rte_mempool *sess_pool;
134 #if RTE_VERSION < RTE_VERSION_NUM(22, 11, 0, 0)
135   struct rte_mempool *sess_priv_pool;
136 #endif
137 } cryptodev_session_pool_t;
138
139 typedef struct
140 {
141   cryptodev_session_pool_t *sess_pools;
142 } cryptodev_numa_data_t;
143
144 typedef struct
145 {
146   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
147   struct rte_crypto_op op;
148   struct rte_crypto_sym_op sop;
149   u8 iv[CRYPTODEV_MAX_IV_SIZE];
150   u8 aad[CRYPTODEV_MAX_AAD_SIZE];
151   vnet_crypto_async_frame_t *frame;
152   u32 n_elts;
153 } cryptodev_op_t;
154
155 typedef struct
156 {
157   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
158   vlib_buffer_t *b[VNET_CRYPTO_FRAME_SIZE];
159   union
160   {
161     struct
162     {
163       cryptodev_op_t **cops;
164       struct rte_mempool *cop_pool;
165       struct rte_ring *ring;
166     };
167     struct
168     {
169       struct rte_crypto_raw_dp_ctx *ctx;
170       struct rte_ring *cached_frame;
171       u16 aad_index;
172       u8 *aad_buf;
173       u64 aad_phy_addr;
174       cryptodev_session_t *reset_sess;
175     };
176   };
177   u16 cryptodev_id;
178   u16 cryptodev_q;
179   u16 inflight;
180 } cryptodev_engine_thread_t;
181
182 typedef struct
183 {
184   cryptodev_numa_data_t *per_numa_data;
185   cryptodev_key_t *keys;
186   cryptodev_engine_thread_t *per_thread_data;
187   enum rte_iova_mode iova_mode;
188   cryptodev_inst_t *cryptodev_inst;
189   clib_bitmap_t *active_cdev_inst_mask;
190   clib_spinlock_t tlock;
191   cryptodev_capability_t *supported_caps;
192   u32 sess_sz;
193   u32 drivers_cnt;
194   u8 is_raw_api;
195 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
196   u8 driver_id;
197 #endif
198 } cryptodev_main_t;
199
200 extern cryptodev_main_t cryptodev_main;
201
202 static_always_inline void
203 cryptodev_mark_frame_err_status (vnet_crypto_async_frame_t *f,
204                                  vnet_crypto_op_status_t s)
205 {
206   u32 n_elts = f->n_elts, i;
207
208   for (i = 0; i < n_elts; i++)
209     f->elts[i].status = s;
210   f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
211 }
212
213 int cryptodev_session_create (vlib_main_t *vm, vnet_crypto_key_index_t idx,
214                               u32 aad_len);
215
216 void cryptodev_sess_handler (vlib_main_t *vm, vnet_crypto_key_op_t kop,
217                              vnet_crypto_key_index_t idx, u32 aad_len);
218
219 int cryptodev_check_cap_support (struct rte_cryptodev_sym_capability_idx *idx,
220                                  u32 key_size, u32 digest_size, u32 aad_size);
221
222 clib_error_t *cryptodev_register_cop_hdl (vlib_main_t *vm, u32 eidx);
223
224 clib_error_t *__clib_weak cryptodev_register_raw_hdl (vlib_main_t *vm,
225                                                       u32 eidx);
226
227 clib_error_t *__clib_weak dpdk_cryptodev_init (vlib_main_t *vm);
228
229 #endif