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