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, |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 171 | "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s", |
| 172 | "Mac-Address", "BD-Idx", "If-Idx", |
| 173 | "BSN-ISN", "Age(min)", "static", "filter", |
| 174 | "bvi", "Interface-Name"); |
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 | |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 186 | if (bd_config->mac_age && !result.fields.static_mac) |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 187 | { |
| 188 | i16 delta = now - result.fields.timestamp; |
| 189 | delta += delta < 0 ? 256 : 0; |
| 190 | s = format (s, "%d", delta); |
| 191 | } |
| 192 | else |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 193 | s = format (s, "-"); |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 194 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 195 | vlib_cli_output (vm, |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 196 | "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 197 | format_ethernet_address, key.fields.mac, |
| 198 | key.fields.bd_index, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 199 | result.fields.sw_if_index == ~0 |
| 200 | ? -1 : result.fields.sw_if_index, |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 201 | result.fields.bd_sn, result.fields.int_sn, |
| 202 | s, result.fields.static_mac ? "*" : "-", |
| 203 | result.fields.filter ? "*" : "-", |
| 204 | result.fields.bvi ? "*" : "-", |
| 205 | format_vnet_sw_if_index_name_with_NA, |
| 206 | msm->vnet_main, result.fields.sw_if_index); |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 207 | vec_reset_length (s); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 208 | } |
| 209 | total_entries++; |
| 210 | } |
| 211 | v++; |
| 212 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (total_entries == 0) |
| 216 | vlib_cli_output (vm, "no l2fib entries"); |
| 217 | else |
| 218 | vlib_cli_output (vm, "%lld l2fib entries", total_entries); |
| 219 | |
| 220 | if (raw) |
| 221 | vlib_cli_output (vm, "Raw Hash Table:\n%U\n", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 222 | BV (format_bihash), h, 1 /* verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 224 | vec_free (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 228 | /*? |
| 229 | * This command dispays the MAC Address entries of the L2 FIB table. |
| 230 | * Output can be filtered to just get the number of MAC Addresses or display |
| 231 | * each MAC Address for all bridge domains or just a single bridge domain. |
| 232 | * |
| 233 | * @cliexpar |
| 234 | * Example of how to display the number of MAC Address entries in the L2 |
| 235 | * FIB table: |
| 236 | * @cliexstart{show l2fib} |
| 237 | * 3 l2fib entries |
| 238 | * @cliexend |
| 239 | * Example of how to display all the MAC Address entries in the L2 |
| 240 | * FIB table: |
| 241 | * @cliexstart{show l2fib verbose} |
| 242 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 243 | * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0 |
| 244 | * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0 |
| 245 | * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0 |
| 246 | * 3 l2fib entries |
| 247 | * @cliexend |
| 248 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 249 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | VLIB_CLI_COMMAND (show_l2fib_cli, static) = { |
| 251 | .path = "show l2fib", |
| 252 | .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]", |
| 253 | .function = show_l2fib, |
| 254 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 255 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | |
| 257 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 258 | /* Remove all entries from the l2fib */ |
| 259 | void |
| 260 | l2fib_clear_table (uint keep_static) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 262 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 264 | if (keep_static) |
| 265 | { |
| 266 | /* TODO: remove only non-static entries */ |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | /* Remove all entries */ |
| 271 | BV (clib_bihash_free) (&mp->mac_table); |
| 272 | BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table", |
| 273 | L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE); |
| 274 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | |
| 276 | l2learn_main.global_learn_count = 0; |
| 277 | } |
| 278 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 279 | /** Clear all entries in L2FIB. |
| 280 | * @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] | 281 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 283 | clear_l2fib (vlib_main_t * vm, |
| 284 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | { |
| 286 | l2fib_clear_table (0); |
| 287 | return 0; |
| 288 | } |
| 289 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 290 | /*? |
| 291 | * This command clears all the MAC Address entries from the L2 FIB table. |
| 292 | * |
| 293 | * @cliexpar |
| 294 | * Example of how to clear the L2 FIB Table: |
| 295 | * @cliexcmd{clear l2fib} |
| 296 | * Example to show the L2 FIB Table has been cleared: |
| 297 | * @cliexstart{show l2fib verbose} |
| 298 | * no l2fib entries |
| 299 | * @cliexend |
| 300 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 301 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | VLIB_CLI_COMMAND (clear_l2fib_cli, static) = { |
| 303 | .path = "clear l2fib", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 304 | .short_help = "clear l2fib", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | .function = clear_l2fib, |
| 306 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 307 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | |
| 309 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 310 | /** |
| 311 | * Add an entry to the l2fib. |
| 312 | * If the entry already exists then overwrite it |
| 313 | */ |
| 314 | void |
| 315 | l2fib_add_entry (u64 mac, |
| 316 | u32 bd_index, |
| 317 | u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac) |
| 318 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | l2fib_entry_key_t key; |
| 320 | l2fib_entry_result_t result; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 321 | __attribute__ ((unused)) u32 bucket_contents; |
| 322 | l2fib_main_t *mp = &l2fib_main; |
| 323 | BVT (clib_bihash_kv) kv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 325 | /* set up key */ |
| 326 | key.raw = l2fib_make_key ((u8 *) & mac, bd_index); |
| 327 | |
| 328 | /* set up result */ |
| 329 | result.raw = 0; /* clear all fields */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 330 | result.fields.sw_if_index = sw_if_index; |
| 331 | result.fields.static_mac = static_mac; |
| 332 | result.fields.filter = filter_mac; |
| 333 | result.fields.bvi = bvi_mac; |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 334 | if (!static_mac) |
| 335 | { |
| 336 | l2_input_config_t *int_config = l2input_intf_config (sw_if_index); |
| 337 | l2_bridge_domain_t *bd_config = |
| 338 | vec_elt_at_index (l2input_main.bd_configs, |
| 339 | bd_index); |
| 340 | result.fields.int_sn = int_config->seq_num; |
| 341 | result.fields.bd_sn = bd_config->seq_num; |
| 342 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | |
| 344 | kv.key = key.raw; |
| 345 | kv.value = result.raw; |
| 346 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 347 | BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ ); |
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 | /* increment counter if dynamically learned mac */ |
| 350 | if (result.fields.static_mac) |
| 351 | { |
| 352 | l2learn_main.global_learn_count++; |
| 353 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 356 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 357 | * Add an entry to the L2FIB. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 358 | * The CLI format is: |
| 359 | * l2fib add <mac> <bd> <intf> [static] [bvi] |
| 360 | * l2fib add <mac> <bd> filter |
| 361 | * Note that filter and bvi entries are always static |
| 362 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 364 | l2fib_add (vlib_main_t * vm, |
| 365 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 367 | bd_main_t *bdm = &bd_main; |
| 368 | vnet_main_t *vnm = vnet_get_main (); |
| 369 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | u64 mac; |
| 371 | u32 bd_id; |
| 372 | u32 bd_index; |
| 373 | u32 sw_if_index = ~0; |
| 374 | u32 filter_mac = 0; |
| 375 | u32 static_mac = 0; |
| 376 | u32 bvi_mac = 0; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 377 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 379 | if (!unformat_user (input, unformat_ethernet_address, &mac)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | { |
| 381 | error = clib_error_return (0, "expected mac address `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 382 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | goto done; |
| 384 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 385 | |
| 386 | if (!unformat (input, "%d", &bd_id)) |
| 387 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | error = clib_error_return (0, "expected bridge domain ID `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 389 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 391 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | |
| 393 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 394 | if (!p) |
| 395 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | error = clib_error_return (0, "bridge domain ID %d invalid", bd_id); |
| 397 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 398 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | bd_index = p[0]; |
| 400 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 401 | if (unformat (input, "filter")) |
| 402 | { |
| 403 | filter_mac = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 404 | static_mac = 1; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 405 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 407 | else |
| 408 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 410 | if (!unformat_user |
| 411 | (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 412 | { |
| 413 | error = clib_error_return (0, "unknown interface `%U'", |
| 414 | format_unformat_error, input); |
| 415 | goto done; |
| 416 | } |
| 417 | if (unformat (input, "static")) |
| 418 | { |
| 419 | static_mac = 1; |
| 420 | } |
| 421 | else if (unformat (input, "bvi")) |
| 422 | { |
| 423 | bvi_mac = 1; |
| 424 | static_mac = 1; |
| 425 | } |
| 426 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 428 | l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac, |
| 429 | bvi_mac); |
| 430 | |
| 431 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | return error; |
| 433 | } |
| 434 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 435 | /*? |
| 436 | * This command adds a MAC Address entry to the L2 FIB table |
| 437 | * of an existing bridge-domain. The MAC Address can be static |
| 438 | * or dynamic. This command also allows a filter to be added, |
| 439 | * such that packets with given MAC Addresses (source mac or |
| 440 | * destination mac match) are dropped. |
| 441 | * |
| 442 | * @cliexpar |
| 443 | * Example of how to add a dynamic MAC Address entry to the L2 FIB table |
| 444 | * of a bridge-domain (where 200 is the bridge-domain-id): |
| 445 | * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200} |
| 446 | * Example of how to add a static MAC Address entry to the L2 FIB table |
| 447 | * of a bridge-domain (where 200 is the bridge-domain-id): |
| 448 | * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static} |
| 449 | * Example of how to add a filter such that a packet with the given MAC |
| 450 | * Address will be dropped in a given bridge-domain (where 200 is the |
| 451 | * bridge-domain-id): |
| 452 | * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter} |
| 453 | * Example of show command of the provisioned MAC Addresses and filters: |
| 454 | * @cliexstart{show l2fib verbose} |
| 455 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 456 | * 52:54:00:53:18:33 1 GigabitEthernet0/8/0.200 3 0 0 0 0 0 |
| 457 | * 52:54:00:53:18:55 1 GigabitEthernet0/8/0.200 3 1 0 0 0 0 |
| 458 | * 52:54:00:53:18:77 1 N/A -1 1 1 0 0 0 |
| 459 | * 3 l2fib entries |
| 460 | * @cliexend |
| 461 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 462 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | VLIB_CLI_COMMAND (l2fib_add_cli, static) = { |
| 464 | .path = "l2fib add", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 465 | .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | .function = l2fib_add, |
| 467 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 468 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | |
| 470 | |
| 471 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 472 | l2fib_test_command_fn (vlib_main_t * vm, |
| 473 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 475 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 476 | u64 mac, save_mac; |
| 477 | u32 bd_index = 0; |
| 478 | u32 sw_if_index = 8; |
| 479 | u32 filter_mac = 0; |
| 480 | u32 bvi_mac = 0; |
| 481 | u32 is_add = 0; |
| 482 | u32 is_del = 0; |
| 483 | u32 is_check = 0; |
| 484 | u32 count = 1; |
| 485 | int mac_set = 0; |
| 486 | int i; |
| 487 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 488 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | { |
| 490 | if (unformat (input, "mac %U", unformat_ethernet_address, &mac)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 491 | mac_set = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | else if (unformat (input, "add")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 493 | is_add = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | else if (unformat (input, "del")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 495 | is_del = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | else if (unformat (input, "check")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 497 | is_check = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | else if (unformat (input, "count %d", &count)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 499 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | else |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 501 | break; |
| 502 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | |
| 504 | if (mac_set == 0) |
| 505 | return clib_error_return (0, "mac not set"); |
| 506 | |
| 507 | if (is_add == 0 && is_del == 0 && is_check == 0) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 508 | return clib_error_return (0, |
| 509 | "noop: pick at least one of (add,del,check)"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | |
| 511 | save_mac = mac; |
| 512 | |
| 513 | if (is_add) |
| 514 | { |
| 515 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 516 | { |
| 517 | u64 tmp; |
| 518 | l2fib_add_entry (mac, bd_index, sw_if_index, mac, |
| 519 | filter_mac, bvi_mac); |
| 520 | tmp = clib_net_to_host_u64 (mac); |
| 521 | tmp >>= 16; |
| 522 | tmp++; |
| 523 | tmp <<= 16; |
| 524 | mac = clib_host_to_net_u64 (tmp); |
| 525 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | if (is_check) |
| 529 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 530 | BVT (clib_bihash_kv) kv; |
| 531 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | |
| 533 | mac = save_mac; |
| 534 | |
| 535 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 536 | { |
| 537 | u64 tmp; |
| 538 | kv.key = l2fib_make_key ((u8 *) & mac, bd_index); |
| 539 | if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv)) |
| 540 | { |
| 541 | clib_warning ("key %U AWOL", format_ethernet_address, &mac); |
| 542 | break; |
| 543 | } |
| 544 | tmp = clib_net_to_host_u64 (mac); |
| 545 | tmp >>= 16; |
| 546 | tmp++; |
| 547 | tmp <<= 16; |
| 548 | mac = clib_host_to_net_u64 (tmp); |
| 549 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (is_del) |
| 553 | { |
| 554 | for (i = 0; i < count; i++) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 555 | { |
| 556 | u64 tmp; |
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 | l2fib_del_entry (mac, bd_index); |
| 559 | |
| 560 | tmp = clib_net_to_host_u64 (mac); |
| 561 | tmp >>= 16; |
| 562 | tmp++; |
| 563 | tmp <<= 16; |
| 564 | mac = clib_host_to_net_u64 (tmp); |
| 565 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | return error; |
| 569 | } |
| 570 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 571 | /*? |
| 572 | * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default |
| 573 | * bridge domain (bridge-domain-id of 0) to be modified. |
| 574 | * |
| 575 | * @cliexpar |
| 576 | * @parblock |
| 577 | * Example of how to add a set of 4 sequential MAC Address entries to L2 |
| 578 | * FIB table of the default bridge-domain: |
| 579 | * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4} |
| 580 | * |
| 581 | * Show the set of 4 sequential MAC Address entries that were added: |
| 582 | * @cliexstart{show l2fib verbose} |
| 583 | * Mac Address BD Idx Interface Index static filter bvi refresh timestamp |
| 584 | * 52:54:00:53:00:00 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 585 | * 52:54:00:53:00:01 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 586 | * 52:54:00:53:00:03 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 587 | * 52:54:00:53:00:02 0 GigabitEthernet0/8/0.300 8 0 0 0 0 0 |
| 588 | * 4 l2fib entries |
| 589 | * @cliexend |
| 590 | * |
| 591 | * Example of how to check that the set of 4 sequential MAC Address |
| 592 | * entries were added to L2 FIB table of the default |
| 593 | * bridge-domain. Used a count of 5 to produce an error: |
| 594 | * |
| 595 | * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5} |
| 596 | * The output of the check command is in the log files. Log file |
| 597 | * location may vary based on your OS and Version: |
| 598 | * |
| 599 | * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b> |
| 600 | * |
| 601 | * Sep 7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL |
| 602 | * |
| 603 | * Example of how to delete a set of 4 sequential MAC Address entries |
| 604 | * from L2 FIB table of the default bridge-domain: |
| 605 | * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4} |
| 606 | * @endparblock |
| 607 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 608 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 609 | VLIB_CLI_COMMAND (l2fib_test_command, static) = { |
| 610 | .path = "test l2fib", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 611 | .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 612 | .function = l2fib_test_command_fn, |
| 613 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 614 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 615 | |
| 616 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 617 | /** |
| 618 | * Delete an entry from the l2fib. |
| 619 | * Return 0 if the entry was deleted, or 1 if it was not found |
| 620 | */ |
| 621 | u32 |
| 622 | l2fib_del_entry (u64 mac, u32 bd_index) |
| 623 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 624 | |
| 625 | l2fib_entry_result_t result; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 626 | l2fib_main_t *mp = &l2fib_main; |
| 627 | BVT (clib_bihash_kv) kv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 628 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 629 | /* set up key */ |
| 630 | kv.key = l2fib_make_key ((u8 *) & mac, bd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 632 | if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | return 1; |
| 634 | |
| 635 | result.raw = kv.value; |
| 636 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 637 | /* decrement counter if dynamically learned mac */ |
| 638 | if (result.fields.static_mac) |
| 639 | { |
| 640 | if (l2learn_main.global_learn_count > 0) |
| 641 | { |
| 642 | l2learn_main.global_learn_count--; |
| 643 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 646 | /* Remove entry from hash table */ |
| 647 | BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 651 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 652 | * Delete an entry from the L2FIB. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 653 | * The CLI format is: |
| 654 | * l2fib del <mac> <bd-id> |
| 655 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 657 | l2fib_del (vlib_main_t * vm, |
| 658 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 659 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 660 | bd_main_t *bdm = &bd_main; |
| 661 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 662 | u64 mac; |
| 663 | u32 bd_id; |
| 664 | u32 bd_index; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 665 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 666 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 667 | if (!unformat_user (input, unformat_ethernet_address, &mac)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 668 | { |
| 669 | error = clib_error_return (0, "expected mac address `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 670 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 671 | goto done; |
| 672 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 673 | |
| 674 | if (!unformat (input, "%d", &bd_id)) |
| 675 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 676 | error = clib_error_return (0, "expected bridge domain ID `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 677 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 679 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 680 | |
| 681 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 682 | if (!p) |
| 683 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 684 | error = clib_error_return (0, "bridge domain ID %d invalid", bd_id); |
| 685 | goto done; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 686 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 687 | bd_index = p[0]; |
| 688 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 689 | /* Delete the entry */ |
| 690 | if (l2fib_del_entry (mac, bd_index)) |
| 691 | { |
| 692 | error = clib_error_return (0, "mac entry not found"); |
| 693 | goto done; |
| 694 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 695 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 696 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 697 | return error; |
| 698 | } |
| 699 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 700 | /*? |
| 701 | * This command deletes an existing MAC Address entry from the L2 FIB |
| 702 | * table of an existing bridge-domain. |
| 703 | * |
| 704 | * @cliexpar |
| 705 | * 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): |
| 706 | * @cliexcmd{l2fib del 52:54:00:53:18:33 200} |
| 707 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 708 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 709 | VLIB_CLI_COMMAND (l2fib_del_cli, static) = { |
| 710 | .path = "l2fib del", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 711 | .short_help = "l2fib del <mac> <bridge-domain-id>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 712 | .function = l2fib_del, |
| 713 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 714 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 716 | /** |
| 717 | Kick off ager to scan MACs to age/delete MAC entries |
| 718 | */ |
| 719 | void |
| 720 | l2fib_start_ager_scan (vlib_main_t * vm) |
| 721 | { |
| 722 | l2_bridge_domain_t *bd_config; |
| 723 | int enable = 0; |
| 724 | |
| 725 | /* check if there is at least one bd with mac aging enabled */ |
| 726 | vec_foreach (bd_config, l2input_main.bd_configs) |
| 727 | if (bd_config->bd_id != ~0 && bd_config->mac_age != 0) |
| 728 | enable = 1; |
| 729 | |
| 730 | vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index, |
| 731 | enable ? L2_MAC_AGE_PROCESS_EVENT_START : |
| 732 | L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0); |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | Flush all learned MACs from an interface |
| 737 | */ |
| 738 | void |
| 739 | l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index) |
| 740 | { |
| 741 | l2_input_config_t *int_config; |
| 742 | int_config = l2input_intf_config (sw_if_index); |
| 743 | int_config->seq_num += 1; |
| 744 | l2fib_start_ager_scan (vm); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | Flush all learned MACs in a bridge domain |
| 749 | */ |
| 750 | void |
| 751 | l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index) |
| 752 | { |
| 753 | l2_bridge_domain_t *bd_config; |
Eyal Bari | afc47aa | 2017-04-20 14:45:17 +0300 | [diff] [blame^] | 754 | bd_config = l2input_bd_config (bd_index); |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 755 | bd_config->seq_num += 1; |
| 756 | l2fib_start_ager_scan (vm); |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | Flush MACs, except static ones, associated with an interface |
| 761 | The CLI format is: |
| 762 | l2fib flush-mac interface <if-name> |
| 763 | */ |
| 764 | static clib_error_t * |
| 765 | l2fib_flush_mac_int (vlib_main_t * vm, |
| 766 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 767 | { |
| 768 | vnet_main_t *vnm = vnet_get_main (); |
| 769 | clib_error_t *error = 0; |
| 770 | u32 sw_if_index; |
| 771 | |
| 772 | if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 773 | { |
| 774 | error = clib_error_return (0, "unknown interface `%U'", |
| 775 | format_unformat_error, input); |
| 776 | goto done; |
| 777 | } |
| 778 | |
| 779 | l2fib_flush_int_mac (vm, sw_if_index); |
| 780 | |
| 781 | done: |
| 782 | return error; |
| 783 | } |
| 784 | |
| 785 | /*? |
| 786 | * This command kick off ager to delete all existing MAC Address entries, |
| 787 | * except static ones, associated with an interface from the L2 FIB table. |
| 788 | * |
| 789 | * @cliexpar |
| 790 | * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table: |
| 791 | * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0} |
| 792 | ?*/ |
| 793 | /* *INDENT-OFF* */ |
| 794 | VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = { |
| 795 | .path = "l2fib flush-mac interface", |
| 796 | .short_help = "l2fib flush-mac interface <if-name>", |
| 797 | .function = l2fib_flush_mac_int, |
| 798 | }; |
| 799 | /* *INDENT-ON* */ |
| 800 | |
| 801 | /** |
| 802 | Flush bridge-domain MACs except static ones. |
| 803 | The CLI format is: |
| 804 | l2fib flush-mac bridge-domain <bd-id> |
| 805 | */ |
| 806 | static clib_error_t * |
| 807 | l2fib_flush_mac_bd (vlib_main_t * vm, |
| 808 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 809 | { |
| 810 | bd_main_t *bdm = &bd_main; |
| 811 | clib_error_t *error = 0; |
| 812 | u32 bd_index, bd_id; |
| 813 | uword *p; |
| 814 | |
| 815 | if (!unformat (input, "%d", &bd_id)) |
| 816 | { |
| 817 | error = clib_error_return (0, "expecting bridge-domain id but got `%U'", |
| 818 | format_unformat_error, input); |
| 819 | goto done; |
| 820 | } |
| 821 | |
| 822 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
| 823 | if (p) |
| 824 | bd_index = *p; |
| 825 | else |
| 826 | return clib_error_return (0, "No such bridge domain %d", bd_id); |
| 827 | |
| 828 | l2fib_flush_bd_mac (vm, bd_index); |
| 829 | |
| 830 | done: |
| 831 | return error; |
| 832 | } |
| 833 | |
| 834 | /*? |
| 835 | * This command kick off ager to delete all existing MAC Address entries, |
| 836 | * except static ones, in a bridge domain from the L2 FIB table. |
| 837 | * |
| 838 | * @cliexpar |
| 839 | * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table: |
| 840 | * @cliexcmd{l2fib flush-mac bridge-domain 1000} |
| 841 | ?*/ |
| 842 | /* *INDENT-OFF* */ |
| 843 | VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = { |
| 844 | .path = "l2fib flush-mac bridge-domain", |
| 845 | .short_help = "l2fib flush-mac bridge-domain <bd-id>", |
| 846 | .function = l2fib_flush_mac_bd, |
| 847 | }; |
| 848 | /* *INDENT-ON* */ |
| 849 | |
Eyal Bari | afc47aa | 2017-04-20 14:45:17 +0300 | [diff] [blame^] | 850 | clib_error_t * |
| 851 | l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags) |
| 852 | { |
| 853 | l2_input_config_t *config = l2input_intf_config (sw_if_index); |
| 854 | if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge) |
| 855 | l2fib_flush_int_mac (vnm->vlib_main, sw_if_index); |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 860 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 861 | BVT (clib_bihash) * get_mac_table (void) |
| 862 | { |
| 863 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 864 | return &mp->mac_table; |
| 865 | } |
| 866 | |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 867 | static uword |
| 868 | l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 869 | vlib_frame_t * f) |
| 870 | { |
| 871 | uword event_type, *event_data = 0; |
| 872 | l2fib_main_t *msm = &l2fib_main; |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 873 | l2_input_config_t *int_config; |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 874 | l2_bridge_domain_t *bd_config; |
| 875 | BVT (clib_bihash) * h = &msm->mac_table; |
| 876 | clib_bihash_bucket_t *b; |
| 877 | BVT (clib_bihash_value) * v; |
| 878 | l2fib_entry_key_t key; |
| 879 | l2fib_entry_result_t result; |
| 880 | int i, j, k; |
| 881 | bool enabled = 0; |
| 882 | f64 start_time, last_run_duration = 0, t; |
| 883 | i16 delta; |
| 884 | |
| 885 | while (1) |
| 886 | { |
| 887 | if (enabled) |
| 888 | vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration); |
| 889 | else |
| 890 | vlib_process_wait_for_event (vm); |
| 891 | |
| 892 | event_type = vlib_process_get_events (vm, &event_data); |
| 893 | vec_reset_length (event_data); |
| 894 | |
| 895 | switch (event_type) |
| 896 | { |
| 897 | case ~0: |
| 898 | break; |
| 899 | case L2_MAC_AGE_PROCESS_EVENT_START: |
| 900 | enabled = 1; |
| 901 | break; |
| 902 | case L2_MAC_AGE_PROCESS_EVENT_STOP: |
| 903 | enabled = 0; |
| 904 | continue; |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 905 | case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS: |
| 906 | enabled = 0; |
| 907 | break; |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 908 | default: |
| 909 | ASSERT (0); |
| 910 | } |
| 911 | last_run_duration = start_time = vlib_time_now (vm); |
| 912 | for (i = 0; i < h->nbuckets; i++) |
| 913 | { |
| 914 | /* Allow no more than 10us without a pause */ |
| 915 | t = vlib_time_now (vm); |
| 916 | if (t > start_time + 10e-6) |
| 917 | { |
| 918 | vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */ |
| 919 | start_time = vlib_time_now (vm); |
| 920 | } |
| 921 | |
| 922 | if (i < (h->nbuckets - 3)) |
| 923 | { |
| 924 | b = &h->buckets[i + 3]; |
| 925 | CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD); |
| 926 | b = &h->buckets[i + 1]; |
| 927 | if (b->offset) |
| 928 | { |
| 929 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 930 | CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | b = &h->buckets[i]; |
| 935 | if (b->offset == 0) |
| 936 | continue; |
| 937 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 938 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 939 | { |
| 940 | for (k = 0; k < BIHASH_KVP_PER_PAGE; k++) |
| 941 | { |
| 942 | if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL) |
| 943 | continue; |
| 944 | |
| 945 | key.raw = v->kvp[k].key; |
| 946 | result.raw = v->kvp[k].value; |
| 947 | |
| 948 | if (result.fields.static_mac) |
| 949 | continue; |
| 950 | |
John Lo | da1f2c7 | 2017-03-24 20:11:15 -0400 | [diff] [blame] | 951 | int_config = |
| 952 | l2input_intf_config (result.fields.sw_if_index); |
| 953 | bd_config = |
| 954 | vec_elt_at_index (l2input_main.bd_configs, |
| 955 | key.fields.bd_index); |
| 956 | |
| 957 | if ((result.fields.int_sn != int_config->seq_num) || |
| 958 | (result.fields.bd_sn != bd_config->seq_num)) |
| 959 | { |
| 960 | void *p = &key.fields.mac; |
| 961 | l2fib_del_entry (*(u64 *) p, key.fields.bd_index); |
| 962 | continue; |
| 963 | } |
Damjan Marion | d171d48 | 2016-12-05 14:16:38 +0100 | [diff] [blame] | 964 | |
| 965 | if (bd_config->mac_age == 0) |
| 966 | continue; |
| 967 | |
| 968 | delta = (u8) (start_time / 60) - result.fields.timestamp; |
| 969 | delta += delta < 0 ? 256 : 0; |
| 970 | |
| 971 | if (delta > bd_config->mac_age) |
| 972 | { |
| 973 | void *p = &key.fields.mac; |
| 974 | l2fib_del_entry (*(u64 *) p, key.fields.bd_index); |
| 975 | } |
| 976 | } |
| 977 | v++; |
| 978 | } |
| 979 | } |
| 980 | last_run_duration = vlib_time_now (vm) - last_run_duration; |
| 981 | } |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | /* *INDENT-OFF* */ |
| 986 | VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = { |
| 987 | .function = l2fib_mac_age_scanner_process, |
| 988 | .type = VLIB_NODE_TYPE_PROCESS, |
| 989 | .name = "l2fib-mac-age-scanner-process", |
| 990 | }; |
| 991 | /* *INDENT-ON* */ |
| 992 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 993 | clib_error_t * |
| 994 | l2fib_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 995 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 996 | l2fib_main_t *mp = &l2fib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 997 | l2fib_entry_key_t test_key; |
| 998 | u8 test_mac[6]; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 999 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1000 | mp->vlib_main = vm; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1001 | mp->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1002 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1003 | /* Create the hash table */ |
| 1004 | BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table", |
| 1005 | L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1006 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1007 | /* verify the key constructor is good, since it is endian-sensitive */ |
| 1008 | memset (test_mac, 0, sizeof (test_mac)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1009 | test_mac[0] = 0x11; |
| 1010 | test_key.raw = 0; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1011 | test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1012 | ASSERT (test_key.fields.mac[0] == 0x11); |
| 1013 | ASSERT (test_key.fields.bd_index == 0x1234); |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | VLIB_INIT_FUNCTION (l2fib_init); |
| 1019 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 1020 | /* |
| 1021 | * fd.io coding-style-patch-verification: ON |
| 1022 | * |
| 1023 | * Local Variables: |
| 1024 | * eval: (c-set-style "gnu") |
| 1025 | * End: |
| 1026 | */ |