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));
85 typedef struct l2fib_dump_walk_ctx_t_
88 l2fib_entry_key_t *l2fe_key;
89 l2fib_entry_result_t *l2fe_res;
90 } l2fib_dump_walk_ctx_t;
93 l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
95 l2fib_dump_walk_ctx_t *ctx = arg;
96 l2fib_entry_result_t result;
97 l2fib_entry_key_t key;
100 result.raw = kvp->value;
102 if ((ctx->bd_index == ~0) || (ctx->bd_index == key.fields.bd_index))
104 vec_add1 (ctx->l2fe_key, key);
105 vec_add1 (ctx->l2fe_res, result);
110 l2fib_table_dump (u32 bd_index,
111 l2fib_entry_key_t ** l2fe_key,
112 l2fib_entry_result_t ** l2fe_res)
114 l2fib_main_t *msm = &l2fib_main;
115 l2fib_dump_walk_ctx_t ctx = {
116 .bd_index = bd_index,
119 BV (clib_bihash_foreach_key_value_pair)
120 (&msm->mac_table, l2fib_dump_walk_cb, &ctx);
122 *l2fe_key = ctx.l2fe_key;
123 *l2fe_res = ctx.l2fe_res;
126 typedef struct l2fib_show_walk_ctx_t_
137 } l2fib_show_walk_ctx_t;
140 l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
142 l2fib_show_walk_ctx_t *ctx = arg;
143 l2_bridge_domain_t *bd_config;
144 l2fib_entry_result_t result;
145 l2fib_entry_key_t key;
147 if (ctx->verbose && ctx->first_entry)
149 ctx->first_entry = 0;
150 vlib_cli_output (ctx->vm,
151 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
152 "Mac-Address", "BD-Idx", "If-Idx",
153 "BSN-ISN", "Age(min)", "static", "filter",
154 "bvi", "Interface-Name");
158 result.raw = kvp->value;
159 ctx->total_entries++;
162 ((ctx->bd_index >> 31) || (ctx->bd_index == key.fields.bd_index)))
166 if (ctx->learn && result.fields.age_not)
167 return; /* skip provisioned macs */
169 if (ctx->add && !result.fields.age_not)
170 return; /* skip learned macs */
172 bd_config = vec_elt_at_index (l2input_main.bd_configs,
173 key.fields.bd_index);
175 if (result.fields.age_not)
176 s = format (s, "no");
177 else if (bd_config->mac_age == 0)
181 i16 delta = ctx->now - result.fields.timestamp;
182 delta += delta < 0 ? 256 : 0;
183 s = format (s, "%d", delta);
186 vlib_cli_output (ctx->vm,
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 ctx->vnm, result.fields.sw_if_index);
202 /** Display the contents of the l2fib. */
203 static clib_error_t *
204 show_l2fib (vlib_main_t * vm,
205 unformat_input_t * input, vlib_cli_command_t * cmd)
207 bd_main_t *bdm = &bd_main;
208 l2fib_main_t *msm = &l2fib_main;
211 l2fib_show_walk_ctx_t ctx = {
214 .now = (u8) (vlib_time_now (vm) / 60),
216 .vnm = msm->vnet_main,
219 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
221 if (unformat (input, "raw"))
227 else if (unformat (input, "verbose"))
229 else if (unformat (input, "all"))
231 else if (unformat (input, "bd_index %d", &ctx.bd_index))
233 else if (unformat (input, "learn"))
239 else if (unformat (input, "add"))
245 else if (unformat (input, "bd_id %d", &bd_id))
247 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
254 return clib_error_return (0,
255 "bridge domain id %d doesn't exist\n",
262 BV (clib_bihash_foreach_key_value_pair)
263 (&msm->mac_table, l2fib_show_walk_cb, &ctx);
265 if (ctx.total_entries == 0)
266 vlib_cli_output (vm, "no l2fib entries");
269 l2learn_main_t *lm = &l2learn_main;
270 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
271 "Last scan time: %.4esec Learn limit: %d ",
272 ctx.total_entries, lm->global_learn_count,
273 msm->age_scan_duration, lm->global_learn_limit);
275 vlib_cli_output (vm, "L2MAC events client PID: %d "
276 "Last e-scan time: %.4esec Delay: %.2esec "
277 "Max macs in event: %d",
278 lm->client_pid, msm->evt_scan_duration,
279 msm->event_scan_delay, msm->max_macs_in_event);
283 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
284 BV (format_bihash), &msm->mac_table, 1 /* verbose */ );
290 * This command dispays the MAC Address entries of the L2 FIB table.
291 * Output can be filtered to just get the number of MAC Addresses or display
292 * each MAC Address for all bridge domains or just a single bridge domain.
295 * Example of how to display the number of MAC Address entries in the L2
297 * @cliexstart{show l2fib}
300 * Example of how to display all the MAC Address entries in the L2
302 * @cliexstart{show l2fib all}
303 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
304 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
305 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
306 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
311 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
312 .path = "show l2fib",
313 .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]",
314 .function = show_l2fib,
319 /* Remove all entries from the l2fib */
321 l2fib_clear_table (void)
323 l2fib_main_t *mp = &l2fib_main;
325 /* Remove all entries */
326 BV (clib_bihash_free) (&mp->mac_table);
327 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
328 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
329 l2learn_main.global_learn_count = 0;
332 /** Clear all entries in L2FIB.
333 * @TODO: Later we may want a way to remove only the non-static entries
335 static clib_error_t *
336 clear_l2fib (vlib_main_t * vm,
337 unformat_input_t * input, vlib_cli_command_t * cmd)
339 l2fib_clear_table ();
344 * This command clears all the MAC Address entries from the L2 FIB table.
347 * Example of how to clear the L2 FIB Table:
348 * @cliexcmd{clear l2fib}
349 * Example to show the L2 FIB Table has been cleared:
350 * @cliexstart{show l2fib verbose}
355 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
356 .path = "clear l2fib",
357 .short_help = "clear l2fib",
358 .function = clear_l2fib,
362 static inline l2fib_seq_num_t
363 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
365 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
367 return (l2fib_seq_num_t) {
368 .swif = *l2fib_swif_seq_num (sw_if_index),
369 .bd = bd_config->seq_num,
375 * Add an entry to the l2fib.
376 * If the entry already exists then overwrite it
379 l2fib_add_entry (u8 * mac, u32 bd_index,
380 u32 sw_if_index, u8 static_mac, u8 filter_mac, u8 bvi_mac)
382 l2fib_entry_key_t key;
383 l2fib_entry_result_t result;
384 __attribute__ ((unused)) u32 bucket_contents;
385 l2fib_main_t *fm = &l2fib_main;
386 l2learn_main_t *lm = &l2learn_main;
387 BVT (clib_bihash_kv) kv;
390 key.raw = l2fib_make_key (mac, bd_index);
392 /* check if entry alread exist */
393 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
395 /* decrement counter if overwriting a learned mac */
396 result.raw = kv.value;
397 if ((result.fields.age_not == 0) && (lm->global_learn_count))
398 lm->global_learn_count--;
402 result.raw = 0; /* clear all fields */
403 result.fields.sw_if_index = sw_if_index;
404 result.fields.static_mac = static_mac;
405 result.fields.filter = filter_mac;
406 result.fields.bvi = bvi_mac;
407 result.fields.age_not = 1; /* no aging for provisioned entry */
410 kv.value = result.raw;
412 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
416 * Add an entry to the L2FIB.
418 * l2fib add <mac> <bd> <intf> [static] [bvi]
419 * l2fib add <mac> <bd> filter
420 * Note that filter and bvi entries are always static
422 static clib_error_t *
423 l2fib_add (vlib_main_t * vm,
424 unformat_input_t * input, vlib_cli_command_t * cmd)
426 bd_main_t *bdm = &bd_main;
427 vnet_main_t *vnm = vnet_get_main ();
428 clib_error_t *error = 0;
432 u32 sw_if_index = ~0;
437 if (!unformat (input, "%U", unformat_ethernet_address, mac))
439 error = clib_error_return (0, "expected mac address `%U'",
440 format_unformat_error, input);
444 if (!unformat (input, "%d", &bd_id))
446 error = clib_error_return (0, "expected bridge domain ID `%U'",
447 format_unformat_error, input);
451 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
454 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
459 if (unformat (input, "filter"))
461 l2fib_add_filter_entry (mac, bd_index);
465 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
467 error = clib_error_return (0, "unknown interface `%U'",
468 format_unformat_error, input);
472 if (unformat (input, "static"))
476 else if (unformat (input, "bvi"))
482 if (vec_len (l2input_main.configs) <= sw_if_index)
484 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
489 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac);
496 * This command adds a MAC Address entry to the L2 FIB table
497 * of an existing bridge-domain. The MAC Address can be static
498 * or dynamic. This command also allows a filter to be added,
499 * such that packets with given MAC Addresses (source mac or
500 * destination mac match) are dropped.
503 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
504 * of a bridge-domain (where 200 is the bridge-domain-id):
505 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
506 * Example of how to add a static MAC Address entry to the L2 FIB table
507 * of a bridge-domain (where 200 is the bridge-domain-id):
508 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
509 * Example of how to add a filter such that a packet with the given MAC
510 * Address will be dropped in a given bridge-domain (where 200 is the
512 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
513 * Example of show command of the provisioned MAC Addresses and filters:
514 * @cliexstart{show l2fib verbose}
515 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
516 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
517 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
518 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
523 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
525 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
526 .function = l2fib_add,
531 static clib_error_t *
532 l2fib_test_command_fn (vlib_main_t * vm,
533 unformat_input_t * input, vlib_cli_command_t * cmd)
535 clib_error_t *error = 0;
536 u8 mac[6], save_mac[6];
547 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
549 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
551 else if (unformat (input, "add"))
553 else if (unformat (input, "del"))
555 else if (unformat (input, "check"))
557 else if (unformat (input, "count %d", &count))
564 return clib_error_return (0, "mac not set");
566 if (is_add == 0 && is_del == 0 && is_check == 0)
567 return clib_error_return (0,
568 "noop: pick at least one of (add,del,check)");
570 clib_memcpy (save_mac, mac, 6);
574 for (i = 0; i < count; i++)
576 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, *mac, bvi_mac);
577 incr_mac_address (mac);
583 BVT (clib_bihash_kv) kv;
584 l2fib_main_t *mp = &l2fib_main;
586 clib_memcpy (mac, save_mac, 6);
588 for (i = 0; i < count; i++)
590 kv.key = l2fib_make_key (mac, bd_index);
591 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
593 clib_warning ("key %U AWOL", format_ethernet_address, mac);
596 incr_mac_address (mac);
602 clib_memcpy (mac, save_mac, 6);
604 for (i = 0; i < count; i++)
606 l2fib_del_entry (mac, bd_index, 0);
607 incr_mac_address (mac);
615 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
616 * bridge domain (bridge-domain-id of 0) to be modified.
620 * Example of how to add a set of 4 sequential MAC Address entries to L2
621 * FIB table of the default bridge-domain:
622 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
624 * Show the set of 4 sequential MAC Address entries that were added:
625 * @cliexstart{show l2fib verbose}
626 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
627 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
628 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
629 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
630 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
634 * Example of how to check that the set of 4 sequential MAC Address
635 * entries were added to L2 FIB table of the default
636 * bridge-domain. Used a count of 5 to produce an error:
638 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
639 * The output of the check command is in the log files. Log file
640 * location may vary based on your OS and Version:
642 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
644 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
646 * Example of how to delete a set of 4 sequential MAC Address entries
647 * from L2 FIB table of the default bridge-domain:
648 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
652 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
653 .path = "test l2fib",
654 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
655 .function = l2fib_test_command_fn,
661 * Delete an entry from the l2fib.
662 * Return 0 if the entry was deleted, or 1 it was not found or if
663 * sw_if_index is non-zero and does not match that in the entry.
666 l2fib_del_entry (u8 * mac, u32 bd_index, u32 sw_if_index)
668 l2fib_entry_result_t result;
669 l2fib_main_t *mp = &l2fib_main;
670 BVT (clib_bihash_kv) kv;
673 kv.key = l2fib_make_key (mac, bd_index);
675 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
678 result.raw = kv.value;
680 /* check if sw_if_index of entry match */
681 if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index))
684 /* decrement counter if dynamically learned mac */
685 if ((result.fields.age_not == 0) && (l2learn_main.global_learn_count))
686 l2learn_main.global_learn_count--;
688 /* Remove entry from hash table */
689 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
694 * Delete an entry from the L2FIB.
696 * l2fib del <mac> <bd-id>
698 static clib_error_t *
699 l2fib_del (vlib_main_t * vm,
700 unformat_input_t * input, vlib_cli_command_t * cmd)
702 bd_main_t *bdm = &bd_main;
703 clib_error_t *error = 0;
709 if (!unformat (input, "%U", unformat_ethernet_address, mac))
711 error = clib_error_return (0, "expected mac address `%U'",
712 format_unformat_error, input);
716 if (!unformat (input, "%d", &bd_id))
718 error = clib_error_return (0, "expected bridge domain ID `%U'",
719 format_unformat_error, input);
723 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
726 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
731 /* Delete the entry */
732 if (l2fib_del_entry (mac, bd_index, 0))
734 error = clib_error_return (0, "mac entry not found");
743 * This command deletes an existing MAC Address entry from the L2 FIB
744 * table of an existing bridge-domain.
747 * 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):
748 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
751 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
753 .short_help = "l2fib del <mac> <bridge-domain-id> []",
754 .function = l2fib_del,
759 Kick off ager to scan MACs to age/delete MAC entries
762 l2fib_start_ager_scan (vlib_main_t * vm)
764 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
766 /* check if there is at least one bd with mac aging enabled */
767 l2_bridge_domain_t *bd_config;
768 vec_foreach (bd_config, l2input_main.bd_configs)
770 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
772 evt = L2_MAC_AGE_PROCESS_EVENT_START;
777 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
782 Flush all non static MACs from an interface
785 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
787 *l2fib_swif_seq_num (sw_if_index) += 1;
788 l2fib_start_ager_scan (vm);
792 Flush all non static MACs in a bridge domain
795 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
797 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
798 bd_config->seq_num += 1;
799 l2fib_start_ager_scan (vm);
803 Flush all non static MACs - flushes all valid BDs
806 l2fib_flush_all_mac (vlib_main_t * vm)
808 l2_bridge_domain_t *bd_config;
809 vec_foreach (bd_config, l2input_main.bd_configs)
810 if (bd_is_valid (bd_config))
811 bd_config->seq_num += 1;
813 l2fib_start_ager_scan (vm);
818 Flush MACs, except static ones, associated with an interface
820 l2fib flush-mac interface <if-name>
822 static clib_error_t *
823 l2fib_flush_mac_int (vlib_main_t * vm,
824 unformat_input_t * input, vlib_cli_command_t * cmd)
826 vnet_main_t *vnm = vnet_get_main ();
827 clib_error_t *error = 0;
830 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
832 error = clib_error_return (0, "unknown interface `%U'",
833 format_unformat_error, input);
837 l2fib_flush_int_mac (vm, sw_if_index);
844 Flush all MACs, except static ones
848 static clib_error_t *
849 l2fib_flush_mac_all (vlib_main_t * vm,
850 unformat_input_t * input, vlib_cli_command_t * cmd)
852 l2fib_flush_all_mac (vm);
857 * This command kick off ager to delete all existing MAC Address entries,
858 * except static ones, associated with an interface from the L2 FIB table.
861 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
862 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
865 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
866 .path = "l2fib flush-mac all",
867 .short_help = "l2fib flush-mac all",
868 .function = l2fib_flush_mac_all,
873 * This command kick off ager to delete all existing MAC Address entries,
874 * except static ones, associated with an interface from the L2 FIB table.
877 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
878 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
881 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
882 .path = "l2fib flush-mac interface",
883 .short_help = "l2fib flush-mac interface <if-name>",
884 .function = l2fib_flush_mac_int,
889 Flush bridge-domain MACs except static ones.
891 l2fib flush-mac bridge-domain <bd-id>
893 static clib_error_t *
894 l2fib_flush_mac_bd (vlib_main_t * vm,
895 unformat_input_t * input, vlib_cli_command_t * cmd)
897 bd_main_t *bdm = &bd_main;
898 clib_error_t *error = 0;
902 if (!unformat (input, "%d", &bd_id))
904 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
905 format_unformat_error, input);
909 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
913 return clib_error_return (0, "No such bridge domain %d", bd_id);
915 l2fib_flush_bd_mac (vm, bd_index);
922 * This command kick off ager to delete all existing MAC Address entries,
923 * except static ones, in a bridge domain from the L2 FIB table.
926 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
927 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
930 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
931 .path = "l2fib flush-mac bridge-domain",
932 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
933 .function = l2fib_flush_mac_bd,
938 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
940 l2_input_config_t *config = l2input_intf_config (sw_if_index);
941 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
942 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
946 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
948 BVT (clib_bihash) * get_mac_table (void)
950 l2fib_main_t *mp = &l2fib_main;
951 return &mp->mac_table;
954 static_always_inline void *
955 allocate_mac_evt_buf (u32 client, u32 client_index)
957 l2fib_main_t *fm = &l2fib_main;
958 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
959 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
960 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
961 mp->pid = htonl (client);
962 mp->client_index = client_index;
966 static_always_inline f64
967 l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
969 l2fib_main_t *fm = &l2fib_main;
970 l2learn_main_t *lm = &l2learn_main;
972 BVT (clib_bihash) * h = &fm->mac_table;
974 f64 last_start = start_time;
979 u32 client = lm->client_pid;
980 u32 cl_idx = lm->client_index;
981 vl_api_l2_macs_event_t *mp = 0;
982 vl_api_registration_t *reg = 0;
986 mp = allocate_mac_evt_buf (client, cl_idx);
987 reg = vl_api_client_index_to_registration (lm->client_index);
990 for (i = 0; i < h->nbuckets; i++)
992 /* allow no more than 20us without a pause */
993 delta_t = vlib_time_now (vm) - last_start;
996 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
997 last_start = vlib_time_now (vm);
1001 if (i < (h->nbuckets - 3))
1003 BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
1004 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
1005 b = &h->buckets[i + 1];
1008 BVT (clib_bihash_value) * v =
1009 BV (clib_bihash_get_value) (h, b->offset);
1010 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1014 BVT (clib_bihash_bucket) * b = &h->buckets[i];
1017 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1018 for (j = 0; j < (1 << b->log2_pages); j++)
1020 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1022 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1025 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1026 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1028 if (result.fields.age_not == 0)
1033 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1035 /* event message full, send it and start a new one */
1036 if (reg && vl_api_can_send_msg (reg))
1038 mp->n_macs = htonl (evt_idx);
1039 vl_api_send_msg (reg, (u8 *) mp);
1040 mp = allocate_mac_evt_buf (client, cl_idx);
1045 clib_warning ("MAC event to pid %d queue stuffed!"
1046 " %d MAC entries lost", client,
1052 if (result.fields.lrn_evt)
1054 /* copy mac entry to event msg */
1055 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac,
1057 mp->mac[evt_idx].action = result.fields.lrn_mov ?
1058 MAC_EVENT_ACTION_MOVE : MAC_EVENT_ACTION_ADD;
1059 mp->mac[evt_idx].sw_if_index =
1060 htonl (result.fields.sw_if_index);
1061 /* clear event bits and update mac entry */
1062 result.fields.lrn_evt = 0;
1063 result.fields.lrn_mov = 0;
1064 BVT (clib_bihash_kv) kv;
1066 kv.value = result.raw;
1067 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1069 continue; /* skip aging */
1073 if (event_only || result.fields.age_not)
1074 continue; /* skip aging - static_mac alsways age_not */
1076 /* start aging processing */
1077 u32 bd_index = key.fields.bd_index;
1078 u32 sw_if_index = result.fields.sw_if_index;
1079 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1080 if (result.fields.sn.as_u16 != sn)
1081 goto age_out; /* stale mac */
1083 l2_bridge_domain_t *bd_config =
1084 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1086 if (bd_config->mac_age == 0)
1087 continue; /* skip aging */
1089 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1090 delta += delta < 0 ? 256 : 0;
1092 if (delta < bd_config->mac_age)
1093 continue; /* still valid */
1098 /* copy mac entry to event msg */
1099 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac, 6);
1100 mp->mac[evt_idx].action = MAC_EVENT_ACTION_DELETE;
1101 mp->mac[evt_idx].sw_if_index =
1102 htonl (result.fields.sw_if_index);
1105 /* delete mac entry */
1106 BVT (clib_bihash_kv) kv;
1108 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1111 * Note: we may have just freed the bucket's backing
1112 * storage, so check right here...
1123 /* keep learn count consistent */
1124 l2learn_main.global_learn_count = learn_count;
1128 /* send any outstanding mac event message else free message buffer */
1131 if (reg && vl_api_can_send_msg (reg))
1133 mp->n_macs = htonl (evt_idx);
1134 vl_api_send_msg (reg, (u8 *) mp);
1139 clib_warning ("MAC event to pid %d queue stuffed!"
1140 " %d MAC entries lost", client, evt_idx);
1141 vl_msg_api_free (mp);
1145 vl_msg_api_free (mp);
1147 return delta_t + accum_t;
1151 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1154 uword event_type, *event_data = 0;
1155 l2fib_main_t *fm = &l2fib_main;
1156 l2learn_main_t *lm = &l2learn_main;
1158 f64 start_time, next_age_scan_time = CLIB_TIME_MAX;
1163 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1166 f64 t = next_age_scan_time - vlib_time_now (vm);
1167 vlib_process_wait_for_event_or_clock (vm, t);
1170 vlib_process_wait_for_event (vm);
1172 event_type = vlib_process_get_events (vm, &event_data);
1173 vec_reset_length (event_data);
1175 start_time = vlib_time_now (vm);
1177 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
1181 case ~0: /* timer expired */
1182 if (lm->client_pid != 0 && start_time < next_age_scan_time)
1183 scan = SCAN_MAC_EVENT;
1186 case L2_MAC_AGE_PROCESS_EVENT_START:
1190 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1192 scan = SCAN_DISABLE;
1195 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
1202 if (scan == SCAN_MAC_EVENT)
1203 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1206 if (scan == SCAN_MAC_AGE)
1207 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1208 if (scan == SCAN_DISABLE)
1210 l2fib_main.age_scan_duration = 0;
1211 l2fib_main.evt_scan_duration = 0;
1213 /* schedule next scan */
1215 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1217 next_age_scan_time = CLIB_TIME_MAX;
1224 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1225 .function = l2fib_mac_age_scanner_process,
1226 .type = VLIB_NODE_TYPE_PROCESS,
1227 .name = "l2fib-mac-age-scanner-process",
1232 l2fib_init (vlib_main_t * vm)
1234 l2fib_main_t *mp = &l2fib_main;
1235 l2fib_entry_key_t test_key;
1239 mp->vnet_main = vnet_get_main ();
1241 /* Create the hash table */
1242 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1243 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
1245 /* verify the key constructor is good, since it is endian-sensitive */
1246 memset (test_mac, 0, sizeof (test_mac));
1249 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1250 ASSERT (test_key.fields.mac[0] == 0x11);
1251 ASSERT (test_key.fields.bd_index == 0x1234);
1256 VLIB_INIT_FUNCTION (l2fib_init);
1259 * fd.io coding-style-patch-verification: ON
1262 * eval: (c-set-style "gnu")