L2FIB:CLI/API to flush all non-static entries
[vpp.git] / src / vnet / l2 / l2_fib.c
index d34836e..d4207e3 100644 (file)
@@ -54,7 +54,6 @@ typedef struct
 
 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)
@@ -168,10 +167,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 +182,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.sn.bd, result.fields.sn.swif,
+                                  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++;
@@ -214,7 +214,9 @@ show_l2fib (vlib_main_t * vm,
   if (total_entries == 0)
     vlib_cli_output (vm, "no l2fib entries");
   else
-    vlib_cli_output (vm, "%lld l2fib entries", total_entries);
+    vlib_cli_output (vm,
+                    "%lld l2fib entries with %d learned (or non-static) entries",
+                    total_entries, l2learn_main.global_learn_count);
 
   if (raw)
     vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
@@ -256,22 +258,14 @@ VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
 
 /* 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;
 }
 
@@ -282,7 +276,7 @@ static clib_error_t *
 clear_l2fib (vlib_main_t * vm,
             unformat_input_t * input, vlib_cli_command_t * cmd)
 {
-  l2fib_clear_table (0);
+  l2fib_clear_table ();
   return 0;
 }
 
@@ -305,14 +299,25 @@ VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
 };
 /* *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;
@@ -330,6 +335,8 @@ l2fib_add_entry (u64 mac,
   result.fields.static_mac = static_mac;
   result.fields.filter = filter_mac;
   result.fields.bvi = bvi_mac;
+  if (!static_mac)
+    result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
 
   kv.key = key.raw;
   kv.value = result.raw;
@@ -337,7 +344,7 @@ l2fib_add_entry (u64 mac,
   BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
 
   /* increment counter if dynamically learned mac */
-  if (result.fields.static_mac)
+  if (result.fields.static_mac == 0)
     {
       l2learn_main.global_learn_count++;
     }
@@ -608,8 +615,8 @@ VLIB_CLI_COMMAND (l2fib_test_command, static) = {
  * 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;
@@ -617,7 +624,7 @@ l2fib_del_entry (u64 mac, u32 bd_index)
   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;
@@ -625,7 +632,7 @@ l2fib_del_entry (u64 mac, u32 bd_index)
   result.raw = kv.value;
 
   /* decrement counter if dynamically learned mac */
-  if (result.fields.static_mac)
+  if (result.fields.static_mac == 0)
     {
       if (l2learn_main.global_learn_count > 0)
        {
@@ -638,6 +645,16 @@ l2fib_del_entry (u64 mac, u32 bd_index)
   return 0;
 }
 
+/**
+ * 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:
@@ -703,6 +720,192 @@ 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 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 = l2input_intf_config (sw_if_index);
+  int_config->seq_num += 1;
+  l2fib_start_ager_scan (vm);
+}
+
+/**
+    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 = 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>
+*/
+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;
+}
+
+/**
+    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.
+ *
+ * @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* */
+
+clib_error_t *
+l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
+{
+  l2_input_config_t *config = l2input_intf_config (sw_if_index);
+  if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
+    l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
+  return 0;
+}
+
+VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
 
 BVT (clib_bihash) * get_mac_table (void)
 {
@@ -716,16 +919,8 @@ 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_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)
     {
@@ -747,10 +942,16 @@ 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);
        }
       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 */
@@ -763,20 +964,22 @@ l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
 
          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++)
@@ -784,26 +987,32 @@ l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
                  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;
 
-                 bd_config = vec_elt_at_index (l2input_main.bd_configs,
-                                               key.fields.bd_index);
+                 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)
+                   {
+                     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++;
            }