blob: 983e0218bfc2b616504c38901547862dff6ab3ef [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
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700288 BV (clib_bihash_foreach_key_value_pair)
289 (&msm->mac_table, l2fib_show_walk_cb, &ctx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700291 if (ctx.total_entries == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 vlib_cli_output (vm, "no l2fib entries");
293 else
John Lo8d00fff2017-08-03 00:35:36 -0400294 {
295 l2learn_main_t *lm = &l2learn_main;
296 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
297 "Last scan time: %.4esec Learn limit: %d ",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700298 ctx.total_entries, lm->global_learn_count,
John Lo8d00fff2017-08-03 00:35:36 -0400299 msm->age_scan_duration, lm->global_learn_limit);
300 if (lm->client_pid)
301 vlib_cli_output (vm, "L2MAC events client PID: %d "
302 "Last e-scan time: %.4esec Delay: %.2esec "
303 "Max macs in event: %d",
304 lm->client_pid, msm->evt_scan_duration,
305 msm->event_scan_delay, msm->max_macs_in_event);
306 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
308 if (raw)
309 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700310 BV (format_bihash), &msm->mac_table, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
312 return 0;
313}
314
Billy McFall22aa3e92016-09-09 08:46:40 -0400315/*?
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700316 * This command displays the MAC Address entries of the L2 FIB table.
Billy McFall22aa3e92016-09-09 08:46:40 -0400317 * Output can be filtered to just get the number of MAC Addresses or display
318 * each MAC Address for all bridge domains or just a single bridge domain.
319 *
320 * @cliexpar
321 * Example of how to display the number of MAC Address entries in the L2
322 * FIB table:
323 * @cliexstart{show l2fib}
324 * 3 l2fib entries
325 * @cliexend
326 * Example of how to display all the MAC Address entries in the L2
327 * FIB table:
John Lo7dbd7262018-05-31 10:25:18 -0400328 * @cliexstart{show l2fib all}
Billy McFall22aa3e92016-09-09 08:46:40 -0400329 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
330 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
331 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
332 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
333 * 3 l2fib entries
334 * @cliexend
335?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400336/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
338 .path = "show l2fib",
John Lo7dbd7262018-05-31 10:25:18 -0400339 .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 .function = show_l2fib,
341};
Dave Barach97d8dc22016-08-15 15:31:15 -0400342/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
344
Dave Barach97d8dc22016-08-15 15:31:15 -0400345/* Remove all entries from the l2fib */
346void
Eyal Bari7537e712017-04-27 14:07:55 +0300347l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348{
Dave Barach97d8dc22016-08-15 15:31:15 -0400349 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
Eyal Bari7537e712017-04-27 14:07:55 +0300351 /* Remove all entries */
352 BV (clib_bihash_free) (&mp->mac_table);
353 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
354 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 l2learn_main.global_learn_count = 0;
356}
357
Chris Luke16bcf7d2016-09-01 14:31:46 -0400358/** Clear all entries in L2FIB.
359 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400360 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400362clear_l2fib (vlib_main_t * vm,
363 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364{
Eyal Bari7537e712017-04-27 14:07:55 +0300365 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366 return 0;
367}
368
Billy McFall22aa3e92016-09-09 08:46:40 -0400369/*?
370 * This command clears all the MAC Address entries from the L2 FIB table.
371 *
372 * @cliexpar
373 * Example of how to clear the L2 FIB Table:
374 * @cliexcmd{clear l2fib}
375 * Example to show the L2 FIB Table has been cleared:
376 * @cliexstart{show l2fib verbose}
377 * no l2fib entries
378 * @cliexend
379?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400380/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
382 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400383 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 .function = clear_l2fib,
385};
Dave Barach97d8dc22016-08-15 15:31:15 -0400386/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387
Eyal Bari7537e712017-04-27 14:07:55 +0300388static inline l2fib_seq_num_t
389l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
390{
Eyal Bari7537e712017-04-27 14:07:55 +0300391 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
392 /* *INDENT-OFF* */
393 return (l2fib_seq_num_t) {
Eyal Bari0f360dc2017-06-14 13:11:20 +0300394 .swif = *l2fib_swif_seq_num (sw_if_index),
Eyal Bari7537e712017-04-27 14:07:55 +0300395 .bd = bd_config->seq_num,
396 };
397 /* *INDENT-ON* */
398}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
Dave Barach97d8dc22016-08-15 15:31:15 -0400400/**
401 * Add an entry to the l2fib.
402 * If the entry already exists then overwrite it
403 */
404void
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700405l2fib_add_entry (const u8 * mac, u32 bd_index,
Neale Rannsb54d0812018-09-06 06:22:56 -0700406 u32 sw_if_index, l2fib_entry_result_flags_t flags)
Dave Barach97d8dc22016-08-15 15:31:15 -0400407{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 l2fib_entry_key_t key;
409 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400410 __attribute__ ((unused)) u32 bucket_contents;
John Lo8d00fff2017-08-03 00:35:36 -0400411 l2fib_main_t *fm = &l2fib_main;
412 l2learn_main_t *lm = &l2learn_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400413 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Dave Barach97d8dc22016-08-15 15:31:15 -0400415 /* set up key */
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200416 key.raw = l2fib_make_key (mac, bd_index);
John Lob2e73b12020-01-07 16:35:29 -0500417 kv.key = key.raw;
Dave Barach97d8dc22016-08-15 15:31:15 -0400418
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700419 /* check if entry already exist */
John Lo8d00fff2017-08-03 00:35:36 -0400420 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
421 {
422 /* decrement counter if overwriting a learned mac */
423 result.raw = kv.value;
Neale Rannsb54d0812018-09-06 06:22:56 -0700424 if ((!l2fib_entry_result_is_set_AGE_NOT (&result))
425 && (lm->global_learn_count))
John Lo8d00fff2017-08-03 00:35:36 -0400426 lm->global_learn_count--;
427 }
428
Dave Barach97d8dc22016-08-15 15:31:15 -0400429 /* set up result */
430 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 result.fields.sw_if_index = sw_if_index;
Neale Rannsb54d0812018-09-06 06:22:56 -0700432 result.fields.flags = flags;
433
434 /* no aging for provisioned entry */
435 l2fib_entry_result_set_AGE_NOT (&result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437 kv.value = result.raw;
438
John Lo8d00fff2017-08-03 00:35:36 -0400439 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440}
441
Dave Barach97d8dc22016-08-15 15:31:15 -0400442/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400443 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400444 * The CLI format is:
445 * l2fib add <mac> <bd> <intf> [static] [bvi]
446 * l2fib add <mac> <bd> filter
447 * Note that filter and bvi entries are always static
448 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400450l2fib_add (vlib_main_t * vm,
451 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452{
Dave Barach97d8dc22016-08-15 15:31:15 -0400453 bd_main_t *bdm = &bd_main;
454 vnet_main_t *vnm = vnet_get_main ();
455 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200456 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 u32 bd_id;
458 u32 bd_index;
459 u32 sw_if_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400460 uword *p;
Neale Rannsb54d0812018-09-06 06:22:56 -0700461 l2fib_entry_result_flags_t flags;
462
463 flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200465 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 {
467 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400468 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469 goto done;
470 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400471
472 if (!unformat (input, "%d", &bd_id))
473 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400475 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400477 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
479 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400480 if (!p)
481 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
483 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400484 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 bd_index = p[0];
486
Dave Barach97d8dc22016-08-15 15:31:15 -0400487 if (unformat (input, "filter"))
488 {
John Lo14edd972017-10-31 13:26:02 -0400489 l2fib_add_filter_entry (mac, bd_index);
490 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492
John Lo14edd972017-10-31 13:26:02 -0400493 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
494 {
495 error = clib_error_return (0, "unknown interface `%U'",
496 format_unformat_error, input);
497 goto done;
498 }
499
500 if (unformat (input, "static"))
Neale Rannsb54d0812018-09-06 06:22:56 -0700501 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
John Lo14edd972017-10-31 13:26:02 -0400502 else if (unformat (input, "bvi"))
Neale Rannsb54d0812018-09-06 06:22:56 -0700503 flags |= (L2FIB_ENTRY_RESULT_FLAG_STATIC | L2FIB_ENTRY_RESULT_FLAG_BVI);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
John Lob2fd6cb2017-07-12 19:56:45 -0400505 if (vec_len (l2input_main.configs) <= sw_if_index)
506 {
507 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
508 sw_if_index);
509 goto done;
510 }
511
Neale Rannsb54d0812018-09-06 06:22:56 -0700512 l2fib_add_entry (mac, bd_index, sw_if_index, flags);
Dave Barach97d8dc22016-08-15 15:31:15 -0400513
514done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 return error;
516}
517
Billy McFall22aa3e92016-09-09 08:46:40 -0400518/*?
519 * This command adds a MAC Address entry to the L2 FIB table
520 * of an existing bridge-domain. The MAC Address can be static
521 * or dynamic. This command also allows a filter to be added,
522 * such that packets with given MAC Addresses (source mac or
523 * destination mac match) are dropped.
524 *
525 * @cliexpar
526 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
527 * of a bridge-domain (where 200 is the bridge-domain-id):
528 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
529 * Example of how to add a static MAC Address entry to the L2 FIB table
530 * of a bridge-domain (where 200 is the bridge-domain-id):
531 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
532 * Example of how to add a filter such that a packet with the given MAC
533 * Address will be dropped in a given bridge-domain (where 200 is the
534 * bridge-domain-id):
535 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
536 * Example of show command of the provisioned MAC Addresses and filters:
537 * @cliexstart{show l2fib verbose}
538 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
539 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
540 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
541 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
542 * 3 l2fib entries
543 * @cliexend
544?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400545/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
547 .path = "l2fib add",
Billy McFall22aa3e92016-09-09 08:46:40 -0400548 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549 .function = l2fib_add,
550};
Dave Barach97d8dc22016-08-15 15:31:15 -0400551/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
553
554static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400555l2fib_test_command_fn (vlib_main_t * vm,
556 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557{
Dave Barach97d8dc22016-08-15 15:31:15 -0400558 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200559 u8 mac[6], save_mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 u32 bd_index = 0;
561 u32 sw_if_index = 8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 u32 is_add = 0;
563 u32 is_del = 0;
564 u32 is_check = 0;
565 u32 count = 1;
566 int mac_set = 0;
567 int i;
568
Dave Barach97d8dc22016-08-15 15:31:15 -0400569 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200571 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400572 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400574 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400576 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400578 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400580 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400582 break;
583 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
585 if (mac_set == 0)
586 return clib_error_return (0, "mac not set");
587
588 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400589 return clib_error_return (0,
590 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
Dave Barach178cf492018-11-13 16:34:13 -0500592 clib_memcpy_fast (save_mac, mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
594 if (is_add)
595 {
596 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400597 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700598 l2fib_add_entry (mac, bd_index, sw_if_index,
599 L2FIB_ENTRY_RESULT_FLAG_NONE);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200600 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400601 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602 }
603
604 if (is_check)
605 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400606 BVT (clib_bihash_kv) kv;
607 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
Dave Barach178cf492018-11-13 16:34:13 -0500609 clib_memcpy_fast (mac, save_mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610
611 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400612 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200613 kv.key = l2fib_make_key (mac, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400614 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
615 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200616 clib_warning ("key %U AWOL", format_ethernet_address, mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400617 break;
618 }
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200619 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400620 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 }
622
623 if (is_del)
624 {
Dave Barach178cf492018-11-13 16:34:13 -0500625 clib_memcpy_fast (mac, save_mac, 6);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200626
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400628 {
John Lo7dbd7262018-05-31 10:25:18 -0400629 l2fib_del_entry (mac, bd_index, 0);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200630 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400631 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632 }
633
634 return error;
635}
636
Billy McFall22aa3e92016-09-09 08:46:40 -0400637/*?
638 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
639 * bridge domain (bridge-domain-id of 0) to be modified.
640 *
641 * @cliexpar
642 * @parblock
643 * Example of how to add a set of 4 sequential MAC Address entries to L2
644 * FIB table of the default bridge-domain:
645 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
646 *
647 * Show the set of 4 sequential MAC Address entries that were added:
648 * @cliexstart{show l2fib verbose}
649 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
650 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
651 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
652 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
653 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
654 * 4 l2fib entries
655 * @cliexend
656 *
657 * Example of how to check that the set of 4 sequential MAC Address
658 * entries were added to L2 FIB table of the default
659 * bridge-domain. Used a count of 5 to produce an error:
660 *
661 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
662 * The output of the check command is in the log files. Log file
663 * location may vary based on your OS and Version:
664 *
665 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
666 *
667 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
668 *
669 * Example of how to delete a set of 4 sequential MAC Address entries
670 * from L2 FIB table of the default bridge-domain:
671 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
672 * @endparblock
673?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400674/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675VLIB_CLI_COMMAND (l2fib_test_command, static) = {
676 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400677 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 .function = l2fib_test_command_fn,
679};
Dave Barach97d8dc22016-08-15 15:31:15 -0400680/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681
682
Dave Barach97d8dc22016-08-15 15:31:15 -0400683/**
684 * Delete an entry from the l2fib.
John Lo7dbd7262018-05-31 10:25:18 -0400685 * Return 0 if the entry was deleted, or 1 it was not found or if
686 * sw_if_index is non-zero and does not match that in the entry.
Dave Barach97d8dc22016-08-15 15:31:15 -0400687 */
John Lo7dbd7262018-05-31 10:25:18 -0400688u32
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700689l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400690{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400692 l2fib_main_t *mp = &l2fib_main;
693 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694
Dave Barach97d8dc22016-08-15 15:31:15 -0400695 /* set up key */
John Lo7dbd7262018-05-31 10:25:18 -0400696 kv.key = l2fib_make_key (mac, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697
Dave Barach97d8dc22016-08-15 15:31:15 -0400698 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 return 1;
700
701 result.raw = kv.value;
702
John Lo7dbd7262018-05-31 10:25:18 -0400703 /* check if sw_if_index of entry match */
704 if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index))
705 return 1;
706
Dave Barach97d8dc22016-08-15 15:31:15 -0400707 /* decrement counter if dynamically learned mac */
Neale Rannsb54d0812018-09-06 06:22:56 -0700708 if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) &&
709 (l2learn_main.global_learn_count))
John Lo8d00fff2017-08-03 00:35:36 -0400710 l2learn_main.global_learn_count--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
Dave Barach97d8dc22016-08-15 15:31:15 -0400712 /* Remove entry from hash table */
713 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714 return 0;
715}
716
Dave Barach97d8dc22016-08-15 15:31:15 -0400717/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400718 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400719 * The CLI format is:
720 * l2fib del <mac> <bd-id>
721 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400723l2fib_del (vlib_main_t * vm,
724 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725{
Dave Barach97d8dc22016-08-15 15:31:15 -0400726 bd_main_t *bdm = &bd_main;
727 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200728 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 u32 bd_id;
730 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400731 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200733 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 {
735 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400736 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 goto done;
738 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400739
740 if (!unformat (input, "%d", &bd_id))
741 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400743 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400745 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746
747 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400748 if (!p)
749 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
751 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400752 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753 bd_index = p[0];
754
Dave Barach97d8dc22016-08-15 15:31:15 -0400755 /* Delete the entry */
John Lo7dbd7262018-05-31 10:25:18 -0400756 if (l2fib_del_entry (mac, bd_index, 0))
Dave Barach97d8dc22016-08-15 15:31:15 -0400757 {
758 error = clib_error_return (0, "mac entry not found");
759 goto done;
760 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Dave Barach97d8dc22016-08-15 15:31:15 -0400762done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763 return error;
764}
765
Billy McFall22aa3e92016-09-09 08:46:40 -0400766/*?
767 * This command deletes an existing MAC Address entry from the L2 FIB
768 * table of an existing bridge-domain.
769 *
770 * @cliexpar
771 * Example of how to delete a MAC Address entry from the L2 FIB table of a bridge-domain (where 200 is the bridge-domain-id):
772 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
773?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400774/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
776 .path = "l2fib del",
John Lo7dbd7262018-05-31 10:25:18 -0400777 .short_help = "l2fib del <mac> <bridge-domain-id> []",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 .function = l2fib_del,
779};
Dave Barach97d8dc22016-08-15 15:31:15 -0400780/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781
John Loda1f2c72017-03-24 20:11:15 -0400782/**
783 Kick off ager to scan MACs to age/delete MAC entries
784*/
785void
786l2fib_start_ager_scan (vlib_main_t * vm)
787{
Eyal Bari24db0ec2017-09-27 21:43:51 +0300788 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
John Loda1f2c72017-03-24 20:11:15 -0400789
790 /* check if there is at least one bd with mac aging enabled */
Eyal Bari24db0ec2017-09-27 21:43:51 +0300791 l2_bridge_domain_t *bd_config;
John Loda1f2c72017-03-24 20:11:15 -0400792 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300793 {
John Loda1f2c72017-03-24 20:11:15 -0400794 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300795 {
796 evt = L2_MAC_AGE_PROCESS_EVENT_START;
797 break;
798 }
799 }
John Loda1f2c72017-03-24 20:11:15 -0400800
801 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
Eyal Bari24db0ec2017-09-27 21:43:51 +0300802 evt, 0);
John Loda1f2c72017-03-24 20:11:15 -0400803}
804
805/**
Eyal Bari7537e712017-04-27 14:07:55 +0300806 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400807*/
808void
809l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
810{
Eyal Bari0f360dc2017-06-14 13:11:20 +0300811 *l2fib_swif_seq_num (sw_if_index) += 1;
John Loda1f2c72017-03-24 20:11:15 -0400812 l2fib_start_ager_scan (vm);
813}
814
815/**
Eyal Bari7537e712017-04-27 14:07:55 +0300816 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400817*/
818void
819l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
820{
Eyal Bari7537e712017-04-27 14:07:55 +0300821 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400822 bd_config->seq_num += 1;
823 l2fib_start_ager_scan (vm);
824}
825
826/**
Eyal Bari7537e712017-04-27 14:07:55 +0300827 Flush all non static MACs - flushes all valid BDs
828*/
829void
830l2fib_flush_all_mac (vlib_main_t * vm)
831{
832 l2_bridge_domain_t *bd_config;
833 vec_foreach (bd_config, l2input_main.bd_configs)
834 if (bd_is_valid (bd_config))
835 bd_config->seq_num += 1;
836
837 l2fib_start_ager_scan (vm);
838}
839
840
841/**
John Loda1f2c72017-03-24 20:11:15 -0400842 Flush MACs, except static ones, associated with an interface
843 The CLI format is:
844 l2fib flush-mac interface <if-name>
845*/
846static clib_error_t *
847l2fib_flush_mac_int (vlib_main_t * vm,
848 unformat_input_t * input, vlib_cli_command_t * cmd)
849{
850 vnet_main_t *vnm = vnet_get_main ();
851 clib_error_t *error = 0;
852 u32 sw_if_index;
853
854 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
855 {
856 error = clib_error_return (0, "unknown interface `%U'",
857 format_unformat_error, input);
858 goto done;
859 }
860
861 l2fib_flush_int_mac (vm, sw_if_index);
862
863done:
864 return error;
865}
866
Eyal Bari7537e712017-04-27 14:07:55 +0300867/**
868 Flush all MACs, except static ones
869 The CLI format is:
870 l2fib flush-mac all
871*/
872static clib_error_t *
873l2fib_flush_mac_all (vlib_main_t * vm,
874 unformat_input_t * input, vlib_cli_command_t * cmd)
875{
876 l2fib_flush_all_mac (vm);
877 return 0;
878}
879
880/*?
881 * This command kick off ager to delete all existing MAC Address entries,
882 * except static ones, associated with an interface from the L2 FIB table.
883 *
884 * @cliexpar
885 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
886 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
887?*/
888/* *INDENT-OFF* */
889VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
890 .path = "l2fib flush-mac all",
891 .short_help = "l2fib flush-mac all",
892 .function = l2fib_flush_mac_all,
893};
894/* *INDENT-ON* */
895
John Loda1f2c72017-03-24 20:11:15 -0400896/*?
897 * This command kick off ager to delete all existing MAC Address entries,
898 * except static ones, associated with an interface from the L2 FIB table.
899 *
900 * @cliexpar
901 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
902 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
903?*/
904/* *INDENT-OFF* */
905VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
906 .path = "l2fib flush-mac interface",
907 .short_help = "l2fib flush-mac interface <if-name>",
908 .function = l2fib_flush_mac_int,
909};
910/* *INDENT-ON* */
911
912/**
913 Flush bridge-domain MACs except static ones.
914 The CLI format is:
915 l2fib flush-mac bridge-domain <bd-id>
916*/
917static clib_error_t *
918l2fib_flush_mac_bd (vlib_main_t * vm,
919 unformat_input_t * input, vlib_cli_command_t * cmd)
920{
921 bd_main_t *bdm = &bd_main;
922 clib_error_t *error = 0;
923 u32 bd_index, bd_id;
924 uword *p;
925
926 if (!unformat (input, "%d", &bd_id))
927 {
928 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
929 format_unformat_error, input);
930 goto done;
931 }
932
933 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
934 if (p)
935 bd_index = *p;
936 else
937 return clib_error_return (0, "No such bridge domain %d", bd_id);
938
939 l2fib_flush_bd_mac (vm, bd_index);
940
941done:
942 return error;
943}
944
945/*?
946 * This command kick off ager to delete all existing MAC Address entries,
947 * except static ones, in a bridge domain from the L2 FIB table.
948 *
949 * @cliexpar
950 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
951 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
952?*/
953/* *INDENT-OFF* */
954VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
955 .path = "l2fib flush-mac bridge-domain",
956 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
957 .function = l2fib_flush_mac_bd,
958};
959/* *INDENT-ON* */
960
Eyal Bariafc47aa2017-04-20 14:45:17 +0300961clib_error_t *
962l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
963{
964 l2_input_config_t *config = l2input_intf_config (sw_if_index);
965 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
966 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
967 return 0;
968}
969
970VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971
Dave Barach97d8dc22016-08-15 15:31:15 -0400972BVT (clib_bihash) * get_mac_table (void)
973{
974 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 return &mp->mac_table;
976}
977
John Lo8d00fff2017-08-03 00:35:36 -0400978static_always_inline void *
979allocate_mac_evt_buf (u32 client, u32 client_index)
980{
981 l2fib_main_t *fm = &l2fib_main;
982 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
983 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
984 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
985 mp->pid = htonl (client);
986 mp->client_index = client_index;
987 return mp;
988}
989
990static_always_inline f64
991l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
992{
993 l2fib_main_t *fm = &l2fib_main;
994 l2learn_main_t *lm = &l2learn_main;
995
996 BVT (clib_bihash) * h = &fm->mac_table;
997 int i, j, k;
998 f64 last_start = start_time;
999 f64 accum_t = 0;
1000 f64 delta_t = 0;
1001 u32 evt_idx = 0;
1002 u32 learn_count = 0;
1003 u32 client = lm->client_pid;
1004 u32 cl_idx = lm->client_index;
1005 vl_api_l2_macs_event_t *mp = 0;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001006 vl_api_registration_t *reg = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001007
Dave Barach32dcd3b2019-07-08 12:25:38 -04001008 /* Don't scan the l2 fib if it hasn't been instantiated yet */
1009 if (alloc_arena (h) == 0)
1010 return 0.0;
1011
John Lo8d00fff2017-08-03 00:35:36 -04001012 if (client)
1013 {
1014 mp = allocate_mac_evt_buf (client, cl_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001015 reg = vl_api_client_index_to_registration (lm->client_index);
John Lo8d00fff2017-08-03 00:35:36 -04001016 }
1017
1018 for (i = 0; i < h->nbuckets; i++)
1019 {
1020 /* allow no more than 20us without a pause */
1021 delta_t = vlib_time_now (vm) - last_start;
1022 if (delta_t > 20e-6)
1023 {
1024 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
1025 last_start = vlib_time_now (vm);
1026 accum_t += delta_t;
1027 }
1028
1029 if (i < (h->nbuckets - 3))
1030 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001031 BVT (clib_bihash_bucket) * b =
1032 BV (clib_bihash_get_bucket) (h, i + 3);
John Lo8d00fff2017-08-03 00:35:36 -04001033 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
Dave Barach16e4a4a2020-04-16 12:00:14 -04001034 b = BV (clib_bihash_get_bucket) (h, i + 1);
1035 if (!BV (clib_bihash_bucket_is_empty) (b))
John Lo8d00fff2017-08-03 00:35:36 -04001036 {
1037 BVT (clib_bihash_value) * v =
1038 BV (clib_bihash_get_value) (h, b->offset);
1039 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1040 }
1041 }
1042
Dave Barach16e4a4a2020-04-16 12:00:14 -04001043 BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i);
1044 if (BV (clib_bihash_bucket_is_empty) (b))
John Lo8d00fff2017-08-03 00:35:36 -04001045 continue;
1046 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1047 for (j = 0; j < (1 << b->log2_pages); j++)
1048 {
1049 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1050 {
1051 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1052 continue;
1053
1054 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1055 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1056
Neale Rannsb54d0812018-09-06 06:22:56 -07001057 if (!l2fib_entry_result_is_set_AGE_NOT (&result))
John Lo8d00fff2017-08-03 00:35:36 -04001058 learn_count++;
1059
John Lo8d00fff2017-08-03 00:35:36 -04001060 if (client)
1061 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001062 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1063 {
1064 /* event message full, send it and start a new one */
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001065 if (reg && vl_api_can_send_msg (reg))
Eyal Bari24db0ec2017-09-27 21:43:51 +03001066 {
1067 mp->n_macs = htonl (evt_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001068 vl_api_send_msg (reg, (u8 *) mp);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001069 mp = allocate_mac_evt_buf (client, cl_idx);
1070 }
1071 else
1072 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001073 if (reg)
Dave Barach59b25652017-09-10 15:04:27 -04001074 clib_warning ("MAC event to pid %d queue stuffed!"
1075 " %d MAC entries lost", client,
1076 evt_idx);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001077 }
1078 evt_idx = 0;
1079 }
1080
Neale Rannsb54d0812018-09-06 06:22:56 -07001081 if (l2fib_entry_result_is_set_LRN_EVT (&result))
John Lo8d00fff2017-08-03 00:35:36 -04001082 {
1083 /* copy mac entry to event msg */
Dave Barach178cf492018-11-13 16:34:13 -05001084 clib_memcpy_fast (mp->mac[evt_idx].mac_addr,
1085 key.fields.mac, 6);
Neale Rannsb54d0812018-09-06 06:22:56 -07001086 mp->mac[evt_idx].action =
1087 l2fib_entry_result_is_set_LRN_MOV (&result) ?
Jakub Grajciar145e3302019-10-24 13:52:42 +02001088 (vl_api_mac_event_action_t) MAC_EVENT_ACTION_MOVE
1089 : (vl_api_mac_event_action_t) MAC_EVENT_ACTION_ADD;
1090 mp->mac[evt_idx].action =
1091 htonl (mp->mac[evt_idx].action);
John Lo8d00fff2017-08-03 00:35:36 -04001092 mp->mac[evt_idx].sw_if_index =
1093 htonl (result.fields.sw_if_index);
John Loe23c99e2018-03-13 21:53:18 -04001094 /* clear event bits and update mac entry */
Neale Rannsb54d0812018-09-06 06:22:56 -07001095 l2fib_entry_result_clear_LRN_EVT (&result);
1096 l2fib_entry_result_clear_LRN_MOV (&result);
John Lo8d00fff2017-08-03 00:35:36 -04001097 BVT (clib_bihash_kv) kv;
1098 kv.key = key.raw;
1099 kv.value = result.raw;
1100 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1101 evt_idx++;
1102 continue; /* skip aging */
1103 }
1104 }
1105
Neale Rannsb54d0812018-09-06 06:22:56 -07001106 if (event_only || l2fib_entry_result_is_set_AGE_NOT (&result))
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001107 continue; /* skip aging - static_mac always age_not */
John Lo8d00fff2017-08-03 00:35:36 -04001108
1109 /* start aging processing */
1110 u32 bd_index = key.fields.bd_index;
1111 u32 sw_if_index = result.fields.sw_if_index;
1112 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1113 if (result.fields.sn.as_u16 != sn)
1114 goto age_out; /* stale mac */
1115
1116 l2_bridge_domain_t *bd_config =
1117 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1118
1119 if (bd_config->mac_age == 0)
1120 continue; /* skip aging */
1121
1122 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1123 delta += delta < 0 ? 256 : 0;
1124
1125 if (delta < bd_config->mac_age)
1126 continue; /* still valid */
1127
1128 age_out:
1129 if (client)
1130 {
1131 /* copy mac entry to event msg */
Dave Barach178cf492018-11-13 16:34:13 -05001132 clib_memcpy_fast (mp->mac[evt_idx].mac_addr, key.fields.mac,
1133 6);
Jakub Grajciar145e3302019-10-24 13:52:42 +02001134 mp->mac[evt_idx].action =
1135 (vl_api_mac_event_action_t) MAC_EVENT_ACTION_DELETE;
1136 mp->mac[evt_idx].action = htonl (mp->mac[evt_idx].action);
John Lo8d00fff2017-08-03 00:35:36 -04001137 mp->mac[evt_idx].sw_if_index =
1138 htonl (result.fields.sw_if_index);
1139 evt_idx++;
1140 }
1141 /* delete mac entry */
1142 BVT (clib_bihash_kv) kv;
1143 kv.key = key.raw;
1144 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1145 learn_count--;
Dave Barach28374ca2018-08-07 12:46:18 -04001146 /*
1147 * Note: we may have just freed the bucket's backing
1148 * storage, so check right here...
1149 */
Dave Barach16e4a4a2020-04-16 12:00:14 -04001150 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barach28374ca2018-08-07 12:46:18 -04001151 goto doublebreak;
John Lo8d00fff2017-08-03 00:35:36 -04001152 }
1153 v++;
1154 }
Dave Barach28374ca2018-08-07 12:46:18 -04001155 doublebreak:
1156 ;
John Lo8d00fff2017-08-03 00:35:36 -04001157 }
1158
1159 /* keep learn count consistent */
1160 l2learn_main.global_learn_count = learn_count;
1161
1162 if (mp)
1163 {
1164 /* send any outstanding mac event message else free message buffer */
1165 if (evt_idx)
1166 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001167 if (reg && vl_api_can_send_msg (reg))
John Lo8d00fff2017-08-03 00:35:36 -04001168 {
1169 mp->n_macs = htonl (evt_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001170 vl_api_send_msg (reg, (u8 *) mp);
John Lo8d00fff2017-08-03 00:35:36 -04001171 }
1172 else
1173 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001174 if (reg)
Dave Barach59b25652017-09-10 15:04:27 -04001175 clib_warning ("MAC event to pid %d queue stuffed!"
1176 " %d MAC entries lost", client, evt_idx);
John Lo8d00fff2017-08-03 00:35:36 -04001177 vl_msg_api_free (mp);
1178 }
1179 }
1180 else
1181 vl_msg_api_free (mp);
1182 }
1183 return delta_t + accum_t;
1184}
1185
Damjan Mariond171d482016-12-05 14:16:38 +01001186static uword
1187l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1188 vlib_frame_t * f)
1189{
1190 uword event_type, *event_data = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001191 l2fib_main_t *fm = &l2fib_main;
1192 l2learn_main_t *lm = &l2learn_main;
Damjan Mariond171d482016-12-05 14:16:38 +01001193 bool enabled = 0;
John Lo7f358b32018-04-28 01:19:24 -04001194 f64 start_time, next_age_scan_time = CLIB_TIME_MAX;
Damjan Mariond171d482016-12-05 14:16:38 +01001195
1196 while (1)
1197 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001198 if (lm->client_pid)
1199 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1200 else if (enabled)
John Lo8d00fff2017-08-03 00:35:36 -04001201 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001202 f64 t = next_age_scan_time - vlib_time_now (vm);
1203 vlib_process_wait_for_event_or_clock (vm, t);
John Lo8d00fff2017-08-03 00:35:36 -04001204 }
Damjan Mariond171d482016-12-05 14:16:38 +01001205 else
1206 vlib_process_wait_for_event (vm);
1207
1208 event_type = vlib_process_get_events (vm, &event_data);
1209 vec_reset_length (event_data);
1210
John Lo8d00fff2017-08-03 00:35:36 -04001211 start_time = vlib_time_now (vm);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001212 enum
1213 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
John Lo8d00fff2017-08-03 00:35:36 -04001214
Damjan Mariond171d482016-12-05 14:16:38 +01001215 switch (event_type)
1216 {
John Lo8d00fff2017-08-03 00:35:36 -04001217 case ~0: /* timer expired */
Eyal Bari24db0ec2017-09-27 21:43:51 +03001218 if (lm->client_pid != 0 && start_time < next_age_scan_time)
John Lo8d00fff2017-08-03 00:35:36 -04001219 scan = SCAN_MAC_EVENT;
Damjan Mariond171d482016-12-05 14:16:38 +01001220 break;
John Lo8d00fff2017-08-03 00:35:36 -04001221
Damjan Mariond171d482016-12-05 14:16:38 +01001222 case L2_MAC_AGE_PROCESS_EVENT_START:
1223 enabled = 1;
1224 break;
John Lo8d00fff2017-08-03 00:35:36 -04001225
Damjan Mariond171d482016-12-05 14:16:38 +01001226 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1227 enabled = 0;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001228 scan = SCAN_DISABLE;
1229 break;
John Lo8d00fff2017-08-03 00:35:36 -04001230
John Loda1f2c72017-03-24 20:11:15 -04001231 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
John Loda1f2c72017-03-24 20:11:15 -04001232 break;
John Lo8d00fff2017-08-03 00:35:36 -04001233
Damjan Mariond171d482016-12-05 14:16:38 +01001234 default:
1235 ASSERT (0);
1236 }
Eyal Bari7537e712017-04-27 14:07:55 +03001237
John Lo8d00fff2017-08-03 00:35:36 -04001238 if (scan == SCAN_MAC_EVENT)
1239 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1240 else
Eyal Bari24db0ec2017-09-27 21:43:51 +03001241 {
1242 if (scan == SCAN_MAC_AGE)
1243 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1244 if (scan == SCAN_DISABLE)
1245 {
1246 l2fib_main.age_scan_duration = 0;
1247 l2fib_main.evt_scan_duration = 0;
1248 }
1249 /* schedule next scan */
1250 if (enabled)
1251 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1252 else
John Lo7f358b32018-04-28 01:19:24 -04001253 next_age_scan_time = CLIB_TIME_MAX;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001254 }
Damjan Mariond171d482016-12-05 14:16:38 +01001255 }
1256 return 0;
1257}
1258
1259/* *INDENT-OFF* */
1260VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1261 .function = l2fib_mac_age_scanner_process,
1262 .type = VLIB_NODE_TYPE_PROCESS,
1263 .name = "l2fib-mac-age-scanner-process",
1264};
1265/* *INDENT-ON* */
1266
Dave Barach97d8dc22016-08-15 15:31:15 -04001267clib_error_t *
1268l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269{
Dave Barach97d8dc22016-08-15 15:31:15 -04001270 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001271 l2fib_entry_key_t test_key;
1272 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001273
Ed Warnickecb9cada2015-12-08 15:45:58 -07001274 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001275 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276
Dave Barach97d8dc22016-08-15 15:31:15 -04001277 /* Create the hash table */
1278 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1279 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001280
Dave Barach97d8dc22016-08-15 15:31:15 -04001281 /* verify the key constructor is good, since it is endian-sensitive */
Dave Barachb7b92992018-10-17 10:38:51 -04001282 clib_memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283 test_mac[0] = 0x11;
1284 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001285 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286 ASSERT (test_key.fields.mac[0] == 0x11);
1287 ASSERT (test_key.fields.bd_index == 0x1234);
1288
1289 return 0;
1290}
1291
1292VLIB_INIT_FUNCTION (l2fib_init);
1293
Dave Barach97d8dc22016-08-15 15:31:15 -04001294/*
1295 * fd.io coding-style-patch-verification: ON
1296 *
1297 * Local Variables:
1298 * eval: (c-set-style "gnu")
1299 * End:
1300 */