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 format_l2fib_entry_result_flags (u8 * s, va_list * args)
60 l2fib_entry_result_flags_t flags = va_arg (*args, int);
62 if (L2FIB_ENTRY_RESULT_FLAG_NONE == flags)
64 s = format (s, "none");
69 if (flags & L2FIB_ENTRY_RESULT_FLAG_##a) \
70 s = format (s, "%s ", t); \
72 foreach_l2fib_entry_result_attr
79 incr_mac_address (u8 * mac)
81 u64 tmp = *((u64 *) mac);
82 tmp = clib_net_to_host_u64 (tmp);
83 tmp += 1 << 16; /* skip unused (least significant) octets */
84 tmp = clib_host_to_net_u64 (tmp);
86 clib_memcpy_fast (mac, &tmp, 6);
89 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
91 format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
93 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
94 u32 sw_if_index = va_arg (*args, u32);
95 if (sw_if_index == ~0)
96 return format (s, "N/A");
98 vnet_sw_interface_t *swif =
99 vnet_get_sw_interface_or_null (vnm, sw_if_index);
101 return format (s, "Stale");
103 return format (s, "%U", format_vnet_sw_interface_name, vnm,
104 vnet_get_sw_interface_or_null (vnm, sw_if_index));
107 typedef struct l2fib_dump_walk_ctx_t_
110 l2fib_entry_key_t *l2fe_key;
111 l2fib_entry_result_t *l2fe_res;
112 } l2fib_dump_walk_ctx_t;
115 l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
117 l2fib_dump_walk_ctx_t *ctx = arg;
118 l2fib_entry_result_t result;
119 l2fib_entry_key_t key;
122 result.raw = kvp->value;
124 if ((ctx->bd_index == ~0) || (ctx->bd_index == key.fields.bd_index))
126 vec_add1 (ctx->l2fe_key, key);
127 vec_add1 (ctx->l2fe_res, result);
130 return (BIHASH_WALK_CONTINUE);
134 l2fib_table_dump (u32 bd_index,
135 l2fib_entry_key_t ** l2fe_key,
136 l2fib_entry_result_t ** l2fe_res)
138 l2fib_main_t *msm = &l2fib_main;
139 l2fib_dump_walk_ctx_t ctx = {
140 .bd_index = bd_index,
143 BV (clib_bihash_foreach_key_value_pair)
144 (&msm->mac_table, l2fib_dump_walk_cb, &ctx);
146 *l2fe_key = ctx.l2fe_key;
147 *l2fe_res = ctx.l2fe_res;
150 typedef struct l2fib_show_walk_ctx_t_
161 } l2fib_show_walk_ctx_t;
164 l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
166 l2fib_show_walk_ctx_t *ctx = arg;
167 l2_bridge_domain_t *bd_config;
168 l2fib_entry_result_t result;
169 l2fib_entry_key_t key;
171 if (ctx->verbose && ctx->first_entry)
173 ctx->first_entry = 0;
174 vlib_cli_output (ctx->vm,
175 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
176 "Mac-Address", "BD-Idx", "If-Idx",
177 "BSN-ISN", "Age(min)", "static", "filter",
178 "bvi", "Interface-Name");
182 result.raw = kvp->value;
183 ctx->total_entries++;
186 ((ctx->bd_index >> 31) || (ctx->bd_index == key.fields.bd_index)))
190 if (ctx->learn && l2fib_entry_result_is_set_AGE_NOT (&result))
191 return (BIHASH_WALK_CONTINUE); /* skip provisioned macs */
193 if (ctx->add && !l2fib_entry_result_is_set_AGE_NOT (&result))
194 return (BIHASH_WALK_CONTINUE); /* skip learned macs */
196 bd_config = vec_elt_at_index (l2input_main.bd_configs,
197 key.fields.bd_index);
199 if (l2fib_entry_result_is_set_AGE_NOT (&result))
200 s = format (s, "no");
201 else if (bd_config->mac_age == 0)
205 i16 delta = ctx->now - result.fields.timestamp;
206 delta += delta < 0 ? 256 : 0;
207 s = format (s, "%d", delta);
210 vlib_cli_output (ctx->vm,
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, s,
217 l2fib_entry_result_is_set_STATIC (&result) ? "*" : "-",
218 l2fib_entry_result_is_set_FILTER (&result) ? "*" : "-",
219 l2fib_entry_result_is_set_BVI (&result) ? "*" : "-",
220 format_vnet_sw_if_index_name_with_NA,
221 ctx->vnm, result.fields.sw_if_index);
225 return (BIHASH_WALK_CONTINUE);
228 /** Display the contents of the l2fib. */
229 static clib_error_t *
230 show_l2fib (vlib_main_t * vm,
231 unformat_input_t * input, vlib_cli_command_t * cmd)
233 bd_main_t *bdm = &bd_main;
234 l2fib_main_t *msm = &l2fib_main;
237 l2fib_show_walk_ctx_t ctx = {
240 .now = (u8) (vlib_time_now (vm) / 60),
242 .vnm = msm->vnet_main,
245 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
247 if (unformat (input, "raw"))
253 else if (unformat (input, "verbose"))
255 else if (unformat (input, "all"))
257 else if (unformat (input, "bd_index %d", &ctx.bd_index))
259 else if (unformat (input, "learn"))
265 else if (unformat (input, "add"))
271 else if (unformat (input, "bd_id %d", &bd_id))
273 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
280 return clib_error_return (0,
281 "bridge domain id %d doesn't exist\n",
288 BV (clib_bihash_foreach_key_value_pair)
289 (&msm->mac_table, l2fib_show_walk_cb, &ctx);
291 if (ctx.total_entries == 0)
292 vlib_cli_output (vm, "no l2fib entries");
295 l2learn_main_t *lm = &l2learn_main;
296 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
297 "Last scan time: %.4esec Learn limit: %d ",
298 ctx.total_entries, lm->global_learn_count,
299 msm->age_scan_duration, lm->global_learn_limit);
301 vlib_cli_output (vm, "L2MAC events client PID: %d "
302 "Last e-scan time: %.4esec Delay: %.2esec "
303 "Max macs in event: %d",
304 lm->client_pid, msm->evt_scan_duration,
305 msm->event_scan_delay, msm->max_macs_in_event);
309 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
310 BV (format_bihash), &msm->mac_table, 1 /* verbose */ );
316 * This command displays the MAC Address entries of the L2 FIB table.
317 * Output can be filtered to just get the number of MAC Addresses or display
318 * each MAC Address for all bridge domains or just a single bridge domain.
321 * Example of how to display the number of MAC Address entries in the L2
323 * @cliexstart{show l2fib}
326 * Example of how to display all the MAC Address entries in the L2
328 * @cliexstart{show l2fib all}
329 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
330 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
331 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
332 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
337 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
338 .path = "show l2fib",
339 .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]",
340 .function = show_l2fib,
345 /* Remove all entries from the l2fib */
347 l2fib_clear_table (void)
349 l2fib_main_t *mp = &l2fib_main;
351 /* Remove all entries */
352 BV (clib_bihash_free) (&mp->mac_table);
353 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
354 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
355 l2learn_main.global_learn_count = 0;
358 /** Clear all entries in L2FIB.
359 * @TODO: Later we may want a way to remove only the non-static entries
361 static clib_error_t *
362 clear_l2fib (vlib_main_t * vm,
363 unformat_input_t * input, vlib_cli_command_t * cmd)
365 l2fib_clear_table ();
370 * This command clears all the MAC Address entries from the L2 FIB table.
373 * Example of how to clear the L2 FIB Table:
374 * @cliexcmd{clear l2fib}
375 * Example to show the L2 FIB Table has been cleared:
376 * @cliexstart{show l2fib verbose}
381 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
382 .path = "clear l2fib",
383 .short_help = "clear l2fib",
384 .function = clear_l2fib,
388 static inline l2fib_seq_num_t
389 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
391 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
393 return (l2fib_seq_num_t) {
394 .swif = *l2fib_swif_seq_num (sw_if_index),
395 .bd = bd_config->seq_num,
401 * Add an entry to the l2fib.
402 * If the entry already exists then overwrite it
405 l2fib_add_entry (const u8 * mac, u32 bd_index,
406 u32 sw_if_index, l2fib_entry_result_flags_t flags)
408 l2fib_entry_key_t key;
409 l2fib_entry_result_t result;
410 __attribute__ ((unused)) u32 bucket_contents;
411 l2fib_main_t *fm = &l2fib_main;
412 l2learn_main_t *lm = &l2learn_main;
413 BVT (clib_bihash_kv) kv;
416 key.raw = l2fib_make_key (mac, bd_index);
419 /* check if entry already exist */
420 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
422 /* decrement counter if overwriting a learned mac */
423 result.raw = kv.value;
424 if ((!l2fib_entry_result_is_set_AGE_NOT (&result))
425 && (lm->global_learn_count))
426 lm->global_learn_count--;
430 result.raw = 0; /* clear all fields */
431 result.fields.sw_if_index = sw_if_index;
432 result.fields.flags = flags;
434 /* no aging for provisioned entry */
435 l2fib_entry_result_set_AGE_NOT (&result);
437 kv.value = result.raw;
439 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
443 * Add an entry to the L2FIB.
445 * l2fib add <mac> <bd> <intf> [static] [bvi]
446 * l2fib add <mac> <bd> filter
447 * Note that filter and bvi entries are always static
449 static clib_error_t *
450 l2fib_add (vlib_main_t * vm,
451 unformat_input_t * input, vlib_cli_command_t * cmd)
453 bd_main_t *bdm = &bd_main;
454 vnet_main_t *vnm = vnet_get_main ();
455 clib_error_t *error = 0;
459 u32 sw_if_index = ~0;
461 l2fib_entry_result_flags_t flags;
463 flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
465 if (!unformat (input, "%U", unformat_ethernet_address, mac))
467 error = clib_error_return (0, "expected mac address `%U'",
468 format_unformat_error, input);
472 if (!unformat (input, "%d", &bd_id))
474 error = clib_error_return (0, "expected bridge domain ID `%U'",
475 format_unformat_error, input);
479 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
482 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
487 if (unformat (input, "filter"))
489 l2fib_add_filter_entry (mac, bd_index);
493 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
495 error = clib_error_return (0, "unknown interface `%U'",
496 format_unformat_error, input);
500 if (unformat (input, "static"))
501 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
502 else if (unformat (input, "bvi"))
503 flags |= (L2FIB_ENTRY_RESULT_FLAG_STATIC | L2FIB_ENTRY_RESULT_FLAG_BVI);
505 if (vec_len (l2input_main.configs) <= sw_if_index)
507 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
512 l2fib_add_entry (mac, bd_index, sw_if_index, flags);
519 * This command adds a MAC Address entry to the L2 FIB table
520 * of an existing bridge-domain. The MAC Address can be static
521 * or dynamic. This command also allows a filter to be added,
522 * such that packets with given MAC Addresses (source mac or
523 * destination mac match) are dropped.
526 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
527 * of a bridge-domain (where 200 is the bridge-domain-id):
528 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
529 * Example of how to add a static MAC Address entry to the L2 FIB table
530 * of a bridge-domain (where 200 is the bridge-domain-id):
531 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
532 * Example of how to add a filter such that a packet with the given MAC
533 * Address will be dropped in a given bridge-domain (where 200 is the
535 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
536 * Example of show command of the provisioned MAC Addresses and filters:
537 * @cliexstart{show l2fib verbose}
538 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
539 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
540 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
541 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
546 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
548 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
549 .function = l2fib_add,
554 static clib_error_t *
555 l2fib_test_command_fn (vlib_main_t * vm,
556 unformat_input_t * input, vlib_cli_command_t * cmd)
558 clib_error_t *error = 0;
559 u8 mac[6], save_mac[6];
569 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
571 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
573 else if (unformat (input, "add"))
575 else if (unformat (input, "del"))
577 else if (unformat (input, "check"))
579 else if (unformat (input, "count %d", &count))
586 return clib_error_return (0, "mac not set");
588 if (is_add == 0 && is_del == 0 && is_check == 0)
589 return clib_error_return (0,
590 "noop: pick at least one of (add,del,check)");
592 clib_memcpy_fast (save_mac, mac, 6);
596 for (i = 0; i < count; i++)
598 l2fib_add_entry (mac, bd_index, sw_if_index,
599 L2FIB_ENTRY_RESULT_FLAG_NONE);
600 incr_mac_address (mac);
606 BVT (clib_bihash_kv) kv;
607 l2fib_main_t *mp = &l2fib_main;
609 clib_memcpy_fast (mac, save_mac, 6);
611 for (i = 0; i < count; i++)
613 kv.key = l2fib_make_key (mac, bd_index);
614 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
616 clib_warning ("key %U AWOL", format_ethernet_address, mac);
619 incr_mac_address (mac);
625 clib_memcpy_fast (mac, save_mac, 6);
627 for (i = 0; i < count; i++)
629 l2fib_del_entry (mac, bd_index, 0);
630 incr_mac_address (mac);
638 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
639 * bridge domain (bridge-domain-id of 0) to be modified.
643 * Example of how to add a set of 4 sequential MAC Address entries to L2
644 * FIB table of the default bridge-domain:
645 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
647 * Show the set of 4 sequential MAC Address entries that were added:
648 * @cliexstart{show l2fib verbose}
649 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
650 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
651 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
652 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
653 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
657 * Example of how to check that the set of 4 sequential MAC Address
658 * entries were added to L2 FIB table of the default
659 * bridge-domain. Used a count of 5 to produce an error:
661 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
662 * The output of the check command is in the log files. Log file
663 * location may vary based on your OS and Version:
665 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
667 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
669 * Example of how to delete a set of 4 sequential MAC Address entries
670 * from L2 FIB table of the default bridge-domain:
671 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
675 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
676 .path = "test l2fib",
677 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
678 .function = l2fib_test_command_fn,
684 * Delete an entry from the l2fib.
685 * Return 0 if the entry was deleted, or 1 it was not found or if
686 * sw_if_index is non-zero and does not match that in the entry.
689 l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index)
691 l2fib_entry_result_t result;
692 l2fib_main_t *mp = &l2fib_main;
693 BVT (clib_bihash_kv) kv;
696 kv.key = l2fib_make_key (mac, bd_index);
698 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
701 result.raw = kv.value;
703 /* check if sw_if_index of entry match */
704 if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index))
707 /* decrement counter if dynamically learned mac */
708 if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) &&
709 (l2learn_main.global_learn_count))
710 l2learn_main.global_learn_count--;
712 /* Remove entry from hash table */
713 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
718 * Delete an entry from the L2FIB.
720 * l2fib del <mac> <bd-id>
722 static clib_error_t *
723 l2fib_del (vlib_main_t * vm,
724 unformat_input_t * input, vlib_cli_command_t * cmd)
726 bd_main_t *bdm = &bd_main;
727 clib_error_t *error = 0;
733 if (!unformat (input, "%U", unformat_ethernet_address, mac))
735 error = clib_error_return (0, "expected mac address `%U'",
736 format_unformat_error, input);
740 if (!unformat (input, "%d", &bd_id))
742 error = clib_error_return (0, "expected bridge domain ID `%U'",
743 format_unformat_error, input);
747 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
750 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
755 /* Delete the entry */
756 if (l2fib_del_entry (mac, bd_index, 0))
758 error = clib_error_return (0, "mac entry not found");
767 * This command deletes an existing MAC Address entry from the L2 FIB
768 * table of an existing bridge-domain.
771 * 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):
772 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
775 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
777 .short_help = "l2fib del <mac> <bridge-domain-id> []",
778 .function = l2fib_del,
783 Kick off ager to scan MACs to age/delete MAC entries
786 l2fib_start_ager_scan (vlib_main_t * vm)
788 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
790 /* check if there is at least one bd with mac aging enabled */
791 l2_bridge_domain_t *bd_config;
792 vec_foreach (bd_config, l2input_main.bd_configs)
794 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
796 evt = L2_MAC_AGE_PROCESS_EVENT_START;
801 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
806 Flush all non static MACs from an interface
809 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
811 *l2fib_swif_seq_num (sw_if_index) += 1;
812 l2fib_start_ager_scan (vm);
816 Flush all non static MACs in a bridge domain
819 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
821 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
822 bd_config->seq_num += 1;
823 l2fib_start_ager_scan (vm);
827 Flush all non static MACs - flushes all valid BDs
830 l2fib_flush_all_mac (vlib_main_t * vm)
832 l2_bridge_domain_t *bd_config;
833 vec_foreach (bd_config, l2input_main.bd_configs)
834 if (bd_is_valid (bd_config))
835 bd_config->seq_num += 1;
837 l2fib_start_ager_scan (vm);
842 Flush MACs, except static ones, associated with an interface
844 l2fib flush-mac interface <if-name>
846 static clib_error_t *
847 l2fib_flush_mac_int (vlib_main_t * vm,
848 unformat_input_t * input, vlib_cli_command_t * cmd)
850 vnet_main_t *vnm = vnet_get_main ();
851 clib_error_t *error = 0;
854 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
856 error = clib_error_return (0, "unknown interface `%U'",
857 format_unformat_error, input);
861 l2fib_flush_int_mac (vm, sw_if_index);
868 Flush all MACs, except static ones
872 static clib_error_t *
873 l2fib_flush_mac_all (vlib_main_t * vm,
874 unformat_input_t * input, vlib_cli_command_t * cmd)
876 l2fib_flush_all_mac (vm);
881 * This command kick off ager to delete all existing MAC Address entries,
882 * except static ones, associated with an interface from the L2 FIB table.
885 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
886 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
889 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
890 .path = "l2fib flush-mac all",
891 .short_help = "l2fib flush-mac all",
892 .function = l2fib_flush_mac_all,
897 * This command kick off ager to delete all existing MAC Address entries,
898 * except static ones, associated with an interface from the L2 FIB table.
901 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
902 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
905 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
906 .path = "l2fib flush-mac interface",
907 .short_help = "l2fib flush-mac interface <if-name>",
908 .function = l2fib_flush_mac_int,
913 Flush bridge-domain MACs except static ones.
915 l2fib flush-mac bridge-domain <bd-id>
917 static clib_error_t *
918 l2fib_flush_mac_bd (vlib_main_t * vm,
919 unformat_input_t * input, vlib_cli_command_t * cmd)
921 bd_main_t *bdm = &bd_main;
922 clib_error_t *error = 0;
926 if (!unformat (input, "%d", &bd_id))
928 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
929 format_unformat_error, input);
933 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
937 return clib_error_return (0, "No such bridge domain %d", bd_id);
939 l2fib_flush_bd_mac (vm, bd_index);
946 * This command kick off ager to delete all existing MAC Address entries,
947 * except static ones, in a bridge domain from the L2 FIB table.
950 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
951 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
954 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
955 .path = "l2fib flush-mac bridge-domain",
956 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
957 .function = l2fib_flush_mac_bd,
962 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
964 l2_input_config_t *config = l2input_intf_config (sw_if_index);
965 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
966 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
970 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
972 BVT (clib_bihash) * get_mac_table (void)
974 l2fib_main_t *mp = &l2fib_main;
975 return &mp->mac_table;
978 static_always_inline void *
979 allocate_mac_evt_buf (u32 client, u32 client_index)
981 l2fib_main_t *fm = &l2fib_main;
982 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
983 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
984 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
985 mp->pid = htonl (client);
986 mp->client_index = client_index;
990 static_always_inline f64
991 l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
993 l2fib_main_t *fm = &l2fib_main;
994 l2learn_main_t *lm = &l2learn_main;
996 BVT (clib_bihash) * h = &fm->mac_table;
998 f64 last_start = start_time;
1002 u32 learn_count = 0;
1003 u32 client = lm->client_pid;
1004 u32 cl_idx = lm->client_index;
1005 vl_api_l2_macs_event_t *mp = 0;
1006 vl_api_registration_t *reg = 0;
1008 /* Don't scan the l2 fib if it hasn't been instantiated yet */
1009 if (alloc_arena (h) == 0)
1014 mp = allocate_mac_evt_buf (client, cl_idx);
1015 reg = vl_api_client_index_to_registration (lm->client_index);
1018 for (i = 0; i < h->nbuckets; i++)
1020 /* allow no more than 20us without a pause */
1021 delta_t = vlib_time_now (vm) - last_start;
1022 if (delta_t > 20e-6)
1024 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
1025 last_start = vlib_time_now (vm);
1029 if (i < (h->nbuckets - 3))
1031 BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
1032 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
1033 b = &h->buckets[i + 1];
1036 BVT (clib_bihash_value) * v =
1037 BV (clib_bihash_get_value) (h, b->offset);
1038 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1042 BVT (clib_bihash_bucket) * b = &h->buckets[i];
1045 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1046 for (j = 0; j < (1 << b->log2_pages); j++)
1048 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1050 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1053 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1054 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1056 if (!l2fib_entry_result_is_set_AGE_NOT (&result))
1061 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1063 /* event message full, send it and start a new one */
1064 if (reg && vl_api_can_send_msg (reg))
1066 mp->n_macs = htonl (evt_idx);
1067 vl_api_send_msg (reg, (u8 *) mp);
1068 mp = allocate_mac_evt_buf (client, cl_idx);
1073 clib_warning ("MAC event to pid %d queue stuffed!"
1074 " %d MAC entries lost", client,
1080 if (l2fib_entry_result_is_set_LRN_EVT (&result))
1082 /* copy mac entry to event msg */
1083 clib_memcpy_fast (mp->mac[evt_idx].mac_addr,
1085 mp->mac[evt_idx].action =
1086 l2fib_entry_result_is_set_LRN_MOV (&result) ?
1087 (vl_api_mac_event_action_t) MAC_EVENT_ACTION_MOVE
1088 : (vl_api_mac_event_action_t) MAC_EVENT_ACTION_ADD;
1089 mp->mac[evt_idx].action =
1090 htonl (mp->mac[evt_idx].action);
1091 mp->mac[evt_idx].sw_if_index =
1092 htonl (result.fields.sw_if_index);
1093 /* clear event bits and update mac entry */
1094 l2fib_entry_result_clear_LRN_EVT (&result);
1095 l2fib_entry_result_clear_LRN_MOV (&result);
1096 BVT (clib_bihash_kv) kv;
1098 kv.value = result.raw;
1099 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1101 continue; /* skip aging */
1105 if (event_only || l2fib_entry_result_is_set_AGE_NOT (&result))
1106 continue; /* skip aging - static_mac always age_not */
1108 /* start aging processing */
1109 u32 bd_index = key.fields.bd_index;
1110 u32 sw_if_index = result.fields.sw_if_index;
1111 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1112 if (result.fields.sn.as_u16 != sn)
1113 goto age_out; /* stale mac */
1115 l2_bridge_domain_t *bd_config =
1116 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1118 if (bd_config->mac_age == 0)
1119 continue; /* skip aging */
1121 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1122 delta += delta < 0 ? 256 : 0;
1124 if (delta < bd_config->mac_age)
1125 continue; /* still valid */
1130 /* copy mac entry to event msg */
1131 clib_memcpy_fast (mp->mac[evt_idx].mac_addr, key.fields.mac,
1133 mp->mac[evt_idx].action =
1134 (vl_api_mac_event_action_t) MAC_EVENT_ACTION_DELETE;
1135 mp->mac[evt_idx].action = htonl (mp->mac[evt_idx].action);
1136 mp->mac[evt_idx].sw_if_index =
1137 htonl (result.fields.sw_if_index);
1140 /* delete mac entry */
1141 BVT (clib_bihash_kv) kv;
1143 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1146 * Note: we may have just freed the bucket's backing
1147 * storage, so check right here...
1158 /* keep learn count consistent */
1159 l2learn_main.global_learn_count = learn_count;
1163 /* send any outstanding mac event message else free message buffer */
1166 if (reg && vl_api_can_send_msg (reg))
1168 mp->n_macs = htonl (evt_idx);
1169 vl_api_send_msg (reg, (u8 *) mp);
1174 clib_warning ("MAC event to pid %d queue stuffed!"
1175 " %d MAC entries lost", client, evt_idx);
1176 vl_msg_api_free (mp);
1180 vl_msg_api_free (mp);
1182 return delta_t + accum_t;
1186 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1189 uword event_type, *event_data = 0;
1190 l2fib_main_t *fm = &l2fib_main;
1191 l2learn_main_t *lm = &l2learn_main;
1193 f64 start_time, next_age_scan_time = CLIB_TIME_MAX;
1198 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1201 f64 t = next_age_scan_time - vlib_time_now (vm);
1202 vlib_process_wait_for_event_or_clock (vm, t);
1205 vlib_process_wait_for_event (vm);
1207 event_type = vlib_process_get_events (vm, &event_data);
1208 vec_reset_length (event_data);
1210 start_time = vlib_time_now (vm);
1212 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
1216 case ~0: /* timer expired */
1217 if (lm->client_pid != 0 && start_time < next_age_scan_time)
1218 scan = SCAN_MAC_EVENT;
1221 case L2_MAC_AGE_PROCESS_EVENT_START:
1225 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1227 scan = SCAN_DISABLE;
1230 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
1237 if (scan == SCAN_MAC_EVENT)
1238 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1241 if (scan == SCAN_MAC_AGE)
1242 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1243 if (scan == SCAN_DISABLE)
1245 l2fib_main.age_scan_duration = 0;
1246 l2fib_main.evt_scan_duration = 0;
1248 /* schedule next scan */
1250 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1252 next_age_scan_time = CLIB_TIME_MAX;
1259 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1260 .function = l2fib_mac_age_scanner_process,
1261 .type = VLIB_NODE_TYPE_PROCESS,
1262 .name = "l2fib-mac-age-scanner-process",
1267 l2fib_init (vlib_main_t * vm)
1269 l2fib_main_t *mp = &l2fib_main;
1270 l2fib_entry_key_t test_key;
1274 mp->vnet_main = vnet_get_main ();
1276 /* Create the hash table */
1277 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1278 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
1280 /* verify the key constructor is good, since it is endian-sensitive */
1281 clib_memset (test_mac, 0, sizeof (test_mac));
1284 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1285 ASSERT (test_key.fields.mac[0] == 0x11);
1286 ASSERT (test_key.fields.bd_index == 0x1234);
1291 VLIB_INIT_FUNCTION (l2fib_init);
1294 * fd.io coding-style-patch-verification: ON
1297 * eval: (c-set-style "gnu")