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>
36 BVT(clib_bihash) mac_table;
38 /* convenience variables */
39 vlib_main_t * vlib_main;
40 vnet_main_t * vnet_main;
43 l2fib_main_t l2fib_main;
46 // Format sw_if_index. If the value is ~0, use the text "N/A"
47 u8 * format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
49 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
50 u32 sw_if_index = va_arg (*args, u32);
51 if (sw_if_index == ~0)
52 return format (s, "N/A");
54 return format (s, "%U",
55 format_vnet_sw_interface_name, vnm,
56 vnet_get_sw_interface (vnm, sw_if_index));
59 void l2fib_table_dump (u32 bd_index, l2fib_entry_key_t **l2fe_key,
60 l2fib_entry_result_t **l2fe_res)
62 l2fib_main_t * msm = &l2fib_main;
63 BVT(clib_bihash) * h = &msm->mac_table;
64 clib_bihash_bucket_t * b;
65 BVT(clib_bihash_value) * v;
66 l2fib_entry_key_t key;
67 l2fib_entry_result_t result;
70 for (i = 0; i < h->nbuckets; i++)
75 v = BV(clib_bihash_get_value) (h, b->offset);
76 for (j = 0; j < (1<<b->log2_pages); j++)
78 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
80 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
83 key.raw = v->kvp[k].key;
84 result.raw = v->kvp[k].value;
86 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
88 vec_add1 (*l2fe_key, key);
89 vec_add1 (*l2fe_res, result);
97 // Display the contents of the l2fib
99 show_l2fib (vlib_main_t * vm,
100 unformat_input_t * input,
101 vlib_cli_command_t * cmd)
103 bd_main_t * bdm = &bd_main;
104 l2fib_main_t * msm = &l2fib_main;
105 BVT(clib_bihash) * h = &msm->mac_table;
106 clib_bihash_bucket_t * b;
107 BVT(clib_bihash_value) * v;
108 l2fib_entry_key_t key;
109 l2fib_entry_result_t result;
111 u64 total_entries = 0;
115 u32 bd_id, bd_index = ~0;
117 if (unformat (input, "raw"))
119 else if (unformat (input, "verbose"))
121 else if (unformat (input, "bd_index %d", &bd_index))
123 else if (unformat (input, "bd_id %d", &bd_id))
125 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
133 vlib_cli_output (vm, "no such bridge domain id");
138 for (i = 0; i < h->nbuckets; i++)
143 v = BV(clib_bihash_get_value) (h, b->offset);
144 for (j = 0; j < (1<<b->log2_pages); j++)
146 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
148 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
151 if (verbose && first_entry)
155 "%=19s%=7s%=30s%=7s%=8s%=8s%=5s%=9s%=11s",
156 "Mac Address", "BD Idx", "Interface",
157 "Index", "static", "filter", "bvi",
158 "refresh", "timestamp");
161 key.raw = v->kvp[k].key;
162 result.raw = v->kvp[k].value;
165 & ((bd_index >>31) || (bd_index == key.fields.bd_index)))
168 "%=19U%=7d%=30U%=7d%=8d%=8d%=5d%=9d%=11X",
169 format_ethernet_address, key.fields.mac,
171 format_vnet_sw_if_index_name_with_NA,
172 msm->vnet_main, result.fields.sw_if_index,
173 result.fields.sw_if_index == ~0
174 ? -1 : result.fields.sw_if_index,
175 result.fields.static_mac,
176 result.fields.filter,
178 result.fields.refresh,
179 result.fields.timestamp);
187 if (total_entries == 0)
188 vlib_cli_output (vm, "no l2fib entries");
190 vlib_cli_output (vm, "%lld l2fib entries", total_entries);
193 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
194 BV(format_bihash), h, 1 /* verbose */);
199 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
200 .path = "show l2fib",
201 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
202 .function = show_l2fib,
206 // Remove all entries from the l2fib
207 void l2fib_clear_table (uint keep_static)
209 l2fib_main_t * mp = &l2fib_main;
212 // TODO: remove only non-static entries
214 // Remove all entries
215 BV(clib_bihash_free) (&mp->mac_table);
216 BV(clib_bihash_init) (&mp->mac_table, "l2fib mac table",
217 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
220 l2learn_main.global_learn_count = 0;
223 // Clear all entries in L2FIB
224 // TODO: Later we may want a way to remove only the non-static entries
225 static clib_error_t *
226 clear_l2fib (vlib_main_t * vm,
227 unformat_input_t * input,
228 vlib_cli_command_t * cmd)
230 l2fib_clear_table (0);
234 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
235 .path = "clear l2fib",
236 .short_help = "Clear l2fib mac forwarding entries",
237 .function = clear_l2fib,
241 // Add an entry to the l2fib.
242 // If the entry already exists then overwrite it
243 void l2fib_add_entry (u64 mac,
249 l2fib_entry_key_t key;
250 l2fib_entry_result_t result;
251 __attribute__((unused)) u32 bucket_contents;
252 l2fib_main_t * mp = &l2fib_main;
253 BVT(clib_bihash_kv) kv;
256 key.raw = l2fib_make_key ((u8 *)&mac, bd_index);
259 result.raw = 0; // clear all fields
260 result.fields.sw_if_index = sw_if_index;
261 result.fields.static_mac = static_mac;
262 result.fields.filter = filter_mac;
263 result.fields.bvi = bvi_mac;
266 kv.value = result.raw;
268 BV(clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */);
270 // increment counter if dynamically learned mac
271 if (result.fields.static_mac) {
272 l2learn_main.global_learn_count++;
276 // Add an entry to the L2FIB
277 // The CLI format is:
278 // l2fib add <mac> <bd> <intf> [static] [bvi]
279 // l2fib add <mac> <bd> filter
280 // Note that filter and bvi entries are always static
281 static clib_error_t *
282 l2fib_add (vlib_main_t * vm,
283 unformat_input_t * input,
284 vlib_cli_command_t * cmd)
286 bd_main_t * bdm = &bd_main;
287 vnet_main_t * vnm = vnet_get_main();
288 clib_error_t * error = 0;
292 u32 sw_if_index = ~0;
298 if (! unformat_user (input, unformat_ethernet_address, &mac))
300 error = clib_error_return (0, "expected mac address `%U'",
301 format_unformat_error, input);
305 if (!unformat (input, "%d", &bd_id)) {
306 error = clib_error_return (0, "expected bridge domain ID `%U'",
307 format_unformat_error, input);
311 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
313 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
318 if (unformat (input, "filter")) {
324 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) {
325 error = clib_error_return (0, "unknown interface `%U'",
326 format_unformat_error, input);
329 if (unformat (input, "static")) {
331 } else if (unformat (input, "bvi")) {
337 l2fib_add_entry(mac, bd_index, sw_if_index, static_mac, filter_mac, bvi_mac);
343 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
345 .short_help = "Add l2fib mac forwarding entry <mac> <bd-id> filter | <intf> [static | bvi]",
346 .function = l2fib_add,
350 static clib_error_t *
351 l2fib_test_command_fn (vlib_main_t * vm,
352 unformat_input_t * input,
353 vlib_cli_command_t * cmd)
355 clib_error_t * error = 0;
368 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
370 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
372 else if (unformat (input, "add"))
374 else if (unformat (input, "del"))
376 else if (unformat (input, "check"))
378 else if (unformat (input, "count %d", &count))
385 return clib_error_return (0, "mac not set");
387 if (is_add == 0 && is_del == 0 && is_check == 0)
388 return clib_error_return (0, "noop: pick at least one of (add,del,check)");
394 for (i = 0; i < count; i++)
397 l2fib_add_entry(mac, bd_index, sw_if_index, mac,
398 filter_mac, bvi_mac);
399 tmp = clib_net_to_host_u64(mac);
403 mac = clib_host_to_net_u64 (tmp);
409 BVT(clib_bihash_kv) kv;
410 l2fib_main_t * mp = &l2fib_main;
414 for (i = 0; i < count; i++)
417 kv.key = l2fib_make_key ((u8 *)&mac, bd_index);
418 if (BV(clib_bihash_search) (&mp->mac_table, &kv, &kv))
420 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
423 tmp = clib_net_to_host_u64(mac);
427 mac = clib_host_to_net_u64 (tmp);
433 for (i = 0; i < count; i++)
437 l2fib_del_entry (mac, bd_index);
439 tmp = clib_net_to_host_u64(mac);
443 mac = clib_host_to_net_u64 (tmp);
450 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
451 .path = "test l2fib",
452 .short_help = "test l2fib [del] mac <base-addr> count <nn>",
453 .function = l2fib_test_command_fn,
457 // Delete an entry from the l2fib.
458 // Return 0 if the entry was deleted, or 1 if it was not found
459 u32 l2fib_del_entry (u64 mac,
462 l2fib_entry_result_t result;
463 l2fib_main_t * mp = &l2fib_main;
464 BVT(clib_bihash_kv) kv;
467 kv.key = l2fib_make_key ((u8 *)&mac, bd_index);
469 if (BV(clib_bihash_search) (&mp->mac_table, &kv, &kv))
472 result.raw = kv.value;
474 // decrement counter if dynamically learned mac
475 if (result.fields.static_mac) {
476 if (l2learn_main.global_learn_count > 0) {
477 l2learn_main.global_learn_count--;
481 // Remove entry from hash table
482 BV(clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */);
486 // Delete an entry from the L2FIB
487 // The CLI format is:
488 // l2fib del <mac> <bd-id>
489 static clib_error_t *
490 l2fib_del (vlib_main_t * vm,
491 unformat_input_t * input,
492 vlib_cli_command_t * cmd)
494 bd_main_t * bdm = &bd_main;
495 clib_error_t * error = 0;
501 if (! unformat_user (input, unformat_ethernet_address, &mac))
503 error = clib_error_return (0, "expected mac address `%U'",
504 format_unformat_error, input);
508 if (!unformat (input, "%d", &bd_id)) {
509 error = clib_error_return (0, "expected bridge domain ID `%U'",
510 format_unformat_error, input);
514 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
516 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
522 if (l2fib_del_entry(mac, bd_index)) {
523 error = clib_error_return (0, "mac entry not found");
531 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
533 .short_help = "Delete l2fib mac forwarding entry <mac> <bd-id>",
534 .function = l2fib_del,
538 BVT(clib_bihash) *get_mac_table(void) {
539 l2fib_main_t * mp = &l2fib_main;
540 return &mp->mac_table;
543 clib_error_t *l2fib_init (vlib_main_t *vm)
545 l2fib_main_t * mp = &l2fib_main;
546 l2fib_entry_key_t test_key;
550 mp->vnet_main = vnet_get_main();
552 // Create the hash table
553 BV(clib_bihash_init) (&mp->mac_table, "l2fib mac table",
554 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
556 // verify the key constructor is good, since it is endian-sensitive
559 test_key.raw = l2fib_make_key ((u8 *)&test_mac, 0x1234);
560 ASSERT (test_key.fields.mac[0] == 0x11);
561 ASSERT (test_key.fields.bd_index == 0x1234);
566 VLIB_INIT_FUNCTION (l2fib_init);