tls: fix multi threaded medium scale test (VPP-1457)

- ensure session enqueue epoch does not wrap between two enqueues
- use 3 states for echo clients app, to distinguish between starting and
closing phases
- force tcp fin retransmit if out of buffers while sending a fin

Change-Id: I6f2cab46affd1148aba2a33fb6d58bcc54f32805
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session-apps/echo_client.c b/src/vnet/session-apps/echo_client.c
index b47dcf2..0258c16 100644
--- a/src/vnet/session-apps/echo_client.c
+++ b/src/vnet/session-apps/echo_client.c
@@ -208,7 +208,7 @@
   connections_this_batch =
     ecm->connections_this_batch_by_thread[my_thread_index];
 
-  if ((ecm->run_test == 0) ||
+  if ((ecm->run_test != ECHO_CLIENTS_RUNNING) ||
       ((vec_len (connection_indices) == 0)
        && vec_len (connections_this_batch) == 0))
     return 0;
@@ -352,6 +352,16 @@
   return 0;
 }
 
+static void
+echo_clients_session_disconnect (stream_session_t * s)
+{
+  echo_client_main_t *ecm = &echo_client_main;
+  vnet_disconnect_args_t _a, *a = &_a;
+  a->handle = session_handle (s);
+  a->app_index = ecm->app_index;
+  vnet_disconnect_session (a);
+}
+
 static int
 echo_clients_session_connected_callback (u32 app_index, u32 api_context,
 					 stream_session_t * s, u8 is_fail)
@@ -361,6 +371,9 @@
   u32 session_index;
   u8 thread_index;
 
+  if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_STARTING))
+    return -1;
+
   if (is_fail)
     {
       clib_warning ("connection %d failed!", api_context);
@@ -407,7 +420,7 @@
   __sync_fetch_and_add (&ecm->ready_connections, 1);
   if (ecm->ready_connections == ecm->expected_connections)
     {
-      ecm->run_test = 1;
+      ecm->run_test = ECHO_CLIENTS_RUNNING;
       /* Signal the CLI process that the action is starting... */
       signal_evt_to_cli (1);
     }
@@ -447,6 +460,12 @@
   echo_client_main_t *ecm = &echo_client_main;
   eclient_session_t *sp;
 
+  if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_RUNNING))
+    {
+      echo_clients_session_disconnect (s);
+      return -1;
+    }
+
   sp = pool_elt_at_index (ecm->sessions,
 			  s->server_rx_fifo->client_session_index);
   receive_data_chunk (ecm, sp);
@@ -624,6 +643,7 @@
   ecm->vlib_main = vm;
   ecm->tls_engine = TLS_ENGINE_OPENSSL;
   ecm->no_copy = 0;
+  ecm->run_test = ECHO_CLIENTS_STARTING;
 
   if (thread_main->n_vlib_mains > 1)
     clib_spinlock_init (&ecm->sessions_lock);
@@ -825,7 +845,7 @@
     error = clib_error_return (0, "failed: test bytes");
 
 cleanup:
-  ecm->run_test = 0;
+  ecm->run_test = ECHO_CLIENTS_EXITING;
   vlib_process_wait_for_event_or_clock (vm, 10e-3);
   for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
     {