session: use generic session pool for listeners
[vpp.git] / src / vnet / session / session_api.c
index 11fa0fa..2a74a19 100755 (executable)
@@ -56,6 +56,8 @@ _(SESSION_ENABLE_DISABLE, session_enable_disable)                     \
 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del)                                \
 _(SESSION_RULE_ADD_DEL, session_rule_add_del)                          \
 _(SESSION_RULES_DUMP, session_rules_dump)                              \
+_(APPLICATION_TLS_CERT_ADD, application_tls_cert_add)                  \
+_(APPLICATION_TLS_KEY_ADD, application_tls_key_add)                    \
 
 static int
 session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
@@ -172,7 +174,7 @@ send_session_accept_callback (stream_session_t * s)
 
   if (session_has_transport (s))
     {
-      listener = listen_session_get (s->session_type, s->listener_index);
+      listener = listen_session_get (s->listener_index);
       mp->listener_handle = listen_session_get_handle (listener);
       if (application_is_proxy (server))
        {
@@ -197,15 +199,24 @@ send_session_accept_callback (stream_session_t * s)
       local_session_t *ll;
       if (application_local_session_listener_has_transport (ls))
        {
-         listener = listen_session_get (ls->listener_session_type,
-                                        ls->listener_index);
+         listener = listen_session_get (ls->listener_index);
          mp->listener_handle = listen_session_get_handle (listener);
+         mp->is_ip4 = session_type_is_ip4 (listener->session_type);
        }
       else
        {
          ll = application_get_local_listen_session (server,
                                                     ls->listener_index);
-         mp->listener_handle = application_local_session_handle (ll);
+         if (ll->transport_listener_index != ~0)
+           {
+             listener = listen_session_get (ll->transport_listener_index);
+             mp->listener_handle = listen_session_get_handle (listener);
+           }
+         else
+           {
+             mp->listener_handle = application_local_session_handle (ll);
+           }
+         mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
        }
       mp->handle = application_local_session_handle (ls);
       mp->port = ls->port;
@@ -548,12 +559,10 @@ vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
       a->uri = (char *) mp->uri;
       a->api_context = mp->context;
       a->app_index = app->index;
-      a->mp = mp;
       if ((error = vnet_connect_uri (a)))
        {
          rv = clib_error_get_code (error);
-         if (rv != VNET_API_ERROR_SESSION_REDIRECT)
-           clib_error_report (error);
+         clib_error_report (error);
        }
     }
   else
@@ -566,7 +575,7 @@ vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
    * the connection is established. In case of the redirects, the reply
    * will come from the server app.
    */
-  if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
+  if (rv == 0)
     return;
 
 done:
@@ -761,6 +770,7 @@ done:
       {
        rmp->handle = a->handle;
         rmp->lcl_port = mp->port;
+       rmp->lcl_is_ip4 = mp->is_ip4;
        if (app && application_has_global_scope (app))
          {
            s = listen_session_get_from_handle (a->handle);
@@ -825,6 +835,7 @@ vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
       svm_queue_t *client_q;
       ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
 
+      memset (a, 0, sizeof (*a));
       client_q = vl_api_client_index_to_input_queue (mp->client_index);
       mp->client_queue_address = pointer_to_uword (client_q);
       a->sep.is_ip4 = mp->is_ip4;
@@ -833,22 +844,26 @@ vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
       a->sep.transport_proto = mp->proto;
       a->sep.fib_index = mp->vrf;
       a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
+      if (mp->hostname_len)
+       {
+         vec_validate (a->sep.hostname, mp->hostname_len - 1);
+         clib_memcpy (a->sep.hostname, mp->hostname, mp->hostname_len);
+       }
       a->api_context = mp->context;
       a->app_index = app->index;
-      a->mp = mp;
       if ((error = vnet_connect (a)))
        {
          rv = clib_error_get_code (error);
-         if (rv != VNET_API_ERROR_SESSION_REDIRECT)
-           clib_error_report (error);
+         clib_error_report (error);
        }
+      vec_free (a->sep.hostname);
     }
   else
     {
       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
     }
 
-  if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
+  if (rv == 0)
     return;
 
   /* Got some error, relay it */
@@ -1091,6 +1106,86 @@ vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
   /* *INDENT-ON* */
 }
 
+static void
+vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
+                                          mp)
+{
+  vl_api_app_namespace_add_del_reply_t *rmp;
+  vnet_app_add_tls_cert_args_t _a, *a = &_a;
+  clib_error_t *error;
+  application_t *app;
+  u32 cert_len;
+  int rv = 0;
+  if (!session_manager_is_enabled ())
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
+  if (!(app = application_lookup (mp->client_index)))
+    {
+      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+      goto done;
+    }
+  memset (a, 0, sizeof (*a));
+  a->app_index = app->index;
+  cert_len = clib_net_to_host_u16 (mp->cert_len);
+  if (cert_len > 10000)
+    {
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto done;
+    }
+  vec_validate (a->cert, cert_len);
+  clib_memcpy (a->cert, mp->cert, cert_len);
+  if ((error = vnet_app_add_tls_cert (a)))
+    {
+      rv = clib_error_get_code (error);
+      clib_error_report (error);
+    }
+  vec_free (a->cert);
+done:
+  REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
+}
+
+static void
+vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
+                                         mp)
+{
+  vl_api_app_namespace_add_del_reply_t *rmp;
+  vnet_app_add_tls_key_args_t _a, *a = &_a;
+  clib_error_t *error;
+  application_t *app;
+  u32 key_len;
+  int rv = 0;
+  if (!session_manager_is_enabled ())
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
+  if (!(app = application_lookup (mp->client_index)))
+    {
+      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+      goto done;
+    }
+  memset (a, 0, sizeof (*a));
+  a->app_index = app->index;
+  key_len = clib_net_to_host_u16 (mp->key_len);
+  if (key_len > 10000)
+    {
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto done;
+    }
+  vec_validate (a->key, key_len);
+  clib_memcpy (a->key, mp->key, key_len);
+  if ((error = vnet_app_add_tls_key (a)))
+    {
+      rv = clib_error_get_code (error);
+      clib_error_report (error);
+    }
+  vec_free (a->key);
+done:
+  REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
+}
+
 static clib_error_t *
 application_reaper_cb (u32 client_index)
 {