Session layer refactoring
[vpp.git] / src / vnet / session / application_interface.c
index 4b30bd8..96d2c62 100644 (file)
@@ -79,81 +79,51 @@ api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
 }
 
 int
-vnet_bind_i (u32 api_client_index, ip46_address_t * ip46, u16 port_host_order,
-            session_type_t sst, u64 * options, session_cb_vft_t * cb_fns,
-            application_t ** app, u32 * len_seg_name, char *seg_name)
+vnet_bind_i (u32 app_index, session_type_t sst,
+            transport_endpoint_t * tep, u64 * handle)
 {
-  u8 *segment_name = 0;
-  application_t *server = 0;
+  application_t *app;
   stream_session_t *listener;
-  u8 is_ip4;
-
-  listener =
-    stream_session_lookup_listener (ip46,
-                                   clib_host_to_net_u16 (port_host_order),
-                                   sst);
-
-  if (listener)
-    return VNET_API_ERROR_ADDRESS_IN_USE;
 
-  if (application_lookup (api_client_index))
+  app = application_get_if_valid (app_index);
+  if (!app)
     {
-      clib_warning ("Only one connection supported for now");
-      return VNET_API_ERROR_ADDRESS_IN_USE;
+      clib_warning ("app not attached");
+      return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
     }
 
-  is_ip4 = SESSION_TYPE_IP4_UDP == sst || SESSION_TYPE_IP4_TCP == sst;
-  if (!ip_is_zero (ip46, is_ip4) && !ip_is_local (ip46, is_ip4))
-    return VNET_API_ERROR_INVALID_VALUE;
-
-  /* Allocate and initialize stream server */
-  server = application_new (APP_SERVER, sst, api_client_index,
-                           options[SESSION_OPTIONS_FLAGS], cb_fns);
+  listener = stream_session_lookup_listener (&tep->ip,
+                                            clib_host_to_net_u16 (tep->port),
+                                            sst);
+  if (listener)
+    return VNET_API_ERROR_ADDRESS_IN_USE;
 
-  application_server_init (server, options[SESSION_OPTIONS_SEGMENT_SIZE],
-                          options[SESSION_OPTIONS_ADD_SEGMENT_SIZE],
-                          options[SESSION_OPTIONS_RX_FIFO_SIZE],
-                          options[SESSION_OPTIONS_TX_FIFO_SIZE],
-                          &segment_name);
+  if (!ip_is_zero (&tep->ip, tep->is_ip4)
+      && !ip_is_local (&tep->ip, tep->is_ip4))
+    return VNET_API_ERROR_INVALID_VALUE_2;
 
   /* Setup listen path down to transport */
-  stream_session_start_listen (server->index, ip46, port_host_order);
-
-  /*
-   * Return values
-   */
-
-  ASSERT (vec_len (segment_name) <= 128);
-  *len_seg_name = vec_len (segment_name);
-  memcpy (seg_name, segment_name, *len_seg_name);
-  *app = server;
-
-  return 0;
+  return application_start_listen (app, sst, tep, handle);
 }
 
 int
-vnet_unbind_i (u32 api_client_index)
+vnet_unbind_i (u32 app_index, u64 handle)
 {
-  application_t *server;
+  application_t *app = application_get_if_valid (app_index);
 
-  /*
-   * Find the stream_server_t corresponding to the api client
-   */
-  server = application_lookup (api_client_index);
-  if (!server)
-    return VNET_API_ERROR_INVALID_VALUE_2;
+  if (!app)
+    {
+      clib_warning ("app not attached");
+      return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+    }
 
   /* Clear the listener */
-  stream_session_stop_listen (server->index);
-  application_del (server);
-
-  return 0;
+  return application_stop_listen (app, handle);
 }
 
 int
-vnet_connect_i (u32 api_client_index, u32 api_context, session_type_t sst,
-               ip46_address_t * ip46, u16 port, u64 * options, void *mp,
-               session_cb_vft_t * cb_fns)
+vnet_connect_i (u32 app_index, u32 api_context, session_type_t sst,
+               transport_endpoint_t * tep, void *mp)
 {
   stream_session_t *listener;
   application_t *server, *app;
@@ -161,8 +131,8 @@ vnet_connect_i (u32 api_client_index, u32 api_context, session_type_t sst,
   /*
    * Figure out if connecting to a local server
    */
-  listener = stream_session_lookup_listener (ip46,
-                                            clib_host_to_net_u16 (port),
+  listener = stream_session_lookup_listener (&tep->ip,
+                                            clib_host_to_net_u16 (tep->port),
                                             sst);
   if (listener)
     {
@@ -177,16 +147,11 @@ vnet_connect_i (u32 api_client_index, u32 api_context, session_type_t sst,
          redirect_connect_callback (server->api_client_index, mp);
     }
 
-  /* Create client app */
-  app = application_new (APP_CLIENT, sst, api_client_index,
-                        options[SESSION_OPTIONS_FLAGS], cb_fns);
-
-  app->api_context = api_context;
-
   /*
    * Not connecting to a local server. Create regular session
    */
-  return stream_session_open (sst, ip46, port, app->index);
+  app = application_get (app_index);
+  return application_open_session (app, sst, tep, api_context);
 }
 
 /**
@@ -209,30 +174,31 @@ vnet_connect_i (u32 api_client_index, u32 api_context, session_type_t sst,
 uword
 unformat_vnet_uri (unformat_input_t * input, va_list * args)
 {
-  ip46_address_t *address = va_arg (*args, ip46_address_t *);
   session_type_t *sst = va_arg (*args, session_type_t *);
-  u16 *port = va_arg (*args, u16 *);
+  transport_endpoint_t *tep = va_arg (*args, transport_endpoint_t *);
 
-  if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &address->ip4,
-               port))
+  if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &tep->ip.ip4,
+               &tep->port))
     {
       *sst = SESSION_TYPE_IP4_TCP;
+      tep->is_ip4 = 1;
       return 1;
     }
-  if (unformat (input, "udp://%U/%d", unformat_ip4_address, &address->ip4,
-               port))
+  if (unformat (input, "udp://%U/%d", unformat_ip4_address, &tep->ip.ip4,
+               &tep->port))
     {
       *sst = SESSION_TYPE_IP4_UDP;
+      tep->is_ip4 = 1;
       return 1;
     }
-  if (unformat (input, "udp://%U/%d", unformat_ip6_address, &address->ip6,
-               port))
+  if (unformat (input, "udp://%U/%d", unformat_ip6_address, &tep->ip.ip6,
+               &tep->port))
     {
       *sst = SESSION_TYPE_IP6_UDP;
       return 1;
     }
-  if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &address->ip6,
-               port))
+  if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &tep->ip.ip6,
+               &tep->port))
     {
       *sst = SESSION_TYPE_IP6_TCP;
       return 1;
@@ -242,8 +208,7 @@ unformat_vnet_uri (unformat_input_t * input, va_list * args)
 }
 
 int
-parse_uri (char *uri, session_type_t * sst, ip46_address_t * addr,
-          u16 * port_number_host_byte_order)
+parse_uri (char *uri, session_type_t * sst, transport_endpoint_t * tep)
 {
   unformat_input_t _input, *input = &_input;
 
@@ -252,8 +217,7 @@ parse_uri (char *uri, session_type_t * sst, ip46_address_t * addr,
 
   /* Parse uri */
   unformat_init_string (input, uri, strlen (uri));
-  if (!unformat (input, "%U", unformat_vnet_uri, addr, sst,
-                port_number_host_byte_order))
+  if (!unformat (input, "%U", unformat_vnet_uri, sst, tep))
     {
       unformat_free (input);
       return VNET_API_ERROR_INVALID_VALUE;
@@ -263,26 +227,51 @@ parse_uri (char *uri, session_type_t * sst, ip46_address_t * addr,
   return 0;
 }
 
+/**
+ * Attaches application.
+ *
+ * Allocates a vpp app, i.e., a structure that keeps back pointers
+ * to external app and a segment manager for shared memory fifo based
+ * communication with the external app.
+ */
 int
-vnet_bind_uri (vnet_bind_args_t * a)
+vnet_application_attach (vnet_app_attach_args_t * a)
 {
-  application_t *server = 0;
-  u16 port_host_order;
-  session_type_t sst = SESSION_N_TYPES;
-  ip46_address_t ip46;
+  application_t *app = 0;
+  segment_manager_t *sm;
+  u8 *seg_name;
   int rv;
 
-  memset (&ip46, 0, sizeof (ip46));
-  rv = parse_uri (a->uri, &sst, &ip46, &port_host_order);
-  if (rv)
+  app = application_new ();
+  if ((rv = application_init (app, a->api_client_index, a->options,
+                             a->session_cb_vft)))
     return rv;
 
-  if ((rv = vnet_bind_i (a->api_client_index, &ip46, port_host_order, sst,
-                        a->options, a->session_cb_vft, &server,
-                        &a->segment_name_length, a->segment_name)))
-    return rv;
+  a->app_event_queue_address = (u64) 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);
 
-  a->server_event_queue_address = (u64) server->event_queue;
+  a->segment_name_length = vec_len (seg_name);
+  a->segment_name = seg_name;
+  ASSERT (vec_len (a->segment_name) <= 128);
+  a->app_index = app->index;
+  return 0;
+}
+
+int
+vnet_application_detach (vnet_app_detach_args_t * a)
+{
+  application_t *app;
+  app = application_get_if_valid (a->app_index);
+
+  if (!app)
+    {
+      clib_warning ("app not attached");
+      return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+    }
+
+  application_del (app);
   return 0;
 }
 
@@ -308,125 +297,102 @@ session_type_from_proto_and_ip (session_api_proto_t proto, u8 is_ip4)
 }
 
 int
-vnet_unbind_uri (char *uri, u32 api_client_index)
+vnet_bind_uri (vnet_bind_args_t * a)
 {
-  u16 port_number_host_byte_order;
   session_type_t sst = SESSION_N_TYPES;
-  ip46_address_t ip46_address;
-  stream_session_t *listener;
+  transport_endpoint_t tep;
   int rv;
 
-  rv = parse_uri (uri, &sst, &ip46_address, &port_number_host_byte_order);
+  memset (&tep, 0, sizeof (tep));
+  rv = parse_uri (a->uri, &sst, &tep);
   if (rv)
     return rv;
 
-  listener =
-    stream_session_lookup_listener (&ip46_address,
-                                   clib_host_to_net_u16
-                                   (port_number_host_byte_order), sst);
+  if ((rv = vnet_bind_i (a->app_index, sst, &tep, &a->handle)))
+    return rv;
+
+  return 0;
+}
+
+int
+vnet_unbind_uri (vnet_unbind_args_t * a)
+{
+  session_type_t sst = SESSION_N_TYPES;
+  stream_session_t *listener;
+  transport_endpoint_t tep;
+  int rv;
+
+  rv = parse_uri (a->uri, &sst, &tep);
+  if (rv)
+    return rv;
 
+  listener = stream_session_lookup_listener (&tep.ip,
+                                            clib_host_to_net_u16 (tep.port),
+                                            sst);
   if (!listener)
     return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
 
-  /* External client? */
-  if (api_client_index != ~0)
-    {
-      ASSERT (vl_api_client_index_to_registration (api_client_index));
-    }
-
-  return vnet_unbind_i (api_client_index);
+  return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
 }
 
 int
 vnet_connect_uri (vnet_connect_args_t * a)
 {
-  ip46_address_t ip46_address;
-  u16 port;
+  transport_endpoint_t tep;
   session_type_t sst;
-  application_t *app;
   int rv;
 
-  app = application_lookup (a->api_client_index);
-  if (app)
-    {
-      clib_warning ("Already have a connect from this app");
-      return VNET_API_ERROR_INVALID_VALUE_2;
-    }
-
   /* Parse uri */
-  rv = parse_uri (a->uri, &sst, &ip46_address, &port);
+  memset (&tep, 0, sizeof (tep));
+  rv = parse_uri (a->uri, &sst, &tep);
   if (rv)
     return rv;
 
-  return vnet_connect_i (a->api_client_index, a->api_context, sst,
-                        &ip46_address, port, a->options, a->mp,
-                        a->session_cb_vft);
+  return vnet_connect_i (a->app_index, a->api_context, sst, &tep, a->mp);
 }
 
 int
-vnet_disconnect_session (u32 session_index, u32 thread_index)
+vnet_disconnect_session (vnet_disconnect_args_t * a)
 {
-  stream_session_t *session;
+  u32 index, thread_index;
+  stream_session_t *s;
 
-  session = stream_session_get (session_index, thread_index);
-  stream_session_disconnect (session);
+  stream_session_parse_handle (a->handle, &index, &thread_index);
+  s = stream_session_get_if_valid (index, thread_index);
+
+  if (!s || s->app_index != a->app_index)
+    return VNET_API_ERROR_INVALID_VALUE;
 
+  stream_session_disconnect (s);
   return 0;
 }
 
-
 int
 vnet_bind (vnet_bind_args_t * a)
 {
-  application_t *server = 0;
   session_type_t sst = SESSION_N_TYPES;
   int rv;
 
   sst = session_type_from_proto_and_ip (a->proto, a->tep.is_ip4);
-  if ((rv = vnet_bind_i (a->api_client_index, &a->tep.ip, a->tep.port, sst,
-                        a->options, a->session_cb_vft, &server,
-                        &a->segment_name_length, a->segment_name)))
+  if ((rv = vnet_bind_i (a->app_index, sst, &a->tep, &a->handle)))
     return rv;
 
-  a->server_event_queue_address = (u64) server->event_queue;
-  a->handle = (u64) a->tep.vrf << 32 | (u64) server->session_index;
   return 0;
 }
 
 int
 vnet_unbind (vnet_unbind_args_t * a)
 {
-  application_t *server;
-
-  if (a->api_client_index != ~0)
-    {
-      ASSERT (vl_api_client_index_to_registration (a->api_client_index));
-    }
-
-  /* Make sure this is the right one */
-  server = application_lookup (a->api_client_index);
-  ASSERT (server->session_index == (0xFFFFFFFF & a->handle));
-
-  /* TODO use handle to disambiguate namespaces/vrfs */
-  return vnet_unbind_i (a->api_client_index);
+  return vnet_unbind_i (a->app_index, a->handle);
 }
 
 int
 vnet_connect (vnet_connect_args_t * a)
 {
   session_type_t sst;
-  application_t *app;
-
-  app = application_lookup (a->api_client_index);
-  if (app)
-    {
-      clib_warning ("Already have a connect from this app");
-      return VNET_API_ERROR_INVALID_VALUE_2;
-    }
 
   sst = session_type_from_proto_and_ip (a->proto, a->tep.is_ip4);
-  return vnet_connect_i (a->api_client_index, a->api_context, sst, &a->tep.ip,
-                        a->tep.port, a->options, a->mp, a->session_cb_vft);
+  return vnet_connect_i (a->app_index, a->api_context, sst, &a->tep, a->mp);
 }
 
 int