l2: API cleanup
[vpp.git] / src / vnet / l2 / l2_fib.c
index 46c041e..160e4e6 100644 (file)
 
 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 +83,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 +95,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 +111,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 +126,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 +160,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 +188,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 +221,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. */
@@ -388,6 +414,7 @@ l2fib_add_entry (const u8 * mac, u32 bd_index,
 
   /* 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))
@@ -407,7 +434,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 */ );
@@ -563,7 +589,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 +606,7 @@ 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);
+      clib_memcpy_fast (mac, save_mac, 6);
 
       for (i = 0; i < count; i++)
        {
@@ -596,7 +622,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++)
        {
@@ -979,6 +1005,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);
@@ -1050,11 +1080,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 */
@@ -1095,8 +1128,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++;