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)
{
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" */
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_
+{
+ u32 bd_index;
+ l2fib_entry_key_t *l2fe_key;
+ l2fib_entry_result_t *l2fe_res;
+} l2fib_dump_walk_ctx_t;
+
+static int
+l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
+{
+ l2fib_dump_walk_ctx_t *ctx = arg;
+ l2fib_entry_result_t result;
+ l2fib_entry_key_t key;
+
+ key.raw = kvp->key;
+ result.raw = kvp->value;
+
+ if ((ctx->bd_index == ~0) || (ctx->bd_index == key.fields.bd_index))
+ {
+ vec_add1 (ctx->l2fe_key, key);
+ vec_add1 (ctx->l2fe_res, result);
+ }
+
+ return (BIHASH_WALK_CONTINUE);
}
void
-l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
+l2fib_table_dump (u32 bd_index,
+ l2fib_entry_key_t ** l2fe_key,
l2fib_entry_result_t ** l2fe_res)
{
l2fib_main_t *msm = &l2fib_main;
- BVT (clib_bihash) * h = &msm->mac_table;
- BVT (clib_bihash_bucket) * b;
- BVT (clib_bihash_value) * v;
- l2fib_entry_key_t key;
+ l2fib_dump_walk_ctx_t ctx = {
+ .bd_index = bd_index,
+ };
+
+ BV (clib_bihash_foreach_key_value_pair)
+ (&msm->mac_table, l2fib_dump_walk_cb, &ctx);
+
+ *l2fe_key = ctx.l2fe_key;
+ *l2fe_res = ctx.l2fe_res;
+}
+
+typedef struct l2fib_show_walk_ctx_t_
+{
+ u8 first_entry;
+ u8 verbose;
+ vlib_main_t *vm;
+ vnet_main_t *vnm;
+ u32 total_entries;
+ u32 bd_index;
+ u8 learn;
+ u8 add;
+ u8 now;
+} l2fib_show_walk_ctx_t;
+
+static int
+l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
+{
+ l2fib_show_walk_ctx_t *ctx = arg;
+ l2_bridge_domain_t *bd_config;
l2fib_entry_result_t result;
- int i, j, k;
+ l2fib_entry_key_t key;
- for (i = 0; i < h->nbuckets; i++)
+ if (ctx->verbose && ctx->first_entry)
{
- 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;
+ ctx->first_entry = 0;
+ vlib_cli_output (ctx->vm,
+ "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
+ "Mac-Address", "BD-Idx", "If-Idx",
+ "BSN-ISN", "Age(min)", "static", "filter",
+ "bvi", "Interface-Name");
+ }
- key.raw = v->kvp[k].key;
- result.raw = v->kvp[k].value;
+ key.raw = kvp->key;
+ result.raw = kvp->value;
+ ctx->total_entries++;
- if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
- {
- vec_add1 (*l2fe_key, key);
- vec_add1 (*l2fe_res, result);
- }
- }
- v++;
+ if (ctx->verbose &&
+ ((ctx->bd_index >> 31) || (ctx->bd_index == key.fields.bd_index)))
+ {
+ u8 *s = NULL;
+
+ if (ctx->learn && l2fib_entry_result_is_set_AGE_NOT (&result))
+ return (BIHASH_WALK_CONTINUE); /* skip provisioned macs */
+
+ if (ctx->add && !l2fib_entry_result_is_set_AGE_NOT (&result))
+ return (BIHASH_WALK_CONTINUE); /* skip learned macs */
+
+ bd_config = vec_elt_at_index (l2input_main.bd_configs,
+ key.fields.bd_index);
+
+ if (l2fib_entry_result_is_set_AGE_NOT (&result))
+ s = format (s, "no");
+ else if (bd_config->mac_age == 0)
+ s = format (s, "-");
+ else
+ {
+ i16 delta = ctx->now - result.fields.timestamp;
+ delta += delta < 0 ? 256 : 0;
+ s = format (s, "%d", delta);
}
+
+ vlib_cli_output (ctx->vm,
+ "%=19U%=7d%=7d %3d/%-3d%=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,
+ l2fib_entry_result_is_set_STATIC (&result) ? "*" : "-",
+ l2fib_entry_result_is_set_FILTER (&result) ? "*" : "-",
+ l2fib_entry_result_is_set_BVI (&result) ? "*" : "-",
+ format_vnet_sw_if_index_name_with_NA,
+ ctx->vnm, result.fields.sw_if_index);
+ vec_free (s);
}
+
+ return (BIHASH_WALK_CONTINUE);
}
/** Display the contents of the l2fib. */
{
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;
- BVT (clib_bihash_bucket) * b;
- BVT (clib_bihash_value) * v;
- l2fib_entry_key_t key;
- l2fib_entry_result_t result;
- u32 first_entry = 1;
- u64 total_entries = 0;
- int i, j, k;
- u8 verbose = 0;
u8 raw = 0;
- u8 learn = 0;
- u32 bd_id, bd_index = ~0;
- u8 now = (u8) (vlib_time_now (vm) / 60);
- u8 *s = 0;
-
- if (unformat (input, "raw"))
- raw = 1;
- else if (unformat (input, "verbose"))
- verbose = 1;
- else if (unformat (input, "bd_index %d", &bd_index))
- verbose = 1;
- else if (unformat (input, "learn"))
- {
- learn = 1;
- verbose = 0;
- }
- else if (unformat (input, "bd_id %d", &bd_id))
+ u32 bd_id;
+ l2fib_show_walk_ctx_t ctx = {
+ .first_entry = 1,
+ .bd_index = ~0,
+ .now = (u8) (vlib_time_now (vm) / 60),
+ .vm = vm,
+ .vnm = msm->vnet_main,
+ };
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
{
- uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
- if (p)
+ if (unformat (input, "raw"))
{
- if (learn == 0)
- verbose = 1;
- bd_index = p[0];
+ raw = 1;
+ ctx.verbose = 0;
+ break;
}
- else
+ else if (unformat (input, "verbose"))
+ ctx.verbose = 1;
+ else if (unformat (input, "all"))
+ ctx.verbose = 1;
+ else if (unformat (input, "bd_index %d", &ctx.bd_index))
+ ctx.verbose = 1;
+ else if (unformat (input, "learn"))
{
- vlib_cli_output (vm, "no such bridge domain id");
- return 0;
+ ctx.add = 0;
+ ctx.learn = 1;
+ ctx.verbose = 1;
}
- }
-
- for (i = 0; i < h->nbuckets; i++)
- {
- 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++)
+ else if (unformat (input, "add"))
{
- for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
+ ctx.learn = 0;
+ ctx.add = 1;
+ ctx.verbose = 1;
+ }
+ else if (unformat (input, "bd_id %d", &bd_id))
+ {
+ uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
+ if (p)
{
- if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
- continue;
-
- if ((verbose || learn) && first_entry)
- {
- first_entry = 0;
- vlib_cli_output (vm,
- "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
- "Mac-Address", "BD-Idx", "If-Idx",
- "BSN-ISN", "Age(min)", "static", "filter",
- "bvi", "Interface-Name");
- }
-
- key.raw = v->kvp[k].key;
- result.raw = v->kvp[k].value;
-
- if ((verbose || learn)
- & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
- {
- if (learn && result.fields.age_not)
- {
- total_entries++;
- continue; /* skip provisioned macs */
- }
-
- bd_config = vec_elt_at_index (l2input_main.bd_configs,
- key.fields.bd_index);
-
- if (bd_config->mac_age && !result.fields.age_not)
- {
- i16 delta = now - result.fields.timestamp;
- delta += delta < 0 ? 256 : 0;
- s = format (s, "%d", delta);
- }
- else
- s = format (s, "-");
-
- vlib_cli_output (vm,
- "%=19U%=7d%=7d %3d/%-3d%=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, result.fields.static_mac ? "*" : "-",
- result.fields.filter ? "*" : "-",
- result.fields.bvi ? "*" : "-",
- format_vnet_sw_if_index_name_with_NA,
- msm->vnet_main, result.fields.sw_if_index);
- vec_reset_length (s);
- }
- total_entries++;
+ ctx.verbose = 1;
+ ctx.bd_index = p[0];
}
- v++;
+ else
+ return clib_error_return (0,
+ "bridge domain id %d doesn't exist\n",
+ bd_id);
}
+ else
+ break;
}
- if (total_entries == 0)
+ BV (clib_bihash_foreach_key_value_pair)
+ (&msm->mac_table, l2fib_show_walk_cb, &ctx);
+
+ if (ctx.total_entries == 0)
vlib_cli_output (vm, "no l2fib entries");
else
{
l2learn_main_t *lm = &l2learn_main;
vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
"Last scan time: %.4esec Learn limit: %d ",
- total_entries, lm->global_learn_count,
+ ctx.total_entries, lm->global_learn_count,
msm->age_scan_duration, lm->global_learn_limit);
if (lm->client_pid)
vlib_cli_output (vm, "L2MAC events client PID: %d "
if (raw)
vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
- BV (format_bihash), h, 1 /* verbose */ );
+ BV (format_bihash), &msm->mac_table, 1 /* verbose */ );
- vec_free (s);
return 0;
}
/*?
- * 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.
*
* @cliexend
* Example of how to display all the MAC Address entries in the L2
* FIB table:
- * @cliexstart{show l2fib verbose}
+ * @cliexstart{show l2fib all}
* Mac Address BD Idx Interface Index static filter bvi refresh timestamp
* 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
* 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
.path = "show l2fib",
- .short_help = "show l2fib [verbose | learn | bd_id <nn> | bd_index <nn> | raw",
+ .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]",
.function = show_l2fib,
};
/* *INDENT-ON* */
* If the entry already exists then overwrite it
*/
void
-l2fib_add_entry (u8 * mac, u32 bd_index,
- u32 sw_if_index, u8 static_mac, u8 filter_mac, u8 bvi_mac)
+l2fib_add_entry (const u8 * mac, u32 bd_index,
+ u32 sw_if_index, l2fib_entry_result_flags_t flags)
{
l2fib_entry_key_t key;
l2fib_entry_result_t result;
/* 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 */
result.raw = kv.value;
- if ((result.fields.age_not == 0) && (lm->global_learn_count))
+ if ((!l2fib_entry_result_is_set_AGE_NOT (&result))
+ && (lm->global_learn_count))
lm->global_learn_count--;
}
/* set up result */
result.raw = 0; /* clear all fields */
result.fields.sw_if_index = sw_if_index;
- result.fields.static_mac = static_mac;
- result.fields.filter = filter_mac;
- result.fields.bvi = bvi_mac;
- result.fields.age_not = 1; /* no aging for provisioned entry */
+ result.fields.flags = flags;
+
+ /* 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 */ );
u32 bd_id;
u32 bd_index;
u32 sw_if_index = ~0;
- u32 static_mac = 0;
- u32 bvi_mac = 0;
uword *p;
+ l2fib_entry_result_flags_t flags;
+
+ flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
if (!unformat (input, "%U", unformat_ethernet_address, mac))
{
}
if (unformat (input, "static"))
- {
- static_mac = 1;
- }
+ flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
else if (unformat (input, "bvi"))
- {
- bvi_mac = 1;
- static_mac = 1;
- }
+ flags |= (L2FIB_ENTRY_RESULT_FLAG_STATIC | L2FIB_ENTRY_RESULT_FLAG_BVI);
if (vec_len (l2input_main.configs) <= sw_if_index)
{
goto done;
}
- l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac);
+ l2fib_add_entry (mac, bd_index, sw_if_index, flags);
done:
return error;
u8 mac[6], save_mac[6];
u32 bd_index = 0;
u32 sw_if_index = 8;
- u32 bvi_mac = 0;
u32 is_add = 0;
u32 is_del = 0;
u32 is_check = 0;
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)
{
for (i = 0; i < count; i++)
{
- l2fib_add_fwd_entry (mac, bd_index, sw_if_index, *mac, bvi_mac);
+ l2fib_add_entry (mac, bd_index, sw_if_index,
+ L2FIB_ENTRY_RESULT_FLAG_NONE);
incr_mac_address (mac);
}
}
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++)
{
if (is_del)
{
- clib_memcpy (mac, save_mac, 6);
+ clib_memcpy_fast (mac, save_mac, 6);
for (i = 0; i < count; i++)
{
- l2fib_del_entry (mac, bd_index);
+ l2fib_del_entry (mac, bd_index, 0);
incr_mac_address (mac);
}
}
/**
* Delete an entry from the l2fib.
- * Return 0 if the entry was deleted, or 1 if it was not found
+ * Return 0 if the entry was deleted, or 1 it was not found or if
+ * sw_if_index is non-zero and does not match that in the entry.
*/
-static u32
-l2fib_del_entry_by_key (u64 raw_key)
+u32
+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;
/* set up key */
- kv.key = raw_key;
+ kv.key = l2fib_make_key (mac, bd_index);
if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
return 1;
result.raw = kv.value;
+ /* check if sw_if_index of entry match */
+ if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index))
+ return 1;
+
/* decrement counter if dynamically learned mac */
- if ((result.fields.age_not == 0) && (l2learn_main.global_learn_count))
+ if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) &&
+ (l2learn_main.global_learn_count))
l2learn_main.global_learn_count--;
/* Remove entry from hash table */
return 0;
}
-/**
- * Delete an entry from the l2fib.
- * Return 0 if the entry was deleted, or 1 if it was not found
- */
-u32
-l2fib_del_entry (u8 * mac, u32 bd_index)
-{
- return l2fib_del_entry_by_key (l2fib_make_key (mac, bd_index));
-}
-
/**
* Delete an entry from the L2FIB.
* The CLI format is:
bd_index = p[0];
/* Delete the entry */
- if (l2fib_del_entry (mac, bd_index))
+ if (l2fib_del_entry (mac, bd_index, 0))
{
error = clib_error_return (0, "mac entry not found");
goto done;
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
.path = "l2fib del",
- .short_help = "l2fib del <mac> <bridge-domain-id>",
+ .short_help = "l2fib del <mac> <bridge-domain-id> []",
.function = l2fib_del,
};
/* *INDENT-ON* */
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);
l2fib_entry_key_t key = {.raw = v->kvp[k].key };
l2fib_entry_result_t result = {.raw = v->kvp[k].value };
- if (result.fields.age_not == 0)
+ if (!l2fib_entry_result_is_set_AGE_NOT (&result))
learn_count++;
if (client)
evt_idx = 0;
}
- if (result.fields.lrn_evt)
+ 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);
- mp->mac[evt_idx].action = result.fields.lrn_mov ?
- MAC_EVENT_ACTION_MOVE : MAC_EVENT_ACTION_ADD;
+ 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) ?
+ (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 */
- result.fields.lrn_evt = 0;
- result.fields.lrn_mov = 0;
+ l2fib_entry_result_clear_LRN_EVT (&result);
+ l2fib_entry_result_clear_LRN_MOV (&result);
BVT (clib_bihash_kv) kv;
kv.key = key.raw;
kv.value = result.raw;
}
}
- if (event_only || result.fields.age_not)
- continue; /* skip aging - static_mac alsways age_not */
+ if (event_only || l2fib_entry_result_is_set_AGE_NOT (&result))
+ continue; /* skip aging - static_mac always age_not */
/* start aging processing */
u32 bd_index = key.fields.bd_index;
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++;
kv.key = key.raw;
BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
learn_count--;
+ /*
+ * Note: we may have just freed the bucket's backing
+ * storage, so check right here...
+ */
+ if (b->offset == 0)
+ goto doublebreak;
}
v++;
}
+ doublebreak:
+ ;
}
/* keep learn count consistent */
L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
/* 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);