2 * l2_fib.c : layer 2 forwarding table (aka mac table)
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <vlib/vlib.h>
20 #include <vnet/vnet.h>
21 #include <vnet/ethernet/ethernet.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/hash.h>
26 #include <vnet/l2/l2_input.h>
27 #include <vnet/l2/l2_fib.h>
28 #include <vnet/l2/l2_learn.h>
29 #include <vnet/l2/l2_bd.h>
31 #include <vppinfra/bihash_template.c>
33 #include <vlibmemory/api.h>
34 #include <vnet/vnet_msg_enum.h>
36 #define vl_typedefs /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 #define vl_endianfun /* define message structures */
41 #include <vnet/vnet_all_api_h.h>
46 * @brief Ethernet MAC Address FIB Table Management.
48 * The MAC Address forwarding table for bridge-domains is called the l2fib.
49 * Entries are added automatically as part of mac learning, but MAC Addresses
50 * entries can also be added manually.
54 l2fib_main_t l2fib_main;
57 format_l2fib_entry_result_flags (u8 * s, va_list * args)
59 l2fib_entry_result_flags_t flags = va_arg (*args, int);
61 if (L2FIB_ENTRY_RESULT_FLAG_NONE == flags)
63 s = format (s, "none");
68 if (flags & L2FIB_ENTRY_RESULT_FLAG_##a) \
69 s = format (s, "%s ", t); \
71 foreach_l2fib_entry_result_attr
78 incr_mac_address (u8 * mac)
80 u64 tmp = *((u64 *) mac);
81 tmp = clib_net_to_host_u64 (tmp);
82 tmp += 1 << 16; /* skip unused (least significant) octets */
83 tmp = clib_host_to_net_u64 (tmp);
85 clib_memcpy_fast (mac, &tmp, 6);
88 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
90 format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
92 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
93 u32 sw_if_index = va_arg (*args, u32);
94 if (sw_if_index == ~0)
95 return format (s, "N/A");
97 vnet_sw_interface_t *swif =
98 vnet_get_sw_interface_or_null (vnm, sw_if_index);
100 return format (s, "Stale");
102 return format (s, "%U", format_vnet_sw_interface_name, vnm,
103 vnet_get_sw_interface_or_null (vnm, sw_if_index));
106 typedef struct l2fib_dump_walk_ctx_t_
109 l2fib_entry_key_t *l2fe_key;
110 l2fib_entry_result_t *l2fe_res;
111 } l2fib_dump_walk_ctx_t;
114 l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
116 l2fib_dump_walk_ctx_t *ctx = arg;
117 l2fib_entry_result_t result;
118 l2fib_entry_key_t key;
121 result.raw = kvp->value;
123 if ((ctx->bd_index == ~0) || (ctx->bd_index == key.fields.bd_index))
125 vec_add1 (ctx->l2fe_key, key);
126 vec_add1 (ctx->l2fe_res, result);
129 return (BIHASH_WALK_CONTINUE);
133 l2fib_table_dump (u32 bd_index,
134 l2fib_entry_key_t ** l2fe_key,
135 l2fib_entry_result_t ** l2fe_res)
137 l2fib_main_t *msm = &l2fib_main;
138 l2fib_dump_walk_ctx_t ctx = {
139 .bd_index = bd_index,
142 BV (clib_bihash_foreach_key_value_pair)
143 (&msm->mac_table, l2fib_dump_walk_cb, &ctx);
145 *l2fe_key = ctx.l2fe_key;
146 *l2fe_res = ctx.l2fe_res;
149 typedef struct l2fib_show_walk_ctx_t_
160 } l2fib_show_walk_ctx_t;
163 l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
165 l2fib_show_walk_ctx_t *ctx = arg;
166 l2_bridge_domain_t *bd_config;
167 l2fib_entry_result_t result;
168 l2fib_entry_key_t key;
170 if (ctx->verbose && ctx->first_entry)
172 ctx->first_entry = 0;
173 vlib_cli_output (ctx->vm,
174 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
175 "Mac-Address", "BD-Idx", "If-Idx",
176 "BSN-ISN", "Age(min)", "static", "filter",
177 "bvi", "Interface-Name");
181 result.raw = kvp->value;
182 ctx->total_entries++;
185 ((ctx->bd_index >> 31) || (ctx->bd_index == key.fields.bd_index)))
189 if (ctx->learn && l2fib_entry_result_is_set_AGE_NOT (&result))
190 return (BIHASH_WALK_CONTINUE); /* skip provisioned macs */
192 if (ctx->add && !l2fib_entry_result_is_set_AGE_NOT (&result))
193 return (BIHASH_WALK_CONTINUE); /* skip learned macs */
195 bd_config = vec_elt_at_index (l2input_main.bd_configs,
196 key.fields.bd_index);
198 if (l2fib_entry_result_is_set_AGE_NOT (&result))
199 s = format (s, "no");
200 else if (bd_config->mac_age == 0)
204 i16 delta = ctx->now - result.fields.timestamp;
205 delta += delta < 0 ? 256 : 0;
206 s = format (s, "%d", delta);
209 vlib_cli_output (ctx->vm,
210 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
211 format_ethernet_address, key.fields.mac,
213 result.fields.sw_if_index == ~0
214 ? -1 : result.fields.sw_if_index,
215 result.fields.sn.bd, result.fields.sn.swif, s,
216 l2fib_entry_result_is_set_STATIC (&result) ? "*" : "-",
217 l2fib_entry_result_is_set_FILTER (&result) ? "*" : "-",
218 l2fib_entry_result_is_set_BVI (&result) ? "*" : "-",
219 format_vnet_sw_if_index_name_with_NA,
220 ctx->vnm, result.fields.sw_if_index);
224 return (BIHASH_WALK_CONTINUE);
227 /** Display the contents of the l2fib. */
228 static clib_error_t *
229 show_l2fib (vlib_main_t * vm,
230 unformat_input_t * input, vlib_cli_command_t * cmd)
232 bd_main_t *bdm = &bd_main;
233 l2fib_main_t *msm = &l2fib_main;
236 l2fib_show_walk_ctx_t ctx = {
239 .now = (u8) (vlib_time_now (vm) / 60),
241 .vnm = msm->vnet_main,
244 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
246 if (unformat (input, "raw"))
252 else if (unformat (input, "verbose"))
254 else if (unformat (input, "all"))
256 else if (unformat (input, "bd_index %d", &ctx.bd_index))
258 else if (unformat (input, "learn"))
264 else if (unformat (input, "add"))
270 else if (unformat (input, "bd_id %d", &bd_id))
272 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
279 return clib_error_return (0,
280 "bridge domain id %d doesn't exist\n",
287 if (msm->mac_table_initialized == 0)
289 vlib_cli_output (vm, "no l2fib entries");
293 BV (clib_bihash_foreach_key_value_pair)
294 (&msm->mac_table, l2fib_show_walk_cb, &ctx);
296 if (ctx.total_entries == 0)
297 vlib_cli_output (vm, "no l2fib entries");
300 l2learn_main_t *lm = &l2learn_main;
301 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
302 "Last scan time: %.4esec Learn limit: %d ",
303 ctx.total_entries, lm->global_learn_count,
304 msm->age_scan_duration, lm->global_learn_limit);
306 vlib_cli_output (vm, "L2MAC events client PID: %d "
307 "Last e-scan time: %.4esec Delay: %.2esec "
308 "Max macs in event: %d",
309 lm->client_pid, msm->evt_scan_duration,
310 msm->event_scan_delay, msm->max_macs_in_event);
314 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
315 BV (format_bihash), &msm->mac_table, 1 /* verbose */ );
321 * This command displays the MAC Address entries of the L2 FIB table.
322 * Output can be filtered to just get the number of MAC Addresses or display
323 * each MAC Address for all bridge domains or just a single bridge domain.
326 * Example of how to display the number of MAC Address entries in the L2
328 * @cliexstart{show l2fib}
331 * Example of how to display all the MAC Address entries in the L2
333 * @cliexstart{show l2fib all}
334 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
335 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
336 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
337 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
342 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
343 .path = "show l2fib",
344 .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]",
345 .function = show_l2fib,
350 l2fib_table_init (void)
352 l2fib_main_t *mp = &l2fib_main;
354 if (mp->mac_table_initialized == 1)
357 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
358 mp->mac_table_n_buckets, mp->mac_table_memory_size);
359 mp->mac_table_initialized = 1;
362 /* Remove all entries from the l2fib */
364 l2fib_clear_table (void)
366 l2fib_main_t *mp = &l2fib_main;
368 if (mp->mac_table_initialized == 0)
371 /* Remove all entries */
372 BV (clib_bihash_free) (&mp->mac_table);
374 l2learn_main.global_learn_count = 0;
377 /** Clear all entries in L2FIB.
378 * @TODO: Later we may want a way to remove only the non-static entries
380 static clib_error_t *
381 clear_l2fib (vlib_main_t * vm,
382 unformat_input_t * input, vlib_cli_command_t * cmd)
384 l2fib_clear_table ();
389 * This command clears all the MAC Address entries from the L2 FIB table.
392 * Example of how to clear the L2 FIB Table:
393 * @cliexcmd{clear l2fib}
394 * Example to show the L2 FIB Table has been cleared:
395 * @cliexstart{show l2fib verbose}
400 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
401 .path = "clear l2fib",
402 .short_help = "clear l2fib",
403 .function = clear_l2fib,
407 static inline l2fib_seq_num_t
408 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
410 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
412 return (l2fib_seq_num_t) {
413 .swif = *l2fib_swif_seq_num (sw_if_index),
414 .bd = bd_config->seq_num,
420 * Add an entry to the l2fib.
421 * If the entry already exists then overwrite it
424 l2fib_add_entry (const u8 * mac, u32 bd_index,
425 u32 sw_if_index, l2fib_entry_result_flags_t flags)
427 l2fib_entry_key_t key;
428 l2fib_entry_result_t result;
429 __attribute__ ((unused)) u32 bucket_contents;
430 l2fib_main_t *fm = &l2fib_main;
431 l2learn_main_t *lm = &l2learn_main;
432 BVT (clib_bihash_kv) kv;
434 if (fm->mac_table_initialized == 0)
438 key.raw = l2fib_make_key (mac, bd_index);
441 /* check if entry already exist */
442 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
444 /* decrement counter if overwriting a learned mac */
445 result.raw = kv.value;
446 if ((!l2fib_entry_result_is_set_AGE_NOT (&result))
447 && (lm->global_learn_count))
448 lm->global_learn_count--;
452 result.raw = 0; /* clear all fields */
453 result.fields.sw_if_index = sw_if_index;
454 result.fields.flags = flags;
456 /* no aging for provisioned entry */
457 l2fib_entry_result_set_AGE_NOT (&result);
459 kv.value = result.raw;
461 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
465 * Add an entry to the L2FIB.
467 * l2fib add <mac> <bd> <intf> [static] [bvi]
468 * l2fib add <mac> <bd> filter
469 * Note that filter and bvi entries are always static
471 static clib_error_t *
472 l2fib_add (vlib_main_t * vm,
473 unformat_input_t * input, vlib_cli_command_t * cmd)
475 bd_main_t *bdm = &bd_main;
476 vnet_main_t *vnm = vnet_get_main ();
477 clib_error_t *error = 0;
481 u32 sw_if_index = ~0;
483 l2fib_entry_result_flags_t flags;
485 flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
487 if (!unformat (input, "%U", unformat_ethernet_address, mac))
489 error = clib_error_return (0, "expected mac address `%U'",
490 format_unformat_error, input);
494 if (!unformat (input, "%d", &bd_id))
496 error = clib_error_return (0, "expected bridge domain ID `%U'",
497 format_unformat_error, input);
501 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
504 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
509 if (unformat (input, "filter"))
511 l2fib_add_filter_entry (mac, bd_index);
515 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
517 error = clib_error_return (0, "unknown interface `%U'",
518 format_unformat_error, input);
522 if (unformat (input, "static"))
523 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
524 else if (unformat (input, "bvi"))
525 flags |= (L2FIB_ENTRY_RESULT_FLAG_STATIC | L2FIB_ENTRY_RESULT_FLAG_BVI);
527 if (vec_len (l2input_main.configs) <= sw_if_index)
529 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
534 l2fib_add_entry (mac, bd_index, sw_if_index, flags);
541 * This command adds a MAC Address entry to the L2 FIB table
542 * of an existing bridge-domain. The MAC Address can be static
543 * or dynamic. This command also allows a filter to be added,
544 * such that packets with given MAC Addresses (source mac or
545 * destination mac match) are dropped.
548 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
549 * of a bridge-domain (where 200 is the bridge-domain-id):
550 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
551 * Example of how to add a static MAC Address entry to the L2 FIB table
552 * of a bridge-domain (where 200 is the bridge-domain-id):
553 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
554 * Example of how to add a filter such that a packet with the given MAC
555 * Address will be dropped in a given bridge-domain (where 200 is the
557 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
558 * Example of show command of the provisioned MAC Addresses and filters:
559 * @cliexstart{show l2fib verbose}
560 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
561 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
562 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
563 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
568 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
570 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
571 .function = l2fib_add,
576 static clib_error_t *
577 l2fib_test_command_fn (vlib_main_t * vm,
578 unformat_input_t * input, vlib_cli_command_t * cmd)
580 u8 mac[6], save_mac[6];
590 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
592 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
594 else if (unformat (input, "add"))
596 else if (unformat (input, "del"))
598 else if (unformat (input, "check"))
600 else if (unformat (input, "count %d", &count))
607 return clib_error_return (0, "mac not set");
609 if (is_add == 0 && is_del == 0 && is_check == 0)
610 return clib_error_return (0,
611 "noop: pick at least one of (add,del,check)");
613 clib_memcpy_fast (save_mac, mac, 6);
617 for (i = 0; i < count; i++)
619 l2fib_add_entry (mac, bd_index, sw_if_index,
620 L2FIB_ENTRY_RESULT_FLAG_NONE);
621 incr_mac_address (mac);
627 BVT (clib_bihash_kv) kv;
628 l2fib_main_t *mp = &l2fib_main;
630 if (mp->mac_table_initialized == 0)
631 return clib_error_return (0, "mac table is not initialized");
633 clib_memcpy_fast (mac, save_mac, 6);
635 for (i = 0; i < count; i++)
637 kv.key = l2fib_make_key (mac, bd_index);
638 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
640 clib_warning ("key %U AWOL", format_ethernet_address, mac);
643 incr_mac_address (mac);
649 clib_memcpy_fast (mac, save_mac, 6);
651 for (i = 0; i < count; i++)
653 l2fib_del_entry (mac, bd_index, 0);
654 incr_mac_address (mac);
662 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
663 * bridge domain (bridge-domain-id of 0) to be modified.
667 * Example of how to add a set of 4 sequential MAC Address entries to L2
668 * FIB table of the default bridge-domain:
669 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
671 * Show the set of 4 sequential MAC Address entries that were added:
672 * @cliexstart{show l2fib verbose}
673 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
674 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
675 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
676 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
677 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
681 * Example of how to check that the set of 4 sequential MAC Address
682 * entries were added to L2 FIB table of the default
683 * bridge-domain. Used a count of 5 to produce an error:
685 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
686 * The output of the check command is in the log files. Log file
687 * location may vary based on your OS and Version:
689 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
691 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
693 * Example of how to delete a set of 4 sequential MAC Address entries
694 * from L2 FIB table of the default bridge-domain:
695 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
699 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
700 .path = "test l2fib",
701 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
702 .function = l2fib_test_command_fn,
708 * Delete an entry from the l2fib.
709 * Return 0 if the entry was deleted, or 1 it was not found or if
710 * sw_if_index is non-zero and does not match that in the entry.
713 l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index)
715 l2fib_entry_result_t result;
716 l2fib_main_t *mp = &l2fib_main;
717 BVT (clib_bihash_kv) kv;
719 if (mp->mac_table_initialized == 0)
723 kv.key = l2fib_make_key (mac, bd_index);
725 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
728 result.raw = kv.value;
730 /* check if sw_if_index of entry match */
731 if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index))
734 /* decrement counter if dynamically learned mac */
735 if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) &&
736 (l2learn_main.global_learn_count))
737 l2learn_main.global_learn_count--;
739 /* Remove entry from hash table */
740 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
745 * Delete an entry from the L2FIB.
747 * l2fib del <mac> <bd-id>
749 static clib_error_t *
750 l2fib_del (vlib_main_t * vm,
751 unformat_input_t * input, vlib_cli_command_t * cmd)
753 bd_main_t *bdm = &bd_main;
754 clib_error_t *error = 0;
760 if (!unformat (input, "%U", unformat_ethernet_address, mac))
762 error = clib_error_return (0, "expected mac address `%U'",
763 format_unformat_error, input);
767 if (!unformat (input, "%d", &bd_id))
769 error = clib_error_return (0, "expected bridge domain ID `%U'",
770 format_unformat_error, input);
774 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
777 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
782 /* Delete the entry */
783 if (l2fib_del_entry (mac, bd_index, 0))
785 error = clib_error_return (0, "mac entry not found");
794 * This command deletes an existing MAC Address entry from the L2 FIB
795 * table of an existing bridge-domain.
798 * Example of how to delete a MAC Address entry from the L2 FIB table of a bridge-domain (where 200 is the bridge-domain-id):
799 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
802 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
804 .short_help = "l2fib del <mac> <bridge-domain-id> []",
805 .function = l2fib_del,
810 Kick off ager to scan MACs to age/delete MAC entries
813 l2fib_start_ager_scan (vlib_main_t * vm)
815 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
817 /* check if there is at least one bd with mac aging enabled */
818 l2_bridge_domain_t *bd_config;
819 vec_foreach (bd_config, l2input_main.bd_configs)
821 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
823 evt = L2_MAC_AGE_PROCESS_EVENT_START;
828 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
833 Flush all non static MACs from an interface
836 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
838 *l2fib_swif_seq_num (sw_if_index) += 1;
839 l2fib_start_ager_scan (vm);
843 Flush all non static MACs in a bridge domain
846 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
848 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
849 bd_config->seq_num += 1;
850 l2fib_start_ager_scan (vm);
854 Flush all non static MACs - flushes all valid BDs
857 l2fib_flush_all_mac (vlib_main_t * vm)
859 l2_bridge_domain_t *bd_config;
860 vec_foreach (bd_config, l2input_main.bd_configs)
861 if (bd_is_valid (bd_config))
862 bd_config->seq_num += 1;
864 l2fib_start_ager_scan (vm);
869 Flush MACs, except static ones, associated with an interface
871 l2fib flush-mac interface <if-name>
873 static clib_error_t *
874 l2fib_flush_mac_int (vlib_main_t * vm,
875 unformat_input_t * input, vlib_cli_command_t * cmd)
877 vnet_main_t *vnm = vnet_get_main ();
878 clib_error_t *error = 0;
881 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
883 error = clib_error_return (0, "unknown interface `%U'",
884 format_unformat_error, input);
888 l2fib_flush_int_mac (vm, sw_if_index);
895 Flush all MACs, except static ones
899 static clib_error_t *
900 l2fib_flush_mac_all (vlib_main_t * vm,
901 unformat_input_t * input, vlib_cli_command_t * cmd)
903 l2fib_flush_all_mac (vm);
908 * This command kick off ager to delete all existing MAC Address entries,
909 * except static ones, associated with an interface from the L2 FIB table.
912 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
913 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
916 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
917 .path = "l2fib flush-mac all",
918 .short_help = "l2fib flush-mac all",
919 .function = l2fib_flush_mac_all,
924 * This command kick off ager to delete all existing MAC Address entries,
925 * except static ones, associated with an interface from the L2 FIB table.
928 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
929 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
932 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
933 .path = "l2fib flush-mac interface",
934 .short_help = "l2fib flush-mac interface <if-name>",
935 .function = l2fib_flush_mac_int,
940 Flush bridge-domain MACs except static ones.
942 l2fib flush-mac bridge-domain <bd-id>
944 static clib_error_t *
945 l2fib_flush_mac_bd (vlib_main_t * vm,
946 unformat_input_t * input, vlib_cli_command_t * cmd)
948 bd_main_t *bdm = &bd_main;
949 clib_error_t *error = 0;
953 if (!unformat (input, "%d", &bd_id))
955 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
956 format_unformat_error, input);
960 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
964 return clib_error_return (0, "No such bridge domain %d", bd_id);
966 l2fib_flush_bd_mac (vm, bd_index);
973 * This command kick off ager to delete all existing MAC Address entries,
974 * except static ones, in a bridge domain from the L2 FIB table.
977 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
978 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
981 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
982 .path = "l2fib flush-mac bridge-domain",
983 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
984 .function = l2fib_flush_mac_bd,
989 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
991 l2_input_config_t *config = l2input_intf_config (sw_if_index);
992 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
993 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
997 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
999 BVT (clib_bihash) * get_mac_table (void)
1001 l2fib_main_t *mp = &l2fib_main;
1002 return &mp->mac_table;
1005 static_always_inline void *
1006 allocate_mac_evt_buf (u32 client, u32 client_index)
1008 l2fib_main_t *fm = &l2fib_main;
1009 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
1010 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
1011 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
1012 mp->pid = htonl (client);
1013 mp->client_index = client_index;
1017 static_always_inline f64
1018 l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
1020 l2fib_main_t *fm = &l2fib_main;
1021 l2learn_main_t *lm = &l2learn_main;
1023 BVT (clib_bihash) * h = &fm->mac_table;
1025 f64 last_start = start_time;
1029 u32 learn_count = 0;
1030 u32 client = lm->client_pid;
1031 u32 cl_idx = lm->client_index;
1032 vl_api_l2_macs_event_t *mp = 0;
1033 vl_api_registration_t *reg = 0;
1035 /* Don't scan the l2 fib if it hasn't been instantiated yet */
1036 if (alloc_arena (h) == 0)
1041 mp = allocate_mac_evt_buf (client, cl_idx);
1042 reg = vl_api_client_index_to_registration (lm->client_index);
1045 for (i = 0; i < h->nbuckets; i++)
1047 /* allow no more than 20us without a pause */
1048 delta_t = vlib_time_now (vm) - last_start;
1049 if (delta_t > 20e-6)
1051 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
1052 last_start = vlib_time_now (vm);
1056 if (i < (h->nbuckets - 3))
1058 BVT (clib_bihash_bucket) * b =
1059 BV (clib_bihash_get_bucket) (h, i + 3);
1060 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
1061 b = BV (clib_bihash_get_bucket) (h, i + 1);
1062 if (!BV (clib_bihash_bucket_is_empty) (b))
1064 BVT (clib_bihash_value) * v =
1065 BV (clib_bihash_get_value) (h, b->offset);
1066 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1070 BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i);
1071 if (BV (clib_bihash_bucket_is_empty) (b))
1073 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1074 for (j = 0; j < (1 << b->log2_pages); j++)
1076 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1078 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1081 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1082 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1084 if (!l2fib_entry_result_is_set_AGE_NOT (&result))
1089 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1091 /* event message full, send it and start a new one */
1092 if (reg && vl_api_can_send_msg (reg))
1094 mp->n_macs = htonl (evt_idx);
1095 vl_api_send_msg (reg, (u8 *) mp);
1096 mp = allocate_mac_evt_buf (client, cl_idx);
1101 clib_warning ("MAC event to pid %d queue stuffed!"
1102 " %d MAC entries lost", client,
1108 if (l2fib_entry_result_is_set_LRN_EVT (&result))
1110 /* copy mac entry to event msg */
1111 clib_memcpy_fast (mp->mac[evt_idx].mac_addr,
1113 mp->mac[evt_idx].action =
1114 l2fib_entry_result_is_set_LRN_MOV (&result) ?
1115 (vl_api_mac_event_action_t) MAC_EVENT_ACTION_MOVE
1116 : (vl_api_mac_event_action_t) MAC_EVENT_ACTION_ADD;
1117 mp->mac[evt_idx].action =
1118 htonl (mp->mac[evt_idx].action);
1119 mp->mac[evt_idx].sw_if_index =
1120 htonl (result.fields.sw_if_index);
1121 /* clear event bits and update mac entry */
1122 l2fib_entry_result_clear_LRN_EVT (&result);
1123 l2fib_entry_result_clear_LRN_MOV (&result);
1124 BVT (clib_bihash_kv) kv;
1126 kv.value = result.raw;
1127 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1129 continue; /* skip aging */
1133 if (event_only || l2fib_entry_result_is_set_AGE_NOT (&result))
1134 continue; /* skip aging - static_mac always age_not */
1136 /* start aging processing */
1137 u32 bd_index = key.fields.bd_index;
1138 u32 sw_if_index = result.fields.sw_if_index;
1139 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1140 if (result.fields.sn.as_u16 != sn)
1141 goto age_out; /* stale mac */
1143 l2_bridge_domain_t *bd_config =
1144 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1146 if (bd_config->mac_age == 0)
1147 continue; /* skip aging */
1149 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1150 delta += delta < 0 ? 256 : 0;
1152 if (delta < bd_config->mac_age)
1153 continue; /* still valid */
1158 /* copy mac entry to event msg */
1159 clib_memcpy_fast (mp->mac[evt_idx].mac_addr, key.fields.mac,
1161 mp->mac[evt_idx].action =
1162 (vl_api_mac_event_action_t) MAC_EVENT_ACTION_DELETE;
1163 mp->mac[evt_idx].action = htonl (mp->mac[evt_idx].action);
1164 mp->mac[evt_idx].sw_if_index =
1165 htonl (result.fields.sw_if_index);
1168 /* delete mac entry */
1169 BVT (clib_bihash_kv) kv;
1171 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1174 * Note: we may have just freed the bucket's backing
1175 * storage, so check right here...
1177 if (BV (clib_bihash_bucket_is_empty) (b))
1186 /* keep learn count consistent */
1187 l2learn_main.global_learn_count = learn_count;
1191 /* send any outstanding mac event message else free message buffer */
1194 if (reg && vl_api_can_send_msg (reg))
1196 mp->n_macs = htonl (evt_idx);
1197 vl_api_send_msg (reg, (u8 *) mp);
1202 clib_warning ("MAC event to pid %d queue stuffed!"
1203 " %d MAC entries lost", client, evt_idx);
1204 vl_msg_api_free (mp);
1208 vl_msg_api_free (mp);
1210 return delta_t + accum_t;
1214 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1217 uword event_type, *event_data = 0;
1218 l2fib_main_t *fm = &l2fib_main;
1219 l2learn_main_t *lm = &l2learn_main;
1221 f64 start_time, next_age_scan_time = CLIB_TIME_MAX;
1226 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1229 f64 t = next_age_scan_time - vlib_time_now (vm);
1230 vlib_process_wait_for_event_or_clock (vm, t);
1233 vlib_process_wait_for_event (vm);
1235 event_type = vlib_process_get_events (vm, &event_data);
1236 vec_reset_length (event_data);
1238 start_time = vlib_time_now (vm);
1240 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
1244 case ~0: /* timer expired */
1245 if (lm->client_pid != 0 && start_time < next_age_scan_time)
1246 scan = SCAN_MAC_EVENT;
1249 case L2_MAC_AGE_PROCESS_EVENT_START:
1253 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1255 scan = SCAN_DISABLE;
1258 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
1265 if (scan == SCAN_MAC_EVENT)
1266 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1269 if (scan == SCAN_MAC_AGE)
1270 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1271 if (scan == SCAN_DISABLE)
1273 l2fib_main.age_scan_duration = 0;
1274 l2fib_main.evt_scan_duration = 0;
1276 /* schedule next scan */
1278 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1280 next_age_scan_time = CLIB_TIME_MAX;
1287 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1288 .function = l2fib_mac_age_scanner_process,
1289 .type = VLIB_NODE_TYPE_PROCESS,
1290 .name = "l2fib-mac-age-scanner-process",
1295 l2fib_init (vlib_main_t * vm)
1297 l2fib_main_t *mp = &l2fib_main;
1298 l2fib_entry_key_t test_key;
1302 mp->vnet_main = vnet_get_main ();
1303 if (mp->mac_table_n_buckets == 0)
1304 mp->mac_table_n_buckets = L2FIB_NUM_BUCKETS;
1305 if (mp->mac_table_memory_size == 0)
1306 mp->mac_table_memory_size = L2FIB_MEMORY_SIZE;
1307 mp->mac_table_initialized = 0;
1309 /* verify the key constructor is good, since it is endian-sensitive */
1310 clib_memset (test_mac, 0, sizeof (test_mac));
1313 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1314 ASSERT (test_key.fields.mac[0] == 0x11);
1315 ASSERT (test_key.fields.bd_index == 0x1234);
1320 VLIB_INIT_FUNCTION (l2fib_init);
1322 static clib_error_t *
1323 lfib_config (vlib_main_t * vm, unformat_input_t * input)
1325 l2fib_main_t *lm = &l2fib_main;
1326 uword table_size = ~0;
1329 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1331 if (unformat (input, "table-size %U", unformat_memory_size,
1334 else if (unformat (input, "num-buckets %u", &n_buckets))
1337 return clib_error_return (0, "unknown input `%U'",
1338 format_unformat_error, input);
1341 if (n_buckets != ~0)
1343 if (!is_pow2 (n_buckets))
1344 return clib_error_return (0, "num-buckets must be power of 2");
1345 lm->mac_table_n_buckets = n_buckets;
1348 if (table_size != ~0)
1349 lm->mac_table_memory_size = table_size;
1353 VLIB_CONFIG_FUNCTION (lfib_config, "l2fib");
1356 * fd.io coding-style-patch-verification: ON
1359 * eval: (c-set-style "gnu")