session: add session event log for session state
[vpp.git] / src / vnet / session / application_local.c
index 05b946e..d2a69dc 100644 (file)
@@ -304,12 +304,7 @@ ct_session_dealloc_fifos (ct_connection_t *ct, svm_fifo_t *rx_fifo,
        app_worker_del_segment_notify (app_wrk, ct->segment_handle);
     }
 
-  if (!segment_manager_app_detached (sm))
-    {
-      app_wrk = app_worker_get (ct->server_wrk);
-      app_worker_del_segment_notify (app_wrk, ct->segment_handle);
-    }
-
+  /* Notify server app and free segment */
   segment_manager_lock_and_del_segment (sm, seg_index);
 
   /* Cleanup segment manager if needed. If server detaches there's a chance
@@ -368,7 +363,7 @@ ct_session_connect_notify (session_t *ss, session_error_t err)
   ss = session_get (ss_index, thread_index);
   cs->session_type = ss->session_type;
   cs->listener_handle = SESSION_INVALID_HANDLE;
-  cs->session_state = SESSION_STATE_CONNECTING;
+  session_set_state (cs, SESSION_STATE_CONNECTING);
   cs->app_wrk_index = client_wrk->wrk_index;
   cs->connection_index = cct->c_c_index;
   cct->c_s_index = cs->session_index;
@@ -384,7 +379,7 @@ ct_session_connect_notify (session_t *ss, session_error_t err)
       goto connect_error;
     }
 
-  cs->session_state = SESSION_STATE_CONNECTING;
+  session_set_state (cs, SESSION_STATE_CONNECTING);
 
   if (app_worker_connect_notify (client_wrk, cs, 0, opaque))
     {
@@ -395,7 +390,7 @@ ct_session_connect_notify (session_t *ss, session_error_t err)
     }
 
   cs = session_get (cct->c_s_index, cct->c_thread_index);
-  cs->session_state = SESSION_STATE_READY;
+  session_set_state (cs, SESSION_STATE_READY);
 
   return 0;
 
@@ -626,10 +621,6 @@ ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct,
   ls->tx_fifo->shr->master_session_index = ls->session_index;
   ls->rx_fifo->master_thread_index = ls->thread_index;
   ls->tx_fifo->master_thread_index = ls->thread_index;
-  ls->rx_fifo->segment_manager = sm_index;
-  ls->tx_fifo->segment_manager = sm_index;
-  ls->rx_fifo->segment_index = fs_index;
-  ls->tx_fifo->segment_index = fs_index;
 
   seg_handle = segment_manager_segment_handle (sm, fs);
   segment_manager_segment_reader_unlock (sm);
@@ -710,7 +701,7 @@ ct_accept_one (u32 thread_index, u32 ho_index)
                                                     sct->c_is_ip4);
   ss->connection_index = sct->c_c_index;
   ss->listener_handle = listen_session_get_handle (ll);
-  ss->session_state = SESSION_STATE_CREATED;
+  session_set_state (ss, SESSION_STATE_CREATED);
 
   server_wrk = application_listener_select_worker (ll);
   ss->app_wrk_index = server_wrk->wrk_index;
@@ -735,7 +726,7 @@ ct_accept_one (u32 thread_index, u32 ho_index)
   cct->client_tx_fifo->refcnt++;
   cct->segment_handle = sct->segment_handle;
 
-  ss->session_state = SESSION_STATE_ACCEPTING;
+  session_set_state (ss, SESSION_STATE_ACCEPTING);
   if (app_worker_accept_notify (server_wrk, ss))
     {
       ct_session_connect_notify (ss, SESSION_E_REFUSED);
@@ -829,7 +820,7 @@ ct_connect (app_worker_t * client_wrk, session_t * ll,
 }
 
 static u32
-ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
+ct_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
 {
   session_endpoint_cfg_t *sep;
   ct_connection_t *ct;
@@ -952,15 +943,35 @@ global_scope:
   return SESSION_E_LOCAL_CONNECT;
 }
 
+static inline int
+ct_close_is_reset (ct_connection_t *ct, session_t *s)
+{
+  if (ct->flags & CT_CONN_F_CLIENT)
+    return (svm_fifo_max_dequeue (ct->client_rx_fifo) > 0);
+  else
+    return (svm_fifo_max_dequeue (s->rx_fifo) > 0);
+}
+
 static void
 ct_session_postponed_cleanup (ct_connection_t *ct)
 {
+  ct_connection_t *peer_ct;
   app_worker_t *app_wrk;
   session_t *s;
 
   s = session_get (ct->c_s_index, ct->c_thread_index);
   app_wrk = app_worker_get_if_valid (s->app_wrk_index);
 
+  peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
+  if (peer_ct)
+    {
+      if (ct_close_is_reset (ct, s))
+       session_transport_reset_notify (&peer_ct->connection);
+      else
+       session_transport_closing_notify (&peer_ct->connection);
+    }
+  session_transport_closed_notify (&ct->connection);
+
   if (ct->flags & CT_CONN_F_CLIENT)
     {
       if (app_wrk)
@@ -1048,15 +1059,6 @@ ct_program_cleanup (ct_connection_t *ct)
     thread_index, ct_handle_cleanups, uword_to_pointer (thread_index, void *));
 }
 
-static inline int
-ct_close_is_reset (ct_connection_t *ct, session_t *s)
-{
-  if (ct->flags & CT_CONN_F_CLIENT)
-    return (svm_fifo_max_dequeue (ct->client_rx_fifo) > 0);
-  else
-    return (svm_fifo_max_dequeue (s->rx_fifo) > 0);
-}
-
 static void
 ct_session_close (u32 ct_index, u32 thread_index)
 {
@@ -1075,14 +1077,7 @@ ct_session_close (u32 ct_index, u32 thread_index)
          ct_session_connect_notify (s, SESSION_E_REFUSED);
          ct->peer_index = ~0;
        }
-      else if (peer_ct->c_s_index != ~0)
-       {
-         if (ct_close_is_reset (ct, s))
-           session_transport_reset_notify (&peer_ct->connection);
-         else
-           session_transport_closing_notify (&peer_ct->connection);
-       }
-      else
+      else if (peer_ct->c_s_index == ~0)
        {
          /* should not happen */
          clib_warning ("ct peer without session");
@@ -1151,12 +1146,17 @@ static int
 ct_app_rx_evt (transport_connection_t * tc)
 {
   ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
-  session_t *ps;
+  session_t *ps, *s;
 
+  s = session_get (ct->c_s_index, ct->c_thread_index);
+  if (session_has_transport (s) || s->session_state < SESSION_STATE_READY)
+    return -1;
   peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
-  if (!peer_ct)
+  if (!peer_ct || (peer_ct->flags & CT_CONN_F_HALF_OPEN))
     return -1;
   ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
+  if (ps->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
+    return -1;
   return session_dequeue_notify (ps);
 }
 
@@ -1263,12 +1263,22 @@ static const transport_proto_vft_t cut_thru_proto = {
 };
 /* *INDENT-ON* */
 
+static inline int
+ct_session_can_tx (session_t *s)
+{
+  return (s->session_state == SESSION_STATE_READY ||
+         s->session_state == SESSION_STATE_CLOSING ||
+         s->session_state == SESSION_STATE_APP_CLOSED);
+}
+
 int
 ct_session_tx (session_t * s)
 {
   ct_connection_t *ct, *peer_ct;
   session_t *peer_s;
 
+  if (!ct_session_can_tx (s))
+    return 0;
   ct = (ct_connection_t *) session_get_transport (s);
   peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
   if (!peer_ct)