Add a new communication channel between VPP and openssl engine
[vpp.git] / src / plugins / tlsopenssl / tls_async.c
1 /*
2  * Copyright (c) 2018 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 #include <openssl/engine.h>
21 #include <tlsopenssl/tls_openssl.h>
22
23 #define MAX_SESSION         4096
24 #define MAX_VECTOR_ASYNC    256
25
26 #define SSL_ASYNC_INFLIGHT  1
27 #define SSL_ASYNC_PENDING   2
28 #define SSL_ASYNC_READY     3
29
30 #define EMPTY_STRUCT {0}
31
32 typedef struct openssl_tls_callback_arg_
33 {
34   int thread_index;
35   int event_index;
36 } openssl_tls_callback_arg_t;
37
38 typedef struct openssl_event_
39 {
40   int status;
41   u32 event_index;
42   u8 thread_index;
43   u32 ctx_index;
44
45   openssl_resume_handler *handler;
46   openssl_tls_callback_t engine_callback;
47   openssl_tls_callback_arg_t cb_args;
48
49   int next;
50 } openssl_evt_t;
51
52 typedef struct openssl_async_status_
53 {
54   int evt_run_head;
55   int evt_run_tail;
56   int evt_pending_head;
57   int poll_config;
58 } openssl_async_status_t;
59
60 typedef struct openssl_async_
61 {
62   openssl_evt_t ***evt_pool;
63   openssl_async_status_t *status;
64   void (*polling) (void);
65   u8 start_polling;
66   ENGINE *engine;
67
68 } openssl_async_t;
69
70 void qat_polling ();
71 void qat_pre_init ();
72 void dasync_polling ();
73
74 struct engine_polling
75 {
76   char *engine;
77   void (*polling) (void);
78   void (*pre_init) (void);
79 };
80
81 struct engine_polling engine_list[] = {
82   {"qat", qat_polling, qat_pre_init},
83   {"dasync", dasync_polling, NULL}
84 };
85
86 openssl_async_t openssl_async_main;
87 static vlib_node_registration_t tls_async_process_node;
88
89 /* to avoid build warning */
90 void session_send_rpc_evt_to_thread (u32 thread_index, void *fp,
91                                      void *rpc_args);
92
93 void
94 evt_pool_init (vlib_main_t * vm)
95 {
96   vlib_thread_main_t *vtm = vlib_get_thread_main ();
97   openssl_async_t *om = &openssl_async_main;
98   int i, num_threads;
99
100   num_threads = 1 /* main thread */  + vtm->n_threads;
101
102   TLS_DBG (2, "Totally there is %d thread\n", num_threads);
103
104   vec_validate (om->evt_pool, num_threads - 1);
105   vec_validate (om->status, num_threads - 1);
106
107   om->start_polling = 0;
108   om->engine = 0;
109
110   for (i = 0; i < num_threads; i++)
111     {
112       om->status[i].evt_run_head = -1;
113       om->status[i].evt_run_tail = -1;
114       om->status[i].evt_pending_head = -1;
115     }
116   om->polling = NULL;
117
118   openssl_async_node_enable_disable (0);
119
120   return;
121 }
122
123 int
124 openssl_engine_register (char *engine_name, char *algorithm)
125 {
126   int i, registered = -1;
127   openssl_async_t *om = &openssl_async_main;
128   void (*p) (void);
129   ENGINE *engine;
130
131   for (i = 0; i < ARRAY_LEN (engine_list); i++)
132     {
133       if (!strcmp (engine_list[i].engine, engine_name))
134         {
135           om->polling = engine_list[i].polling;
136
137           registered = i;
138         }
139     }
140   if (registered < 0)
141     {
142       return 0;
143     }
144
145   ENGINE_load_builtin_engines ();
146   ENGINE_load_dynamic ();
147   engine = ENGINE_by_id (engine_name);
148
149   if (engine == NULL)
150     {
151       return 0;
152     }
153
154   om->engine = engine;
155   /* call pre-init */
156   p = engine_list[registered].pre_init;
157   if (p)
158     (*p) ();
159
160   if (algorithm)
161     {
162       if (!ENGINE_set_default_string (engine, algorithm))
163         {
164           clib_warning ("Failed to set engine %s algorithm %s\n",
165                         engine_name, algorithm);
166           return 0;
167         }
168     }
169   else
170     {
171       if (!ENGINE_set_default (engine, ENGINE_METHOD_ALL))
172         {
173           clib_warning ("Failed to set engine %s to all algorithm",
174                         engine_name);
175           return 0;
176         }
177     }
178
179   om->start_polling = 1;
180
181   return 1;
182
183 }
184
185 static openssl_evt_t *
186 openssl_evt_get (u32 evt_index)
187 {
188   openssl_evt_t **evt;
189   evt =
190     pool_elt_at_index (openssl_async_main.evt_pool[vlib_get_thread_index ()],
191                        evt_index);
192   return *evt;
193 }
194
195 static openssl_evt_t *
196 openssl_evt_get_w_thread (int evt_index, u8 thread_index)
197 {
198   openssl_evt_t **evt;
199
200   evt =
201     pool_elt_at_index (openssl_async_main.evt_pool[thread_index], evt_index);
202   return *evt;
203 }
204
205 int
206 openssl_evt_free (int event_idx, u8 thread_index)
207 {
208   openssl_evt_t *evt;
209   openssl_async_t *om = &openssl_async_main;
210   int *evt_run_tail = &om->status[thread_index].evt_run_tail;
211
212   if (event_idx < 0)
213     return 0;
214
215   evt = openssl_evt_get_w_thread (event_idx, thread_index);
216
217   evt->status = 0;
218
219   /*pool operation */
220   pool_put_index (om->evt_pool[thread_index], event_idx);
221
222   if (*evt_run_tail == event_idx)
223     *evt_run_tail = -1;
224
225   return 1;
226 }
227
228 static u32
229 openssl_evt_alloc (void)
230 {
231   u8 thread_index = vlib_get_thread_index ();
232   openssl_async_t *tm = &openssl_async_main;
233   openssl_evt_t **evt;
234
235   pool_get (tm->evt_pool[thread_index], evt);
236   if (!(*evt))
237     *evt = clib_mem_alloc (sizeof (openssl_evt_t));
238
239   memset (*evt, 0, sizeof (openssl_evt_t));
240   (*evt)->event_index = evt - tm->evt_pool[thread_index];
241   return ((*evt)->event_index);
242 }
243
244 int
245 openssl_async_run (void *evt)
246 {
247   openssl_evt_t *event, *event_tail;
248   openssl_async_t *om = &openssl_async_main;
249   openssl_tls_callback_arg_t *args = (openssl_tls_callback_arg_t *) evt;
250   int thread_index = args->thread_index;
251   int event_index = args->event_index;
252   int *evt_run_tail = &om->status[thread_index].evt_run_tail;
253   int *evt_run_head = &om->status[thread_index].evt_run_head;
254
255   TLS_DBG (2, "Set event %d to run\n", event_index);
256
257   event = openssl_evt_get_w_thread (event_index, thread_index);
258
259   if (event->status == SSL_ASYNC_READY)
260     return 0;
261
262   event->status = SSL_ASYNC_READY;
263   event->next = -1;
264
265
266   if (*evt_run_tail >= 0)
267     {
268       event_tail = openssl_evt_get_w_thread (*evt_run_tail, thread_index);
269       event_tail->next = event_index;
270     }
271   *evt_run_tail = event_index;
272   if (*evt_run_head < 0)
273     {
274       *evt_run_head = event_index;
275     }
276
277   return 1;
278 }
279
280 openssl_tls_callback_t *
281 vpp_add_async_pending_event (tls_ctx_t * ctx,
282                              openssl_resume_handler * handler)
283 {
284   u32 eidx;
285   openssl_evt_t *event;
286   openssl_async_t *om = &openssl_async_main;
287   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
288   int *evt_pending_head;
289   u32 thread_id = ctx->c_thread_index;
290
291   eidx = openssl_evt_alloc ();
292   event = openssl_evt_get (eidx);
293
294   event->ctx_index = oc->openssl_ctx_index;
295   event->status = SSL_ASYNC_PENDING;
296   event->handler = handler;
297   event->cb_args.event_index = eidx;
298   event->cb_args.thread_index = thread_id;
299   event->engine_callback.callback = openssl_async_run;
300   event->engine_callback.arg = &event->cb_args;
301
302   /* add to pending list */
303   evt_pending_head = &om->status[thread_id].evt_pending_head;
304   event->next = *evt_pending_head;
305   *evt_pending_head = eidx;
306
307   return &event->engine_callback;
308 }
309
310 int
311 vpp_add_async_run_event (tls_ctx_t * ctx, openssl_resume_handler * handler)
312 {
313   u32 eidx;
314   openssl_evt_t *event;
315   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
316   u32 thread_id = ctx->c_thread_index;
317
318   eidx = openssl_evt_alloc ();
319   event = openssl_evt_get (eidx);
320
321   event->ctx_index = oc->openssl_ctx_index;
322   event->status = SSL_ASYNC_PENDING;
323   event->handler = handler;
324   event->cb_args.event_index = eidx;
325   event->cb_args.thread_index = thread_id;
326   event->engine_callback.callback = openssl_async_run;
327   event->engine_callback.arg = &event->cb_args;
328
329   /* This is a retry event, and need to put to ring to make it run again */
330   return openssl_async_run (&event->cb_args);
331
332 }
333
334 void
335 event_handler (void *tls_async)
336 {
337
338   openssl_resume_handler *handler;
339   openssl_evt_t *callback;
340   stream_session_t *tls_session;
341   int thread_index;
342   tls_ctx_t *ctx;
343
344   callback = (openssl_evt_t *) tls_async;
345   thread_index = callback->cb_args.thread_index;
346   ctx = openssl_ctx_get_w_thread (callback->ctx_index, thread_index);
347   handler = callback->handler;
348   tls_session = session_get_from_handle (ctx->tls_session_handle);
349
350   if (handler)
351     {
352       (*handler) (ctx, tls_session);
353     }
354
355   /* Need to free the event */
356   openssl_evt_free (callback->cb_args.event_index, thread_index);
357
358   return;
359 }
360
361  /* engine specific code to polling the response ring */
362 void
363 dasync_polling ()
364 {
365   openssl_async_t *om = &openssl_async_main;
366   openssl_evt_t *event;
367   int *evt_pending;
368   openssl_tls_callback_t *engine_cb;
369   u8 thread_index = vlib_get_thread_index ();
370
371   /* POC code here to simulate the engine to call callback */
372   evt_pending = &om->status[thread_index].evt_pending_head;
373   while (*evt_pending >= 0)
374     {
375       TLS_DBG (2, "polling... current head = %d\n", *evt_pending);
376       event = openssl_evt_get_w_thread (*evt_pending, thread_index);
377       *evt_pending = event->next;
378       if (event->status == SSL_ASYNC_PENDING)
379         {
380           engine_cb = &event->engine_callback;
381           (*engine_cb->callback) (engine_cb->arg);
382         }
383     }
384
385 }
386
387 void
388 qat_pre_init ()
389 {
390   openssl_async_t *om = &openssl_async_main;
391
392   ENGINE_ctrl_cmd (om->engine, "ENABLE_EXTERNAL_POLLING", 0, NULL, NULL, 0);
393 }
394
395 /* Below code is spefic to QAT engine, and other vendors can refer to this code to enable a new engine */
396 void
397 qat_polling_config ()
398 {
399   openssl_async_t *om = &openssl_async_main;
400   u8 thread_index = vlib_get_thread_index ();
401   int *config;
402
403   config = &om->status[thread_index].poll_config;
404   if (*config)
405     return;
406
407   ENGINE_ctrl_cmd (om->engine, "SET_INSTANCE_FOR_THREAD", thread_index,
408                    NULL, NULL, 0);
409   *config = 1;
410
411   TLS_DBG (2, "set thread %d and instance %d mapping\n", thread_index,
412            thread_index);
413
414 }
415
416 void
417 qat_polling ()
418 {
419   openssl_async_t *om = &openssl_async_main;
420   int poll_status = 0;
421
422   if (om->start_polling)
423     {
424       qat_polling_config ();
425       ENGINE_ctrl_cmd (om->engine, "POLL", 0, &poll_status, NULL, 0);
426     }
427 }
428
429 void
430 openssl_async_polling ()
431 {
432   openssl_async_t *om = &openssl_async_main;
433   if (om->polling)
434     {
435       (*om->polling) ();
436     }
437 }
438
439 void
440 openssl_async_node_enable_disable (u8 is_en)
441 {
442   u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
443   /* *INDENT-OFF* */
444   foreach_vlib_main (({
445     vlib_node_set_state (this_vlib_main, tls_async_process_node.index,
446                          state);
447   }));
448   /* *INDENT-ON* */
449 }
450
451 int
452 tls_async_do_job (int eidx, u32 thread_index)
453 {
454   tls_ctx_t *ctx;
455   openssl_evt_t *event;
456
457   /* do the real job */
458   event = openssl_evt_get_w_thread (eidx, thread_index);
459   ctx = openssl_ctx_get_w_thread (event->ctx_index, thread_index);
460
461   if (ctx)
462     {
463       ctx->resume = 1;
464       session_send_rpc_evt_to_thread (thread_index, event_handler, event);
465     }
466   return 1;
467 }
468
469 int
470 tls_resume_from_crypto (int thread_index)
471 {
472   int i;
473
474   openssl_async_t *om = &openssl_async_main;
475   openssl_evt_t *event;
476   int *evt_run_head = &om->status[thread_index].evt_run_head;
477
478   if (*evt_run_head < 0)
479     return 0;
480
481   for (i = 0; i < MAX_VECTOR_ASYNC; i++)
482     {
483       if (*evt_run_head >= 0)
484         {
485           event = openssl_evt_get_w_thread (*evt_run_head, thread_index);
486           TLS_DBG (2, "event run = %d\n", *evt_run_head);
487           tls_async_do_job (*evt_run_head, thread_index);
488
489           *evt_run_head = event->next;
490
491         }
492       else
493         {
494           break;
495         }
496     }
497
498   return 0;
499
500 }
501
502 static clib_error_t *
503 tls_async_init (vlib_main_t * vm)
504 {
505   evt_pool_init (vm);
506   return 0;
507
508 }
509
510 static uword
511 tls_async_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
512                    vlib_frame_t * f)
513 {
514   u8 thread_index;
515
516   thread_index = vlib_get_thread_index ();
517   openssl_async_polling ();
518
519   tls_resume_from_crypto (thread_index);
520
521   return 0;
522 }
523
524 VLIB_INIT_FUNCTION (tls_async_init);
525
526 /* *INDENT-OFF* */
527 VLIB_REGISTER_NODE (tls_async_process_node,static) = {
528     .function = tls_async_process,
529     .type = VLIB_NODE_TYPE_INPUT,
530     .name = "tls-async-process",
531 };
532
533
534 /* *INDENT-ON* */
535
536 /*
537  * fd.io coding-style-patch-verification: ON
538  *
539  * Local Variables:
540  * eval: (c-set-style "gnu")
541  * End:
542  */