session: separate ctrl, new and old events
[vpp.git] / src / vnet / session / session.c
index 0e5fb8e..318e01d 100644 (file)
@@ -55,6 +55,7 @@ session_send_evt_to_thread (void *data, void *args, u32 thread_index,
       evt->rpc_args.fp = data;
       evt->rpc_args.arg = args;
       break;
+    case SESSION_IO_EVT_RX:
     case SESSION_IO_EVT_TX:
     case SESSION_IO_EVT_TX_FLUSH:
     case SESSION_IO_EVT_BUILTIN_RX:
@@ -117,22 +118,47 @@ session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
     }
 }
 
+void
+session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
+{
+  session_t *s;
+
+  s = session_get (tc->s_index, tc->thread_index);
+  ASSERT (s->thread_index == vlib_get_thread_index ());
+  if (!(s->flags & SESSION_F_CUSTOM_TX))
+    {
+      s->flags |= SESSION_F_CUSTOM_TX;
+      if (svm_fifo_set_event (s->tx_fifo))
+       {
+         session_worker_t *wrk;
+         session_evt_elt_t *elt;
+         wrk = session_main_get_worker (tc->thread_index);
+         if (has_prio)
+           elt = session_evt_alloc_new (wrk);
+         else
+           elt = session_evt_alloc_old (wrk);
+         elt->evt.session_index = tc->s_index;
+         elt->evt.event_type = SESSION_IO_EVT_TX;
+       }
+    }
+}
+
 static void
 session_program_transport_close (session_t * s)
 {
   u32 thread_index = vlib_get_thread_index ();
+  session_evt_elt_t *elt;
   session_worker_t *wrk;
-  session_event_t *evt;
 
   /* If we are in the handler thread, or being called with the worker barrier
    * held, just append a new event to pending disconnects vector. */
   if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
     {
       wrk = session_main_get_worker (s->thread_index);
-      vec_add2 (wrk->pending_disconnects, evt, 1);
-      clib_memset (evt, 0, sizeof (*evt));
-      evt->session_handle = session_handle (s);
-      evt->event_type = SESSION_CTRL_EVT_CLOSE;
+      elt = session_evt_alloc_ctrl (wrk);
+      clib_memset (&elt->evt, 0, sizeof (session_event_t));
+      elt->evt.session_handle = session_handle (s);
+      elt->evt.event_type = SESSION_CTRL_EVT_CLOSE;
     }
   else
     session_send_ctrl_evt_to_thread (s, SESSION_CTRL_EVT_CLOSE);
@@ -174,13 +200,25 @@ session_free (session_t * s)
       pool_put (session_main.wrk[thread_index].sessions, s);
       return;
     }
-  SESSION_EVT_DBG (SESSION_EVT_FREE, s);
+  SESSION_EVT (SESSION_EVT_FREE, s);
   pool_put (session_main.wrk[s->thread_index].sessions, s);
 }
 
+static void
+session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
+{
+  app_worker_t *app_wrk;
+
+  app_wrk = app_worker_get_if_valid (s->app_wrk_index);
+  if (!app_wrk)
+    return;
+  app_worker_cleanup_notify (app_wrk, s, ntf);
+}
+
 void
 session_free_w_fifos (session_t * s)
 {
+  session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
   segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
   session_free (s);
 }
@@ -449,7 +487,7 @@ session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
 
   rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
 
-  if (svm_fifo_needs_tx_ntf (s->tx_fifo, max_bytes))
+  if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
     session_dequeue_notify (s);
 
   return rv;
@@ -504,12 +542,7 @@ session_enqueue_notify_inline (session_t * s)
       return 0;
     }
 
-  /* *INDENT-OFF* */
-  SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
-      ed->data[0] = SESSION_IO_EVT_RX;
-      ed->data[1] = svm_fifo_max_dequeue_prod (s->rx_fifo);
-  }));
-  /* *INDENT-ON* */
+  SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
 
   s->flags &= ~SESSION_F_RX_EVT;
   if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
@@ -549,7 +582,7 @@ session_dequeue_notify (session_t * s)
     return session_notify_subscribers (app_wrk->app_index, s,
                                       s->tx_fifo, SESSION_IO_EVT_TX);
 
-  svm_fifo_clear_tx_ntf (s->tx_fifo);
+  svm_fifo_clear_deq_ntf (s->tx_fifo);
 
   return 0;
 }
@@ -601,8 +634,9 @@ session_main_flush_all_enqueue_events (u8 transport_proto)
   return errors;
 }
 
-int
-session_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
+static inline int
+session_stream_connect_notify_inline (transport_connection_t * tc, u8 is_fail,
+                                     session_state_t opened_state)
 {
   u32 opaque = 0, new_ti, new_si;
   app_worker_t *app_wrk;
@@ -645,6 +679,10 @@ session_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
       return -1;
     }
 
+  s = session_get (new_si, new_ti);
+  s->session_state = opened_state;
+  session_lookup_add_connection (tc, session_handle (s));
+
   if (app_worker_connect_notify (app_wrk, s, opaque))
     {
       s = session_get (new_si, new_ti);
@@ -652,13 +690,23 @@ session_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
       return -1;
     }
 
-  s = session_get (new_si, new_ti);
-  s->session_state = SESSION_STATE_READY;
-  session_lookup_add_connection (tc, session_handle (s));
-
   return 0;
 }
 
+int
+session_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
+{
+  return session_stream_connect_notify_inline (tc, is_fail,
+                                              SESSION_STATE_READY);
+}
+
+int
+session_ho_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
+{
+  return session_stream_connect_notify_inline (tc, is_fail,
+                                              SESSION_STATE_OPENED);
+}
+
 typedef struct _session_switch_pool_args
 {
   u32 session_index;
@@ -758,9 +806,6 @@ session_transport_delete_notify (transport_connection_t * tc)
   if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
     return;
 
-  /* Make sure we don't try to send anything more */
-  svm_fifo_dequeue_drop_all (s->tx_fifo);
-
   switch (s->session_state)
     {
     case SESSION_STATE_CREATED:
@@ -771,48 +816,56 @@ session_transport_delete_notify (transport_connection_t * tc)
       break;
     case SESSION_STATE_ACCEPTING:
     case SESSION_STATE_TRANSPORT_CLOSING:
+    case SESSION_STATE_CLOSING:
+    case SESSION_STATE_TRANSPORT_CLOSED:
       /* If transport finishes or times out before we get a reply
        * from the app, mark transport as closed and wait for reply
        * before removing the session. Cleanup session table in advance
        * because transport will soon be closed and closed sessions
        * are assumed to have been removed from the lookup table */
       session_lookup_del_session (s);
-      s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
+      s->session_state = SESSION_STATE_TRANSPORT_DELETED;
+      session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
+      svm_fifo_dequeue_drop_all (s->tx_fifo);
       break;
-    case SESSION_STATE_CLOSING:
-    case SESSION_STATE_CLOSED_WAITING:
+    case SESSION_STATE_APP_CLOSED:
       /* Cleanup lookup table as transport needs to still be valid.
        * Program transport close to ensure that all session events
        * have been cleaned up. Once transport close is called, the
        * session is just removed because both transport and app have
        * confirmed the close*/
       session_lookup_del_session (s);
-      s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
+      s->session_state = SESSION_STATE_CLOSED;
+      session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
+      svm_fifo_dequeue_drop_all (s->tx_fifo);
       session_program_transport_close (s);
       break;
-    case SESSION_STATE_TRANSPORT_CLOSED:
+    case SESSION_STATE_TRANSPORT_DELETED:
       break;
     case SESSION_STATE_CLOSED:
+      session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
       session_delete (s);
       break;
     default:
       clib_warning ("session state %u", s->session_state);
+      session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
       session_delete (s);
       break;
     }
 }
 
 /**
- * Notification from transport that session can be closed
+ * Notification from transport that it is closed
  *
- * Should be called by transport only if it was closed with non-empty
- * tx fifo and once it decides to begin the closing procedure prior to
- * issuing a delete notify. This gives the chance to the session layer
- * to cleanup any outstanding events.
+ * Should be called by transport, prior to calling delete notify, once it
+ * knows that no more data will be exchanged. This could serve as an
+ * early acknowledgment of an active close especially if transport delete
+ * can be delayed a long time, e.g., tcp time-wait.
  */
 void
 session_transport_closed_notify (transport_connection_t * tc)
 {
+  app_worker_t *app_wrk;
   session_t *s;
 
   if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
@@ -824,16 +877,21 @@ session_transport_closed_notify (transport_connection_t * tc)
     {
       session_transport_closing_notify (tc);
       svm_fifo_dequeue_drop_all (s->tx_fifo);
+      s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
     }
   /* If app close has not been received or has not yet resulted in
    * a transport close, only mark the session transport as closed */
   else if (s->session_state <= SESSION_STATE_CLOSING)
     {
-      session_lookup_del_session (s);
       s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
     }
-  else
+  /* If app also closed, switch to closed */
+  else if (s->session_state == SESSION_STATE_APP_CLOSED)
     s->session_state = SESSION_STATE_CLOSED;
+
+  app_wrk = app_worker_get_if_valid (s->app_wrk_index);
+  if (app_wrk)
+    app_worker_transport_closed_notify (app_wrk, s);
 }
 
 /**
@@ -930,7 +988,6 @@ session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
 
   sh = session_handle (s);
   session_lookup_add_connection (tc, sh);
-
   return app_worker_connect_notify (app_wrk, s, opaque);
 }
 
@@ -966,6 +1023,8 @@ session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
    * thing but better than allocating a separate half-open pool.
    */
   tc->s_index = opaque;
+  if (transport_half_open_has_fifos (rmt->transport_proto))
+    return session_ho_stream_connect_notify (tc, 0 /* is_fail */ );
   return 0;
 }
 
@@ -1061,7 +1120,8 @@ session_stop_listen (session_t * s)
   if (!tc)
     return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
 
-  session_lookup_del_connection (tc);
+  if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
+    session_lookup_del_connection (tc);
   transport_stop_listen (tp, s->connection_index);
   return 0;
 }
@@ -1082,12 +1142,9 @@ session_close (session_t * s)
     {
       /* Session will only be removed once both app and transport
        * acknowledge the close */
-      if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
+      if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
+         || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
        session_program_transport_close (s);
-
-      /* Session already closed. Clear the tx fifo */
-      if (s->session_state == SESSION_STATE_CLOSED)
-       svm_fifo_dequeue_drop_all (s->tx_fifo);
       return;
     }
 
@@ -1105,23 +1162,23 @@ session_close (session_t * s)
 void
 session_transport_close (session_t * s)
 {
-  /* If transport is already closed, just free the session */
-  if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
+  if (s->session_state >= SESSION_STATE_APP_CLOSED)
     {
-      session_free_w_fifos (s);
+      if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
+       s->session_state = SESSION_STATE_CLOSED;
+      /* If transport is already deleted, just free the session */
+      else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
+       session_free_w_fifos (s);
       return;
     }
 
-  /* If tx queue wasn't drained, change state to closed waiting for transport.
-   * This way, the transport, if it so wishes, can continue to try sending the
-   * outstanding data (in closed state it cannot). It MUST however at one
-   * point, either after sending everything or after a timeout, call delete
-   * notify. This will finally lead to the complete cleanup of the session.
+  /* If the tx queue wasn't drained, the transport can continue to try
+   * sending the outstanding data (in closed state it cannot). It MUST however
+   * at one point, either after sending everything or after a timeout, call
+   * delete notify. This will finally lead to the complete cleanup of the
+   * session.
    */
-  if (svm_fifo_max_dequeue_cons (s->tx_fifo))
-    s->session_state = SESSION_STATE_CLOSED_WAITING;
-  else
-    s->session_state = SESSION_STATE_CLOSED;
+  s->session_state = SESSION_STATE_APP_CLOSED;
 
   transport_close (session_get_transport_proto (s), s->connection_index,
                   s->thread_index);
@@ -1346,26 +1403,16 @@ session_manager_main_enable (vlib_main_t * vm)
   for (i = 0; i < num_threads; i++)
     {
       wrk = &smm->wrk[i];
-      vec_validate (wrk->free_event_vector, 128);
-      _vec_len (wrk->free_event_vector) = 0;
-      vec_validate (wrk->pending_event_vector, 128);
-      _vec_len (wrk->pending_event_vector) = 0;
-      vec_validate (wrk->pending_disconnects, 128);
-      _vec_len (wrk->pending_disconnects) = 0;
-      vec_validate (wrk->postponed_event_vector, 128);
-      _vec_len (wrk->postponed_event_vector) = 0;
-
+      wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
+      wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
+      wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
+      wrk->vm = vlib_mains[i];
       wrk->last_vlib_time = vlib_time_now (vlib_mains[i]);
-      wrk->dispatch_period = 500e-6;
 
       if (num_threads > 1)
        clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
     }
 
-#if SESSION_DEBUG
-  vec_validate (smm->last_event_poll_by_thread, num_threads - 1);
-#endif
-
   /* Allocate vpp event queues segment and queue */
   session_vpp_event_queues_allocate (smm);
 
@@ -1451,8 +1498,8 @@ vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
       if (session_main.is_enabled)
        return 0;
 
-      session_node_enable_disable (is_en);
       error = session_manager_main_enable (vm);
+      session_node_enable_disable (is_en);
     }
   else
     {