VPP-1507: Added binary api to dump configured ip_punt_redirect

Change-Id: I790f7785e183cc9aaffd5b593617c4e12a32e20d
Signed-off-by: Pavel Kotucek <pavel.kotucek@pantheon.tech>
diff --git a/src/vnet/ip/ip.api b/src/vnet/ip/ip.api
index 6ae0e02..ca0088f 100644
--- a/src/vnet/ip/ip.api
+++ b/src/vnet/ip/ip.api
@@ -630,26 +630,47 @@
   u8 is_ip6;
 };
 
-/** \brief IP punt redirect
-    @param client_index - opaque cookie to identify the sender
-    @param context - sender context, to match reply w/ request
-    @param is_add - 1 to add neighbor, 0 to delete
-    @param is_ipv6 - 1 for IPv6 neighbor, 0 for IPv4
+/** \brief Punt redirect type
     @param rx_sw_if_index - specify the original RX interface of traffic
                             that should be redirected. ~0 means any interface.
     @param tx_sw_if_index - the TX interface to which traffic shoulde be
                             redirected.
-    @param nh - The next-hop to redirect the traffic to.
+    @param nh - the next-hop to redirect the traffic to.
+    @param is_ipv6 - 1 for IPv6 neighbor, 0 for IPv4
+*/
+typeonly define punt_redirect
+{
+  u32 rx_sw_if_index;
+  u32 tx_sw_if_index;
+  vl_api_address_t nh;
+};
+
+/** \brief IP punt redirect
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param punt - punt definition
+    @param is_add - 1 to add neighbor, 0 to delete
 */
 autoreply define ip_punt_redirect
 {
   u32 client_index;
   u32 context;
-  u32 rx_sw_if_index;
-  u32 tx_sw_if_index;
+  vl_api_punt_redirect_t punt;
   u8 is_add;
-  u8 is_ip6;
-  u8 nh[16];
+};
+
+define ip_punt_redirect_dump
+{
+  u32 client_index;
+  u32 context;
+  u32 sw_if_index;
+  u8 is_ipv6;
+};
+
+define ip_punt_redirect_details
+{
+  u32 context;
+  vl_api_punt_redirect_t punt;
 };
 
 autoreply define ip_container_proxy_add_del
diff --git a/src/vnet/ip/ip4_punt_drop.c b/src/vnet/ip/ip4_punt_drop.c
index cec2c5a..e36980c 100644
--- a/src/vnet/ip/ip4_punt_drop.c
+++ b/src/vnet/ip/ip4_punt_drop.c
@@ -521,6 +521,42 @@
   return (s);
 }
 
+ip_punt_redirect_detail_t *
+ip4_punt_redirect_entries (u32 sw_if_index)
+{
+  ip_punt_redirect_rx_t *pr;
+  ip_punt_redirect_detail_t *prs = 0;
+  u32 rx_sw_if_index;
+
+  vec_foreach_index (rx_sw_if_index,
+		     ip4_punt_redirect_cfg.redirect_by_rx_sw_if_index)
+  {
+    if (sw_if_index == ~0 || sw_if_index == rx_sw_if_index)
+      {
+	pr =
+	  &ip4_punt_redirect_cfg.redirect_by_rx_sw_if_index[rx_sw_if_index];
+	if (~0 != pr->tx_sw_if_index)
+	  {
+	    ip_punt_redirect_detail_t detail = {.rx_sw_if_index =
+		rx_sw_if_index,
+	      .punt_redirect = *pr
+	    };
+	    vec_add1 (prs, detail);
+	  }
+      }
+  }
+  if (~0 != ip4_punt_redirect_cfg.any_rx_sw_if_index.tx_sw_if_index)
+    {
+      pr = &ip4_punt_redirect_cfg.any_rx_sw_if_index;
+      ip_punt_redirect_detail_t detail = {.rx_sw_if_index = ~0,
+	.punt_redirect = *pr
+      };
+      vec_add1 (prs, detail);
+    }
+
+  return prs;
+}
+
 static clib_error_t *
 ip4_punt_redirect_show_cmd (vlib_main_t * vm,
 			    unformat_input_t * main_input,
diff --git a/src/vnet/ip/ip6_punt_drop.c b/src/vnet/ip/ip6_punt_drop.c
index ea8477f..3c839ff 100644
--- a/src/vnet/ip/ip6_punt_drop.c
+++ b/src/vnet/ip/ip6_punt_drop.c
@@ -399,6 +399,46 @@
 };
 /* *INDENT-ON* */
 
+ip_punt_redirect_detail_t *
+ip6_punt_redirect_entries (u32 sw_if_index)
+{
+  ip_punt_redirect_rx_t *pr;
+  ip_punt_redirect_detail_t *prs = 0;
+  u32 rx_sw_if_index;
+
+  vec_foreach_index (rx_sw_if_index,
+		     ip6_punt_redirect_cfg.redirect_by_rx_sw_if_index)
+  {
+    if (sw_if_index == ~0 || sw_if_index == rx_sw_if_index)
+      {
+	pr =
+	  &ip6_punt_redirect_cfg.redirect_by_rx_sw_if_index[rx_sw_if_index];
+	if (NULL != pr && ~0 != pr->tx_sw_if_index)
+	  {
+	    ip_punt_redirect_detail_t detail = {.rx_sw_if_index =
+		rx_sw_if_index,
+	      .punt_redirect = *pr
+	    };
+	    vec_add1 (prs, detail);
+	  }
+      }
+  }
+  if (~0 != ip6_punt_redirect_cfg.any_rx_sw_if_index.tx_sw_if_index)
+    {
+      pr = &ip6_punt_redirect_cfg.any_rx_sw_if_index;
+      if (NULL != pr)
+	{
+	  ip_punt_redirect_detail_t detail = {.rx_sw_if_index =
+	      rx_sw_if_index,
+	    .punt_redirect = *pr
+	  };
+	  vec_add1 (prs, detail);
+	}
+    }
+
+  return prs;
+}
+
 static clib_error_t *
 ip6_punt_redirect_show_cmd (vlib_main_t * vm,
 			    unformat_input_t * main_input,
diff --git a/src/vnet/ip/ip_api.c b/src/vnet/ip/ip_api.c
index 1a31b5a..2f476a3 100644
--- a/src/vnet/ip/ip_api.c
+++ b/src/vnet/ip/ip_api.c
@@ -26,6 +26,7 @@
 #include <vnet/ip/ip.h>
 #include <vnet/ip/ip_neighbor.h>
 #include <vnet/ip/ip6_neighbor.h>
+#include <vnet/ip/ip_punt_drop.h>
 #include <vnet/fib/fib_table.h>
 #include <vnet/fib/fib_api.h>
 #include <vnet/dpo/drop_dpo.h>
@@ -110,7 +111,9 @@
   ip_source_and_port_range_check_interface_add_del)                     \
 _(IP_REASSEMBLY_SET, ip_reassembly_set)                                 \
 _(IP_REASSEMBLY_GET, ip_reassembly_get)                                 \
-_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable)
+_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable)           \
+_(IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump)
+
 
 extern void stats_dslock_with_hint (int hint, int tag);
 extern void stats_dsunlock (void);
@@ -615,40 +618,40 @@
 {
   vl_api_ip_punt_redirect_reply_t *rmp;
   int rv = 0;
+  ip46_type_t ipv;
+  ip46_address_t nh;
 
+  if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
+    goto bad_sw_if_index;
+
+  ipv = ip_address_decode (&mp->punt.nh, &nh);
   if (mp->is_add)
     {
-      ip46_address_t nh;
-
-      clib_memset (&nh, 0, sizeof (nh));
-
-      if (mp->is_ip6)
+      if (ipv == IP46_TYPE_IP6)
 	{
-	  memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
-
-	  ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
-				 ntohl (mp->tx_sw_if_index), &nh);
+	  ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
+				 ntohl (mp->punt.tx_sw_if_index), &nh);
 	}
-      else
+      else if (ipv == IP46_TYPE_IP4)
 	{
-	  memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
-
-	  ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
-				 ntohl (mp->tx_sw_if_index), &nh);
+	  ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
+				 ntohl (mp->punt.tx_sw_if_index), &nh);
 	}
     }
   else
     {
-      if (mp->is_ip6)
+      if (ipv == IP46_TYPE_IP6)
 	{
-	  ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
+	  ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
 	}
-      else
+      else if (ipv == IP46_TYPE_IP4)
 	{
-	  ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
+	  ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
 	}
     }
 
+  BAD_SW_IF_INDEX_LABEL;
+
   REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
 }
 
@@ -3309,6 +3312,76 @@
   REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
 }
 
+void
+send_ip_punt_redirect_details (vl_api_registration_t * reg,
+			       u32 context, u32 sw_if_index,
+			       ip_punt_redirect_rx_t * pr, u8 is_ipv6)
+{
+  vl_api_ip_punt_redirect_details_t *mp;
+
+  mp = vl_msg_api_alloc (sizeof (*mp));
+  if (!mp)
+    return;
+
+  clib_memset (mp, 0, sizeof (*mp));
+  mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
+  mp->context = context;
+  mp->punt.rx_sw_if_index = htonl (sw_if_index);
+  mp->punt.tx_sw_if_index = htonl (pr->tx_sw_if_index);
+  if (is_ipv6)
+    {
+      ip_address_encode (&pr->nh, IP46_TYPE_IP6, &mp->punt.nh);
+    }
+  else
+    {
+      ip_address_encode (&pr->nh, IP46_TYPE_IP4, &mp->punt.nh);
+    }
+
+  vl_api_send_msg (reg, (u8 *) mp);
+}
+
+static void
+vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
+{
+  vl_api_registration_t *reg;
+  u32 sw_if_index;
+  int rv __attribute__ ((unused)) = 0;
+
+  sw_if_index = ntohl (mp->sw_if_index);
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  if (~0 != sw_if_index)
+    VALIDATE_SW_IF_INDEX (mp);
+
+  ip_punt_redirect_detail_t *pr, *prs;
+  if (mp->is_ipv6)
+    {
+      prs = ip6_punt_redirect_entries (sw_if_index);
+      /* *INDENT-OFF* */
+      vec_foreach (pr, prs)
+      {
+        send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 1);
+      }
+      /* *INDENT-ON* */
+      vec_free (prs);
+    }
+  else
+    {
+      prs = ip4_punt_redirect_entries (sw_if_index);
+      /* *INDENT-OFF* */
+      vec_foreach (pr, prs)
+      {
+        send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 0);
+      }
+      /* *INDENT-ON* */
+      vec_free (prs);
+    }
+
+  BAD_SW_IF_INDEX_LABEL;
+}
+
 #define vl_msg_name_crc_list
 #include <vnet/ip/ip.api.h>
 #undef vl_msg_name_crc_list
diff --git a/src/vnet/ip/ip_punt_drop.h b/src/vnet/ip/ip_punt_drop.h
index 7ba65e1..ccf88e5 100644
--- a/src/vnet/ip/ip_punt_drop.h
+++ b/src/vnet/ip/ip_punt_drop.h
@@ -245,6 +245,18 @@
   u32 next;
 } ip_punt_redirect_trace_t;
 
+typedef struct ip_punt_redirect_detail_t_
+{
+  /**
+   * the RX interface
+   */
+  u32 rx_sw_if_index;
+  /**
+   * IP punt redirect configuration
+   */
+  ip_punt_redirect_rx_t punt_redirect;
+} ip_punt_redirect_detail_t;
+
 /**
  * Add a punt redirect entry
  */
@@ -258,6 +270,9 @@
 
 extern u8 *format_ip_punt_redirect_trace (u8 * s, va_list * args);
 
+extern ip_punt_redirect_detail_t *ip4_punt_redirect_entries (u32 sw_if_index);
+extern ip_punt_redirect_detail_t *ip6_punt_redirect_entries (u32 sw_if_index);
+
 always_inline u32
 ip_punt_redirect_tx_via_adj (vlib_buffer_t * b0, adj_index_t ai)
 {