VPP-25 Add API for GRE tunnel create/delete/show. 21/921/4
authorChris Luke <chrisy@flirble.org>
Thu, 28 Apr 2016 17:44:38 +0000 (13:44 -0400)
committerDave Barach <openvpp@barachs.net>
Fri, 29 Apr 2016 14:32:58 +0000 (14:32 +0000)
Add API methods to create, delete and show GRE tunnels.

Also add missing CLI functionality for deleting and listing
configured tunnels.

Change-Id: I7565966037d94ade07938e4ff0d9333419716857
Signed-off-by: Chris Luke <chrisy@flirble.org>
vnet/vnet/gre/gre.h
vnet/vnet/gre/interface.c
vpp-api-test/vat/api_format.c
vpp/api/api.c
vpp/api/custom_dump.c
vpp/api/vpe.api

index de9db32..6cbe4aa 100644 (file)
@@ -54,6 +54,7 @@ typedef struct {
   ip4_address_t tunnel_dst;
   u32 outer_fib_index;
   u32 hw_if_index;
+  u32 sw_if_index;
 } gre_tunnel_t;
 
 typedef struct {
@@ -67,6 +68,12 @@ typedef struct {
   /* Hash mapping src/dst addr pair to tunnel */
   uword * tunnel_by_key;
 
+  /* Free vlib hw_if_indices */
+  u32 * free_vxlan_tunnel_hw_if_indices;
+
+  /* Mapping from sw_if_index to tunnel index */
+  u32 * tunnel_index_by_sw_if_index;
+
   /* convenience */
   vlib_main_t * vlib_main;
   vnet_main_t * vnet_main;
@@ -115,4 +122,14 @@ gre_register_input_protocol (vlib_main_t * vm,
 /* manually added to the interface output node in gre.c */
 #define GRE_OUTPUT_NEXT_LOOKUP 1
 
+typedef struct {
+  u8 is_add;
+
+  ip4_address_t src, dst;
+  u32 outer_table_id;
+} vnet_gre_add_del_tunnel_args_t;
+
+int vnet_gre_add_del_tunnel
+  (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp);
+
 #endif /* included_gre_h */
index 69c1583..c1b9ddd 100644 (file)
 #include <vnet/vnet.h>
 #include <vnet/pg/pg.h>
 #include <vnet/gre/gre.h>
+#include <vnet/ip/format.h>
 
-int
-gre_register_interface (vnet_main_t * vnm,
-                        u32 dev_class_index,
-                        ip4_address_t *tunnel_src,
-                        ip4_address_t *tunnel_dst,
-                        u32 outer_fib_id,
-                        u32 * gi_index_return)
+u8 * format_gre_tunnel (u8 * s, va_list * args)
 {
+  gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
   gre_main_t * gm = &gre_main;
+
+  s = format (s,
+              "[%d] %U (src) %U (dst) outer_fib_index %d",
+              t - gm->tunnels,
+              format_ip4_address, &t->tunnel_src,
+              format_ip4_address, &t->tunnel_dst,
+              t->outer_fib_index);
+  return s;
+}
+
+int vnet_gre_add_del_tunnel
+  (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
+{
+  gre_main_t * gm = &gre_main;
+  vnet_main_t * vnm = gm->vnet_main;
   ip4_main_t * im = &ip4_main;
   gre_tunnel_t * t;
   vnet_hw_interface_t * hi;
-  u32 hw_if_index;
+  u32 hw_if_index, sw_if_index;
   u32 slot;
   u32 outer_fib_index;
   uword * p;
+  u64 key;
+
+  key = (u64)a->src.as_u32 << 32 | (u64)a->dst.as_u32;
+  p = hash_get (gm->tunnel_by_key, key);
+
+  if (a->is_add) {
+    /* check if same src/dst pair exists */
+    if (p)
+      return VNET_API_ERROR_INVALID_VALUE;
+
+    p = hash_get (im->fib_index_by_table_id, a->outer_table_id);
+    if (! p)
+      return VNET_API_ERROR_NO_SUCH_FIB;
+
+    outer_fib_index = p[0];
+
+    pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
+    memset (t, 0, sizeof (*t));
+
+    if (vec_len (gm->free_vxlan_tunnel_hw_if_indices) > 0) {
+        vnet_interface_main_t * im = &vnm->interface_main;
+
+        hw_if_index = gm->free_vxlan_tunnel_hw_if_indices
+          [vec_len (gm->free_vxlan_tunnel_hw_if_indices)-1];
+          _vec_len (gm->free_vxlan_tunnel_hw_if_indices) -= 1;
+
+        hi = vnet_get_hw_interface (vnm, hw_if_index);
+        hi->dev_instance = t - gm->tunnels;
+        hi->hw_instance = hi->dev_instance;
+
+        /* clear old stats of freed tunnel before reuse */
+        sw_if_index = hi->sw_if_index;
+        vnet_interface_counter_lock(im);
+        vlib_zero_combined_counter
+          (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
+        vlib_zero_combined_counter
+          (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
+        vlib_zero_simple_counter
+          (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
+        vnet_interface_counter_unlock(im);
+    } else {
+        hw_if_index = vnet_register_interface
+          (vnm, gre_device_class.index, t - gm->tunnels,
+           gre_hw_interface_class.index,
+           t - gm->tunnels);
+        hi = vnet_get_hw_interface (vnm, hw_if_index);
+        sw_if_index = hi->sw_if_index;
+    }
 
-  u64 key = (u64)tunnel_src->as_u32 << 32 | (u64)tunnel_dst->as_u32;
-
-  /* check if same src/dst pair exists */
-  if (hash_get (gm->tunnel_by_key, key))
-    return VNET_API_ERROR_INVALID_VALUE;
-
-  p = hash_get (im->fib_index_by_table_id, outer_fib_id);
-  if (! p)
-    return VNET_API_ERROR_NO_SUCH_FIB;
+    t->hw_if_index = hw_if_index;
+    t->outer_fib_index = outer_fib_index;
+    t->sw_if_index = sw_if_index;
 
-  outer_fib_index = p[0];
+    vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
+    gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
 
-  pool_get (gm->tunnels, t);
-  memset (t, 0, sizeof (*t));
+    hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
+    hi->per_packet_overhead_bytes =
+      /* preamble */ 8 + /* inter frame gap */ 12;
 
-  hw_if_index = vnet_register_interface
-    (vnm, gre_device_class.index, t - gm->tunnels,
-     gre_hw_interface_class.index,
-     t - gm->tunnels);
+    /* Standard default gre MTU. */
+    hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
 
-  *gi_index_return = t - gm->tunnels;
+    clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
+    clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
 
-  t->hw_if_index = hw_if_index;
-  t->outer_fib_index = outer_fib_index;
+    hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
 
-  hi = vnet_get_hw_interface (vnm, hw_if_index);
+    slot = vlib_node_add_named_next_with_slot
+      (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
 
-  hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
-  hi->per_packet_overhead_bytes =
-    /* preamble */ 8 + /* inter frame gap */ 12;
+    ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
 
-  /* Standard default gre MTU. */
-  hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
+  } else { /* !is_add => delete */
+    /* tunnel needs to exist */
+    if (! p)
+      return VNET_API_ERROR_NO_SUCH_ENTRY;
 
-  clib_memcpy (&t->tunnel_src, tunnel_src, sizeof (t->tunnel_src));
-  clib_memcpy (&t->tunnel_dst, tunnel_dst, sizeof (t->tunnel_dst));
+    t = pool_elt_at_index (gm->tunnels, p[0]);
 
-  hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
+    sw_if_index = t->sw_if_index;
+    vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
+    /* make sure tunnel is removed from l2 bd or xconnect */
+    set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
+    vec_add1 (gm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
+    gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
 
-  slot = vlib_node_add_named_next_with_slot
-    (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
+    hash_unset (gm->tunnel_by_key, key);
+    pool_put (gm->tunnels, t);
+  }
 
-  ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
+  if (sw_if_indexp)
+    *sw_if_indexp = sw_if_index;
 
   return 0;
 }
@@ -90,19 +150,21 @@ create_gre_tunnel_command_fn (vlib_main_t * vm,
                 vlib_cli_command_t * cmd)
 {
   unformat_input_t _line_input, * line_input = &_line_input;
-  vnet_main_t * vnm = vnet_get_main();
+  vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
   ip4_address_t src, dst;
   u32 outer_fib_id = 0;
   int rv;
-  u32 gi_index;
   u32 num_m_args = 0;
+  u8 is_add = 1;
 
   /* Get a line of input. */
   if (! unformat_user (input, unformat_line_input, line_input))
     return 0;
 
   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
-    if (unformat (line_input, "src %U", unformat_ip4_address, &src))
+    if (unformat (line_input, "del"))
+      is_add = 0;
+    else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
       num_m_args++;
     else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
       num_m_args++;
@@ -117,10 +179,18 @@ create_gre_tunnel_command_fn (vlib_main_t * vm,
   if (num_m_args < 2)
       return clib_error_return (0, "mandatory argument(s) missing");
 
-  rv = gre_register_interface (vnm, gre_hw_interface_class.index,
-                                      &src, &dst, outer_fib_id, &gi_index);
+  if (memcmp (&src, &dst, sizeof(src)) == 0)
+      return clib_error_return (0, "src and dst are identical");
 
- switch(rv)
+  memset (a, 0, sizeof (*a));
+  a->is_add = is_add;
+  a->outer_table_id = outer_fib_id;
+  clib_memcpy(&a->src, &src, sizeof(src));
+  clib_memcpy(&a->dst, &dst, sizeof(dst));
+
+  rv = vnet_gre_add_del_tunnel (a, 0);
+
+  switch(rv)
     {
     case 0:
       break;
@@ -130,7 +200,7 @@ create_gre_tunnel_command_fn (vlib_main_t * vm,
       return clib_error_return (0, "outer fib ID %d doesn't exist\n",
                                 outer_fib_id);
     default:
-      return clib_error_return (0, "gre_register_interface returned %d", rv);
+      return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
     }
 
   return 0;
@@ -138,10 +208,35 @@ create_gre_tunnel_command_fn (vlib_main_t * vm,
 
 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
   .path = "create gre tunnel",
-  .short_help = "create gre tunnel src <addr> dst <addr> [outer-fib-id <fib>]",
+  .short_help = "create gre tunnel src <addr> dst <addr> "
+                "[outer-fib-id <fib>] [del]",
   .function = create_gre_tunnel_command_fn,
 };
 
+static clib_error_t *
+show_gre_tunnel_command_fn (vlib_main_t * vm,
+                            unformat_input_t * input,
+                            vlib_cli_command_t * cmd)
+{
+  gre_main_t * gm = &gre_main;
+  gre_tunnel_t * t;
+
+  if (pool_elts (gm->tunnels) == 0)
+    vlib_cli_output (vm, "No GRE tunnels configured...");
+
+  pool_foreach (t, gm->tunnels,
+  ({
+    vlib_cli_output (vm, "%U", format_gre_tunnel, t);
+  }));
+
+  return 0;
+}
+
+VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
+    .path = "show gre tunnel",
+    .function = show_gre_tunnel_command_fn,
+};
+
 /* force inclusion from application's main.c */
 clib_error_t *gre_interface_init (vlib_main_t *vm)
 {
index 72eb339..bbb3e25 100644 (file)
@@ -26,6 +26,7 @@
 #include <vnet/l2/l2_input.h>
 #include <vnet/l2tp/l2tp.h>
 #include <vnet/vxlan/vxlan.h>
+#include <vnet/gre/gre.h>
 #include <vnet/nsh-gre/nsh_gre.h>
 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
 #include <vnet/lisp-gpe/lisp_gpe.h>
@@ -1354,6 +1355,36 @@ static void vl_api_vxlan_add_del_tunnel_reply_t_handler_json
     vam->result_ready = 1;
 }
 
+static void vl_api_gre_add_del_tunnel_reply_t_handler
+(vl_api_gre_add_del_tunnel_reply_t * mp)
+{
+    vat_main_t * vam = &vat_main;
+    i32 retval = ntohl(mp->retval);
+    if (vam->async_mode) {
+        vam->async_errors += (retval < 0);
+    } else {
+        vam->retval = retval;
+        vam->result_ready = 1;
+    }
+}
+
+static void vl_api_gre_add_del_tunnel_reply_t_handler_json
+(vl_api_gre_add_del_tunnel_reply_t * mp)
+{
+    vat_main_t * vam = &vat_main;
+    vat_json_node_t node;
+
+    vat_json_init_object(&node);
+    vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
+    vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
+
+    vat_json_print(vam->ofp, &node);
+    vat_json_free(&node);
+
+    vam->retval = ntohl(mp->retval);
+    vam->result_ready = 1;
+}
+
 static void vl_api_create_vhost_user_if_reply_t_handler
 (vl_api_create_vhost_user_if_reply_t * mp)
 {
@@ -2283,6 +2314,8 @@ _(L2TPV3_SET_LOOKUP_KEY_REPLY, l2tpv3_set_lookup_key_reply)             \
 _(SW_IF_L2TPV3_TUNNEL_DETAILS, sw_if_l2tpv3_tunnel_details)             \
 _(VXLAN_ADD_DEL_TUNNEL_REPLY, vxlan_add_del_tunnel_reply)               \
 _(VXLAN_TUNNEL_DETAILS, vxlan_tunnel_details)                           \
+_(GRE_ADD_DEL_TUNNEL_REPLY, gre_add_del_tunnel_reply)                   \
+_(GRE_TUNNEL_DETAILS, gre_tunnel_details)                               \
 _(L2_FIB_CLEAR_TABLE_REPLY, l2_fib_clear_table_reply)                   \
 _(L2_INTERFACE_EFP_FILTER_REPLY, l2_interface_efp_filter_reply)         \
 _(L2_INTERFACE_VLAN_TAG_REWRITE_REPLY, l2_interface_vlan_tag_rewrite_reply) \
@@ -7255,6 +7288,132 @@ static int api_vxlan_tunnel_dump (vat_main_t * vam)
     W;
 }
 
+static int api_gre_add_del_tunnel (vat_main_t * vam)
+{
+    unformat_input_t * line_input = vam->input;
+    vl_api_gre_add_del_tunnel_t *mp;
+    f64 timeout;
+    ip4_address_t src4, dst4;
+    u8 is_add = 1;
+    u8 src_set = 0;
+    u8 dst_set = 0;
+    u32 outer_fib_id = 0;
+
+    while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
+        if (unformat (line_input, "del"))
+            is_add = 0;
+        else if (unformat (line_input, "src %U",
+                           unformat_ip4_address, &src4))
+            src_set = 1;
+        else if (unformat (line_input, "dst %U",
+                           unformat_ip4_address, &dst4))
+            dst_set = 1;
+        else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
+            ;
+        else {
+            errmsg ("parse error '%U'\n", format_unformat_error, line_input);
+            return -99;
+        }
+    }
+
+    if (src_set == 0) {
+        errmsg ("tunnel src address not specified\n");
+        return -99;
+    }
+    if (dst_set == 0) {
+        errmsg ("tunnel dst address not specified\n");
+        return -99;
+    }
+
+
+    M (GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel);
+
+    clib_memcpy(&mp->src_address, &src4, sizeof(src4));
+    clib_memcpy(&mp->dst_address, &dst4, sizeof(dst4));
+    mp->outer_table_id = ntohl(outer_fib_id);
+    mp->is_add = is_add;
+
+    S; W;
+    /* NOTREACHED */
+    return 0;
+}
+
+static void vl_api_gre_tunnel_details_t_handler
+(vl_api_gre_tunnel_details_t * mp)
+{
+    vat_main_t * vam = &vat_main;
+
+    fformat(vam->ofp, "%11d%15U%15U%14d\n",
+            ntohl(mp->sw_if_index),
+            format_ip4_address, &mp->src_address,
+            format_ip4_address, &mp->dst_address,
+            ntohl(mp->outer_table_id));
+}
+
+static void vl_api_gre_tunnel_details_t_handler_json
+(vl_api_gre_tunnel_details_t * mp)
+{
+    vat_main_t * vam = &vat_main;
+    vat_json_node_t *node = NULL;
+    struct in_addr ip4;
+
+    if (VAT_JSON_ARRAY != vam->json_tree.type) {
+        ASSERT(VAT_JSON_NONE == vam->json_tree.type);
+        vat_json_init_array(&vam->json_tree);
+    }
+    node = vat_json_array_add(&vam->json_tree);
+
+    vat_json_init_object(node);
+    vat_json_object_add_uint(node, "sw_if_index", ntohl(mp->sw_if_index));
+    clib_memcpy(&ip4, &mp->src_address, sizeof(ip4));
+    vat_json_object_add_ip4(node, "src_address", ip4);
+    clib_memcpy(&ip4, &mp->dst_address, sizeof(ip4));
+    vat_json_object_add_ip4(node, "dst_address", ip4);
+    vat_json_object_add_uint(node, "outer_fib_id", ntohl(mp->outer_table_id));
+}
+
+static int api_gre_tunnel_dump (vat_main_t * vam)
+{
+    unformat_input_t * i = vam->input;
+    vl_api_gre_tunnel_dump_t *mp;
+    f64 timeout;
+    u32 sw_if_index;
+    u8 sw_if_index_set = 0;
+
+    /* Parse args required to build the message */
+    while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
+        if (unformat (i, "sw_if_index %d", &sw_if_index))
+            sw_if_index_set = 1;
+        else
+            break;
+    }
+
+    if (sw_if_index_set == 0) {
+        sw_if_index = ~0;
+    }
+
+    if (!vam->json_output) {
+        fformat(vam->ofp, "%11s%15s%15s%14s\n",
+                "sw_if_index", "src_address", "dst_address",
+                "outer_fib_id");
+    }
+
+    /* Get list of gre-tunnel interfaces */
+    M(GRE_TUNNEL_DUMP, gre_tunnel_dump);
+
+    mp->sw_if_index = htonl(sw_if_index);
+
+    S;
+
+    /* Use a control ping for synchronization */
+    {
+        vl_api_control_ping_t * mp;
+        M(CONTROL_PING, control_ping);
+        S;
+    }
+    W;
+}
+
 static int api_l2_fib_clear_table (vat_main_t * vam)
 {
 //  unformat_input_t * i = vam->input;
@@ -10378,9 +10537,12 @@ _(l2tpv3_set_lookup_key,                                                \
   "lookup_v6_src | lookup_v6_dst | lookup_session_id")                  \
 _(sw_if_l2tpv3_tunnel_dump, "")                                         \
 _(vxlan_add_del_tunnel,                                                 \
-  "src <ip4-addr> dst <ip4-addr> vni [encap-vrf-id <nn>]\n"             \
+  "src <ip4-addr> dst <ip4-addr> vni <vni> [encap-vrf-id <nn>]\n"       \
   " [decap-next l2|ip4|ip6] [del]")                                     \
 _(vxlan_tunnel_dump, "[<intfc> | sw_if_index <nn>]")                    \
+_(gre_add_del_tunnel,                                                   \
+  "src <ip4-addr> dst <ip4-addr> [outer-fib-id <nn>] [del]\n")          \
+_(gre_tunnel_dump, "[<intfc> | sw_if_index <nn>]")                      \
 _(l2_fib_clear_table, "")                                               \
 _(l2_interface_efp_filter, "sw_if_index <nn> enable | disable")         \
 _(l2_interface_vlan_tag_rewrite,                                        \
index 9c988a1..eaae51a 100644 (file)
@@ -64,6 +64,7 @@
 #include <vnet/classify/input_acl.h>
 #include <vnet/l2/l2_classify.h>
 #include <vnet/vxlan/vxlan.h>
+#include <vnet/gre/gre.h>
 #include <vnet/l2/l2_vtr.h>
 #include <vnet/nsh-gre/nsh_gre.h>
 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
@@ -272,6 +273,8 @@ _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
+_(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)                               \
+_(GRE_TUNNEL_DUMP, gre_tunnel_dump)                                     \
 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
@@ -4286,6 +4289,95 @@ static void vl_api_vxlan_tunnel_dump_t_handler
     }
 }
 
+static void vl_api_gre_add_del_tunnel_t_handler
+(vl_api_gre_add_del_tunnel_t * mp)
+{
+    vl_api_gre_add_del_tunnel_reply_t * rmp;
+    int rv = 0;
+    vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
+    u32 outer_table_id;
+    uword * p;
+    ip4_main_t * im = &ip4_main;
+    u32 sw_if_index = ~0;
+
+    p = hash_get (im->fib_index_by_table_id, ntohl(mp->outer_table_id));
+    if (! p) {
+        rv = VNET_API_ERROR_NO_SUCH_FIB;
+        goto out;
+    }
+    outer_table_id = p[0];
+
+    /* Check src & dst are different */
+    if (memcmp(&mp->src_address, &mp->dst_address, 4) == 0) {
+        rv = VNET_API_ERROR_SAME_SRC_DST;
+        goto out;
+    }
+
+    memset (a, 0, sizeof (*a));
+
+    a->is_add = mp->is_add;
+
+    /* ip addresses sent in network byte order */
+    a->src.as_u32 = mp->src_address;
+    a->dst.as_u32 = mp->dst_address;
+
+    a->outer_table_id = outer_table_id;
+    rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
+
+out:
+    REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
+    ({
+        rmp->sw_if_index = ntohl (sw_if_index);
+    }));
+}
+
+static void send_gre_tunnel_details
+(gre_tunnel_t * t, unix_shared_memory_queue_t * q)
+{
+    vl_api_gre_tunnel_details_t * rmp;
+    ip4_main_t * im = &ip4_main;
+
+    rmp = vl_msg_api_alloc (sizeof (*rmp));
+    memset (rmp, 0, sizeof (*rmp));
+    rmp->_vl_msg_id = ntohs(VL_API_GRE_TUNNEL_DETAILS);
+    rmp->src_address = t->tunnel_src.data_u32;
+    rmp->dst_address = t->tunnel_dst.data_u32;
+    rmp->outer_table_id = htonl(im->fibs[t->outer_fib_index].table_id);
+    rmp->sw_if_index = htonl(t->sw_if_index);
+
+    vl_msg_api_send_shmem (q, (u8 *)&rmp);
+}
+
+static void vl_api_gre_tunnel_dump_t_handler
+(vl_api_gre_tunnel_dump_t * mp)
+{
+    unix_shared_memory_queue_t * q;
+    gre_main_t * gm = &gre_main;
+    gre_tunnel_t * t;
+    u32 sw_if_index;
+
+    q = vl_api_client_index_to_input_queue (mp->client_index);
+    if (q == 0) {
+        return;
+    }
+
+    sw_if_index = ntohl(mp->sw_if_index);
+
+    if (~0 == sw_if_index) {
+        pool_foreach (t, gm->tunnels,
+        ({
+            send_gre_tunnel_details(t, q);
+        }));
+    } else {
+        if ((sw_if_index >= vec_len(gm->tunnel_index_by_sw_if_index)) ||
+                (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) {
+            return;
+        }
+        t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
+        send_gre_tunnel_details(t, q);
+    }
+}
+
 static void 
 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t *mp)
 {
index f4e5e75..cd17328 100644 (file)
@@ -1297,6 +1297,40 @@ static void * vl_api_vxlan_tunnel_dump_t_print
     FINISH;
 }
 
+static void * vl_api_gre_add_del_tunnel_t_print
+(vl_api_gre_add_del_tunnel_t * mp, void *handle)
+{
+    u8 * s;
+
+    s = format (0, "SCRIPT: gre_add_del_tunnel ");
+
+    s = format (s, "dst %U ", format_ip4_address,
+                (ip4_address_t *)&(mp->dst_address));
+
+    s = format (s, "src %U ", format_ip4_address,
+                (ip4_address_t *)&(mp->src_address));
+
+    if (mp->outer_table_id)
+        s = format (s, "outer-fib-id %d ", ntohl(mp->outer_table_id));
+
+    if (mp->is_add == 0)
+        s = format (s, "del ");
+
+    FINISH;
+}
+
+static void * vl_api_gre_tunnel_dump_t_print
+(vl_api_gre_tunnel_dump_t * mp, void *handle)
+{
+    u8 * s;
+
+    s = format (0, "SCRIPT: gre_tunnel_dump ");
+
+    s = format (s, "sw_if_index %d ", ntohl(mp->sw_if_index));
+
+    FINISH;
+}
+
 static void *vl_api_l2_fib_clear_table_t_print
 (vl_api_l2_fib_clear_table_t * mp, void *handle)
 {
@@ -1795,6 +1829,8 @@ _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
+_(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)                               \
+_(GRE_TUNNEL_DUMP, gre_tunnel_dump)                                     \
 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
index 52893b5..88b6070 100644 (file)
@@ -1799,6 +1799,35 @@ manual_java define vxlan_tunnel_details {
     u32 vni;
     u8 is_ipv6;
 };
+
+define gre_add_del_tunnel {
+    u32 client_index;
+    u32 context;
+    u8 is_add;
+    u32 src_address;
+    u32 dst_address;
+    u32 outer_table_id;
+};
+
+define gre_add_del_tunnel_reply {
+    u32 context;
+    i32 retval;
+    u32 sw_if_index;
+};
+
+manual_java define gre_tunnel_dump {
+    u32 client_index;
+    u32 context;
+    u32 sw_if_index;
+};
+
+manual_java define gre_tunnel_details {
+    u32 context;
+    u32 sw_if_index;
+    u32 src_address;
+    u32 dst_address;
+    u32 outer_table_id;
+};
   
 /** \brief L2 interface vlan tag rewrite configure request
     @param client_index - opaque cookie to identify the sender