blob: 085ac8c3256af1800d318bbc6ad3579c935969f7 [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
Mohsin Kazmi57938f62017-10-27 21:28:07 +020057static void
58incr_mac_address (u8 * mac)
59{
60 u64 tmp = *((u64 *) mac);
61 tmp = clib_net_to_host_u64 (tmp);
62 tmp += 1 << 16; /* skip unused (least significant) octets */
63 tmp = clib_host_to_net_u64 (tmp);
64
65 clib_memcpy (mac, &tmp, 6);
66}
67
Dave Barach97d8dc22016-08-15 15:31:15 -040068/** Format sw_if_index. If the value is ~0, use the text "N/A" */
69u8 *
70format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070071{
Dave Barach97d8dc22016-08-15 15:31:15 -040072 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 u32 sw_if_index = va_arg (*args, u32);
74 if (sw_if_index == ~0)
75 return format (s, "N/A");
Eyal Barib823df52017-06-12 17:07:22 +030076
77 vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
78 if (!swif)
Eyal Bari0f360dc2017-06-14 13:11:20 +030079 return format (s, "Stale");
Eyal Barib823df52017-06-12 17:07:22 +030080
81 return format (s, "%U", format_vnet_sw_interface_name, vnm,
82 vnet_get_sw_interface_safe (vnm, sw_if_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -070083}
84
Neale Ranns99a3c6c2018-08-07 02:54:31 -070085typedef struct l2fib_dump_walk_ctx_t_
86{
87 u32 bd_index;
88 l2fib_entry_key_t *l2fe_key;
89 l2fib_entry_result_t *l2fe_res;
90} l2fib_dump_walk_ctx_t;
91
92static void
93l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
94{
95 l2fib_dump_walk_ctx_t *ctx = arg;
96 l2fib_entry_result_t result;
97 l2fib_entry_key_t key;
98
99 key.raw = kvp->key;
100 result.raw = kvp->value;
101
102 if ((ctx->bd_index == ~0) || (ctx->bd_index == key.fields.bd_index))
103 {
104 vec_add1 (ctx->l2fe_key, key);
105 vec_add1 (ctx->l2fe_res, result);
106 }
107}
108
Dave Barach97d8dc22016-08-15 15:31:15 -0400109void
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700110l2fib_table_dump (u32 bd_index,
111 l2fib_entry_key_t ** l2fe_key,
Dave Barach97d8dc22016-08-15 15:31:15 -0400112 l2fib_entry_result_t ** l2fe_res)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113{
Dave Barach97d8dc22016-08-15 15:31:15 -0400114 l2fib_main_t *msm = &l2fib_main;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700115 l2fib_dump_walk_ctx_t ctx = {
116 .bd_index = bd_index,
117 };
118
119 BV (clib_bihash_foreach_key_value_pair)
120 (&msm->mac_table, l2fib_dump_walk_cb, &ctx);
121
122 *l2fe_key = ctx.l2fe_key;
123 *l2fe_res = ctx.l2fe_res;
124}
125
126typedef struct l2fib_show_walk_ctx_t_
127{
128 u8 first_entry;
129 u8 verbose;
130 vlib_main_t *vm;
131 vnet_main_t *vnm;
132 u32 total_entries;
133 u32 bd_index;
134 u8 learn;
135 u8 add;
136 u8 now;
137} l2fib_show_walk_ctx_t;
138
139static void
140l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
141{
142 l2fib_show_walk_ctx_t *ctx = arg;
143 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 l2fib_entry_result_t result;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700145 l2fib_entry_key_t key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700147 if (ctx->verbose && ctx->first_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700149 ctx->first_entry = 0;
150 vlib_cli_output (ctx->vm,
151 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
152 "Mac-Address", "BD-Idx", "If-Idx",
153 "BSN-ISN", "Age(min)", "static", "filter",
154 "bvi", "Interface-Name");
155 }
156
157 key.raw = kvp->key;
158 result.raw = kvp->value;
159 ctx->total_entries++;
160
161 if (ctx->verbose &&
162 ((ctx->bd_index >> 31) || (ctx->bd_index == key.fields.bd_index)))
163 {
164 u8 *s = NULL;
165
166 if (ctx->learn && result.fields.age_not)
167 return; /* skip provisioned macs */
168
169 if (ctx->add && !result.fields.age_not)
170 return; /* skip learned macs */
171
172 bd_config = vec_elt_at_index (l2input_main.bd_configs,
173 key.fields.bd_index);
174
175 if (result.fields.age_not)
176 s = format (s, "no");
177 else if (bd_config->mac_age == 0)
178 s = format (s, "-");
179 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400180 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700181 i16 delta = ctx->now - result.fields.timestamp;
182 delta += delta < 0 ? 256 : 0;
183 s = format (s, "%d", delta);
Dave Barach97d8dc22016-08-15 15:31:15 -0400184 }
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700185
186 vlib_cli_output (ctx->vm,
187 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
188 format_ethernet_address, key.fields.mac,
189 key.fields.bd_index,
190 result.fields.sw_if_index == ~0
191 ? -1 : result.fields.sw_if_index,
192 result.fields.sn.bd, result.fields.sn.swif,
193 s, result.fields.static_mac ? "*" : "-",
194 result.fields.filter ? "*" : "-",
195 result.fields.bvi ? "*" : "-",
196 format_vnet_sw_if_index_name_with_NA,
197 ctx->vnm, result.fields.sw_if_index);
198 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 }
200}
201
Chris Luke16bcf7d2016-09-01 14:31:46 -0400202/** Display the contents of the l2fib. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400204show_l2fib (vlib_main_t * vm,
205 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206{
Dave Barach97d8dc22016-08-15 15:31:15 -0400207 bd_main_t *bdm = &bd_main;
208 l2fib_main_t *msm = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209 u8 raw = 0;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700210 u32 bd_id;
211 l2fib_show_walk_ctx_t ctx = {
212 .first_entry = 1,
213 .bd_index = ~0,
214 .now = (u8) (vlib_time_now (vm) / 60),
215 .vm = vm,
216 .vnm = msm->vnet_main,
217 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
John Lo7dbd7262018-05-31 10:25:18 -0400219 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
John Lo8d00fff2017-08-03 00:35:36 -0400220 {
John Lo7dbd7262018-05-31 10:25:18 -0400221 if (unformat (input, "raw"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400222 {
John Lo7dbd7262018-05-31 10:25:18 -0400223 raw = 1;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700224 ctx.verbose = 0;
John Lo7dbd7262018-05-31 10:25:18 -0400225 break;
226 }
227 else if (unformat (input, "verbose"))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700228 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400229 else if (unformat (input, "all"))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700230 ctx.verbose = 1;
231 else if (unformat (input, "bd_index %d", &ctx.bd_index))
232 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400233 else if (unformat (input, "learn"))
234 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700235 ctx.add = 0;
236 ctx.learn = 1;
237 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400238 }
239 else if (unformat (input, "add"))
240 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700241 ctx.learn = 0;
242 ctx.add = 1;
243 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400244 }
245 else if (unformat (input, "bd_id %d", &bd_id))
246 {
247 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
248 if (p)
249 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700250 ctx.verbose = 1;
251 ctx.bd_index = p[0];
John Lo7dbd7262018-05-31 10:25:18 -0400252 }
253 else
254 return clib_error_return (0,
255 "bridge domain id %d doesn't exist\n",
256 bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400257 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 else
John Lo7dbd7262018-05-31 10:25:18 -0400259 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 }
261
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700262 BV (clib_bihash_foreach_key_value_pair)
263 (&msm->mac_table, l2fib_show_walk_cb, &ctx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700265 if (ctx.total_entries == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 vlib_cli_output (vm, "no l2fib entries");
267 else
John Lo8d00fff2017-08-03 00:35:36 -0400268 {
269 l2learn_main_t *lm = &l2learn_main;
270 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
271 "Last scan time: %.4esec Learn limit: %d ",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700272 ctx.total_entries, lm->global_learn_count,
John Lo8d00fff2017-08-03 00:35:36 -0400273 msm->age_scan_duration, lm->global_learn_limit);
274 if (lm->client_pid)
275 vlib_cli_output (vm, "L2MAC events client PID: %d "
276 "Last e-scan time: %.4esec Delay: %.2esec "
277 "Max macs in event: %d",
278 lm->client_pid, msm->evt_scan_duration,
279 msm->event_scan_delay, msm->max_macs_in_event);
280 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
282 if (raw)
283 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700284 BV (format_bihash), &msm->mac_table, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
286 return 0;
287}
288
Billy McFall22aa3e92016-09-09 08:46:40 -0400289/*?
290 * This command dispays the MAC Address entries of the L2 FIB table.
291 * Output can be filtered to just get the number of MAC Addresses or display
292 * each MAC Address for all bridge domains or just a single bridge domain.
293 *
294 * @cliexpar
295 * Example of how to display the number of MAC Address entries in the L2
296 * FIB table:
297 * @cliexstart{show l2fib}
298 * 3 l2fib entries
299 * @cliexend
300 * Example of how to display all the MAC Address entries in the L2
301 * FIB table:
John Lo7dbd7262018-05-31 10:25:18 -0400302 * @cliexstart{show l2fib all}
Billy McFall22aa3e92016-09-09 08:46:40 -0400303 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
304 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
305 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
306 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
307 * 3 l2fib entries
308 * @cliexend
309?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400310/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
312 .path = "show l2fib",
John Lo7dbd7262018-05-31 10:25:18 -0400313 .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 .function = show_l2fib,
315};
Dave Barach97d8dc22016-08-15 15:31:15 -0400316/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
318
Dave Barach97d8dc22016-08-15 15:31:15 -0400319/* Remove all entries from the l2fib */
320void
Eyal Bari7537e712017-04-27 14:07:55 +0300321l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322{
Dave Barach97d8dc22016-08-15 15:31:15 -0400323 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Eyal Bari7537e712017-04-27 14:07:55 +0300325 /* Remove all entries */
326 BV (clib_bihash_free) (&mp->mac_table);
327 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
328 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 l2learn_main.global_learn_count = 0;
330}
331
Chris Luke16bcf7d2016-09-01 14:31:46 -0400332/** Clear all entries in L2FIB.
333 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400334 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400336clear_l2fib (vlib_main_t * vm,
337 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338{
Eyal Bari7537e712017-04-27 14:07:55 +0300339 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 return 0;
341}
342
Billy McFall22aa3e92016-09-09 08:46:40 -0400343/*?
344 * This command clears all the MAC Address entries from the L2 FIB table.
345 *
346 * @cliexpar
347 * Example of how to clear the L2 FIB Table:
348 * @cliexcmd{clear l2fib}
349 * Example to show the L2 FIB Table has been cleared:
350 * @cliexstart{show l2fib verbose}
351 * no l2fib entries
352 * @cliexend
353?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400354/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
356 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400357 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 .function = clear_l2fib,
359};
Dave Barach97d8dc22016-08-15 15:31:15 -0400360/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
Eyal Bari7537e712017-04-27 14:07:55 +0300362static inline l2fib_seq_num_t
363l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
364{
Eyal Bari7537e712017-04-27 14:07:55 +0300365 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
366 /* *INDENT-OFF* */
367 return (l2fib_seq_num_t) {
Eyal Bari0f360dc2017-06-14 13:11:20 +0300368 .swif = *l2fib_swif_seq_num (sw_if_index),
Eyal Bari7537e712017-04-27 14:07:55 +0300369 .bd = bd_config->seq_num,
370 };
371 /* *INDENT-ON* */
372}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Dave Barach97d8dc22016-08-15 15:31:15 -0400374/**
375 * Add an entry to the l2fib.
376 * If the entry already exists then overwrite it
377 */
378void
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200379l2fib_add_entry (u8 * mac, u32 bd_index,
John Lo8d00fff2017-08-03 00:35:36 -0400380 u32 sw_if_index, u8 static_mac, u8 filter_mac, u8 bvi_mac)
Dave Barach97d8dc22016-08-15 15:31:15 -0400381{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 l2fib_entry_key_t key;
383 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400384 __attribute__ ((unused)) u32 bucket_contents;
John Lo8d00fff2017-08-03 00:35:36 -0400385 l2fib_main_t *fm = &l2fib_main;
386 l2learn_main_t *lm = &l2learn_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400387 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Dave Barach97d8dc22016-08-15 15:31:15 -0400389 /* set up key */
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200390 key.raw = l2fib_make_key (mac, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400391
John Lo8d00fff2017-08-03 00:35:36 -0400392 /* check if entry alread exist */
393 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
394 {
395 /* decrement counter if overwriting a learned mac */
396 result.raw = kv.value;
397 if ((result.fields.age_not == 0) && (lm->global_learn_count))
398 lm->global_learn_count--;
399 }
400
Dave Barach97d8dc22016-08-15 15:31:15 -0400401 /* set up result */
402 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 result.fields.sw_if_index = sw_if_index;
404 result.fields.static_mac = static_mac;
405 result.fields.filter = filter_mac;
406 result.fields.bvi = bvi_mac;
John Lo8d00fff2017-08-03 00:35:36 -0400407 result.fields.age_not = 1; /* no aging for provisioned entry */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
409 kv.key = key.raw;
410 kv.value = result.raw;
411
John Lo8d00fff2017-08-03 00:35:36 -0400412 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413}
414
Dave Barach97d8dc22016-08-15 15:31:15 -0400415/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400416 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400417 * The CLI format is:
418 * l2fib add <mac> <bd> <intf> [static] [bvi]
419 * l2fib add <mac> <bd> filter
420 * Note that filter and bvi entries are always static
421 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400423l2fib_add (vlib_main_t * vm,
424 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425{
Dave Barach97d8dc22016-08-15 15:31:15 -0400426 bd_main_t *bdm = &bd_main;
427 vnet_main_t *vnm = vnet_get_main ();
428 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200429 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 u32 bd_id;
431 u32 bd_index;
432 u32 sw_if_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 u32 static_mac = 0;
434 u32 bvi_mac = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400435 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200437 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 {
439 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400440 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441 goto done;
442 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400443
444 if (!unformat (input, "%d", &bd_id))
445 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400447 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400449 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
451 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400452 if (!p)
453 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
455 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400456 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 bd_index = p[0];
458
Dave Barach97d8dc22016-08-15 15:31:15 -0400459 if (unformat (input, "filter"))
460 {
John Lo14edd972017-10-31 13:26:02 -0400461 l2fib_add_filter_entry (mac, bd_index);
462 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464
John Lo14edd972017-10-31 13:26:02 -0400465 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
466 {
467 error = clib_error_return (0, "unknown interface `%U'",
468 format_unformat_error, input);
469 goto done;
470 }
471
472 if (unformat (input, "static"))
473 {
474 static_mac = 1;
475 }
476 else if (unformat (input, "bvi"))
477 {
478 bvi_mac = 1;
479 static_mac = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400480 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481
John Lob2fd6cb2017-07-12 19:56:45 -0400482 if (vec_len (l2input_main.configs) <= sw_if_index)
483 {
484 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
485 sw_if_index);
486 goto done;
487 }
488
John Lo14edd972017-10-31 13:26:02 -0400489 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400490
491done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 return error;
493}
494
Billy McFall22aa3e92016-09-09 08:46:40 -0400495/*?
496 * This command adds a MAC Address entry to the L2 FIB table
497 * of an existing bridge-domain. The MAC Address can be static
498 * or dynamic. This command also allows a filter to be added,
499 * such that packets with given MAC Addresses (source mac or
500 * destination mac match) are dropped.
501 *
502 * @cliexpar
503 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
504 * of a bridge-domain (where 200 is the bridge-domain-id):
505 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
506 * Example of how to add a static MAC Address entry to the L2 FIB table
507 * of a bridge-domain (where 200 is the bridge-domain-id):
508 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
509 * Example of how to add a filter such that a packet with the given MAC
510 * Address will be dropped in a given bridge-domain (where 200 is the
511 * bridge-domain-id):
512 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
513 * Example of show command of the provisioned MAC Addresses and filters:
514 * @cliexstart{show l2fib verbose}
515 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
516 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
517 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
518 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
519 * 3 l2fib entries
520 * @cliexend
521?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400522/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
524 .path = "l2fib add",
Billy McFall22aa3e92016-09-09 08:46:40 -0400525 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 .function = l2fib_add,
527};
Dave Barach97d8dc22016-08-15 15:31:15 -0400528/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
530
531static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400532l2fib_test_command_fn (vlib_main_t * vm,
533 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534{
Dave Barach97d8dc22016-08-15 15:31:15 -0400535 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200536 u8 mac[6], save_mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 u32 bd_index = 0;
538 u32 sw_if_index = 8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 u32 bvi_mac = 0;
540 u32 is_add = 0;
541 u32 is_del = 0;
542 u32 is_check = 0;
543 u32 count = 1;
544 int mac_set = 0;
545 int i;
546
Dave Barach97d8dc22016-08-15 15:31:15 -0400547 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200549 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400550 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400552 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400554 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400556 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400558 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400560 break;
561 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
563 if (mac_set == 0)
564 return clib_error_return (0, "mac not set");
565
566 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400567 return clib_error_return (0,
568 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200570 clib_memcpy (save_mac, mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571
572 if (is_add)
573 {
574 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400575 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200576 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, *mac, bvi_mac);
577 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400578 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579 }
580
581 if (is_check)
582 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400583 BVT (clib_bihash_kv) kv;
584 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200586 clib_memcpy (mac, save_mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
588 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400589 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200590 kv.key = l2fib_make_key (mac, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400591 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
592 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200593 clib_warning ("key %U AWOL", format_ethernet_address, mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400594 break;
595 }
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200596 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400597 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 }
599
600 if (is_del)
601 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200602 clib_memcpy (mac, save_mac, 6);
603
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400605 {
John Lo7dbd7262018-05-31 10:25:18 -0400606 l2fib_del_entry (mac, bd_index, 0);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200607 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400608 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 }
610
611 return error;
612}
613
Billy McFall22aa3e92016-09-09 08:46:40 -0400614/*?
615 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
616 * bridge domain (bridge-domain-id of 0) to be modified.
617 *
618 * @cliexpar
619 * @parblock
620 * Example of how to add a set of 4 sequential MAC Address entries to L2
621 * FIB table of the default bridge-domain:
622 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
623 *
624 * Show the set of 4 sequential MAC Address entries that were added:
625 * @cliexstart{show l2fib verbose}
626 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
627 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
628 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
629 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
630 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
631 * 4 l2fib entries
632 * @cliexend
633 *
634 * Example of how to check that the set of 4 sequential MAC Address
635 * entries were added to L2 FIB table of the default
636 * bridge-domain. Used a count of 5 to produce an error:
637 *
638 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
639 * The output of the check command is in the log files. Log file
640 * location may vary based on your OS and Version:
641 *
642 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
643 *
644 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
645 *
646 * Example of how to delete a set of 4 sequential MAC Address entries
647 * from L2 FIB table of the default bridge-domain:
648 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
649 * @endparblock
650?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400651/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652VLIB_CLI_COMMAND (l2fib_test_command, static) = {
653 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400654 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655 .function = l2fib_test_command_fn,
656};
Dave Barach97d8dc22016-08-15 15:31:15 -0400657/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
659
Dave Barach97d8dc22016-08-15 15:31:15 -0400660/**
661 * Delete an entry from the l2fib.
John Lo7dbd7262018-05-31 10:25:18 -0400662 * Return 0 if the entry was deleted, or 1 it was not found or if
663 * sw_if_index is non-zero and does not match that in the entry.
Dave Barach97d8dc22016-08-15 15:31:15 -0400664 */
John Lo7dbd7262018-05-31 10:25:18 -0400665u32
666l2fib_del_entry (u8 * mac, u32 bd_index, u32 sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400667{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400669 l2fib_main_t *mp = &l2fib_main;
670 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Dave Barach97d8dc22016-08-15 15:31:15 -0400672 /* set up key */
John Lo7dbd7262018-05-31 10:25:18 -0400673 kv.key = l2fib_make_key (mac, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674
Dave Barach97d8dc22016-08-15 15:31:15 -0400675 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676 return 1;
677
678 result.raw = kv.value;
679
John Lo7dbd7262018-05-31 10:25:18 -0400680 /* check if sw_if_index of entry match */
681 if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index))
682 return 1;
683
Dave Barach97d8dc22016-08-15 15:31:15 -0400684 /* decrement counter if dynamically learned mac */
John Lo8d00fff2017-08-03 00:35:36 -0400685 if ((result.fields.age_not == 0) && (l2learn_main.global_learn_count))
686 l2learn_main.global_learn_count--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687
Dave Barach97d8dc22016-08-15 15:31:15 -0400688 /* Remove entry from hash table */
689 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690 return 0;
691}
692
Dave Barach97d8dc22016-08-15 15:31:15 -0400693/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400694 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400695 * The CLI format is:
696 * l2fib del <mac> <bd-id>
697 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400699l2fib_del (vlib_main_t * vm,
700 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701{
Dave Barach97d8dc22016-08-15 15:31:15 -0400702 bd_main_t *bdm = &bd_main;
703 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200704 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705 u32 bd_id;
706 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400707 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200709 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 {
711 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400712 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 goto done;
714 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400715
716 if (!unformat (input, "%d", &bd_id))
717 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400719 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400721 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
723 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400724 if (!p)
725 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
727 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400728 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 bd_index = p[0];
730
Dave Barach97d8dc22016-08-15 15:31:15 -0400731 /* Delete the entry */
John Lo7dbd7262018-05-31 10:25:18 -0400732 if (l2fib_del_entry (mac, bd_index, 0))
Dave Barach97d8dc22016-08-15 15:31:15 -0400733 {
734 error = clib_error_return (0, "mac entry not found");
735 goto done;
736 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
Dave Barach97d8dc22016-08-15 15:31:15 -0400738done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739 return error;
740}
741
Billy McFall22aa3e92016-09-09 08:46:40 -0400742/*?
743 * This command deletes an existing MAC Address entry from the L2 FIB
744 * table of an existing bridge-domain.
745 *
746 * @cliexpar
747 * 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):
748 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
749?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400750/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
752 .path = "l2fib del",
John Lo7dbd7262018-05-31 10:25:18 -0400753 .short_help = "l2fib del <mac> <bridge-domain-id> []",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 .function = l2fib_del,
755};
Dave Barach97d8dc22016-08-15 15:31:15 -0400756/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757
John Loda1f2c72017-03-24 20:11:15 -0400758/**
759 Kick off ager to scan MACs to age/delete MAC entries
760*/
761void
762l2fib_start_ager_scan (vlib_main_t * vm)
763{
Eyal Bari24db0ec2017-09-27 21:43:51 +0300764 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
John Loda1f2c72017-03-24 20:11:15 -0400765
766 /* check if there is at least one bd with mac aging enabled */
Eyal Bari24db0ec2017-09-27 21:43:51 +0300767 l2_bridge_domain_t *bd_config;
John Loda1f2c72017-03-24 20:11:15 -0400768 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300769 {
John Loda1f2c72017-03-24 20:11:15 -0400770 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300771 {
772 evt = L2_MAC_AGE_PROCESS_EVENT_START;
773 break;
774 }
775 }
John Loda1f2c72017-03-24 20:11:15 -0400776
777 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
Eyal Bari24db0ec2017-09-27 21:43:51 +0300778 evt, 0);
John Loda1f2c72017-03-24 20:11:15 -0400779}
780
781/**
Eyal Bari7537e712017-04-27 14:07:55 +0300782 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400783*/
784void
785l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
786{
Eyal Bari0f360dc2017-06-14 13:11:20 +0300787 *l2fib_swif_seq_num (sw_if_index) += 1;
John Loda1f2c72017-03-24 20:11:15 -0400788 l2fib_start_ager_scan (vm);
789}
790
791/**
Eyal Bari7537e712017-04-27 14:07:55 +0300792 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400793*/
794void
795l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
796{
Eyal Bari7537e712017-04-27 14:07:55 +0300797 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400798 bd_config->seq_num += 1;
799 l2fib_start_ager_scan (vm);
800}
801
802/**
Eyal Bari7537e712017-04-27 14:07:55 +0300803 Flush all non static MACs - flushes all valid BDs
804*/
805void
806l2fib_flush_all_mac (vlib_main_t * vm)
807{
808 l2_bridge_domain_t *bd_config;
809 vec_foreach (bd_config, l2input_main.bd_configs)
810 if (bd_is_valid (bd_config))
811 bd_config->seq_num += 1;
812
813 l2fib_start_ager_scan (vm);
814}
815
816
817/**
John Loda1f2c72017-03-24 20:11:15 -0400818 Flush MACs, except static ones, associated with an interface
819 The CLI format is:
820 l2fib flush-mac interface <if-name>
821*/
822static clib_error_t *
823l2fib_flush_mac_int (vlib_main_t * vm,
824 unformat_input_t * input, vlib_cli_command_t * cmd)
825{
826 vnet_main_t *vnm = vnet_get_main ();
827 clib_error_t *error = 0;
828 u32 sw_if_index;
829
830 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
831 {
832 error = clib_error_return (0, "unknown interface `%U'",
833 format_unformat_error, input);
834 goto done;
835 }
836
837 l2fib_flush_int_mac (vm, sw_if_index);
838
839done:
840 return error;
841}
842
Eyal Bari7537e712017-04-27 14:07:55 +0300843/**
844 Flush all MACs, except static ones
845 The CLI format is:
846 l2fib flush-mac all
847*/
848static clib_error_t *
849l2fib_flush_mac_all (vlib_main_t * vm,
850 unformat_input_t * input, vlib_cli_command_t * cmd)
851{
852 l2fib_flush_all_mac (vm);
853 return 0;
854}
855
856/*?
857 * This command kick off ager to delete all existing MAC Address entries,
858 * except static ones, associated with an interface from the L2 FIB table.
859 *
860 * @cliexpar
861 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
862 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
863?*/
864/* *INDENT-OFF* */
865VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
866 .path = "l2fib flush-mac all",
867 .short_help = "l2fib flush-mac all",
868 .function = l2fib_flush_mac_all,
869};
870/* *INDENT-ON* */
871
John Loda1f2c72017-03-24 20:11:15 -0400872/*?
873 * This command kick off ager to delete all existing MAC Address entries,
874 * except static ones, associated with an interface from the L2 FIB table.
875 *
876 * @cliexpar
877 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
878 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
879?*/
880/* *INDENT-OFF* */
881VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
882 .path = "l2fib flush-mac interface",
883 .short_help = "l2fib flush-mac interface <if-name>",
884 .function = l2fib_flush_mac_int,
885};
886/* *INDENT-ON* */
887
888/**
889 Flush bridge-domain MACs except static ones.
890 The CLI format is:
891 l2fib flush-mac bridge-domain <bd-id>
892*/
893static clib_error_t *
894l2fib_flush_mac_bd (vlib_main_t * vm,
895 unformat_input_t * input, vlib_cli_command_t * cmd)
896{
897 bd_main_t *bdm = &bd_main;
898 clib_error_t *error = 0;
899 u32 bd_index, bd_id;
900 uword *p;
901
902 if (!unformat (input, "%d", &bd_id))
903 {
904 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
905 format_unformat_error, input);
906 goto done;
907 }
908
909 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
910 if (p)
911 bd_index = *p;
912 else
913 return clib_error_return (0, "No such bridge domain %d", bd_id);
914
915 l2fib_flush_bd_mac (vm, bd_index);
916
917done:
918 return error;
919}
920
921/*?
922 * This command kick off ager to delete all existing MAC Address entries,
923 * except static ones, in a bridge domain from the L2 FIB table.
924 *
925 * @cliexpar
926 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
927 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
928?*/
929/* *INDENT-OFF* */
930VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
931 .path = "l2fib flush-mac bridge-domain",
932 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
933 .function = l2fib_flush_mac_bd,
934};
935/* *INDENT-ON* */
936
Eyal Bariafc47aa2017-04-20 14:45:17 +0300937clib_error_t *
938l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
939{
940 l2_input_config_t *config = l2input_intf_config (sw_if_index);
941 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
942 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
943 return 0;
944}
945
946VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947
Dave Barach97d8dc22016-08-15 15:31:15 -0400948BVT (clib_bihash) * get_mac_table (void)
949{
950 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951 return &mp->mac_table;
952}
953
John Lo8d00fff2017-08-03 00:35:36 -0400954static_always_inline void *
955allocate_mac_evt_buf (u32 client, u32 client_index)
956{
957 l2fib_main_t *fm = &l2fib_main;
958 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
959 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
960 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
961 mp->pid = htonl (client);
962 mp->client_index = client_index;
963 return mp;
964}
965
966static_always_inline f64
967l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
968{
969 l2fib_main_t *fm = &l2fib_main;
970 l2learn_main_t *lm = &l2learn_main;
971
972 BVT (clib_bihash) * h = &fm->mac_table;
973 int i, j, k;
974 f64 last_start = start_time;
975 f64 accum_t = 0;
976 f64 delta_t = 0;
977 u32 evt_idx = 0;
978 u32 learn_count = 0;
979 u32 client = lm->client_pid;
980 u32 cl_idx = lm->client_index;
981 vl_api_l2_macs_event_t *mp = 0;
Florin Corasaf0ff5a2018-01-10 08:17:03 -0800982 vl_api_registration_t *reg = 0;
John Lo8d00fff2017-08-03 00:35:36 -0400983
984 if (client)
985 {
986 mp = allocate_mac_evt_buf (client, cl_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -0800987 reg = vl_api_client_index_to_registration (lm->client_index);
John Lo8d00fff2017-08-03 00:35:36 -0400988 }
989
990 for (i = 0; i < h->nbuckets; i++)
991 {
992 /* allow no more than 20us without a pause */
993 delta_t = vlib_time_now (vm) - last_start;
994 if (delta_t > 20e-6)
995 {
996 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
997 last_start = vlib_time_now (vm);
998 accum_t += delta_t;
999 }
1000
1001 if (i < (h->nbuckets - 3))
1002 {
1003 BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
1004 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
1005 b = &h->buckets[i + 1];
1006 if (b->offset)
1007 {
1008 BVT (clib_bihash_value) * v =
1009 BV (clib_bihash_get_value) (h, b->offset);
1010 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1011 }
1012 }
1013
1014 BVT (clib_bihash_bucket) * b = &h->buckets[i];
1015 if (b->offset == 0)
1016 continue;
1017 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1018 for (j = 0; j < (1 << b->log2_pages); j++)
1019 {
1020 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1021 {
1022 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1023 continue;
1024
1025 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1026 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1027
1028 if (result.fields.age_not == 0)
1029 learn_count++;
1030
John Lo8d00fff2017-08-03 00:35:36 -04001031 if (client)
1032 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001033 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1034 {
1035 /* event message full, send it and start a new one */
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001036 if (reg && vl_api_can_send_msg (reg))
Eyal Bari24db0ec2017-09-27 21:43:51 +03001037 {
1038 mp->n_macs = htonl (evt_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001039 vl_api_send_msg (reg, (u8 *) mp);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001040 mp = allocate_mac_evt_buf (client, cl_idx);
1041 }
1042 else
1043 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001044 if (reg)
Dave Barach59b25652017-09-10 15:04:27 -04001045 clib_warning ("MAC event to pid %d queue stuffed!"
1046 " %d MAC entries lost", client,
1047 evt_idx);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001048 }
1049 evt_idx = 0;
1050 }
1051
John Lo8d00fff2017-08-03 00:35:36 -04001052 if (result.fields.lrn_evt)
1053 {
1054 /* copy mac entry to event msg */
1055 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac,
1056 6);
John Loe23c99e2018-03-13 21:53:18 -04001057 mp->mac[evt_idx].action = result.fields.lrn_mov ?
1058 MAC_EVENT_ACTION_MOVE : MAC_EVENT_ACTION_ADD;
John Lo8d00fff2017-08-03 00:35:36 -04001059 mp->mac[evt_idx].sw_if_index =
1060 htonl (result.fields.sw_if_index);
John Loe23c99e2018-03-13 21:53:18 -04001061 /* clear event bits and update mac entry */
John Lo8d00fff2017-08-03 00:35:36 -04001062 result.fields.lrn_evt = 0;
John Loe23c99e2018-03-13 21:53:18 -04001063 result.fields.lrn_mov = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001064 BVT (clib_bihash_kv) kv;
1065 kv.key = key.raw;
1066 kv.value = result.raw;
1067 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1068 evt_idx++;
1069 continue; /* skip aging */
1070 }
1071 }
1072
1073 if (event_only || result.fields.age_not)
1074 continue; /* skip aging - static_mac alsways age_not */
1075
1076 /* start aging processing */
1077 u32 bd_index = key.fields.bd_index;
1078 u32 sw_if_index = result.fields.sw_if_index;
1079 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1080 if (result.fields.sn.as_u16 != sn)
1081 goto age_out; /* stale mac */
1082
1083 l2_bridge_domain_t *bd_config =
1084 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1085
1086 if (bd_config->mac_age == 0)
1087 continue; /* skip aging */
1088
1089 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1090 delta += delta < 0 ? 256 : 0;
1091
1092 if (delta < bd_config->mac_age)
1093 continue; /* still valid */
1094
1095 age_out:
1096 if (client)
1097 {
1098 /* copy mac entry to event msg */
1099 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac, 6);
John Loe23c99e2018-03-13 21:53:18 -04001100 mp->mac[evt_idx].action = MAC_EVENT_ACTION_DELETE;
John Lo8d00fff2017-08-03 00:35:36 -04001101 mp->mac[evt_idx].sw_if_index =
1102 htonl (result.fields.sw_if_index);
1103 evt_idx++;
1104 }
1105 /* delete mac entry */
1106 BVT (clib_bihash_kv) kv;
1107 kv.key = key.raw;
1108 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1109 learn_count--;
Dave Barach28374ca2018-08-07 12:46:18 -04001110 /*
1111 * Note: we may have just freed the bucket's backing
1112 * storage, so check right here...
1113 */
1114 if (b->offset == 0)
1115 goto doublebreak;
John Lo8d00fff2017-08-03 00:35:36 -04001116 }
1117 v++;
1118 }
Dave Barach28374ca2018-08-07 12:46:18 -04001119 doublebreak:
1120 ;
John Lo8d00fff2017-08-03 00:35:36 -04001121 }
1122
1123 /* keep learn count consistent */
1124 l2learn_main.global_learn_count = learn_count;
1125
1126 if (mp)
1127 {
1128 /* send any outstanding mac event message else free message buffer */
1129 if (evt_idx)
1130 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001131 if (reg && vl_api_can_send_msg (reg))
John Lo8d00fff2017-08-03 00:35:36 -04001132 {
1133 mp->n_macs = htonl (evt_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001134 vl_api_send_msg (reg, (u8 *) mp);
John Lo8d00fff2017-08-03 00:35:36 -04001135 }
1136 else
1137 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001138 if (reg)
Dave Barach59b25652017-09-10 15:04:27 -04001139 clib_warning ("MAC event to pid %d queue stuffed!"
1140 " %d MAC entries lost", client, evt_idx);
John Lo8d00fff2017-08-03 00:35:36 -04001141 vl_msg_api_free (mp);
1142 }
1143 }
1144 else
1145 vl_msg_api_free (mp);
1146 }
1147 return delta_t + accum_t;
1148}
1149
Damjan Mariond171d482016-12-05 14:16:38 +01001150static uword
1151l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1152 vlib_frame_t * f)
1153{
1154 uword event_type, *event_data = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001155 l2fib_main_t *fm = &l2fib_main;
1156 l2learn_main_t *lm = &l2learn_main;
Damjan Mariond171d482016-12-05 14:16:38 +01001157 bool enabled = 0;
John Lo7f358b32018-04-28 01:19:24 -04001158 f64 start_time, next_age_scan_time = CLIB_TIME_MAX;
Damjan Mariond171d482016-12-05 14:16:38 +01001159
1160 while (1)
1161 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001162 if (lm->client_pid)
1163 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1164 else if (enabled)
John Lo8d00fff2017-08-03 00:35:36 -04001165 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001166 f64 t = next_age_scan_time - vlib_time_now (vm);
1167 vlib_process_wait_for_event_or_clock (vm, t);
John Lo8d00fff2017-08-03 00:35:36 -04001168 }
Damjan Mariond171d482016-12-05 14:16:38 +01001169 else
1170 vlib_process_wait_for_event (vm);
1171
1172 event_type = vlib_process_get_events (vm, &event_data);
1173 vec_reset_length (event_data);
1174
John Lo8d00fff2017-08-03 00:35:36 -04001175 start_time = vlib_time_now (vm);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001176 enum
1177 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
John Lo8d00fff2017-08-03 00:35:36 -04001178
Damjan Mariond171d482016-12-05 14:16:38 +01001179 switch (event_type)
1180 {
John Lo8d00fff2017-08-03 00:35:36 -04001181 case ~0: /* timer expired */
Eyal Bari24db0ec2017-09-27 21:43:51 +03001182 if (lm->client_pid != 0 && start_time < next_age_scan_time)
John Lo8d00fff2017-08-03 00:35:36 -04001183 scan = SCAN_MAC_EVENT;
Damjan Mariond171d482016-12-05 14:16:38 +01001184 break;
John Lo8d00fff2017-08-03 00:35:36 -04001185
Damjan Mariond171d482016-12-05 14:16:38 +01001186 case L2_MAC_AGE_PROCESS_EVENT_START:
1187 enabled = 1;
1188 break;
John Lo8d00fff2017-08-03 00:35:36 -04001189
Damjan Mariond171d482016-12-05 14:16:38 +01001190 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1191 enabled = 0;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001192 scan = SCAN_DISABLE;
1193 break;
John Lo8d00fff2017-08-03 00:35:36 -04001194
John Loda1f2c72017-03-24 20:11:15 -04001195 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
John Loda1f2c72017-03-24 20:11:15 -04001196 break;
John Lo8d00fff2017-08-03 00:35:36 -04001197
Damjan Mariond171d482016-12-05 14:16:38 +01001198 default:
1199 ASSERT (0);
1200 }
Eyal Bari7537e712017-04-27 14:07:55 +03001201
John Lo8d00fff2017-08-03 00:35:36 -04001202 if (scan == SCAN_MAC_EVENT)
1203 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1204 else
Eyal Bari24db0ec2017-09-27 21:43:51 +03001205 {
1206 if (scan == SCAN_MAC_AGE)
1207 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1208 if (scan == SCAN_DISABLE)
1209 {
1210 l2fib_main.age_scan_duration = 0;
1211 l2fib_main.evt_scan_duration = 0;
1212 }
1213 /* schedule next scan */
1214 if (enabled)
1215 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1216 else
John Lo7f358b32018-04-28 01:19:24 -04001217 next_age_scan_time = CLIB_TIME_MAX;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001218 }
Damjan Mariond171d482016-12-05 14:16:38 +01001219 }
1220 return 0;
1221}
1222
1223/* *INDENT-OFF* */
1224VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1225 .function = l2fib_mac_age_scanner_process,
1226 .type = VLIB_NODE_TYPE_PROCESS,
1227 .name = "l2fib-mac-age-scanner-process",
1228};
1229/* *INDENT-ON* */
1230
Dave Barach97d8dc22016-08-15 15:31:15 -04001231clib_error_t *
1232l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233{
Dave Barach97d8dc22016-08-15 15:31:15 -04001234 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235 l2fib_entry_key_t test_key;
1236 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001237
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001239 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001240
Dave Barach97d8dc22016-08-15 15:31:15 -04001241 /* Create the hash table */
1242 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1243 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244
Dave Barach97d8dc22016-08-15 15:31:15 -04001245 /* verify the key constructor is good, since it is endian-sensitive */
1246 memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001247 test_mac[0] = 0x11;
1248 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001249 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250 ASSERT (test_key.fields.mac[0] == 0x11);
1251 ASSERT (test_key.fields.bd_index == 0x1234);
1252
1253 return 0;
1254}
1255
1256VLIB_INIT_FUNCTION (l2fib_init);
1257
Dave Barach97d8dc22016-08-15 15:31:15 -04001258/*
1259 * fd.io coding-style-patch-verification: ON
1260 *
1261 * Local Variables:
1262 * eval: (c-set-style "gnu")
1263 * End:
1264 */