LISP: add dump calls for GPE entries API

Change-Id: Ie7f51643fd3522a0fa8df8d0309305481c211f5f
Signed-off-by: Filip Tehlar <ftehlar@cisco.com>
diff --git a/src/vat/api_format.c b/src/vat/api_format.c
index 6b8c5fb..6a45099 100644
--- a/src/vat/api_format.c
+++ b/src/vat/api_format.c
@@ -2683,6 +2683,161 @@
 }
 
 static void
+api_lisp_gpe_fwd_entry_net_to_host (vl_api_lisp_gpe_fwd_entry_t * e)
+{
+  e->dp_table = clib_net_to_host_u32 (e->dp_table);
+  e->fwd_entry_index = clib_net_to_host_u32 (e->fwd_entry_index);
+}
+
+static void
+  lisp_gpe_fwd_entries_get_reply_t_net_to_host
+  (vl_api_lisp_gpe_fwd_entries_get_reply_t * mp)
+{
+  u32 i;
+
+  mp->count = clib_net_to_host_u32 (mp->count);
+  for (i = 0; i < mp->count; i++)
+    {
+      api_lisp_gpe_fwd_entry_net_to_host (&mp->entries[i]);
+    }
+}
+
+static void
+  vl_api_lisp_gpe_fwd_entry_path_details_t_handler
+  (vl_api_lisp_gpe_fwd_entry_path_details_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+  u8 *(*format_ip_address_fcn) (u8 *, va_list *) = 0;
+
+  if (mp->lcl_loc.is_ip4)
+    format_ip_address_fcn = format_ip4_address;
+  else
+    format_ip_address_fcn = format_ip6_address;
+
+  print (vam->ofp, "w:%d %30U %30U", mp->rmt_loc.weight,
+	 format_ip_address_fcn, &mp->lcl_loc,
+	 format_ip_address_fcn, &mp->rmt_loc);
+}
+
+static void
+lisp_fill_locator_node (vat_json_node_t * n, vl_api_lisp_gpe_locator_t * loc)
+{
+  struct in6_addr ip6;
+  struct in_addr ip4;
+
+  if (loc->is_ip4)
+    {
+      clib_memcpy (&ip4, loc->addr, sizeof (ip4));
+      vat_json_object_add_ip4 (n, "address", ip4);
+    }
+  else
+    {
+      clib_memcpy (&ip6, loc->addr, sizeof (ip6));
+      vat_json_object_add_ip6 (n, "address", ip6);
+    }
+  vat_json_object_add_uint (n, "weight", loc->weight);
+}
+
+static void
+  vl_api_lisp_gpe_fwd_entry_path_details_t_handler_json
+  (vl_api_lisp_gpe_fwd_entry_path_details_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+  vat_json_node_t *node = NULL;
+  vat_json_node_t *loc_node;
+
+  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);
+
+  loc_node = vat_json_object_add (node, "local_locator");
+  vat_json_init_object (loc_node);
+  lisp_fill_locator_node (loc_node, &mp->lcl_loc);
+
+  loc_node = vat_json_object_add (node, "remote_locator");
+  vat_json_init_object (loc_node);
+  lisp_fill_locator_node (loc_node, &mp->rmt_loc);
+}
+
+static void
+  vl_api_lisp_gpe_fwd_entries_get_reply_t_handler
+  (vl_api_lisp_gpe_fwd_entries_get_reply_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+  u32 i;
+  int retval = clib_net_to_host_u32 (mp->retval);
+  vl_api_lisp_gpe_fwd_entry_t *e;
+
+  if (retval)
+    goto end;
+
+  lisp_gpe_fwd_entries_get_reply_t_net_to_host (mp);
+
+  for (i = 0; i < mp->count; i++)
+    {
+      e = &mp->entries[i];
+      print (vam->ofp, "%10d %10d %U %40U", e->fwd_entry_index, e->dp_table,
+	     format_lisp_flat_eid, e->eid_type, e->leid, e->leid_prefix_len,
+	     format_lisp_flat_eid, e->eid_type, e->reid, e->reid_prefix_len);
+    }
+
+end:
+  vam->retval = retval;
+  vam->result_ready = 1;
+}
+
+static void
+  vl_api_lisp_gpe_fwd_entries_get_reply_t_handler_json
+  (vl_api_lisp_gpe_fwd_entries_get_reply_t * mp)
+{
+  u8 *s = 0;
+  vat_main_t *vam = &vat_main;
+  vat_json_node_t *e = 0, root;
+  u32 i;
+  int retval = clib_net_to_host_u32 (mp->retval);
+  vl_api_lisp_gpe_fwd_entry_t *fwd;
+
+  if (retval)
+    goto end;
+
+  lisp_gpe_fwd_entries_get_reply_t_net_to_host (mp);
+  vat_json_init_array (&root);
+
+  for (i = 0; i < mp->count; i++)
+    {
+      e = vat_json_array_add (&root);
+      fwd = &mp->entries[i];
+
+      vat_json_init_object (e);
+      vat_json_object_add_int (e, "fwd_entry_index", fwd->fwd_entry_index);
+      vat_json_object_add_int (e, "dp_table", fwd->dp_table);
+
+      s = format (0, "%U", format_lisp_flat_eid, fwd->eid_type, fwd->leid,
+		  fwd->leid_prefix_len);
+      vec_add1 (s, 0);
+      vat_json_object_add_string_copy (e, "leid", s);
+      vec_free (s);
+
+      s = format (0, "%U", format_lisp_flat_eid, fwd->eid_type, fwd->reid,
+		  fwd->reid_prefix_len);
+      vec_add1 (s, 0);
+      vat_json_object_add_string_copy (e, "reid", s);
+      vec_free (s);
+    }
+
+  vat_json_print (vam->ofp, &root);
+  vat_json_free (&root);
+
+end:
+  vam->retval = retval;
+  vam->result_ready = 1;
+}
+
+static void
   vl_api_lisp_adjacencies_get_reply_t_handler
   (vl_api_lisp_adjacencies_get_reply_t * mp)
 {
@@ -3969,6 +4124,9 @@
 _(LISP_MAP_RESOLVER_DETAILS, lisp_map_resolver_details)                 \
 _(LISP_MAP_SERVER_DETAILS, lisp_map_server_details)                     \
 _(LISP_ADJACENCIES_GET_REPLY, lisp_adjacencies_get_reply)               \
+_(LISP_GPE_FWD_ENTRIES_GET_REPLY, lisp_gpe_fwd_entries_get_reply)       \
+_(LISP_GPE_FWD_ENTRY_PATH_DETAILS,                                      \
+  lisp_gpe_fwd_entry_path_details)                                      \
 _(SHOW_LISP_STATUS_REPLY, show_lisp_status_reply)                       \
 _(LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY,                             \
   lisp_add_del_map_request_itr_rlocs_reply)                             \
@@ -14855,6 +15013,58 @@
 }
 
 static int
+api_lisp_gpe_fwd_entries_get (vat_main_t * vam)
+{
+  unformat_input_t *i = vam->input;
+  vl_api_lisp_gpe_fwd_entries_get_t *mp;
+  f64 timeout = ~0;
+  u8 vni_set = 0;
+  u32 vni = ~0;
+
+  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (i, "vni %d", &vni))
+	{
+	  vni_set = 1;
+	}
+      else
+	{
+	  errmsg ("parse error '%U'", format_unformat_error, i);
+	  return -99;
+	}
+    }
+
+  if (!vni_set)
+    {
+      errmsg ("vni not set!");
+      return -99;
+    }
+
+  if (!vam->json_output)
+    {
+      print (vam->ofp, "%10s %10s %s %40s", "fwd_index", "dp_table",
+	     "leid", "reid");
+    }
+
+  M (LISP_GPE_FWD_ENTRIES_GET, lisp_gpe_fwd_entries_get);
+  mp->vni = clib_host_to_net_u32 (vni);
+
+  /* send it... */
+  S;
+
+  /* Wait for a reply... */
+  W;
+
+  /* NOTREACHED */
+  return 0;
+}
+
+#define vl_api_lisp_gpe_fwd_entries_get_reply_t_endian vl_noop_handler
+#define vl_api_lisp_gpe_fwd_entries_get_reply_t_print vl_noop_handler
+#define vl_api_lisp_gpe_fwd_entry_path_details_t_endian vl_noop_handler
+#define vl_api_lisp_gpe_fwd_entry_path_details_t_print vl_noop_handler
+
+static int
 api_lisp_adjacencies_get (vat_main_t * vam)
 {
   unformat_input_t *i = vam->input;
@@ -14978,6 +15188,50 @@
 }
 
 static int
+api_lisp_gpe_fwd_entry_path_dump (vat_main_t * vam)
+{
+  vl_api_lisp_gpe_fwd_entry_path_dump_t *mp;
+  f64 timeout = ~0;
+  unformat_input_t *i = vam->input;
+  u32 fwd_entry_index = ~0;
+
+  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (i, "index %d", &fwd_entry_index))
+	;
+      else
+	break;
+    }
+
+  if (~0 == fwd_entry_index)
+    {
+      errmsg ("no index specified!");
+      return -99;
+    }
+
+  if (!vam->json_output)
+    {
+      print (vam->ofp, "first line");
+    }
+
+  M (LISP_GPE_FWD_ENTRY_PATH_DUMP, lisp_gpe_fwd_entry_path_dump);
+
+  /* send it... */
+  S;
+  /* Use a control ping for synchronization */
+  {
+    vl_api_control_ping_t *mp;
+    M (CONTROL_PING, control_ping);
+    S;
+  }
+  /* Wait for a reply... */
+  W;
+
+  /* NOTREACHED */
+  return 0;
+}
+
+static int
 api_lisp_get_map_request_itr_rlocs (vat_main_t * vam)
 {
   vl_api_lisp_get_map_request_itr_rlocs_t *mp;
@@ -17864,6 +18118,8 @@
 _(lisp_map_resolver_dump, "")                                           \
 _(lisp_map_server_dump, "")                                             \
 _(lisp_adjacencies_get, "vni <vni>")                                    \
+_(lisp_gpe_fwd_entries_get, "vni <vni>")                                \
+_(lisp_gpe_fwd_entry_path_dump, "index <fwd_entry_index>")              \
 _(show_lisp_rloc_probe_state, "")                                       \
 _(show_lisp_map_register_state, "")                                     \
 _(show_lisp_status, "")                                                 \
diff --git a/src/vnet/lisp-cp/lisp_api.c b/src/vnet/lisp-cp/lisp_api.c
index 6e07a5c..767f4c6 100644
--- a/src/vnet/lisp-cp/lisp_api.c
+++ b/src/vnet/lisp-cp/lisp_api.c
@@ -1092,12 +1092,11 @@
   vl_api_lisp_adjacencies_get_reply_t *rmp = 0;
   lisp_adjacency_t *adjs = 0;
   int rv = 0;
-  vl_api_lisp_adjacency_t a;
   u32 size = ~0;
   u32 vni = clib_net_to_host_u32 (mp->vni);
 
   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
-  size = vec_len (adjs) * sizeof (a);
+  size = vec_len (adjs) * sizeof (vl_api_lisp_adjacency_t);
 
   /* *INDENT-OFF* */
   REPLY_MACRO4 (VL_API_LISP_ADJACENCIES_GET_REPLY, size,
diff --git a/src/vnet/lisp-gpe/lisp_gpe.api b/src/vnet/lisp-gpe/lisp_gpe.api
index 2a79bec..48baa2f 100644
--- a/src/vnet/lisp-gpe/lisp_gpe.api
+++ b/src/vnet/lisp-gpe/lisp_gpe.api
@@ -117,6 +117,47 @@
   i32 retval;
 };
 
+define lisp_gpe_fwd_entries_get
+{
+  u32 client_index;
+  u32 context;
+  u32 vni;
+};
+
+typeonly manual_print manual_endian define lisp_gpe_fwd_entry
+{
+  u32 fwd_entry_index;
+  u32 dp_table;
+  u8 eid_type;
+  u8 leid_prefix_len;
+  u8 reid_prefix_len;
+  u8 leid[16];
+  u8 reid[16];
+};
+
+manual_print manual_endian define lisp_gpe_fwd_entries_get_reply
+{
+  u32 context;
+  i32 retval;
+  u32 count;
+  vl_api_lisp_gpe_fwd_entry_t entries[count];
+};
+
+define lisp_gpe_fwd_entry_path_dump
+{
+  u32 client_index;
+  u32 context;
+  u32 fwd_entry_index;
+};
+
+manual_endian manual_print define lisp_gpe_fwd_entry_path_details
+{
+  u32 client_index;
+  u32 context;
+  vl_api_lisp_gpe_locator_t lcl_loc;
+  vl_api_lisp_gpe_locator_t rmt_loc;
+};
+
 /*
  * Local Variables:
  * eval: (c-set-style "gnu")
diff --git a/src/vnet/lisp-gpe/lisp_gpe.h b/src/vnet/lisp-gpe/lisp_gpe.h
index bb0f788..3288c99 100644
--- a/src/vnet/lisp-gpe/lisp_gpe.h
+++ b/src/vnet/lisp-gpe/lisp_gpe.h
@@ -220,6 +220,15 @@
   };
 } vnet_lisp_gpe_add_del_fwd_entry_args_t;
 
+typedef struct
+{
+  u32 fwd_entry_index;
+  u32 dp_table;
+  u32 vni;
+  dp_address_t leid;
+  dp_address_t reid;
+} lisp_api_gpe_fwd_entry_t;
+
 #define foreach_lgpe_ip4_lookup_next    \
   _(DROP, "error-drop")                 \
   _(LISP_CP_LOOKUP, "lisp-cp-lookup")
@@ -246,6 +255,8 @@
 
 u8 *format_vnet_lisp_gpe_status (u8 * s, va_list * args);
 
+lisp_api_gpe_fwd_entry_t *vnet_lisp_gpe_fwd_entries_get_by_vni (u32 vni);
+
 #endif /* included_vnet_lisp_gpe_h */
 
 /*
diff --git a/src/vnet/lisp-gpe/lisp_gpe_api.c b/src/vnet/lisp-gpe/lisp_gpe_api.c
index 93b6053..29f7639 100644
--- a/src/vnet/lisp-gpe/lisp_gpe_api.c
+++ b/src/vnet/lisp-gpe/lisp_gpe_api.c
@@ -23,6 +23,8 @@
 #include <vnet/interface.h>
 #include <vnet/api_errno.h>
 #include <vnet/lisp-gpe/lisp_gpe.h>
+#include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
+#include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
 
@@ -51,6 +53,8 @@
 
 #define foreach_vpe_api_msg                             \
 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry)               \
+_(LISP_GPE_FWD_ENTRIES_GET, lisp_gpe_fwd_entries_get)                   \
+_(LISP_GPE_FWD_ENTRY_PATH_DUMP, lisp_gpe_fwd_entry_path_dump)           \
 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable)                     \
 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface)
 
@@ -115,6 +119,172 @@
 }
 
 static void
+  lisp_gpe_fwd_entry_path_dump_t_net_to_host
+  (vl_api_lisp_gpe_fwd_entry_path_dump_t * mp)
+{
+  mp->fwd_entry_index = clib_net_to_host_u32 (mp->fwd_entry_index);
+}
+
+static void
+lisp_api_set_locator (vl_api_lisp_gpe_locator_t * loc,
+		      const ip_address_t * addr, u8 weight)
+{
+  loc->weight = weight;
+  if (IP4 == ip_addr_version (addr))
+    {
+      loc->is_ip4 = 1;
+      memcpy (loc->addr, addr, 4);
+    }
+  else
+    {
+      loc->is_ip4 = 0;
+      memcpy (loc->addr, addr, 16);
+    }
+}
+
+static void
+  vl_api_lisp_gpe_fwd_entry_path_dump_t_handler
+  (vl_api_lisp_gpe_fwd_entry_path_dump_t * mp)
+{
+  lisp_fwd_path_t *path;
+  vl_api_lisp_gpe_fwd_entry_path_details_t *rmp = NULL;
+  lisp_gpe_main_t *lgm = &lisp_gpe_main;
+  unix_shared_memory_queue_t *q = NULL;
+  lisp_gpe_fwd_entry_t *lfe;
+
+  lisp_gpe_fwd_entry_path_dump_t_net_to_host (mp);
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    return;
+
+  if (pool_is_free_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index))
+    return;
+
+  lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index);
+
+  if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
+    return;
+
+  vec_foreach (path, lfe->paths)
+  {
+    rmp = vl_msg_api_alloc (sizeof (*rmp));
+    memset (rmp, 0, sizeof (*rmp));
+    const lisp_gpe_tunnel_t *lgt;
+
+    rmp->_vl_msg_id =
+      clib_host_to_net_u16 (VL_API_LISP_GPE_FWD_ENTRY_PATH_DETAILS);
+
+    const lisp_gpe_adjacency_t *ladj =
+      lisp_gpe_adjacency_get (path->lisp_adj);
+    lisp_api_set_locator (&rmp->rmt_loc, &ladj->remote_rloc, path->weight);
+    lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
+    lisp_api_set_locator (&rmp->lcl_loc, &lgt->key->lcl, path->weight);
+
+    rmp->context = mp->context;
+    vl_msg_api_send_shmem (q, (u8 *) & rmp);
+  }
+}
+
+static void
+lisp_gpe_fwd_entries_copy (vl_api_lisp_gpe_fwd_entry_t * dst,
+			   lisp_api_gpe_fwd_entry_t * src)
+{
+  lisp_api_gpe_fwd_entry_t *e;
+  u32 i = 0;
+
+  vec_foreach (e, src)
+  {
+    memset (dst, 0, sizeof (*dst));
+    dst[i].dp_table = src->dp_table;
+    dst[i].fwd_entry_index = src->fwd_entry_index;
+    switch (fid_addr_type (&e->leid))
+      {
+      case FID_ADDR_IP_PREF:
+	if (IP4 == ip_prefix_version (&fid_addr_ippref (&e->leid)))
+	  {
+	    memcpy (&dst[i].leid, &fid_addr_ippref (&e->leid), 4);
+	    memcpy (&dst[i].reid, &fid_addr_ippref (&e->reid), 4);
+	    dst[i].eid_type = 0;
+	  }
+	else
+	  {
+	    memcpy (&dst[i].leid, &fid_addr_ippref (&e->leid), 16);
+	    memcpy (&dst[i].reid, &fid_addr_ippref (&e->reid), 16);
+	    dst[i].eid_type = 1;
+	  }
+	dst[i].leid_prefix_len = ip_prefix_len (&fid_addr_ippref (&e->leid));
+	dst[i].reid_prefix_len = ip_prefix_len (&fid_addr_ippref (&e->reid));
+	break;
+      case FID_ADDR_MAC:
+	memcpy (&dst[i].leid, fid_addr_mac (&e->leid), 6);
+	memcpy (&dst[i].reid, fid_addr_mac (&e->reid), 6);
+	dst[i].eid_type = 2;
+	break;
+      default:
+	clib_warning ("unknown fid type %d!", fid_addr_type (&e->leid));
+	break;
+      }
+    i++;
+  }
+}
+
+static void
+  lisp_gpe_fwd_entries_get_t_net_to_host
+  (vl_api_lisp_gpe_fwd_entries_get_t * mp)
+{
+  mp->vni = clib_net_to_host_u32 (mp->vni);
+}
+
+static void
+lisp_gpe_entry_t_host_to_net (vl_api_lisp_gpe_fwd_entry_t * e)
+{
+  e->fwd_entry_index = clib_host_to_net_u32 (e->fwd_entry_index);
+  e->dp_table = clib_host_to_net_u32 (e->dp_table);
+}
+
+static void
+  lisp_gpe_fwd_entries_get_reply_t_host_to_net
+  (vl_api_lisp_gpe_fwd_entries_get_reply_t * mp)
+{
+  u32 i;
+  vl_api_lisp_gpe_fwd_entry_t *e;
+
+  for (i = 0; i < mp->count; i++)
+    {
+      e = &mp->entries[i];
+      lisp_gpe_entry_t_host_to_net (e);
+    }
+  mp->count = clib_host_to_net_u32 (mp->count);
+}
+
+static void
+  vl_api_lisp_gpe_fwd_entries_get_t_handler
+  (vl_api_lisp_gpe_fwd_entries_get_t * mp)
+{
+  lisp_api_gpe_fwd_entry_t *e;
+  vl_api_lisp_gpe_fwd_entries_get_reply_t *rmp = 0;
+  u32 size = 0;
+  int rv = 0;
+
+  lisp_gpe_fwd_entries_get_t_net_to_host (mp);
+
+  e = vnet_lisp_gpe_fwd_entries_get_by_vni (mp->vni);
+  size = vec_len (e) * sizeof (vl_api_lisp_gpe_fwd_entry_t);
+
+  /* *INDENT-OFF* */
+  REPLY_MACRO4 (VL_API_LISP_GPE_FWD_ENTRIES_GET_REPLY, size,
+  {
+    rmp->count = vec_len (e);
+    lisp_gpe_fwd_entries_copy (rmp->entries, e);
+    lisp_gpe_fwd_entries_get_reply_t_host_to_net (rmp);
+  });
+  /* *INDENT-ON* */
+
+  vec_free (e);
+}
+
+static void
   lisp_gpe_add_del_fwd_entry_t_net_to_host
   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
 {
diff --git a/src/vnet/lisp-gpe/lisp_gpe_fwd_entry.c b/src/vnet/lisp-gpe/lisp_gpe_fwd_entry.c
index e05f0e0..7ad8679 100644
--- a/src/vnet/lisp-gpe/lisp_gpe_fwd_entry.c
+++ b/src/vnet/lisp-gpe/lisp_gpe_fwd_entry.c
@@ -1042,6 +1042,32 @@
   return (error);
 }
 
+lisp_api_gpe_fwd_entry_t *
+vnet_lisp_gpe_fwd_entries_get_by_vni (u32 vni)
+{
+  lisp_gpe_main_t *lgm = &lisp_gpe_main;
+  lisp_gpe_fwd_entry_t *lfe;
+  lisp_api_gpe_fwd_entry_t *entries = 0, e;
+
+  /* *INDENT-OFF* */
+  pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
+  ({
+    if (lfe->key->vni == vni)
+      {
+        memset (&e, 0, sizeof (e));
+        e.dp_table = lfe->eid_table_id;
+        e.vni = lfe->key->vni;
+        e.fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
+        memcpy (&e.reid, &lfe->key->rmt, sizeof (e.reid));
+        memcpy (&e.leid, &lfe->key->lcl, sizeof (e.leid));
+        vec_add1 (entries, e);
+      }
+  }));
+  /* *INDENT-ON* */
+
+  return entries;
+}
+
 VLIB_INIT_FUNCTION (lisp_gpe_fwd_entry_init);
 
 /*