FD.io VPP  v18.10-32-g1161dda
Vector Packet Processing
tls.c
Go to the documentation of this file.
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 
17 #include <vppinfra/lock.h>
18 #include <vnet/tls/tls.h>
19 
22 
23 #define TLS_INVALID_HANDLE ~0
24 #define TLS_IDX_MASK 0x00FFFFFF
25 #define TLS_ENGINE_TYPE_SHIFT 29
26 
27 void tls_disconnect (u32 ctx_handle, u32 thread_index);
28 
29 static void
31 {
33  .handle = ctx->tls_session_handle,
34  .app_index = tls_main.app_index,
35  };
36 
37  if (vnet_disconnect_session (&a))
38  clib_warning ("disconnect returned");
39 }
40 
43 {
44  int i;
45  for (i = 0; i < vec_len (tls_vfts); i++)
46  {
47  if (tls_vfts[i].ctx_alloc)
48  return i;
49  }
50  return TLS_ENGINE_NONE;
51 }
52 
53 int
55 {
56  if (svm_fifo_set_event (s->server_rx_fifo))
58  return 0;
59 }
60 
61 int
63 {
64  if (svm_fifo_set_event (s->server_rx_fifo))
66  return 0;
67 }
68 
69 int
71 {
72  if (svm_fifo_set_event (s->server_tx_fifo))
74  return 0;
75 }
76 
77 int
79 {
80  if (svm_fifo_set_event (s->server_tx_fifo))
81  session_send_io_evt_to_thread_custom (s, s->thread_index,
83  return 0;
84 }
85 
86 static inline int
88 {
89  return app_worker_lock_and_send_event (app, app_session, FIFO_EVENT_APP_RX);
90 }
91 
92 u32
94 {
95  tls_main_t *tm = &tls_main;
96  tls_ctx_t *ctx;
97 
98  pool_get (tm->listener_ctx_pool, ctx);
99  memset (ctx, 0, sizeof (*ctx));
100  return ctx - tm->listener_ctx_pool;
101 }
102 
103 void
105 {
106  if (CLIB_DEBUG)
107  memset (ctx, 0xfb, sizeof (*ctx));
108  pool_put (tls_main.listener_ctx_pool, ctx);
109 }
110 
111 tls_ctx_t *
113 {
114  return pool_elt_at_index (tls_main.listener_ctx_pool, ctx_index);
115 }
116 
117 u32
119 {
120  return (ctx - tls_main.listener_ctx_pool);
121 }
122 
123 u32
125 {
126  tls_main_t *tm = &tls_main;
127  u8 will_expand = 0;
128  tls_ctx_t *ctx;
129  u32 ctx_index;
130 
131  pool_get_aligned_will_expand (tm->half_open_ctx_pool, will_expand, 0);
132  if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
133  {
135  pool_get (tm->half_open_ctx_pool, ctx);
136  ctx_index = ctx - tm->half_open_ctx_pool;
138  }
139  else
140  {
141  /* reader lock assumption: only main thread will call pool_get */
143  pool_get (tm->half_open_ctx_pool, ctx);
144  ctx_index = ctx - tm->half_open_ctx_pool;
146  }
147  memset (ctx, 0, sizeof (*ctx));
148  return ctx_index;
149 }
150 
151 void
153 {
154  tls_main_t *tm = &tls_main;
156  pool_put_index (tls_main.half_open_ctx_pool, ho_index);
158 }
159 
160 tls_ctx_t *
162 {
163  tls_main_t *tm = &tls_main;
165  return pool_elt_at_index (tm->half_open_ctx_pool, ctx_index);
166 }
167 
168 void
170 {
172 }
173 
174 u32
176 {
177  return (ctx - tls_main.half_open_ctx_pool);
178 }
179 
180 void
182 {
183  app_worker_t *app;
184  app = app_worker_get_if_valid (app_session->app_wrk_index);
185  if (PREDICT_TRUE (app != 0))
186  tls_add_app_q_evt (app, app_session);
187 }
188 
189 int
191 {
192  stream_session_t *app_listener, *app_session;
193  segment_manager_t *sm;
194  app_worker_t *app_wrk;
195  application_t *app;
196  tls_ctx_t *lctx;
197  int rv;
198 
199  app_wrk = app_worker_get_if_valid (ctx->parent_app_index);
200  if (!app_wrk)
201  {
202  tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
203  return -1;
204  }
205 
206  app = application_get (app_wrk->app_index);
207  lctx = tls_listener_ctx_get (ctx->listener_ctx_index);
208 
209  app_session = session_alloc (vlib_get_thread_index ());
210  app_session->app_wrk_index = ctx->parent_app_index;
211  app_session->connection_index = ctx->tls_ctx_handle;
212 
213  app_listener = listen_session_get_from_handle (lctx->app_session_handle);
214  app_session->session_type = app_listener->session_type;
215  app_session->listener_index = app_listener->session_index;
216  sm = app_worker_get_listen_segment_manager (app_wrk, app_listener);
217  app_session->t_app_index = tls_main.app_index;
218 
219  if ((rv = session_alloc_fifos (sm, app_session)))
220  {
221  TLS_DBG (1, "failed to allocate fifos");
222  return rv;
223  }
224  ctx->c_s_index = app_session->session_index;
225  ctx->app_session_handle = session_handle (app_session);
227  session_handle (app_session));
228  return app->cb_fns.session_accept_callback (app_session);
229 }
230 
231 int
233 {
234  int (*cb_fn) (u32, u32, stream_session_t *, u8);
235  stream_session_t *app_session;
236  segment_manager_t *sm;
237  app_worker_t *app_wrk;
238  application_t *app;
239 
240  app_wrk = app_worker_get_if_valid (ctx->parent_app_index);
241  if (!app_wrk)
242  {
244  return -1;
245  }
246 
247  app = application_get (app_wrk->app_index);
248  cb_fn = app->cb_fns.session_connected_callback;
249 
250  if (is_failed)
251  goto failed;
252 
254  app_session = session_alloc (vlib_get_thread_index ());
255  app_session->app_wrk_index = ctx->parent_app_index;
256  app_session->connection_index = ctx->tls_ctx_handle;
257  app_session->session_type =
259  app_session->t_app_index = tls_main.app_index;
260 
261  if (session_alloc_fifos (sm, app_session))
262  goto failed;
263 
264  ctx->app_session_handle = session_handle (app_session);
265  ctx->c_s_index = app_session->session_index;
266  app_session->session_state = SESSION_STATE_CONNECTING;
267  if (cb_fn (ctx->parent_app_index, ctx->parent_app_api_context,
268  app_session, 0 /* not failed */ ))
269  {
270  TLS_DBG (1, "failed to notify app");
271  tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
272  return -1;
273  }
274 
275  app_session->session_state = SESSION_STATE_READY;
277  session_handle (app_session));
278 
279  return 0;
280 
281 failed:
282  tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
283  return cb_fn (ctx->parent_app_index, ctx->parent_app_api_context, 0,
284  1 /* failed */ );
285 }
286 
287 static inline void
288 tls_ctx_parse_handle (u32 ctx_handle, u32 * ctx_index, u32 * engine_type)
289 {
290  *ctx_index = ctx_handle & TLS_IDX_MASK;
291  *engine_type = ctx_handle >> TLS_ENGINE_TYPE_SHIFT;
292 }
293 
294 static inline tls_engine_type_t
296 {
297  if (!tls_vfts[preferred].ctx_alloc)
298  return tls_get_available_engine ();
299  return preferred;
300 }
301 
302 static inline u32
304 {
305  u32 ctx_index;
306  ctx_index = tls_vfts[engine_type].ctx_alloc ();
307  return (((u32) engine_type << TLS_ENGINE_TYPE_SHIFT) | ctx_index);
308 }
309 
310 static inline void
312 {
313  vec_free (ctx->srv_hostname);
314  tls_vfts[ctx->tls_ctx_engine].ctx_free (ctx);
315 }
316 
317 static inline tls_ctx_t *
318 tls_ctx_get (u32 ctx_handle)
319 {
320  u32 ctx_index, engine_type;
321  tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type);
322  return tls_vfts[engine_type].ctx_get (ctx_index);
323 }
324 
325 static inline tls_ctx_t *
326 tls_ctx_get_w_thread (u32 ctx_handle, u8 thread_index)
327 {
328  u32 ctx_index, engine_type;
329  tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type);
330  return tls_vfts[engine_type].ctx_get_w_thread (ctx_index, thread_index);
331 }
332 
333 static inline int
335 {
336  return tls_vfts[ctx->tls_ctx_engine].ctx_init_server (ctx);
337 }
338 
339 static inline int
341 {
342  return tls_vfts[ctx->tls_ctx_engine].ctx_init_client (ctx);
343 }
344 
345 static inline int
347 {
348  return tls_vfts[ctx->tls_ctx_engine].ctx_write (ctx, app_session);
349 }
350 
351 static inline int
353 {
354  return tls_vfts[ctx->tls_ctx_engine].ctx_read (ctx, tls_session);
355 }
356 
357 static inline u8
359 {
360  return tls_vfts[ctx->tls_ctx_engine].ctx_handshake_is_over (ctx);
361 }
362 
363 void
365 {
366  clib_warning ("called...");
367 }
368 
369 int
370 tls_add_segment_callback (u32 client_index, const ssvm_private_t * fs)
371 {
372  /* No-op for builtin */
373  return 0;
374 }
375 
376 int
377 tls_del_segment_callback (u32 client_index, const ssvm_private_t * fs)
378 {
379  return 0;
380 }
381 
382 void
384 {
385  stream_session_t *app_session;
386  tls_ctx_t *ctx;
387  app_worker_t *app_wrk;
388  application_t *app;
389 
390  ctx = tls_ctx_get (tls_session->opaque);
391  if (!tls_ctx_handshake_is_over (ctx))
392  {
393  stream_session_disconnect (tls_session);
394  return;
395  }
396  ctx->is_passive_close = 1;
397  app_wrk = app_worker_get (ctx->parent_app_index);
398  app = application_get (app_wrk->app_index);
399  app_session = session_get_from_handle (ctx->app_session_handle);
400  app->cb_fns.session_disconnect_callback (app_session);
401 }
402 
403 int
405 {
406  stream_session_t *tls_listener;
407  tls_ctx_t *lctx, *ctx;
408  u32 ctx_handle;
409 
410  tls_listener = listen_session_get (tls_session->listener_index);
411  lctx = tls_listener_ctx_get (tls_listener->opaque);
412 
413  ctx_handle = tls_ctx_alloc (lctx->tls_ctx_engine);
414  ctx = tls_ctx_get (ctx_handle);
415  memcpy (ctx, lctx, sizeof (*lctx));
416  ctx->c_thread_index = vlib_get_thread_index ();
417  ctx->tls_ctx_handle = ctx_handle;
418  tls_session->session_state = SESSION_STATE_READY;
419  tls_session->opaque = ctx_handle;
420  ctx->tls_session_handle = session_handle (tls_session);
421  ctx->listener_ctx_index = tls_listener->opaque;
422 
423  TLS_DBG (1, "Accept on listener %u new connection [%u]%x",
424  tls_listener->opaque, vlib_get_thread_index (), ctx_handle);
425 
426  return tls_ctx_init_server (ctx);
427 }
428 
429 int
431 {
432  tls_ctx_t *ctx;
433  if (PREDICT_FALSE (app_session->session_state == SESSION_STATE_CLOSED))
434  return 0;
435  ctx = tls_ctx_get (app_session->connection_index);
436  tls_ctx_write (ctx, app_session);
437  return 0;
438 }
439 
440 int
442 {
443  tls_ctx_t *ctx;
444 
445  ctx = tls_ctx_get (tls_session->opaque);
446  tls_ctx_read (ctx, tls_session);
447  return 0;
448 }
449 
450 int
451 tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index,
452  stream_session_t * tls_session, u8 is_fail)
453 {
454  tls_ctx_t *ho_ctx, *ctx;
455  u32 ctx_handle;
456 
457  ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
458 
459  if (is_fail)
460  {
461  int (*cb_fn) (u32, u32, stream_session_t *, u8), rv = 0;
462  u32 wrk_index, api_context;
463  app_worker_t *app_wrk;
464  application_t *app;
465 
466  wrk_index = ho_ctx->parent_app_index;
467  app_wrk = app_worker_get_if_valid (ho_ctx->parent_app_index);
468  if (app_wrk)
469  {
470  api_context = ho_ctx->c_s_index;
471  app = application_get (app_wrk->app_index);
472  cb_fn = app->cb_fns.session_connected_callback;
473  rv = cb_fn (wrk_index, api_context, 0, 1 /* failed */ );
474  }
476  tls_ctx_half_open_free (ho_ctx_index);
477  return rv;
478  }
479 
480  ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine);
481  ctx = tls_ctx_get (ctx_handle);
482  clib_memcpy (ctx, ho_ctx, sizeof (*ctx));
484  tls_ctx_half_open_free (ho_ctx_index);
485 
486  ctx->c_thread_index = vlib_get_thread_index ();
487  ctx->tls_ctx_handle = ctx_handle;
488 
489  TLS_DBG (1, "TCP connect for %u returned %u. New connection [%u]%x",
490  ho_ctx_index, is_fail, vlib_get_thread_index (),
491  (ctx) ? ctx_handle : ~0);
492 
493  ctx->tls_session_handle = session_handle (tls_session);
494  tls_session->opaque = ctx_handle;
495  tls_session->session_state = SESSION_STATE_READY;
496 
497  return tls_ctx_init_client (ctx);
498 }
499 
500 /* *INDENT-OFF* */
502  .session_accept_callback = tls_session_accept_callback,
503  .session_disconnect_callback = tls_session_disconnect_callback,
504  .session_connected_callback = tls_session_connected_callback,
505  .session_reset_callback = tls_session_reset_callback,
506  .add_segment_callback = tls_add_segment_callback,
507  .del_segment_callback = tls_del_segment_callback,
508  .builtin_app_rx_callback = tls_app_rx_callback,
509  .builtin_app_tx_callback = tls_app_tx_callback,
510 };
511 /* *INDENT-ON* */
512 
513 int
515 {
516  vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
518  tls_engine_type_t engine_type;
519  tls_main_t *tm = &tls_main;
520  app_worker_t *app_wrk;
521  clib_error_t *error;
522  application_t *app;
523  tls_ctx_t *ctx;
524  u32 ctx_index;
525 
526  sep = (session_endpoint_extended_t *) tep;
527  app_wrk = app_worker_get (sep->app_wrk_index);
528  app = application_get (app_wrk->app_index);
529  engine_type = tls_get_engine_type (app->tls_engine);
530  if (engine_type == TLS_ENGINE_NONE)
531  {
532  clib_warning ("No tls engine_type available");
533  return -1;
534  }
535 
536  ctx_index = tls_ctx_half_open_alloc ();
537  ctx = tls_ctx_half_open_get (ctx_index);
538  ctx->parent_app_index = sep->app_wrk_index;
539  ctx->parent_app_api_context = sep->opaque;
540  ctx->tcp_is_ip4 = sep->is_ip4;
541  if (sep->hostname)
542  {
543  ctx->srv_hostname = format (0, "%v", sep->hostname);
545  }
547 
549  ctx->tls_ctx_engine = engine_type;
550 
551  clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_t));
552  cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
553  cargs->app_index = tm->app_index;
554  cargs->api_context = ctx_index;
555  if ((error = vnet_connect (cargs)))
556  return clib_error_get_code (error);
557 
558  TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
559  return 0;
560 }
561 
562 void
563 tls_disconnect (u32 ctx_handle, u32 thread_index)
564 {
565  tls_ctx_t *ctx;
566 
567  TLS_DBG (1, "Disconnecting %x", ctx_handle);
568 
569  ctx = tls_ctx_get (ctx_handle);
572  tls_ctx_free (ctx);
573 }
574 
575 u32
576 tls_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
577 {
578  vnet_bind_args_t _bargs, *args = &_bargs;
579  app_worker_t *app_wrk;
580  tls_main_t *tm = &tls_main;
581  session_handle_t tls_handle;
583  stream_session_t *tls_listener;
584  stream_session_t *app_listener;
585  tls_engine_type_t engine_type;
586  application_t *app;
587  tls_ctx_t *lctx;
588  u32 lctx_index;
589 
590  sep = (session_endpoint_extended_t *) tep;
591  app_wrk = app_worker_get (sep->app_wrk_index);
592  app = application_get (app_wrk->app_index);
593  engine_type = tls_get_engine_type (app->tls_engine);
594  if (engine_type == TLS_ENGINE_NONE)
595  {
596  clib_warning ("No tls engine_type available");
597  return -1;
598  }
599 
600  sep->transport_proto = TRANSPORT_PROTO_TCP;
601  memset (args, 0, sizeof (*args));
602  args->app_index = tm->app_index;
603  args->sep_ext = *sep;
604  if (vnet_bind (args))
605  return -1;
606 
607  tls_handle = args->handle;
608  lctx_index = tls_listener_ctx_alloc ();
609  tls_listener = listen_session_get_from_handle (tls_handle);
610  tls_listener->opaque = lctx_index;
611 
612  app_listener = listen_session_get (app_listener_index);
613 
614  lctx = tls_listener_ctx_get (lctx_index);
615  lctx->parent_app_index = sep->app_wrk_index;
616  lctx->tls_session_handle = tls_handle;
617  lctx->app_session_handle = listen_session_get_handle (app_listener);
618  lctx->tcp_is_ip4 = sep->is_ip4;
619  lctx->tls_ctx_engine = engine_type;
620 
621  tls_vfts[engine_type].ctx_start_listen (lctx);
622 
623  TLS_DBG (1, "Started listening %d, engine type %d", lctx_index,
624  engine_type);
625  return lctx_index;
626 }
627 
628 u32
629 tls_stop_listen (u32 lctx_index)
630 {
631  tls_engine_type_t engine_type;
632  tls_ctx_t *lctx;
633 
634  lctx = tls_listener_ctx_get (lctx_index);
636  .handle = lctx->tls_session_handle,
637  .app_index = tls_main.app_index,
638  .wrk_map_index = 0 /* default wrk */
639  };
640  if (vnet_unbind (&a))
641  clib_warning ("unbind returned");
642 
643  engine_type = lctx->tls_ctx_engine;
644  tls_vfts[engine_type].ctx_stop_listen (lctx);
645 
646  tls_listener_ctx_free (lctx);
647  return 0;
648 }
649 
651 tls_connection_get (u32 ctx_index, u32 thread_index)
652 {
653  tls_ctx_t *ctx;
654  ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
655  return &ctx->connection;
656 }
657 
659 tls_listener_get (u32 listener_index)
660 {
661  tls_ctx_t *ctx;
662  ctx = tls_listener_ctx_get (listener_index);
663  return &ctx->connection;
664 }
665 
666 u8 *
667 format_tls_ctx (u8 * s, va_list * args)
668 {
669  tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *);
670  u32 thread_index = va_arg (*args, u32);
671  u32 child_si, child_ti;
672 
673  session_parse_handle (ctx->tls_session_handle, &child_si, &child_ti);
674  if (thread_index != child_ti)
675  clib_warning ("app and tls sessions are on different threads!");
676 
677  s = format (s, "[#%d][TLS] app %u child %u", child_ti,
678  ctx->parent_app_index, child_si);
679  return s;
680 }
681 
682 u8 *
683 format_tls_connection (u8 * s, va_list * args)
684 {
685  u32 ctx_index = va_arg (*args, u32);
686  u32 thread_index = va_arg (*args, u32);
687  u32 verbose = va_arg (*args, u32);
688  tls_ctx_t *ctx;
689 
690  ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
691  if (!ctx)
692  return s;
693 
694  s = format (s, "%-50U", format_tls_ctx, ctx, thread_index);
695  if (verbose)
696  {
697  stream_session_t *ts;
698  ts = session_get_from_handle (ctx->app_session_handle);
699  s = format (s, "state: %-7u", ts->session_state);
700  if (verbose > 1)
701  s = format (s, "\n");
702  }
703  return s;
704 }
705 
706 u8 *
707 format_tls_listener (u8 * s, va_list * args)
708 {
709  u32 tc_index = va_arg (*args, u32);
710  tls_ctx_t *ctx = tls_listener_ctx_get (tc_index);
711  u32 listener_index, thread_index;
712 
713  listen_session_parse_handle (ctx->tls_session_handle, &listener_index,
714  &thread_index);
715  return format (s, "[TLS] listener app %u child %u", ctx->parent_app_index,
716  listener_index);
717 }
718 
719 u8 *
720 format_tls_half_open (u8 * s, va_list * args)
721 {
722  u32 tc_index = va_arg (*args, u32);
723  tls_ctx_t *ctx = tls_ctx_half_open_get (tc_index);
724  s = format (s, "[TLS] half-open app %u", ctx->parent_app_index);
726  return s;
727 }
728 
729 /* *INDENT-OFF* */
731  .open = tls_connect,
732  .close = tls_disconnect,
733  .bind = tls_start_listen,
734  .get_connection = tls_connection_get,
735  .get_listener = tls_listener_get,
736  .unbind = tls_stop_listen,
737  .tx_type = TRANSPORT_TX_INTERNAL,
738  .service_type = TRANSPORT_SERVICE_APP,
739  .format_connection = format_tls_connection,
740  .format_half_open = format_tls_half_open,
741  .format_listener = format_tls_listener,
742 };
743 /* *INDENT-ON* */
744 
745 void
747 {
748  vec_validate (tls_vfts, type);
749  tls_vfts[type] = *vft;
750 }
751 
752 static clib_error_t *
754 {
756  vnet_app_attach_args_t _a, *a = &_a;
757  u64 options[APP_OPTIONS_N_OPTIONS];
758  u32 segment_size = 512 << 20;
759  tls_main_t *tm = &tls_main;
760  u32 fifo_size = 64 << 10;
761  u32 num_threads;
762 
763  num_threads = 1 /* main thread */ + vtm->n_threads;
764 
765  memset (a, 0, sizeof (*a));
766  memset (options, 0, sizeof (options));
767 
768  a->session_cb_vft = &tls_app_cb_vft;
769  a->api_client_index = APP_INVALID_INDEX;
770  a->options = options;
771  a->name = format (0, "tls");
772  a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
773  a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
774  a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
775  a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
776  a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
777  a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
778 
779  if (vnet_application_attach (a))
780  {
781  clib_warning ("failed to attach tls app");
782  return clib_error_return (0, "failed to attach tls app");
783  }
784 
785  if (!tm->ca_cert_path)
787 
788  tm->app_index = a->app_index;
790 
791  vec_validate (tm->rx_bufs, num_threads - 1);
792  vec_validate (tm->tx_bufs, num_threads - 1);
793 
795  FIB_PROTOCOL_IP4, ~0);
797  FIB_PROTOCOL_IP6, ~0);
798  vec_free (a->name);
799  return 0;
800 }
801 
803 
804 static clib_error_t *
806 {
807  tls_main_t *tm = &tls_main;
809  {
810  if (unformat (input, "use-test-cert-in-ca"))
811  tm->use_test_cert_in_ca = 1;
812  else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path))
813  ;
814  else
815  return clib_error_return (0, "unknown input `%U'",
816  format_unformat_error, input);
817  }
818  return 0;
819 }
820 
822 
823 tls_main_t *
825 {
826  return &tls_main;
827 }
828 
829 /*
830  * fd.io coding-style-patch-verification: ON
831  *
832  * Local Variables:
833  * eval: (c-set-style "gnu")
834  * End:
835  */
int tls_add_segment_callback(u32 client_index, const ssvm_private_t *fs)
Definition: tls.c:370
tls_main_t * vnet_tls_get_main(void)
Definition: tls.c:824
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
static void clib_rwlock_reader_lock(clib_rwlock_t *p)
Definition: lock.h:139
static tls_main_t tls_main
Definition: tls.c:20
void tls_session_reset_callback(stream_session_t *s)
Definition: tls.c:364
app_worker_t * app_worker_get(u32 wrk_index)
Definition: application.c:499
static tls_engine_type_t tls_get_engine_type(tls_engine_type_t preferred)
Definition: tls.c:295
enum tls_engine_type_ tls_engine_type_t
int tls_session_accept_callback(stream_session_t *tls_session)
Definition: tls.c:404
a
Definition: bitmap.h:538
static const transport_proto_vft_t tls_proto
Definition: tls.c:730
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
Definition: lock.h:177
#define TLS_ENGINE_TYPE_SHIFT
Definition: tls.c:25
struct _transport_connection transport_connection_t
struct _vnet_connect_args vnet_connect_args_t
u32 tls_ctx_half_open_alloc(void)
Definition: tls.c:124
#define PREDICT_TRUE(x)
Definition: clib.h:108
u32 tls_listener_ctx_alloc(void)
Definition: tls.c:93
int tls_app_rx_callback(stream_session_t *tls_session)
Definition: tls.c:441
clib_rwlock_t half_open_rwlock
Definition: tls.h:82
unsigned long u64
Definition: types.h:89
static int tls_add_app_q_evt(app_worker_t *app, stream_session_t *app_session)
Definition: tls.c:87
char * ca_cert_path
Definition: tls.h:90
int(* ctx_init_server)(tls_ctx_t *ctx)
Definition: tls.h:100
clib_error_t * vnet_unbind(vnet_unbind_args_t *a)
static clib_error_t * tls_config_fn(vlib_main_t *vm, unformat_input_t *input)
Definition: tls.c:805
#define pool_get_aligned_will_expand(P, YESNO, A)
See if pool_get will expand the pool or not.
Definition: pool.h:231
struct _transport_proto_vft transport_proto_vft_t
#define vec_terminate_c_string(V)
(If necessary) NULL terminate a vector containing a c-string.
Definition: vec.h:1016
int tls_connect(transport_endpoint_t *tep)
Definition: tls.c:514
tls_ctx_t *(* ctx_get_w_thread)(u32 ctx_index, u8 thread_index)
Definition: tls.h:98
u8 * format_tls_connection(u8 *s, va_list *args)
Definition: tls.c:683
int i
void tls_notify_app_enqueue(tls_ctx_t *ctx, stream_session_t *app_session)
Definition: tls.c:181
void(* ctx_free)(tls_ctx_t *ctx)
Definition: tls.h:96
int(* ctx_init_client)(tls_ctx_t *ctx)
Definition: tls.h:99
u8 use_test_cert_in_ca
Definition: tls.h:89
transport_connection_t * tls_connection_get(u32 ctx_index, u32 thread_index)
Definition: tls.c:651
static void session_parse_handle(session_handle_t handle, u32 *index, u32 *thread_index)
Definition: session.h:352
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
u64 session_handle_t
Definition: session.h:107
static stream_session_t * session_get_from_handle(session_handle_t handle)
Definition: session.h:360
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:228
static tls_ctx_t * tls_ctx_get_w_thread(u32 ctx_handle, u8 thread_index)
Definition: tls.c:326
unsigned char u8
Definition: types.h:56
static stream_session_t * listen_session_get_from_handle(session_handle_t handle)
Definition: session.h:606
app transport service
u32 app_index
Definition: tls.h:79
segment_manager_t * app_worker_get_listen_segment_manager(app_worker_t *app, stream_session_t *listener)
Definition: application.c:874
static void tls_ctx_free(tls_ctx_t *ctx)
Definition: tls.c:311
memset(h->entries, 0, sizeof(h->entries[0])*entries)
void tls_listener_ctx_free(tls_ctx_t *ctx)
Definition: tls.c:104
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
int(* ctx_read)(tls_ctx_t *ctx, stream_session_t *tls_session)
Definition: tls.h:101
struct _vnet_disconnect_args_t vnet_disconnect_args_t
int tls_add_vpp_q_builtin_tx_evt(stream_session_t *s)
Definition: tls.c:78
int tls_app_tx_callback(stream_session_t *app_session)
Definition: tls.c:430
u8 * format_tls_listener(u8 *s, va_list *args)
Definition: tls.c:707
static tls_engine_vft_t * tls_vfts
Definition: tls.c:21
struct _stream_session_cb_vft session_cb_vft_t
struct _vnet_unbind_args_t vnet_unbind_args_t
int tls_add_vpp_q_rx_evt(stream_session_t *s)
Definition: tls.c:54
static stream_session_t * listen_session_get(u32 index)
Definition: session.h:629
u8 is_passive_close
Definition: tls.h:72
#define clib_error_return(e, args...)
Definition: error.h:99
static void tls_ctx_parse_handle(u32 ctx_handle, u32 *ctx_index, u32 *engine_type)
Definition: tls.c:288
void stream_session_delete_notify(transport_connection_t *tc)
Notification from transport that connection is being deleted.
Definition: session.c:799
unsigned int u32
Definition: types.h:88
struct _stream_session_t stream_session_t
int session_send_io_evt_to_thread(svm_fifo_t *f, session_evt_type_t evt_type)
Definition: session.c:87
tls_ctx_t *(* ctx_get)(u32 ctx_index)
Definition: tls.h:97
static u8 tls_ctx_handshake_is_over(tls_ctx_t *ctx)
Definition: tls.c:358
u8 tls_engine
Preferred tls engine.
Definition: application.h:167
struct _vnet_app_attach_args_t vnet_app_attach_args_t
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:464
static void clib_rwlock_init(clib_rwlock_t *p)
Definition: lock.h:122
#define TLS_IDX_MASK
Definition: tls.c:24
struct _session_endpoint session_endpoint_t
u32 tls_listener_ctx_index(tls_ctx_t *ctx)
Definition: tls.c:118
static void clib_rwlock_reader_unlock(clib_rwlock_t *p)
Definition: lock.h:157
u8(* ctx_handshake_is_over)(tls_ctx_t *ctx)
Definition: tls.h:103
long ctx[MAX_CONNS]
Definition: main.c:144
void tls_ctx_half_open_reader_unlock()
Definition: tls.c:169
app_worker_t * app_worker_get_if_valid(u32 wrk_index)
Definition: application.c:505
struct _unformat_input_t unformat_input_t
int tls_add_vpp_q_tx_evt(stream_session_t *s)
Definition: tls.c:70
Definition: tls.h:77
static session_handle_t session_handle(stream_session_t *s)
Definition: session.h:334
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:274
#define APP_INVALID_INDEX
Definition: application.h:218
int tls_notify_app_connected(tls_ctx_t *ctx, u8 is_failed)
Definition: tls.c:232
#define PREDICT_FALSE(x)
Definition: clib.h:107
int(* ctx_start_listen)(tls_ctx_t *ctx)
Definition: tls.h:104
clib_error_t * vnet_connect(vnet_connect_args_t *a)
Definition: tls.h:53
u32(* ctx_alloc)(void)
Definition: tls.h:95
int session_alloc_fifos(segment_manager_t *sm, stream_session_t *s)
Definition: session.c:165
static void clib_rwlock_writer_unlock(clib_rwlock_t *p)
Definition: lock.h:185
static u8 svm_fifo_set_event(svm_fifo_t *f)
Sets fifo event flag.
Definition: svm_fifo.h:157
u8 * format_tls_ctx(u8 *s, va_list *args)
Definition: tls.c:667
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:216
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
u8 ** rx_bufs
Definition: tls.h:83
tls_engine_type_t tls_get_available_engine(void)
Definition: tls.c:42
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:211
void tls_disconnect(u32 ctx_handle, u32 thread_index)
Definition: tls.c:563
int tls_del_segment_callback(u32 client_index, const ssvm_private_t *fs)
Definition: tls.c:377
static session_type_t session_type_from_proto_and_ip(transport_proto_t proto, u8 is_ip4)
Definition: session.h:410
vlib_main_t * vm
Definition: buffer.c:294
transport_connection_t * tls_listener_get(u32 listener_index)
Definition: tls.c:659
static int tls_ctx_read(tls_ctx_t *ctx, stream_session_t *tls_session)
Definition: tls.c:352
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
#define clib_warning(format, args...)
Definition: error.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:75
apps acting as transports
transport_connection_t connection
Definition: tls.h:57
static int tls_ctx_init_client(tls_ctx_t *ctx)
Definition: tls.c:340
int vnet_disconnect_session(vnet_disconnect_args_t *a)
void tls_register_engine(const tls_engine_vft_t *vft, tls_engine_type_t type)
Definition: tls.c:746
application_t * application_get(u32 app_index)
Definition: application.c:243
void transport_register_protocol(transport_proto_t transport_proto, const transport_proto_vft_t *vft, fib_protocol_t fib_proto, u32 output_node)
Register transport virtual function table.
Definition: transport.c:173
static tls_ctx_t * tls_ctx_get(u32 ctx_handle)
Definition: tls.c:318
int app_worker_lock_and_send_event(app_worker_t *app, stream_session_t *s, u8 evt_type)
Send event to application.
Definition: application.c:1339
static int tls_ctx_init_server(tls_ctx_t *ctx)
Definition: tls.c:334
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:299
clib_error_t * vnet_bind(vnet_bind_args_t *a)
int(* ctx_stop_listen)(tls_ctx_t *ctx)
Definition: tls.h:105
session_cb_vft_t cb_fns
Callbacks: shoulder-taps for the server/client.
Definition: application.h:131
int app_worker_alloc_connects_segment_manager(app_worker_t *app_wrk)
Definition: application.c:852
void stream_session_disconnect(stream_session_t *s)
Initialize session disconnect.
Definition: session.c:1066
tls_ctx_t * tls_listener_ctx_get(u32 ctx_index)
Definition: tls.c:112
tls_ctx_t * half_open_ctx_pool
Definition: tls.h:81
stream_session_t * session_alloc(u32 thread_index)
Definition: session.c:122
u8 ** tx_bufs
Definition: tls.h:84
u32 tls_stop_listen(u32 lctx_index)
Definition: tls.c:629
int tls_session_connected_callback(u32 tls_app_index, u32 ho_ctx_index, stream_session_t *tls_session, u8 is_fail)
Definition: tls.c:451
u32 tls_ctx_half_open_index(tls_ctx_t *ctx)
Definition: tls.c:175
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
clib_error_t * vnet_application_attach(vnet_app_attach_args_t *a)
Attach application to vpp.
int(* ctx_write)(tls_ctx_t *ctx, stream_session_t *app_session)
Definition: tls.h:102
int tls_add_vpp_q_builtin_rx_evt(stream_session_t *s)
Definition: tls.c:62
struct _transport_endpoint transport_endpoint_t
#define clib_error_get_code(err)
Definition: error.h:77
struct _segment_manager segment_manager_t
u8 * srv_hostname
Definition: tls.h:74
int session_send_io_evt_to_thread_custom(void *data, u32 thread_index, session_evt_type_t evt_type)
Definition: session.c:93
int tls_notify_app_accept(tls_ctx_t *ctx)
Definition: tls.c:190
u32 app_index
Index of owning app.
Definition: application.h:72
int session_lookup_add_connection(transport_connection_t *tc, u64 value)
Add transport connection to a session table.
void tls_ctx_half_open_free(u32 ho_index)
Definition: tls.c:152
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static u32 vlib_num_workers()
Definition: threads.h:365
static u32 tls_ctx_alloc(tls_engine_type_t engine_type)
Definition: tls.c:303
void tls_session_disconnect_callback(stream_session_t *tls_session)
Definition: tls.c:383
static void tls_disconnect_transport(tls_ctx_t *ctx)
Definition: tls.c:30
static int tls_ctx_write(tls_ctx_t *ctx, stream_session_t *app_session)
Definition: tls.c:346
#define TLS_CA_CERT_PATH
Definition: tls.h:28
segment_manager_t * app_worker_get_connect_segment_manager(app_worker_t *app)
Definition: application.c:867
u8 * format_tls_half_open(u8 *s, va_list *args)
Definition: tls.c:720
static clib_error_t * tls_init(vlib_main_t *vm)
Definition: tls.c:753
static session_cb_vft_t tls_app_cb_vft
Definition: tls.c:501
struct _vnet_bind_args_t vnet_bind_args_t
static u64 listen_session_get_handle(stream_session_t *s)
Definition: session.h:599
#define TLS_DBG(_lvl, _fmt, _args...)
Definition: tls.h:35
u32 tls_start_listen(u32 app_listener_index, transport_endpoint_t *tep)
Definition: tls.c:576
struct _session_endpoint_extended session_endpoint_extended_t
tls_ctx_t * listener_ctx_pool
Definition: tls.h:80
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
tls_ctx_t * tls_ctx_half_open_get(u32 ctx_index)
Definition: tls.c:161
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
static void listen_session_parse_handle(session_handle_t handle, u32 *index, u32 *thread_index)
Definition: session.h:612