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;
58 incr_mac_address (u8 * mac)
60 u64 tmp = *((u64 *) mac);
61 tmp = clib_net_to_host_u64 (tmp);
62 tmp += 1 << 16; /* skip unused (least significant) octets */
63 tmp = clib_host_to_net_u64 (tmp);
65 clib_memcpy (mac, &tmp, 6);
68 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
70 format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
72 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
73 u32 sw_if_index = va_arg (*args, u32);
74 if (sw_if_index == ~0)
75 return format (s, "N/A");
77 vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
79 return format (s, "Stale");
81 return format (s, "%U", format_vnet_sw_interface_name, vnm,
82 vnet_get_sw_interface_safe (vnm, sw_if_index));
86 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
87 l2fib_entry_result_t ** l2fe_res)
89 l2fib_main_t *msm = &l2fib_main;
90 BVT (clib_bihash) * h = &msm->mac_table;
91 BVT (clib_bihash_bucket) * b;
92 BVT (clib_bihash_value) * v;
93 l2fib_entry_key_t key;
94 l2fib_entry_result_t result;
97 for (i = 0; i < h->nbuckets; i++)
102 v = BV (clib_bihash_get_value) (h, b->offset);
103 for (j = 0; j < (1 << b->log2_pages); j++)
105 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
107 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
110 key.raw = v->kvp[k].key;
111 result.raw = v->kvp[k].value;
113 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
115 vec_add1 (*l2fe_key, key);
116 vec_add1 (*l2fe_res, result);
124 /** Display the contents of the l2fib. */
125 static clib_error_t *
126 show_l2fib (vlib_main_t * vm,
127 unformat_input_t * input, vlib_cli_command_t * cmd)
129 bd_main_t *bdm = &bd_main;
130 l2fib_main_t *msm = &l2fib_main;
131 l2_bridge_domain_t *bd_config;
132 BVT (clib_bihash) * h = &msm->mac_table;
133 BVT (clib_bihash_bucket) * b;
134 BVT (clib_bihash_value) * v;
135 l2fib_entry_key_t key;
136 l2fib_entry_result_t result;
138 u64 total_entries = 0;
143 u32 bd_id, bd_index = ~0;
144 u8 now = (u8) (vlib_time_now (vm) / 60);
147 if (unformat (input, "raw"))
149 else if (unformat (input, "verbose"))
151 else if (unformat (input, "bd_index %d", &bd_index))
153 else if (unformat (input, "learn"))
158 else if (unformat (input, "bd_id %d", &bd_id))
160 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
169 vlib_cli_output (vm, "no such bridge domain id");
174 for (i = 0; i < h->nbuckets; i++)
179 v = BV (clib_bihash_get_value) (h, b->offset);
180 for (j = 0; j < (1 << b->log2_pages); j++)
182 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
184 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
187 if ((verbose || learn) && first_entry)
191 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
192 "Mac-Address", "BD-Idx", "If-Idx",
193 "BSN-ISN", "Age(min)", "static", "filter",
194 "bvi", "Interface-Name");
197 key.raw = v->kvp[k].key;
198 result.raw = v->kvp[k].value;
200 if ((verbose || learn)
201 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
203 if (learn && result.fields.age_not)
206 continue; /* skip provisioned macs */
209 bd_config = vec_elt_at_index (l2input_main.bd_configs,
210 key.fields.bd_index);
212 if (bd_config->mac_age && !result.fields.age_not)
214 i16 delta = now - result.fields.timestamp;
215 delta += delta < 0 ? 256 : 0;
216 s = format (s, "%d", delta);
222 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
223 format_ethernet_address, key.fields.mac,
225 result.fields.sw_if_index == ~0
226 ? -1 : result.fields.sw_if_index,
227 result.fields.sn.bd, result.fields.sn.swif,
228 s, result.fields.static_mac ? "*" : "-",
229 result.fields.filter ? "*" : "-",
230 result.fields.bvi ? "*" : "-",
231 format_vnet_sw_if_index_name_with_NA,
232 msm->vnet_main, result.fields.sw_if_index);
233 vec_reset_length (s);
241 if (total_entries == 0)
242 vlib_cli_output (vm, "no l2fib entries");
245 l2learn_main_t *lm = &l2learn_main;
246 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
247 "Last scan time: %.4esec Learn limit: %d ",
248 total_entries, lm->global_learn_count,
249 msm->age_scan_duration, lm->global_learn_limit);
251 vlib_cli_output (vm, "L2MAC events client PID: %d "
252 "Last e-scan time: %.4esec Delay: %.2esec "
253 "Max macs in event: %d",
254 lm->client_pid, msm->evt_scan_duration,
255 msm->event_scan_delay, msm->max_macs_in_event);
259 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
260 BV (format_bihash), h, 1 /* verbose */ );
267 * This command dispays the MAC Address entries of the L2 FIB table.
268 * Output can be filtered to just get the number of MAC Addresses or display
269 * each MAC Address for all bridge domains or just a single bridge domain.
272 * Example of how to display the number of MAC Address entries in the L2
274 * @cliexstart{show l2fib}
277 * Example of how to display all the MAC Address entries in the L2
279 * @cliexstart{show l2fib verbose}
280 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
281 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
282 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
283 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
288 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
289 .path = "show l2fib",
290 .short_help = "show l2fib [verbose | learn | bd_id <nn> | bd_index <nn> | raw",
291 .function = show_l2fib,
296 /* Remove all entries from the l2fib */
298 l2fib_clear_table (void)
300 l2fib_main_t *mp = &l2fib_main;
302 /* Remove all entries */
303 BV (clib_bihash_free) (&mp->mac_table);
304 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
305 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
306 l2learn_main.global_learn_count = 0;
309 /** Clear all entries in L2FIB.
310 * @TODO: Later we may want a way to remove only the non-static entries
312 static clib_error_t *
313 clear_l2fib (vlib_main_t * vm,
314 unformat_input_t * input, vlib_cli_command_t * cmd)
316 l2fib_clear_table ();
321 * This command clears all the MAC Address entries from the L2 FIB table.
324 * Example of how to clear the L2 FIB Table:
325 * @cliexcmd{clear l2fib}
326 * Example to show the L2 FIB Table has been cleared:
327 * @cliexstart{show l2fib verbose}
332 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
333 .path = "clear l2fib",
334 .short_help = "clear l2fib",
335 .function = clear_l2fib,
339 static inline l2fib_seq_num_t
340 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
342 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
344 return (l2fib_seq_num_t) {
345 .swif = *l2fib_swif_seq_num (sw_if_index),
346 .bd = bd_config->seq_num,
352 * Add an entry to the l2fib.
353 * If the entry already exists then overwrite it
356 l2fib_add_entry (u8 * mac, u32 bd_index,
357 u32 sw_if_index, u8 static_mac, u8 filter_mac, u8 bvi_mac)
359 l2fib_entry_key_t key;
360 l2fib_entry_result_t result;
361 __attribute__ ((unused)) u32 bucket_contents;
362 l2fib_main_t *fm = &l2fib_main;
363 l2learn_main_t *lm = &l2learn_main;
364 BVT (clib_bihash_kv) kv;
367 key.raw = l2fib_make_key (mac, bd_index);
369 /* check if entry alread exist */
370 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
372 /* decrement counter if overwriting a learned mac */
373 result.raw = kv.value;
374 if ((result.fields.age_not == 0) && (lm->global_learn_count))
375 lm->global_learn_count--;
379 result.raw = 0; /* clear all fields */
380 result.fields.sw_if_index = sw_if_index;
381 result.fields.static_mac = static_mac;
382 result.fields.filter = filter_mac;
383 result.fields.bvi = bvi_mac;
384 result.fields.age_not = 1; /* no aging for provisioned entry */
387 kv.value = result.raw;
389 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
393 * Add an entry to the L2FIB.
395 * l2fib add <mac> <bd> <intf> [static] [bvi]
396 * l2fib add <mac> <bd> filter
397 * Note that filter and bvi entries are always static
399 static clib_error_t *
400 l2fib_add (vlib_main_t * vm,
401 unformat_input_t * input, vlib_cli_command_t * cmd)
403 bd_main_t *bdm = &bd_main;
404 vnet_main_t *vnm = vnet_get_main ();
405 clib_error_t *error = 0;
409 u32 sw_if_index = ~0;
415 if (!unformat (input, "%U", unformat_ethernet_address, mac))
417 error = clib_error_return (0, "expected mac address `%U'",
418 format_unformat_error, input);
422 if (!unformat (input, "%d", &bd_id))
424 error = clib_error_return (0, "expected bridge domain ID `%U'",
425 format_unformat_error, input);
429 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
432 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
437 if (unformat (input, "filter"))
447 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
449 error = clib_error_return (0, "unknown interface `%U'",
450 format_unformat_error, input);
453 if (unformat (input, "static"))
457 else if (unformat (input, "bvi"))
464 if (vec_len (l2input_main.configs) <= sw_if_index)
466 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
472 l2fib_add_filter_entry (mac, bd_index);
474 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac);
481 * This command adds a MAC Address entry to the L2 FIB table
482 * of an existing bridge-domain. The MAC Address can be static
483 * or dynamic. This command also allows a filter to be added,
484 * such that packets with given MAC Addresses (source mac or
485 * destination mac match) are dropped.
488 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
489 * of a bridge-domain (where 200 is the bridge-domain-id):
490 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
491 * Example of how to add a static MAC Address entry to the L2 FIB table
492 * of a bridge-domain (where 200 is the bridge-domain-id):
493 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
494 * Example of how to add a filter such that a packet with the given MAC
495 * Address will be dropped in a given bridge-domain (where 200 is the
497 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
498 * Example of show command of the provisioned MAC Addresses and filters:
499 * @cliexstart{show l2fib verbose}
500 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
501 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
502 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
503 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
508 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
510 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
511 .function = l2fib_add,
516 static clib_error_t *
517 l2fib_test_command_fn (vlib_main_t * vm,
518 unformat_input_t * input, vlib_cli_command_t * cmd)
520 clib_error_t *error = 0;
521 u8 mac[6], save_mac[6];
532 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
534 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
536 else if (unformat (input, "add"))
538 else if (unformat (input, "del"))
540 else if (unformat (input, "check"))
542 else if (unformat (input, "count %d", &count))
549 return clib_error_return (0, "mac not set");
551 if (is_add == 0 && is_del == 0 && is_check == 0)
552 return clib_error_return (0,
553 "noop: pick at least one of (add,del,check)");
555 clib_memcpy (save_mac, mac, 6);
559 for (i = 0; i < count; i++)
561 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, *mac, bvi_mac);
562 incr_mac_address (mac);
568 BVT (clib_bihash_kv) kv;
569 l2fib_main_t *mp = &l2fib_main;
571 clib_memcpy (mac, save_mac, 6);
573 for (i = 0; i < count; i++)
575 kv.key = l2fib_make_key (mac, bd_index);
576 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
578 clib_warning ("key %U AWOL", format_ethernet_address, mac);
581 incr_mac_address (mac);
587 clib_memcpy (mac, save_mac, 6);
589 for (i = 0; i < count; i++)
591 l2fib_del_entry (mac, bd_index);
592 incr_mac_address (mac);
600 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
601 * bridge domain (bridge-domain-id of 0) to be modified.
605 * Example of how to add a set of 4 sequential MAC Address entries to L2
606 * FIB table of the default bridge-domain:
607 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
609 * Show the set of 4 sequential MAC Address entries that were added:
610 * @cliexstart{show l2fib verbose}
611 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
612 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
613 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
614 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
615 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
619 * Example of how to check that the set of 4 sequential MAC Address
620 * entries were added to L2 FIB table of the default
621 * bridge-domain. Used a count of 5 to produce an error:
623 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
624 * The output of the check command is in the log files. Log file
625 * location may vary based on your OS and Version:
627 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
629 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
631 * Example of how to delete a set of 4 sequential MAC Address entries
632 * from L2 FIB table of the default bridge-domain:
633 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
637 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
638 .path = "test l2fib",
639 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
640 .function = l2fib_test_command_fn,
646 * Delete an entry from the l2fib.
647 * Return 0 if the entry was deleted, or 1 if it was not found
650 l2fib_del_entry_by_key (u64 raw_key)
653 l2fib_entry_result_t result;
654 l2fib_main_t *mp = &l2fib_main;
655 BVT (clib_bihash_kv) kv;
660 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
663 result.raw = kv.value;
665 /* decrement counter if dynamically learned mac */
666 if ((result.fields.age_not == 0) && (l2learn_main.global_learn_count))
667 l2learn_main.global_learn_count--;
669 /* Remove entry from hash table */
670 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
675 * Delete an entry from the l2fib.
676 * Return 0 if the entry was deleted, or 1 if it was not found
679 l2fib_del_entry (u8 * mac, u32 bd_index)
681 return l2fib_del_entry_by_key (l2fib_make_key (mac, bd_index));
685 * Delete an entry from the L2FIB.
687 * l2fib del <mac> <bd-id>
689 static clib_error_t *
690 l2fib_del (vlib_main_t * vm,
691 unformat_input_t * input, vlib_cli_command_t * cmd)
693 bd_main_t *bdm = &bd_main;
694 clib_error_t *error = 0;
700 if (!unformat (input, "%U", unformat_ethernet_address, mac))
702 error = clib_error_return (0, "expected mac address `%U'",
703 format_unformat_error, input);
707 if (!unformat (input, "%d", &bd_id))
709 error = clib_error_return (0, "expected bridge domain ID `%U'",
710 format_unformat_error, input);
714 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
717 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
722 /* Delete the entry */
723 if (l2fib_del_entry (mac, bd_index))
725 error = clib_error_return (0, "mac entry not found");
734 * This command deletes an existing MAC Address entry from the L2 FIB
735 * table of an existing bridge-domain.
738 * 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):
739 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
742 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
744 .short_help = "l2fib del <mac> <bridge-domain-id>",
745 .function = l2fib_del,
750 Kick off ager to scan MACs to age/delete MAC entries
753 l2fib_start_ager_scan (vlib_main_t * vm)
755 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
757 /* check if there is at least one bd with mac aging enabled */
758 l2_bridge_domain_t *bd_config;
759 vec_foreach (bd_config, l2input_main.bd_configs)
761 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
763 evt = L2_MAC_AGE_PROCESS_EVENT_START;
768 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
773 Flush all non static MACs from an interface
776 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
778 *l2fib_swif_seq_num (sw_if_index) += 1;
779 l2fib_start_ager_scan (vm);
783 Flush all non static MACs in a bridge domain
786 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
788 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
789 bd_config->seq_num += 1;
790 l2fib_start_ager_scan (vm);
794 Flush all non static MACs - flushes all valid BDs
797 l2fib_flush_all_mac (vlib_main_t * vm)
799 l2_bridge_domain_t *bd_config;
800 vec_foreach (bd_config, l2input_main.bd_configs)
801 if (bd_is_valid (bd_config))
802 bd_config->seq_num += 1;
804 l2fib_start_ager_scan (vm);
809 Flush MACs, except static ones, associated with an interface
811 l2fib flush-mac interface <if-name>
813 static clib_error_t *
814 l2fib_flush_mac_int (vlib_main_t * vm,
815 unformat_input_t * input, vlib_cli_command_t * cmd)
817 vnet_main_t *vnm = vnet_get_main ();
818 clib_error_t *error = 0;
821 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
823 error = clib_error_return (0, "unknown interface `%U'",
824 format_unformat_error, input);
828 l2fib_flush_int_mac (vm, sw_if_index);
835 Flush all MACs, except static ones
839 static clib_error_t *
840 l2fib_flush_mac_all (vlib_main_t * vm,
841 unformat_input_t * input, vlib_cli_command_t * cmd)
843 l2fib_flush_all_mac (vm);
848 * This command kick off ager to delete all existing MAC Address entries,
849 * except static ones, associated with an interface from the L2 FIB table.
852 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
853 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
856 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
857 .path = "l2fib flush-mac all",
858 .short_help = "l2fib flush-mac all",
859 .function = l2fib_flush_mac_all,
864 * This command kick off ager to delete all existing MAC Address entries,
865 * except static ones, associated with an interface from the L2 FIB table.
868 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
869 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
872 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
873 .path = "l2fib flush-mac interface",
874 .short_help = "l2fib flush-mac interface <if-name>",
875 .function = l2fib_flush_mac_int,
880 Flush bridge-domain MACs except static ones.
882 l2fib flush-mac bridge-domain <bd-id>
884 static clib_error_t *
885 l2fib_flush_mac_bd (vlib_main_t * vm,
886 unformat_input_t * input, vlib_cli_command_t * cmd)
888 bd_main_t *bdm = &bd_main;
889 clib_error_t *error = 0;
893 if (!unformat (input, "%d", &bd_id))
895 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
896 format_unformat_error, input);
900 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
904 return clib_error_return (0, "No such bridge domain %d", bd_id);
906 l2fib_flush_bd_mac (vm, bd_index);
913 * This command kick off ager to delete all existing MAC Address entries,
914 * except static ones, in a bridge domain from the L2 FIB table.
917 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
918 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
921 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
922 .path = "l2fib flush-mac bridge-domain",
923 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
924 .function = l2fib_flush_mac_bd,
929 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
931 l2_input_config_t *config = l2input_intf_config (sw_if_index);
932 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
933 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
937 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
939 BVT (clib_bihash) * get_mac_table (void)
941 l2fib_main_t *mp = &l2fib_main;
942 return &mp->mac_table;
945 static_always_inline void *
946 allocate_mac_evt_buf (u32 client, u32 client_index)
948 l2fib_main_t *fm = &l2fib_main;
949 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
950 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
951 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
952 mp->pid = htonl (client);
953 mp->client_index = client_index;
957 static_always_inline f64
958 l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
960 l2fib_main_t *fm = &l2fib_main;
961 l2learn_main_t *lm = &l2learn_main;
963 BVT (clib_bihash) * h = &fm->mac_table;
965 f64 last_start = start_time;
970 u32 client = lm->client_pid;
971 u32 cl_idx = lm->client_index;
972 vl_api_l2_macs_event_t *mp = 0;
973 unix_shared_memory_queue_t *q = 0;
977 mp = allocate_mac_evt_buf (client, cl_idx);
978 q = vl_api_client_index_to_input_queue (lm->client_index);
981 for (i = 0; i < h->nbuckets; i++)
983 /* allow no more than 20us without a pause */
984 delta_t = vlib_time_now (vm) - last_start;
987 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
988 last_start = vlib_time_now (vm);
992 if (i < (h->nbuckets - 3))
994 BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
995 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
996 b = &h->buckets[i + 1];
999 BVT (clib_bihash_value) * v =
1000 BV (clib_bihash_get_value) (h, b->offset);
1001 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1005 BVT (clib_bihash_bucket) * b = &h->buckets[i];
1008 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1009 for (j = 0; j < (1 << b->log2_pages); j++)
1011 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1013 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1016 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1017 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1019 if (result.fields.age_not == 0)
1024 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1026 /* event message full, send it and start a new one */
1027 if (q && (q->cursize < q->maxsize))
1029 mp->n_macs = htonl (evt_idx);
1030 vl_msg_api_send_shmem (q, (u8 *) & mp);
1031 mp = allocate_mac_evt_buf (client, cl_idx);
1036 clib_warning ("MAC event to pid %d queue stuffed!"
1037 " %d MAC entries lost", client,
1043 if (result.fields.lrn_evt)
1045 /* copy mac entry to event msg */
1046 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac,
1048 mp->mac[evt_idx].is_del = 0;
1049 mp->mac[evt_idx].sw_if_index =
1050 htonl (result.fields.sw_if_index);
1051 /* clear event bit and update mac entry */
1052 result.fields.lrn_evt = 0;
1053 BVT (clib_bihash_kv) kv;
1055 kv.value = result.raw;
1056 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1058 continue; /* skip aging */
1062 if (event_only || result.fields.age_not)
1063 continue; /* skip aging - static_mac alsways age_not */
1065 /* start aging processing */
1066 u32 bd_index = key.fields.bd_index;
1067 u32 sw_if_index = result.fields.sw_if_index;
1068 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1069 if (result.fields.sn.as_u16 != sn)
1070 goto age_out; /* stale mac */
1072 l2_bridge_domain_t *bd_config =
1073 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1075 if (bd_config->mac_age == 0)
1076 continue; /* skip aging */
1078 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1079 delta += delta < 0 ? 256 : 0;
1081 if (delta < bd_config->mac_age)
1082 continue; /* still valid */
1087 /* copy mac entry to event msg */
1088 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac, 6);
1089 mp->mac[evt_idx].is_del = 1;
1090 mp->mac[evt_idx].sw_if_index =
1091 htonl (result.fields.sw_if_index);
1094 /* delete mac entry */
1095 BVT (clib_bihash_kv) kv;
1097 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1104 /* keep learn count consistent */
1105 l2learn_main.global_learn_count = learn_count;
1109 /* send any outstanding mac event message else free message buffer */
1112 if (q && (q->cursize < q->maxsize))
1114 mp->n_macs = htonl (evt_idx);
1115 vl_msg_api_send_shmem (q, (u8 *) & mp);
1120 clib_warning ("MAC event to pid %d queue stuffed!"
1121 " %d MAC entries lost", client, evt_idx);
1122 vl_msg_api_free (mp);
1126 vl_msg_api_free (mp);
1128 return delta_t + accum_t;
1131 /* Maximum f64 value */
1132 #define TIME_MAX (1.7976931348623157e+308)
1135 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1138 uword event_type, *event_data = 0;
1139 l2fib_main_t *fm = &l2fib_main;
1140 l2learn_main_t *lm = &l2learn_main;
1142 f64 start_time, next_age_scan_time = TIME_MAX;
1147 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1150 f64 t = next_age_scan_time - vlib_time_now (vm);
1151 vlib_process_wait_for_event_or_clock (vm, t);
1154 vlib_process_wait_for_event (vm);
1156 event_type = vlib_process_get_events (vm, &event_data);
1157 vec_reset_length (event_data);
1159 start_time = vlib_time_now (vm);
1161 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
1165 case ~0: /* timer expired */
1166 if (lm->client_pid != 0 && start_time < next_age_scan_time)
1167 scan = SCAN_MAC_EVENT;
1170 case L2_MAC_AGE_PROCESS_EVENT_START:
1174 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1176 scan = SCAN_DISABLE;
1179 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
1186 if (scan == SCAN_MAC_EVENT)
1187 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1190 if (scan == SCAN_MAC_AGE)
1191 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1192 if (scan == SCAN_DISABLE)
1194 l2fib_main.age_scan_duration = 0;
1195 l2fib_main.evt_scan_duration = 0;
1197 /* schedule next scan */
1199 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1201 next_age_scan_time = TIME_MAX;
1208 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1209 .function = l2fib_mac_age_scanner_process,
1210 .type = VLIB_NODE_TYPE_PROCESS,
1211 .name = "l2fib-mac-age-scanner-process",
1216 l2fib_init (vlib_main_t * vm)
1218 l2fib_main_t *mp = &l2fib_main;
1219 l2fib_entry_key_t test_key;
1223 mp->vnet_main = vnet_get_main ();
1225 /* Create the hash table */
1226 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1227 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
1229 /* verify the key constructor is good, since it is endian-sensitive */
1230 memset (test_mac, 0, sizeof (test_mac));
1233 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1234 ASSERT (test_key.fields.mac[0] == 0x11);
1235 ASSERT (test_key.fields.bd_index == 0x1234);
1240 VLIB_INIT_FUNCTION (l2fib_init);
1243 * fd.io coding-style-patch-verification: ON
1246 * eval: (c-set-style "gnu")