ipsec: allow null/null for crypto/integ algorithms pair
[vpp.git] / src / plugins / dpdk / ipsec / ipsec.c
1 /*
2  * Copyright (c) 2017 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 #include <vnet/vnet.h>
16 #include <vnet/ip/ip.h>
17 #include <vnet/api_errno.h>
18 #include <vnet/ipsec/ipsec.h>
19 #include <vlib/node_funcs.h>
20
21 #include <dpdk/device/dpdk.h>
22 #include <dpdk/ipsec/ipsec.h>
23
24 dpdk_crypto_main_t dpdk_crypto_main;
25
26 #define EMPTY_STRUCT {0}
27
28 static void
29 algos_init (u32 n_mains)
30 {
31   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
32   crypto_alg_t *a;
33
34   vec_validate_aligned (dcm->cipher_algs, IPSEC_CRYPTO_N_ALG - 1, 8);
35
36   {
37 #define _(v,f,str) \
38   dcm->cipher_algs[IPSEC_CRYPTO_ALG_##f].name = str; \
39   dcm->cipher_algs[IPSEC_CRYPTO_ALG_##f].disabled = n_mains;
40     foreach_ipsec_crypto_alg
41 #undef _
42   }
43
44   /* Minimum boundary for ciphers is 4B, required by ESP */
45   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_NONE];
46   a->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
47   a->alg = RTE_CRYPTO_CIPHER_NULL;
48   a->boundary = 4;              /* 1 */
49   a->key_len = 0;
50   a->iv_len = 0;
51
52   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_CBC_128];
53   a->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
54   a->alg = RTE_CRYPTO_CIPHER_AES_CBC;
55   a->boundary = 16;
56   a->key_len = 16;
57   a->iv_len = 16;
58
59   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_CBC_192];
60   a->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
61   a->alg = RTE_CRYPTO_CIPHER_AES_CBC;
62   a->boundary = 16;
63   a->key_len = 24;
64   a->iv_len = 16;
65
66   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_CBC_256];
67   a->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
68   a->alg = RTE_CRYPTO_CIPHER_AES_CBC;
69   a->boundary = 16;
70   a->key_len = 32;
71   a->iv_len = 16;
72
73   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_CTR_128];
74   a->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
75   a->alg = RTE_CRYPTO_CIPHER_AES_CTR;
76   a->boundary = 4;              /* 1 */
77   a->key_len = 16;
78   a->iv_len = 8;
79
80   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_CTR_192];
81   a->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
82   a->alg = RTE_CRYPTO_CIPHER_AES_CTR;
83   a->boundary = 4;              /* 1 */
84   a->key_len = 24;
85   a->iv_len = 8;
86
87   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_CTR_256];
88   a->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
89   a->alg = RTE_CRYPTO_CIPHER_AES_CTR;
90   a->boundary = 4;              /* 1 */
91   a->key_len = 32;
92   a->iv_len = 8;
93
94 #define AES_GCM_TYPE RTE_CRYPTO_SYM_XFORM_AEAD
95 #define AES_GCM_ALG RTE_CRYPTO_AEAD_AES_GCM
96
97   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_GCM_128];
98   a->type = AES_GCM_TYPE;
99   a->alg = AES_GCM_ALG;
100   a->boundary = 4;              /* 1 */
101   a->key_len = 16;
102   a->iv_len = 8;
103   a->trunc_size = 16;
104
105   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_GCM_192];
106   a->type = AES_GCM_TYPE;
107   a->alg = AES_GCM_ALG;
108   a->boundary = 4;              /* 1 */
109   a->key_len = 24;
110   a->iv_len = 8;
111   a->trunc_size = 16;
112
113   a = &dcm->cipher_algs[IPSEC_CRYPTO_ALG_AES_GCM_256];
114   a->type = AES_GCM_TYPE;
115   a->alg = AES_GCM_ALG;
116   a->boundary = 4;              /* 1 */
117   a->key_len = 32;
118   a->iv_len = 8;
119   a->trunc_size = 16;
120
121   vec_validate (dcm->auth_algs, IPSEC_INTEG_N_ALG - 1);
122
123   {
124 #define _(v,f,str) \
125   dcm->auth_algs[IPSEC_INTEG_ALG_##f].name = str; \
126   dcm->auth_algs[IPSEC_INTEG_ALG_##f].disabled = n_mains;
127     foreach_ipsec_integ_alg
128 #undef _
129   }
130
131   a = &dcm->auth_algs[IPSEC_INTEG_ALG_NONE];
132   a->type = RTE_CRYPTO_SYM_XFORM_AUTH;
133   a->alg = RTE_CRYPTO_AUTH_NULL;
134   a->key_len = 0;
135   a->trunc_size = 0;
136
137   a = &dcm->auth_algs[IPSEC_INTEG_ALG_MD5_96];
138   a->type = RTE_CRYPTO_SYM_XFORM_AUTH;
139   a->alg = RTE_CRYPTO_AUTH_MD5_HMAC;
140   a->key_len = 16;
141   a->trunc_size = 12;
142
143   a = &dcm->auth_algs[IPSEC_INTEG_ALG_SHA1_96];
144   a->type = RTE_CRYPTO_SYM_XFORM_AUTH;
145   a->alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
146   a->key_len = 20;
147   a->trunc_size = 12;
148
149   a = &dcm->auth_algs[IPSEC_INTEG_ALG_SHA_256_96];
150   a->type = RTE_CRYPTO_SYM_XFORM_AUTH;
151   a->alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
152   a->key_len = 32;
153   a->trunc_size = 12;
154
155   a = &dcm->auth_algs[IPSEC_INTEG_ALG_SHA_256_128];
156   a->type = RTE_CRYPTO_SYM_XFORM_AUTH;
157   a->alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
158   a->key_len = 32;
159   a->trunc_size = 16;
160
161   a = &dcm->auth_algs[IPSEC_INTEG_ALG_SHA_384_192];
162   a->type = RTE_CRYPTO_SYM_XFORM_AUTH;
163   a->alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
164   a->key_len = 48;
165   a->trunc_size = 24;
166
167   a = &dcm->auth_algs[IPSEC_INTEG_ALG_SHA_512_256];
168   a->type = RTE_CRYPTO_SYM_XFORM_AUTH;
169   a->alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
170   a->key_len = 64;
171   a->trunc_size = 32;
172 }
173
174 static u8
175 cipher_alg_index (const crypto_alg_t * alg)
176 {
177   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
178
179   return (alg - dcm->cipher_algs);
180 }
181
182 static u8
183 auth_alg_index (const crypto_alg_t * alg)
184 {
185   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
186
187   return (alg - dcm->auth_algs);
188 }
189
190 static crypto_alg_t *
191 cipher_cap_to_alg (const struct rte_cryptodev_capabilities *cap, u8 key_len)
192 {
193   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
194   crypto_alg_t *alg;
195
196   if (cap->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
197     return NULL;
198
199   /* *INDENT-OFF* */
200   vec_foreach (alg, dcm->cipher_algs)
201     {
202       if ((cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) &&
203           (alg->type == RTE_CRYPTO_SYM_XFORM_CIPHER) &&
204           (cap->sym.cipher.algo == alg->alg) &&
205           (alg->key_len == key_len))
206         return alg;
207       if ((cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) &&
208           (alg->type == RTE_CRYPTO_SYM_XFORM_AEAD) &&
209           (cap->sym.aead.algo == alg->alg) &&
210           (alg->key_len == key_len))
211         return alg;
212     }
213   /* *INDENT-ON* */
214
215   return NULL;
216 }
217
218 static crypto_alg_t *
219 auth_cap_to_alg (const struct rte_cryptodev_capabilities *cap, u8 trunc_size)
220 {
221   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
222   crypto_alg_t *alg;
223
224   if ((cap->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC) ||
225       (cap->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH))
226     return NULL;
227
228   /* *INDENT-OFF* */
229   vec_foreach (alg, dcm->auth_algs)
230     {
231       if ((cap->sym.auth.algo == alg->alg) &&
232           (alg->trunc_size == trunc_size))
233         return alg;
234     }
235   /* *INDENT-ON* */
236
237   return NULL;
238 }
239
240 static void
241 crypto_set_aead_xform (struct rte_crypto_sym_xform *xform,
242                        ipsec_sa_t * sa, u8 is_outbound)
243 {
244   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
245   crypto_alg_t *c;
246
247   c = vec_elt_at_index (dcm->cipher_algs, sa->crypto_alg);
248
249   ASSERT (c->type == RTE_CRYPTO_SYM_XFORM_AEAD);
250
251   xform->type = RTE_CRYPTO_SYM_XFORM_AEAD;
252   xform->aead.algo = c->alg;
253   xform->aead.key.data = sa->crypto_key;
254   xform->aead.key.length = c->key_len;
255   xform->aead.iv.offset =
256     crypto_op_get_priv_offset () + offsetof (dpdk_op_priv_t, cb);
257   xform->aead.iv.length = 12;
258   xform->aead.digest_length = c->trunc_size;
259   xform->aead.aad_length = sa->use_esn ? 12 : 8;
260   xform->next = NULL;
261
262   if (is_outbound)
263     xform->aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
264   else
265     xform->aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
266 }
267
268 static void
269 crypto_set_cipher_xform (struct rte_crypto_sym_xform *xform,
270                          ipsec_sa_t * sa, u8 is_outbound)
271 {
272   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
273   crypto_alg_t *c;
274
275   c = vec_elt_at_index (dcm->cipher_algs, sa->crypto_alg);
276
277   ASSERT (c->type == RTE_CRYPTO_SYM_XFORM_CIPHER);
278
279   xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
280   xform->cipher.algo = c->alg;
281   xform->cipher.key.data = sa->crypto_key;
282   xform->cipher.key.length = c->key_len;
283   xform->cipher.iv.offset =
284     crypto_op_get_priv_offset () + offsetof (dpdk_op_priv_t, cb);
285   xform->cipher.iv.length = c->iv_len;
286   xform->next = NULL;
287
288   if (is_outbound)
289     xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
290   else
291     xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
292 }
293
294 static void
295 crypto_set_auth_xform (struct rte_crypto_sym_xform *xform,
296                        ipsec_sa_t * sa, u8 is_outbound)
297 {
298   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
299   crypto_alg_t *a;
300
301   a = vec_elt_at_index (dcm->auth_algs, sa->integ_alg);
302
303   ASSERT (a->type == RTE_CRYPTO_SYM_XFORM_AUTH);
304
305   xform->type = RTE_CRYPTO_SYM_XFORM_AUTH;
306   xform->auth.algo = a->alg;
307   xform->auth.key.data = sa->integ_key;
308   xform->auth.key.length = a->key_len;
309   xform->auth.digest_length = a->trunc_size;
310   xform->next = NULL;
311
312   if (is_outbound)
313     xform->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
314   else
315     xform->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
316 }
317
318 clib_error_t *
319 create_sym_session (struct rte_cryptodev_sym_session **session,
320                     u32 sa_idx,
321                     crypto_resource_t * res,
322                     crypto_worker_main_t * cwm, u8 is_outbound)
323 {
324   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
325   ipsec_main_t *im = &ipsec_main;
326   crypto_data_t *data;
327   ipsec_sa_t *sa;
328   struct rte_crypto_sym_xform cipher_xform = { 0 };
329   struct rte_crypto_sym_xform auth_xform = { 0 };
330   struct rte_crypto_sym_xform *xfs;
331   struct rte_cryptodev_sym_session **s;
332   crypto_session_key_t key = { 0 };
333   clib_error_t *erorr = 0;
334
335   key.drv_id = res->drv_id;
336   key.sa_idx = sa_idx;
337
338   sa = pool_elt_at_index (im->sad, sa_idx);
339
340   if ((sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) |
341       (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) |
342       (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256))
343     {
344       crypto_set_aead_xform (&cipher_xform, sa, is_outbound);
345       xfs = &cipher_xform;
346     }
347   else
348     {
349       crypto_set_cipher_xform (&cipher_xform, sa, is_outbound);
350       crypto_set_auth_xform (&auth_xform, sa, is_outbound);
351
352       if (is_outbound)
353         {
354           cipher_xform.next = &auth_xform;
355           xfs = &cipher_xform;
356         }
357       else
358         {
359           auth_xform.next = &cipher_xform;
360           xfs = &auth_xform;
361         }
362     }
363
364   data = vec_elt_at_index (dcm->data, res->numa);
365   clib_spinlock_lock_if_init (&data->lockp);
366
367   /*
368    * DPDK_VER >= 1708:
369    *   Multiple worker/threads share the session for an SA
370    *   Single session per SA, initialized for each device driver
371    */
372   s = (void *) hash_get (data->session_by_sa_index, sa_idx);
373
374   if (!s)
375     {
376       session[0] = rte_cryptodev_sym_session_create (data->session_h);
377       if (!session[0])
378         {
379           data->session_h_failed += 1;
380           erorr = clib_error_return (0, "failed to create session header");
381           goto done;
382         }
383       hash_set (data->session_by_sa_index, sa_idx, session[0]);
384     }
385   else
386     session[0] = s[0];
387
388   struct rte_mempool **mp;
389   mp = vec_elt_at_index (data->session_drv, res->drv_id);
390   ASSERT (mp[0] != NULL);
391
392   i32 ret =
393     rte_cryptodev_sym_session_init (res->dev_id, session[0], xfs, mp[0]);
394   if (ret)
395     {
396       data->session_drv_failed[res->drv_id] += 1;
397       erorr = clib_error_return (0, "failed to init session for drv %u",
398                                  res->drv_id);
399       goto done;
400     }
401
402   hash_set (data->session_by_drv_id_and_sa_index, key.val, session[0]);
403
404 done:
405   clib_spinlock_unlock_if_init (&data->lockp);
406   return erorr;
407 }
408
409 static void __attribute__ ((unused)) clear_and_free_obj (void *obj)
410 {
411   struct rte_mempool *mp = rte_mempool_from_obj (obj);
412
413   memset (obj, 0, mp->elt_size);
414
415   rte_mempool_put (mp, obj);
416 }
417
418 /* This is from rte_cryptodev_pmd.h */
419 static inline void *
420 get_session_private_data (const struct rte_cryptodev_sym_session *sess,
421                           uint8_t driver_id)
422 {
423   return sess->sess_private_data[driver_id];
424 }
425
426 /* This is from rte_cryptodev_pmd.h */
427 static inline void
428 set_session_private_data (struct rte_cryptodev_sym_session *sess,
429                           uint8_t driver_id, void *private_data)
430 {
431   sess->sess_private_data[driver_id] = private_data;
432 }
433
434 static clib_error_t *
435 dpdk_crypto_session_disposal (crypto_session_disposal_t * v, u64 ts)
436 {
437   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
438   crypto_session_disposal_t *s;
439   void *drv_session;
440   u32 drv_id;
441   i32 ret;
442
443   /* *INDENT-OFF* */
444   vec_foreach (s, v)
445     {
446       /* ordered vector by timestamp */
447       if (!(s->ts + dcm->session_timeout < ts))
448         break;
449
450       vec_foreach_index (drv_id, dcm->drv)
451         {
452           drv_session = get_session_private_data (s->session, drv_id);
453           if (!drv_session)
454             continue;
455
456           /*
457            * Custom clear to avoid finding a dev_id for drv_id:
458            *  ret = rte_cryptodev_sym_session_clear (dev_id, drv_session);
459            *  ASSERT (!ret);
460            */
461           clear_and_free_obj (drv_session);
462
463           set_session_private_data (s->session, drv_id, NULL);
464         }
465
466       if (rte_mempool_from_obj(s->session))
467         {
468           ret = rte_cryptodev_sym_session_free (s->session);
469           ASSERT (!ret);
470         }
471     }
472   /* *INDENT-ON* */
473
474   if (s < vec_end (v))
475     vec_delete (v, s - v, 0);
476   else
477     vec_reset_length (v);
478
479   return 0;
480 }
481
482 static clib_error_t *
483 add_del_sa_session (u32 sa_index, u8 is_add)
484 {
485   ipsec_main_t *im = &ipsec_main;
486   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
487   crypto_data_t *data;
488   struct rte_cryptodev_sym_session *s;
489   crypto_session_key_t key = { 0 };
490   uword *val;
491   u32 drv_id;
492
493   if (is_add)
494     {
495 #if 1
496       ipsec_sa_t *sa = pool_elt_at_index (im->sad, sa_index);
497       u32 seed;
498       switch (sa->crypto_alg)
499         {
500         case IPSEC_CRYPTO_ALG_AES_GCM_128:
501         case IPSEC_CRYPTO_ALG_AES_GCM_192:
502         case IPSEC_CRYPTO_ALG_AES_GCM_256:
503           clib_memcpy (&sa->salt, &sa->crypto_key[sa->crypto_key_len - 4], 4);
504           break;
505         default:
506           seed = (u32) clib_cpu_time_now ();
507           sa->salt = random_u32 (&seed);
508         }
509 #endif
510       return 0;
511     }
512
513   key.sa_idx = sa_index;
514
515   /* *INDENT-OFF* */
516   vec_foreach (data, dcm->data)
517     {
518       clib_spinlock_lock_if_init (&data->lockp);
519       val = hash_get (data->session_by_sa_index, sa_index);
520       s = (struct rte_cryptodev_sym_session *) val;
521       if (s)
522         {
523           vec_foreach_index (drv_id, dcm->drv)
524             {
525               key.drv_id = drv_id;
526               val = hash_get (data->session_by_drv_id_and_sa_index, key.val);
527               s = (struct rte_cryptodev_sym_session *) val;
528               if (s)
529                 hash_unset (data->session_by_drv_id_and_sa_index, key.val);
530             }
531
532           hash_unset (data->session_by_sa_index, sa_index);
533
534           u64 ts = unix_time_now_nsec ();
535           dpdk_crypto_session_disposal (data->session_disposal, ts);
536
537           crypto_session_disposal_t sd;
538           sd.ts = ts;
539           sd.session = s;
540
541           vec_add1 (data->session_disposal, sd);
542         }
543       clib_spinlock_unlock_if_init (&data->lockp);
544     }
545   /* *INDENT-ON* */
546
547   return 0;
548 }
549
550 static clib_error_t *
551 dpdk_ipsec_check_support (ipsec_sa_t * sa)
552 {
553   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
554
555   if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
556     switch (sa->crypto_alg)
557       {
558       case IPSEC_CRYPTO_ALG_NONE:
559       case IPSEC_CRYPTO_ALG_AES_GCM_128:
560       case IPSEC_CRYPTO_ALG_AES_GCM_192:
561       case IPSEC_CRYPTO_ALG_AES_GCM_256:
562         break;
563       default:
564         return clib_error_return (0, "unsupported integ-alg %U crypto-alg %U",
565                                   format_ipsec_integ_alg, sa->integ_alg,
566                                   format_ipsec_crypto_alg, sa->crypto_alg);
567       }
568
569   /* XXX do we need the NONE check? */
570   if (sa->crypto_alg != IPSEC_CRYPTO_ALG_NONE &&
571       dcm->cipher_algs[sa->crypto_alg].disabled)
572     return clib_error_return (0, "disabled crypto-alg %U",
573                               format_ipsec_crypto_alg, sa->crypto_alg);
574
575   /* XXX do we need the NONE check? */
576   if (sa->integ_alg != IPSEC_INTEG_ALG_NONE &&
577       dcm->auth_algs[sa->integ_alg].disabled)
578     return clib_error_return (0, "disabled integ-alg %U",
579                               format_ipsec_integ_alg, sa->integ_alg);
580   return NULL;
581 }
582
583 static void
584 crypto_parse_capabilities (crypto_dev_t * dev,
585                            const struct rte_cryptodev_capabilities *cap,
586                            u32 n_mains)
587 {
588   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
589   crypto_alg_t *alg;
590   u8 len, inc;
591
592   for (; cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; cap++)
593     {
594       /* A single capability maps to multiple cipher/auth algorithms */
595       switch (cap->sym.xform_type)
596         {
597         case RTE_CRYPTO_SYM_XFORM_AEAD:
598         case RTE_CRYPTO_SYM_XFORM_CIPHER:
599           inc = cap->sym.cipher.key_size.increment;
600           inc = inc ? inc : 1;
601           for (len = cap->sym.cipher.key_size.min;
602                len <= cap->sym.cipher.key_size.max; len += inc)
603             {
604               alg = cipher_cap_to_alg (cap, len);
605               if (!alg)
606                 continue;
607               dev->cipher_support[cipher_alg_index (alg)] = 1;
608               alg->resources += vec_len (dev->free_resources);
609               /* At least enough resources to support one algo */
610               dcm->enabled |= (alg->resources >= n_mains);
611             }
612           break;
613         case RTE_CRYPTO_SYM_XFORM_AUTH:
614           inc = cap->sym.auth.digest_size.increment;
615           inc = inc ? inc : 1;
616           for (len = cap->sym.auth.digest_size.min;
617                len <= cap->sym.auth.digest_size.max; len += inc)
618             {
619               alg = auth_cap_to_alg (cap, len);
620               if (!alg)
621                 continue;
622               dev->auth_support[auth_alg_index (alg)] = 1;
623               alg->resources += vec_len (dev->free_resources);
624               /* At least enough resources to support one algo */
625               dcm->enabled |= (alg->resources >= n_mains);
626             }
627           break;
628         default:
629           ;
630         }
631     }
632 }
633
634 #define DPDK_CRYPTO_N_QUEUE_DESC  2048
635 #define DPDK_CRYPTO_NB_SESS_OBJS  20000
636
637 static clib_error_t *
638 crypto_dev_conf (u8 dev, u16 n_qp, u8 numa)
639 {
640   struct rte_cryptodev_config dev_conf;
641   struct rte_cryptodev_qp_conf qp_conf;
642   i32 ret;
643   u16 qp;
644   i8 *error_str;
645
646   dev_conf.socket_id = numa;
647   dev_conf.nb_queue_pairs = n_qp;
648
649   error_str = "failed to configure crypto device %u";
650   ret = rte_cryptodev_configure (dev, &dev_conf);
651   if (ret < 0)
652     return clib_error_return (0, error_str, dev);
653
654   error_str = "failed to setup crypto device %u queue pair %u";
655   qp_conf.nb_descriptors = DPDK_CRYPTO_N_QUEUE_DESC;
656   for (qp = 0; qp < n_qp; qp++)
657     {
658       ret = rte_cryptodev_queue_pair_setup (dev, qp, &qp_conf, numa, NULL);
659       if (ret < 0)
660         return clib_error_return (0, error_str, dev, qp);
661     }
662
663   return 0;
664 }
665
666 static void
667 crypto_scan_devs (u32 n_mains)
668 {
669   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
670   struct rte_cryptodev *cryptodev;
671   struct rte_cryptodev_info info;
672   crypto_dev_t *dev;
673   crypto_resource_t *res;
674   clib_error_t *error;
675   u32 i;
676   u16 max_res_idx, res_idx, j;
677   u8 drv_id;
678
679   vec_validate_init_empty (dcm->dev, rte_cryptodev_count () - 1,
680                            (crypto_dev_t) EMPTY_STRUCT);
681
682   for (i = 0; i < rte_cryptodev_count (); i++)
683     {
684       dev = vec_elt_at_index (dcm->dev, i);
685
686       cryptodev = &rte_cryptodevs[i];
687       rte_cryptodev_info_get (i, &info);
688
689       dev->id = i;
690       dev->name = cryptodev->data->name;
691       dev->numa = rte_cryptodev_socket_id (i);
692       dev->features = info.feature_flags;
693       dev->max_qp = info.max_nb_queue_pairs;
694       drv_id = info.driver_id;
695       if (drv_id >= vec_len (dcm->drv))
696         vec_validate_init_empty (dcm->drv, drv_id,
697                                  (crypto_drv_t) EMPTY_STRUCT);
698       vec_elt_at_index (dcm->drv, drv_id)->name = info.driver_name;
699       dev->drv_id = drv_id;
700       vec_add1 (vec_elt_at_index (dcm->drv, drv_id)->devs, i);
701
702       if (!(info.feature_flags & RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
703         continue;
704
705       if ((error = crypto_dev_conf (i, dev->max_qp, dev->numa)))
706         {
707           clib_error_report (error);
708           continue;
709         }
710
711       max_res_idx = (dev->max_qp / 2) - 1;
712
713       vec_validate (dev->free_resources, max_res_idx);
714
715       res_idx = vec_len (dcm->resource);
716       vec_validate_init_empty_aligned (dcm->resource, res_idx + max_res_idx,
717                                        (crypto_resource_t) EMPTY_STRUCT,
718                                        CLIB_CACHE_LINE_BYTES);
719
720       for (j = 0; j <= max_res_idx; j++, res_idx++)
721         {
722           vec_elt (dev->free_resources, max_res_idx - j) = res_idx;
723           res = &dcm->resource[res_idx];
724           res->dev_id = i;
725           res->drv_id = drv_id;
726           res->qp_id = j * 2;
727           res->numa = dev->numa;
728           res->thread_idx = (u16) ~ 0;
729         }
730
731       crypto_parse_capabilities (dev, info.capabilities, n_mains);
732     }
733 }
734
735 void
736 crypto_auto_placement (void)
737 {
738   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
739   crypto_resource_t *res;
740   crypto_worker_main_t *cwm;
741   crypto_dev_t *dev;
742   u32 thread_idx, skip_master;
743   u16 res_idx, *idx;
744   u8 used;
745   u16 i;
746
747   skip_master = vlib_num_workers () > 0;
748
749   /* *INDENT-OFF* */
750   vec_foreach (dev, dcm->dev)
751     {
752       vec_foreach_index (thread_idx, dcm->workers_main)
753         {
754           if (vec_len (dev->free_resources) == 0)
755             break;
756
757           if (thread_idx < skip_master)
758             continue;
759
760           /* Check thread is not already using the device */
761           vec_foreach (idx, dev->used_resources)
762             if (dcm->resource[idx[0]].thread_idx == thread_idx)
763               continue;
764
765           cwm = vec_elt_at_index (dcm->workers_main, thread_idx);
766
767           used = 0;
768           res_idx = vec_pop (dev->free_resources);
769
770           /* Set device only for supported algos */
771           for (i = 0; i < IPSEC_CRYPTO_N_ALG; i++)
772             if (dev->cipher_support[i] &&
773                 cwm->cipher_resource_idx[i] == (u16) ~0)
774               {
775                 dcm->cipher_algs[i].disabled--;
776                 cwm->cipher_resource_idx[i] = res_idx;
777                 used = 1;
778               }
779
780           for (i = 0; i < IPSEC_INTEG_N_ALG; i++)
781             if (dev->auth_support[i] &&
782                 cwm->auth_resource_idx[i] == (u16) ~0)
783               {
784                 dcm->auth_algs[i].disabled--;
785                 cwm->auth_resource_idx[i] = res_idx;
786                 used = 1;
787               }
788
789           if (!used)
790             {
791               vec_add1 (dev->free_resources, res_idx);
792               continue;
793             }
794
795           vec_add1 (dev->used_resources, res_idx);
796
797           res = vec_elt_at_index (dcm->resource, res_idx);
798
799           ASSERT (res->thread_idx == (u16) ~0);
800           res->thread_idx = thread_idx;
801
802           /* Add device to vector of polling resources */
803           vec_add1 (cwm->resource_idx, res_idx);
804         }
805     }
806   /* *INDENT-ON* */
807 }
808
809 static void
810 crypto_op_init (struct rte_mempool *mempool,
811                 void *_arg __attribute__ ((unused)),
812                 void *_obj, unsigned i __attribute__ ((unused)))
813 {
814   struct rte_crypto_op *op = _obj;
815
816   op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
817   op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
818   op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
819   op->phys_addr = rte_mempool_virt2iova (_obj);
820   op->mempool = mempool;
821 }
822
823 static clib_error_t *
824 crypto_create_crypto_op_pool (vlib_main_t * vm, u8 numa)
825 {
826   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
827   dpdk_config_main_t *conf = &dpdk_config_main;
828   crypto_data_t *data;
829   u8 *pool_name;
830   u32 pool_priv_size = sizeof (struct rte_crypto_op_pool_private);
831   struct rte_crypto_op_pool_private *priv;
832   struct rte_mempool *mp;
833   clib_error_t *error = NULL;
834   vlib_physmem_region_index_t pri;
835
836   data = vec_elt_at_index (dcm->data, numa);
837
838   /* Already allocated */
839   if (data->crypto_op)
840     return NULL;
841
842   pool_name = format (0, "crypto_pool_numa%u%c", numa, 0);
843
844   error =
845     dpdk_pool_create (vm, pool_name, crypto_op_len (), conf->num_mbufs,
846                       pool_priv_size, 512, numa, &mp, &pri);
847
848   vec_free (pool_name);
849
850   if (error)
851     return error;
852
853   /* Initialize mempool private data */
854   priv = rte_mempool_get_priv (mp);
855   priv->priv_size = pool_priv_size;
856   priv->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
857
858   /* call the object initializers */
859   rte_mempool_obj_iter (mp, crypto_op_init, 0);
860
861   data->crypto_op = mp;
862
863   return NULL;
864 }
865
866 static clib_error_t *
867 crypto_create_session_h_pool (vlib_main_t * vm, u8 numa)
868 {
869   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
870   crypto_data_t *data;
871   u8 *pool_name;
872   struct rte_mempool *mp;
873   clib_error_t *error = NULL;
874   vlib_physmem_region_index_t pri;
875   u32 elt_size;
876
877   data = vec_elt_at_index (dcm->data, numa);
878
879   if (data->session_h)
880     return NULL;
881
882   pool_name = format (0, "session_h_pool_numa%u%c", numa, 0);
883
884   elt_size = rte_cryptodev_get_header_session_size ();
885
886   error =
887     dpdk_pool_create (vm, pool_name, elt_size, DPDK_CRYPTO_NB_SESS_OBJS,
888                       0, 512, numa, &mp, &pri);
889
890   vec_free (pool_name);
891
892   if (error)
893     return error;
894
895   data->session_h = mp;
896
897   return NULL;
898 }
899
900 static clib_error_t *
901 crypto_create_session_drv_pool (vlib_main_t * vm, crypto_dev_t * dev)
902 {
903   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
904   crypto_data_t *data;
905   u8 *pool_name;
906   struct rte_mempool *mp;
907   clib_error_t *error = NULL;
908   vlib_physmem_region_index_t pri;
909   u32 elt_size;
910   u8 numa = dev->numa;
911
912   data = vec_elt_at_index (dcm->data, numa);
913
914   vec_validate (data->session_drv, dev->drv_id);
915   vec_validate (data->session_drv_failed, dev->drv_id);
916
917   if (data->session_drv[dev->drv_id])
918     return NULL;
919
920   pool_name = format (0, "session_drv%u_pool_numa%u%c", dev->drv_id, numa, 0);
921   elt_size = rte_cryptodev_get_private_session_size (dev->id);
922
923   error =
924     dpdk_pool_create (vm, pool_name, elt_size, DPDK_CRYPTO_NB_SESS_OBJS,
925                       0, 512, numa, &mp, &pri);
926
927   vec_free (pool_name);
928
929   if (error)
930     return error;
931
932   data->session_drv[dev->drv_id] = mp;
933   clib_spinlock_init (&data->lockp);
934
935   return NULL;
936 }
937
938 static clib_error_t *
939 crypto_create_pools (vlib_main_t * vm)
940 {
941   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
942   clib_error_t *error = NULL;
943   crypto_dev_t *dev;
944
945   /* *INDENT-OFF* */
946   vec_foreach (dev, dcm->dev)
947     {
948       vec_validate_aligned (dcm->data, dev->numa, CLIB_CACHE_LINE_BYTES);
949
950       error = crypto_create_crypto_op_pool (vm, dev->numa);
951       if (error)
952         return error;
953
954       error = crypto_create_session_h_pool (vm, dev->numa);
955       if (error)
956         return error;
957
958       error = crypto_create_session_drv_pool (vm, dev);
959       if (error)
960         return error;
961     }
962   /* *INDENT-ON* */
963
964   return NULL;
965 }
966
967 static void
968 crypto_disable (void)
969 {
970   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
971   crypto_data_t *data;
972   u8 i;
973
974   dcm->enabled = 0;
975
976   /* *INDENT-OFF* */
977   vec_foreach (data, dcm->data)
978     {
979       rte_mempool_free (data->crypto_op);
980       rte_mempool_free (data->session_h);
981
982       vec_foreach_index (i, data->session_drv)
983         rte_mempool_free (data->session_drv[i]);
984
985       vec_free (data->session_drv);
986       clib_spinlock_free (&data->lockp);
987     }
988   /* *INDENT-ON* */
989
990   vec_free (dcm->data);
991   vec_free (dcm->workers_main);
992   vec_free (dcm->sa_session);
993   vec_free (dcm->dev);
994   vec_free (dcm->resource);
995   vec_free (dcm->cipher_algs);
996   vec_free (dcm->auth_algs);
997 }
998
999 static uword
1000 dpdk_ipsec_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1001                     vlib_frame_t * f)
1002 {
1003   ipsec_main_t *im = &ipsec_main;
1004   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
1005   vlib_thread_main_t *tm = vlib_get_thread_main ();
1006   crypto_worker_main_t *cwm;
1007   clib_error_t *error = NULL;
1008   u32 i, skip_master, n_mains;
1009
1010   n_mains = tm->n_vlib_mains;
1011   skip_master = vlib_num_workers () > 0;
1012
1013   algos_init (n_mains - skip_master);
1014
1015   crypto_scan_devs (n_mains - skip_master);
1016
1017   if (!(dcm->enabled))
1018     {
1019       clib_warning ("not enough DPDK crypto resources, default to OpenSSL");
1020       crypto_disable ();
1021       return 0;
1022     }
1023
1024   dcm->session_timeout = 10e9;
1025
1026   vec_validate_init_empty_aligned (dcm->workers_main, n_mains - 1,
1027                                    (crypto_worker_main_t) EMPTY_STRUCT,
1028                                    CLIB_CACHE_LINE_BYTES);
1029
1030   /* *INDENT-OFF* */
1031   vec_foreach (cwm, dcm->workers_main)
1032     {
1033       vec_validate_init_empty_aligned (cwm->ops, VLIB_FRAME_SIZE - 1, 0,
1034                                        CLIB_CACHE_LINE_BYTES);
1035       memset (cwm->cipher_resource_idx, ~0,
1036               IPSEC_CRYPTO_N_ALG * sizeof(*cwm->cipher_resource_idx));
1037       memset (cwm->auth_resource_idx, ~0,
1038               IPSEC_INTEG_N_ALG * sizeof(*cwm->auth_resource_idx));
1039     }
1040   /* *INDENT-ON* */
1041
1042   crypto_auto_placement ();
1043
1044   error = crypto_create_pools (vm);
1045   if (error)
1046     {
1047       clib_error_report (error);
1048       crypto_disable ();
1049       return 0;
1050     }
1051
1052   /* Add new next node and set it as default */
1053   vlib_node_t *node, *next_node;
1054
1055   next_node = vlib_get_node_by_name (vm, (u8 *) "dpdk-esp-encrypt");
1056   ASSERT (next_node);
1057   node = vlib_get_node_by_name (vm, (u8 *) "ipsec-output-ip4");
1058   ASSERT (node);
1059   im->esp_encrypt_node_index = next_node->index;
1060   im->esp_encrypt_next_index =
1061     vlib_node_add_next (vm, node->index, next_node->index);
1062
1063   next_node = vlib_get_node_by_name (vm, (u8 *) "dpdk-esp-decrypt");
1064   ASSERT (next_node);
1065   node = vlib_get_node_by_name (vm, (u8 *) "ipsec-input-ip4");
1066   ASSERT (node);
1067   im->esp_decrypt_node_index = next_node->index;
1068   im->esp_decrypt_next_index =
1069     vlib_node_add_next (vm, node->index, next_node->index);
1070
1071   im->cb.check_support_cb = dpdk_ipsec_check_support;
1072   im->cb.add_del_sa_sess_cb = add_del_sa_session;
1073
1074   node = vlib_get_node_by_name (vm, (u8 *) "dpdk-crypto-input");
1075   ASSERT (node);
1076   for (i = skip_master; i < n_mains; i++)
1077     vlib_node_set_state (vlib_mains[i], node->index, VLIB_NODE_STATE_POLLING);
1078   return 0;
1079 }
1080
1081 /* *INDENT-OFF* */
1082 VLIB_REGISTER_NODE (dpdk_ipsec_process_node,static) = {
1083     .function = dpdk_ipsec_process,
1084     .type = VLIB_NODE_TYPE_PROCESS,
1085     .name = "dpdk-ipsec-process",
1086     .process_log2_n_stack_bytes = 17,
1087 };
1088 /* *INDENT-ON* */
1089
1090 /*
1091  * fd.io coding-style-patch-verification: ON
1092  *
1093  * Local Variables:
1094  * eval: (c-set-style "gnu")
1095  * End:
1096  */