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/pg/pg.h>
22 #include <vnet/ethernet/ethernet.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/hash.h>
27 #include <vnet/l2/l2_input.h>
28 #include <vnet/l2/l2_fib.h>
29 #include <vnet/l2/l2_learn.h>
30 #include <vnet/l2/l2_bd.h>
32 #include <vppinfra/bihash_template.c>
36 * @brief Ethernet MAC Address FIB Table Management.
38 * The MAC Address forwarding table for bridge-domains is called the l2fib.
39 * Entries are added automatically as part of mac learning, but MAC Addresses
40 * entries can also be added manually.
48 BVT (clib_bihash) mac_table;
50 /* convenience variables */
51 vlib_main_t *vlib_main;
52 vnet_main_t *vnet_main;
55 l2fib_main_t l2fib_main;
57 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
59 format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
61 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
62 u32 sw_if_index = va_arg (*args, u32);
63 if (sw_if_index == ~0)
64 return format (s, "N/A");
66 vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
68 return format (s, "Deleted");
70 return format (s, "%U", format_vnet_sw_interface_name, vnm,
71 vnet_get_sw_interface_safe (vnm, sw_if_index));
75 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
76 l2fib_entry_result_t ** l2fe_res)
78 l2fib_main_t *msm = &l2fib_main;
79 BVT (clib_bihash) * h = &msm->mac_table;
80 clib_bihash_bucket_t *b;
81 BVT (clib_bihash_value) * v;
82 l2fib_entry_key_t key;
83 l2fib_entry_result_t result;
86 for (i = 0; i < h->nbuckets; i++)
91 v = BV (clib_bihash_get_value) (h, b->offset);
92 for (j = 0; j < (1 << b->log2_pages); j++)
94 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
96 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
99 key.raw = v->kvp[k].key;
100 result.raw = v->kvp[k].value;
102 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
104 vec_add1 (*l2fe_key, key);
105 vec_add1 (*l2fe_res, result);
113 /** Display the contents of the l2fib. */
114 static clib_error_t *
115 show_l2fib (vlib_main_t * vm,
116 unformat_input_t * input, vlib_cli_command_t * cmd)
118 bd_main_t *bdm = &bd_main;
119 l2fib_main_t *msm = &l2fib_main;
120 l2_bridge_domain_t *bd_config;
121 BVT (clib_bihash) * h = &msm->mac_table;
122 clib_bihash_bucket_t *b;
123 BVT (clib_bihash_value) * v;
124 l2fib_entry_key_t key;
125 l2fib_entry_result_t result;
127 u64 total_entries = 0;
131 u32 bd_id, bd_index = ~0;
132 u8 now = (u8) (vlib_time_now (vm) / 60);
135 if (unformat (input, "raw"))
137 else if (unformat (input, "verbose"))
139 else if (unformat (input, "bd_index %d", &bd_index))
141 else if (unformat (input, "bd_id %d", &bd_id))
143 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
151 vlib_cli_output (vm, "no such bridge domain id");
156 for (i = 0; i < h->nbuckets; i++)
161 v = BV (clib_bihash_get_value) (h, b->offset);
162 for (j = 0; j < (1 << b->log2_pages); j++)
164 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
166 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
169 if (verbose && first_entry)
173 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
174 "Mac-Address", "BD-Idx", "If-Idx",
175 "BSN-ISN", "Age(min)", "static", "filter",
176 "bvi", "Interface-Name");
179 key.raw = v->kvp[k].key;
180 result.raw = v->kvp[k].value;
183 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
185 bd_config = vec_elt_at_index (l2input_main.bd_configs,
186 key.fields.bd_index);
188 if (bd_config->mac_age && !result.fields.static_mac)
190 i16 delta = now - result.fields.timestamp;
191 delta += delta < 0 ? 256 : 0;
192 s = format (s, "%d", delta);
198 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
199 format_ethernet_address, key.fields.mac,
201 result.fields.sw_if_index == ~0
202 ? -1 : result.fields.sw_if_index,
203 result.fields.sn.bd, result.fields.sn.swif,
204 s, result.fields.static_mac ? "*" : "-",
205 result.fields.filter ? "*" : "-",
206 result.fields.bvi ? "*" : "-",
207 format_vnet_sw_if_index_name_with_NA,
208 msm->vnet_main, result.fields.sw_if_index);
209 vec_reset_length (s);
217 if (total_entries == 0)
218 vlib_cli_output (vm, "no l2fib entries");
221 "%lld l2fib entries with %d learned (or non-static) entries",
222 total_entries, l2learn_main.global_learn_count);
225 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
226 BV (format_bihash), h, 1 /* verbose */ );
233 * This command dispays the MAC Address entries of the L2 FIB table.
234 * Output can be filtered to just get the number of MAC Addresses or display
235 * each MAC Address for all bridge domains or just a single bridge domain.
238 * Example of how to display the number of MAC Address entries in the L2
240 * @cliexstart{show l2fib}
243 * Example of how to display all the MAC Address entries in the L2
245 * @cliexstart{show l2fib verbose}
246 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
247 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
248 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
249 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
254 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
255 .path = "show l2fib",
256 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
257 .function = show_l2fib,
262 /* Remove all entries from the l2fib */
264 l2fib_clear_table (void)
266 l2fib_main_t *mp = &l2fib_main;
268 /* Remove all entries */
269 BV (clib_bihash_free) (&mp->mac_table);
270 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
271 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
272 l2learn_main.global_learn_count = 0;
275 /** Clear all entries in L2FIB.
276 * @TODO: Later we may want a way to remove only the non-static entries
278 static clib_error_t *
279 clear_l2fib (vlib_main_t * vm,
280 unformat_input_t * input, vlib_cli_command_t * cmd)
282 l2fib_clear_table ();
287 * This command clears all the MAC Address entries from the L2 FIB table.
290 * Example of how to clear the L2 FIB Table:
291 * @cliexcmd{clear l2fib}
292 * Example to show the L2 FIB Table has been cleared:
293 * @cliexstart{show l2fib verbose}
298 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
299 .path = "clear l2fib",
300 .short_help = "clear l2fib",
301 .function = clear_l2fib,
305 static inline l2fib_seq_num_t
306 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
308 l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
309 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
311 return (l2fib_seq_num_t) {
312 .swif = int_config->seq_num,
313 .bd = bd_config->seq_num,
319 * Add an entry to the l2fib.
320 * If the entry already exists then overwrite it
323 l2fib_add_entry (u64 mac, u32 bd_index,
324 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
326 l2fib_entry_key_t key;
327 l2fib_entry_result_t result;
328 __attribute__ ((unused)) u32 bucket_contents;
329 l2fib_main_t *mp = &l2fib_main;
330 BVT (clib_bihash_kv) kv;
333 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
336 result.raw = 0; /* clear all fields */
337 result.fields.sw_if_index = sw_if_index;
338 result.fields.static_mac = static_mac;
339 result.fields.filter = filter_mac;
340 result.fields.bvi = bvi_mac;
342 result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
345 kv.value = result.raw;
347 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
349 /* increment counter if dynamically learned mac */
350 if (result.fields.static_mac == 0)
352 l2learn_main.global_learn_count++;
357 * Add an entry to the L2FIB.
359 * l2fib add <mac> <bd> <intf> [static] [bvi]
360 * l2fib add <mac> <bd> filter
361 * Note that filter and bvi entries are always static
363 static clib_error_t *
364 l2fib_add (vlib_main_t * vm,
365 unformat_input_t * input, vlib_cli_command_t * cmd)
367 bd_main_t *bdm = &bd_main;
368 vnet_main_t *vnm = vnet_get_main ();
369 clib_error_t *error = 0;
373 u32 sw_if_index = ~0;
379 if (!unformat_user (input, unformat_ethernet_address, &mac))
381 error = clib_error_return (0, "expected mac address `%U'",
382 format_unformat_error, input);
386 if (!unformat (input, "%d", &bd_id))
388 error = clib_error_return (0, "expected bridge domain ID `%U'",
389 format_unformat_error, input);
393 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
396 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
401 if (unformat (input, "filter"))
411 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
413 error = clib_error_return (0, "unknown interface `%U'",
414 format_unformat_error, input);
417 if (unformat (input, "static"))
421 else if (unformat (input, "bvi"))
428 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
436 * This command adds a MAC Address entry to the L2 FIB table
437 * of an existing bridge-domain. The MAC Address can be static
438 * or dynamic. This command also allows a filter to be added,
439 * such that packets with given MAC Addresses (source mac or
440 * destination mac match) are dropped.
443 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
444 * of a bridge-domain (where 200 is the bridge-domain-id):
445 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
446 * Example of how to add a static MAC Address entry to the L2 FIB table
447 * of a bridge-domain (where 200 is the bridge-domain-id):
448 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
449 * Example of how to add a filter such that a packet with the given MAC
450 * Address will be dropped in a given bridge-domain (where 200 is the
452 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
453 * Example of show command of the provisioned MAC Addresses and filters:
454 * @cliexstart{show l2fib verbose}
455 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
456 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
457 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
458 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
463 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
465 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
466 .function = l2fib_add,
471 static clib_error_t *
472 l2fib_test_command_fn (vlib_main_t * vm,
473 unformat_input_t * input, vlib_cli_command_t * cmd)
475 clib_error_t *error = 0;
488 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
490 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
492 else if (unformat (input, "add"))
494 else if (unformat (input, "del"))
496 else if (unformat (input, "check"))
498 else if (unformat (input, "count %d", &count))
505 return clib_error_return (0, "mac not set");
507 if (is_add == 0 && is_del == 0 && is_check == 0)
508 return clib_error_return (0,
509 "noop: pick at least one of (add,del,check)");
515 for (i = 0; i < count; i++)
518 l2fib_add_entry (mac, bd_index, sw_if_index, mac,
519 filter_mac, bvi_mac);
520 tmp = clib_net_to_host_u64 (mac);
524 mac = clib_host_to_net_u64 (tmp);
530 BVT (clib_bihash_kv) kv;
531 l2fib_main_t *mp = &l2fib_main;
535 for (i = 0; i < count; i++)
538 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
539 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
541 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
544 tmp = clib_net_to_host_u64 (mac);
548 mac = clib_host_to_net_u64 (tmp);
554 for (i = 0; i < count; i++)
558 l2fib_del_entry (mac, bd_index);
560 tmp = clib_net_to_host_u64 (mac);
564 mac = clib_host_to_net_u64 (tmp);
572 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
573 * bridge domain (bridge-domain-id of 0) to be modified.
577 * Example of how to add a set of 4 sequential MAC Address entries to L2
578 * FIB table of the default bridge-domain:
579 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
581 * Show the set of 4 sequential MAC Address entries that were added:
582 * @cliexstart{show l2fib verbose}
583 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
584 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
585 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
586 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
587 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
591 * Example of how to check that the set of 4 sequential MAC Address
592 * entries were added to L2 FIB table of the default
593 * bridge-domain. Used a count of 5 to produce an error:
595 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
596 * The output of the check command is in the log files. Log file
597 * location may vary based on your OS and Version:
599 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
601 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
603 * Example of how to delete a set of 4 sequential MAC Address entries
604 * from L2 FIB table of the default bridge-domain:
605 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
609 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
610 .path = "test l2fib",
611 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
612 .function = l2fib_test_command_fn,
618 * Delete an entry from the l2fib.
619 * Return 0 if the entry was deleted, or 1 if it was not found
622 l2fib_del_entry_by_key (u64 raw_key)
625 l2fib_entry_result_t result;
626 l2fib_main_t *mp = &l2fib_main;
627 BVT (clib_bihash_kv) kv;
632 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
635 result.raw = kv.value;
637 /* decrement counter if dynamically learned mac */
638 if (result.fields.static_mac == 0)
640 if (l2learn_main.global_learn_count > 0)
642 l2learn_main.global_learn_count--;
646 /* Remove entry from hash table */
647 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
652 * Delete an entry from the l2fib.
653 * Return 0 if the entry was deleted, or 1 if it was not found
656 l2fib_del_entry (u64 mac, u32 bd_index)
658 return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
662 * Delete an entry from the L2FIB.
664 * l2fib del <mac> <bd-id>
666 static clib_error_t *
667 l2fib_del (vlib_main_t * vm,
668 unformat_input_t * input, vlib_cli_command_t * cmd)
670 bd_main_t *bdm = &bd_main;
671 clib_error_t *error = 0;
677 if (!unformat_user (input, unformat_ethernet_address, &mac))
679 error = clib_error_return (0, "expected mac address `%U'",
680 format_unformat_error, input);
684 if (!unformat (input, "%d", &bd_id))
686 error = clib_error_return (0, "expected bridge domain ID `%U'",
687 format_unformat_error, input);
691 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
694 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
699 /* Delete the entry */
700 if (l2fib_del_entry (mac, bd_index))
702 error = clib_error_return (0, "mac entry not found");
711 * This command deletes an existing MAC Address entry from the L2 FIB
712 * table of an existing bridge-domain.
715 * 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):
716 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
719 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
721 .short_help = "l2fib del <mac> <bridge-domain-id>",
722 .function = l2fib_del,
727 Kick off ager to scan MACs to age/delete MAC entries
730 l2fib_start_ager_scan (vlib_main_t * vm)
732 l2_bridge_domain_t *bd_config;
735 /* check if there is at least one bd with mac aging enabled */
736 vec_foreach (bd_config, l2input_main.bd_configs)
737 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
740 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
741 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
742 L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
746 Flush all non static MACs from an interface
749 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
751 l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
752 int_config->seq_num += 1;
753 l2fib_start_ager_scan (vm);
757 Flush all non static MACs in a bridge domain
760 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
762 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
763 bd_config->seq_num += 1;
764 l2fib_start_ager_scan (vm);
768 Flush all non static MACs - flushes all valid BDs
771 l2fib_flush_all_mac (vlib_main_t * vm)
773 l2_bridge_domain_t *bd_config;
774 vec_foreach (bd_config, l2input_main.bd_configs)
775 if (bd_is_valid (bd_config))
776 bd_config->seq_num += 1;
778 l2fib_start_ager_scan (vm);
783 Flush MACs, except static ones, associated with an interface
785 l2fib flush-mac interface <if-name>
787 static clib_error_t *
788 l2fib_flush_mac_int (vlib_main_t * vm,
789 unformat_input_t * input, vlib_cli_command_t * cmd)
791 vnet_main_t *vnm = vnet_get_main ();
792 clib_error_t *error = 0;
795 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
797 error = clib_error_return (0, "unknown interface `%U'",
798 format_unformat_error, input);
802 l2fib_flush_int_mac (vm, sw_if_index);
809 Flush all MACs, except static ones
813 static clib_error_t *
814 l2fib_flush_mac_all (vlib_main_t * vm,
815 unformat_input_t * input, vlib_cli_command_t * cmd)
817 l2fib_flush_all_mac (vm);
822 * This command kick off ager to delete all existing MAC Address entries,
823 * except static ones, associated with an interface from the L2 FIB table.
826 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
827 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
830 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
831 .path = "l2fib flush-mac all",
832 .short_help = "l2fib flush-mac all",
833 .function = l2fib_flush_mac_all,
838 * This command kick off ager to delete all existing MAC Address entries,
839 * except static ones, associated with an interface from the L2 FIB table.
842 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
843 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
846 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
847 .path = "l2fib flush-mac interface",
848 .short_help = "l2fib flush-mac interface <if-name>",
849 .function = l2fib_flush_mac_int,
854 Flush bridge-domain MACs except static ones.
856 l2fib flush-mac bridge-domain <bd-id>
858 static clib_error_t *
859 l2fib_flush_mac_bd (vlib_main_t * vm,
860 unformat_input_t * input, vlib_cli_command_t * cmd)
862 bd_main_t *bdm = &bd_main;
863 clib_error_t *error = 0;
867 if (!unformat (input, "%d", &bd_id))
869 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
870 format_unformat_error, input);
874 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
878 return clib_error_return (0, "No such bridge domain %d", bd_id);
880 l2fib_flush_bd_mac (vm, bd_index);
887 * This command kick off ager to delete all existing MAC Address entries,
888 * except static ones, in a bridge domain from the L2 FIB table.
891 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
892 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
895 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
896 .path = "l2fib flush-mac bridge-domain",
897 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
898 .function = l2fib_flush_mac_bd,
903 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
905 l2_input_config_t *config = l2input_intf_config (sw_if_index);
906 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
907 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
911 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
913 BVT (clib_bihash) * get_mac_table (void)
915 l2fib_main_t *mp = &l2fib_main;
916 return &mp->mac_table;
920 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
923 uword event_type, *event_data = 0;
924 l2fib_main_t *msm = &l2fib_main;
926 f64 start_time, last_run_duration = 0, t;
931 vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
933 vlib_process_wait_for_event (vm);
935 event_type = vlib_process_get_events (vm, &event_data);
936 vec_reset_length (event_data);
942 case L2_MAC_AGE_PROCESS_EVENT_START:
945 case L2_MAC_AGE_PROCESS_EVENT_STOP:
948 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
954 last_run_duration = start_time = vlib_time_now (vm);
956 BVT (clib_bihash) * h = &msm->mac_table;
958 for (i = 0; i < h->nbuckets; i++)
960 /* Allow no more than 10us without a pause */
961 t = vlib_time_now (vm);
962 if (t > start_time + 10e-6)
964 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
965 start_time = vlib_time_now (vm);
968 if (i < (h->nbuckets - 3))
970 clib_bihash_bucket_t *b = &h->buckets[i + 3];
971 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
972 b = &h->buckets[i + 1];
975 BVT (clib_bihash_value) * v =
976 BV (clib_bihash_get_value) (h, b->offset);
977 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
981 clib_bihash_bucket_t *b = &h->buckets[i];
984 BVT (clib_bihash_value) * v =
985 BV (clib_bihash_get_value) (h, b->offset);
986 for (j = 0; j < (1 << b->log2_pages); j++)
988 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
990 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
993 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
994 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
996 if (result.fields.static_mac)
999 u32 bd_index = key.fields.bd_index;
1000 u32 sw_if_index = result.fields.sw_if_index;
1001 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1002 if (result.fields.sn.as_u16 != sn)
1004 l2fib_del_entry_by_key (key.raw);
1007 l2_bridge_domain_t *bd_config =
1008 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1010 if (bd_config->mac_age == 0)
1014 (u8) (start_time / 60) - result.fields.timestamp;
1015 delta += delta < 0 ? 256 : 0;
1017 if (delta > bd_config->mac_age)
1018 l2fib_del_entry_by_key (key.raw);
1023 last_run_duration = vlib_time_now (vm) - last_run_duration;
1029 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1030 .function = l2fib_mac_age_scanner_process,
1031 .type = VLIB_NODE_TYPE_PROCESS,
1032 .name = "l2fib-mac-age-scanner-process",
1037 l2fib_init (vlib_main_t * vm)
1039 l2fib_main_t *mp = &l2fib_main;
1040 l2fib_entry_key_t test_key;
1044 mp->vnet_main = vnet_get_main ();
1046 /* Create the hash table */
1047 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1048 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
1050 /* verify the key constructor is good, since it is endian-sensitive */
1051 memset (test_mac, 0, sizeof (test_mac));
1054 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1055 ASSERT (test_key.fields.mac[0] == 0x11);
1056 ASSERT (test_key.fields.bd_index == 0x1234);
1061 VLIB_INIT_FUNCTION (l2fib_init);
1064 * fd.io coding-style-patch-verification: ON
1067 * eval: (c-set-style "gnu")