crypto: encrypt/decrypt queues sw_scheduler
[vpp.git] / src / vnet / crypto / crypto.c
1 /*
2  * Copyright (c) 2018 Cisco 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
16 #include <stdbool.h>
17 #include <vlib/vlib.h>
18 #include <vnet/crypto/crypto.h>
19
20 vnet_crypto_main_t crypto_main;
21
22 static_always_inline void
23 crypto_set_op_status (vnet_crypto_op_t * ops[], u32 n_ops, int status)
24 {
25   while (n_ops--)
26     {
27       ops[0]->status = status;
28       ops++;
29     }
30 }
31
32 static_always_inline u32
33 vnet_crypto_process_ops_call_handler (vlib_main_t * vm,
34                                       vnet_crypto_main_t * cm,
35                                       vnet_crypto_op_id_t opt,
36                                       vnet_crypto_op_t * ops[],
37                                       vnet_crypto_op_chunk_t * chunks,
38                                       u32 n_ops)
39 {
40   u32 rv = 0;
41   if (n_ops == 0)
42     return 0;
43
44   if (chunks)
45     {
46
47       if (cm->chained_ops_handlers[opt] == 0)
48         crypto_set_op_status (ops, n_ops,
49                               VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER);
50       else
51         rv = (cm->chained_ops_handlers[opt]) (vm, ops, chunks, n_ops);
52     }
53   else
54     {
55       if (cm->ops_handlers[opt] == 0)
56         crypto_set_op_status (ops, n_ops,
57                               VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER);
58       else
59         rv = (cm->ops_handlers[opt]) (vm, ops, n_ops);
60     }
61   return rv;
62 }
63
64 static_always_inline u32
65 vnet_crypto_process_ops_inline (vlib_main_t * vm, vnet_crypto_op_t ops[],
66                                 vnet_crypto_op_chunk_t * chunks, u32 n_ops)
67 {
68   vnet_crypto_main_t *cm = &crypto_main;
69   const int op_q_size = VLIB_FRAME_SIZE;
70   vnet_crypto_op_t *op_queue[op_q_size];
71   vnet_crypto_op_id_t opt, current_op_type = ~0;
72   u32 n_op_queue = 0;
73   u32 rv = 0, i;
74
75   ASSERT (n_ops >= 1);
76
77   for (i = 0; i < n_ops; i++)
78     {
79       opt = ops[i].op;
80
81       if (current_op_type != opt || n_op_queue >= op_q_size)
82         {
83           rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
84                                                       op_queue, chunks,
85                                                       n_op_queue);
86           n_op_queue = 0;
87           current_op_type = opt;
88         }
89
90       op_queue[n_op_queue++] = &ops[i];
91     }
92
93   rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
94                                               op_queue, chunks, n_op_queue);
95   return rv;
96 }
97
98 u32
99 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
100 {
101   return vnet_crypto_process_ops_inline (vm, ops, 0, n_ops);
102 }
103
104 u32
105 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
106                                  vnet_crypto_op_chunk_t * chunks, u32 n_ops)
107 {
108   return vnet_crypto_process_ops_inline (vm, ops, chunks, n_ops);
109 }
110
111 u32
112 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
113                              char *desc)
114 {
115   vnet_crypto_main_t *cm = &crypto_main;
116   vnet_crypto_engine_t *p;
117
118   vec_add2 (cm->engines, p, 1);
119   p->name = name;
120   p->desc = desc;
121   p->priority = prio;
122
123   hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
124
125   return p - cm->engines;
126 }
127
128 static_always_inline void
129 crypto_set_active_engine (vnet_crypto_op_data_t * od,
130                           vnet_crypto_op_id_t id, u32 ei,
131                           crypto_op_class_type_t oct)
132 {
133   vnet_crypto_main_t *cm = &crypto_main;
134   vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
135
136   if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_CHAINED)
137     {
138       if (ce->chained_ops_handlers[id])
139         {
140           od->active_engine_index_chained = ei;
141           cm->chained_ops_handlers[id] = ce->chained_ops_handlers[id];
142         }
143     }
144
145   if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_SIMPLE)
146     {
147       if (ce->ops_handlers[id])
148         {
149           od->active_engine_index_simple = ei;
150           cm->ops_handlers[id] = ce->ops_handlers[id];
151         }
152     }
153 }
154
155 int
156 vnet_crypto_set_handler2 (char *alg_name, char *engine,
157                           crypto_op_class_type_t oct)
158 {
159   uword *p;
160   vnet_crypto_main_t *cm = &crypto_main;
161   vnet_crypto_alg_data_t *ad;
162   int i;
163
164   p = hash_get_mem (cm->alg_index_by_name, alg_name);
165   if (!p)
166     return -1;
167
168   ad = vec_elt_at_index (cm->algs, p[0]);
169
170   p = hash_get_mem (cm->engine_index_by_name, engine);
171   if (!p)
172     return -1;
173
174   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
175     {
176       vnet_crypto_op_data_t *od;
177       vnet_crypto_op_id_t id = ad->op_by_type[i];
178       if (id == 0)
179         continue;
180
181       od = cm->opt_data + id;
182       crypto_set_active_engine (od, id, p[0], oct);
183     }
184
185   return 0;
186 }
187
188 int
189 vnet_crypto_is_set_handler (vnet_crypto_alg_t alg)
190 {
191   vnet_crypto_main_t *cm = &crypto_main;
192   vnet_crypto_op_id_t opt = 0;
193   int i;
194
195   if (alg > vec_len (cm->algs))
196     return 0;
197
198   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
199     if ((opt = cm->algs[alg].op_by_type[i]) != 0)
200       break;
201
202   return NULL != cm->ops_handlers[opt];
203 }
204
205 void
206 vnet_crypto_register_ops_handler_inline (vlib_main_t * vm, u32 engine_index,
207                                          vnet_crypto_op_id_t opt,
208                                          vnet_crypto_ops_handler_t * fn,
209                                          vnet_crypto_chained_ops_handler_t *
210                                          cfn)
211 {
212   vnet_crypto_main_t *cm = &crypto_main;
213   vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
214   vnet_crypto_op_data_t *otd = cm->opt_data + opt;
215   vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
216                         CLIB_CACHE_LINE_BYTES);
217   vec_validate_aligned (cm->chained_ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
218                         CLIB_CACHE_LINE_BYTES);
219
220   if (fn)
221     {
222       e->ops_handlers[opt] = fn;
223       if (otd->active_engine_index_simple == ~0)
224         {
225           otd->active_engine_index_simple = engine_index;
226           cm->ops_handlers[opt] = fn;
227         }
228
229       ae = vec_elt_at_index (cm->engines, otd->active_engine_index_simple);
230       if (ae->priority < e->priority)
231         crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_SIMPLE);
232     }
233
234   if (cfn)
235     {
236       e->chained_ops_handlers[opt] = cfn;
237       if (otd->active_engine_index_chained == ~0)
238         {
239           otd->active_engine_index_chained = engine_index;
240           cm->chained_ops_handlers[opt] = cfn;
241         }
242
243       ae = vec_elt_at_index (cm->engines, otd->active_engine_index_chained);
244       if (ae->priority < e->priority)
245         crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_CHAINED);
246     }
247
248   return;
249 }
250
251 void
252 vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
253                                   vnet_crypto_op_id_t opt,
254                                   vnet_crypto_ops_handler_t * fn)
255 {
256   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, 0);
257 }
258
259 void
260 vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, u32 engine_index,
261                                           vnet_crypto_op_id_t opt,
262                                           vnet_crypto_chained_ops_handler_t *
263                                           fn)
264 {
265   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, 0, fn);
266 }
267
268 void
269 vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
270                                    vnet_crypto_op_id_t opt,
271                                    vnet_crypto_ops_handler_t * fn,
272                                    vnet_crypto_chained_ops_handler_t * cfn)
273 {
274   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, cfn);
275 }
276
277 void
278 vnet_crypto_register_enqueue_handler (vlib_main_t *vm, u32 engine_index,
279                                       vnet_crypto_async_op_id_t opt,
280                                       vnet_crypto_frame_enqueue_t *enqueue_hdl)
281 {
282   vnet_crypto_main_t *cm = &crypto_main;
283   vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
284   vnet_crypto_async_op_data_t *otd = cm->async_opt_data + opt;
285   vec_validate_aligned (cm->enqueue_handlers, VNET_CRYPTO_ASYNC_OP_N_IDS,
286                         CLIB_CACHE_LINE_BYTES);
287   vec_validate_aligned (cm->dequeue_handlers, VNET_CRYPTO_ASYNC_OP_N_IDS,
288                         CLIB_CACHE_LINE_BYTES);
289
290   if (!enqueue_hdl)
291     return;
292
293   e->enqueue_handlers[opt] = enqueue_hdl;
294   if (otd->active_engine_index_async == ~0)
295     {
296       otd->active_engine_index_async = engine_index;
297       cm->enqueue_handlers[opt] = enqueue_hdl;
298     }
299
300   ae = vec_elt_at_index (cm->engines, otd->active_engine_index_async);
301   if (ae->priority <= e->priority)
302     {
303       otd->active_engine_index_async = engine_index;
304       cm->enqueue_handlers[opt] = enqueue_hdl;
305     }
306
307   return;
308 }
309
310 static int
311 engine_index_cmp (void *v1, void *v2)
312 {
313   u32 *a1 = v1;
314   u32 *a2 = v2;
315
316   if (*a1 > *a2)
317     return 1;
318   if (*a1 < *a2)
319     return -1;
320   return 0;
321 }
322
323 static void
324 vnet_crypto_update_cm_dequeue_handlers (void)
325 {
326   vnet_crypto_main_t *cm = &crypto_main;
327   vnet_crypto_async_op_data_t *otd;
328   vnet_crypto_engine_t *e;
329   u32 *active_engines = 0, *ei, last_ei = ~0, i;
330
331   vec_reset_length (cm->dequeue_handlers);
332
333   for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_IDS; i++)
334     {
335       otd = cm->async_opt_data + i;
336       e = cm->engines + otd->active_engine_index_async;
337       if (!e->dequeue_handler)
338         continue;
339       vec_add1 (active_engines, otd->active_engine_index_async);
340     }
341
342   vec_sort_with_function (active_engines, engine_index_cmp);
343
344   vec_foreach (ei, active_engines)
345     {
346       if (ei[0] == last_ei)
347         continue;
348
349       e = cm->engines + ei[0];
350       vec_add1 (cm->dequeue_handlers, e->dequeue_handler);
351       last_ei = ei[0];
352     }
353
354   vec_free (active_engines);
355 }
356
357 void
358 vnet_crypto_register_dequeue_handler (vlib_main_t *vm, u32 engine_index,
359                                       vnet_crypto_frame_dequeue_t *deq_fn)
360 {
361   vnet_crypto_main_t *cm = &crypto_main;
362   vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
363
364   if (!deq_fn)
365     return;
366
367   e->dequeue_handler = deq_fn;
368
369   return;
370 }
371
372 void
373 vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
374                                   vnet_crypto_key_handler_t * key_handler)
375 {
376   vnet_crypto_main_t *cm = &crypto_main;
377   vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
378   e->key_op_handler = key_handler;
379   return;
380 }
381
382 static int
383 vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length)
384 {
385   switch (alg)
386     {
387     case VNET_CRYPTO_N_ALGS:
388       return 0;
389     case VNET_CRYPTO_ALG_NONE:
390       return 1;
391
392 #define _(n, s, l) \
393       case VNET_CRYPTO_ALG_##n: \
394         if ((l) == length) \
395           return 1;        \
396         break;
397       foreach_crypto_cipher_alg foreach_crypto_aead_alg
398 #undef _
399         /* HMAC allows any key length */
400 #define _(n, s) \
401       case VNET_CRYPTO_ALG_HMAC_##n: \
402         return 1;
403         foreach_crypto_hmac_alg
404 #undef _
405
406 #define _(n, s)                                                               \
407   case VNET_CRYPTO_ALG_HASH_##n:                                              \
408     return 1;
409           foreach_crypto_hash_alg
410 #undef _
411     }
412
413   return 0;
414 }
415
416 u32
417 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, u8 * data,
418                      u16 length)
419 {
420   u32 index;
421   vnet_crypto_main_t *cm = &crypto_main;
422   vnet_crypto_engine_t *engine;
423   vnet_crypto_key_t *key;
424
425   u8 need_barrier_sync = 0;
426
427   if (!vnet_crypto_key_len_check (alg, length))
428     return ~0;
429
430   pool_get_aligned_will_expand (cm->keys, need_barrier_sync,
431                                 CLIB_CACHE_LINE_BYTES);
432   /* If the cm->keys will expand, stop the parade. */
433   if (need_barrier_sync)
434     vlib_worker_thread_barrier_sync (vm);
435
436   pool_get_zero (cm->keys, key);
437
438   if (need_barrier_sync)
439     vlib_worker_thread_barrier_release (vm);
440
441   index = key - cm->keys;
442   key->type = VNET_CRYPTO_KEY_TYPE_DATA;
443   key->alg = alg;
444   vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES);
445   clib_memcpy (key->data, data, length);
446   /* *INDENT-OFF* */
447   vec_foreach (engine, cm->engines)
448     if (engine->key_op_handler)
449       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
450   /* *INDENT-ON* */
451   return index;
452 }
453
454 void
455 vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index)
456 {
457   vnet_crypto_main_t *cm = &crypto_main;
458   vnet_crypto_engine_t *engine;
459   vnet_crypto_key_t *key = pool_elt_at_index (cm->keys, index);
460
461   /* *INDENT-OFF* */
462   vec_foreach (engine, cm->engines)
463     if (engine->key_op_handler)
464       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index);
465   /* *INDENT-ON* */
466
467   if (key->type == VNET_CRYPTO_KEY_TYPE_DATA)
468     {
469       clib_memset (key->data, 0, vec_len (key->data));
470       vec_free (key->data);
471     }
472   else if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
473     {
474       key->index_crypto = key->index_integ = 0;
475     }
476
477   pool_put (cm->keys, key);
478 }
479
480 vnet_crypto_async_alg_t
481 vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
482                        vnet_crypto_alg_t integ_alg)
483 {
484 #define _(c, h, s, k ,d) \
485   if (crypto_alg == VNET_CRYPTO_ALG_##c && \
486       integ_alg == VNET_CRYPTO_ALG_HMAC_##h) \
487     return VNET_CRYPTO_ALG_##c##_##h##_TAG##d;
488   foreach_crypto_link_async_alg
489 #undef _
490     return ~0;
491 }
492
493 u32
494 vnet_crypto_key_add_linked (vlib_main_t * vm,
495                             vnet_crypto_key_index_t index_crypto,
496                             vnet_crypto_key_index_t index_integ)
497 {
498   u32 index;
499   vnet_crypto_main_t *cm = &crypto_main;
500   vnet_crypto_engine_t *engine;
501   vnet_crypto_key_t *key_crypto, *key_integ, *key;
502   vnet_crypto_async_alg_t linked_alg;
503
504   key_crypto = pool_elt_at_index (cm->keys, index_crypto);
505   key_integ = pool_elt_at_index (cm->keys, index_integ);
506
507   linked_alg = vnet_crypto_link_algs (key_crypto->alg, key_integ->alg);
508   if (linked_alg == ~0)
509     return ~0;
510
511   pool_get_zero (cm->keys, key);
512   index = key - cm->keys;
513   key->type = VNET_CRYPTO_KEY_TYPE_LINK;
514   key->index_crypto = index_crypto;
515   key->index_integ = index_integ;
516   key->async_alg = linked_alg;
517
518   /* *INDENT-OFF* */
519   vec_foreach (engine, cm->engines)
520     if (engine->key_op_handler)
521       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
522   /* *INDENT-ON* */
523
524   return index;
525 }
526
527 clib_error_t *
528 crypto_dispatch_enable_disable (int is_enable)
529 {
530   vnet_crypto_main_t *cm = &crypto_main;
531   vlib_thread_main_t *tm = vlib_get_thread_main ();
532   u32 skip_master = vlib_num_workers () > 0, i;
533   vlib_node_state_t state = VLIB_NODE_STATE_DISABLED;
534   u8 state_change = 0;
535
536   CLIB_MEMORY_STORE_BARRIER ();
537   if (is_enable && cm->async_refcnt > 0)
538     {
539       state_change = 1;
540       state =
541         cm->dispatch_mode ==
542         VNET_CRYPTO_ASYNC_DISPATCH_POLLING ? VLIB_NODE_STATE_POLLING :
543         VLIB_NODE_STATE_INTERRUPT;
544     }
545
546   if (!is_enable && cm->async_refcnt == 0)
547     {
548       state_change = 1;
549       state = VLIB_NODE_STATE_DISABLED;
550     }
551
552   if (state_change)
553     for (i = skip_master; i < tm->n_vlib_mains; i++)
554       {
555         vlib_main_t *ovm = vlib_get_main_by_index (i);
556         if (state != vlib_node_get_state (ovm, cm->crypto_node_index))
557           vlib_node_set_state (ovm, cm->crypto_node_index, state);
558       }
559   return 0;
560 }
561
562 static_always_inline void
563 crypto_set_active_async_engine (vnet_crypto_async_op_data_t * od,
564                                 vnet_crypto_async_op_id_t id, u32 ei)
565 {
566   vnet_crypto_main_t *cm = &crypto_main;
567   vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
568
569   if (ce->enqueue_handlers[id] && ce->dequeue_handler)
570     {
571       od->active_engine_index_async = ei;
572       cm->enqueue_handlers[id] = ce->enqueue_handlers[id];
573       cm->dequeue_handlers[id] = ce->dequeue_handler;
574     }
575 }
576
577 int
578 vnet_crypto_set_async_handler2 (char *alg_name, char *engine)
579 {
580   uword *p;
581   vnet_crypto_main_t *cm = &crypto_main;
582   vnet_crypto_async_alg_data_t *ad;
583   int i;
584
585   if (cm->async_refcnt)
586     return -EBUSY;
587
588   p = hash_get_mem (cm->async_alg_index_by_name, alg_name);
589   if (!p)
590     return -1;
591
592   ad = vec_elt_at_index (cm->async_algs, p[0]);
593
594   p = hash_get_mem (cm->engine_index_by_name, engine);
595   if (!p)
596     return -1;
597
598   for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_TYPES; i++)
599     {
600       vnet_crypto_async_op_data_t *od;
601       vnet_crypto_async_op_id_t id = ad->op_by_type[i];
602       if (id == 0)
603         continue;
604
605       od = cm->async_opt_data + id;
606       crypto_set_active_async_engine (od, id, p[0]);
607     }
608
609   vnet_crypto_update_cm_dequeue_handlers ();
610
611   return 0;
612 }
613
614 u32
615 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name)
616 {
617   vnet_crypto_main_t *cm = &crypto_main;
618   vnet_crypto_async_next_node_t *nn = 0;
619   vlib_node_t *cc, *pn;
620   uword index = vec_len (cm->next_nodes);
621
622   pn = vlib_get_node_by_name (vm, (u8 *) post_node_name);
623   if (!pn)
624     return ~0;
625
626   /* *INDENT-OFF* */
627   vec_foreach (cm->next_nodes, nn)
628   {
629     if (nn->node_idx == pn->index)
630       return nn->next_idx;
631   }
632   /* *INDENT-ON* */
633
634   vec_validate (cm->next_nodes, index);
635   nn = vec_elt_at_index (cm->next_nodes, index);
636
637   cc = vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch");
638   nn->next_idx = vlib_node_add_named_next (vm, cc->index, post_node_name);
639   nn->node_idx = pn->index;
640
641   return nn->next_idx;
642 }
643
644 void
645 vnet_crypto_request_async_mode (int is_enable)
646 {
647   vnet_crypto_main_t *cm = &crypto_main;
648   vlib_thread_main_t *tm = vlib_get_thread_main ();
649   u32 skip_master = vlib_num_workers () > 0, i;
650   vlib_node_state_t state = VLIB_NODE_STATE_DISABLED;
651   u8 state_change = 0;
652
653   CLIB_MEMORY_STORE_BARRIER ();
654   if (is_enable && cm->async_refcnt == 0)
655     {
656       state_change = 1;
657       state =
658         cm->dispatch_mode == VNET_CRYPTO_ASYNC_DISPATCH_POLLING ?
659         VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_INTERRUPT;
660     }
661   if (!is_enable && cm->async_refcnt == 1)
662     {
663       state_change = 1;
664       state = VLIB_NODE_STATE_DISABLED;
665     }
666
667   if (state_change)
668     {
669
670       for (i = skip_master; i < tm->n_vlib_mains; i++)
671         {
672           vlib_main_t *ovm = vlib_get_main_by_index (i);
673           if (state != vlib_node_get_state (ovm, cm->crypto_node_index))
674             vlib_node_set_state (ovm, cm->crypto_node_index, state);
675         }
676
677       if (is_enable)
678         vnet_crypto_update_cm_dequeue_handlers ();
679     }
680
681   if (is_enable)
682     cm->async_refcnt += 1;
683   else if (cm->async_refcnt > 0)
684     cm->async_refcnt -= 1;
685 }
686
687 void
688 vnet_crypto_set_async_dispatch_mode (u8 mode)
689 {
690   vnet_crypto_main_t *cm = &crypto_main;
691   u32 skip_master = vlib_num_workers () > 0, i;
692   vlib_thread_main_t *tm = vlib_get_thread_main ();
693   vlib_node_state_t state = VLIB_NODE_STATE_DISABLED;
694
695   CLIB_MEMORY_STORE_BARRIER ();
696   cm->dispatch_mode = mode;
697   if (mode == VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT)
698     {
699       state =
700         cm->async_refcnt == 0 ?
701         VLIB_NODE_STATE_DISABLED : VLIB_NODE_STATE_INTERRUPT;
702     }
703   else if (mode == VNET_CRYPTO_ASYNC_DISPATCH_POLLING)
704     {
705       state =
706         cm->async_refcnt == 0 ?
707         VLIB_NODE_STATE_DISABLED : VLIB_NODE_STATE_POLLING;
708     }
709
710   for (i = skip_master; i < tm->n_vlib_mains; i++)
711     {
712       vlib_main_t *ovm = vlib_get_main_by_index (i);
713       if (state != vlib_node_get_state (ovm, cm->crypto_node_index))
714         vlib_node_set_state (ovm, cm->crypto_node_index, state);
715     }
716 }
717
718 int
719 vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t op)
720 {
721   vnet_crypto_main_t *cm = &crypto_main;
722
723   return (op < vec_len (cm->enqueue_handlers) &&
724           NULL != cm->enqueue_handlers[op]);
725 }
726
727 static void
728 vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
729                               vnet_crypto_op_id_t did, char *name, u8 is_aead)
730 {
731   vnet_crypto_op_type_t eopt, dopt;
732   vnet_crypto_main_t *cm = &crypto_main;
733
734   cm->algs[alg].name = name;
735   cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
736   cm->opt_data[eid].active_engine_index_simple = ~0;
737   cm->opt_data[did].active_engine_index_simple = ~0;
738   cm->opt_data[eid].active_engine_index_chained = ~0;
739   cm->opt_data[did].active_engine_index_chained = ~0;
740   if (is_aead)
741     {
742       eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
743       dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
744     }
745   else
746     {
747       eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
748       dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
749     }
750   cm->opt_data[eid].type = eopt;
751   cm->opt_data[did].type = dopt;
752   cm->algs[alg].op_by_type[eopt] = eid;
753   cm->algs[alg].op_by_type[dopt] = did;
754   hash_set_mem (cm->alg_index_by_name, name, alg);
755 }
756
757 static void
758 vnet_crypto_init_hash_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t id,
759                             char *name)
760 {
761   vnet_crypto_main_t *cm = &crypto_main;
762   cm->algs[alg].name = name;
763   cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HASH] = id;
764   cm->opt_data[id].alg = alg;
765   cm->opt_data[id].active_engine_index_simple = ~0;
766   cm->opt_data[id].active_engine_index_chained = ~0;
767   cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HASH;
768   hash_set_mem (cm->alg_index_by_name, name, alg);
769 }
770
771 static void
772 vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
773                             vnet_crypto_op_id_t id, char *name)
774 {
775   vnet_crypto_main_t *cm = &crypto_main;
776   cm->algs[alg].name = name;
777   cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
778   cm->opt_data[id].alg = alg;
779   cm->opt_data[id].active_engine_index_simple = ~0;
780   cm->opt_data[id].active_engine_index_chained = ~0;
781   cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
782   hash_set_mem (cm->alg_index_by_name, name, alg);
783 }
784
785 static void
786 vnet_crypto_init_async_data (vnet_crypto_async_alg_t alg,
787                              vnet_crypto_async_op_id_t eid,
788                              vnet_crypto_async_op_id_t did, char *name)
789 {
790   vnet_crypto_main_t *cm = &crypto_main;
791
792   cm->async_algs[alg].name = name;
793   cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT] = eid;
794   cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT] = did;
795   cm->async_opt_data[eid].type = VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT;
796   cm->async_opt_data[eid].alg = alg;
797   cm->async_opt_data[eid].active_engine_index_async = ~0;
798   cm->async_opt_data[eid].active_engine_index_async = ~0;
799   cm->async_opt_data[did].type = VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT;
800   cm->async_opt_data[did].alg = alg;
801   cm->async_opt_data[did].active_engine_index_async = ~0;
802   cm->async_opt_data[did].active_engine_index_async = ~0;
803   hash_set_mem (cm->async_alg_index_by_name, name, alg);
804 }
805
806 clib_error_t *
807 vnet_crypto_init (vlib_main_t * vm)
808 {
809   vnet_crypto_main_t *cm = &crypto_main;
810   vlib_thread_main_t *tm = vlib_get_thread_main ();
811   vnet_crypto_thread_t *ct = 0;
812
813   cm->dispatch_mode = VNET_CRYPTO_ASYNC_DISPATCH_POLLING;
814   cm->engine_index_by_name = hash_create_string ( /* size */ 0,
815                                                  sizeof (uword));
816   cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
817   cm->async_alg_index_by_name = hash_create_string (0, sizeof (uword));
818   vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
819   vec_foreach (ct, cm->threads)
820     pool_alloc_aligned (ct->frame_pool, VNET_CRYPTO_FRAME_POOL_SIZE,
821                         CLIB_CACHE_LINE_BYTES);
822   vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
823   vec_validate (cm->async_algs, VNET_CRYPTO_N_ASYNC_ALGS);
824
825 #define _(n, s, l) \
826   vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
827                                 VNET_CRYPTO_OP_##n##_ENC, \
828                                 VNET_CRYPTO_OP_##n##_DEC, s, 0);
829   foreach_crypto_cipher_alg;
830 #undef _
831 #define _(n, s, l) \
832   vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
833                                 VNET_CRYPTO_OP_##n##_ENC, \
834                                 VNET_CRYPTO_OP_##n##_DEC, s, 1);
835   foreach_crypto_aead_alg;
836 #undef _
837 #define _(n, s) \
838   vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
839                               VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
840   foreach_crypto_hmac_alg;
841 #undef _
842 #define _(n, s)                                                               \
843   vnet_crypto_init_hash_data (VNET_CRYPTO_ALG_HASH_##n,                       \
844                               VNET_CRYPTO_OP_##n##_HASH, s);
845   foreach_crypto_hash_alg;
846 #undef _
847 #define _(n, s, k, t, a) \
848   vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a, \
849                                VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
850                                VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC, \
851                                s);
852   foreach_crypto_aead_async_alg
853 #undef _
854 #define _(c, h, s, k ,d) \
855   vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##c##_##h##_TAG##d, \
856                                VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
857                                VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC, \
858                                s);
859     foreach_crypto_link_async_alg
860 #undef _
861     cm->crypto_node_index =
862     vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch")->index;
863
864   return 0;
865 }
866
867 VLIB_INIT_FUNCTION (vnet_crypto_init);
868
869 /*
870  * fd.io coding-style-patch-verification: ON
871  *
872  * Local Variables:
873  * eval: (c-set-style "gnu")
874  * End:
875  */