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