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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
23 #define MAX_SESSION 4096
24 #define MAX_VECTOR_ASYNC 256
26 #define SSL_ASYNC_INFLIGHT 1
27 #define SSL_ASYNC_PENDING 2
28 #define SSL_ASYNC_READY 3
30 #define EMPTY_STRUCT {0}
32 typedef struct openssl_tls_callback_arg_
36 } openssl_tls_callback_arg_t;
38 typedef struct openssl_event_
45 openssl_resume_handler *handler;
46 openssl_tls_callback_t engine_callback;
47 openssl_tls_callback_arg_t cb_args;
52 typedef struct openssl_async_status_
58 } openssl_async_status_t;
60 typedef struct openssl_async_
62 openssl_evt_t ***evt_pool;
63 openssl_async_status_t *status;
64 void (*polling) (void);
72 void dasync_polling ();
77 void (*polling) (void);
78 void (*pre_init) (void);
81 struct engine_polling engine_list[] = {
82 {"qat", qat_polling, qat_pre_init},
83 {"dasync", dasync_polling, NULL}
86 openssl_async_t openssl_async_main;
87 static vlib_node_registration_t tls_async_process_node;
89 /* to avoid build warning */
90 void session_send_rpc_evt_to_thread (u32 thread_index, void *fp,
94 evt_pool_init (vlib_main_t * vm)
96 vlib_thread_main_t *vtm = vlib_get_thread_main ();
97 openssl_async_t *om = &openssl_async_main;
100 num_threads = 1 /* main thread */ + vtm->n_threads;
102 TLS_DBG (2, "Totally there is %d thread\n", num_threads);
104 vec_validate (om->evt_pool, num_threads - 1);
105 vec_validate (om->status, num_threads - 1);
107 om->start_polling = 0;
110 for (i = 0; i < num_threads; i++)
112 om->status[i].evt_run_head = -1;
113 om->status[i].evt_run_tail = -1;
114 om->status[i].evt_pending_head = -1;
118 openssl_async_node_enable_disable (0);
124 openssl_engine_register (char *engine_name, char *algorithm)
126 int i, registered = -1;
127 openssl_async_t *om = &openssl_async_main;
131 for (i = 0; i < ARRAY_LEN (engine_list); i++)
133 if (!strcmp (engine_list[i].engine, engine_name))
135 om->polling = engine_list[i].polling;
145 ENGINE_load_builtin_engines ();
146 ENGINE_load_dynamic ();
147 engine = ENGINE_by_id (engine_name);
156 p = engine_list[registered].pre_init;
162 if (!ENGINE_set_default_string (engine, algorithm))
164 clib_warning ("Failed to set engine %s algorithm %s\n",
165 engine_name, algorithm);
171 if (!ENGINE_set_default (engine, ENGINE_METHOD_ALL))
173 clib_warning ("Failed to set engine %s to all algorithm",
179 om->start_polling = 1;
185 static openssl_evt_t *
186 openssl_evt_get (u32 evt_index)
190 pool_elt_at_index (openssl_async_main.evt_pool[vlib_get_thread_index ()],
195 static openssl_evt_t *
196 openssl_evt_get_w_thread (int evt_index, u8 thread_index)
201 pool_elt_at_index (openssl_async_main.evt_pool[thread_index], evt_index);
206 openssl_evt_free (int event_idx, u8 thread_index)
209 openssl_async_t *om = &openssl_async_main;
210 int *evt_run_tail = &om->status[thread_index].evt_run_tail;
215 evt = openssl_evt_get_w_thread (event_idx, thread_index);
220 pool_put_index (om->evt_pool[thread_index], event_idx);
222 if (*evt_run_tail == event_idx)
229 openssl_evt_alloc (void)
231 u8 thread_index = vlib_get_thread_index ();
232 openssl_async_t *tm = &openssl_async_main;
235 pool_get (tm->evt_pool[thread_index], evt);
237 *evt = clib_mem_alloc (sizeof (openssl_evt_t));
239 memset (*evt, 0, sizeof (openssl_evt_t));
240 (*evt)->event_index = evt - tm->evt_pool[thread_index];
241 return ((*evt)->event_index);
245 openssl_async_run (void *evt)
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;
255 TLS_DBG (2, "Set event %d to run\n", event_index);
257 event = openssl_evt_get_w_thread (event_index, thread_index);
259 if (event->status == SSL_ASYNC_READY)
262 event->status = SSL_ASYNC_READY;
266 if (*evt_run_tail >= 0)
268 event_tail = openssl_evt_get_w_thread (*evt_run_tail, thread_index);
269 event_tail->next = event_index;
271 *evt_run_tail = event_index;
272 if (*evt_run_head < 0)
274 *evt_run_head = event_index;
280 openssl_tls_callback_t *
281 vpp_add_async_pending_event (tls_ctx_t * ctx,
282 openssl_resume_handler * handler)
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;
291 eidx = openssl_evt_alloc ();
292 event = openssl_evt_get (eidx);
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;
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;
307 return &event->engine_callback;
311 vpp_add_async_run_event (tls_ctx_t * ctx, openssl_resume_handler * handler)
314 openssl_evt_t *event;
315 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
316 u32 thread_id = ctx->c_thread_index;
318 eidx = openssl_evt_alloc ();
319 event = openssl_evt_get (eidx);
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;
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);
335 event_handler (void *tls_async)
338 openssl_resume_handler *handler;
339 openssl_evt_t *callback;
340 stream_session_t *tls_session;
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);
352 (*handler) (ctx, tls_session);
355 /* Need to free the event */
356 openssl_evt_free (callback->cb_args.event_index, thread_index);
361 /* engine specific code to polling the response ring */
365 openssl_async_t *om = &openssl_async_main;
366 openssl_evt_t *event;
368 openssl_tls_callback_t *engine_cb;
369 u8 thread_index = vlib_get_thread_index ();
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)
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)
380 engine_cb = &event->engine_callback;
381 (*engine_cb->callback) (engine_cb->arg);
390 openssl_async_t *om = &openssl_async_main;
392 ENGINE_ctrl_cmd (om->engine, "ENABLE_EXTERNAL_POLLING", 0, NULL, NULL, 0);
395 /* Below code is spefic to QAT engine, and other vendors can refer to this code to enable a new engine */
397 qat_polling_config ()
399 openssl_async_t *om = &openssl_async_main;
400 u8 thread_index = vlib_get_thread_index ();
403 config = &om->status[thread_index].poll_config;
407 ENGINE_ctrl_cmd (om->engine, "SET_INSTANCE_FOR_THREAD", thread_index,
411 TLS_DBG (2, "set thread %d and instance %d mapping\n", thread_index,
419 openssl_async_t *om = &openssl_async_main;
422 if (om->start_polling)
424 qat_polling_config ();
425 ENGINE_ctrl_cmd (om->engine, "POLL", 0, &poll_status, NULL, 0);
430 openssl_async_polling ()
432 openssl_async_t *om = &openssl_async_main;
440 openssl_async_node_enable_disable (u8 is_en)
442 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
444 foreach_vlib_main (({
445 vlib_node_set_state (this_vlib_main, tls_async_process_node.index,
452 tls_async_do_job (int eidx, u32 thread_index)
455 openssl_evt_t *event;
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);
464 session_send_rpc_evt_to_thread (thread_index, event_handler, event);
470 tls_resume_from_crypto (int thread_index)
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;
478 if (*evt_run_head < 0)
481 for (i = 0; i < MAX_VECTOR_ASYNC; i++)
483 if (*evt_run_head >= 0)
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);
489 *evt_run_head = event->next;
502 static clib_error_t *
503 tls_async_init (vlib_main_t * vm)
511 tls_async_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
515 openssl_async_t *om = &openssl_async_main;
517 thread_index = vlib_get_thread_index ();
518 if (pool_elts (om->evt_pool[thread_index]) > 0)
520 openssl_async_polling ();
521 tls_resume_from_crypto (thread_index);
527 VLIB_INIT_FUNCTION (tls_async_init);
530 VLIB_REGISTER_NODE (tls_async_process_node,static) = {
531 .function = tls_async_process,
532 .type = VLIB_NODE_TYPE_INPUT,
533 .name = "tls-async-process",
540 * fd.io coding-style-patch-verification: ON
543 * eval: (c-set-style "gnu")