blob: a24d11044869980229c3e7430e90308ff83ac965 [file] [log] [blame]
Steven9cd2d7a2017-12-20 12:43:01 -08001/*
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 Luong0f09a822019-08-07 12:20:22 -070023#include <vpp/stats/stat_segment.h>
Steven9cd2d7a2017-12-20 12:43:01 -080024
25void
Steven Luong4c4223e2020-07-15 08:44:54 -070026bond_disable_collecting_distributing (vlib_main_t * vm, member_if_t * mif)
Steven9cd2d7a2017-12-20 12:43:01 -080027{
Steven9f781d82018-06-05 11:09:32 -070028 bond_main_t *bm = &bond_main;
Steven9cd2d7a2017-12-20 12:43:01 -080029 bond_if_t *bif;
30 int i;
31 uword p;
Steven9f781d82018-06-05 11:09:32 -070032 u8 switching_active = 0;
Steven9cd2d7a2017-12-20 12:43:01 -080033
Steven Luong4c4223e2020-07-15 08:44:54 -070034 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
Stevena005e7f2018-03-22 17:46:58 -070035 clib_spinlock_lock_if_init (&bif->lockp);
Steven Luong4c4223e2020-07-15 08:44:54 -070036 vec_foreach_index (i, bif->active_members)
Steven9cd2d7a2017-12-20 12:43:01 -080037 {
Steven Luong4c4223e2020-07-15 08:44:54 -070038 p = *vec_elt_at_index (bif->active_members, i);
39 if (p == mif->sw_if_index)
Steven9cd2d7a2017-12-20 12:43:01 -080040 {
Steven Luonga1876b82019-08-20 16:58:00 -070041 if ((bif->mode == BOND_MODE_ACTIVE_BACKUP) && (i == 0) &&
Steven Luong4c4223e2020-07-15 08:44:54 -070042 (vec_len (bif->active_members) > 1))
43 /* deleting the active member for active-backup */
Steven Luonga1876b82019-08-20 16:58:00 -070044 switching_active = 1;
Steven Luong4c4223e2020-07-15 08:44:54 -070045 vec_del1 (bif->active_members, i);
46 if (mif->lacp_enabled && bif->numa_only)
Zhiyong Yang751e3f32019-06-26 05:49:14 -040047 {
Steven Luong4c4223e2020-07-15 08:44:54 -070048 /* 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 Yang751e3f32019-06-26 05:49:14 -040052 {
Steven Luong4c4223e2020-07-15 08:44:54 -070053 bif->n_numa_members--;
54 ASSERT (bif->n_numa_members >= 0);
Zhiyong Yang751e3f32019-06-26 05:49:14 -040055 }
56 }
Steven9cd2d7a2017-12-20 12:43:01 -080057 break;
58 }
59 }
Zhiyong Yang6865d3c2019-05-15 04:25:20 -040060
Steven Luong4c4223e2020-07-15 08:44:54 -070061 /* We get a new member just becoming active */
Steven Luonga1876b82019-08-20 16:58:00 -070062 if (switching_active)
63 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
64 BOND_SEND_GARP_NA, bif->hw_if_index);
Stevena005e7f2018-03-22 17:46:58 -070065 clib_spinlock_unlock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -080066}
67
Steven Luonga1876b82019-08-20 16:58:00 -070068/*
69 * return 1 if s2 is preferred.
70 * return -1 if s1 is preferred.
71 */
72static int
Steven Luong4c4223e2020-07-15 08:44:54 -070073bond_member_sort (void *a1, void *a2)
Steven Luonga1876b82019-08-20 16:58:00 -070074{
75 u32 *s1 = a1;
76 u32 *s2 = a2;
Steven Luong4c4223e2020-07-15 08:44:54 -070077 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 Luonga1876b82019-08-20 16:58:00 -070079 bond_if_t *bif;
80
Steven Luong4c4223e2020-07-15 08:44:54 -070081 ALWAYS_ASSERT (mif1);
82 ALWAYS_ASSERT (mif2);
Steven Luonga1876b82019-08-20 16:58:00 -070083 /*
84 * sort entries according to preference rules:
85 * 1. biggest weight
86 * 2. numa-node
Steven Luong4c4223e2020-07-15 08:44:54 -070087 * 3. current active member (to prevent churning)
Steven Luonga1876b82019-08-20 16:58:00 -070088 * 4. lowest sw_if_index (for deterministic behavior)
89 *
90 */
Steven Luong4c4223e2020-07-15 08:44:54 -070091 if (mif2->weight > mif1->weight)
Steven Luonga1876b82019-08-20 16:58:00 -070092 return 1;
Steven Luong4c4223e2020-07-15 08:44:54 -070093 if (mif2->weight < mif1->weight)
Steven Luonga1876b82019-08-20 16:58:00 -070094 return -1;
95 else
96 {
Steven Luong4c4223e2020-07-15 08:44:54 -070097 if (mif2->is_local_numa > mif1->is_local_numa)
Steven Luonga1876b82019-08-20 16:58:00 -070098 return 1;
Steven Luong4c4223e2020-07-15 08:44:54 -070099 if (mif2->is_local_numa < mif1->is_local_numa)
Steven Luonga1876b82019-08-20 16:58:00 -0700100 return -1;
101 else
102 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700103 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 Luonga1876b82019-08-20 16:58:00 -0700106 return 1;
Steven Luong4c4223e2020-07-15 08:44:54 -0700107 if (bif->active_members[0] == mif1->sw_if_index)
Steven Luonga1876b82019-08-20 16:58:00 -0700108 return -1;
109 /* go for the tiebreaker as the last resort */
Steven Luong4c4223e2020-07-15 08:44:54 -0700110 if (mif1->sw_if_index > mif2->sw_if_index)
Steven Luonga1876b82019-08-20 16:58:00 -0700111 return 1;
Steven Luong4c4223e2020-07-15 08:44:54 -0700112 if (mif1->sw_if_index < mif2->sw_if_index)
Steven Luonga1876b82019-08-20 16:58:00 -0700113 return -1;
114 else
115 ASSERT (0);
116 }
117 }
118 return 0;
119}
120
121static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700122bond_sort_members (bond_if_t * bif)
Steven Luonga1876b82019-08-20 16:58:00 -0700123{
124 bond_main_t *bm = &bond_main;
Steven Luong4c4223e2020-07-15 08:44:54 -0700125 u32 old_active = bif->active_members[0];
Steven Luonga1876b82019-08-20 16:58:00 -0700126
Steven Luong4c4223e2020-07-15 08:44:54 -0700127 vec_sort_with_function (bif->active_members, bond_member_sort);
128 if (old_active != bif->active_members[0])
Steven Luonga1876b82019-08-20 16:58:00 -0700129 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
130 BOND_SEND_GARP_NA, bif->hw_if_index);
131}
132
Steven9cd2d7a2017-12-20 12:43:01 -0800133void
Steven Luong4c4223e2020-07-15 08:44:54 -0700134bond_enable_collecting_distributing (vlib_main_t * vm, member_if_t * mif)
Steven9cd2d7a2017-12-20 12:43:01 -0800135{
136 bond_if_t *bif;
Steven9f781d82018-06-05 11:09:32 -0700137 bond_main_t *bm = &bond_main;
Zhiyong Yang6865d3c2019-05-15 04:25:20 -0400138 vnet_main_t *vnm = vnet_get_main ();
Steven Luong4c4223e2020-07-15 08:44:54 -0700139 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
Steven Luong002723c2019-10-22 21:27:22 -0700140 int i;
141 uword p;
Steven9cd2d7a2017-12-20 12:43:01 -0800142
Steven Luong4c4223e2020-07-15 08:44:54 -0700143 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
Stevena005e7f2018-03-22 17:46:58 -0700144 clib_spinlock_lock_if_init (&bif->lockp);
Steven Luong4c4223e2020-07-15 08:44:54 -0700145 vec_foreach_index (i, bif->active_members)
Steven Luong002723c2019-10-22 21:27:22 -0700146 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700147 p = *vec_elt_at_index (bif->active_members, i);
148 if (p == mif->sw_if_index)
Steven Luong002723c2019-10-22 21:27:22 -0700149 goto done;
150 }
151
Steven Luong4c4223e2020-07-15 08:44:54 -0700152 if (mif->lacp_enabled && bif->numa_only && (vm->numa_node == hw->numa_node))
Steven9cd2d7a2017-12-20 12:43:01 -0800153 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700154 vec_insert_elts (bif->active_members, &mif->sw_if_index, 1,
155 bif->n_numa_members);
156 bif->n_numa_members++;
Steven9cd2d7a2017-12-20 12:43:01 -0800157 }
Steven Luong002723c2019-10-22 21:27:22 -0700158 else
Steven Luong4c4223e2020-07-15 08:44:54 -0700159 vec_add1 (bif->active_members, mif->sw_if_index);
Steven Luong002723c2019-10-22 21:27:22 -0700160
Steven Luong4c4223e2020-07-15 08:44:54 -0700161 mif->is_local_numa = (vm->numa_node == hw->numa_node) ? 1 : 0;
Steven Luong002723c2019-10-22 21:27:22 -0700162 if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
163 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700164 if (vec_len (bif->active_members) == 1)
165 /* First member becomes active? */
Steven Luong002723c2019-10-22 21:27:22 -0700166 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
167 BOND_SEND_GARP_NA, bif->hw_if_index);
168 else
Steven Luong4c4223e2020-07-15 08:44:54 -0700169 bond_sort_members (bif);
Steven Luong002723c2019-10-22 21:27:22 -0700170 }
171
172done:
Stevena005e7f2018-03-22 17:46:58 -0700173 clib_spinlock_unlock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -0800174}
175
176int
177bond_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
186 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100187 pool_foreach (bif, bm->interfaces) {
Steven9cd2d7a2017-12-20 12:43:01 -0800188 vec_add2(r_bondifs, bondif, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400189 clib_memset (bondif, 0, sizeof (*bondif));
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500190 bondif->id = bif->id;
Steven9cd2d7a2017-12-20 12:43:01 -0800191 bondif->sw_if_index = bif->sw_if_index;
192 hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
193 clib_memcpy(bondif->interface_name, hi->name,
194 MIN (ARRAY_LEN (bondif->interface_name) - 1,
Benoît Ganne5c828bc2019-09-27 18:08:22 +0200195 vec_len ((const char *) hi->name)));
196 /* enforce by memset() above */
197 ASSERT(0 == bondif->interface_name[ARRAY_LEN (bondif->interface_name) - 1]);
Steven9cd2d7a2017-12-20 12:43:01 -0800198 bondif->mode = bif->mode;
199 bondif->lb = bif->lb;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400200 bondif->numa_only = bif->numa_only;
Steven Luong4c4223e2020-07-15 08:44:54 -0700201 bondif->active_members = vec_len (bif->active_members);
202 bondif->members = vec_len (bif->members);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100203 }
Steven9cd2d7a2017-12-20 12:43:01 -0800204 /* *INDENT-ON* */
205
206 *out_bondifs = r_bondifs;
207
208 return 0;
209}
210
211int
Steven Luong4c4223e2020-07-15 08:44:54 -0700212bond_dump_member_ifs (member_interface_details_t ** out_memberifs,
213 u32 bond_sw_if_index)
Steven9cd2d7a2017-12-20 12:43:01 -0800214{
215 vnet_main_t *vnm = vnet_get_main ();
216 bond_if_t *bif;
217 vnet_hw_interface_t *hi;
218 vnet_sw_interface_t *sw;
Steven Luong4c4223e2020-07-15 08:44:54 -0700219 member_interface_details_t *r_memberifs = NULL;
220 member_interface_details_t *memberif = NULL;
Steven9cd2d7a2017-12-20 12:43:01 -0800221 u32 *sw_if_index = NULL;
Steven Luong4c4223e2020-07-15 08:44:54 -0700222 member_if_t *mif;
Steven9cd2d7a2017-12-20 12:43:01 -0800223
Steven Luong4c4223e2020-07-15 08:44:54 -0700224 bif = bond_get_bond_if_by_sw_if_index (bond_sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800225 if (!bif)
226 return 1;
227
Steven Luong4c4223e2020-07-15 08:44:54 -0700228 vec_foreach (sw_if_index, bif->members)
Steven9cd2d7a2017-12-20 12:43:01 -0800229 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700230 vec_add2 (r_memberifs, memberif, 1);
231 clib_memset (memberif, 0, sizeof (*memberif));
232 mif = bond_get_member_by_sw_if_index (*sw_if_index);
233 if (mif)
Steven9cd2d7a2017-12-20 12:43:01 -0800234 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700235 sw = vnet_get_sw_interface (vnm, mif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800236 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
Steven Luong4c4223e2020-07-15 08:44:54 -0700237 clib_memcpy (memberif->interface_name, hi->name,
238 MIN (ARRAY_LEN (memberif->interface_name) - 1,
Benoît Ganne5c828bc2019-09-27 18:08:22 +0200239 vec_len ((const char *) hi->name)));
240 /* enforce by memset() above */
241 ASSERT (0 ==
Steven Luong4c4223e2020-07-15 08:44:54 -0700242 memberif->interface_name[ARRAY_LEN (memberif->interface_name)
243 - 1]);
244 memberif->sw_if_index = mif->sw_if_index;
245 memberif->is_passive = mif->is_passive;
246 memberif->is_long_timeout = mif->is_long_timeout;
247 memberif->is_local_numa = mif->is_local_numa;
248 memberif->weight = mif->weight;
Steven9cd2d7a2017-12-20 12:43:01 -0800249 }
250 }
Steven Luong4c4223e2020-07-15 08:44:54 -0700251 *out_memberifs = r_memberifs;
Steven9cd2d7a2017-12-20 12:43:01 -0800252
253 return 0;
254}
255
Matthew Smithe83aa452019-11-14 10:36:02 -0600256/*
Steven Luong4c4223e2020-07-15 08:44:54 -0700257 * Manage secondary mac addresses when attaching/detaching a member.
258 * If adding, copy any secondary addresses from bond interface to member.
259 * If deleting, delete the bond interface's secondary addresses from the
260 * member.
Matthew Smithe83aa452019-11-14 10:36:02 -0600261 */
262static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700263bond_member_add_del_mac_addrs (bond_if_t * bif, u32 mif_sw_if_index,
264 u8 is_add)
Matthew Smithe83aa452019-11-14 10:36:02 -0600265{
266 vnet_main_t *vnm = vnet_get_main ();
267 ethernet_interface_t *b_ei;
Benoît Ganneb44c77d2020-10-20 16:24:17 +0200268 ethernet_interface_address_t *sec_mac;
Matthew Smithe83aa452019-11-14 10:36:02 -0600269 vnet_hw_interface_t *s_hwif;
270
271 b_ei = ethernet_get_interface (&ethernet_main, bif->hw_if_index);
272 if (!b_ei || !b_ei->secondary_addrs)
273 return;
274
Steven Luong4c4223e2020-07-15 08:44:54 -0700275 s_hwif = vnet_get_sup_hw_interface (vnm, mif_sw_if_index);
Matthew Smithe83aa452019-11-14 10:36:02 -0600276
277 vec_foreach (sec_mac, b_ei->secondary_addrs)
278 vnet_hw_interface_add_del_mac_address (vnm, s_hwif->hw_if_index,
Benoît Ganneb44c77d2020-10-20 16:24:17 +0200279 sec_mac->mac.bytes, is_add);
Matthew Smithe83aa452019-11-14 10:36:02 -0600280}
281
Steven9cd2d7a2017-12-20 12:43:01 -0800282static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700283bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, member_if_t * mif)
Steven9cd2d7a2017-12-20 12:43:01 -0800284{
285 bond_main_t *bm = &bond_main;
286 vnet_main_t *vnm = vnet_get_main ();
287 int i;
Steven Luong4c4223e2020-07-15 08:44:54 -0700288 vnet_hw_interface_t *mif_hw;
Steven4f8863b2018-04-12 19:36:19 -0700289
Steven Luong4c4223e2020-07-15 08:44:54 -0700290 mif_hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800291
292 bif->port_number_bitmap =
293 clib_bitmap_set (bif->port_number_bitmap,
Steven Luong4c4223e2020-07-15 08:44:54 -0700294 ntohs (mif->actor_admin.port_number) - 1, 0);
295 bm->member_by_sw_if_index[mif->sw_if_index] = 0;
296 vec_free (mif->last_marker_pkt);
297 vec_free (mif->last_rx_pkt);
298 vec_foreach_index (i, bif->members)
Steven9cd2d7a2017-12-20 12:43:01 -0800299 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700300 uword p = *vec_elt_at_index (bif->members, i);
301 if (p == mif->sw_if_index)
Steven9cd2d7a2017-12-20 12:43:01 -0800302 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700303 vec_del1 (bif->members, i);
Steven9cd2d7a2017-12-20 12:43:01 -0800304 break;
305 }
306 }
307
Steven Luong4c4223e2020-07-15 08:44:54 -0700308 bond_disable_collecting_distributing (vm, mif);
Steven9cd2d7a2017-12-20 12:43:01 -0800309
Steven5967baf2018-09-24 21:09:36 -0700310 vnet_feature_enable_disable ("device-input", "bond-input",
Steven Luong4c4223e2020-07-15 08:44:54 -0700311 mif->sw_if_index, 0, 0, 0);
Steven5967baf2018-09-24 21:09:36 -0700312
Steven9cd2d7a2017-12-20 12:43:01 -0800313 /* Put back the old mac */
Steven Luong4c4223e2020-07-15 08:44:54 -0700314 vnet_hw_interface_change_mac_address (vnm, mif_hw->hw_if_index,
315 mif->persistent_hw_address);
Steven9cd2d7a2017-12-20 12:43:01 -0800316
Steven Luong4c4223e2020-07-15 08:44:54 -0700317 /* delete the bond's secondary/virtual mac addrs from the member */
318 bond_member_add_del_mac_addrs (bif, mif->sw_if_index, 0 /* is_add */ );
Matthew Smithe83aa452019-11-14 10:36:02 -0600319
320
Steven9cd2d7a2017-12-20 12:43:01 -0800321 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
Steven Luong4c4223e2020-07-15 08:44:54 -0700322 (*bm->lacp_enable_disable) (vm, bif, mif, 0);
Steven5967baf2018-09-24 21:09:36 -0700323
Steven Luong0f09a822019-08-07 12:20:22 -0700324 if (bif->mode == BOND_MODE_LACP)
Steven Luongaa725782019-11-12 19:45:49 -0800325 {
326 stat_segment_deregister_state_counter
Steven Luong4c4223e2020-07-15 08:44:54 -0700327 (bm->stats[bif->sw_if_index][mif->sw_if_index].actor_state);
Steven Luongaa725782019-11-12 19:45:49 -0800328 stat_segment_deregister_state_counter
Steven Luong4c4223e2020-07-15 08:44:54 -0700329 (bm->stats[bif->sw_if_index][mif->sw_if_index].partner_state);
Steven Luongaa725782019-11-12 19:45:49 -0800330 }
Steven Luong0f09a822019-08-07 12:20:22 -0700331
Steven Luong4c4223e2020-07-15 08:44:54 -0700332 pool_put (bm->neighbors, mif);
Steven9cd2d7a2017-12-20 12:43:01 -0800333}
334
335int
336bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
337{
338 bond_main_t *bm = &bond_main;
339 vnet_main_t *vnm = vnet_get_main ();
340 bond_if_t *bif;
Steven Luong4c4223e2020-07-15 08:44:54 -0700341 member_if_t *mif;
Steven9cd2d7a2017-12-20 12:43:01 -0800342 vnet_hw_interface_t *hw;
Steven Luong4c4223e2020-07-15 08:44:54 -0700343 u32 *mif_sw_if_index;
Benoît Gannecc3aac02019-10-16 15:03:06 +0200344 u32 *s_list = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800345
346 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
347 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
348 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
349
Steven Luong4c4223e2020-07-15 08:44:54 -0700350 bif = bond_get_bond_if_by_dev_instance (hw->dev_instance);
Steven9cd2d7a2017-12-20 12:43:01 -0800351
Steven Luong4c4223e2020-07-15 08:44:54 -0700352 vec_append (s_list, bif->members);
353 vec_foreach (mif_sw_if_index, s_list)
Steven9cd2d7a2017-12-20 12:43:01 -0800354 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700355 mif = bond_get_member_by_sw_if_index (*mif_sw_if_index);
356 if (mif)
357 bond_delete_neighbor (vm, bif, mif);
Steven9cd2d7a2017-12-20 12:43:01 -0800358 }
Benoît Gannecc3aac02019-10-16 15:03:06 +0200359 vec_free (s_list);
Steven70488ab2018-03-28 17:59:00 -0700360
Steven9cd2d7a2017-12-20 12:43:01 -0800361 /* bring down the interface */
362 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
363 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
364
365 ethernet_delete_interface (vnm, bif->hw_if_index);
366
367 clib_bitmap_free (bif->port_number_bitmap);
368 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500369 hash_unset (bm->id_used, bif->id);
Dave Barachb7b92992018-10-17 10:38:51 -0400370 clib_memset (bif, 0, sizeof (*bif));
Steven9cd2d7a2017-12-20 12:43:01 -0800371 pool_put (bm->interfaces, bif);
372
373 return 0;
374}
375
376void
377bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
378{
379 bond_main_t *bm = &bond_main;
380 vnet_main_t *vnm = vnet_get_main ();
381 vnet_sw_interface_t *sw;
382 bond_if_t *bif;
Steven Luong2e1fa542020-01-06 15:14:46 -0800383 vnet_hw_interface_t *hw;
Steven9cd2d7a2017-12-20 12:43:01 -0800384
385 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
386 {
387 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
388 args->error = clib_error_return (0, "LACP plugin is not loaded");
389 return;
390 }
391 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
392 {
393 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
394 args->error = clib_error_return (0, "Invalid mode");
395 return;
396 }
397 if (args->lb > BOND_LB_L23)
398 {
399 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
400 args->error = clib_error_return (0, "Invalid load-balance");
401 return;
402 }
403 pool_get (bm->interfaces, bif);
Dave Barachb7b92992018-10-17 10:38:51 -0400404 clib_memset (bif, 0, sizeof (*bif));
Steven9cd2d7a2017-12-20 12:43:01 -0800405 bif->dev_instance = bif - bm->interfaces;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500406 bif->id = args->id;
Steven9cd2d7a2017-12-20 12:43:01 -0800407 bif->lb = args->lb;
408 bif->mode = args->mode;
Steven Luong2e1fa542020-01-06 15:14:46 -0800409 bif->gso = args->gso;
Steven9cd2d7a2017-12-20 12:43:01 -0800410
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500411 // Adjust requested interface id
412 if (bif->id == ~0)
413 bif->id = bif->dev_instance;
414 if (hash_get (bm->id_used, bif->id))
415 {
416 args->rv = VNET_API_ERROR_INSTANCE_IN_USE;
417 pool_put (bm->interfaces, bif);
418 return;
419 }
420 hash_set (bm->id_used, bif->id, 1);
421
Steven9cd2d7a2017-12-20 12:43:01 -0800422 // Special load-balance mode used for rr and bc
423 if (bif->mode == BOND_MODE_ROUND_ROBIN)
424 bif->lb = BOND_LB_RR;
425 else if (bif->mode == BOND_MODE_BROADCAST)
426 bif->lb = BOND_LB_BC;
Steven318de5d2018-10-06 22:30:50 -0700427 else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
428 bif->lb = BOND_LB_AB;
Steven9cd2d7a2017-12-20 12:43:01 -0800429
430 bif->use_custom_mac = args->hw_addr_set;
431 if (!args->hw_addr_set)
432 {
433 f64 now = vlib_time_now (vm);
434 u32 rnd;
435 rnd = (u32) (now * 1e6);
436 rnd = random_u32 (&rnd);
437
438 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
439 args->hw_addr[0] = 2;
440 args->hw_addr[1] = 0xfe;
441 }
442 memcpy (bif->hw_address, args->hw_addr, 6);
443 args->error = ethernet_register_interface
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500444 (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ ,
Steven9cd2d7a2017-12-20 12:43:01 -0800445 bif->hw_address /* ethernet address */ ,
446 &bif->hw_if_index, 0 /* flag change */ );
447
448 if (args->error)
449 {
450 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500451 hash_unset (bm->id_used, bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800452 pool_put (bm->interfaces, bif);
453 return;
454 }
455
456 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
457 bif->sw_if_index = sw->sw_if_index;
458 bif->group = bif->sw_if_index;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400459 bif->numa_only = args->numa_only;
Steven Luong2e1fa542020-01-06 15:14:46 -0800460
461 hw = vnet_get_hw_interface (vnm, bif->hw_if_index);
Mohsin Kazmi689666c2020-05-12 14:28:13 +0200462 /*
463 * Add GSO and Checksum offload flags if GSO is enabled on Bond
464 */
465 if (args->gso)
466 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100467 hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
468 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
469 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM);
Mohsin Kazmi689666c2020-05-12 14:28:13 +0200470 }
Stevena005e7f2018-03-22 17:46:58 -0700471 if (vlib_get_thread_main ()->n_vlib_mains > 1)
472 clib_spinlock_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -0800473
Matthew Smith72851032020-05-29 12:29:45 -0500474 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
475 VNET_HW_INTERFACE_FLAG_LINK_UP);
476
Steven9cd2d7a2017-12-20 12:43:01 -0800477 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
478
479 // for return
480 args->sw_if_index = bif->sw_if_index;
Mohsin Kazmi8c1280f2019-07-01 11:08:20 +0200481 args->rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800482}
483
484static clib_error_t *
485bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
486 vlib_cli_command_t * cmd)
487{
488 unformat_input_t _line_input, *line_input = &_line_input;
489 bond_create_if_args_t args = { 0 };
490 u8 mode_is_set = 0;
491
492 /* Get a line of input. */
493 if (!unformat_user (input, unformat_line_input, line_input))
494 return clib_error_return (0, "Missing required arguments.");
495
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500496 args.id = ~0;
Steven9cd2d7a2017-12-20 12:43:01 -0800497 args.mode = -1;
498 args.lb = BOND_LB_L2;
Mohsin Kazmi8c1280f2019-07-01 11:08:20 +0200499 args.rv = -1;
Steven9cd2d7a2017-12-20 12:43:01 -0800500 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
501 {
502 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
503 mode_is_set = 1;
504 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
505 && unformat (line_input, "load-balance %U",
506 unformat_bond_load_balance, &args.lb))
507 ;
508 else if (unformat (line_input, "hw-addr %U",
509 unformat_ethernet_address, args.hw_addr))
510 args.hw_addr_set = 1;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500511 else if (unformat (line_input, "id %u", &args.id))
512 ;
Steven Luong2e1fa542020-01-06 15:14:46 -0800513 else if (unformat (line_input, "gso"))
514 args.gso = 1;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400515 else if (unformat (line_input, "numa-only"))
516 {
517 if (args.mode == BOND_MODE_LACP)
518 args.numa_only = 1;
519 else
520 return clib_error_return (0,
521 "Only lacp mode supports numa-only so far!");
522 }
Steven9cd2d7a2017-12-20 12:43:01 -0800523 else
524 return clib_error_return (0, "unknown input `%U'",
525 format_unformat_error, input);
526 }
527 unformat_free (line_input);
528
529 if (mode_is_set == 0)
530 return clib_error_return (0, "Missing bond mode");
531
532 bond_create_if (vm, &args);
533
Mohsin Kazmi8c1280f2019-07-01 11:08:20 +0200534 if (!args.rv)
535 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
536 vnet_get_main (), args.sw_if_index);
537
Steven9cd2d7a2017-12-20 12:43:01 -0800538 return args.error;
539}
540
541/* *INDENT-OFF* */
542VLIB_CLI_COMMAND (bond_create_command, static) = {
543 .path = "create bond",
544 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
Steven Luong2e1fa542020-01-06 15:14:46 -0800545 "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} "
546 "[hw-addr <mac-address>] [id <if-id>] [gso]",
Steven9cd2d7a2017-12-20 12:43:01 -0800547 .function = bond_create_command_fn,
548};
549/* *INDENT-ON* */
550
551static clib_error_t *
552bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
553 vlib_cli_command_t * cmd)
554{
555 unformat_input_t _line_input, *line_input = &_line_input;
556 u32 sw_if_index = ~0;
557 vnet_main_t *vnm = vnet_get_main ();
558 int rv;
559
560 /* Get a line of input. */
561 if (!unformat_user (input, unformat_line_input, line_input))
562 return clib_error_return (0, "Missing <interface>");
563
564 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
565 {
566 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
567 ;
568 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
569 vnm, &sw_if_index))
570 ;
571 else
572 return clib_error_return (0, "unknown input `%U'",
573 format_unformat_error, input);
574 }
575 unformat_free (line_input);
576
577 if (sw_if_index == ~0)
578 return clib_error_return (0,
579 "please specify interface name or sw_if_index");
580
581 rv = bond_delete_if (vm, sw_if_index);
582 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
583 return clib_error_return (0, "not a bond interface");
584 else if (rv != 0)
585 return clib_error_return (0, "error on deleting bond interface");
586
587 return 0;
588}
589
590/* *INDENT-OFF* */
591VLIB_CLI_COMMAND (bond_delete__command, static) =
592{
593 .path = "delete bond",
594 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
595 .function = bond_delete_command_fn,
596};
597/* *INDENT-ON* */
598
599void
Steven Luong4c4223e2020-07-15 08:44:54 -0700600bond_add_member (vlib_main_t * vm, bond_add_member_args_t * args)
Steven9cd2d7a2017-12-20 12:43:01 -0800601{
602 bond_main_t *bm = &bond_main;
603 vnet_main_t *vnm = vnet_get_main ();
604 bond_if_t *bif;
Steven Luong4c4223e2020-07-15 08:44:54 -0700605 member_if_t *mif;
Steven9cd2d7a2017-12-20 12:43:01 -0800606 vnet_interface_main_t *im = &vnm->interface_main;
Steven Luong4c4223e2020-07-15 08:44:54 -0700607 vnet_hw_interface_t *bif_hw, *mif_hw;
Steven9cd2d7a2017-12-20 12:43:01 -0800608 vnet_sw_interface_t *sw;
Stevenc4e99c52018-09-27 20:06:26 -0700609 u32 thread_index;
Steven Luong4c4223e2020-07-15 08:44:54 -0700610 u32 mif_if_index;
Steven9cd2d7a2017-12-20 12:43:01 -0800611
Steven Luong4c4223e2020-07-15 08:44:54 -0700612 bif = bond_get_bond_if_by_sw_if_index (args->group);
Steven9cd2d7a2017-12-20 12:43:01 -0800613 if (!bif)
614 {
615 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
616 args->error = clib_error_return (0, "bond interface not found");
617 return;
618 }
Steven Luong4c4223e2020-07-15 08:44:54 -0700619 // make sure the interface is not already added as member
620 if (bond_get_member_by_sw_if_index (args->member))
Steven9cd2d7a2017-12-20 12:43:01 -0800621 {
622 args->rv = VNET_API_ERROR_VALUE_EXIST;
Steven Luong4c4223e2020-07-15 08:44:54 -0700623 args->error = clib_error_return
624 (0, "interface was already added as member");
Steven9cd2d7a2017-12-20 12:43:01 -0800625 return;
626 }
Steven Luong4c4223e2020-07-15 08:44:54 -0700627 mif_hw = vnet_get_sup_hw_interface (vnm, args->member);
628 if (mif_hw->dev_class_index == bond_dev_class.index)
Steven9cd2d7a2017-12-20 12:43:01 -0800629 {
630 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
631 args->error =
Steven Luong4c4223e2020-07-15 08:44:54 -0700632 clib_error_return (0, "bond interface cannot be added as member");
Steven9cd2d7a2017-12-20 12:43:01 -0800633 return;
634 }
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100635 if (bif->gso && !(mif_hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO))
Steven Luong2e1fa542020-01-06 15:14:46 -0800636 {
637 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
638 args->error =
Steven Luong4c4223e2020-07-15 08:44:54 -0700639 clib_error_return (0, "member interface is not gso capable");
Steven Luong2e1fa542020-01-06 15:14:46 -0800640 return;
641 }
Steven Luong0f09a822019-08-07 12:20:22 -0700642 if (bif->mode == BOND_MODE_LACP)
643 {
Benoît Gannea03c7d52019-11-06 14:36:38 +0100644 u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index,
Steven Luong4c4223e2020-07-15 08:44:54 -0700645 args->member, 0);
Steven Luong0f09a822019-08-07 12:20:22 -0700646
647 vec_validate (bm->stats, bif->sw_if_index);
Steven Luong4c4223e2020-07-15 08:44:54 -0700648 vec_validate (bm->stats[bif->sw_if_index], args->member);
Steven Luong0f09a822019-08-07 12:20:22 -0700649
650 args->error = stat_segment_register_state_counter
Steven Luong4c4223e2020-07-15 08:44:54 -0700651 (name, &bm->stats[bif->sw_if_index][args->member].actor_state);
Steven Luongaa725782019-11-12 19:45:49 -0800652 if (args->error != 0)
653 {
654 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
655 vec_free (name);
656 return;
657 }
658
659 vec_reset_length (name);
660 name = format (0, "/if/lacp/%u/%u/partner-state%c", bif->sw_if_index,
Steven Luong4c4223e2020-07-15 08:44:54 -0700661 args->member, 0);
Steven Luongaa725782019-11-12 19:45:49 -0800662 args->error = stat_segment_register_state_counter
Steven Luong4c4223e2020-07-15 08:44:54 -0700663 (name, &bm->stats[bif->sw_if_index][args->member].partner_state);
Steven Luong0f09a822019-08-07 12:20:22 -0700664 vec_free (name);
665 if (args->error != 0)
666 {
667 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
668 return;
669 }
670 }
671
Steven Luong4c4223e2020-07-15 08:44:54 -0700672 pool_get (bm->neighbors, mif);
673 clib_memset (mif, 0, sizeof (*mif));
674 sw = pool_elt_at_index (im->sw_interfaces, args->member);
Steven Luongbac326c2019-08-05 09:47:58 -0700675 /* port_enabled is both admin up and hw link up */
Steven Luong4c4223e2020-07-15 08:44:54 -0700676 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;
Steven9cd2d7a2017-12-20 12:43:01 -0800684
Steven Luong4c4223e2020-07-15 08:44:54 -0700685 mif->is_long_timeout = args->is_long_timeout;
Steven9cd2d7a2017-12-20 12:43:01 -0800686 if (args->is_long_timeout)
Steven Luong4c4223e2020-07-15 08:44:54 -0700687 mif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
Steven9cd2d7a2017-12-20 12:43:01 -0800688 else
Steven Luong4c4223e2020-07-15 08:44:54 -0700689 mif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
Steven9cd2d7a2017-12-20 12:43:01 -0800690
Steven Luong4c4223e2020-07-15 08:44:54 -0700691 vec_validate_aligned (bm->member_by_sw_if_index, mif->sw_if_index,
Steven0d883012018-05-11 11:06:23 -0700692 CLIB_CACHE_LINE_BYTES);
693 /*
Steven Luong4c4223e2020-07-15 08:44:54 -0700694 * mif - bm->neighbors may be 0
Steven0d883012018-05-11 11:06:23 -0700695 * Left shift it by 1 bit to distinguish the valid entry that we actually
696 * store from the null entries
697 */
Steven Luong4c4223e2020-07-15 08:44:54 -0700698 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);
Steven9cd2d7a2017-12-20 12:43:01 -0800701
Steven Luong4c4223e2020-07-15 08:44:54 -0700702 mif_hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
Steven4f8863b2018-04-12 19:36:19 -0700703
Steven9cd2d7a2017-12-20 12:43:01 -0800704 /* Save the old mac */
Steven Luong4c4223e2020-07-15 08:44:54 -0700705 memcpy (mif->persistent_hw_address, mif_hw->hw_address, 6);
Steven4f8863b2018-04-12 19:36:19 -0700706 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800707 if (bif->use_custom_mac)
708 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700709 vnet_hw_interface_change_mac_address (vnm, mif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800710 bif->hw_address);
711 }
712 else
713 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700714 // bond interface gets the mac address from the first member
715 if (vec_len (bif->members) == 1)
Steven9cd2d7a2017-12-20 12:43:01 -0800716 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700717 memcpy (bif->hw_address, mif_hw->hw_address, 6);
Steven4f8863b2018-04-12 19:36:19 -0700718 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
Steven Luong4c4223e2020-07-15 08:44:54 -0700719 mif_hw->hw_address);
Steven9cd2d7a2017-12-20 12:43:01 -0800720 }
721 else
722 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700723 // subsequent members gets the mac address of the bond interface
724 vnet_hw_interface_change_mac_address (vnm, mif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800725 bif->hw_address);
726 }
727 }
728
Steven Luong4c4223e2020-07-15 08:44:54 -0700729 /* 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 Smithe83aa452019-11-14 10:36:02 -0600731
Steven4f8863b2018-04-12 19:36:19 -0700732 if (bif_hw->l2_if_count)
Steven Luong4c4223e2020-07-15 08:44:54 -0700733 ethernet_set_flags (vnm, mif_hw->hw_if_index,
John Lof415a3b2020-05-14 15:02:16 -0400734 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
735 else
Steven Luong4c4223e2020-07-15 08:44:54 -0700736 ethernet_set_flags (vnm, mif_hw->hw_if_index,
John Lof415a3b2020-05-14 15:02:16 -0400737 /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
Steven4f8863b2018-04-12 19:36:19 -0700738
Stevene43278f2019-01-17 15:11:29 -0800739 if (bif->mode == BOND_MODE_LACP)
Steven9cd2d7a2017-12-20 12:43:01 -0800740 {
Stevene43278f2019-01-17 15:11:29 -0800741 if (bm->lacp_enable_disable)
Steven Luong4c4223e2020-07-15 08:44:54 -0700742 (*bm->lacp_enable_disable) (vm, bif, mif, 1);
Steven9cd2d7a2017-12-20 12:43:01 -0800743 }
Steven Luong4c4223e2020-07-15 08:44:54 -0700744 else if (mif->port_enabled)
Steven9cd2d7a2017-12-20 12:43:01 -0800745 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700746 bond_enable_collecting_distributing (vm, mif);
Steven9cd2d7a2017-12-20 12:43:01 -0800747 }
748
Stevenc4e99c52018-09-27 20:06:26 -0700749 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 Luong4c4223e2020-07-15 08:44:54 -0700754 vec_validate_aligned (ptd->per_port_queue, vec_len (bif->members) - 1,
Stevenc4e99c52018-09-27 20:06:26 -0700755 CLIB_CACHE_LINE_BYTES);
756
Steven Luong4c4223e2020-07-15 08:44:54 -0700757 vec_foreach_index (mif_if_index, ptd->per_port_queue)
Stevenc4e99c52018-09-27 20:06:26 -0700758 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700759 ptd->per_port_queue[mif_if_index].n_buffers = 0;
Stevenc4e99c52018-09-27 20:06:26 -0700760 }
761 }
762
Steven9cd2d7a2017-12-20 12:43:01 -0800763 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
Steven Luong4c4223e2020-07-15 08:44:54 -0700764 mif->sw_if_index, 1, 0, 0);
Steven9cd2d7a2017-12-20 12:43:01 -0800765
766 if (args->rv)
767 {
768 args->error =
769 clib_error_return (0,
770 "Error encountered on input feature arc enable");
771 }
772}
773
774static clib_error_t *
Steven Luong4c4223e2020-07-15 08:44:54 -0700775add_member_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
776 vlib_cli_command_t * cmd)
Steven9cd2d7a2017-12-20 12:43:01 -0800777{
Steven Luong4c4223e2020-07-15 08:44:54 -0700778 bond_add_member_args_t args = { 0 };
Steven9cd2d7a2017-12-20 12:43:01 -0800779 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 Luong4c4223e2020-07-15 08:44:54 -0700786 args.member = ~0;
Steven9cd2d7a2017-12-20 12:43:01 -0800787 args.group = ~0;
788 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
789 {
Stevenb3caf552018-03-27 15:04:47 -0700790 if (unformat (line_input, "%U %U",
791 unformat_vnet_sw_interface, vnm, &args.group,
Steven Luong4c4223e2020-07-15 08:44:54 -0700792 unformat_vnet_sw_interface, vnm, &args.member))
Steven9cd2d7a2017-12-20 12:43:01 -0800793 ;
Steven9cd2d7a2017-12-20 12:43:01 -0800794 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 Luong4c4223e2020-07-15 08:44:54 -0700811 if (args.member == ~0)
812 return clib_error_return (0,
813 "please specify valid member interface name");
Steven9cd2d7a2017-12-20 12:43:01 -0800814
Steven Luong4c4223e2020-07-15 08:44:54 -0700815 bond_add_member (vm, &args);
Steven9cd2d7a2017-12-20 12:43:01 -0800816
817 return args.error;
818}
819
820/* *INDENT-OFF* */
Steven Luong4c4223e2020-07-15 08:44:54 -0700821VLIB_CLI_COMMAND (add_member_interface_command, static) = {
Stevenb3caf552018-03-27 15:04:47 -0700822 .path = "bond add",
Steven Luong4c4223e2020-07-15 08:44:54 -0700823 .short_help = "bond add <BondEthernetx> <member-interface> "
Stevenb3caf552018-03-27 15:04:47 -0700824 "[passive] [long-timeout]",
Steven Luong4c4223e2020-07-15 08:44:54 -0700825 .function = add_member_interface_command_fn,
Steven9cd2d7a2017-12-20 12:43:01 -0800826};
827/* *INDENT-ON* */
828
829void
Steven Luong4c4223e2020-07-15 08:44:54 -0700830bond_detach_member (vlib_main_t * vm, bond_detach_member_args_t * args)
Steven9cd2d7a2017-12-20 12:43:01 -0800831{
832 bond_if_t *bif;
Steven Luong4c4223e2020-07-15 08:44:54 -0700833 member_if_t *mif;
Steven9cd2d7a2017-12-20 12:43:01 -0800834
Steven Luong4c4223e2020-07-15 08:44:54 -0700835 mif = bond_get_member_by_sw_if_index (args->member);
836 if (!mif)
Steven9cd2d7a2017-12-20 12:43:01 -0800837 {
838 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Steven Luong4c4223e2020-07-15 08:44:54 -0700839 args->error = clib_error_return (0, "interface was not a member");
Steven9cd2d7a2017-12-20 12:43:01 -0800840 return;
841 }
Steven Luong4c4223e2020-07-15 08:44:54 -0700842 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
843 bond_delete_neighbor (vm, bif, mif);
Steven9cd2d7a2017-12-20 12:43:01 -0800844}
845
846static clib_error_t *
847detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
848 vlib_cli_command_t * cmd)
849{
Steven Luong4c4223e2020-07-15 08:44:54 -0700850 bond_detach_member_args_t args = { 0 };
Steven9cd2d7a2017-12-20 12:43:01 -0800851 unformat_input_t _line_input, *line_input = &_line_input;
852 vnet_main_t *vnm = vnet_get_main ();
853
854 /* Get a line of input. */
855 if (!unformat_user (input, unformat_line_input, line_input))
856 return clib_error_return (0, "Missing required arguments.");
857
Steven Luong4c4223e2020-07-15 08:44:54 -0700858 args.member = ~0;
Steven9cd2d7a2017-12-20 12:43:01 -0800859 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
860 {
Stevenb3caf552018-03-27 15:04:47 -0700861 if (unformat (line_input, "%U",
Steven Luong4c4223e2020-07-15 08:44:54 -0700862 unformat_vnet_sw_interface, vnm, &args.member))
Steven9cd2d7a2017-12-20 12:43:01 -0800863 ;
864 else
865 {
866 args.error = clib_error_return (0, "unknown input `%U'",
867 format_unformat_error, input);
868 break;
869 }
870 }
871 unformat_free (line_input);
872
873 if (args.error)
874 return args.error;
Steven Luong4c4223e2020-07-15 08:44:54 -0700875 if (args.member == ~0)
876 return clib_error_return (0,
877 "please specify valid member interface name");
Steven9cd2d7a2017-12-20 12:43:01 -0800878
Steven Luong4c4223e2020-07-15 08:44:54 -0700879 bond_detach_member (vm, &args);
Steven9cd2d7a2017-12-20 12:43:01 -0800880
881 return args.error;
882}
883
884/* *INDENT-OFF* */
885VLIB_CLI_COMMAND (detach_interface_command, static) = {
Stevenb3caf552018-03-27 15:04:47 -0700886 .path = "bond del",
Steven Luong4c4223e2020-07-15 08:44:54 -0700887 .short_help = "bond del <member-interface>",
Steven9cd2d7a2017-12-20 12:43:01 -0800888 .function = detach_interface_command_fn,
889};
890/* *INDENT-ON* */
891
892static void
893show_bond (vlib_main_t * vm)
894{
895 bond_main_t *bm = &bond_main;
896 bond_if_t *bif;
897
Steven318de5d2018-10-06 22:30:50 -0700898 vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
Steven9cd2d7a2017-12-20 12:43:01 -0800899 "interface name", "sw_if_index", "mode",
Steven Luong4c4223e2020-07-15 08:44:54 -0700900 "load balance", "active members", "members");
Steven9cd2d7a2017-12-20 12:43:01 -0800901
902 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100903 pool_foreach (bif, bm->interfaces)
904 {
Steven318de5d2018-10-06 22:30:50 -0700905 vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
Steven9cd2d7a2017-12-20 12:43:01 -0800906 format_bond_interface_name, bif->dev_instance,
907 bif->sw_if_index, format_bond_mode, bif->mode,
908 format_bond_load_balance, bif->lb,
Steven Luong4c4223e2020-07-15 08:44:54 -0700909 vec_len (bif->active_members), vec_len (bif->members));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100910 }
Steven9cd2d7a2017-12-20 12:43:01 -0800911 /* *INDENT-ON* */
912}
913
914static void
915show_bond_details (vlib_main_t * vm)
916{
917 bond_main_t *bm = &bond_main;
918 bond_if_t *bif;
919 u32 *sw_if_index;
920
921 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100922 pool_foreach (bif, bm->interfaces)
923 {
Steven9cd2d7a2017-12-20 12:43:01 -0800924 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
925 vlib_cli_output (vm, " mode: %U",
926 format_bond_mode, bif->mode);
927 vlib_cli_output (vm, " load balance: %U",
928 format_bond_load_balance, bif->lb);
Steven Luong2e1fa542020-01-06 15:14:46 -0800929 if (bif->gso)
930 vlib_cli_output (vm, " gso enable");
Steven9cd2d7a2017-12-20 12:43:01 -0800931 if (bif->mode == BOND_MODE_ROUND_ROBIN)
Steven Luong4c4223e2020-07-15 08:44:54 -0700932 vlib_cli_output (vm, " last xmit member index: %u",
Steven9cd2d7a2017-12-20 12:43:01 -0800933 bif->lb_rr_last_index);
Steven Luong4c4223e2020-07-15 08:44:54 -0700934 vlib_cli_output (vm, " number of active members: %d",
935 vec_len (bif->active_members));
936 vec_foreach (sw_if_index, bif->active_members)
Steven9cd2d7a2017-12-20 12:43:01 -0800937 {
938 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
939 vnet_get_main (), *sw_if_index);
Steven Luonga1876b82019-08-20 16:58:00 -0700940 if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
941 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700942 member_if_t *mif = bond_get_member_by_sw_if_index (*sw_if_index);
943 if (mif)
Steven Luonga1876b82019-08-20 16:58:00 -0700944 vlib_cli_output (vm, " weight: %u, is_local_numa: %u, "
Steven Luong4c4223e2020-07-15 08:44:54 -0700945 "sw_if_index: %u", mif->weight,
946 mif->is_local_numa, mif->sw_if_index);
Steven Luonga1876b82019-08-20 16:58:00 -0700947 }
Steven9cd2d7a2017-12-20 12:43:01 -0800948 }
Steven Luong4c4223e2020-07-15 08:44:54 -0700949 vlib_cli_output (vm, " number of members: %d", vec_len (bif->members));
950 vec_foreach (sw_if_index, bif->members)
Steven9cd2d7a2017-12-20 12:43:01 -0800951 {
952 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
953 vnet_get_main (), *sw_if_index);
954 }
955 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500956 vlib_cli_output (vm, " interface id: %d", bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800957 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
958 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100959 }
Steven9cd2d7a2017-12-20 12:43:01 -0800960 /* *INDENT-ON* */
961}
962
963static clib_error_t *
964show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
965 vlib_cli_command_t * cmd)
966{
967 u8 details = 0;
968
969 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
970 {
971 if (unformat (input, "details"))
972 details = 1;
973 else
974 {
975 return clib_error_return (0, "unknown input `%U'",
976 format_unformat_error, input);
977 }
978 }
979
980 if (details)
981 show_bond_details (vm);
982 else
983 show_bond (vm);
984
985 return 0;
986}
987
988/* *INDENT-OFF* */
989VLIB_CLI_COMMAND (show_bond_command, static) = {
990 .path = "show bond",
991 .short_help = "show bond [details]",
992 .function = show_bond_fn,
993};
994/* *INDENT-ON* */
995
Steven Luonga1876b82019-08-20 16:58:00 -0700996void
997bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args)
998{
Steven Luong4c4223e2020-07-15 08:44:54 -0700999 member_if_t *mif;
Steven Luonga1876b82019-08-20 16:58:00 -07001000 bond_if_t *bif;
1001 vnet_main_t *vnm;
1002 u32 old_weight;
1003
Steven Luong4c4223e2020-07-15 08:44:54 -07001004 mif = bond_get_member_by_sw_if_index (args->sw_if_index);
1005 if (!mif)
Steven Luonga1876b82019-08-20 16:58:00 -07001006 {
1007 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Steven Luong4c4223e2020-07-15 08:44:54 -07001008 args->error = clib_error_return (0, "Interface not a member");
Steven Luonga1876b82019-08-20 16:58:00 -07001009 return;
1010 }
Steven Luong4c4223e2020-07-15 08:44:54 -07001011 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
Steven Luonga1876b82019-08-20 16:58:00 -07001012 if (!bif)
1013 {
1014 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1015 args->error = clib_error_return (0, "bond interface not found");
1016 return;
1017 }
1018 if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
1019 {
1020 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
1021 args->error =
1022 clib_error_return (0, "Weight valid for active-backup only");
1023 return;
1024 }
1025
Steven Luong4c4223e2020-07-15 08:44:54 -07001026 old_weight = mif->weight;
1027 mif->weight = args->weight;
Steven Luonga1876b82019-08-20 16:58:00 -07001028 vnm = vnet_get_main ();
1029 /*
Steven Luong4c4223e2020-07-15 08:44:54 -07001030 * No need to sort the list if the affected member is not up (not in active
1031 * member set), active member count is 1, or the current member is already the
1032 * primary member and new weight > old weight.
Steven Luonga1876b82019-08-20 16:58:00 -07001033 */
Steven Luong4c4223e2020-07-15 08:44:54 -07001034 if (!vnet_sw_interface_is_up (vnm, mif->sw_if_index) ||
1035 (vec_len (bif->active_members) == 1) ||
1036 ((bif->active_members[0] == mif->sw_if_index) &&
1037 (mif->weight >= old_weight)))
Steven Luonga1876b82019-08-20 16:58:00 -07001038 return;
1039
Steven Luong4c4223e2020-07-15 08:44:54 -07001040 bond_sort_members (bif);
Steven Luonga1876b82019-08-20 16:58:00 -07001041}
1042
1043static clib_error_t *
1044bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
1045 vlib_cli_command_t * cmd)
1046{
1047 bond_set_intf_weight_args_t args = { 0 };
1048 u32 sw_if_index = (u32) ~ 0;
1049 unformat_input_t _line_input, *line_input = &_line_input;
1050 vnet_main_t *vnm = vnet_get_main ();
1051 u8 weight_enter = 0;
1052 u32 weight = 0;
1053
1054 /* Get a line of input. */
1055 if (!unformat_user (input, unformat_line_input, line_input))
1056 return clib_error_return (0, "Missing required arguments.");
1057
1058 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1059 {
1060 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1061 ;
1062 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
1063 &sw_if_index))
1064 ;
1065 else if (unformat (line_input, "weight %u", &weight))
1066 weight_enter = 1;
1067 else
1068 {
1069 clib_error_return (0, "unknown input `%U'", format_unformat_error,
1070 input);
1071 break;
1072 }
1073 }
1074
1075 unformat_free (line_input);
1076 if (sw_if_index == (u32) ~ 0)
1077 {
1078 args.rv = VNET_API_ERROR_INVALID_INTERFACE;
1079 clib_error_return (0, "Interface name is invalid!");
1080 }
1081 if (weight_enter == 0)
1082 {
1083 args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
1084 clib_error_return (0, "weight missing");
1085 }
1086
1087 args.sw_if_index = sw_if_index;
1088 args.weight = weight;
1089 bond_set_intf_weight (vm, &args);
1090
1091 return args.error;
1092}
1093
1094/* *INDENT-OFF* */
1095VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
1096 .path = "set interface bond",
1097 .short_help = "set interface bond <interface> | sw_if_index <idx>"
1098 " weight <value>",
1099 .function = bond_set_intf_cmd,
1100};
1101/* *INDENT-ON* */
1102
Steven9cd2d7a2017-12-20 12:43:01 -08001103clib_error_t *
1104bond_cli_init (vlib_main_t * vm)
1105{
1106 bond_main_t *bm = &bond_main;
1107
1108 bm->vlib_main = vm;
1109 bm->vnet_main = vnet_get_main ();
Steven Luong4c4223e2020-07-15 08:44:54 -07001110 vec_validate_aligned (bm->member_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
Stevenc4e99c52018-09-27 20:06:26 -07001111 vec_validate_aligned (bm->per_thread_data,
1112 vlib_get_thread_main ()->n_vlib_mains - 1,
1113 CLIB_CACHE_LINE_BYTES);
Steven9cd2d7a2017-12-20 12:43:01 -08001114
1115 return 0;
1116}
1117
1118VLIB_INIT_FUNCTION (bond_cli_init);
1119
1120/*
1121 * fd.io coding-style-patch-verification: ON
1122 *
1123 * Local Variables:
1124 * eval: (c-set-style "gnu")
1125 * End:
1126 */