FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
tls_async.c
Go to the documentation of this file.
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/api_errno.h>
17 #include <vlib/node_funcs.h>
18 #include <openssl/engine.h>
19 #include <tlsopenssl/tls_openssl.h>
20 
21 #define SSL_ASYNC_INFLIGHT 1
22 #define SSL_ASYNC_READY 2
23 #define SSL_ASYNC_REENTER 3
24 #define MAX_VECTOR_ASYNC 256
25 
27 {
31 
32 typedef struct openssl_event_
33 {
37 
40 #define thread_idx cb_args.thread_index
41 #define event_idx cb_args.event_index
42  int next;
44 
45 typedef struct openssl_async_queue_
46 {
50 
51 typedef struct openssl_async_
52 {
55  void (*polling) (void);
57  ENGINE *engine;
58 
60 
61 void qat_polling ();
62 void qat_pre_init ();
63 void qat_polling_config ();
64 void dasync_polling ();
65 
67 {
68  char *engine;
69  void (*polling) (void);
70  void (*pre_init) (void);
71  void (*thread_init) (void *);
72 };
73 
74 void qat_init_thread (void *arg);
75 
78  {"dasync", dasync_polling, NULL, NULL}
79 };
80 
83 
84 /* to avoid build warning */
86  void *rpc_args);
87 
88 void
90 {
93  int i, num_threads;
94 
95  num_threads = 1 /* main thread */ + vtm->n_threads;
96 
97  TLS_DBG (2, "Totally there is %d thread\n", num_threads);
98 
99  vec_validate (om->evt_pool, num_threads - 1);
100  vec_validate (om->queue, num_threads - 1);
101 
102  om->start_polling = 0;
103  om->engine = 0;
104 
105  for (i = 0; i < num_threads; i++)
106  {
107  om->queue[i].evt_run_head = -1;
108  om->queue[i].evt_run_tail = -1;
109  }
110  om->polling = NULL;
111 
112  return;
113 }
114 
115 int
116 openssl_engine_register (char *engine_name, char *algorithm, int async)
117 {
118  int i, registered = -1;
120  void (*p) (void);
121  ENGINE *engine;
122 
123  for (i = 0; i < ARRAY_LEN (engine_list); i++)
124  {
125  if (!strcmp (engine_list[i].engine, engine_name))
126  {
127  om->polling = engine_list[i].polling;
128  registered = i;
129  }
130  }
131  if (registered < 0)
132  {
133  clib_error ("engine %s is not regisered in VPP", engine_name);
134  return -1;
135  }
136 
137  ENGINE_load_builtin_engines ();
138  ENGINE_load_dynamic ();
139  engine = ENGINE_by_id (engine_name);
140 
141  if (engine == NULL)
142  {
143  clib_warning ("Failed to find engine ENGINE_by_id %s", engine_name);
144  return -1;
145  }
146 
147  om->engine = engine;
148  /* call pre-init */
149  p = engine_list[registered].pre_init;
150  if (p)
151  (*p) ();
152 
153  if (algorithm)
154  {
155  if (!ENGINE_set_default_string (engine, algorithm))
156  {
157  clib_warning ("Failed to set engine %s algorithm %s\n",
158  engine_name, algorithm);
159  return -1;
160  }
161  }
162  else
163  {
164  if (!ENGINE_set_default (engine, ENGINE_METHOD_ALL))
165  {
166  clib_warning ("Failed to set engine %s to all algorithm",
167  engine_name);
168  return -1;
169  }
170  }
171 
172  if (async)
173  {
175  }
176 
177  for (i = 0; i < vlib_num_workers (); i++)
178  {
179  if (engine_list[registered].thread_init)
181  engine_list[registered].thread_init,
182  uword_to_pointer (i, void *));
183  }
184 
185  om->start_polling = 1;
186 
187  return 0;
188 
189 }
190 
191 static openssl_evt_t *
192 openssl_evt_get (u32 evt_index)
193 {
194  openssl_evt_t **evt;
195  evt =
197  evt_index);
198  return *evt;
199 }
200 
201 static openssl_evt_t *
203 {
204  openssl_evt_t **evt;
205 
206  evt =
208  return *evt;
209 }
210 
211 int
212 openssl_evt_free (int event_index, u8 thread_index)
213 {
215 
216  /*pool operation */
217  pool_put_index (om->evt_pool[thread_index], event_index);
218 
219  return 1;
220 }
221 
222 static u32
224 {
227  openssl_evt_t **evt;
228 
229  pool_get (tm->evt_pool[thread_index], evt);
230  if (!(*evt))
231  *evt = clib_mem_alloc (sizeof (openssl_evt_t));
232 
233  clib_memset (*evt, 0, sizeof (openssl_evt_t));
234  (*evt)->event_idx = evt - tm->evt_pool[thread_index];
235  return ((*evt)->event_idx);
236 }
237 
238 
239 /* In most cases, tls_async_openssl_callback is called by HW to make event active
240  * When EAGAIN received, VPP will call this callback to retry
241  */
242 int
243 tls_async_openssl_callback (SSL * s, void *cb_arg)
244 {
245  openssl_evt_t *event, *event_tail;
248  int thread_index = args->thread_index;
249  int event_index = args->event_index;
250  int *evt_run_tail = &om->queue[thread_index].evt_run_tail;
251  int *evt_run_head = &om->queue[thread_index].evt_run_head;
252 
253  TLS_DBG (2, "Set event %d to run\n", event_index);
254  event = openssl_evt_get_w_thread (event_index, thread_index);
255 
256  /* Happend when a recursive case, especially in SW simulation */
257  if (PREDICT_FALSE (event->status == SSL_ASYNC_READY))
258  {
259  event->status = SSL_ASYNC_REENTER;
260  return 0;
261  }
262  event->status = SSL_ASYNC_READY;
263  event->next = -1;
264 
265  if (*evt_run_tail >= 0)
266  {
267  event_tail = openssl_evt_get_w_thread (*evt_run_tail, thread_index);
268  event_tail->next = event_index;
269  }
270  *evt_run_tail = event_index;
271  if (*evt_run_head < 0)
272  {
273  *evt_run_head = event_index;
274  }
275 
276  return 1;
277 }
278 
279 int
281  openssl_resume_handler * handler,
282  session_t * session)
283 {
284  u32 eidx;
285  openssl_evt_t *event;
286  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
287  u32 thread_id = ctx->c_thread_index;
288 
289  eidx = openssl_evt_alloc ();
290  event = openssl_evt_get (eidx);
291  event->ctx_index = oc->openssl_ctx_index;
292  event->event_idx = eidx;
293  event->thread_idx = thread_id;
294  event->handler = handler;
295  event->session_index = session->session_index;
296  event->status = 0;
297  ctx->evt_index = eidx;
298 #ifdef HAVE_OPENSSL_ASYNC
299  SSL_set_async_callback_arg (oc->ssl, &event->cb_args);
300 #endif
301 
302  return 1;
303 }
304 
305 int
307 {
308  u32 eidx;
309  openssl_evt_t *event;
310  eidx = ctx->evt_index;
311  event = openssl_evt_get (eidx);
312 
313  if (event->status == SSL_ASYNC_INFLIGHT)
314  return 1;
315  return 0;
316 }
317 
318 int
320 {
321  u32 eidx;
322  openssl_evt_t *event;
323 
324  eidx = ctx->evt_index;
325  event = openssl_evt_get (eidx);
326  event->status = SSL_ASYNC_INFLIGHT;
327  if (eagain)
328  return tls_async_openssl_callback (0, &event->cb_args);
329 
330  return 1;
331 }
332 
333 void
334 event_handler (void *tls_async)
335 {
336  openssl_resume_handler *handler;
337  openssl_evt_t *event;
338  session_t *session;
339  int thread_index;
340  tls_ctx_t *ctx;
341 
342  event = (openssl_evt_t *) tls_async;
343  thread_index = event->thread_idx;
345  handler = event->handler;
346  session = session_get (event->session_index, thread_index);
347 
348  if (handler)
349  {
350  (*handler) (ctx, session);
351  }
352 
353  return;
354 }
355 
356  /* engine specific code to polling the response ring */
357 void
359 {
360 /* dasync is a fake async device, and could not be polled.
361  * We have added code in the dasync engine to triggered the callback already,
362  * so nothing can be done here
363  */
364 }
365 
366 void
368 {
370 
371  ENGINE_ctrl_cmd (om->engine, "ENABLE_EXTERNAL_POLLING", 0, NULL, NULL, 0);
372 }
373 
374 /* Below code is spefic to QAT engine, and other vendors can refer to this code to enable a new engine */
375 void
376 qat_init_thread (void *arg)
377 {
379  int thread_index = pointer_to_uword (arg);
380 
381  ENGINE_ctrl_cmd (om->engine, "SET_INSTANCE_FOR_THREAD", thread_index,
382  NULL, NULL, 0);
383 
384  TLS_DBG (2, "set thread %d and instance %d mapping\n", thread_index,
385  thread_index);
386 
387 }
388 
389 void
391 {
393  int poll_status = 0;
394 
395  if (om->start_polling)
396  {
397  ENGINE_ctrl_cmd (om->engine, "POLL", 0, &poll_status, NULL, 0);
398  }
399 }
400 
401 void
403 {
405  if (om->polling)
406  {
407  (*om->polling) ();
408  }
409 }
410 
411 void
413 {
414  u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
416  u8 have_workers = vtm->n_threads != 0;
417 
419  {
420  if (have_workers && this_vlib_main->thread_index)
421  {
422  vlib_node_set_state (this_vlib_main, tls_async_process_node.index,
423  state);
424  }
425  }
426 }
427 
428 int
430 {
431  tls_ctx_t *ctx;
432  openssl_evt_t *event;
433 
434  /* do the real job */
435  event = openssl_evt_get_w_thread (eidx, thread_index);
437 
438  if (ctx)
439  {
440  ctx->resume = 1;
442  }
443  return 1;
444 }
445 
446 int
448 {
449  int i;
450 
452  openssl_evt_t *event;
453  int *evt_run_head = &om->queue[thread_index].evt_run_head;
454  int *evt_run_tail = &om->queue[thread_index].evt_run_tail;
455 
456  if (*evt_run_head < 0)
457  return 0;
458 
459  for (i = 0; i < MAX_VECTOR_ASYNC; i++)
460  {
461  if (*evt_run_head >= 0)
462  {
463  event = openssl_evt_get_w_thread (*evt_run_head, thread_index);
464  tls_async_do_job (*evt_run_head, thread_index);
465  if (PREDICT_FALSE (event->status == SSL_ASYNC_REENTER))
466  {
467  /* recusive event triggered */
468  event->status = SSL_ASYNC_READY;
469  continue;
470  }
471 
472  event->status = 0;
473  *evt_run_head = event->next;
474 
475  if (event->next < 0)
476  {
477  *evt_run_tail = -1;
478  break;
479  }
480  }
481  }
482 
483  return 0;
484 
485 }
486 
487 static clib_error_t *
489 {
490  evt_pool_init (vm);
491  return 0;
492 }
493 
494 static uword
496  vlib_frame_t * f)
497 {
500 
502  if (pool_elts (om->evt_pool[thread_index]) > 0)
503  {
506  }
507 
508  return 0;
509 }
510 
512 
513 /* *INDENT-OFF* */
515  .function = tls_async_process,
516  .type = VLIB_NODE_TYPE_INPUT,
517  .name = "tls-async-process",
518  .state = VLIB_NODE_STATE_DISABLED,
519 };
520 
521 /* *INDENT-ON* */
522 
523 /*
524  * fd.io coding-style-patch-verification: ON
525  *
526  * Local Variables:
527  * eval: (c-set-style "gnu")
528  * End:
529  */
tls_async_process_node
static vlib_node_registration_t tls_async_process_node
(constructor) VLIB_REGISTER_NODE (tls_async_process_node)
Definition: tls_async.c:82
vlib_num_workers
static u32 vlib_num_workers()
Definition: threads.h:354
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
openssl_evt_t
struct openssl_event_ openssl_evt_t
openssl_async_
Definition: tls_async.c:51
openssl_async_queue_::evt_run_tail
int evt_run_tail
Definition: tls_async.c:48
SSL_ASYNC_REENTER
#define SSL_ASYNC_REENTER
Definition: tls_async.c:23
session_::session_index
u32 session_index
Index in thread pool where session was allocated.
Definition: session_types.h:188
openssl_async_::queue
openssl_async_queue_t * queue
Definition: tls_async.c:54
openssl_async_main
openssl_async_t openssl_async_main
Definition: tls_async.c:81
vpp_tls_async_update_event
int vpp_tls_async_update_event(tls_ctx_t *ctx, int eagain)
Definition: tls_async.c:319
qat_init_thread
void qat_init_thread(void *arg)
Definition: tls_async.c:376
f
vlib_frame_t * f
Definition: interface_output.c:1080
pointer_to_uword
static uword pointer_to_uword(const void *p)
Definition: types.h:131
openssl_tls_callback_arg_::thread_index
int thread_index
Definition: tls_async.c:28
vlib_node_set_state
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:175
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
openssl_async_polling
void openssl_async_polling()
Definition: tls_async.c:402
openssl_tls_callback_arg_::event_index
int event_index
Definition: tls_async.c:29
openssl_engine_register
int openssl_engine_register(char *engine_name, char *algorithm, int async)
Definition: tls_async.c:116
openssl_event_
Definition: tls_async.c:32
session_
Definition: session_types.h:175
openssl_async_::evt_pool
openssl_evt_t *** evt_pool
Definition: tls_async.c:53
VLIB_NODE_TYPE_INPUT
@ VLIB_NODE_TYPE_INPUT
Definition: node.h:76
tls_openssl.h
engine_polling::pre_init
void(* pre_init)(void)
Definition: tls_async.c:70
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
state
vl_api_dhcp_client_state_t state
Definition: dhcp.api:201
openssl_async_::engine
ENGINE * engine
Definition: tls_async.c:57
vpp_openssl_is_inflight
int vpp_openssl_is_inflight(tls_ctx_t *ctx)
Definition: tls_async.c:306
openssl_async_queue_t
struct openssl_async_queue_ openssl_async_queue_t
vlib_frame_t
Definition: node.h:372
openssl_event_::ctx_index
u32 ctx_index
Definition: tls_async.c:34
pool_put_index
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:337
tls_async_process
static uword tls_async_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: tls_async.c:495
evt_pool_init
void evt_pool_init(vlib_main_t *vm)
Definition: tls_async.c:89
openssl_async_queue_
Definition: tls_async.c:45
foreach_vlib_main
#define foreach_vlib_main()
Definition: threads.h:237
openssl_async_t
struct openssl_async_ openssl_async_t
tls_ctx_openssl_::openssl_ctx_index
u32 openssl_ctx_index
Definition: tls_openssl.h:35
tls_async_do_job
int tls_async_do_job(int eidx, u32 thread_index)
Definition: tls_async.c:429
TLS_DBG
#define TLS_DBG(_lvl, _fmt, _args...)
Definition: tls.h:36
openssl_event_::status
u8 status
Definition: tls_async.c:36
tls_ctx_
Definition: tls.h:59
openssl_async_::start_polling
u8 start_polling
Definition: tls_async.c:56
openssl_event_::cb_args
openssl_tls_callback_arg_t cb_args
Definition: tls_async.c:39
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_get_thread_index
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:208
SSL_ASYNC_READY
#define SSL_ASYNC_READY
Definition: tls_async.c:22
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
uword
u64 uword
Definition: types.h:112
openssl_evt_get_w_thread
static openssl_evt_t * openssl_evt_get_w_thread(int evt_index, u8 thread_index)
Definition: tls_async.c:202
tls_async_init
static clib_error_t * tls_async_init(vlib_main_t *vm)
Definition: tls_async.c:488
tls_ctx_openssl_::ssl
SSL * ssl
Definition: tls_openssl.h:37
openssl_evt_get
static openssl_evt_t * openssl_evt_get(u32 evt_index)
Definition: tls_async.c:192
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
pool_get
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
vpp_tls_async_init_event
int vpp_tls_async_init_event(tls_ctx_t *ctx, openssl_resume_handler *handler, session_t *session)
Definition: tls_async.c:280
openssl_evt_free
int openssl_evt_free(int event_index, u8 thread_index)
Definition: tls_async.c:212
engine_polling::thread_init
void(* thread_init)(void *)
Definition: tls_async.c:71
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
session_get
static session_t * session_get(u32 si, u32 thread_index)
Definition: session.h:336
tls_ctx_openssl_
Definition: tls_openssl.h:32
engine_polling
Definition: tls_async.c:66
tls_async_openssl_callback
int tls_async_openssl_callback(SSL *s, void *cb_arg)
Definition: tls_async.c:243
openssl_evt_alloc
static u32 openssl_evt_alloc(void)
Definition: tls_async.c:223
openssl_event_::session_index
int session_index
Definition: tls_async.c:35
openssl_event_::handler
openssl_resume_handler * handler
Definition: tls_async.c:38
openssl_event_::next
int next
Definition: tls_async.c:42
engine_list
struct engine_polling engine_list[]
Definition: tls_async.c:76
node_funcs.h
openssl_tls_callback_arg_t
struct openssl_tls_callback_arg_ openssl_tls_callback_arg_t
openssl_tls_callback_arg_
Definition: tls_async.c:26
qat_polling_config
void qat_polling_config()
session_send_rpc_evt_to_thread
void session_send_rpc_evt_to_thread(u32 thread_index, void *fp, void *rpc_args)
Definition: session.c:116
vlib_thread_main_t::n_threads
u32 n_threads
Definition: threads.h:292
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vlib_thread_main_t
Definition: threads.h:264
engine_polling::polling
void(* polling)(void)
Definition: tls_async.c:69
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
pool_elts
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127
dasync_polling
void dasync_polling()
Definition: tls_async.c:358
tls_resume_from_crypto
int tls_resume_from_crypto(int thread_index)
Definition: tls_async.c:447
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
engine_polling::engine
char * engine
Definition: tls_async.c:68
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
openssl_async_queue_::evt_run_head
int evt_run_head
Definition: tls_async.c:47
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:399
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
uword_to_pointer
#define uword_to_pointer(u, type)
Definition: types.h:136
clib_error
#define clib_error(format, args...)
Definition: error.h:62
clib_warning
#define clib_warning(format, args...)
Definition: error.h:59
MAX_VECTOR_ASYNC
#define MAX_VECTOR_ASYNC
Definition: tls_async.c:24
qat_polling
void qat_polling()
Definition: tls_async.c:390
openssl_async_::polling
void(* polling)(void)
Definition: tls_async.c:55
vnet.h
api_errno.h
event_handler
void event_handler(void *tls_async)
Definition: tls_async.c:334
vlib_node_runtime_t
Definition: node.h:454
openssl_async_node_enable_disable
void openssl_async_node_enable_disable(u8 is_en)
Definition: tls_async.c:412
openssl_resume_handler
int openssl_resume_handler(tls_ctx_t *ctx, session_t *tls_session)
Definition: tls_openssl.h:68
vlib_get_thread_main
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
openssl_ctx_get_w_thread
tls_ctx_t * openssl_ctx_get_w_thread(u32 ctx_index, u8 thread_index)
Definition: tls_openssl.c:126
SSL_ASYNC_INFLIGHT
#define SSL_ASYNC_INFLIGHT
Definition: tls_async.c:21
qat_pre_init
void qat_pre_init()
Definition: tls_async.c:367
clib_mem_alloc
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169