Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <vlib/vlib.h> |
| 20 | #include <vlib/unix/unix.h> |
| 21 | #include <vnet/ethernet/ethernet.h> |
| 22 | #include <vnet/bonding/node.h> |
| 23 | |
| 24 | void |
| 25 | bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif) |
| 26 | { |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 27 | bond_main_t *bm = &bond_main; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 28 | bond_if_t *bif; |
| 29 | int i; |
| 30 | uword p; |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 31 | u8 switching_active = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 32 | |
| 33 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 34 | clib_spinlock_lock_if_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 35 | vec_foreach_index (i, bif->active_slaves) |
| 36 | { |
| 37 | p = *vec_elt_at_index (bif->active_slaves, i); |
| 38 | if (p == sif->sw_if_index) |
| 39 | { |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 40 | /* Are we disabling the very 1st slave? */ |
| 41 | if (sif->sw_if_index == *vec_elt_at_index (bif->active_slaves, 0)) |
| 42 | switching_active = 1; |
| 43 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 44 | vec_del1 (bif->active_slaves, i); |
| 45 | hash_unset (bif->active_slave_by_sw_if_index, sif->sw_if_index); |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 46 | |
| 47 | /* We got a new slave just becoming active? */ |
| 48 | if ((vec_len (bif->active_slaves) >= 1) && |
| 49 | (bif->mode == BOND_MODE_ACTIVE_BACKUP) && switching_active) |
| 50 | vlib_process_signal_event (bm->vlib_main, bond_process_node.index, |
| 51 | BOND_SEND_GARP_NA, bif->hw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 52 | break; |
| 53 | } |
| 54 | } |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 55 | clib_spinlock_unlock_if_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void |
| 59 | bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif) |
| 60 | { |
| 61 | bond_if_t *bif; |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 62 | bond_main_t *bm = &bond_main; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 63 | |
| 64 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 65 | clib_spinlock_lock_if_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 66 | if (!hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index)) |
| 67 | { |
| 68 | hash_set (bif->active_slave_by_sw_if_index, sif->sw_if_index, |
| 69 | sif->sw_if_index); |
| 70 | vec_add1 (bif->active_slaves, sif->sw_if_index); |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 71 | |
| 72 | /* First slave becomes active? */ |
| 73 | if ((vec_len (bif->active_slaves) == 1) && |
| 74 | (bif->mode == BOND_MODE_ACTIVE_BACKUP)) |
| 75 | vlib_process_signal_event (bm->vlib_main, bond_process_node.index, |
| 76 | BOND_SEND_GARP_NA, bif->hw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 77 | } |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 78 | clib_spinlock_unlock_if_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | int |
| 82 | bond_dump_ifs (bond_interface_details_t ** out_bondifs) |
| 83 | { |
| 84 | vnet_main_t *vnm = vnet_get_main (); |
| 85 | bond_main_t *bm = &bond_main; |
| 86 | bond_if_t *bif; |
| 87 | vnet_hw_interface_t *hi; |
| 88 | bond_interface_details_t *r_bondifs = NULL; |
| 89 | bond_interface_details_t *bondif = NULL; |
| 90 | |
| 91 | /* *INDENT-OFF* */ |
| 92 | pool_foreach (bif, bm->interfaces, |
| 93 | vec_add2(r_bondifs, bondif, 1); |
| 94 | memset (bondif, 0, sizeof (*bondif)); |
| 95 | bondif->sw_if_index = bif->sw_if_index; |
| 96 | hi = vnet_get_hw_interface (vnm, bif->hw_if_index); |
| 97 | clib_memcpy(bondif->interface_name, hi->name, |
| 98 | MIN (ARRAY_LEN (bondif->interface_name) - 1, |
| 99 | strlen ((const char *) hi->name))); |
| 100 | bondif->mode = bif->mode; |
| 101 | bondif->lb = bif->lb; |
| 102 | bondif->active_slaves = vec_len (bif->active_slaves); |
| 103 | bondif->slaves = vec_len (bif->slaves); |
| 104 | ); |
| 105 | /* *INDENT-ON* */ |
| 106 | |
| 107 | *out_bondifs = r_bondifs; |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int |
| 113 | bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs, |
| 114 | u32 bond_sw_if_index) |
| 115 | { |
| 116 | vnet_main_t *vnm = vnet_get_main (); |
| 117 | bond_if_t *bif; |
| 118 | vnet_hw_interface_t *hi; |
| 119 | vnet_sw_interface_t *sw; |
| 120 | slave_interface_details_t *r_slaveifs = NULL; |
| 121 | slave_interface_details_t *slaveif = NULL; |
| 122 | u32 *sw_if_index = NULL; |
| 123 | slave_if_t *sif; |
| 124 | |
| 125 | bif = bond_get_master_by_sw_if_index (bond_sw_if_index); |
| 126 | if (!bif) |
| 127 | return 1; |
| 128 | |
| 129 | vec_foreach (sw_if_index, bif->slaves) |
| 130 | { |
| 131 | vec_add2 (r_slaveifs, slaveif, 1); |
| 132 | memset (slaveif, 0, sizeof (*slaveif)); |
| 133 | sif = bond_get_slave_by_sw_if_index (*sw_if_index); |
| 134 | if (sif) |
| 135 | { |
| 136 | sw = vnet_get_sw_interface (vnm, sif->sw_if_index); |
| 137 | hi = vnet_get_hw_interface (vnm, sw->hw_if_index); |
| 138 | clib_memcpy (slaveif->interface_name, hi->name, |
| 139 | MIN (ARRAY_LEN (slaveif->interface_name) - 1, |
| 140 | strlen ((const char *) hi->name))); |
| 141 | slaveif->sw_if_index = sif->sw_if_index; |
| 142 | slaveif->is_passive = sif->is_passive; |
| 143 | slaveif->is_long_timeout = sif->is_long_timeout; |
| 144 | } |
| 145 | } |
| 146 | *out_slaveifs = r_slaveifs; |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif) |
| 153 | { |
| 154 | bond_main_t *bm = &bond_main; |
| 155 | vnet_main_t *vnm = vnet_get_main (); |
| 156 | int i; |
Steven | ce2db6a | 2018-04-23 17:06:24 -0700 | [diff] [blame] | 157 | vnet_hw_interface_t *sif_hw; |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 158 | |
| 159 | sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 160 | |
| 161 | bif->port_number_bitmap = |
| 162 | clib_bitmap_set (bif->port_number_bitmap, |
| 163 | ntohs (sif->actor_admin.port_number) - 1, 0); |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 164 | bm->slave_by_sw_if_index[sif->sw_if_index] = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 165 | vec_free (sif->last_marker_pkt); |
| 166 | vec_free (sif->last_rx_pkt); |
| 167 | vec_foreach_index (i, bif->slaves) |
| 168 | { |
| 169 | uword p = *vec_elt_at_index (bif->slaves, i); |
| 170 | if (p == sif->sw_if_index) |
| 171 | { |
| 172 | vec_del1 (bif->slaves, i); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bond_disable_collecting_distributing (vm, sif); |
| 178 | |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 179 | vnet_feature_enable_disable ("device-input", "bond-input", |
| 180 | sif_hw->hw_if_index, 0, 0, 0); |
| 181 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 182 | /* Put back the old mac */ |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 183 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 184 | sif->persistent_hw_address); |
| 185 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 186 | if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable) |
| 187 | (*bm->lacp_enable_disable) (vm, bif, sif, 0); |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 188 | |
| 189 | pool_put (bm->neighbors, sif); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int |
| 193 | bond_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 194 | { |
| 195 | bond_main_t *bm = &bond_main; |
| 196 | vnet_main_t *vnm = vnet_get_main (); |
| 197 | bond_if_t *bif; |
| 198 | slave_if_t *sif; |
| 199 | vnet_hw_interface_t *hw; |
| 200 | u32 *sif_sw_if_index; |
Steven | 70488ab | 2018-03-28 17:59:00 -0700 | [diff] [blame] | 201 | u32 **s_list = 0; |
| 202 | u32 i; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 203 | |
| 204 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 205 | if (hw == NULL || bond_dev_class.index != hw->dev_class_index) |
| 206 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 207 | |
| 208 | bif = bond_get_master_by_dev_instance (hw->dev_instance); |
| 209 | |
| 210 | vec_foreach (sif_sw_if_index, bif->slaves) |
| 211 | { |
Steven | 70488ab | 2018-03-28 17:59:00 -0700 | [diff] [blame] | 212 | vec_add1 (s_list, sif_sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Steven | 70488ab | 2018-03-28 17:59:00 -0700 | [diff] [blame] | 215 | for (i = 0; i < vec_len (s_list); i++) |
| 216 | { |
| 217 | sif_sw_if_index = s_list[i]; |
| 218 | sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index); |
| 219 | if (sif) |
| 220 | bond_delete_neighbor (vm, bif, sif); |
| 221 | } |
| 222 | |
| 223 | if (s_list) |
| 224 | vec_free (s_list); |
| 225 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 226 | /* bring down the interface */ |
| 227 | vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0); |
| 228 | vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0); |
| 229 | |
| 230 | ethernet_delete_interface (vnm, bif->hw_if_index); |
| 231 | |
| 232 | clib_bitmap_free (bif->port_number_bitmap); |
| 233 | hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index); |
| 234 | memset (bif, 0, sizeof (*bif)); |
| 235 | pool_put (bm->interfaces, bif); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args) |
| 242 | { |
| 243 | bond_main_t *bm = &bond_main; |
| 244 | vnet_main_t *vnm = vnet_get_main (); |
| 245 | vnet_sw_interface_t *sw; |
| 246 | bond_if_t *bif; |
| 247 | |
| 248 | if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0) |
| 249 | { |
| 250 | args->rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 251 | args->error = clib_error_return (0, "LACP plugin is not loaded"); |
| 252 | return; |
| 253 | } |
| 254 | if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN) |
| 255 | { |
| 256 | args->rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 257 | args->error = clib_error_return (0, "Invalid mode"); |
| 258 | return; |
| 259 | } |
| 260 | if (args->lb > BOND_LB_L23) |
| 261 | { |
| 262 | args->rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 263 | args->error = clib_error_return (0, "Invalid load-balance"); |
| 264 | return; |
| 265 | } |
| 266 | pool_get (bm->interfaces, bif); |
| 267 | memset (bif, 0, sizeof (*bif)); |
| 268 | bif->dev_instance = bif - bm->interfaces; |
| 269 | bif->lb = args->lb; |
| 270 | bif->mode = args->mode; |
| 271 | |
| 272 | // Special load-balance mode used for rr and bc |
| 273 | if (bif->mode == BOND_MODE_ROUND_ROBIN) |
| 274 | bif->lb = BOND_LB_RR; |
| 275 | else if (bif->mode == BOND_MODE_BROADCAST) |
| 276 | bif->lb = BOND_LB_BC; |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 277 | else if (bif->mode == BOND_MODE_ACTIVE_BACKUP) |
| 278 | bif->lb = BOND_LB_AB; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 279 | |
| 280 | bif->use_custom_mac = args->hw_addr_set; |
| 281 | if (!args->hw_addr_set) |
| 282 | { |
| 283 | f64 now = vlib_time_now (vm); |
| 284 | u32 rnd; |
| 285 | rnd = (u32) (now * 1e6); |
| 286 | rnd = random_u32 (&rnd); |
| 287 | |
| 288 | memcpy (args->hw_addr + 2, &rnd, sizeof (rnd)); |
| 289 | args->hw_addr[0] = 2; |
| 290 | args->hw_addr[1] = 0xfe; |
| 291 | } |
| 292 | memcpy (bif->hw_address, args->hw_addr, 6); |
| 293 | args->error = ethernet_register_interface |
| 294 | (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ , |
| 295 | bif->hw_address /* ethernet address */ , |
| 296 | &bif->hw_if_index, 0 /* flag change */ ); |
| 297 | |
| 298 | if (args->error) |
| 299 | { |
| 300 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 301 | pool_put (bm->interfaces, bif); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index); |
| 306 | bif->sw_if_index = sw->sw_if_index; |
| 307 | bif->group = bif->sw_if_index; |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 308 | if (vlib_get_thread_main ()->n_vlib_mains > 1) |
| 309 | clib_spinlock_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 310 | |
| 311 | vnet_hw_interface_set_flags (vnm, bif->hw_if_index, |
| 312 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 313 | |
| 314 | hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance); |
| 315 | |
| 316 | // for return |
| 317 | args->sw_if_index = bif->sw_if_index; |
| 318 | } |
| 319 | |
| 320 | static clib_error_t * |
| 321 | bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 322 | vlib_cli_command_t * cmd) |
| 323 | { |
| 324 | unformat_input_t _line_input, *line_input = &_line_input; |
| 325 | bond_create_if_args_t args = { 0 }; |
| 326 | u8 mode_is_set = 0; |
| 327 | |
| 328 | /* Get a line of input. */ |
| 329 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 330 | return clib_error_return (0, "Missing required arguments."); |
| 331 | |
| 332 | args.mode = -1; |
| 333 | args.lb = BOND_LB_L2; |
| 334 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 335 | { |
| 336 | if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode)) |
| 337 | mode_is_set = 1; |
| 338 | else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR)) |
| 339 | && unformat (line_input, "load-balance %U", |
| 340 | unformat_bond_load_balance, &args.lb)) |
| 341 | ; |
| 342 | else if (unformat (line_input, "hw-addr %U", |
| 343 | unformat_ethernet_address, args.hw_addr)) |
| 344 | args.hw_addr_set = 1; |
| 345 | else |
| 346 | return clib_error_return (0, "unknown input `%U'", |
| 347 | format_unformat_error, input); |
| 348 | } |
| 349 | unformat_free (line_input); |
| 350 | |
| 351 | if (mode_is_set == 0) |
| 352 | return clib_error_return (0, "Missing bond mode"); |
| 353 | |
| 354 | bond_create_if (vm, &args); |
| 355 | |
| 356 | return args.error; |
| 357 | } |
| 358 | |
| 359 | /* *INDENT-OFF* */ |
| 360 | VLIB_CLI_COMMAND (bond_create_command, static) = { |
| 361 | .path = "create bond", |
| 362 | .short_help = "create bond mode {round-robin | active-backup | broadcast | " |
| 363 | "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]", |
| 364 | .function = bond_create_command_fn, |
| 365 | }; |
| 366 | /* *INDENT-ON* */ |
| 367 | |
| 368 | static clib_error_t * |
| 369 | bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 370 | vlib_cli_command_t * cmd) |
| 371 | { |
| 372 | unformat_input_t _line_input, *line_input = &_line_input; |
| 373 | u32 sw_if_index = ~0; |
| 374 | vnet_main_t *vnm = vnet_get_main (); |
| 375 | int rv; |
| 376 | |
| 377 | /* Get a line of input. */ |
| 378 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 379 | return clib_error_return (0, "Missing <interface>"); |
| 380 | |
| 381 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 382 | { |
| 383 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 384 | ; |
| 385 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 386 | vnm, &sw_if_index)) |
| 387 | ; |
| 388 | else |
| 389 | return clib_error_return (0, "unknown input `%U'", |
| 390 | format_unformat_error, input); |
| 391 | } |
| 392 | unformat_free (line_input); |
| 393 | |
| 394 | if (sw_if_index == ~0) |
| 395 | return clib_error_return (0, |
| 396 | "please specify interface name or sw_if_index"); |
| 397 | |
| 398 | rv = bond_delete_if (vm, sw_if_index); |
| 399 | if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX) |
| 400 | return clib_error_return (0, "not a bond interface"); |
| 401 | else if (rv != 0) |
| 402 | return clib_error_return (0, "error on deleting bond interface"); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /* *INDENT-OFF* */ |
| 408 | VLIB_CLI_COMMAND (bond_delete__command, static) = |
| 409 | { |
| 410 | .path = "delete bond", |
| 411 | .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}", |
| 412 | .function = bond_delete_command_fn, |
| 413 | }; |
| 414 | /* *INDENT-ON* */ |
| 415 | |
| 416 | void |
| 417 | bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args) |
| 418 | { |
| 419 | bond_main_t *bm = &bond_main; |
| 420 | vnet_main_t *vnm = vnet_get_main (); |
| 421 | bond_if_t *bif; |
| 422 | slave_if_t *sif; |
| 423 | vnet_interface_main_t *im = &vnm->interface_main; |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 424 | vnet_hw_interface_t *bif_hw, *sif_hw; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 425 | vnet_sw_interface_t *sw; |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 426 | u32 thread_index; |
| 427 | u32 sif_if_index; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 428 | |
| 429 | bif = bond_get_master_by_sw_if_index (args->group); |
| 430 | if (!bif) |
| 431 | { |
| 432 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 433 | args->error = clib_error_return (0, "bond interface not found"); |
| 434 | return; |
| 435 | } |
| 436 | // make sure the interface is not already enslaved |
| 437 | if (bond_get_slave_by_sw_if_index (args->slave)) |
| 438 | { |
| 439 | args->rv = VNET_API_ERROR_VALUE_EXIST; |
| 440 | args->error = clib_error_return (0, "interface was already enslaved"); |
| 441 | return; |
| 442 | } |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 443 | sif_hw = vnet_get_sup_hw_interface (vnm, args->slave); |
| 444 | if (sif_hw->dev_class_index == bond_dev_class.index) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 445 | { |
| 446 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 447 | args->error = |
| 448 | clib_error_return (0, "bond interface cannot be enslaved"); |
| 449 | return; |
| 450 | } |
| 451 | pool_get (bm->neighbors, sif); |
| 452 | memset (sif, 0, sizeof (*sif)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 453 | sw = pool_elt_at_index (im->sw_interfaces, args->slave); |
| 454 | sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP; |
| 455 | sif->sw_if_index = sw->sw_if_index; |
| 456 | sif->hw_if_index = sw->hw_if_index; |
| 457 | sif->packet_template_index = (u8) ~ 0; |
| 458 | sif->is_passive = args->is_passive; |
| 459 | sif->group = args->group; |
| 460 | sif->bif_dev_instance = bif->dev_instance; |
| 461 | sif->mode = bif->mode; |
| 462 | |
| 463 | sif->is_long_timeout = args->is_long_timeout; |
| 464 | if (args->is_long_timeout) |
| 465 | sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME; |
| 466 | else |
| 467 | sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME; |
| 468 | |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 469 | vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index, |
| 470 | CLIB_CACHE_LINE_BYTES); |
| 471 | /* |
| 472 | * sif - bm->neighbors may be 0 |
| 473 | * Left shift it by 1 bit to distinguish the valid entry that we actually |
| 474 | * store from the null entries |
| 475 | */ |
| 476 | bm->slave_by_sw_if_index[sif->sw_if_index] = |
| 477 | (uword) (((sif - bm->neighbors) << 1) | 1); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 478 | vec_add1 (bif->slaves, sif->sw_if_index); |
| 479 | |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 480 | sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index); |
| 481 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 482 | /* Save the old mac */ |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 483 | memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6); |
| 484 | bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 485 | if (bif->use_custom_mac) |
| 486 | { |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 487 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 488 | bif->hw_address); |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | // bond interface gets the mac address from the first slave |
| 493 | if (vec_len (bif->slaves) == 1) |
| 494 | { |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 495 | memcpy (bif->hw_address, sif_hw->hw_address, 6); |
| 496 | vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index, |
| 497 | sif_hw->hw_address); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 498 | } |
| 499 | else |
| 500 | { |
| 501 | // subsequent slaves gets the mac address of the bond interface |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 502 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 503 | bif->hw_address); |
| 504 | } |
| 505 | } |
| 506 | |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 507 | if (bif_hw->l2_if_count) |
| 508 | { |
| 509 | ethernet_set_flags (vnm, sif_hw->hw_if_index, |
| 510 | ETHERNET_INTERFACE_FLAG_ACCEPT_ALL); |
| 511 | /* ensure all packets go to ethernet-input */ |
| 512 | ethernet_set_rx_redirect (vnm, sif_hw, 1); |
| 513 | } |
| 514 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 515 | if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable) |
| 516 | { |
| 517 | (*bm->lacp_enable_disable) (vm, bif, sif, 1); |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | bond_enable_collecting_distributing (vm, sif); |
| 522 | } |
| 523 | |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 524 | vec_foreach_index (thread_index, bm->per_thread_data) |
| 525 | { |
| 526 | bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data, |
| 527 | thread_index); |
| 528 | |
Damjan Marion | 69fdfee | 2018-10-06 14:33:18 +0200 | [diff] [blame^] | 529 | vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1, |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 530 | CLIB_CACHE_LINE_BYTES); |
| 531 | |
| 532 | vec_foreach_index (sif_if_index, ptd->per_port_queue) |
| 533 | { |
| 534 | ptd->per_port_queue[sif_if_index].n_buffers = 0; |
| 535 | } |
| 536 | } |
| 537 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 538 | args->rv = vnet_feature_enable_disable ("device-input", "bond-input", |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 539 | sif_hw->hw_if_index, 1, 0, 0); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 540 | |
| 541 | if (args->rv) |
| 542 | { |
| 543 | args->error = |
| 544 | clib_error_return (0, |
| 545 | "Error encountered on input feature arc enable"); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static clib_error_t * |
| 550 | enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 551 | vlib_cli_command_t * cmd) |
| 552 | { |
| 553 | bond_enslave_args_t args = { 0 }; |
| 554 | unformat_input_t _line_input, *line_input = &_line_input; |
| 555 | vnet_main_t *vnm = vnet_get_main (); |
| 556 | |
| 557 | /* Get a line of input. */ |
| 558 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 559 | return clib_error_return (0, "Missing required arguments."); |
| 560 | |
| 561 | args.slave = ~0; |
| 562 | args.group = ~0; |
| 563 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 564 | { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 565 | if (unformat (line_input, "%U %U", |
| 566 | unformat_vnet_sw_interface, vnm, &args.group, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 567 | unformat_vnet_sw_interface, vnm, &args.slave)) |
| 568 | ; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 569 | else if (unformat (line_input, "passive")) |
| 570 | args.is_passive = 1; |
| 571 | else if (unformat (line_input, "long-timeout")) |
| 572 | args.is_long_timeout = 1; |
| 573 | else |
| 574 | { |
| 575 | args.error = clib_error_return (0, "unknown input `%U'", |
| 576 | format_unformat_error, input); |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | unformat_free (line_input); |
| 581 | |
| 582 | if (args.error) |
| 583 | return args.error; |
| 584 | if (args.group == ~0) |
| 585 | return clib_error_return (0, "Missing bond interface"); |
| 586 | if (args.slave == ~0) |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 587 | return clib_error_return (0, "please specify valid slave interface name"); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 588 | |
| 589 | bond_enslave (vm, &args); |
| 590 | |
| 591 | return args.error; |
| 592 | } |
| 593 | |
| 594 | /* *INDENT-OFF* */ |
| 595 | VLIB_CLI_COMMAND (enslave_interface_command, static) = { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 596 | .path = "bond add", |
| 597 | .short_help = "bond add <BondEthernetx> <slave-interface> " |
| 598 | "[passive] [long-timeout]", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 599 | .function = enslave_interface_command_fn, |
| 600 | }; |
| 601 | /* *INDENT-ON* */ |
| 602 | |
| 603 | void |
| 604 | bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args) |
| 605 | { |
| 606 | bond_if_t *bif; |
| 607 | slave_if_t *sif; |
| 608 | |
| 609 | sif = bond_get_slave_by_sw_if_index (args->slave); |
| 610 | if (!sif) |
| 611 | { |
| 612 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 613 | args->error = clib_error_return (0, "interface was not enslaved"); |
| 614 | return; |
| 615 | } |
| 616 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
| 617 | bond_delete_neighbor (vm, bif, sif); |
| 618 | } |
| 619 | |
| 620 | static clib_error_t * |
| 621 | detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 622 | vlib_cli_command_t * cmd) |
| 623 | { |
| 624 | bond_detach_slave_args_t args = { 0 }; |
| 625 | unformat_input_t _line_input, *line_input = &_line_input; |
| 626 | vnet_main_t *vnm = vnet_get_main (); |
| 627 | |
| 628 | /* Get a line of input. */ |
| 629 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 630 | return clib_error_return (0, "Missing required arguments."); |
| 631 | |
| 632 | args.slave = ~0; |
| 633 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 634 | { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 635 | if (unformat (line_input, "%U", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 636 | unformat_vnet_sw_interface, vnm, &args.slave)) |
| 637 | ; |
| 638 | else |
| 639 | { |
| 640 | args.error = clib_error_return (0, "unknown input `%U'", |
| 641 | format_unformat_error, input); |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | unformat_free (line_input); |
| 646 | |
| 647 | if (args.error) |
| 648 | return args.error; |
| 649 | if (args.slave == ~0) |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 650 | return clib_error_return (0, "please specify valid slave interface name"); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 651 | |
| 652 | bond_detach_slave (vm, &args); |
| 653 | |
| 654 | return args.error; |
| 655 | } |
| 656 | |
| 657 | /* *INDENT-OFF* */ |
| 658 | VLIB_CLI_COMMAND (detach_interface_command, static) = { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 659 | .path = "bond del", |
| 660 | .short_help = "bond del <slave-interface>", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 661 | .function = detach_interface_command_fn, |
| 662 | }; |
| 663 | /* *INDENT-ON* */ |
| 664 | |
| 665 | static void |
| 666 | show_bond (vlib_main_t * vm) |
| 667 | { |
| 668 | bond_main_t *bm = &bond_main; |
| 669 | bond_if_t *bif; |
| 670 | |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 671 | vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 672 | "interface name", "sw_if_index", "mode", |
| 673 | "load balance", "active slaves", "slaves"); |
| 674 | |
| 675 | /* *INDENT-OFF* */ |
| 676 | pool_foreach (bif, bm->interfaces, |
| 677 | ({ |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 678 | vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 679 | format_bond_interface_name, bif->dev_instance, |
| 680 | bif->sw_if_index, format_bond_mode, bif->mode, |
| 681 | format_bond_load_balance, bif->lb, |
| 682 | vec_len (bif->active_slaves), vec_len (bif->slaves)); |
| 683 | })); |
| 684 | /* *INDENT-ON* */ |
| 685 | } |
| 686 | |
| 687 | static void |
| 688 | show_bond_details (vlib_main_t * vm) |
| 689 | { |
| 690 | bond_main_t *bm = &bond_main; |
| 691 | bond_if_t *bif; |
| 692 | u32 *sw_if_index; |
| 693 | |
| 694 | /* *INDENT-OFF* */ |
| 695 | pool_foreach (bif, bm->interfaces, |
| 696 | ({ |
| 697 | vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance); |
| 698 | vlib_cli_output (vm, " mode: %U", |
| 699 | format_bond_mode, bif->mode); |
| 700 | vlib_cli_output (vm, " load balance: %U", |
| 701 | format_bond_load_balance, bif->lb); |
| 702 | if (bif->mode == BOND_MODE_ROUND_ROBIN) |
| 703 | vlib_cli_output (vm, " last xmit slave index: %u", |
| 704 | bif->lb_rr_last_index); |
| 705 | vlib_cli_output (vm, " number of active slaves: %d", |
| 706 | vec_len (bif->active_slaves)); |
| 707 | vec_foreach (sw_if_index, bif->active_slaves) |
| 708 | { |
| 709 | vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, |
| 710 | vnet_get_main (), *sw_if_index); |
| 711 | } |
| 712 | vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves)); |
| 713 | vec_foreach (sw_if_index, bif->slaves) |
| 714 | { |
| 715 | vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, |
| 716 | vnet_get_main (), *sw_if_index); |
| 717 | } |
| 718 | vlib_cli_output (vm, " device instance: %d", bif->dev_instance); |
| 719 | vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index); |
| 720 | vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index); |
| 721 | })); |
| 722 | /* *INDENT-ON* */ |
| 723 | } |
| 724 | |
| 725 | static clib_error_t * |
| 726 | show_bond_fn (vlib_main_t * vm, unformat_input_t * input, |
| 727 | vlib_cli_command_t * cmd) |
| 728 | { |
| 729 | u8 details = 0; |
| 730 | |
| 731 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 732 | { |
| 733 | if (unformat (input, "details")) |
| 734 | details = 1; |
| 735 | else |
| 736 | { |
| 737 | return clib_error_return (0, "unknown input `%U'", |
| 738 | format_unformat_error, input); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | if (details) |
| 743 | show_bond_details (vm); |
| 744 | else |
| 745 | show_bond (vm); |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | /* *INDENT-OFF* */ |
| 751 | VLIB_CLI_COMMAND (show_bond_command, static) = { |
| 752 | .path = "show bond", |
| 753 | .short_help = "show bond [details]", |
| 754 | .function = show_bond_fn, |
| 755 | }; |
| 756 | /* *INDENT-ON* */ |
| 757 | |
| 758 | clib_error_t * |
| 759 | bond_cli_init (vlib_main_t * vm) |
| 760 | { |
| 761 | bond_main_t *bm = &bond_main; |
| 762 | |
| 763 | bm->vlib_main = vm; |
| 764 | bm->vnet_main = vnet_get_main (); |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 765 | vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES); |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 766 | vec_validate_aligned (bm->per_thread_data, |
| 767 | vlib_get_thread_main ()->n_vlib_mains - 1, |
| 768 | CLIB_CACHE_LINE_BYTES); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | VLIB_INIT_FUNCTION (bond_cli_init); |
| 774 | |
| 775 | /* |
| 776 | * fd.io coding-style-patch-verification: ON |
| 777 | * |
| 778 | * Local Variables: |
| 779 | * eval: (c-set-style "gnu") |
| 780 | * End: |
| 781 | */ |