udp: move cli to separate file

Type: refactor

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I2057ebb4b6a4af3ef8fd9b73aadfa00d63bae618
diff --git a/src/vnet/CMakeLists.txt b/src/vnet/CMakeLists.txt
index 21780e2..151b099 100644
--- a/src/vnet/CMakeLists.txt
+++ b/src/vnet/CMakeLists.txt
@@ -693,6 +693,7 @@
 ##############################################################################
 list(APPEND VNET_SOURCES
   udp/udp.c
+  udp/udp_cli.c
   udp/udp_input.c
   udp/udp_format.c
   udp/udp_local.c
diff --git a/src/vnet/udp/udp.c b/src/vnet/udp/udp.c
index 8f26b57..1a5621f 100644
--- a/src/vnet/udp/udp.c
+++ b/src/vnet/udp/udp.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2019 Cisco and/or its affiliates.
+ * Copyright (c) 2016-2020 Cisco and/or its affiliates.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
@@ -13,10 +13,6 @@
  * limitations under the License.
  */
 
-/** @file
-    udp state machine, etc.
-*/
-
 #include <vnet/udp/udp.h>
 #include <vnet/session/session.h>
 #include <vnet/dpo/load_balance.h>
@@ -160,7 +156,14 @@
 	  && udp_is_valid_dst_port (lcl_port, is_ip4));
 }
 
-u32
+static u16
+udp_default_mtu (udp_main_t * um, u8 is_ip4)
+{
+  u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
+  return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
+}
+
+static u32
 udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
 {
   udp_main_t *um = vnet_get_udp_main ();
@@ -196,7 +199,7 @@
   listener->c_proto = TRANSPORT_PROTO_UDP;
   listener->c_s_index = session_index;
   listener->c_fib_index = lcl->fib_index;
-  listener->mss = um->default_mtu - sizeof (udp_header_t);
+  listener->mss = udp_default_mtu (um, listener->c_is_ip4);
   listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
   lcl_ext = (transport_endpoint_cfg_t *) lcl;
   if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
@@ -209,7 +212,7 @@
   return listener->c_c_index;
 }
 
-u32
+static u32
 udp_session_unbind (u32 listener_index)
 {
   udp_main_t *um = &udp_main;
@@ -222,7 +225,7 @@
   return 0;
 }
 
-transport_connection_t *
+static transport_connection_t *
 udp_session_get_listener (u32 listener_index)
 {
   udp_connection_t *us;
@@ -231,7 +234,7 @@
   return &us->connection;
 }
 
-u32
+static u32
 udp_push_header (transport_connection_t * tc, vlib_buffer_t * b)
 {
   udp_connection_t *uc;
@@ -260,7 +263,7 @@
   return 0;
 }
 
-transport_connection_t *
+static transport_connection_t *
 udp_session_get (u32 connection_index, u32 thread_index)
 {
   udp_connection_t *uc;
@@ -270,7 +273,7 @@
   return 0;
 }
 
-void
+static void
 udp_session_close (u32 connection_index, u32 thread_index)
 {
   udp_connection_t *uc;
@@ -285,7 +288,7 @@
     uc->flags |= UDP_CONN_F_CLOSING;
 }
 
-void
+static void
 udp_session_cleanup (u32 connection_index, u32 thread_index)
 {
   udp_connection_t *uc;
@@ -298,111 +301,6 @@
     udp_connection_cleanup (uc);
 }
 
-u8 *
-format_udp_connection_id (u8 * s, va_list * args)
-{
-  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
-  if (!uc)
-    return s;
-  if (uc->c_is_ip4)
-    s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
-		uc->c_s_index, "U", format_ip4_address, &uc->c_lcl_ip4,
-		clib_net_to_host_u16 (uc->c_lcl_port), format_ip4_address,
-		&uc->c_rmt_ip4, clib_net_to_host_u16 (uc->c_rmt_port));
-  else
-    s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
-		uc->c_s_index, "U", format_ip6_address, &uc->c_lcl_ip6,
-		clib_net_to_host_u16 (uc->c_lcl_port), format_ip6_address,
-		&uc->c_rmt_ip6, clib_net_to_host_u16 (uc->c_rmt_port));
-  return s;
-}
-
-const char *udp_connection_flags_str[] = {
-#define _(sym, str) str,
-  foreach_udp_connection_flag
-#undef _
-};
-
-static u8 *
-format_udp_connection_flags (u8 * s, va_list * args)
-{
-  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
-  int i, last = -1;
-
-  for (i = 0; i < UDP_CONN_N_FLAGS; i++)
-    if (uc->flags & (1 << i))
-      last = i;
-  for (i = 0; i < last; i++)
-    {
-      if (uc->flags & (1 << i))
-	s = format (s, "%s, ", udp_connection_flags_str[i]);
-    }
-  if (last >= 0)
-    s = format (s, "%s", udp_connection_flags_str[last]);
-  return s;
-}
-
-static u8 *
-format_udp_vars (u8 * s, va_list * args)
-{
-  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
-  s = format (s, " index %u flags: %U", uc->c_c_index,
-	      format_udp_connection_flags, uc);
-
-  if (!(uc->flags & UDP_CONN_F_LISTEN))
-    s = format (s, "\n");
-  return s;
-}
-
-u8 *
-format_udp_connection (u8 * s, va_list * args)
-{
-  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
-  u32 verbose = va_arg (*args, u32);
-  if (!uc)
-    return s;
-  s = format (s, "%-50U", format_udp_connection_id, uc);
-  if (verbose)
-    {
-      s = format (s, "%-15s",
-		  (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
-      if (verbose > 1)
-	s = format (s, "\n%U", format_udp_vars, uc);
-    }
-  return s;
-}
-
-u8 *
-format_udp_session (u8 * s, va_list * args)
-{
-  u32 uci = va_arg (*args, u32);
-  u32 thread_index = va_arg (*args, u32);
-  u32 verbose = va_arg (*args, u32);
-  udp_connection_t *uc;
-
-  uc = udp_connection_get (uci, thread_index);
-  return format (s, "%U", format_udp_connection, uc, verbose);
-}
-
-u8 *
-format_udp_half_open_session (u8 * s, va_list * args)
-{
-  u32 __clib_unused tci = va_arg (*args, u32);
-  u32 __clib_unused thread_index = va_arg (*args, u32);
-  clib_warning ("BUG");
-  return 0;
-}
-
-u8 *
-format_udp_listener_session (u8 * s, va_list * args)
-{
-  u32 tci = va_arg (*args, u32);
-  u32 __clib_unused thread_index = va_arg (*args, u32);
-  u32 verbose = va_arg (*args, u32);
-  udp_connection_t *uc = udp_listener_get (tci);
-  return format (s, "%U", format_udp_connection, uc, verbose);
-}
-
 static int
 udp_session_send_params (transport_connection_t * tconn,
 			 transport_send_params_t * sp)
@@ -420,7 +318,7 @@
   return 0;
 }
 
-int
+static int
 udp_open_connection (transport_endpoint_cfg_t * rmt)
 {
   vlib_main_t *vm = vlib_get_main ();
@@ -486,7 +384,7 @@
   uc->c_is_ip4 = rmt->is_ip4;
   uc->c_proto = TRANSPORT_PROTO_UDP;
   uc->c_fib_index = rmt->fib_index;
-  uc->mss = rmt->mss ? rmt->mss : (um->default_mtu - sizeof (udp_header_t));
+  uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
   uc->flags |= UDP_CONN_F_OWNS_PORT;
   if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
     uc->flags |= UDP_CONN_F_CONNECTED;
@@ -496,7 +394,7 @@
   return uc->c_c_index;
 }
 
-transport_connection_t *
+static transport_connection_t *
 udp_session_get_half_open (u32 conn_index)
 {
   udp_connection_t *uc;
@@ -510,6 +408,37 @@
   return &uc->connection;
 }
 
+static u8 *
+format_udp_session (u8 * s, va_list * args)
+{
+  u32 uci = va_arg (*args, u32);
+  u32 thread_index = va_arg (*args, u32);
+  u32 verbose = va_arg (*args, u32);
+  udp_connection_t *uc;
+
+  uc = udp_connection_get (uci, thread_index);
+  return format (s, "%U", format_udp_connection, uc, verbose);
+}
+
+static u8 *
+format_udp_half_open_session (u8 * s, va_list * args)
+{
+  u32 __clib_unused tci = va_arg (*args, u32);
+  u32 __clib_unused thread_index = va_arg (*args, u32);
+  clib_warning ("BUG");
+  return 0;
+}
+
+static u8 *
+format_udp_listener_session (u8 * s, va_list * args)
+{
+  u32 tci = va_arg (*args, u32);
+  u32 __clib_unused thread_index = va_arg (*args, u32);
+  u32 verbose = va_arg (*args, u32);
+  udp_connection_t *uc = udp_listener_get (tci);
+  return format (s, "%U", format_udp_connection, uc, verbose);
+}
+
 /* *INDENT-OFF* */
 static const transport_proto_vft_t udp_proto = {
   .start_listen = udp_session_bind,
@@ -534,7 +463,6 @@
 };
 /* *INDENT-ON* */
 
-
 int
 udpc_connection_open (transport_endpoint_cfg_t * rmt)
 {
@@ -656,85 +584,6 @@
 };
 /* *INDENT-ON* */
 
-static clib_error_t *
-udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
-{
-  udp_main_t *um = &udp_main;
-  u32 tmp;
-
-  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
-    {
-      if (unformat (input, "mtu %u", &tmp))
-	um->default_mtu = tmp;
-      else
-	return clib_error_return (0, "unknown input `%U'",
-				  format_unformat_error, input);
-    }
-  return 0;
-}
-
-VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
-
-static clib_error_t *
-show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
-		  vlib_cli_command_t * cmd_arg)
-{
-  udp_main_t *um = vnet_get_udp_main ();
-
-  clib_error_t *error = NULL;
-
-  if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
-    return clib_error_return (0, "unknown input `%U'", format_unformat_error,
-			      input);
-
-  udp_dst_port_info_t *port_info;
-  if (um->punt_unknown4)
-    {
-      vlib_cli_output (vm, "IPv4 UDP punt: enabled");
-    }
-  else
-    {
-      u8 *s = NULL;
-      vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
-      {
-	if (udp_is_valid_dst_port (port_info->dst_port, 1))
-	  {
-	    s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
-	  }
-      }
-      s = format (s, "%c", 0);
-      vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
-    }
-
-  if (um->punt_unknown6)
-    {
-      vlib_cli_output (vm, "IPv6 UDP punt: enabled");
-    }
-  else
-    {
-      u8 *s = NULL;
-      vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
-      {
-	if (udp_is_valid_dst_port (port_info->dst_port, 01))
-	  {
-	    s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
-	  }
-      }
-      s = format (s, "%c", 0);
-      vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
-    }
-
-  return (error);
-}
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
-{
-  .path = "show udp punt",
-  .short_help = "show udp punt [ipv4|ipv6]",
-  .function = show_udp_punt_fn,
-};
-/* *INDENT-ON* */
-
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vnet/udp/udp.h b/src/vnet/udp/udp.h
index ddc9a0e..b8ddbc2 100644
--- a/src/vnet/udp/udp.h
+++ b/src/vnet/udp/udp.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Copyright (c) 2017-2020 Cisco and/or its affiliates.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
@@ -269,7 +269,6 @@
   return new_c;
 }
 
-
 always_inline udp_dst_port_info_t *
 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
 {
@@ -279,6 +278,7 @@
 
 format_function_t format_udp_header;
 format_function_t format_udp_rx_trace;
+format_function_t format_udp_connection;
 unformat_function_t unformat_udp_header;
 
 void udp_add_dst_port (udp_main_t * um, udp_dst_port_t dst_port,
@@ -289,7 +289,6 @@
 void udp_unregister_dst_port (vlib_main_t * vm,
 			      udp_dst_port_t dst_port, u8 is_ip4);
 bool udp_is_valid_dst_port (udp_dst_port_t dst_port, u8 is_ip4);
-
 void udp_connection_share_port (u16 lcl_port, u8 is_ip4);
 
 void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
diff --git a/src/vnet/udp/udp_cli.c b/src/vnet/udp/udp_cli.c
new file mode 100644
index 0000000..89cea68
--- /dev/null
+++ b/src/vnet/udp/udp_cli.c
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <vnet/udp/udp.h>
+
+u8 *
+format_udp_connection_id (u8 * s, va_list * args)
+{
+  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
+  if (!uc)
+    return s;
+  if (uc->c_is_ip4)
+    s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
+		uc->c_s_index, "U", format_ip4_address, &uc->c_lcl_ip4,
+		clib_net_to_host_u16 (uc->c_lcl_port), format_ip4_address,
+		&uc->c_rmt_ip4, clib_net_to_host_u16 (uc->c_rmt_port));
+  else
+    s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
+		uc->c_s_index, "U", format_ip6_address, &uc->c_lcl_ip6,
+		clib_net_to_host_u16 (uc->c_lcl_port), format_ip6_address,
+		&uc->c_rmt_ip6, clib_net_to_host_u16 (uc->c_rmt_port));
+  return s;
+}
+
+static const char *udp_connection_flags_str[] = {
+#define _(sym, str) str,
+  foreach_udp_connection_flag
+#undef _
+};
+
+static u8 *
+format_udp_connection_flags (u8 * s, va_list * args)
+{
+  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
+  int i, last = -1;
+
+  for (i = 0; i < UDP_CONN_N_FLAGS; i++)
+    if (uc->flags & (1 << i))
+      last = i;
+  for (i = 0; i < last; i++)
+    {
+      if (uc->flags & (1 << i))
+	s = format (s, "%s, ", udp_connection_flags_str[i]);
+    }
+  if (last >= 0)
+    s = format (s, "%s", udp_connection_flags_str[last]);
+  return s;
+}
+
+static u8 *
+format_udp_vars (u8 * s, va_list * args)
+{
+  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
+  s = format (s, " index %u flags: %U", uc->c_c_index,
+	      format_udp_connection_flags, uc);
+
+  if (!(uc->flags & UDP_CONN_F_LISTEN))
+    s = format (s, "\n");
+  return s;
+}
+
+u8 *
+format_udp_connection (u8 * s, va_list * args)
+{
+  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
+  u32 verbose = va_arg (*args, u32);
+  if (!uc)
+    return s;
+  s = format (s, "%-50U", format_udp_connection_id, uc);
+  if (verbose)
+    {
+      s = format (s, "%-15s",
+		  (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
+      if (verbose > 1)
+	s = format (s, "\n%U", format_udp_vars, uc);
+    }
+  return s;
+}
+
+static clib_error_t *
+udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
+{
+  udp_main_t *um = &udp_main;
+  u32 tmp;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "mtu %u", &tmp))
+	um->default_mtu = tmp;
+      else
+	return clib_error_return (0, "unknown input `%U'",
+				  format_unformat_error, input);
+    }
+  return 0;
+}
+
+VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
+
+static clib_error_t *
+show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
+		  vlib_cli_command_t * cmd_arg)
+{
+  udp_main_t *um = vnet_get_udp_main ();
+
+  clib_error_t *error = NULL;
+
+  if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    return clib_error_return (0, "unknown input `%U'", format_unformat_error,
+			      input);
+
+  udp_dst_port_info_t *port_info;
+  if (um->punt_unknown4)
+    {
+      vlib_cli_output (vm, "IPv4 UDP punt: enabled");
+    }
+  else
+    {
+      u8 *s = NULL;
+      vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
+      {
+	if (udp_is_valid_dst_port (port_info->dst_port, 1))
+	  {
+	    s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
+	  }
+      }
+      s = format (s, "%c", 0);
+      vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
+    }
+
+  if (um->punt_unknown6)
+    {
+      vlib_cli_output (vm, "IPv6 UDP punt: enabled");
+    }
+  else
+    {
+      u8 *s = NULL;
+      vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
+      {
+	if (udp_is_valid_dst_port (port_info->dst_port, 01))
+	  {
+	    s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
+	  }
+      }
+      s = format (s, "%c", 0);
+      vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
+    }
+
+  return (error);
+}
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
+{
+  .path = "show udp punt",
+  .short_help = "show udp punt [ipv4|ipv6]",
+  .function = show_udp_punt_fn,
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/udp/udp_input.c b/src/vnet/udp/udp_input.c
index 1e1c60b..1c55ee5 100644
--- a/src/vnet/udp/udp_input.c
+++ b/src/vnet/udp/udp_input.c
@@ -300,7 +300,6 @@
   return frame->n_vectors;
 }
 
-
 static uword
 udp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
 	    vlib_frame_t * frame)