dpdk: required changes for 17.08
[vpp.git] / src / plugins / dpdk / ipsec / esp.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_ESP_H__
16 #define __DPDK_ESP_H__
17
18 #include <dpdk/ipsec/ipsec.h>
19 #include <vnet/ipsec/ipsec.h>
20 #include <vnet/ipsec/esp.h>
21
22 typedef struct
23 {
24   enum rte_crypto_cipher_algorithm algo;
25 #if ! DPDK_NO_AEAD
26   enum rte_crypto_aead_algorithm aead_algo;
27 #endif
28   u8 key_len;
29   u8 iv_len;
30 } dpdk_esp_crypto_alg_t;
31
32 typedef struct
33 {
34   enum rte_crypto_auth_algorithm algo;
35   u8 trunc_size;
36 } dpdk_esp_integ_alg_t;
37
38 typedef struct
39 {
40   dpdk_esp_crypto_alg_t *esp_crypto_algs;
41   dpdk_esp_integ_alg_t *esp_integ_algs;
42 } dpdk_esp_main_t;
43
44 dpdk_esp_main_t dpdk_esp_main;
45
46 static_always_inline void
47 dpdk_esp_init ()
48 {
49   dpdk_esp_main_t *em = &dpdk_esp_main;
50   dpdk_esp_integ_alg_t *i;
51   dpdk_esp_crypto_alg_t *c;
52
53   vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
54
55   c = &em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128];
56   c->algo = RTE_CRYPTO_CIPHER_AES_CBC;
57   c->key_len = 16;
58   c->iv_len = 16;
59
60   c = &em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192];
61   c->algo = RTE_CRYPTO_CIPHER_AES_CBC;
62   c->key_len = 24;
63   c->iv_len = 16;
64
65   c = &em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256];
66   c->algo = RTE_CRYPTO_CIPHER_AES_CBC;
67   c->key_len = 32;
68   c->iv_len = 16;
69
70   c = &em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_GCM_128];
71 #if DPDK_NO_AEAD
72   c->algo = RTE_CRYPTO_CIPHER_AES_GCM;
73 #else
74   c->aead_algo = RTE_CRYPTO_AEAD_AES_GCM;
75 #endif
76   c->key_len = 16;
77   c->iv_len = 8;
78
79   vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
80
81   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
82   i->algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
83   i->trunc_size = 12;
84
85   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
86   i->algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
87   i->trunc_size = 12;
88
89   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
90   i->algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
91   i->trunc_size = 16;
92
93   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
94   i->algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
95   i->trunc_size = 24;
96
97   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
98   i->algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
99   i->trunc_size = 32;
100 #if DPDK_NO_AEAD
101   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_AES_GCM_128];
102   i->algo = RTE_CRYPTO_AUTH_AES_GCM;
103   i->trunc_size = 16;
104 #endif
105 }
106
107 static_always_inline int
108 translate_crypto_algo (ipsec_crypto_alg_t crypto_algo,
109                        struct rte_crypto_sym_xform *xform, u8 use_esn)
110 {
111 #if ! DPDK_NO_AEAD
112   const u16 iv_off =
113     sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op) +
114     offsetof (dpdk_cop_priv_t, cb);
115 #endif
116
117   xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
118
119   switch (crypto_algo)
120     {
121     case IPSEC_CRYPTO_ALG_NONE:
122 #if ! DPDK_NO_AEAD
123       xform->cipher.iv.offset = iv_off;
124       xform->cipher.iv.length = 0;
125 #endif
126       xform->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
127       break;
128     case IPSEC_CRYPTO_ALG_AES_CBC_128:
129     case IPSEC_CRYPTO_ALG_AES_CBC_192:
130     case IPSEC_CRYPTO_ALG_AES_CBC_256:
131 #if ! DPDK_NO_AEAD
132       xform->cipher.iv.offset = iv_off;
133       xform->cipher.iv.length = 16;
134 #endif
135       xform->cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
136       break;
137     case IPSEC_CRYPTO_ALG_AES_GCM_128:
138 #if DPDK_NO_AEAD
139       xform->cipher.algo = RTE_CRYPTO_CIPHER_AES_GCM;
140 #else
141       xform->type = RTE_CRYPTO_SYM_XFORM_AEAD;
142       xform->aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
143       xform->aead.iv.offset = iv_off;
144       xform->aead.iv.length = 12;       /* GCM IV, not ESP IV */
145       xform->aead.digest_length = 16;
146       xform->aead.aad_length = use_esn ? 12 : 8;
147 #endif
148       break;
149     default:
150       return -1;
151     }
152
153   return 0;
154 }
155
156 static_always_inline int
157 translate_integ_algo (ipsec_integ_alg_t integ_alg,
158                       struct rte_crypto_sym_xform *auth_xform, u8 use_esn)
159 {
160   auth_xform->type = RTE_CRYPTO_SYM_XFORM_AUTH;
161
162   switch (integ_alg)
163     {
164     case IPSEC_INTEG_ALG_NONE:
165       auth_xform->auth.algo = RTE_CRYPTO_AUTH_NULL;
166       auth_xform->auth.digest_length = 0;
167       break;
168     case IPSEC_INTEG_ALG_SHA1_96:
169       auth_xform->auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
170       auth_xform->auth.digest_length = 12;
171       break;
172     case IPSEC_INTEG_ALG_SHA_256_96:
173       auth_xform->auth.algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
174       auth_xform->auth.digest_length = 12;
175       break;
176     case IPSEC_INTEG_ALG_SHA_256_128:
177       auth_xform->auth.algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
178       auth_xform->auth.digest_length = 16;
179       break;
180     case IPSEC_INTEG_ALG_SHA_384_192:
181       auth_xform->auth.algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
182       auth_xform->auth.digest_length = 24;
183       break;
184     case IPSEC_INTEG_ALG_SHA_512_256:
185       auth_xform->auth.algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
186       auth_xform->auth.digest_length = 32;
187       break;
188 #if DPDK_NO_AEAD
189     case IPSEC_INTEG_ALG_AES_GCM_128:
190       auth_xform->auth.algo = RTE_CRYPTO_AUTH_AES_GCM;
191       auth_xform->auth.digest_length = 16;
192       auth_xform->auth.add_auth_data_length = use_esn ? 12 : 8;
193       break;
194 #endif
195     default:
196       return -1;
197     }
198
199   return 0;
200 }
201
202 static_always_inline i32
203 create_sym_sess (ipsec_sa_t * sa, crypto_sa_session_t * sa_sess,
204                  u8 is_outbound)
205 {
206   u32 thread_index = vlib_get_thread_index ();
207   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
208   crypto_worker_main_t *cwm = &dcm->workers_main[thread_index];
209   struct rte_crypto_sym_xform cipher_xform = { 0 };
210   struct rte_crypto_sym_xform auth_xform = { 0 };
211   struct rte_crypto_sym_xform *xfs;
212   uword key = 0, *data;
213   crypto_worker_qp_key_t *p_key = (crypto_worker_qp_key_t *) & key;
214 #if ! DPDK_NO_AEAD
215   i32 socket_id = rte_socket_id ();
216   i32 ret;
217 #endif
218
219   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
220     {
221       sa->crypto_key_len -= 4;
222       clib_memcpy (&sa->salt, &sa->crypto_key[sa->crypto_key_len], 4);
223     }
224   else
225     {
226       u32 seed = (u32) clib_cpu_time_now ();
227       sa->salt = random_u32 (&seed);
228     }
229
230   if (translate_crypto_algo (sa->crypto_alg, &cipher_xform, sa->use_esn) < 0)
231     return -1;
232   p_key->cipher_algo = cipher_xform.cipher.algo;
233
234   if (translate_integ_algo (sa->integ_alg, &auth_xform, sa->use_esn) < 0)
235     return -1;
236   p_key->auth_algo = auth_xform.auth.algo;
237
238 #if ! DPDK_NO_AEAD
239   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
240     {
241       cipher_xform.aead.key.data = sa->crypto_key;
242       cipher_xform.aead.key.length = sa->crypto_key_len;
243
244       if (is_outbound)
245         cipher_xform.cipher.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
246       else
247         cipher_xform.cipher.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
248       cipher_xform.next = NULL;
249       xfs = &cipher_xform;
250       p_key->is_aead = 1;
251     }
252   else                          /* Cipher + Auth */
253 #endif
254     {
255       cipher_xform.cipher.key.data = sa->crypto_key;
256       cipher_xform.cipher.key.length = sa->crypto_key_len;
257
258       auth_xform.auth.key.data = sa->integ_key;
259       auth_xform.auth.key.length = sa->integ_key_len;
260
261       if (is_outbound)
262         {
263           cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
264           auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
265           cipher_xform.next = &auth_xform;
266           xfs = &cipher_xform;
267         }
268       else
269         {
270           cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
271           auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
272           auth_xform.next = &cipher_xform;
273           xfs = &auth_xform;
274         }
275       p_key->is_aead = 0;
276     }
277
278   p_key->is_outbound = is_outbound;
279
280   data = hash_get (cwm->algo_qp_map, key);
281   if (!data)
282     return -1;
283
284 #if DPDK_NO_AEAD
285   sa_sess->sess =
286     rte_cryptodev_sym_session_create (cwm->qp_data[*data].dev_id, xfs);
287   if (!sa_sess->sess)
288     return -1;
289 #else
290   sa_sess->sess =
291     rte_cryptodev_sym_session_create (dcm->sess_h_pools[socket_id]);
292   if (!sa_sess->sess)
293     return -1;
294
295   ret =
296     rte_cryptodev_sym_session_init (cwm->qp_data[*data].dev_id, sa_sess->sess,
297                                     xfs, dcm->sess_pools[socket_id]);
298   if (ret)
299     return -1;
300 #endif
301
302   sa_sess->qp_index = (u8) * data;
303
304   return 0;
305 }
306
307 static_always_inline void
308 crypto_set_icb (dpdk_gcm_cnt_blk * icb, u32 salt, u32 seq, u32 seq_hi)
309 {
310   icb->salt = salt;
311   icb->iv[0] = seq;
312   icb->iv[1] = seq_hi;
313 #if DPDK_NO_AEAD
314   icb->cnt = clib_host_to_net_u32 (1);
315 #endif
316 }
317
318 #define __unused __attribute__((unused))
319 static_always_inline void
320 crypto_op_setup (u8 is_aead, struct rte_mbuf *mb0,
321                  struct rte_crypto_op *cop, void *session,
322                  u32 cipher_off, u32 cipher_len,
323                  u8 * icb __unused, u32 iv_size __unused,
324                  u32 auth_off, u32 auth_len,
325                  u8 * aad __unused, u32 aad_size __unused,
326                  u8 * digest, u64 digest_paddr, u32 digest_size __unused)
327 {
328   struct rte_crypto_sym_op *sym_cop;
329
330   sym_cop = (struct rte_crypto_sym_op *) (cop + 1);
331
332   sym_cop->m_src = mb0;
333   rte_crypto_op_attach_sym_session (cop, session);
334
335   if (!digest_paddr)
336     digest_paddr =
337       rte_pktmbuf_mtophys_offset (mb0, (uintptr_t) digest - (uintptr_t) mb0);
338
339 #if DPDK_NO_AEAD
340   sym_cop->cipher.data.offset = cipher_off;
341   sym_cop->cipher.data.length = cipher_len;
342
343   sym_cop->cipher.iv.data = icb;
344   sym_cop->cipher.iv.phys_addr =
345     cop->phys_addr + (uintptr_t) icb - (uintptr_t) cop;
346   sym_cop->cipher.iv.length = iv_size;
347
348   if (is_aead)
349     {
350       sym_cop->auth.aad.data = aad;
351       sym_cop->auth.aad.phys_addr =
352         cop->phys_addr + (uintptr_t) aad - (uintptr_t) cop;
353       sym_cop->auth.aad.length = aad_size;
354     }
355   else
356     {
357       sym_cop->auth.data.offset = auth_off;
358       sym_cop->auth.data.length = auth_len;
359     }
360
361   sym_cop->auth.digest.data = digest;
362   sym_cop->auth.digest.phys_addr = digest_paddr;
363   sym_cop->auth.digest.length = digest_size;
364 #else /* ! DPDK_NO_AEAD */
365   if (is_aead)
366     {
367       sym_cop->aead.data.offset = cipher_off;
368       sym_cop->aead.data.length = cipher_len;
369
370       sym_cop->aead.aad.data = aad;
371       sym_cop->aead.aad.phys_addr =
372         cop->phys_addr + (uintptr_t) aad - (uintptr_t) cop;
373
374       sym_cop->aead.digest.data = digest;
375       sym_cop->aead.digest.phys_addr = digest_paddr;
376     }
377   else
378     {
379       sym_cop->cipher.data.offset = cipher_off;
380       sym_cop->cipher.data.length = cipher_len;
381
382       sym_cop->auth.data.offset = auth_off;
383       sym_cop->auth.data.length = auth_len;
384
385       sym_cop->auth.digest.data = digest;
386       sym_cop->auth.digest.phys_addr = digest_paddr;
387     }
388 #endif /* DPDK_NO_AEAD */
389 }
390
391 #undef __unused
392
393 #endif /* __DPDK_ESP_H__ */
394
395 /*
396  * fd.io coding-style-patch-verification: ON
397  *
398  * Local Variables:
399  * eval: (c-set-style "gnu")
400  * End:
401  */