Add support for API client to receive L2 MAC events
[vpp.git] / src / vnet / l2 / l2_learn.c
index 7f19f93..47c036b 100644 (file)
@@ -116,72 +116,61 @@ l2learn_process (vlib_node_runtime_t * node,
                 u32 * bucket0,
                 l2fib_entry_result_t * result0, u32 * next0, u8 timestamp)
 {
-  u32 feature_bitmap;
-
   /* Set up the default next node (typically L2FWD) */
-
-  /* Remove ourself from the feature bitmap */
-  feature_bitmap = vnet_buffer (b0)->l2.feature_bitmap & ~L2INPUT_FEAT_LEARN;
-
-  /* Save for next feature graph nodes */
-  vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
-
-  /* Determine the next node */
-  *next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
-                                           feature_bitmap);
+  *next0 = vnet_l2_feature_next (b0, msm->feat_next_node_index,
+                                L2INPUT_FEAT_LEARN);
 
   /* Check mac table lookup result */
-
   if (PREDICT_TRUE (result0->fields.sw_if_index == sw_if_index0))
     {
-      /*
-       * The entry was in the table, and the sw_if_index matched, the normal case
-       */
+      /* Entry in L2FIB with matching sw_if_index matched - normal fast path */
       counter_base[L2LEARN_ERROR_HIT] += 1;
-      if (PREDICT_FALSE (result0->fields.timestamp != timestamp))
-       result0->fields.timestamp = timestamp;
+      int update = !result0->fields.age_not && /* static_mac always age_not */
+       (result0->fields.timestamp != timestamp ||
+        result0->fields.sn.as_u16 != vnet_buffer (b0)->l2.l2fib_sn);
 
+      if (PREDICT_TRUE (!update))
+       return;
     }
   else if (result0->raw == ~0)
     {
-
-      /* The entry was not in table, so add it  */
-
+      /* Entry not in L2FIB - add it  */
       counter_base[L2LEARN_ERROR_MISS] += 1;
 
-      if (msm->global_learn_count == msm->global_learn_limit)
+      if (msm->global_learn_count >= msm->global_learn_limit)
        {
          /*
           * Global limit reached. Do not learn the mac but forward the packet.
           * In the future, limits could also be per-interface or bridge-domain.
           */
          counter_base[L2LEARN_ERROR_LIMIT] += 1;
-         goto done;
-
-       }
-      else
-       {
-         BVT (clib_bihash_kv) kv;
-         /* It is ok to learn */
-
-         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;
-
-         BV (clib_bihash_add_del) (msm->mac_table, &kv, 1 /* is_add */ );
-
-         cached_key->raw = ~0; /* invalidate the cache */
-         msm->global_learn_count++;
+         return;
        }
 
+      /* Do not learn if mac is 0 */
+      l2fib_entry_key_t key = *key0;
+      key.fields.bd_index = 0;
+      if (key.raw == 0)
+       return;
+
+      /* It is ok to learn */
+      msm->global_learn_count++;
+      result0->raw = 0;                /* clear all fields */
+      result0->fields.sw_if_index = sw_if_index0;
+      result0->fields.lrn_evt = (msm->client_pid != 0);
+      cached_key->raw = ~0;    /* invalidate the cache */
     }
   else
     {
-
-      /* The entry was in the table, but with the wrong sw_if_index mapping (mac move) */
-      counter_base[L2LEARN_ERROR_MAC_MOVE] += 1;
+      /* Entry in L2FIB with different sw_if_index - mac move or filter */
+      if (result0->fields.filter)
+       {
+         ASSERT (result0->fields.sw_if_index == ~0);
+         /* drop packet because lookup matched a filter mac entry */
+         b0->error = node->errors[L2LEARN_ERROR_FILTER_DROP];
+         *next0 = L2LEARN_NEXT_DROP;
+         return;
+       }
 
       if (result0->fields.static_mac)
        {
@@ -191,49 +180,37 @@ l2learn_process (vlib_node_runtime_t * node,
           */
          b0->error = node->errors[L2LEARN_ERROR_MAC_MOVE_VIOLATE];
          *next0 = L2LEARN_NEXT_DROP;
+         return;
        }
-      else
-       {
-         /*
-          * Update the entry
-          * TODO: may want to rate limit mac moves
-          * TODO: check global/bridge domain/interface learn limits
-          */
-         BVT (clib_bihash_kv) kv;
-
-         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;
-
-         cached_key->raw = ~0; /* invalidate the cache */
-
-         BV (clib_bihash_add_del) (msm->mac_table, &kv, 1 /* is_add */ );
-       }
-    }
 
-  if (result0->fields.filter)
-    {
-      /* drop packet because lookup matched a filter mac entry */
-
-      if (*next0 != L2LEARN_NEXT_DROP)
+      /*
+       * TODO: may want to rate limit mac moves
+       * TODO: check global/bridge domain/interface learn limits
+       */
+      result0->fields.sw_if_index = sw_if_index0;
+      if (result0->fields.age_not)     /* The mac was provisioned */
        {
-         /* if we're not already dropping the packet, do it now */
-         b0->error = node->errors[L2LEARN_ERROR_FILTER_DROP];
-         *next0 = L2LEARN_NEXT_DROP;
+         msm->global_learn_count++;
+         result0->fields.age_not = 0;
        }
+      result0->fields.lrn_evt = (msm->client_pid != 0);
+      counter_base[L2LEARN_ERROR_MAC_MOVE] += 1;
     }
 
-done:
-  return;
+  /* Update the entry */
+  result0->fields.timestamp = timestamp;
+  result0->fields.sn.as_u16 = vnet_buffer (b0)->l2.l2fib_sn;
+
+  BVT (clib_bihash_kv) kv;
+  kv.key = key0->raw;
+  kv.value = result0->raw;
+  BV (clib_bihash_add_del) (msm->mac_table, &kv, 1 /* is_add */ );
 }
 
 
-static uword
-l2learn_node_fn (vlib_main_t * vm,
-                vlib_node_runtime_t * node, vlib_frame_t * frame)
+static_always_inline uword
+l2learn_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
+                    vlib_frame_t * frame, int do_trace)
 {
   u32 n_left_from, *from, *to_next;
   l2learn_next_t next_index;
@@ -320,7 +297,7 @@ l2learn_node_fn (vlib_main_t * vm,
          h2 = vlib_buffer_get_current (b2);
          h3 = vlib_buffer_get_current (b3);
 
-         if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
+         if (do_trace)
            {
              if (b0->flags & VLIB_BUFFER_IS_TRACED)
                {
@@ -426,8 +403,7 @@ l2learn_node_fn (vlib_main_t * vm,
 
          h0 = vlib_buffer_get_current (b0);
 
-         if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
-                            && (b0->flags & VLIB_BUFFER_IS_TRACED)))
+         if (do_trace && PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
            {
              l2learn_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
              t->sw_if_index = sw_if_index0;
@@ -461,6 +437,14 @@ l2learn_node_fn (vlib_main_t * vm,
   return frame->n_vectors;
 }
 
+static uword
+l2learn_node_fn (vlib_main_t * vm,
+                vlib_node_runtime_t * node, vlib_frame_t * frame)
+{
+  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
+    return l2learn_node_inline (vm, node, frame, 1 /* do_trace */ );
+  return l2learn_node_inline (vm, node, frame, 0 /* do_trace */ );
+}
 
 /* *INDENT-OFF* */
 VLIB_REGISTER_NODE (l2learn_node,static) = {
@@ -505,7 +489,7 @@ VLIB_NODE_FUNCTION_MULTIARCH (l2learn_node, l2learn_node_fn)
    * Set the default number of dynamically learned macs to the number
    * of buckets.
    */
-  mp->global_learn_limit = L2FIB_NUM_BUCKETS * 16;
+  mp->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
 
   return 0;
 }