blob: f17eee2acf90a475986e33a5c374cb1c08cf9946 [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");
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)
68 return format (s, "Deleted");
69
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;
80 clib_bihash_bucket_t *b;
81 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;
122 clib_bihash_bucket_t *b;
123 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;
131 u32 bd_id, bd_index = ~0;
Damjan Mariond171d482016-12-05 14:16:38 +0100132 u8 now = (u8) (vlib_time_now (vm) / 60);
133 u8 *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 if (unformat (input, "raw"))
136 raw = 1;
137 else if (unformat (input, "verbose"))
138 verbose = 1;
139 else if (unformat (input, "bd_index %d", &bd_index))
140 verbose = 1;
141 else if (unformat (input, "bd_id %d", &bd_id))
142 {
143 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400144 if (p)
145 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 verbose = 1;
147 bd_index = p[0];
Dave Barach97d8dc22016-08-15 15:31:15 -0400148 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400150 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 vlib_cli_output (vm, "no such bridge domain id");
152 return 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400153 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 }
155
156 for (i = 0; i < h->nbuckets; i++)
157 {
158 b = &h->buckets[i];
159 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400160 continue;
161 v = BV (clib_bihash_get_value) (h, b->offset);
162 for (j = 0; j < (1 << b->log2_pages); j++)
163 {
164 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
165 {
166 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
167 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Dave Barach97d8dc22016-08-15 15:31:15 -0400169 if (verbose && first_entry)
170 {
171 first_entry = 0;
172 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400173 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
174 "Mac-Address", "BD-Idx", "If-Idx",
175 "BSN-ISN", "Age(min)", "static", "filter",
176 "bvi", "Interface-Name");
Dave Barach97d8dc22016-08-15 15:31:15 -0400177 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Dave Barach97d8dc22016-08-15 15:31:15 -0400179 key.raw = v->kvp[k].key;
180 result.raw = v->kvp[k].value;
181
182 if (verbose
183 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
184 {
Damjan Mariond171d482016-12-05 14:16:38 +0100185 bd_config = vec_elt_at_index (l2input_main.bd_configs,
186 key.fields.bd_index);
187
John Loda1f2c72017-03-24 20:11:15 -0400188 if (bd_config->mac_age && !result.fields.static_mac)
Damjan Mariond171d482016-12-05 14:16:38 +0100189 {
190 i16 delta = now - result.fields.timestamp;
191 delta += delta < 0 ? 256 : 0;
192 s = format (s, "%d", delta);
193 }
194 else
John Loda1f2c72017-03-24 20:11:15 -0400195 s = format (s, "-");
Damjan Mariond171d482016-12-05 14:16:38 +0100196
Dave Barach97d8dc22016-08-15 15:31:15 -0400197 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400198 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -0400199 format_ethernet_address, key.fields.mac,
200 key.fields.bd_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400201 result.fields.sw_if_index == ~0
202 ? -1 : result.fields.sw_if_index,
Eyal Bari7537e712017-04-27 14:07:55 +0300203 result.fields.sn.bd, result.fields.sn.swif,
John Loda1f2c72017-03-24 20:11:15 -0400204 s, result.fields.static_mac ? "*" : "-",
205 result.fields.filter ? "*" : "-",
206 result.fields.bvi ? "*" : "-",
207 format_vnet_sw_if_index_name_with_NA,
208 msm->vnet_main, result.fields.sw_if_index);
Damjan Mariond171d482016-12-05 14:16:38 +0100209 vec_reset_length (s);
Dave Barach97d8dc22016-08-15 15:31:15 -0400210 }
211 total_entries++;
212 }
213 v++;
214 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 }
216
217 if (total_entries == 0)
218 vlib_cli_output (vm, "no l2fib entries");
219 else
John Lod48c8eb2017-05-05 12:35:25 -0400220 vlib_cli_output (vm,
221 "%lld l2fib entries with %d learned (or non-static) entries",
222 total_entries, l2learn_main.global_learn_count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
224 if (raw)
225 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Dave Barach97d8dc22016-08-15 15:31:15 -0400226 BV (format_bihash), h, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Damjan Mariond171d482016-12-05 14:16:38 +0100228 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 return 0;
230}
231
Billy McFall22aa3e92016-09-09 08:46:40 -0400232/*?
233 * This command dispays the MAC Address entries of the L2 FIB table.
234 * Output can be filtered to just get the number of MAC Addresses or display
235 * each MAC Address for all bridge domains or just a single bridge domain.
236 *
237 * @cliexpar
238 * Example of how to display the number of MAC Address entries in the L2
239 * FIB table:
240 * @cliexstart{show l2fib}
241 * 3 l2fib entries
242 * @cliexend
243 * Example of how to display all the MAC Address entries in the L2
244 * FIB table:
245 * @cliexstart{show l2fib verbose}
246 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
247 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
248 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
249 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
250 * 3 l2fib entries
251 * @cliexend
252?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400253/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
255 .path = "show l2fib",
256 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
257 .function = show_l2fib,
258};
Dave Barach97d8dc22016-08-15 15:31:15 -0400259/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
261
Dave Barach97d8dc22016-08-15 15:31:15 -0400262/* Remove all entries from the l2fib */
263void
Eyal Bari7537e712017-04-27 14:07:55 +0300264l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265{
Dave Barach97d8dc22016-08-15 15:31:15 -0400266 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
Eyal Bari7537e712017-04-27 14:07:55 +0300268 /* Remove all entries */
269 BV (clib_bihash_free) (&mp->mac_table);
270 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
271 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 l2learn_main.global_learn_count = 0;
273}
274
Chris Luke16bcf7d2016-09-01 14:31:46 -0400275/** Clear all entries in L2FIB.
276 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400277 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400279clear_l2fib (vlib_main_t * vm,
280 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281{
Eyal Bari7537e712017-04-27 14:07:55 +0300282 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283 return 0;
284}
285
Billy McFall22aa3e92016-09-09 08:46:40 -0400286/*?
287 * This command clears all the MAC Address entries from the L2 FIB table.
288 *
289 * @cliexpar
290 * Example of how to clear the L2 FIB Table:
291 * @cliexcmd{clear l2fib}
292 * Example to show the L2 FIB Table has been cleared:
293 * @cliexstart{show l2fib verbose}
294 * no l2fib entries
295 * @cliexend
296?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400297/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
299 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400300 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 .function = clear_l2fib,
302};
Dave Barach97d8dc22016-08-15 15:31:15 -0400303/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
Eyal Bari7537e712017-04-27 14:07:55 +0300305static inline l2fib_seq_num_t
306l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
307{
308 l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
309 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
310 /* *INDENT-OFF* */
311 return (l2fib_seq_num_t) {
312 .swif = int_config->seq_num,
313 .bd = bd_config->seq_num,
314 };
315 /* *INDENT-ON* */
316}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
Dave Barach97d8dc22016-08-15 15:31:15 -0400318/**
319 * Add an entry to the l2fib.
320 * If the entry already exists then overwrite it
321 */
322void
Eyal Bari7537e712017-04-27 14:07:55 +0300323l2fib_add_entry (u64 mac, u32 bd_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400324 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
325{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 l2fib_entry_key_t key;
327 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400328 __attribute__ ((unused)) u32 bucket_contents;
329 l2fib_main_t *mp = &l2fib_main;
330 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
Dave Barach97d8dc22016-08-15 15:31:15 -0400332 /* set up key */
333 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
334
335 /* set up result */
336 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 result.fields.sw_if_index = sw_if_index;
338 result.fields.static_mac = static_mac;
339 result.fields.filter = filter_mac;
340 result.fields.bvi = bvi_mac;
John Loda1f2c72017-03-24 20:11:15 -0400341 if (!static_mac)
Eyal Bari7537e712017-04-27 14:07:55 +0300342 result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
344 kv.key = key.raw;
345 kv.value = result.raw;
346
Dave Barach97d8dc22016-08-15 15:31:15 -0400347 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Dave Barach97d8dc22016-08-15 15:31:15 -0400349 /* increment counter if dynamically learned mac */
John Lod48c8eb2017-05-05 12:35:25 -0400350 if (result.fields.static_mac == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400351 {
352 l2learn_main.global_learn_count++;
353 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354}
355
Dave Barach97d8dc22016-08-15 15:31:15 -0400356/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400357 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400358 * The CLI format is:
359 * l2fib add <mac> <bd> <intf> [static] [bvi]
360 * l2fib add <mac> <bd> filter
361 * Note that filter and bvi entries are always static
362 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400364l2fib_add (vlib_main_t * vm,
365 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366{
Dave Barach97d8dc22016-08-15 15:31:15 -0400367 bd_main_t *bdm = &bd_main;
368 vnet_main_t *vnm = vnet_get_main ();
369 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 u64 mac;
371 u32 bd_id;
372 u32 bd_index;
373 u32 sw_if_index = ~0;
374 u32 filter_mac = 0;
375 u32 static_mac = 0;
376 u32 bvi_mac = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400377 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Dave Barach97d8dc22016-08-15 15:31:15 -0400379 if (!unformat_user (input, unformat_ethernet_address, &mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 {
381 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400382 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 goto done;
384 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400385
386 if (!unformat (input, "%d", &bd_id))
387 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400389 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400391 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
393 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400394 if (!p)
395 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
397 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400398 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 bd_index = p[0];
400
Dave Barach97d8dc22016-08-15 15:31:15 -0400401 if (unformat (input, "filter"))
402 {
403 filter_mac = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 static_mac = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400405
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400407 else
408 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Dave Barach97d8dc22016-08-15 15:31:15 -0400410 if (!unformat_user
411 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
412 {
413 error = clib_error_return (0, "unknown interface `%U'",
414 format_unformat_error, input);
415 goto done;
416 }
417 if (unformat (input, "static"))
418 {
419 static_mac = 1;
420 }
421 else if (unformat (input, "bvi"))
422 {
423 bvi_mac = 1;
424 static_mac = 1;
425 }
426 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Dave Barach97d8dc22016-08-15 15:31:15 -0400428 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
429 bvi_mac);
430
431done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 return error;
433}
434
Billy McFall22aa3e92016-09-09 08:46:40 -0400435/*?
436 * This command adds a MAC Address entry to the L2 FIB table
437 * of an existing bridge-domain. The MAC Address can be static
438 * or dynamic. This command also allows a filter to be added,
439 * such that packets with given MAC Addresses (source mac or
440 * destination mac match) are dropped.
441 *
442 * @cliexpar
443 * Example of how to add a dynamic 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:33 200 GigabitEthernet0/8/0.200}
446 * Example of how to add a static MAC Address entry to the L2 FIB table
447 * of a bridge-domain (where 200 is the bridge-domain-id):
448 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
449 * Example of how to add a filter such that a packet with the given MAC
450 * Address will be dropped in a given bridge-domain (where 200 is the
451 * bridge-domain-id):
452 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
453 * Example of show command of the provisioned MAC Addresses and filters:
454 * @cliexstart{show l2fib verbose}
455 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
456 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
457 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
458 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
459 * 3 l2fib entries
460 * @cliexend
461?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400462/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
464 .path = "l2fib add",
Billy McFall22aa3e92016-09-09 08:46:40 -0400465 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 .function = l2fib_add,
467};
Dave Barach97d8dc22016-08-15 15:31:15 -0400468/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
470
471static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400472l2fib_test_command_fn (vlib_main_t * vm,
473 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474{
Dave Barach97d8dc22016-08-15 15:31:15 -0400475 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 u64 mac, save_mac;
477 u32 bd_index = 0;
478 u32 sw_if_index = 8;
479 u32 filter_mac = 0;
480 u32 bvi_mac = 0;
481 u32 is_add = 0;
482 u32 is_del = 0;
483 u32 is_check = 0;
484 u32 count = 1;
485 int mac_set = 0;
486 int i;
487
Dave Barach97d8dc22016-08-15 15:31:15 -0400488 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 {
490 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400491 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400493 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400495 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400497 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400499 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400501 break;
502 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
504 if (mac_set == 0)
505 return clib_error_return (0, "mac not set");
506
507 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400508 return clib_error_return (0,
509 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510
511 save_mac = mac;
512
513 if (is_add)
514 {
515 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400516 {
517 u64 tmp;
518 l2fib_add_entry (mac, bd_index, sw_if_index, mac,
519 filter_mac, bvi_mac);
520 tmp = clib_net_to_host_u64 (mac);
521 tmp >>= 16;
522 tmp++;
523 tmp <<= 16;
524 mac = clib_host_to_net_u64 (tmp);
525 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 }
527
528 if (is_check)
529 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400530 BVT (clib_bihash_kv) kv;
531 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
533 mac = save_mac;
534
535 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400536 {
537 u64 tmp;
538 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
539 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
540 {
541 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
542 break;
543 }
544 tmp = clib_net_to_host_u64 (mac);
545 tmp >>= 16;
546 tmp++;
547 tmp <<= 16;
548 mac = clib_host_to_net_u64 (tmp);
549 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 }
551
552 if (is_del)
553 {
554 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400555 {
556 u64 tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557
Dave Barach97d8dc22016-08-15 15:31:15 -0400558 l2fib_del_entry (mac, bd_index);
559
560 tmp = clib_net_to_host_u64 (mac);
561 tmp >>= 16;
562 tmp++;
563 tmp <<= 16;
564 mac = clib_host_to_net_u64 (tmp);
565 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 }
567
568 return error;
569}
570
Billy McFall22aa3e92016-09-09 08:46:40 -0400571/*?
572 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
573 * bridge domain (bridge-domain-id of 0) to be modified.
574 *
575 * @cliexpar
576 * @parblock
577 * Example of how to add a set of 4 sequential MAC Address entries to L2
578 * FIB table of the default bridge-domain:
579 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
580 *
581 * Show the set of 4 sequential MAC Address entries that were added:
582 * @cliexstart{show l2fib verbose}
583 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
584 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
585 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
586 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
587 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
588 * 4 l2fib entries
589 * @cliexend
590 *
591 * Example of how to check that the set of 4 sequential MAC Address
592 * entries were added to L2 FIB table of the default
593 * bridge-domain. Used a count of 5 to produce an error:
594 *
595 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
596 * The output of the check command is in the log files. Log file
597 * location may vary based on your OS and Version:
598 *
599 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
600 *
601 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
602 *
603 * Example of how to delete a set of 4 sequential MAC Address entries
604 * from L2 FIB table of the default bridge-domain:
605 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
606 * @endparblock
607?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400608/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609VLIB_CLI_COMMAND (l2fib_test_command, static) = {
610 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400611 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612 .function = l2fib_test_command_fn,
613};
Dave Barach97d8dc22016-08-15 15:31:15 -0400614/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615
616
Dave Barach97d8dc22016-08-15 15:31:15 -0400617/**
618 * Delete an entry from the l2fib.
619 * Return 0 if the entry was deleted, or 1 if it was not found
620 */
Eyal Bari7537e712017-04-27 14:07:55 +0300621static u32
622l2fib_del_entry_by_key (u64 raw_key)
Dave Barach97d8dc22016-08-15 15:31:15 -0400623{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
625 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400626 l2fib_main_t *mp = &l2fib_main;
627 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628
Dave Barach97d8dc22016-08-15 15:31:15 -0400629 /* set up key */
Eyal Bari7537e712017-04-27 14:07:55 +0300630 kv.key = raw_key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
Dave Barach97d8dc22016-08-15 15:31:15 -0400632 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 return 1;
634
635 result.raw = kv.value;
636
Dave Barach97d8dc22016-08-15 15:31:15 -0400637 /* decrement counter if dynamically learned mac */
John Lod48c8eb2017-05-05 12:35:25 -0400638 if (result.fields.static_mac == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400639 {
640 if (l2learn_main.global_learn_count > 0)
641 {
642 l2learn_main.global_learn_count--;
643 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645
Dave Barach97d8dc22016-08-15 15:31:15 -0400646 /* Remove entry from hash table */
647 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648 return 0;
649}
650
Dave Barach97d8dc22016-08-15 15:31:15 -0400651/**
Eyal Bari7537e712017-04-27 14:07:55 +0300652 * Delete an entry from the l2fib.
653 * Return 0 if the entry was deleted, or 1 if it was not found
654 */
655u32
656l2fib_del_entry (u64 mac, u32 bd_index)
657{
658 return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
659}
660
661/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400662 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400663 * The CLI format is:
664 * l2fib del <mac> <bd-id>
665 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400667l2fib_del (vlib_main_t * vm,
668 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669{
Dave Barach97d8dc22016-08-15 15:31:15 -0400670 bd_main_t *bdm = &bd_main;
671 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672 u64 mac;
673 u32 bd_id;
674 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400675 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Dave Barach97d8dc22016-08-15 15:31:15 -0400677 if (!unformat_user (input, unformat_ethernet_address, &mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 {
679 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400680 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681 goto done;
682 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400683
684 if (!unformat (input, "%d", &bd_id))
685 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400687 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400689 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
691 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400692 if (!p)
693 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
695 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400696 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697 bd_index = p[0];
698
Dave Barach97d8dc22016-08-15 15:31:15 -0400699 /* Delete the entry */
700 if (l2fib_del_entry (mac, bd_index))
701 {
702 error = clib_error_return (0, "mac entry not found");
703 goto done;
704 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Dave Barach97d8dc22016-08-15 15:31:15 -0400706done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707 return error;
708}
709
Billy McFall22aa3e92016-09-09 08:46:40 -0400710/*?
711 * This command deletes an existing MAC Address entry from the L2 FIB
712 * table of an existing bridge-domain.
713 *
714 * @cliexpar
715 * 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):
716 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
717?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400718/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
720 .path = "l2fib del",
Billy McFall22aa3e92016-09-09 08:46:40 -0400721 .short_help = "l2fib del <mac> <bridge-domain-id>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722 .function = l2fib_del,
723};
Dave Barach97d8dc22016-08-15 15:31:15 -0400724/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725
John Loda1f2c72017-03-24 20:11:15 -0400726/**
727 Kick off ager to scan MACs to age/delete MAC entries
728*/
729void
730l2fib_start_ager_scan (vlib_main_t * vm)
731{
732 l2_bridge_domain_t *bd_config;
733 int enable = 0;
734
735 /* check if there is at least one bd with mac aging enabled */
736 vec_foreach (bd_config, l2input_main.bd_configs)
737 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
738 enable = 1;
739
740 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
741 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
742 L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
743}
744
745/**
Eyal Bari7537e712017-04-27 14:07:55 +0300746 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400747*/
748void
749l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
750{
Eyal Bari7537e712017-04-27 14:07:55 +0300751 l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
John Loda1f2c72017-03-24 20:11:15 -0400752 int_config->seq_num += 1;
753 l2fib_start_ager_scan (vm);
754}
755
756/**
Eyal Bari7537e712017-04-27 14:07:55 +0300757 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400758*/
759void
760l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
761{
Eyal Bari7537e712017-04-27 14:07:55 +0300762 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400763 bd_config->seq_num += 1;
764 l2fib_start_ager_scan (vm);
765}
766
767/**
Eyal Bari7537e712017-04-27 14:07:55 +0300768 Flush all non static MACs - flushes all valid BDs
769*/
770void
771l2fib_flush_all_mac (vlib_main_t * vm)
772{
773 l2_bridge_domain_t *bd_config;
774 vec_foreach (bd_config, l2input_main.bd_configs)
775 if (bd_is_valid (bd_config))
776 bd_config->seq_num += 1;
777
778 l2fib_start_ager_scan (vm);
779}
780
781
782/**
John Loda1f2c72017-03-24 20:11:15 -0400783 Flush MACs, except static ones, associated with an interface
784 The CLI format is:
785 l2fib flush-mac interface <if-name>
786*/
787static clib_error_t *
788l2fib_flush_mac_int (vlib_main_t * vm,
789 unformat_input_t * input, vlib_cli_command_t * cmd)
790{
791 vnet_main_t *vnm = vnet_get_main ();
792 clib_error_t *error = 0;
793 u32 sw_if_index;
794
795 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
796 {
797 error = clib_error_return (0, "unknown interface `%U'",
798 format_unformat_error, input);
799 goto done;
800 }
801
802 l2fib_flush_int_mac (vm, sw_if_index);
803
804done:
805 return error;
806}
807
Eyal Bari7537e712017-04-27 14:07:55 +0300808/**
809 Flush all MACs, except static ones
810 The CLI format is:
811 l2fib flush-mac all
812*/
813static clib_error_t *
814l2fib_flush_mac_all (vlib_main_t * vm,
815 unformat_input_t * input, vlib_cli_command_t * cmd)
816{
817 l2fib_flush_all_mac (vm);
818 return 0;
819}
820
821/*?
822 * This command kick off ager to delete all existing MAC Address entries,
823 * except static ones, associated with an interface from the L2 FIB table.
824 *
825 * @cliexpar
826 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
827 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
828?*/
829/* *INDENT-OFF* */
830VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
831 .path = "l2fib flush-mac all",
832 .short_help = "l2fib flush-mac all",
833 .function = l2fib_flush_mac_all,
834};
835/* *INDENT-ON* */
836
John Loda1f2c72017-03-24 20:11:15 -0400837/*?
838 * This command kick off ager to delete all existing MAC Address entries,
839 * except static ones, associated with an interface from the L2 FIB table.
840 *
841 * @cliexpar
842 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
843 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
844?*/
845/* *INDENT-OFF* */
846VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
847 .path = "l2fib flush-mac interface",
848 .short_help = "l2fib flush-mac interface <if-name>",
849 .function = l2fib_flush_mac_int,
850};
851/* *INDENT-ON* */
852
853/**
854 Flush bridge-domain MACs except static ones.
855 The CLI format is:
856 l2fib flush-mac bridge-domain <bd-id>
857*/
858static clib_error_t *
859l2fib_flush_mac_bd (vlib_main_t * vm,
860 unformat_input_t * input, vlib_cli_command_t * cmd)
861{
862 bd_main_t *bdm = &bd_main;
863 clib_error_t *error = 0;
864 u32 bd_index, bd_id;
865 uword *p;
866
867 if (!unformat (input, "%d", &bd_id))
868 {
869 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
870 format_unformat_error, input);
871 goto done;
872 }
873
874 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
875 if (p)
876 bd_index = *p;
877 else
878 return clib_error_return (0, "No such bridge domain %d", bd_id);
879
880 l2fib_flush_bd_mac (vm, bd_index);
881
882done:
883 return error;
884}
885
886/*?
887 * This command kick off ager to delete all existing MAC Address entries,
888 * except static ones, in a bridge domain from the L2 FIB table.
889 *
890 * @cliexpar
891 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
892 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
893?*/
894/* *INDENT-OFF* */
895VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
896 .path = "l2fib flush-mac bridge-domain",
897 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
898 .function = l2fib_flush_mac_bd,
899};
900/* *INDENT-ON* */
901
Eyal Bariafc47aa2017-04-20 14:45:17 +0300902clib_error_t *
903l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
904{
905 l2_input_config_t *config = l2input_intf_config (sw_if_index);
906 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
907 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
908 return 0;
909}
910
911VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
Dave Barach97d8dc22016-08-15 15:31:15 -0400913BVT (clib_bihash) * get_mac_table (void)
914{
915 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 return &mp->mac_table;
917}
918
Damjan Mariond171d482016-12-05 14:16:38 +0100919static uword
920l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
921 vlib_frame_t * f)
922{
923 uword event_type, *event_data = 0;
924 l2fib_main_t *msm = &l2fib_main;
Damjan Mariond171d482016-12-05 14:16:38 +0100925 bool enabled = 0;
926 f64 start_time, last_run_duration = 0, t;
Damjan Mariond171d482016-12-05 14:16:38 +0100927
928 while (1)
929 {
930 if (enabled)
931 vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
932 else
933 vlib_process_wait_for_event (vm);
934
935 event_type = vlib_process_get_events (vm, &event_data);
936 vec_reset_length (event_data);
937
938 switch (event_type)
939 {
940 case ~0:
941 break;
942 case L2_MAC_AGE_PROCESS_EVENT_START:
943 enabled = 1;
944 break;
945 case L2_MAC_AGE_PROCESS_EVENT_STOP:
946 enabled = 0;
947 continue;
John Loda1f2c72017-03-24 20:11:15 -0400948 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
949 enabled = 0;
950 break;
Damjan Mariond171d482016-12-05 14:16:38 +0100951 default:
952 ASSERT (0);
953 }
954 last_run_duration = start_time = vlib_time_now (vm);
Eyal Bari7537e712017-04-27 14:07:55 +0300955
956 BVT (clib_bihash) * h = &msm->mac_table;
957 int i, j, k;
Damjan Mariond171d482016-12-05 14:16:38 +0100958 for (i = 0; i < h->nbuckets; i++)
959 {
960 /* Allow no more than 10us without a pause */
961 t = vlib_time_now (vm);
962 if (t > start_time + 10e-6)
963 {
964 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
965 start_time = vlib_time_now (vm);
966 }
967
968 if (i < (h->nbuckets - 3))
969 {
Eyal Bari7537e712017-04-27 14:07:55 +0300970 clib_bihash_bucket_t *b = &h->buckets[i + 3];
Damjan Mariond171d482016-12-05 14:16:38 +0100971 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
972 b = &h->buckets[i + 1];
973 if (b->offset)
974 {
Eyal Bari7537e712017-04-27 14:07:55 +0300975 BVT (clib_bihash_value) * v =
976 BV (clib_bihash_get_value) (h, b->offset);
Damjan Mariond171d482016-12-05 14:16:38 +0100977 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
978 }
979 }
980
Eyal Bari7537e712017-04-27 14:07:55 +0300981 clib_bihash_bucket_t *b = &h->buckets[i];
Damjan Mariond171d482016-12-05 14:16:38 +0100982 if (b->offset == 0)
983 continue;
Eyal Bari7537e712017-04-27 14:07:55 +0300984 BVT (clib_bihash_value) * v =
985 BV (clib_bihash_get_value) (h, b->offset);
Damjan Mariond171d482016-12-05 14:16:38 +0100986 for (j = 0; j < (1 << b->log2_pages); j++)
987 {
988 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
989 {
990 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
991 continue;
992
Eyal Bari7537e712017-04-27 14:07:55 +0300993 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
994 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
Damjan Mariond171d482016-12-05 14:16:38 +0100995
996 if (result.fields.static_mac)
997 continue;
998
Eyal Bari7537e712017-04-27 14:07:55 +0300999 u32 bd_index = key.fields.bd_index;
1000 u32 sw_if_index = result.fields.sw_if_index;
1001 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1002 if (result.fields.sn.as_u16 != sn)
John Loda1f2c72017-03-24 20:11:15 -04001003 {
Eyal Bari7537e712017-04-27 14:07:55 +03001004 l2fib_del_entry_by_key (key.raw);
John Loda1f2c72017-03-24 20:11:15 -04001005 continue;
1006 }
Eyal Bari7537e712017-04-27 14:07:55 +03001007 l2_bridge_domain_t *bd_config =
1008 vec_elt_at_index (l2input_main.bd_configs, bd_index);
Damjan Mariond171d482016-12-05 14:16:38 +01001009
1010 if (bd_config->mac_age == 0)
1011 continue;
1012
Eyal Bari7537e712017-04-27 14:07:55 +03001013 i16 delta =
1014 (u8) (start_time / 60) - result.fields.timestamp;
Damjan Mariond171d482016-12-05 14:16:38 +01001015 delta += delta < 0 ? 256 : 0;
1016
1017 if (delta > bd_config->mac_age)
Eyal Bari7537e712017-04-27 14:07:55 +03001018 l2fib_del_entry_by_key (key.raw);
Damjan Mariond171d482016-12-05 14:16:38 +01001019 }
1020 v++;
1021 }
1022 }
1023 last_run_duration = vlib_time_now (vm) - last_run_duration;
1024 }
1025 return 0;
1026}
1027
1028/* *INDENT-OFF* */
1029VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1030 .function = l2fib_mac_age_scanner_process,
1031 .type = VLIB_NODE_TYPE_PROCESS,
1032 .name = "l2fib-mac-age-scanner-process",
1033};
1034/* *INDENT-ON* */
1035
Dave Barach97d8dc22016-08-15 15:31:15 -04001036clib_error_t *
1037l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038{
Dave Barach97d8dc22016-08-15 15:31:15 -04001039 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040 l2fib_entry_key_t test_key;
1041 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001042
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001044 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045
Dave Barach97d8dc22016-08-15 15:31:15 -04001046 /* Create the hash table */
1047 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1048 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049
Dave Barach97d8dc22016-08-15 15:31:15 -04001050 /* verify the key constructor is good, since it is endian-sensitive */
1051 memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052 test_mac[0] = 0x11;
1053 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001054 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055 ASSERT (test_key.fields.mac[0] == 0x11);
1056 ASSERT (test_key.fields.bd_index == 0x1234);
1057
1058 return 0;
1059}
1060
1061VLIB_INIT_FUNCTION (l2fib_init);
1062
Dave Barach97d8dc22016-08-15 15:31:15 -04001063/*
1064 * fd.io coding-style-patch-verification: ON
1065 *
1066 * Local Variables:
1067 * eval: (c-set-style "gnu")
1068 * End:
1069 */