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