4c85e85a172a6ae51508089028857c32a98df15e
[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       10240
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
48 /**
49  * crypto (alg, cryptodev_alg, key_size), hash (alg, digest-size)
50  **/
51 #define foreach_cryptodev_link_async_alg                                      \
52   _ (AES_128_CBC, AES_CBC, 16, SHA1, 12)                                      \
53   _ (AES_192_CBC, AES_CBC, 24, SHA1, 12)                                      \
54   _ (AES_256_CBC, AES_CBC, 32, SHA1, 12)                                      \
55   _ (AES_128_CBC, AES_CBC, 16, SHA224, 14)                                    \
56   _ (AES_192_CBC, AES_CBC, 24, SHA224, 14)                                    \
57   _ (AES_256_CBC, AES_CBC, 32, SHA224, 14)                                    \
58   _ (AES_128_CBC, AES_CBC, 16, SHA256, 16)                                    \
59   _ (AES_192_CBC, AES_CBC, 24, SHA256, 16)                                    \
60   _ (AES_256_CBC, AES_CBC, 32, SHA256, 16)                                    \
61   _ (AES_128_CBC, AES_CBC, 16, SHA384, 24)                                    \
62   _ (AES_192_CBC, AES_CBC, 24, SHA384, 24)                                    \
63   _ (AES_256_CBC, AES_CBC, 32, SHA384, 24)                                    \
64   _ (AES_128_CBC, AES_CBC, 16, SHA512, 32)                                    \
65   _ (AES_192_CBC, AES_CBC, 24, SHA512, 32)                                    \
66   _ (AES_256_CBC, AES_CBC, 32, SHA512, 32)
67
68 typedef enum
69 {
70   CRYPTODEV_OP_TYPE_ENCRYPT = 0,
71   CRYPTODEV_OP_TYPE_DECRYPT,
72   CRYPTODEV_N_OP_TYPES,
73 } cryptodev_op_type_t;
74
75 /* Cryptodev session data, one data per direction per numa */
76 typedef struct
77 {
78   struct rte_cryptodev_sym_session ***keys;
79 } cryptodev_key_t;
80
81 /* Replicate DPDK rte_cryptodev_sym_capability structure with key size ranges
82  * in favor of vpp vector */
83 typedef struct
84 {
85   enum rte_crypto_sym_xform_type xform_type;
86   union
87   {
88     struct
89     {
90       enum rte_crypto_auth_algorithm algo; /*auth algo */
91       u32 *digest_sizes;                   /* vector of auth digest sizes */
92     } auth;
93     struct
94     {
95       enum rte_crypto_cipher_algorithm algo; /* cipher algo */
96       u32 *key_sizes;                        /* vector of cipher key sizes */
97     } cipher;
98     struct
99     {
100       enum rte_crypto_aead_algorithm algo; /* aead algo */
101       u32 *key_sizes;                      /*vector of aead key sizes */
102       u32 *aad_sizes;                      /*vector of aad sizes */
103       u32 *digest_sizes;                   /* vector of aead digest sizes */
104     } aead;
105   };
106 } cryptodev_capability_t;
107
108 /* Cryptodev instance data */
109 typedef struct
110 {
111   u32 dev_id;
112   u32 q_id;
113   char *desc;
114 } cryptodev_inst_t;
115
116 typedef struct
117 {
118   struct rte_mempool *sess_pool;
119   struct rte_mempool *sess_priv_pool;
120 } cryptodev_numa_data_t;
121
122 typedef struct
123 {
124   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
125   struct rte_crypto_op op;
126   struct rte_crypto_sym_op sop;
127   u8 iv[CRYPTODEV_MAX_IV_SIZE];
128   u8 aad[CRYPTODEV_MAX_AAD_SIZE];
129   vnet_crypto_async_frame_t *frame;
130   u32 n_elts;
131 } cryptodev_op_t;
132
133 typedef struct
134 {
135   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
136   vlib_buffer_t *b[VNET_CRYPTO_FRAME_SIZE];
137   union
138   {
139     struct
140     {
141       cryptodev_op_t **cops;
142       struct rte_mempool *cop_pool;
143       struct rte_ring *ring;
144     };
145     struct
146     {
147       struct rte_crypto_raw_dp_ctx *ctx;
148       struct rte_ring *cached_frame;
149       u16 aad_index;
150       u8 *aad_buf;
151       u64 aad_phy_addr;
152       struct rte_cryptodev_sym_session *reset_sess;
153     };
154   };
155   u16 cryptodev_id;
156   u16 cryptodev_q;
157   u16 inflight;
158 } cryptodev_engine_thread_t;
159
160 typedef struct
161 {
162   cryptodev_numa_data_t *per_numa_data;
163   cryptodev_key_t *keys;
164   cryptodev_engine_thread_t *per_thread_data;
165   enum rte_iova_mode iova_mode;
166   cryptodev_inst_t *cryptodev_inst;
167   clib_bitmap_t *active_cdev_inst_mask;
168   clib_spinlock_t tlock;
169   cryptodev_capability_t *supported_caps;
170   u8 is_raw_api;
171 } cryptodev_main_t;
172
173 extern cryptodev_main_t cryptodev_main;
174
175 static_always_inline void
176 cryptodev_mark_frame_err_status (vnet_crypto_async_frame_t *f,
177                                  vnet_crypto_op_status_t s)
178 {
179   u32 n_elts = f->n_elts, i;
180
181   for (i = 0; i < n_elts; i++)
182     f->elts[i].status = s;
183   f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
184 }
185
186 int cryptodev_session_create (vlib_main_t *vm, vnet_crypto_key_index_t idx,
187                               u32 aad_len);
188
189 void cryptodev_sess_handler (vlib_main_t *vm, vnet_crypto_key_op_t kop,
190                              vnet_crypto_key_index_t idx, u32 aad_len);
191
192 int cryptodev_check_cap_support (struct rte_cryptodev_sym_capability_idx *idx,
193                                  u32 key_size, u32 digest_size, u32 aad_size);
194
195 clib_error_t *cryptodev_register_cop_hdl (vlib_main_t *vm, u32 eidx);
196
197 clib_error_t *__clib_weak cryptodev_register_raw_hdl (vlib_main_t *vm,
198                                                       u32 eidx);
199
200 clib_error_t *
201 dpdk_cryptodev_init (vlib_main_t * vm);
202
203 #endif