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