af_packet: Add support for dump interfaces

Change-Id: I8d8ecc80edb7665125ba625a3ce7b30d2dea88f0
Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
diff --git a/src/vat/api_format.c b/src/vat/api_format.c
index c3ba701..df62e81 100644
--- a/src/vat/api_format.c
+++ b/src/vat/api_format.c
@@ -5825,6 +5825,7 @@
   show_one_map_register_fallback_threshold_reply)                       \
 _(AF_PACKET_CREATE_REPLY, af_packet_create_reply)                       \
 _(AF_PACKET_DELETE_REPLY, af_packet_delete_reply)                       \
+_(AF_PACKET_DETAILS, af_packet_details)					\
 _(POLICER_ADD_DEL_REPLY, policer_add_del_reply)                         \
 _(POLICER_DETAILS, policer_details)                                     \
 _(POLICER_CLASSIFY_SET_INTERFACE_REPLY, policer_classify_set_interface_reply) \
@@ -19815,6 +19816,53 @@
   return ret;
 }
 
+static void vl_api_af_packet_details_t_handler
+  (vl_api_af_packet_details_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+
+  print (vam->ofp, "%-16s %d",
+	 mp->host_if_name, clib_net_to_host_u32 (mp->sw_if_index));
+}
+
+static void vl_api_af_packet_details_t_handler_json
+  (vl_api_af_packet_details_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+  vat_json_node_t *node = NULL;
+
+  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));
+  vat_json_object_add_string_copy (node, "dev_name", mp->host_if_name);
+}
+
+static int
+api_af_packet_dump (vat_main_t * vam)
+{
+  vl_api_af_packet_dump_t *mp;
+  vl_api_control_ping_t *mp_ping;
+  int ret;
+
+  print (vam->ofp, "\n%-16s %s", "dev_name", "sw_if_index");
+  /* Get list of tap interfaces */
+  M (AF_PACKET_DUMP, mp);
+  S (mp);
+
+  /* Use a control ping for synchronization */
+  MPING (CONTROL_PING, mp_ping);
+  S (mp_ping);
+
+  W (ret);
+  return ret;
+}
+
 static int
 api_policer_add_del (vat_main_t * vam)
 {
@@ -23670,6 +23718,7 @@
 _(show_lisp_map_request_mode, "")                                       \
 _(af_packet_create, "name <host interface name> [hw_addr <mac>]")       \
 _(af_packet_delete, "name <host interface name>")                       \
+_(af_packet_dump, "")							\
 _(policer_add_del, "name <policer name> <params> [del]")                \
 _(policer_dump, "[name <policer name>]")                                \
 _(policer_classify_set_interface,                                       \
diff --git a/src/vnet/devices/af_packet/af_packet.api b/src/vnet/devices/af_packet/af_packet.api
index d7637da..1399011 100644
--- a/src/vnet/devices/af_packet/af_packet.api
+++ b/src/vnet/devices/af_packet/af_packet.api
@@ -69,6 +69,24 @@
   u8 set;
 };
 
+/** \brief Dump af_packet interfaces request */
+define af_packet_dump
+{
+  u32 client_index;
+  u32 context;
+};
+
+/** \brief Reply for af_packet dump request
+    @param sw_if_index - software index of af_packet interface
+    @param host_if_name - interface name
+*/
+define af_packet_details
+{
+  u32 context;
+  u32 sw_if_index;
+  u8 host_if_name[64];
+};
+
 /*
  * Local Variables:
  * eval: (c-set-style "gnu")
diff --git a/src/vnet/devices/af_packet/af_packet.c b/src/vnet/devices/af_packet/af_packet.c
index 2e2c8e6..f1a7b6a 100644
--- a/src/vnet/devices/af_packet/af_packet.c
+++ b/src/vnet/devices/af_packet/af_packet.c
@@ -471,6 +471,31 @@
   return 0;
 }
 
+int
+af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
+{
+  af_packet_main_t *apm = &af_packet_main;
+  af_packet_if_t *apif;
+  af_packet_if_detail_t *r_af_packet_ifs = NULL;
+  af_packet_if_detail_t *af_packet_if = NULL;
+
+  vec_foreach (apif, apm->interfaces)
+  {
+    vec_add2 (r_af_packet_ifs, af_packet_if, 1);
+    af_packet_if->sw_if_index = apif->sw_if_index;
+    if (apif->host_if_name)
+      {
+	clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
+		     MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
+			  strlen ((const char *) apif->host_if_name)));
+      }
+  }
+
+  *out_af_packet_ifs = r_af_packet_ifs;
+
+  return 0;
+}
+
 static clib_error_t *
 af_packet_init (vlib_main_t * vm)
 {
diff --git a/src/vnet/devices/af_packet/af_packet.h b/src/vnet/devices/af_packet/af_packet.h
index 1136be6..b4621f6 100644
--- a/src/vnet/devices/af_packet/af_packet.h
+++ b/src/vnet/devices/af_packet/af_packet.h
@@ -23,6 +23,12 @@
 
 typedef struct
 {
+  u32 sw_if_index;
+  u8 host_if_name[64];
+} af_packet_if_detail_t;
+
+typedef struct
+{
   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
   clib_spinlock_t lockp;
   u8 *host_if_name;
@@ -70,9 +76,12 @@
 int af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name);
 int af_packet_set_l4_cksum_offload (vlib_main_t * vm, u32 sw_if_index,
 				    u8 set);
+int af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs);
 
 format_function_t format_af_packet_device_name;
 
+#define MIN(x,y) (((x)<(y))?(x):(y))
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vnet/devices/af_packet/af_packet_api.c b/src/vnet/devices/af_packet/af_packet_api.c
index c7dfabc..3e03b59 100644
--- a/src/vnet/devices/af_packet/af_packet_api.c
+++ b/src/vnet/devices/af_packet/af_packet_api.c
@@ -45,7 +45,8 @@
 #define foreach_vpe_api_msg                                          \
 _(AF_PACKET_CREATE, af_packet_create)                                \
 _(AF_PACKET_DELETE, af_packet_delete)                                \
-_(AF_PACKET_SET_L4_CKSUM_OFFLOAD, af_packet_set_l4_cksum_offload)
+_(AF_PACKET_SET_L4_CKSUM_OFFLOAD, af_packet_set_l4_cksum_offload)    \
+_(AF_PACKET_DUMP, af_packet_dump)
 
 static void
 vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp)
@@ -103,6 +104,50 @@
   REPLY_MACRO (VL_API_AF_PACKET_SET_L4_CKSUM_OFFLOAD_REPLY);
 }
 
+static void
+af_packet_send_details (vpe_api_main_t * am,
+			vl_api_registration_t * reg,
+			af_packet_if_detail_t * af_packet_if, u32 context)
+{
+  vl_api_af_packet_details_t *mp;
+  mp = vl_msg_api_alloc (sizeof (*mp));
+  memset (mp, 0, sizeof (*mp));
+  mp->_vl_msg_id = htons (VL_API_AF_PACKET_DETAILS);
+  mp->sw_if_index = htonl (af_packet_if->sw_if_index);
+  clib_memcpy (mp->host_if_name, af_packet_if->host_if_name,
+	       MIN (ARRAY_LEN (mp->host_if_name) - 1,
+		    strlen ((const char *) af_packet_if->host_if_name)));
+
+  mp->context = context;
+  vl_api_send_msg (reg, (u8 *) mp);
+}
+
+
+static void
+vl_api_af_packet_dump_t_handler (vl_api_af_packet_dump_t * mp)
+{
+  int rv;
+  vpe_api_main_t *am = &vpe_api_main;
+  vl_api_registration_t *reg;
+  af_packet_if_detail_t *out_af_packet_ifs = NULL;
+  af_packet_if_detail_t *af_packet_if = NULL;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  rv = af_packet_dump_ifs (&out_af_packet_ifs);
+  if (rv)
+    return;
+
+  vec_foreach (af_packet_if, out_af_packet_ifs)
+  {
+    af_packet_send_details (am, reg, af_packet_if, mp->context);
+  }
+
+  vec_free (out_af_packet_ifs);
+}
+
 /*
  * af_packet_api_hookup
  * Add vpe's API message handlers to the table.
diff --git a/src/vpp/api/custom_dump.c b/src/vpp/api/custom_dump.c
index 15b1f69..99ad189 100644
--- a/src/vpp/api/custom_dump.c
+++ b/src/vpp/api/custom_dump.c
@@ -2205,6 +2205,16 @@
   FINISH;
 }
 
+static void *vl_api_af_packet_dump_t_print
+  (vl_api_af_packet_dump_t * mp, void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: af_packet_dump ");
+
+  FINISH;
+}
+
 static u8 *
 format_policer_action (u8 * s, va_list * va)
 {
@@ -3591,6 +3601,7 @@
 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
 _(AF_PACKET_CREATE, af_packet_create)					\
 _(AF_PACKET_DELETE, af_packet_delete)					\
+_(AF_PACKET_DUMP, af_packet_dump)                                       \
 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)                   \
 _(MPLS_FIB_DUMP, mpls_fib_dump)                                         \
 _(MPLS_TUNNEL_DUMP, mpls_tunnel_dump)                                   \