blob: 0ce4eb84a6bc8373608722eb039bb55486696a15 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_fib.c : layer 2 forwarding table (aka mac table)
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18
19#include <vlib/vlib.h>
20#include <vnet/vnet.h>
21#include <vnet/pg/pg.h>
22#include <vnet/ethernet/ethernet.h>
23#include <vlib/cli.h>
24
25#include <vppinfra/error.h>
26#include <vppinfra/hash.h>
Damjan Mariond171d482016-12-05 14:16:38 +010027#include <vnet/l2/l2_input.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028#include <vnet/l2/l2_fib.h>
29#include <vnet/l2/l2_learn.h>
30#include <vnet/l2/l2_bd.h>
31
32#include <vppinfra/bihash_template.c>
33
John Lo8d00fff2017-08-03 00:35:36 -040034#include <vlibmemory/api.h>
35#include <vnet/vnet_msg_enum.h>
36
37#define vl_typedefs /* define message structures */
38#include <vnet/vnet_all_api_h.h>
39#undef vl_typedefs
40
41#define vl_endianfun /* define message structures */
42#include <vnet/vnet_all_api_h.h>
43#undef vl_endianfun
44
Billy McFall22aa3e92016-09-09 08:46:40 -040045/**
46 * @file
47 * @brief Ethernet MAC Address FIB Table Management.
48 *
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.
52 *
53 */
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055l2fib_main_t l2fib_main;
56
Neale Ranns7d645f72018-10-24 02:25:06 -070057u8 *
58format_l2fib_entry_result_flags (u8 * s, va_list * args)
59{
60 l2fib_entry_result_flags_t flags = va_arg (*args, int);
61
62 if (L2FIB_ENTRY_RESULT_FLAG_NONE == flags)
63 {
64 s = format (s, "none");
65 }
66 else
67 {
68#define _(a,v,t) { \
69 if (flags & L2FIB_ENTRY_RESULT_FLAG_##a) \
70 s = format (s, "%s ", t); \
71 }
72 foreach_l2fib_entry_result_attr
73#undef _
74 }
75 return (s);
76}
77
Mohsin Kazmi57938f62017-10-27 21:28:07 +020078static void
79incr_mac_address (u8 * mac)
80{
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);
85
Dave Barach178cf492018-11-13 16:34:13 -050086 clib_memcpy_fast (mac, &tmp, 6);
Mohsin Kazmi57938f62017-10-27 21:28:07 +020087}
88
Dave Barach97d8dc22016-08-15 15:31:15 -040089/** Format sw_if_index. If the value is ~0, use the text "N/A" */
90u8 *
91format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070092{
Dave Barach97d8dc22016-08-15 15:31:15 -040093 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 u32 sw_if_index = va_arg (*args, u32);
95 if (sw_if_index == ~0)
96 return format (s, "N/A");
Eyal Barib823df52017-06-12 17:07:22 +030097
Dave Barach3940de32019-07-23 16:28:36 -040098 vnet_sw_interface_t *swif =
99 vnet_get_sw_interface_or_null (vnm, sw_if_index);
Eyal Barib823df52017-06-12 17:07:22 +0300100 if (!swif)
Eyal Bari0f360dc2017-06-14 13:11:20 +0300101 return format (s, "Stale");
Eyal Barib823df52017-06-12 17:07:22 +0300102
103 return format (s, "%U", format_vnet_sw_interface_name, vnm,
Dave Barach3940de32019-07-23 16:28:36 -0400104 vnet_get_sw_interface_or_null (vnm, sw_if_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105}
106
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700107typedef struct l2fib_dump_walk_ctx_t_
108{
109 u32 bd_index;
110 l2fib_entry_key_t *l2fe_key;
111 l2fib_entry_result_t *l2fe_res;
112} l2fib_dump_walk_ctx_t;
113
Neale Rannsf50bac12019-12-06 05:53:17 +0000114static int
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700115l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
116{
117 l2fib_dump_walk_ctx_t *ctx = arg;
118 l2fib_entry_result_t result;
119 l2fib_entry_key_t key;
120
121 key.raw = kvp->key;
122 result.raw = kvp->value;
123
124 if ((ctx->bd_index == ~0) || (ctx->bd_index == key.fields.bd_index))
125 {
126 vec_add1 (ctx->l2fe_key, key);
127 vec_add1 (ctx->l2fe_res, result);
128 }
Neale Rannsf50bac12019-12-06 05:53:17 +0000129
130 return (BIHASH_WALK_CONTINUE);
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700131}
132
Dave Barach97d8dc22016-08-15 15:31:15 -0400133void
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700134l2fib_table_dump (u32 bd_index,
135 l2fib_entry_key_t ** l2fe_key,
Dave Barach97d8dc22016-08-15 15:31:15 -0400136 l2fib_entry_result_t ** l2fe_res)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137{
Dave Barach97d8dc22016-08-15 15:31:15 -0400138 l2fib_main_t *msm = &l2fib_main;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700139 l2fib_dump_walk_ctx_t ctx = {
140 .bd_index = bd_index,
141 };
142
143 BV (clib_bihash_foreach_key_value_pair)
144 (&msm->mac_table, l2fib_dump_walk_cb, &ctx);
145
146 *l2fe_key = ctx.l2fe_key;
147 *l2fe_res = ctx.l2fe_res;
148}
149
150typedef struct l2fib_show_walk_ctx_t_
151{
152 u8 first_entry;
153 u8 verbose;
154 vlib_main_t *vm;
155 vnet_main_t *vnm;
156 u32 total_entries;
157 u32 bd_index;
158 u8 learn;
159 u8 add;
160 u8 now;
161} l2fib_show_walk_ctx_t;
162
Neale Rannsf50bac12019-12-06 05:53:17 +0000163static int
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700164l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
165{
166 l2fib_show_walk_ctx_t *ctx = arg;
167 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 l2fib_entry_result_t result;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700169 l2fib_entry_key_t key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700171 if (ctx->verbose && ctx->first_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700173 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");
179 }
180
181 key.raw = kvp->key;
182 result.raw = kvp->value;
183 ctx->total_entries++;
184
185 if (ctx->verbose &&
186 ((ctx->bd_index >> 31) || (ctx->bd_index == key.fields.bd_index)))
187 {
188 u8 *s = NULL;
189
Neale Rannsb54d0812018-09-06 06:22:56 -0700190 if (ctx->learn && l2fib_entry_result_is_set_AGE_NOT (&result))
Neale Rannsf50bac12019-12-06 05:53:17 +0000191 return (BIHASH_WALK_CONTINUE); /* skip provisioned macs */
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700192
Neale Rannsb54d0812018-09-06 06:22:56 -0700193 if (ctx->add && !l2fib_entry_result_is_set_AGE_NOT (&result))
Neale Rannsf50bac12019-12-06 05:53:17 +0000194 return (BIHASH_WALK_CONTINUE); /* skip learned macs */
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700195
196 bd_config = vec_elt_at_index (l2input_main.bd_configs,
197 key.fields.bd_index);
198
Neale Rannsb54d0812018-09-06 06:22:56 -0700199 if (l2fib_entry_result_is_set_AGE_NOT (&result))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700200 s = format (s, "no");
201 else if (bd_config->mac_age == 0)
202 s = format (s, "-");
203 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400204 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700205 i16 delta = ctx->now - result.fields.timestamp;
206 delta += delta < 0 ? 256 : 0;
207 s = format (s, "%d", delta);
Dave Barach97d8dc22016-08-15 15:31:15 -0400208 }
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700209
210 vlib_cli_output (ctx->vm,
211 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
212 format_ethernet_address, key.fields.mac,
213 key.fields.bd_index,
214 result.fields.sw_if_index == ~0
215 ? -1 : result.fields.sw_if_index,
Neale Rannsb54d0812018-09-06 06:22:56 -0700216 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) ? "*" : "-",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700220 format_vnet_sw_if_index_name_with_NA,
221 ctx->vnm, result.fields.sw_if_index);
222 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 }
Neale Rannsf50bac12019-12-06 05:53:17 +0000224
225 return (BIHASH_WALK_CONTINUE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226}
227
Chris Luke16bcf7d2016-09-01 14:31:46 -0400228/** Display the contents of the l2fib. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400230show_l2fib (vlib_main_t * vm,
231 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232{
Dave Barach97d8dc22016-08-15 15:31:15 -0400233 bd_main_t *bdm = &bd_main;
234 l2fib_main_t *msm = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 u8 raw = 0;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700236 u32 bd_id;
237 l2fib_show_walk_ctx_t ctx = {
238 .first_entry = 1,
239 .bd_index = ~0,
240 .now = (u8) (vlib_time_now (vm) / 60),
241 .vm = vm,
242 .vnm = msm->vnet_main,
243 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
John Lo7dbd7262018-05-31 10:25:18 -0400245 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
John Lo8d00fff2017-08-03 00:35:36 -0400246 {
John Lo7dbd7262018-05-31 10:25:18 -0400247 if (unformat (input, "raw"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400248 {
John Lo7dbd7262018-05-31 10:25:18 -0400249 raw = 1;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700250 ctx.verbose = 0;
John Lo7dbd7262018-05-31 10:25:18 -0400251 break;
252 }
253 else if (unformat (input, "verbose"))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700254 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400255 else if (unformat (input, "all"))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700256 ctx.verbose = 1;
257 else if (unformat (input, "bd_index %d", &ctx.bd_index))
258 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400259 else if (unformat (input, "learn"))
260 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700261 ctx.add = 0;
262 ctx.learn = 1;
263 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400264 }
265 else if (unformat (input, "add"))
266 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700267 ctx.learn = 0;
268 ctx.add = 1;
269 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400270 }
271 else if (unformat (input, "bd_id %d", &bd_id))
272 {
273 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
274 if (p)
275 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700276 ctx.verbose = 1;
277 ctx.bd_index = p[0];
John Lo7dbd7262018-05-31 10:25:18 -0400278 }
279 else
280 return clib_error_return (0,
281 "bridge domain id %d doesn't exist\n",
282 bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400283 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 else
John Lo7dbd7262018-05-31 10:25:18 -0400285 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286 }
287
Damjan Marion95147812020-09-14 12:18:44 +0200288 if (msm->mac_table_initialized == 0)
289 {
290 vlib_cli_output (vm, "no l2fib entries");
291 return 0;
292 }
293
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700294 BV (clib_bihash_foreach_key_value_pair)
295 (&msm->mac_table, l2fib_show_walk_cb, &ctx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700297 if (ctx.total_entries == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 vlib_cli_output (vm, "no l2fib entries");
299 else
John Lo8d00fff2017-08-03 00:35:36 -0400300 {
301 l2learn_main_t *lm = &l2learn_main;
302 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
303 "Last scan time: %.4esec Learn limit: %d ",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700304 ctx.total_entries, lm->global_learn_count,
John Lo8d00fff2017-08-03 00:35:36 -0400305 msm->age_scan_duration, lm->global_learn_limit);
306 if (lm->client_pid)
307 vlib_cli_output (vm, "L2MAC events client PID: %d "
308 "Last e-scan time: %.4esec Delay: %.2esec "
309 "Max macs in event: %d",
310 lm->client_pid, msm->evt_scan_duration,
311 msm->event_scan_delay, msm->max_macs_in_event);
312 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
314 if (raw)
315 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700316 BV (format_bihash), &msm->mac_table, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
318 return 0;
319}
320
Billy McFall22aa3e92016-09-09 08:46:40 -0400321/*?
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700322 * This command displays the MAC Address entries of the L2 FIB table.
Billy McFall22aa3e92016-09-09 08:46:40 -0400323 * Output can be filtered to just get the number of MAC Addresses or display
324 * each MAC Address for all bridge domains or just a single bridge domain.
325 *
326 * @cliexpar
327 * Example of how to display the number of MAC Address entries in the L2
328 * FIB table:
329 * @cliexstart{show l2fib}
330 * 3 l2fib entries
331 * @cliexend
332 * Example of how to display all the MAC Address entries in the L2
333 * FIB table:
John Lo7dbd7262018-05-31 10:25:18 -0400334 * @cliexstart{show l2fib all}
Billy McFall22aa3e92016-09-09 08:46:40 -0400335 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
336 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
337 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
338 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
339 * 3 l2fib entries
340 * @cliexend
341?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400342/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
344 .path = "show l2fib",
John Lo7dbd7262018-05-31 10:25:18 -0400345 .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 .function = show_l2fib,
347};
Dave Barach97d8dc22016-08-15 15:31:15 -0400348/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
Damjan Marion95147812020-09-14 12:18:44 +0200350void
351l2fib_table_init (void)
352{
353 l2fib_main_t *mp = &l2fib_main;
354
355 if (mp->mac_table_initialized == 1)
356 return;
357
358 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
359 mp->mac_table_n_buckets, mp->mac_table_memory_size);
360 mp->mac_table_initialized = 1;
361}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362
Dave Barach97d8dc22016-08-15 15:31:15 -0400363/* Remove all entries from the l2fib */
364void
Eyal Bari7537e712017-04-27 14:07:55 +0300365l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366{
Dave Barach97d8dc22016-08-15 15:31:15 -0400367 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Damjan Marion95147812020-09-14 12:18:44 +0200369 if (mp->mac_table_initialized == 0)
370 return;
371
Eyal Bari7537e712017-04-27 14:07:55 +0300372 /* Remove all entries */
373 BV (clib_bihash_free) (&mp->mac_table);
Damjan Marion95147812020-09-14 12:18:44 +0200374 l2fib_table_init ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 l2learn_main.global_learn_count = 0;
376}
377
Chris Luke16bcf7d2016-09-01 14:31:46 -0400378/** Clear all entries in L2FIB.
379 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400380 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400382clear_l2fib (vlib_main_t * vm,
383 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384{
Eyal Bari7537e712017-04-27 14:07:55 +0300385 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 return 0;
387}
388
Billy McFall22aa3e92016-09-09 08:46:40 -0400389/*?
390 * This command clears all the MAC Address entries from the L2 FIB table.
391 *
392 * @cliexpar
393 * Example of how to clear the L2 FIB Table:
394 * @cliexcmd{clear l2fib}
395 * Example to show the L2 FIB Table has been cleared:
396 * @cliexstart{show l2fib verbose}
397 * no l2fib entries
398 * @cliexend
399?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400400/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
402 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400403 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 .function = clear_l2fib,
405};
Dave Barach97d8dc22016-08-15 15:31:15 -0400406/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407
Eyal Bari7537e712017-04-27 14:07:55 +0300408static inline l2fib_seq_num_t
409l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
410{
Eyal Bari7537e712017-04-27 14:07:55 +0300411 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
412 /* *INDENT-OFF* */
413 return (l2fib_seq_num_t) {
Eyal Bari0f360dc2017-06-14 13:11:20 +0300414 .swif = *l2fib_swif_seq_num (sw_if_index),
Eyal Bari7537e712017-04-27 14:07:55 +0300415 .bd = bd_config->seq_num,
416 };
417 /* *INDENT-ON* */
418}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
Dave Barach97d8dc22016-08-15 15:31:15 -0400420/**
421 * Add an entry to the l2fib.
422 * If the entry already exists then overwrite it
423 */
424void
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700425l2fib_add_entry (const u8 * mac, u32 bd_index,
Neale Rannsb54d0812018-09-06 06:22:56 -0700426 u32 sw_if_index, l2fib_entry_result_flags_t flags)
Dave Barach97d8dc22016-08-15 15:31:15 -0400427{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 l2fib_entry_key_t key;
429 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400430 __attribute__ ((unused)) u32 bucket_contents;
John Lo8d00fff2017-08-03 00:35:36 -0400431 l2fib_main_t *fm = &l2fib_main;
432 l2learn_main_t *lm = &l2learn_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400433 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Damjan Marion95147812020-09-14 12:18:44 +0200435 if (fm->mac_table_initialized == 0)
436 l2fib_table_init ();
437
Dave Barach97d8dc22016-08-15 15:31:15 -0400438 /* set up key */
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200439 key.raw = l2fib_make_key (mac, bd_index);
John Lob2e73b12020-01-07 16:35:29 -0500440 kv.key = key.raw;
Dave Barach97d8dc22016-08-15 15:31:15 -0400441
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700442 /* check if entry already exist */
John Lo8d00fff2017-08-03 00:35:36 -0400443 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
444 {
445 /* decrement counter if overwriting a learned mac */
446 result.raw = kv.value;
Neale Rannsb54d0812018-09-06 06:22:56 -0700447 if ((!l2fib_entry_result_is_set_AGE_NOT (&result))
448 && (lm->global_learn_count))
John Lo8d00fff2017-08-03 00:35:36 -0400449 lm->global_learn_count--;
450 }
451
Dave Barach97d8dc22016-08-15 15:31:15 -0400452 /* set up result */
453 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454 result.fields.sw_if_index = sw_if_index;
Neale Rannsb54d0812018-09-06 06:22:56 -0700455 result.fields.flags = flags;
456
457 /* no aging for provisioned entry */
458 l2fib_entry_result_set_AGE_NOT (&result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 kv.value = result.raw;
461
John Lo8d00fff2017-08-03 00:35:36 -0400462 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463}
464
Dave Barach97d8dc22016-08-15 15:31:15 -0400465/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400466 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400467 * The CLI format is:
468 * l2fib add <mac> <bd> <intf> [static] [bvi]
469 * l2fib add <mac> <bd> filter
470 * Note that filter and bvi entries are always static
471 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400473l2fib_add (vlib_main_t * vm,
474 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475{
Dave Barach97d8dc22016-08-15 15:31:15 -0400476 bd_main_t *bdm = &bd_main;
477 vnet_main_t *vnm = vnet_get_main ();
478 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200479 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 u32 bd_id;
481 u32 bd_index;
482 u32 sw_if_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400483 uword *p;
Neale Rannsb54d0812018-09-06 06:22:56 -0700484 l2fib_entry_result_flags_t flags;
485
486 flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200488 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 {
490 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400491 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 goto done;
493 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400494
495 if (!unformat (input, "%d", &bd_id))
496 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400498 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400500 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501
502 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400503 if (!p)
504 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
506 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400507 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 bd_index = p[0];
509
Dave Barach97d8dc22016-08-15 15:31:15 -0400510 if (unformat (input, "filter"))
511 {
John Lo14edd972017-10-31 13:26:02 -0400512 l2fib_add_filter_entry (mac, bd_index);
513 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
John Lo14edd972017-10-31 13:26:02 -0400516 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
517 {
518 error = clib_error_return (0, "unknown interface `%U'",
519 format_unformat_error, input);
520 goto done;
521 }
522
523 if (unformat (input, "static"))
Neale Rannsb54d0812018-09-06 06:22:56 -0700524 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
John Lo14edd972017-10-31 13:26:02 -0400525 else if (unformat (input, "bvi"))
Neale Rannsb54d0812018-09-06 06:22:56 -0700526 flags |= (L2FIB_ENTRY_RESULT_FLAG_STATIC | L2FIB_ENTRY_RESULT_FLAG_BVI);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
John Lob2fd6cb2017-07-12 19:56:45 -0400528 if (vec_len (l2input_main.configs) <= sw_if_index)
529 {
530 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
531 sw_if_index);
532 goto done;
533 }
534
Neale Rannsb54d0812018-09-06 06:22:56 -0700535 l2fib_add_entry (mac, bd_index, sw_if_index, flags);
Dave Barach97d8dc22016-08-15 15:31:15 -0400536
537done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 return error;
539}
540
Billy McFall22aa3e92016-09-09 08:46:40 -0400541/*?
542 * This command adds a MAC Address entry to the L2 FIB table
543 * of an existing bridge-domain. The MAC Address can be static
544 * or dynamic. This command also allows a filter to be added,
545 * such that packets with given MAC Addresses (source mac or
546 * destination mac match) are dropped.
547 *
548 * @cliexpar
549 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
550 * of a bridge-domain (where 200 is the bridge-domain-id):
551 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
552 * Example of how to add a static MAC Address entry to the L2 FIB table
553 * of a bridge-domain (where 200 is the bridge-domain-id):
554 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
555 * Example of how to add a filter such that a packet with the given MAC
556 * Address will be dropped in a given bridge-domain (where 200 is the
557 * bridge-domain-id):
558 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
559 * Example of show command of the provisioned MAC Addresses and filters:
560 * @cliexstart{show l2fib verbose}
561 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
562 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
563 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
564 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
565 * 3 l2fib entries
566 * @cliexend
567?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400568/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
570 .path = "l2fib add",
Billy McFall22aa3e92016-09-09 08:46:40 -0400571 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 .function = l2fib_add,
573};
Dave Barach97d8dc22016-08-15 15:31:15 -0400574/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
576
577static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400578l2fib_test_command_fn (vlib_main_t * vm,
579 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580{
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200581 u8 mac[6], save_mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 u32 bd_index = 0;
583 u32 sw_if_index = 8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584 u32 is_add = 0;
585 u32 is_del = 0;
586 u32 is_check = 0;
587 u32 count = 1;
588 int mac_set = 0;
589 int i;
590
Dave Barach97d8dc22016-08-15 15:31:15 -0400591 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200593 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400594 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400596 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400598 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400600 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400602 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400604 break;
605 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
607 if (mac_set == 0)
608 return clib_error_return (0, "mac not set");
609
610 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400611 return clib_error_return (0,
612 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613
Dave Barach178cf492018-11-13 16:34:13 -0500614 clib_memcpy_fast (save_mac, mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615
616 if (is_add)
617 {
618 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400619 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700620 l2fib_add_entry (mac, bd_index, sw_if_index,
621 L2FIB_ENTRY_RESULT_FLAG_NONE);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200622 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400623 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624 }
625
626 if (is_check)
627 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400628 BVT (clib_bihash_kv) kv;
629 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630
Damjan Marion95147812020-09-14 12:18:44 +0200631 if (mp->mac_table_initialized == 0)
632 return clib_error_return (0, "mac table is not initialized");
633
Dave Barach178cf492018-11-13 16:34:13 -0500634 clib_memcpy_fast (mac, save_mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
636 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400637 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200638 kv.key = l2fib_make_key (mac, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400639 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
640 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200641 clib_warning ("key %U AWOL", format_ethernet_address, mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400642 break;
643 }
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200644 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400645 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646 }
647
648 if (is_del)
649 {
Dave Barach178cf492018-11-13 16:34:13 -0500650 clib_memcpy_fast (mac, save_mac, 6);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200651
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400653 {
John Lo7dbd7262018-05-31 10:25:18 -0400654 l2fib_del_entry (mac, bd_index, 0);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200655 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400656 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 }
658
Damjan Marion95147812020-09-14 12:18:44 +0200659 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660}
661
Billy McFall22aa3e92016-09-09 08:46:40 -0400662/*?
663 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
664 * bridge domain (bridge-domain-id of 0) to be modified.
665 *
666 * @cliexpar
667 * @parblock
668 * Example of how to add a set of 4 sequential MAC Address entries to L2
669 * FIB table of the default bridge-domain:
670 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
671 *
672 * Show the set of 4 sequential MAC Address entries that were added:
673 * @cliexstart{show l2fib verbose}
674 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
675 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
676 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
677 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
678 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
679 * 4 l2fib entries
680 * @cliexend
681 *
682 * Example of how to check that the set of 4 sequential MAC Address
683 * entries were added to L2 FIB table of the default
684 * bridge-domain. Used a count of 5 to produce an error:
685 *
686 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
687 * The output of the check command is in the log files. Log file
688 * location may vary based on your OS and Version:
689 *
690 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
691 *
692 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
693 *
694 * Example of how to delete a set of 4 sequential MAC Address entries
695 * from L2 FIB table of the default bridge-domain:
696 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
697 * @endparblock
698?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400699/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700VLIB_CLI_COMMAND (l2fib_test_command, static) = {
701 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400702 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703 .function = l2fib_test_command_fn,
704};
Dave Barach97d8dc22016-08-15 15:31:15 -0400705/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706
707
Dave Barach97d8dc22016-08-15 15:31:15 -0400708/**
709 * Delete an entry from the l2fib.
John Lo7dbd7262018-05-31 10:25:18 -0400710 * Return 0 if the entry was deleted, or 1 it was not found or if
711 * sw_if_index is non-zero and does not match that in the entry.
Dave Barach97d8dc22016-08-15 15:31:15 -0400712 */
John Lo7dbd7262018-05-31 10:25:18 -0400713u32
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700714l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400715{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400717 l2fib_main_t *mp = &l2fib_main;
718 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719
Damjan Marion95147812020-09-14 12:18:44 +0200720 if (mp->mac_table_initialized == 0)
721 return 1;
722
Dave Barach97d8dc22016-08-15 15:31:15 -0400723 /* set up key */
John Lo7dbd7262018-05-31 10:25:18 -0400724 kv.key = l2fib_make_key (mac, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725
Dave Barach97d8dc22016-08-15 15:31:15 -0400726 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727 return 1;
728
729 result.raw = kv.value;
730
John Lo7dbd7262018-05-31 10:25:18 -0400731 /* check if sw_if_index of entry match */
732 if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index))
733 return 1;
734
Dave Barach97d8dc22016-08-15 15:31:15 -0400735 /* decrement counter if dynamically learned mac */
Neale Rannsb54d0812018-09-06 06:22:56 -0700736 if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) &&
737 (l2learn_main.global_learn_count))
John Lo8d00fff2017-08-03 00:35:36 -0400738 l2learn_main.global_learn_count--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739
Dave Barach97d8dc22016-08-15 15:31:15 -0400740 /* Remove entry from hash table */
741 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742 return 0;
743}
744
Dave Barach97d8dc22016-08-15 15:31:15 -0400745/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400746 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400747 * The CLI format is:
748 * l2fib del <mac> <bd-id>
749 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400751l2fib_del (vlib_main_t * vm,
752 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753{
Dave Barach97d8dc22016-08-15 15:31:15 -0400754 bd_main_t *bdm = &bd_main;
755 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200756 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757 u32 bd_id;
758 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400759 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200761 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762 {
763 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400764 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765 goto done;
766 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400767
768 if (!unformat (input, "%d", &bd_id))
769 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400771 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400773 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
775 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400776 if (!p)
777 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
779 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400780 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 bd_index = p[0];
782
Dave Barach97d8dc22016-08-15 15:31:15 -0400783 /* Delete the entry */
John Lo7dbd7262018-05-31 10:25:18 -0400784 if (l2fib_del_entry (mac, bd_index, 0))
Dave Barach97d8dc22016-08-15 15:31:15 -0400785 {
786 error = clib_error_return (0, "mac entry not found");
787 goto done;
788 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789
Dave Barach97d8dc22016-08-15 15:31:15 -0400790done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 return error;
792}
793
Billy McFall22aa3e92016-09-09 08:46:40 -0400794/*?
795 * This command deletes an existing MAC Address entry from the L2 FIB
796 * table of an existing bridge-domain.
797 *
798 * @cliexpar
799 * 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):
800 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
801?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400802/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
804 .path = "l2fib del",
John Lo7dbd7262018-05-31 10:25:18 -0400805 .short_help = "l2fib del <mac> <bridge-domain-id> []",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806 .function = l2fib_del,
807};
Dave Barach97d8dc22016-08-15 15:31:15 -0400808/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809
John Loda1f2c72017-03-24 20:11:15 -0400810/**
811 Kick off ager to scan MACs to age/delete MAC entries
812*/
813void
814l2fib_start_ager_scan (vlib_main_t * vm)
815{
Eyal Bari24db0ec2017-09-27 21:43:51 +0300816 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
John Loda1f2c72017-03-24 20:11:15 -0400817
818 /* check if there is at least one bd with mac aging enabled */
Eyal Bari24db0ec2017-09-27 21:43:51 +0300819 l2_bridge_domain_t *bd_config;
John Loda1f2c72017-03-24 20:11:15 -0400820 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300821 {
John Loda1f2c72017-03-24 20:11:15 -0400822 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300823 {
824 evt = L2_MAC_AGE_PROCESS_EVENT_START;
825 break;
826 }
827 }
John Loda1f2c72017-03-24 20:11:15 -0400828
829 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
Eyal Bari24db0ec2017-09-27 21:43:51 +0300830 evt, 0);
John Loda1f2c72017-03-24 20:11:15 -0400831}
832
833/**
Eyal Bari7537e712017-04-27 14:07:55 +0300834 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400835*/
836void
837l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
838{
Eyal Bari0f360dc2017-06-14 13:11:20 +0300839 *l2fib_swif_seq_num (sw_if_index) += 1;
John Loda1f2c72017-03-24 20:11:15 -0400840 l2fib_start_ager_scan (vm);
841}
842
843/**
Eyal Bari7537e712017-04-27 14:07:55 +0300844 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400845*/
846void
847l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
848{
Eyal Bari7537e712017-04-27 14:07:55 +0300849 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400850 bd_config->seq_num += 1;
851 l2fib_start_ager_scan (vm);
852}
853
854/**
Eyal Bari7537e712017-04-27 14:07:55 +0300855 Flush all non static MACs - flushes all valid BDs
856*/
857void
858l2fib_flush_all_mac (vlib_main_t * vm)
859{
860 l2_bridge_domain_t *bd_config;
861 vec_foreach (bd_config, l2input_main.bd_configs)
862 if (bd_is_valid (bd_config))
863 bd_config->seq_num += 1;
864
865 l2fib_start_ager_scan (vm);
866}
867
868
869/**
John Loda1f2c72017-03-24 20:11:15 -0400870 Flush MACs, except static ones, associated with an interface
871 The CLI format is:
872 l2fib flush-mac interface <if-name>
873*/
874static clib_error_t *
875l2fib_flush_mac_int (vlib_main_t * vm,
876 unformat_input_t * input, vlib_cli_command_t * cmd)
877{
878 vnet_main_t *vnm = vnet_get_main ();
879 clib_error_t *error = 0;
880 u32 sw_if_index;
881
882 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
883 {
884 error = clib_error_return (0, "unknown interface `%U'",
885 format_unformat_error, input);
886 goto done;
887 }
888
889 l2fib_flush_int_mac (vm, sw_if_index);
890
891done:
892 return error;
893}
894
Eyal Bari7537e712017-04-27 14:07:55 +0300895/**
896 Flush all MACs, except static ones
897 The CLI format is:
898 l2fib flush-mac all
899*/
900static clib_error_t *
901l2fib_flush_mac_all (vlib_main_t * vm,
902 unformat_input_t * input, vlib_cli_command_t * cmd)
903{
904 l2fib_flush_all_mac (vm);
905 return 0;
906}
907
908/*?
909 * This command kick off ager to delete all existing MAC Address entries,
910 * except static ones, associated with an interface from the L2 FIB table.
911 *
912 * @cliexpar
913 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
914 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
915?*/
916/* *INDENT-OFF* */
917VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
918 .path = "l2fib flush-mac all",
919 .short_help = "l2fib flush-mac all",
920 .function = l2fib_flush_mac_all,
921};
922/* *INDENT-ON* */
923
John Loda1f2c72017-03-24 20:11:15 -0400924/*?
925 * This command kick off ager to delete all existing MAC Address entries,
926 * except static ones, associated with an interface from the L2 FIB table.
927 *
928 * @cliexpar
929 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
930 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
931?*/
932/* *INDENT-OFF* */
933VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
934 .path = "l2fib flush-mac interface",
935 .short_help = "l2fib flush-mac interface <if-name>",
936 .function = l2fib_flush_mac_int,
937};
938/* *INDENT-ON* */
939
940/**
941 Flush bridge-domain MACs except static ones.
942 The CLI format is:
943 l2fib flush-mac bridge-domain <bd-id>
944*/
945static clib_error_t *
946l2fib_flush_mac_bd (vlib_main_t * vm,
947 unformat_input_t * input, vlib_cli_command_t * cmd)
948{
949 bd_main_t *bdm = &bd_main;
950 clib_error_t *error = 0;
951 u32 bd_index, bd_id;
952 uword *p;
953
954 if (!unformat (input, "%d", &bd_id))
955 {
956 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
957 format_unformat_error, input);
958 goto done;
959 }
960
961 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
962 if (p)
963 bd_index = *p;
964 else
965 return clib_error_return (0, "No such bridge domain %d", bd_id);
966
967 l2fib_flush_bd_mac (vm, bd_index);
968
969done:
970 return error;
971}
972
973/*?
974 * This command kick off ager to delete all existing MAC Address entries,
975 * except static ones, in a bridge domain from the L2 FIB table.
976 *
977 * @cliexpar
978 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
979 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
980?*/
981/* *INDENT-OFF* */
982VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
983 .path = "l2fib flush-mac bridge-domain",
984 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
985 .function = l2fib_flush_mac_bd,
986};
987/* *INDENT-ON* */
988
Eyal Bariafc47aa2017-04-20 14:45:17 +0300989clib_error_t *
990l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
991{
992 l2_input_config_t *config = l2input_intf_config (sw_if_index);
993 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
994 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
995 return 0;
996}
997
998VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999
Dave Barach97d8dc22016-08-15 15:31:15 -04001000BVT (clib_bihash) * get_mac_table (void)
1001{
1002 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 return &mp->mac_table;
1004}
1005
John Lo8d00fff2017-08-03 00:35:36 -04001006static_always_inline void *
1007allocate_mac_evt_buf (u32 client, u32 client_index)
1008{
1009 l2fib_main_t *fm = &l2fib_main;
1010 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
1011 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
1012 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
1013 mp->pid = htonl (client);
1014 mp->client_index = client_index;
1015 return mp;
1016}
1017
1018static_always_inline f64
1019l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
1020{
1021 l2fib_main_t *fm = &l2fib_main;
1022 l2learn_main_t *lm = &l2learn_main;
1023
1024 BVT (clib_bihash) * h = &fm->mac_table;
1025 int i, j, k;
1026 f64 last_start = start_time;
1027 f64 accum_t = 0;
1028 f64 delta_t = 0;
1029 u32 evt_idx = 0;
1030 u32 learn_count = 0;
1031 u32 client = lm->client_pid;
1032 u32 cl_idx = lm->client_index;
1033 vl_api_l2_macs_event_t *mp = 0;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001034 vl_api_registration_t *reg = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001035
Dave Barach32dcd3b2019-07-08 12:25:38 -04001036 /* Don't scan the l2 fib if it hasn't been instantiated yet */
1037 if (alloc_arena (h) == 0)
1038 return 0.0;
1039
John Lo8d00fff2017-08-03 00:35:36 -04001040 if (client)
1041 {
1042 mp = allocate_mac_evt_buf (client, cl_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001043 reg = vl_api_client_index_to_registration (lm->client_index);
John Lo8d00fff2017-08-03 00:35:36 -04001044 }
1045
1046 for (i = 0; i < h->nbuckets; i++)
1047 {
1048 /* allow no more than 20us without a pause */
1049 delta_t = vlib_time_now (vm) - last_start;
1050 if (delta_t > 20e-6)
1051 {
1052 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
1053 last_start = vlib_time_now (vm);
1054 accum_t += delta_t;
1055 }
1056
1057 if (i < (h->nbuckets - 3))
1058 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001059 BVT (clib_bihash_bucket) * b =
1060 BV (clib_bihash_get_bucket) (h, i + 3);
John Lo8d00fff2017-08-03 00:35:36 -04001061 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
Dave Barach16e4a4a2020-04-16 12:00:14 -04001062 b = BV (clib_bihash_get_bucket) (h, i + 1);
1063 if (!BV (clib_bihash_bucket_is_empty) (b))
John Lo8d00fff2017-08-03 00:35:36 -04001064 {
1065 BVT (clib_bihash_value) * v =
1066 BV (clib_bihash_get_value) (h, b->offset);
1067 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1068 }
1069 }
1070
Dave Barach16e4a4a2020-04-16 12:00:14 -04001071 BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i);
1072 if (BV (clib_bihash_bucket_is_empty) (b))
John Lo8d00fff2017-08-03 00:35:36 -04001073 continue;
1074 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1075 for (j = 0; j < (1 << b->log2_pages); j++)
1076 {
1077 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1078 {
1079 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1080 continue;
1081
1082 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1083 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1084
Neale Rannsb54d0812018-09-06 06:22:56 -07001085 if (!l2fib_entry_result_is_set_AGE_NOT (&result))
John Lo8d00fff2017-08-03 00:35:36 -04001086 learn_count++;
1087
John Lo8d00fff2017-08-03 00:35:36 -04001088 if (client)
1089 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001090 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1091 {
1092 /* event message full, send it and start a new one */
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001093 if (reg && vl_api_can_send_msg (reg))
Eyal Bari24db0ec2017-09-27 21:43:51 +03001094 {
1095 mp->n_macs = htonl (evt_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001096 vl_api_send_msg (reg, (u8 *) mp);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001097 mp = allocate_mac_evt_buf (client, cl_idx);
1098 }
1099 else
1100 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001101 if (reg)
Dave Barach59b25652017-09-10 15:04:27 -04001102 clib_warning ("MAC event to pid %d queue stuffed!"
1103 " %d MAC entries lost", client,
1104 evt_idx);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001105 }
1106 evt_idx = 0;
1107 }
1108
Neale Rannsb54d0812018-09-06 06:22:56 -07001109 if (l2fib_entry_result_is_set_LRN_EVT (&result))
John Lo8d00fff2017-08-03 00:35:36 -04001110 {
1111 /* copy mac entry to event msg */
Dave Barach178cf492018-11-13 16:34:13 -05001112 clib_memcpy_fast (mp->mac[evt_idx].mac_addr,
1113 key.fields.mac, 6);
Neale Rannsb54d0812018-09-06 06:22:56 -07001114 mp->mac[evt_idx].action =
1115 l2fib_entry_result_is_set_LRN_MOV (&result) ?
Jakub Grajciar145e3302019-10-24 13:52:42 +02001116 (vl_api_mac_event_action_t) MAC_EVENT_ACTION_MOVE
1117 : (vl_api_mac_event_action_t) MAC_EVENT_ACTION_ADD;
1118 mp->mac[evt_idx].action =
1119 htonl (mp->mac[evt_idx].action);
John Lo8d00fff2017-08-03 00:35:36 -04001120 mp->mac[evt_idx].sw_if_index =
1121 htonl (result.fields.sw_if_index);
John Loe23c99e2018-03-13 21:53:18 -04001122 /* clear event bits and update mac entry */
Neale Rannsb54d0812018-09-06 06:22:56 -07001123 l2fib_entry_result_clear_LRN_EVT (&result);
1124 l2fib_entry_result_clear_LRN_MOV (&result);
John Lo8d00fff2017-08-03 00:35:36 -04001125 BVT (clib_bihash_kv) kv;
1126 kv.key = key.raw;
1127 kv.value = result.raw;
1128 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1129 evt_idx++;
1130 continue; /* skip aging */
1131 }
1132 }
1133
Neale Rannsb54d0812018-09-06 06:22:56 -07001134 if (event_only || l2fib_entry_result_is_set_AGE_NOT (&result))
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001135 continue; /* skip aging - static_mac always age_not */
John Lo8d00fff2017-08-03 00:35:36 -04001136
1137 /* start aging processing */
1138 u32 bd_index = key.fields.bd_index;
1139 u32 sw_if_index = result.fields.sw_if_index;
1140 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1141 if (result.fields.sn.as_u16 != sn)
1142 goto age_out; /* stale mac */
1143
1144 l2_bridge_domain_t *bd_config =
1145 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1146
1147 if (bd_config->mac_age == 0)
1148 continue; /* skip aging */
1149
1150 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1151 delta += delta < 0 ? 256 : 0;
1152
1153 if (delta < bd_config->mac_age)
1154 continue; /* still valid */
1155
1156 age_out:
1157 if (client)
1158 {
1159 /* copy mac entry to event msg */
Dave Barach178cf492018-11-13 16:34:13 -05001160 clib_memcpy_fast (mp->mac[evt_idx].mac_addr, key.fields.mac,
1161 6);
Jakub Grajciar145e3302019-10-24 13:52:42 +02001162 mp->mac[evt_idx].action =
1163 (vl_api_mac_event_action_t) MAC_EVENT_ACTION_DELETE;
1164 mp->mac[evt_idx].action = htonl (mp->mac[evt_idx].action);
John Lo8d00fff2017-08-03 00:35:36 -04001165 mp->mac[evt_idx].sw_if_index =
1166 htonl (result.fields.sw_if_index);
1167 evt_idx++;
1168 }
1169 /* delete mac entry */
1170 BVT (clib_bihash_kv) kv;
1171 kv.key = key.raw;
1172 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1173 learn_count--;
Dave Barach28374ca2018-08-07 12:46:18 -04001174 /*
1175 * Note: we may have just freed the bucket's backing
1176 * storage, so check right here...
1177 */
Dave Barach16e4a4a2020-04-16 12:00:14 -04001178 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barach28374ca2018-08-07 12:46:18 -04001179 goto doublebreak;
John Lo8d00fff2017-08-03 00:35:36 -04001180 }
1181 v++;
1182 }
Dave Barach28374ca2018-08-07 12:46:18 -04001183 doublebreak:
1184 ;
John Lo8d00fff2017-08-03 00:35:36 -04001185 }
1186
1187 /* keep learn count consistent */
1188 l2learn_main.global_learn_count = learn_count;
1189
1190 if (mp)
1191 {
1192 /* send any outstanding mac event message else free message buffer */
1193 if (evt_idx)
1194 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001195 if (reg && vl_api_can_send_msg (reg))
John Lo8d00fff2017-08-03 00:35:36 -04001196 {
1197 mp->n_macs = htonl (evt_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001198 vl_api_send_msg (reg, (u8 *) mp);
John Lo8d00fff2017-08-03 00:35:36 -04001199 }
1200 else
1201 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001202 if (reg)
Dave Barach59b25652017-09-10 15:04:27 -04001203 clib_warning ("MAC event to pid %d queue stuffed!"
1204 " %d MAC entries lost", client, evt_idx);
John Lo8d00fff2017-08-03 00:35:36 -04001205 vl_msg_api_free (mp);
1206 }
1207 }
1208 else
1209 vl_msg_api_free (mp);
1210 }
1211 return delta_t + accum_t;
1212}
1213
Damjan Mariond171d482016-12-05 14:16:38 +01001214static uword
1215l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1216 vlib_frame_t * f)
1217{
1218 uword event_type, *event_data = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001219 l2fib_main_t *fm = &l2fib_main;
1220 l2learn_main_t *lm = &l2learn_main;
Damjan Mariond171d482016-12-05 14:16:38 +01001221 bool enabled = 0;
John Lo7f358b32018-04-28 01:19:24 -04001222 f64 start_time, next_age_scan_time = CLIB_TIME_MAX;
Damjan Mariond171d482016-12-05 14:16:38 +01001223
1224 while (1)
1225 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001226 if (lm->client_pid)
1227 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1228 else if (enabled)
John Lo8d00fff2017-08-03 00:35:36 -04001229 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001230 f64 t = next_age_scan_time - vlib_time_now (vm);
1231 vlib_process_wait_for_event_or_clock (vm, t);
John Lo8d00fff2017-08-03 00:35:36 -04001232 }
Damjan Mariond171d482016-12-05 14:16:38 +01001233 else
1234 vlib_process_wait_for_event (vm);
1235
1236 event_type = vlib_process_get_events (vm, &event_data);
1237 vec_reset_length (event_data);
1238
John Lo8d00fff2017-08-03 00:35:36 -04001239 start_time = vlib_time_now (vm);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001240 enum
1241 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
John Lo8d00fff2017-08-03 00:35:36 -04001242
Damjan Mariond171d482016-12-05 14:16:38 +01001243 switch (event_type)
1244 {
John Lo8d00fff2017-08-03 00:35:36 -04001245 case ~0: /* timer expired */
Eyal Bari24db0ec2017-09-27 21:43:51 +03001246 if (lm->client_pid != 0 && start_time < next_age_scan_time)
John Lo8d00fff2017-08-03 00:35:36 -04001247 scan = SCAN_MAC_EVENT;
Damjan Mariond171d482016-12-05 14:16:38 +01001248 break;
John Lo8d00fff2017-08-03 00:35:36 -04001249
Damjan Mariond171d482016-12-05 14:16:38 +01001250 case L2_MAC_AGE_PROCESS_EVENT_START:
1251 enabled = 1;
1252 break;
John Lo8d00fff2017-08-03 00:35:36 -04001253
Damjan Mariond171d482016-12-05 14:16:38 +01001254 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1255 enabled = 0;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001256 scan = SCAN_DISABLE;
1257 break;
John Lo8d00fff2017-08-03 00:35:36 -04001258
John Loda1f2c72017-03-24 20:11:15 -04001259 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
John Loda1f2c72017-03-24 20:11:15 -04001260 break;
John Lo8d00fff2017-08-03 00:35:36 -04001261
Damjan Mariond171d482016-12-05 14:16:38 +01001262 default:
1263 ASSERT (0);
1264 }
Eyal Bari7537e712017-04-27 14:07:55 +03001265
John Lo8d00fff2017-08-03 00:35:36 -04001266 if (scan == SCAN_MAC_EVENT)
1267 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1268 else
Eyal Bari24db0ec2017-09-27 21:43:51 +03001269 {
1270 if (scan == SCAN_MAC_AGE)
1271 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1272 if (scan == SCAN_DISABLE)
1273 {
1274 l2fib_main.age_scan_duration = 0;
1275 l2fib_main.evt_scan_duration = 0;
1276 }
1277 /* schedule next scan */
1278 if (enabled)
1279 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1280 else
John Lo7f358b32018-04-28 01:19:24 -04001281 next_age_scan_time = CLIB_TIME_MAX;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001282 }
Damjan Mariond171d482016-12-05 14:16:38 +01001283 }
1284 return 0;
1285}
1286
1287/* *INDENT-OFF* */
1288VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1289 .function = l2fib_mac_age_scanner_process,
1290 .type = VLIB_NODE_TYPE_PROCESS,
1291 .name = "l2fib-mac-age-scanner-process",
1292};
1293/* *INDENT-ON* */
1294
Dave Barach97d8dc22016-08-15 15:31:15 -04001295clib_error_t *
1296l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297{
Dave Barach97d8dc22016-08-15 15:31:15 -04001298 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001299 l2fib_entry_key_t test_key;
1300 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001301
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001303 mp->vnet_main = vnet_get_main ();
Damjan Marion95147812020-09-14 12:18:44 +02001304 if (mp->mac_table_n_buckets == 0)
1305 mp->mac_table_n_buckets = L2FIB_NUM_BUCKETS;
1306 if (mp->mac_table_memory_size == 0)
1307 mp->mac_table_memory_size = L2FIB_MEMORY_SIZE;
1308 mp->mac_table_initialized = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309
Dave Barach97d8dc22016-08-15 15:31:15 -04001310 /* verify the key constructor is good, since it is endian-sensitive */
Dave Barachb7b92992018-10-17 10:38:51 -04001311 clib_memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312 test_mac[0] = 0x11;
1313 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001314 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315 ASSERT (test_key.fields.mac[0] == 0x11);
1316 ASSERT (test_key.fields.bd_index == 0x1234);
1317
1318 return 0;
1319}
1320
1321VLIB_INIT_FUNCTION (l2fib_init);
1322
Damjan Marion95147812020-09-14 12:18:44 +02001323static clib_error_t *
1324lfib_config (vlib_main_t * vm, unformat_input_t * input)
1325{
1326 l2fib_main_t *lm = &l2fib_main;
1327 uword table_size = ~0;
1328 u32 n_buckets = ~0;
1329
1330 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1331 {
1332 if (unformat (input, "table-size %U", unformat_memory_size,
1333 &table_size))
1334 ;
1335 else if (unformat (input, "num-buckets %u", &n_buckets))
1336 ;
1337 else
1338 return clib_error_return (0, "unknown input `%U'",
1339 format_unformat_error, input);
1340 }
1341
1342 if (n_buckets != ~0)
1343 {
1344 if (!is_pow2 (n_buckets))
1345 return clib_error_return (0, "num-buckets must be power of 2");
1346 lm->mac_table_n_buckets = n_buckets;
1347 }
1348
1349 if (table_size != ~0)
1350 lm->mac_table_memory_size = table_size;
1351 return 0;
1352}
1353
1354VLIB_CONFIG_FUNCTION (lfib_config, "l2fib");
1355
Dave Barach97d8dc22016-08-15 15:31:15 -04001356/*
1357 * fd.io coding-style-patch-verification: ON
1358 *
1359 * Local Variables:
1360 * eval: (c-set-style "gnu")
1361 * End:
1362 */