blob: 0ad56f38fe092e715584464a5668a96a6390f041 [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
Dave Barach97d8dc22016-08-15 15:31:15 -040057/** Format sw_if_index. If the value is ~0, use the text "N/A" */
58u8 *
59format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070060{
Dave Barach97d8dc22016-08-15 15:31:15 -040061 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 u32 sw_if_index = va_arg (*args, u32);
63 if (sw_if_index == ~0)
64 return format (s, "N/A");
Eyal Barib823df52017-06-12 17:07:22 +030065
66 vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
67 if (!swif)
Eyal Bari0f360dc2017-06-14 13:11:20 +030068 return format (s, "Stale");
Eyal Barib823df52017-06-12 17:07:22 +030069
70 return format (s, "%U", format_vnet_sw_interface_name, vnm,
71 vnet_get_sw_interface_safe (vnm, sw_if_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -070072}
73
Dave Barach97d8dc22016-08-15 15:31:15 -040074void
75l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
76 l2fib_entry_result_t ** l2fe_res)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
Dave Barach97d8dc22016-08-15 15:31:15 -040078 l2fib_main_t *msm = &l2fib_main;
79 BVT (clib_bihash) * h = &msm->mac_table;
Dave Barach908a5ea2017-07-14 12:42:21 -040080 BVT (clib_bihash_bucket) * b;
Dave Barach97d8dc22016-08-15 15:31:15 -040081 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 l2fib_entry_key_t key;
83 l2fib_entry_result_t result;
84 int i, j, k;
85
86 for (i = 0; i < h->nbuckets; i++)
87 {
88 b = &h->buckets[i];
89 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -040090 continue;
91 v = BV (clib_bihash_get_value) (h, b->offset);
92 for (j = 0; j < (1 << b->log2_pages); j++)
93 {
94 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
95 {
96 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
97 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Dave Barach97d8dc22016-08-15 15:31:15 -040099 key.raw = v->kvp[k].key;
100 result.raw = v->kvp[k].value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Dave Barach97d8dc22016-08-15 15:31:15 -0400102 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
103 {
104 vec_add1 (*l2fe_key, key);
105 vec_add1 (*l2fe_res, result);
106 }
107 }
108 v++;
109 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 }
111}
112
Chris Luke16bcf7d2016-09-01 14:31:46 -0400113/** Display the contents of the l2fib. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400115show_l2fib (vlib_main_t * vm,
116 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117{
Dave Barach97d8dc22016-08-15 15:31:15 -0400118 bd_main_t *bdm = &bd_main;
119 l2fib_main_t *msm = &l2fib_main;
Damjan Mariond171d482016-12-05 14:16:38 +0100120 l2_bridge_domain_t *bd_config;
Dave Barach97d8dc22016-08-15 15:31:15 -0400121 BVT (clib_bihash) * h = &msm->mac_table;
Dave Barach908a5ea2017-07-14 12:42:21 -0400122 BVT (clib_bihash_bucket) * b;
Dave Barach97d8dc22016-08-15 15:31:15 -0400123 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 l2fib_entry_key_t key;
125 l2fib_entry_result_t result;
126 u32 first_entry = 1;
127 u64 total_entries = 0;
128 int i, j, k;
129 u8 verbose = 0;
130 u8 raw = 0;
John Lo8d00fff2017-08-03 00:35:36 -0400131 u8 learn = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 u32 bd_id, bd_index = ~0;
Damjan Mariond171d482016-12-05 14:16:38 +0100133 u8 now = (u8) (vlib_time_now (vm) / 60);
134 u8 *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
136 if (unformat (input, "raw"))
137 raw = 1;
138 else if (unformat (input, "verbose"))
139 verbose = 1;
140 else if (unformat (input, "bd_index %d", &bd_index))
141 verbose = 1;
John Lo8d00fff2017-08-03 00:35:36 -0400142 else if (unformat (input, "learn"))
143 {
144 learn = 1;
145 verbose = 0;
146 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 else if (unformat (input, "bd_id %d", &bd_id))
148 {
149 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400150 if (p)
151 {
John Lo8d00fff2017-08-03 00:35:36 -0400152 if (learn == 0)
153 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 bd_index = p[0];
Dave Barach97d8dc22016-08-15 15:31:15 -0400155 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400157 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 vlib_cli_output (vm, "no such bridge domain id");
159 return 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400160 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 }
162
163 for (i = 0; i < h->nbuckets; i++)
164 {
165 b = &h->buckets[i];
166 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400167 continue;
168 v = BV (clib_bihash_get_value) (h, b->offset);
169 for (j = 0; j < (1 << b->log2_pages); j++)
170 {
171 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
172 {
173 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
174 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
John Lo8d00fff2017-08-03 00:35:36 -0400176 if ((verbose || learn) && first_entry)
Dave Barach97d8dc22016-08-15 15:31:15 -0400177 {
178 first_entry = 0;
179 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400180 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
181 "Mac-Address", "BD-Idx", "If-Idx",
182 "BSN-ISN", "Age(min)", "static", "filter",
183 "bvi", "Interface-Name");
Dave Barach97d8dc22016-08-15 15:31:15 -0400184 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barach97d8dc22016-08-15 15:31:15 -0400186 key.raw = v->kvp[k].key;
187 result.raw = v->kvp[k].value;
188
John Lo8d00fff2017-08-03 00:35:36 -0400189 if ((verbose || learn)
Dave Barach97d8dc22016-08-15 15:31:15 -0400190 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
191 {
John Lo8d00fff2017-08-03 00:35:36 -0400192 if (learn && result.fields.age_not)
193 {
194 total_entries++;
195 continue; /* skip provisioned macs */
196 }
197
Damjan Mariond171d482016-12-05 14:16:38 +0100198 bd_config = vec_elt_at_index (l2input_main.bd_configs,
199 key.fields.bd_index);
200
John Lo8d00fff2017-08-03 00:35:36 -0400201 if (bd_config->mac_age && !result.fields.age_not)
Damjan Mariond171d482016-12-05 14:16:38 +0100202 {
203 i16 delta = now - result.fields.timestamp;
204 delta += delta < 0 ? 256 : 0;
205 s = format (s, "%d", delta);
206 }
207 else
John Loda1f2c72017-03-24 20:11:15 -0400208 s = format (s, "-");
Damjan Mariond171d482016-12-05 14:16:38 +0100209
Dave Barach97d8dc22016-08-15 15:31:15 -0400210 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400211 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -0400212 format_ethernet_address, key.fields.mac,
213 key.fields.bd_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400214 result.fields.sw_if_index == ~0
215 ? -1 : result.fields.sw_if_index,
Eyal Bari7537e712017-04-27 14:07:55 +0300216 result.fields.sn.bd, result.fields.sn.swif,
John Loda1f2c72017-03-24 20:11:15 -0400217 s, result.fields.static_mac ? "*" : "-",
218 result.fields.filter ? "*" : "-",
219 result.fields.bvi ? "*" : "-",
220 format_vnet_sw_if_index_name_with_NA,
221 msm->vnet_main, result.fields.sw_if_index);
Damjan Mariond171d482016-12-05 14:16:38 +0100222 vec_reset_length (s);
Dave Barach97d8dc22016-08-15 15:31:15 -0400223 }
224 total_entries++;
225 }
226 v++;
227 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 }
229
230 if (total_entries == 0)
231 vlib_cli_output (vm, "no l2fib entries");
232 else
John Lo8d00fff2017-08-03 00:35:36 -0400233 {
234 l2learn_main_t *lm = &l2learn_main;
235 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
236 "Last scan time: %.4esec Learn limit: %d ",
237 total_entries, lm->global_learn_count,
238 msm->age_scan_duration, lm->global_learn_limit);
239 if (lm->client_pid)
240 vlib_cli_output (vm, "L2MAC events client PID: %d "
241 "Last e-scan time: %.4esec Delay: %.2esec "
242 "Max macs in event: %d",
243 lm->client_pid, msm->evt_scan_duration,
244 msm->event_scan_delay, msm->max_macs_in_event);
245 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
247 if (raw)
248 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Dave Barach97d8dc22016-08-15 15:31:15 -0400249 BV (format_bihash), h, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Damjan Mariond171d482016-12-05 14:16:38 +0100251 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 return 0;
253}
254
Billy McFall22aa3e92016-09-09 08:46:40 -0400255/*?
256 * This command dispays the MAC Address entries of the L2 FIB table.
257 * Output can be filtered to just get the number of MAC Addresses or display
258 * each MAC Address for all bridge domains or just a single bridge domain.
259 *
260 * @cliexpar
261 * Example of how to display the number of MAC Address entries in the L2
262 * FIB table:
263 * @cliexstart{show l2fib}
264 * 3 l2fib entries
265 * @cliexend
266 * Example of how to display all the MAC Address entries in the L2
267 * FIB table:
268 * @cliexstart{show l2fib verbose}
269 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
270 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
271 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
272 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
273 * 3 l2fib entries
274 * @cliexend
275?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400276/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
278 .path = "show l2fib",
John Lo8d00fff2017-08-03 00:35:36 -0400279 .short_help = "show l2fib [verbose | learn | bd_id <nn> | bd_index <nn> | raw",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 .function = show_l2fib,
281};
Dave Barach97d8dc22016-08-15 15:31:15 -0400282/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
284
Dave Barach97d8dc22016-08-15 15:31:15 -0400285/* Remove all entries from the l2fib */
286void
Eyal Bari7537e712017-04-27 14:07:55 +0300287l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288{
Dave Barach97d8dc22016-08-15 15:31:15 -0400289 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Eyal Bari7537e712017-04-27 14:07:55 +0300291 /* Remove all entries */
292 BV (clib_bihash_free) (&mp->mac_table);
293 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
294 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 l2learn_main.global_learn_count = 0;
296}
297
Chris Luke16bcf7d2016-09-01 14:31:46 -0400298/** Clear all entries in L2FIB.
299 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400300 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400302clear_l2fib (vlib_main_t * vm,
303 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304{
Eyal Bari7537e712017-04-27 14:07:55 +0300305 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 return 0;
307}
308
Billy McFall22aa3e92016-09-09 08:46:40 -0400309/*?
310 * This command clears all the MAC Address entries from the L2 FIB table.
311 *
312 * @cliexpar
313 * Example of how to clear the L2 FIB Table:
314 * @cliexcmd{clear l2fib}
315 * Example to show the L2 FIB Table has been cleared:
316 * @cliexstart{show l2fib verbose}
317 * no l2fib entries
318 * @cliexend
319?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400320/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
322 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400323 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 .function = clear_l2fib,
325};
Dave Barach97d8dc22016-08-15 15:31:15 -0400326/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Eyal Bari7537e712017-04-27 14:07:55 +0300328static inline l2fib_seq_num_t
329l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
330{
Eyal Bari7537e712017-04-27 14:07:55 +0300331 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
332 /* *INDENT-OFF* */
333 return (l2fib_seq_num_t) {
Eyal Bari0f360dc2017-06-14 13:11:20 +0300334 .swif = *l2fib_swif_seq_num (sw_if_index),
Eyal Bari7537e712017-04-27 14:07:55 +0300335 .bd = bd_config->seq_num,
336 };
337 /* *INDENT-ON* */
338}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339
Dave Barach97d8dc22016-08-15 15:31:15 -0400340/**
341 * Add an entry to the l2fib.
342 * If the entry already exists then overwrite it
343 */
344void
Eyal Bari7537e712017-04-27 14:07:55 +0300345l2fib_add_entry (u64 mac, u32 bd_index,
John Lo8d00fff2017-08-03 00:35:36 -0400346 u32 sw_if_index, u8 static_mac, u8 filter_mac, u8 bvi_mac)
Dave Barach97d8dc22016-08-15 15:31:15 -0400347{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 l2fib_entry_key_t key;
349 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400350 __attribute__ ((unused)) u32 bucket_contents;
John Lo8d00fff2017-08-03 00:35:36 -0400351 l2fib_main_t *fm = &l2fib_main;
352 l2learn_main_t *lm = &l2learn_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400353 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Dave Barach97d8dc22016-08-15 15:31:15 -0400355 /* set up key */
356 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
357
John Lo8d00fff2017-08-03 00:35:36 -0400358 /* check if entry alread exist */
359 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
360 {
361 /* decrement counter if overwriting a learned mac */
362 result.raw = kv.value;
363 if ((result.fields.age_not == 0) && (lm->global_learn_count))
364 lm->global_learn_count--;
365 }
366
Dave Barach97d8dc22016-08-15 15:31:15 -0400367 /* set up result */
368 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 result.fields.sw_if_index = sw_if_index;
370 result.fields.static_mac = static_mac;
371 result.fields.filter = filter_mac;
372 result.fields.bvi = bvi_mac;
John Lo8d00fff2017-08-03 00:35:36 -0400373 result.fields.age_not = 1; /* no aging for provisioned entry */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374
375 kv.key = key.raw;
376 kv.value = result.raw;
377
John Lo8d00fff2017-08-03 00:35:36 -0400378 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379}
380
Dave Barach97d8dc22016-08-15 15:31:15 -0400381/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400382 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400383 * The CLI format is:
384 * l2fib add <mac> <bd> <intf> [static] [bvi]
385 * l2fib add <mac> <bd> filter
386 * Note that filter and bvi entries are always static
387 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400389l2fib_add (vlib_main_t * vm,
390 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391{
Dave Barach97d8dc22016-08-15 15:31:15 -0400392 bd_main_t *bdm = &bd_main;
393 vnet_main_t *vnm = vnet_get_main ();
394 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 u64 mac;
396 u32 bd_id;
397 u32 bd_index;
398 u32 sw_if_index = ~0;
399 u32 filter_mac = 0;
400 u32 static_mac = 0;
401 u32 bvi_mac = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400402 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403
Dave Barach97d8dc22016-08-15 15:31:15 -0400404 if (!unformat_user (input, unformat_ethernet_address, &mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 {
406 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400407 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 goto done;
409 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400410
411 if (!unformat (input, "%d", &bd_id))
412 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400414 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400416 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417
418 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400419 if (!p)
420 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
422 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400423 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 bd_index = p[0];
425
Dave Barach97d8dc22016-08-15 15:31:15 -0400426 if (unformat (input, "filter"))
427 {
428 filter_mac = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 static_mac = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400430
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400432 else
433 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Dave Barach97d8dc22016-08-15 15:31:15 -0400435 if (!unformat_user
436 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
437 {
438 error = clib_error_return (0, "unknown interface `%U'",
439 format_unformat_error, input);
440 goto done;
441 }
442 if (unformat (input, "static"))
443 {
444 static_mac = 1;
445 }
446 else if (unformat (input, "bvi"))
447 {
448 bvi_mac = 1;
449 static_mac = 1;
450 }
451 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452
John Lob2fd6cb2017-07-12 19:56:45 -0400453 if (vec_len (l2input_main.configs) <= sw_if_index)
454 {
455 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
456 sw_if_index);
457 goto done;
458 }
459
Eyal Bari31a71ab2017-06-25 14:42:33 +0300460 if (filter_mac)
461 l2fib_add_filter_entry (mac, bd_index);
462 else
463 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400464
465done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 return error;
467}
468
Billy McFall22aa3e92016-09-09 08:46:40 -0400469/*?
470 * This command adds a MAC Address entry to the L2 FIB table
471 * of an existing bridge-domain. The MAC Address can be static
472 * or dynamic. This command also allows a filter to be added,
473 * such that packets with given MAC Addresses (source mac or
474 * destination mac match) are dropped.
475 *
476 * @cliexpar
477 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
478 * of a bridge-domain (where 200 is the bridge-domain-id):
479 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
480 * Example of how to add a static MAC Address entry to the L2 FIB table
481 * of a bridge-domain (where 200 is the bridge-domain-id):
482 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
483 * Example of how to add a filter such that a packet with the given MAC
484 * Address will be dropped in a given bridge-domain (where 200 is the
485 * bridge-domain-id):
486 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
487 * Example of show command of the provisioned MAC Addresses and filters:
488 * @cliexstart{show l2fib verbose}
489 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
490 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
491 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
492 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
493 * 3 l2fib entries
494 * @cliexend
495?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400496/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
498 .path = "l2fib add",
Billy McFall22aa3e92016-09-09 08:46:40 -0400499 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 .function = l2fib_add,
501};
Dave Barach97d8dc22016-08-15 15:31:15 -0400502/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
504
505static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400506l2fib_test_command_fn (vlib_main_t * vm,
507 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508{
Dave Barach97d8dc22016-08-15 15:31:15 -0400509 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 u64 mac, save_mac;
511 u32 bd_index = 0;
512 u32 sw_if_index = 8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513 u32 bvi_mac = 0;
514 u32 is_add = 0;
515 u32 is_del = 0;
516 u32 is_check = 0;
517 u32 count = 1;
518 int mac_set = 0;
519 int i;
520
Dave Barach97d8dc22016-08-15 15:31:15 -0400521 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522 {
523 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400524 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400526 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400528 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400530 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400532 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400534 break;
535 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
537 if (mac_set == 0)
538 return clib_error_return (0, "mac not set");
539
540 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400541 return clib_error_return (0,
542 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
544 save_mac = mac;
545
546 if (is_add)
547 {
548 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400549 {
550 u64 tmp;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300551 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, mac, bvi_mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400552 tmp = clib_net_to_host_u64 (mac);
553 tmp >>= 16;
554 tmp++;
555 tmp <<= 16;
556 mac = clib_host_to_net_u64 (tmp);
557 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 }
559
560 if (is_check)
561 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400562 BVT (clib_bihash_kv) kv;
563 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
565 mac = save_mac;
566
567 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400568 {
569 u64 tmp;
570 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
571 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
572 {
573 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
574 break;
575 }
576 tmp = clib_net_to_host_u64 (mac);
577 tmp >>= 16;
578 tmp++;
579 tmp <<= 16;
580 mac = clib_host_to_net_u64 (tmp);
581 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 }
583
584 if (is_del)
585 {
586 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400587 {
588 u64 tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589
Dave Barach97d8dc22016-08-15 15:31:15 -0400590 l2fib_del_entry (mac, bd_index);
591
592 tmp = clib_net_to_host_u64 (mac);
593 tmp >>= 16;
594 tmp++;
595 tmp <<= 16;
596 mac = clib_host_to_net_u64 (tmp);
597 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 }
599
600 return error;
601}
602
Billy McFall22aa3e92016-09-09 08:46:40 -0400603/*?
604 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
605 * bridge domain (bridge-domain-id of 0) to be modified.
606 *
607 * @cliexpar
608 * @parblock
609 * Example of how to add a set of 4 sequential MAC Address entries to L2
610 * FIB table of the default bridge-domain:
611 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
612 *
613 * Show the set of 4 sequential MAC Address entries that were added:
614 * @cliexstart{show l2fib verbose}
615 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
616 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
617 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
618 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
619 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
620 * 4 l2fib entries
621 * @cliexend
622 *
623 * Example of how to check that the set of 4 sequential MAC Address
624 * entries were added to L2 FIB table of the default
625 * bridge-domain. Used a count of 5 to produce an error:
626 *
627 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
628 * The output of the check command is in the log files. Log file
629 * location may vary based on your OS and Version:
630 *
631 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
632 *
633 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
634 *
635 * Example of how to delete a set of 4 sequential MAC Address entries
636 * from L2 FIB table of the default bridge-domain:
637 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
638 * @endparblock
639?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400640/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641VLIB_CLI_COMMAND (l2fib_test_command, static) = {
642 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400643 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 .function = l2fib_test_command_fn,
645};
Dave Barach97d8dc22016-08-15 15:31:15 -0400646/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647
648
Dave Barach97d8dc22016-08-15 15:31:15 -0400649/**
650 * Delete an entry from the l2fib.
651 * Return 0 if the entry was deleted, or 1 if it was not found
652 */
Eyal Bari7537e712017-04-27 14:07:55 +0300653static u32
654l2fib_del_entry_by_key (u64 raw_key)
Dave Barach97d8dc22016-08-15 15:31:15 -0400655{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
657 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400658 l2fib_main_t *mp = &l2fib_main;
659 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660
Dave Barach97d8dc22016-08-15 15:31:15 -0400661 /* set up key */
Eyal Bari7537e712017-04-27 14:07:55 +0300662 kv.key = raw_key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
Dave Barach97d8dc22016-08-15 15:31:15 -0400664 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665 return 1;
666
667 result.raw = kv.value;
668
Dave Barach97d8dc22016-08-15 15:31:15 -0400669 /* decrement counter if dynamically learned mac */
John Lo8d00fff2017-08-03 00:35:36 -0400670 if ((result.fields.age_not == 0) && (l2learn_main.global_learn_count))
671 l2learn_main.global_learn_count--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672
Dave Barach97d8dc22016-08-15 15:31:15 -0400673 /* Remove entry from hash table */
674 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 return 0;
676}
677
Dave Barach97d8dc22016-08-15 15:31:15 -0400678/**
Eyal Bari7537e712017-04-27 14:07:55 +0300679 * Delete an entry from the l2fib.
680 * Return 0 if the entry was deleted, or 1 if it was not found
681 */
682u32
683l2fib_del_entry (u64 mac, u32 bd_index)
684{
685 return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
686}
687
688/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400689 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400690 * The CLI format is:
691 * l2fib del <mac> <bd-id>
692 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400694l2fib_del (vlib_main_t * vm,
695 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696{
Dave Barach97d8dc22016-08-15 15:31:15 -0400697 bd_main_t *bdm = &bd_main;
698 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 u64 mac;
700 u32 bd_id;
701 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400702 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703
Dave Barach97d8dc22016-08-15 15:31:15 -0400704 if (!unformat_user (input, unformat_ethernet_address, &mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705 {
706 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400707 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708 goto done;
709 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400710
711 if (!unformat (input, "%d", &bd_id))
712 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400714 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400716 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717
718 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400719 if (!p)
720 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
722 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400723 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724 bd_index = p[0];
725
Dave Barach97d8dc22016-08-15 15:31:15 -0400726 /* Delete the entry */
727 if (l2fib_del_entry (mac, bd_index))
728 {
729 error = clib_error_return (0, "mac entry not found");
730 goto done;
731 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732
Dave Barach97d8dc22016-08-15 15:31:15 -0400733done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 return error;
735}
736
Billy McFall22aa3e92016-09-09 08:46:40 -0400737/*?
738 * This command deletes an existing MAC Address entry from the L2 FIB
739 * table of an existing bridge-domain.
740 *
741 * @cliexpar
742 * 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):
743 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
744?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400745/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
747 .path = "l2fib del",
Billy McFall22aa3e92016-09-09 08:46:40 -0400748 .short_help = "l2fib del <mac> <bridge-domain-id>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 .function = l2fib_del,
750};
Dave Barach97d8dc22016-08-15 15:31:15 -0400751/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752
John Loda1f2c72017-03-24 20:11:15 -0400753/**
754 Kick off ager to scan MACs to age/delete MAC entries
755*/
756void
757l2fib_start_ager_scan (vlib_main_t * vm)
758{
Eyal Bari24db0ec2017-09-27 21:43:51 +0300759 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
John Loda1f2c72017-03-24 20:11:15 -0400760
761 /* check if there is at least one bd with mac aging enabled */
Eyal Bari24db0ec2017-09-27 21:43:51 +0300762 l2_bridge_domain_t *bd_config;
John Loda1f2c72017-03-24 20:11:15 -0400763 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300764 {
John Loda1f2c72017-03-24 20:11:15 -0400765 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300766 {
767 evt = L2_MAC_AGE_PROCESS_EVENT_START;
768 break;
769 }
770 }
John Loda1f2c72017-03-24 20:11:15 -0400771
772 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
Eyal Bari24db0ec2017-09-27 21:43:51 +0300773 evt, 0);
John Loda1f2c72017-03-24 20:11:15 -0400774}
775
776/**
Eyal Bari7537e712017-04-27 14:07:55 +0300777 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400778*/
779void
780l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
781{
Eyal Bari0f360dc2017-06-14 13:11:20 +0300782 *l2fib_swif_seq_num (sw_if_index) += 1;
John Loda1f2c72017-03-24 20:11:15 -0400783 l2fib_start_ager_scan (vm);
784}
785
786/**
Eyal Bari7537e712017-04-27 14:07:55 +0300787 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400788*/
789void
790l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
791{
Eyal Bari7537e712017-04-27 14:07:55 +0300792 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400793 bd_config->seq_num += 1;
794 l2fib_start_ager_scan (vm);
795}
796
797/**
Eyal Bari7537e712017-04-27 14:07:55 +0300798 Flush all non static MACs - flushes all valid BDs
799*/
800void
801l2fib_flush_all_mac (vlib_main_t * vm)
802{
803 l2_bridge_domain_t *bd_config;
804 vec_foreach (bd_config, l2input_main.bd_configs)
805 if (bd_is_valid (bd_config))
806 bd_config->seq_num += 1;
807
808 l2fib_start_ager_scan (vm);
809}
810
811
812/**
John Loda1f2c72017-03-24 20:11:15 -0400813 Flush MACs, except static ones, associated with an interface
814 The CLI format is:
815 l2fib flush-mac interface <if-name>
816*/
817static clib_error_t *
818l2fib_flush_mac_int (vlib_main_t * vm,
819 unformat_input_t * input, vlib_cli_command_t * cmd)
820{
821 vnet_main_t *vnm = vnet_get_main ();
822 clib_error_t *error = 0;
823 u32 sw_if_index;
824
825 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
826 {
827 error = clib_error_return (0, "unknown interface `%U'",
828 format_unformat_error, input);
829 goto done;
830 }
831
832 l2fib_flush_int_mac (vm, sw_if_index);
833
834done:
835 return error;
836}
837
Eyal Bari7537e712017-04-27 14:07:55 +0300838/**
839 Flush all MACs, except static ones
840 The CLI format is:
841 l2fib flush-mac all
842*/
843static clib_error_t *
844l2fib_flush_mac_all (vlib_main_t * vm,
845 unformat_input_t * input, vlib_cli_command_t * cmd)
846{
847 l2fib_flush_all_mac (vm);
848 return 0;
849}
850
851/*?
852 * This command kick off ager to delete all existing MAC Address entries,
853 * except static ones, associated with an interface from the L2 FIB table.
854 *
855 * @cliexpar
856 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
857 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
858?*/
859/* *INDENT-OFF* */
860VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
861 .path = "l2fib flush-mac all",
862 .short_help = "l2fib flush-mac all",
863 .function = l2fib_flush_mac_all,
864};
865/* *INDENT-ON* */
866
John Loda1f2c72017-03-24 20:11:15 -0400867/*?
868 * This command kick off ager to delete all existing MAC Address entries,
869 * except static ones, associated with an interface from the L2 FIB table.
870 *
871 * @cliexpar
872 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
873 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
874?*/
875/* *INDENT-OFF* */
876VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
877 .path = "l2fib flush-mac interface",
878 .short_help = "l2fib flush-mac interface <if-name>",
879 .function = l2fib_flush_mac_int,
880};
881/* *INDENT-ON* */
882
883/**
884 Flush bridge-domain MACs except static ones.
885 The CLI format is:
886 l2fib flush-mac bridge-domain <bd-id>
887*/
888static clib_error_t *
889l2fib_flush_mac_bd (vlib_main_t * vm,
890 unformat_input_t * input, vlib_cli_command_t * cmd)
891{
892 bd_main_t *bdm = &bd_main;
893 clib_error_t *error = 0;
894 u32 bd_index, bd_id;
895 uword *p;
896
897 if (!unformat (input, "%d", &bd_id))
898 {
899 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
900 format_unformat_error, input);
901 goto done;
902 }
903
904 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
905 if (p)
906 bd_index = *p;
907 else
908 return clib_error_return (0, "No such bridge domain %d", bd_id);
909
910 l2fib_flush_bd_mac (vm, bd_index);
911
912done:
913 return error;
914}
915
916/*?
917 * This command kick off ager to delete all existing MAC Address entries,
918 * except static ones, in a bridge domain from the L2 FIB table.
919 *
920 * @cliexpar
921 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
922 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
923?*/
924/* *INDENT-OFF* */
925VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
926 .path = "l2fib flush-mac bridge-domain",
927 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
928 .function = l2fib_flush_mac_bd,
929};
930/* *INDENT-ON* */
931
Eyal Bariafc47aa2017-04-20 14:45:17 +0300932clib_error_t *
933l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
934{
935 l2_input_config_t *config = l2input_intf_config (sw_if_index);
936 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
937 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
938 return 0;
939}
940
941VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942
Dave Barach97d8dc22016-08-15 15:31:15 -0400943BVT (clib_bihash) * get_mac_table (void)
944{
945 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946 return &mp->mac_table;
947}
948
John Lo8d00fff2017-08-03 00:35:36 -0400949static_always_inline void *
950allocate_mac_evt_buf (u32 client, u32 client_index)
951{
952 l2fib_main_t *fm = &l2fib_main;
953 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
954 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
955 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
956 mp->pid = htonl (client);
957 mp->client_index = client_index;
958 return mp;
959}
960
961static_always_inline f64
962l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
963{
964 l2fib_main_t *fm = &l2fib_main;
965 l2learn_main_t *lm = &l2learn_main;
966
967 BVT (clib_bihash) * h = &fm->mac_table;
968 int i, j, k;
969 f64 last_start = start_time;
970 f64 accum_t = 0;
971 f64 delta_t = 0;
972 u32 evt_idx = 0;
973 u32 learn_count = 0;
974 u32 client = lm->client_pid;
975 u32 cl_idx = lm->client_index;
976 vl_api_l2_macs_event_t *mp = 0;
977 unix_shared_memory_queue_t *q = 0;
978
979 if (client)
980 {
981 mp = allocate_mac_evt_buf (client, cl_idx);
982 q = vl_api_client_index_to_input_queue (lm->client_index);
983 }
984
985 for (i = 0; i < h->nbuckets; i++)
986 {
987 /* allow no more than 20us without a pause */
988 delta_t = vlib_time_now (vm) - last_start;
989 if (delta_t > 20e-6)
990 {
991 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
992 last_start = vlib_time_now (vm);
993 accum_t += delta_t;
994 }
995
996 if (i < (h->nbuckets - 3))
997 {
998 BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
999 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
1000 b = &h->buckets[i + 1];
1001 if (b->offset)
1002 {
1003 BVT (clib_bihash_value) * v =
1004 BV (clib_bihash_get_value) (h, b->offset);
1005 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1006 }
1007 }
1008
1009 BVT (clib_bihash_bucket) * b = &h->buckets[i];
1010 if (b->offset == 0)
1011 continue;
1012 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1013 for (j = 0; j < (1 << b->log2_pages); j++)
1014 {
1015 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1016 {
1017 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1018 continue;
1019
1020 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1021 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1022
1023 if (result.fields.age_not == 0)
1024 learn_count++;
1025
John Lo8d00fff2017-08-03 00:35:36 -04001026 if (client)
1027 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001028 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1029 {
1030 /* event message full, send it and start a new one */
1031 if (q && (q->cursize < q->maxsize))
1032 {
1033 mp->n_macs = htonl (evt_idx);
1034 vl_msg_api_send_shmem (q, (u8 *) & mp);
1035 mp = allocate_mac_evt_buf (client, cl_idx);
1036 }
1037 else
1038 {
Dave Barach59b25652017-09-10 15:04:27 -04001039 if (q)
1040 clib_warning ("MAC event to pid %d queue stuffed!"
1041 " %d MAC entries lost", client,
1042 evt_idx);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001043 }
1044 evt_idx = 0;
1045 }
1046
John Lo8d00fff2017-08-03 00:35:36 -04001047 if (result.fields.lrn_evt)
1048 {
1049 /* copy mac entry to event msg */
1050 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac,
1051 6);
1052 mp->mac[evt_idx].is_del = 0;
1053 mp->mac[evt_idx].sw_if_index =
1054 htonl (result.fields.sw_if_index);
1055 /* clear event bit and update mac entry */
1056 result.fields.lrn_evt = 0;
1057 BVT (clib_bihash_kv) kv;
1058 kv.key = key.raw;
1059 kv.value = result.raw;
1060 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1061 evt_idx++;
1062 continue; /* skip aging */
1063 }
1064 }
1065
1066 if (event_only || result.fields.age_not)
1067 continue; /* skip aging - static_mac alsways age_not */
1068
1069 /* start aging processing */
1070 u32 bd_index = key.fields.bd_index;
1071 u32 sw_if_index = result.fields.sw_if_index;
1072 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1073 if (result.fields.sn.as_u16 != sn)
1074 goto age_out; /* stale mac */
1075
1076 l2_bridge_domain_t *bd_config =
1077 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1078
1079 if (bd_config->mac_age == 0)
1080 continue; /* skip aging */
1081
1082 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1083 delta += delta < 0 ? 256 : 0;
1084
1085 if (delta < bd_config->mac_age)
1086 continue; /* still valid */
1087
1088 age_out:
1089 if (client)
1090 {
1091 /* copy mac entry to event msg */
1092 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac, 6);
1093 mp->mac[evt_idx].is_del = 1;
1094 mp->mac[evt_idx].sw_if_index =
1095 htonl (result.fields.sw_if_index);
1096 evt_idx++;
1097 }
1098 /* delete mac entry */
1099 BVT (clib_bihash_kv) kv;
1100 kv.key = key.raw;
1101 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1102 learn_count--;
1103 }
1104 v++;
1105 }
1106 }
1107
1108 /* keep learn count consistent */
1109 l2learn_main.global_learn_count = learn_count;
1110
1111 if (mp)
1112 {
1113 /* send any outstanding mac event message else free message buffer */
1114 if (evt_idx)
1115 {
1116 if (q && (q->cursize < q->maxsize))
1117 {
1118 mp->n_macs = htonl (evt_idx);
1119 vl_msg_api_send_shmem (q, (u8 *) & mp);
1120 }
1121 else
1122 {
Dave Barach59b25652017-09-10 15:04:27 -04001123 if (q)
1124 clib_warning ("MAC event to pid %d queue stuffed!"
1125 " %d MAC entries lost", client, evt_idx);
John Lo8d00fff2017-08-03 00:35:36 -04001126 vl_msg_api_free (mp);
1127 }
1128 }
1129 else
1130 vl_msg_api_free (mp);
1131 }
1132 return delta_t + accum_t;
1133}
1134
John Lo8d00fff2017-08-03 00:35:36 -04001135/* Maximum f64 value */
1136#define TIME_MAX (1.7976931348623157e+308)
1137
Damjan Mariond171d482016-12-05 14:16:38 +01001138static uword
1139l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1140 vlib_frame_t * f)
1141{
1142 uword event_type, *event_data = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001143 l2fib_main_t *fm = &l2fib_main;
1144 l2learn_main_t *lm = &l2learn_main;
Damjan Mariond171d482016-12-05 14:16:38 +01001145 bool enabled = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001146 f64 start_time, next_age_scan_time = TIME_MAX;
Damjan Mariond171d482016-12-05 14:16:38 +01001147
1148 while (1)
1149 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001150 if (lm->client_pid)
1151 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1152 else if (enabled)
John Lo8d00fff2017-08-03 00:35:36 -04001153 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001154 f64 t = next_age_scan_time - vlib_time_now (vm);
1155 vlib_process_wait_for_event_or_clock (vm, t);
John Lo8d00fff2017-08-03 00:35:36 -04001156 }
Damjan Mariond171d482016-12-05 14:16:38 +01001157 else
1158 vlib_process_wait_for_event (vm);
1159
1160 event_type = vlib_process_get_events (vm, &event_data);
1161 vec_reset_length (event_data);
1162
John Lo8d00fff2017-08-03 00:35:36 -04001163 start_time = vlib_time_now (vm);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001164 enum
1165 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
John Lo8d00fff2017-08-03 00:35:36 -04001166
Damjan Mariond171d482016-12-05 14:16:38 +01001167 switch (event_type)
1168 {
John Lo8d00fff2017-08-03 00:35:36 -04001169 case ~0: /* timer expired */
Eyal Bari24db0ec2017-09-27 21:43:51 +03001170 if (lm->client_pid != 0 && start_time < next_age_scan_time)
John Lo8d00fff2017-08-03 00:35:36 -04001171 scan = SCAN_MAC_EVENT;
Damjan Mariond171d482016-12-05 14:16:38 +01001172 break;
John Lo8d00fff2017-08-03 00:35:36 -04001173
Damjan Mariond171d482016-12-05 14:16:38 +01001174 case L2_MAC_AGE_PROCESS_EVENT_START:
1175 enabled = 1;
1176 break;
John Lo8d00fff2017-08-03 00:35:36 -04001177
Damjan Mariond171d482016-12-05 14:16:38 +01001178 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1179 enabled = 0;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001180 scan = SCAN_DISABLE;
1181 break;
John Lo8d00fff2017-08-03 00:35:36 -04001182
John Loda1f2c72017-03-24 20:11:15 -04001183 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
John Loda1f2c72017-03-24 20:11:15 -04001184 break;
John Lo8d00fff2017-08-03 00:35:36 -04001185
Damjan Mariond171d482016-12-05 14:16:38 +01001186 default:
1187 ASSERT (0);
1188 }
Eyal Bari7537e712017-04-27 14:07:55 +03001189
John Lo8d00fff2017-08-03 00:35:36 -04001190 if (scan == SCAN_MAC_EVENT)
1191 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1192 else
Eyal Bari24db0ec2017-09-27 21:43:51 +03001193 {
1194 if (scan == SCAN_MAC_AGE)
1195 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1196 if (scan == SCAN_DISABLE)
1197 {
1198 l2fib_main.age_scan_duration = 0;
1199 l2fib_main.evt_scan_duration = 0;
1200 }
1201 /* schedule next scan */
1202 if (enabled)
1203 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1204 else
1205 next_age_scan_time = TIME_MAX;
1206 }
Damjan Mariond171d482016-12-05 14:16:38 +01001207 }
1208 return 0;
1209}
1210
1211/* *INDENT-OFF* */
1212VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1213 .function = l2fib_mac_age_scanner_process,
1214 .type = VLIB_NODE_TYPE_PROCESS,
1215 .name = "l2fib-mac-age-scanner-process",
1216};
1217/* *INDENT-ON* */
1218
Dave Barach97d8dc22016-08-15 15:31:15 -04001219clib_error_t *
1220l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001221{
Dave Barach97d8dc22016-08-15 15:31:15 -04001222 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223 l2fib_entry_key_t test_key;
1224 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001225
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001227 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
Dave Barach97d8dc22016-08-15 15:31:15 -04001229 /* Create the hash table */
1230 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1231 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232
Dave Barach97d8dc22016-08-15 15:31:15 -04001233 /* verify the key constructor is good, since it is endian-sensitive */
1234 memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235 test_mac[0] = 0x11;
1236 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001237 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238 ASSERT (test_key.fields.mac[0] == 0x11);
1239 ASSERT (test_key.fields.bd_index == 0x1234);
1240
1241 return 0;
1242}
1243
1244VLIB_INIT_FUNCTION (l2fib_init);
1245
Dave Barach97d8dc22016-08-15 15:31:15 -04001246/*
1247 * fd.io coding-style-patch-verification: ON
1248 *
1249 * Local Variables:
1250 * eval: (c-set-style "gnu")
1251 * End:
1252 */