session: fix allocation of proxy fifos
[vpp.git] / src / plugins / hs_apps / proxy.c
index db741f3..1010bf4 100644 (file)
@@ -105,6 +105,32 @@ proxy_session_free (proxy_session_t *ps)
   pool_put (pm->sessions, ps);
 }
 
+static int
+proxy_session_postponed_free_rpc (void *arg)
+{
+  uword ps_index = pointer_to_uword (arg);
+  proxy_main_t *pm = &proxy_main;
+  proxy_session_t *ps = 0;
+
+  clib_spinlock_lock_if_init (&pm->sessions_lock);
+
+  ps = proxy_session_get (ps_index);
+  segment_manager_dealloc_fifos (ps->server_rx_fifo, ps->server_tx_fifo);
+  proxy_session_free (ps);
+
+  clib_spinlock_unlock_if_init (&pm->sessions_lock);
+
+  return 0;
+}
+
+static void
+proxy_session_postponed_free (proxy_session_t *ps)
+{
+  session_send_rpc_evt_to_thread (ps->po_thread_index,
+                                 proxy_session_postponed_free_rpc,
+                                 uword_to_pointer (ps->ps_index, void *));
+}
+
 static void
 proxy_try_close_session (session_t * s, int is_active_open)
 {
@@ -168,8 +194,25 @@ proxy_try_delete_session (session_t * s, u8 is_active_open)
     {
       ps->vpp_active_open_handle = SESSION_INVALID_HANDLE;
 
+      /* Revert master thread index change on connect notification */
+      ps->server_rx_fifo->master_thread_index = ps->po_thread_index;
+
+      /* Passive open already cleaned up */
       if (ps->vpp_server_handle == SESSION_INVALID_HANDLE)
-       proxy_session_free (ps);
+       {
+         ASSERT (s->rx_fifo->refcnt == 1);
+
+         /* The two sides of the proxy on different threads */
+         if (ps->po_thread_index != s->thread_index)
+           {
+             /* This is not the right thread to delete the fifos */
+             s->rx_fifo = 0;
+             s->tx_fifo = 0;
+             proxy_session_postponed_free (ps);
+           }
+         else
+           proxy_session_free (ps);
+       }
     }
   else
     {
@@ -239,6 +282,7 @@ proxy_accept_callback (session_t * s)
   ps = proxy_session_alloc ();
   ps->vpp_server_handle = session_handle (s);
   ps->vpp_active_open_handle = SESSION_INVALID_HANDLE;
+  ps->po_thread_index = s->thread_index;
 
   s->opaque = ps->ps_index;
 
@@ -434,9 +478,46 @@ static session_cb_vft_t proxy_session_cb_vft = {
   .builtin_app_tx_callback = proxy_tx_callback,
   .session_reset_callback = proxy_reset_callback,
   .session_cleanup_callback = proxy_cleanup_callback,
-  .fifo_tuning_callback = common_fifo_tuning_callback
+  .fifo_tuning_callback = common_fifo_tuning_callback,
 };
 
+static int
+active_open_alloc_session_fifos (session_t *s)
+{
+  proxy_main_t *pm = &proxy_main;
+  svm_fifo_t *rxf, *txf;
+  proxy_session_t *ps;
+
+  clib_spinlock_lock_if_init (&pm->sessions_lock);
+
+  ps = proxy_session_get (s->opaque);
+
+  txf = ps->server_rx_fifo;
+  rxf = ps->server_tx_fifo;
+
+  /*
+   * Reset the active-open tx-fifo master indices so the active-open session
+   * will receive data, etc.
+   */
+  txf->shr->master_session_index = s->session_index;
+  txf->master_thread_index = s->thread_index;
+
+  /*
+   * Account for the active-open session's use of the fifos
+   * so they won't disappear until the last session which uses
+   * them disappears
+   */
+  rxf->refcnt++;
+  txf->refcnt++;
+
+  clib_spinlock_unlock_if_init (&pm->sessions_lock);
+
+  s->rx_fifo = rxf;
+  s->tx_fifo = txf;
+
+  return 0;
+}
+
 static int
 active_open_connected_callback (u32 app_index, u32 opaque,
                                session_t * s, session_error_t err)
@@ -477,24 +558,6 @@ active_open_connected_callback (u32 app_index, u32 opaque,
       return -1;
     }
 
-  s->tx_fifo = ps->server_rx_fifo;
-  s->rx_fifo = ps->server_tx_fifo;
-
-  /*
-   * Reset the active-open tx-fifo master indices so the active-open session
-   * will receive data, etc.
-   */
-  s->tx_fifo->shr->master_session_index = s->session_index;
-  s->tx_fifo->master_thread_index = s->thread_index;
-
-  /*
-   * Account for the active-open session's use of the fifos
-   * so they won't disappear until the last session which uses
-   * them disappears
-   */
-  s->tx_fifo->refcnt++;
-  s->rx_fifo->refcnt++;
-
   s->opaque = opaque;
 
   clib_spinlock_unlock_if_init (&pm->sessions_lock);
@@ -607,7 +670,8 @@ static session_cb_vft_t active_open_clients = {
   .session_cleanup_callback = active_open_cleanup_callback,
   .builtin_app_rx_callback = active_open_rx_callback,
   .builtin_app_tx_callback = active_open_tx_callback,
-  .fifo_tuning_callback = common_fifo_tuning_callback
+  .fifo_tuning_callback = common_fifo_tuning_callback,
+  .proxy_alloc_session_fifos = active_open_alloc_session_fifos,
 };
 /* *INDENT-ON* */
 
@@ -617,19 +681,16 @@ proxy_server_attach ()
   proxy_main_t *pm = &proxy_main;
   u64 options[APP_OPTIONS_N_OPTIONS];
   vnet_app_attach_args_t _a, *a = &_a;
-  u32 segment_size = 512 << 20;
 
   clib_memset (a, 0, sizeof (*a));
   clib_memset (options, 0, sizeof (options));
 
-  if (pm->private_segment_size)
-    segment_size = pm->private_segment_size;
   a->name = format (0, "proxy-server");
   a->api_client_index = pm->server_client_index;
   a->session_cb_vft = &proxy_session_cb_vft;
   a->options = options;
-  a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
-  a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
+  a->options[APP_OPTIONS_SEGMENT_SIZE] = pm->segment_size;
+  a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = pm->segment_size;
   a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
   a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
   a->options[APP_OPTIONS_MAX_FIFO_SIZE] = pm->max_fifo_size;
@@ -788,7 +849,7 @@ proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
   pm->rcv_buffer_size = 1024;
   pm->prealloc_fifos = 0;
   pm->private_segment_count = 0;
-  pm->private_segment_size = 0;
+  pm->segment_size = 512 << 20;
 
   if (vlib_num_workers ())
     clib_spinlock_init (&pm->sessions_lock);
@@ -818,13 +879,7 @@ proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
       else if (unformat (line_input, "private-segment-size %U",
                         unformat_memory_size, &tmp64))
        {
-         if (tmp64 >= 0x100000000ULL)
-           {
-             error = clib_error_return (
-               0, "private segment size %lld (%llu) too large", tmp64, tmp64);
-             goto done;
-           }
-         pm->private_segment_size = tmp64;
+         pm->segment_size = tmp64;
        }
       else if (unformat (line_input, "server-uri %s", &server_uri))
        vec_add1 (server_uri, 0);