session: segment manager refactor
[vpp.git] / src / vnet / session / application_interface.c
index 8599c74..efdd3dd 100644 (file)
@@ -109,7 +109,7 @@ vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
 
   table_index = application_session_table (app,
                                           session_endpoint_fib_proto (sep));
-  listener = session_lookup_session_endpoint (table_index, sep);
+  listener = session_lookup_endpoint_listener (table_index, sep, 1);
   if (listener != SESSION_INVALID_HANDLE)
     return VNET_API_ERROR_ADDRESS_IN_USE;
 
@@ -120,7 +120,7 @@ vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
   if (application_has_local_scope (app) && session_endpoint_is_zero (sep))
     {
       table_index = application_local_session_table (app);
-      listener = session_lookup_session_endpoint (table_index, sep);
+      listener = session_lookup_endpoint_listener (table_index, sep, 1);
       if (listener != SESSION_INVALID_HANDLE)
        return VNET_API_ERROR_ADDRESS_IN_USE;
       session_lookup_add_session_endpoint (table_index, sep, app->index);
@@ -206,7 +206,7 @@ vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
                void *mp)
 {
   application_t *server, *app;
-  u32 table_index;
+  u32 table_index, server_index;
   stream_session_t *listener;
 
   if (session_endpoint_is_zero (sep))
@@ -223,14 +223,25 @@ vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
   if (application_has_local_scope (app))
     {
       table_index = application_local_session_table (app);
-      app_index = session_lookup_local_session_endpoint (table_index, sep);
-      server = application_get (app_index);
+      server_index = session_lookup_local_endpoint (table_index, sep);
+      if (server_index == APP_DROP_INDEX)
+       return VNET_API_ERROR_APP_CONNECT_FILTERED;
+
       /*
-       * Server is willing to have a direct fifo connection created
-       * instead of going through the state machine, etc.
+       * Break loop if rule in local table points to connecting app. This
+       * can happen if client is a generic proxy. Route connect through
+       * global table instead.
        */
-      if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
-       return app_connect_redirect (server, mp);
+      if (server_index != app_index)
+       {
+         server = application_get (server_index);
+         /*
+          * Server is willing to have a direct fifo connection created
+          * instead of going through the state machine, etc.
+          */
+         if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
+           return app_connect_redirect (server, mp);
+       }
     }
 
   /*
@@ -264,9 +275,8 @@ vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
 /**
  * unformat a vnet URI
  *
- * fifo://name
- * tcp://ip46-addr:port
- * udp://ip46-addr:port
+ * transport-proto://ip46-addr:port
+ * eg. tcp://ip46-addr:port
  *
  * u8 ip46_address[16];
  * u16  port_in_host_byte_order;
@@ -282,40 +292,25 @@ uword
 unformat_vnet_uri (unformat_input_t * input, va_list * args)
 {
   session_endpoint_t *sep = va_arg (*args, session_endpoint_t *);
-
-  if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &sep->ip.ip4,
-               &sep->port))
-    {
-      sep->transport_proto = TRANSPORT_PROTO_TCP;
-      sep->port = clib_host_to_net_u16 (sep->port);
-      sep->is_ip4 = 1;
-      return 1;
-    }
-  if (unformat (input, "udp://%U/%d", unformat_ip4_address, &sep->ip.ip4,
+  u32 transport_proto = 0;
+  if (unformat (input, "%U://%U/%d", unformat_transport_proto,
+               &transport_proto, unformat_ip4_address, &sep->ip.ip4,
                &sep->port))
     {
-      sep->transport_proto = TRANSPORT_PROTO_UDP;
+      sep->transport_proto = transport_proto;
       sep->port = clib_host_to_net_u16 (sep->port);
       sep->is_ip4 = 1;
       return 1;
     }
-  if (unformat (input, "udp://%U/%d", unformat_ip6_address, &sep->ip.ip6,
+  if (unformat (input, "%U://%U/%d", unformat_transport_proto,
+               &transport_proto, unformat_ip6_address, &sep->ip.ip6,
                &sep->port))
     {
-      sep->transport_proto = TRANSPORT_PROTO_UDP;
+      sep->transport_proto = transport_proto;
       sep->port = clib_host_to_net_u16 (sep->port);
       sep->is_ip4 = 0;
       return 1;
     }
-  if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &sep->ip.ip6,
-               &sep->port))
-    {
-      sep->transport_proto = TRANSPORT_PROTO_TCP;
-      sep->port = clib_host_to_net_u16 (sep->port);
-      sep->is_ip4 = 0;
-      return 1;
-    }
-
   return 0;
 }
 
@@ -387,11 +382,11 @@ session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
 clib_error_t *
 vnet_application_attach (vnet_app_attach_args_t * a)
 {
+  svm_fifo_segment_private_t *fs;
   application_t *app = 0;
   segment_manager_t *sm;
-  u8 *seg_name;
-  u64 secret;
   u32 app_ns_index = 0;
+  u64 secret;
   int rv;
 
   app = application_lookup (a->api_client_index);
@@ -411,13 +406,17 @@ vnet_application_attach (vnet_app_attach_args_t * a)
 
   a->app_event_queue_address = pointer_to_uword (app->event_queue);
   sm = segment_manager_get (app->first_segment_manager);
-  segment_manager_get_segment_info (sm->segment_indices[0],
-                                   &seg_name, &a->segment_size);
+  fs = segment_manager_get_segment_w_lock (sm, 0);
 
-  a->segment_name_length = vec_len (seg_name);
-  a->segment_name = seg_name;
-  ASSERT (vec_len (a->segment_name) <= 128);
+  if (application_is_proxy (app))
+    application_setup_proxy (app);
+
+  ASSERT (vec_len (fs->ssvm.name) <= 128);
+  a->segment = &fs->ssvm;
   a->app_index = app->index;
+
+  segment_manager_segment_reader_unlock (sm);
+
   return 0;
 }
 
@@ -503,8 +502,7 @@ vnet_disconnect_session (vnet_disconnect_args_t * a)
   /* We're peeking into another's thread pool. Make sure */
   ASSERT (s->session_index == index);
 
-  session_send_session_evt_to_thread (a->handle, FIFO_EVENT_DISCONNECT,
-                                     thread_index);
+  stream_session_disconnect (s);
   return 0;
 }