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