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