dpdk: cryptodev support enabled by default
[vpp.git] / src / plugins / dpdk / ipsec / ipsec.h
1 /*
2  * Copyright (c) 2016 Intel 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 #ifndef __DPDK_IPSEC_H__
16 #define __DPDK_IPSEC_H__
17
18 #include <vnet/vnet.h>
19
20 #undef always_inline
21 #include <rte_config.h>
22 #include <rte_crypto.h>
23 #include <rte_cryptodev.h>
24
25 #if CLIB_DEBUG > 0
26 #define always_inline static inline
27 #else
28 #define always_inline static inline __attribute__ ((__always_inline__))
29 #endif
30
31
32 #define MAX_QP_PER_LCORE 16
33
34 typedef struct
35 {
36   u32 salt;
37   u32 iv[2];
38   u32 cnt;
39 } dpdk_gcm_cnt_blk;
40
41 typedef struct
42 {
43   dpdk_gcm_cnt_blk cb;
44   union
45   {
46     u8 aad[12];
47     u8 icv[64];
48   };
49 } dpdk_cop_priv_t;
50
51 typedef struct
52 {
53   u8 cipher_algo;
54   u8 auth_algo;
55   u8 is_outbound;
56 } crypto_worker_qp_key_t;
57
58 typedef struct
59 {
60   u16 dev_id;
61   u16 qp_id;
62   u16 is_outbound;
63   i16 inflights;
64   u32 bi[VLIB_FRAME_SIZE];
65   struct rte_crypto_op *cops[VLIB_FRAME_SIZE];
66   struct rte_crypto_op **free_cops;
67 } crypto_qp_data_t;
68
69 typedef struct
70 {
71   u8 qp_index;
72   void *sess;
73 } crypto_sa_session_t;
74
75 typedef struct
76 {
77   crypto_sa_session_t *sa_sess_d[2];
78   crypto_qp_data_t *qp_data;
79   uword *algo_qp_map;
80 } crypto_worker_main_t;
81
82 typedef struct
83 {
84   struct rte_mempool **cop_pools;
85   crypto_worker_main_t *workers_main;
86   u8 enabled;
87 } dpdk_crypto_main_t;
88
89 dpdk_crypto_main_t dpdk_crypto_main;
90
91 extern vlib_node_registration_t dpdk_crypto_input_node;
92
93 #define CRYPTO_N_FREE_COPS (VLIB_FRAME_SIZE * 3)
94
95 static_always_inline void
96 crypto_alloc_cops ()
97 {
98   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
99   u32 thread_index = vlib_get_thread_index ();
100   crypto_worker_main_t *cwm = &dcm->workers_main[thread_index];
101   unsigned socket_id = rte_socket_id ();
102   crypto_qp_data_t *qpd;
103
104   /* *INDENT-OFF* */
105   vec_foreach (qpd, cwm->qp_data)
106     {
107       u32 l = vec_len (qpd->free_cops);
108
109       if (PREDICT_FALSE (l < VLIB_FRAME_SIZE))
110         {
111           u32 n_alloc;
112
113           if (PREDICT_FALSE (!qpd->free_cops))
114             vec_alloc (qpd->free_cops, CRYPTO_N_FREE_COPS);
115
116           n_alloc = rte_crypto_op_bulk_alloc (dcm->cop_pools[socket_id],
117                                               RTE_CRYPTO_OP_TYPE_SYMMETRIC,
118                                               &qpd->free_cops[l],
119                                               CRYPTO_N_FREE_COPS - l - 1);
120
121           _vec_len (qpd->free_cops) = l + n_alloc;
122         }
123     }
124   /* *INDENT-ON* */
125 }
126
127 static_always_inline void
128 crypto_free_cop (crypto_qp_data_t * qpd, struct rte_crypto_op **cops, u32 n)
129 {
130   u32 l = vec_len (qpd->free_cops);
131
132   if (l + n >= CRYPTO_N_FREE_COPS)
133     {
134       l -= VLIB_FRAME_SIZE;
135       rte_mempool_put_bulk (cops[0]->mempool,
136                             (void **) &qpd->free_cops[l], VLIB_FRAME_SIZE);
137     }
138   clib_memcpy (&qpd->free_cops[l], cops, sizeof (*cops) * n);
139
140   _vec_len (qpd->free_cops) = l + n;
141 }
142
143 static_always_inline int
144 check_algo_is_supported (const struct rte_cryptodev_capabilities *cap,
145                          char *name)
146 {
147   struct
148   {
149     uint8_t cipher_algo;
150     enum rte_crypto_sym_xform_type type;
151     union
152     {
153       enum rte_crypto_auth_algorithm auth;
154       enum rte_crypto_cipher_algorithm cipher;
155     };
156     char *name;
157   } supported_algo[] =
158   {
159     {
160     .type = RTE_CRYPTO_SYM_XFORM_CIPHER,.cipher =
161         RTE_CRYPTO_CIPHER_NULL,.name = "NULL"},
162     {
163     .type = RTE_CRYPTO_SYM_XFORM_CIPHER,.cipher =
164         RTE_CRYPTO_CIPHER_AES_CBC,.name = "AES_CBC"},
165     {
166     .type = RTE_CRYPTO_SYM_XFORM_CIPHER,.cipher =
167         RTE_CRYPTO_CIPHER_AES_CTR,.name = "AES_CTR"},
168     {
169     .type = RTE_CRYPTO_SYM_XFORM_CIPHER,.cipher =
170         RTE_CRYPTO_CIPHER_3DES_CBC,.name = "3DES-CBC"},
171     {
172     .type = RTE_CRYPTO_SYM_XFORM_CIPHER,.cipher =
173         RTE_CRYPTO_CIPHER_AES_GCM,.name = "AES-GCM"},
174     {
175     .type = RTE_CRYPTO_SYM_XFORM_AUTH,.auth =
176         RTE_CRYPTO_AUTH_SHA1_HMAC,.name = "HMAC-SHA1"},
177     {
178     .type = RTE_CRYPTO_SYM_XFORM_AUTH,.auth =
179         RTE_CRYPTO_AUTH_SHA256_HMAC,.name = "HMAC-SHA256"},
180     {
181     .type = RTE_CRYPTO_SYM_XFORM_AUTH,.auth =
182         RTE_CRYPTO_AUTH_SHA384_HMAC,.name = "HMAC-SHA384"},
183     {
184     .type = RTE_CRYPTO_SYM_XFORM_AUTH,.auth =
185         RTE_CRYPTO_AUTH_SHA512_HMAC,.name = "HMAC-SHA512"},
186     {
187     .type = RTE_CRYPTO_SYM_XFORM_AUTH,.auth =
188         RTE_CRYPTO_AUTH_AES_XCBC_MAC,.name = "AES-XCBC-MAC"},
189     {
190     .type = RTE_CRYPTO_SYM_XFORM_AUTH,.auth =
191         RTE_CRYPTO_AUTH_AES_GCM,.name = "AES-GCM"},
192     {
193       /* tail */
194   .type = RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED},};
195   uint32_t i = 0;
196
197   if (cap->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
198     return -1;
199
200   while (supported_algo[i].type != RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED)
201     {
202       if (cap->sym.xform_type == supported_algo[i].type)
203         {
204           if ((cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
205                cap->sym.cipher.algo == supported_algo[i].cipher) ||
206               (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH &&
207                cap->sym.auth.algo == supported_algo[i].auth))
208             {
209               if (name)
210                 strcpy (name, supported_algo[i].name);
211               return 0;
212             }
213         }
214
215       i++;
216     }
217
218   return -1;
219 }
220
221 #endif /* __DPDK_IPSEC_H__ */
222
223 /*
224  * fd.io coding-style-patch-verification: ON
225  *
226  * Local Variables:
227  * eval: (c-set-style "gnu")
228  * End:
229  */