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