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> |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 23 | #include <vpp/stats/stat_segment.h> |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 24 | |
| 25 | void |
| 26 | bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif) |
| 27 | { |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 28 | bond_main_t *bm = &bond_main; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 29 | bond_if_t *bif; |
| 30 | int i; |
| 31 | uword p; |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 32 | u8 switching_active = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 33 | |
| 34 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 35 | clib_spinlock_lock_if_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 36 | vec_foreach_index (i, bif->active_slaves) |
| 37 | { |
| 38 | p = *vec_elt_at_index (bif->active_slaves, i); |
| 39 | if (p == sif->sw_if_index) |
| 40 | { |
Steven Luong | a1876b8 | 2019-08-20 16:58:00 -0700 | [diff] [blame] | 41 | if ((bif->mode == BOND_MODE_ACTIVE_BACKUP) && (i == 0) && |
| 42 | (vec_len (bif->active_slaves) > 1)) |
| 43 | /* deleting the active slave for active-backup */ |
| 44 | switching_active = 1; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 45 | vec_del1 (bif->active_slaves, i); |
Zhiyong Yang | 751e3f3 | 2019-06-26 05:49:14 -0400 | [diff] [blame] | 46 | if (sif->lacp_enabled && bif->numa_only) |
| 47 | { |
| 48 | /* For lacp mode, if we check it is a slave on local numa node, |
| 49 | bif->n_numa_slaves should be decreased by 1 becasue the first |
| 50 | bif->n_numa_slaves are all slaves on local numa node */ |
| 51 | if (i < bif->n_numa_slaves) |
| 52 | { |
| 53 | bif->n_numa_slaves--; |
| 54 | ASSERT (bif->n_numa_slaves >= 0); |
| 55 | } |
| 56 | } |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 57 | break; |
| 58 | } |
| 59 | } |
Zhiyong Yang | 6865d3c | 2019-05-15 04:25:20 -0400 | [diff] [blame] | 60 | |
| 61 | /* We get a new slave just becoming active */ |
Steven Luong | a1876b8 | 2019-08-20 16:58:00 -0700 | [diff] [blame] | 62 | if (switching_active) |
| 63 | vlib_process_signal_event (bm->vlib_main, bond_process_node.index, |
| 64 | BOND_SEND_GARP_NA, bif->hw_if_index); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 65 | clib_spinlock_unlock_if_init (&bif->lockp); |
Zhiyong Yang | 6865d3c | 2019-05-15 04:25:20 -0400 | [diff] [blame] | 66 | |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 67 | if (bif->mode == BOND_MODE_LACP) |
| 68 | stat_segment_set_state_counter (bm->stats[bif->sw_if_index] |
| 69 | [sif->sw_if_index], sif->actor.state); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Steven Luong | a1876b8 | 2019-08-20 16:58:00 -0700 | [diff] [blame] | 72 | /* |
| 73 | * return 1 if s2 is preferred. |
| 74 | * return -1 if s1 is preferred. |
| 75 | */ |
| 76 | static int |
| 77 | bond_slave_sort (void *a1, void *a2) |
| 78 | { |
| 79 | u32 *s1 = a1; |
| 80 | u32 *s2 = a2; |
| 81 | slave_if_t *sif1 = bond_get_slave_by_sw_if_index (*s1); |
| 82 | slave_if_t *sif2 = bond_get_slave_by_sw_if_index (*s2); |
| 83 | bond_if_t *bif; |
| 84 | |
| 85 | ASSERT (sif1); |
| 86 | ASSERT (sif2); |
| 87 | /* |
| 88 | * sort entries according to preference rules: |
| 89 | * 1. biggest weight |
| 90 | * 2. numa-node |
| 91 | * 3. current active slave (to prevent churning) |
| 92 | * 4. lowest sw_if_index (for deterministic behavior) |
| 93 | * |
| 94 | */ |
| 95 | if (sif2->weight > sif1->weight) |
| 96 | return 1; |
| 97 | if (sif2->weight < sif1->weight) |
| 98 | return -1; |
| 99 | else |
| 100 | { |
| 101 | if (sif2->is_local_numa > sif1->is_local_numa) |
| 102 | return 1; |
| 103 | if (sif2->is_local_numa < sif1->is_local_numa) |
| 104 | return -1; |
| 105 | else |
| 106 | { |
| 107 | bif = bond_get_master_by_dev_instance (sif1->bif_dev_instance); |
| 108 | /* Favor the current active slave to avoid churning */ |
| 109 | if (bif->active_slaves[0] == sif2->sw_if_index) |
| 110 | return 1; |
| 111 | if (bif->active_slaves[0] == sif1->sw_if_index) |
| 112 | return -1; |
| 113 | /* go for the tiebreaker as the last resort */ |
| 114 | if (sif1->sw_if_index > sif2->sw_if_index) |
| 115 | return 1; |
| 116 | if (sif1->sw_if_index < sif2->sw_if_index) |
| 117 | return -1; |
| 118 | else |
| 119 | ASSERT (0); |
| 120 | } |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | bond_sort_slaves (bond_if_t * bif) |
| 127 | { |
| 128 | bond_main_t *bm = &bond_main; |
| 129 | u32 old_active = bif->active_slaves[0]; |
| 130 | |
| 131 | vec_sort_with_function (bif->active_slaves, bond_slave_sort); |
| 132 | if (old_active != bif->active_slaves[0]) |
| 133 | vlib_process_signal_event (bm->vlib_main, bond_process_node.index, |
| 134 | BOND_SEND_GARP_NA, bif->hw_if_index); |
| 135 | } |
| 136 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 137 | void |
| 138 | bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif) |
| 139 | { |
| 140 | bond_if_t *bif; |
Steven | 9f781d8 | 2018-06-05 11:09:32 -0700 | [diff] [blame] | 141 | bond_main_t *bm = &bond_main; |
Zhiyong Yang | 6865d3c | 2019-05-15 04:25:20 -0400 | [diff] [blame] | 142 | vnet_main_t *vnm = vnet_get_main (); |
| 143 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index); |
Steven Luong | 002723c | 2019-10-22 21:27:22 -0700 | [diff] [blame] | 144 | int i; |
| 145 | uword p; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 146 | |
| 147 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 148 | clib_spinlock_lock_if_init (&bif->lockp); |
Steven Luong | 002723c | 2019-10-22 21:27:22 -0700 | [diff] [blame] | 149 | vec_foreach_index (i, bif->active_slaves) |
| 150 | { |
| 151 | p = *vec_elt_at_index (bif->active_slaves, i); |
| 152 | if (p == sif->sw_if_index) |
| 153 | goto done; |
| 154 | } |
| 155 | |
| 156 | if (sif->lacp_enabled && bif->numa_only && (vm->numa_node == hw->numa_node)) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 157 | { |
Steven Luong | 002723c | 2019-10-22 21:27:22 -0700 | [diff] [blame] | 158 | vec_insert_elts (bif->active_slaves, &sif->sw_if_index, 1, |
| 159 | bif->n_numa_slaves); |
| 160 | bif->n_numa_slaves++; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 161 | } |
Steven Luong | 002723c | 2019-10-22 21:27:22 -0700 | [diff] [blame] | 162 | else |
| 163 | vec_add1 (bif->active_slaves, sif->sw_if_index); |
| 164 | |
| 165 | sif->is_local_numa = (vm->numa_node == hw->numa_node) ? 1 : 0; |
| 166 | if (bif->mode == BOND_MODE_ACTIVE_BACKUP) |
| 167 | { |
| 168 | if (vec_len (bif->active_slaves) == 1) |
| 169 | /* First slave becomes active? */ |
| 170 | vlib_process_signal_event (bm->vlib_main, bond_process_node.index, |
| 171 | BOND_SEND_GARP_NA, bif->hw_if_index); |
| 172 | else |
| 173 | bond_sort_slaves (bif); |
| 174 | } |
| 175 | |
| 176 | done: |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 177 | clib_spinlock_unlock_if_init (&bif->lockp); |
Zhiyong Yang | 6865d3c | 2019-05-15 04:25:20 -0400 | [diff] [blame] | 178 | |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 179 | if (bif->mode == BOND_MODE_LACP) |
| 180 | stat_segment_set_state_counter (bm->stats[bif->sw_if_index] |
| 181 | [sif->sw_if_index], sif->actor.state); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int |
| 185 | bond_dump_ifs (bond_interface_details_t ** out_bondifs) |
| 186 | { |
| 187 | vnet_main_t *vnm = vnet_get_main (); |
| 188 | bond_main_t *bm = &bond_main; |
| 189 | bond_if_t *bif; |
| 190 | vnet_hw_interface_t *hi; |
| 191 | bond_interface_details_t *r_bondifs = NULL; |
| 192 | bond_interface_details_t *bondif = NULL; |
| 193 | |
| 194 | /* *INDENT-OFF* */ |
| 195 | pool_foreach (bif, bm->interfaces, |
| 196 | vec_add2(r_bondifs, bondif, 1); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 197 | clib_memset (bondif, 0, sizeof (*bondif)); |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 198 | bondif->id = bif->id; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 199 | bondif->sw_if_index = bif->sw_if_index; |
| 200 | hi = vnet_get_hw_interface (vnm, bif->hw_if_index); |
| 201 | clib_memcpy(bondif->interface_name, hi->name, |
| 202 | MIN (ARRAY_LEN (bondif->interface_name) - 1, |
Benoît Ganne | 5c828bc | 2019-09-27 18:08:22 +0200 | [diff] [blame] | 203 | vec_len ((const char *) hi->name))); |
| 204 | /* enforce by memset() above */ |
| 205 | ASSERT(0 == bondif->interface_name[ARRAY_LEN (bondif->interface_name) - 1]); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 206 | bondif->mode = bif->mode; |
| 207 | bondif->lb = bif->lb; |
Zhiyong Yang | 751e3f3 | 2019-06-26 05:49:14 -0400 | [diff] [blame] | 208 | bondif->numa_only = bif->numa_only; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 209 | bondif->active_slaves = vec_len (bif->active_slaves); |
| 210 | bondif->slaves = vec_len (bif->slaves); |
| 211 | ); |
| 212 | /* *INDENT-ON* */ |
| 213 | |
| 214 | *out_bondifs = r_bondifs; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | int |
| 220 | bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs, |
| 221 | u32 bond_sw_if_index) |
| 222 | { |
| 223 | vnet_main_t *vnm = vnet_get_main (); |
| 224 | bond_if_t *bif; |
| 225 | vnet_hw_interface_t *hi; |
| 226 | vnet_sw_interface_t *sw; |
| 227 | slave_interface_details_t *r_slaveifs = NULL; |
| 228 | slave_interface_details_t *slaveif = NULL; |
| 229 | u32 *sw_if_index = NULL; |
| 230 | slave_if_t *sif; |
| 231 | |
| 232 | bif = bond_get_master_by_sw_if_index (bond_sw_if_index); |
| 233 | if (!bif) |
| 234 | return 1; |
| 235 | |
| 236 | vec_foreach (sw_if_index, bif->slaves) |
| 237 | { |
| 238 | vec_add2 (r_slaveifs, slaveif, 1); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 239 | clib_memset (slaveif, 0, sizeof (*slaveif)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 240 | sif = bond_get_slave_by_sw_if_index (*sw_if_index); |
| 241 | if (sif) |
| 242 | { |
| 243 | sw = vnet_get_sw_interface (vnm, sif->sw_if_index); |
| 244 | hi = vnet_get_hw_interface (vnm, sw->hw_if_index); |
| 245 | clib_memcpy (slaveif->interface_name, hi->name, |
| 246 | MIN (ARRAY_LEN (slaveif->interface_name) - 1, |
Benoît Ganne | 5c828bc | 2019-09-27 18:08:22 +0200 | [diff] [blame] | 247 | vec_len ((const char *) hi->name))); |
| 248 | /* enforce by memset() above */ |
| 249 | ASSERT (0 == |
| 250 | slaveif->interface_name[ARRAY_LEN (slaveif->interface_name) - |
| 251 | 1]); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 252 | slaveif->sw_if_index = sif->sw_if_index; |
| 253 | slaveif->is_passive = sif->is_passive; |
| 254 | slaveif->is_long_timeout = sif->is_long_timeout; |
Steven Luong | a1876b8 | 2019-08-20 16:58:00 -0700 | [diff] [blame] | 255 | slaveif->is_local_numa = sif->is_local_numa; |
| 256 | slaveif->weight = sif->weight; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | *out_slaveifs = r_slaveifs; |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif) |
| 266 | { |
| 267 | bond_main_t *bm = &bond_main; |
| 268 | vnet_main_t *vnm = vnet_get_main (); |
| 269 | int i; |
Steven | ce2db6a | 2018-04-23 17:06:24 -0700 | [diff] [blame] | 270 | vnet_hw_interface_t *sif_hw; |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 271 | |
| 272 | sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 273 | |
| 274 | bif->port_number_bitmap = |
| 275 | clib_bitmap_set (bif->port_number_bitmap, |
| 276 | ntohs (sif->actor_admin.port_number) - 1, 0); |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 277 | bm->slave_by_sw_if_index[sif->sw_if_index] = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 278 | vec_free (sif->last_marker_pkt); |
| 279 | vec_free (sif->last_rx_pkt); |
| 280 | vec_foreach_index (i, bif->slaves) |
| 281 | { |
| 282 | uword p = *vec_elt_at_index (bif->slaves, i); |
| 283 | if (p == sif->sw_if_index) |
| 284 | { |
| 285 | vec_del1 (bif->slaves, i); |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | bond_disable_collecting_distributing (vm, sif); |
| 291 | |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 292 | vnet_feature_enable_disable ("device-input", "bond-input", |
Steven Luong | 1a41a35 | 2019-10-09 10:29:47 -0700 | [diff] [blame] | 293 | sif->sw_if_index, 0, 0, 0); |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 294 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 295 | /* Put back the old mac */ |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 296 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 297 | sif->persistent_hw_address); |
| 298 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 299 | if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable) |
| 300 | (*bm->lacp_enable_disable) (vm, bif, sif, 0); |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 301 | |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 302 | if (bif->mode == BOND_MODE_LACP) |
| 303 | stat_segment_deregister_state_counter |
| 304 | (bm->stats[bif->sw_if_index][sif->sw_if_index]); |
| 305 | |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 306 | pool_put (bm->neighbors, sif); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | int |
| 310 | bond_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 311 | { |
| 312 | bond_main_t *bm = &bond_main; |
| 313 | vnet_main_t *vnm = vnet_get_main (); |
| 314 | bond_if_t *bif; |
| 315 | slave_if_t *sif; |
| 316 | vnet_hw_interface_t *hw; |
| 317 | u32 *sif_sw_if_index; |
Benoît Ganne | cc3aac0 | 2019-10-16 15:03:06 +0200 | [diff] [blame] | 318 | u32 *s_list = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 319 | |
| 320 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 321 | if (hw == NULL || bond_dev_class.index != hw->dev_class_index) |
| 322 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 323 | |
| 324 | bif = bond_get_master_by_dev_instance (hw->dev_instance); |
| 325 | |
Benoît Ganne | cc3aac0 | 2019-10-16 15:03:06 +0200 | [diff] [blame] | 326 | vec_append (s_list, bif->slaves); |
| 327 | vec_foreach (sif_sw_if_index, s_list) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 328 | { |
Benoît Ganne | cc3aac0 | 2019-10-16 15:03:06 +0200 | [diff] [blame] | 329 | sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index); |
| 330 | if (sif) |
| 331 | bond_delete_neighbor (vm, bif, sif); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 332 | } |
Benoît Ganne | cc3aac0 | 2019-10-16 15:03:06 +0200 | [diff] [blame] | 333 | vec_free (s_list); |
Steven | 70488ab | 2018-03-28 17:59:00 -0700 | [diff] [blame] | 334 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 335 | /* bring down the interface */ |
| 336 | vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0); |
| 337 | vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0); |
| 338 | |
| 339 | ethernet_delete_interface (vnm, bif->hw_if_index); |
| 340 | |
| 341 | clib_bitmap_free (bif->port_number_bitmap); |
| 342 | hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index); |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 343 | hash_unset (bm->id_used, bif->id); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 344 | clib_memset (bif, 0, sizeof (*bif)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 345 | pool_put (bm->interfaces, bif); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | void |
| 351 | bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args) |
| 352 | { |
| 353 | bond_main_t *bm = &bond_main; |
| 354 | vnet_main_t *vnm = vnet_get_main (); |
| 355 | vnet_sw_interface_t *sw; |
| 356 | bond_if_t *bif; |
| 357 | |
| 358 | if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0) |
| 359 | { |
| 360 | args->rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 361 | args->error = clib_error_return (0, "LACP plugin is not loaded"); |
| 362 | return; |
| 363 | } |
| 364 | if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN) |
| 365 | { |
| 366 | args->rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 367 | args->error = clib_error_return (0, "Invalid mode"); |
| 368 | return; |
| 369 | } |
| 370 | if (args->lb > BOND_LB_L23) |
| 371 | { |
| 372 | args->rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 373 | args->error = clib_error_return (0, "Invalid load-balance"); |
| 374 | return; |
| 375 | } |
| 376 | pool_get (bm->interfaces, bif); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 377 | clib_memset (bif, 0, sizeof (*bif)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 378 | bif->dev_instance = bif - bm->interfaces; |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 379 | bif->id = args->id; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 380 | bif->lb = args->lb; |
| 381 | bif->mode = args->mode; |
| 382 | |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 383 | // Adjust requested interface id |
| 384 | if (bif->id == ~0) |
| 385 | bif->id = bif->dev_instance; |
| 386 | if (hash_get (bm->id_used, bif->id)) |
| 387 | { |
| 388 | args->rv = VNET_API_ERROR_INSTANCE_IN_USE; |
| 389 | pool_put (bm->interfaces, bif); |
| 390 | return; |
| 391 | } |
| 392 | hash_set (bm->id_used, bif->id, 1); |
| 393 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 394 | // Special load-balance mode used for rr and bc |
| 395 | if (bif->mode == BOND_MODE_ROUND_ROBIN) |
| 396 | bif->lb = BOND_LB_RR; |
| 397 | else if (bif->mode == BOND_MODE_BROADCAST) |
| 398 | bif->lb = BOND_LB_BC; |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 399 | else if (bif->mode == BOND_MODE_ACTIVE_BACKUP) |
| 400 | bif->lb = BOND_LB_AB; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 401 | |
| 402 | bif->use_custom_mac = args->hw_addr_set; |
| 403 | if (!args->hw_addr_set) |
| 404 | { |
| 405 | f64 now = vlib_time_now (vm); |
| 406 | u32 rnd; |
| 407 | rnd = (u32) (now * 1e6); |
| 408 | rnd = random_u32 (&rnd); |
| 409 | |
| 410 | memcpy (args->hw_addr + 2, &rnd, sizeof (rnd)); |
| 411 | args->hw_addr[0] = 2; |
| 412 | args->hw_addr[1] = 0xfe; |
| 413 | } |
| 414 | memcpy (bif->hw_address, args->hw_addr, 6); |
| 415 | args->error = ethernet_register_interface |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 416 | (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ , |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 417 | bif->hw_address /* ethernet address */ , |
| 418 | &bif->hw_if_index, 0 /* flag change */ ); |
| 419 | |
| 420 | if (args->error) |
| 421 | { |
| 422 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 423 | hash_unset (bm->id_used, bif->id); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 424 | pool_put (bm->interfaces, bif); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index); |
| 429 | bif->sw_if_index = sw->sw_if_index; |
| 430 | bif->group = bif->sw_if_index; |
Zhiyong Yang | 751e3f3 | 2019-06-26 05:49:14 -0400 | [diff] [blame] | 431 | bif->numa_only = args->numa_only; |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 432 | if (vlib_get_thread_main ()->n_vlib_mains > 1) |
| 433 | clib_spinlock_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 434 | |
| 435 | vnet_hw_interface_set_flags (vnm, bif->hw_if_index, |
| 436 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 437 | |
| 438 | hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance); |
| 439 | |
| 440 | // for return |
| 441 | args->sw_if_index = bif->sw_if_index; |
Mohsin Kazmi | 8c1280f | 2019-07-01 11:08:20 +0200 | [diff] [blame] | 442 | args->rv = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static clib_error_t * |
| 446 | bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 447 | vlib_cli_command_t * cmd) |
| 448 | { |
| 449 | unformat_input_t _line_input, *line_input = &_line_input; |
| 450 | bond_create_if_args_t args = { 0 }; |
| 451 | u8 mode_is_set = 0; |
| 452 | |
| 453 | /* Get a line of input. */ |
| 454 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 455 | return clib_error_return (0, "Missing required arguments."); |
| 456 | |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 457 | args.id = ~0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 458 | args.mode = -1; |
| 459 | args.lb = BOND_LB_L2; |
Mohsin Kazmi | 8c1280f | 2019-07-01 11:08:20 +0200 | [diff] [blame] | 460 | args.rv = -1; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 461 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 462 | { |
| 463 | if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode)) |
| 464 | mode_is_set = 1; |
| 465 | else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR)) |
| 466 | && unformat (line_input, "load-balance %U", |
| 467 | unformat_bond_load_balance, &args.lb)) |
| 468 | ; |
| 469 | else if (unformat (line_input, "hw-addr %U", |
| 470 | unformat_ethernet_address, args.hw_addr)) |
| 471 | args.hw_addr_set = 1; |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 472 | else if (unformat (line_input, "id %u", &args.id)) |
| 473 | ; |
Zhiyong Yang | 751e3f3 | 2019-06-26 05:49:14 -0400 | [diff] [blame] | 474 | else if (unformat (line_input, "numa-only")) |
| 475 | { |
| 476 | if (args.mode == BOND_MODE_LACP) |
| 477 | args.numa_only = 1; |
| 478 | else |
| 479 | return clib_error_return (0, |
| 480 | "Only lacp mode supports numa-only so far!"); |
| 481 | } |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 482 | else |
| 483 | return clib_error_return (0, "unknown input `%U'", |
| 484 | format_unformat_error, input); |
| 485 | } |
| 486 | unformat_free (line_input); |
| 487 | |
| 488 | if (mode_is_set == 0) |
| 489 | return clib_error_return (0, "Missing bond mode"); |
| 490 | |
| 491 | bond_create_if (vm, &args); |
| 492 | |
Mohsin Kazmi | 8c1280f | 2019-07-01 11:08:20 +0200 | [diff] [blame] | 493 | if (!args.rv) |
| 494 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, |
| 495 | vnet_get_main (), args.sw_if_index); |
| 496 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 497 | return args.error; |
| 498 | } |
| 499 | |
| 500 | /* *INDENT-OFF* */ |
| 501 | VLIB_CLI_COMMAND (bond_create_command, static) = { |
| 502 | .path = "create bond", |
| 503 | .short_help = "create bond mode {round-robin | active-backup | broadcast | " |
Zhiyong Yang | a58fec1 | 2019-07-21 21:51:21 -0400 | [diff] [blame] | 504 | "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} [hw-addr <mac-address>] " |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 505 | "[id <if-id>]", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 506 | .function = bond_create_command_fn, |
| 507 | }; |
| 508 | /* *INDENT-ON* */ |
| 509 | |
| 510 | static clib_error_t * |
| 511 | bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 512 | vlib_cli_command_t * cmd) |
| 513 | { |
| 514 | unformat_input_t _line_input, *line_input = &_line_input; |
| 515 | u32 sw_if_index = ~0; |
| 516 | vnet_main_t *vnm = vnet_get_main (); |
| 517 | int rv; |
| 518 | |
| 519 | /* Get a line of input. */ |
| 520 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 521 | return clib_error_return (0, "Missing <interface>"); |
| 522 | |
| 523 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 524 | { |
| 525 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 526 | ; |
| 527 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 528 | vnm, &sw_if_index)) |
| 529 | ; |
| 530 | else |
| 531 | return clib_error_return (0, "unknown input `%U'", |
| 532 | format_unformat_error, input); |
| 533 | } |
| 534 | unformat_free (line_input); |
| 535 | |
| 536 | if (sw_if_index == ~0) |
| 537 | return clib_error_return (0, |
| 538 | "please specify interface name or sw_if_index"); |
| 539 | |
| 540 | rv = bond_delete_if (vm, sw_if_index); |
| 541 | if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX) |
| 542 | return clib_error_return (0, "not a bond interface"); |
| 543 | else if (rv != 0) |
| 544 | return clib_error_return (0, "error on deleting bond interface"); |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | /* *INDENT-OFF* */ |
| 550 | VLIB_CLI_COMMAND (bond_delete__command, static) = |
| 551 | { |
| 552 | .path = "delete bond", |
| 553 | .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}", |
| 554 | .function = bond_delete_command_fn, |
| 555 | }; |
| 556 | /* *INDENT-ON* */ |
| 557 | |
| 558 | void |
| 559 | bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args) |
| 560 | { |
| 561 | bond_main_t *bm = &bond_main; |
| 562 | vnet_main_t *vnm = vnet_get_main (); |
| 563 | bond_if_t *bif; |
| 564 | slave_if_t *sif; |
| 565 | vnet_interface_main_t *im = &vnm->interface_main; |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 566 | vnet_hw_interface_t *bif_hw, *sif_hw; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 567 | vnet_sw_interface_t *sw; |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 568 | u32 thread_index; |
| 569 | u32 sif_if_index; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 570 | |
| 571 | bif = bond_get_master_by_sw_if_index (args->group); |
| 572 | if (!bif) |
| 573 | { |
| 574 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 575 | args->error = clib_error_return (0, "bond interface not found"); |
| 576 | return; |
| 577 | } |
| 578 | // make sure the interface is not already enslaved |
| 579 | if (bond_get_slave_by_sw_if_index (args->slave)) |
| 580 | { |
| 581 | args->rv = VNET_API_ERROR_VALUE_EXIST; |
| 582 | args->error = clib_error_return (0, "interface was already enslaved"); |
| 583 | return; |
| 584 | } |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 585 | sif_hw = vnet_get_sup_hw_interface (vnm, args->slave); |
| 586 | if (sif_hw->dev_class_index == bond_dev_class.index) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 587 | { |
| 588 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 589 | args->error = |
| 590 | clib_error_return (0, "bond interface cannot be enslaved"); |
| 591 | return; |
| 592 | } |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 593 | if (bif->mode == BOND_MODE_LACP) |
| 594 | { |
Benoît Ganne | a03c7d5 | 2019-11-06 14:36:38 +0100 | [diff] [blame] | 595 | u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index, |
| 596 | args->slave, 0); |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 597 | |
| 598 | vec_validate (bm->stats, bif->sw_if_index); |
| 599 | vec_validate (bm->stats[bif->sw_if_index], args->slave); |
| 600 | |
| 601 | args->error = stat_segment_register_state_counter |
| 602 | (name, &bm->stats[bif->sw_if_index][args->slave]); |
| 603 | vec_free (name); |
| 604 | if (args->error != 0) |
| 605 | { |
| 606 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 607 | return; |
| 608 | } |
| 609 | } |
| 610 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 611 | pool_get (bm->neighbors, sif); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 612 | clib_memset (sif, 0, sizeof (*sif)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 613 | sw = pool_elt_at_index (im->sw_interfaces, args->slave); |
Steven Luong | bac326c | 2019-08-05 09:47:58 -0700 | [diff] [blame] | 614 | /* port_enabled is both admin up and hw link up */ |
| 615 | sif->port_enabled = vnet_sw_interface_is_up (vnm, sw->sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 616 | sif->sw_if_index = sw->sw_if_index; |
| 617 | sif->hw_if_index = sw->hw_if_index; |
| 618 | sif->packet_template_index = (u8) ~ 0; |
| 619 | sif->is_passive = args->is_passive; |
| 620 | sif->group = args->group; |
| 621 | sif->bif_dev_instance = bif->dev_instance; |
| 622 | sif->mode = bif->mode; |
| 623 | |
| 624 | sif->is_long_timeout = args->is_long_timeout; |
| 625 | if (args->is_long_timeout) |
| 626 | sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME; |
| 627 | else |
| 628 | sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME; |
| 629 | |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 630 | vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index, |
| 631 | CLIB_CACHE_LINE_BYTES); |
| 632 | /* |
| 633 | * sif - bm->neighbors may be 0 |
| 634 | * Left shift it by 1 bit to distinguish the valid entry that we actually |
| 635 | * store from the null entries |
| 636 | */ |
| 637 | bm->slave_by_sw_if_index[sif->sw_if_index] = |
| 638 | (uword) (((sif - bm->neighbors) << 1) | 1); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 639 | vec_add1 (bif->slaves, sif->sw_if_index); |
| 640 | |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 641 | sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index); |
| 642 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 643 | /* Save the old mac */ |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 644 | memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6); |
| 645 | bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 646 | if (bif->use_custom_mac) |
| 647 | { |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 648 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 649 | bif->hw_address); |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | // bond interface gets the mac address from the first slave |
| 654 | if (vec_len (bif->slaves) == 1) |
| 655 | { |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 656 | memcpy (bif->hw_address, sif_hw->hw_address, 6); |
| 657 | vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index, |
| 658 | sif_hw->hw_address); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 659 | } |
| 660 | else |
| 661 | { |
| 662 | // subsequent slaves gets the mac address of the bond interface |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 663 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 664 | bif->hw_address); |
| 665 | } |
| 666 | } |
| 667 | |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 668 | if (bif_hw->l2_if_count) |
| 669 | { |
| 670 | ethernet_set_flags (vnm, sif_hw->hw_if_index, |
| 671 | ETHERNET_INTERFACE_FLAG_ACCEPT_ALL); |
| 672 | /* ensure all packets go to ethernet-input */ |
| 673 | ethernet_set_rx_redirect (vnm, sif_hw, 1); |
| 674 | } |
| 675 | |
Steven | e43278f | 2019-01-17 15:11:29 -0800 | [diff] [blame] | 676 | if (bif->mode == BOND_MODE_LACP) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 677 | { |
Steven | e43278f | 2019-01-17 15:11:29 -0800 | [diff] [blame] | 678 | if (bm->lacp_enable_disable) |
| 679 | (*bm->lacp_enable_disable) (vm, bif, sif, 1); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 680 | } |
Steven Luong | bac326c | 2019-08-05 09:47:58 -0700 | [diff] [blame] | 681 | else if (sif->port_enabled) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 682 | { |
| 683 | bond_enable_collecting_distributing (vm, sif); |
| 684 | } |
| 685 | |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 686 | vec_foreach_index (thread_index, bm->per_thread_data) |
| 687 | { |
| 688 | bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data, |
| 689 | thread_index); |
| 690 | |
Damjan Marion | 69fdfee | 2018-10-06 14:33:18 +0200 | [diff] [blame] | 691 | vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1, |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 692 | CLIB_CACHE_LINE_BYTES); |
| 693 | |
| 694 | vec_foreach_index (sif_if_index, ptd->per_port_queue) |
| 695 | { |
| 696 | ptd->per_port_queue[sif_if_index].n_buffers = 0; |
| 697 | } |
| 698 | } |
| 699 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 700 | args->rv = vnet_feature_enable_disable ("device-input", "bond-input", |
Steven Luong | 1a41a35 | 2019-10-09 10:29:47 -0700 | [diff] [blame] | 701 | sif->sw_if_index, 1, 0, 0); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 702 | |
| 703 | if (args->rv) |
| 704 | { |
| 705 | args->error = |
| 706 | clib_error_return (0, |
| 707 | "Error encountered on input feature arc enable"); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | static clib_error_t * |
| 712 | enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 713 | vlib_cli_command_t * cmd) |
| 714 | { |
| 715 | bond_enslave_args_t args = { 0 }; |
| 716 | unformat_input_t _line_input, *line_input = &_line_input; |
| 717 | vnet_main_t *vnm = vnet_get_main (); |
| 718 | |
| 719 | /* Get a line of input. */ |
| 720 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 721 | return clib_error_return (0, "Missing required arguments."); |
| 722 | |
| 723 | args.slave = ~0; |
| 724 | args.group = ~0; |
| 725 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 726 | { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 727 | if (unformat (line_input, "%U %U", |
| 728 | unformat_vnet_sw_interface, vnm, &args.group, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 729 | unformat_vnet_sw_interface, vnm, &args.slave)) |
| 730 | ; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 731 | else if (unformat (line_input, "passive")) |
| 732 | args.is_passive = 1; |
| 733 | else if (unformat (line_input, "long-timeout")) |
| 734 | args.is_long_timeout = 1; |
| 735 | else |
| 736 | { |
| 737 | args.error = clib_error_return (0, "unknown input `%U'", |
| 738 | format_unformat_error, input); |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | unformat_free (line_input); |
| 743 | |
| 744 | if (args.error) |
| 745 | return args.error; |
| 746 | if (args.group == ~0) |
| 747 | return clib_error_return (0, "Missing bond interface"); |
| 748 | if (args.slave == ~0) |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 749 | return clib_error_return (0, "please specify valid slave interface name"); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 750 | |
| 751 | bond_enslave (vm, &args); |
| 752 | |
| 753 | return args.error; |
| 754 | } |
| 755 | |
| 756 | /* *INDENT-OFF* */ |
| 757 | VLIB_CLI_COMMAND (enslave_interface_command, static) = { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 758 | .path = "bond add", |
| 759 | .short_help = "bond add <BondEthernetx> <slave-interface> " |
| 760 | "[passive] [long-timeout]", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 761 | .function = enslave_interface_command_fn, |
| 762 | }; |
| 763 | /* *INDENT-ON* */ |
| 764 | |
| 765 | void |
| 766 | bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args) |
| 767 | { |
| 768 | bond_if_t *bif; |
| 769 | slave_if_t *sif; |
| 770 | |
| 771 | sif = bond_get_slave_by_sw_if_index (args->slave); |
| 772 | if (!sif) |
| 773 | { |
| 774 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 775 | args->error = clib_error_return (0, "interface was not enslaved"); |
| 776 | return; |
| 777 | } |
| 778 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
| 779 | bond_delete_neighbor (vm, bif, sif); |
| 780 | } |
| 781 | |
| 782 | static clib_error_t * |
| 783 | detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 784 | vlib_cli_command_t * cmd) |
| 785 | { |
| 786 | bond_detach_slave_args_t args = { 0 }; |
| 787 | unformat_input_t _line_input, *line_input = &_line_input; |
| 788 | vnet_main_t *vnm = vnet_get_main (); |
| 789 | |
| 790 | /* Get a line of input. */ |
| 791 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 792 | return clib_error_return (0, "Missing required arguments."); |
| 793 | |
| 794 | args.slave = ~0; |
| 795 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 796 | { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 797 | if (unformat (line_input, "%U", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 798 | unformat_vnet_sw_interface, vnm, &args.slave)) |
| 799 | ; |
| 800 | else |
| 801 | { |
| 802 | args.error = clib_error_return (0, "unknown input `%U'", |
| 803 | format_unformat_error, input); |
| 804 | break; |
| 805 | } |
| 806 | } |
| 807 | unformat_free (line_input); |
| 808 | |
| 809 | if (args.error) |
| 810 | return args.error; |
| 811 | if (args.slave == ~0) |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 812 | return clib_error_return (0, "please specify valid slave interface name"); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 813 | |
| 814 | bond_detach_slave (vm, &args); |
| 815 | |
| 816 | return args.error; |
| 817 | } |
| 818 | |
| 819 | /* *INDENT-OFF* */ |
| 820 | VLIB_CLI_COMMAND (detach_interface_command, static) = { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 821 | .path = "bond del", |
| 822 | .short_help = "bond del <slave-interface>", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 823 | .function = detach_interface_command_fn, |
| 824 | }; |
| 825 | /* *INDENT-ON* */ |
| 826 | |
| 827 | static void |
| 828 | show_bond (vlib_main_t * vm) |
| 829 | { |
| 830 | bond_main_t *bm = &bond_main; |
| 831 | bond_if_t *bif; |
| 832 | |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 833 | vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 834 | "interface name", "sw_if_index", "mode", |
| 835 | "load balance", "active slaves", "slaves"); |
| 836 | |
| 837 | /* *INDENT-OFF* */ |
| 838 | pool_foreach (bif, bm->interfaces, |
| 839 | ({ |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 840 | vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 841 | format_bond_interface_name, bif->dev_instance, |
| 842 | bif->sw_if_index, format_bond_mode, bif->mode, |
| 843 | format_bond_load_balance, bif->lb, |
| 844 | vec_len (bif->active_slaves), vec_len (bif->slaves)); |
| 845 | })); |
| 846 | /* *INDENT-ON* */ |
| 847 | } |
| 848 | |
| 849 | static void |
| 850 | show_bond_details (vlib_main_t * vm) |
| 851 | { |
| 852 | bond_main_t *bm = &bond_main; |
| 853 | bond_if_t *bif; |
| 854 | u32 *sw_if_index; |
| 855 | |
| 856 | /* *INDENT-OFF* */ |
| 857 | pool_foreach (bif, bm->interfaces, |
| 858 | ({ |
| 859 | vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance); |
| 860 | vlib_cli_output (vm, " mode: %U", |
| 861 | format_bond_mode, bif->mode); |
| 862 | vlib_cli_output (vm, " load balance: %U", |
| 863 | format_bond_load_balance, bif->lb); |
| 864 | if (bif->mode == BOND_MODE_ROUND_ROBIN) |
| 865 | vlib_cli_output (vm, " last xmit slave index: %u", |
| 866 | bif->lb_rr_last_index); |
| 867 | vlib_cli_output (vm, " number of active slaves: %d", |
| 868 | vec_len (bif->active_slaves)); |
| 869 | vec_foreach (sw_if_index, bif->active_slaves) |
| 870 | { |
| 871 | vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, |
| 872 | vnet_get_main (), *sw_if_index); |
Steven Luong | a1876b8 | 2019-08-20 16:58:00 -0700 | [diff] [blame] | 873 | if (bif->mode == BOND_MODE_ACTIVE_BACKUP) |
| 874 | { |
| 875 | slave_if_t *sif = bond_get_slave_by_sw_if_index (*sw_if_index); |
| 876 | if (sif) |
| 877 | vlib_cli_output (vm, " weight: %u, is_local_numa: %u, " |
| 878 | "sw_if_index: %u", sif->weight, |
| 879 | sif->is_local_numa, sif->sw_if_index); |
| 880 | } |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 881 | } |
| 882 | vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves)); |
| 883 | vec_foreach (sw_if_index, bif->slaves) |
| 884 | { |
| 885 | vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, |
| 886 | vnet_get_main (), *sw_if_index); |
| 887 | } |
| 888 | vlib_cli_output (vm, " device instance: %d", bif->dev_instance); |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 889 | vlib_cli_output (vm, " interface id: %d", bif->id); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 890 | vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index); |
| 891 | vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index); |
| 892 | })); |
| 893 | /* *INDENT-ON* */ |
| 894 | } |
| 895 | |
| 896 | static clib_error_t * |
| 897 | show_bond_fn (vlib_main_t * vm, unformat_input_t * input, |
| 898 | vlib_cli_command_t * cmd) |
| 899 | { |
| 900 | u8 details = 0; |
| 901 | |
| 902 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 903 | { |
| 904 | if (unformat (input, "details")) |
| 905 | details = 1; |
| 906 | else |
| 907 | { |
| 908 | return clib_error_return (0, "unknown input `%U'", |
| 909 | format_unformat_error, input); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if (details) |
| 914 | show_bond_details (vm); |
| 915 | else |
| 916 | show_bond (vm); |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | /* *INDENT-OFF* */ |
| 922 | VLIB_CLI_COMMAND (show_bond_command, static) = { |
| 923 | .path = "show bond", |
| 924 | .short_help = "show bond [details]", |
| 925 | .function = show_bond_fn, |
| 926 | }; |
| 927 | /* *INDENT-ON* */ |
| 928 | |
Steven Luong | a1876b8 | 2019-08-20 16:58:00 -0700 | [diff] [blame] | 929 | void |
| 930 | bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args) |
| 931 | { |
| 932 | slave_if_t *sif; |
| 933 | bond_if_t *bif; |
| 934 | vnet_main_t *vnm; |
| 935 | u32 old_weight; |
| 936 | |
| 937 | sif = bond_get_slave_by_sw_if_index (args->sw_if_index); |
| 938 | if (!sif) |
| 939 | { |
| 940 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 941 | args->error = clib_error_return (0, "Interface not enslaved"); |
| 942 | return; |
| 943 | } |
| 944 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
| 945 | if (!bif) |
| 946 | { |
| 947 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 948 | args->error = clib_error_return (0, "bond interface not found"); |
| 949 | return; |
| 950 | } |
| 951 | if (bif->mode != BOND_MODE_ACTIVE_BACKUP) |
| 952 | { |
| 953 | args->rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 954 | args->error = |
| 955 | clib_error_return (0, "Weight valid for active-backup only"); |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | old_weight = sif->weight; |
| 960 | sif->weight = args->weight; |
| 961 | vnm = vnet_get_main (); |
| 962 | /* |
| 963 | * No need to sort the list if the affected slave is not up (not in active |
| 964 | * slave set), active slave count is 1, or the current slave is already the |
| 965 | * primary slave and new weight > old weight. |
| 966 | */ |
| 967 | if (!vnet_sw_interface_is_up (vnm, sif->sw_if_index) || |
| 968 | (vec_len (bif->active_slaves) == 1) || |
| 969 | ((bif->active_slaves[0] == sif->sw_if_index) && |
| 970 | (sif->weight >= old_weight))) |
| 971 | return; |
| 972 | |
| 973 | bond_sort_slaves (bif); |
| 974 | } |
| 975 | |
| 976 | static clib_error_t * |
| 977 | bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input, |
| 978 | vlib_cli_command_t * cmd) |
| 979 | { |
| 980 | bond_set_intf_weight_args_t args = { 0 }; |
| 981 | u32 sw_if_index = (u32) ~ 0; |
| 982 | unformat_input_t _line_input, *line_input = &_line_input; |
| 983 | vnet_main_t *vnm = vnet_get_main (); |
| 984 | u8 weight_enter = 0; |
| 985 | u32 weight = 0; |
| 986 | |
| 987 | /* Get a line of input. */ |
| 988 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 989 | return clib_error_return (0, "Missing required arguments."); |
| 990 | |
| 991 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 992 | { |
| 993 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 994 | ; |
| 995 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm, |
| 996 | &sw_if_index)) |
| 997 | ; |
| 998 | else if (unformat (line_input, "weight %u", &weight)) |
| 999 | weight_enter = 1; |
| 1000 | else |
| 1001 | { |
| 1002 | clib_error_return (0, "unknown input `%U'", format_unformat_error, |
| 1003 | input); |
| 1004 | break; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | unformat_free (line_input); |
| 1009 | if (sw_if_index == (u32) ~ 0) |
| 1010 | { |
| 1011 | args.rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1012 | clib_error_return (0, "Interface name is invalid!"); |
| 1013 | } |
| 1014 | if (weight_enter == 0) |
| 1015 | { |
| 1016 | args.rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 1017 | clib_error_return (0, "weight missing"); |
| 1018 | } |
| 1019 | |
| 1020 | args.sw_if_index = sw_if_index; |
| 1021 | args.weight = weight; |
| 1022 | bond_set_intf_weight (vm, &args); |
| 1023 | |
| 1024 | return args.error; |
| 1025 | } |
| 1026 | |
| 1027 | /* *INDENT-OFF* */ |
| 1028 | VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = { |
| 1029 | .path = "set interface bond", |
| 1030 | .short_help = "set interface bond <interface> | sw_if_index <idx>" |
| 1031 | " weight <value>", |
| 1032 | .function = bond_set_intf_cmd, |
| 1033 | }; |
| 1034 | /* *INDENT-ON* */ |
| 1035 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 1036 | clib_error_t * |
| 1037 | bond_cli_init (vlib_main_t * vm) |
| 1038 | { |
| 1039 | bond_main_t *bm = &bond_main; |
| 1040 | |
| 1041 | bm->vlib_main = vm; |
| 1042 | bm->vnet_main = vnet_get_main (); |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 1043 | 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] | 1044 | vec_validate_aligned (bm->per_thread_data, |
| 1045 | vlib_get_thread_main ()->n_vlib_mains - 1, |
| 1046 | CLIB_CACHE_LINE_BYTES); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | VLIB_INIT_FUNCTION (bond_cli_init); |
| 1052 | |
| 1053 | /* |
| 1054 | * fd.io coding-style-patch-verification: ON |
| 1055 | * |
| 1056 | * Local Variables: |
| 1057 | * eval: (c-set-style "gnu") |
| 1058 | * End: |
| 1059 | */ |