ip-neighbor: ARP and ND stats per-interface.
[vpp.git] / src / vnet / ip-neighbor / ip4_neighbor.c
index 2a9e267..368703d 100644 (file)
 
 #include <vnet/ip-neighbor/ip4_neighbor.h>
 #include <vnet/ethernet/ethernet.h>
+#include <vnet/util/throttle.h>
+#include <vnet/fib/fib_sas.h>
+#include <vnet/ip/ip_sas.h>
+
+/** ARP throttling */
+static throttle_t arp_throttle;
+
+VLIB_REGISTER_LOG_CLASS (ip4_neighbor_log, static) = {
+  .class_name = "ip4",
+  .subclass_name = "neighbor",
+};
+
+#define log_debug(fmt, ...)                                                   \
+  vlib_log_debug (ip4_neighbor_log.class, fmt, __VA_ARGS__)
 
 void
-ip4_neighbor_probe_dst (const ip_adjacency_t * adj, const ip4_address_t * dst)
+ip4_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
+                       const ip4_address_t *dst)
 {
-  ip_interface_address_t *ia;
-  ip4_address_t *src;
+  ip4_address_t src;
+  adj_index_t ai;
 
-  src = ip4_interface_address_matching_destination
-    (&ip4_main,
-     &adj->sub_type.nbr.next_hop.ip4, adj->rewrite_header.sw_if_index, &ia);
-  if (!src)
-    return;
+  /* any glean will do, it's just for the rewrite */
+  ai = adj_glean_get (FIB_PROTOCOL_IP4, sw_if_index, NULL);
 
-  ip4_neighbor_probe (vlib_get_main (), vnet_get_main (), adj, src, dst);
+  if (ADJ_INDEX_INVALID != ai &&
+      (fib_sas4_get (sw_if_index, dst, &src) ||
+       ip4_sas_by_sw_if_index (sw_if_index, dst, &src)))
+    ip4_neighbor_probe (vlib_get_main (),
+                       vnet_get_main (), adj_get (ai), &src, dst);
 }
 
 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;
   u8 *rewrite, rewrite_len;
+  ip4_address_t tmp;
 
   if (NULL == addr)
     {
-      ip4_main_t *i4m = &ip4_main;
-      addr = ip4_interface_first_address (i4m, sw_if_index, 0);
+      if (fib_sas4_get (sw_if_index, NULL, &tmp) ||
+         ip4_sas_by_sw_if_index (sw_if_index, NULL, &tmp))
+       addr = &tmp;
     }
 
   if (addr)
     {
-      clib_warning ("Sending GARP for IP4 address %U on sw_if_idex %d",
-                   format_ip4_address, addr, sw_if_index);
+      log_debug ("Sending GARP for IP4 address %U on sw_if_idex %d",
+                format_ip4_address, addr, sw_if_index);
 
       /* Form GARP packet for output - Gratuitous ARP is an ARP request packet
          where the interface IP/MAC pair is used for both source and request
@@ -109,6 +126,10 @@ ip4_neighbor_advertise (vlib_main_t * vm,
       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);
     }
 }
 
@@ -118,8 +139,6 @@ ip4_arp_inline (vlib_main_t * vm,
                vlib_frame_t * frame, int is_glean)
 {
   vnet_main_t *vnm = vnet_get_main ();
-  ip4_main_t *im = &ip4_main;
-  ip_lookup_main_t *lm = &im->lookup_main;
   u32 *from, *to_next_drop;
   uword n_left_from, n_left_to_next_drop, next_index;
   u32 thread_index = vm->thread_index;
@@ -128,7 +147,7 @@ ip4_arp_inline (vlib_main_t * vm,
   if (node->flags & VLIB_NODE_FLAG_TRACE)
     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
 
-  seed = throttle_seed (&im->arp_throttle, thread_index, vlib_time_now (vm));
+  seed = throttle_seed (&arp_throttle, thread_index, vlib_time_now (vm));
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
@@ -167,14 +186,15 @@ ip4_arp_inline (vlib_main_t * vm,
              /* resolve the packet's destination */
              ip4_header_t *ip0 = vlib_buffer_get_current (p0);
              resolve0 = ip0->dst_address;
-             src0 = adj0->sub_type.glean.receive_addr.ip4;
+             src0 = adj0->sub_type.glean.rx_pfx.fp_addr.ip4;
            }
          else
            {
              /* resolve the incomplete adj */
              resolve0 = adj0->sub_type.nbr.next_hop.ip4;
              /* Src IP address in ARP header. */
-             if (ip4_src_address_for_packet (lm, sw_if_index0, &src0))
+             if (!fib_sas4_get (sw_if_index0, &resolve0, &src0) &&
+                 !ip4_sas_by_sw_if_index (sw_if_index0, &resolve0, &src0))
                {
                  /* No source address available */
                  p0->error = node->errors[IP4_ARP_ERROR_NO_SOURCE_ADDRESS];
@@ -186,7 +206,7 @@ ip4_arp_inline (vlib_main_t * vm,
          r0 = (u64) resolve0.data_u32 << 32;
          r0 |= sw_if_index0;
 
-         if (throttle_check (&im->arp_throttle, thread_index, r0, seed))
+         if (throttle_check (&arp_throttle, thread_index, r0, seed))
            {
              p0->error = node->errors[IP4_ARP_ERROR_THROTTLED];
              continue;
@@ -310,6 +330,20 @@ arp_notrace_init (vlib_main_t * vm)
 
 VLIB_INIT_FUNCTION (arp_notrace_init);
 
+static clib_error_t *
+ip4_neighbor_main_loop_enter (vlib_main_t * vm)
+{
+  vlib_thread_main_t *tm = &vlib_thread_main;
+  u32 n_vlib_mains = tm->n_vlib_mains;
+
+  throttle_init (&arp_throttle, n_vlib_mains, 1e-3);
+
+  return (NULL);
+}
+
+VLIB_MAIN_LOOP_ENTER_FUNCTION (ip4_neighbor_main_loop_enter);
+
+
 /*
  * fd.io coding-style-patch-verification: ON
  *