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>
37 BVT (clib_bihash) mac_table;
39 /* convenience variables */
40 vlib_main_t *vlib_main;
41 vnet_main_t *vnet_main;
44 l2fib_main_t l2fib_main;
47 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
49 format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
51 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
52 u32 sw_if_index = va_arg (*args, u32);
53 if (sw_if_index == ~0)
54 return format (s, "N/A");
56 return format (s, "%U",
57 format_vnet_sw_interface_name, vnm,
58 vnet_get_sw_interface (vnm, sw_if_index));
62 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
63 l2fib_entry_result_t ** l2fe_res)
65 l2fib_main_t *msm = &l2fib_main;
66 BVT (clib_bihash) * h = &msm->mac_table;
67 clib_bihash_bucket_t *b;
68 BVT (clib_bihash_value) * v;
69 l2fib_entry_key_t key;
70 l2fib_entry_result_t result;
73 for (i = 0; i < h->nbuckets; i++)
78 v = BV (clib_bihash_get_value) (h, b->offset);
79 for (j = 0; j < (1 << b->log2_pages); j++)
81 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
83 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
86 key.raw = v->kvp[k].key;
87 result.raw = v->kvp[k].value;
89 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
91 vec_add1 (*l2fe_key, key);
92 vec_add1 (*l2fe_res, result);
100 /** Display the contents of the l2fib */
101 static clib_error_t *
102 show_l2fib (vlib_main_t * vm,
103 unformat_input_t * input, vlib_cli_command_t * cmd)
105 bd_main_t *bdm = &bd_main;
106 l2fib_main_t *msm = &l2fib_main;
107 BVT (clib_bihash) * h = &msm->mac_table;
108 clib_bihash_bucket_t *b;
109 BVT (clib_bihash_value) * v;
110 l2fib_entry_key_t key;
111 l2fib_entry_result_t result;
113 u64 total_entries = 0;
117 u32 bd_id, bd_index = ~0;
119 if (unformat (input, "raw"))
121 else if (unformat (input, "verbose"))
123 else if (unformat (input, "bd_index %d", &bd_index))
125 else if (unformat (input, "bd_id %d", &bd_id))
127 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
135 vlib_cli_output (vm, "no such bridge domain id");
140 for (i = 0; i < h->nbuckets; i++)
145 v = BV (clib_bihash_get_value) (h, b->offset);
146 for (j = 0; j < (1 << b->log2_pages); j++)
148 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
150 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
153 if (verbose && first_entry)
157 "%=19s%=7s%=30s%=7s%=8s%=8s%=5s%=9s%=11s",
158 "Mac Address", "BD Idx", "Interface",
159 "Index", "static", "filter", "bvi",
160 "refresh", "timestamp");
163 key.raw = v->kvp[k].key;
164 result.raw = v->kvp[k].value;
167 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
170 "%=19U%=7d%=30U%=7d%=8d%=8d%=5d%=9d%=11X",
171 format_ethernet_address, key.fields.mac,
173 format_vnet_sw_if_index_name_with_NA,
174 msm->vnet_main, result.fields.sw_if_index,
175 result.fields.sw_if_index == ~0
176 ? -1 : result.fields.sw_if_index,
177 result.fields.static_mac,
178 result.fields.filter,
180 result.fields.refresh,
181 result.fields.timestamp);
189 if (total_entries == 0)
190 vlib_cli_output (vm, "no l2fib entries");
192 vlib_cli_output (vm, "%lld l2fib entries", total_entries);
195 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
196 BV (format_bihash), h, 1 /* verbose */ );
202 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
203 .path = "show l2fib",
204 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
205 .function = show_l2fib,
210 /* Remove all entries from the l2fib */
212 l2fib_clear_table (uint keep_static)
214 l2fib_main_t *mp = &l2fib_main;
218 /* TODO: remove only non-static entries */
222 /* Remove all entries */
223 BV (clib_bihash_free) (&mp->mac_table);
224 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
225 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
228 l2learn_main.global_learn_count = 0;
231 /** Clear all entries in L2FIB
232 * TODO: Later we may want a way to remove only the non-static entries
234 static clib_error_t *
235 clear_l2fib (vlib_main_t * vm,
236 unformat_input_t * input, vlib_cli_command_t * cmd)
238 l2fib_clear_table (0);
243 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
244 .path = "clear l2fib",
245 .short_help = "Clear l2fib mac forwarding entries",
246 .function = clear_l2fib,
252 * Add an entry to the l2fib.
253 * If the entry already exists then overwrite it
256 l2fib_add_entry (u64 mac,
258 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
260 l2fib_entry_key_t key;
261 l2fib_entry_result_t result;
262 __attribute__ ((unused)) u32 bucket_contents;
263 l2fib_main_t *mp = &l2fib_main;
264 BVT (clib_bihash_kv) kv;
267 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
270 result.raw = 0; /* clear all fields */
271 result.fields.sw_if_index = sw_if_index;
272 result.fields.static_mac = static_mac;
273 result.fields.filter = filter_mac;
274 result.fields.bvi = bvi_mac;
277 kv.value = result.raw;
279 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
281 /* increment counter if dynamically learned mac */
282 if (result.fields.static_mac)
284 l2learn_main.global_learn_count++;
289 * Add an entry to the L2FIB
291 * l2fib add <mac> <bd> <intf> [static] [bvi]
292 * l2fib add <mac> <bd> filter
293 * Note that filter and bvi entries are always static
295 static clib_error_t *
296 l2fib_add (vlib_main_t * vm,
297 unformat_input_t * input, vlib_cli_command_t * cmd)
299 bd_main_t *bdm = &bd_main;
300 vnet_main_t *vnm = vnet_get_main ();
301 clib_error_t *error = 0;
305 u32 sw_if_index = ~0;
311 if (!unformat_user (input, unformat_ethernet_address, &mac))
313 error = clib_error_return (0, "expected mac address `%U'",
314 format_unformat_error, input);
318 if (!unformat (input, "%d", &bd_id))
320 error = clib_error_return (0, "expected bridge domain ID `%U'",
321 format_unformat_error, input);
325 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
328 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
333 if (unformat (input, "filter"))
343 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
345 error = clib_error_return (0, "unknown interface `%U'",
346 format_unformat_error, input);
349 if (unformat (input, "static"))
353 else if (unformat (input, "bvi"))
360 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
368 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
370 .short_help = "Add l2fib mac forwarding entry <mac> <bd-id> filter | <intf> [static | bvi]",
371 .function = l2fib_add,
376 static clib_error_t *
377 l2fib_test_command_fn (vlib_main_t * vm,
378 unformat_input_t * input, vlib_cli_command_t * cmd)
380 clib_error_t *error = 0;
393 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
395 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
397 else if (unformat (input, "add"))
399 else if (unformat (input, "del"))
401 else if (unformat (input, "check"))
403 else if (unformat (input, "count %d", &count))
410 return clib_error_return (0, "mac not set");
412 if (is_add == 0 && is_del == 0 && is_check == 0)
413 return clib_error_return (0,
414 "noop: pick at least one of (add,del,check)");
420 for (i = 0; i < count; i++)
423 l2fib_add_entry (mac, bd_index, sw_if_index, mac,
424 filter_mac, bvi_mac);
425 tmp = clib_net_to_host_u64 (mac);
429 mac = clib_host_to_net_u64 (tmp);
435 BVT (clib_bihash_kv) kv;
436 l2fib_main_t *mp = &l2fib_main;
440 for (i = 0; i < count; i++)
443 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
444 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
446 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
449 tmp = clib_net_to_host_u64 (mac);
453 mac = clib_host_to_net_u64 (tmp);
459 for (i = 0; i < count; i++)
463 l2fib_del_entry (mac, bd_index);
465 tmp = clib_net_to_host_u64 (mac);
469 mac = clib_host_to_net_u64 (tmp);
477 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
478 .path = "test l2fib",
479 .short_help = "test l2fib [del] mac <base-addr> count <nn>",
480 .function = l2fib_test_command_fn,
486 * Delete an entry from the l2fib.
487 * Return 0 if the entry was deleted, or 1 if it was not found
490 l2fib_del_entry (u64 mac, u32 bd_index)
493 l2fib_entry_result_t result;
494 l2fib_main_t *mp = &l2fib_main;
495 BVT (clib_bihash_kv) kv;
498 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
500 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
503 result.raw = kv.value;
505 /* decrement counter if dynamically learned mac */
506 if (result.fields.static_mac)
508 if (l2learn_main.global_learn_count > 0)
510 l2learn_main.global_learn_count--;
514 /* Remove entry from hash table */
515 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
520 * Delete an entry from the L2FIB
522 * l2fib del <mac> <bd-id>
524 static clib_error_t *
525 l2fib_del (vlib_main_t * vm,
526 unformat_input_t * input, vlib_cli_command_t * cmd)
528 bd_main_t *bdm = &bd_main;
529 clib_error_t *error = 0;
535 if (!unformat_user (input, unformat_ethernet_address, &mac))
537 error = clib_error_return (0, "expected mac address `%U'",
538 format_unformat_error, input);
542 if (!unformat (input, "%d", &bd_id))
544 error = clib_error_return (0, "expected bridge domain ID `%U'",
545 format_unformat_error, input);
549 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
552 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
557 /* Delete the entry */
558 if (l2fib_del_entry (mac, bd_index))
560 error = clib_error_return (0, "mac entry not found");
569 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
571 .short_help = "Delete l2fib mac forwarding entry <mac> <bd-id>",
572 .function = l2fib_del,
577 BVT (clib_bihash) * get_mac_table (void)
579 l2fib_main_t *mp = &l2fib_main;
580 return &mp->mac_table;
584 l2fib_init (vlib_main_t * vm)
586 l2fib_main_t *mp = &l2fib_main;
587 l2fib_entry_key_t test_key;
591 mp->vnet_main = vnet_get_main ();
593 /* Create the hash table */
594 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
595 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
597 /* verify the key constructor is good, since it is endian-sensitive */
598 memset (test_mac, 0, sizeof (test_mac));
601 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
602 ASSERT (test_key.fields.mac[0] == 0x11);
603 ASSERT (test_key.fields.bd_index == 0x1234);
608 VLIB_INIT_FUNCTION (l2fib_init);
611 * fd.io coding-style-patch-verification: ON
614 * eval: (c-set-style "gnu")