tunnel: Common types for IP tunnels

Type: refactor

Signed-off-by: Neale Ranns <nranns@cisco.com>
Change-Id: I18dcdb7af3e327f6cacdbcb1e52b89f13d6ba6e2
diff --git a/src/vnet/gre/gre.api b/src/vnet/gre/gre.api
index d79beeb..0a98178 100644
--- a/src/vnet/gre/gre.api
+++ b/src/vnet/gre/gre.api
@@ -14,9 +14,10 @@
  * limitations under the License.
  */
 
-option version = "2.0.1";
+option version = "2.0.2";
 
 import "vnet/interface_types.api";
+import "vnet/tunnel/tunnel_types.api";
 import "vnet/ip/ip_types.api";
 
 /** \brief A GRE tunnel type
@@ -28,16 +29,6 @@
   GRE_API_TUNNEL_TYPE_ERSPAN,
 };
 
-/** \brief A GRE tunnel mode
-*/
-enum gre_tunnel_mode : u8
-{
-  /* point-to-point */
-  GRE_API_TUNNEL_MODE_P2P,
-  /* multi-point */
-  GRE_API_TUNNEL_MODE_MP,
-};
-
 /** \brief A GRE tunnel
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request
@@ -52,7 +43,7 @@
 typedef gre_tunnel
 {
   vl_api_gre_tunnel_type_t type;
-  vl_api_gre_tunnel_mode_t mode;
+  vl_api_tunnel_mode_t mode;
   u16 session_id;
   u32 instance;
   u32 outer_table_id;
diff --git a/src/vnet/gre/gre.h b/src/vnet/gre/gre.h
index 6989cf4..7f6ff0b 100644
--- a/src/vnet/gre/gre.h
+++ b/src/vnet/gre/gre.h
@@ -24,6 +24,7 @@
 #include <vnet/pg/pg.h>
 #include <vnet/ip/format.h>
 #include <vnet/adj/adj_types.h>
+#include <vnet/tunnel/tunnel.h>
 
 extern vnet_hw_interface_class_t gre_hw_interface_class;
 extern vnet_hw_interface_class_t mgre_hw_interface_class;
@@ -61,18 +62,6 @@
 
 extern u8 *format_gre_tunnel_type (u8 * s, va_list * args);
 
-#define foreach_gre_tunnel_mode \
-  _(P2P, "point-to-point")      \
-  _(MP, "multi-point")          \
-
-typedef enum gre_tunnel_mode_t_
-{
-#define _(n, s) GRE_TUNNEL_MODE_##n,
-  foreach_gre_tunnel_mode
-#undef _
-} __clib_packed gre_tunnel_mode_t;
-
-extern u8 *format_gre_tunnel_mode (u8 * s, va_list * args);
 
 /**
  * A GRE payload protocol registration
@@ -107,7 +96,7 @@
       u32 fib_index;
       u16 session_id;
       gre_tunnel_type_t type;
-      gre_tunnel_mode_t mode;
+      tunnel_mode_t mode;
     };
     u64 as_u64;
   };
@@ -215,7 +204,7 @@
   u32 hw_if_index;
   u32 sw_if_index;
   gre_tunnel_type_t type;
-  gre_tunnel_mode_t mode;
+  tunnel_mode_t mode;
 
   /**
    * an L2 tunnel always rquires an L2 midchain. cache here for DP.
@@ -363,7 +352,7 @@
 {
   u8 is_add;
   gre_tunnel_type_t type;
-  gre_tunnel_mode_t mode;
+  tunnel_mode_t mode;
   u8 is_ipv6;
   u32 instance;
   ip46_address_t src, dst;
@@ -379,7 +368,7 @@
 	     ip4_address_t dst,
 	     u32 fib_index,
 	     gre_tunnel_type_t ttype,
-	     gre_tunnel_mode_t tmode, u16 session_id, gre_tunnel_key4_t * key)
+	     tunnel_mode_t tmode, u16 session_id, gre_tunnel_key4_t * key)
 {
   key->gtk_src = src;
   key->gtk_dst = dst;
@@ -402,7 +391,7 @@
 	     const ip6_address_t * dst,
 	     u32 fib_index,
 	     gre_tunnel_type_t ttype,
-	     gre_tunnel_mode_t tmode, u16 session_id, gre_tunnel_key6_t * key)
+	     tunnel_mode_t tmode, u16 session_id, gre_tunnel_key6_t * key)
 {
   key->gtk_src = *src;
   key->gtk_dst = *dst;
diff --git a/src/vnet/gre/gre_api.c b/src/vnet/gre/gre_api.c
index be91f75..619b5fc 100644
--- a/src/vnet/gre/gre_api.c
+++ b/src/vnet/gre/gre_api.c
@@ -25,6 +25,7 @@
 
 #include <vnet/gre/gre.h>
 #include <vnet/fib/fib_table.h>
+#include <vnet/tunnel/tunnel_types_api.h>
 #include <vnet/ip/ip_types_api.h>
 
 #include <vnet/vnet_msg_enum.h>
@@ -83,40 +84,6 @@
   return (out);
 }
 
-static int
-gre_tunnel_mode_decode (vl_api_gre_tunnel_mode_t in, gre_tunnel_mode_t * out)
-{
-  switch (in)
-    {
-#define _(n, v)                                           \
-      case GRE_API_TUNNEL_MODE_##n:                       \
-        *out = GRE_TUNNEL_MODE_##n;                       \
-        return (0);
-      foreach_gre_tunnel_mode
-#undef _
-    }
-
-  return (VNET_API_ERROR_INVALID_VALUE_2);
-}
-
-static vl_api_gre_tunnel_mode_t
-gre_tunnel_mode_encode (gre_tunnel_mode_t in)
-{
-  vl_api_gre_tunnel_mode_t out = GRE_API_TUNNEL_MODE_P2P;
-
-  switch (in)
-    {
-#define _(n, v)                                           \
-      case GRE_TUNNEL_MODE_##n:                           \
-        out = GRE_API_TUNNEL_MODE_##n;                    \
-        break;
-      foreach_gre_tunnel_mode
-#undef _
-    }
-
-  return (out);
-}
-
 static void vl_api_gre_tunnel_add_del_t_handler
   (vl_api_gre_tunnel_add_del_t * mp)
 {
@@ -146,7 +113,7 @@
   if (rv)
     goto out;
 
-  rv = gre_tunnel_mode_decode (mp->tunnel.mode, &a->mode);
+  rv = tunnel_mode_decode (mp->tunnel.mode, &a->mode);
 
   if (rv)
     goto out;
@@ -185,7 +152,7 @@
 	   (t->outer_fib_index, t->tunnel_dst.fp_proto));
 
   rmp->tunnel.type = gre_tunnel_type_encode (t->type);
-  rmp->tunnel.mode = gre_tunnel_mode_encode (t->mode);
+  rmp->tunnel.mode = tunnel_mode_encode (t->mode);
   rmp->tunnel.instance = htonl (t->user_instance);
   rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
   rmp->tunnel.session_id = htons (t->session_id);
diff --git a/src/vnet/gre/interface.c b/src/vnet/gre/interface.c
index fab12b4..59bf21d 100644
--- a/src/vnet/gre/interface.c
+++ b/src/vnet/gre/interface.c
@@ -44,23 +44,6 @@
   return (s);
 }
 
-u8 *
-format_gre_tunnel_mode (u8 * s, va_list * args)
-{
-  gre_tunnel_mode_t mode = va_arg (*args, int);
-
-  switch (mode)
-    {
-#define _(n, v) case GRE_TUNNEL_MODE_##n:       \
-      s = format (s, "%s", v);                  \
-      break;
-      foreach_gre_tunnel_mode
-#undef _
-    }
-
-  return (s);
-}
-
 static u8 *
 format_gre_tunnel (u8 * s, va_list * args)
 {
@@ -73,7 +56,7 @@
 	      t->outer_fib_index, t->sw_if_index);
 
   s = format (s, "payload %U ", format_gre_tunnel_type, t->type);
-  s = format (s, "%U ", format_gre_tunnel_mode, t->mode);
+  s = format (s, "%U ", format_tunnel_mode, t->mode);
 
   if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
     s = format (s, "session %d ", t->session_id);
@@ -212,12 +195,12 @@
     gre_mk_key4 (t->tunnel_src.ip4,
 		 nh->fp_addr.ip4,
 		 nhrp_entry_get_fib_index (ne),
-		 t->type, GRE_TUNNEL_MODE_P2P, 0, &key->gtk_v4);
+		 t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v4);
   else
     gre_mk_key6 (&t->tunnel_src.ip6,
 		 &nh->fp_addr.ip6,
 		 nhrp_entry_get_fib_index (ne),
-		 t->type, GRE_TUNNEL_MODE_P2P, 0, &key->gtk_v6);
+		 t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v6);
 }
 
 static void
@@ -336,7 +319,7 @@
 
   if (t->type == GRE_TUNNEL_TYPE_L3)
     {
-      if (t->mode == GRE_TUNNEL_MODE_P2P)
+      if (t->mode == TUNNEL_MODE_P2P)
 	hw_if_index =
 	  vnet_register_interface (vnm, gre_device_class.index, t_idx,
 				   gre_hw_interface_class.index, t_idx);
@@ -404,7 +387,7 @@
 
   gre_tunnel_db_add (t, &key);
 
-  if (t->mode == GRE_TUNNEL_MODE_MP)
+  if (t->mode == TUNNEL_MODE_MP)
     nhrp_walk_itf (t->sw_if_index, gre_tunnel_add_nhrp_walk, t);
 
   if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
@@ -460,7 +443,7 @@
   if (NULL == t)
     return VNET_API_ERROR_NO_SUCH_ENTRY;
 
-  if (t->mode == GRE_TUNNEL_MODE_MP)
+  if (t->mode == TUNNEL_MODE_MP)
     nhrp_walk_itf (t->sw_if_index, gre_tunnel_delete_nhrp_walk, t);
 
   sw_if_index = t->sw_if_index;
@@ -518,7 +501,7 @@
   if (a->session_id > GTK_SESSION_ID_MAX)
     return VNET_API_ERROR_INVALID_SESSION_ID;
 
-  if (a->mode == GRE_TUNNEL_MODE_MP && !ip46_address_is_zero (&a->dst))
+  if (a->mode == TUNNEL_MODE_MP && !ip46_address_is_zero (&a->dst))
     return (VNET_API_ERROR_INVALID_DST_ADDRESS);
 
   if (a->is_add)
@@ -572,7 +555,7 @@
   u32 instance = ~0;
   u32 outer_table_id = 0;
   gre_tunnel_type_t t_type = GRE_TUNNEL_TYPE_L3;
-  gre_tunnel_mode_t t_mode = GRE_TUNNEL_MODE_P2P;
+  tunnel_mode_t t_mode = TUNNEL_MODE_P2P;
   u32 session_id = 0;
   int rv;
   u8 is_add = 1;
@@ -596,7 +579,7 @@
       else if (unformat (line_input, "outer-table-id %d", &outer_table_id))
 	;
       else if (unformat (line_input, "multipoint"))
-	t_mode = GRE_TUNNEL_MODE_MP;
+	t_mode = TUNNEL_MODE_MP;
       else if (unformat (line_input, "teb"))
 	t_type = GRE_TUNNEL_TYPE_TEB;
       else if (unformat (line_input, "erspan %d", &session_id))
@@ -615,7 +598,7 @@
       goto done;
     }
 
-  if (t_mode != GRE_TUNNEL_MODE_MP && ip46_address_is_zero (&dst))
+  if (t_mode != TUNNEL_MODE_MP && ip46_address_is_zero (&dst))
     {
       error = clib_error_return (0, "destination address not specified");
       goto done;
diff --git a/src/vnet/gre/node.c b/src/vnet/gre/node.c
index 14fb087..9252306 100644
--- a/src/vnet/gre/node.c
+++ b/src/vnet/gre/node.c
@@ -245,11 +245,11 @@
 	  gre_mk_key6 (&ip6[0]->dst_address,
 		       &ip6[0]->src_address,
 		       vnet_buffer (b[0])->ip.fib_index,
-		       type[0], GRE_TUNNEL_MODE_P2P, 0, &key[0].gtk_v6);
+		       type[0], TUNNEL_MODE_P2P, 0, &key[0].gtk_v6);
 	  gre_mk_key6 (&ip6[1]->dst_address,
 		       &ip6[1]->src_address,
 		       vnet_buffer (b[1])->ip.fib_index,
-		       type[1], GRE_TUNNEL_MODE_P2P, 0, &key[1].gtk_v6);
+		       type[1], TUNNEL_MODE_P2P, 0, &key[1].gtk_v6);
 	  matched[0] = gre_match_key6 (&cached_key.gtk_v6, &key[0].gtk_v6);
 	  matched[1] = gre_match_key6 (&cached_key.gtk_v6, &key[1].gtk_v6);
 	}
@@ -258,11 +258,11 @@
 	  gre_mk_key4 (ip4[0]->dst_address,
 		       ip4[0]->src_address,
 		       vnet_buffer (b[0])->ip.fib_index,
-		       type[0], GRE_TUNNEL_MODE_P2P, 0, &key[0].gtk_v4);
+		       type[0], TUNNEL_MODE_P2P, 0, &key[0].gtk_v4);
 	  gre_mk_key4 (ip4[1]->dst_address,
 		       ip4[1]->src_address,
 		       vnet_buffer (b[1])->ip.fib_index,
-		       type[1], GRE_TUNNEL_MODE_P2P, 0, &key[1].gtk_v4);
+		       type[1], TUNNEL_MODE_P2P, 0, &key[1].gtk_v4);
 	  matched[0] = gre_match_key4 (&cached_key.gtk_v4, &key[0].gtk_v4);
 	  matched[1] = gre_match_key4 (&cached_key.gtk_v4, &key[1].gtk_v4);
 	}
@@ -381,7 +381,7 @@
 	  gre_mk_key6 (&ip6[0]->dst_address,
 		       &ip6[0]->src_address,
 		       vnet_buffer (b[0])->ip.fib_index,
-		       type[0], GRE_TUNNEL_MODE_P2P, 0, &key[0].gtk_v6);
+		       type[0], TUNNEL_MODE_P2P, 0, &key[0].gtk_v6);
 	  matched[0] = gre_match_key6 (&cached_key.gtk_v6, &key[0].gtk_v6);
 	}
       else
@@ -389,7 +389,7 @@
 	  gre_mk_key4 (ip4[0]->dst_address,
 		       ip4[0]->src_address,
 		       vnet_buffer (b[0])->ip.fib_index,
-		       type[0], GRE_TUNNEL_MODE_P2P, 0, &key[0].gtk_v4);
+		       type[0], TUNNEL_MODE_P2P, 0, &key[0].gtk_v4);
 	  matched[0] = gre_match_key4 (&cached_key.gtk_v4, &key[0].gtk_v4);
 	}