Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | #include <vnet/ethernet/ethernet.h> |
| 22 | #include <vlib/cli.h> |
| 23 | |
| 24 | #include <vppinfra/error.h> |
| 25 | #include <vppinfra/hash.h> |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 26 | #include <vnet/l2/l2_input.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | #include <vnet/l2/l2_fib.h> |
| 28 | #include <vnet/l2/l2_learn.h> |
| 29 | #include <vnet/l2/l2_bd.h> |
| 30 | |
| 31 | #include <vppinfra/bihash_template.c> |
| 32 | |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 33 | #include <vlibmemory/api.h> |
| 34 | #include <vnet/vnet_msg_enum.h> |
| 35 | |
| 36 | #define vl_typedefs /* define message structures */ |
| 37 | #include <vnet/vnet_all_api_h.h> |
| 38 | #undef vl_typedefs |
| 39 | |
| 40 | #define vl_endianfun /* define message structures */ |
| 41 | #include <vnet/vnet_all_api_h.h> |
| 42 | #undef vl_endianfun |
| 43 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 44 | /** |
| 45 | * @file |
| 46 | * @brief Ethernet MAC Address FIB Table Management. |
| 47 | * |
| 48 | * The MAC Address forwarding table for bridge-domains is called the l2fib. |
| 49 | * Entries are added automatically as part of mac learning, but MAC Addresses |
| 50 | * entries can also be added manually. |
| 51 | * |
| 52 | */ |
| 53 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | l2fib_main_t l2fib_main; |
| 55 | |
Neale Ranns | 7d645f7 | 2018-10-24 02:25:06 -0700 | [diff] [blame] | 56 | u8 * |
| 57 | format_l2fib_entry_result_flags (u8 * s, va_list * args) |
| 58 | { |
| 59 | l2fib_entry_result_flags_t flags = va_arg (*args, int); |
| 60 | |
| 61 | if (L2FIB_ENTRY_RESULT_FLAG_NONE == flags) |
| 62 | { |
| 63 | s = format (s, "none"); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | #define _(a,v,t) { \ |
| 68 | if (flags & L2FIB_ENTRY_RESULT_FLAG_##a) \ |
| 69 | s = format (s, "%s ", t); \ |
| 70 | } |
| 71 | foreach_l2fib_entry_result_attr |
| 72 | #undef _ |
| 73 | } |
| 74 | return (s); |
| 75 | } |
| 76 | |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 77 | static void |
| 78 | incr_mac_address (u8 * mac) |
| 79 | { |
| 80 | u64 tmp = *((u64 *) mac); |
| 81 | tmp = clib_net_to_host_u64 (tmp); |
| 82 | tmp += 1 << 16; /* skip unused (least significant) octets */ |
| 83 | tmp = clib_host_to_net_u64 (tmp); |
| 84 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 85 | clib_memcpy_fast (mac, &tmp, 6); |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 88 | /** Format sw_if_index. If the value is ~0, use the text "N/A" */ |
| 89 | u8 * |
| 90 | format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 92 | vnet_main_t *vnm = va_arg (*args, vnet_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | u32 sw_if_index = va_arg (*args, u32); |
| 94 | if (sw_if_index == ~0) |
| 95 | return format (s, "N/A"); |
Eyal Bari | b823df5 | 2017-06-12 17:07:22 +0300 | [diff] [blame] | 96 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 97 | vnet_sw_interface_t *swif = |
| 98 | vnet_get_sw_interface_or_null (vnm, sw_if_index); |
Eyal Bari | b823df5 | 2017-06-12 17:07:22 +0300 | [diff] [blame] | 99 | if (!swif) |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 100 | return format (s, "Stale"); |
Eyal Bari | b823df5 | 2017-06-12 17:07:22 +0300 | [diff] [blame] | 101 | |
| 102 | return format (s, "%U", format_vnet_sw_interface_name, vnm, |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 103 | vnet_get_sw_interface_or_null (vnm, sw_if_index)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 106 | typedef struct l2fib_dump_walk_ctx_t_ |
| 107 | { |
| 108 | u32 bd_index; |
| 109 | l2fib_entry_key_t *l2fe_key; |
| 110 | l2fib_entry_result_t *l2fe_res; |
| 111 | } l2fib_dump_walk_ctx_t; |
| 112 | |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 113 | static int |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 114 | l2fib_dump_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg) |
| 115 | { |
| 116 | l2fib_dump_walk_ctx_t *ctx = arg; |
| 117 | l2fib_entry_result_t result; |
| 118 | l2fib_entry_key_t key; |
| 119 | |
| 120 | key.raw = kvp->key; |
| 121 | result.raw = kvp->value; |
| 122 | |
| 123 | if ((ctx->bd_index == ~0) || (ctx->bd_index == key.fields.bd_index)) |
| 124 | { |
| 125 | vec_add1 (ctx->l2fe_key, key); |
| 126 | vec_add1 (ctx->l2fe_res, result); |
| 127 | } |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 128 | |
| 129 | return (BIHASH_WALK_CONTINUE); |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 132 | void |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 133 | l2fib_table_dump (u32 bd_index, |
| 134 | l2fib_entry_key_t ** l2fe_key, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 135 | l2fib_entry_result_t ** l2fe_res) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 137 | l2fib_main_t *msm = &l2fib_main; |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 138 | l2fib_dump_walk_ctx_t ctx = { |
| 139 | .bd_index = bd_index, |
| 140 | }; |
| 141 | |
| 142 | BV (clib_bihash_foreach_key_value_pair) |
| 143 | (&msm->mac_table, l2fib_dump_walk_cb, &ctx); |
| 144 | |
| 145 | *l2fe_key = ctx.l2fe_key; |
| 146 | *l2fe_res = ctx.l2fe_res; |
| 147 | } |
| 148 | |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 149 | void |
| 150 | l2_fib_extract_seq_num (l2fib_seq_num_t sn, u8 * bd_sn, u8 * if_sn) |
| 151 | { |
| 152 | *bd_sn = sn >> 8; |
| 153 | *if_sn = sn & 0xff; |
| 154 | } |
| 155 | |
| 156 | u8 * |
| 157 | format_l2_fib_seq_num (u8 * s, va_list * a) |
| 158 | { |
| 159 | l2fib_seq_num_t sn = va_arg (*a, int); |
| 160 | u8 bd_sn, if_sn; |
| 161 | |
| 162 | l2_fib_extract_seq_num (sn, &bd_sn, &if_sn); |
| 163 | |
| 164 | s = format (s, "%3d/%-3d", bd_sn, if_sn); |
| 165 | |
| 166 | return (s); |
| 167 | } |
| 168 | |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 169 | typedef struct l2fib_show_walk_ctx_t_ |
| 170 | { |
| 171 | u8 first_entry; |
| 172 | u8 verbose; |
| 173 | vlib_main_t *vm; |
| 174 | vnet_main_t *vnm; |
| 175 | u32 total_entries; |
| 176 | u32 bd_index; |
| 177 | u8 learn; |
| 178 | u8 add; |
| 179 | u8 now; |
| 180 | } l2fib_show_walk_ctx_t; |
| 181 | |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 182 | static int |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 183 | l2fib_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg) |
| 184 | { |
| 185 | l2fib_show_walk_ctx_t *ctx = arg; |
| 186 | l2_bridge_domain_t *bd_config; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | l2fib_entry_result_t result; |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 188 | l2fib_entry_key_t key; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 190 | if (ctx->verbose && ctx->first_entry) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | { |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 192 | ctx->first_entry = 0; |
| 193 | vlib_cli_output (ctx->vm, |
| 194 | "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s", |
| 195 | "Mac-Address", "BD-Idx", "If-Idx", |
| 196 | "BSN-ISN", "Age(min)", "static", "filter", |
| 197 | "bvi", "Interface-Name"); |
| 198 | } |
| 199 | |
| 200 | key.raw = kvp->key; |
| 201 | result.raw = kvp->value; |
| 202 | ctx->total_entries++; |
| 203 | |
| 204 | if (ctx->verbose && |
| 205 | ((ctx->bd_index >> 31) || (ctx->bd_index == key.fields.bd_index))) |
| 206 | { |
| 207 | u8 *s = NULL; |
| 208 | |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 209 | if (ctx->learn && l2fib_entry_result_is_set_AGE_NOT (&result)) |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 210 | return (BIHASH_WALK_CONTINUE); /* skip provisioned macs */ |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 211 | |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 212 | if (ctx->add && !l2fib_entry_result_is_set_AGE_NOT (&result)) |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 213 | return (BIHASH_WALK_CONTINUE); /* skip learned macs */ |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 214 | |
| 215 | bd_config = vec_elt_at_index (l2input_main.bd_configs, |
| 216 | key.fields.bd_index); |
| 217 | |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 218 | if (l2fib_entry_result_is_set_AGE_NOT (&result)) |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 219 | s = format (s, "no"); |
| 220 | else if (bd_config->mac_age == 0) |
| 221 | s = format (s, "-"); |
| 222 | else |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 223 | { |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 224 | i16 delta = ctx->now - result.fields.timestamp; |
| 225 | delta += delta < 0 ? 256 : 0; |
| 226 | s = format (s, "%d", delta); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 227 | } |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 228 | |
| 229 | vlib_cli_output (ctx->vm, |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 230 | "%=19U%=7d%=7d %U%=9v%=7s%=7s%=5s%=30U", |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 231 | format_ethernet_address, key.fields.mac, |
| 232 | key.fields.bd_index, |
| 233 | result.fields.sw_if_index == ~0 |
| 234 | ? -1 : result.fields.sw_if_index, |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 235 | format_l2_fib_seq_num, result.fields.sn, s, |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 236 | l2fib_entry_result_is_set_STATIC (&result) ? "*" : "-", |
| 237 | l2fib_entry_result_is_set_FILTER (&result) ? "*" : "-", |
| 238 | l2fib_entry_result_is_set_BVI (&result) ? "*" : "-", |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 239 | format_vnet_sw_if_index_name_with_NA, |
| 240 | ctx->vnm, result.fields.sw_if_index); |
| 241 | vec_free (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | } |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 243 | |
| 244 | return (BIHASH_WALK_CONTINUE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 247 | /** Display the contents of the l2fib. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 249 | show_l2fib (vlib_main_t * vm, |
| 250 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 252 | bd_main_t *bdm = &bd_main; |
| 253 | l2fib_main_t *msm = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | u8 raw = 0; |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 255 | u32 bd_id; |
| 256 | l2fib_show_walk_ctx_t ctx = { |
| 257 | .first_entry = 1, |
| 258 | .bd_index = ~0, |
| 259 | .now = (u8) (vlib_time_now (vm) / 60), |
| 260 | .vm = vm, |
| 261 | .vnm = msm->vnet_main, |
| 262 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 264 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 265 | { |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 266 | if (unformat (input, "raw")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 267 | { |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 268 | raw = 1; |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 269 | ctx.verbose = 0; |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | else if (unformat (input, "verbose")) |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 273 | ctx.verbose = 1; |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 274 | else if (unformat (input, "all")) |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 275 | ctx.verbose = 1; |
| 276 | else if (unformat (input, "bd_index %d", &ctx.bd_index)) |
| 277 | ctx.verbose = 1; |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 278 | else if (unformat (input, "learn")) |
| 279 | { |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 280 | ctx.add = 0; |
| 281 | ctx.learn = 1; |
| 282 | ctx.verbose = 1; |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 283 | } |
| 284 | else if (unformat (input, "add")) |
| 285 | { |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 286 | ctx.learn = 0; |
| 287 | ctx.add = 1; |
| 288 | ctx.verbose = 1; |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 289 | } |
| 290 | else if (unformat (input, "bd_id %d", &bd_id)) |
| 291 | { |
| 292 | uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
| 293 | if (p) |
| 294 | { |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 295 | ctx.verbose = 1; |
| 296 | ctx.bd_index = p[0]; |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 297 | } |
| 298 | else |
| 299 | return clib_error_return (0, |
| 300 | "bridge domain id %d doesn't exist\n", |
| 301 | bd_id); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 302 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | else |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 304 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 307 | if (msm->mac_table_initialized == 0) |
| 308 | { |
| 309 | vlib_cli_output (vm, "no l2fib entries"); |
| 310 | return 0; |
| 311 | } |
| 312 | |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 313 | BV (clib_bihash_foreach_key_value_pair) |
| 314 | (&msm->mac_table, l2fib_show_walk_cb, &ctx); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 316 | if (ctx.total_entries == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | vlib_cli_output (vm, "no l2fib entries"); |
| 318 | else |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 319 | { |
| 320 | l2learn_main_t *lm = &l2learn_main; |
| 321 | vlib_cli_output (vm, "L2FIB total/learned entries: %d/%d " |
| 322 | "Last scan time: %.4esec Learn limit: %d ", |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 323 | ctx.total_entries, lm->global_learn_count, |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 324 | msm->age_scan_duration, lm->global_learn_limit); |
| 325 | if (lm->client_pid) |
| 326 | vlib_cli_output (vm, "L2MAC events client PID: %d " |
| 327 | "Last e-scan time: %.4esec Delay: %.2esec " |
| 328 | "Max macs in event: %d", |
| 329 | lm->client_pid, msm->evt_scan_duration, |
| 330 | msm->event_scan_delay, msm->max_macs_in_event); |
| 331 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | |
| 333 | if (raw) |
| 334 | vlib_cli_output (vm, "Raw Hash Table:\n%U\n", |
Neale Ranns | 99a3c6c | 2018-08-07 02:54:31 -0700 | [diff] [blame] | 335 | BV (format_bihash), &msm->mac_table, 1 /* verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 340 | /*? |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 341 | * This command displays the MAC Address entries of the L2 FIB table. |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 342 | * Output can be filtered to just get the number of MAC Addresses or display |
| 343 | * each MAC Address for all bridge domains or just a single bridge domain. |
| 344 | * |
| 345 | * @cliexpar |
| 346 | * Example of how to display the number of MAC Address entries in the L2 |
| 347 | * FIB table: |
| 348 | * @cliexstart{show l2fib} |
| 349 | * 3 l2fib entries |
| 350 | * @cliexend |
| 351 | * Example of how to display all the MAC Address entries in the L2 |
| 352 | * FIB table: |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 353 | * @cliexstart{show l2fib all} |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 354 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 355 | * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0 |
| 356 | * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0 |
| 357 | * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0 |
| 358 | * 3 l2fib entries |
| 359 | * @cliexend |
| 360 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 361 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | VLIB_CLI_COMMAND (show_l2fib_cli, static) = { |
| 363 | .path = "show l2fib", |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 364 | .short_help = "show l2fib [all] | [bd_id <nn> | bd_index <nn>] [learn | add] | [raw]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | .function = show_l2fib, |
| 366 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 367 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 369 | void |
| 370 | l2fib_table_init (void) |
| 371 | { |
| 372 | l2fib_main_t *mp = &l2fib_main; |
| 373 | |
| 374 | if (mp->mac_table_initialized == 1) |
| 375 | return; |
| 376 | |
| 377 | BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table", |
| 378 | mp->mac_table_n_buckets, mp->mac_table_memory_size); |
| 379 | mp->mac_table_initialized = 1; |
| 380 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 382 | /* Remove all entries from the l2fib */ |
| 383 | void |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 384 | l2fib_clear_table (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 386 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 388 | if (mp->mac_table_initialized == 0) |
| 389 | return; |
| 390 | |
Matthew Smith | 3a97a45 | 2020-10-12 15:04:39 -0500 | [diff] [blame] | 391 | mp->mac_table_initialized = 0; |
| 392 | |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 393 | /* Remove all entries */ |
| 394 | BV (clib_bihash_free) (&mp->mac_table); |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 395 | l2fib_table_init (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | l2learn_main.global_learn_count = 0; |
| 397 | } |
| 398 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 399 | /** Clear all entries in L2FIB. |
| 400 | * @TODO: Later we may want a way to remove only the non-static entries |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 401 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 402 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 403 | clear_l2fib (vlib_main_t * vm, |
| 404 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | { |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 406 | l2fib_clear_table (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 410 | /*? |
| 411 | * This command clears all the MAC Address entries from the L2 FIB table. |
| 412 | * |
| 413 | * @cliexpar |
| 414 | * Example of how to clear the L2 FIB Table: |
| 415 | * @cliexcmd{clear l2fib} |
| 416 | * Example to show the L2 FIB Table has been cleared: |
| 417 | * @cliexstart{show l2fib verbose} |
| 418 | * no l2fib entries |
| 419 | * @cliexend |
| 420 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 421 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | VLIB_CLI_COMMAND (clear_l2fib_cli, static) = { |
| 423 | .path = "clear l2fib", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 424 | .short_help = "clear l2fib", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | .function = clear_l2fib, |
| 426 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 427 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 429 | static l2fib_seq_num_t |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 430 | l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index) |
| 431 | { |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 432 | l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index); |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 433 | |
| 434 | return l2_fib_mk_seq_num (bd_config->seq_num, |
| 435 | l2_input_seq_num (sw_if_index)); |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 436 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 437 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 438 | /** |
| 439 | * Add an entry to the l2fib. |
| 440 | * If the entry already exists then overwrite it |
| 441 | */ |
| 442 | void |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 443 | l2fib_add_entry (const u8 * mac, u32 bd_index, |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 444 | u32 sw_if_index, l2fib_entry_result_flags_t flags) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 445 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | l2fib_entry_key_t key; |
| 447 | l2fib_entry_result_t result; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 448 | __attribute__ ((unused)) u32 bucket_contents; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 449 | l2fib_main_t *fm = &l2fib_main; |
| 450 | l2learn_main_t *lm = &l2learn_main; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 451 | BVT (clib_bihash_kv) kv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 453 | if (fm->mac_table_initialized == 0) |
| 454 | l2fib_table_init (); |
| 455 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 456 | /* set up key */ |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 457 | key.raw = l2fib_make_key (mac, bd_index); |
John Lo | b2e73b1 | 2020-01-07 16:35:29 -0500 | [diff] [blame] | 458 | kv.key = key.raw; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 459 | |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 460 | /* check if entry already exist */ |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 461 | if (BV (clib_bihash_search) (&fm->mac_table, &kv, &kv)) |
| 462 | { |
| 463 | /* decrement counter if overwriting a learned mac */ |
| 464 | result.raw = kv.value; |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 465 | if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) |
| 466 | && (lm->global_learn_count)) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 467 | lm->global_learn_count--; |
| 468 | } |
| 469 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 470 | /* set up result */ |
| 471 | result.raw = 0; /* clear all fields */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | result.fields.sw_if_index = sw_if_index; |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 473 | result.fields.flags = flags; |
| 474 | |
| 475 | /* no aging for provisioned entry */ |
| 476 | l2fib_entry_result_set_AGE_NOT (&result); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | kv.value = result.raw; |
| 479 | |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 480 | BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1 /* is_add */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 483 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 484 | * Add an entry to the L2FIB. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 485 | * The CLI format is: |
| 486 | * l2fib add <mac> <bd> <intf> [static] [bvi] |
| 487 | * l2fib add <mac> <bd> filter |
| 488 | * Note that filter and bvi entries are always static |
| 489 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 491 | l2fib_add (vlib_main_t * vm, |
| 492 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 494 | bd_main_t *bdm = &bd_main; |
| 495 | vnet_main_t *vnm = vnet_get_main (); |
| 496 | clib_error_t *error = 0; |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 497 | u8 mac[6]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | u32 bd_id; |
| 499 | u32 bd_index; |
| 500 | u32 sw_if_index = ~0; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 501 | uword *p; |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 502 | l2fib_entry_result_flags_t flags; |
| 503 | |
| 504 | flags = L2FIB_ENTRY_RESULT_FLAG_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 506 | if (!unformat (input, "%U", unformat_ethernet_address, mac)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | { |
| 508 | error = clib_error_return (0, "expected mac address `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 509 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | goto done; |
| 511 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 512 | |
| 513 | if (!unformat (input, "%d", &bd_id)) |
| 514 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 515 | error = clib_error_return (0, "expected bridge domain ID `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 516 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 518 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | |
| 520 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 521 | if (!p) |
| 522 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | error = clib_error_return (0, "bridge domain ID %d invalid", bd_id); |
| 524 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 525 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | bd_index = p[0]; |
| 527 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 528 | if (unformat (input, "filter")) |
| 529 | { |
John Lo | 14edd97 | 2017-10-31 13:26:02 -0400 | [diff] [blame] | 530 | l2fib_add_filter_entry (mac, bd_index); |
| 531 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | |
John Lo | 14edd97 | 2017-10-31 13:26:02 -0400 | [diff] [blame] | 534 | if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 535 | { |
| 536 | error = clib_error_return (0, "unknown interface `%U'", |
| 537 | format_unformat_error, input); |
| 538 | goto done; |
| 539 | } |
| 540 | |
| 541 | if (unformat (input, "static")) |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 542 | flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC; |
John Lo | 14edd97 | 2017-10-31 13:26:02 -0400 | [diff] [blame] | 543 | else if (unformat (input, "bvi")) |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 544 | flags |= (L2FIB_ENTRY_RESULT_FLAG_STATIC | L2FIB_ENTRY_RESULT_FLAG_BVI); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 545 | |
John Lo | b2fd6cb | 2017-07-12 19:56:45 -0400 | [diff] [blame] | 546 | if (vec_len (l2input_main.configs) <= sw_if_index) |
| 547 | { |
| 548 | error = clib_error_return (0, "Interface sw_if_index %d not in L2 mode", |
| 549 | sw_if_index); |
| 550 | goto done; |
| 551 | } |
| 552 | |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 553 | l2fib_add_entry (mac, bd_index, sw_if_index, flags); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 554 | |
| 555 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | return error; |
| 557 | } |
| 558 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 559 | /*? |
| 560 | * This command adds a MAC Address entry to the L2 FIB table |
| 561 | * of an existing bridge-domain. The MAC Address can be static |
| 562 | * or dynamic. This command also allows a filter to be added, |
| 563 | * such that packets with given MAC Addresses (source mac or |
| 564 | * destination mac match) are dropped. |
| 565 | * |
| 566 | * @cliexpar |
| 567 | * Example of how to add a dynamic MAC Address entry to the L2 FIB table |
| 568 | * of a bridge-domain (where 200 is the bridge-domain-id): |
| 569 | * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200} |
| 570 | * Example of how to add a static MAC Address entry to the L2 FIB table |
| 571 | * of a bridge-domain (where 200 is the bridge-domain-id): |
| 572 | * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static} |
| 573 | * Example of how to add a filter such that a packet with the given MAC |
| 574 | * Address will be dropped in a given bridge-domain (where 200 is the |
| 575 | * bridge-domain-id): |
| 576 | * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter} |
| 577 | * Example of show command of the provisioned MAC Addresses and filters: |
| 578 | * @cliexstart{show l2fib verbose} |
| 579 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 580 | * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0 |
| 581 | * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0 |
| 582 | * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0 |
| 583 | * 3 l2fib entries |
| 584 | * @cliexend |
| 585 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 586 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 587 | VLIB_CLI_COMMAND (l2fib_add_cli, static) = { |
| 588 | .path = "l2fib add", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 589 | .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 590 | .function = l2fib_add, |
| 591 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 592 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 593 | |
| 594 | |
| 595 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 596 | l2fib_test_command_fn (vlib_main_t * vm, |
| 597 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | { |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 599 | u8 mac[6], save_mac[6]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 600 | u32 bd_index = 0; |
| 601 | u32 sw_if_index = 8; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | u32 is_add = 0; |
| 603 | u32 is_del = 0; |
| 604 | u32 is_check = 0; |
| 605 | u32 count = 1; |
| 606 | int mac_set = 0; |
| 607 | int i; |
| 608 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 609 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 610 | { |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 611 | if (unformat (input, "mac %U", unformat_ethernet_address, mac)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 612 | mac_set = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | else if (unformat (input, "add")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 614 | is_add = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 615 | else if (unformat (input, "del")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 616 | is_del = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 617 | else if (unformat (input, "check")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 618 | is_check = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 619 | else if (unformat (input, "count %d", &count)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 620 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 621 | else |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 622 | break; |
| 623 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 624 | |
| 625 | if (mac_set == 0) |
| 626 | return clib_error_return (0, "mac not set"); |
| 627 | |
| 628 | if (is_add == 0 && is_del == 0 && is_check == 0) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 629 | return clib_error_return (0, |
| 630 | "noop: pick at least one of (add,del,check)"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 632 | clib_memcpy_fast (save_mac, mac, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | |
| 634 | if (is_add) |
| 635 | { |
| 636 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 637 | { |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 638 | l2fib_add_entry (mac, bd_index, sw_if_index, |
| 639 | L2FIB_ENTRY_RESULT_FLAG_NONE); |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 640 | incr_mac_address (mac); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 641 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | if (is_check) |
| 645 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 646 | BVT (clib_bihash_kv) kv; |
| 647 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 649 | if (mp->mac_table_initialized == 0) |
| 650 | return clib_error_return (0, "mac table is not initialized"); |
| 651 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 652 | clib_memcpy_fast (mac, save_mac, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 653 | |
| 654 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 655 | { |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 656 | kv.key = l2fib_make_key (mac, bd_index); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 657 | if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv)) |
| 658 | { |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 659 | clib_warning ("key %U AWOL", format_ethernet_address, mac); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 660 | break; |
| 661 | } |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 662 | incr_mac_address (mac); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 663 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | if (is_del) |
| 667 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 668 | clib_memcpy_fast (mac, save_mac, 6); |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 669 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 670 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 671 | { |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 672 | l2fib_del_entry (mac, bd_index, 0); |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 673 | incr_mac_address (mac); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 674 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 677 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 680 | /*? |
| 681 | * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default |
| 682 | * bridge domain (bridge-domain-id of 0) to be modified. |
| 683 | * |
| 684 | * @cliexpar |
| 685 | * @parblock |
| 686 | * Example of how to add a set of 4 sequential MAC Address entries to L2 |
| 687 | * FIB table of the default bridge-domain: |
| 688 | * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4} |
| 689 | * |
| 690 | * Show the set of 4 sequential MAC Address entries that were added: |
| 691 | * @cliexstart{show l2fib verbose} |
| 692 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 693 | * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 694 | * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 695 | * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 696 | * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 697 | * 4 l2fib entries |
| 698 | * @cliexend |
| 699 | * |
| 700 | * Example of how to check that the set of 4 sequential MAC Address |
| 701 | * entries were added to L2 FIB table of the default |
| 702 | * bridge-domain. Used a count of 5 to produce an error: |
| 703 | * |
| 704 | * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5} |
| 705 | * The output of the check command is in the log files. Log file |
| 706 | * location may vary based on your OS and Version: |
| 707 | * |
| 708 | * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b> |
| 709 | * |
| 710 | * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL |
| 711 | * |
| 712 | * Example of how to delete a set of 4 sequential MAC Address entries |
| 713 | * from L2 FIB table of the default bridge-domain: |
| 714 | * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4} |
| 715 | * @endparblock |
| 716 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 717 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 718 | VLIB_CLI_COMMAND (l2fib_test_command, static) = { |
| 719 | .path = "test l2fib", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 720 | .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 721 | .function = l2fib_test_command_fn, |
| 722 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 723 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 724 | |
| 725 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 726 | /** |
| 727 | * Delete an entry from the l2fib. |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 728 | * Return 0 if the entry was deleted, or 1 it was not found or if |
| 729 | * sw_if_index is non-zero and does not match that in the entry. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 730 | */ |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 731 | u32 |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 732 | l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 733 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 734 | l2fib_entry_result_t result; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 735 | l2fib_main_t *mp = &l2fib_main; |
| 736 | BVT (clib_bihash_kv) kv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 737 | |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 738 | if (mp->mac_table_initialized == 0) |
| 739 | return 1; |
| 740 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 741 | /* set up key */ |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 742 | kv.key = l2fib_make_key (mac, bd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 743 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 744 | if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 745 | return 1; |
| 746 | |
| 747 | result.raw = kv.value; |
| 748 | |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 749 | /* check if sw_if_index of entry match */ |
| 750 | if ((sw_if_index != 0) && (sw_if_index != result.fields.sw_if_index)) |
| 751 | return 1; |
| 752 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 753 | /* decrement counter if dynamically learned mac */ |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 754 | if ((!l2fib_entry_result_is_set_AGE_NOT (&result)) && |
| 755 | (l2learn_main.global_learn_count)) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 756 | l2learn_main.global_learn_count--; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 757 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 758 | /* Remove entry from hash table */ |
| 759 | BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 763 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 764 | * Delete an entry from the L2FIB. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 765 | * The CLI format is: |
| 766 | * l2fib del <mac> <bd-id> |
| 767 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 768 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 769 | l2fib_del (vlib_main_t * vm, |
| 770 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 771 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 772 | bd_main_t *bdm = &bd_main; |
| 773 | clib_error_t *error = 0; |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 774 | u8 mac[6]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 775 | u32 bd_id; |
| 776 | u32 bd_index; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 777 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | |
Mohsin Kazmi | 57938f6 | 2017-10-27 21:28:07 +0200 | [diff] [blame] | 779 | if (!unformat (input, "%U", unformat_ethernet_address, mac)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 780 | { |
| 781 | error = clib_error_return (0, "expected mac address `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 782 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 783 | goto done; |
| 784 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 785 | |
| 786 | if (!unformat (input, "%d", &bd_id)) |
| 787 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 788 | error = clib_error_return (0, "expected bridge domain ID `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 789 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 790 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 791 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 792 | |
| 793 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 794 | if (!p) |
| 795 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 796 | error = clib_error_return (0, "bridge domain ID %d invalid", bd_id); |
| 797 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 798 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | bd_index = p[0]; |
| 800 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 801 | /* Delete the entry */ |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 802 | if (l2fib_del_entry (mac, bd_index, 0)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 803 | { |
| 804 | error = clib_error_return (0, "mac entry not found"); |
| 805 | goto done; |
| 806 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 807 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 808 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 809 | return error; |
| 810 | } |
| 811 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 812 | /*? |
| 813 | * This command deletes an existing MAC Address entry from the L2 FIB |
| 814 | * table of an existing bridge-domain. |
| 815 | * |
| 816 | * @cliexpar |
| 817 | * 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): |
| 818 | * @cliexcmd{l2fib del 52:54:00:53:18:33 200} |
| 819 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 820 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 821 | VLIB_CLI_COMMAND (l2fib_del_cli, static) = { |
| 822 | .path = "l2fib del", |
John Lo | 7dbd726 | 2018-05-31 10:25:18 -0400 | [diff] [blame] | 823 | .short_help = "l2fib del <mac> <bridge-domain-id> []", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 824 | .function = l2fib_del, |
| 825 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 826 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 827 | |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 828 | /** |
| 829 | Kick off ager to scan MACs to age/delete MAC entries |
| 830 | */ |
| 831 | void |
| 832 | l2fib_start_ager_scan (vlib_main_t * vm) |
| 833 | { |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 834 | uword evt = L2_MAC_AGE_PROCESS_EVENT_ONE_PASS; |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 835 | |
| 836 | /* check if there is at least one bd with mac aging enabled */ |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 837 | l2_bridge_domain_t *bd_config; |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 838 | vec_foreach (bd_config, l2input_main.bd_configs) |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 839 | { |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 840 | if (bd_config->bd_id != ~0 && bd_config->mac_age != 0) |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 841 | { |
| 842 | evt = L2_MAC_AGE_PROCESS_EVENT_START; |
| 843 | break; |
| 844 | } |
| 845 | } |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 846 | |
| 847 | vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index, |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 848 | evt, 0); |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /** |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 852 | Flush all non static MACs from an interface |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 853 | */ |
| 854 | void |
| 855 | l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index) |
| 856 | { |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 857 | l2_input_seq_num_inc (sw_if_index); |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 858 | l2fib_start_ager_scan (vm); |
| 859 | } |
| 860 | |
| 861 | /** |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 862 | Flush all non static MACs in a bridge domain |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 863 | */ |
| 864 | void |
| 865 | l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index) |
| 866 | { |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 867 | l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index); |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 868 | bd_config->seq_num += 1; |
| 869 | l2fib_start_ager_scan (vm); |
| 870 | } |
| 871 | |
| 872 | /** |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 873 | Flush all non static MACs - flushes all valid BDs |
| 874 | */ |
| 875 | void |
| 876 | l2fib_flush_all_mac (vlib_main_t * vm) |
| 877 | { |
| 878 | l2_bridge_domain_t *bd_config; |
| 879 | vec_foreach (bd_config, l2input_main.bd_configs) |
| 880 | if (bd_is_valid (bd_config)) |
| 881 | bd_config->seq_num += 1; |
| 882 | |
| 883 | l2fib_start_ager_scan (vm); |
| 884 | } |
| 885 | |
| 886 | |
| 887 | /** |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 888 | Flush MACs, except static ones, associated with an interface |
| 889 | The CLI format is: |
| 890 | l2fib flush-mac interface <if-name> |
| 891 | */ |
| 892 | static clib_error_t * |
| 893 | l2fib_flush_mac_int (vlib_main_t * vm, |
| 894 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 895 | { |
| 896 | vnet_main_t *vnm = vnet_get_main (); |
| 897 | clib_error_t *error = 0; |
| 898 | u32 sw_if_index; |
| 899 | |
| 900 | if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 901 | { |
| 902 | error = clib_error_return (0, "unknown interface `%U'", |
| 903 | format_unformat_error, input); |
| 904 | goto done; |
| 905 | } |
| 906 | |
| 907 | l2fib_flush_int_mac (vm, sw_if_index); |
| 908 | |
| 909 | done: |
| 910 | return error; |
| 911 | } |
| 912 | |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 913 | /** |
| 914 | Flush all MACs, except static ones |
| 915 | The CLI format is: |
| 916 | l2fib flush-mac all |
| 917 | */ |
| 918 | static clib_error_t * |
| 919 | l2fib_flush_mac_all (vlib_main_t * vm, |
| 920 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 921 | { |
| 922 | l2fib_flush_all_mac (vm); |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | /*? |
| 927 | * This command kick off ager to delete all existing MAC Address entries, |
| 928 | * except static ones, associated with an interface from the L2 FIB table. |
| 929 | * |
| 930 | * @cliexpar |
| 931 | * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table: |
| 932 | * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0} |
| 933 | ?*/ |
| 934 | /* *INDENT-OFF* */ |
| 935 | VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = { |
| 936 | .path = "l2fib flush-mac all", |
| 937 | .short_help = "l2fib flush-mac all", |
| 938 | .function = l2fib_flush_mac_all, |
| 939 | }; |
| 940 | /* *INDENT-ON* */ |
| 941 | |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 942 | /*? |
| 943 | * This command kick off ager to delete all existing MAC Address entries, |
| 944 | * except static ones, associated with an interface from the L2 FIB table. |
| 945 | * |
| 946 | * @cliexpar |
| 947 | * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table: |
| 948 | * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0} |
| 949 | ?*/ |
| 950 | /* *INDENT-OFF* */ |
| 951 | VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = { |
| 952 | .path = "l2fib flush-mac interface", |
| 953 | .short_help = "l2fib flush-mac interface <if-name>", |
| 954 | .function = l2fib_flush_mac_int, |
| 955 | }; |
| 956 | /* *INDENT-ON* */ |
| 957 | |
| 958 | /** |
| 959 | Flush bridge-domain MACs except static ones. |
| 960 | The CLI format is: |
| 961 | l2fib flush-mac bridge-domain <bd-id> |
| 962 | */ |
| 963 | static clib_error_t * |
| 964 | l2fib_flush_mac_bd (vlib_main_t * vm, |
| 965 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 966 | { |
| 967 | bd_main_t *bdm = &bd_main; |
| 968 | clib_error_t *error = 0; |
| 969 | u32 bd_index, bd_id; |
| 970 | uword *p; |
| 971 | |
| 972 | if (!unformat (input, "%d", &bd_id)) |
| 973 | { |
| 974 | error = clib_error_return (0, "expecting bridge-domain id but got `%U'", |
| 975 | format_unformat_error, input); |
| 976 | goto done; |
| 977 | } |
| 978 | |
| 979 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
| 980 | if (p) |
| 981 | bd_index = *p; |
| 982 | else |
| 983 | return clib_error_return (0, "No such bridge domain %d", bd_id); |
| 984 | |
| 985 | l2fib_flush_bd_mac (vm, bd_index); |
| 986 | |
| 987 | done: |
| 988 | return error; |
| 989 | } |
| 990 | |
| 991 | /*? |
| 992 | * This command kick off ager to delete all existing MAC Address entries, |
| 993 | * except static ones, in a bridge domain from the L2 FIB table. |
| 994 | * |
| 995 | * @cliexpar |
| 996 | * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table: |
| 997 | * @cliexcmd{l2fib flush-mac bridge-domain 1000} |
| 998 | ?*/ |
| 999 | /* *INDENT-OFF* */ |
| 1000 | VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = { |
| 1001 | .path = "l2fib flush-mac bridge-domain", |
| 1002 | .short_help = "l2fib flush-mac bridge-domain <bd-id>", |
| 1003 | .function = l2fib_flush_mac_bd, |
| 1004 | }; |
| 1005 | /* *INDENT-ON* */ |
| 1006 | |
Eyal Bari | afc47aa | 2017-04-20 14:45:17 +0300 | [diff] [blame] | 1007 | clib_error_t * |
| 1008 | l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags) |
| 1009 | { |
| 1010 | l2_input_config_t *config = l2input_intf_config (sw_if_index); |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 1011 | if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && |
| 1012 | l2_input_is_bridge (config)) |
Eyal Bari | afc47aa | 2017-04-20 14:45:17 +0300 | [diff] [blame] | 1013 | l2fib_flush_int_mac (vnm->vlib_main, sw_if_index); |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1018 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1019 | BVT (clib_bihash) * get_mac_table (void) |
| 1020 | { |
| 1021 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1022 | return &mp->mac_table; |
| 1023 | } |
| 1024 | |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1025 | static_always_inline void * |
| 1026 | allocate_mac_evt_buf (u32 client, u32 client_index) |
| 1027 | { |
| 1028 | l2fib_main_t *fm = &l2fib_main; |
| 1029 | vl_api_l2_macs_event_t *mp = vl_msg_api_alloc |
| 1030 | (sizeof (*mp) + (fm->max_macs_in_event * sizeof (vl_api_mac_entry_t))); |
| 1031 | mp->_vl_msg_id = htons (VL_API_L2_MACS_EVENT); |
| 1032 | mp->pid = htonl (client); |
| 1033 | mp->client_index = client_index; |
| 1034 | return mp; |
| 1035 | } |
| 1036 | |
| 1037 | static_always_inline f64 |
| 1038 | l2fib_scan (vlib_main_t * vm, f64 start_time, u8 event_only) |
| 1039 | { |
| 1040 | l2fib_main_t *fm = &l2fib_main; |
| 1041 | l2learn_main_t *lm = &l2learn_main; |
| 1042 | |
| 1043 | BVT (clib_bihash) * h = &fm->mac_table; |
| 1044 | int i, j, k; |
| 1045 | f64 last_start = start_time; |
| 1046 | f64 accum_t = 0; |
| 1047 | f64 delta_t = 0; |
| 1048 | u32 evt_idx = 0; |
| 1049 | u32 learn_count = 0; |
| 1050 | u32 client = lm->client_pid; |
| 1051 | u32 cl_idx = lm->client_index; |
| 1052 | vl_api_l2_macs_event_t *mp = 0; |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 1053 | vl_api_registration_t *reg = 0; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1054 | |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 1055 | /* Don't scan the l2 fib if it hasn't been instantiated yet */ |
| 1056 | if (alloc_arena (h) == 0) |
| 1057 | return 0.0; |
| 1058 | |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1059 | if (client) |
| 1060 | { |
| 1061 | mp = allocate_mac_evt_buf (client, cl_idx); |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 1062 | reg = vl_api_client_index_to_registration (lm->client_index); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | for (i = 0; i < h->nbuckets; i++) |
| 1066 | { |
| 1067 | /* allow no more than 20us without a pause */ |
| 1068 | delta_t = vlib_time_now (vm) - last_start; |
| 1069 | if (delta_t > 20e-6) |
| 1070 | { |
| 1071 | vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */ |
| 1072 | last_start = vlib_time_now (vm); |
| 1073 | accum_t += delta_t; |
| 1074 | } |
| 1075 | |
| 1076 | if (i < (h->nbuckets - 3)) |
| 1077 | { |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1078 | BVT (clib_bihash_bucket) * b = |
| 1079 | BV (clib_bihash_get_bucket) (h, i + 3); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1080 | CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD); |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1081 | b = BV (clib_bihash_get_bucket) (h, i + 1); |
| 1082 | if (!BV (clib_bihash_bucket_is_empty) (b)) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1083 | { |
| 1084 | BVT (clib_bihash_value) * v = |
| 1085 | BV (clib_bihash_get_value) (h, b->offset); |
| 1086 | CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD); |
| 1087 | } |
| 1088 | } |
| 1089 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1090 | BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i); |
| 1091 | if (BV (clib_bihash_bucket_is_empty) (b)) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1092 | continue; |
| 1093 | BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset); |
| 1094 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 1095 | { |
| 1096 | for (k = 0; k < BIHASH_KVP_PER_PAGE; k++) |
| 1097 | { |
| 1098 | if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL) |
| 1099 | continue; |
| 1100 | |
| 1101 | l2fib_entry_key_t key = {.raw = v->kvp[k].key }; |
| 1102 | l2fib_entry_result_t result = {.raw = v->kvp[k].value }; |
| 1103 | |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 1104 | if (!l2fib_entry_result_is_set_AGE_NOT (&result)) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1105 | learn_count++; |
| 1106 | |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1107 | if (client) |
| 1108 | { |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1109 | if (PREDICT_FALSE (evt_idx >= fm->max_macs_in_event)) |
| 1110 | { |
| 1111 | /* event message full, send it and start a new one */ |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 1112 | if (reg && vl_api_can_send_msg (reg)) |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1113 | { |
| 1114 | mp->n_macs = htonl (evt_idx); |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 1115 | vl_api_send_msg (reg, (u8 *) mp); |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1116 | mp = allocate_mac_evt_buf (client, cl_idx); |
| 1117 | } |
| 1118 | else |
| 1119 | { |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 1120 | if (reg) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1121 | clib_warning ("MAC event to pid %d queue stuffed!" |
| 1122 | " %d MAC entries lost", client, |
| 1123 | evt_idx); |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1124 | } |
| 1125 | evt_idx = 0; |
| 1126 | } |
| 1127 | |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 1128 | if (l2fib_entry_result_is_set_LRN_EVT (&result)) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1129 | { |
| 1130 | /* copy mac entry to event msg */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1131 | clib_memcpy_fast (mp->mac[evt_idx].mac_addr, |
| 1132 | key.fields.mac, 6); |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 1133 | mp->mac[evt_idx].action = |
| 1134 | l2fib_entry_result_is_set_LRN_MOV (&result) ? |
Jakub Grajciar | 145e330 | 2019-10-24 13:52:42 +0200 | [diff] [blame] | 1135 | (vl_api_mac_event_action_t) MAC_EVENT_ACTION_MOVE |
| 1136 | : (vl_api_mac_event_action_t) MAC_EVENT_ACTION_ADD; |
| 1137 | mp->mac[evt_idx].action = |
| 1138 | htonl (mp->mac[evt_idx].action); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1139 | mp->mac[evt_idx].sw_if_index = |
| 1140 | htonl (result.fields.sw_if_index); |
John Lo | e23c99e | 2018-03-13 21:53:18 -0400 | [diff] [blame] | 1141 | /* clear event bits and update mac entry */ |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 1142 | l2fib_entry_result_clear_LRN_EVT (&result); |
| 1143 | l2fib_entry_result_clear_LRN_MOV (&result); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1144 | BVT (clib_bihash_kv) kv; |
| 1145 | kv.key = key.raw; |
| 1146 | kv.value = result.raw; |
| 1147 | BV (clib_bihash_add_del) (&fm->mac_table, &kv, 1); |
| 1148 | evt_idx++; |
| 1149 | continue; /* skip aging */ |
| 1150 | } |
| 1151 | } |
| 1152 | |
Neale Ranns | b54d081 | 2018-09-06 06:22:56 -0700 | [diff] [blame] | 1153 | if (event_only || l2fib_entry_result_is_set_AGE_NOT (&result)) |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 1154 | continue; /* skip aging - static_mac always age_not */ |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1155 | |
| 1156 | /* start aging processing */ |
| 1157 | u32 bd_index = key.fields.bd_index; |
| 1158 | u32 sw_if_index = result.fields.sw_if_index; |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 1159 | u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index); |
| 1160 | if (result.fields.sn != sn) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1161 | goto age_out; /* stale mac */ |
| 1162 | |
| 1163 | l2_bridge_domain_t *bd_config = |
| 1164 | vec_elt_at_index (l2input_main.bd_configs, bd_index); |
| 1165 | |
| 1166 | if (bd_config->mac_age == 0) |
| 1167 | continue; /* skip aging */ |
| 1168 | |
| 1169 | i16 delta = (u8) (start_time / 60) - result.fields.timestamp; |
| 1170 | delta += delta < 0 ? 256 : 0; |
| 1171 | |
| 1172 | if (delta < bd_config->mac_age) |
| 1173 | continue; /* still valid */ |
| 1174 | |
| 1175 | age_out: |
| 1176 | if (client) |
| 1177 | { |
| 1178 | /* copy mac entry to event msg */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1179 | clib_memcpy_fast (mp->mac[evt_idx].mac_addr, key.fields.mac, |
| 1180 | 6); |
Jakub Grajciar | 145e330 | 2019-10-24 13:52:42 +0200 | [diff] [blame] | 1181 | mp->mac[evt_idx].action = |
| 1182 | (vl_api_mac_event_action_t) MAC_EVENT_ACTION_DELETE; |
| 1183 | mp->mac[evt_idx].action = htonl (mp->mac[evt_idx].action); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1184 | mp->mac[evt_idx].sw_if_index = |
| 1185 | htonl (result.fields.sw_if_index); |
| 1186 | evt_idx++; |
| 1187 | } |
| 1188 | /* delete mac entry */ |
| 1189 | BVT (clib_bihash_kv) kv; |
| 1190 | kv.key = key.raw; |
| 1191 | BV (clib_bihash_add_del) (&fm->mac_table, &kv, 0); |
| 1192 | learn_count--; |
Dave Barach | 28374ca | 2018-08-07 12:46:18 -0400 | [diff] [blame] | 1193 | /* |
| 1194 | * Note: we may have just freed the bucket's backing |
| 1195 | * storage, so check right here... |
| 1196 | */ |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1197 | if (BV (clib_bihash_bucket_is_empty) (b)) |
Dave Barach | 28374ca | 2018-08-07 12:46:18 -0400 | [diff] [blame] | 1198 | goto doublebreak; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1199 | } |
| 1200 | v++; |
| 1201 | } |
Dave Barach | 28374ca | 2018-08-07 12:46:18 -0400 | [diff] [blame] | 1202 | doublebreak: |
| 1203 | ; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | /* keep learn count consistent */ |
| 1207 | l2learn_main.global_learn_count = learn_count; |
| 1208 | |
| 1209 | if (mp) |
| 1210 | { |
| 1211 | /* send any outstanding mac event message else free message buffer */ |
| 1212 | if (evt_idx) |
| 1213 | { |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 1214 | if (reg && vl_api_can_send_msg (reg)) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1215 | { |
| 1216 | mp->n_macs = htonl (evt_idx); |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 1217 | vl_api_send_msg (reg, (u8 *) mp); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1218 | } |
| 1219 | else |
| 1220 | { |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 1221 | if (reg) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1222 | clib_warning ("MAC event to pid %d queue stuffed!" |
| 1223 | " %d MAC entries lost", client, evt_idx); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1224 | vl_msg_api_free (mp); |
| 1225 | } |
| 1226 | } |
| 1227 | else |
| 1228 | vl_msg_api_free (mp); |
| 1229 | } |
| 1230 | return delta_t + accum_t; |
| 1231 | } |
| 1232 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1233 | static uword |
| 1234 | l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 1235 | vlib_frame_t * f) |
| 1236 | { |
| 1237 | uword event_type, *event_data = 0; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1238 | l2fib_main_t *fm = &l2fib_main; |
| 1239 | l2learn_main_t *lm = &l2learn_main; |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1240 | bool enabled = 0; |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 1241 | f64 start_time, next_age_scan_time = CLIB_TIME_MAX; |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1242 | |
| 1243 | while (1) |
| 1244 | { |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1245 | if (lm->client_pid) |
| 1246 | vlib_process_wait_for_event_or_clock (vm, fm->event_scan_delay); |
| 1247 | else if (enabled) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1248 | { |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1249 | f64 t = next_age_scan_time - vlib_time_now (vm); |
| 1250 | vlib_process_wait_for_event_or_clock (vm, t); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1251 | } |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1252 | else |
| 1253 | vlib_process_wait_for_event (vm); |
| 1254 | |
| 1255 | event_type = vlib_process_get_events (vm, &event_data); |
| 1256 | vec_reset_length (event_data); |
| 1257 | |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1258 | start_time = vlib_time_now (vm); |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1259 | enum |
| 1260 | { SCAN_MAC_AGE, SCAN_MAC_EVENT, SCAN_DISABLE } scan = SCAN_MAC_AGE; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1261 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1262 | switch (event_type) |
| 1263 | { |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1264 | case ~0: /* timer expired */ |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1265 | if (lm->client_pid != 0 && start_time < next_age_scan_time) |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1266 | scan = SCAN_MAC_EVENT; |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1267 | break; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1268 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1269 | case L2_MAC_AGE_PROCESS_EVENT_START: |
| 1270 | enabled = 1; |
| 1271 | break; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1272 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1273 | case L2_MAC_AGE_PROCESS_EVENT_STOP: |
| 1274 | enabled = 0; |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1275 | scan = SCAN_DISABLE; |
| 1276 | break; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1277 | |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 1278 | case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS: |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 1279 | break; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1280 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1281 | default: |
| 1282 | ASSERT (0); |
| 1283 | } |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 1284 | |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 1285 | if (scan == SCAN_MAC_EVENT) |
| 1286 | l2fib_main.evt_scan_duration = l2fib_scan (vm, start_time, 1); |
| 1287 | else |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1288 | { |
| 1289 | if (scan == SCAN_MAC_AGE) |
| 1290 | l2fib_main.age_scan_duration = l2fib_scan (vm, start_time, 0); |
| 1291 | if (scan == SCAN_DISABLE) |
| 1292 | { |
| 1293 | l2fib_main.age_scan_duration = 0; |
| 1294 | l2fib_main.evt_scan_duration = 0; |
| 1295 | } |
| 1296 | /* schedule next scan */ |
| 1297 | if (enabled) |
| 1298 | next_age_scan_time = start_time + L2FIB_AGE_SCAN_INTERVAL; |
| 1299 | else |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 1300 | next_age_scan_time = CLIB_TIME_MAX; |
Eyal Bari | 24db0ec | 2017-09-27 21:43:51 +0300 | [diff] [blame] | 1301 | } |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 1302 | } |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | /* *INDENT-OFF* */ |
| 1307 | VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = { |
| 1308 | .function = l2fib_mac_age_scanner_process, |
| 1309 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1310 | .name = "l2fib-mac-age-scanner-process", |
| 1311 | }; |
| 1312 | /* *INDENT-ON* */ |
| 1313 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1314 | clib_error_t * |
| 1315 | l2fib_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1316 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1317 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1318 | l2fib_entry_key_t test_key; |
| 1319 | u8 test_mac[6]; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1320 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1321 | mp->vlib_main = vm; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1322 | mp->vnet_main = vnet_get_main (); |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 1323 | if (mp->mac_table_n_buckets == 0) |
| 1324 | mp->mac_table_n_buckets = L2FIB_NUM_BUCKETS; |
| 1325 | if (mp->mac_table_memory_size == 0) |
| 1326 | mp->mac_table_memory_size = L2FIB_MEMORY_SIZE; |
| 1327 | mp->mac_table_initialized = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1328 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1329 | /* verify the key constructor is good, since it is endian-sensitive */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1330 | clib_memset (test_mac, 0, sizeof (test_mac)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1331 | test_mac[0] = 0x11; |
| 1332 | test_key.raw = 0; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1333 | test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1334 | ASSERT (test_key.fields.mac[0] == 0x11); |
| 1335 | ASSERT (test_key.fields.bd_index == 0x1234); |
| 1336 | |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | VLIB_INIT_FUNCTION (l2fib_init); |
| 1341 | |
Damjan Marion | 9514781 | 2020-09-14 12:18:44 +0200 | [diff] [blame] | 1342 | static clib_error_t * |
| 1343 | lfib_config (vlib_main_t * vm, unformat_input_t * input) |
| 1344 | { |
| 1345 | l2fib_main_t *lm = &l2fib_main; |
| 1346 | uword table_size = ~0; |
| 1347 | u32 n_buckets = ~0; |
| 1348 | |
| 1349 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1350 | { |
| 1351 | if (unformat (input, "table-size %U", unformat_memory_size, |
| 1352 | &table_size)) |
| 1353 | ; |
| 1354 | else if (unformat (input, "num-buckets %u", &n_buckets)) |
| 1355 | ; |
| 1356 | else |
| 1357 | return clib_error_return (0, "unknown input `%U'", |
| 1358 | format_unformat_error, input); |
| 1359 | } |
| 1360 | |
| 1361 | if (n_buckets != ~0) |
| 1362 | { |
| 1363 | if (!is_pow2 (n_buckets)) |
| 1364 | return clib_error_return (0, "num-buckets must be power of 2"); |
| 1365 | lm->mac_table_n_buckets = n_buckets; |
| 1366 | } |
| 1367 | |
| 1368 | if (table_size != ~0) |
| 1369 | lm->mac_table_memory_size = table_size; |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | VLIB_CONFIG_FUNCTION (lfib_config, "l2fib"); |
| 1374 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1375 | /* |
| 1376 | * fd.io coding-style-patch-verification: ON |
| 1377 | * |
| 1378 | * Local Variables: |
| 1379 | * eval: (c-set-style "gnu") |
| 1380 | * End: |
| 1381 | */ |