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 return format (s, "%U",
67 format_vnet_sw_interface_name, vnm,
68 vnet_get_sw_interface (vnm, sw_if_index));
72 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
73 l2fib_entry_result_t ** l2fe_res)
75 l2fib_main_t *msm = &l2fib_main;
76 BVT (clib_bihash) * h = &msm->mac_table;
77 clib_bihash_bucket_t *b;
78 BVT (clib_bihash_value) * v;
79 l2fib_entry_key_t key;
80 l2fib_entry_result_t result;
83 for (i = 0; i < h->nbuckets; i++)
88 v = BV (clib_bihash_get_value) (h, b->offset);
89 for (j = 0; j < (1 << b->log2_pages); j++)
91 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
93 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
96 key.raw = v->kvp[k].key;
97 result.raw = v->kvp[k].value;
99 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
101 vec_add1 (*l2fe_key, key);
102 vec_add1 (*l2fe_res, result);
110 /** Display the contents of the l2fib. */
111 static clib_error_t *
112 show_l2fib (vlib_main_t * vm,
113 unformat_input_t * input, vlib_cli_command_t * cmd)
115 bd_main_t *bdm = &bd_main;
116 l2fib_main_t *msm = &l2fib_main;
117 l2_bridge_domain_t *bd_config;
118 BVT (clib_bihash) * h = &msm->mac_table;
119 clib_bihash_bucket_t *b;
120 BVT (clib_bihash_value) * v;
121 l2fib_entry_key_t key;
122 l2fib_entry_result_t result;
124 u64 total_entries = 0;
128 u32 bd_id, bd_index = ~0;
129 u8 now = (u8) (vlib_time_now (vm) / 60);
132 if (unformat (input, "raw"))
134 else if (unformat (input, "verbose"))
136 else if (unformat (input, "bd_index %d", &bd_index))
138 else if (unformat (input, "bd_id %d", &bd_id))
140 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
148 vlib_cli_output (vm, "no such bridge domain id");
153 for (i = 0; i < h->nbuckets; i++)
158 v = BV (clib_bihash_get_value) (h, b->offset);
159 for (j = 0; j < (1 << b->log2_pages); j++)
161 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
163 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
166 if (verbose && first_entry)
170 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
171 "Mac-Address", "BD-Idx", "If-Idx",
172 "BSN-ISN", "Age(min)", "static", "filter",
173 "bvi", "Interface-Name");
176 key.raw = v->kvp[k].key;
177 result.raw = v->kvp[k].value;
180 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
182 bd_config = vec_elt_at_index (l2input_main.bd_configs,
183 key.fields.bd_index);
185 if (bd_config->mac_age && !result.fields.static_mac)
187 i16 delta = now - result.fields.timestamp;
188 delta += delta < 0 ? 256 : 0;
189 s = format (s, "%d", delta);
195 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
196 format_ethernet_address, key.fields.mac,
198 result.fields.sw_if_index == ~0
199 ? -1 : result.fields.sw_if_index,
200 result.fields.sn.bd, result.fields.sn.swif,
201 s, result.fields.static_mac ? "*" : "-",
202 result.fields.filter ? "*" : "-",
203 result.fields.bvi ? "*" : "-",
204 format_vnet_sw_if_index_name_with_NA,
205 msm->vnet_main, result.fields.sw_if_index);
206 vec_reset_length (s);
214 if (total_entries == 0)
215 vlib_cli_output (vm, "no l2fib entries");
218 "%lld l2fib entries with %d learned (or non-static) entries",
219 total_entries, l2learn_main.global_learn_count);
222 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
223 BV (format_bihash), h, 1 /* verbose */ );
230 * This command dispays the MAC Address entries of the L2 FIB table.
231 * Output can be filtered to just get the number of MAC Addresses or display
232 * each MAC Address for all bridge domains or just a single bridge domain.
235 * Example of how to display the number of MAC Address entries in the L2
237 * @cliexstart{show l2fib}
240 * Example of how to display all the MAC Address entries in the L2
242 * @cliexstart{show l2fib verbose}
243 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
244 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
245 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
246 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
251 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
252 .path = "show l2fib",
253 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
254 .function = show_l2fib,
259 /* Remove all entries from the l2fib */
261 l2fib_clear_table (void)
263 l2fib_main_t *mp = &l2fib_main;
265 /* Remove all entries */
266 BV (clib_bihash_free) (&mp->mac_table);
267 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
268 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
269 l2learn_main.global_learn_count = 0;
272 /** Clear all entries in L2FIB.
273 * @TODO: Later we may want a way to remove only the non-static entries
275 static clib_error_t *
276 clear_l2fib (vlib_main_t * vm,
277 unformat_input_t * input, vlib_cli_command_t * cmd)
279 l2fib_clear_table ();
284 * This command clears all the MAC Address entries from the L2 FIB table.
287 * Example of how to clear the L2 FIB Table:
288 * @cliexcmd{clear l2fib}
289 * Example to show the L2 FIB Table has been cleared:
290 * @cliexstart{show l2fib verbose}
295 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
296 .path = "clear l2fib",
297 .short_help = "clear l2fib",
298 .function = clear_l2fib,
302 static inline l2fib_seq_num_t
303 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
305 l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
306 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
308 return (l2fib_seq_num_t) {
309 .swif = int_config->seq_num,
310 .bd = bd_config->seq_num,
316 * Add an entry to the l2fib.
317 * If the entry already exists then overwrite it
320 l2fib_add_entry (u64 mac, u32 bd_index,
321 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
323 l2fib_entry_key_t key;
324 l2fib_entry_result_t result;
325 __attribute__ ((unused)) u32 bucket_contents;
326 l2fib_main_t *mp = &l2fib_main;
327 BVT (clib_bihash_kv) kv;
330 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
333 result.raw = 0; /* clear all fields */
334 result.fields.sw_if_index = sw_if_index;
335 result.fields.static_mac = static_mac;
336 result.fields.filter = filter_mac;
337 result.fields.bvi = bvi_mac;
339 result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
342 kv.value = result.raw;
344 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
346 /* increment counter if dynamically learned mac */
347 if (result.fields.static_mac == 0)
349 l2learn_main.global_learn_count++;
354 * Add an entry to the L2FIB.
356 * l2fib add <mac> <bd> <intf> [static] [bvi]
357 * l2fib add <mac> <bd> filter
358 * Note that filter and bvi entries are always static
360 static clib_error_t *
361 l2fib_add (vlib_main_t * vm,
362 unformat_input_t * input, vlib_cli_command_t * cmd)
364 bd_main_t *bdm = &bd_main;
365 vnet_main_t *vnm = vnet_get_main ();
366 clib_error_t *error = 0;
370 u32 sw_if_index = ~0;
376 if (!unformat_user (input, unformat_ethernet_address, &mac))
378 error = clib_error_return (0, "expected mac address `%U'",
379 format_unformat_error, input);
383 if (!unformat (input, "%d", &bd_id))
385 error = clib_error_return (0, "expected bridge domain ID `%U'",
386 format_unformat_error, input);
390 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
393 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
398 if (unformat (input, "filter"))
408 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
410 error = clib_error_return (0, "unknown interface `%U'",
411 format_unformat_error, input);
414 if (unformat (input, "static"))
418 else if (unformat (input, "bvi"))
425 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
433 * This command adds a MAC Address entry to the L2 FIB table
434 * of an existing bridge-domain. The MAC Address can be static
435 * or dynamic. This command also allows a filter to be added,
436 * such that packets with given MAC Addresses (source mac or
437 * destination mac match) are dropped.
440 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
441 * of a bridge-domain (where 200 is the bridge-domain-id):
442 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
443 * Example of how to add a static 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:55 200 GigabitEthernet0/8/0.200 static}
446 * Example of how to add a filter such that a packet with the given MAC
447 * Address will be dropped in a given bridge-domain (where 200 is the
449 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
450 * Example of show command of the provisioned MAC Addresses and filters:
451 * @cliexstart{show l2fib verbose}
452 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
453 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
454 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
455 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
460 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
462 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
463 .function = l2fib_add,
468 static clib_error_t *
469 l2fib_test_command_fn (vlib_main_t * vm,
470 unformat_input_t * input, vlib_cli_command_t * cmd)
472 clib_error_t *error = 0;
485 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
487 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
489 else if (unformat (input, "add"))
491 else if (unformat (input, "del"))
493 else if (unformat (input, "check"))
495 else if (unformat (input, "count %d", &count))
502 return clib_error_return (0, "mac not set");
504 if (is_add == 0 && is_del == 0 && is_check == 0)
505 return clib_error_return (0,
506 "noop: pick at least one of (add,del,check)");
512 for (i = 0; i < count; i++)
515 l2fib_add_entry (mac, bd_index, sw_if_index, mac,
516 filter_mac, bvi_mac);
517 tmp = clib_net_to_host_u64 (mac);
521 mac = clib_host_to_net_u64 (tmp);
527 BVT (clib_bihash_kv) kv;
528 l2fib_main_t *mp = &l2fib_main;
532 for (i = 0; i < count; i++)
535 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
536 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
538 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
541 tmp = clib_net_to_host_u64 (mac);
545 mac = clib_host_to_net_u64 (tmp);
551 for (i = 0; i < count; i++)
555 l2fib_del_entry (mac, bd_index);
557 tmp = clib_net_to_host_u64 (mac);
561 mac = clib_host_to_net_u64 (tmp);
569 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
570 * bridge domain (bridge-domain-id of 0) to be modified.
574 * Example of how to add a set of 4 sequential MAC Address entries to L2
575 * FIB table of the default bridge-domain:
576 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
578 * Show the set of 4 sequential MAC Address entries that were added:
579 * @cliexstart{show l2fib verbose}
580 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
581 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
582 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
583 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
584 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
588 * Example of how to check that the set of 4 sequential MAC Address
589 * entries were added to L2 FIB table of the default
590 * bridge-domain. Used a count of 5 to produce an error:
592 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
593 * The output of the check command is in the log files. Log file
594 * location may vary based on your OS and Version:
596 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
598 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
600 * Example of how to delete a set of 4 sequential MAC Address entries
601 * from L2 FIB table of the default bridge-domain:
602 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
606 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
607 .path = "test l2fib",
608 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
609 .function = l2fib_test_command_fn,
615 * Delete an entry from the l2fib.
616 * Return 0 if the entry was deleted, or 1 if it was not found
619 l2fib_del_entry_by_key (u64 raw_key)
622 l2fib_entry_result_t result;
623 l2fib_main_t *mp = &l2fib_main;
624 BVT (clib_bihash_kv) kv;
629 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
632 result.raw = kv.value;
634 /* decrement counter if dynamically learned mac */
635 if (result.fields.static_mac == 0)
637 if (l2learn_main.global_learn_count > 0)
639 l2learn_main.global_learn_count--;
643 /* Remove entry from hash table */
644 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
649 * Delete an entry from the l2fib.
650 * Return 0 if the entry was deleted, or 1 if it was not found
653 l2fib_del_entry (u64 mac, u32 bd_index)
655 return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
659 * Delete an entry from the L2FIB.
661 * l2fib del <mac> <bd-id>
663 static clib_error_t *
664 l2fib_del (vlib_main_t * vm,
665 unformat_input_t * input, vlib_cli_command_t * cmd)
667 bd_main_t *bdm = &bd_main;
668 clib_error_t *error = 0;
674 if (!unformat_user (input, unformat_ethernet_address, &mac))
676 error = clib_error_return (0, "expected mac address `%U'",
677 format_unformat_error, input);
681 if (!unformat (input, "%d", &bd_id))
683 error = clib_error_return (0, "expected bridge domain ID `%U'",
684 format_unformat_error, input);
688 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
691 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
696 /* Delete the entry */
697 if (l2fib_del_entry (mac, bd_index))
699 error = clib_error_return (0, "mac entry not found");
708 * This command deletes an existing MAC Address entry from the L2 FIB
709 * table of an existing bridge-domain.
712 * 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):
713 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
716 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
718 .short_help = "l2fib del <mac> <bridge-domain-id>",
719 .function = l2fib_del,
724 Kick off ager to scan MACs to age/delete MAC entries
727 l2fib_start_ager_scan (vlib_main_t * vm)
729 l2_bridge_domain_t *bd_config;
732 /* check if there is at least one bd with mac aging enabled */
733 vec_foreach (bd_config, l2input_main.bd_configs)
734 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
737 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
738 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
739 L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
743 Flush all non static MACs from an interface
746 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
748 l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
749 int_config->seq_num += 1;
750 l2fib_start_ager_scan (vm);
754 Flush all non static MACs in a bridge domain
757 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
759 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
760 bd_config->seq_num += 1;
761 l2fib_start_ager_scan (vm);
765 Flush all non static MACs - flushes all valid BDs
768 l2fib_flush_all_mac (vlib_main_t * vm)
770 l2_bridge_domain_t *bd_config;
771 vec_foreach (bd_config, l2input_main.bd_configs)
772 if (bd_is_valid (bd_config))
773 bd_config->seq_num += 1;
775 l2fib_start_ager_scan (vm);
780 Flush MACs, except static ones, associated with an interface
782 l2fib flush-mac interface <if-name>
784 static clib_error_t *
785 l2fib_flush_mac_int (vlib_main_t * vm,
786 unformat_input_t * input, vlib_cli_command_t * cmd)
788 vnet_main_t *vnm = vnet_get_main ();
789 clib_error_t *error = 0;
792 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
794 error = clib_error_return (0, "unknown interface `%U'",
795 format_unformat_error, input);
799 l2fib_flush_int_mac (vm, sw_if_index);
806 Flush all MACs, except static ones
810 static clib_error_t *
811 l2fib_flush_mac_all (vlib_main_t * vm,
812 unformat_input_t * input, vlib_cli_command_t * cmd)
814 l2fib_flush_all_mac (vm);
819 * This command kick off ager to delete all existing MAC Address entries,
820 * except static ones, associated with an interface from the L2 FIB table.
823 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
824 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
827 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
828 .path = "l2fib flush-mac all",
829 .short_help = "l2fib flush-mac all",
830 .function = l2fib_flush_mac_all,
835 * This command kick off ager to delete all existing MAC Address entries,
836 * except static ones, associated with an interface from the L2 FIB table.
839 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
840 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
843 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
844 .path = "l2fib flush-mac interface",
845 .short_help = "l2fib flush-mac interface <if-name>",
846 .function = l2fib_flush_mac_int,
851 Flush bridge-domain MACs except static ones.
853 l2fib flush-mac bridge-domain <bd-id>
855 static clib_error_t *
856 l2fib_flush_mac_bd (vlib_main_t * vm,
857 unformat_input_t * input, vlib_cli_command_t * cmd)
859 bd_main_t *bdm = &bd_main;
860 clib_error_t *error = 0;
864 if (!unformat (input, "%d", &bd_id))
866 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
867 format_unformat_error, input);
871 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
875 return clib_error_return (0, "No such bridge domain %d", bd_id);
877 l2fib_flush_bd_mac (vm, bd_index);
884 * This command kick off ager to delete all existing MAC Address entries,
885 * except static ones, in a bridge domain from the L2 FIB table.
888 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
889 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
892 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
893 .path = "l2fib flush-mac bridge-domain",
894 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
895 .function = l2fib_flush_mac_bd,
900 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
902 l2_input_config_t *config = l2input_intf_config (sw_if_index);
903 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
904 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
908 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
910 BVT (clib_bihash) * get_mac_table (void)
912 l2fib_main_t *mp = &l2fib_main;
913 return &mp->mac_table;
917 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
920 uword event_type, *event_data = 0;
921 l2fib_main_t *msm = &l2fib_main;
923 f64 start_time, last_run_duration = 0, t;
928 vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
930 vlib_process_wait_for_event (vm);
932 event_type = vlib_process_get_events (vm, &event_data);
933 vec_reset_length (event_data);
939 case L2_MAC_AGE_PROCESS_EVENT_START:
942 case L2_MAC_AGE_PROCESS_EVENT_STOP:
945 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
951 last_run_duration = start_time = vlib_time_now (vm);
953 BVT (clib_bihash) * h = &msm->mac_table;
955 for (i = 0; i < h->nbuckets; i++)
957 /* Allow no more than 10us without a pause */
958 t = vlib_time_now (vm);
959 if (t > start_time + 10e-6)
961 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
962 start_time = vlib_time_now (vm);
965 if (i < (h->nbuckets - 3))
967 clib_bihash_bucket_t *b = &h->buckets[i + 3];
968 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
969 b = &h->buckets[i + 1];
972 BVT (clib_bihash_value) * v =
973 BV (clib_bihash_get_value) (h, b->offset);
974 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
978 clib_bihash_bucket_t *b = &h->buckets[i];
981 BVT (clib_bihash_value) * v =
982 BV (clib_bihash_get_value) (h, b->offset);
983 for (j = 0; j < (1 << b->log2_pages); j++)
985 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
987 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
990 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
991 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
993 if (result.fields.static_mac)
996 u32 bd_index = key.fields.bd_index;
997 u32 sw_if_index = result.fields.sw_if_index;
998 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
999 if (result.fields.sn.as_u16 != sn)
1001 l2fib_del_entry_by_key (key.raw);
1004 l2_bridge_domain_t *bd_config =
1005 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1007 if (bd_config->mac_age == 0)
1011 (u8) (start_time / 60) - result.fields.timestamp;
1012 delta += delta < 0 ? 256 : 0;
1014 if (delta > bd_config->mac_age)
1015 l2fib_del_entry_by_key (key.raw);
1020 last_run_duration = vlib_time_now (vm) - last_run_duration;
1026 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1027 .function = l2fib_mac_age_scanner_process,
1028 .type = VLIB_NODE_TYPE_PROCESS,
1029 .name = "l2fib-mac-age-scanner-process",
1034 l2fib_init (vlib_main_t * vm)
1036 l2fib_main_t *mp = &l2fib_main;
1037 l2fib_entry_key_t test_key;
1041 mp->vnet_main = vnet_get_main ();
1043 /* Create the hash table */
1044 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1045 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
1047 /* verify the key constructor is good, since it is endian-sensitive */
1048 memset (test_mac, 0, sizeof (test_mac));
1051 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1052 ASSERT (test_key.fields.mac[0] == 0x11);
1053 ASSERT (test_key.fields.bd_index == 0x1234);
1058 VLIB_INIT_FUNCTION (l2fib_init);
1061 * fd.io coding-style-patch-verification: ON
1064 * eval: (c-set-style "gnu")