vcl: avoid ip format function vnet dependency

Type: improvement

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I89103d4c99b21c5a466dbe511f0151bd754559a0
diff --git a/src/vcl/vcl_private.c b/src/vcl/vcl_private.c
index 3cbf025..d12d2d3 100644
--- a/src/vcl/vcl_private.c
+++ b/src/vcl/vcl_private.c
@@ -594,6 +594,126 @@
   return 0;
 }
 
+const char *
+vcl_session_state_str (vcl_session_state_t state)
+{
+  char *st;
+
+  switch (state)
+    {
+    case VCL_STATE_CLOSED:
+      st = "STATE_CLOSED";
+      break;
+    case VCL_STATE_LISTEN:
+      st = "STATE_LISTEN";
+      break;
+    case VCL_STATE_READY:
+      st = "STATE_READY";
+      break;
+    case VCL_STATE_VPP_CLOSING:
+      st = "STATE_VPP_CLOSING";
+      break;
+    case VCL_STATE_DISCONNECT:
+      st = "STATE_DISCONNECT";
+      break;
+    case VCL_STATE_DETACHED:
+      st = "STATE_DETACHED";
+      break;
+    case VCL_STATE_UPDATED:
+      st = "STATE_UPDATED";
+      break;
+    case VCL_STATE_LISTEN_NO_MQ:
+      st = "STATE_LISTEN_NO_MQ";
+      break;
+    default:
+      st = "UNKNOWN_STATE";
+      break;
+    }
+
+  return st;
+}
+
+u8 *
+vcl_format_ip4_address (u8 *s, va_list *args)
+{
+  u8 *a = va_arg (*args, u8 *);
+  return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
+}
+
+u8 *
+vcl_format_ip6_address (u8 *s, va_list *args)
+{
+  ip6_address_t *a = va_arg (*args, ip6_address_t *);
+  u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
+
+  i_max_n_zero = ARRAY_LEN (a->as_u16);
+  max_n_zeros = 0;
+  i_first_zero = i_max_n_zero;
+  n_zeros = 0;
+  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
+    {
+      u32 is_zero = a->as_u16[i] == 0;
+      if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
+	{
+	  i_first_zero = i;
+	  n_zeros = 0;
+	}
+      n_zeros += is_zero;
+      if ((!is_zero && n_zeros > max_n_zeros) ||
+	  (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
+	{
+	  i_max_n_zero = i_first_zero;
+	  max_n_zeros = n_zeros;
+	  i_first_zero = ARRAY_LEN (a->as_u16);
+	  n_zeros = 0;
+	}
+    }
+
+  last_double_colon = 0;
+  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
+    {
+      if (i == i_max_n_zero && max_n_zeros > 1)
+	{
+	  s = format (s, "::");
+	  i += max_n_zeros - 1;
+	  last_double_colon = 1;
+	}
+      else
+	{
+	  s = format (s, "%s%x", (last_double_colon || i == 0) ? "" : ":",
+		      clib_net_to_host_u16 (a->as_u16[i]));
+	  last_double_colon = 0;
+	}
+    }
+
+  return s;
+}
+
+/* Format an IP46 address. */
+u8 *
+vcl_format_ip46_address (u8 *s, va_list *args)
+{
+  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
+  ip46_type_t type = va_arg (*args, ip46_type_t);
+  int is_ip4 = 1;
+
+  switch (type)
+    {
+    case IP46_TYPE_ANY:
+      is_ip4 = ip46_address_is_ip4 (ip46);
+      break;
+    case IP46_TYPE_IP4:
+      is_ip4 = 1;
+      break;
+    case IP46_TYPE_IP6:
+      is_ip4 = 0;
+      break;
+    }
+
+  return is_ip4 ? format (s, "%U", vcl_format_ip4_address, &ip46->ip4) :
+		  format (s, "%U", vcl_format_ip6_address, &ip46->ip6);
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *