misc: Purge unused pg includes
[vpp.git] / src / vnet / l2 / l2_fib.c
index 60d0db5..4b7fdcb 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
@@ -136,7 +159,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,10 +187,10 @@ 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);
@@ -197,6 +220,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 +284,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);
 
@@ -287,7 +318,7 @@ show_l2fib (vlib_main_t * vm,
 }
 
 /*?
- * This command dispays the MAC Address entries of the L2 FIB table.
+ * This command displays the MAC Address entries of the L2 FIB table.
  * Output can be filtered to just get the number of MAC Addresses or display
  * each MAC Address for all bridge domains or just a single bridge domain.
  *
@@ -315,6 +346,18 @@ 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
@@ -322,10 +365,12 @@ l2fib_clear_table (void)
 {
   l2fib_main_t *mp = &l2fib_main;
 
+  if (mp->mac_table_initialized == 0)
+    return;
+
   /* 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;
 }
 
@@ -376,7 +421,7 @@ l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
  * If the entry already exists then overwrite it
  */
 void
-l2fib_add_entry (u8 * mac, u32 bd_index,
+l2fib_add_entry (const u8 * mac, u32 bd_index,
                 u32 sw_if_index, l2fib_entry_result_flags_t flags)
 {
   l2fib_entry_key_t key;
@@ -386,10 +431,14 @@ l2fib_add_entry (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 alread exist */
+  /* check if entry already exist */
   if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
     {
       /* decrement counter if overwriting a learned mac  */
@@ -407,7 +456,6 @@ l2fib_add_entry (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 +577,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 +610,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 +627,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 +646,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 +655,7 @@ l2fib_test_command_fn (vlib_main_t * vm,
        }
     }
 
-  return error;
+  return 0;
 }
 
 /*?
@@ -660,12 +710,15 @@ VLIB_CLI_COMMAND (l2fib_test_command, static) = {
  * sw_if_index is non-zero and does not match that in the entry.
  */
 u32
-l2fib_del_entry (u8 * mac, u32 bd_index, u32 sw_if_index)
+l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index)
 {
   l2fib_entry_result_t result;
   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);
 
@@ -979,6 +1032,10 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
   vl_api_l2_macs_event_t *mp = 0;
   vl_api_registration_t *reg = 0;
 
+  /* Don't scan the l2 fib if it hasn't been instantiated yet */
+  if (alloc_arena (h) == 0)
+    return 0.0;
+
   if (client)
     {
       mp = allocate_mac_evt_buf (client, cl_idx);
@@ -998,10 +1055,11 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
 
       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 +1067,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++)
@@ -1050,11 +1108,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 */
@@ -1070,7 +1131,7 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
                }
 
              if (event_only || l2fib_entry_result_is_set_AGE_NOT (&result))
-               continue;       /* skip aging - static_mac alsways age_not */
+               continue;       /* skip aging - static_mac always age_not */
 
              /* start aging processing */
              u32 bd_index = key.fields.bd_index;
@@ -1095,8 +1156,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++;
@@ -1110,7 +1174,7 @@ l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
               * 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++;
@@ -1236,13 +1300,14 @@ 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 */
-  memset (test_mac, 0, sizeof (test_mac));
+  clib_memset (test_mac, 0, sizeof (test_mac));
   test_mac[0] = 0x11;
   test_key.raw = 0;
   test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
@@ -1254,6 +1319,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
  *