session: add session stats
[vpp.git] / src / vnet / session / session.h
index 54740e6..e92b1f0 100644 (file)
@@ -40,6 +40,11 @@ typedef enum
     SESSION_N_ERROR,
 } session_input_error_t;
 
+typedef struct session_wrk_stats_
+{
+  u32 errors[SESSION_N_ERRORS];
+} session_wrk_stats_t;
+
 typedef struct session_tx_context_
 {
   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
@@ -157,9 +162,6 @@ typedef struct session_worker_
   /** Flag that is set if main thread signaled to handle connects */
   u32 n_pending_connects;
 
-  /** Main thread loops in poll mode without a connect */
-  u32 no_connect_loops;
-
   /** List head for first worker evts pending handling on main */
   clib_llist_index_t evts_pending_main;
 
@@ -172,6 +174,8 @@ typedef struct session_worker_
   u16 batch_num;
   vlib_dma_batch_t *batch;
 
+  session_wrk_stats_t stats;
+
 #if SESSION_DEBUG
   /** last event poll time by thread */
   clib_time_type_t last_event_poll;
@@ -212,7 +216,9 @@ typedef struct session_main_
    * Trade memory for speed, for now */
   u32 *session_type_to_next;
 
-  /** Thread for cl and ho that rely on cl allocs */
+  /** Thread used for allocating active open connections, i.e., half-opens
+   * for transports like tcp, and sessions that will be migrated for cl
+   * transports like udp. If vpp has workers, this will be first worker. */
   u32 transport_cl_thread;
 
   transport_proto_t last_transport_proto_type;
@@ -334,7 +340,7 @@ session_evt_ctrl_data (session_worker_t * wrk, session_evt_elt_t * elt)
 static inline void
 session_evt_ctrl_data_free (session_worker_t * wrk, session_evt_elt_t * elt)
 {
-  ASSERT (elt->evt.event_type > SESSION_IO_EVT_BUILTIN_TX);
+  ASSERT (elt->evt.event_type >= SESSION_CTRL_EVT_RPC);
   pool_put_index (wrk->ctrl_evts_data, elt->evt.ctrl_data_index);
 }
 
@@ -616,6 +622,13 @@ transport_cl_thread (void)
   return session_main.transport_cl_thread;
 }
 
+always_inline u32
+session_vlib_thread_is_cl_thread (void)
+{
+  return (vlib_get_thread_index () == transport_cl_thread () ||
+         vlib_thread_is_main_w_barrier ());
+}
+
 /*
  * Listen sessions
  */
@@ -668,28 +681,17 @@ always_inline session_t *
 ho_session_alloc (void)
 {
   session_t *s;
-  ASSERT (vlib_get_thread_index () == 0);
-  s = session_alloc (0);
+  ASSERT (session_vlib_thread_is_cl_thread ());
+  s = session_alloc (transport_cl_thread ());
   s->session_state = SESSION_STATE_CONNECTING;
   s->flags |= SESSION_F_HALF_OPEN;
-  /* Not ideal. Half-opens are only allocated from main with worker barrier
-   * but can be cleaned up, i.e., session_half_open_free, from main without
-   * a barrier. In debug images, the free_bitmap can grow while workers peek
-   * the sessions pool, e.g., session_half_open_migrate_notify, and as a
-   * result crash while validating the session. To avoid this, grow the bitmap
-   * now. */
-  if (CLIB_DEBUG)
-    {
-      session_t *sp = session_main.wrk[0].sessions;
-      clib_bitmap_validate (pool_header (sp)->free_bitmap, s->session_index);
-    }
   return s;
 }
 
 always_inline session_t *
 ho_session_get (u32 ho_index)
 {
-  return session_get (ho_index, 0 /* half-open thread */);
+  return session_get (ho_index, transport_cl_thread ());
 }
 
 always_inline void
@@ -714,7 +716,7 @@ vnet_get_session_main ()
 always_inline session_worker_t *
 session_main_get_worker (u32 thread_index)
 {
-  return &session_main.wrk[thread_index];
+  return vec_elt_at_index (session_main.wrk, thread_index);
 }
 
 static inline session_worker_t *
@@ -722,13 +724,13 @@ session_main_get_worker_if_valid (u32 thread_index)
 {
   if (thread_index > vec_len (session_main.wrk))
     return 0;
-  return &session_main.wrk[thread_index];
+  return session_main_get_worker (thread_index);
 }
 
 always_inline svm_msg_q_t *
 session_main_get_vpp_event_queue (u32 thread_index)
 {
-  return session_main.wrk[thread_index].vpp_event_queue;
+  return session_main_get_worker (thread_index)->vpp_event_queue;
 }
 
 always_inline u8
@@ -737,6 +739,23 @@ session_main_is_enabled ()
   return session_main.is_enabled == 1;
 }
 
+always_inline void
+session_worker_stat_error_inc (session_worker_t *wrk, int error, int value)
+{
+  if ((-(error) >= 0 && -(error) < SESSION_N_ERRORS))
+    wrk->stats.errors[-error] += value;
+  else
+    SESSION_DBG ("unknown session counter");
+}
+
+always_inline void
+session_stat_error_inc (int error, int value)
+{
+  session_worker_t *wrk;
+  wrk = session_main_get_worker (vlib_get_thread_index ());
+  session_worker_stat_error_inc (wrk, error, value);
+}
+
 #define session_cli_return_if_not_enabled()                            \
 do {                                                                   \
     if (!session_main.is_enabled)                                      \
@@ -820,8 +839,7 @@ pool_program_safe_realloc_rpc (void *args)
     {
       max_elts = _vec_max_len (*pra->pool, pra->elt_size);
       n_alloc = clib_max (2 * max_elts, POOL_REALLOC_SAFE_ELT_THRESH);
-      _pool_alloc (pra->pool, free_elts + n_alloc, pra->align, 0,
-                  pra->elt_size);
+      _pool_alloc (pra->pool, n_alloc, pra->align, 0, pra->elt_size);
     }
   pool_realloc_flag (*pra->pool) = 0;
   clib_mem_free (args);