L2FIB:CLI/API to flush all non-static entries

added CLI l2fib flush-mac all
added API l2fib_flush_all
flushes all non static l2fib entries on all valid BDs

Change-Id: Ic963c88f4bed56308c03ab43106033132a0e87be
Signed-off-by: Eyal Bari <ebari@cisco.com>
diff --git a/src/vnet/buffer.h b/src/vnet/buffer.h
index 5d1b1c4..ec5e2f7 100644
--- a/src/vnet/buffer.h
+++ b/src/vnet/buffer.h
@@ -176,8 +176,7 @@
       u16 bd_index;		/* bridge-domain index */
       u8 l2_len;		/* ethernet header length */
       u8 shg;			/* split-horizon group */
-      u8 bd_sn;			/* bridge domain seq# */
-      u8 int_sn;		/* interface seq# */
+      u16 l2fib_sn;		/* l2fib bd/int seq_num */
     } l2;
 
     /* l2tpv3 softwire encap, only valid there */
diff --git a/src/vnet/l2/l2.api b/src/vnet/l2/l2.api
index db42d63..e9a1f36 100644
--- a/src/vnet/l2/l2.api
+++ b/src/vnet/l2/l2.api
@@ -76,6 +76,16 @@
   u32 context;
 };
 
+/** \brief L2 FIB flush all  entries
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+*/
+autoreply define l2fib_flush_all
+{
+  u32 client_index;
+  u32 context;
+};
+
 /** \brief L2 FIB flush bridge domain entries
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request
diff --git a/src/vnet/l2/l2_api.c b/src/vnet/l2/l2_api.c
index 8cc7c79..5f371cc 100644
--- a/src/vnet/l2/l2_api.c
+++ b/src/vnet/l2/l2_api.c
@@ -48,6 +48,7 @@
 _(L2_XCONNECT_DUMP, l2_xconnect_dump)                       \
 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                   \
 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                     \
+_(L2FIB_FLUSH_ALL, l2fib_flush_all)                         \
 _(L2FIB_FLUSH_INT, l2fib_flush_int)                         \
 _(L2FIB_FLUSH_BD, l2fib_flush_bd)                           \
 _(L2FIB_ADD_DEL, l2fib_add_del)                             \
@@ -106,11 +107,8 @@
   int rv = 0;
   vl_api_l2_fib_clear_table_reply_t *rmp;
 
-  /* DAW-FIXME: This API should only clear non-static l2fib entries, but
-   *            that is not currently implemented.  When that TODO is fixed
-   *            this call should be changed to pass 1 instead of 0.
-   */
-  l2fib_clear_table (0);
+  /* Clear all MACs including static MACs  */
+  l2fib_clear_table ();
 
   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
 }
@@ -259,6 +257,16 @@
 }
 
 static void
+vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
+{
+  int rv = 0;
+  vl_api_l2fib_flush_all_reply_t *rmp;
+
+  l2fib_flush_all_mac (vlib_get_main ());
+  REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
+}
+
+static void
 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
 {
   int rv = 0;
diff --git a/src/vnet/l2/l2_fib.c b/src/vnet/l2/l2_fib.c
index 028a732..d4207e3 100644
--- a/src/vnet/l2/l2_fib.c
+++ b/src/vnet/l2/l2_fib.c
@@ -54,7 +54,6 @@
 
 l2fib_main_t l2fib_main;
 
-
 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
 u8 *
 format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
@@ -198,7 +197,7 @@
 				   key.fields.bd_index,
 				   result.fields.sw_if_index == ~0
 				   ? -1 : result.fields.sw_if_index,
-				   result.fields.bd_sn, result.fields.int_sn,
+				   result.fields.sn.bd, result.fields.sn.swif,
 				   s, result.fields.static_mac ? "*" : "-",
 				   result.fields.filter ? "*" : "-",
 				   result.fields.bvi ? "*" : "-",
@@ -259,22 +258,14 @@
 
 /* Remove all entries from the l2fib */
 void
-l2fib_clear_table (uint keep_static)
+l2fib_clear_table (void)
 {
   l2fib_main_t *mp = &l2fib_main;
 
-  if (keep_static)
-    {
-      /* TODO: remove only non-static entries */
-    }
-  else
-    {
-      /* Remove all entries */
-      BV (clib_bihash_free) (&mp->mac_table);
-      BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
-			     L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
-    }
-
+  /* Remove all entries */
+  BV (clib_bihash_free) (&mp->mac_table);
+  BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
+			 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
   l2learn_main.global_learn_count = 0;
 }
 
@@ -285,7 +276,7 @@
 clear_l2fib (vlib_main_t * vm,
 	     unformat_input_t * input, vlib_cli_command_t * cmd)
 {
-  l2fib_clear_table (0);
+  l2fib_clear_table ();
   return 0;
 }
 
@@ -308,14 +299,25 @@
 };
 /* *INDENT-ON* */
 
+static inline l2fib_seq_num_t
+l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
+{
+  l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
+  l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
+  /* *INDENT-OFF* */
+  return (l2fib_seq_num_t) {
+    .swif = int_config->seq_num,
+    .bd = bd_config->seq_num,
+  };
+  /* *INDENT-ON* */
+}
 
 /**
  * Add an entry to the l2fib.
  * If the entry already exists then overwrite it
  */
 void
-l2fib_add_entry (u64 mac,
-		 u32 bd_index,
+l2fib_add_entry (u64 mac, u32 bd_index,
 		 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
 {
   l2fib_entry_key_t key;
@@ -334,14 +336,7 @@
   result.fields.filter = filter_mac;
   result.fields.bvi = bvi_mac;
   if (!static_mac)
-    {
-      l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
-      l2_bridge_domain_t *bd_config =
-	vec_elt_at_index (l2input_main.bd_configs,
-			  bd_index);
-      result.fields.int_sn = int_config->seq_num;
-      result.fields.bd_sn = bd_config->seq_num;
-    }
+    result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
 
   kv.key = key.raw;
   kv.value = result.raw;
@@ -620,8 +615,8 @@
  * Delete an entry from the l2fib.
  * Return 0 if the entry was deleted, or 1 if it was not found
  */
-u32
-l2fib_del_entry (u64 mac, u32 bd_index)
+static u32
+l2fib_del_entry_by_key (u64 raw_key)
 {
 
   l2fib_entry_result_t result;
@@ -629,7 +624,7 @@
   BVT (clib_bihash_kv) kv;
 
   /* set up key */
-  kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
+  kv.key = raw_key;
 
   if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
     return 1;
@@ -651,6 +646,16 @@
 }
 
 /**
+ * Delete an entry from the l2fib.
+ * Return 0 if the entry was deleted, or 1 if it was not found
+ */
+u32
+l2fib_del_entry (u64 mac, u32 bd_index)
+{
+  return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
+}
+
+/**
  * Delete an entry from the L2FIB.
  * The CLI format is:
  *    l2fib del <mac> <bd-id>
@@ -735,30 +740,43 @@
 }
 
 /**
-    Flush all learned MACs from an interface
+    Flush all non static MACs from an interface
 */
 void
 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
 {
-  l2_input_config_t *int_config;
-  int_config = l2input_intf_config (sw_if_index);
+  l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
   int_config->seq_num += 1;
   l2fib_start_ager_scan (vm);
 }
 
 /**
-    Flush all learned MACs in a bridge domain
+    Flush all non static MACs in a bridge domain
 */
 void
 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
 {
-  l2_bridge_domain_t *bd_config;
-  bd_config = l2input_bd_config (bd_index);
+  l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
   bd_config->seq_num += 1;
   l2fib_start_ager_scan (vm);
 }
 
 /**
+    Flush all non static MACs - flushes all valid BDs
+*/
+void
+l2fib_flush_all_mac (vlib_main_t * vm)
+{
+  l2_bridge_domain_t *bd_config;
+  vec_foreach (bd_config, l2input_main.bd_configs)
+    if (bd_is_valid (bd_config))
+    bd_config->seq_num += 1;
+
+  l2fib_start_ager_scan (vm);
+}
+
+
+/**
     Flush MACs, except static ones, associated with an interface
     The CLI format is:
     l2fib flush-mac interface <if-name>
@@ -784,6 +802,35 @@
   return error;
 }
 
+/**
+    Flush all MACs, except static ones
+    The CLI format is:
+    l2fib flush-mac all
+*/
+static clib_error_t *
+l2fib_flush_mac_all (vlib_main_t * vm,
+		     unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  l2fib_flush_all_mac (vm);
+  return 0;
+}
+
+/*?
+ * This command kick off ager to delete all existing MAC Address entries,
+ * except static ones, associated with an interface from the L2 FIB table.
+ *
+ * @cliexpar
+ * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
+ * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
+?*/
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
+  .path = "l2fib flush-mac all",
+  .short_help = "l2fib flush-mac all",
+  .function = l2fib_flush_mac_all,
+};
+/* *INDENT-ON* */
+
 /*?
  * This command kick off ager to delete all existing MAC Address entries,
  * except static ones, associated with an interface from the L2 FIB table.
@@ -872,17 +919,8 @@
 {
   uword event_type, *event_data = 0;
   l2fib_main_t *msm = &l2fib_main;
-  l2_input_config_t *int_config;
-  l2_bridge_domain_t *bd_config;
-  BVT (clib_bihash) * h = &msm->mac_table;
-  clib_bihash_bucket_t *b;
-  BVT (clib_bihash_value) * v;
-  l2fib_entry_key_t key;
-  l2fib_entry_result_t result;
-  int i, j, k;
   bool enabled = 0;
   f64 start_time, last_run_duration = 0, t;
-  i16 delta;
 
   while (1)
     {
@@ -911,6 +949,9 @@
 	  ASSERT (0);
 	}
       last_run_duration = start_time = vlib_time_now (vm);
+
+      BVT (clib_bihash) * h = &msm->mac_table;
+      int i, j, k;
       for (i = 0; i < h->nbuckets; i++)
 	{
 	  /* Allow no more than 10us without a pause */
@@ -923,20 +964,22 @@
 
 	  if (i < (h->nbuckets - 3))
 	    {
-	      b = &h->buckets[i + 3];
+	      clib_bihash_bucket_t *b = &h->buckets[i + 3];
 	      CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
 	      b = &h->buckets[i + 1];
 	      if (b->offset)
 		{
-		  v = BV (clib_bihash_get_value) (h, b->offset);
+		  BVT (clib_bihash_value) * v =
+		    BV (clib_bihash_get_value) (h, b->offset);
 		  CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
 		}
 	    }
 
-	  b = &h->buckets[i];
+	  clib_bihash_bucket_t *b = &h->buckets[i];
 	  if (b->offset == 0)
 	    continue;
-	  v = BV (clib_bihash_get_value) (h, b->offset);
+	  BVT (clib_bihash_value) * v =
+	    BV (clib_bihash_get_value) (h, b->offset);
 	  for (j = 0; j < (1 << b->log2_pages); j++)
 	    {
 	      for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
@@ -944,37 +987,32 @@
 		  if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
 		    continue;
 
-		  key.raw = v->kvp[k].key;
-		  result.raw = v->kvp[k].value;
+		  l2fib_entry_key_t key = {.raw = v->kvp[k].key };
+		  l2fib_entry_result_t result = {.raw = v->kvp[k].value };
 
 		  if (result.fields.static_mac)
 		    continue;
 
-		  int_config =
-		    l2input_intf_config (result.fields.sw_if_index);
-		  bd_config =
-		    vec_elt_at_index (l2input_main.bd_configs,
-				      key.fields.bd_index);
-
-		  if ((result.fields.int_sn != int_config->seq_num) ||
-		      (result.fields.bd_sn != bd_config->seq_num))
+		  u32 bd_index = key.fields.bd_index;
+		  u32 sw_if_index = result.fields.sw_if_index;
+		  u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
+		  if (result.fields.sn.as_u16 != sn)
 		    {
-		      void *p = &key.fields.mac;
-		      l2fib_del_entry (*(u64 *) p, key.fields.bd_index);
+		      l2fib_del_entry_by_key (key.raw);
 		      continue;
 		    }
+		  l2_bridge_domain_t *bd_config =
+		    vec_elt_at_index (l2input_main.bd_configs, bd_index);
 
 		  if (bd_config->mac_age == 0)
 		    continue;
 
-		  delta = (u8) (start_time / 60) - result.fields.timestamp;
+		  i16 delta =
+		    (u8) (start_time / 60) - result.fields.timestamp;
 		  delta += delta < 0 ? 256 : 0;
 
 		  if (delta > bd_config->mac_age)
-		    {
-		      void *p = &key.fields.mac;
-		      l2fib_del_entry (*(u64 *) p, key.fields.bd_index);
-		    }
+		    l2fib_del_entry_by_key (key.raw);
 		}
 	      v++;
 	    }
diff --git a/src/vnet/l2/l2_fib.h b/src/vnet/l2/l2_fib.h
index 7e49d74..e571a21 100644
--- a/src/vnet/l2/l2_fib.h
+++ b/src/vnet/l2/l2_fib.h
@@ -50,6 +50,20 @@
 
 STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);
 
+
+typedef struct
+{
+  union
+  {
+    struct
+    {
+      u8 swif;
+      u8 bd;
+    };
+    u16 as_u16;
+  };
+} l2fib_seq_num_t;
+
 /*
  * The l2fib entry results
  */
@@ -66,8 +80,7 @@
       u8 filter:1;		/* drop packets to/from this mac */
       u8 unused1:5;
       u8 timestamp;		/* timestamp for aging */
-      u8 int_sn;		/* interface seq num */
-      u8 bd_sn;			/* bridge domain seq num */
+      l2fib_seq_num_t sn;	/* bd/int seq num */
     } fields;
     u64 raw;
   };
@@ -314,7 +327,7 @@
     }
 }
 
-void l2fib_clear_table (uint keep_static);
+void l2fib_clear_table (void);
 
 void
 l2fib_add_entry (u64 mac,
@@ -329,6 +342,8 @@
 
 void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
 
+void l2fib_flush_all_mac (vlib_main_t * vm);
+
 void
 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
 		  l2fib_entry_result_t ** l2fe_res);
diff --git a/src/vnet/l2/l2_input.c b/src/vnet/l2/l2_input.c
index fe65e69..41a93f5 100644
--- a/src/vnet/l2/l2_input.c
+++ b/src/vnet/l2/l2_input.c
@@ -202,8 +202,14 @@
       /* Get config for the bridge domain interface */
       bd_config = vec_elt_at_index (msm->bd_configs, bd_index0);
 
-      /* Save bridge domain seq_num */
-      vnet_buffer (b0)->l2.bd_sn = bd_config->seq_num;
+      /* Save bridge domain and interface seq_num */
+      /* *INDENT-OFF* */
+      l2fib_seq_num_t sn = {
+        .swif = config->seq_num,
+	.bd = bd_config->seq_num,
+      };
+      /* *INDENT-ON* */
+      vnet_buffer (b0)->l2.l2fib_sn = sn.as_u16;;
 
       /*
        * Process bridge domain feature enables.
@@ -218,9 +224,6 @@
   /* mask out features from bitmap using packet type and bd config */
   feature_bitmap = config->feature_bitmap & feat_mask;
 
-  /* Save interface seq_num */
-  vnet_buffer (b0)->l2.int_sn = config->seq_num;
-
   /* save for next feature graph nodes */
   vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
 
diff --git a/src/vnet/l2/l2_learn.c b/src/vnet/l2/l2_learn.c
index faed0d6..adc5e70 100644
--- a/src/vnet/l2/l2_learn.c
+++ b/src/vnet/l2/l2_learn.c
@@ -141,10 +141,8 @@
       if (PREDICT_FALSE (result0->fields.timestamp != timestamp))
 	result0->fields.timestamp = timestamp;
       if (PREDICT_FALSE
-	  (result0->fields.int_sn != vnet_buffer (b0)->l2.int_sn))
-	result0->fields.int_sn = vnet_buffer (b0)->l2.int_sn;
-      if (PREDICT_FALSE (result0->fields.bd_sn != vnet_buffer (b0)->l2.bd_sn))
-	result0->fields.bd_sn = vnet_buffer (b0)->l2.bd_sn;
+	  (result0->fields.sn.as_u16 != vnet_buffer (b0)->l2.l2fib_sn))
+	result0->fields.sn.as_u16 = vnet_buffer (b0)->l2.l2fib_sn;
     }
   else if (result0->raw == ~0)
     {
@@ -171,8 +169,7 @@
 	  result0->raw = 0;	/* clear all fields */
 	  result0->fields.sw_if_index = sw_if_index0;
 	  result0->fields.timestamp = timestamp;
-	  result0->fields.bd_sn = vnet_buffer (b0)->l2.bd_sn;
-	  result0->fields.int_sn = vnet_buffer (b0)->l2.int_sn;
+	  result0->fields.sn.as_u16 = vnet_buffer (b0)->l2.l2fib_sn;
 	  kv.key = key0->raw;
 	  kv.value = result0->raw;
 
@@ -210,8 +207,7 @@
 	  result0->raw = 0;	/* clear all fields */
 	  result0->fields.sw_if_index = sw_if_index0;
 	  result0->fields.timestamp = timestamp;
-	  result0->fields.bd_sn = vnet_buffer (b0)->l2.bd_sn;
-	  result0->fields.int_sn = vnet_buffer (b0)->l2.int_sn;
+	  result0->fields.sn.as_u16 = vnet_buffer (b0)->l2.l2fib_sn;
 
 	  kv.key = key0->raw;
 	  kv.value = result0->raw;