session: add support for memfd segments
[vpp.git] / src / vnet / tcp / builtin_proxy.c
index 91377e7..a4827bf 100644 (file)
@@ -32,7 +32,7 @@ delete_proxy_session (stream_session_t * s, int is_active_open)
   uword *p;
   u64 handle;
 
-  handle = stream_session_handle (s);
+  handle = session_handle (s);
 
   clib_spinlock_lock_if_init (&bpm->sessions_lock);
   if (is_active_open)
@@ -50,8 +50,7 @@ delete_proxy_session (stream_session_t * s, int is_active_open)
        {
          ps = pool_elt_at_index (bpm->sessions, p[0]);
          if (ps->vpp_server_handle != ~0)
-           server_session = stream_session_get_from_handle
-             (ps->vpp_server_handle);
+           server_session = session_get_from_handle (ps->vpp_server_handle);
          else
            server_session = 0;
        }
@@ -71,7 +70,7 @@ delete_proxy_session (stream_session_t * s, int is_active_open)
        {
          ps = pool_elt_at_index (bpm->sessions, p[0]);
          if (ps->vpp_server_handle != ~0)
-           active_open_session = stream_session_get_from_handle
+           active_open_session = session_get_from_handle
              (ps->vpp_server_handle);
          else
            active_open_session = 0;
@@ -89,19 +88,19 @@ delete_proxy_session (stream_session_t * s, int is_active_open)
 
   if (active_open_session)
     {
-      a->handle = stream_session_handle (active_open_session);
+      a->handle = session_handle (active_open_session);
       a->app_index = bpm->active_open_app_index;
       hash_unset (bpm->proxy_session_by_active_open_handle,
-                 stream_session_handle (active_open_session));
+                 session_handle (active_open_session));
       vnet_disconnect_session (a);
     }
 
   if (server_session)
     {
-      a->handle = stream_session_handle (server_session);
+      a->handle = session_handle (server_session);
       a->app_index = bpm->server_app_index;
       hash_unset (bpm->proxy_session_by_server_handle,
-                 stream_session_handle (server_session));
+                 session_handle (server_session));
       vnet_disconnect_session (a);
     }
 }
@@ -140,8 +139,7 @@ server_connected_callback (u32 app_index, u32 api_context,
 }
 
 static int
-server_add_segment_callback (u32 client_index,
-                            const u8 * seg_name, u32 seg_size)
+server_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
 {
   clib_warning ("called...");
   return -1;
@@ -172,8 +170,7 @@ server_rx_callback (stream_session_t * s)
   ASSERT (s->thread_index == thread_index);
 
   clib_spinlock_lock_if_init (&bpm->sessions_lock);
-  p =
-    hash_get (bpm->proxy_session_by_server_handle, stream_session_handle (s));
+  p = hash_get (bpm->proxy_session_by_server_handle, session_handle (s));
 
   if (PREDICT_TRUE (p != 0))
     {
@@ -187,7 +184,7 @@ server_rx_callback (stream_session_t * s)
        {
          evt.fifo = active_open_tx_fifo;
          evt.event_type = FIFO_EVENT_APP_TX;
-         if (unix_shared_memory_queue_add
+         if (svm_queue_add
              (bpm->active_open_event_queue[thread_index], (u8 *) & evt,
               0 /* do wait for mutex */ ))
            clib_warning ("failed to enqueue tx evt");
@@ -219,7 +216,7 @@ server_rx_callback (stream_session_t * s)
       memset (ps, 0, sizeof (*ps));
       ps->server_rx_fifo = rx_fifo;
       ps->server_tx_fifo = tx_fifo;
-      ps->vpp_server_handle = stream_session_handle (s);
+      ps->vpp_server_handle = session_handle (s);
 
       proxy_index = ps - bpm->sessions;
 
@@ -228,7 +225,7 @@ server_rx_callback (stream_session_t * s)
 
       clib_spinlock_unlock_if_init (&bpm->sessions_lock);
 
-      a->uri = "tcp://6.0.2.2/23";
+      a->uri = (char *) bpm->client_uri;
       a->api_context = proxy_index;
       a->app_index = bpm->active_open_app_index;
       a->mp = 0;
@@ -269,7 +266,7 @@ active_open_connected_callback (u32 app_index, u32 opaque,
   clib_spinlock_lock_if_init (&bpm->sessions_lock);
 
   ps = pool_elt_at_index (bpm->sessions, opaque);
-  ps->vpp_active_open_handle = stream_session_handle (s);
+  ps->vpp_active_open_handle = session_handle (s);
 
   s->server_tx_fifo = ps->server_rx_fifo;
   s->server_rx_fifo = ps->server_tx_fifo;
@@ -301,7 +298,7 @@ active_open_connected_callback (u32 app_index, u32 opaque,
     {
       evt.fifo = s->server_tx_fifo;
       evt.event_type = FIFO_EVENT_APP_TX;
-      if (unix_shared_memory_queue_add
+      if (svm_queue_add
          (bpm->active_open_event_queue[thread_index], (u8 *) & evt,
           0 /* do wait for mutex */ ))
        clib_warning ("failed to enqueue tx evt");
@@ -345,7 +342,7 @@ active_open_rx_callback (stream_session_t * s)
     {
       evt.fifo = server_rx_fifo;
       evt.event_type = FIFO_EVENT_APP_TX;
-      if (unix_shared_memory_queue_add
+      if (svm_queue_add
          (bpm->server_event_queue[thread_index], (u8 *) & evt,
           0 /* do wait for mutex */ ))
        clib_warning ("failed to enqueue server rx evt");
@@ -384,28 +381,26 @@ static int
 server_attach ()
 {
   builtin_proxy_main_t *bpm = &builtin_proxy_main;
-  u8 segment_name[128];
-  u64 options[SESSION_OPTIONS_N_OPTIONS];
+  u64 options[APP_OPTIONS_N_OPTIONS];
   vnet_app_attach_args_t _a, *a = &_a;
+  u32 segment_size = 512 << 20;
 
   memset (a, 0, sizeof (*a));
   memset (options, 0, sizeof (options));
 
+  if (bpm->private_segment_size)
+    segment_size = bpm->private_segment_size;
   a->api_client_index = bpm->server_client_index;
   a->session_cb_vft = &builtin_session_cb_vft;
   a->options = options;
-  a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20;
-  a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = bpm->fifo_size;
-  a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = bpm->fifo_size;
+  a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
+  a->options[APP_OPTIONS_RX_FIFO_SIZE] = bpm->fifo_size;
+  a->options[APP_OPTIONS_TX_FIFO_SIZE] = bpm->fifo_size;
   a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = bpm->private_segment_count;
-  a->options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = bpm->private_segment_size;
   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
     bpm->prealloc_fifos ? bpm->prealloc_fifos : 1;
 
-  a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
-
-  a->segment_name = segment_name;
-  a->segment_name_length = ARRAY_LEN (segment_name);
+  a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
 
   if (vnet_application_attach (a))
     {
@@ -422,30 +417,23 @@ active_open_attach (void)
 {
   builtin_proxy_main_t *bpm = &builtin_proxy_main;
   vnet_app_attach_args_t _a, *a = &_a;
-  u8 segment_name[128];
-  u32 segment_name_length;
   u64 options[16];
 
-  segment_name_length = ARRAY_LEN (segment_name);
-
   memset (a, 0, sizeof (*a));
   memset (options, 0, sizeof (options));
 
   a->api_client_index = bpm->active_open_client_index;
-  a->segment_name = segment_name;
-  a->segment_name_length = segment_name_length;
   a->session_cb_vft = &builtin_clients;
 
-  options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
-  options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20;
-  options[SESSION_OPTIONS_RX_FIFO_SIZE] = bpm->fifo_size;
-  options[SESSION_OPTIONS_TX_FIFO_SIZE] = bpm->fifo_size;
+  options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
+  options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
+  options[APP_OPTIONS_RX_FIFO_SIZE] = bpm->fifo_size;
+  options[APP_OPTIONS_TX_FIFO_SIZE] = bpm->fifo_size;
   options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = bpm->private_segment_count;
-  options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = bpm->private_segment_size;
   options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
     bpm->prealloc_fifos ? bpm->prealloc_fifos : 1;
 
-  options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP
+  options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
     | APP_OPTIONS_FLAGS_IS_PROXY;
 
   a->options = options;
@@ -465,7 +453,7 @@ server_listen ()
   vnet_bind_args_t _a, *a = &_a;
   memset (a, 0, sizeof (*a));
   a->app_index = bpm->server_app_index;
-  a->uri = "tcp://0.0.0.0/23";
+  a->uri = (char *) bpm->server_uri;
   return vnet_bind_uri (a);
 }
 
@@ -530,6 +518,7 @@ proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
   bpm->prealloc_fifos = 0;
   bpm->private_segment_count = 0;
   bpm->private_segment_size = 0;
+  bpm->server_uri = 0;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -550,11 +539,20 @@ proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
              (0, "private segment size %lld (%llu) too large", tmp, tmp);
          bpm->private_segment_size = tmp;
        }
+      else if (unformat (input, "server-uri %s", &bpm->server_uri))
+       ;
+      else if (unformat (input, "client-uri %s", &bpm->client_uri))
+       ;
       else
        return clib_error_return (0, "unknown input `%U'",
                                  format_unformat_error, input);
     }
 
+  if (!bpm->server_uri)
+    bpm->server_uri = format (0, "%s%c", "tcp://0.0.0.0/23", 0);
+  if (!bpm->client_uri)
+    bpm->client_uri = format (0, "%s%c", "tcp://6.0.2.2/23", 0);
+
   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
 
   rv = server_create (vm);
@@ -573,7 +571,10 @@ proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
 VLIB_CLI_COMMAND (server_create_command, static) =
 {
   .path = "test proxy server",
-  .short_help = "test proxy server",
+  .short_help = "test proxy server [server-uri <tcp://ip/port>]"
+      "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
+      "[prealloc-fifos <nn>][private-segment-size <mem>]"
+      "[private-segment-count <nn>]",
   .function = proxy_server_create_command_fn,
 };
 /* *INDENT-ON* */