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>
34 #include <vlibmemory/api.h>
35 #include <vnet/vnet_msg_enum.h>
37 #define vl_typedefs /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
41 #define vl_endianfun /* define message structures */
42 #include <vnet/vnet_all_api_h.h>
47 * @brief Ethernet MAC Address FIB Table Management.
49 * The MAC Address forwarding table for bridge-domains is called the l2fib.
50 * Entries are added automatically as part of mac learning, but MAC Addresses
51 * entries can also be added manually.
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, "Stale");
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 BVT (clib_bihash_bucket) * 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 BVT (clib_bihash_bucket) * b;
123 BVT (clib_bihash_value) * v;
124 l2fib_entry_key_t key;
125 l2fib_entry_result_t result;
127 u64 total_entries = 0;
132 u32 bd_id, bd_index = ~0;
133 u8 now = (u8) (vlib_time_now (vm) / 60);
136 if (unformat (input, "raw"))
138 else if (unformat (input, "verbose"))
140 else if (unformat (input, "bd_index %d", &bd_index))
142 else if (unformat (input, "learn"))
147 else if (unformat (input, "bd_id %d", &bd_id))
149 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
158 vlib_cli_output (vm, "no such bridge domain id");
163 for (i = 0; i < h->nbuckets; i++)
168 v = BV (clib_bihash_get_value) (h, b->offset);
169 for (j = 0; j < (1 << b->log2_pages); j++)
171 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
173 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
176 if ((verbose || learn) && first_entry)
180 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
181 "Mac-Address", "BD-Idx", "If-Idx",
182 "BSN-ISN", "Age(min)", "static", "filter",
183 "bvi", "Interface-Name");
186 key.raw = v->kvp[k].key;
187 result.raw = v->kvp[k].value;
189 if ((verbose || learn)
190 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
192 if (learn && result.fields.age_not)
195 continue; /* skip provisioned macs */
198 bd_config = vec_elt_at_index (l2input_main.bd_configs,
199 key.fields.bd_index);
201 if (bd_config->mac_age && !result.fields.age_not)
203 i16 delta = now - result.fields.timestamp;
204 delta += delta < 0 ? 256 : 0;
205 s = format (s, "%d", delta);
211 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
212 format_ethernet_address, key.fields.mac,
214 result.fields.sw_if_index == ~0
215 ? -1 : result.fields.sw_if_index,
216 result.fields.sn.bd, result.fields.sn.swif,
217 s, result.fields.static_mac ? "*" : "-",
218 result.fields.filter ? "*" : "-",
219 result.fields.bvi ? "*" : "-",
220 format_vnet_sw_if_index_name_with_NA,
221 msm->vnet_main, result.fields.sw_if_index);
222 vec_reset_length (s);
230 if (total_entries == 0)
231 vlib_cli_output (vm, "no l2fib entries");
234 l2learn_main_t *lm = &l2learn_main;
235 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
236 "Last scan time: %.4esec Learn limit: %d ",
237 total_entries, lm->global_learn_count,
238 msm->age_scan_duration, lm->global_learn_limit);
240 vlib_cli_output (vm, "L2MAC events client PID: %d "
241 "Last e-scan time: %.4esec Delay: %.2esec "
242 "Max macs in event: %d",
243 lm->client_pid, msm->evt_scan_duration,
244 msm->event_scan_delay, msm->max_macs_in_event);
248 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
249 BV (format_bihash), h, 1 /* verbose */ );
256 * This command dispays the MAC Address entries of the L2 FIB table.
257 * Output can be filtered to just get the number of MAC Addresses or display
258 * each MAC Address for all bridge domains or just a single bridge domain.
261 * Example of how to display the number of MAC Address entries in the L2
263 * @cliexstart{show l2fib}
266 * Example of how to display all the MAC Address entries in the L2
268 * @cliexstart{show l2fib verbose}
269 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
270 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
271 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
272 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
277 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
278 .path = "show l2fib",
279 .short_help = "show l2fib [verbose | learn | bd_id <nn> | bd_index <nn> | raw",
280 .function = show_l2fib,
285 /* Remove all entries from the l2fib */
287 l2fib_clear_table (void)
289 l2fib_main_t *mp = &l2fib_main;
291 /* Remove all entries */
292 BV (clib_bihash_free) (&mp->mac_table);
293 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
294 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
295 l2learn_main.global_learn_count = 0;
298 /** Clear all entries in L2FIB.
299 * @TODO: Later we may want a way to remove only the non-static entries
301 static clib_error_t *
302 clear_l2fib (vlib_main_t * vm,
303 unformat_input_t * input, vlib_cli_command_t * cmd)
305 l2fib_clear_table ();
310 * This command clears all the MAC Address entries from the L2 FIB table.
313 * Example of how to clear the L2 FIB Table:
314 * @cliexcmd{clear l2fib}
315 * Example to show the L2 FIB Table has been cleared:
316 * @cliexstart{show l2fib verbose}
321 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
322 .path = "clear l2fib",
323 .short_help = "clear l2fib",
324 .function = clear_l2fib,
328 static inline l2fib_seq_num_t
329 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
331 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
333 return (l2fib_seq_num_t) {
334 .swif = *l2fib_swif_seq_num (sw_if_index),
335 .bd = bd_config->seq_num,
341 * Add an entry to the l2fib.
342 * If the entry already exists then overwrite it
345 l2fib_add_entry (u64 mac, u32 bd_index,
346 u32 sw_if_index, u8 static_mac, u8 filter_mac, u8 bvi_mac)
348 l2fib_entry_key_t key;
349 l2fib_entry_result_t result;
350 __attribute__ ((unused)) u32 bucket_contents;
351 l2fib_main_t *fm = &l2fib_main;
352 l2learn_main_t *lm = &l2learn_main;
353 BVT (clib_bihash_kv) kv;
356 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
358 /* check if entry alread exist */
359 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
361 /* decrement counter if overwriting a learned mac */
362 result.raw = kv.value;
363 if ((result.fields.age_not == 0) && (lm->global_learn_count))
364 lm->global_learn_count--;
368 result.raw = 0; /* clear all fields */
369 result.fields.sw_if_index = sw_if_index;
370 result.fields.static_mac = static_mac;
371 result.fields.filter = filter_mac;
372 result.fields.bvi = bvi_mac;
373 result.fields.age_not = 1; /* no aging for provisioned entry */
376 kv.value = result.raw;
378 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
382 * Add an entry to the L2FIB.
384 * l2fib add <mac> <bd> <intf> [static] [bvi]
385 * l2fib add <mac> <bd> filter
386 * Note that filter and bvi entries are always static
388 static clib_error_t *
389 l2fib_add (vlib_main_t * vm,
390 unformat_input_t * input, vlib_cli_command_t * cmd)
392 bd_main_t *bdm = &bd_main;
393 vnet_main_t *vnm = vnet_get_main ();
394 clib_error_t *error = 0;
398 u32 sw_if_index = ~0;
404 if (!unformat_user (input, unformat_ethernet_address, &mac))
406 error = clib_error_return (0, "expected mac address `%U'",
407 format_unformat_error, input);
411 if (!unformat (input, "%d", &bd_id))
413 error = clib_error_return (0, "expected bridge domain ID `%U'",
414 format_unformat_error, input);
418 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
421 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
426 if (unformat (input, "filter"))
436 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
438 error = clib_error_return (0, "unknown interface `%U'",
439 format_unformat_error, input);
442 if (unformat (input, "static"))
446 else if (unformat (input, "bvi"))
453 if (vec_len (l2input_main.configs) <= sw_if_index)
455 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
461 l2fib_add_filter_entry (mac, bd_index);
463 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac);
470 * This command adds a MAC Address entry to the L2 FIB table
471 * of an existing bridge-domain. The MAC Address can be static
472 * or dynamic. This command also allows a filter to be added,
473 * such that packets with given MAC Addresses (source mac or
474 * destination mac match) are dropped.
477 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
478 * of a bridge-domain (where 200 is the bridge-domain-id):
479 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
480 * Example of how to add a static MAC Address entry to the L2 FIB table
481 * of a bridge-domain (where 200 is the bridge-domain-id):
482 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
483 * Example of how to add a filter such that a packet with the given MAC
484 * Address will be dropped in a given bridge-domain (where 200 is the
486 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
487 * Example of show command of the provisioned MAC Addresses and filters:
488 * @cliexstart{show l2fib verbose}
489 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
490 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
491 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
492 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
497 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
499 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
500 .function = l2fib_add,
505 static clib_error_t *
506 l2fib_test_command_fn (vlib_main_t * vm,
507 unformat_input_t * input, vlib_cli_command_t * cmd)
509 clib_error_t *error = 0;
521 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
523 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
525 else if (unformat (input, "add"))
527 else if (unformat (input, "del"))
529 else if (unformat (input, "check"))
531 else if (unformat (input, "count %d", &count))
538 return clib_error_return (0, "mac not set");
540 if (is_add == 0 && is_del == 0 && is_check == 0)
541 return clib_error_return (0,
542 "noop: pick at least one of (add,del,check)");
548 for (i = 0; i < count; i++)
551 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, mac, bvi_mac);
552 tmp = clib_net_to_host_u64 (mac);
556 mac = clib_host_to_net_u64 (tmp);
562 BVT (clib_bihash_kv) kv;
563 l2fib_main_t *mp = &l2fib_main;
567 for (i = 0; i < count; i++)
570 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
571 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
573 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
576 tmp = clib_net_to_host_u64 (mac);
580 mac = clib_host_to_net_u64 (tmp);
586 for (i = 0; i < count; i++)
590 l2fib_del_entry (mac, bd_index);
592 tmp = clib_net_to_host_u64 (mac);
596 mac = clib_host_to_net_u64 (tmp);
604 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
605 * bridge domain (bridge-domain-id of 0) to be modified.
609 * Example of how to add a set of 4 sequential MAC Address entries to L2
610 * FIB table of the default bridge-domain:
611 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
613 * Show the set of 4 sequential MAC Address entries that were added:
614 * @cliexstart{show l2fib verbose}
615 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
616 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
617 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
618 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
619 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
623 * Example of how to check that the set of 4 sequential MAC Address
624 * entries were added to L2 FIB table of the default
625 * bridge-domain. Used a count of 5 to produce an error:
627 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
628 * The output of the check command is in the log files. Log file
629 * location may vary based on your OS and Version:
631 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
633 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
635 * Example of how to delete a set of 4 sequential MAC Address entries
636 * from L2 FIB table of the default bridge-domain:
637 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
641 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
642 .path = "test l2fib",
643 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
644 .function = l2fib_test_command_fn,
650 * Delete an entry from the l2fib.
651 * Return 0 if the entry was deleted, or 1 if it was not found
654 l2fib_del_entry_by_key (u64 raw_key)
657 l2fib_entry_result_t result;
658 l2fib_main_t *mp = &l2fib_main;
659 BVT (clib_bihash_kv) kv;
664 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
667 result.raw = kv.value;
669 /* decrement counter if dynamically learned mac */
670 if ((result.fields.age_not == 0) && (l2learn_main.global_learn_count))
671 l2learn_main.global_learn_count--;
673 /* Remove entry from hash table */
674 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
679 * Delete an entry from the l2fib.
680 * Return 0 if the entry was deleted, or 1 if it was not found
683 l2fib_del_entry (u64 mac, u32 bd_index)
685 return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
689 * Delete an entry from the L2FIB.
691 * l2fib del <mac> <bd-id>
693 static clib_error_t *
694 l2fib_del (vlib_main_t * vm,
695 unformat_input_t * input, vlib_cli_command_t * cmd)
697 bd_main_t *bdm = &bd_main;
698 clib_error_t *error = 0;
704 if (!unformat_user (input, unformat_ethernet_address, &mac))
706 error = clib_error_return (0, "expected mac address `%U'",
707 format_unformat_error, input);
711 if (!unformat (input, "%d", &bd_id))
713 error = clib_error_return (0, "expected bridge domain ID `%U'",
714 format_unformat_error, input);
718 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
721 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
726 /* Delete the entry */
727 if (l2fib_del_entry (mac, bd_index))
729 error = clib_error_return (0, "mac entry not found");
738 * This command deletes an existing MAC Address entry from the L2 FIB
739 * table of an existing bridge-domain.
742 * 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):
743 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
746 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
748 .short_help = "l2fib del <mac> <bridge-domain-id>",
749 .function = l2fib_del,
754 Kick off ager to scan MACs to age/delete MAC entries
757 l2fib_start_ager_scan (vlib_main_t * vm)
759 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
761 /* check if there is at least one bd with mac aging enabled */
762 l2_bridge_domain_t *bd_config;
763 vec_foreach (bd_config, l2input_main.bd_configs)
765 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
767 evt = L2_MAC_AGE_PROCESS_EVENT_START;
772 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
777 Flush all non static MACs from an interface
780 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
782 *l2fib_swif_seq_num (sw_if_index) += 1;
783 l2fib_start_ager_scan (vm);
787 Flush all non static MACs in a bridge domain
790 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
792 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
793 bd_config->seq_num += 1;
794 l2fib_start_ager_scan (vm);
798 Flush all non static MACs - flushes all valid BDs
801 l2fib_flush_all_mac (vlib_main_t * vm)
803 l2_bridge_domain_t *bd_config;
804 vec_foreach (bd_config, l2input_main.bd_configs)
805 if (bd_is_valid (bd_config))
806 bd_config->seq_num += 1;
808 l2fib_start_ager_scan (vm);
813 Flush MACs, except static ones, associated with an interface
815 l2fib flush-mac interface <if-name>
817 static clib_error_t *
818 l2fib_flush_mac_int (vlib_main_t * vm,
819 unformat_input_t * input, vlib_cli_command_t * cmd)
821 vnet_main_t *vnm = vnet_get_main ();
822 clib_error_t *error = 0;
825 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
827 error = clib_error_return (0, "unknown interface `%U'",
828 format_unformat_error, input);
832 l2fib_flush_int_mac (vm, sw_if_index);
839 Flush all MACs, except static ones
843 static clib_error_t *
844 l2fib_flush_mac_all (vlib_main_t * vm,
845 unformat_input_t * input, vlib_cli_command_t * cmd)
847 l2fib_flush_all_mac (vm);
852 * This command kick off ager to delete all existing MAC Address entries,
853 * except static ones, associated with an interface from the L2 FIB table.
856 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
857 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
860 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
861 .path = "l2fib flush-mac all",
862 .short_help = "l2fib flush-mac all",
863 .function = l2fib_flush_mac_all,
868 * This command kick off ager to delete all existing MAC Address entries,
869 * except static ones, associated with an interface from the L2 FIB table.
872 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
873 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
876 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
877 .path = "l2fib flush-mac interface",
878 .short_help = "l2fib flush-mac interface <if-name>",
879 .function = l2fib_flush_mac_int,
884 Flush bridge-domain MACs except static ones.
886 l2fib flush-mac bridge-domain <bd-id>
888 static clib_error_t *
889 l2fib_flush_mac_bd (vlib_main_t * vm,
890 unformat_input_t * input, vlib_cli_command_t * cmd)
892 bd_main_t *bdm = &bd_main;
893 clib_error_t *error = 0;
897 if (!unformat (input, "%d", &bd_id))
899 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
900 format_unformat_error, input);
904 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
908 return clib_error_return (0, "No such bridge domain %d", bd_id);
910 l2fib_flush_bd_mac (vm, bd_index);
917 * This command kick off ager to delete all existing MAC Address entries,
918 * except static ones, in a bridge domain from the L2 FIB table.
921 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
922 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
925 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
926 .path = "l2fib flush-mac bridge-domain",
927 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
928 .function = l2fib_flush_mac_bd,
933 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
935 l2_input_config_t *config = l2input_intf_config (sw_if_index);
936 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
937 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
941 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
943 BVT (clib_bihash) * get_mac_table (void)
945 l2fib_main_t *mp = &l2fib_main;
946 return &mp->mac_table;
949 static_always_inline void *
950 allocate_mac_evt_buf (u32 client, u32 client_index)
952 l2fib_main_t *fm = &l2fib_main;
953 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
954 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
955 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
956 mp->pid = htonl (client);
957 mp->client_index = client_index;
961 static_always_inline f64
962 l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
964 l2fib_main_t *fm = &l2fib_main;
965 l2learn_main_t *lm = &l2learn_main;
967 BVT (clib_bihash) * h = &fm->mac_table;
969 f64 last_start = start_time;
974 u32 client = lm->client_pid;
975 u32 cl_idx = lm->client_index;
976 vl_api_l2_macs_event_t *mp = 0;
977 unix_shared_memory_queue_t *q = 0;
981 mp = allocate_mac_evt_buf (client, cl_idx);
982 q = vl_api_client_index_to_input_queue (lm->client_index);
985 for (i = 0; i < h->nbuckets; i++)
987 /* allow no more than 20us without a pause */
988 delta_t = vlib_time_now (vm) - last_start;
991 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
992 last_start = vlib_time_now (vm);
996 if (i < (h->nbuckets - 3))
998 BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
999 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
1000 b = &h->buckets[i + 1];
1003 BVT (clib_bihash_value) * v =
1004 BV (clib_bihash_get_value) (h, b->offset);
1005 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1009 BVT (clib_bihash_bucket) * b = &h->buckets[i];
1012 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1013 for (j = 0; j < (1 << b->log2_pages); j++)
1015 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1017 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1020 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1021 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1023 if (result.fields.age_not == 0)
1028 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1030 /* event message full, send it and start a new one */
1031 if (q && (q->cursize < q->maxsize))
1033 mp->n_macs = htonl (evt_idx);
1034 vl_msg_api_send_shmem (q, (u8 *) & mp);
1035 mp = allocate_mac_evt_buf (client, cl_idx);
1040 clib_warning ("MAC event to pid %d queue stuffed!"
1041 " %d MAC entries lost", client,
1047 if (result.fields.lrn_evt)
1049 /* copy mac entry to event msg */
1050 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac,
1052 mp->mac[evt_idx].is_del = 0;
1053 mp->mac[evt_idx].sw_if_index =
1054 htonl (result.fields.sw_if_index);
1055 /* clear event bit and update mac entry */
1056 result.fields.lrn_evt = 0;
1057 BVT (clib_bihash_kv) kv;
1059 kv.value = result.raw;
1060 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1062 continue; /* skip aging */
1066 if (event_only || result.fields.age_not)
1067 continue; /* skip aging - static_mac alsways age_not */
1069 /* start aging processing */
1070 u32 bd_index = key.fields.bd_index;
1071 u32 sw_if_index = result.fields.sw_if_index;
1072 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1073 if (result.fields.sn.as_u16 != sn)
1074 goto age_out; /* stale mac */
1076 l2_bridge_domain_t *bd_config =
1077 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1079 if (bd_config->mac_age == 0)
1080 continue; /* skip aging */
1082 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1083 delta += delta < 0 ? 256 : 0;
1085 if (delta < bd_config->mac_age)
1086 continue; /* still valid */
1091 /* copy mac entry to event msg */
1092 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac, 6);
1093 mp->mac[evt_idx].is_del = 1;
1094 mp->mac[evt_idx].sw_if_index =
1095 htonl (result.fields.sw_if_index);
1098 /* delete mac entry */
1099 BVT (clib_bihash_kv) kv;
1101 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1108 /* keep learn count consistent */
1109 l2learn_main.global_learn_count = learn_count;
1113 /* send any outstanding mac event message else free message buffer */
1116 if (q && (q->cursize < q->maxsize))
1118 mp->n_macs = htonl (evt_idx);
1119 vl_msg_api_send_shmem (q, (u8 *) & mp);
1124 clib_warning ("MAC event to pid %d queue stuffed!"
1125 " %d MAC entries lost", client, evt_idx);
1126 vl_msg_api_free (mp);
1130 vl_msg_api_free (mp);
1132 return delta_t + accum_t;
1135 /* Maximum f64 value */
1136 #define TIME_MAX (1.7976931348623157e+308)
1139 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1142 uword event_type, *event_data = 0;
1143 l2fib_main_t *fm = &l2fib_main;
1144 l2learn_main_t *lm = &l2learn_main;
1146 f64 start_time, next_age_scan_time = TIME_MAX;
1151 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1154 f64 t = next_age_scan_time - vlib_time_now (vm);
1155 vlib_process_wait_for_event_or_clock (vm, t);
1158 vlib_process_wait_for_event (vm);
1160 event_type = vlib_process_get_events (vm, &event_data);
1161 vec_reset_length (event_data);
1163 start_time = vlib_time_now (vm);
1165 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
1169 case ~0: /* timer expired */
1170 if (lm->client_pid != 0 && start_time < next_age_scan_time)
1171 scan = SCAN_MAC_EVENT;
1174 case L2_MAC_AGE_PROCESS_EVENT_START:
1178 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1180 scan = SCAN_DISABLE;
1183 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
1190 if (scan == SCAN_MAC_EVENT)
1191 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1194 if (scan == SCAN_MAC_AGE)
1195 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1196 if (scan == SCAN_DISABLE)
1198 l2fib_main.age_scan_duration = 0;
1199 l2fib_main.evt_scan_duration = 0;
1201 /* schedule next scan */
1203 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1205 next_age_scan_time = TIME_MAX;
1212 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1213 .function = l2fib_mac_age_scanner_process,
1214 .type = VLIB_NODE_TYPE_PROCESS,
1215 .name = "l2fib-mac-age-scanner-process",
1220 l2fib_init (vlib_main_t * vm)
1222 l2fib_main_t *mp = &l2fib_main;
1223 l2fib_entry_key_t test_key;
1227 mp->vnet_main = vnet_get_main ();
1229 /* Create the hash table */
1230 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1231 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
1233 /* verify the key constructor is good, since it is endian-sensitive */
1234 memset (test_mac, 0, sizeof (test_mac));
1237 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1238 ASSERT (test_key.fields.mac[0] == 0x11);
1239 ASSERT (test_key.fields.bd_index == 0x1234);
1244 VLIB_INIT_FUNCTION (l2fib_init);
1247 * fd.io coding-style-patch-verification: ON
1250 * eval: (c-set-style "gnu")