blob: 4ed16987404b95b21a65eb840ed754eadc710cbd [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
Ed Warnickecb9cada2015-12-08 15:45:58 -070044l2fib_main_t l2fib_main;
45
Dave Barach97d8dc22016-08-15 15:31:15 -040046/** Format sw_if_index. If the value is ~0, use the text "N/A" */
47u8 *
48format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070049{
Dave Barach97d8dc22016-08-15 15:31:15 -040050 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 u32 sw_if_index = va_arg (*args, u32);
52 if (sw_if_index == ~0)
53 return format (s, "N/A");
Eyal Barib823df52017-06-12 17:07:22 +030054
55 vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
56 if (!swif)
Eyal Bari0f360dc2017-06-14 13:11:20 +030057 return format (s, "Stale");
Eyal Barib823df52017-06-12 17:07:22 +030058
59 return format (s, "%U", format_vnet_sw_interface_name, vnm,
60 vnet_get_sw_interface_safe (vnm, sw_if_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -070061}
62
Dave Barach97d8dc22016-08-15 15:31:15 -040063void
64l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
65 l2fib_entry_result_t ** l2fe_res)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066{
Dave Barach97d8dc22016-08-15 15:31:15 -040067 l2fib_main_t *msm = &l2fib_main;
68 BVT (clib_bihash) * h = &msm->mac_table;
69 clib_bihash_bucket_t *b;
70 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 l2fib_entry_key_t key;
72 l2fib_entry_result_t result;
73 int i, j, k;
74
75 for (i = 0; i < h->nbuckets; i++)
76 {
77 b = &h->buckets[i];
78 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -040079 continue;
80 v = BV (clib_bihash_get_value) (h, b->offset);
81 for (j = 0; j < (1 << b->log2_pages); j++)
82 {
83 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
84 {
85 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
86 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
Dave Barach97d8dc22016-08-15 15:31:15 -040088 key.raw = v->kvp[k].key;
89 result.raw = v->kvp[k].value;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
Dave Barach97d8dc22016-08-15 15:31:15 -040091 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
92 {
93 vec_add1 (*l2fe_key, key);
94 vec_add1 (*l2fe_res, result);
95 }
96 }
97 v++;
98 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 }
100}
101
Chris Luke16bcf7d2016-09-01 14:31:46 -0400102/** Display the contents of the l2fib. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400104show_l2fib (vlib_main_t * vm,
105 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106{
Dave Barach97d8dc22016-08-15 15:31:15 -0400107 bd_main_t *bdm = &bd_main;
108 l2fib_main_t *msm = &l2fib_main;
Damjan Mariond171d482016-12-05 14:16:38 +0100109 l2_bridge_domain_t *bd_config;
Dave Barach97d8dc22016-08-15 15:31:15 -0400110 BVT (clib_bihash) * h = &msm->mac_table;
111 clib_bihash_bucket_t *b;
112 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 l2fib_entry_key_t key;
114 l2fib_entry_result_t result;
115 u32 first_entry = 1;
116 u64 total_entries = 0;
117 int i, j, k;
118 u8 verbose = 0;
119 u8 raw = 0;
120 u32 bd_id, bd_index = ~0;
Damjan Mariond171d482016-12-05 14:16:38 +0100121 u8 now = (u8) (vlib_time_now (vm) / 60);
122 u8 *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
124 if (unformat (input, "raw"))
125 raw = 1;
126 else if (unformat (input, "verbose"))
127 verbose = 1;
128 else if (unformat (input, "bd_index %d", &bd_index))
129 verbose = 1;
130 else if (unformat (input, "bd_id %d", &bd_id))
131 {
132 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400133 if (p)
134 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 verbose = 1;
136 bd_index = p[0];
Dave Barach97d8dc22016-08-15 15:31:15 -0400137 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400139 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 vlib_cli_output (vm, "no such bridge domain id");
141 return 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400142 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 }
144
145 for (i = 0; i < h->nbuckets; i++)
146 {
147 b = &h->buckets[i];
148 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400149 continue;
150 v = BV (clib_bihash_get_value) (h, b->offset);
151 for (j = 0; j < (1 << b->log2_pages); j++)
152 {
153 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
154 {
155 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
156 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Dave Barach97d8dc22016-08-15 15:31:15 -0400158 if (verbose && first_entry)
159 {
160 first_entry = 0;
161 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400162 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
163 "Mac-Address", "BD-Idx", "If-Idx",
164 "BSN-ISN", "Age(min)", "static", "filter",
165 "bvi", "Interface-Name");
Dave Barach97d8dc22016-08-15 15:31:15 -0400166 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
Dave Barach97d8dc22016-08-15 15:31:15 -0400168 key.raw = v->kvp[k].key;
169 result.raw = v->kvp[k].value;
170
171 if (verbose
172 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
173 {
Damjan Mariond171d482016-12-05 14:16:38 +0100174 bd_config = vec_elt_at_index (l2input_main.bd_configs,
175 key.fields.bd_index);
176
John Loda1f2c72017-03-24 20:11:15 -0400177 if (bd_config->mac_age && !result.fields.static_mac)
Damjan Mariond171d482016-12-05 14:16:38 +0100178 {
179 i16 delta = now - result.fields.timestamp;
180 delta += delta < 0 ? 256 : 0;
181 s = format (s, "%d", delta);
182 }
183 else
John Loda1f2c72017-03-24 20:11:15 -0400184 s = format (s, "-");
Damjan Mariond171d482016-12-05 14:16:38 +0100185
Dave Barach97d8dc22016-08-15 15:31:15 -0400186 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400187 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -0400188 format_ethernet_address, key.fields.mac,
189 key.fields.bd_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400190 result.fields.sw_if_index == ~0
191 ? -1 : result.fields.sw_if_index,
Eyal Bari7537e712017-04-27 14:07:55 +0300192 result.fields.sn.bd, result.fields.sn.swif,
John Loda1f2c72017-03-24 20:11:15 -0400193 s, result.fields.static_mac ? "*" : "-",
194 result.fields.filter ? "*" : "-",
195 result.fields.bvi ? "*" : "-",
196 format_vnet_sw_if_index_name_with_NA,
197 msm->vnet_main, result.fields.sw_if_index);
Damjan Mariond171d482016-12-05 14:16:38 +0100198 vec_reset_length (s);
Dave Barach97d8dc22016-08-15 15:31:15 -0400199 }
200 total_entries++;
201 }
202 v++;
203 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 }
205
206 if (total_entries == 0)
207 vlib_cli_output (vm, "no l2fib entries");
208 else
John Lod48c8eb2017-05-05 12:35:25 -0400209 vlib_cli_output (vm,
210 "%lld l2fib entries with %d learned (or non-static) entries",
211 total_entries, l2learn_main.global_learn_count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
213 if (raw)
214 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Dave Barach97d8dc22016-08-15 15:31:15 -0400215 BV (format_bihash), h, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Damjan Mariond171d482016-12-05 14:16:38 +0100217 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218 return 0;
219}
220
Billy McFall22aa3e92016-09-09 08:46:40 -0400221/*?
222 * This command dispays the MAC Address entries of the L2 FIB table.
223 * Output can be filtered to just get the number of MAC Addresses or display
224 * each MAC Address for all bridge domains or just a single bridge domain.
225 *
226 * @cliexpar
227 * Example of how to display the number of MAC Address entries in the L2
228 * FIB table:
229 * @cliexstart{show l2fib}
230 * 3 l2fib entries
231 * @cliexend
232 * Example of how to display all the MAC Address entries in the L2
233 * FIB table:
234 * @cliexstart{show l2fib verbose}
235 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
236 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
237 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
238 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
239 * 3 l2fib entries
240 * @cliexend
241?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400242/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
244 .path = "show l2fib",
245 .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
246 .function = show_l2fib,
247};
Dave Barach97d8dc22016-08-15 15:31:15 -0400248/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250
Dave Barach97d8dc22016-08-15 15:31:15 -0400251/* Remove all entries from the l2fib */
252void
Eyal Bari7537e712017-04-27 14:07:55 +0300253l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254{
Dave Barach97d8dc22016-08-15 15:31:15 -0400255 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Eyal Bari7537e712017-04-27 14:07:55 +0300257 /* Remove all entries */
258 BV (clib_bihash_free) (&mp->mac_table);
259 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
260 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 l2learn_main.global_learn_count = 0;
262}
263
Chris Luke16bcf7d2016-09-01 14:31:46 -0400264/** Clear all entries in L2FIB.
265 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400266 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400268clear_l2fib (vlib_main_t * vm,
269 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270{
Eyal Bari7537e712017-04-27 14:07:55 +0300271 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 return 0;
273}
274
Billy McFall22aa3e92016-09-09 08:46:40 -0400275/*?
276 * This command clears all the MAC Address entries from the L2 FIB table.
277 *
278 * @cliexpar
279 * Example of how to clear the L2 FIB Table:
280 * @cliexcmd{clear l2fib}
281 * Example to show the L2 FIB Table has been cleared:
282 * @cliexstart{show l2fib verbose}
283 * no l2fib entries
284 * @cliexend
285?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400286/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
288 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400289 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 .function = clear_l2fib,
291};
Dave Barach97d8dc22016-08-15 15:31:15 -0400292/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
Eyal Bari7537e712017-04-27 14:07:55 +0300294static inline l2fib_seq_num_t
295l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
296{
Eyal Bari7537e712017-04-27 14:07:55 +0300297 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
298 /* *INDENT-OFF* */
299 return (l2fib_seq_num_t) {
Eyal Bari0f360dc2017-06-14 13:11:20 +0300300 .swif = *l2fib_swif_seq_num (sw_if_index),
Eyal Bari7537e712017-04-27 14:07:55 +0300301 .bd = bd_config->seq_num,
302 };
303 /* *INDENT-ON* */
304}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
Dave Barach97d8dc22016-08-15 15:31:15 -0400306/**
307 * Add an entry to the l2fib.
308 * If the entry already exists then overwrite it
309 */
310void
Eyal Bari7537e712017-04-27 14:07:55 +0300311l2fib_add_entry (u64 mac, u32 bd_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400312 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
313{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 l2fib_entry_key_t key;
315 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400316 __attribute__ ((unused)) u32 bucket_contents;
317 l2fib_main_t *mp = &l2fib_main;
318 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
Dave Barach97d8dc22016-08-15 15:31:15 -0400320 /* set up key */
321 key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
322
323 /* set up result */
324 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 result.fields.sw_if_index = sw_if_index;
326 result.fields.static_mac = static_mac;
327 result.fields.filter = filter_mac;
328 result.fields.bvi = bvi_mac;
John Loda1f2c72017-03-24 20:11:15 -0400329 if (!static_mac)
Eyal Bari7537e712017-04-27 14:07:55 +0300330 result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
332 kv.key = key.raw;
333 kv.value = result.raw;
334
Dave Barach97d8dc22016-08-15 15:31:15 -0400335 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Dave Barach97d8dc22016-08-15 15:31:15 -0400337 /* increment counter if dynamically learned mac */
John Lod48c8eb2017-05-05 12:35:25 -0400338 if (result.fields.static_mac == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400339 {
340 l2learn_main.global_learn_count++;
341 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342}
343
Dave Barach97d8dc22016-08-15 15:31:15 -0400344/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400345 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400346 * The CLI format is:
347 * l2fib add <mac> <bd> <intf> [static] [bvi]
348 * l2fib add <mac> <bd> filter
349 * Note that filter and bvi entries are always static
350 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400352l2fib_add (vlib_main_t * vm,
353 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354{
Dave Barach97d8dc22016-08-15 15:31:15 -0400355 bd_main_t *bdm = &bd_main;
356 vnet_main_t *vnm = vnet_get_main ();
357 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 u64 mac;
359 u32 bd_id;
360 u32 bd_index;
361 u32 sw_if_index = ~0;
362 u32 filter_mac = 0;
363 u32 static_mac = 0;
364 u32 bvi_mac = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400365 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366
Dave Barach97d8dc22016-08-15 15:31:15 -0400367 if (!unformat_user (input, unformat_ethernet_address, &mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 {
369 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400370 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371 goto done;
372 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400373
374 if (!unformat (input, "%d", &bd_id))
375 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400377 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400379 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
381 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400382 if (!p)
383 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
385 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400386 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 bd_index = p[0];
388
Dave Barach97d8dc22016-08-15 15:31:15 -0400389 if (unformat (input, "filter"))
390 {
391 filter_mac = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 static_mac = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400393
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400395 else
396 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397
Dave Barach97d8dc22016-08-15 15:31:15 -0400398 if (!unformat_user
399 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
400 {
401 error = clib_error_return (0, "unknown interface `%U'",
402 format_unformat_error, input);
403 goto done;
404 }
405 if (unformat (input, "static"))
406 {
407 static_mac = 1;
408 }
409 else if (unformat (input, "bvi"))
410 {
411 bvi_mac = 1;
412 static_mac = 1;
413 }
414 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415
John Lob2fd6cb2017-07-12 19:56:45 -0400416 if (vec_len (l2input_main.configs) <= sw_if_index)
417 {
418 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
419 sw_if_index);
420 goto done;
421 }
422
Eyal Bari31a71ab2017-06-25 14:42:33 +0300423 if (filter_mac)
424 l2fib_add_filter_entry (mac, bd_index);
425 else
426 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400427
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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 u32 bvi_mac = 0;
477 u32 is_add = 0;
478 u32 is_del = 0;
479 u32 is_check = 0;
480 u32 count = 1;
481 int mac_set = 0;
482 int i;
483
Dave Barach97d8dc22016-08-15 15:31:15 -0400484 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 {
486 if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400487 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400489 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400491 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400493 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400495 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400497 break;
498 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499
500 if (mac_set == 0)
501 return clib_error_return (0, "mac not set");
502
503 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400504 return clib_error_return (0,
505 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
507 save_mac = mac;
508
509 if (is_add)
510 {
511 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400512 {
513 u64 tmp;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300514 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, mac, bvi_mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400515 tmp = clib_net_to_host_u64 (mac);
516 tmp >>= 16;
517 tmp++;
518 tmp <<= 16;
519 mac = clib_host_to_net_u64 (tmp);
520 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 }
522
523 if (is_check)
524 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400525 BVT (clib_bihash_kv) kv;
526 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
528 mac = save_mac;
529
530 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400531 {
532 u64 tmp;
533 kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
534 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
535 {
536 clib_warning ("key %U AWOL", format_ethernet_address, &mac);
537 break;
538 }
539 tmp = clib_net_to_host_u64 (mac);
540 tmp >>= 16;
541 tmp++;
542 tmp <<= 16;
543 mac = clib_host_to_net_u64 (tmp);
544 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 }
546
547 if (is_del)
548 {
549 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400550 {
551 u64 tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
Dave Barach97d8dc22016-08-15 15:31:15 -0400553 l2fib_del_entry (mac, bd_index);
554
555 tmp = clib_net_to_host_u64 (mac);
556 tmp >>= 16;
557 tmp++;
558 tmp <<= 16;
559 mac = clib_host_to_net_u64 (tmp);
560 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 }
562
563 return error;
564}
565
Billy McFall22aa3e92016-09-09 08:46:40 -0400566/*?
567 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
568 * bridge domain (bridge-domain-id of 0) to be modified.
569 *
570 * @cliexpar
571 * @parblock
572 * Example of how to add a set of 4 sequential MAC Address entries to L2
573 * FIB table of the default bridge-domain:
574 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
575 *
576 * Show the set of 4 sequential MAC Address entries that were added:
577 * @cliexstart{show l2fib verbose}
578 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
579 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
580 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
581 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
582 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
583 * 4 l2fib entries
584 * @cliexend
585 *
586 * Example of how to check that the set of 4 sequential MAC Address
587 * entries were added to L2 FIB table of the default
588 * bridge-domain. Used a count of 5 to produce an error:
589 *
590 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
591 * The output of the check command is in the log files. Log file
592 * location may vary based on your OS and Version:
593 *
594 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
595 *
596 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
597 *
598 * Example of how to delete a set of 4 sequential MAC Address entries
599 * from L2 FIB table of the default bridge-domain:
600 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
601 * @endparblock
602?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400603/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604VLIB_CLI_COMMAND (l2fib_test_command, static) = {
605 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400606 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607 .function = l2fib_test_command_fn,
608};
Dave Barach97d8dc22016-08-15 15:31:15 -0400609/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610
611
Dave Barach97d8dc22016-08-15 15:31:15 -0400612/**
613 * Delete an entry from the l2fib.
614 * Return 0 if the entry was deleted, or 1 if it was not found
615 */
Eyal Bari7537e712017-04-27 14:07:55 +0300616static u32
617l2fib_del_entry_by_key (u64 raw_key)
Dave Barach97d8dc22016-08-15 15:31:15 -0400618{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
620 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400621 l2fib_main_t *mp = &l2fib_main;
622 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
Dave Barach97d8dc22016-08-15 15:31:15 -0400624 /* set up key */
Eyal Bari7537e712017-04-27 14:07:55 +0300625 kv.key = raw_key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Dave Barach97d8dc22016-08-15 15:31:15 -0400627 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628 return 1;
629
630 result.raw = kv.value;
631
Dave Barach97d8dc22016-08-15 15:31:15 -0400632 /* decrement counter if dynamically learned mac */
John Lod48c8eb2017-05-05 12:35:25 -0400633 if (result.fields.static_mac == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400634 {
635 if (l2learn_main.global_learn_count > 0)
636 {
637 l2learn_main.global_learn_count--;
638 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640
Dave Barach97d8dc22016-08-15 15:31:15 -0400641 /* Remove entry from hash table */
642 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 return 0;
644}
645
Dave Barach97d8dc22016-08-15 15:31:15 -0400646/**
Eyal Bari7537e712017-04-27 14:07:55 +0300647 * Delete an entry from the l2fib.
648 * Return 0 if the entry was deleted, or 1 if it was not found
649 */
650u32
651l2fib_del_entry (u64 mac, u32 bd_index)
652{
653 return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
654}
655
656/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400657 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400658 * The CLI format is:
659 * l2fib del <mac> <bd-id>
660 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400662l2fib_del (vlib_main_t * vm,
663 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664{
Dave Barach97d8dc22016-08-15 15:31:15 -0400665 bd_main_t *bdm = &bd_main;
666 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667 u64 mac;
668 u32 bd_id;
669 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400670 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Dave Barach97d8dc22016-08-15 15:31:15 -0400672 if (!unformat_user (input, unformat_ethernet_address, &mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673 {
674 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400675 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676 goto done;
677 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400678
679 if (!unformat (input, "%d", &bd_id))
680 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400682 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400684 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685
686 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400687 if (!p)
688 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
690 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400691 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 bd_index = p[0];
693
Dave Barach97d8dc22016-08-15 15:31:15 -0400694 /* Delete the entry */
695 if (l2fib_del_entry (mac, bd_index))
696 {
697 error = clib_error_return (0, "mac entry not found");
698 goto done;
699 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
Dave Barach97d8dc22016-08-15 15:31:15 -0400701done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702 return error;
703}
704
Billy McFall22aa3e92016-09-09 08:46:40 -0400705/*?
706 * This command deletes an existing MAC Address entry from the L2 FIB
707 * table of an existing bridge-domain.
708 *
709 * @cliexpar
710 * 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):
711 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
712?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400713/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
715 .path = "l2fib del",
Billy McFall22aa3e92016-09-09 08:46:40 -0400716 .short_help = "l2fib del <mac> <bridge-domain-id>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 .function = l2fib_del,
718};
Dave Barach97d8dc22016-08-15 15:31:15 -0400719/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
John Loda1f2c72017-03-24 20:11:15 -0400721/**
722 Kick off ager to scan MACs to age/delete MAC entries
723*/
724void
725l2fib_start_ager_scan (vlib_main_t * vm)
726{
727 l2_bridge_domain_t *bd_config;
728 int enable = 0;
729
730 /* check if there is at least one bd with mac aging enabled */
731 vec_foreach (bd_config, l2input_main.bd_configs)
732 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
733 enable = 1;
734
735 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
736 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
737 L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
738}
739
740/**
Eyal Bari7537e712017-04-27 14:07:55 +0300741 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400742*/
743void
744l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
745{
Eyal Bari0f360dc2017-06-14 13:11:20 +0300746 *l2fib_swif_seq_num (sw_if_index) += 1;
John Loda1f2c72017-03-24 20:11:15 -0400747 l2fib_start_ager_scan (vm);
748}
749
750/**
Eyal Bari7537e712017-04-27 14:07:55 +0300751 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400752*/
753void
754l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
755{
Eyal Bari7537e712017-04-27 14:07:55 +0300756 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400757 bd_config->seq_num += 1;
758 l2fib_start_ager_scan (vm);
759}
760
761/**
Eyal Bari7537e712017-04-27 14:07:55 +0300762 Flush all non static MACs - flushes all valid BDs
763*/
764void
765l2fib_flush_all_mac (vlib_main_t * vm)
766{
767 l2_bridge_domain_t *bd_config;
768 vec_foreach (bd_config, l2input_main.bd_configs)
769 if (bd_is_valid (bd_config))
770 bd_config->seq_num += 1;
771
772 l2fib_start_ager_scan (vm);
773}
774
775
776/**
John Loda1f2c72017-03-24 20:11:15 -0400777 Flush MACs, except static ones, associated with an interface
778 The CLI format is:
779 l2fib flush-mac interface <if-name>
780*/
781static clib_error_t *
782l2fib_flush_mac_int (vlib_main_t * vm,
783 unformat_input_t * input, vlib_cli_command_t * cmd)
784{
785 vnet_main_t *vnm = vnet_get_main ();
786 clib_error_t *error = 0;
787 u32 sw_if_index;
788
789 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
790 {
791 error = clib_error_return (0, "unknown interface `%U'",
792 format_unformat_error, input);
793 goto done;
794 }
795
796 l2fib_flush_int_mac (vm, sw_if_index);
797
798done:
799 return error;
800}
801
Eyal Bari7537e712017-04-27 14:07:55 +0300802/**
803 Flush all MACs, except static ones
804 The CLI format is:
805 l2fib flush-mac all
806*/
807static clib_error_t *
808l2fib_flush_mac_all (vlib_main_t * vm,
809 unformat_input_t * input, vlib_cli_command_t * cmd)
810{
811 l2fib_flush_all_mac (vm);
812 return 0;
813}
814
815/*?
816 * This command kick off ager to delete all existing MAC Address entries,
817 * except static ones, associated with an interface from the L2 FIB table.
818 *
819 * @cliexpar
820 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
821 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
822?*/
823/* *INDENT-OFF* */
824VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
825 .path = "l2fib flush-mac all",
826 .short_help = "l2fib flush-mac all",
827 .function = l2fib_flush_mac_all,
828};
829/* *INDENT-ON* */
830
John Loda1f2c72017-03-24 20:11:15 -0400831/*?
832 * This command kick off ager to delete all existing MAC Address entries,
833 * except static ones, associated with an interface from the L2 FIB table.
834 *
835 * @cliexpar
836 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
837 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
838?*/
839/* *INDENT-OFF* */
840VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
841 .path = "l2fib flush-mac interface",
842 .short_help = "l2fib flush-mac interface <if-name>",
843 .function = l2fib_flush_mac_int,
844};
845/* *INDENT-ON* */
846
847/**
848 Flush bridge-domain MACs except static ones.
849 The CLI format is:
850 l2fib flush-mac bridge-domain <bd-id>
851*/
852static clib_error_t *
853l2fib_flush_mac_bd (vlib_main_t * vm,
854 unformat_input_t * input, vlib_cli_command_t * cmd)
855{
856 bd_main_t *bdm = &bd_main;
857 clib_error_t *error = 0;
858 u32 bd_index, bd_id;
859 uword *p;
860
861 if (!unformat (input, "%d", &bd_id))
862 {
863 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
864 format_unformat_error, input);
865 goto done;
866 }
867
868 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
869 if (p)
870 bd_index = *p;
871 else
872 return clib_error_return (0, "No such bridge domain %d", bd_id);
873
874 l2fib_flush_bd_mac (vm, bd_index);
875
876done:
877 return error;
878}
879
880/*?
881 * This command kick off ager to delete all existing MAC Address entries,
882 * except static ones, in a bridge domain from the L2 FIB table.
883 *
884 * @cliexpar
885 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
886 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
887?*/
888/* *INDENT-OFF* */
889VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
890 .path = "l2fib flush-mac bridge-domain",
891 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
892 .function = l2fib_flush_mac_bd,
893};
894/* *INDENT-ON* */
895
Eyal Bariafc47aa2017-04-20 14:45:17 +0300896clib_error_t *
897l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
898{
899 l2_input_config_t *config = l2input_intf_config (sw_if_index);
900 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
901 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
902 return 0;
903}
904
905VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906
Dave Barach97d8dc22016-08-15 15:31:15 -0400907BVT (clib_bihash) * get_mac_table (void)
908{
909 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910 return &mp->mac_table;
911}
912
Damjan Mariond171d482016-12-05 14:16:38 +0100913static uword
914l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
915 vlib_frame_t * f)
916{
917 uword event_type, *event_data = 0;
918 l2fib_main_t *msm = &l2fib_main;
Damjan Mariond171d482016-12-05 14:16:38 +0100919 bool enabled = 0;
920 f64 start_time, last_run_duration = 0, t;
Damjan Mariond171d482016-12-05 14:16:38 +0100921
922 while (1)
923 {
924 if (enabled)
925 vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
926 else
927 vlib_process_wait_for_event (vm);
928
929 event_type = vlib_process_get_events (vm, &event_data);
930 vec_reset_length (event_data);
931
932 switch (event_type)
933 {
934 case ~0:
935 break;
936 case L2_MAC_AGE_PROCESS_EVENT_START:
937 enabled = 1;
938 break;
939 case L2_MAC_AGE_PROCESS_EVENT_STOP:
940 enabled = 0;
941 continue;
John Loda1f2c72017-03-24 20:11:15 -0400942 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
943 enabled = 0;
944 break;
Damjan Mariond171d482016-12-05 14:16:38 +0100945 default:
946 ASSERT (0);
947 }
948 last_run_duration = start_time = vlib_time_now (vm);
Eyal Bari7537e712017-04-27 14:07:55 +0300949
950 BVT (clib_bihash) * h = &msm->mac_table;
951 int i, j, k;
Damjan Mariond171d482016-12-05 14:16:38 +0100952 for (i = 0; i < h->nbuckets; i++)
953 {
954 /* Allow no more than 10us without a pause */
955 t = vlib_time_now (vm);
956 if (t > start_time + 10e-6)
957 {
958 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
959 start_time = vlib_time_now (vm);
960 }
961
962 if (i < (h->nbuckets - 3))
963 {
Eyal Bari7537e712017-04-27 14:07:55 +0300964 clib_bihash_bucket_t *b = &h->buckets[i + 3];
Damjan Mariond171d482016-12-05 14:16:38 +0100965 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
966 b = &h->buckets[i + 1];
967 if (b->offset)
968 {
Eyal Bari7537e712017-04-27 14:07:55 +0300969 BVT (clib_bihash_value) * v =
970 BV (clib_bihash_get_value) (h, b->offset);
Damjan Mariond171d482016-12-05 14:16:38 +0100971 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
972 }
973 }
974
Eyal Bari7537e712017-04-27 14:07:55 +0300975 clib_bihash_bucket_t *b = &h->buckets[i];
Damjan Mariond171d482016-12-05 14:16:38 +0100976 if (b->offset == 0)
977 continue;
Eyal Bari7537e712017-04-27 14:07:55 +0300978 BVT (clib_bihash_value) * v =
979 BV (clib_bihash_get_value) (h, b->offset);
Damjan Mariond171d482016-12-05 14:16:38 +0100980 for (j = 0; j < (1 << b->log2_pages); j++)
981 {
982 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
983 {
984 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
985 continue;
986
Eyal Bari7537e712017-04-27 14:07:55 +0300987 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
988 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
Damjan Mariond171d482016-12-05 14:16:38 +0100989
990 if (result.fields.static_mac)
991 continue;
992
Eyal Bari7537e712017-04-27 14:07:55 +0300993 u32 bd_index = key.fields.bd_index;
994 u32 sw_if_index = result.fields.sw_if_index;
995 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
996 if (result.fields.sn.as_u16 != sn)
John Loda1f2c72017-03-24 20:11:15 -0400997 {
Eyal Bari7537e712017-04-27 14:07:55 +0300998 l2fib_del_entry_by_key (key.raw);
John Loda1f2c72017-03-24 20:11:15 -0400999 continue;
1000 }
Eyal Bari7537e712017-04-27 14:07:55 +03001001 l2_bridge_domain_t *bd_config =
1002 vec_elt_at_index (l2input_main.bd_configs, bd_index);
Damjan Mariond171d482016-12-05 14:16:38 +01001003
1004 if (bd_config->mac_age == 0)
1005 continue;
1006
Eyal Bari7537e712017-04-27 14:07:55 +03001007 i16 delta =
1008 (u8) (start_time / 60) - result.fields.timestamp;
Damjan Mariond171d482016-12-05 14:16:38 +01001009 delta += delta < 0 ? 256 : 0;
1010
1011 if (delta > bd_config->mac_age)
Eyal Bari7537e712017-04-27 14:07:55 +03001012 l2fib_del_entry_by_key (key.raw);
Damjan Mariond171d482016-12-05 14:16:38 +01001013 }
1014 v++;
1015 }
1016 }
1017 last_run_duration = vlib_time_now (vm) - last_run_duration;
1018 }
1019 return 0;
1020}
1021
1022/* *INDENT-OFF* */
1023VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1024 .function = l2fib_mac_age_scanner_process,
1025 .type = VLIB_NODE_TYPE_PROCESS,
1026 .name = "l2fib-mac-age-scanner-process",
1027};
1028/* *INDENT-ON* */
1029
Dave Barach97d8dc22016-08-15 15:31:15 -04001030clib_error_t *
1031l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032{
Dave Barach97d8dc22016-08-15 15:31:15 -04001033 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034 l2fib_entry_key_t test_key;
1035 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001036
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001038 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039
Dave Barach97d8dc22016-08-15 15:31:15 -04001040 /* Create the hash table */
1041 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1042 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043
Dave Barach97d8dc22016-08-15 15:31:15 -04001044 /* verify the key constructor is good, since it is endian-sensitive */
1045 memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046 test_mac[0] = 0x11;
1047 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001048 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049 ASSERT (test_key.fields.mac[0] == 0x11);
1050 ASSERT (test_key.fields.bd_index == 0x1234);
1051
1052 return 0;
1053}
1054
1055VLIB_INIT_FUNCTION (l2fib_init);
1056
Dave Barach97d8dc22016-08-15 15:31:15 -04001057/*
1058 * fd.io coding-style-patch-verification: ON
1059 *
1060 * Local Variables:
1061 * eval: (c-set-style "gnu")
1062 * End:
1063 */