Implement MAC Flush for BD or Interface from the L2FIB

Allow non-static MACs in the L2FIB which is associated with an
interface or a bridge domain (BD) be flushed. MAC flush are
initiated automatically when an interface is removed from a BD
or when a BD is deleted. MAC flush can also be invoked manually
via the following CLI:
 l2fib mac-flush interface <if-name>
 l2fib mac-flush bridge-domain <bd-id>

Change-Id: Ie33243622834810a765f48ebcd22bdb8e8fc87a4
Signed-off-by: John Lo <loj@cisco.com>
diff --git a/src/vnet/buffer.h b/src/vnet/buffer.h
index f08b4fc..ea3ce09 100644
--- a/src/vnet/buffer.h
+++ b/src/vnet/buffer.h
@@ -169,9 +169,11 @@
     struct
     {
       u32 feature_bitmap;
-      u16 bd_index;		// bridge-domain index
-      u8 l2_len;		// ethernet header length
-      u8 shg;			// split-horizon group
+      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# */
     } l2;
 
     /* l2tpv3 softwire encap, only valid there */
diff --git a/src/vnet/l2/l2_bd.c b/src/vnet/l2/l2_bd.c
index f741b64..6c01368 100644
--- a/src/vnet/l2/l2_bd.c
+++ b/src/vnet/l2/l2_bd.c
@@ -115,6 +115,8 @@
   l2input_main.bd_configs[bd_index].bd_id = ~0;
   l2input_main.bd_configs[bd_index].feature_bitmap = 0;
 
+  l2fib_flush_bd_mac (vlib_get_main (), bd_index);
+
   return 0;
 }
 
@@ -900,7 +902,6 @@
   u32 bd_index = ~0;
   l2_bridge_domain_t *bd_config;
   u32 start, end;
-  u32 printed;
   u32 detail = 0;
   u32 intf = 0;
   u32 arp = 0;
@@ -942,7 +943,8 @@
     }
 
   /* Show all bridge-domains that have been initialized */
-  printed = 0;
+  u32 printed = 0;
+  u8 *as = 0;
   for (bd_index = start; bd_index < end; bd_index++)
     {
       bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
@@ -952,26 +954,32 @@
 	    {
 	      printed = 1;
 	      vlib_cli_output (vm,
-			       "%=5s %=7s %=10s %=10s %=10s %=10s %=10s %=14s",
-			       "ID", "Index", "Learning", "U-Forwrd",
-			       "UU-Flood", "Flooding", "ARP-Term",
+			       "%=5s %=7s %=4s %=9s %=9s %=9s %=9s %=9s %=9s %=9s",
+			       "ID", "Index", "BSN", "Age(min)", "Learning",
+			       "U-Forwrd", "UU-Flood", "Flooding", "ARP-Term",
 			       "BVI-Intf");
 	    }
 
+	  if (bd_config->mac_age)
+	    as = format (as, "%d", bd_config->mac_age);
+	  else
+	    as = format (as, "off");
 	  vlib_cli_output (vm,
-			   "%=5d %=7d %=10s %=10s %=10s %=10s %=10s %=14U",
-			   bd_config->bd_id, bd_index,
+			   "%=5d %=7d %=4d %=9v %=9s %=9s %=9s %=9s %=9s %=9U",
+			   bd_config->bd_id, bd_index, bd_config->seq_num, as,
 			   bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
 			   "on" : "off",
-			   bd_config->feature_bitmap & L2INPUT_FEAT_FWD ? "on"
-			   : "off",
+			   bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
+			   "on" : "off",
 			   bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD ?
 			   "on" : "off",
 			   bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
 			   "on" : "off",
 			   bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
-			   "on" : "off", format_vnet_sw_if_index_name_with_NA,
+			   "on" : "off",
+			   format_vnet_sw_if_index_name_with_NA,
 			   vnm, bd_config->bvi_sw_if_index);
+	  vec_reset_length (as);
 
 	  if (detail || intf)
 	    {
@@ -981,19 +989,21 @@
 	      {
 		l2_flood_member_t *member =
 		  vec_elt_at_index (bd_config->members, i);
+		l2_input_config_t *int_config =
+		  l2input_intf_config (member->sw_if_index);
 		u32 vtr_opr, dot1q, tag1, tag2;
 		if (i == 0)
 		  {
-		    vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=9s%=30s",
-				     "Interface", "Index", "SHG", "BVI",
-				     "TxFlood", "VLAN-Tag-Rewrite");
+		    vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
+				     "Interface", "If-idx", "ISN", "SHG",
+				     "BVI", "TxFlood", "VLAN-Tag-Rewrite");
 		  }
 		l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
 			   &tag1, &tag2);
-		vlib_cli_output (vm, "%=30U%=7d%=5d%=5s%=9s%=30U",
+		vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
 				 format_vnet_sw_if_index_name, vnm,
 				 member->sw_if_index, member->sw_if_index,
-				 member->shg,
+				 int_config->seq_num, member->shg,
 				 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
 				 "-", i < bd_config->flood_count ? "*" : "-",
 				 format_vtr, vtr_opr, dot1q, tag1, tag2);
@@ -1027,6 +1037,7 @@
 	    }
 	}
     }
+  vec_free (as);
 
   if (!printed)
     {
diff --git a/src/vnet/l2/l2_bd.h b/src/vnet/l2/l2_bd.h
index 4bb9bc9..5c2502d 100644
--- a/src/vnet/l2/l2_bd.h
+++ b/src/vnet/l2/l2_bd.h
@@ -86,6 +86,9 @@
   /* mac aging */
   u8 mac_age;
 
+  /* sequence number for bridge domain based flush of MACs */
+  u8 seq_num;
+
 } l2_bridge_domain_t;
 
 /* Return 1 if bridge domain has been initialized */
diff --git a/src/vnet/l2/l2_fib.c b/src/vnet/l2/l2_fib.c
index d34836e..fadd79e 100644
--- a/src/vnet/l2/l2_fib.c
+++ b/src/vnet/l2/l2_fib.c
@@ -168,10 +168,10 @@
 		{
 		  first_entry = 0;
 		  vlib_cli_output (vm,
-				   "%=19s%=7s%=30s%=7s%=8s%=8s%=5s%=16s",
-				   "Mac Address", "BD Idx", "Interface",
-				   "Index", "static", "filter", "bvi",
-				   "Mac Age (min)");
+				   "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
+				   "Mac-Address", "BD-Idx", "If-Idx",
+				   "BSN-ISN", "Age(min)", "static", "filter",
+				   "bvi", "Interface-Name");
 		}
 
 	      key.raw = v->kvp[k].key;
@@ -183,26 +183,27 @@
 		  bd_config = vec_elt_at_index (l2input_main.bd_configs,
 						key.fields.bd_index);
 
-		  if (bd_config->mac_age)
+		  if (bd_config->mac_age && !result.fields.static_mac)
 		    {
 		      i16 delta = now - result.fields.timestamp;
 		      delta += delta < 0 ? 256 : 0;
 		      s = format (s, "%d", delta);
 		    }
 		  else
-		    s = format (s, "disabled");
+		    s = format (s, "-");
 
 		  vlib_cli_output (vm,
-				   "%=19U%=7d%=30U%=7d%=8d%=8d%=5d%=16v",
+				   "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
 				   format_ethernet_address, key.fields.mac,
 				   key.fields.bd_index,
-				   format_vnet_sw_if_index_name_with_NA,
-				   msm->vnet_main, result.fields.sw_if_index,
 				   result.fields.sw_if_index == ~0
 				   ? -1 : result.fields.sw_if_index,
-				   result.fields.static_mac,
-				   result.fields.filter,
-				   result.fields.bvi, s);
+				   result.fields.bd_sn, result.fields.int_sn,
+				   s, result.fields.static_mac ? "*" : "-",
+				   result.fields.filter ? "*" : "-",
+				   result.fields.bvi ? "*" : "-",
+				   format_vnet_sw_if_index_name_with_NA,
+				   msm->vnet_main, result.fields.sw_if_index);
 		  vec_reset_length (s);
 		}
 	      total_entries++;
@@ -330,6 +331,15 @@
   result.fields.static_mac = static_mac;
   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;
+    }
 
   kv.key = key.raw;
   kv.value = result.raw;
@@ -703,6 +713,141 @@
 };
 /* *INDENT-ON* */
 
+/**
+    Kick off ager to scan MACs to age/delete MAC entries
+*/
+void
+l2fib_start_ager_scan (vlib_main_t * vm)
+{
+  l2_bridge_domain_t *bd_config;
+  int enable = 0;
+
+  /* check if there is at least one bd with mac aging enabled */
+  vec_foreach (bd_config, l2input_main.bd_configs)
+    if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
+    enable = 1;
+
+  vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
+			     enable ? L2_MAC_AGE_PROCESS_EVENT_START :
+			     L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
+}
+
+/**
+    Flush all learned 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);
+  int_config->seq_num += 1;
+  l2fib_start_ager_scan (vm);
+}
+
+/**
+    Flush all learned MACs in a bridge domain
+*/
+void
+l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
+{
+  l2_bridge_domain_t *bd_config;
+  vec_validate (l2input_main.bd_configs, bd_index);
+  bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
+  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>
+*/
+static clib_error_t *
+l2fib_flush_mac_int (vlib_main_t * vm,
+		     unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  vnet_main_t *vnm = vnet_get_main ();
+  clib_error_t *error = 0;
+  u32 sw_if_index;
+
+  if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
+    {
+      error = clib_error_return (0, "unknown interface `%U'",
+				 format_unformat_error, input);
+      goto done;
+    }
+
+  l2fib_flush_int_mac (vm, sw_if_index);
+
+done:
+  return error;
+}
+
+/*?
+ * 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_int_cli, static) = {
+  .path = "l2fib flush-mac interface",
+  .short_help = "l2fib flush-mac interface <if-name>",
+  .function = l2fib_flush_mac_int,
+};
+/* *INDENT-ON* */
+
+/**
+    Flush bridge-domain MACs except static ones.
+    The CLI format is:
+    l2fib flush-mac bridge-domain <bd-id>
+*/
+static clib_error_t *
+l2fib_flush_mac_bd (vlib_main_t * vm,
+		    unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  bd_main_t *bdm = &bd_main;
+  clib_error_t *error = 0;
+  u32 bd_index, bd_id;
+  uword *p;
+
+  if (!unformat (input, "%d", &bd_id))
+    {
+      error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
+				 format_unformat_error, input);
+      goto done;
+    }
+
+  p = hash_get (bdm->bd_index_by_bd_id, bd_id);
+  if (p)
+    bd_index = *p;
+  else
+    return clib_error_return (0, "No such bridge domain %d", bd_id);
+
+  l2fib_flush_bd_mac (vm, bd_index);
+
+done:
+  return error;
+}
+
+/*?
+ * This command kick off ager to delete all existing MAC Address entries,
+ * except static ones, in a bridge domain from the L2 FIB table.
+ *
+ * @cliexpar
+ * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
+ * @cliexcmd{l2fib flush-mac bridge-domain 1000}
+?*/
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
+  .path = "l2fib flush-mac bridge-domain",
+  .short_help = "l2fib flush-mac bridge-domain <bd-id>",
+  .function = l2fib_flush_mac_bd,
+};
+/* *INDENT-ON* */
+
 
 BVT (clib_bihash) * get_mac_table (void)
 {
@@ -716,6 +861,7 @@
 {
   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;
@@ -747,6 +893,9 @@
 	case L2_MAC_AGE_PROCESS_EVENT_STOP:
 	  enabled = 0;
 	  continue;
+	case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
+	  enabled = 0;
+	  break;
 	default:
 	  ASSERT (0);
 	}
@@ -790,8 +939,19 @@
 		  if (result.fields.static_mac)
 		    continue;
 
-		  bd_config = vec_elt_at_index (l2input_main.bd_configs,
-						key.fields.bd_index);
+		  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))
+		    {
+		      void *p = &key.fields.mac;
+		      l2fib_del_entry (*(u64 *) p, key.fields.bd_index);
+		      continue;
+		    }
 
 		  if (bd_config->mac_age == 0)
 		    continue;
diff --git a/src/vnet/l2/l2_fib.h b/src/vnet/l2/l2_fib.h
index 4a2da59..7e49d74 100644
--- a/src/vnet/l2/l2_fib.h
+++ b/src/vnet/l2/l2_fib.h
@@ -66,7 +66,8 @@
       u8 filter:1;		/* drop packets to/from this mac */
       u8 unused1:5;
       u8 timestamp;		/* timestamp for aging */
-      u16 unused2;
+      u8 int_sn;		/* interface seq num */
+      u8 bd_sn;			/* bridge domain seq num */
     } fields;
     u64 raw;
   };
@@ -313,22 +314,28 @@
     }
 }
 
+void l2fib_clear_table (uint keep_static);
+
+void
+l2fib_add_entry (u64 mac,
+		 u32 bd_index,
+		 u32 sw_if_index, u32 static_mac, u32 drop_mac, u32 bvi_mac);
+
+u32 l2fib_del_entry (u64 mac, u32 bd_index);
+
+void l2fib_start_ager_scan (vlib_main_t * vm);
+
+void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);
+
+void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
+
+void
+l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
+		  l2fib_entry_result_t ** l2fe_res);
+
+u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
+
 BVT (clib_bihash) * get_mac_table (void);
-     void
-     l2fib_clear_table (uint keep_static);
-     void
-     l2fib_add_entry (u64 mac,
-		      u32 bd_index,
-		      u32 sw_if_index,
-		      u32 static_mac, u32 drop_mac, u32 bvi_mac);
-u32
-l2fib_del_entry (u64 mac, u32 bd_index);
-
-     void
-       l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
-			 l2fib_entry_result_t ** l2fe_res);
-
-     u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
 
 #endif
 
diff --git a/src/vnet/l2/l2_input.c b/src/vnet/l2/l2_input.c
index 3aa5331..041ff38 100644
--- a/src/vnet/l2/l2_input.c
+++ b/src/vnet/l2/l2_input.c
@@ -29,6 +29,7 @@
 #include <vnet/l2/feat_bitmap.h>
 #include <vnet/l2/l2_bvi.h>
 #include <vnet/l2/l2_fib.h>
+#include <vnet/l2/l2_bd.h>
 
 #include <vppinfra/error.h>
 #include <vppinfra/hash.h>
@@ -201,6 +202,9 @@
       /* 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;
+
       /*
        * Process bridge domain feature enables.
        * To perform learning/flooding/forwarding, the corresponding bit
@@ -214,6 +218,9 @@
   /* 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;
 
@@ -561,6 +568,12 @@
 						VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
 	  ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
 	}
+
+      /* Clear MACs learned on the interface */
+      if ((config->feature_bitmap | L2INPUT_FEAT_LEARN) ||
+	  (bd_config->feature_bitmap | L2INPUT_FEAT_LEARN))
+	l2fib_flush_int_mac (vm, sw_if_index);
+
       l2_if_adjust--;
     }
   else if (config->xconnect)
@@ -632,6 +645,7 @@
 	  config->xconnect = 0;
 	  config->bridge = 1;
 	  config->bd_index = bd_index;
+	  config->seq_num += 1;
 
 	  /*
 	   * Enable forwarding, flooding, learning and ARP termination by default
diff --git a/src/vnet/l2/l2_input.h b/src/vnet/l2/l2_input.h
index f3fada6..262f75c 100644
--- a/src/vnet/l2/l2_input.h
+++ b/src/vnet/l2/l2_input.h
@@ -53,6 +53,9 @@
   /* split horizon group */
   u8 shg;
 
+  /* sequence number for interface based flush of MACs */
+  u8 seq_num;
+
 } l2_input_config_t;
 
 
diff --git a/src/vnet/l2/l2_learn.c b/src/vnet/l2/l2_learn.c
index afe7f47..faed0d6 100644
--- a/src/vnet/l2/l2_learn.c
+++ b/src/vnet/l2/l2_learn.c
@@ -140,7 +140,11 @@
       counter_base[L2LEARN_ERROR_HIT] += 1;
       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;
     }
   else if (result0->raw == ~0)
     {
@@ -167,6 +171,8 @@
 	  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;
 	  kv.key = key0->raw;
 	  kv.value = result0->raw;
 
@@ -204,6 +210,8 @@
 	  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;
 
 	  kv.key = key0->raw;
 	  kv.value = result0->raw;
diff --git a/src/vnet/l2/l2_learn.h b/src/vnet/l2/l2_learn.h
index 5bb1130..0d95de0 100644
--- a/src/vnet/l2/l2_learn.h
+++ b/src/vnet/l2/l2_learn.h
@@ -51,6 +51,7 @@
 {
   L2_MAC_AGE_PROCESS_EVENT_START = 1,
   L2_MAC_AGE_PROCESS_EVENT_STOP = 2,
+  L2_MAC_AGE_PROCESS_EVENT_ONE_PASS = 3,
 } l2_mac_age_process_event_t;
 
 #endif