l2: Separating scan-delay and learn-limit into a separate API from want_l2_macs_events
[vpp.git] / src / vnet / l2 / l2_fib.c
index 46c041e..4407cb5 100644 (file)
@@ -18,7 +18,6 @@
 
 #include <vlib/vlib.h>
 #include <vnet/vnet.h>
-#include <vnet/pg/pg.h>
 #include <vnet/ethernet/ethernet.h>
 #include <vlib/cli.h>
 
 
 l2fib_main_t l2fib_main;
 
+u8 *
+format_l2fib_entry_result_flags (u8 * s, va_list * args)
+{
+  l2fib_entry_result_flags_t flags = va_arg (*args, int);
+
+  if (L2FIB_ENTRY_RESULT_FLAG_NONE == flags)
+    {
+      s = format (s, "none");
+    }
+  else
+    {
+#define _(a,v,t) {                              \
+      if (flags & L2FIB_ENTRY_RESULT_FLAG_##a)  \
+        s = format (s, "%s ", t);               \
+    }
+      foreach_l2fib_entry_result_attr
+#undef _
+    }
+  return (s);
+}
+
 static void
 incr_mac_address (u8 * mac)
 {
@@ -62,7 +82,7 @@ incr_mac_address (u8 * mac)
   tmp += 1 << 16;              /* skip unused (least significant) octets */
   tmp = clib_host_to_net_u64 (tmp);
 
-  clib_memcpy (mac, &tmp, 6);
+  clib_memcpy_fast (mac, &tmp, 6);
 }
 
 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
@@ -74,12 +94,13 @@ format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
   if (sw_if_index == ~0)
     return format (s, "N/A");
 
-  vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
+  vnet_sw_interface_t *swif =
+    vnet_get_sw_interface_or_null (vnm, sw_if_index);
   if (!swif)
     return format (s, "Stale");
 
   return format (s, "%U", format_vnet_sw_interface_name, vnm,
-                vnet_get_sw_interface_safe (vnm, sw_if_index));
+                vnet_get_sw_interface_or_null (vnm, sw_if_index));
 }
 
 typedef struct l2fib_dump_walk_ctx_t_
@@ -89,7 +110,7 @@ typedef struct l2fib_dump_walk_ctx_t_
   l2fib_entry_result_t *l2fe_res;
 } l2fib_dump_walk_ctx_t;
 
-static void
+static int
 l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
 {
   l2fib_dump_walk_ctx_t *ctx = arg;
@@ -104,6 +125,8 @@ l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
       vec_add1 (ctx->l2fe_key, key);
       vec_add1 (ctx->l2fe_res, result);
     }
+
+  return (BIHASH_WALK_CONTINUE);
 }
 
 void
@@ -123,6 +146,26 @@ l2fib_table_dump (u32 bd_index,
   *l2fe_res = ctx.l2fe_res;
 }
 
+void
+l2_fib_extract_seq_num (l2fib_seq_num_t sn, u8 * bd_sn, u8 * if_sn)
+{
+  *bd_sn = sn >> 8;
+  *if_sn = sn & 0xff;
+}
+
+u8 *
+format_l2_fib_seq_num (u8 * s, va_list * a)
+{
+  l2fib_seq_num_t sn = va_arg (*a, int);
+  u8 bd_sn, if_sn;
+
+  l2_fib_extract_seq_num (sn, &bd_sn, &if_sn);
+
+  s = format (s, "%3d/%-3d", bd_sn, if_sn);
+
+  return (s);
+}
+
 typedef struct l2fib_show_walk_ctx_t_
 {
   u8 first_entry;
@@ -136,7 +179,7 @@ typedef struct l2fib_show_walk_ctx_t_
   u8 now;
 } l2fib_show_walk_ctx_t;
 
-static void
+static int
 l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
 {
   l2fib_show_walk_ctx_t *ctx = arg;
@@ -164,13 +207,12 @@ l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
       u8 *s = NULL;
 
       if (ctx->learn && l2fib_entry_result_is_set_AGE_NOT (&result))
-       return;                 /* skip provisioned macs */
+       return (BIHASH_WALK_CONTINUE);  /* skip provisioned macs */
 
       if (ctx->add && !l2fib_entry_result_is_set_AGE_NOT (&result))
-       return;                 /* skip learned macs */
+       return (BIHASH_WALK_CONTINUE);  /* skip learned macs */
 
-      bd_config = vec_elt_at_index (l2input_main.bd_configs,
-                                   key.fields.bd_index);
+      bd_config = &vec_elt (l2input_main.bd_configs, key.fields.bd_index);
 
       if (l2fib_entry_result_is_set_AGE_NOT (&result))
        s = format (s, "no");
@@ -184,12 +226,12 @@ l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
        }
 
       vlib_cli_output (ctx->vm,
-                      "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
+                      "%=19U%=7d%=7d %U%=9v%=7s%=7s%=5s%=30U",
                       format_ethernet_address, key.fields.mac,
                       key.fields.bd_index,
                       result.fields.sw_if_index == ~0
                       ? -1 : result.fields.sw_if_index,
-                      result.fields.sn.bd, result.fields.sn.swif, s,
+                      format_l2_fib_seq_num, result.fields.sn, s,
                       l2fib_entry_result_is_set_STATIC (&result) ? "*" : "-",
                       l2fib_entry_result_is_set_FILTER (&result) ? "*" : "-",
                       l2fib_entry_result_is_set_BVI (&result) ? "*" : "-",
@@ -197,6 +239,8 @@ l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
                       ctx->vnm, result.fields.sw_if_index);
       vec_free (s);
     }
+
+  return (BIHASH_WALK_CONTINUE);
 }
 
 /** Display the contents of the l2fib. */
@@ -259,6 +303,12 @@ show_l2fib (vlib_main_t * vm,
        break;
     }
 
+  if (msm->mac_table_initialized == 0)
+    {
+      vlib_cli_output (vm, "no l2fib entries");
+      return 0;
+    }
+
   BV (clib_bihash_foreach_key_value_pair)
     (&msm->mac_table, l2fib_show_walk_cb, &ctx);
 
@@ -315,18 +365,37 @@ VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
 };
 /* *INDENT-ON* */
 
+void
+l2fib_table_init (void)
+{
+  l2fib_main_t *mp = &l2fib_main;
+
+  if (mp->mac_table_initialized == 1)
+    return;
+
+  BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
+                        mp->mac_table_n_buckets, mp->mac_table_memory_size);
+  mp->mac_table_initialized = 1;
+}
 
 /* Remove all entries from the l2fib */
 void
 l2fib_clear_table (void)
 {
   l2fib_main_t *mp = &l2fib_main;
+  l2_bridge_domain_t *bd_config;
+
+  if (mp->mac_table_initialized == 0)
+    return;
+
+  mp->mac_table_initialized = 0;
 
   /* 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);
+  l2fib_table_init ();
   l2learn_main.global_learn_count = 0;
+  vec_foreach (bd_config, l2input_main.bd_configs)
+    bd_config->learn_count = 0;
 }
 
 /** Clear all entries in L2FIB.
@@ -359,16 +428,13 @@ VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
 };
 /* *INDENT-ON* */
 
-static inline l2fib_seq_num_t
+static l2fib_seq_num_t
 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
 {
   l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
-  /* *INDENT-OFF* */
-  return (l2fib_seq_num_t) {
-    .swif = *l2fib_swif_seq_num (sw_if_index),
-    .bd = bd_config->seq_num,
-  };
-  /* *INDENT-ON* */
+
+  return l2_fib_mk_seq_num (bd_config->seq_num,
+                           l2_input_seq_num (sw_if_index));
 }
 
 /**
@@ -386,17 +452,33 @@ l2fib_add_entry (const u8 * mac, u32 bd_index,
   l2learn_main_t *lm = &l2learn_main;
   BVT (clib_bihash_kv) kv;
 
+  if (fm->mac_table_initialized == 0)
+    l2fib_table_init ();
+
   /* set up key */
   key.raw = l2fib_make_key (mac, bd_index);
+  kv.key = key.raw;
 
   /* check if entry already exist */
   if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
     {
       /* decrement counter if overwriting a learned mac  */
       result.raw = kv.value;
-      if ((!l2fib_entry_result_is_set_AGE_NOT (&result))
-         && (lm->global_learn_count))
-       lm->global_learn_count--;
+      if (!l2fib_entry_result_is_set_AGE_NOT (&result))
+       {
+         l2_bridge_domain_t *bd_config =
+           vec_elt_at_index (l2input_main.bd_configs, bd_index);
+
+         /* check if learn_count == 0 in case of race condition between 2
+          * workers adding an entry simultaneously */
+         /* learn_count variable may have little inaccuracy because they are
+          * not incremented/decremented with atomic operations */
+         /* l2fib_scan is call every 2sec fixing potential inaccuracy */
+         if (lm->global_learn_count)
+           lm->global_learn_count--;
+         if (bd_config->learn_count)
+           bd_config->learn_count--;
+       }
     }
 
   /* set up result */
@@ -407,7 +489,6 @@ l2fib_add_entry (const u8 * mac, u32 bd_index,
   /* no aging for provisioned entry */
   l2fib_entry_result_set_AGE_NOT (&result);
 
-  kv.key = key.raw;
   kv.value = result.raw;
 
   BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
@@ -529,7 +610,6 @@ static clib_error_t *
 l2fib_test_command_fn (vlib_main_t * vm,
                       unformat_input_t * input, vlib_cli_command_t * cmd)
 {
-  clib_error_t *error = 0;
   u8 mac[6], save_mac[6];
   u32 bd_index = 0;
   u32 sw_if_index = 8;
@@ -563,7 +643,7 @@ l2fib_test_command_fn (vlib_main_t * vm,
     return clib_error_return (0,
                              "noop: pick at least one of (add,del,check)");
 
-  clib_memcpy (save_mac, mac, 6);
+  clib_memcpy_fast (save_mac, mac, 6);
 
   if (is_add)
     {
@@ -580,7 +660,10 @@ l2fib_test_command_fn (vlib_main_t * vm,
       BVT (clib_bihash_kv) kv;
       l2fib_main_t *mp = &l2fib_main;
 
-      clib_memcpy (mac, save_mac, 6);
+      if (mp->mac_table_initialized == 0)
+       return clib_error_return (0, "mac table is not initialized");
+
+      clib_memcpy_fast (mac, save_mac, 6);
 
       for (i = 0; i < count; i++)
        {
@@ -596,7 +679,7 @@ l2fib_test_command_fn (vlib_main_t * vm,
 
   if (is_del)
     {
-      clib_memcpy (mac, save_mac, 6);
+      clib_memcpy_fast (mac, save_mac, 6);
 
       for (i = 0; i < count; i++)
        {
@@ -605,7 +688,7 @@ l2fib_test_command_fn (vlib_main_t * vm,
        }
     }
 
-  return error;
+  return 0;
 }
 
 /*?
@@ -666,6 +749,9 @@ l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index)
   l2fib_main_t *mp = &l2fib_main;
   BVT (clib_bihash_kv) kv;
 
+  if (mp->mac_table_initialized == 0)
+    return 1;
+
   /* set up key */
   kv.key = l2fib_make_key (mac, bd_index);
 
@@ -679,9 +765,15 @@ l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index)
     return 1;
 
   /* decrement counter if dynamically learned mac */
-  if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) &&
-      (l2learn_main.global_learn_count))
-    l2learn_main.global_learn_count--;
+  if (!l2fib_entry_result_is_set_AGE_NOT (&result))
+    {
+      l2_bridge_domain_t *bd_config =
+       vec_elt_at_index (l2input_main.bd_configs, bd_index);
+      if (l2learn_main.global_learn_count)
+       l2learn_main.global_learn_count--;
+      if (bd_config->learn_count)
+       bd_config->learn_count--;
+    }
 
   /* Remove entry from hash table */
   BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
@@ -753,6 +845,36 @@ VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+l2fib_set_scan_delay (vlib_main_t *vm, unformat_input_t *input,
+                     vlib_cli_command_t *cmd)
+{
+  clib_error_t *error = 0;
+  u32 scan_delay;
+  l2fib_main_t *fm = &l2fib_main;
+
+  if (!unformat (input, "%d", &scan_delay))
+    {
+      error = clib_error_return (0, "expecting delay but got `%U'",
+                                format_unformat_error, input);
+      goto done;
+    }
+  fm->event_scan_delay = (f64) (scan_delay) *10e-3;
+  l2fib_flush_all_mac (vlib_get_main ());
+done:
+  return error;
+}
+
+/*?
+ * This command set scan delay (in 1/10s unit)
+ *
+?*/
+VLIB_CLI_COMMAND (l2fib_set_scan_delay_cli, static) = {
+  .path = "set l2fib scan-delay",
+  .short_help = "set l2fib scan-delay <delay>",
+  .function = l2fib_set_scan_delay,
+};
+
 /**
     Kick off ager to scan MACs to age/delete MAC entries
 */
@@ -782,7 +904,7 @@ l2fib_start_ager_scan (vlib_main_t * vm)
 void
 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
 {
-  *l2fib_swif_seq_num (sw_if_index) += 1;
+  l2_input_seq_num_inc (sw_if_index);
   l2fib_start_ager_scan (vm);
 }
 
@@ -936,7 +1058,8 @@ 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)
+  if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 &&
+      l2_input_is_bridge (config))
     l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
   return 0;
 }
@@ -978,6 +1101,15 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
   u32 cl_idx = lm->client_index;
   vl_api_l2_macs_event_t *mp = 0;
   vl_api_registration_t *reg = 0;
+  u32 bd_index;
+  static u32 *bd_learn_counts = 0;
+
+  /* Don't scan the l2 fib if it hasn't been instantiated yet */
+  if (alloc_arena (h) == 0)
+    return 0.0;
+
+  vec_reset_length (bd_learn_counts);
+  vec_validate (bd_learn_counts, vec_len (l2input_main.bd_configs) - 1);
 
   if (client)
     {
@@ -992,16 +1124,20 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
       if (delta_t > 20e-6)
        {
          vlib_process_suspend (vm, 100e-6);    /* suspend for 100 us */
+         /* in case a new bd was created while sleeping */
+         vec_validate (bd_learn_counts,
+                       vec_len (l2input_main.bd_configs) - 1);
          last_start = vlib_time_now (vm);
          accum_t += delta_t;
        }
 
       if (i < (h->nbuckets - 3))
        {
-         BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
+         BVT (clib_bihash_bucket) * b =
+           BV (clib_bihash_get_bucket) (h, i + 3);
          CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
-         b = &h->buckets[i + 1];
-         if (b->offset)
+         b = BV (clib_bihash_get_bucket) (h, i + 1);
+         if (!BV (clib_bihash_bucket_is_empty) (b))
            {
              BVT (clib_bihash_value) * v =
                BV (clib_bihash_get_value) (h, b->offset);
@@ -1009,8 +1145,8 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
            }
        }
 
-      BVT (clib_bihash_bucket) * b = &h->buckets[i];
-      if (b->offset == 0)
+      BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i);
+      if (BV (clib_bihash_bucket_is_empty) (b))
        continue;
       BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
       for (j = 0; j < (1 << b->log2_pages); j++)
@@ -1024,7 +1160,10 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
              l2fib_entry_result_t result = {.raw = v->kvp[k].value };
 
              if (!l2fib_entry_result_is_set_AGE_NOT (&result))
-               learn_count++;
+               {
+                 learn_count++;
+                 vec_elt (bd_learn_counts, key.fields.bd_index)++;
+               }
 
              if (client)
                {
@@ -1050,11 +1189,14 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
                  if (l2fib_entry_result_is_set_LRN_EVT (&result))
                    {
                      /* copy mac entry to event msg */
-                     clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac,
-                                  6);
+                     clib_memcpy_fast (mp->mac[evt_idx].mac_addr,
+                                       key.fields.mac, 6);
                      mp->mac[evt_idx].action =
                        l2fib_entry_result_is_set_LRN_MOV (&result) ?
-                       MAC_EVENT_ACTION_MOVE : MAC_EVENT_ACTION_ADD;
+                       (vl_api_mac_event_action_t) MAC_EVENT_ACTION_MOVE
+                       : (vl_api_mac_event_action_t) MAC_EVENT_ACTION_ADD;
+                     mp->mac[evt_idx].action =
+                       htonl (mp->mac[evt_idx].action);
                      mp->mac[evt_idx].sw_if_index =
                        htonl (result.fields.sw_if_index);
                      /* clear event bits and update mac entry */
@@ -1075,8 +1217,8 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
              /* start aging processing */
              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)
+             u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index);
+             if (result.fields.sn != sn)
                goto age_out;   /* stale mac */
 
              l2_bridge_domain_t *bd_config =
@@ -1095,8 +1237,11 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
              if (client)
                {
                  /* copy mac entry to event msg */
-                 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac, 6);
-                 mp->mac[evt_idx].action = MAC_EVENT_ACTION_DELETE;
+                 clib_memcpy_fast (mp->mac[evt_idx].mac_addr, key.fields.mac,
+                                   6);
+                 mp->mac[evt_idx].action =
+                   (vl_api_mac_event_action_t) MAC_EVENT_ACTION_DELETE;
+                 mp->mac[evt_idx].action = htonl (mp->mac[evt_idx].action);
                  mp->mac[evt_idx].sw_if_index =
                    htonl (result.fields.sw_if_index);
                  evt_idx++;
@@ -1106,11 +1251,12 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
              kv.key = key.raw;
              BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
              learn_count--;
+             vec_elt (bd_learn_counts, key.fields.bd_index)--;
              /*
               * Note: we may have just freed the bucket's backing
               * storage, so check right here...
               */
-             if (b->offset == 0)
+             if (BV (clib_bihash_bucket_is_empty) (b))
                goto doublebreak;
            }
          v++;
@@ -1121,6 +1267,11 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
 
   /* keep learn count consistent */
   l2learn_main.global_learn_count = learn_count;
+  vec_foreach_index (bd_index, l2input_main.bd_configs)
+    {
+      vec_elt (l2input_main.bd_configs, bd_index).learn_count =
+       vec_elt (bd_learn_counts, bd_index);
+    }
 
   if (mp)
     {
@@ -1236,10 +1387,11 @@ l2fib_init (vlib_main_t * vm)
 
   mp->vlib_main = vm;
   mp->vnet_main = vnet_get_main ();
-
-  /* Create the hash table  */
-  BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
-                        L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
+  if (mp->mac_table_n_buckets == 0)
+    mp->mac_table_n_buckets = L2FIB_NUM_BUCKETS;
+  if (mp->mac_table_memory_size == 0)
+    mp->mac_table_memory_size = L2FIB_MEMORY_SIZE;
+  mp->mac_table_initialized = 0;
 
   /* verify the key constructor is good, since it is endian-sensitive */
   clib_memset (test_mac, 0, sizeof (test_mac));
@@ -1254,6 +1406,39 @@ l2fib_init (vlib_main_t * vm)
 
 VLIB_INIT_FUNCTION (l2fib_init);
 
+static clib_error_t *
+lfib_config (vlib_main_t * vm, unformat_input_t * input)
+{
+  l2fib_main_t *lm = &l2fib_main;
+  uword table_size = ~0;
+  u32 n_buckets = ~0;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "table-size %U", unformat_memory_size,
+                   &table_size))
+       ;
+      else if (unformat (input, "num-buckets %u", &n_buckets))
+       ;
+      else
+       return clib_error_return (0, "unknown input `%U'",
+                                 format_unformat_error, input);
+    }
+
+  if (n_buckets != ~0)
+    {
+      if (!is_pow2 (n_buckets))
+       return clib_error_return (0, "num-buckets must be power of 2");
+      lm->mac_table_n_buckets = n_buckets;
+    }
+
+  if (table_size != ~0)
+    lm->mac_table_memory_size = table_size;
+  return 0;
+}
+
+VLIB_CONFIG_FUNCTION (lfib_config, "l2fib");
+
 /*
  * fd.io coding-style-patch-verification: ON
  *