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.
44 l2fib_main_t l2fib_main;
46 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
48 format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
50 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
51 u32 sw_if_index = va_arg (*args, u32);
52 if (sw_if_index == ~0)
53 return format (s, "N/A");
55 vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
57 return format (s, "Stale");
59 return format (s, "%U", format_vnet_sw_interface_name, vnm,
60 vnet_get_sw_interface_safe (vnm, sw_if_index));
64 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
65 l2fib_entry_result_t ** l2fe_res)
67 l2fib_main_t *msm = &l2fib_main;
68 BVT (clib_bihash) * h = &msm->mac_table;
69 clib_bihash_bucket_t *b;
70 BVT (clib_bihash_value) * v;
71 l2fib_entry_key_t key;
72 l2fib_entry_result_t result;
75 for (i = 0; i < h->nbuckets; i++)
80 v = BV (clib_bihash_get_value) (h, b->offset);
81 for (j = 0; j < (1 << b->log2_pages); j++)
83 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
85 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
88 key.raw = v->kvp[k].key;
89 result.raw = v->kvp[k].value;
91 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
93 vec_add1 (*l2fe_key, key);
94 vec_add1 (*l2fe_res, result);
102 /** Display the contents of the l2fib. */
103 static clib_error_t *
104 show_l2fib (vlib_main_t * vm,
105 unformat_input_t * input, vlib_cli_command_t * cmd)
107 bd_main_t *bdm = &bd_main;
108 l2fib_main_t *msm = &l2fib_main;
109 l2_bridge_domain_t *bd_config;
110 BVT (clib_bihash) * h = &msm->mac_table;
111 clib_bihash_bucket_t *b;
112 BVT (clib_bihash_value) * v;
113 l2fib_entry_key_t key;
114 l2fib_entry_result_t result;
116 u64 total_entries = 0;
120 u32 bd_id, bd_index = ~0;
121 u8 now = (u8) (vlib_time_now (vm) / 60);
124 if (unformat (input, "raw"))
126 else if (unformat (input, "verbose"))
128 else if (unformat (input, "bd_index %d", &bd_index))
130 else if (unformat (input, "bd_id %d", &bd_id))
132 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
140 vlib_cli_output (vm, "no such bridge domain id");
145 for (i = 0; i < h->nbuckets; i++)
150 v = BV (clib_bihash_get_value) (h, b->offset);
151 for (j = 0; j < (1 << b->log2_pages); j++)
153 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
155 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
158 if (verbose && first_entry)
162 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
163 "Mac-Address", "BD-Idx", "If-Idx",
164 "BSN-ISN", "Age(min)", "static", "filter",
165 "bvi", "Interface-Name");
168 key.raw = v->kvp[k].key;
169 result.raw = v->kvp[k].value;
172 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
174 bd_config = vec_elt_at_index (l2input_main.bd_configs,
175 key.fields.bd_index);
177 if (bd_config->mac_age && !result.fields.static_mac)
179 i16 delta = now - result.fields.timestamp;
180 delta += delta < 0 ? 256 : 0;
181 s = format (s, "%d", delta);
187 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
188 format_ethernet_address, key.fields.mac,
190 result.fields.sw_if_index == ~0
191 ? -1 : result.fields.sw_if_index,
192 result.fields.sn.bd, result.fields.sn.swif,
193 s, result.fields.static_mac ? "*" : "-",
194 result.fields.filter ? "*" : "-",
195 result.fields.bvi ? "*" : "-",
196 format_vnet_sw_if_index_name_with_NA,
197 msm->vnet_main, result.fields.sw_if_index);
198 vec_reset_length (s);
206 if (total_entries == 0)
207 vlib_cli_output (vm, "no l2fib entries");
210 "%lld l2fib entries with %d learned (or non-static) entries",
211 total_entries, l2learn_main.global_learn_count);
214 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
215 BV (format_bihash), h, 1 /* verbose */ );
222 * This command dispays the MAC Address entries of the L2 FIB table.
223 * Output can be filtered to just get the number of MAC Addresses or display
224 * each MAC Address for all bridge domains or just a single bridge domain.
227 * Example of how to display the number of MAC Address entries in the L2
229 * @cliexstart{show l2fib}
232 * Example of how to display all the MAC Address entries in the L2
234 * @cliexstart{show l2fib verbose}
235 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
236 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
237 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
238 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
243 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
244 .path = "show l2fib",
245 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
246 .function = show_l2fib,
251 /* Remove all entries from the l2fib */
253 l2fib_clear_table (void)
255 l2fib_main_t *mp = &l2fib_main;
257 /* Remove all entries */
258 BV (clib_bihash_free) (&mp->mac_table);
259 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
260 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
261 l2learn_main.global_learn_count = 0;
264 /** Clear all entries in L2FIB.
265 * @TODO: Later we may want a way to remove only the non-static entries
267 static clib_error_t *
268 clear_l2fib (vlib_main_t * vm,
269 unformat_input_t * input, vlib_cli_command_t * cmd)
271 l2fib_clear_table ();
276 * This command clears all the MAC Address entries from the L2 FIB table.
279 * Example of how to clear the L2 FIB Table:
280 * @cliexcmd{clear l2fib}
281 * Example to show the L2 FIB Table has been cleared:
282 * @cliexstart{show l2fib verbose}
287 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
288 .path = "clear l2fib",
289 .short_help = "clear l2fib",
290 .function = clear_l2fib,
294 static inline l2fib_seq_num_t
295 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
297 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
299 return (l2fib_seq_num_t) {
300 .swif = *l2fib_swif_seq_num (sw_if_index),
301 .bd = bd_config->seq_num,
307 * Add an entry to the l2fib.
308 * If the entry already exists then overwrite it
311 l2fib_add_entry (u64 mac, u32 bd_index,
312 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
314 l2fib_entry_key_t key;
315 l2fib_entry_result_t result;
316 __attribute__ ((unused)) u32 bucket_contents;
317 l2fib_main_t *mp = &l2fib_main;
318 BVT (clib_bihash_kv) kv;
321 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
324 result.raw = 0; /* clear all fields */
325 result.fields.sw_if_index = sw_if_index;
326 result.fields.static_mac = static_mac;
327 result.fields.filter = filter_mac;
328 result.fields.bvi = bvi_mac;
330 result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
333 kv.value = result.raw;
335 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
337 /* increment counter if dynamically learned mac */
338 if (result.fields.static_mac == 0)
340 l2learn_main.global_learn_count++;
345 * Add an entry to the L2FIB.
347 * l2fib add <mac> <bd> <intf> [static] [bvi]
348 * l2fib add <mac> <bd> filter
349 * Note that filter and bvi entries are always static
351 static clib_error_t *
352 l2fib_add (vlib_main_t * vm,
353 unformat_input_t * input, vlib_cli_command_t * cmd)
355 bd_main_t *bdm = &bd_main;
356 vnet_main_t *vnm = vnet_get_main ();
357 clib_error_t *error = 0;
361 u32 sw_if_index = ~0;
367 if (!unformat_user (input, unformat_ethernet_address, &mac))
369 error = clib_error_return (0, "expected mac address `%U'",
370 format_unformat_error, input);
374 if (!unformat (input, "%d", &bd_id))
376 error = clib_error_return (0, "expected bridge domain ID `%U'",
377 format_unformat_error, input);
381 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
384 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
389 if (unformat (input, "filter"))
399 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
401 error = clib_error_return (0, "unknown interface `%U'",
402 format_unformat_error, input);
405 if (unformat (input, "static"))
409 else if (unformat (input, "bvi"))
416 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
424 * This command adds a MAC Address entry to the L2 FIB table
425 * of an existing bridge-domain. The MAC Address can be static
426 * or dynamic. This command also allows a filter to be added,
427 * such that packets with given MAC Addresses (source mac or
428 * destination mac match) are dropped.
431 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
432 * of a bridge-domain (where 200 is the bridge-domain-id):
433 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
434 * Example of how to add a static MAC Address entry to the L2 FIB table
435 * of a bridge-domain (where 200 is the bridge-domain-id):
436 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
437 * Example of how to add a filter such that a packet with the given MAC
438 * Address will be dropped in a given bridge-domain (where 200 is the
440 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
441 * Example of show command of the provisioned MAC Addresses and filters:
442 * @cliexstart{show l2fib verbose}
443 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
444 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
445 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
446 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
451 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
453 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
454 .function = l2fib_add,
459 static clib_error_t *
460 l2fib_test_command_fn (vlib_main_t * vm,
461 unformat_input_t * input, vlib_cli_command_t * cmd)
463 clib_error_t *error = 0;
476 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
478 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
480 else if (unformat (input, "add"))
482 else if (unformat (input, "del"))
484 else if (unformat (input, "check"))
486 else if (unformat (input, "count %d", &count))
493 return clib_error_return (0, "mac not set");
495 if (is_add == 0 && is_del == 0 && is_check == 0)
496 return clib_error_return (0,
497 "noop: pick at least one of (add,del,check)");
503 for (i = 0; i < count; i++)
506 l2fib_add_entry (mac, bd_index, sw_if_index, mac,
507 filter_mac, bvi_mac);
508 tmp = clib_net_to_host_u64 (mac);
512 mac = clib_host_to_net_u64 (tmp);
518 BVT (clib_bihash_kv) kv;
519 l2fib_main_t *mp = &l2fib_main;
523 for (i = 0; i < count; i++)
526 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
527 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
529 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
532 tmp = clib_net_to_host_u64 (mac);
536 mac = clib_host_to_net_u64 (tmp);
542 for (i = 0; i < count; i++)
546 l2fib_del_entry (mac, bd_index);
548 tmp = clib_net_to_host_u64 (mac);
552 mac = clib_host_to_net_u64 (tmp);
560 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
561 * bridge domain (bridge-domain-id of 0) to be modified.
565 * Example of how to add a set of 4 sequential MAC Address entries to L2
566 * FIB table of the default bridge-domain:
567 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
569 * Show the set of 4 sequential MAC Address entries that were added:
570 * @cliexstart{show l2fib verbose}
571 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
572 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
573 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
574 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
575 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
579 * Example of how to check that the set of 4 sequential MAC Address
580 * entries were added to L2 FIB table of the default
581 * bridge-domain. Used a count of 5 to produce an error:
583 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
584 * The output of the check command is in the log files. Log file
585 * location may vary based on your OS and Version:
587 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
589 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
591 * Example of how to delete a set of 4 sequential MAC Address entries
592 * from L2 FIB table of the default bridge-domain:
593 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
597 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
598 .path = "test l2fib",
599 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
600 .function = l2fib_test_command_fn,
606 * Delete an entry from the l2fib.
607 * Return 0 if the entry was deleted, or 1 if it was not found
610 l2fib_del_entry_by_key (u64 raw_key)
613 l2fib_entry_result_t result;
614 l2fib_main_t *mp = &l2fib_main;
615 BVT (clib_bihash_kv) kv;
620 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
623 result.raw = kv.value;
625 /* decrement counter if dynamically learned mac */
626 if (result.fields.static_mac == 0)
628 if (l2learn_main.global_learn_count > 0)
630 l2learn_main.global_learn_count--;
634 /* Remove entry from hash table */
635 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
640 * Delete an entry from the l2fib.
641 * Return 0 if the entry was deleted, or 1 if it was not found
644 l2fib_del_entry (u64 mac, u32 bd_index)
646 return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
650 * Delete an entry from the L2FIB.
652 * l2fib del <mac> <bd-id>
654 static clib_error_t *
655 l2fib_del (vlib_main_t * vm,
656 unformat_input_t * input, vlib_cli_command_t * cmd)
658 bd_main_t *bdm = &bd_main;
659 clib_error_t *error = 0;
665 if (!unformat_user (input, unformat_ethernet_address, &mac))
667 error = clib_error_return (0, "expected mac address `%U'",
668 format_unformat_error, input);
672 if (!unformat (input, "%d", &bd_id))
674 error = clib_error_return (0, "expected bridge domain ID `%U'",
675 format_unformat_error, input);
679 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
682 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
687 /* Delete the entry */
688 if (l2fib_del_entry (mac, bd_index))
690 error = clib_error_return (0, "mac entry not found");
699 * This command deletes an existing MAC Address entry from the L2 FIB
700 * table of an existing bridge-domain.
703 * 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):
704 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
707 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
709 .short_help = "l2fib del <mac> <bridge-domain-id>",
710 .function = l2fib_del,
715 Kick off ager to scan MACs to age/delete MAC entries
718 l2fib_start_ager_scan (vlib_main_t * vm)
720 l2_bridge_domain_t *bd_config;
723 /* check if there is at least one bd with mac aging enabled */
724 vec_foreach (bd_config, l2input_main.bd_configs)
725 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
728 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
729 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
730 L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
734 Flush all non static MACs from an interface
737 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
739 *l2fib_swif_seq_num (sw_if_index) += 1;
740 l2fib_start_ager_scan (vm);
744 Flush all non static MACs in a bridge domain
747 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
749 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
750 bd_config->seq_num += 1;
751 l2fib_start_ager_scan (vm);
755 Flush all non static MACs - flushes all valid BDs
758 l2fib_flush_all_mac (vlib_main_t * vm)
760 l2_bridge_domain_t *bd_config;
761 vec_foreach (bd_config, l2input_main.bd_configs)
762 if (bd_is_valid (bd_config))
763 bd_config->seq_num += 1;
765 l2fib_start_ager_scan (vm);
770 Flush MACs, except static ones, associated with an interface
772 l2fib flush-mac interface <if-name>
774 static clib_error_t *
775 l2fib_flush_mac_int (vlib_main_t * vm,
776 unformat_input_t * input, vlib_cli_command_t * cmd)
778 vnet_main_t *vnm = vnet_get_main ();
779 clib_error_t *error = 0;
782 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
784 error = clib_error_return (0, "unknown interface `%U'",
785 format_unformat_error, input);
789 l2fib_flush_int_mac (vm, sw_if_index);
796 Flush all MACs, except static ones
800 static clib_error_t *
801 l2fib_flush_mac_all (vlib_main_t * vm,
802 unformat_input_t * input, vlib_cli_command_t * cmd)
804 l2fib_flush_all_mac (vm);
809 * This command kick off ager to delete all existing MAC Address entries,
810 * except static ones, associated with an interface from the L2 FIB table.
813 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
814 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
817 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
818 .path = "l2fib flush-mac all",
819 .short_help = "l2fib flush-mac all",
820 .function = l2fib_flush_mac_all,
825 * This command kick off ager to delete all existing MAC Address entries,
826 * except static ones, associated with an interface from the L2 FIB table.
829 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
830 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
833 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
834 .path = "l2fib flush-mac interface",
835 .short_help = "l2fib flush-mac interface <if-name>",
836 .function = l2fib_flush_mac_int,
841 Flush bridge-domain MACs except static ones.
843 l2fib flush-mac bridge-domain <bd-id>
845 static clib_error_t *
846 l2fib_flush_mac_bd (vlib_main_t * vm,
847 unformat_input_t * input, vlib_cli_command_t * cmd)
849 bd_main_t *bdm = &bd_main;
850 clib_error_t *error = 0;
854 if (!unformat (input, "%d", &bd_id))
856 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
857 format_unformat_error, input);
861 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
865 return clib_error_return (0, "No such bridge domain %d", bd_id);
867 l2fib_flush_bd_mac (vm, bd_index);
874 * This command kick off ager to delete all existing MAC Address entries,
875 * except static ones, in a bridge domain from the L2 FIB table.
878 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
879 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
882 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
883 .path = "l2fib flush-mac bridge-domain",
884 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
885 .function = l2fib_flush_mac_bd,
890 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
892 l2_input_config_t *config = l2input_intf_config (sw_if_index);
893 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
894 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
898 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
900 BVT (clib_bihash) * get_mac_table (void)
902 l2fib_main_t *mp = &l2fib_main;
903 return &mp->mac_table;
907 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
910 uword event_type, *event_data = 0;
911 l2fib_main_t *msm = &l2fib_main;
913 f64 start_time, last_run_duration = 0, t;
918 vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
920 vlib_process_wait_for_event (vm);
922 event_type = vlib_process_get_events (vm, &event_data);
923 vec_reset_length (event_data);
929 case L2_MAC_AGE_PROCESS_EVENT_START:
932 case L2_MAC_AGE_PROCESS_EVENT_STOP:
935 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
941 last_run_duration = start_time = vlib_time_now (vm);
943 BVT (clib_bihash) * h = &msm->mac_table;
945 for (i = 0; i < h->nbuckets; i++)
947 /* Allow no more than 10us without a pause */
948 t = vlib_time_now (vm);
949 if (t > start_time + 10e-6)
951 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
952 start_time = vlib_time_now (vm);
955 if (i < (h->nbuckets - 3))
957 clib_bihash_bucket_t *b = &h->buckets[i + 3];
958 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
959 b = &h->buckets[i + 1];
962 BVT (clib_bihash_value) * v =
963 BV (clib_bihash_get_value) (h, b->offset);
964 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
968 clib_bihash_bucket_t *b = &h->buckets[i];
971 BVT (clib_bihash_value) * v =
972 BV (clib_bihash_get_value) (h, b->offset);
973 for (j = 0; j < (1 << b->log2_pages); j++)
975 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
977 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
980 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
981 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
983 if (result.fields.static_mac)
986 u32 bd_index = key.fields.bd_index;
987 u32 sw_if_index = result.fields.sw_if_index;
988 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
989 if (result.fields.sn.as_u16 != sn)
991 l2fib_del_entry_by_key (key.raw);
994 l2_bridge_domain_t *bd_config =
995 vec_elt_at_index (l2input_main.bd_configs, bd_index);
997 if (bd_config->mac_age == 0)
1001 (u8) (start_time / 60) - result.fields.timestamp;
1002 delta += delta < 0 ? 256 : 0;
1004 if (delta > bd_config->mac_age)
1005 l2fib_del_entry_by_key (key.raw);
1010 last_run_duration = vlib_time_now (vm) - last_run_duration;
1016 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1017 .function = l2fib_mac_age_scanner_process,
1018 .type = VLIB_NODE_TYPE_PROCESS,
1019 .name = "l2fib-mac-age-scanner-process",
1024 l2fib_init (vlib_main_t * vm)
1026 l2fib_main_t *mp = &l2fib_main;
1027 l2fib_entry_key_t test_key;
1031 mp->vnet_main = vnet_get_main ();
1033 /* Create the hash table */
1034 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1035 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
1037 /* verify the key constructor is good, since it is endian-sensitive */
1038 memset (test_mac, 0, sizeof (test_mac));
1041 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1042 ASSERT (test_key.fields.mac[0] == 0x11);
1043 ASSERT (test_key.fields.bd_index == 0x1234);
1048 VLIB_INIT_FUNCTION (l2fib_init);
1051 * fd.io coding-style-patch-verification: ON
1054 * eval: (c-set-style "gnu")