blob: fc518fe9dbf25549de6f87b0d7ddcb8cff965ddb [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
98 vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
99 if (!swif)
Eyal Bari0f360dc2017-06-14 13:11:20 +0300100 return format (s, "Stale");
Eyal Barib823df52017-06-12 17:07:22 +0300101
102 return format (s, "%U", format_vnet_sw_interface_name, vnm,
103 vnet_get_sw_interface_safe (vnm, sw_if_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104}
105
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700106typedef struct l2fib_dump_walk_ctx_t_
107{
108 u32 bd_index;
109 l2fib_entry_key_t *l2fe_key;
110 l2fib_entry_result_t *l2fe_res;
111} l2fib_dump_walk_ctx_t;
112
113static void
114l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
115{
116 l2fib_dump_walk_ctx_t *ctx = arg;
117 l2fib_entry_result_t result;
118 l2fib_entry_key_t key;
119
120 key.raw = kvp->key;
121 result.raw = kvp->value;
122
123 if ((ctx->bd_index == ~0) || (ctx->bd_index == key.fields.bd_index))
124 {
125 vec_add1 (ctx->l2fe_key, key);
126 vec_add1 (ctx->l2fe_res, result);
127 }
128}
129
Dave Barach97d8dc22016-08-15 15:31:15 -0400130void
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700131l2fib_table_dump (u32 bd_index,
132 l2fib_entry_key_t ** l2fe_key,
Dave Barach97d8dc22016-08-15 15:31:15 -0400133 l2fib_entry_result_t ** l2fe_res)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134{
Dave Barach97d8dc22016-08-15 15:31:15 -0400135 l2fib_main_t *msm = &l2fib_main;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700136 l2fib_dump_walk_ctx_t ctx = {
137 .bd_index = bd_index,
138 };
139
140 BV (clib_bihash_foreach_key_value_pair)
141 (&msm->mac_table, l2fib_dump_walk_cb, &ctx);
142
143 *l2fe_key = ctx.l2fe_key;
144 *l2fe_res = ctx.l2fe_res;
145}
146
147typedef struct l2fib_show_walk_ctx_t_
148{
149 u8 first_entry;
150 u8 verbose;
151 vlib_main_t *vm;
152 vnet_main_t *vnm;
153 u32 total_entries;
154 u32 bd_index;
155 u8 learn;
156 u8 add;
157 u8 now;
158} l2fib_show_walk_ctx_t;
159
160static void
161l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
162{
163 l2fib_show_walk_ctx_t *ctx = arg;
164 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 l2fib_entry_result_t result;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700166 l2fib_entry_key_t key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700168 if (ctx->verbose && ctx->first_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700170 ctx->first_entry = 0;
171 vlib_cli_output (ctx->vm,
172 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
173 "Mac-Address", "BD-Idx", "If-Idx",
174 "BSN-ISN", "Age(min)", "static", "filter",
175 "bvi", "Interface-Name");
176 }
177
178 key.raw = kvp->key;
179 result.raw = kvp->value;
180 ctx->total_entries++;
181
182 if (ctx->verbose &&
183 ((ctx->bd_index >> 31) || (ctx->bd_index == key.fields.bd_index)))
184 {
185 u8 *s = NULL;
186
Neale Rannsb54d0812018-09-06 06:22:56 -0700187 if (ctx->learn && l2fib_entry_result_is_set_AGE_NOT (&result))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700188 return; /* skip provisioned macs */
189
Neale Rannsb54d0812018-09-06 06:22:56 -0700190 if (ctx->add && !l2fib_entry_result_is_set_AGE_NOT (&result))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700191 return; /* skip learned macs */
192
193 bd_config = vec_elt_at_index (l2input_main.bd_configs,
194 key.fields.bd_index);
195
Neale Rannsb54d0812018-09-06 06:22:56 -0700196 if (l2fib_entry_result_is_set_AGE_NOT (&result))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700197 s = format (s, "no");
198 else if (bd_config->mac_age == 0)
199 s = format (s, "-");
200 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400201 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700202 i16 delta = ctx->now - result.fields.timestamp;
203 delta += delta < 0 ? 256 : 0;
204 s = format (s, "%d", delta);
Dave Barach97d8dc22016-08-15 15:31:15 -0400205 }
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700206
207 vlib_cli_output (ctx->vm,
208 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
209 format_ethernet_address, key.fields.mac,
210 key.fields.bd_index,
211 result.fields.sw_if_index == ~0
212 ? -1 : result.fields.sw_if_index,
Neale Rannsb54d0812018-09-06 06:22:56 -0700213 result.fields.sn.bd, result.fields.sn.swif, s,
214 l2fib_entry_result_is_set_STATIC (&result) ? "*" : "-",
215 l2fib_entry_result_is_set_FILTER (&result) ? "*" : "-",
216 l2fib_entry_result_is_set_BVI (&result) ? "*" : "-",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700217 format_vnet_sw_if_index_name_with_NA,
218 ctx->vnm, result.fields.sw_if_index);
219 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220 }
221}
222
Chris Luke16bcf7d2016-09-01 14:31:46 -0400223/** Display the contents of the l2fib. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400225show_l2fib (vlib_main_t * vm,
226 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227{
Dave Barach97d8dc22016-08-15 15:31:15 -0400228 bd_main_t *bdm = &bd_main;
229 l2fib_main_t *msm = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 u8 raw = 0;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700231 u32 bd_id;
232 l2fib_show_walk_ctx_t ctx = {
233 .first_entry = 1,
234 .bd_index = ~0,
235 .now = (u8) (vlib_time_now (vm) / 60),
236 .vm = vm,
237 .vnm = msm->vnet_main,
238 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
John Lo7dbd7262018-05-31 10:25:18 -0400240 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
John Lo8d00fff2017-08-03 00:35:36 -0400241 {
John Lo7dbd7262018-05-31 10:25:18 -0400242 if (unformat (input, "raw"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400243 {
John Lo7dbd7262018-05-31 10:25:18 -0400244 raw = 1;
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700245 ctx.verbose = 0;
John Lo7dbd7262018-05-31 10:25:18 -0400246 break;
247 }
248 else if (unformat (input, "verbose"))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700249 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400250 else if (unformat (input, "all"))
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700251 ctx.verbose = 1;
252 else if (unformat (input, "bd_index %d", &ctx.bd_index))
253 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400254 else if (unformat (input, "learn"))
255 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700256 ctx.add = 0;
257 ctx.learn = 1;
258 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400259 }
260 else if (unformat (input, "add"))
261 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700262 ctx.learn = 0;
263 ctx.add = 1;
264 ctx.verbose = 1;
John Lo7dbd7262018-05-31 10:25:18 -0400265 }
266 else if (unformat (input, "bd_id %d", &bd_id))
267 {
268 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
269 if (p)
270 {
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700271 ctx.verbose = 1;
272 ctx.bd_index = p[0];
John Lo7dbd7262018-05-31 10:25:18 -0400273 }
274 else
275 return clib_error_return (0,
276 "bridge domain id %d doesn't exist\n",
277 bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400278 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 else
John Lo7dbd7262018-05-31 10:25:18 -0400280 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281 }
282
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700283 BV (clib_bihash_foreach_key_value_pair)
284 (&msm->mac_table, l2fib_show_walk_cb, &ctx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700286 if (ctx.total_entries == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 vlib_cli_output (vm, "no l2fib entries");
288 else
John Lo8d00fff2017-08-03 00:35:36 -0400289 {
290 l2learn_main_t *lm = &l2learn_main;
291 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
292 "Last scan time: %.4esec Learn limit: %d ",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700293 ctx.total_entries, lm->global_learn_count,
John Lo8d00fff2017-08-03 00:35:36 -0400294 msm->age_scan_duration, lm->global_learn_limit);
295 if (lm->client_pid)
296 vlib_cli_output (vm, "L2MAC events client PID: %d "
297 "Last e-scan time: %.4esec Delay: %.2esec "
298 "Max macs in event: %d",
299 lm->client_pid, msm->evt_scan_duration,
300 msm->event_scan_delay, msm->max_macs_in_event);
301 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
303 if (raw)
304 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Neale Ranns99a3c6c2018-08-07 02:54:31 -0700305 BV (format_bihash), &msm->mac_table, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
307 return 0;
308}
309
Billy McFall22aa3e92016-09-09 08:46:40 -0400310/*?
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700311 * This command displays the MAC Address entries of the L2 FIB table.
Billy McFall22aa3e92016-09-09 08:46:40 -0400312 * Output can be filtered to just get the number of MAC Addresses or display
313 * each MAC Address for all bridge domains or just a single bridge domain.
314 *
315 * @cliexpar
316 * Example of how to display the number of MAC Address entries in the L2
317 * FIB table:
318 * @cliexstart{show l2fib}
319 * 3 l2fib entries
320 * @cliexend
321 * Example of how to display all the MAC Address entries in the L2
322 * FIB table:
John Lo7dbd7262018-05-31 10:25:18 -0400323 * @cliexstart{show l2fib all}
Billy McFall22aa3e92016-09-09 08:46:40 -0400324 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
325 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
326 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
327 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
328 * 3 l2fib entries
329 * @cliexend
330?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400331/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
333 .path = "show l2fib",
John Lo7dbd7262018-05-31 10:25:18 -0400334 .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335 .function = show_l2fib,
336};
Dave Barach97d8dc22016-08-15 15:31:15 -0400337/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
339
Dave Barach97d8dc22016-08-15 15:31:15 -0400340/* Remove all entries from the l2fib */
341void
Eyal Bari7537e712017-04-27 14:07:55 +0300342l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343{
Dave Barach97d8dc22016-08-15 15:31:15 -0400344 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Eyal Bari7537e712017-04-27 14:07:55 +0300346 /* Remove all entries */
347 BV (clib_bihash_free) (&mp->mac_table);
348 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
349 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 l2learn_main.global_learn_count = 0;
351}
352
Chris Luke16bcf7d2016-09-01 14:31:46 -0400353/** Clear all entries in L2FIB.
354 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400355 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400357clear_l2fib (vlib_main_t * vm,
358 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359{
Eyal Bari7537e712017-04-27 14:07:55 +0300360 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 return 0;
362}
363
Billy McFall22aa3e92016-09-09 08:46:40 -0400364/*?
365 * This command clears all the MAC Address entries from the L2 FIB table.
366 *
367 * @cliexpar
368 * Example of how to clear the L2 FIB Table:
369 * @cliexcmd{clear l2fib}
370 * Example to show the L2 FIB Table has been cleared:
371 * @cliexstart{show l2fib verbose}
372 * no l2fib entries
373 * @cliexend
374?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400375/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
377 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400378 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 .function = clear_l2fib,
380};
Dave Barach97d8dc22016-08-15 15:31:15 -0400381/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382
Eyal Bari7537e712017-04-27 14:07:55 +0300383static inline l2fib_seq_num_t
384l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
385{
Eyal Bari7537e712017-04-27 14:07:55 +0300386 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
387 /* *INDENT-OFF* */
388 return (l2fib_seq_num_t) {
Eyal Bari0f360dc2017-06-14 13:11:20 +0300389 .swif = *l2fib_swif_seq_num (sw_if_index),
Eyal Bari7537e712017-04-27 14:07:55 +0300390 .bd = bd_config->seq_num,
391 };
392 /* *INDENT-ON* */
393}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Dave Barach97d8dc22016-08-15 15:31:15 -0400395/**
396 * Add an entry to the l2fib.
397 * If the entry already exists then overwrite it
398 */
399void
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700400l2fib_add_entry (const u8 * mac, u32 bd_index,
Neale Rannsb54d0812018-09-06 06:22:56 -0700401 u32 sw_if_index, l2fib_entry_result_flags_t flags)
Dave Barach97d8dc22016-08-15 15:31:15 -0400402{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 l2fib_entry_key_t key;
404 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400405 __attribute__ ((unused)) u32 bucket_contents;
John Lo8d00fff2017-08-03 00:35:36 -0400406 l2fib_main_t *fm = &l2fib_main;
407 l2learn_main_t *lm = &l2learn_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400408 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Dave Barach97d8dc22016-08-15 15:31:15 -0400410 /* set up key */
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200411 key.raw = l2fib_make_key (mac, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400412
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700413 /* check if entry already exist */
John Lo8d00fff2017-08-03 00:35:36 -0400414 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
415 {
416 /* decrement counter if overwriting a learned mac */
417 result.raw = kv.value;
Neale Rannsb54d0812018-09-06 06:22:56 -0700418 if ((!l2fib_entry_result_is_set_AGE_NOT (&result))
419 && (lm->global_learn_count))
John Lo8d00fff2017-08-03 00:35:36 -0400420 lm->global_learn_count--;
421 }
422
Dave Barach97d8dc22016-08-15 15:31:15 -0400423 /* set up result */
424 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 result.fields.sw_if_index = sw_if_index;
Neale Rannsb54d0812018-09-06 06:22:56 -0700426 result.fields.flags = flags;
427
428 /* no aging for provisioned entry */
429 l2fib_entry_result_set_AGE_NOT (&result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430
431 kv.key = key.raw;
432 kv.value = result.raw;
433
John Lo8d00fff2017-08-03 00:35:36 -0400434 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435}
436
Dave Barach97d8dc22016-08-15 15:31:15 -0400437/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400438 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400439 * The CLI format is:
440 * l2fib add <mac> <bd> <intf> [static] [bvi]
441 * l2fib add <mac> <bd> filter
442 * Note that filter and bvi entries are always static
443 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400445l2fib_add (vlib_main_t * vm,
446 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447{
Dave Barach97d8dc22016-08-15 15:31:15 -0400448 bd_main_t *bdm = &bd_main;
449 vnet_main_t *vnm = vnet_get_main ();
450 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200451 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 u32 bd_id;
453 u32 bd_index;
454 u32 sw_if_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400455 uword *p;
Neale Rannsb54d0812018-09-06 06:22:56 -0700456 l2fib_entry_result_flags_t flags;
457
458 flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200460 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 {
462 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400463 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 goto done;
465 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400466
467 if (!unformat (input, "%d", &bd_id))
468 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400470 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400472 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473
474 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400475 if (!p)
476 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
478 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400479 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 bd_index = p[0];
481
Dave Barach97d8dc22016-08-15 15:31:15 -0400482 if (unformat (input, "filter"))
483 {
John Lo14edd972017-10-31 13:26:02 -0400484 l2fib_add_filter_entry (mac, bd_index);
485 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487
John Lo14edd972017-10-31 13:26:02 -0400488 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
489 {
490 error = clib_error_return (0, "unknown interface `%U'",
491 format_unformat_error, input);
492 goto done;
493 }
494
495 if (unformat (input, "static"))
Neale Rannsb54d0812018-09-06 06:22:56 -0700496 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
John Lo14edd972017-10-31 13:26:02 -0400497 else if (unformat (input, "bvi"))
Neale Rannsb54d0812018-09-06 06:22:56 -0700498 flags |= (L2FIB_ENTRY_RESULT_FLAG_STATIC | L2FIB_ENTRY_RESULT_FLAG_BVI);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499
John Lob2fd6cb2017-07-12 19:56:45 -0400500 if (vec_len (l2input_main.configs) <= sw_if_index)
501 {
502 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
503 sw_if_index);
504 goto done;
505 }
506
Neale Rannsb54d0812018-09-06 06:22:56 -0700507 l2fib_add_entry (mac, bd_index, sw_if_index, flags);
Dave Barach97d8dc22016-08-15 15:31:15 -0400508
509done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 return error;
511}
512
Billy McFall22aa3e92016-09-09 08:46:40 -0400513/*?
514 * This command adds a MAC Address entry to the L2 FIB table
515 * of an existing bridge-domain. The MAC Address can be static
516 * or dynamic. This command also allows a filter to be added,
517 * such that packets with given MAC Addresses (source mac or
518 * destination mac match) are dropped.
519 *
520 * @cliexpar
521 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
522 * of a bridge-domain (where 200 is the bridge-domain-id):
523 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
524 * Example of how to add a static MAC Address entry to the L2 FIB table
525 * of a bridge-domain (where 200 is the bridge-domain-id):
526 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
527 * Example of how to add a filter such that a packet with the given MAC
528 * Address will be dropped in a given bridge-domain (where 200 is the
529 * bridge-domain-id):
530 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
531 * Example of show command of the provisioned MAC Addresses and filters:
532 * @cliexstart{show l2fib verbose}
533 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
534 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
535 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
536 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
537 * 3 l2fib entries
538 * @cliexend
539?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400540/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
542 .path = "l2fib add",
Billy McFall22aa3e92016-09-09 08:46:40 -0400543 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 .function = l2fib_add,
545};
Dave Barach97d8dc22016-08-15 15:31:15 -0400546/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
548
549static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400550l2fib_test_command_fn (vlib_main_t * vm,
551 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552{
Dave Barach97d8dc22016-08-15 15:31:15 -0400553 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200554 u8 mac[6], save_mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555 u32 bd_index = 0;
556 u32 sw_if_index = 8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 u32 is_add = 0;
558 u32 is_del = 0;
559 u32 is_check = 0;
560 u32 count = 1;
561 int mac_set = 0;
562 int i;
563
Dave Barach97d8dc22016-08-15 15:31:15 -0400564 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200566 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400567 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400569 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400571 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400573 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400575 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400577 break;
578 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
580 if (mac_set == 0)
581 return clib_error_return (0, "mac not set");
582
583 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400584 return clib_error_return (0,
585 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
Dave Barach178cf492018-11-13 16:34:13 -0500587 clib_memcpy_fast (save_mac, mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588
589 if (is_add)
590 {
591 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400592 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700593 l2fib_add_entry (mac, bd_index, sw_if_index,
594 L2FIB_ENTRY_RESULT_FLAG_NONE);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200595 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400596 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 }
598
599 if (is_check)
600 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400601 BVT (clib_bihash_kv) kv;
602 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
Dave Barach178cf492018-11-13 16:34:13 -0500604 clib_memcpy_fast (mac, save_mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
606 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400607 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200608 kv.key = l2fib_make_key (mac, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400609 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
610 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200611 clib_warning ("key %U AWOL", format_ethernet_address, mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400612 break;
613 }
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200614 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400615 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616 }
617
618 if (is_del)
619 {
Dave Barach178cf492018-11-13 16:34:13 -0500620 clib_memcpy_fast (mac, save_mac, 6);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200621
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400623 {
John Lo7dbd7262018-05-31 10:25:18 -0400624 l2fib_del_entry (mac, bd_index, 0);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200625 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400626 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627 }
628
629 return error;
630}
631
Billy McFall22aa3e92016-09-09 08:46:40 -0400632/*?
633 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
634 * bridge domain (bridge-domain-id of 0) to be modified.
635 *
636 * @cliexpar
637 * @parblock
638 * Example of how to add a set of 4 sequential MAC Address entries to L2
639 * FIB table of the default bridge-domain:
640 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
641 *
642 * Show the set of 4 sequential MAC Address entries that were added:
643 * @cliexstart{show l2fib verbose}
644 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
645 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
646 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
647 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
648 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
649 * 4 l2fib entries
650 * @cliexend
651 *
652 * Example of how to check that the set of 4 sequential MAC Address
653 * entries were added to L2 FIB table of the default
654 * bridge-domain. Used a count of 5 to produce an error:
655 *
656 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
657 * The output of the check command is in the log files. Log file
658 * location may vary based on your OS and Version:
659 *
660 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
661 *
662 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
663 *
664 * Example of how to delete a set of 4 sequential MAC Address entries
665 * from L2 FIB table of the default bridge-domain:
666 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
667 * @endparblock
668?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400669/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670VLIB_CLI_COMMAND (l2fib_test_command, static) = {
671 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400672 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673 .function = l2fib_test_command_fn,
674};
Dave Barach97d8dc22016-08-15 15:31:15 -0400675/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
677
Dave Barach97d8dc22016-08-15 15:31:15 -0400678/**
679 * Delete an entry from the l2fib.
John Lo7dbd7262018-05-31 10:25:18 -0400680 * Return 0 if the entry was deleted, or 1 it was not found or if
681 * sw_if_index is non-zero and does not match that in the entry.
Dave Barach97d8dc22016-08-15 15:31:15 -0400682 */
John Lo7dbd7262018-05-31 10:25:18 -0400683u32
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700684l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400685{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400687 l2fib_main_t *mp = &l2fib_main;
688 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
Dave Barach97d8dc22016-08-15 15:31:15 -0400690 /* set up key */
John Lo7dbd7262018-05-31 10:25:18 -0400691 kv.key = l2fib_make_key (mac, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692
Dave Barach97d8dc22016-08-15 15:31:15 -0400693 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 return 1;
695
696 result.raw = kv.value;
697
John Lo7dbd7262018-05-31 10:25:18 -0400698 /* check if sw_if_index of entry match */
699 if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index))
700 return 1;
701
Dave Barach97d8dc22016-08-15 15:31:15 -0400702 /* decrement counter if dynamically learned mac */
Neale Rannsb54d0812018-09-06 06:22:56 -0700703 if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) &&
704 (l2learn_main.global_learn_count))
John Lo8d00fff2017-08-03 00:35:36 -0400705 l2learn_main.global_learn_count--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706
Dave Barach97d8dc22016-08-15 15:31:15 -0400707 /* Remove entry from hash table */
708 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709 return 0;
710}
711
Dave Barach97d8dc22016-08-15 15:31:15 -0400712/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400713 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400714 * The CLI format is:
715 * l2fib del <mac> <bd-id>
716 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400718l2fib_del (vlib_main_t * vm,
719 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720{
Dave Barach97d8dc22016-08-15 15:31:15 -0400721 bd_main_t *bdm = &bd_main;
722 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200723 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724 u32 bd_id;
725 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400726 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200728 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 {
730 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400731 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732 goto done;
733 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400734
735 if (!unformat (input, "%d", &bd_id))
736 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400738 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400740 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741
742 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400743 if (!p)
744 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
746 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400747 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 bd_index = p[0];
749
Dave Barach97d8dc22016-08-15 15:31:15 -0400750 /* Delete the entry */
John Lo7dbd7262018-05-31 10:25:18 -0400751 if (l2fib_del_entry (mac, bd_index, 0))
Dave Barach97d8dc22016-08-15 15:31:15 -0400752 {
753 error = clib_error_return (0, "mac entry not found");
754 goto done;
755 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756
Dave Barach97d8dc22016-08-15 15:31:15 -0400757done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758 return error;
759}
760
Billy McFall22aa3e92016-09-09 08:46:40 -0400761/*?
762 * This command deletes an existing MAC Address entry from the L2 FIB
763 * table of an existing bridge-domain.
764 *
765 * @cliexpar
766 * 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):
767 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
768?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400769/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
771 .path = "l2fib del",
John Lo7dbd7262018-05-31 10:25:18 -0400772 .short_help = "l2fib del <mac> <bridge-domain-id> []",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773 .function = l2fib_del,
774};
Dave Barach97d8dc22016-08-15 15:31:15 -0400775/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776
John Loda1f2c72017-03-24 20:11:15 -0400777/**
778 Kick off ager to scan MACs to age/delete MAC entries
779*/
780void
781l2fib_start_ager_scan (vlib_main_t * vm)
782{
Eyal Bari24db0ec2017-09-27 21:43:51 +0300783 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
John Loda1f2c72017-03-24 20:11:15 -0400784
785 /* check if there is at least one bd with mac aging enabled */
Eyal Bari24db0ec2017-09-27 21:43:51 +0300786 l2_bridge_domain_t *bd_config;
John Loda1f2c72017-03-24 20:11:15 -0400787 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300788 {
John Loda1f2c72017-03-24 20:11:15 -0400789 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300790 {
791 evt = L2_MAC_AGE_PROCESS_EVENT_START;
792 break;
793 }
794 }
John Loda1f2c72017-03-24 20:11:15 -0400795
796 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
Eyal Bari24db0ec2017-09-27 21:43:51 +0300797 evt, 0);
John Loda1f2c72017-03-24 20:11:15 -0400798}
799
800/**
Eyal Bari7537e712017-04-27 14:07:55 +0300801 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400802*/
803void
804l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
805{
Eyal Bari0f360dc2017-06-14 13:11:20 +0300806 *l2fib_swif_seq_num (sw_if_index) += 1;
John Loda1f2c72017-03-24 20:11:15 -0400807 l2fib_start_ager_scan (vm);
808}
809
810/**
Eyal Bari7537e712017-04-27 14:07:55 +0300811 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400812*/
813void
814l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
815{
Eyal Bari7537e712017-04-27 14:07:55 +0300816 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400817 bd_config->seq_num += 1;
818 l2fib_start_ager_scan (vm);
819}
820
821/**
Eyal Bari7537e712017-04-27 14:07:55 +0300822 Flush all non static MACs - flushes all valid BDs
823*/
824void
825l2fib_flush_all_mac (vlib_main_t * vm)
826{
827 l2_bridge_domain_t *bd_config;
828 vec_foreach (bd_config, l2input_main.bd_configs)
829 if (bd_is_valid (bd_config))
830 bd_config->seq_num += 1;
831
832 l2fib_start_ager_scan (vm);
833}
834
835
836/**
John Loda1f2c72017-03-24 20:11:15 -0400837 Flush MACs, except static ones, associated with an interface
838 The CLI format is:
839 l2fib flush-mac interface <if-name>
840*/
841static clib_error_t *
842l2fib_flush_mac_int (vlib_main_t * vm,
843 unformat_input_t * input, vlib_cli_command_t * cmd)
844{
845 vnet_main_t *vnm = vnet_get_main ();
846 clib_error_t *error = 0;
847 u32 sw_if_index;
848
849 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
850 {
851 error = clib_error_return (0, "unknown interface `%U'",
852 format_unformat_error, input);
853 goto done;
854 }
855
856 l2fib_flush_int_mac (vm, sw_if_index);
857
858done:
859 return error;
860}
861
Eyal Bari7537e712017-04-27 14:07:55 +0300862/**
863 Flush all MACs, except static ones
864 The CLI format is:
865 l2fib flush-mac all
866*/
867static clib_error_t *
868l2fib_flush_mac_all (vlib_main_t * vm,
869 unformat_input_t * input, vlib_cli_command_t * cmd)
870{
871 l2fib_flush_all_mac (vm);
872 return 0;
873}
874
875/*?
876 * This command kick off ager to delete all existing MAC Address entries,
877 * except static ones, associated with an interface from the L2 FIB table.
878 *
879 * @cliexpar
880 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
881 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
882?*/
883/* *INDENT-OFF* */
884VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
885 .path = "l2fib flush-mac all",
886 .short_help = "l2fib flush-mac all",
887 .function = l2fib_flush_mac_all,
888};
889/* *INDENT-ON* */
890
John Loda1f2c72017-03-24 20:11:15 -0400891/*?
892 * This command kick off ager to delete all existing MAC Address entries,
893 * except static ones, associated with an interface from the L2 FIB table.
894 *
895 * @cliexpar
896 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
897 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
898?*/
899/* *INDENT-OFF* */
900VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
901 .path = "l2fib flush-mac interface",
902 .short_help = "l2fib flush-mac interface <if-name>",
903 .function = l2fib_flush_mac_int,
904};
905/* *INDENT-ON* */
906
907/**
908 Flush bridge-domain MACs except static ones.
909 The CLI format is:
910 l2fib flush-mac bridge-domain <bd-id>
911*/
912static clib_error_t *
913l2fib_flush_mac_bd (vlib_main_t * vm,
914 unformat_input_t * input, vlib_cli_command_t * cmd)
915{
916 bd_main_t *bdm = &bd_main;
917 clib_error_t *error = 0;
918 u32 bd_index, bd_id;
919 uword *p;
920
921 if (!unformat (input, "%d", &bd_id))
922 {
923 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
924 format_unformat_error, input);
925 goto done;
926 }
927
928 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
929 if (p)
930 bd_index = *p;
931 else
932 return clib_error_return (0, "No such bridge domain %d", bd_id);
933
934 l2fib_flush_bd_mac (vm, bd_index);
935
936done:
937 return error;
938}
939
940/*?
941 * This command kick off ager to delete all existing MAC Address entries,
942 * except static ones, in a bridge domain from the L2 FIB table.
943 *
944 * @cliexpar
945 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
946 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
947?*/
948/* *INDENT-OFF* */
949VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
950 .path = "l2fib flush-mac bridge-domain",
951 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
952 .function = l2fib_flush_mac_bd,
953};
954/* *INDENT-ON* */
955
Eyal Bariafc47aa2017-04-20 14:45:17 +0300956clib_error_t *
957l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
958{
959 l2_input_config_t *config = l2input_intf_config (sw_if_index);
960 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
961 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
962 return 0;
963}
964
965VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700966
Dave Barach97d8dc22016-08-15 15:31:15 -0400967BVT (clib_bihash) * get_mac_table (void)
968{
969 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970 return &mp->mac_table;
971}
972
John Lo8d00fff2017-08-03 00:35:36 -0400973static_always_inline void *
974allocate_mac_evt_buf (u32 client, u32 client_index)
975{
976 l2fib_main_t *fm = &l2fib_main;
977 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
978 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
979 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
980 mp->pid = htonl (client);
981 mp->client_index = client_index;
982 return mp;
983}
984
985static_always_inline f64
986l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
987{
988 l2fib_main_t *fm = &l2fib_main;
989 l2learn_main_t *lm = &l2learn_main;
990
991 BVT (clib_bihash) * h = &fm->mac_table;
992 int i, j, k;
993 f64 last_start = start_time;
994 f64 accum_t = 0;
995 f64 delta_t = 0;
996 u32 evt_idx = 0;
997 u32 learn_count = 0;
998 u32 client = lm->client_pid;
999 u32 cl_idx = lm->client_index;
1000 vl_api_l2_macs_event_t *mp = 0;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001001 vl_api_registration_t *reg = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001002
1003 if (client)
1004 {
1005 mp = allocate_mac_evt_buf (client, cl_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001006 reg = vl_api_client_index_to_registration (lm->client_index);
John Lo8d00fff2017-08-03 00:35:36 -04001007 }
1008
1009 for (i = 0; i < h->nbuckets; i++)
1010 {
1011 /* allow no more than 20us without a pause */
1012 delta_t = vlib_time_now (vm) - last_start;
1013 if (delta_t > 20e-6)
1014 {
1015 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
1016 last_start = vlib_time_now (vm);
1017 accum_t += delta_t;
1018 }
1019
1020 if (i < (h->nbuckets - 3))
1021 {
1022 BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
1023 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
1024 b = &h->buckets[i + 1];
1025 if (b->offset)
1026 {
1027 BVT (clib_bihash_value) * v =
1028 BV (clib_bihash_get_value) (h, b->offset);
1029 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1030 }
1031 }
1032
1033 BVT (clib_bihash_bucket) * b = &h->buckets[i];
1034 if (b->offset == 0)
1035 continue;
1036 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1037 for (j = 0; j < (1 << b->log2_pages); j++)
1038 {
1039 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1040 {
1041 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1042 continue;
1043
1044 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1045 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1046
Neale Rannsb54d0812018-09-06 06:22:56 -07001047 if (!l2fib_entry_result_is_set_AGE_NOT (&result))
John Lo8d00fff2017-08-03 00:35:36 -04001048 learn_count++;
1049
John Lo8d00fff2017-08-03 00:35:36 -04001050 if (client)
1051 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001052 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1053 {
1054 /* event message full, send it and start a new one */
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001055 if (reg && vl_api_can_send_msg (reg))
Eyal Bari24db0ec2017-09-27 21:43:51 +03001056 {
1057 mp->n_macs = htonl (evt_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001058 vl_api_send_msg (reg, (u8 *) mp);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001059 mp = allocate_mac_evt_buf (client, cl_idx);
1060 }
1061 else
1062 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001063 if (reg)
Dave Barach59b25652017-09-10 15:04:27 -04001064 clib_warning ("MAC event to pid %d queue stuffed!"
1065 " %d MAC entries lost", client,
1066 evt_idx);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001067 }
1068 evt_idx = 0;
1069 }
1070
Neale Rannsb54d0812018-09-06 06:22:56 -07001071 if (l2fib_entry_result_is_set_LRN_EVT (&result))
John Lo8d00fff2017-08-03 00:35:36 -04001072 {
1073 /* copy mac entry to event msg */
Dave Barach178cf492018-11-13 16:34:13 -05001074 clib_memcpy_fast (mp->mac[evt_idx].mac_addr,
1075 key.fields.mac, 6);
Neale Rannsb54d0812018-09-06 06:22:56 -07001076 mp->mac[evt_idx].action =
1077 l2fib_entry_result_is_set_LRN_MOV (&result) ?
John Loe23c99e2018-03-13 21:53:18 -04001078 MAC_EVENT_ACTION_MOVE : MAC_EVENT_ACTION_ADD;
John Lo8d00fff2017-08-03 00:35:36 -04001079 mp->mac[evt_idx].sw_if_index =
1080 htonl (result.fields.sw_if_index);
John Loe23c99e2018-03-13 21:53:18 -04001081 /* clear event bits and update mac entry */
Neale Rannsb54d0812018-09-06 06:22:56 -07001082 l2fib_entry_result_clear_LRN_EVT (&result);
1083 l2fib_entry_result_clear_LRN_MOV (&result);
John Lo8d00fff2017-08-03 00:35:36 -04001084 BVT (clib_bihash_kv) kv;
1085 kv.key = key.raw;
1086 kv.value = result.raw;
1087 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1088 evt_idx++;
1089 continue; /* skip aging */
1090 }
1091 }
1092
Neale Rannsb54d0812018-09-06 06:22:56 -07001093 if (event_only || l2fib_entry_result_is_set_AGE_NOT (&result))
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001094 continue; /* skip aging - static_mac always age_not */
John Lo8d00fff2017-08-03 00:35:36 -04001095
1096 /* start aging processing */
1097 u32 bd_index = key.fields.bd_index;
1098 u32 sw_if_index = result.fields.sw_if_index;
1099 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1100 if (result.fields.sn.as_u16 != sn)
1101 goto age_out; /* stale mac */
1102
1103 l2_bridge_domain_t *bd_config =
1104 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1105
1106 if (bd_config->mac_age == 0)
1107 continue; /* skip aging */
1108
1109 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1110 delta += delta < 0 ? 256 : 0;
1111
1112 if (delta < bd_config->mac_age)
1113 continue; /* still valid */
1114
1115 age_out:
1116 if (client)
1117 {
1118 /* copy mac entry to event msg */
Dave Barach178cf492018-11-13 16:34:13 -05001119 clib_memcpy_fast (mp->mac[evt_idx].mac_addr, key.fields.mac,
1120 6);
John Loe23c99e2018-03-13 21:53:18 -04001121 mp->mac[evt_idx].action = MAC_EVENT_ACTION_DELETE;
John Lo8d00fff2017-08-03 00:35:36 -04001122 mp->mac[evt_idx].sw_if_index =
1123 htonl (result.fields.sw_if_index);
1124 evt_idx++;
1125 }
1126 /* delete mac entry */
1127 BVT (clib_bihash_kv) kv;
1128 kv.key = key.raw;
1129 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1130 learn_count--;
Dave Barach28374ca2018-08-07 12:46:18 -04001131 /*
1132 * Note: we may have just freed the bucket's backing
1133 * storage, so check right here...
1134 */
1135 if (b->offset == 0)
1136 goto doublebreak;
John Lo8d00fff2017-08-03 00:35:36 -04001137 }
1138 v++;
1139 }
Dave Barach28374ca2018-08-07 12:46:18 -04001140 doublebreak:
1141 ;
John Lo8d00fff2017-08-03 00:35:36 -04001142 }
1143
1144 /* keep learn count consistent */
1145 l2learn_main.global_learn_count = learn_count;
1146
1147 if (mp)
1148 {
1149 /* send any outstanding mac event message else free message buffer */
1150 if (evt_idx)
1151 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001152 if (reg && vl_api_can_send_msg (reg))
John Lo8d00fff2017-08-03 00:35:36 -04001153 {
1154 mp->n_macs = htonl (evt_idx);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001155 vl_api_send_msg (reg, (u8 *) mp);
John Lo8d00fff2017-08-03 00:35:36 -04001156 }
1157 else
1158 {
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001159 if (reg)
Dave Barach59b25652017-09-10 15:04:27 -04001160 clib_warning ("MAC event to pid %d queue stuffed!"
1161 " %d MAC entries lost", client, evt_idx);
John Lo8d00fff2017-08-03 00:35:36 -04001162 vl_msg_api_free (mp);
1163 }
1164 }
1165 else
1166 vl_msg_api_free (mp);
1167 }
1168 return delta_t + accum_t;
1169}
1170
Damjan Mariond171d482016-12-05 14:16:38 +01001171static uword
1172l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1173 vlib_frame_t * f)
1174{
1175 uword event_type, *event_data = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001176 l2fib_main_t *fm = &l2fib_main;
1177 l2learn_main_t *lm = &l2learn_main;
Damjan Mariond171d482016-12-05 14:16:38 +01001178 bool enabled = 0;
John Lo7f358b32018-04-28 01:19:24 -04001179 f64 start_time, next_age_scan_time = CLIB_TIME_MAX;
Damjan Mariond171d482016-12-05 14:16:38 +01001180
1181 while (1)
1182 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001183 if (lm->client_pid)
1184 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1185 else if (enabled)
John Lo8d00fff2017-08-03 00:35:36 -04001186 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001187 f64 t = next_age_scan_time - vlib_time_now (vm);
1188 vlib_process_wait_for_event_or_clock (vm, t);
John Lo8d00fff2017-08-03 00:35:36 -04001189 }
Damjan Mariond171d482016-12-05 14:16:38 +01001190 else
1191 vlib_process_wait_for_event (vm);
1192
1193 event_type = vlib_process_get_events (vm, &event_data);
1194 vec_reset_length (event_data);
1195
John Lo8d00fff2017-08-03 00:35:36 -04001196 start_time = vlib_time_now (vm);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001197 enum
1198 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
John Lo8d00fff2017-08-03 00:35:36 -04001199
Damjan Mariond171d482016-12-05 14:16:38 +01001200 switch (event_type)
1201 {
John Lo8d00fff2017-08-03 00:35:36 -04001202 case ~0: /* timer expired */
Eyal Bari24db0ec2017-09-27 21:43:51 +03001203 if (lm->client_pid != 0 && start_time < next_age_scan_time)
John Lo8d00fff2017-08-03 00:35:36 -04001204 scan = SCAN_MAC_EVENT;
Damjan Mariond171d482016-12-05 14:16:38 +01001205 break;
John Lo8d00fff2017-08-03 00:35:36 -04001206
Damjan Mariond171d482016-12-05 14:16:38 +01001207 case L2_MAC_AGE_PROCESS_EVENT_START:
1208 enabled = 1;
1209 break;
John Lo8d00fff2017-08-03 00:35:36 -04001210
Damjan Mariond171d482016-12-05 14:16:38 +01001211 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1212 enabled = 0;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001213 scan = SCAN_DISABLE;
1214 break;
John Lo8d00fff2017-08-03 00:35:36 -04001215
John Loda1f2c72017-03-24 20:11:15 -04001216 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
John Loda1f2c72017-03-24 20:11:15 -04001217 break;
John Lo8d00fff2017-08-03 00:35:36 -04001218
Damjan Mariond171d482016-12-05 14:16:38 +01001219 default:
1220 ASSERT (0);
1221 }
Eyal Bari7537e712017-04-27 14:07:55 +03001222
John Lo8d00fff2017-08-03 00:35:36 -04001223 if (scan == SCAN_MAC_EVENT)
1224 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1225 else
Eyal Bari24db0ec2017-09-27 21:43:51 +03001226 {
1227 if (scan == SCAN_MAC_AGE)
1228 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1229 if (scan == SCAN_DISABLE)
1230 {
1231 l2fib_main.age_scan_duration = 0;
1232 l2fib_main.evt_scan_duration = 0;
1233 }
1234 /* schedule next scan */
1235 if (enabled)
1236 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1237 else
John Lo7f358b32018-04-28 01:19:24 -04001238 next_age_scan_time = CLIB_TIME_MAX;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001239 }
Damjan Mariond171d482016-12-05 14:16:38 +01001240 }
1241 return 0;
1242}
1243
1244/* *INDENT-OFF* */
1245VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1246 .function = l2fib_mac_age_scanner_process,
1247 .type = VLIB_NODE_TYPE_PROCESS,
1248 .name = "l2fib-mac-age-scanner-process",
1249};
1250/* *INDENT-ON* */
1251
Dave Barach97d8dc22016-08-15 15:31:15 -04001252clib_error_t *
1253l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001254{
Dave Barach97d8dc22016-08-15 15:31:15 -04001255 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256 l2fib_entry_key_t test_key;
1257 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001258
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001260 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261
Dave Barach97d8dc22016-08-15 15:31:15 -04001262 /* Create the hash table */
1263 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1264 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265
Dave Barach97d8dc22016-08-15 15:31:15 -04001266 /* verify the key constructor is good, since it is endian-sensitive */
Dave Barachb7b92992018-10-17 10:38:51 -04001267 clib_memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001268 test_mac[0] = 0x11;
1269 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001270 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001271 ASSERT (test_key.fields.mac[0] == 0x11);
1272 ASSERT (test_key.fields.bd_index == 0x1234);
1273
1274 return 0;
1275}
1276
1277VLIB_INIT_FUNCTION (l2fib_init);
1278
Dave Barach97d8dc22016-08-15 15:31:15 -04001279/*
1280 * fd.io coding-style-patch-verification: ON
1281 *
1282 * Local Variables:
1283 * eval: (c-set-style "gnu")
1284 * End:
1285 */