Implement MAC Flush for BD or Interface from the L2FIB
[vpp.git] / src / vnet / l2 / l2_fib.c
index d34836e..fadd79e 100644 (file)
@@ -168,10 +168,10 @@ show_l2fib (vlib_main_t * vm,
                {
                  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 @@ show_l2fib (vlib_main_t * vm,
                  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 @@ l2fib_add_entry (u64 mac,
   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 @@ VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
 };
 /* *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 @@ l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
 {
   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 @@ l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
        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 @@ l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
                  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;