blob: d4207e351a305cd49d87c587daa2d7ef178eff31 [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
Billy McFall22aa3e92016-09-09 08:46:40 -040034/**
35 * @file
36 * @brief Ethernet MAC Address FIB Table Management.
37 *
38 * The MAC Address forwarding table for bridge-domains is called the l2fib.
39 * Entries are added automatically as part of mac learning, but MAC Addresses
40 * entries can also be added manually.
41 *
42 */
43
Dave Barach97d8dc22016-08-15 15:31:15 -040044typedef struct
45{
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47 /* hash table */
Dave Barach97d8dc22016-08-15 15:31:15 -040048 BVT (clib_bihash) mac_table;
Ed Warnickecb9cada2015-12-08 15:45:58 -070049
50 /* convenience variables */
Dave Barach97d8dc22016-08-15 15:31:15 -040051 vlib_main_t *vlib_main;
52 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053} l2fib_main_t;
54
55l2fib_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");
Dave Barach97d8dc22016-08-15 15:31:15 -040065 else
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 return format (s, "%U",
Dave Barach97d8dc22016-08-15 15:31:15 -040067 format_vnet_sw_interface_name, vnm,
68 vnet_get_sw_interface (vnm, sw_if_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -070069}
70
Dave Barach97d8dc22016-08-15 15:31:15 -040071void
72l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
73 l2fib_entry_result_t ** l2fe_res)
Ed Warnickecb9cada2015-12-08 15:45:58 -070074{
Dave Barach97d8dc22016-08-15 15:31:15 -040075 l2fib_main_t *msm = &l2fib_main;
76 BVT (clib_bihash) * h = &msm->mac_table;
77 clib_bihash_bucket_t *b;
78 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 l2fib_entry_key_t key;
80 l2fib_entry_result_t result;
81 int i, j, k;
82
83 for (i = 0; i < h->nbuckets; i++)
84 {
85 b = &h->buckets[i];
86 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -040087 continue;
88 v = BV (clib_bihash_get_value) (h, b->offset);
89 for (j = 0; j < (1 << b->log2_pages); j++)
90 {
91 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
92 {
93 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
94 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
Dave Barach97d8dc22016-08-15 15:31:15 -040096 key.raw = v->kvp[k].key;
97 result.raw = v->kvp[k].value;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Dave Barach97d8dc22016-08-15 15:31:15 -040099 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
100 {
101 vec_add1 (*l2fe_key, key);
102 vec_add1 (*l2fe_res, result);
103 }
104 }
105 v++;
106 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 }
108}
109
Chris Luke16bcf7d2016-09-01 14:31:46 -0400110/** Display the contents of the l2fib. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400112show_l2fib (vlib_main_t * vm,
113 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
Dave Barach97d8dc22016-08-15 15:31:15 -0400115 bd_main_t *bdm = &bd_main;
116 l2fib_main_t *msm = &l2fib_main;
Damjan Mariond171d482016-12-05 14:16:38 +0100117 l2_bridge_domain_t *bd_config;
Dave Barach97d8dc22016-08-15 15:31:15 -0400118 BVT (clib_bihash) * h = &msm->mac_table;
119 clib_bihash_bucket_t *b;
120 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 l2fib_entry_key_t key;
122 l2fib_entry_result_t result;
123 u32 first_entry = 1;
124 u64 total_entries = 0;
125 int i, j, k;
126 u8 verbose = 0;
127 u8 raw = 0;
128 u32 bd_id, bd_index = ~0;
Damjan Mariond171d482016-12-05 14:16:38 +0100129 u8 now = (u8) (vlib_time_now (vm) / 60);
130 u8 *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
132 if (unformat (input, "raw"))
133 raw = 1;
134 else if (unformat (input, "verbose"))
135 verbose = 1;
136 else if (unformat (input, "bd_index %d", &bd_index))
137 verbose = 1;
138 else if (unformat (input, "bd_id %d", &bd_id))
139 {
140 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400141 if (p)
142 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 verbose = 1;
144 bd_index = p[0];
Dave Barach97d8dc22016-08-15 15:31:15 -0400145 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400147 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 vlib_cli_output (vm, "no such bridge domain id");
149 return 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400150 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 }
152
153 for (i = 0; i < h->nbuckets; i++)
154 {
155 b = &h->buckets[i];
156 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400157 continue;
158 v = BV (clib_bihash_get_value) (h, b->offset);
159 for (j = 0; j < (1 << b->log2_pages); j++)
160 {
161 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
162 {
163 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
164 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Dave Barach97d8dc22016-08-15 15:31:15 -0400166 if (verbose && first_entry)
167 {
168 first_entry = 0;
169 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400170 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
171 "Mac-Address", "BD-Idx", "If-Idx",
172 "BSN-ISN", "Age(min)", "static", "filter",
173 "bvi", "Interface-Name");
Dave Barach97d8dc22016-08-15 15:31:15 -0400174 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Dave Barach97d8dc22016-08-15 15:31:15 -0400176 key.raw = v->kvp[k].key;
177 result.raw = v->kvp[k].value;
178
179 if (verbose
180 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
181 {
Damjan Mariond171d482016-12-05 14:16:38 +0100182 bd_config = vec_elt_at_index (l2input_main.bd_configs,
183 key.fields.bd_index);
184
John Loda1f2c72017-03-24 20:11:15 -0400185 if (bd_config->mac_age && !result.fields.static_mac)
Damjan Mariond171d482016-12-05 14:16:38 +0100186 {
187 i16 delta = now - result.fields.timestamp;
188 delta += delta < 0 ? 256 : 0;
189 s = format (s, "%d", delta);
190 }
191 else
John Loda1f2c72017-03-24 20:11:15 -0400192 s = format (s, "-");
Damjan Mariond171d482016-12-05 14:16:38 +0100193
Dave Barach97d8dc22016-08-15 15:31:15 -0400194 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400195 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -0400196 format_ethernet_address, key.fields.mac,
197 key.fields.bd_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400198 result.fields.sw_if_index == ~0
199 ? -1 : result.fields.sw_if_index,
Eyal Bari7537e712017-04-27 14:07:55 +0300200 result.fields.sn.bd, result.fields.sn.swif,
John Loda1f2c72017-03-24 20:11:15 -0400201 s, result.fields.static_mac ? "*" : "-",
202 result.fields.filter ? "*" : "-",
203 result.fields.bvi ? "*" : "-",
204 format_vnet_sw_if_index_name_with_NA,
205 msm->vnet_main, result.fields.sw_if_index);
Damjan Mariond171d482016-12-05 14:16:38 +0100206 vec_reset_length (s);
Dave Barach97d8dc22016-08-15 15:31:15 -0400207 }
208 total_entries++;
209 }
210 v++;
211 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 }
213
214 if (total_entries == 0)
215 vlib_cli_output (vm, "no l2fib entries");
216 else
John Lod48c8eb2017-05-05 12:35:25 -0400217 vlib_cli_output (vm,
218 "%lld l2fib entries with %d learned (or non-static) entries",
219 total_entries, l2learn_main.global_learn_count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
221 if (raw)
222 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Dave Barach97d8dc22016-08-15 15:31:15 -0400223 BV (format_bihash), h, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
Damjan Mariond171d482016-12-05 14:16:38 +0100225 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 return 0;
227}
228
Billy McFall22aa3e92016-09-09 08:46:40 -0400229/*?
230 * This command dispays the MAC Address entries of the L2 FIB table.
231 * Output can be filtered to just get the number of MAC Addresses or display
232 * each MAC Address for all bridge domains or just a single bridge domain.
233 *
234 * @cliexpar
235 * Example of how to display the number of MAC Address entries in the L2
236 * FIB table:
237 * @cliexstart{show l2fib}
238 * 3 l2fib entries
239 * @cliexend
240 * Example of how to display all the MAC Address entries in the L2
241 * FIB table:
242 * @cliexstart{show l2fib verbose}
243 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
244 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
245 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
246 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
247 * 3 l2fib entries
248 * @cliexend
249?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400250/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
252 .path = "show l2fib",
253 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
254 .function = show_l2fib,
255};
Dave Barach97d8dc22016-08-15 15:31:15 -0400256/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
258
Dave Barach97d8dc22016-08-15 15:31:15 -0400259/* Remove all entries from the l2fib */
260void
Eyal Bari7537e712017-04-27 14:07:55 +0300261l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262{
Dave Barach97d8dc22016-08-15 15:31:15 -0400263 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Eyal Bari7537e712017-04-27 14:07:55 +0300265 /* Remove all entries */
266 BV (clib_bihash_free) (&mp->mac_table);
267 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
268 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 l2learn_main.global_learn_count = 0;
270}
271
Chris Luke16bcf7d2016-09-01 14:31:46 -0400272/** Clear all entries in L2FIB.
273 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400274 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400276clear_l2fib (vlib_main_t * vm,
277 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278{
Eyal Bari7537e712017-04-27 14:07:55 +0300279 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 return 0;
281}
282
Billy McFall22aa3e92016-09-09 08:46:40 -0400283/*?
284 * This command clears all the MAC Address entries from the L2 FIB table.
285 *
286 * @cliexpar
287 * Example of how to clear the L2 FIB Table:
288 * @cliexcmd{clear l2fib}
289 * Example to show the L2 FIB Table has been cleared:
290 * @cliexstart{show l2fib verbose}
291 * no l2fib entries
292 * @cliexend
293?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400294/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
296 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400297 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 .function = clear_l2fib,
299};
Dave Barach97d8dc22016-08-15 15:31:15 -0400300/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
Eyal Bari7537e712017-04-27 14:07:55 +0300302static inline l2fib_seq_num_t
303l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
304{
305 l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
306 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
307 /* *INDENT-OFF* */
308 return (l2fib_seq_num_t) {
309 .swif = int_config->seq_num,
310 .bd = bd_config->seq_num,
311 };
312 /* *INDENT-ON* */
313}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
Dave Barach97d8dc22016-08-15 15:31:15 -0400315/**
316 * Add an entry to the l2fib.
317 * If the entry already exists then overwrite it
318 */
319void
Eyal Bari7537e712017-04-27 14:07:55 +0300320l2fib_add_entry (u64 mac, u32 bd_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400321 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
322{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 l2fib_entry_key_t key;
324 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400325 __attribute__ ((unused)) u32 bucket_contents;
326 l2fib_main_t *mp = &l2fib_main;
327 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328
Dave Barach97d8dc22016-08-15 15:31:15 -0400329 /* set up key */
330 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
331
332 /* set up result */
333 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 result.fields.sw_if_index = sw_if_index;
335 result.fields.static_mac = static_mac;
336 result.fields.filter = filter_mac;
337 result.fields.bvi = bvi_mac;
John Loda1f2c72017-03-24 20:11:15 -0400338 if (!static_mac)
Eyal Bari7537e712017-04-27 14:07:55 +0300339 result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
341 kv.key = key.raw;
342 kv.value = result.raw;
343
Dave Barach97d8dc22016-08-15 15:31:15 -0400344 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Dave Barach97d8dc22016-08-15 15:31:15 -0400346 /* increment counter if dynamically learned mac */
John Lod48c8eb2017-05-05 12:35:25 -0400347 if (result.fields.static_mac == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400348 {
349 l2learn_main.global_learn_count++;
350 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351}
352
Dave Barach97d8dc22016-08-15 15:31:15 -0400353/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400354 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400355 * The CLI format is:
356 * l2fib add <mac> <bd> <intf> [static] [bvi]
357 * l2fib add <mac> <bd> filter
358 * Note that filter and bvi entries are always static
359 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400361l2fib_add (vlib_main_t * vm,
362 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363{
Dave Barach97d8dc22016-08-15 15:31:15 -0400364 bd_main_t *bdm = &bd_main;
365 vnet_main_t *vnm = vnet_get_main ();
366 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 u64 mac;
368 u32 bd_id;
369 u32 bd_index;
370 u32 sw_if_index = ~0;
371 u32 filter_mac = 0;
372 u32 static_mac = 0;
373 u32 bvi_mac = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400374 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
Dave Barach97d8dc22016-08-15 15:31:15 -0400376 if (!unformat_user (input, unformat_ethernet_address, &mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377 {
378 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400379 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 goto done;
381 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400382
383 if (!unformat (input, "%d", &bd_id))
384 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400386 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400388 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389
390 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400391 if (!p)
392 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
394 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400395 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396 bd_index = p[0];
397
Dave Barach97d8dc22016-08-15 15:31:15 -0400398 if (unformat (input, "filter"))
399 {
400 filter_mac = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401 static_mac = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400402
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400404 else
405 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
Dave Barach97d8dc22016-08-15 15:31:15 -0400407 if (!unformat_user
408 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
409 {
410 error = clib_error_return (0, "unknown interface `%U'",
411 format_unformat_error, input);
412 goto done;
413 }
414 if (unformat (input, "static"))
415 {
416 static_mac = 1;
417 }
418 else if (unformat (input, "bvi"))
419 {
420 bvi_mac = 1;
421 static_mac = 1;
422 }
423 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Dave Barach97d8dc22016-08-15 15:31:15 -0400425 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
426 bvi_mac);
427
428done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 return error;
430}
431
Billy McFall22aa3e92016-09-09 08:46:40 -0400432/*?
433 * This command adds a MAC Address entry to the L2 FIB table
434 * of an existing bridge-domain. The MAC Address can be static
435 * or dynamic. This command also allows a filter to be added,
436 * such that packets with given MAC Addresses (source mac or
437 * destination mac match) are dropped.
438 *
439 * @cliexpar
440 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
441 * of a bridge-domain (where 200 is the bridge-domain-id):
442 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
443 * Example of how to add a static MAC Address entry to the L2 FIB table
444 * of a bridge-domain (where 200 is the bridge-domain-id):
445 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
446 * Example of how to add a filter such that a packet with the given MAC
447 * Address will be dropped in a given bridge-domain (where 200 is the
448 * bridge-domain-id):
449 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
450 * Example of show command of the provisioned MAC Addresses and filters:
451 * @cliexstart{show l2fib verbose}
452 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
453 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
454 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
455 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
456 * 3 l2fib entries
457 * @cliexend
458?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400459/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
461 .path = "l2fib add",
Billy McFall22aa3e92016-09-09 08:46:40 -0400462 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 .function = l2fib_add,
464};
Dave Barach97d8dc22016-08-15 15:31:15 -0400465/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
467
468static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400469l2fib_test_command_fn (vlib_main_t * vm,
470 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471{
Dave Barach97d8dc22016-08-15 15:31:15 -0400472 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473 u64 mac, save_mac;
474 u32 bd_index = 0;
475 u32 sw_if_index = 8;
476 u32 filter_mac = 0;
477 u32 bvi_mac = 0;
478 u32 is_add = 0;
479 u32 is_del = 0;
480 u32 is_check = 0;
481 u32 count = 1;
482 int mac_set = 0;
483 int i;
484
Dave Barach97d8dc22016-08-15 15:31:15 -0400485 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486 {
487 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400488 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400490 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400492 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400494 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400496 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400498 break;
499 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
501 if (mac_set == 0)
502 return clib_error_return (0, "mac not set");
503
504 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400505 return clib_error_return (0,
506 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507
508 save_mac = mac;
509
510 if (is_add)
511 {
512 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400513 {
514 u64 tmp;
515 l2fib_add_entry (mac, bd_index, sw_if_index, mac,
516 filter_mac, bvi_mac);
517 tmp = clib_net_to_host_u64 (mac);
518 tmp >>= 16;
519 tmp++;
520 tmp <<= 16;
521 mac = clib_host_to_net_u64 (tmp);
522 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 }
524
525 if (is_check)
526 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400527 BVT (clib_bihash_kv) kv;
528 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
530 mac = save_mac;
531
532 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400533 {
534 u64 tmp;
535 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
536 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
537 {
538 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
539 break;
540 }
541 tmp = clib_net_to_host_u64 (mac);
542 tmp >>= 16;
543 tmp++;
544 tmp <<= 16;
545 mac = clib_host_to_net_u64 (tmp);
546 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 }
548
549 if (is_del)
550 {
551 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400552 {
553 u64 tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
Dave Barach97d8dc22016-08-15 15:31:15 -0400555 l2fib_del_entry (mac, bd_index);
556
557 tmp = clib_net_to_host_u64 (mac);
558 tmp >>= 16;
559 tmp++;
560 tmp <<= 16;
561 mac = clib_host_to_net_u64 (tmp);
562 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 }
564
565 return error;
566}
567
Billy McFall22aa3e92016-09-09 08:46:40 -0400568/*?
569 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
570 * bridge domain (bridge-domain-id of 0) to be modified.
571 *
572 * @cliexpar
573 * @parblock
574 * Example of how to add a set of 4 sequential MAC Address entries to L2
575 * FIB table of the default bridge-domain:
576 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
577 *
578 * Show the set of 4 sequential MAC Address entries that were added:
579 * @cliexstart{show l2fib verbose}
580 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
581 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
582 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
583 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
584 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
585 * 4 l2fib entries
586 * @cliexend
587 *
588 * Example of how to check that the set of 4 sequential MAC Address
589 * entries were added to L2 FIB table of the default
590 * bridge-domain. Used a count of 5 to produce an error:
591 *
592 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
593 * The output of the check command is in the log files. Log file
594 * location may vary based on your OS and Version:
595 *
596 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
597 *
598 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
599 *
600 * Example of how to delete a set of 4 sequential MAC Address entries
601 * from L2 FIB table of the default bridge-domain:
602 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
603 * @endparblock
604?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400605/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606VLIB_CLI_COMMAND (l2fib_test_command, static) = {
607 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400608 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 .function = l2fib_test_command_fn,
610};
Dave Barach97d8dc22016-08-15 15:31:15 -0400611/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612
613
Dave Barach97d8dc22016-08-15 15:31:15 -0400614/**
615 * Delete an entry from the l2fib.
616 * Return 0 if the entry was deleted, or 1 if it was not found
617 */
Eyal Bari7537e712017-04-27 14:07:55 +0300618static u32
619l2fib_del_entry_by_key (u64 raw_key)
Dave Barach97d8dc22016-08-15 15:31:15 -0400620{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621
622 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400623 l2fib_main_t *mp = &l2fib_main;
624 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625
Dave Barach97d8dc22016-08-15 15:31:15 -0400626 /* set up key */
Eyal Bari7537e712017-04-27 14:07:55 +0300627 kv.key = raw_key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628
Dave Barach97d8dc22016-08-15 15:31:15 -0400629 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 return 1;
631
632 result.raw = kv.value;
633
Dave Barach97d8dc22016-08-15 15:31:15 -0400634 /* decrement counter if dynamically learned mac */
John Lod48c8eb2017-05-05 12:35:25 -0400635 if (result.fields.static_mac == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400636 {
637 if (l2learn_main.global_learn_count > 0)
638 {
639 l2learn_main.global_learn_count--;
640 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642
Dave Barach97d8dc22016-08-15 15:31:15 -0400643 /* Remove entry from hash table */
644 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 return 0;
646}
647
Dave Barach97d8dc22016-08-15 15:31:15 -0400648/**
Eyal Bari7537e712017-04-27 14:07:55 +0300649 * Delete an entry from the l2fib.
650 * Return 0 if the entry was deleted, or 1 if it was not found
651 */
652u32
653l2fib_del_entry (u64 mac, u32 bd_index)
654{
655 return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
656}
657
658/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400659 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400660 * The CLI format is:
661 * l2fib del <mac> <bd-id>
662 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400664l2fib_del (vlib_main_t * vm,
665 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666{
Dave Barach97d8dc22016-08-15 15:31:15 -0400667 bd_main_t *bdm = &bd_main;
668 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669 u64 mac;
670 u32 bd_id;
671 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400672 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Dave Barach97d8dc22016-08-15 15:31:15 -0400674 if (!unformat_user (input, unformat_ethernet_address, &mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 {
676 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400677 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 goto done;
679 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400680
681 if (!unformat (input, "%d", &bd_id))
682 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400684 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400686 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687
688 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400689 if (!p)
690 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
692 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400693 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 bd_index = p[0];
695
Dave Barach97d8dc22016-08-15 15:31:15 -0400696 /* Delete the entry */
697 if (l2fib_del_entry (mac, bd_index))
698 {
699 error = clib_error_return (0, "mac entry not found");
700 goto done;
701 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702
Dave Barach97d8dc22016-08-15 15:31:15 -0400703done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 return error;
705}
706
Billy McFall22aa3e92016-09-09 08:46:40 -0400707/*?
708 * This command deletes an existing MAC Address entry from the L2 FIB
709 * table of an existing bridge-domain.
710 *
711 * @cliexpar
712 * 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):
713 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
714?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400715/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
717 .path = "l2fib del",
Billy McFall22aa3e92016-09-09 08:46:40 -0400718 .short_help = "l2fib del <mac> <bridge-domain-id>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719 .function = l2fib_del,
720};
Dave Barach97d8dc22016-08-15 15:31:15 -0400721/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
John Loda1f2c72017-03-24 20:11:15 -0400723/**
724 Kick off ager to scan MACs to age/delete MAC entries
725*/
726void
727l2fib_start_ager_scan (vlib_main_t * vm)
728{
729 l2_bridge_domain_t *bd_config;
730 int enable = 0;
731
732 /* check if there is at least one bd with mac aging enabled */
733 vec_foreach (bd_config, l2input_main.bd_configs)
734 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
735 enable = 1;
736
737 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
738 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
739 L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
740}
741
742/**
Eyal Bari7537e712017-04-27 14:07:55 +0300743 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400744*/
745void
746l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
747{
Eyal Bari7537e712017-04-27 14:07:55 +0300748 l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
John Loda1f2c72017-03-24 20:11:15 -0400749 int_config->seq_num += 1;
750 l2fib_start_ager_scan (vm);
751}
752
753/**
Eyal Bari7537e712017-04-27 14:07:55 +0300754 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400755*/
756void
757l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
758{
Eyal Bari7537e712017-04-27 14:07:55 +0300759 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400760 bd_config->seq_num += 1;
761 l2fib_start_ager_scan (vm);
762}
763
764/**
Eyal Bari7537e712017-04-27 14:07:55 +0300765 Flush all non static MACs - flushes all valid BDs
766*/
767void
768l2fib_flush_all_mac (vlib_main_t * vm)
769{
770 l2_bridge_domain_t *bd_config;
771 vec_foreach (bd_config, l2input_main.bd_configs)
772 if (bd_is_valid (bd_config))
773 bd_config->seq_num += 1;
774
775 l2fib_start_ager_scan (vm);
776}
777
778
779/**
John Loda1f2c72017-03-24 20:11:15 -0400780 Flush MACs, except static ones, associated with an interface
781 The CLI format is:
782 l2fib flush-mac interface <if-name>
783*/
784static clib_error_t *
785l2fib_flush_mac_int (vlib_main_t * vm,
786 unformat_input_t * input, vlib_cli_command_t * cmd)
787{
788 vnet_main_t *vnm = vnet_get_main ();
789 clib_error_t *error = 0;
790 u32 sw_if_index;
791
792 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
793 {
794 error = clib_error_return (0, "unknown interface `%U'",
795 format_unformat_error, input);
796 goto done;
797 }
798
799 l2fib_flush_int_mac (vm, sw_if_index);
800
801done:
802 return error;
803}
804
Eyal Bari7537e712017-04-27 14:07:55 +0300805/**
806 Flush all MACs, except static ones
807 The CLI format is:
808 l2fib flush-mac all
809*/
810static clib_error_t *
811l2fib_flush_mac_all (vlib_main_t * vm,
812 unformat_input_t * input, vlib_cli_command_t * cmd)
813{
814 l2fib_flush_all_mac (vm);
815 return 0;
816}
817
818/*?
819 * This command kick off ager to delete all existing MAC Address entries,
820 * except static ones, associated with an interface from the L2 FIB table.
821 *
822 * @cliexpar
823 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
824 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
825?*/
826/* *INDENT-OFF* */
827VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
828 .path = "l2fib flush-mac all",
829 .short_help = "l2fib flush-mac all",
830 .function = l2fib_flush_mac_all,
831};
832/* *INDENT-ON* */
833
John Loda1f2c72017-03-24 20:11:15 -0400834/*?
835 * This command kick off ager to delete all existing MAC Address entries,
836 * except static ones, associated with an interface from the L2 FIB table.
837 *
838 * @cliexpar
839 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
840 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
841?*/
842/* *INDENT-OFF* */
843VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
844 .path = "l2fib flush-mac interface",
845 .short_help = "l2fib flush-mac interface <if-name>",
846 .function = l2fib_flush_mac_int,
847};
848/* *INDENT-ON* */
849
850/**
851 Flush bridge-domain MACs except static ones.
852 The CLI format is:
853 l2fib flush-mac bridge-domain <bd-id>
854*/
855static clib_error_t *
856l2fib_flush_mac_bd (vlib_main_t * vm,
857 unformat_input_t * input, vlib_cli_command_t * cmd)
858{
859 bd_main_t *bdm = &bd_main;
860 clib_error_t *error = 0;
861 u32 bd_index, bd_id;
862 uword *p;
863
864 if (!unformat (input, "%d", &bd_id))
865 {
866 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
867 format_unformat_error, input);
868 goto done;
869 }
870
871 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
872 if (p)
873 bd_index = *p;
874 else
875 return clib_error_return (0, "No such bridge domain %d", bd_id);
876
877 l2fib_flush_bd_mac (vm, bd_index);
878
879done:
880 return error;
881}
882
883/*?
884 * This command kick off ager to delete all existing MAC Address entries,
885 * except static ones, in a bridge domain from the L2 FIB table.
886 *
887 * @cliexpar
888 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
889 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
890?*/
891/* *INDENT-OFF* */
892VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
893 .path = "l2fib flush-mac bridge-domain",
894 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
895 .function = l2fib_flush_mac_bd,
896};
897/* *INDENT-ON* */
898
Eyal Bariafc47aa2017-04-20 14:45:17 +0300899clib_error_t *
900l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
901{
902 l2_input_config_t *config = l2input_intf_config (sw_if_index);
903 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
904 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
905 return 0;
906}
907
908VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909
Dave Barach97d8dc22016-08-15 15:31:15 -0400910BVT (clib_bihash) * get_mac_table (void)
911{
912 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913 return &mp->mac_table;
914}
915
Damjan Mariond171d482016-12-05 14:16:38 +0100916static uword
917l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
918 vlib_frame_t * f)
919{
920 uword event_type, *event_data = 0;
921 l2fib_main_t *msm = &l2fib_main;
Damjan Mariond171d482016-12-05 14:16:38 +0100922 bool enabled = 0;
923 f64 start_time, last_run_duration = 0, t;
Damjan Mariond171d482016-12-05 14:16:38 +0100924
925 while (1)
926 {
927 if (enabled)
928 vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
929 else
930 vlib_process_wait_for_event (vm);
931
932 event_type = vlib_process_get_events (vm, &event_data);
933 vec_reset_length (event_data);
934
935 switch (event_type)
936 {
937 case ~0:
938 break;
939 case L2_MAC_AGE_PROCESS_EVENT_START:
940 enabled = 1;
941 break;
942 case L2_MAC_AGE_PROCESS_EVENT_STOP:
943 enabled = 0;
944 continue;
John Loda1f2c72017-03-24 20:11:15 -0400945 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
946 enabled = 0;
947 break;
Damjan Mariond171d482016-12-05 14:16:38 +0100948 default:
949 ASSERT (0);
950 }
951 last_run_duration = start_time = vlib_time_now (vm);
Eyal Bari7537e712017-04-27 14:07:55 +0300952
953 BVT (clib_bihash) * h = &msm->mac_table;
954 int i, j, k;
Damjan Mariond171d482016-12-05 14:16:38 +0100955 for (i = 0; i < h->nbuckets; i++)
956 {
957 /* Allow no more than 10us without a pause */
958 t = vlib_time_now (vm);
959 if (t > start_time + 10e-6)
960 {
961 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
962 start_time = vlib_time_now (vm);
963 }
964
965 if (i < (h->nbuckets - 3))
966 {
Eyal Bari7537e712017-04-27 14:07:55 +0300967 clib_bihash_bucket_t *b = &h->buckets[i + 3];
Damjan Mariond171d482016-12-05 14:16:38 +0100968 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
969 b = &h->buckets[i + 1];
970 if (b->offset)
971 {
Eyal Bari7537e712017-04-27 14:07:55 +0300972 BVT (clib_bihash_value) * v =
973 BV (clib_bihash_get_value) (h, b->offset);
Damjan Mariond171d482016-12-05 14:16:38 +0100974 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
975 }
976 }
977
Eyal Bari7537e712017-04-27 14:07:55 +0300978 clib_bihash_bucket_t *b = &h->buckets[i];
Damjan Mariond171d482016-12-05 14:16:38 +0100979 if (b->offset == 0)
980 continue;
Eyal Bari7537e712017-04-27 14:07:55 +0300981 BVT (clib_bihash_value) * v =
982 BV (clib_bihash_get_value) (h, b->offset);
Damjan Mariond171d482016-12-05 14:16:38 +0100983 for (j = 0; j < (1 << b->log2_pages); j++)
984 {
985 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
986 {
987 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
988 continue;
989
Eyal Bari7537e712017-04-27 14:07:55 +0300990 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
991 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
Damjan Mariond171d482016-12-05 14:16:38 +0100992
993 if (result.fields.static_mac)
994 continue;
995
Eyal Bari7537e712017-04-27 14:07:55 +0300996 u32 bd_index = key.fields.bd_index;
997 u32 sw_if_index = result.fields.sw_if_index;
998 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
999 if (result.fields.sn.as_u16 != sn)
John Loda1f2c72017-03-24 20:11:15 -04001000 {
Eyal Bari7537e712017-04-27 14:07:55 +03001001 l2fib_del_entry_by_key (key.raw);
John Loda1f2c72017-03-24 20:11:15 -04001002 continue;
1003 }
Eyal Bari7537e712017-04-27 14:07:55 +03001004 l2_bridge_domain_t *bd_config =
1005 vec_elt_at_index (l2input_main.bd_configs, bd_index);
Damjan Mariond171d482016-12-05 14:16:38 +01001006
1007 if (bd_config->mac_age == 0)
1008 continue;
1009
Eyal Bari7537e712017-04-27 14:07:55 +03001010 i16 delta =
1011 (u8) (start_time / 60) - result.fields.timestamp;
Damjan Mariond171d482016-12-05 14:16:38 +01001012 delta += delta < 0 ? 256 : 0;
1013
1014 if (delta > bd_config->mac_age)
Eyal Bari7537e712017-04-27 14:07:55 +03001015 l2fib_del_entry_by_key (key.raw);
Damjan Mariond171d482016-12-05 14:16:38 +01001016 }
1017 v++;
1018 }
1019 }
1020 last_run_duration = vlib_time_now (vm) - last_run_duration;
1021 }
1022 return 0;
1023}
1024
1025/* *INDENT-OFF* */
1026VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1027 .function = l2fib_mac_age_scanner_process,
1028 .type = VLIB_NODE_TYPE_PROCESS,
1029 .name = "l2fib-mac-age-scanner-process",
1030};
1031/* *INDENT-ON* */
1032
Dave Barach97d8dc22016-08-15 15:31:15 -04001033clib_error_t *
1034l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035{
Dave Barach97d8dc22016-08-15 15:31:15 -04001036 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037 l2fib_entry_key_t test_key;
1038 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001039
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001041 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042
Dave Barach97d8dc22016-08-15 15:31:15 -04001043 /* Create the hash table */
1044 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1045 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046
Dave Barach97d8dc22016-08-15 15:31:15 -04001047 /* verify the key constructor is good, since it is endian-sensitive */
1048 memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049 test_mac[0] = 0x11;
1050 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001051 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052 ASSERT (test_key.fields.mac[0] == 0x11);
1053 ASSERT (test_key.fields.bd_index == 0x1234);
1054
1055 return 0;
1056}
1057
1058VLIB_INIT_FUNCTION (l2fib_init);
1059
Dave Barach97d8dc22016-08-15 15:31:15 -04001060/*
1061 * fd.io coding-style-patch-verification: ON
1062 *
1063 * Local Variables:
1064 * eval: (c-set-style "gnu")
1065 * End:
1066 */