tls: fixes and improvements

- disconnect sessions through session layer apis instead of directly
notifying the app worker.
- improve cli
- increase fifo sizes for tls app

Change-Id: I8a7d2865b3b00724e2a9da29fa4a906ea867da9b
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session/application.h b/src/vnet/session/application.h
index ebb3cf5..3e1590e 100644
--- a/src/vnet/session/application.h
+++ b/src/vnet/session/application.h
@@ -222,6 +222,8 @@
 int app_worker_init_connected (app_worker_t * app_wrk, session_t * s);
 int app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
 			       u32 opaque);
+int app_worker_close_notify (app_worker_t * app_wrk, session_t * s);
+int app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s);
 segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
 							  session_t *);
 segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
diff --git a/src/vnet/session/application_worker.c b/src/vnet/session/application_worker.c
index 85a6fed..6b294ec 100644
--- a/src/vnet/session/application_worker.c
+++ b/src/vnet/session/application_worker.c
@@ -322,6 +322,22 @@
 }
 
 int
+app_worker_close_notify (app_worker_t * app_wrk, session_t * s)
+{
+  application_t *app = application_get (app_wrk->app_index);
+  app->cb_fns.session_disconnect_callback (s);
+  return 0;
+}
+
+int
+app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s)
+{
+  application_t *app = application_get (app_wrk->app_index);
+  app->cb_fns.builtin_app_rx_callback (s);
+  return 0;
+}
+
+int
 app_worker_own_session (app_worker_t * app_wrk, session_t * s)
 {
   segment_manager_t *sm;
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
index 0a294dc..c9eea1d 100644
--- a/src/vnet/session/session.c
+++ b/src/vnet/session/session.c
@@ -715,18 +715,14 @@
 session_transport_closing_notify (transport_connection_t * tc)
 {
   app_worker_t *app_wrk;
-  application_t *app;
   session_t *s;
 
   s = session_get (tc->s_index, tc->thread_index);
   if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
     return;
   s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
-  app_wrk = app_worker_get_if_valid (s->app_wrk_index);
-  if (!app_wrk)
-    return;
-  app = application_get (app_wrk->app_index);
-  app->cb_fns.session_disconnect_callback (s);
+  app_wrk = app_worker_get (s->app_wrk_index);
+  app_worker_close_notify (app_wrk, s);
 }
 
 /**
diff --git a/src/vnet/session/session_node.c b/src/vnet/session/session_node.c
index a7ff214..8167947 100644
--- a/src/vnet/session/session_node.c
+++ b/src/vnet/session/session_node.c
@@ -856,7 +856,6 @@
   f64 now = vlib_time_now (vm);
   int n_tx_packets = 0, i, rv;
   app_worker_t *app_wrk;
-  application_t *app;
   svm_msg_q_t *mq;
   void (*fp) (void *);
 
@@ -973,8 +972,7 @@
 	    continue;
 	  svm_fifo_unset_event (s->rx_fifo);
 	  app_wrk = app_worker_get (s->app_wrk_index);
-	  app = application_get (app_wrk->app_index);
-	  app->cb_fns.builtin_app_rx_callback (s);
+	  app_worker_builtin_rx (app_wrk, s);
 	  break;
 	case SESSION_IO_EVT_BUILTIN_TX:
 	  s = session_get_from_handle_if_valid (e->session_handle);
diff --git a/src/vnet/tls/tls.c b/src/vnet/tls/tls.c
index c64a6d1..714ebfe 100644
--- a/src/vnet/tls/tls.c
+++ b/src/vnet/tls/tls.c
@@ -180,10 +180,10 @@
 void
 tls_notify_app_enqueue (tls_ctx_t * ctx, session_t * app_session)
 {
-  app_worker_t *app;
-  app = app_worker_get_if_valid (app_session->app_wrk_index);
-  if (PREDICT_TRUE (app != 0))
-    tls_add_app_q_evt (app, app_session);
+  app_worker_t *app_wrk;
+  app_wrk = app_worker_get_if_valid (app_session->app_wrk_index);
+  if (PREDICT_TRUE (app_wrk != 0))
+    tls_add_app_q_evt (app_wrk, app_session);
 }
 
 int
@@ -362,10 +362,10 @@
 void
 tls_session_disconnect_callback (session_t * tls_session)
 {
-  session_t *app_session;
   tls_ctx_t *ctx;
-  app_worker_t *app_wrk;
-  application_t *app;
+
+  TLS_DBG (1, "TCP disconnecting handle %x session %u", tls_session->opaque,
+	   tls_session->session_index);
 
   ctx = tls_ctx_get (tls_session->opaque);
   if (!tls_ctx_handshake_is_over (ctx))
@@ -374,10 +374,7 @@
       return;
     }
   ctx->is_passive_close = 1;
-  app_wrk = app_worker_get (ctx->parent_app_wrk_index);
-  app = application_get (app_wrk->app_index);
-  app_session = session_get_from_handle (ctx->app_session_handle);
-  app->cb_fns.session_disconnect_callback (app_session);
+  session_transport_closing_notify (&ctx->connection);
 }
 
 int
@@ -654,8 +651,11 @@
 {
   session_t *app_session = (session_t *) session;
   tls_ctx_t *ctx;
-  if (PREDICT_FALSE (app_session->session_state == SESSION_STATE_CLOSED))
+
+  if (PREDICT_FALSE (app_session->session_state
+		     >= SESSION_STATE_TRANSPORT_CLOSED))
     return 0;
+
   ctx = tls_ctx_get (app_session->connection_index);
   tls_ctx_write (ctx, app_session);
   return 0;
@@ -664,16 +664,16 @@
 u8 *
 format_tls_ctx (u8 * s, va_list * args)
 {
+  u32 tcp_si, tcp_ti, ctx_index, ctx_engine, app_si, app_ti;
   tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *);
-  u32 thread_index = va_arg (*args, u32);
-  u32 child_si, child_ti;
 
-  session_parse_handle (ctx->tls_session_handle, &child_si, &child_ti);
-  if (thread_index != child_ti)
-    clib_warning ("app and tls sessions are on different threads!");
+  session_parse_handle (ctx->tls_session_handle, &tcp_si, &tcp_ti);
+  tls_ctx_parse_handle (ctx->tls_ctx_handle, &ctx_index, &ctx_engine);
+  session_parse_handle (ctx->app_session_handle, &app_si, &app_ti);
+  s = format (s, "[%d:%d][TLS] app_wrk %u index %u engine %u tcp %d:%d",
+	      app_ti, app_si, ctx->parent_app_wrk_index, ctx_index,
+	      ctx_engine, tcp_ti, tcp_si);
 
-  s = format (s, "[#%d][TLS] app %u child %u", child_ti,
-	      ctx->parent_app_wrk_index, child_si);
   return s;
 }
 
@@ -689,7 +689,7 @@
   if (!ctx)
     return s;
 
-  s = format (s, "%-50U", format_tls_ctx, ctx, thread_index);
+  s = format (s, "%-50U", format_tls_ctx, ctx);
   if (verbose)
     {
       session_t *ts;
@@ -707,12 +707,17 @@
   u32 tc_index = va_arg (*args, u32);
   u32 __clib_unused verbose = va_arg (*args, u32);
   tls_ctx_t *ctx = tls_listener_ctx_get (tc_index);
-  u32 listener_index, thread_index;
+  session_t *tls_listener;
+  app_listener_t *al;
+  u32 app_si, app_ti;
 
-  listen_session_parse_handle (ctx->tls_session_handle, &listener_index,
-			       &thread_index);
-  return format (s, "[TLS] listener app %u child %u",
-		 ctx->parent_app_wrk_index, listener_index);
+  al = app_listener_get_w_handle (ctx->tls_session_handle);
+  tls_listener = app_listener_get_session (al);
+  session_parse_handle (ctx->app_session_handle, &app_si, &app_ti);
+  s = format (s, "[%d:%d][TLS] app_wrk %u engine %u tcp %d:%d",
+	      app_ti, app_si, ctx->parent_app_wrk_index, ctx->tls_ctx_engine,
+	      tls_listener->thread_index, tls_listener->session_index);
+  return s;
 }
 
 u8 *
@@ -755,9 +760,9 @@
   vlib_thread_main_t *vtm = vlib_get_thread_main ();
   vnet_app_attach_args_t _a, *a = &_a;
   u64 options[APP_OPTIONS_N_OPTIONS];
-  u32 segment_size = 512 << 20;
+  u32 segment_size = 2048 << 20;
   tls_main_t *tm = &tls_main;
-  u32 fifo_size = 64 << 10;
+  u32 fifo_size = 128 << 10;
   u32 num_threads;
 
   num_threads = 1 /* main thread */  + vtm->n_threads;