session: implement app_ns deletion

Type: feature

Change-Id: If0edbb21a0283d66c648a9e190d238c8cfa56353
Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c
index d3e598c..d8b2b3e 100644
--- a/src/vnet/session/application.c
+++ b/src/vnet/session/application.c
@@ -947,6 +947,31 @@
   vec_free (wrks);
 }
 
+void
+application_namespace_cleanup (app_namespace_t *app_ns)
+{
+  u32 *app_indices = 0, *app_index;
+  application_t *app;
+  u32 ns_index;
+
+  ns_index = app_namespace_index (app_ns);
+  pool_foreach (app, app_main.app_pool)
+    if (app->ns_index == ns_index)
+      vec_add1 (app_indices, app->ns_index);
+
+  vec_foreach (app_index, app_indices)
+    {
+      app = application_get (*app_index);
+
+      if (application_is_proxy (app))
+	application_remove_proxy (app);
+      app->flags &= ~APP_OPTIONS_FLAGS_IS_PROXY;
+
+      application_free (app);
+    }
+  vec_free (app_indices);
+}
+
 app_worker_t *
 application_get_worker (application_t * app, u32 wrk_map_index)
 {
@@ -1611,12 +1636,8 @@
 
   ASSERT (application_is_proxy (app));
 
-  /* *INDENT-OFF* */
-  transport_proto_foreach (tp, ({
-    if (transports & (1 << tp))
-      application_start_stop_proxy (app, tp, 1);
-  }));
-  /* *INDENT-ON* */
+  transport_proto_foreach (tp, transports)
+    application_start_stop_proxy (app, tp, 1);
 }
 
 void
@@ -1627,12 +1648,8 @@
 
   ASSERT (application_is_proxy (app));
 
-  /* *INDENT-OFF* */
-  transport_proto_foreach (tp, ({
-    if (transports & (1 << tp))
-      application_start_stop_proxy (app, tp, 0);
-  }));
-  /* *INDENT-ON* */
+  transport_proto_foreach (tp, transports)
+    application_start_stop_proxy (app, tp, 0);
 }
 
 segment_manager_props_t *
diff --git a/src/vnet/session/application.h b/src/vnet/session/application.h
index 6afbaa3..b3201b9 100644
--- a/src/vnet/session/application.h
+++ b/src/vnet/session/application.h
@@ -280,6 +280,7 @@
 u8 application_has_global_scope (application_t * app);
 void application_setup_proxy (application_t * app);
 void application_remove_proxy (application_t * app);
+void application_namespace_cleanup (app_namespace_t *app_ns);
 
 segment_manager_props_t *application_get_segment_manager_properties (u32
 								     app_index);
diff --git a/src/vnet/session/application_namespace.c b/src/vnet/session/application_namespace.c
index 349e79b..bece746 100644
--- a/src/vnet/session/application_namespace.c
+++ b/src/vnet/session/application_namespace.c
@@ -55,6 +55,15 @@
   return (app_ns - app_namespace_pool);
 }
 
+void
+app_namespace_free (app_namespace_t *app_ns)
+{
+  hash_unset_mem (app_namespace_lookup_table, app_ns->ns_id);
+  vec_free (app_ns->ns_id);
+
+  pool_put (app_namespace_pool, app_ns);
+}
+
 app_namespace_t *
 app_namespace_alloc (const u8 *ns_id)
 {
@@ -77,6 +86,7 @@
 {
   app_namespace_t *app_ns;
   session_table_t *st;
+  u32 ns_index;
   int rv;
 
   if (a->is_add)
@@ -109,10 +119,28 @@
 	  st->is_local = 1;
 	  st->appns_index = app_namespace_index (app_ns);
 	  app_ns->local_table_index = session_table_index (st);
+	  if (a->netns)
+	    {
+	      app_ns->netns = vec_dup (a->netns);
+	      vec_terminate_c_string (app_ns->netns);
+	    }
+	  if (a->sock_name)
+	    {
+	      app_ns->sock_name = vec_dup (a->sock_name);
+	      vec_terminate_c_string (app_ns->sock_name);
+	    }
+
+	  /* Add socket for namespace,
+	   * only at creation time */
+	  if (app_sapi_enabled)
+	    {
+	      rv = appns_sapi_add_ns_socket (app_ns);
+	      if (rv)
+		return rv;
+	    }
 	}
+
       app_ns->ns_secret = a->secret;
-      app_ns->netns = a->netns ? vec_dup (a->netns) : 0;
-      app_ns->sock_name = a->sock_name ? vec_dup (a->sock_name) : 0;
       app_ns->sw_if_index = a->sw_if_index;
       app_ns->ip4_fib_index =
 	fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
@@ -120,18 +148,33 @@
 	fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
       session_lookup_set_tables_appns (app_ns);
 
-      /* Add socket for namespace */
-      if (app_sapi_enabled)
-	{
-	  rv = appns_sapi_add_ns_socket (app_ns);
-	  if (rv)
-	    return rv;
-	}
     }
   else
     {
-      return VNET_API_ERROR_UNIMPLEMENTED;
+      ns_index = app_namespace_index_from_id (a->ns_id);
+      if (ns_index == APP_NAMESPACE_INVALID_INDEX)
+	return VNET_API_ERROR_INVALID_VALUE;
+
+      app_ns = app_namespace_get (ns_index);
+      if (!app_ns)
+	return VNET_API_ERROR_INVALID_VALUE;
+
+      application_namespace_cleanup (app_ns);
+
+      if (app_sapi_enabled)
+	appns_sapi_del_ns_socket (app_ns);
+
+      st = session_table_get (app_ns->local_table_index);
+
+      session_table_free (st, FIB_PROTOCOL_MAX);
+      if (app_ns->netns)
+	vec_free (app_ns->netns);
+      if (app_ns->sock_name)
+	vec_free (app_ns->sock_name);
+
+      app_namespace_free (app_ns);
     }
+
   return 0;
 }
 
@@ -241,6 +284,8 @@
     {
       if (unformat (line_input, "add"))
 	is_add = 1;
+      else if (unformat (line_input, "del"))
+	is_add = 0;
       else if (unformat (line_input, "id %_%v%_", &ns_id))
 	;
       else if (unformat (line_input, "secret %lu", &secret))
@@ -264,31 +309,33 @@
 	}
     }
 
-  if (!ns_id || !secret_set || !sw_if_index_set)
+  if (!ns_id)
     {
-      vlib_cli_output (vm, "namespace-id, secret and interface must be "
-			   "provided");
+      vlib_cli_output (vm, "namespace-id must be provided");
       goto done;
     }
 
-  if (is_add)
+  if (is_add && (!secret_set || !sw_if_index_set))
     {
-      /* clang-format off */
-      vnet_app_namespace_add_del_args_t args = {
-	.ns_id = ns_id,
-	.netns = netns,
-	.sock_name = sock_name,
-	.secret = secret,
-	.sw_if_index = sw_if_index,
-	.ip4_fib_id = fib_id,
-	.is_add = 1
-      };
-      /* clang-format on */
-
-      if ((rv = vnet_app_namespace_add_del (&args)))
-	error = clib_error_return (0, "app namespace add del returned %d", rv);
+      vlib_cli_output (vm, "secret and interface must be provided");
+      goto done;
     }
 
+  /* clang-format off */
+  vnet_app_namespace_add_del_args_t args = {
+    .ns_id = ns_id,
+    .netns = netns,
+    .secret = secret,
+    .sw_if_index = sw_if_index,
+    .sock_name = sock_name,
+    .ip4_fib_id = fib_id,
+    .is_add = is_add,
+  };
+  /* clang-format on */
+
+  if ((rv = vnet_app_namespace_add_del (&args)))
+    error = clib_error_return (0, "app namespace add del returned %d", rv);
+
 done:
 
   vec_free (ns_id);
@@ -302,8 +349,8 @@
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (app_ns_command, static) = {
   .path = "app ns",
-  .short_help = "app ns [add] id <namespace-id> secret <secret> "
-		"if <intfc> [netns <ns>]",
+  .short_help = "app ns [add|del] id <namespace-id> secret <secret> "
+		"sw_if_index <sw_if_index> if <interface> [netns <ns>]",
   .function = app_ns_fn,
 };
 /* *INDENT-ON* */
diff --git a/src/vnet/session/application_namespace.h b/src/vnet/session/application_namespace.h
index 5449d47..14e6a65 100644
--- a/src/vnet/session/application_namespace.h
+++ b/src/vnet/session/application_namespace.h
@@ -160,6 +160,7 @@
 }
 
 int appns_sapi_add_ns_socket (app_namespace_t * app_ns);
+void appns_sapi_del_ns_socket (app_namespace_t *app_ns);
 u8 appns_sapi_enabled (void);
 void appns_sapi_enable (void);
 
diff --git a/src/vnet/session/mma_template.c b/src/vnet/session/mma_template.c
index ae730e5..4b2770b 100644
--- a/src/vnet/session/mma_template.c
+++ b/src/vnet/session/mma_template.c
@@ -65,6 +65,11 @@
   return rule;
 }
 
+void RT (mma_rules_table_free) (RTT (mma_rules_table) * srt)
+{
+  pool_free (srt->rules);
+}
+
 RTT (mma_rule) *
 RT (mma_rules_table_get_rule) (RTT (mma_rules_table) * srt, u32 srt_index)
 {
diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c
index c0ed125..c031545 100644
--- a/src/vnet/session/session_api.c
+++ b/src/vnet/session/session_api.c
@@ -930,7 +930,7 @@
   mp->netns[sizeof (mp->netns) - 1] = 0;
   ns_id = format (0, "%s", &mp->namespace_id);
   netns = format (0, "%s", &mp->netns);
-  sock_name = format (0, "%s", &mp->sock_name);
+  sock_name = vl_api_from_api_to_new_vec (mp, &mp->sock_name);
   vnet_app_namespace_add_del_args_t args = {
     .ns_id = ns_id,
     .netns = netns,
@@ -1762,6 +1762,23 @@
   return err;
 }
 
+void
+appns_sapi_del_ns_socket (app_namespace_t *app_ns)
+{
+  app_ns_api_handle_t *handle;
+  clib_socket_t *cs;
+
+  pool_foreach (cs, app_ns->app_sockets)
+    {
+      handle = (app_ns_api_handle_t *) &cs->private_data;
+      clib_file_del_by_index (&file_main, handle->aah_file_index);
+
+      clib_socket_close (cs);
+      clib_socket_free (cs);
+    }
+  pool_free (app_ns->app_sockets);
+}
+
 int
 appns_sapi_add_ns_socket (app_namespace_t * app_ns)
 {
@@ -1799,7 +1816,7 @@
    * Create and initialize socket to listen on
    */
   cs = appns_sapi_alloc_socket (app_ns);
-  cs->config = (char *) app_ns->sock_name;
+  cs->config = (char *) vec_dup (app_ns->sock_name);
   cs->flags = CLIB_SOCKET_F_IS_SERVER |
     CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
     CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
diff --git a/src/vnet/session/session_rules_table.c b/src/vnet/session/session_rules_table.c
index 34bd6a3..5108c00 100644
--- a/src/vnet/session/session_rules_table.c
+++ b/src/vnet/session/session_rules_table.c
@@ -514,6 +514,13 @@
 }
 
 void
+session_rules_table_free (session_rules_table_t *srt)
+{
+  mma_rules_table_free_16 (&srt->session_rules_tables_16);
+  mma_rules_table_free_40 (&srt->session_rules_tables_40);
+}
+
+void
 session_rules_table_init (session_rules_table_t * srt)
 {
   mma_rules_table_16_t *srt4;
diff --git a/src/vnet/session/session_rules_table.h b/src/vnet/session/session_rules_table.h
index 8679cb8..206ef2f 100644
--- a/src/vnet/session/session_rules_table.h
+++ b/src/vnet/session/session_rules_table.h
@@ -116,6 +116,7 @@
 u8 *session_rules_table_rule_tag (session_rules_table_t * srt, u32 ri,
 				  u8 is_ip4);
 void session_rules_table_init (session_rules_table_t * srt);
+void session_rules_table_free (session_rules_table_t *srt);
 #endif /* SRC_VNET_SESSION_SESSION_RULES_TABLE_H_ */
 /*
  * fd.io coding-style-patch-verification: ON
diff --git a/src/vnet/session/session_table.c b/src/vnet/session/session_table.c
index d0b576f..9af8ae6 100644
--- a/src/vnet/session/session_table.c
+++ b/src/vnet/session/session_table.c
@@ -60,6 +60,31 @@
   _(v6,halfopen,buckets,20000)                  \
   _(v6,halfopen,memory,(64<<20))
 
+void
+session_table_free (session_table_t *slt, u8 fib_proto)
+{
+  u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0;
+  int i;
+
+  for (i = 0; i < TRANSPORT_N_PROTOS; i++)
+    session_rules_table_free (&slt->session_rules[i]);
+
+  vec_free (slt->session_rules);
+
+  if (fib_proto == FIB_PROTOCOL_IP4 || all)
+    {
+      clib_bihash_free_16_8 (&slt->v4_session_hash);
+      clib_bihash_free_16_8 (&slt->v4_half_open_hash);
+    }
+  if (fib_proto == FIB_PROTOCOL_IP6 || all)
+    {
+      clib_bihash_free_48_8 (&slt->v6_session_hash);
+      clib_bihash_free_48_8 (&slt->v6_half_open_hash);
+    }
+
+  pool_put (lookup_tables, slt);
+}
+
 /**
  * Initialize session table hash tables
  *
diff --git a/src/vnet/session/session_table.h b/src/vnet/session/session_table.h
index ead3c30..2127ea4 100644
--- a/src/vnet/session/session_table.h
+++ b/src/vnet/session/session_table.h
@@ -67,6 +67,7 @@
 session_table_t *session_table_get (u32 table_index);
 u32 session_table_index (session_table_t * slt);
 void session_table_init (session_table_t * slt, u8 fib_proto);
+void session_table_free (session_table_t *slt, u8 fib_proto);
 
 /* Internal, try not to use it! */
 session_table_t *_get_session_tables ();
diff --git a/src/vnet/session/transport.h b/src/vnet/session/transport.h
index 447552c..2cfec06 100644
--- a/src/vnet/session/transport.h
+++ b/src/vnet/session/transport.h
@@ -127,12 +127,10 @@
 
 extern transport_proto_vft_t *tp_vfts;
 
-#define transport_proto_foreach(VAR, BODY)			\
-do {								\
-    for (VAR = 0; VAR < vec_len (tp_vfts); VAR++)		\
-      if (tp_vfts[VAR].push_header != 0)			\
-	do { BODY; } while (0);					\
-} while (0)
+#define transport_proto_foreach(VAR, VAR_ALLOW_BM)                            \
+  for (VAR = 0; VAR < vec_len (tp_vfts); VAR++)                               \
+    if (tp_vfts[VAR].push_header != 0)                                        \
+      if (VAR_ALLOW_BM & (1 << VAR))
 
 int transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep);
 void transport_half_close (transport_proto_t tp, u32 conn_index,