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 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 34 | /** |
| 35 | * @file |
| 36 | * @brief Ethernet MAC Address FIB Table Management. |
| 37 | * |
| 38 | * The MAC Address forwarding table for bridge-domains is called the l2fib. |
| 39 | * Entries are added automatically as part of mac learning, but MAC Addresses |
| 40 | * entries can also be added manually. |
| 41 | * |
| 42 | */ |
| 43 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 44 | typedef struct |
| 45 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
| 47 | /* hash table */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 48 | BVT (clib_bihash) mac_table; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
| 50 | /* convenience variables */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 51 | vlib_main_t *vlib_main; |
| 52 | vnet_main_t *vnet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | } l2fib_main_t; |
| 54 | |
| 55 | l2fib_main_t l2fib_main; |
| 56 | |
| 57 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 58 | /** Format sw_if_index. If the value is ~0, use the text "N/A" */ |
| 59 | u8 * |
| 60 | 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] | 61 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 62 | vnet_main_t *vnm = va_arg (*args, vnet_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | u32 sw_if_index = va_arg (*args, u32); |
| 64 | if (sw_if_index == ~0) |
| 65 | return format (s, "N/A"); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 66 | else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | return format (s, "%U", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 68 | format_vnet_sw_interface_name, vnm, |
| 69 | vnet_get_sw_interface (vnm, sw_if_index)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 72 | void |
| 73 | l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key, |
| 74 | l2fib_entry_result_t ** l2fe_res) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 76 | l2fib_main_t *msm = &l2fib_main; |
| 77 | BVT (clib_bihash) * h = &msm->mac_table; |
| 78 | clib_bihash_bucket_t *b; |
| 79 | BVT (clib_bihash_value) * v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | l2fib_entry_key_t key; |
| 81 | l2fib_entry_result_t result; |
| 82 | int i, j, k; |
| 83 | |
| 84 | for (i = 0; i < h->nbuckets; i++) |
| 85 | { |
| 86 | b = &h->buckets[i]; |
| 87 | if (b->offset == 0) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 88 | continue; |
| 89 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 90 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 91 | { |
| 92 | for (k = 0; k < BIHASH_KVP_PER_PAGE; k++) |
| 93 | { |
| 94 | if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL) |
| 95 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 97 | key.raw = v->kvp[k].key; |
| 98 | result.raw = v->kvp[k].value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 100 | if ((bd_index == ~0) || (bd_index == key.fields.bd_index)) |
| 101 | { |
| 102 | vec_add1 (*l2fe_key, key); |
| 103 | vec_add1 (*l2fe_res, result); |
| 104 | } |
| 105 | } |
| 106 | v++; |
| 107 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 111 | /** Display the contents of the l2fib. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 113 | show_l2fib (vlib_main_t * vm, |
| 114 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 116 | bd_main_t *bdm = &bd_main; |
| 117 | l2fib_main_t *msm = &l2fib_main; |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 118 | l2_bridge_domain_t *bd_config; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 119 | BVT (clib_bihash) * h = &msm->mac_table; |
| 120 | clib_bihash_bucket_t *b; |
| 121 | BVT (clib_bihash_value) * v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | l2fib_entry_key_t key; |
| 123 | l2fib_entry_result_t result; |
| 124 | u32 first_entry = 1; |
| 125 | u64 total_entries = 0; |
| 126 | int i, j, k; |
| 127 | u8 verbose = 0; |
| 128 | u8 raw = 0; |
| 129 | u32 bd_id, bd_index = ~0; |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 130 | u8 now = (u8) (vlib_time_now (vm) / 60); |
| 131 | u8 *s = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
| 133 | if (unformat (input, "raw")) |
| 134 | raw = 1; |
| 135 | else if (unformat (input, "verbose")) |
| 136 | verbose = 1; |
| 137 | else if (unformat (input, "bd_index %d", &bd_index)) |
| 138 | verbose = 1; |
| 139 | else if (unformat (input, "bd_id %d", &bd_id)) |
| 140 | { |
| 141 | uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 142 | if (p) |
| 143 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | verbose = 1; |
| 145 | bd_index = p[0]; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 146 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | else |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 148 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | vlib_cli_output (vm, "no such bridge domain id"); |
| 150 | return 0; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 151 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | for (i = 0; i < h->nbuckets; i++) |
| 155 | { |
| 156 | b = &h->buckets[i]; |
| 157 | if (b->offset == 0) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 158 | continue; |
| 159 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 160 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 161 | { |
| 162 | for (k = 0; k < BIHASH_KVP_PER_PAGE; k++) |
| 163 | { |
| 164 | if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL) |
| 165 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 167 | if (verbose && first_entry) |
| 168 | { |
| 169 | first_entry = 0; |
| 170 | vlib_cli_output (vm, |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 171 | "%=19s%=7s%=30s%=7s%=8s%=8s%=5s%=16s", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 172 | "Mac Address", "BD Idx", "Interface", |
| 173 | "Index", "static", "filter", "bvi", |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 174 | "Mac Age (min)"); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 175 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 177 | key.raw = v->kvp[k].key; |
| 178 | result.raw = v->kvp[k].value; |
| 179 | |
| 180 | if (verbose |
| 181 | & ((bd_index >> 31) || (bd_index == key.fields.bd_index))) |
| 182 | { |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 183 | bd_config = vec_elt_at_index (l2input_main.bd_configs, |
| 184 | key.fields.bd_index); |
| 185 | |
| 186 | if (bd_config->mac_age) |
| 187 | { |
| 188 | i16 delta = now - result.fields.timestamp; |
| 189 | delta += delta < 0 ? 256 : 0; |
| 190 | s = format (s, "%d", delta); |
| 191 | } |
| 192 | else |
| 193 | s = format (s, "disabled"); |
| 194 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 195 | vlib_cli_output (vm, |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 196 | "%=19U%=7d%=30U%=7d%=8d%=8d%=5d%=16v", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 197 | format_ethernet_address, key.fields.mac, |
| 198 | key.fields.bd_index, |
| 199 | format_vnet_sw_if_index_name_with_NA, |
| 200 | msm->vnet_main, result.fields.sw_if_index, |
| 201 | result.fields.sw_if_index == ~0 |
| 202 | ? -1 : result.fields.sw_if_index, |
| 203 | result.fields.static_mac, |
| 204 | result.fields.filter, |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 205 | result.fields.bvi, s); |
| 206 | vec_reset_length (s); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 207 | } |
| 208 | total_entries++; |
| 209 | } |
| 210 | v++; |
| 211 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (total_entries == 0) |
| 215 | vlib_cli_output (vm, "no l2fib entries"); |
| 216 | else |
| 217 | vlib_cli_output (vm, "%lld l2fib entries", total_entries); |
| 218 | |
| 219 | if (raw) |
| 220 | vlib_cli_output (vm, "Raw Hash Table:\n%U\n", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 221 | BV (format_bihash), h, 1 /* verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 223 | vec_free (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 227 | /*? |
| 228 | * This command dispays the MAC Address entries of the L2 FIB table. |
| 229 | * Output can be filtered to just get the number of MAC Addresses or display |
| 230 | * each MAC Address for all bridge domains or just a single bridge domain. |
| 231 | * |
| 232 | * @cliexpar |
| 233 | * Example of how to display the number of MAC Address entries in the L2 |
| 234 | * FIB table: |
| 235 | * @cliexstart{show l2fib} |
| 236 | * 3 l2fib entries |
| 237 | * @cliexend |
| 238 | * Example of how to display all the MAC Address entries in the L2 |
| 239 | * FIB table: |
| 240 | * @cliexstart{show l2fib verbose} |
| 241 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 242 | * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0 |
| 243 | * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0 |
| 244 | * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0 |
| 245 | * 3 l2fib entries |
| 246 | * @cliexend |
| 247 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 248 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | VLIB_CLI_COMMAND (show_l2fib_cli, static) = { |
| 250 | .path = "show l2fib", |
| 251 | .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]", |
| 252 | .function = show_l2fib, |
| 253 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 254 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | |
| 256 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 257 | /* Remove all entries from the l2fib */ |
| 258 | void |
| 259 | l2fib_clear_table (uint keep_static) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 261 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 263 | if (keep_static) |
| 264 | { |
| 265 | /* TODO: remove only non-static entries */ |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | /* Remove all entries */ |
| 270 | BV (clib_bihash_free) (&mp->mac_table); |
| 271 | BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table", |
| 272 | L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE); |
| 273 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | |
| 275 | l2learn_main.global_learn_count = 0; |
| 276 | } |
| 277 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 278 | /** Clear all entries in L2FIB. |
| 279 | * @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] | 280 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 282 | clear_l2fib (vlib_main_t * vm, |
| 283 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | { |
| 285 | l2fib_clear_table (0); |
| 286 | return 0; |
| 287 | } |
| 288 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 289 | /*? |
| 290 | * This command clears all the MAC Address entries from the L2 FIB table. |
| 291 | * |
| 292 | * @cliexpar |
| 293 | * Example of how to clear the L2 FIB Table: |
| 294 | * @cliexcmd{clear l2fib} |
| 295 | * Example to show the L2 FIB Table has been cleared: |
| 296 | * @cliexstart{show l2fib verbose} |
| 297 | * no l2fib entries |
| 298 | * @cliexend |
| 299 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 300 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | VLIB_CLI_COMMAND (clear_l2fib_cli, static) = { |
| 302 | .path = "clear l2fib", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 303 | .short_help = "clear l2fib", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | .function = clear_l2fib, |
| 305 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 306 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | |
| 308 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 309 | /** |
| 310 | * Add an entry to the l2fib. |
| 311 | * If the entry already exists then overwrite it |
| 312 | */ |
| 313 | void |
| 314 | l2fib_add_entry (u64 mac, |
| 315 | u32 bd_index, |
| 316 | u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac) |
| 317 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | l2fib_entry_key_t key; |
| 319 | l2fib_entry_result_t result; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 320 | __attribute__ ((unused)) u32 bucket_contents; |
| 321 | l2fib_main_t *mp = &l2fib_main; |
| 322 | BVT (clib_bihash_kv) kv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 324 | /* set up key */ |
| 325 | key.raw = l2fib_make_key ((u8 *) & mac, bd_index); |
| 326 | |
| 327 | /* set up result */ |
| 328 | result.raw = 0; /* clear all fields */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | result.fields.sw_if_index = sw_if_index; |
| 330 | result.fields.static_mac = static_mac; |
| 331 | result.fields.filter = filter_mac; |
| 332 | result.fields.bvi = bvi_mac; |
| 333 | |
| 334 | kv.key = key.raw; |
| 335 | kv.value = result.raw; |
| 336 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 337 | BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 339 | /* increment counter if dynamically learned mac */ |
| 340 | if (result.fields.static_mac) |
| 341 | { |
| 342 | l2learn_main.global_learn_count++; |
| 343 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 346 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 347 | * Add an entry to the L2FIB. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 348 | * The CLI format is: |
| 349 | * l2fib add <mac> <bd> <intf> [static] [bvi] |
| 350 | * l2fib add <mac> <bd> filter |
| 351 | * Note that filter and bvi entries are always static |
| 352 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 354 | l2fib_add (vlib_main_t * vm, |
| 355 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 357 | bd_main_t *bdm = &bd_main; |
| 358 | vnet_main_t *vnm = vnet_get_main (); |
| 359 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | u64 mac; |
| 361 | u32 bd_id; |
| 362 | u32 bd_index; |
| 363 | u32 sw_if_index = ~0; |
| 364 | u32 filter_mac = 0; |
| 365 | u32 static_mac = 0; |
| 366 | u32 bvi_mac = 0; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 367 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 369 | if (!unformat_user (input, unformat_ethernet_address, &mac)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | { |
| 371 | error = clib_error_return (0, "expected mac address `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 372 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | goto done; |
| 374 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 375 | |
| 376 | if (!unformat (input, "%d", &bd_id)) |
| 377 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | error = clib_error_return (0, "expected bridge domain ID `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 379 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 381 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | |
| 383 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 384 | if (!p) |
| 385 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | error = clib_error_return (0, "bridge domain ID %d invalid", bd_id); |
| 387 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 388 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | bd_index = p[0]; |
| 390 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 391 | if (unformat (input, "filter")) |
| 392 | { |
| 393 | filter_mac = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | static_mac = 1; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 395 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 397 | else |
| 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 | if (!unformat_user |
| 401 | (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 402 | { |
| 403 | error = clib_error_return (0, "unknown interface `%U'", |
| 404 | format_unformat_error, input); |
| 405 | goto done; |
| 406 | } |
| 407 | if (unformat (input, "static")) |
| 408 | { |
| 409 | static_mac = 1; |
| 410 | } |
| 411 | else if (unformat (input, "bvi")) |
| 412 | { |
| 413 | bvi_mac = 1; |
| 414 | static_mac = 1; |
| 415 | } |
| 416 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 418 | l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac, |
| 419 | bvi_mac); |
| 420 | |
| 421 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | return error; |
| 423 | } |
| 424 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 425 | /*? |
| 426 | * This command adds a MAC Address entry to the L2 FIB table |
| 427 | * of an existing bridge-domain. The MAC Address can be static |
| 428 | * or dynamic. This command also allows a filter to be added, |
| 429 | * such that packets with given MAC Addresses (source mac or |
| 430 | * destination mac match) are dropped. |
| 431 | * |
| 432 | * @cliexpar |
| 433 | * Example of how to add a dynamic MAC Address entry to the L2 FIB table |
| 434 | * of a bridge-domain (where 200 is the bridge-domain-id): |
| 435 | * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200} |
| 436 | * Example of how to add a static MAC Address entry to the L2 FIB table |
| 437 | * of a bridge-domain (where 200 is the bridge-domain-id): |
| 438 | * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static} |
| 439 | * Example of how to add a filter such that a packet with the given MAC |
| 440 | * Address will be dropped in a given bridge-domain (where 200 is the |
| 441 | * bridge-domain-id): |
| 442 | * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter} |
| 443 | * Example of show command of the provisioned MAC Addresses and filters: |
| 444 | * @cliexstart{show l2fib verbose} |
| 445 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 446 | * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0 |
| 447 | * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0 |
| 448 | * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0 |
| 449 | * 3 l2fib entries |
| 450 | * @cliexend |
| 451 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 452 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | VLIB_CLI_COMMAND (l2fib_add_cli, static) = { |
| 454 | .path = "l2fib add", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 455 | .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 456 | .function = l2fib_add, |
| 457 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 458 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 459 | |
| 460 | |
| 461 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 462 | l2fib_test_command_fn (vlib_main_t * vm, |
| 463 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 464 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 465 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | u64 mac, save_mac; |
| 467 | u32 bd_index = 0; |
| 468 | u32 sw_if_index = 8; |
| 469 | u32 filter_mac = 0; |
| 470 | u32 bvi_mac = 0; |
| 471 | u32 is_add = 0; |
| 472 | u32 is_del = 0; |
| 473 | u32 is_check = 0; |
| 474 | u32 count = 1; |
| 475 | int mac_set = 0; |
| 476 | int i; |
| 477 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 478 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | { |
| 480 | if (unformat (input, "mac %U", unformat_ethernet_address, &mac)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 481 | mac_set = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 482 | else if (unformat (input, "add")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 483 | is_add = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 484 | else if (unformat (input, "del")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 485 | is_del = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 486 | else if (unformat (input, "check")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 487 | is_check = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | else if (unformat (input, "count %d", &count)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 489 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | else |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 491 | break; |
| 492 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | |
| 494 | if (mac_set == 0) |
| 495 | return clib_error_return (0, "mac not set"); |
| 496 | |
| 497 | if (is_add == 0 && is_del == 0 && is_check == 0) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 498 | return clib_error_return (0, |
| 499 | "noop: pick at least one of (add,del,check)"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | |
| 501 | save_mac = mac; |
| 502 | |
| 503 | if (is_add) |
| 504 | { |
| 505 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 506 | { |
| 507 | u64 tmp; |
| 508 | l2fib_add_entry (mac, bd_index, sw_if_index, mac, |
| 509 | filter_mac, bvi_mac); |
| 510 | tmp = clib_net_to_host_u64 (mac); |
| 511 | tmp >>= 16; |
| 512 | tmp++; |
| 513 | tmp <<= 16; |
| 514 | mac = clib_host_to_net_u64 (tmp); |
| 515 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | if (is_check) |
| 519 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 520 | BVT (clib_bihash_kv) kv; |
| 521 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | |
| 523 | mac = save_mac; |
| 524 | |
| 525 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 526 | { |
| 527 | u64 tmp; |
| 528 | kv.key = l2fib_make_key ((u8 *) & mac, bd_index); |
| 529 | if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv)) |
| 530 | { |
| 531 | clib_warning ("key %U AWOL", format_ethernet_address, &mac); |
| 532 | break; |
| 533 | } |
| 534 | tmp = clib_net_to_host_u64 (mac); |
| 535 | tmp >>= 16; |
| 536 | tmp++; |
| 537 | tmp <<= 16; |
| 538 | mac = clib_host_to_net_u64 (tmp); |
| 539 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | if (is_del) |
| 543 | { |
| 544 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 545 | { |
| 546 | u64 tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 548 | l2fib_del_entry (mac, bd_index); |
| 549 | |
| 550 | tmp = clib_net_to_host_u64 (mac); |
| 551 | tmp >>= 16; |
| 552 | tmp++; |
| 553 | tmp <<= 16; |
| 554 | mac = clib_host_to_net_u64 (tmp); |
| 555 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | return error; |
| 559 | } |
| 560 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 561 | /*? |
| 562 | * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default |
| 563 | * bridge domain (bridge-domain-id of 0) to be modified. |
| 564 | * |
| 565 | * @cliexpar |
| 566 | * @parblock |
| 567 | * Example of how to add a set of 4 sequential MAC Address entries to L2 |
| 568 | * FIB table of the default bridge-domain: |
| 569 | * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4} |
| 570 | * |
| 571 | * Show the set of 4 sequential MAC Address entries that were added: |
| 572 | * @cliexstart{show l2fib verbose} |
| 573 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 574 | * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 575 | * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 576 | * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 577 | * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 578 | * 4 l2fib entries |
| 579 | * @cliexend |
| 580 | * |
| 581 | * Example of how to check that the set of 4 sequential MAC Address |
| 582 | * entries were added to L2 FIB table of the default |
| 583 | * bridge-domain. Used a count of 5 to produce an error: |
| 584 | * |
| 585 | * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5} |
| 586 | * The output of the check command is in the log files. Log file |
| 587 | * location may vary based on your OS and Version: |
| 588 | * |
| 589 | * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b> |
| 590 | * |
| 591 | * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL |
| 592 | * |
| 593 | * Example of how to delete a set of 4 sequential MAC Address entries |
| 594 | * from L2 FIB table of the default bridge-domain: |
| 595 | * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4} |
| 596 | * @endparblock |
| 597 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 598 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | VLIB_CLI_COMMAND (l2fib_test_command, static) = { |
| 600 | .path = "test l2fib", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 601 | .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | .function = l2fib_test_command_fn, |
| 603 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 604 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 605 | |
| 606 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 607 | /** |
| 608 | * Delete an entry from the l2fib. |
| 609 | * Return 0 if the entry was deleted, or 1 if it was not found |
| 610 | */ |
| 611 | u32 |
| 612 | l2fib_del_entry (u64 mac, u32 bd_index) |
| 613 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 614 | |
| 615 | l2fib_entry_result_t result; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 616 | l2fib_main_t *mp = &l2fib_main; |
| 617 | BVT (clib_bihash_kv) kv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 618 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 619 | /* set up key */ |
| 620 | kv.key = l2fib_make_key ((u8 *) & mac, bd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 621 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 622 | if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 623 | return 1; |
| 624 | |
| 625 | result.raw = kv.value; |
| 626 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 627 | /* decrement counter if dynamically learned mac */ |
| 628 | if (result.fields.static_mac) |
| 629 | { |
| 630 | if (l2learn_main.global_learn_count > 0) |
| 631 | { |
| 632 | l2learn_main.global_learn_count--; |
| 633 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 634 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 636 | /* Remove entry from hash table */ |
| 637 | BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 641 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 642 | * Delete an entry from the L2FIB. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 643 | * The CLI format is: |
| 644 | * l2fib del <mac> <bd-id> |
| 645 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 646 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 647 | l2fib_del (vlib_main_t * vm, |
| 648 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 650 | bd_main_t *bdm = &bd_main; |
| 651 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | u64 mac; |
| 653 | u32 bd_id; |
| 654 | u32 bd_index; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 655 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 657 | if (!unformat_user (input, unformat_ethernet_address, &mac)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 658 | { |
| 659 | error = clib_error_return (0, "expected mac address `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 660 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | goto done; |
| 662 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 663 | |
| 664 | if (!unformat (input, "%d", &bd_id)) |
| 665 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 666 | error = clib_error_return (0, "expected bridge domain ID `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 667 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 668 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 669 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 670 | |
| 671 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 672 | if (!p) |
| 673 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 674 | error = clib_error_return (0, "bridge domain ID %d invalid", bd_id); |
| 675 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 676 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | bd_index = p[0]; |
| 678 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 679 | /* Delete the entry */ |
| 680 | if (l2fib_del_entry (mac, bd_index)) |
| 681 | { |
| 682 | error = clib_error_return (0, "mac entry not found"); |
| 683 | goto done; |
| 684 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 686 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 687 | return error; |
| 688 | } |
| 689 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 690 | /*? |
| 691 | * This command deletes an existing MAC Address entry from the L2 FIB |
| 692 | * table of an existing bridge-domain. |
| 693 | * |
| 694 | * @cliexpar |
| 695 | * 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): |
| 696 | * @cliexcmd{l2fib del 52:54:00:53:18:33 200} |
| 697 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 698 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 699 | VLIB_CLI_COMMAND (l2fib_del_cli, static) = { |
| 700 | .path = "l2fib del", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 701 | .short_help = "l2fib del <mac> <bridge-domain-id>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 702 | .function = l2fib_del, |
| 703 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 704 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 705 | |
| 706 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 707 | BVT (clib_bihash) * get_mac_table (void) |
| 708 | { |
| 709 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | return &mp->mac_table; |
| 711 | } |
| 712 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 713 | static uword |
| 714 | l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 715 | vlib_frame_t * f) |
| 716 | { |
| 717 | uword event_type, *event_data = 0; |
| 718 | l2fib_main_t *msm = &l2fib_main; |
| 719 | l2_bridge_domain_t *bd_config; |
| 720 | BVT (clib_bihash) * h = &msm->mac_table; |
| 721 | clib_bihash_bucket_t *b; |
| 722 | BVT (clib_bihash_value) * v; |
| 723 | l2fib_entry_key_t key; |
| 724 | l2fib_entry_result_t result; |
| 725 | int i, j, k; |
| 726 | bool enabled = 0; |
| 727 | f64 start_time, last_run_duration = 0, t; |
| 728 | i16 delta; |
| 729 | |
| 730 | while (1) |
| 731 | { |
| 732 | if (enabled) |
| 733 | vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration); |
| 734 | else |
| 735 | vlib_process_wait_for_event (vm); |
| 736 | |
| 737 | event_type = vlib_process_get_events (vm, &event_data); |
| 738 | vec_reset_length (event_data); |
| 739 | |
| 740 | switch (event_type) |
| 741 | { |
| 742 | case ~0: |
| 743 | break; |
| 744 | case L2_MAC_AGE_PROCESS_EVENT_START: |
| 745 | enabled = 1; |
| 746 | break; |
| 747 | case L2_MAC_AGE_PROCESS_EVENT_STOP: |
| 748 | enabled = 0; |
| 749 | continue; |
| 750 | default: |
| 751 | ASSERT (0); |
| 752 | } |
| 753 | last_run_duration = start_time = vlib_time_now (vm); |
| 754 | for (i = 0; i < h->nbuckets; i++) |
| 755 | { |
| 756 | /* Allow no more than 10us without a pause */ |
| 757 | t = vlib_time_now (vm); |
| 758 | if (t > start_time + 10e-6) |
| 759 | { |
| 760 | vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */ |
| 761 | start_time = vlib_time_now (vm); |
| 762 | } |
| 763 | |
| 764 | if (i < (h->nbuckets - 3)) |
| 765 | { |
| 766 | b = &h->buckets[i + 3]; |
| 767 | CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD); |
| 768 | b = &h->buckets[i + 1]; |
| 769 | if (b->offset) |
| 770 | { |
| 771 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 772 | CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | b = &h->buckets[i]; |
| 777 | if (b->offset == 0) |
| 778 | continue; |
| 779 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 780 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 781 | { |
| 782 | for (k = 0; k < BIHASH_KVP_PER_PAGE; k++) |
| 783 | { |
| 784 | if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL) |
| 785 | continue; |
| 786 | |
| 787 | key.raw = v->kvp[k].key; |
| 788 | result.raw = v->kvp[k].value; |
| 789 | |
| 790 | if (result.fields.static_mac) |
| 791 | continue; |
| 792 | |
| 793 | bd_config = vec_elt_at_index (l2input_main.bd_configs, |
| 794 | key.fields.bd_index); |
| 795 | |
| 796 | if (bd_config->mac_age == 0) |
| 797 | continue; |
| 798 | |
| 799 | delta = (u8) (start_time / 60) - result.fields.timestamp; |
| 800 | delta += delta < 0 ? 256 : 0; |
| 801 | |
| 802 | if (delta > bd_config->mac_age) |
| 803 | { |
| 804 | void *p = &key.fields.mac; |
| 805 | l2fib_del_entry (*(u64 *) p, key.fields.bd_index); |
| 806 | } |
| 807 | } |
| 808 | v++; |
| 809 | } |
| 810 | } |
| 811 | last_run_duration = vlib_time_now (vm) - last_run_duration; |
| 812 | } |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | /* *INDENT-OFF* */ |
| 817 | VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = { |
| 818 | .function = l2fib_mac_age_scanner_process, |
| 819 | .type = VLIB_NODE_TYPE_PROCESS, |
| 820 | .name = "l2fib-mac-age-scanner-process", |
| 821 | }; |
| 822 | /* *INDENT-ON* */ |
| 823 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 824 | clib_error_t * |
| 825 | l2fib_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 826 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 827 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | l2fib_entry_key_t test_key; |
| 829 | u8 test_mac[6]; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 830 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 831 | mp->vlib_main = vm; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 832 | mp->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 833 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 834 | /* Create the hash table */ |
| 835 | BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table", |
| 836 | L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 837 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 838 | /* verify the key constructor is good, since it is endian-sensitive */ |
| 839 | memset (test_mac, 0, sizeof (test_mac)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 840 | test_mac[0] = 0x11; |
| 841 | test_key.raw = 0; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 842 | test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 843 | ASSERT (test_key.fields.mac[0] == 0x11); |
| 844 | ASSERT (test_key.fields.bd_index == 0x1234); |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | VLIB_INIT_FUNCTION (l2fib_init); |
| 850 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 851 | /* |
| 852 | * fd.io coding-style-patch-verification: ON |
| 853 | * |
| 854 | * Local Variables: |
| 855 | * eval: (c-set-style "gnu") |
| 856 | * End: |
| 857 | */ |