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