session svm: track session indices in private struct 57/41157/13
authorFlorin Coras <[email protected]>
Wed, 19 Jun 2024 05:00:41 +0000 (22:00 -0700)
committerDave Barach <[email protected]>
Wed, 22 Jan 2025 20:38:06 +0000 (20:38 +0000)
Type: improvement

Signed-off-by: Florin Coras <[email protected]>
Change-Id: I898baf3e2a7586124f4678eaeaa4516db7186f8f

17 files changed:
src/plugins/hs_apps/proxy.c
src/plugins/hs_apps/sapi/vpp_echo.c
src/plugins/hs_apps/sapi/vpp_echo_bapi.c
src/plugins/unittest/session_test.c
src/svm/fifo_segment.c
src/svm/fifo_types.h
src/svm/svm_fifo.c
src/vcl/vcl_private.c
src/vcl/vppcom.c
src/vnet/session/application_interface.h
src/vnet/session/application_local.c
src/vnet/session/application_worker.c
src/vnet/session/segment_manager.c
src/vnet/session/session.c
src/vnet/session/session_api.c
src/vnet/session/session_debug.c
src/vnet/session/session_node.c

index 7407bcb..f6b7f52 100644 (file)
@@ -830,7 +830,7 @@ active_open_alloc_session_fifos (session_t *s)
    * will receive data, etc.
    */
   txf->shr->master_session_index = s->session_index;
-  txf->master_thread_index = s->thread_index;
+  txf->vpp_sh = s->handle;
 
   /*
    * Account for the active-open session's use of the fifos
@@ -1101,7 +1101,7 @@ active_open_rx_callback (session_t * s)
   if (svm_fifo_set_event (proxy_tx_fifo))
     {
       u8 thread_index = proxy_tx_fifo->master_thread_index;
-      u32 session_index = proxy_tx_fifo->shr->master_session_index;
+      u32 session_index = proxy_tx_fifo->vpp_session_index;
       return session_send_io_evt_to_thread_custom (&session_index,
                                                   thread_index,
                                                   SESSION_IO_EVT_TX);
index 08fd4e1..9ae95e6 100644 (file)
@@ -69,9 +69,9 @@ echo_session_dequeue_notify (echo_session_t * s)
   int rv;
   if (!svm_fifo_set_event (s->rx_fifo))
     return;
-  if ((rv = app_send_io_evt_to_vpp (s->vpp_evt_q,
-                                   s->rx_fifo->shr->master_session_index,
-                                   SESSION_IO_EVT_RX, SVM_Q_WAIT)))
+  if ((rv =
+        app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->vpp_session_index,
+                                SESSION_IO_EVT_RX, SVM_Q_WAIT)))
     ECHO_FAIL (ECHO_FAIL_SEND_IO_EVT, "app_send_io_evt_to_vpp errored %d",
               rv);
   svm_fifo_clear_deq_ntf (s->rx_fifo);
index 868cc3a..12be205 100644 (file)
@@ -332,6 +332,10 @@ echo_attach_session (uword segment_handle, uword rxf_offset, uword txf_offset,
   s->tx_fifo = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
   s->rx_fifo->segment_index = fs_index;
   s->tx_fifo->segment_index = fs_index;
+  s->rx_fifo->vpp_session_index = s->rx_fifo->shr->master_session_index;
+  s->tx_fifo->vpp_session_index = s->tx_fifo->shr->master_session_index;
+  s->rx_fifo->app_session_index = s->session_index;
+  s->tx_fifo->app_session_index = s->session_index;
   s->rx_fifo->shr->client_session_index = s->session_index;
   s->tx_fifo->shr->client_session_index = s->session_index;
 
index fe4664b..993f1be 100644 (file)
@@ -1864,7 +1864,7 @@ test_app_send_io_evt_rx (app_worker_t *app_wrk, session_t *s)
   if (svm_fifo_has_event (s->rx_fifo))
     return 0;
 
-  app_session = s->rx_fifo->shr->client_session_index;
+  app_session = s->rx_fifo->app_session_index;
   mq = app_wrk->event_queue;
 
   rv = test_mq_try_lock_and_alloc_msg (mq, SESSION_MQ_IO_EVT_RING, mq_msg);
index d5f6291..86f4ff2 100644 (file)
@@ -969,6 +969,7 @@ fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
   if (CLIB_DEBUG)
     {
       sf->master_session_index = ~0;
+      f->vpp_session_index = ~0;
       f->master_thread_index = ~0;
     }
 
@@ -1002,12 +1003,12 @@ fifo_segment_detach_fifo (fifo_segment_t *fs, svm_fifo_t **f)
     pfss_fifo_del_active_list (pfss, of);
 
   /* Collect chunks that were provided in return for those detached */
-  fsh_slice_collect_chunks (fs->h, fss, of->chunks_at_attach);
-  of->chunks_at_attach = 0;
+  fsh_slice_collect_chunks (fs->h, fss, svm_fifo_chunks_at_attach (of));
+  svm_fifo_chunks_at_attach (of) = 0;
 
   /* Collect hdr that was provided in return for the detached */
-  fss_fifo_free_list_push (fs->h, fss, of->hdr_at_attach);
-  of->hdr_at_attach = 0;
+  fss_fifo_free_list_push (fs->h, fss, svm_fifo_hdr_at_attach (of));
+  svm_fifo_hdr_at_attach (of) = 0;
 
   clib_mem_bulk_free (pfss->fifos, *f);
   *f = 0;
@@ -1034,10 +1035,11 @@ fifo_segment_attach_fifo (fifo_segment_t *fs, svm_fifo_t **f, u32 slice_index)
   /* Allocate shared hdr and chunks to be collected at detach in return
    * for those that are being attached now */
   of = *f;
-  of->hdr_at_attach = fsh_try_alloc_fifo_hdr (fs->h, fss);
+  svm_fifo_hdr_at_attach (of) = fsh_try_alloc_fifo_hdr (fs->h, fss);
 
   c = fs_chunk_ptr (fs->h, nf->shr->start_chunk);
-  of->chunks_at_attach = pc = fsh_try_alloc_chunk (fs->h, fss, c->length);
+  svm_fifo_chunks_at_attach (of) = pc =
+    fsh_try_alloc_chunk (fs->h, fss, c->length);
 
   while ((c = fs_chunk_ptr (fs->h, c->next)))
     {
index 742351b..e4b72ea 100644 (file)
@@ -101,23 +101,39 @@ typedef struct _svm_fifo
   u32 ooos_newest;              /**< Last segment to have been updated */
 
   u8 flags;              /**< fifo flags */
-  u8 master_thread_index; /**< session layer thread index */
-  u8 client_thread_index; /**< app worker index */
   i8 refcnt;             /**< reference count  */
-  u32 segment_manager;   /**< session layer segment manager index */
-  u32 segment_index;     /**< segment index in segment manager */
+  u8 client_thread_index; /**< app worker index */
+  u32 app_session_index;  /**< app session index */
+  union
+  {
+    struct
+    {
+      u32 vpp_session_index;   /**< session layer session index */
+      u32 master_thread_index; /**< session layer thread index */
+    };
+    u64 vpp_sh;
+  };
+  u32 segment_manager; /**< session layer segment manager index */
+  u32 segment_index;   /**< segment index in segment manager */
 
   struct _svm_fifo *next; /**< prev in active chain */
   struct _svm_fifo *prev; /**< prev in active chain */
 
-  svm_fifo_chunk_t *chunks_at_attach; /**< chunks to be accounted at detach */
-  svm_fifo_shared_t *hdr_at_attach;   /**< hdr to be freed at detach */
-
 #if SVM_FIFO_TRACE
   svm_fifo_trace_elem_t *trace;
 #endif
 } svm_fifo_t;
 
+/* To minimize size of svm_fifo_t reuse ooo pointers for tracking chunks and
+ * hdr at attach/detach. Fifo being migrated should not receive new data */
+#define svm_fifo_chunks_at_attach(f) f->ooo_deq
+#define svm_fifo_hdr_at_attach(f)                                             \
+  ((union {                                                                   \
+     svm_fifo_shared_t *hdr;                                                  \
+     svm_fifo_chunk_t *ooo_enq;                                               \
+   } *) &f->ooo_enq)                                                          \
+    ->hdr
+
 typedef struct fifo_segment_slice_
 {
   CLIB_CACHE_LINE_ALIGN_MARK (cacheline);
index 49b3d17..6107ea1 100644 (file)
@@ -1650,8 +1650,8 @@ format_svm_fifo (u8 * s, va_list * args)
 
   if (verbose > 1)
     s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
-               format_white_space, indent, f->shr->master_session_index,
-               f->master_thread_index, f->shr->client_session_index,
+               format_white_space, indent, f->vpp_session_index,
+               f->master_thread_index, f->app_session_index,
                f->client_thread_index);
 
   if (verbose)
index d3ad233..d981439 100644 (file)
@@ -600,8 +600,12 @@ vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
 
   if (!is_ct)
     {
+      rxf->vpp_session_index = rxf->shr->master_session_index;
+      txf->vpp_session_index = txf->shr->master_session_index;
       rxf->shr->client_session_index = s->session_index;
       txf->shr->client_session_index = s->session_index;
+      rxf->app_session_index = s->session_index;
+      txf->app_session_index = s->session_index;
       rxf->client_thread_index = vcl_get_worker_index ();
       txf->client_thread_index = vcl_get_worker_index ();
       s->rx_fifo = rxf;
index e1eba29..19d58c3 100644 (file)
@@ -659,8 +659,7 @@ vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
 
   /* Generate new tx event if we have outstanding data */
   if (svm_fifo_has_event (s->tx_fifo))
-    app_send_io_evt_to_vpp (s->vpp_evt_q,
-                           s->tx_fifo->shr->master_session_index,
+    app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->vpp_session_index,
                            SESSION_IO_EVT_TX, SVM_Q_WAIT);
 
   VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
@@ -2201,8 +2200,7 @@ read_again:
   if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
     {
       svm_fifo_clear_deq_ntf (rx_fifo);
-      app_send_io_evt_to_vpp (s->vpp_evt_q,
-                             s->rx_fifo->shr->master_session_index,
+      app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->vpp_session_index,
                              SESSION_IO_EVT_RX, SVM_Q_WAIT);
     }
 
@@ -2407,8 +2405,8 @@ vppcom_session_write_inline (vcl_worker_t *wrk, vcl_session_t *s, void *buf,
     }
 
   if (svm_fifo_set_event (s->tx_fifo))
-    app_send_io_evt_to_vpp (
-      s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
+    app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->vpp_session_index, et,
+                           SVM_Q_WAIT);
 
   /* The underlying fifo segment can run out of memory */
   if (PREDICT_FALSE (n_write < 0))
index d5656ff..21ed979 100644 (file)
@@ -688,8 +688,8 @@ app_send_dgram_raw_gso (svm_fifo_t *f, app_session_transport_t *at,
   if (do_evt)
     {
       if (svm_fifo_set_event (f))
-       app_send_io_evt_to_vpp (vpp_evt_q, f->shr->master_session_index,
-                               evt_type, noblock);
+       app_send_io_evt_to_vpp (vpp_evt_q, f->vpp_session_index, evt_type,
+                               noblock);
     }
   return len;
 }
@@ -712,8 +712,8 @@ app_send_stream_raw (svm_fifo_t * f, svm_msg_q_t * vpp_evt_q, u8 * data,
   if (do_evt)
     {
       if (rv > 0 && svm_fifo_set_event (f))
-       app_send_io_evt_to_vpp (vpp_evt_q, f->shr->master_session_index,
-                               evt_type, noblock);
+       app_send_io_evt_to_vpp (vpp_evt_q, f->vpp_session_index, evt_type,
+                               noblock);
     }
   return rv;
 }
index 0800ce2..18ea77d 100644 (file)
@@ -647,8 +647,8 @@ ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct,
 
   ls->rx_fifo->shr->master_session_index = ls->session_index;
   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->vpp_sh = ls->handle;
+  ls->tx_fifo->vpp_sh = ls->handle;
 
   seg_handle = segment_manager_segment_handle (sm, fs);
   segment_manager_segment_reader_unlock (sm);
index cae340c..ad0b18e 100644 (file)
@@ -175,10 +175,10 @@ app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s)
     return rv;
 
   rx_fifo->shr->master_session_index = s->session_index;
-  rx_fifo->master_thread_index = s->thread_index;
+  rx_fifo->vpp_sh = s->handle;
 
   tx_fifo->shr->master_session_index = s->session_index;
-  tx_fifo->master_thread_index = s->thread_index;
+  tx_fifo->vpp_sh = s->handle;
 
   s->rx_fifo = rx_fifo;
   s->tx_fifo = tx_fifo;
@@ -210,10 +210,10 @@ app_worker_alloc_wrk_cl_session (app_worker_t *app_wrk, session_t *ls)
                                       &tx_fifo);
 
   rx_fifo->shr->master_session_index = s->session_index;
-  rx_fifo->master_thread_index = s->thread_index;
+  rx_fifo->vpp_sh = s->handle;
 
   tx_fifo->shr->master_session_index = s->session_index;
-  tx_fifo->master_thread_index = s->thread_index;
+  tx_fifo->vpp_sh = s->handle;
 
   s->rx_fifo = rx_fifo;
   s->tx_fifo = tx_fifo;
index 2b44d92..8c8b904 100644 (file)
@@ -625,7 +625,7 @@ segment_manager_del_sessions (segment_manager_t * sm)
          */
         while (f)
           {
-           session = session_get_if_valid (f->shr->master_session_index,
+           session = session_get_if_valid (f->vpp_session_index,
                                            f->master_thread_index);
            if (session)
              vec_add1 (handles, session_handle (session));
@@ -672,7 +672,7 @@ segment_manager_del_sessions_filter (segment_manager_t *sm,
          f = fifo_segment_get_slice_fifo_list (fs, slice_index);
          while (f)
            {
-             session = session_get_if_valid (f->shr->master_session_index,
+             session = session_get_if_valid (f->vpp_session_index,
                                              f->master_thread_index);
              if (session)
                {
@@ -920,7 +920,7 @@ segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f,
   segment_manager_segment_reader_unlock (sm);
 
   (*f)->shr->master_session_index = s->session_index;
-  (*f)->master_thread_index = s->thread_index;
+  (*f)->vpp_sh = s->handle;
 }
 
 u32
@@ -1195,7 +1195,7 @@ segment_manager_format_sessions (segment_manager_t * sm, int verbose)
             u32 session_index, thread_index;
             session_t *session;
 
-           session_index = f->shr->master_session_index;
+           session_index = f->vpp_session_index;
            thread_index = f->master_thread_index;
 
            session = session_get (session_index, thread_index);
index cc0e89f..1b6e8ce 100644 (file)
@@ -86,7 +86,7 @@ session_send_evt_to_thread (void *data, void *args, u32 thread_index,
 int
 session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
 {
-  return session_send_evt_to_thread (&f->shr->master_session_index, 0,
+  return session_send_evt_to_thread (&f->vpp_session_index, 0,
                                     f->master_thread_index, evt_type);
 }
 
index c6df47b..5ac21c4 100644 (file)
@@ -426,9 +426,12 @@ mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
     }
 
   /* Setup client session index in advance, in case data arrives
-   * before the app processes message and updates it */
+   * before the app processes message and updates it
+   * Maybe this needs to be done via a reply message from app */
   s->rx_fifo->shr->client_session_index = api_context;
   s->tx_fifo->shr->client_session_index = api_context;
+  s->rx_fifo->app_session_index = api_context;
+  s->tx_fifo->app_session_index = api_context;
 
 snd_msg:
 
@@ -637,7 +640,7 @@ mq_send_io_rx_event (session_t *s)
   mq_evt = svm_msg_q_msg_data (mq, &mq_msg);
 
   mq_evt->event_type = SESSION_IO_EVT_RX;
-  mq_evt->session_index = s->rx_fifo->shr->client_session_index;
+  mq_evt->session_index = s->rx_fifo->app_session_index;
 
   (void) svm_fifo_set_event (s->rx_fifo);
 
@@ -658,7 +661,7 @@ mq_send_io_tx_event (session_t *s)
   mq_evt = svm_msg_q_msg_data (mq, &mq_msg);
 
   mq_evt->event_type = SESSION_IO_EVT_TX;
-  mq_evt->session_index = s->tx_fifo->shr->client_session_index;
+  mq_evt->session_index = s->tx_fifo->app_session_index;
 
   svm_msg_q_add_raw (mq, &mq_msg);
 
index 2a50ada..158751c 100644 (file)
@@ -278,7 +278,7 @@ session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
     case SESSION_IO_EVT_BUILTIN_RX:
     case SESSION_IO_EVT_TX_MAIN:
     case SESSION_IO_EVT_TX_FLUSH:
-      if (e->session_index == f->shr->master_session_index)
+      if (e->session_index == f->vpp_session_index)
        return 1;
       break;
     case SESSION_CTRL_EVT_CLOSE:
index 4d86d40..1d4f45e 100644 (file)
@@ -474,6 +474,10 @@ session_mq_accepted_reply_handler (session_worker_t *wrk,
       return;
     }
 
+  /* TODO(fcoras) This needs to be part of the reply message */
+  s->rx_fifo->app_session_index = s->rx_fifo->shr->client_session_index;
+  s->tx_fifo->app_session_index = s->tx_fifo->shr->client_session_index;
+
   /* Special handling for cut-through sessions */
   if (!session_has_transport (s))
     {