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_fib.h>
28 #include <vnet/l2/l2_learn.h>
29 #include <vnet/l2/l2_bd.h>
31 #include <vppinfra/bihash_template.c>
35 * @brief Ethernet MAC Address FIB Table Management.
37 * The MAC Address forwarding table for bridge-domains is called the l2fib.
38 * Entries are added automatically as part of mac learning, but MAC Addresses
39 * entries can also be added manually.
47 BVT (clib_bihash) mac_table;
49 /* convenience variables */
50 vlib_main_t *vlib_main;
51 vnet_main_t *vnet_main;
54 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 return format (s, "%U",
67 format_vnet_sw_interface_name, vnm,
68 vnet_get_sw_interface (vnm, sw_if_index));
72 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
73 l2fib_entry_result_t ** l2fe_res)
75 l2fib_main_t *msm = &l2fib_main;
76 BVT (clib_bihash) * h = &msm->mac_table;
77 clib_bihash_bucket_t *b;
78 BVT (clib_bihash_value) * v;
79 l2fib_entry_key_t key;
80 l2fib_entry_result_t result;
83 for (i = 0; i < h->nbuckets; i++)
88 v = BV (clib_bihash_get_value) (h, b->offset);
89 for (j = 0; j < (1 << b->log2_pages); j++)
91 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
93 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
96 key.raw = v->kvp[k].key;
97 result.raw = v->kvp[k].value;
99 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
101 vec_add1 (*l2fe_key, key);
102 vec_add1 (*l2fe_res, result);
110 /** Display the contents of the l2fib. */
111 static clib_error_t *
112 show_l2fib (vlib_main_t * vm,
113 unformat_input_t * input, vlib_cli_command_t * cmd)
115 bd_main_t *bdm = &bd_main;
116 l2fib_main_t *msm = &l2fib_main;
117 BVT (clib_bihash) * h = &msm->mac_table;
118 clib_bihash_bucket_t *b;
119 BVT (clib_bihash_value) * v;
120 l2fib_entry_key_t key;
121 l2fib_entry_result_t result;
123 u64 total_entries = 0;
127 u32 bd_id, bd_index = ~0;
129 if (unformat (input, "raw"))
131 else if (unformat (input, "verbose"))
133 else if (unformat (input, "bd_index %d", &bd_index))
135 else if (unformat (input, "bd_id %d", &bd_id))
137 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
145 vlib_cli_output (vm, "no such bridge domain id");
150 for (i = 0; i < h->nbuckets; i++)
155 v = BV (clib_bihash_get_value) (h, b->offset);
156 for (j = 0; j < (1 << b->log2_pages); j++)
158 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
160 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
163 if (verbose && first_entry)
167 "%=19s%=7s%=30s%=7s%=8s%=8s%=5s%=9s%=11s",
168 "Mac Address", "BD Idx", "Interface",
169 "Index", "static", "filter", "bvi",
170 "refresh", "timestamp");
173 key.raw = v->kvp[k].key;
174 result.raw = v->kvp[k].value;
177 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
180 "%=19U%=7d%=30U%=7d%=8d%=8d%=5d%=9d%=11X",
181 format_ethernet_address, key.fields.mac,
183 format_vnet_sw_if_index_name_with_NA,
184 msm->vnet_main, result.fields.sw_if_index,
185 result.fields.sw_if_index == ~0
186 ? -1 : result.fields.sw_if_index,
187 result.fields.static_mac,
188 result.fields.filter,
190 result.fields.refresh,
191 result.fields.timestamp);
199 if (total_entries == 0)
200 vlib_cli_output (vm, "no l2fib entries");
202 vlib_cli_output (vm, "%lld l2fib entries", total_entries);
205 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
206 BV (format_bihash), h, 1 /* verbose */ );
212 * This command dispays the MAC Address entries of the L2 FIB table.
213 * Output can be filtered to just get the number of MAC Addresses or display
214 * each MAC Address for all bridge domains or just a single bridge domain.
217 * Example of how to display the number of MAC Address entries in the L2
219 * @cliexstart{show l2fib}
222 * Example of how to display all the MAC Address entries in the L2
224 * @cliexstart{show l2fib verbose}
225 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
226 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
227 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
228 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
233 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
234 .path = "show l2fib",
235 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
236 .function = show_l2fib,
241 /* Remove all entries from the l2fib */
243 l2fib_clear_table (uint keep_static)
245 l2fib_main_t *mp = &l2fib_main;
249 /* TODO: remove only non-static entries */
253 /* Remove all entries */
254 BV (clib_bihash_free) (&mp->mac_table);
255 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
256 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
259 l2learn_main.global_learn_count = 0;
262 /** Clear all entries in L2FIB.
263 * @TODO: Later we may want a way to remove only the non-static entries
265 static clib_error_t *
266 clear_l2fib (vlib_main_t * vm,
267 unformat_input_t * input, vlib_cli_command_t * cmd)
269 l2fib_clear_table (0);
274 * This command clears all the MAC Address entries from the L2 FIB table.
277 * Example of how to clear the L2 FIB Table:
278 * @cliexcmd{clear l2fib}
279 * Example to show the L2 FIB Table has been cleared:
280 * @cliexstart{show l2fib verbose}
285 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
286 .path = "clear l2fib",
287 .short_help = "clear l2fib",
288 .function = clear_l2fib,
294 * Add an entry to the l2fib.
295 * If the entry already exists then overwrite it
298 l2fib_add_entry (u64 mac,
300 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
302 l2fib_entry_key_t key;
303 l2fib_entry_result_t result;
304 __attribute__ ((unused)) u32 bucket_contents;
305 l2fib_main_t *mp = &l2fib_main;
306 BVT (clib_bihash_kv) kv;
309 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
312 result.raw = 0; /* clear all fields */
313 result.fields.sw_if_index = sw_if_index;
314 result.fields.static_mac = static_mac;
315 result.fields.filter = filter_mac;
316 result.fields.bvi = bvi_mac;
319 kv.value = result.raw;
321 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
323 /* increment counter if dynamically learned mac */
324 if (result.fields.static_mac)
326 l2learn_main.global_learn_count++;
331 * Add an entry to the L2FIB.
333 * l2fib add <mac> <bd> <intf> [static] [bvi]
334 * l2fib add <mac> <bd> filter
335 * Note that filter and bvi entries are always static
337 static clib_error_t *
338 l2fib_add (vlib_main_t * vm,
339 unformat_input_t * input, vlib_cli_command_t * cmd)
341 bd_main_t *bdm = &bd_main;
342 vnet_main_t *vnm = vnet_get_main ();
343 clib_error_t *error = 0;
347 u32 sw_if_index = ~0;
353 if (!unformat_user (input, unformat_ethernet_address, &mac))
355 error = clib_error_return (0, "expected mac address `%U'",
356 format_unformat_error, input);
360 if (!unformat (input, "%d", &bd_id))
362 error = clib_error_return (0, "expected bridge domain ID `%U'",
363 format_unformat_error, input);
367 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
370 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
375 if (unformat (input, "filter"))
385 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
387 error = clib_error_return (0, "unknown interface `%U'",
388 format_unformat_error, input);
391 if (unformat (input, "static"))
395 else if (unformat (input, "bvi"))
402 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
410 * This command adds a MAC Address entry to the L2 FIB table
411 * of an existing bridge-domain. The MAC Address can be static
412 * or dynamic. This command also allows a filter to be added,
413 * such that packets with given MAC Addresses (source mac or
414 * destination mac match) are dropped.
417 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
418 * of a bridge-domain (where 200 is the bridge-domain-id):
419 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
420 * Example of how to add a static MAC Address entry to the L2 FIB table
421 * of a bridge-domain (where 200 is the bridge-domain-id):
422 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
423 * Example of how to add a filter such that a packet with the given MAC
424 * Address will be dropped in a given bridge-domain (where 200 is the
426 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
427 * Example of show command of the provisioned MAC Addresses and filters:
428 * @cliexstart{show l2fib verbose}
429 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
430 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
431 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
432 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
437 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
439 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
440 .function = l2fib_add,
445 static clib_error_t *
446 l2fib_test_command_fn (vlib_main_t * vm,
447 unformat_input_t * input, vlib_cli_command_t * cmd)
449 clib_error_t *error = 0;
462 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
464 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
466 else if (unformat (input, "add"))
468 else if (unformat (input, "del"))
470 else if (unformat (input, "check"))
472 else if (unformat (input, "count %d", &count))
479 return clib_error_return (0, "mac not set");
481 if (is_add == 0 && is_del == 0 && is_check == 0)
482 return clib_error_return (0,
483 "noop: pick at least one of (add,del,check)");
489 for (i = 0; i < count; i++)
492 l2fib_add_entry (mac, bd_index, sw_if_index, mac,
493 filter_mac, bvi_mac);
494 tmp = clib_net_to_host_u64 (mac);
498 mac = clib_host_to_net_u64 (tmp);
504 BVT (clib_bihash_kv) kv;
505 l2fib_main_t *mp = &l2fib_main;
509 for (i = 0; i < count; i++)
512 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
513 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
515 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
518 tmp = clib_net_to_host_u64 (mac);
522 mac = clib_host_to_net_u64 (tmp);
528 for (i = 0; i < count; i++)
532 l2fib_del_entry (mac, bd_index);
534 tmp = clib_net_to_host_u64 (mac);
538 mac = clib_host_to_net_u64 (tmp);
546 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
547 * bridge domain (bridge-domain-id of 0) to be modified.
551 * Example of how to add a set of 4 sequential MAC Address entries to L2
552 * FIB table of the default bridge-domain:
553 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
555 * Show the set of 4 sequential MAC Address entries that were added:
556 * @cliexstart{show l2fib verbose}
557 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
558 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
559 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
560 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
561 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
565 * Example of how to check that the set of 4 sequential MAC Address
566 * entries were added to L2 FIB table of the default
567 * bridge-domain. Used a count of 5 to produce an error:
569 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
570 * The output of the check command is in the log files. Log file
571 * location may vary based on your OS and Version:
573 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
575 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
577 * Example of how to delete a set of 4 sequential MAC Address entries
578 * from L2 FIB table of the default bridge-domain:
579 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
583 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
584 .path = "test l2fib",
585 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
586 .function = l2fib_test_command_fn,
592 * Delete an entry from the l2fib.
593 * Return 0 if the entry was deleted, or 1 if it was not found
596 l2fib_del_entry (u64 mac, u32 bd_index)
599 l2fib_entry_result_t result;
600 l2fib_main_t *mp = &l2fib_main;
601 BVT (clib_bihash_kv) kv;
604 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
606 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
609 result.raw = kv.value;
611 /* decrement counter if dynamically learned mac */
612 if (result.fields.static_mac)
614 if (l2learn_main.global_learn_count > 0)
616 l2learn_main.global_learn_count--;
620 /* Remove entry from hash table */
621 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
626 * Delete an entry from the L2FIB.
628 * l2fib del <mac> <bd-id>
630 static clib_error_t *
631 l2fib_del (vlib_main_t * vm,
632 unformat_input_t * input, vlib_cli_command_t * cmd)
634 bd_main_t *bdm = &bd_main;
635 clib_error_t *error = 0;
641 if (!unformat_user (input, unformat_ethernet_address, &mac))
643 error = clib_error_return (0, "expected mac address `%U'",
644 format_unformat_error, input);
648 if (!unformat (input, "%d", &bd_id))
650 error = clib_error_return (0, "expected bridge domain ID `%U'",
651 format_unformat_error, input);
655 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
658 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
663 /* Delete the entry */
664 if (l2fib_del_entry (mac, bd_index))
666 error = clib_error_return (0, "mac entry not found");
675 * This command deletes an existing MAC Address entry from the L2 FIB
676 * table of an existing bridge-domain.
679 * 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):
680 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
683 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
685 .short_help = "l2fib del <mac> <bridge-domain-id>",
686 .function = l2fib_del,
691 BVT (clib_bihash) * get_mac_table (void)
693 l2fib_main_t *mp = &l2fib_main;
694 return &mp->mac_table;
698 l2fib_init (vlib_main_t * vm)
700 l2fib_main_t *mp = &l2fib_main;
701 l2fib_entry_key_t test_key;
705 mp->vnet_main = vnet_get_main ();
707 /* Create the hash table */
708 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
709 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
711 /* verify the key constructor is good, since it is endian-sensitive */
712 memset (test_mac, 0, sizeof (test_mac));
715 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
716 ASSERT (test_key.fields.mac[0] == 0x11);
717 ASSERT (test_key.fields.bd_index == 0x1234);
722 VLIB_INIT_FUNCTION (l2fib_init);
725 * fd.io coding-style-patch-verification: ON
728 * eval: (c-set-style "gnu")