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