session: add session event log for session state
[vpp.git] / src / vnet / session / session.h
index 71f6d35..54740e6 100644 (file)
@@ -21,6 +21,7 @@
 #include <vnet/session/session_debug.h>
 #include <svm/message_queue.h>
 #include <svm/fifo_segment.h>
+#include <vlib/dma/dma.h>
 
 #define foreach_session_input_error                                            \
 _(NO_SESSION, "No session drops")                                       \
@@ -85,6 +86,13 @@ typedef enum session_wrk_flags_
   SESSION_WRK_F_ADAPTIVE = 1 << 0,
 } __clib_packed session_wrk_flag_t;
 
+#define DMA_TRANS_SIZE 1024
+typedef struct
+{
+  u32 *pending_tx_buffers;
+  u16 *pending_tx_nexts;
+} session_dma_transfer;
+
 typedef struct session_worker_
 {
   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
@@ -134,9 +142,6 @@ typedef struct session_worker_
   /** Head of list of pending events */
   clib_llist_index_t old_head;
 
-  /** Peekers rw lock */
-  clib_rwlock_t peekers_rw_locks;
-
   /** Vector of buffers to be sent */
   u32 *pending_tx_buffers;
 
@@ -158,6 +163,15 @@ typedef struct session_worker_
   /** List head for first worker evts pending handling on main */
   clib_llist_index_t evts_pending_main;
 
+  int config_index;
+  u8 dma_enabled;
+  session_dma_transfer *dma_trans;
+  u16 trans_head;
+  u16 trans_tail;
+  u16 trans_size;
+  u16 batch_num;
+  vlib_dma_batch_t *batch;
+
 #if SESSION_DEBUG
   /** last event poll time by thread */
   clib_time_type_t last_event_poll;
@@ -204,7 +218,10 @@ typedef struct session_main_
   transport_proto_t last_transport_proto_type;
 
   /** Number of workers at pool realloc barrier */
-  u32 pool_realloc_at_barrier;
+  volatile u32 pool_realloc_at_barrier;
+
+  /** Number of workers doing reallocs */
+  volatile u32 pool_realloc_doing_work;
 
   /** Lock to synchronize parallel forced reallocs */
   clib_spinlock_t pool_realloc_lock;
@@ -237,6 +254,9 @@ typedef struct session_main_
   /** Session ssvm segment configs*/
   uword wrk_mqs_segment_size;
 
+  /** Session enable dma*/
+  u8 dma_enabled;
+
   /** Session table size parameters */
   u32 configured_v4_session_table_buckets;
   u32 configured_v4_session_table_memory;
@@ -386,37 +406,9 @@ session_get_from_handle_if_valid (session_handle_t handle)
 u64 session_segment_handle (session_t * s);
 
 /**
- * Acquires a lock that blocks a session pool from expanding.
- *
- * This is typically used for safely peeking into other threads'
- * pools in order to clone elements. Lock should be dropped as soon
- * as possible by calling @ref session_pool_remove_peeker.
+ * Get session from handle and avoid pool validation if no same thread
  *
- * NOTE: Avoid using pool_elt_at_index while the lock is held because
- * it may lead to free elt bitmap expansion/contraction!
- */
-always_inline void
-session_pool_add_peeker (u32 thread_index)
-{
-  session_worker_t *wrk = &session_main.wrk[thread_index];
-  if (thread_index == vlib_get_thread_index ())
-    return;
-  clib_rwlock_reader_lock (&wrk->peekers_rw_locks);
-}
-
-always_inline void
-session_pool_remove_peeker (u32 thread_index)
-{
-  session_worker_t *wrk = &session_main.wrk[thread_index];
-  if (thread_index == vlib_get_thread_index ())
-    return;
-  clib_rwlock_reader_unlock (&wrk->peekers_rw_locks);
-}
-
-/**
- * Get session from handle and 'lock' pool resize if not in same thread
- *
- * Caller should drop the peek 'lock' as soon as possible.
+ * Peekers are fine because pool grows with barrier (see @ref session_alloc)
  */
 always_inline session_t *
 session_get_from_handle_safe (u64 handle)
@@ -431,36 +423,24 @@ session_get_from_handle_safe (u64 handle)
     }
   else
     {
-      session_pool_add_peeker (thread_index);
-      /* Don't use pool_elt_at index. See @ref session_pool_add_peeker */
+      /* Don't use pool_elt_at index to avoid pool bitmap reallocs */
       return wrk->sessions + session_index_from_handle (handle);
     }
 }
 
-always_inline u32
-session_get_index (session_t * s)
-{
-  return (s - session_main.wrk[s->thread_index].sessions);
-}
-
 always_inline session_t *
 session_clone_safe (u32 session_index, u32 thread_index)
 {
+  u32 current_thread_index = vlib_get_thread_index (), new_index;
   session_t *old_s, *new_s;
-  u32 current_thread_index = vlib_get_thread_index ();
 
-  /* If during the memcpy pool is reallocated AND the memory allocator
-   * decides to give the old chunk of memory to somebody in a hurry to
-   * scribble something on it, we have a problem. So add this thread as
-   * a session pool peeker.
-   */
-  session_pool_add_peeker (thread_index);
   new_s = session_alloc (current_thread_index);
+  new_index = new_s->session_index;
+  /* Session pools are reallocated with barrier (see @ref session_alloc) */
   old_s = session_main.wrk[thread_index].sessions + session_index;
   clib_memcpy_fast (new_s, old_s, sizeof (*new_s));
-  session_pool_remove_peeker (thread_index);
   new_s->thread_index = current_thread_index;
-  new_s->session_index = session_get_index (new_s);
+  new_s->session_index = new_index;
   return new_s;
 }
 
@@ -547,6 +527,13 @@ int session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
                                u32 offset, u32 max_bytes);
 u32 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
 
+always_inline void
+session_set_state (session_t *s, session_state_t session_state)
+{
+  s->session_state = session_state;
+  SESSION_EVT (SESSION_EVT_STATE_CHANGE, s);
+}
+
 always_inline u32
 transport_max_rx_enqueue (transport_connection_t * tc)
 {
@@ -789,7 +776,7 @@ void session_wrk_enable_adaptive_mode (session_worker_t *wrk);
 fifo_segment_t *session_main_get_wrk_mqs_segment (void);
 void session_node_enable_disable (u8 is_en);
 clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
-void session_wrk_handle_evts_main_rpc ();
+void session_wrk_handle_evts_main_rpc (void *);
 
 session_t *session_alloc_for_connection (transport_connection_t * tc);
 session_t *session_alloc_for_half_open (transport_connection_t *tc);
@@ -809,126 +796,83 @@ STATIC_ASSERT_SIZEOF (pool_safe_realloc_header_t, sizeof (pool_header_t));
 #define pool_realloc_flag(PH)                                                 \
   ((pool_safe_realloc_header_t *) pool_header (PH))->flag
 
-#define pool_realloc_safe_aligned(P, align)                                   \
-  do                                                                          \
-    {                                                                         \
-      vlib_main_t *vm = vlib_get_main ();                                     \
-      u32 free_elts, max_elts, n_alloc;                                       \
-      ASSERT (vlib_get_thread_index () == 0);                                 \
-      vlib_worker_thread_barrier_sync (vm);                                   \
-      free_elts = pool_free_elts (P);                                         \
-      max_elts = pool_max_len (P);                                            \
-      n_alloc = clib_max (2 * max_elts, POOL_REALLOC_SAFE_ELT_THRESH);        \
-      pool_alloc_aligned (P, free_elts + n_alloc, align);                     \
-      clib_bitmap_validate (pool_header (P)->free_bitmap,                     \
-                           max_elts + n_alloc);                              \
-      pool_realloc_flag (P) = 0;                                              \
-      vlib_worker_thread_barrier_release (vm);                                \
-    }                                                                         \
-  while (0)
+typedef struct pool_realloc_rpc_args_
+{
+  void **pool;
+  uword elt_size;
+  uword align;
+} pool_realloc_rpc_args_t;
 
 always_inline void
-pool_program_safe_realloc (void *p, u32 thread_index,
-                          pool_safe_realloc_rpc_fn *rpc_fn)
+pool_program_safe_realloc_rpc (void *args)
 {
-  /* Reuse pad as a realloc flag */
-  if (pool_realloc_flag (p))
-    return;
+  vlib_main_t *vm = vlib_get_main ();
+  u32 free_elts, max_elts, n_alloc;
+  pool_realloc_rpc_args_t *pra;
 
-  pool_realloc_flag (p) = 1;
-  session_send_rpc_evt_to_thread (0 /* thread index */, rpc_fn,
-                                 uword_to_pointer (thread_index, void *));
+  ASSERT (vlib_get_thread_index () == 0);
+  pra = (pool_realloc_rpc_args_t *) args;
+
+  vlib_worker_thread_barrier_sync (vm);
+
+  free_elts = _pool_free_elts (*pra->pool, pra->elt_size);
+  if (free_elts < POOL_REALLOC_SAFE_ELT_THRESH)
+    {
+      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_realloc_flag (*pra->pool) = 0;
+  clib_mem_free (args);
+
+  vlib_worker_thread_barrier_release (vm);
 }
 
 always_inline void
-pool_realloc_maybe_wait_at_barrier (void)
+pool_program_safe_realloc (void **p, u32 elt_size, u32 align)
 {
-  if (!(*vlib_worker_threads->wait_at_barrier))
-    return;
+  pool_realloc_rpc_args_t *pra;
 
-  /* Node refork required. Don't stop at the barrier from within a node */
-  if (*vlib_worker_threads->node_reforks_required)
+  /* Reuse pad as a realloc flag */
+  if (pool_realloc_flag (*p))
     return;
 
-  clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
+  pra = clib_mem_alloc (sizeof (*pra));
+  pra->pool = p;
+  pra->elt_size = elt_size;
+  pra->align = align;
+  pool_realloc_flag (*p) = 1;
 
-  while (*vlib_worker_threads->wait_at_barrier)
-    ;
-
-  clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
+  session_send_rpc_evt_to_thread (0 /* thread index */,
+                                 pool_program_safe_realloc_rpc, pra);
 }
 
-#define pool_realloc_all_at_barrier(_not)                                     \
-  (*vlib_worker_threads->workers_at_barrier >= (vlib_num_workers () - _not))
-
-#define pool_realloc_safe_force(P)                                            \
-  do                                                                          \
-    {                                                                         \
-      ALWAYS_ASSERT (*vlib_worker_threads->node_reforks_required);            \
-      if (pool_realloc_all_at_barrier (1))                                    \
-       {                                                                     \
-         pool_alloc (P, pool_max_len (P));                                   \
-       }                                                                     \
-      else                                                                    \
-       {                                                                     \
-         session_main_t *sm = &session_main;                                 \
-         clib_warning ("forced pool realloc");                               \
-         clib_atomic_fetch_add (&sm->pool_realloc_at_barrier, 1);            \
-         while (!pool_realloc_all_at_barrier (sm->pool_realloc_at_barrier))  \
-           ;                                                                 \
-         clib_spinlock_lock (&sm->pool_realloc_lock);                        \
-         pool_alloc (P, pool_max_len (P));                                   \
-         clib_spinlock_unlock (&sm->pool_realloc_lock);                      \
-         clib_atomic_fetch_add (&sm->pool_realloc_at_barrier, -1);           \
-       }                                                                     \
-    }                                                                         \
-  while (0)
-
 #define pool_needs_realloc(P)                                                 \
   ((!P) ||                                                                    \
    (vec_len (pool_header (P)->free_indices) < POOL_REALLOC_SAFE_ELT_THRESH && \
     pool_free_elts (P) < POOL_REALLOC_SAFE_ELT_THRESH))
 
-#define pool_get_aligned_safe(P, E, thread_index, rpc_fn, align)              \
+#define pool_get_aligned_safe(P, E, align)                                    \
   do                                                                          \
     {                                                                         \
-      ASSERT (vlib_get_thread_index () == thread_index ||                     \
-             vlib_thread_is_main_w_barrier ());                              \
       if (PREDICT_FALSE (pool_needs_realloc (P)))                             \
        {                                                                     \
          if (PREDICT_FALSE (!(P)))                                           \
            {                                                                 \
-             pool_alloc_aligned (P, 2 * POOL_REALLOC_SAFE_ELT_THRESH,        \
-                                 align);                                     \
+             pool_alloc_aligned (P, POOL_REALLOC_SAFE_ELT_THRESH, align);    \
            }                                                                 \
-         else if (PREDICT_FALSE (pool_free_elts (P) <                        \
-                                 POOL_REALLOC_SAFE_ELT_THRESH / 2))          \
+         else if (PREDICT_FALSE (!pool_free_elts (P)))                       \
            {                                                                 \
-             volatile typeof (P) *PP = &(P);                                 \
-             pool_program_safe_realloc (P, thread_index, rpc_fn);            \
-             if (thread_index)                                               \
-               {                                                             \
-                 while (pool_realloc_flag (P))                               \
-                   {                                                         \
-                     /* If refork required abort and consume existing elt */ \
-                     if (*vlib_worker_threads->node_reforks_required)        \
-                       {                                                     \
-                         /* All workers at barrier realloc now */            \
-                         if (pool_realloc_all_at_barrier (1))                \
-                           pool_alloc_aligned (P, pool_max_len (P), align);  \
-                         break;                                              \
-                       }                                                     \
-                     pool_realloc_maybe_wait_at_barrier ();                  \
-                   }                                                         \
-                 if (pool_free_elts (P) == 0)                                \
-                   pool_realloc_safe_force (P);                              \
-                 ALWAYS_ASSERT (pool_free_elts (P) > 0);                     \
-               }                                                             \
-             (P) = *PP;                                                      \
+             vlib_workers_sync ();                                           \
+             pool_alloc_aligned (P, pool_max_len (P), align);                \
+             vlib_workers_continue ();                                       \
+             ALWAYS_ASSERT (pool_free_elts (P) > 0);                         \
            }                                                                 \
          else                                                                \
            {                                                                 \
-             pool_program_safe_realloc (P, thread_index, rpc_fn);            \
+             pool_program_safe_realloc ((void **) &(P), sizeof ((P)[0]),     \
+                                        _vec_align (P, align));              \
            }                                                                 \
        }                                                                     \
       pool_get_aligned (P, E, align);                                         \