VXLAN-GBP: use common types on the API

Change-Id: I11ec0d7048d36c30a97d437e5b0abd05f06ab0eb
Signed-off-by: Neale Ranns <nranns@cisco.com>
diff --git a/src/vnet/vxlan-gbp/vxlan_gbp.api b/src/vnet/vxlan-gbp/vxlan_gbp.api
index ecb5ba2..6e41ec8 100644
--- a/src/vnet/vxlan-gbp/vxlan_gbp.api
+++ b/src/vnet/vxlan-gbp/vxlan_gbp.api
@@ -1,3 +1,4 @@
+/* Hey Emacs use -*- mode: C -*- */
 /*
  * Copyright (c) 2018 Cisco and/or its affiliates.
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,39 +14,45 @@
  * limitations under the License.
  */
 
-option version = "1.0.0";
+option version = "1.1.0";
+import "vnet/ip/ip_types.api";
+
+/** \brief Definition of a VXLAN GBP tunnel
+    @param instance - optional unique custom device instance, else ~0.
+    @param src - Source IP address
+    @param dst - Destination IP address, can be multicast
+    @param mcast_sw_if_index - Interface for multicast destination
+    @param encap_table_id - Encap route table 
+    @param decap_next_index - Name of decap next graph node
+    @param vni - The VXLAN Network Identifier, uint24
+    @param sw_ifindex - Ignored in add message, set in details
+*/
+typedef vxlan_gbp_tunnel
+{
+  u32 instance;
+  vl_api_address_t src;
+  vl_api_address_t dst;
+  u32 mcast_sw_if_index;
+  u32 encap_table_id;
+  u32 decap_next_index;
+  u32 vni;
+  u32 sw_if_index;
+};
 
 /** \brief Create or delete a VXLAN-GBP tunnel
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request
     @param is_add - Use 1 to create the tunnel, 0 to remove it
-    @param is_ipv6 - Use 0 for IPv4, 1 for IPv6
-    @param instance - optional unique custom device instance, else ~0.
-    @param src_address - Source IP address
-    @param dst_address - Destination IP address, can be multicast
-    @param mcast_sw_if_index - Interface for multicast destination
-    @param encap_vrf_id - Encap route table FIB index
-    @param decap_next_index - Name of decap next graph node
-    @param vni - The VXLAN Network Identifier, uint24
-    @param sclass - The policy group id
 */
-define vxlan_gbp_add_del_tunnel
+define vxlan_gbp_tunnel_add_del
 {
   u32 client_index;
   u32 context;
   u8 is_add;
-  u8 is_ipv6;
-  u32 instance;		/* If non-~0, specifies a custom dev instance */
-  u8 src_address[16];
-  u8 dst_address[16];
-  u32 mcast_sw_if_index;
-  u32 encap_vrf_id;
-  u32 decap_next_index;
-  u32 vni;
-  u16 sclass;
+  vl_api_vxlan_gbp_tunnel_t tunnel;
 };
 
-define vxlan_gbp_add_del_tunnel_reply
+define vxlan_gbp_tunnel_add_del_reply
 {
   u32 context;
   i32 retval;
@@ -62,16 +69,7 @@
 define vxlan_gbp_tunnel_details
 {
   u32 context;
-  u32 sw_if_index;
-  u32 instance;
-  u8 src_address[16];
-  u8 dst_address[16];
-  u32 mcast_sw_if_index;
-  u32 encap_vrf_id;
-  u32 decap_next_index;
-  u32 vni;
-  u16 sclass;
-  u8 is_ipv6;
+  vl_api_vxlan_gbp_tunnel_t tunnel;
 };
 
 /** \brief Interface set vxlan-bypass request
diff --git a/src/vnet/vxlan-gbp/vxlan_gbp.c b/src/vnet/vxlan-gbp/vxlan_gbp.c
index 14b3e66..253261d 100644
--- a/src/vnet/vxlan-gbp/vxlan_gbp.c
+++ b/src/vnet/vxlan-gbp/vxlan_gbp.c
@@ -352,8 +352,8 @@
   hash_unset_mem_free (&vxlan_gbp_main.mcast_shared, dst);
 }
 
-int vnet_vxlan_gbp_add_del_tunnel
-  (vnet_vxlan_gbp_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
+int vnet_vxlan_gbp_tunnel_add_del
+  (vnet_vxlan_gbp_tunnel_add_del_args_t * a, u32 * sw_if_indexp)
 {
   vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
   vxlan_gbp_tunnel_t *t = 0;
@@ -654,7 +654,7 @@
 }
 
 static clib_error_t *
-vxlan_gbp_add_del_tunnel_command_fn (vlib_main_t * vm,
+vxlan_gbp_tunnel_add_del_command_fn (vlib_main_t * vm,
 				     unformat_input_t * input,
 				     vlib_cli_command_t * cmd)
 {
@@ -763,7 +763,7 @@
   if (vni >> 24)
     return clib_error_return (0, "vni %d out of range", vni);
 
-  vnet_vxlan_gbp_add_del_tunnel_args_t a = {
+  vnet_vxlan_gbp_tunnel_add_del_args_t a = {
     .is_add = is_add,
     .is_ip6 = ipv6_set,
     .instance = instance,
@@ -773,7 +773,7 @@
   };
 
   u32 tunnel_sw_if_index;
-  int rv = vnet_vxlan_gbp_add_del_tunnel (&a, &tunnel_sw_if_index);
+  int rv = vnet_vxlan_gbp_tunnel_add_del (&a, &tunnel_sw_if_index);
 
   switch (rv)
     {
@@ -794,7 +794,7 @@
 
     default:
       return clib_error_return
-	(0, "vnet_vxlan_gbp_add_del_tunnel returned %d", rv);
+	(0, "vnet_vxlan_gbp_tunnel_add_del returned %d", rv);
     }
 
   return 0;
@@ -832,7 +832,7 @@
   " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
   " [instance <id>]"
   " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
-  .function = vxlan_gbp_add_del_tunnel_command_fn,
+  .function = vxlan_gbp_tunnel_add_del_command_fn,
 };
 /* *INDENT-ON* */
 
diff --git a/src/vnet/vxlan-gbp/vxlan_gbp.h b/src/vnet/vxlan-gbp/vxlan_gbp.h
index bc0ccf3..472866e 100644
--- a/src/vnet/vxlan-gbp/vxlan_gbp.h
+++ b/src/vnet/vxlan-gbp/vxlan_gbp.h
@@ -185,15 +185,14 @@
   u8 is_ip6;
   u32 instance;
   ip46_address_t src, dst;
-  u16 sclass;
   u32 mcast_sw_if_index;
   u32 encap_fib_index;
   u32 decap_next_index;
   u32 vni;
-} vnet_vxlan_gbp_add_del_tunnel_args_t;
+} vnet_vxlan_gbp_tunnel_add_del_args_t;
 
-int vnet_vxlan_gbp_add_del_tunnel
-  (vnet_vxlan_gbp_add_del_tunnel_args_t * a, u32 * sw_if_indexp);
+int vnet_vxlan_gbp_tunnel_add_del
+  (vnet_vxlan_gbp_tunnel_add_del_args_t * a, u32 * sw_if_indexp);
 
 void vnet_int_vxlan_gbp_bypass_mode (u32 sw_if_index, u8 is_ip6,
 				     u8 is_enable);
diff --git a/src/vnet/vxlan-gbp/vxlan_gbp_api.c b/src/vnet/vxlan-gbp/vxlan_gbp_api.c
index b064a1f..1bba74d 100644
--- a/src/vnet/vxlan-gbp/vxlan_gbp_api.c
+++ b/src/vnet/vxlan-gbp/vxlan_gbp_api.c
@@ -25,6 +25,7 @@
 #include <vnet/feature/feature.h>
 #include <vnet/vxlan-gbp/vxlan_gbp.h>
 #include <vnet/fib/fib_table.h>
+#include <vnet/ip/ip_types_api.h>
 
 #include <vnet/vnet_msg_enum.h>
 
@@ -46,7 +47,7 @@
 
 #define foreach_vpe_api_msg                             \
 _(SW_INTERFACE_SET_VXLAN_GBP_BYPASS, sw_interface_set_vxlan_gbp_bypass)         \
-_(VXLAN_GBP_ADD_DEL_TUNNEL, vxlan_gbp_add_del_tunnel)                           \
+_(VXLAN_GBP_TUNNEL_ADD_DEL, vxlan_gbp_tunnel_add_del)                           \
 _(VXLAN_GBP_TUNNEL_DUMP, vxlan_gbp_tunnel_dump)
 
 static void
@@ -65,32 +66,36 @@
   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_GBP_BYPASS_REPLY);
 }
 
-static void vl_api_vxlan_gbp_add_del_tunnel_t_handler
-  (vl_api_vxlan_gbp_add_del_tunnel_t * mp)
+static void vl_api_vxlan_gbp_tunnel_add_del_t_handler
+  (vl_api_vxlan_gbp_tunnel_add_del_t * mp)
 {
-  vl_api_vxlan_gbp_add_del_tunnel_reply_t *rmp;
+  vl_api_vxlan_gbp_tunnel_add_del_reply_t *rmp;
+  ip46_address_t src, dst;
+  ip46_type_t itype;
   int rv = 0;
   u32 fib_index;
 
-  fib_index = fib_table_find (fib_ip_proto (mp->is_ipv6),
-			      ntohl (mp->encap_vrf_id));
+  itype = ip_address_decode (&mp->tunnel.src, &src);
+  itype = ip_address_decode (&mp->tunnel.dst, &dst);
+
+  fib_index = fib_table_find (fib_proto_from_ip46 (itype),
+			      ntohl (mp->tunnel.encap_table_id));
   if (fib_index == ~0)
     {
       rv = VNET_API_ERROR_NO_SUCH_FIB;
       goto out;
     }
 
-  vnet_vxlan_gbp_add_del_tunnel_args_t a = {
+  vnet_vxlan_gbp_tunnel_add_del_args_t a = {
     .is_add = mp->is_add,
-    .is_ip6 = mp->is_ipv6,
-    .instance = ntohl (mp->instance),
-    .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
+    .is_ip6 = (itype == IP46_TYPE_IP6),
+    .instance = ntohl (mp->tunnel.instance),
+    .mcast_sw_if_index = ntohl (mp->tunnel.mcast_sw_if_index),
     .encap_fib_index = fib_index,
-    .decap_next_index = ntohl (mp->decap_next_index),
-    .vni = ntohl (mp->vni),
-    .sclass = ntohs (mp->sclass),
-    .dst = to_ip46 (mp->is_ipv6, mp->dst_address),
-    .src = to_ip46 (mp->is_ipv6, mp->src_address),
+    .decap_next_index = ntohl (mp->tunnel.decap_next_index),
+    .vni = ntohl (mp->tunnel.vni),
+    .dst = dst,
+    .src = src,
   };
 
   /* Check src & dst are different */
@@ -107,11 +112,11 @@
     }
 
   u32 sw_if_index = ~0;
-  rv = vnet_vxlan_gbp_add_del_tunnel (&a, &sw_if_index);
+  rv = vnet_vxlan_gbp_tunnel_add_del (&a, &sw_if_index);
 
 out:
   /* *INDENT-OFF* */
-  REPLY_MACRO2(VL_API_VXLAN_GBP_ADD_DEL_TUNNEL_REPLY,
+  REPLY_MACRO2(VL_API_VXLAN_GBP_TUNNEL_ADD_DEL_REPLY,
   ({
     rmp->sw_if_index = ntohl (sw_if_index);
   }));
@@ -122,33 +127,23 @@
   (vxlan_gbp_tunnel_t * t, vl_api_registration_t * reg, u32 context)
 {
   vl_api_vxlan_gbp_tunnel_details_t *rmp;
-  ip4_main_t *im4 = &ip4_main;
-  ip6_main_t *im6 = &ip6_main;
-  u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
+  ip46_type_t itype = (ip46_address_is_ip4 (&t->dst) ?
+		       IP46_TYPE_IP4 : IP46_TYPE_IP6);
 
   rmp = vl_msg_api_alloc (sizeof (*rmp));
   memset (rmp, 0, sizeof (*rmp));
   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_GBP_TUNNEL_DETAILS);
-  if (is_ipv6)
-    {
-      memcpy (rmp->src_address, t->src.ip6.as_u8, 16);
-      memcpy (rmp->dst_address, t->dst.ip6.as_u8, 16);
-      rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
-    }
-  else
-    {
-      memcpy (rmp->src_address, t->src.ip4.as_u8, 4);
-      memcpy (rmp->dst_address, t->dst.ip4.as_u8, 4);
-      rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
-    }
 
-  rmp->instance = htonl (t->user_instance);
-  rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
-  rmp->vni = htonl (t->vni);
-  rmp->sclass = htons (t->sclass);
-  rmp->decap_next_index = htonl (t->decap_next_index);
-  rmp->sw_if_index = htonl (t->sw_if_index);
-  rmp->is_ipv6 = is_ipv6;
+  ip_address_encode (&t->src, itype, &rmp->tunnel.src);
+  ip_address_encode (&t->dst, itype, &rmp->tunnel.dst);
+  rmp->tunnel.encap_table_id =
+    fib_table_get_table_id (t->encap_fib_index, fib_proto_from_ip46 (itype));
+
+  rmp->tunnel.instance = htonl (t->user_instance);
+  rmp->tunnel.mcast_sw_if_index = htonl (t->mcast_sw_if_index);
+  rmp->tunnel.vni = htonl (t->vni);
+  rmp->tunnel.decap_next_index = htonl (t->decap_next_index);
+  rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
   rmp->context = context;
 
   vl_api_send_msg (reg, (u8 *) rmp);
diff --git a/test/test_vxlan_gbp.py b/test/test_vxlan_gbp.py
index 55594a5..919ecbc 100644
--- a/test/test_vxlan_gbp.py
+++ b/test/test_vxlan_gbp.py
@@ -1,10 +1,11 @@
 #!/usr/bin/env python
 
 import socket
-from util import ip4n_range, ip4_range
+from util import ip4_range
 import unittest
 from framework import VppTestCase, VppTestRunner
 from template_bd import BridgeDomain
+from vpp_ip import VppIpAddress
 
 from scapy.layers.l2 import Ether, Raw
 from scapy.layers.inet import IP, UDP
@@ -106,13 +107,15 @@
         ip_range_start = 10
         ip_range_end = ip_range_start + n_ucast_tunnels
         next_hop_address = cls.pg0.remote_ip4n
-        for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
-                                    ip_range_end):
+        for dest_ip4 in ip4_range(cls.pg0.remote_ip4,
+                                  ip_range_start,
+                                  ip_range_end):
             # add host route so dest_ip4n will not be resolved
-            cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
-            r = cls.vapi.vxlan_gbp_add_del_tunnel(
-                src_addr=cls.pg0.local_ip4n,
-                dst_addr=dest_ip4n,
+            vip = VppIpAddress(dest_ip4)
+            cls.vapi.ip_add_del_route(vip.bytes, 32, next_hop_address)
+            r = cls.vapi.vxlan_gbp_tunnel_add_del(
+                VppIpAddress(cls.pg0.local_ip4).encode(),
+                vip.encode(),
                 vni=vni)
             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
 
@@ -145,9 +148,9 @@
             # Create VXLAN GBP VTEP on VPP pg0, and put vxlan_gbp_tunnel0 and
             # pg1 into BD.
             cls.single_tunnel_bd = 1
-            r = cls.vapi.vxlan_gbp_add_del_tunnel(
-                src_addr=cls.pg0.local_ip4n,
-                dst_addr=cls.pg0.remote_ip4n,
+            r = cls.vapi.vxlan_gbp_tunnel_add_del(
+                VppIpAddress(cls.pg0.local_ip4).encode(),
+                VppIpAddress(cls.pg0.remote_ip4).encode(),
                 vni=cls.single_tunnel_bd)
             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
                                                 bd_id=cls.single_tunnel_bd)
diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py
index 3028a25..13d0d93 100644
--- a/test/vpp_papi_provider.py
+++ b/test/vpp_papi_provider.py
@@ -2723,14 +2723,14 @@
                          'protocol': protocol,
                          'vni': vni})
 
-    def vxlan_gbp_add_del_tunnel(
+    def vxlan_gbp_tunnel_add_del(
             self,
-            src_addr,
-            dst_addr,
+            src,
+            dst,
             mcast_sw_if_index=0xFFFFFFFF,
             is_add=1,
             is_ipv6=0,
-            encap_vrf_id=0,
+            encap_table_id=0,
             decap_next_index=0xFFFFFFFF,
             vni=0,
             instance=0xFFFFFFFF):
@@ -2740,23 +2740,27 @@
         :param src_addr:
         :param is_add:  (Default value = 1)
         :param is_ipv6:  (Default value = 0)
-        :param encap_vrf_id:  (Default value = 0)
+        :param encap_table_id:  (Default value = 0)
         :param decap_next_index:  (Default value = 0xFFFFFFFF)
         :param mcast_sw_if_index:  (Default value = 0xFFFFFFFF)
         :param vni:  (Default value = 0)
         :param instance:  (Default value = 0xFFFFFFFF)
 
         """
-        return self.api(self.papi.vxlan_gbp_add_del_tunnel,
+        return self.api(self.papi.vxlan_gbp_tunnel_add_del,
                         {'is_add': is_add,
-                         'is_ipv6': is_ipv6,
-                         'src_address': src_addr,
-                         'dst_address': dst_addr,
-                         'mcast_sw_if_index': mcast_sw_if_index,
-                         'encap_vrf_id': encap_vrf_id,
-                         'decap_next_index': decap_next_index,
-                         'vni': vni,
-                         'instance': instance})
+                         'tunnel': {
+                             'src': src,
+                             'dst': dst,
+                             'mcast_sw_if_index': mcast_sw_if_index,
+                             'encap_table_id': encap_table_id,
+                             'decap_next_index': decap_next_index,
+                             'vni': vni,
+                             'instance': instance}})
+
+    def vxlan_gbp_tunnel_dump(self, sw_if_index=0xffffffff):
+        return self.api(self.papi.vxlan_gbp_tunnel_dump,
+                        {'sw_if_index': sw_if_index})
 
     def pppoe_add_del_session(
             self,