l2fib: add mac aging support 08/4108/9
authorDamjan Marion <damarion@cisco.com>
Mon, 5 Dec 2016 13:16:38 +0000 (14:16 +0100)
committerDave Barach <openvpp@barachs.net>
Wed, 7 Dec 2016 17:42:07 +0000 (17:42 +0000)
Change-Id: Ib617ae0f76320d596cc6c4b384da76c91d701a24
Signed-off-by: Damjan Marion <damarion@cisco.com>
vnet/vnet/l2/l2_bd.c
vnet/vnet/l2/l2_bd.h
vnet/vnet/l2/l2_fib.c
vnet/vnet/l2/l2_fib.h
vnet/vnet/l2/l2_learn.c
vnet/vnet/l2/l2_learn.h
vpp-api-test/vat/api_format.c
vpp/vpp-api/api.c
vpp/vpp-api/vpe.api
vppinfra/vppinfra/error_bootstrap.h

index e2ef679..22f83d0 100644 (file)
@@ -23,6 +23,7 @@
 #include <vnet/l2/l2_input.h>
 #include <vnet/l2/feat_bitmap.h>
 #include <vnet/l2/l2_bd.h>
+#include <vnet/l2/l2_learn.h>
 #include <vnet/l2/l2_fib.h>
 #include <vnet/l2/l2_vtr.h>
 #include <vnet/ip/ip4_packet.h>
@@ -264,6 +265,29 @@ bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable)
   return 0;
 }
 
+/**
+    Set the mac age for the bridge domain.
+*/
+void
+bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
+{
+  l2_bridge_domain_t *bd_config;
+  int enable = 0;
+
+  vec_validate (l2input_main.bd_configs, bd_index);
+  bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
+  bd_config->mac_age = age;
+
+  /* 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_STOP, 0);
+}
+
 /**
    Set bridge-domain learn enable/disable.
    The CLI format is:
@@ -572,6 +596,71 @@ done:
   return error;
 }
 
+static clib_error_t *
+bd_mac_age (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;
+  u32 age;
+  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 == 0)
+    return clib_error_return (0, "No such bridge domain %d", bd_id);
+
+  bd_index = p[0];
+
+  if (!unformat (input, "%u", &age))
+    {
+      error =
+       clib_error_return (0, "expecting ageing time in minutes but got `%U'",
+                          format_unformat_error, input);
+      goto done;
+    }
+
+  /* set the bridge domain flag */
+  if (age > 255)
+    {
+      error =
+       clib_error_return (0, "mac aging time cannot be bigger than 255");
+      goto done;
+    }
+  bd_set_mac_age (vm, bd_index, (u8) age);
+
+done:
+  return error;
+}
+
+/*?
+ * Layer 2 mac aging can be enabled and disabled on each
+ * bridge-domain. Use this command to set or disable mac aging
+ * on specific bridge-domains. It is disabled by default.
+ *
+ * @cliexpar
+ * Example of how to set mac aging (where 200 is the bridge-domain-id and
+ * 5 is aging time in minutes):
+ * @cliexcmd{set bridge-domain mac-age 200 5}
+ * Example of how to disable mac aging (where 200 is the bridge-domain-id):
+ * @cliexcmd{set bridge-domain flood 200 0}
+?*/
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
+  .path = "set bridge-domain mac-age",
+  .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
+  .function = bd_mac_age,
+};
+/* *INDENT-ON* */
+
 /*?
  * Modify whether or not an existing bridge-domain should terminate and respond
  * to ARP Requests. ARP Termination is disabled by default.
index b9ee823..4bb9bc9 100644 (file)
@@ -83,6 +83,9 @@ typedef struct
   uword *mac_by_ip4;
   uword *mac_by_ip6;
 
+  /* mac aging */
+  u8 mac_age;
+
 } l2_bridge_domain_t;
 
 /* Return 1 if bridge domain has been initialized */
@@ -109,6 +112,7 @@ u32 bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index);
 #define L2_ARP_TERM (1<<4)
 
 u32 bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable);
+void bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age);
 
 /**
  * \brief Get or create a bridge domain.
index 2054676..d34836e 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <vppinfra/error.h>
 #include <vppinfra/hash.h>
+#include <vnet/l2/l2_input.h>
 #include <vnet/l2/l2_fib.h>
 #include <vnet/l2/l2_learn.h>
 #include <vnet/l2/l2_bd.h>
@@ -114,6 +115,7 @@ show_l2fib (vlib_main_t * vm,
 {
   bd_main_t *bdm = &bd_main;
   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;
@@ -125,6 +127,8 @@ show_l2fib (vlib_main_t * vm,
   u8 verbose = 0;
   u8 raw = 0;
   u32 bd_id, bd_index = ~0;
+  u8 now = (u8) (vlib_time_now (vm) / 60);
+  u8 *s = 0;
 
   if (unformat (input, "raw"))
     raw = 1;
@@ -164,10 +168,10 @@ show_l2fib (vlib_main_t * vm,
                {
                  first_entry = 0;
                  vlib_cli_output (vm,
-                                  "%=19s%=7s%=30s%=7s%=8s%=8s%=5s%=9s%=11s",
+                                  "%=19s%=7s%=30s%=7s%=8s%=8s%=5s%=16s",
                                   "Mac Address", "BD Idx", "Interface",
                                   "Index", "static", "filter", "bvi",
-                                  "refresh", "timestamp");
+                                  "Mac Age (min)");
                }
 
              key.raw = v->kvp[k].key;
@@ -176,8 +180,20 @@ show_l2fib (vlib_main_t * vm,
              if (verbose
                  & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
                {
+                 bd_config = vec_elt_at_index (l2input_main.bd_configs,
+                                               key.fields.bd_index);
+
+                 if (bd_config->mac_age)
+                   {
+                     i16 delta = now - result.fields.timestamp;
+                     delta += delta < 0 ? 256 : 0;
+                     s = format (s, "%d", delta);
+                   }
+                 else
+                   s = format (s, "disabled");
+
                  vlib_cli_output (vm,
-                                  "%=19U%=7d%=30U%=7d%=8d%=8d%=5d%=9d%=11X",
+                                  "%=19U%=7d%=30U%=7d%=8d%=8d%=5d%=16v",
                                   format_ethernet_address, key.fields.mac,
                                   key.fields.bd_index,
                                   format_vnet_sw_if_index_name_with_NA,
@@ -186,9 +202,8 @@ show_l2fib (vlib_main_t * vm,
                                   ? -1 : result.fields.sw_if_index,
                                   result.fields.static_mac,
                                   result.fields.filter,
-                                  result.fields.bvi,
-                                  result.fields.refresh,
-                                  result.fields.timestamp);
+                                  result.fields.bvi, s);
+                 vec_reset_length (s);
                }
              total_entries++;
            }
@@ -205,6 +220,7 @@ show_l2fib (vlib_main_t * vm,
     vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
                     BV (format_bihash), h, 1 /* verbose */ );
 
+  vec_free (s);
   return 0;
 }
 
@@ -694,6 +710,117 @@ BVT (clib_bihash) * get_mac_table (void)
   return &mp->mac_table;
 }
 
+static uword
+l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
+                              vlib_frame_t * f)
+{
+  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)
+    {
+      if (enabled)
+       vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
+      else
+       vlib_process_wait_for_event (vm);
+
+      event_type = vlib_process_get_events (vm, &event_data);
+      vec_reset_length (event_data);
+
+      switch (event_type)
+       {
+       case ~0:
+         break;
+       case L2_MAC_AGE_PROCESS_EVENT_START:
+         enabled = 1;
+         break;
+       case L2_MAC_AGE_PROCESS_EVENT_STOP:
+         enabled = 0;
+         continue;
+       default:
+         ASSERT (0);
+       }
+      last_run_duration = start_time = vlib_time_now (vm);
+      for (i = 0; i < h->nbuckets; i++)
+       {
+         /* Allow no more than 10us without a pause */
+         t = vlib_time_now (vm);
+         if (t > start_time + 10e-6)
+           {
+             vlib_process_suspend (vm, 100e-6);        /* suspend for 100 us */
+             start_time = vlib_time_now (vm);
+           }
+
+         if (i < (h->nbuckets - 3))
+           {
+             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);
+                 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
+               }
+           }
+
+         b = &h->buckets[i];
+         if (b->offset == 0)
+           continue;
+         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++)
+               {
+                 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
+                   continue;
+
+                 key.raw = v->kvp[k].key;
+                 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);
+
+                 if (bd_config->mac_age == 0)
+                   continue;
+
+                 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);
+                   }
+               }
+             v++;
+           }
+       }
+      last_run_duration = vlib_time_now (vm) - last_run_duration;
+    }
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
+    .function = l2fib_mac_age_scanner_process,
+    .type = VLIB_NODE_TYPE_PROCESS,
+    .name = "l2fib-mac-age-scanner-process",
+};
+/* *INDENT-ON* */
+
 clib_error_t *
 l2fib_init (vlib_main_t * vm)
 {
index 63f394c..4a2da59 100644 (file)
@@ -48,6 +48,8 @@ typedef struct
   };
 } l2fib_entry_key_t;
 
+STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);
+
 /*
  * The l2fib entry results
  */
@@ -62,8 +64,7 @@ typedef struct
       u8 static_mac:1;         /* static mac, no dataplane learning */
       u8 bvi:1;                        /* mac is for a bridged virtual interface */
       u8 filter:1;             /* drop packets to/from this mac */
-      u8 refresh:1;            /* refresh flag for aging */
-      u8 unused1:4;
+      u8 unused1:5;
       u8 timestamp;            /* timestamp for aging */
       u16 unused2;
     } fields;
@@ -71,6 +72,7 @@ typedef struct
   };
 } l2fib_entry_result_t;
 
+STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);
 
 /**
  * Compute the hash for the given key and return
index 9feb728..7f19f93 100644 (file)
@@ -113,7 +113,8 @@ l2learn_process (vlib_node_runtime_t * node,
                 u32 sw_if_index0,
                 l2fib_entry_key_t * key0,
                 l2fib_entry_key_t * cached_key,
-                u32 * bucket0, l2fib_entry_result_t * result0, u32 * next0)
+                u32 * bucket0,
+                l2fib_entry_result_t * result0, u32 * next0, u8 timestamp)
 {
   u32 feature_bitmap;
 
@@ -135,11 +136,10 @@ l2learn_process (vlib_node_runtime_t * node,
     {
       /*
        * The entry was in the table, and the sw_if_index matched, the normal case
-       *
-       * TODO: for dataplane learning and aging, do this:
-       *      if refresh=0 and not a static mac, set refresh=1
        */
       counter_base[L2LEARN_ERROR_HIT] += 1;
+      if (PREDICT_FALSE (result0->fields.timestamp != timestamp))
+       result0->fields.timestamp = timestamp;
 
     }
   else if (result0->raw == ~0)
@@ -166,7 +166,7 @@ l2learn_process (vlib_node_runtime_t * node,
 
          result0->raw = 0;     /* clear all fields */
          result0->fields.sw_if_index = sw_if_index0;
-         /* TODO: set timestamp in entry to clock for dataplane aging */
+         result0->fields.timestamp = timestamp;
          kv.key = key0->raw;
          kv.value = result0->raw;
 
@@ -203,6 +203,7 @@ l2learn_process (vlib_node_runtime_t * node,
 
          result0->raw = 0;     /* clear all fields */
          result0->fields.sw_if_index = sw_if_index0;
+         result0->fields.timestamp = timestamp;
 
          kv.key = key0->raw;
          kv.value = result0->raw;
@@ -242,6 +243,7 @@ l2learn_node_fn (vlib_main_t * vm,
   vlib_error_main_t *em = &vm->error_main;
   l2fib_entry_key_t cached_key;
   l2fib_entry_result_t cached_result;
+  u8 timestamp = (u8) (vlib_time_now (vm) / 60);
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;      /* number of packets to process */
@@ -377,19 +379,19 @@ l2learn_node_fn (vlib_main_t * vm,
 
          l2learn_process (node, msm, &em->counters[node_counter_base_index],
                           b0, sw_if_index0, &key0, &cached_key,
-                          &bucket0, &result0, &next0);
+                          &bucket0, &result0, &next0, timestamp);
 
          l2learn_process (node, msm, &em->counters[node_counter_base_index],
                           b1, sw_if_index1, &key1, &cached_key,
-                          &bucket1, &result1, &next1);
+                          &bucket1, &result1, &next1, timestamp);
 
          l2learn_process (node, msm, &em->counters[node_counter_base_index],
                           b2, sw_if_index2, &key2, &cached_key,
-                          &bucket2, &result2, &next2);
+                          &bucket2, &result2, &next2, timestamp);
 
          l2learn_process (node, msm, &em->counters[node_counter_base_index],
                           b3, sw_if_index3, &key3, &cached_key,
-                          &bucket3, &result3, &next3);
+                          &bucket3, &result3, &next3, timestamp);
 
          /* verify speculative enqueues, maybe switch current next frame */
          /* if next0==next1==next_index then nothing special needs to be done */
@@ -445,7 +447,7 @@ l2learn_node_fn (vlib_main_t * vm,
 
          l2learn_process (node, msm, &em->counters[node_counter_base_index],
                           b0, sw_if_index0, &key0, &cached_key,
-                          &bucket0, &result0, &next0);
+                          &bucket0, &result0, &next0, timestamp);
 
          /* verify speculative enqueue, maybe switch current next frame */
          vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
index 5b5eff7..5bb1130 100644 (file)
@@ -45,6 +45,14 @@ typedef struct
 
 l2learn_main_t l2learn_main;
 
+extern vlib_node_registration_t l2fib_mac_age_scanner_process_node;
+
+enum
+{
+  L2_MAC_AGE_PROCESS_EVENT_START = 1,
+  L2_MAC_AGE_PROCESS_EVENT_STOP = 2,
+} l2_mac_age_process_event_t;
+
 #endif
 
 /*
index c6e5ac8..e9cef11 100644 (file)
@@ -5441,6 +5441,7 @@ api_bridge_domain_add_del (vat_main_t * vam)
   u32 bd_id = ~0;
   u8 is_add = 1;
   u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
+  u32 mac_age = 0;
 
   /* Parse args required to build the message */
   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
@@ -5457,6 +5458,8 @@ api_bridge_domain_add_del (vat_main_t * vam)
        ;
       else if (unformat (i, "arp-term %d", &arp_term))
        ;
+      else if (unformat (i, "mac-age %d", &mac_age))
+       ;
       else if (unformat (i, "del"))
        {
          is_add = 0;
@@ -5472,6 +5475,12 @@ api_bridge_domain_add_del (vat_main_t * vam)
       return -99;
     }
 
+  if (mac_age > 255)
+    {
+      errmsg ("mac age must be less than 256 \n");
+      return -99;
+    }
+
   M (BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del);
 
   mp->bd_id = ntohl (bd_id);
@@ -5481,6 +5490,7 @@ api_bridge_domain_add_del (vat_main_t * vam)
   mp->learn = learn;
   mp->arp_term = arp_term;
   mp->is_add = is_add;
+  mp->mac_age = (u8) mac_age;
 
   S;
   W;
index f1cd93a..b775309 100644 (file)
@@ -974,6 +974,7 @@ vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
       if (disable_flags)
        bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
 
+      bd_set_mac_age (vm, mp->mac_age, mp->mac_age);
     }
   else
     rv = bd_delete_bd_index (bdm, bd_id);
@@ -1011,6 +1012,7 @@ send_bridge_domain_details (unix_shared_memory_queue_t * q,
   mp->learn = bd_feature_learn (bd_config);
   mp->arp_term = bd_feature_arp_term (bd_config);
   mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
+  mp->mac_age = bd_config->mac_age;
   mp->n_sw_ifs = ntohl (n_sw_ifs);
   mp->context = context;
 
index 5116cbf..1c33f70 100644 (file)
@@ -2890,6 +2890,7 @@ define ip6_nd_event
     @param forward - enable/disable forwarding on all interfaces in the bd
     @param learn - enable/disable learning on all interfaces in the bd
     @param arp_term - enable/disable arp termination in the bd
+    @param mac_age - mac aging time in min, 0 for disabled
     @param is_add - add or delete flag
 */
 define bridge_domain_add_del
@@ -2902,6 +2903,7 @@ define bridge_domain_add_del
   u8 forward;
   u8 learn;
   u8 arp_term;
+  u8 mac_age;
   u8 is_add;
 };
 
@@ -2934,6 +2936,7 @@ define bridge_domain_dump
     @param forward - forwarding state on all interfaces in the bd
     @param learn - learning state on all interfaces in the bd
     @param arp_term - arp termination state on all interfaces in the bd
+    @param mac_age - mac aging time in min, 0 for disabled
     @param n_sw_ifs - number of sw_if_index's in the domain
 */
 define bridge_domain_details
@@ -2945,6 +2948,7 @@ define bridge_domain_details
   u8 forward;
   u8 learn;
   u8 arp_term;
+  u8 mac_age;
   u32 bvi_sw_if_index;
   u32 n_sw_ifs;
 };
index b03ec88..3416c2f 100644 (file)
@@ -85,6 +85,9 @@ do {                                                  \
 #define STATIC_ASSERT(truth,...) _Static_assert(truth, __VA_ARGS__)
 #endif
 
+#define STATIC_ASSERT_SIZEOF(d, s) \
+  STATIC_ASSERT (sizeof (d) == s, "Size of " #d " must be " # s " bytes")
+
 /* Assert without allocating memory. */
 #define ASSERT_AND_PANIC(truth)                        \
 do {                                           \