blob: 9e095d236cfd09828c9012a03363220458b4e03a [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_fib.c : layer 2 forwarding table (aka mac table)
3 *
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19#include <vlib/vlib.h>
20#include <vnet/vnet.h>
21#include <vnet/pg/pg.h>
22#include <vnet/ethernet/ethernet.h>
23#include <vlib/cli.h>
24
25#include <vppinfra/error.h>
26#include <vppinfra/hash.h>
Damjan Mariond171d482016-12-05 14:16:38 +010027#include <vnet/l2/l2_input.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028#include <vnet/l2/l2_fib.h>
29#include <vnet/l2/l2_learn.h>
30#include <vnet/l2/l2_bd.h>
31
32#include <vppinfra/bihash_template.c>
33
John Lo8d00fff2017-08-03 00:35:36 -040034#include <vlibmemory/api.h>
35#include <vnet/vnet_msg_enum.h>
36
37#define vl_typedefs /* define message structures */
38#include <vnet/vnet_all_api_h.h>
39#undef vl_typedefs
40
41#define vl_endianfun /* define message structures */
42#include <vnet/vnet_all_api_h.h>
43#undef vl_endianfun
44
Billy McFall22aa3e92016-09-09 08:46:40 -040045/**
46 * @file
47 * @brief Ethernet MAC Address FIB Table Management.
48 *
49 * The MAC Address forwarding table for bridge-domains is called the l2fib.
50 * Entries are added automatically as part of mac learning, but MAC Addresses
51 * entries can also be added manually.
52 *
53 */
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055l2fib_main_t l2fib_main;
56
Mohsin Kazmi57938f62017-10-27 21:28:07 +020057static void
58incr_mac_address (u8 * mac)
59{
60 u64 tmp = *((u64 *) mac);
61 tmp = clib_net_to_host_u64 (tmp);
62 tmp += 1 << 16; /* skip unused (least significant) octets */
63 tmp = clib_host_to_net_u64 (tmp);
64
65 clib_memcpy (mac, &tmp, 6);
66}
67
Dave Barach97d8dc22016-08-15 15:31:15 -040068/** Format sw_if_index. If the value is ~0, use the text "N/A" */
69u8 *
70format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070071{
Dave Barach97d8dc22016-08-15 15:31:15 -040072 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 u32 sw_if_index = va_arg (*args, u32);
74 if (sw_if_index == ~0)
75 return format (s, "N/A");
Eyal Barib823df52017-06-12 17:07:22 +030076
77 vnet_sw_interface_t *swif = vnet_get_sw_interface_safe (vnm, sw_if_index);
78 if (!swif)
Eyal Bari0f360dc2017-06-14 13:11:20 +030079 return format (s, "Stale");
Eyal Barib823df52017-06-12 17:07:22 +030080
81 return format (s, "%U", format_vnet_sw_interface_name, vnm,
82 vnet_get_sw_interface_safe (vnm, sw_if_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -070083}
84
Dave Barach97d8dc22016-08-15 15:31:15 -040085void
86l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
87 l2fib_entry_result_t ** l2fe_res)
Ed Warnickecb9cada2015-12-08 15:45:58 -070088{
Dave Barach97d8dc22016-08-15 15:31:15 -040089 l2fib_main_t *msm = &l2fib_main;
90 BVT (clib_bihash) * h = &msm->mac_table;
Dave Barach908a5ea2017-07-14 12:42:21 -040091 BVT (clib_bihash_bucket) * b;
Dave Barach97d8dc22016-08-15 15:31:15 -040092 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 l2fib_entry_key_t key;
94 l2fib_entry_result_t result;
95 int i, j, k;
96
97 for (i = 0; i < h->nbuckets; i++)
98 {
99 b = &h->buckets[i];
100 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400101 continue;
102 v = BV (clib_bihash_get_value) (h, b->offset);
103 for (j = 0; j < (1 << b->log2_pages); j++)
104 {
105 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
106 {
107 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
108 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Dave Barach97d8dc22016-08-15 15:31:15 -0400110 key.raw = v->kvp[k].key;
111 result.raw = v->kvp[k].value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112
Dave Barach97d8dc22016-08-15 15:31:15 -0400113 if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
114 {
115 vec_add1 (*l2fe_key, key);
116 vec_add1 (*l2fe_res, result);
117 }
118 }
119 v++;
120 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 }
122}
123
Chris Luke16bcf7d2016-09-01 14:31:46 -0400124/** Display the contents of the l2fib. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400126show_l2fib (vlib_main_t * vm,
127 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128{
Dave Barach97d8dc22016-08-15 15:31:15 -0400129 bd_main_t *bdm = &bd_main;
130 l2fib_main_t *msm = &l2fib_main;
Damjan Mariond171d482016-12-05 14:16:38 +0100131 l2_bridge_domain_t *bd_config;
Dave Barach97d8dc22016-08-15 15:31:15 -0400132 BVT (clib_bihash) * h = &msm->mac_table;
Dave Barach908a5ea2017-07-14 12:42:21 -0400133 BVT (clib_bihash_bucket) * b;
Dave Barach97d8dc22016-08-15 15:31:15 -0400134 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 l2fib_entry_key_t key;
136 l2fib_entry_result_t result;
137 u32 first_entry = 1;
138 u64 total_entries = 0;
139 int i, j, k;
140 u8 verbose = 0;
141 u8 raw = 0;
John Lo8d00fff2017-08-03 00:35:36 -0400142 u8 learn = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 u32 bd_id, bd_index = ~0;
Damjan Mariond171d482016-12-05 14:16:38 +0100144 u8 now = (u8) (vlib_time_now (vm) / 60);
145 u8 *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147 if (unformat (input, "raw"))
148 raw = 1;
149 else if (unformat (input, "verbose"))
150 verbose = 1;
151 else if (unformat (input, "bd_index %d", &bd_index))
152 verbose = 1;
John Lo8d00fff2017-08-03 00:35:36 -0400153 else if (unformat (input, "learn"))
154 {
155 learn = 1;
156 verbose = 0;
157 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 else if (unformat (input, "bd_id %d", &bd_id))
159 {
160 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400161 if (p)
162 {
John Lo8d00fff2017-08-03 00:35:36 -0400163 if (learn == 0)
164 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 bd_index = p[0];
Dave Barach97d8dc22016-08-15 15:31:15 -0400166 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400168 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 vlib_cli_output (vm, "no such bridge domain id");
170 return 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400171 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 }
173
174 for (i = 0; i < h->nbuckets; i++)
175 {
176 b = &h->buckets[i];
177 if (b->offset == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400178 continue;
179 v = BV (clib_bihash_get_value) (h, b->offset);
180 for (j = 0; j < (1 << b->log2_pages); j++)
181 {
182 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
183 {
184 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
185 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
John Lo8d00fff2017-08-03 00:35:36 -0400187 if ((verbose || learn) && first_entry)
Dave Barach97d8dc22016-08-15 15:31:15 -0400188 {
189 first_entry = 0;
190 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400191 "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
192 "Mac-Address", "BD-Idx", "If-Idx",
193 "BSN-ISN", "Age(min)", "static", "filter",
194 "bvi", "Interface-Name");
Dave Barach97d8dc22016-08-15 15:31:15 -0400195 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Dave Barach97d8dc22016-08-15 15:31:15 -0400197 key.raw = v->kvp[k].key;
198 result.raw = v->kvp[k].value;
199
John Lo8d00fff2017-08-03 00:35:36 -0400200 if ((verbose || learn)
Dave Barach97d8dc22016-08-15 15:31:15 -0400201 & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
202 {
John Lo8d00fff2017-08-03 00:35:36 -0400203 if (learn && result.fields.age_not)
204 {
205 total_entries++;
206 continue; /* skip provisioned macs */
207 }
208
Damjan Mariond171d482016-12-05 14:16:38 +0100209 bd_config = vec_elt_at_index (l2input_main.bd_configs,
210 key.fields.bd_index);
211
John Lo8d00fff2017-08-03 00:35:36 -0400212 if (bd_config->mac_age && !result.fields.age_not)
Damjan Mariond171d482016-12-05 14:16:38 +0100213 {
214 i16 delta = now - result.fields.timestamp;
215 delta += delta < 0 ? 256 : 0;
216 s = format (s, "%d", delta);
217 }
218 else
John Loda1f2c72017-03-24 20:11:15 -0400219 s = format (s, "-");
Damjan Mariond171d482016-12-05 14:16:38 +0100220
Dave Barach97d8dc22016-08-15 15:31:15 -0400221 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400222 "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -0400223 format_ethernet_address, key.fields.mac,
224 key.fields.bd_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400225 result.fields.sw_if_index == ~0
226 ? -1 : result.fields.sw_if_index,
Eyal Bari7537e712017-04-27 14:07:55 +0300227 result.fields.sn.bd, result.fields.sn.swif,
John Loda1f2c72017-03-24 20:11:15 -0400228 s, result.fields.static_mac ? "*" : "-",
229 result.fields.filter ? "*" : "-",
230 result.fields.bvi ? "*" : "-",
231 format_vnet_sw_if_index_name_with_NA,
232 msm->vnet_main, result.fields.sw_if_index);
Damjan Mariond171d482016-12-05 14:16:38 +0100233 vec_reset_length (s);
Dave Barach97d8dc22016-08-15 15:31:15 -0400234 }
235 total_entries++;
236 }
237 v++;
238 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239 }
240
241 if (total_entries == 0)
242 vlib_cli_output (vm, "no l2fib entries");
243 else
John Lo8d00fff2017-08-03 00:35:36 -0400244 {
245 l2learn_main_t *lm = &l2learn_main;
246 vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d "
247 "Last scan time: %.4esec Learn limit: %d ",
248 total_entries, lm->global_learn_count,
249 msm->age_scan_duration, lm->global_learn_limit);
250 if (lm->client_pid)
251 vlib_cli_output (vm, "L2MAC events client PID: %d "
252 "Last e-scan time: %.4esec Delay: %.2esec "
253 "Max macs in event: %d",
254 lm->client_pid, msm->evt_scan_duration,
255 msm->event_scan_delay, msm->max_macs_in_event);
256 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
258 if (raw)
259 vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
Dave Barach97d8dc22016-08-15 15:31:15 -0400260 BV (format_bihash), h, 1 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Damjan Mariond171d482016-12-05 14:16:38 +0100262 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 return 0;
264}
265
Billy McFall22aa3e92016-09-09 08:46:40 -0400266/*?
267 * This command dispays the MAC Address entries of the L2 FIB table.
268 * Output can be filtered to just get the number of MAC Addresses or display
269 * each MAC Address for all bridge domains or just a single bridge domain.
270 *
271 * @cliexpar
272 * Example of how to display the number of MAC Address entries in the L2
273 * FIB table:
274 * @cliexstart{show l2fib}
275 * 3 l2fib entries
276 * @cliexend
277 * Example of how to display all the MAC Address entries in the L2
278 * FIB table:
279 * @cliexstart{show l2fib verbose}
280 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
281 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
282 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
283 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
284 * 3 l2fib entries
285 * @cliexend
286?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400287/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
289 .path = "show l2fib",
John Lo8d00fff2017-08-03 00:35:36 -0400290 .short_help = "show l2fib [verbose | learn | bd_id <nn> | bd_index <nn> | raw",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 .function = show_l2fib,
292};
Dave Barach97d8dc22016-08-15 15:31:15 -0400293/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
295
Dave Barach97d8dc22016-08-15 15:31:15 -0400296/* Remove all entries from the l2fib */
297void
Eyal Bari7537e712017-04-27 14:07:55 +0300298l2fib_clear_table (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299{
Dave Barach97d8dc22016-08-15 15:31:15 -0400300 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
Eyal Bari7537e712017-04-27 14:07:55 +0300302 /* Remove all entries */
303 BV (clib_bihash_free) (&mp->mac_table);
304 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
305 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 l2learn_main.global_learn_count = 0;
307}
308
Chris Luke16bcf7d2016-09-01 14:31:46 -0400309/** Clear all entries in L2FIB.
310 * @TODO: Later we may want a way to remove only the non-static entries
Dave Barach97d8dc22016-08-15 15:31:15 -0400311 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400313clear_l2fib (vlib_main_t * vm,
314 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315{
Eyal Bari7537e712017-04-27 14:07:55 +0300316 l2fib_clear_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 return 0;
318}
319
Billy McFall22aa3e92016-09-09 08:46:40 -0400320/*?
321 * This command clears all the MAC Address entries from the L2 FIB table.
322 *
323 * @cliexpar
324 * Example of how to clear the L2 FIB Table:
325 * @cliexcmd{clear l2fib}
326 * Example to show the L2 FIB Table has been cleared:
327 * @cliexstart{show l2fib verbose}
328 * no l2fib entries
329 * @cliexend
330?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400331/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
333 .path = "clear l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400334 .short_help = "clear l2fib",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335 .function = clear_l2fib,
336};
Dave Barach97d8dc22016-08-15 15:31:15 -0400337/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
Eyal Bari7537e712017-04-27 14:07:55 +0300339static inline l2fib_seq_num_t
340l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
341{
Eyal Bari7537e712017-04-27 14:07:55 +0300342 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
343 /* *INDENT-OFF* */
344 return (l2fib_seq_num_t) {
Eyal Bari0f360dc2017-06-14 13:11:20 +0300345 .swif = *l2fib_swif_seq_num (sw_if_index),
Eyal Bari7537e712017-04-27 14:07:55 +0300346 .bd = bd_config->seq_num,
347 };
348 /* *INDENT-ON* */
349}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
Dave Barach97d8dc22016-08-15 15:31:15 -0400351/**
352 * Add an entry to the l2fib.
353 * If the entry already exists then overwrite it
354 */
355void
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200356l2fib_add_entry (u8 * mac, u32 bd_index,
John Lo8d00fff2017-08-03 00:35:36 -0400357 u32 sw_if_index, u8 static_mac, u8 filter_mac, u8 bvi_mac)
Dave Barach97d8dc22016-08-15 15:31:15 -0400358{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359 l2fib_entry_key_t key;
360 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400361 __attribute__ ((unused)) u32 bucket_contents;
John Lo8d00fff2017-08-03 00:35:36 -0400362 l2fib_main_t *fm = &l2fib_main;
363 l2learn_main_t *lm = &l2learn_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400364 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
Dave Barach97d8dc22016-08-15 15:31:15 -0400366 /* set up key */
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200367 key.raw = l2fib_make_key (mac, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400368
John Lo8d00fff2017-08-03 00:35:36 -0400369 /* check if entry alread exist */
370 if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv))
371 {
372 /* decrement counter if overwriting a learned mac */
373 result.raw = kv.value;
374 if ((result.fields.age_not == 0) && (lm->global_learn_count))
375 lm->global_learn_count--;
376 }
377
Dave Barach97d8dc22016-08-15 15:31:15 -0400378 /* set up result */
379 result.raw = 0; /* clear all fields */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 result.fields.sw_if_index = sw_if_index;
381 result.fields.static_mac = static_mac;
382 result.fields.filter = filter_mac;
383 result.fields.bvi = bvi_mac;
John Lo8d00fff2017-08-03 00:35:36 -0400384 result.fields.age_not = 1; /* no aging for provisioned entry */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
386 kv.key = key.raw;
387 kv.value = result.raw;
388
John Lo8d00fff2017-08-03 00:35:36 -0400389 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390}
391
Dave Barach97d8dc22016-08-15 15:31:15 -0400392/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400393 * Add an entry to the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400394 * The CLI format is:
395 * l2fib add <mac> <bd> <intf> [static] [bvi]
396 * l2fib add <mac> <bd> filter
397 * Note that filter and bvi entries are always static
398 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400400l2fib_add (vlib_main_t * vm,
401 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402{
Dave Barach97d8dc22016-08-15 15:31:15 -0400403 bd_main_t *bdm = &bd_main;
404 vnet_main_t *vnm = vnet_get_main ();
405 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200406 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 u32 bd_id;
408 u32 bd_index;
409 u32 sw_if_index = ~0;
410 u32 filter_mac = 0;
411 u32 static_mac = 0;
412 u32 bvi_mac = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400413 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200415 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 {
417 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400418 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 goto done;
420 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400421
422 if (!unformat (input, "%d", &bd_id))
423 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400425 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400427 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
429 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400430 if (!p)
431 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
433 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400434 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 bd_index = p[0];
436
Dave Barach97d8dc22016-08-15 15:31:15 -0400437 if (unformat (input, "filter"))
438 {
439 filter_mac = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440 static_mac = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400441
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400443 else
444 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
Dave Barach97d8dc22016-08-15 15:31:15 -0400446 if (!unformat_user
447 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
448 {
449 error = clib_error_return (0, "unknown interface `%U'",
450 format_unformat_error, input);
451 goto done;
452 }
453 if (unformat (input, "static"))
454 {
455 static_mac = 1;
456 }
457 else if (unformat (input, "bvi"))
458 {
459 bvi_mac = 1;
460 static_mac = 1;
461 }
462 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
John Lob2fd6cb2017-07-12 19:56:45 -0400464 if (vec_len (l2input_main.configs) <= sw_if_index)
465 {
466 error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode",
467 sw_if_index);
468 goto done;
469 }
470
Eyal Bari31a71ab2017-06-25 14:42:33 +0300471 if (filter_mac)
472 l2fib_add_filter_entry (mac, bd_index);
473 else
474 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400475
476done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 return error;
478}
479
Billy McFall22aa3e92016-09-09 08:46:40 -0400480/*?
481 * This command adds a MAC Address entry to the L2 FIB table
482 * of an existing bridge-domain. The MAC Address can be static
483 * or dynamic. This command also allows a filter to be added,
484 * such that packets with given MAC Addresses (source mac or
485 * destination mac match) are dropped.
486 *
487 * @cliexpar
488 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
489 * of a bridge-domain (where 200 is the bridge-domain-id):
490 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
491 * Example of how to add a static MAC Address entry to the L2 FIB table
492 * of a bridge-domain (where 200 is the bridge-domain-id):
493 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
494 * Example of how to add a filter such that a packet with the given MAC
495 * Address will be dropped in a given bridge-domain (where 200 is the
496 * bridge-domain-id):
497 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
498 * Example of show command of the provisioned MAC Addresses and filters:
499 * @cliexstart{show l2fib verbose}
500 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
501 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0
502 * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0
503 * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0
504 * 3 l2fib entries
505 * @cliexend
506?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400507/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
509 .path = "l2fib add",
Billy McFall22aa3e92016-09-09 08:46:40 -0400510 .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511 .function = l2fib_add,
512};
Dave Barach97d8dc22016-08-15 15:31:15 -0400513/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514
515
516static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400517l2fib_test_command_fn (vlib_main_t * vm,
518 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519{
Dave Barach97d8dc22016-08-15 15:31:15 -0400520 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200521 u8 mac[6], save_mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522 u32 bd_index = 0;
523 u32 sw_if_index = 8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 u32 bvi_mac = 0;
525 u32 is_add = 0;
526 u32 is_del = 0;
527 u32 is_check = 0;
528 u32 count = 1;
529 int mac_set = 0;
530 int i;
531
Dave Barach97d8dc22016-08-15 15:31:15 -0400532 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200534 if (unformat (input, "mac %U", unformat_ethernet_address, mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400535 mac_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 else if (unformat (input, "add"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400537 is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 else if (unformat (input, "del"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400539 is_del = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 else if (unformat (input, "check"))
Dave Barach97d8dc22016-08-15 15:31:15 -0400541 is_check = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 else if (unformat (input, "count %d", &count))
Dave Barach97d8dc22016-08-15 15:31:15 -0400543 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400545 break;
546 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
548 if (mac_set == 0)
549 return clib_error_return (0, "mac not set");
550
551 if (is_add == 0 && is_del == 0 && is_check == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400552 return clib_error_return (0,
553 "noop: pick at least one of (add,del,check)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200555 clib_memcpy (save_mac, mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556
557 if (is_add)
558 {
559 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400560 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200561 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, *mac, bvi_mac);
562 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400563 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564 }
565
566 if (is_check)
567 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400568 BVT (clib_bihash_kv) kv;
569 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200571 clib_memcpy (mac, save_mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
573 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400574 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200575 kv.key = l2fib_make_key (mac, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400576 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
577 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200578 clib_warning ("key %U AWOL", format_ethernet_address, mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400579 break;
580 }
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200581 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400582 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583 }
584
585 if (is_del)
586 {
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200587 clib_memcpy (mac, save_mac, 6);
588
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589 for (i = 0; i < count; i++)
Dave Barach97d8dc22016-08-15 15:31:15 -0400590 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400591 l2fib_del_entry (mac, bd_index);
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200592 incr_mac_address (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400593 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594 }
595
596 return error;
597}
598
Billy McFall22aa3e92016-09-09 08:46:40 -0400599/*?
600 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
601 * bridge domain (bridge-domain-id of 0) to be modified.
602 *
603 * @cliexpar
604 * @parblock
605 * Example of how to add a set of 4 sequential MAC Address entries to L2
606 * FIB table of the default bridge-domain:
607 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
608 *
609 * Show the set of 4 sequential MAC Address entries that were added:
610 * @cliexstart{show l2fib verbose}
611 * Mac Address BD Idx Interface Index static filter bvi refresh timestamp
612 * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
613 * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
614 * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
615 * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0
616 * 4 l2fib entries
617 * @cliexend
618 *
619 * Example of how to check that the set of 4 sequential MAC Address
620 * entries were added to L2 FIB table of the default
621 * bridge-domain. Used a count of 5 to produce an error:
622 *
623 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
624 * The output of the check command is in the log files. Log file
625 * location may vary based on your OS and Version:
626 *
627 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
628 *
629 * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
630 *
631 * Example of how to delete a set of 4 sequential MAC Address entries
632 * from L2 FIB table of the default bridge-domain:
633 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
634 * @endparblock
635?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400636/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637VLIB_CLI_COMMAND (l2fib_test_command, static) = {
638 .path = "test l2fib",
Billy McFall22aa3e92016-09-09 08:46:40 -0400639 .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640 .function = l2fib_test_command_fn,
641};
Dave Barach97d8dc22016-08-15 15:31:15 -0400642/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
644
Dave Barach97d8dc22016-08-15 15:31:15 -0400645/**
646 * Delete an entry from the l2fib.
647 * Return 0 if the entry was deleted, or 1 if it was not found
648 */
Eyal Bari7537e712017-04-27 14:07:55 +0300649static u32
650l2fib_del_entry_by_key (u64 raw_key)
Dave Barach97d8dc22016-08-15 15:31:15 -0400651{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652
653 l2fib_entry_result_t result;
Dave Barach97d8dc22016-08-15 15:31:15 -0400654 l2fib_main_t *mp = &l2fib_main;
655 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Dave Barach97d8dc22016-08-15 15:31:15 -0400657 /* set up key */
Eyal Bari7537e712017-04-27 14:07:55 +0300658 kv.key = raw_key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Dave Barach97d8dc22016-08-15 15:31:15 -0400660 if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 return 1;
662
663 result.raw = kv.value;
664
Dave Barach97d8dc22016-08-15 15:31:15 -0400665 /* decrement counter if dynamically learned mac */
John Lo8d00fff2017-08-03 00:35:36 -0400666 if ((result.fields.age_not == 0) && (l2learn_main.global_learn_count))
667 l2learn_main.global_learn_count--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
Dave Barach97d8dc22016-08-15 15:31:15 -0400669 /* Remove entry from hash table */
670 BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671 return 0;
672}
673
Dave Barach97d8dc22016-08-15 15:31:15 -0400674/**
Eyal Bari7537e712017-04-27 14:07:55 +0300675 * Delete an entry from the l2fib.
676 * Return 0 if the entry was deleted, or 1 if it was not found
677 */
678u32
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200679l2fib_del_entry (u8 * mac, u32 bd_index)
Eyal Bari7537e712017-04-27 14:07:55 +0300680{
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200681 return l2fib_del_entry_by_key (l2fib_make_key (mac, bd_index));
Eyal Bari7537e712017-04-27 14:07:55 +0300682}
683
684/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400685 * Delete an entry from the L2FIB.
Dave Barach97d8dc22016-08-15 15:31:15 -0400686 * The CLI format is:
687 * l2fib del <mac> <bd-id>
688 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400690l2fib_del (vlib_main_t * vm,
691 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692{
Dave Barach97d8dc22016-08-15 15:31:15 -0400693 bd_main_t *bdm = &bd_main;
694 clib_error_t *error = 0;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200695 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696 u32 bd_id;
697 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400698 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200700 if (!unformat (input, "%U", unformat_ethernet_address, mac))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 {
702 error = clib_error_return (0, "expected mac address `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400703 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 goto done;
705 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400706
707 if (!unformat (input, "%d", &bd_id))
708 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400710 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400712 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713
714 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400715 if (!p)
716 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
718 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400719 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 bd_index = p[0];
721
Dave Barach97d8dc22016-08-15 15:31:15 -0400722 /* Delete the entry */
723 if (l2fib_del_entry (mac, bd_index))
724 {
725 error = clib_error_return (0, "mac entry not found");
726 goto done;
727 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728
Dave Barach97d8dc22016-08-15 15:31:15 -0400729done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730 return error;
731}
732
Billy McFall22aa3e92016-09-09 08:46:40 -0400733/*?
734 * This command deletes an existing MAC Address entry from the L2 FIB
735 * table of an existing bridge-domain.
736 *
737 * @cliexpar
738 * 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):
739 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
740?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400741/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
743 .path = "l2fib del",
Billy McFall22aa3e92016-09-09 08:46:40 -0400744 .short_help = "l2fib del <mac> <bridge-domain-id>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 .function = l2fib_del,
746};
Dave Barach97d8dc22016-08-15 15:31:15 -0400747/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
John Loda1f2c72017-03-24 20:11:15 -0400749/**
750 Kick off ager to scan MACs to age/delete MAC entries
751*/
752void
753l2fib_start_ager_scan (vlib_main_t * vm)
754{
Eyal Bari24db0ec2017-09-27 21:43:51 +0300755 uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS;
John Loda1f2c72017-03-24 20:11:15 -0400756
757 /* check if there is at least one bd with mac aging enabled */
Eyal Bari24db0ec2017-09-27 21:43:51 +0300758 l2_bridge_domain_t *bd_config;
John Loda1f2c72017-03-24 20:11:15 -0400759 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300760 {
John Loda1f2c72017-03-24 20:11:15 -0400761 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300762 {
763 evt = L2_MAC_AGE_PROCESS_EVENT_START;
764 break;
765 }
766 }
John Loda1f2c72017-03-24 20:11:15 -0400767
768 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
Eyal Bari24db0ec2017-09-27 21:43:51 +0300769 evt, 0);
John Loda1f2c72017-03-24 20:11:15 -0400770}
771
772/**
Eyal Bari7537e712017-04-27 14:07:55 +0300773 Flush all non static MACs from an interface
John Loda1f2c72017-03-24 20:11:15 -0400774*/
775void
776l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
777{
Eyal Bari0f360dc2017-06-14 13:11:20 +0300778 *l2fib_swif_seq_num (sw_if_index) += 1;
John Loda1f2c72017-03-24 20:11:15 -0400779 l2fib_start_ager_scan (vm);
780}
781
782/**
Eyal Bari7537e712017-04-27 14:07:55 +0300783 Flush all non static MACs in a bridge domain
John Loda1f2c72017-03-24 20:11:15 -0400784*/
785void
786l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
787{
Eyal Bari7537e712017-04-27 14:07:55 +0300788 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400789 bd_config->seq_num += 1;
790 l2fib_start_ager_scan (vm);
791}
792
793/**
Eyal Bari7537e712017-04-27 14:07:55 +0300794 Flush all non static MACs - flushes all valid BDs
795*/
796void
797l2fib_flush_all_mac (vlib_main_t * vm)
798{
799 l2_bridge_domain_t *bd_config;
800 vec_foreach (bd_config, l2input_main.bd_configs)
801 if (bd_is_valid (bd_config))
802 bd_config->seq_num += 1;
803
804 l2fib_start_ager_scan (vm);
805}
806
807
808/**
John Loda1f2c72017-03-24 20:11:15 -0400809 Flush MACs, except static ones, associated with an interface
810 The CLI format is:
811 l2fib flush-mac interface <if-name>
812*/
813static clib_error_t *
814l2fib_flush_mac_int (vlib_main_t * vm,
815 unformat_input_t * input, vlib_cli_command_t * cmd)
816{
817 vnet_main_t *vnm = vnet_get_main ();
818 clib_error_t *error = 0;
819 u32 sw_if_index;
820
821 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
822 {
823 error = clib_error_return (0, "unknown interface `%U'",
824 format_unformat_error, input);
825 goto done;
826 }
827
828 l2fib_flush_int_mac (vm, sw_if_index);
829
830done:
831 return error;
832}
833
Eyal Bari7537e712017-04-27 14:07:55 +0300834/**
835 Flush all MACs, except static ones
836 The CLI format is:
837 l2fib flush-mac all
838*/
839static clib_error_t *
840l2fib_flush_mac_all (vlib_main_t * vm,
841 unformat_input_t * input, vlib_cli_command_t * cmd)
842{
843 l2fib_flush_all_mac (vm);
844 return 0;
845}
846
847/*?
848 * This command kick off ager to delete all existing MAC Address entries,
849 * except static ones, associated with an interface from the L2 FIB table.
850 *
851 * @cliexpar
852 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
853 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
854?*/
855/* *INDENT-OFF* */
856VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
857 .path = "l2fib flush-mac all",
858 .short_help = "l2fib flush-mac all",
859 .function = l2fib_flush_mac_all,
860};
861/* *INDENT-ON* */
862
John Loda1f2c72017-03-24 20:11:15 -0400863/*?
864 * This command kick off ager to delete all existing MAC Address entries,
865 * except static ones, associated with an interface from the L2 FIB table.
866 *
867 * @cliexpar
868 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
869 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
870?*/
871/* *INDENT-OFF* */
872VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
873 .path = "l2fib flush-mac interface",
874 .short_help = "l2fib flush-mac interface <if-name>",
875 .function = l2fib_flush_mac_int,
876};
877/* *INDENT-ON* */
878
879/**
880 Flush bridge-domain MACs except static ones.
881 The CLI format is:
882 l2fib flush-mac bridge-domain <bd-id>
883*/
884static clib_error_t *
885l2fib_flush_mac_bd (vlib_main_t * vm,
886 unformat_input_t * input, vlib_cli_command_t * cmd)
887{
888 bd_main_t *bdm = &bd_main;
889 clib_error_t *error = 0;
890 u32 bd_index, bd_id;
891 uword *p;
892
893 if (!unformat (input, "%d", &bd_id))
894 {
895 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
896 format_unformat_error, input);
897 goto done;
898 }
899
900 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
901 if (p)
902 bd_index = *p;
903 else
904 return clib_error_return (0, "No such bridge domain %d", bd_id);
905
906 l2fib_flush_bd_mac (vm, bd_index);
907
908done:
909 return error;
910}
911
912/*?
913 * This command kick off ager to delete all existing MAC Address entries,
914 * except static ones, in a bridge domain from the L2 FIB table.
915 *
916 * @cliexpar
917 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
918 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
919?*/
920/* *INDENT-OFF* */
921VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
922 .path = "l2fib flush-mac bridge-domain",
923 .short_help = "l2fib flush-mac bridge-domain <bd-id>",
924 .function = l2fib_flush_mac_bd,
925};
926/* *INDENT-ON* */
927
Eyal Bariafc47aa2017-04-20 14:45:17 +0300928clib_error_t *
929l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
930{
931 l2_input_config_t *config = l2input_intf_config (sw_if_index);
932 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
933 l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
934 return 0;
935}
936
937VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938
Dave Barach97d8dc22016-08-15 15:31:15 -0400939BVT (clib_bihash) * get_mac_table (void)
940{
941 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 return &mp->mac_table;
943}
944
John Lo8d00fff2017-08-03 00:35:36 -0400945static_always_inline void *
946allocate_mac_evt_buf (u32 client, u32 client_index)
947{
948 l2fib_main_t *fm = &l2fib_main;
949 vl_api_l2_macs_event_t *mp = vl_msg_api_alloc
950 (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t)));
951 mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT);
952 mp->pid = htonl (client);
953 mp->client_index = client_index;
954 return mp;
955}
956
957static_always_inline f64
958l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only)
959{
960 l2fib_main_t *fm = &l2fib_main;
961 l2learn_main_t *lm = &l2learn_main;
962
963 BVT (clib_bihash) * h = &fm->mac_table;
964 int i, j, k;
965 f64 last_start = start_time;
966 f64 accum_t = 0;
967 f64 delta_t = 0;
968 u32 evt_idx = 0;
969 u32 learn_count = 0;
970 u32 client = lm->client_pid;
971 u32 cl_idx = lm->client_index;
972 vl_api_l2_macs_event_t *mp = 0;
973 unix_shared_memory_queue_t *q = 0;
974
975 if (client)
976 {
977 mp = allocate_mac_evt_buf (client, cl_idx);
978 q = vl_api_client_index_to_input_queue (lm->client_index);
979 }
980
981 for (i = 0; i < h->nbuckets; i++)
982 {
983 /* allow no more than 20us without a pause */
984 delta_t = vlib_time_now (vm) - last_start;
985 if (delta_t > 20e-6)
986 {
987 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
988 last_start = vlib_time_now (vm);
989 accum_t += delta_t;
990 }
991
992 if (i < (h->nbuckets - 3))
993 {
994 BVT (clib_bihash_bucket) * b = &h->buckets[i + 3];
995 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
996 b = &h->buckets[i + 1];
997 if (b->offset)
998 {
999 BVT (clib_bihash_value) * v =
1000 BV (clib_bihash_get_value) (h, b->offset);
1001 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
1002 }
1003 }
1004
1005 BVT (clib_bihash_bucket) * b = &h->buckets[i];
1006 if (b->offset == 0)
1007 continue;
1008 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
1009 for (j = 0; j < (1 << b->log2_pages); j++)
1010 {
1011 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1012 {
1013 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
1014 continue;
1015
1016 l2fib_entry_key_t key = {.raw = v->kvp[k].key };
1017 l2fib_entry_result_t result = {.raw = v->kvp[k].value };
1018
1019 if (result.fields.age_not == 0)
1020 learn_count++;
1021
John Lo8d00fff2017-08-03 00:35:36 -04001022 if (client)
1023 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001024 if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event))
1025 {
1026 /* event message full, send it and start a new one */
1027 if (q && (q->cursize < q->maxsize))
1028 {
1029 mp->n_macs = htonl (evt_idx);
1030 vl_msg_api_send_shmem (q, (u8 *) & mp);
1031 mp = allocate_mac_evt_buf (client, cl_idx);
1032 }
1033 else
1034 {
Dave Barach59b25652017-09-10 15:04:27 -04001035 if (q)
1036 clib_warning ("MAC event to pid %d queue stuffed!"
1037 " %d MAC entries lost", client,
1038 evt_idx);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001039 }
1040 evt_idx = 0;
1041 }
1042
John Lo8d00fff2017-08-03 00:35:36 -04001043 if (result.fields.lrn_evt)
1044 {
1045 /* copy mac entry to event msg */
1046 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac,
1047 6);
1048 mp->mac[evt_idx].is_del = 0;
1049 mp->mac[evt_idx].sw_if_index =
1050 htonl (result.fields.sw_if_index);
1051 /* clear event bit and update mac entry */
1052 result.fields.lrn_evt = 0;
1053 BVT (clib_bihash_kv) kv;
1054 kv.key = key.raw;
1055 kv.value = result.raw;
1056 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1);
1057 evt_idx++;
1058 continue; /* skip aging */
1059 }
1060 }
1061
1062 if (event_only || result.fields.age_not)
1063 continue; /* skip aging - static_mac alsways age_not */
1064
1065 /* start aging processing */
1066 u32 bd_index = key.fields.bd_index;
1067 u32 sw_if_index = result.fields.sw_if_index;
1068 u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
1069 if (result.fields.sn.as_u16 != sn)
1070 goto age_out; /* stale mac */
1071
1072 l2_bridge_domain_t *bd_config =
1073 vec_elt_at_index (l2input_main.bd_configs, bd_index);
1074
1075 if (bd_config->mac_age == 0)
1076 continue; /* skip aging */
1077
1078 i16 delta = (u8) (start_time / 60) - result.fields.timestamp;
1079 delta += delta < 0 ? 256 : 0;
1080
1081 if (delta < bd_config->mac_age)
1082 continue; /* still valid */
1083
1084 age_out:
1085 if (client)
1086 {
1087 /* copy mac entry to event msg */
1088 clib_memcpy (mp->mac[evt_idx].mac_addr, key.fields.mac, 6);
1089 mp->mac[evt_idx].is_del = 1;
1090 mp->mac[evt_idx].sw_if_index =
1091 htonl (result.fields.sw_if_index);
1092 evt_idx++;
1093 }
1094 /* delete mac entry */
1095 BVT (clib_bihash_kv) kv;
1096 kv.key = key.raw;
1097 BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0);
1098 learn_count--;
1099 }
1100 v++;
1101 }
1102 }
1103
1104 /* keep learn count consistent */
1105 l2learn_main.global_learn_count = learn_count;
1106
1107 if (mp)
1108 {
1109 /* send any outstanding mac event message else free message buffer */
1110 if (evt_idx)
1111 {
1112 if (q && (q->cursize < q->maxsize))
1113 {
1114 mp->n_macs = htonl (evt_idx);
1115 vl_msg_api_send_shmem (q, (u8 *) & mp);
1116 }
1117 else
1118 {
Dave Barach59b25652017-09-10 15:04:27 -04001119 if (q)
1120 clib_warning ("MAC event to pid %d queue stuffed!"
1121 " %d MAC entries lost", client, evt_idx);
John Lo8d00fff2017-08-03 00:35:36 -04001122 vl_msg_api_free (mp);
1123 }
1124 }
1125 else
1126 vl_msg_api_free (mp);
1127 }
1128 return delta_t + accum_t;
1129}
1130
John Lo8d00fff2017-08-03 00:35:36 -04001131/* Maximum f64 value */
1132#define TIME_MAX (1.7976931348623157e+308)
1133
Damjan Mariond171d482016-12-05 14:16:38 +01001134static uword
1135l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1136 vlib_frame_t * f)
1137{
1138 uword event_type, *event_data = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001139 l2fib_main_t *fm = &l2fib_main;
1140 l2learn_main_t *lm = &l2learn_main;
Damjan Mariond171d482016-12-05 14:16:38 +01001141 bool enabled = 0;
John Lo8d00fff2017-08-03 00:35:36 -04001142 f64 start_time, next_age_scan_time = TIME_MAX;
Damjan Mariond171d482016-12-05 14:16:38 +01001143
1144 while (1)
1145 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001146 if (lm->client_pid)
1147 vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay);
1148 else if (enabled)
John Lo8d00fff2017-08-03 00:35:36 -04001149 {
Eyal Bari24db0ec2017-09-27 21:43:51 +03001150 f64 t = next_age_scan_time - vlib_time_now (vm);
1151 vlib_process_wait_for_event_or_clock (vm, t);
John Lo8d00fff2017-08-03 00:35:36 -04001152 }
Damjan Mariond171d482016-12-05 14:16:38 +01001153 else
1154 vlib_process_wait_for_event (vm);
1155
1156 event_type = vlib_process_get_events (vm, &event_data);
1157 vec_reset_length (event_data);
1158
John Lo8d00fff2017-08-03 00:35:36 -04001159 start_time = vlib_time_now (vm);
Eyal Bari24db0ec2017-09-27 21:43:51 +03001160 enum
1161 { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE;
John Lo8d00fff2017-08-03 00:35:36 -04001162
Damjan Mariond171d482016-12-05 14:16:38 +01001163 switch (event_type)
1164 {
John Lo8d00fff2017-08-03 00:35:36 -04001165 case ~0: /* timer expired */
Eyal Bari24db0ec2017-09-27 21:43:51 +03001166 if (lm->client_pid != 0 && start_time < next_age_scan_time)
John Lo8d00fff2017-08-03 00:35:36 -04001167 scan = SCAN_MAC_EVENT;
Damjan Mariond171d482016-12-05 14:16:38 +01001168 break;
John Lo8d00fff2017-08-03 00:35:36 -04001169
Damjan Mariond171d482016-12-05 14:16:38 +01001170 case L2_MAC_AGE_PROCESS_EVENT_START:
1171 enabled = 1;
1172 break;
John Lo8d00fff2017-08-03 00:35:36 -04001173
Damjan Mariond171d482016-12-05 14:16:38 +01001174 case L2_MAC_AGE_PROCESS_EVENT_STOP:
1175 enabled = 0;
Eyal Bari24db0ec2017-09-27 21:43:51 +03001176 scan = SCAN_DISABLE;
1177 break;
John Lo8d00fff2017-08-03 00:35:36 -04001178
John Loda1f2c72017-03-24 20:11:15 -04001179 case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
John Loda1f2c72017-03-24 20:11:15 -04001180 break;
John Lo8d00fff2017-08-03 00:35:36 -04001181
Damjan Mariond171d482016-12-05 14:16:38 +01001182 default:
1183 ASSERT (0);
1184 }
Eyal Bari7537e712017-04-27 14:07:55 +03001185
John Lo8d00fff2017-08-03 00:35:36 -04001186 if (scan == SCAN_MAC_EVENT)
1187 l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1);
1188 else
Eyal Bari24db0ec2017-09-27 21:43:51 +03001189 {
1190 if (scan == SCAN_MAC_AGE)
1191 l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0);
1192 if (scan == SCAN_DISABLE)
1193 {
1194 l2fib_main.age_scan_duration = 0;
1195 l2fib_main.evt_scan_duration = 0;
1196 }
1197 /* schedule next scan */
1198 if (enabled)
1199 next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL;
1200 else
1201 next_age_scan_time = TIME_MAX;
1202 }
Damjan Mariond171d482016-12-05 14:16:38 +01001203 }
1204 return 0;
1205}
1206
1207/* *INDENT-OFF* */
1208VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1209 .function = l2fib_mac_age_scanner_process,
1210 .type = VLIB_NODE_TYPE_PROCESS,
1211 .name = "l2fib-mac-age-scanner-process",
1212};
1213/* *INDENT-ON* */
1214
Dave Barach97d8dc22016-08-15 15:31:15 -04001215clib_error_t *
1216l2fib_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217{
Dave Barach97d8dc22016-08-15 15:31:15 -04001218 l2fib_main_t *mp = &l2fib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219 l2fib_entry_key_t test_key;
1220 u8 test_mac[6];
Dave Barach97d8dc22016-08-15 15:31:15 -04001221
Ed Warnickecb9cada2015-12-08 15:45:58 -07001222 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -04001223 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001224
Dave Barach97d8dc22016-08-15 15:31:15 -04001225 /* Create the hash table */
1226 BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1227 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
Dave Barach97d8dc22016-08-15 15:31:15 -04001229 /* verify the key constructor is good, since it is endian-sensitive */
1230 memset (test_mac, 0, sizeof (test_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231 test_mac[0] = 0x11;
1232 test_key.raw = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001233 test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234 ASSERT (test_key.fields.mac[0] == 0x11);
1235 ASSERT (test_key.fields.bd_index == 0x1234);
1236
1237 return 0;
1238}
1239
1240VLIB_INIT_FUNCTION (l2fib_init);
1241
Dave Barach97d8dc22016-08-15 15:31:15 -04001242/*
1243 * fd.io coding-style-patch-verification: ON
1244 *
1245 * Local Variables:
1246 * eval: (c-set-style "gnu")
1247 * End:
1248 */