ip-neighbor: ARP and ND stats per-interface.

Type: feature

stats of the like from:
  https://datatracker.ietf.org/doc/html/draft-ietf-rtgwg-arp-yang-model-03#section-4

Signed-off-by: Neale Ranns <neale@graphiant.com>
Change-Id: Icb1bf4f6f7e6ccc2f44b0008d4774b61cae96184
diff --git a/src/vnet/ip-neighbor/ip4_neighbor.c b/src/vnet/ip-neighbor/ip4_neighbor.c
index cf0e81a..368703d 100644
--- a/src/vnet/ip-neighbor/ip4_neighbor.c
+++ b/src/vnet/ip-neighbor/ip4_neighbor.c
@@ -55,7 +55,8 @@
   vlib_log_debug (ip4_neighbor_log.class, fmt, __VA_ARGS__)
 
 void
-ip4_neighbor_probe_dst (u32 sw_if_index, const ip4_address_t * dst)
+ip4_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
+			const ip4_address_t *dst)
 {
   ip4_address_t src;
   adj_index_t ai;
@@ -71,9 +72,8 @@
 }
 
 void
-ip4_neighbor_advertise (vlib_main_t * vm,
-			vnet_main_t * vnm,
-			u32 sw_if_index, const ip4_address_t * addr)
+ip4_neighbor_advertise (vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index,
+			u32 thread_index, const ip4_address_t *addr)
 {
   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
   ip4_main_t *i4m = &ip4_main;
@@ -126,6 +126,10 @@
       to_next[0] = bi;
       f->n_vectors = 1;
       vlib_put_frame_to_node (vm, hi->output_node_index, f);
+
+      vlib_increment_simple_counter (
+	&ip_neighbor_counters[AF_IP4].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_GRAT],
+	thread_index, sw_if_index, 1);
     }
 }
 
diff --git a/src/vnet/ip-neighbor/ip4_neighbor.h b/src/vnet/ip-neighbor/ip4_neighbor.h
index c330dfa..8ace8d1 100644
--- a/src/vnet/ip-neighbor/ip4_neighbor.h
+++ b/src/vnet/ip-neighbor/ip4_neighbor.h
@@ -18,19 +18,18 @@
 
 #include <vnet/ip/ip.h>
 #include <vnet/ethernet/arp_packet.h>
+#include <vnet/ip-neighbor/ip_neighbor_types.h>
 
-extern void ip4_neighbor_probe_dst (u32 sw_if_index,
-				    const ip4_address_t * dst);
-extern void ip4_neighbor_advertise (vlib_main_t * vm,
-				    vnet_main_t * vnm,
-				    u32 sw_if_index,
-				    const ip4_address_t * addr);
+extern void ip4_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
+				    const ip4_address_t *dst);
+extern void ip4_neighbor_advertise (vlib_main_t *vm, vnet_main_t *vnm,
+				    u32 sw_if_index, u32 thread_index,
+				    const ip4_address_t *addr);
 
 always_inline vlib_buffer_t *
-ip4_neighbor_probe (vlib_main_t * vm,
-		    vnet_main_t * vnm,
-		    const ip_adjacency_t * adj0,
-		    const ip4_address_t * src, const ip4_address_t * dst)
+ip4_neighbor_probe (vlib_main_t *vm, vnet_main_t *vnm,
+		    const ip_adjacency_t *adj0, const ip4_address_t *src,
+		    const ip4_address_t *dst)
 {
   vnet_hw_interface_t *hw_if0;
   ethernet_arp_header_t *h0;
@@ -73,6 +72,10 @@
     vlib_put_frame_to_node (vm, hw_if0->output_node_index, f);
   }
 
+  vlib_increment_simple_counter (
+    &ip_neighbor_counters[AF_IP4].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_REQUEST],
+    vm->thread_index, adj0->rewrite_header.sw_if_index, 1);
+
   return b0;
 }
 
diff --git a/src/vnet/ip-neighbor/ip6_neighbor.c b/src/vnet/ip-neighbor/ip6_neighbor.c
index cf14954..a59495e 100644
--- a/src/vnet/ip-neighbor/ip6_neighbor.c
+++ b/src/vnet/ip-neighbor/ip6_neighbor.c
@@ -31,20 +31,20 @@
 #define log_debug(fmt, ...)                                                   \
   vlib_log_debug (ip6_neighbor_log.class, fmt, __VA_ARGS__)
 void
-ip6_neighbor_probe_dst (u32 sw_if_index, const ip6_address_t * dst)
+ip6_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
+			const ip6_address_t *dst)
 {
   ip6_address_t src;
 
   if (fib_sas6_get (sw_if_index, dst, &src) ||
       ip6_sas_by_sw_if_index (sw_if_index, dst, &src))
-    ip6_neighbor_probe (vlib_get_main (), vnet_get_main (),
-			sw_if_index, &src, dst);
+    ip6_neighbor_probe (vlib_get_main (), vnet_get_main (), sw_if_index,
+			thread_index, &src, dst);
 }
 
 void
-ip6_neighbor_advertise (vlib_main_t * vm,
-			vnet_main_t * vnm,
-			u32 sw_if_index, const ip6_address_t * addr)
+ip6_neighbor_advertise (vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index,
+			u32 thread_index, const ip6_address_t *addr)
 {
   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
   ip6_main_t *i6m = &ip6_main;
@@ -105,6 +105,10 @@
       to_next[0] = bi;
       f->n_vectors = 1;
       vlib_put_frame_to_node (vm, hi->output_node_index, f);
+
+      vlib_increment_simple_counter (
+	&ip_neighbor_counters[AF_IP6].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_GRAT],
+	thread_index, sw_if_index, 1);
     }
 }
 
@@ -222,8 +226,8 @@
 	      continue;
 	    }
 
-	  b0 = ip6_neighbor_probe (vm, vnm, sw_if_index0,
-				   &src, &ip0->dst_address);
+	  b0 = ip6_neighbor_probe (vm, vnm, sw_if_index0, thread_index, &src,
+				   &ip0->dst_address);
 
 	  if (PREDICT_TRUE (NULL != b0))
 	    {
diff --git a/src/vnet/ip-neighbor/ip6_neighbor.h b/src/vnet/ip-neighbor/ip6_neighbor.h
index ad2ace2..c6e718d 100644
--- a/src/vnet/ip-neighbor/ip6_neighbor.h
+++ b/src/vnet/ip-neighbor/ip6_neighbor.h
@@ -25,23 +25,22 @@
 #include <vnet/ip/icmp46_packet.h>
 #include <vnet/ethernet/ethernet.h>
 #include <vnet/adj/adj_internal.h>
+#include <vnet/ip-neighbor/ip_neighbor_types.h>
 
 /* Template used to generate IP6 neighbor solicitation packets. */
 extern vlib_packet_template_t ip6_neighbor_packet_template;
 
-extern void ip6_neighbor_advertise (vlib_main_t * vm,
-				    vnet_main_t * vnm,
-				    u32 sw_if_index,
-				    const ip6_address_t * addr);
+extern void ip6_neighbor_advertise (vlib_main_t *vm, vnet_main_t *vnm,
+				    u32 sw_if_index, u32 thread_index,
+				    const ip6_address_t *addr);
 
-extern void ip6_neighbor_probe_dst (u32 sw_if_index,
-				    const ip6_address_t * dst);
+extern void ip6_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
+				    const ip6_address_t *dst);
 
 always_inline vlib_buffer_t *
-ip6_neighbor_probe (vlib_main_t * vm,
-		    vnet_main_t * vnm,
-		    u32 sw_if_index,
-		    const ip6_address_t * src, const ip6_address_t * dst)
+ip6_neighbor_probe (vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index,
+		    u32 thread_index, const ip6_address_t *src,
+		    const ip6_address_t *dst)
 {
   icmp6_neighbor_solicitation_header_t *h0;
   vnet_hw_interface_t *hw_if0;
@@ -104,6 +103,10 @@
     vlib_put_frame_to_node (vm, adj->ia_node_index, f);
   }
 
+  vlib_increment_simple_counter (
+    &ip_neighbor_counters[AF_IP6].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_REQUEST],
+    thread_index, sw_if_index, 1);
+
   return b0;
 }
 
diff --git a/src/vnet/ip-neighbor/ip_neighbor.c b/src/vnet/ip-neighbor/ip_neighbor.c
index 6ba191a..b2d4fa0 100644
--- a/src/vnet/ip-neighbor/ip_neighbor.c
+++ b/src/vnet/ip-neighbor/ip_neighbor.c
@@ -27,6 +27,74 @@
 #include <vnet/fib/fib_table.h>
 #include <vnet/adj/adj_mcast.h>
 
+ip_neighbor_counters_t ip_neighbor_counters[] =
+{
+ [AF_IP4] = {
+   .ipnc = {
+     [VLIB_RX] = {
+        [IP_NEIGHBOR_CTR_REPLY] = {
+          .name = "arp-rx-replies",
+          .stat_segment_name = "/net/arp/rx/replies",
+        },
+        [IP_NEIGHBOR_CTR_REQUEST] = {
+          .name = "arp-rx-requests",
+          .stat_segment_name = "/net/arp/rx/requests",
+        },
+        [IP_NEIGHBOR_CTR_GRAT] = {
+          .name = "arp-rx-gratuitous",
+          .stat_segment_name = "/net/arp/rx/gratuitous",
+        },
+      },
+      [VLIB_TX] = {
+        [IP_NEIGHBOR_CTR_REPLY] = {
+          .name = "arp-tx-replies",
+          .stat_segment_name = "/net/arp/tx/replies",
+        },
+        [IP_NEIGHBOR_CTR_REQUEST] = {
+          .name = "arp-tx-requests",
+          .stat_segment_name = "/net/arp/tx/requests",
+        },
+        [IP_NEIGHBOR_CTR_GRAT] = {
+          .name = "arp-tx-gratuitous",
+          .stat_segment_name = "/net/arp/tx/gratuitous",
+        },
+      },
+            },
+ },
+ [AF_IP6] = {
+   .ipnc = {
+     [VLIB_RX] = {
+        [IP_NEIGHBOR_CTR_REPLY] = {
+          .name = "ip6-nd-rx-replies",
+          .stat_segment_name = "/net/ip6-nd/rx/replies",
+        },
+        [IP_NEIGHBOR_CTR_REQUEST] = {
+          .name = "ip6-nd-rx-requests",
+          .stat_segment_name = "/net/ip6-nd/rx/requests",
+        },
+        [IP_NEIGHBOR_CTR_GRAT] = {
+          .name = "ip6-nd-rx-gratuitous",
+          .stat_segment_name = "/net/ip6-nd/rx/gratuitous",
+        },
+      },
+      [VLIB_TX] = {
+        [IP_NEIGHBOR_CTR_REPLY] = {
+          .name = "ip6-nd-tx-replies",
+          .stat_segment_name = "/net/ip6-nd/tx/replies",
+        },
+        [IP_NEIGHBOR_CTR_REQUEST] = {
+          .name = "ip6-nd-tx-requests",
+          .stat_segment_name = "/net/ip6-nd/tx/requests",
+        },
+        [IP_NEIGHBOR_CTR_GRAT] = {
+          .name = "ip6-nd-tx-gratuitous",
+          .stat_segment_name = "/net/ip6-nd/tx/gratuitous",
+        },
+      },
+    },
+ },
+};
+
 /** Pool for All IP neighbors */
 static ip_neighbor_t *ip_neighbor_pool;
 
@@ -1017,8 +1085,8 @@
 }
 
 void
-ip_neighbor_probe_dst (u32 sw_if_index,
-		       ip_address_family_t af, const ip46_address_t * dst)
+ip_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
+		       ip_address_family_t af, const ip46_address_t *dst)
 {
   if (!vnet_sw_interface_is_admin_up (vnet_get_main (), sw_if_index))
     return;
@@ -1026,10 +1094,10 @@
   switch (af)
     {
     case AF_IP6:
-      ip6_neighbor_probe_dst (sw_if_index, &dst->ip6);
+      ip6_neighbor_probe_dst (sw_if_index, thread_index, &dst->ip6);
       break;
     case AF_IP4:
-      ip4_neighbor_probe_dst (sw_if_index, &dst->ip4);
+      ip4_neighbor_probe_dst (sw_if_index, thread_index, &dst->ip4);
       break;
     }
 }
@@ -1038,6 +1106,7 @@
 ip_neighbor_probe (const ip_adjacency_t * adj)
 {
   ip_neighbor_probe_dst (adj->rewrite_header.sw_if_index,
+			 vlib_get_thread_index (),
 			 ip_address_family_from_fib_proto (adj->ia_nh_proto),
 			 &adj->sub_type.nbr.next_hop);
 }
@@ -1291,8 +1360,8 @@
  * Remove any arp entries associated with the specified interface
  */
 static clib_error_t *
-ip_neighbor_delete_sw_interface (vnet_main_t * vnm,
-				 u32 sw_if_index, u32 is_add)
+ip_neighbor_add_del_sw_interface (vnet_main_t *vnm, u32 sw_if_index,
+				  u32 is_add)
 {
   IP_NEIGHBOR_DBG ("interface-change: %U  %s",
 		   format_vnet_sw_if_index_name, vnet_get_main (),
@@ -1305,10 +1374,16 @@
       FOR_EACH_IP_ADDRESS_FAMILY (af) ip_neighbor_flush (af, sw_if_index);
     }
 
+  if (is_add)
+    {
+      ip_neighbor_alloc_ctr (&ip_neighbor_counters[AF_IP4], sw_if_index);
+      ip_neighbor_alloc_ctr (&ip_neighbor_counters[AF_IP6], sw_if_index);
+    }
+
   return (NULL);
 }
 
-VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip_neighbor_delete_sw_interface);
+VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip_neighbor_add_del_sw_interface);
 
 typedef struct ip_neighbor_walk_covered_ctx_t_
 {
@@ -1519,8 +1594,9 @@
 	}
       else
 	{
-	  ip_neighbor_probe_dst (ip_neighbor_get_sw_if_index (ipn),
-				 af, &ip_addr_46 (&ipn->ipn_key->ipnk_ip));
+	  ip_neighbor_probe_dst (ip_neighbor_get_sw_if_index (ipn), af,
+				 vlib_get_thread_index (),
+				 &ip_addr_46 (&ipn->ipn_key->ipnk_ip));
 
 	  ipn->ipn_n_probes++;
 	  *wait = 1;
@@ -1745,6 +1821,47 @@
   return error;
 }
 
+static void
+ip_neighbor_stats_show_one (vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index)
+{
+  vlib_cli_output (vm, "  %U", format_vnet_sw_if_index_name, vnm, sw_if_index);
+  vlib_cli_output (vm, "    arp:%U", format_ip_neighbor_counters,
+		   &ip_neighbor_counters[AF_IP4], sw_if_index);
+  vlib_cli_output (vm, "    nd: %U", format_ip_neighbor_counters,
+		   &ip_neighbor_counters[AF_IP6], sw_if_index);
+}
+
+static walk_rc_t
+ip_neighbor_stats_show_cb (vnet_main_t *vnm, vnet_sw_interface_t *si,
+			   void *ctx)
+{
+  ip_neighbor_stats_show_one (ctx, vnm, si->sw_if_index);
+
+  return (WALK_CONTINUE);
+}
+
+static clib_error_t *
+ip_neighbor_stats_show (vlib_main_t *vm, unformat_input_t *input,
+			vlib_cli_command_t *cmd)
+{
+  vnet_main_t *vnm;
+  u32 sw_if_index;
+
+  vnm = vnet_get_main ();
+  sw_if_index = ~0;
+  (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
+
+  if (~0 == sw_if_index)
+    {
+      vnet_sw_interface_walk (vnm, ip_neighbor_stats_show_cb, vm);
+    }
+  else
+    {
+      ip_neighbor_stats_show_one (vm, vnm, sw_if_index);
+    }
+  return (NULL);
+}
+
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (show_ip_neighbor_cfg_cmd_node, static) = {
   .path = "show ip neighbor-config",
@@ -1757,6 +1874,11 @@
   .short_help = "set ip neighbor-config ip4|ip6 [limit <limit>] [age <age>] "
 		"[recycle|norecycle]",
 };
+VLIB_CLI_COMMAND (show_ip_neighbor_stats_cmd_node, static) = {
+  .path = "show ip neighbor-stats",
+  .function = ip_neighbor_stats_show,
+  .short_help = "show ip neighbor-stats [interface]",
+};
 /* *INDENT-ON* */
 
 static clib_error_t *
diff --git a/src/vnet/ip-neighbor/ip_neighbor.h b/src/vnet/ip-neighbor/ip_neighbor.h
index 1702801..8c07df8 100644
--- a/src/vnet/ip-neighbor/ip_neighbor.h
+++ b/src/vnet/ip-neighbor/ip_neighbor.h
@@ -54,9 +54,9 @@
 extern void ip_neighbor_update (vnet_main_t * vnm, adj_index_t ai);
 
 extern void ip_neighbor_probe (const ip_adjacency_t * adj);
-extern void ip_neighbor_probe_dst (u32 sw_if_index,
+extern void ip_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
 				   ip_address_family_t af,
-				   const ip46_address_t * ip);
+				   const ip46_address_t *ip);
 
 extern void ip_neighbor_mark (ip_address_family_t af);
 extern void ip_neighbor_sweep (ip_address_family_t af);
@@ -112,7 +112,6 @@
 extern void ip_neighbor_register (ip_address_family_t af,
 				  const ip_neighbor_vft_t * vft);
 
-
 #endif /* __INCLUDE_IP_NEIGHBOR_H__ */
 
 /*
diff --git a/src/vnet/ip-neighbor/ip_neighbor_types.c b/src/vnet/ip-neighbor/ip_neighbor_types.c
index 76fbc5a..39039a4 100644
--- a/src/vnet/ip-neighbor/ip_neighbor_types.c
+++ b/src/vnet/ip-neighbor/ip_neighbor_types.c
@@ -83,6 +83,53 @@
 		  ipn->ipn_key->ipnk_sw_if_index));
 }
 
+static void
+ip_neighbor_alloc_one_ctr (ip_neighbor_counters_t *ctr, vlib_dir_t dir,
+			   ip_neighbor_counter_type_t type, u32 sw_if_index)
+{
+  vlib_validate_simple_counter (&(ctr->ipnc[dir][type]), sw_if_index);
+  vlib_zero_simple_counter (&(ctr->ipnc[dir][type]), sw_if_index);
+}
+
+void
+ip_neighbor_alloc_ctr (ip_neighbor_counters_t *ctr, u32 sw_if_index)
+{
+  ip_neighbor_counter_type_t type;
+  vlib_dir_t dir;
+
+  FOREACH_VLIB_DIR (dir)
+  {
+    FOREACH_IP_NEIGHBOR_CTR (type)
+    {
+      ip_neighbor_alloc_one_ctr (ctr, dir, type, sw_if_index);
+    }
+  }
+}
+
+u8 *
+format_ip_neighbor_counters (u8 *s, va_list *args)
+{
+  ip_neighbor_counters_t *ctr = va_arg (*args, ip_neighbor_counters_t *);
+  u32 sw_if_index = va_arg (*args, u32);
+  vlib_dir_t dir;
+
+  FOREACH_VLIB_DIR (dir)
+  {
+    s = format (s, " %U:[", format_vlib_rx_tx, dir);
+
+#define _(a, b)                                                               \
+  s = format (s, "%s:%lld ", b,                                               \
+	      vlib_get_simple_counter (&ctr->ipnc[dir][IP_NEIGHBOR_CTR_##a],  \
+				       sw_if_index));
+    foreach_ip_neighbor_counter_type
+#undef _
+
+      s = format (s, "]");
+  }
+
+  return (s);
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vnet/ip-neighbor/ip_neighbor_types.h b/src/vnet/ip-neighbor/ip_neighbor_types.h
index 2eb8fd0..d7e818b 100644
--- a/src/vnet/ip-neighbor/ip_neighbor_types.h
+++ b/src/vnet/ip-neighbor/ip_neighbor_types.h
@@ -120,7 +120,37 @@
 
 extern void ip_neighbor_free (ip_neighbor_t * ipn);
 
+/**
+ * Keep RX and TX counts per-AF
+ */
+#define foreach_ip_neighbor_counter_type                                      \
+  _ (REPLY, "reply")                                                          \
+  _ (REQUEST, "request")                                                      \
+  _ (GRAT, "gratuitous")
 
+typedef enum ip_neighbor_counter_type_t_
+{
+#define _(a, b) IP_NEIGHBOR_CTR_##a,
+  foreach_ip_neighbor_counter_type
+#undef _
+} ip_neighbor_counter_type_t;
+
+#define N_IP_NEIGHBOR_CTRS (IP_NEIGHBOR_CTR_GRAT + 1)
+
+#define FOREACH_IP_NEIGHBOR_CTR(_type)                                        \
+  for (_type = 0; _type < N_IP_NEIGHBOR_CTRS; _type++)
+
+typedef struct ip_neighbor_counters_t_
+{
+  vlib_simple_counter_main_t ipnc[VLIB_N_DIR][N_IP_NEIGHBOR_CTRS];
+} ip_neighbor_counters_t;
+
+extern u8 *format_ip_neighbor_counters (u8 *s, va_list *args);
+
+extern void ip_neighbor_alloc_ctr (ip_neighbor_counters_t *ctr,
+				   u32 sw_if_index);
+
+extern ip_neighbor_counters_t ip_neighbor_counters[N_AF];
 
 #endif /* __INCLUDE_IP_NEIGHBOR_H__ */