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 | |
Matthew Smith | e83aa45 | 2019-11-14 10:36:02 -0600 | [diff] [blame] | 264 | /* |
| 265 | * Manage secondary mac addresses when attaching/detaching a slave. |
| 266 | * If adding, copies any secondary addresses from master to slave |
| 267 | * If deleting, deletes the master's secondary addresses from the slave |
| 268 | * |
| 269 | */ |
| 270 | static void |
| 271 | bond_slave_add_del_mac_addrs (bond_if_t * bif, u32 sif_sw_if_index, u8 is_add) |
| 272 | { |
| 273 | vnet_main_t *vnm = vnet_get_main (); |
| 274 | ethernet_interface_t *b_ei; |
| 275 | mac_address_t *sec_mac; |
| 276 | vnet_hw_interface_t *s_hwif; |
| 277 | |
| 278 | b_ei = ethernet_get_interface (ðernet_main, bif->hw_if_index); |
| 279 | if (!b_ei || !b_ei->secondary_addrs) |
| 280 | return; |
| 281 | |
| 282 | s_hwif = vnet_get_sup_hw_interface (vnm, sif_sw_if_index); |
| 283 | |
| 284 | vec_foreach (sec_mac, b_ei->secondary_addrs) |
| 285 | vnet_hw_interface_add_del_mac_address (vnm, s_hwif->hw_if_index, |
| 286 | sec_mac->bytes, is_add); |
| 287 | } |
| 288 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 289 | static void |
| 290 | bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif) |
| 291 | { |
| 292 | bond_main_t *bm = &bond_main; |
| 293 | vnet_main_t *vnm = vnet_get_main (); |
| 294 | int i; |
Steven | ce2db6a | 2018-04-23 17:06:24 -0700 | [diff] [blame] | 295 | vnet_hw_interface_t *sif_hw; |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 296 | |
| 297 | sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 298 | |
| 299 | bif->port_number_bitmap = |
| 300 | clib_bitmap_set (bif->port_number_bitmap, |
| 301 | ntohs (sif->actor_admin.port_number) - 1, 0); |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 302 | bm->slave_by_sw_if_index[sif->sw_if_index] = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 303 | vec_free (sif->last_marker_pkt); |
| 304 | vec_free (sif->last_rx_pkt); |
| 305 | vec_foreach_index (i, bif->slaves) |
| 306 | { |
| 307 | uword p = *vec_elt_at_index (bif->slaves, i); |
| 308 | if (p == sif->sw_if_index) |
| 309 | { |
| 310 | vec_del1 (bif->slaves, i); |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | bond_disable_collecting_distributing (vm, sif); |
| 316 | |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 317 | vnet_feature_enable_disable ("device-input", "bond-input", |
Steven Luong | 1a41a35 | 2019-10-09 10:29:47 -0700 | [diff] [blame] | 318 | sif->sw_if_index, 0, 0, 0); |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 319 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 320 | /* Put back the old mac */ |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 321 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 322 | sif->persistent_hw_address); |
| 323 | |
Matthew Smith | e83aa45 | 2019-11-14 10:36:02 -0600 | [diff] [blame] | 324 | /* delete the bond's secondary/virtual mac addrs from the slave */ |
| 325 | bond_slave_add_del_mac_addrs (bif, sif->sw_if_index, 0 /* is_add */ ); |
| 326 | |
| 327 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 328 | if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable) |
| 329 | (*bm->lacp_enable_disable) (vm, bif, sif, 0); |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 330 | |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 331 | if (bif->mode == BOND_MODE_LACP) |
| 332 | stat_segment_deregister_state_counter |
| 333 | (bm->stats[bif->sw_if_index][sif->sw_if_index]); |
| 334 | |
Steven | 5967baf | 2018-09-24 21:09:36 -0700 | [diff] [blame] | 335 | pool_put (bm->neighbors, sif); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | int |
| 339 | bond_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 340 | { |
| 341 | bond_main_t *bm = &bond_main; |
| 342 | vnet_main_t *vnm = vnet_get_main (); |
| 343 | bond_if_t *bif; |
| 344 | slave_if_t *sif; |
| 345 | vnet_hw_interface_t *hw; |
| 346 | u32 *sif_sw_if_index; |
Benoît Ganne | cc3aac0 | 2019-10-16 15:03:06 +0200 | [diff] [blame] | 347 | u32 *s_list = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 348 | |
| 349 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 350 | if (hw == NULL || bond_dev_class.index != hw->dev_class_index) |
| 351 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 352 | |
| 353 | bif = bond_get_master_by_dev_instance (hw->dev_instance); |
| 354 | |
Benoît Ganne | cc3aac0 | 2019-10-16 15:03:06 +0200 | [diff] [blame] | 355 | vec_append (s_list, bif->slaves); |
| 356 | vec_foreach (sif_sw_if_index, s_list) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 357 | { |
Benoît Ganne | cc3aac0 | 2019-10-16 15:03:06 +0200 | [diff] [blame] | 358 | sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index); |
| 359 | if (sif) |
| 360 | bond_delete_neighbor (vm, bif, sif); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 361 | } |
Benoît Ganne | cc3aac0 | 2019-10-16 15:03:06 +0200 | [diff] [blame] | 362 | vec_free (s_list); |
Steven | 70488ab | 2018-03-28 17:59:00 -0700 | [diff] [blame] | 363 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 364 | /* bring down the interface */ |
| 365 | vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0); |
| 366 | vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0); |
| 367 | |
| 368 | ethernet_delete_interface (vnm, bif->hw_if_index); |
| 369 | |
| 370 | clib_bitmap_free (bif->port_number_bitmap); |
| 371 | hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index); |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 372 | hash_unset (bm->id_used, bif->id); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 373 | clib_memset (bif, 0, sizeof (*bif)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 374 | pool_put (bm->interfaces, bif); |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | void |
| 380 | bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args) |
| 381 | { |
| 382 | bond_main_t *bm = &bond_main; |
| 383 | vnet_main_t *vnm = vnet_get_main (); |
| 384 | vnet_sw_interface_t *sw; |
| 385 | bond_if_t *bif; |
| 386 | |
| 387 | if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0) |
| 388 | { |
| 389 | args->rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 390 | args->error = clib_error_return (0, "LACP plugin is not loaded"); |
| 391 | return; |
| 392 | } |
| 393 | if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN) |
| 394 | { |
| 395 | args->rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 396 | args->error = clib_error_return (0, "Invalid mode"); |
| 397 | return; |
| 398 | } |
| 399 | if (args->lb > BOND_LB_L23) |
| 400 | { |
| 401 | args->rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 402 | args->error = clib_error_return (0, "Invalid load-balance"); |
| 403 | return; |
| 404 | } |
| 405 | pool_get (bm->interfaces, bif); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 406 | clib_memset (bif, 0, sizeof (*bif)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 407 | bif->dev_instance = bif - bm->interfaces; |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 408 | bif->id = args->id; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 409 | bif->lb = args->lb; |
| 410 | bif->mode = args->mode; |
| 411 | |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 412 | // Adjust requested interface id |
| 413 | if (bif->id == ~0) |
| 414 | bif->id = bif->dev_instance; |
| 415 | if (hash_get (bm->id_used, bif->id)) |
| 416 | { |
| 417 | args->rv = VNET_API_ERROR_INSTANCE_IN_USE; |
| 418 | pool_put (bm->interfaces, bif); |
| 419 | return; |
| 420 | } |
| 421 | hash_set (bm->id_used, bif->id, 1); |
| 422 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 423 | // Special load-balance mode used for rr and bc |
| 424 | if (bif->mode == BOND_MODE_ROUND_ROBIN) |
| 425 | bif->lb = BOND_LB_RR; |
| 426 | else if (bif->mode == BOND_MODE_BROADCAST) |
| 427 | bif->lb = BOND_LB_BC; |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 428 | else if (bif->mode == BOND_MODE_ACTIVE_BACKUP) |
| 429 | bif->lb = BOND_LB_AB; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 430 | |
| 431 | bif->use_custom_mac = args->hw_addr_set; |
| 432 | if (!args->hw_addr_set) |
| 433 | { |
| 434 | f64 now = vlib_time_now (vm); |
| 435 | u32 rnd; |
| 436 | rnd = (u32) (now * 1e6); |
| 437 | rnd = random_u32 (&rnd); |
| 438 | |
| 439 | memcpy (args->hw_addr + 2, &rnd, sizeof (rnd)); |
| 440 | args->hw_addr[0] = 2; |
| 441 | args->hw_addr[1] = 0xfe; |
| 442 | } |
| 443 | memcpy (bif->hw_address, args->hw_addr, 6); |
| 444 | args->error = ethernet_register_interface |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 445 | (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ , |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 446 | bif->hw_address /* ethernet address */ , |
| 447 | &bif->hw_if_index, 0 /* flag change */ ); |
| 448 | |
| 449 | if (args->error) |
| 450 | { |
| 451 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 452 | hash_unset (bm->id_used, bif->id); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 453 | pool_put (bm->interfaces, bif); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index); |
| 458 | bif->sw_if_index = sw->sw_if_index; |
| 459 | bif->group = bif->sw_if_index; |
Zhiyong Yang | 751e3f3 | 2019-06-26 05:49:14 -0400 | [diff] [blame] | 460 | bif->numa_only = args->numa_only; |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 461 | if (vlib_get_thread_main ()->n_vlib_mains > 1) |
| 462 | clib_spinlock_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 463 | |
| 464 | vnet_hw_interface_set_flags (vnm, bif->hw_if_index, |
| 465 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 466 | |
| 467 | hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance); |
| 468 | |
| 469 | // for return |
| 470 | args->sw_if_index = bif->sw_if_index; |
Mohsin Kazmi | 8c1280f | 2019-07-01 11:08:20 +0200 | [diff] [blame] | 471 | args->rv = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static clib_error_t * |
| 475 | bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 476 | vlib_cli_command_t * cmd) |
| 477 | { |
| 478 | unformat_input_t _line_input, *line_input = &_line_input; |
| 479 | bond_create_if_args_t args = { 0 }; |
| 480 | u8 mode_is_set = 0; |
| 481 | |
| 482 | /* Get a line of input. */ |
| 483 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 484 | return clib_error_return (0, "Missing required arguments."); |
| 485 | |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 486 | args.id = ~0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 487 | args.mode = -1; |
| 488 | args.lb = BOND_LB_L2; |
Mohsin Kazmi | 8c1280f | 2019-07-01 11:08:20 +0200 | [diff] [blame] | 489 | args.rv = -1; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 490 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 491 | { |
| 492 | if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode)) |
| 493 | mode_is_set = 1; |
| 494 | else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR)) |
| 495 | && unformat (line_input, "load-balance %U", |
| 496 | unformat_bond_load_balance, &args.lb)) |
| 497 | ; |
| 498 | else if (unformat (line_input, "hw-addr %U", |
| 499 | unformat_ethernet_address, args.hw_addr)) |
| 500 | args.hw_addr_set = 1; |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 501 | else if (unformat (line_input, "id %u", &args.id)) |
| 502 | ; |
Zhiyong Yang | 751e3f3 | 2019-06-26 05:49:14 -0400 | [diff] [blame] | 503 | else if (unformat (line_input, "numa-only")) |
| 504 | { |
| 505 | if (args.mode == BOND_MODE_LACP) |
| 506 | args.numa_only = 1; |
| 507 | else |
| 508 | return clib_error_return (0, |
| 509 | "Only lacp mode supports numa-only so far!"); |
| 510 | } |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 511 | else |
| 512 | return clib_error_return (0, "unknown input `%U'", |
| 513 | format_unformat_error, input); |
| 514 | } |
| 515 | unformat_free (line_input); |
| 516 | |
| 517 | if (mode_is_set == 0) |
| 518 | return clib_error_return (0, "Missing bond mode"); |
| 519 | |
| 520 | bond_create_if (vm, &args); |
| 521 | |
Mohsin Kazmi | 8c1280f | 2019-07-01 11:08:20 +0200 | [diff] [blame] | 522 | if (!args.rv) |
| 523 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, |
| 524 | vnet_get_main (), args.sw_if_index); |
| 525 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 526 | return args.error; |
| 527 | } |
| 528 | |
| 529 | /* *INDENT-OFF* */ |
| 530 | VLIB_CLI_COMMAND (bond_create_command, static) = { |
| 531 | .path = "create bond", |
| 532 | .short_help = "create bond mode {round-robin | active-backup | broadcast | " |
Zhiyong Yang | a58fec1 | 2019-07-21 21:51:21 -0400 | [diff] [blame] | 533 | "{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] | 534 | "[id <if-id>]", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 535 | .function = bond_create_command_fn, |
| 536 | }; |
| 537 | /* *INDENT-ON* */ |
| 538 | |
| 539 | static clib_error_t * |
| 540 | bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 541 | vlib_cli_command_t * cmd) |
| 542 | { |
| 543 | unformat_input_t _line_input, *line_input = &_line_input; |
| 544 | u32 sw_if_index = ~0; |
| 545 | vnet_main_t *vnm = vnet_get_main (); |
| 546 | int rv; |
| 547 | |
| 548 | /* Get a line of input. */ |
| 549 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 550 | return clib_error_return (0, "Missing <interface>"); |
| 551 | |
| 552 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 553 | { |
| 554 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 555 | ; |
| 556 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 557 | vnm, &sw_if_index)) |
| 558 | ; |
| 559 | else |
| 560 | return clib_error_return (0, "unknown input `%U'", |
| 561 | format_unformat_error, input); |
| 562 | } |
| 563 | unformat_free (line_input); |
| 564 | |
| 565 | if (sw_if_index == ~0) |
| 566 | return clib_error_return (0, |
| 567 | "please specify interface name or sw_if_index"); |
| 568 | |
| 569 | rv = bond_delete_if (vm, sw_if_index); |
| 570 | if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX) |
| 571 | return clib_error_return (0, "not a bond interface"); |
| 572 | else if (rv != 0) |
| 573 | return clib_error_return (0, "error on deleting bond interface"); |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /* *INDENT-OFF* */ |
| 579 | VLIB_CLI_COMMAND (bond_delete__command, static) = |
| 580 | { |
| 581 | .path = "delete bond", |
| 582 | .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}", |
| 583 | .function = bond_delete_command_fn, |
| 584 | }; |
| 585 | /* *INDENT-ON* */ |
| 586 | |
| 587 | void |
| 588 | bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args) |
| 589 | { |
| 590 | bond_main_t *bm = &bond_main; |
| 591 | vnet_main_t *vnm = vnet_get_main (); |
| 592 | bond_if_t *bif; |
| 593 | slave_if_t *sif; |
| 594 | vnet_interface_main_t *im = &vnm->interface_main; |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 595 | vnet_hw_interface_t *bif_hw, *sif_hw; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 596 | vnet_sw_interface_t *sw; |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 597 | u32 thread_index; |
| 598 | u32 sif_if_index; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 599 | |
| 600 | bif = bond_get_master_by_sw_if_index (args->group); |
| 601 | if (!bif) |
| 602 | { |
| 603 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 604 | args->error = clib_error_return (0, "bond interface not found"); |
| 605 | return; |
| 606 | } |
| 607 | // make sure the interface is not already enslaved |
| 608 | if (bond_get_slave_by_sw_if_index (args->slave)) |
| 609 | { |
| 610 | args->rv = VNET_API_ERROR_VALUE_EXIST; |
| 611 | args->error = clib_error_return (0, "interface was already enslaved"); |
| 612 | return; |
| 613 | } |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 614 | sif_hw = vnet_get_sup_hw_interface (vnm, args->slave); |
| 615 | if (sif_hw->dev_class_index == bond_dev_class.index) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 616 | { |
| 617 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 618 | args->error = |
| 619 | clib_error_return (0, "bond interface cannot be enslaved"); |
| 620 | return; |
| 621 | } |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 622 | if (bif->mode == BOND_MODE_LACP) |
| 623 | { |
Benoît Ganne | a03c7d5 | 2019-11-06 14:36:38 +0100 | [diff] [blame] | 624 | u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index, |
| 625 | args->slave, 0); |
Steven Luong | 0f09a82 | 2019-08-07 12:20:22 -0700 | [diff] [blame] | 626 | |
| 627 | vec_validate (bm->stats, bif->sw_if_index); |
| 628 | vec_validate (bm->stats[bif->sw_if_index], args->slave); |
| 629 | |
| 630 | args->error = stat_segment_register_state_counter |
| 631 | (name, &bm->stats[bif->sw_if_index][args->slave]); |
| 632 | vec_free (name); |
| 633 | if (args->error != 0) |
| 634 | { |
| 635 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 636 | return; |
| 637 | } |
| 638 | } |
| 639 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 640 | pool_get (bm->neighbors, sif); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 641 | clib_memset (sif, 0, sizeof (*sif)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 642 | sw = pool_elt_at_index (im->sw_interfaces, args->slave); |
Steven Luong | bac326c | 2019-08-05 09:47:58 -0700 | [diff] [blame] | 643 | /* port_enabled is both admin up and hw link up */ |
| 644 | sif->port_enabled = vnet_sw_interface_is_up (vnm, sw->sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 645 | sif->sw_if_index = sw->sw_if_index; |
| 646 | sif->hw_if_index = sw->hw_if_index; |
| 647 | sif->packet_template_index = (u8) ~ 0; |
| 648 | sif->is_passive = args->is_passive; |
| 649 | sif->group = args->group; |
| 650 | sif->bif_dev_instance = bif->dev_instance; |
| 651 | sif->mode = bif->mode; |
| 652 | |
| 653 | sif->is_long_timeout = args->is_long_timeout; |
| 654 | if (args->is_long_timeout) |
| 655 | sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME; |
| 656 | else |
| 657 | sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME; |
| 658 | |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 659 | vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index, |
| 660 | CLIB_CACHE_LINE_BYTES); |
| 661 | /* |
| 662 | * sif - bm->neighbors may be 0 |
| 663 | * Left shift it by 1 bit to distinguish the valid entry that we actually |
| 664 | * store from the null entries |
| 665 | */ |
| 666 | bm->slave_by_sw_if_index[sif->sw_if_index] = |
| 667 | (uword) (((sif - bm->neighbors) << 1) | 1); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 668 | vec_add1 (bif->slaves, sif->sw_if_index); |
| 669 | |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 670 | sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index); |
| 671 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 672 | /* Save the old mac */ |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 673 | memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6); |
| 674 | bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 675 | if (bif->use_custom_mac) |
| 676 | { |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 677 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 678 | bif->hw_address); |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | // bond interface gets the mac address from the first slave |
| 683 | if (vec_len (bif->slaves) == 1) |
| 684 | { |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 685 | memcpy (bif->hw_address, sif_hw->hw_address, 6); |
| 686 | vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index, |
| 687 | sif_hw->hw_address); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 688 | } |
| 689 | else |
| 690 | { |
| 691 | // subsequent slaves gets the mac address of the bond interface |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 692 | vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 693 | bif->hw_address); |
| 694 | } |
| 695 | } |
| 696 | |
Matthew Smith | e83aa45 | 2019-11-14 10:36:02 -0600 | [diff] [blame] | 697 | /* if there are secondary/virtual mac addrs, propagate to the slave */ |
| 698 | bond_slave_add_del_mac_addrs (bif, sif->sw_if_index, 1 /* is_add */ ); |
| 699 | |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 700 | if (bif_hw->l2_if_count) |
| 701 | { |
| 702 | ethernet_set_flags (vnm, sif_hw->hw_if_index, |
| 703 | ETHERNET_INTERFACE_FLAG_ACCEPT_ALL); |
| 704 | /* ensure all packets go to ethernet-input */ |
| 705 | ethernet_set_rx_redirect (vnm, sif_hw, 1); |
| 706 | } |
| 707 | |
Steven | e43278f | 2019-01-17 15:11:29 -0800 | [diff] [blame] | 708 | if (bif->mode == BOND_MODE_LACP) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 709 | { |
Steven | e43278f | 2019-01-17 15:11:29 -0800 | [diff] [blame] | 710 | if (bm->lacp_enable_disable) |
| 711 | (*bm->lacp_enable_disable) (vm, bif, sif, 1); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 712 | } |
Steven Luong | bac326c | 2019-08-05 09:47:58 -0700 | [diff] [blame] | 713 | else if (sif->port_enabled) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 714 | { |
| 715 | bond_enable_collecting_distributing (vm, sif); |
| 716 | } |
| 717 | |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 718 | vec_foreach_index (thread_index, bm->per_thread_data) |
| 719 | { |
| 720 | bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data, |
| 721 | thread_index); |
| 722 | |
Damjan Marion | 69fdfee | 2018-10-06 14:33:18 +0200 | [diff] [blame] | 723 | vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1, |
Steven | c4e99c5 | 2018-09-27 20:06:26 -0700 | [diff] [blame] | 724 | CLIB_CACHE_LINE_BYTES); |
| 725 | |
| 726 | vec_foreach_index (sif_if_index, ptd->per_port_queue) |
| 727 | { |
| 728 | ptd->per_port_queue[sif_if_index].n_buffers = 0; |
| 729 | } |
| 730 | } |
| 731 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 732 | args->rv = vnet_feature_enable_disable ("device-input", "bond-input", |
Steven Luong | 1a41a35 | 2019-10-09 10:29:47 -0700 | [diff] [blame] | 733 | sif->sw_if_index, 1, 0, 0); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 734 | |
| 735 | if (args->rv) |
| 736 | { |
| 737 | args->error = |
| 738 | clib_error_return (0, |
| 739 | "Error encountered on input feature arc enable"); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | static clib_error_t * |
| 744 | enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 745 | vlib_cli_command_t * cmd) |
| 746 | { |
| 747 | bond_enslave_args_t args = { 0 }; |
| 748 | unformat_input_t _line_input, *line_input = &_line_input; |
| 749 | vnet_main_t *vnm = vnet_get_main (); |
| 750 | |
| 751 | /* Get a line of input. */ |
| 752 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 753 | return clib_error_return (0, "Missing required arguments."); |
| 754 | |
| 755 | args.slave = ~0; |
| 756 | args.group = ~0; |
| 757 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 758 | { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 759 | if (unformat (line_input, "%U %U", |
| 760 | unformat_vnet_sw_interface, vnm, &args.group, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 761 | unformat_vnet_sw_interface, vnm, &args.slave)) |
| 762 | ; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 763 | else if (unformat (line_input, "passive")) |
| 764 | args.is_passive = 1; |
| 765 | else if (unformat (line_input, "long-timeout")) |
| 766 | args.is_long_timeout = 1; |
| 767 | else |
| 768 | { |
| 769 | args.error = clib_error_return (0, "unknown input `%U'", |
| 770 | format_unformat_error, input); |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | unformat_free (line_input); |
| 775 | |
| 776 | if (args.error) |
| 777 | return args.error; |
| 778 | if (args.group == ~0) |
| 779 | return clib_error_return (0, "Missing bond interface"); |
| 780 | if (args.slave == ~0) |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 781 | return clib_error_return (0, "please specify valid slave interface name"); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 782 | |
| 783 | bond_enslave (vm, &args); |
| 784 | |
| 785 | return args.error; |
| 786 | } |
| 787 | |
| 788 | /* *INDENT-OFF* */ |
| 789 | VLIB_CLI_COMMAND (enslave_interface_command, static) = { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 790 | .path = "bond add", |
| 791 | .short_help = "bond add <BondEthernetx> <slave-interface> " |
| 792 | "[passive] [long-timeout]", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 793 | .function = enslave_interface_command_fn, |
| 794 | }; |
| 795 | /* *INDENT-ON* */ |
| 796 | |
| 797 | void |
| 798 | bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args) |
| 799 | { |
| 800 | bond_if_t *bif; |
| 801 | slave_if_t *sif; |
| 802 | |
| 803 | sif = bond_get_slave_by_sw_if_index (args->slave); |
| 804 | if (!sif) |
| 805 | { |
| 806 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 807 | args->error = clib_error_return (0, "interface was not enslaved"); |
| 808 | return; |
| 809 | } |
| 810 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
| 811 | bond_delete_neighbor (vm, bif, sif); |
| 812 | } |
| 813 | |
| 814 | static clib_error_t * |
| 815 | detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 816 | vlib_cli_command_t * cmd) |
| 817 | { |
| 818 | bond_detach_slave_args_t args = { 0 }; |
| 819 | unformat_input_t _line_input, *line_input = &_line_input; |
| 820 | vnet_main_t *vnm = vnet_get_main (); |
| 821 | |
| 822 | /* Get a line of input. */ |
| 823 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 824 | return clib_error_return (0, "Missing required arguments."); |
| 825 | |
| 826 | args.slave = ~0; |
| 827 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 828 | { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 829 | if (unformat (line_input, "%U", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 830 | unformat_vnet_sw_interface, vnm, &args.slave)) |
| 831 | ; |
| 832 | else |
| 833 | { |
| 834 | args.error = clib_error_return (0, "unknown input `%U'", |
| 835 | format_unformat_error, input); |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | unformat_free (line_input); |
| 840 | |
| 841 | if (args.error) |
| 842 | return args.error; |
| 843 | if (args.slave == ~0) |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 844 | return clib_error_return (0, "please specify valid slave interface name"); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 845 | |
| 846 | bond_detach_slave (vm, &args); |
| 847 | |
| 848 | return args.error; |
| 849 | } |
| 850 | |
| 851 | /* *INDENT-OFF* */ |
| 852 | VLIB_CLI_COMMAND (detach_interface_command, static) = { |
Steven | b3caf55 | 2018-03-27 15:04:47 -0700 | [diff] [blame] | 853 | .path = "bond del", |
| 854 | .short_help = "bond del <slave-interface>", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 855 | .function = detach_interface_command_fn, |
| 856 | }; |
| 857 | /* *INDENT-ON* */ |
| 858 | |
| 859 | static void |
| 860 | show_bond (vlib_main_t * vm) |
| 861 | { |
| 862 | bond_main_t *bm = &bond_main; |
| 863 | bond_if_t *bif; |
| 864 | |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 865 | vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 866 | "interface name", "sw_if_index", "mode", |
| 867 | "load balance", "active slaves", "slaves"); |
| 868 | |
| 869 | /* *INDENT-OFF* */ |
| 870 | pool_foreach (bif, bm->interfaces, |
| 871 | ({ |
Steven | 318de5d | 2018-10-06 22:30:50 -0700 | [diff] [blame] | 872 | vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u", |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 873 | format_bond_interface_name, bif->dev_instance, |
| 874 | bif->sw_if_index, format_bond_mode, bif->mode, |
| 875 | format_bond_load_balance, bif->lb, |
| 876 | vec_len (bif->active_slaves), vec_len (bif->slaves)); |
| 877 | })); |
| 878 | /* *INDENT-ON* */ |
| 879 | } |
| 880 | |
| 881 | static void |
| 882 | show_bond_details (vlib_main_t * vm) |
| 883 | { |
| 884 | bond_main_t *bm = &bond_main; |
| 885 | bond_if_t *bif; |
| 886 | u32 *sw_if_index; |
| 887 | |
| 888 | /* *INDENT-OFF* */ |
| 889 | pool_foreach (bif, bm->interfaces, |
| 890 | ({ |
| 891 | vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance); |
| 892 | vlib_cli_output (vm, " mode: %U", |
| 893 | format_bond_mode, bif->mode); |
| 894 | vlib_cli_output (vm, " load balance: %U", |
| 895 | format_bond_load_balance, bif->lb); |
| 896 | if (bif->mode == BOND_MODE_ROUND_ROBIN) |
| 897 | vlib_cli_output (vm, " last xmit slave index: %u", |
| 898 | bif->lb_rr_last_index); |
| 899 | vlib_cli_output (vm, " number of active slaves: %d", |
| 900 | vec_len (bif->active_slaves)); |
| 901 | vec_foreach (sw_if_index, bif->active_slaves) |
| 902 | { |
| 903 | vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, |
| 904 | vnet_get_main (), *sw_if_index); |
Steven Luong | a1876b8 | 2019-08-20 16:58:00 -0700 | [diff] [blame] | 905 | if (bif->mode == BOND_MODE_ACTIVE_BACKUP) |
| 906 | { |
| 907 | slave_if_t *sif = bond_get_slave_by_sw_if_index (*sw_if_index); |
| 908 | if (sif) |
| 909 | vlib_cli_output (vm, " weight: %u, is_local_numa: %u, " |
| 910 | "sw_if_index: %u", sif->weight, |
| 911 | sif->is_local_numa, sif->sw_if_index); |
| 912 | } |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 913 | } |
| 914 | vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves)); |
| 915 | vec_foreach (sw_if_index, bif->slaves) |
| 916 | { |
| 917 | vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, |
| 918 | vnet_get_main (), *sw_if_index); |
| 919 | } |
| 920 | vlib_cli_output (vm, " device instance: %d", bif->dev_instance); |
Alexander Chernavin | ad9d528 | 2018-12-13 09:08:09 -0500 | [diff] [blame] | 921 | vlib_cli_output (vm, " interface id: %d", bif->id); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 922 | vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index); |
| 923 | vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index); |
| 924 | })); |
| 925 | /* *INDENT-ON* */ |
| 926 | } |
| 927 | |
| 928 | static clib_error_t * |
| 929 | show_bond_fn (vlib_main_t * vm, unformat_input_t * input, |
| 930 | vlib_cli_command_t * cmd) |
| 931 | { |
| 932 | u8 details = 0; |
| 933 | |
| 934 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 935 | { |
| 936 | if (unformat (input, "details")) |
| 937 | details = 1; |
| 938 | else |
| 939 | { |
| 940 | return clib_error_return (0, "unknown input `%U'", |
| 941 | format_unformat_error, input); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (details) |
| 946 | show_bond_details (vm); |
| 947 | else |
| 948 | show_bond (vm); |
| 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | /* *INDENT-OFF* */ |
| 954 | VLIB_CLI_COMMAND (show_bond_command, static) = { |
| 955 | .path = "show bond", |
| 956 | .short_help = "show bond [details]", |
| 957 | .function = show_bond_fn, |
| 958 | }; |
| 959 | /* *INDENT-ON* */ |
| 960 | |
Steven Luong | a1876b8 | 2019-08-20 16:58:00 -0700 | [diff] [blame] | 961 | void |
| 962 | bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args) |
| 963 | { |
| 964 | slave_if_t *sif; |
| 965 | bond_if_t *bif; |
| 966 | vnet_main_t *vnm; |
| 967 | u32 old_weight; |
| 968 | |
| 969 | sif = bond_get_slave_by_sw_if_index (args->sw_if_index); |
| 970 | if (!sif) |
| 971 | { |
| 972 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 973 | args->error = clib_error_return (0, "Interface not enslaved"); |
| 974 | return; |
| 975 | } |
| 976 | bif = bond_get_master_by_dev_instance (sif->bif_dev_instance); |
| 977 | if (!bif) |
| 978 | { |
| 979 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 980 | args->error = clib_error_return (0, "bond interface not found"); |
| 981 | return; |
| 982 | } |
| 983 | if (bif->mode != BOND_MODE_ACTIVE_BACKUP) |
| 984 | { |
| 985 | args->rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 986 | args->error = |
| 987 | clib_error_return (0, "Weight valid for active-backup only"); |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | old_weight = sif->weight; |
| 992 | sif->weight = args->weight; |
| 993 | vnm = vnet_get_main (); |
| 994 | /* |
| 995 | * No need to sort the list if the affected slave is not up (not in active |
| 996 | * slave set), active slave count is 1, or the current slave is already the |
| 997 | * primary slave and new weight > old weight. |
| 998 | */ |
| 999 | if (!vnet_sw_interface_is_up (vnm, sif->sw_if_index) || |
| 1000 | (vec_len (bif->active_slaves) == 1) || |
| 1001 | ((bif->active_slaves[0] == sif->sw_if_index) && |
| 1002 | (sif->weight >= old_weight))) |
| 1003 | return; |
| 1004 | |
| 1005 | bond_sort_slaves (bif); |
| 1006 | } |
| 1007 | |
| 1008 | static clib_error_t * |
| 1009 | bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input, |
| 1010 | vlib_cli_command_t * cmd) |
| 1011 | { |
| 1012 | bond_set_intf_weight_args_t args = { 0 }; |
| 1013 | u32 sw_if_index = (u32) ~ 0; |
| 1014 | unformat_input_t _line_input, *line_input = &_line_input; |
| 1015 | vnet_main_t *vnm = vnet_get_main (); |
| 1016 | u8 weight_enter = 0; |
| 1017 | u32 weight = 0; |
| 1018 | |
| 1019 | /* Get a line of input. */ |
| 1020 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 1021 | return clib_error_return (0, "Missing required arguments."); |
| 1022 | |
| 1023 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 1024 | { |
| 1025 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 1026 | ; |
| 1027 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm, |
| 1028 | &sw_if_index)) |
| 1029 | ; |
| 1030 | else if (unformat (line_input, "weight %u", &weight)) |
| 1031 | weight_enter = 1; |
| 1032 | else |
| 1033 | { |
| 1034 | clib_error_return (0, "unknown input `%U'", format_unformat_error, |
| 1035 | input); |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | unformat_free (line_input); |
| 1041 | if (sw_if_index == (u32) ~ 0) |
| 1042 | { |
| 1043 | args.rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1044 | clib_error_return (0, "Interface name is invalid!"); |
| 1045 | } |
| 1046 | if (weight_enter == 0) |
| 1047 | { |
| 1048 | args.rv = VNET_API_ERROR_INVALID_ARGUMENT; |
| 1049 | clib_error_return (0, "weight missing"); |
| 1050 | } |
| 1051 | |
| 1052 | args.sw_if_index = sw_if_index; |
| 1053 | args.weight = weight; |
| 1054 | bond_set_intf_weight (vm, &args); |
| 1055 | |
| 1056 | return args.error; |
| 1057 | } |
| 1058 | |
| 1059 | /* *INDENT-OFF* */ |
| 1060 | VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = { |
| 1061 | .path = "set interface bond", |
| 1062 | .short_help = "set interface bond <interface> | sw_if_index <idx>" |
| 1063 | " weight <value>", |
| 1064 | .function = bond_set_intf_cmd, |
| 1065 | }; |
| 1066 | /* *INDENT-ON* */ |
| 1067 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 1068 | clib_error_t * |
| 1069 | bond_cli_init (vlib_main_t * vm) |
| 1070 | { |
| 1071 | bond_main_t *bm = &bond_main; |
| 1072 | |
| 1073 | bm->vlib_main = vm; |
| 1074 | bm->vnet_main = vnet_get_main (); |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 1075 | 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] | 1076 | vec_validate_aligned (bm->per_thread_data, |
| 1077 | vlib_get_thread_main ()->n_vlib_mains - 1, |
| 1078 | CLIB_CACHE_LINE_BYTES); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | VLIB_INIT_FUNCTION (bond_cli_init); |
| 1084 | |
| 1085 | /* |
| 1086 | * fd.io coding-style-patch-verification: ON |
| 1087 | * |
| 1088 | * Local Variables: |
| 1089 | * eval: (c-set-style "gnu") |
| 1090 | * End: |
| 1091 | */ |