blob: 92a9ff067f65d1c5ca45bd271e06ae6ff7dda048 [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
26bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
27{
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
34 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
Stevena005e7f2018-03-22 17:46:58 -070035 clib_spinlock_lock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -080036 vec_foreach_index (i, bif->active_slaves)
37 {
38 p = *vec_elt_at_index (bif->active_slaves, i);
39 if (p == sif->sw_if_index)
40 {
Steven Luonga1876b82019-08-20 16:58:00 -070041 if ((bif->mode == BOND_MODE_ACTIVE_BACKUP) && (i == 0) &&
42 (vec_len (bif->active_slaves) > 1))
43 /* deleting the active slave for active-backup */
44 switching_active = 1;
Steven9cd2d7a2017-12-20 12:43:01 -080045 vec_del1 (bif->active_slaves, i);
Zhiyong Yang751e3f32019-06-26 05:49:14 -040046 if (sif->lacp_enabled && bif->numa_only)
47 {
48 /* For lacp mode, if we check it is a slave on local numa node,
49 bif->n_numa_slaves should be decreased by 1 becasue the first
50 bif->n_numa_slaves are all slaves on local numa node */
51 if (i < bif->n_numa_slaves)
52 {
53 bif->n_numa_slaves--;
54 ASSERT (bif->n_numa_slaves >= 0);
55 }
56 }
Steven9cd2d7a2017-12-20 12:43:01 -080057 break;
58 }
59 }
Zhiyong Yang6865d3c2019-05-15 04:25:20 -040060
61 /* We get a new slave 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
73bond_slave_sort (void *a1, void *a2)
74{
75 u32 *s1 = a1;
76 u32 *s2 = a2;
77 slave_if_t *sif1 = bond_get_slave_by_sw_if_index (*s1);
78 slave_if_t *sif2 = bond_get_slave_by_sw_if_index (*s2);
79 bond_if_t *bif;
80
Dave Barachffd15462020-02-18 10:12:23 -050081 ALWAYS_ASSERT (sif1);
82 ALWAYS_ASSERT (sif2);
Steven Luonga1876b82019-08-20 16:58:00 -070083 /*
84 * sort entries according to preference rules:
85 * 1. biggest weight
86 * 2. numa-node
87 * 3. current active slave (to prevent churning)
88 * 4. lowest sw_if_index (for deterministic behavior)
89 *
90 */
91 if (sif2->weight > sif1->weight)
92 return 1;
93 if (sif2->weight < sif1->weight)
94 return -1;
95 else
96 {
97 if (sif2->is_local_numa > sif1->is_local_numa)
98 return 1;
99 if (sif2->is_local_numa < sif1->is_local_numa)
100 return -1;
101 else
102 {
103 bif = bond_get_master_by_dev_instance (sif1->bif_dev_instance);
104 /* Favor the current active slave to avoid churning */
105 if (bif->active_slaves[0] == sif2->sw_if_index)
106 return 1;
107 if (bif->active_slaves[0] == sif1->sw_if_index)
108 return -1;
109 /* go for the tiebreaker as the last resort */
110 if (sif1->sw_if_index > sif2->sw_if_index)
111 return 1;
112 if (sif1->sw_if_index < sif2->sw_if_index)
113 return -1;
114 else
115 ASSERT (0);
116 }
117 }
118 return 0;
119}
120
121static void
122bond_sort_slaves (bond_if_t * bif)
123{
124 bond_main_t *bm = &bond_main;
125 u32 old_active = bif->active_slaves[0];
126
127 vec_sort_with_function (bif->active_slaves, bond_slave_sort);
128 if (old_active != bif->active_slaves[0])
129 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
134bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
135{
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 ();
139 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
Steven Luong002723c2019-10-22 21:27:22 -0700140 int i;
141 uword p;
Steven9cd2d7a2017-12-20 12:43:01 -0800142
143 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
Stevena005e7f2018-03-22 17:46:58 -0700144 clib_spinlock_lock_if_init (&bif->lockp);
Steven Luong002723c2019-10-22 21:27:22 -0700145 vec_foreach_index (i, bif->active_slaves)
146 {
147 p = *vec_elt_at_index (bif->active_slaves, i);
148 if (p == sif->sw_if_index)
149 goto done;
150 }
151
152 if (sif->lacp_enabled && bif->numa_only && (vm->numa_node == hw->numa_node))
Steven9cd2d7a2017-12-20 12:43:01 -0800153 {
Steven Luong002723c2019-10-22 21:27:22 -0700154 vec_insert_elts (bif->active_slaves, &sif->sw_if_index, 1,
155 bif->n_numa_slaves);
156 bif->n_numa_slaves++;
Steven9cd2d7a2017-12-20 12:43:01 -0800157 }
Steven Luong002723c2019-10-22 21:27:22 -0700158 else
159 vec_add1 (bif->active_slaves, sif->sw_if_index);
160
161 sif->is_local_numa = (vm->numa_node == hw->numa_node) ? 1 : 0;
162 if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
163 {
164 if (vec_len (bif->active_slaves) == 1)
165 /* First slave becomes active? */
166 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
167 BOND_SEND_GARP_NA, bif->hw_if_index);
168 else
169 bond_sort_slaves (bif);
170 }
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* */
187 pool_foreach (bif, bm->interfaces,
188 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;
Steven9cd2d7a2017-12-20 12:43:01 -0800201 bondif->active_slaves = vec_len (bif->active_slaves);
202 bondif->slaves = vec_len (bif->slaves);
203 );
204 /* *INDENT-ON* */
205
206 *out_bondifs = r_bondifs;
207
208 return 0;
209}
210
211int
212bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
213 u32 bond_sw_if_index)
214{
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;
219 slave_interface_details_t *r_slaveifs = NULL;
220 slave_interface_details_t *slaveif = NULL;
221 u32 *sw_if_index = NULL;
222 slave_if_t *sif;
223
224 bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
225 if (!bif)
226 return 1;
227
228 vec_foreach (sw_if_index, bif->slaves)
229 {
230 vec_add2 (r_slaveifs, slaveif, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400231 clib_memset (slaveif, 0, sizeof (*slaveif));
Steven9cd2d7a2017-12-20 12:43:01 -0800232 sif = bond_get_slave_by_sw_if_index (*sw_if_index);
233 if (sif)
234 {
235 sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
236 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
237 clib_memcpy (slaveif->interface_name, hi->name,
238 MIN (ARRAY_LEN (slaveif->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 ==
242 slaveif->interface_name[ARRAY_LEN (slaveif->interface_name) -
243 1]);
Steven9cd2d7a2017-12-20 12:43:01 -0800244 slaveif->sw_if_index = sif->sw_if_index;
245 slaveif->is_passive = sif->is_passive;
246 slaveif->is_long_timeout = sif->is_long_timeout;
Steven Luonga1876b82019-08-20 16:58:00 -0700247 slaveif->is_local_numa = sif->is_local_numa;
248 slaveif->weight = sif->weight;
Steven9cd2d7a2017-12-20 12:43:01 -0800249 }
250 }
251 *out_slaveifs = r_slaveifs;
252
253 return 0;
254}
255
Matthew Smithe83aa452019-11-14 10:36:02 -0600256/*
257 * Manage secondary mac addresses when attaching/detaching a slave.
258 * If adding, copies any secondary addresses from master to slave
259 * If deleting, deletes the master's secondary addresses from the slave
260 *
261 */
262static void
263bond_slave_add_del_mac_addrs (bond_if_t * bif, u32 sif_sw_if_index, u8 is_add)
264{
265 vnet_main_t *vnm = vnet_get_main ();
266 ethernet_interface_t *b_ei;
267 mac_address_t *sec_mac;
268 vnet_hw_interface_t *s_hwif;
269
270 b_ei = ethernet_get_interface (&ethernet_main, bif->hw_if_index);
271 if (!b_ei || !b_ei->secondary_addrs)
272 return;
273
274 s_hwif = vnet_get_sup_hw_interface (vnm, sif_sw_if_index);
275
276 vec_foreach (sec_mac, b_ei->secondary_addrs)
277 vnet_hw_interface_add_del_mac_address (vnm, s_hwif->hw_if_index,
278 sec_mac->bytes, is_add);
279}
280
Steven9cd2d7a2017-12-20 12:43:01 -0800281static void
282bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
283{
284 bond_main_t *bm = &bond_main;
285 vnet_main_t *vnm = vnet_get_main ();
286 int i;
Stevence2db6a2018-04-23 17:06:24 -0700287 vnet_hw_interface_t *sif_hw;
Steven4f8863b2018-04-12 19:36:19 -0700288
289 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800290
291 bif->port_number_bitmap =
292 clib_bitmap_set (bif->port_number_bitmap,
293 ntohs (sif->actor_admin.port_number) - 1, 0);
Steven0d883012018-05-11 11:06:23 -0700294 bm->slave_by_sw_if_index[sif->sw_if_index] = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800295 vec_free (sif->last_marker_pkt);
296 vec_free (sif->last_rx_pkt);
297 vec_foreach_index (i, bif->slaves)
298 {
299 uword p = *vec_elt_at_index (bif->slaves, i);
300 if (p == sif->sw_if_index)
301 {
302 vec_del1 (bif->slaves, i);
303 break;
304 }
305 }
306
307 bond_disable_collecting_distributing (vm, sif);
308
Steven5967baf2018-09-24 21:09:36 -0700309 vnet_feature_enable_disable ("device-input", "bond-input",
Steven Luong1a41a352019-10-09 10:29:47 -0700310 sif->sw_if_index, 0, 0, 0);
Steven5967baf2018-09-24 21:09:36 -0700311
Steven9cd2d7a2017-12-20 12:43:01 -0800312 /* Put back the old mac */
Steven4f8863b2018-04-12 19:36:19 -0700313 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800314 sif->persistent_hw_address);
315
Matthew Smithe83aa452019-11-14 10:36:02 -0600316 /* delete the bond's secondary/virtual mac addrs from the slave */
317 bond_slave_add_del_mac_addrs (bif, sif->sw_if_index, 0 /* is_add */ );
318
319
Steven9cd2d7a2017-12-20 12:43:01 -0800320 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
321 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
Steven5967baf2018-09-24 21:09:36 -0700322
Steven Luong0f09a822019-08-07 12:20:22 -0700323 if (bif->mode == BOND_MODE_LACP)
Steven Luongaa725782019-11-12 19:45:49 -0800324 {
325 stat_segment_deregister_state_counter
326 (bm->stats[bif->sw_if_index][sif->sw_if_index].actor_state);
327 stat_segment_deregister_state_counter
328 (bm->stats[bif->sw_if_index][sif->sw_if_index].partner_state);
329 }
Steven Luong0f09a822019-08-07 12:20:22 -0700330
Steven5967baf2018-09-24 21:09:36 -0700331 pool_put (bm->neighbors, sif);
Steven9cd2d7a2017-12-20 12:43:01 -0800332}
333
334int
335bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
336{
337 bond_main_t *bm = &bond_main;
338 vnet_main_t *vnm = vnet_get_main ();
339 bond_if_t *bif;
340 slave_if_t *sif;
341 vnet_hw_interface_t *hw;
342 u32 *sif_sw_if_index;
Benoît Gannecc3aac02019-10-16 15:03:06 +0200343 u32 *s_list = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800344
345 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
346 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
347 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
348
349 bif = bond_get_master_by_dev_instance (hw->dev_instance);
350
Benoît Gannecc3aac02019-10-16 15:03:06 +0200351 vec_append (s_list, bif->slaves);
352 vec_foreach (sif_sw_if_index, s_list)
Steven9cd2d7a2017-12-20 12:43:01 -0800353 {
Benoît Gannecc3aac02019-10-16 15:03:06 +0200354 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
355 if (sif)
356 bond_delete_neighbor (vm, bif, sif);
Steven9cd2d7a2017-12-20 12:43:01 -0800357 }
Benoît Gannecc3aac02019-10-16 15:03:06 +0200358 vec_free (s_list);
Steven70488ab2018-03-28 17:59:00 -0700359
Steven9cd2d7a2017-12-20 12:43:01 -0800360 /* bring down the interface */
361 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
362 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
363
364 ethernet_delete_interface (vnm, bif->hw_if_index);
365
366 clib_bitmap_free (bif->port_number_bitmap);
367 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500368 hash_unset (bm->id_used, bif->id);
Dave Barachb7b92992018-10-17 10:38:51 -0400369 clib_memset (bif, 0, sizeof (*bif));
Steven9cd2d7a2017-12-20 12:43:01 -0800370 pool_put (bm->interfaces, bif);
371
372 return 0;
373}
374
375void
376bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
377{
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;
Steven Luong2e1fa542020-01-06 15:14:46 -0800382 vnet_hw_interface_t *hw;
Steven9cd2d7a2017-12-20 12:43:01 -0800383
384 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
385 {
386 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
387 args->error = clib_error_return (0, "LACP plugin is not loaded");
388 return;
389 }
390 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
391 {
392 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
393 args->error = clib_error_return (0, "Invalid mode");
394 return;
395 }
396 if (args->lb > BOND_LB_L23)
397 {
398 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
399 args->error = clib_error_return (0, "Invalid load-balance");
400 return;
401 }
402 pool_get (bm->interfaces, bif);
Dave Barachb7b92992018-10-17 10:38:51 -0400403 clib_memset (bif, 0, sizeof (*bif));
Steven9cd2d7a2017-12-20 12:43:01 -0800404 bif->dev_instance = bif - bm->interfaces;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500405 bif->id = args->id;
Steven9cd2d7a2017-12-20 12:43:01 -0800406 bif->lb = args->lb;
407 bif->mode = args->mode;
Steven Luong2e1fa542020-01-06 15:14:46 -0800408 bif->gso = args->gso;
Steven9cd2d7a2017-12-20 12:43:01 -0800409
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500410 // Adjust requested interface id
411 if (bif->id == ~0)
412 bif->id = bif->dev_instance;
413 if (hash_get (bm->id_used, bif->id))
414 {
415 args->rv = VNET_API_ERROR_INSTANCE_IN_USE;
416 pool_put (bm->interfaces, bif);
417 return;
418 }
419 hash_set (bm->id_used, bif->id, 1);
420
Steven9cd2d7a2017-12-20 12:43:01 -0800421 // Special load-balance mode used for rr and bc
422 if (bif->mode == BOND_MODE_ROUND_ROBIN)
423 bif->lb = BOND_LB_RR;
424 else if (bif->mode == BOND_MODE_BROADCAST)
425 bif->lb = BOND_LB_BC;
Steven318de5d2018-10-06 22:30:50 -0700426 else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
427 bif->lb = BOND_LB_AB;
Steven9cd2d7a2017-12-20 12:43:01 -0800428
429 bif->use_custom_mac = args->hw_addr_set;
430 if (!args->hw_addr_set)
431 {
432 f64 now = vlib_time_now (vm);
433 u32 rnd;
434 rnd = (u32) (now * 1e6);
435 rnd = random_u32 (&rnd);
436
437 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
438 args->hw_addr[0] = 2;
439 args->hw_addr[1] = 0xfe;
440 }
441 memcpy (bif->hw_address, args->hw_addr, 6);
442 args->error = ethernet_register_interface
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500443 (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ ,
Steven9cd2d7a2017-12-20 12:43:01 -0800444 bif->hw_address /* ethernet address */ ,
445 &bif->hw_if_index, 0 /* flag change */ );
446
447 if (args->error)
448 {
449 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500450 hash_unset (bm->id_used, bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800451 pool_put (bm->interfaces, bif);
452 return;
453 }
454
455 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
456 bif->sw_if_index = sw->sw_if_index;
457 bif->group = bif->sw_if_index;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400458 bif->numa_only = args->numa_only;
Steven Luong2e1fa542020-01-06 15:14:46 -0800459
460 hw = vnet_get_hw_interface (vnm, bif->hw_if_index);
Mohsin Kazmi689666c2020-05-12 14:28:13 +0200461 /*
462 * Add GSO and Checksum offload flags if GSO is enabled on Bond
463 */
464 if (args->gso)
465 {
466 hw->flags |= (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
467 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
468 }
Stevena005e7f2018-03-22 17:46:58 -0700469 if (vlib_get_thread_main ()->n_vlib_mains > 1)
470 clib_spinlock_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -0800471
Matthew Smith72851032020-05-29 12:29:45 -0500472 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
473 VNET_HW_INTERFACE_FLAG_LINK_UP);
474
Steven9cd2d7a2017-12-20 12:43:01 -0800475 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
476
477 // for return
478 args->sw_if_index = bif->sw_if_index;
Mohsin Kazmi8c1280f2019-07-01 11:08:20 +0200479 args->rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800480}
481
482static clib_error_t *
483bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
484 vlib_cli_command_t * cmd)
485{
486 unformat_input_t _line_input, *line_input = &_line_input;
487 bond_create_if_args_t args = { 0 };
488 u8 mode_is_set = 0;
489
490 /* Get a line of input. */
491 if (!unformat_user (input, unformat_line_input, line_input))
492 return clib_error_return (0, "Missing required arguments.");
493
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500494 args.id = ~0;
Steven9cd2d7a2017-12-20 12:43:01 -0800495 args.mode = -1;
496 args.lb = BOND_LB_L2;
Mohsin Kazmi8c1280f2019-07-01 11:08:20 +0200497 args.rv = -1;
Steven9cd2d7a2017-12-20 12:43:01 -0800498 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
499 {
500 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
501 mode_is_set = 1;
502 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
503 && unformat (line_input, "load-balance %U",
504 unformat_bond_load_balance, &args.lb))
505 ;
506 else if (unformat (line_input, "hw-addr %U",
507 unformat_ethernet_address, args.hw_addr))
508 args.hw_addr_set = 1;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500509 else if (unformat (line_input, "id %u", &args.id))
510 ;
Steven Luong2e1fa542020-01-06 15:14:46 -0800511 else if (unformat (line_input, "gso"))
512 args.gso = 1;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400513 else if (unformat (line_input, "numa-only"))
514 {
515 if (args.mode == BOND_MODE_LACP)
516 args.numa_only = 1;
517 else
518 return clib_error_return (0,
519 "Only lacp mode supports numa-only so far!");
520 }
Steven9cd2d7a2017-12-20 12:43:01 -0800521 else
522 return clib_error_return (0, "unknown input `%U'",
523 format_unformat_error, input);
524 }
525 unformat_free (line_input);
526
527 if (mode_is_set == 0)
528 return clib_error_return (0, "Missing bond mode");
529
530 bond_create_if (vm, &args);
531
Mohsin Kazmi8c1280f2019-07-01 11:08:20 +0200532 if (!args.rv)
533 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
534 vnet_get_main (), args.sw_if_index);
535
Steven9cd2d7a2017-12-20 12:43:01 -0800536 return args.error;
537}
538
539/* *INDENT-OFF* */
540VLIB_CLI_COMMAND (bond_create_command, static) = {
541 .path = "create bond",
542 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
Steven Luong2e1fa542020-01-06 15:14:46 -0800543 "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} "
544 "[hw-addr <mac-address>] [id <if-id>] [gso]",
Steven9cd2d7a2017-12-20 12:43:01 -0800545 .function = bond_create_command_fn,
546};
547/* *INDENT-ON* */
548
549static clib_error_t *
550bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
551 vlib_cli_command_t * cmd)
552{
553 unformat_input_t _line_input, *line_input = &_line_input;
554 u32 sw_if_index = ~0;
555 vnet_main_t *vnm = vnet_get_main ();
556 int rv;
557
558 /* Get a line of input. */
559 if (!unformat_user (input, unformat_line_input, line_input))
560 return clib_error_return (0, "Missing <interface>");
561
562 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
563 {
564 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
565 ;
566 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
567 vnm, &sw_if_index))
568 ;
569 else
570 return clib_error_return (0, "unknown input `%U'",
571 format_unformat_error, input);
572 }
573 unformat_free (line_input);
574
575 if (sw_if_index == ~0)
576 return clib_error_return (0,
577 "please specify interface name or sw_if_index");
578
579 rv = bond_delete_if (vm, sw_if_index);
580 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
581 return clib_error_return (0, "not a bond interface");
582 else if (rv != 0)
583 return clib_error_return (0, "error on deleting bond interface");
584
585 return 0;
586}
587
588/* *INDENT-OFF* */
589VLIB_CLI_COMMAND (bond_delete__command, static) =
590{
591 .path = "delete bond",
592 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
593 .function = bond_delete_command_fn,
594};
595/* *INDENT-ON* */
596
597void
598bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
599{
600 bond_main_t *bm = &bond_main;
601 vnet_main_t *vnm = vnet_get_main ();
602 bond_if_t *bif;
603 slave_if_t *sif;
604 vnet_interface_main_t *im = &vnm->interface_main;
Steven4f8863b2018-04-12 19:36:19 -0700605 vnet_hw_interface_t *bif_hw, *sif_hw;
Steven9cd2d7a2017-12-20 12:43:01 -0800606 vnet_sw_interface_t *sw;
Stevenc4e99c52018-09-27 20:06:26 -0700607 u32 thread_index;
608 u32 sif_if_index;
Steven9cd2d7a2017-12-20 12:43:01 -0800609
610 bif = bond_get_master_by_sw_if_index (args->group);
611 if (!bif)
612 {
613 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
614 args->error = clib_error_return (0, "bond interface not found");
615 return;
616 }
617 // make sure the interface is not already enslaved
618 if (bond_get_slave_by_sw_if_index (args->slave))
619 {
620 args->rv = VNET_API_ERROR_VALUE_EXIST;
621 args->error = clib_error_return (0, "interface was already enslaved");
622 return;
623 }
Steven4f8863b2018-04-12 19:36:19 -0700624 sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
625 if (sif_hw->dev_class_index == bond_dev_class.index)
Steven9cd2d7a2017-12-20 12:43:01 -0800626 {
627 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
628 args->error =
629 clib_error_return (0, "bond interface cannot be enslaved");
630 return;
631 }
Steven Luong2e1fa542020-01-06 15:14:46 -0800632 if (bif->gso && !(sif_hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO))
633 {
634 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
635 args->error =
636 clib_error_return (0, "slave interface is not gso capable");
637 return;
638 }
Steven Luong0f09a822019-08-07 12:20:22 -0700639 if (bif->mode == BOND_MODE_LACP)
640 {
Benoît Gannea03c7d52019-11-06 14:36:38 +0100641 u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index,
642 args->slave, 0);
Steven Luong0f09a822019-08-07 12:20:22 -0700643
644 vec_validate (bm->stats, bif->sw_if_index);
645 vec_validate (bm->stats[bif->sw_if_index], args->slave);
646
647 args->error = stat_segment_register_state_counter
Steven Luongaa725782019-11-12 19:45:49 -0800648 (name, &bm->stats[bif->sw_if_index][args->slave].actor_state);
649 if (args->error != 0)
650 {
651 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
652 vec_free (name);
653 return;
654 }
655
656 vec_reset_length (name);
657 name = format (0, "/if/lacp/%u/%u/partner-state%c", bif->sw_if_index,
658 args->slave, 0);
659 args->error = stat_segment_register_state_counter
660 (name, &bm->stats[bif->sw_if_index][args->slave].partner_state);
Steven Luong0f09a822019-08-07 12:20:22 -0700661 vec_free (name);
662 if (args->error != 0)
663 {
664 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
665 return;
666 }
667 }
668
Steven9cd2d7a2017-12-20 12:43:01 -0800669 pool_get (bm->neighbors, sif);
Dave Barachb7b92992018-10-17 10:38:51 -0400670 clib_memset (sif, 0, sizeof (*sif));
Steven9cd2d7a2017-12-20 12:43:01 -0800671 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
Steven Luongbac326c2019-08-05 09:47:58 -0700672 /* port_enabled is both admin up and hw link up */
673 sif->port_enabled = vnet_sw_interface_is_up (vnm, sw->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800674 sif->sw_if_index = sw->sw_if_index;
675 sif->hw_if_index = sw->hw_if_index;
676 sif->packet_template_index = (u8) ~ 0;
677 sif->is_passive = args->is_passive;
678 sif->group = args->group;
679 sif->bif_dev_instance = bif->dev_instance;
680 sif->mode = bif->mode;
681
682 sif->is_long_timeout = args->is_long_timeout;
683 if (args->is_long_timeout)
684 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
685 else
686 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
687
Steven0d883012018-05-11 11:06:23 -0700688 vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
689 CLIB_CACHE_LINE_BYTES);
690 /*
691 * sif - bm->neighbors may be 0
692 * Left shift it by 1 bit to distinguish the valid entry that we actually
693 * store from the null entries
694 */
695 bm->slave_by_sw_if_index[sif->sw_if_index] =
696 (uword) (((sif - bm->neighbors) << 1) | 1);
Steven9cd2d7a2017-12-20 12:43:01 -0800697 vec_add1 (bif->slaves, sif->sw_if_index);
698
Steven4f8863b2018-04-12 19:36:19 -0700699 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
700
Steven9cd2d7a2017-12-20 12:43:01 -0800701 /* Save the old mac */
Steven4f8863b2018-04-12 19:36:19 -0700702 memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
703 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800704 if (bif->use_custom_mac)
705 {
Steven4f8863b2018-04-12 19:36:19 -0700706 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800707 bif->hw_address);
708 }
709 else
710 {
711 // bond interface gets the mac address from the first slave
712 if (vec_len (bif->slaves) == 1)
713 {
Steven4f8863b2018-04-12 19:36:19 -0700714 memcpy (bif->hw_address, sif_hw->hw_address, 6);
715 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
716 sif_hw->hw_address);
Steven9cd2d7a2017-12-20 12:43:01 -0800717 }
718 else
719 {
720 // subsequent slaves gets the mac address of the bond interface
Steven4f8863b2018-04-12 19:36:19 -0700721 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800722 bif->hw_address);
723 }
724 }
725
Matthew Smithe83aa452019-11-14 10:36:02 -0600726 /* if there are secondary/virtual mac addrs, propagate to the slave */
727 bond_slave_add_del_mac_addrs (bif, sif->sw_if_index, 1 /* is_add */ );
728
Steven4f8863b2018-04-12 19:36:19 -0700729 if (bif_hw->l2_if_count)
John Lof415a3b2020-05-14 15:02:16 -0400730 ethernet_set_flags (vnm, sif_hw->hw_if_index,
731 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
732 else
733 ethernet_set_flags (vnm, sif_hw->hw_if_index,
734 /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
Steven4f8863b2018-04-12 19:36:19 -0700735
Stevene43278f2019-01-17 15:11:29 -0800736 if (bif->mode == BOND_MODE_LACP)
Steven9cd2d7a2017-12-20 12:43:01 -0800737 {
Stevene43278f2019-01-17 15:11:29 -0800738 if (bm->lacp_enable_disable)
739 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
Steven9cd2d7a2017-12-20 12:43:01 -0800740 }
Steven Luongbac326c2019-08-05 09:47:58 -0700741 else if (sif->port_enabled)
Steven9cd2d7a2017-12-20 12:43:01 -0800742 {
743 bond_enable_collecting_distributing (vm, sif);
744 }
745
Stevenc4e99c52018-09-27 20:06:26 -0700746 vec_foreach_index (thread_index, bm->per_thread_data)
747 {
748 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
749 thread_index);
750
Damjan Marion69fdfee2018-10-06 14:33:18 +0200751 vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1,
Stevenc4e99c52018-09-27 20:06:26 -0700752 CLIB_CACHE_LINE_BYTES);
753
754 vec_foreach_index (sif_if_index, ptd->per_port_queue)
755 {
756 ptd->per_port_queue[sif_if_index].n_buffers = 0;
757 }
758 }
759
Steven9cd2d7a2017-12-20 12:43:01 -0800760 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
Steven Luong1a41a352019-10-09 10:29:47 -0700761 sif->sw_if_index, 1, 0, 0);
Steven9cd2d7a2017-12-20 12:43:01 -0800762
763 if (args->rv)
764 {
765 args->error =
766 clib_error_return (0,
767 "Error encountered on input feature arc enable");
768 }
769}
770
771static clib_error_t *
772enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
773 vlib_cli_command_t * cmd)
774{
775 bond_enslave_args_t args = { 0 };
776 unformat_input_t _line_input, *line_input = &_line_input;
777 vnet_main_t *vnm = vnet_get_main ();
778
779 /* Get a line of input. */
780 if (!unformat_user (input, unformat_line_input, line_input))
781 return clib_error_return (0, "Missing required arguments.");
782
783 args.slave = ~0;
784 args.group = ~0;
785 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
786 {
Stevenb3caf552018-03-27 15:04:47 -0700787 if (unformat (line_input, "%U %U",
788 unformat_vnet_sw_interface, vnm, &args.group,
Steven9cd2d7a2017-12-20 12:43:01 -0800789 unformat_vnet_sw_interface, vnm, &args.slave))
790 ;
Steven9cd2d7a2017-12-20 12:43:01 -0800791 else if (unformat (line_input, "passive"))
792 args.is_passive = 1;
793 else if (unformat (line_input, "long-timeout"))
794 args.is_long_timeout = 1;
795 else
796 {
797 args.error = clib_error_return (0, "unknown input `%U'",
798 format_unformat_error, input);
799 break;
800 }
801 }
802 unformat_free (line_input);
803
804 if (args.error)
805 return args.error;
806 if (args.group == ~0)
807 return clib_error_return (0, "Missing bond interface");
808 if (args.slave == ~0)
Stevenb3caf552018-03-27 15:04:47 -0700809 return clib_error_return (0, "please specify valid slave interface name");
Steven9cd2d7a2017-12-20 12:43:01 -0800810
811 bond_enslave (vm, &args);
812
813 return args.error;
814}
815
816/* *INDENT-OFF* */
817VLIB_CLI_COMMAND (enslave_interface_command, static) = {
Stevenb3caf552018-03-27 15:04:47 -0700818 .path = "bond add",
819 .short_help = "bond add <BondEthernetx> <slave-interface> "
820 "[passive] [long-timeout]",
Steven9cd2d7a2017-12-20 12:43:01 -0800821 .function = enslave_interface_command_fn,
822};
823/* *INDENT-ON* */
824
825void
826bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
827{
828 bond_if_t *bif;
829 slave_if_t *sif;
830
831 sif = bond_get_slave_by_sw_if_index (args->slave);
832 if (!sif)
833 {
834 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
835 args->error = clib_error_return (0, "interface was not enslaved");
836 return;
837 }
838 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
839 bond_delete_neighbor (vm, bif, sif);
840}
841
842static clib_error_t *
843detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
844 vlib_cli_command_t * cmd)
845{
846 bond_detach_slave_args_t args = { 0 };
847 unformat_input_t _line_input, *line_input = &_line_input;
848 vnet_main_t *vnm = vnet_get_main ();
849
850 /* Get a line of input. */
851 if (!unformat_user (input, unformat_line_input, line_input))
852 return clib_error_return (0, "Missing required arguments.");
853
854 args.slave = ~0;
855 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
856 {
Stevenb3caf552018-03-27 15:04:47 -0700857 if (unformat (line_input, "%U",
Steven9cd2d7a2017-12-20 12:43:01 -0800858 unformat_vnet_sw_interface, vnm, &args.slave))
859 ;
860 else
861 {
862 args.error = clib_error_return (0, "unknown input `%U'",
863 format_unformat_error, input);
864 break;
865 }
866 }
867 unformat_free (line_input);
868
869 if (args.error)
870 return args.error;
871 if (args.slave == ~0)
Stevenb3caf552018-03-27 15:04:47 -0700872 return clib_error_return (0, "please specify valid slave interface name");
Steven9cd2d7a2017-12-20 12:43:01 -0800873
874 bond_detach_slave (vm, &args);
875
876 return args.error;
877}
878
879/* *INDENT-OFF* */
880VLIB_CLI_COMMAND (detach_interface_command, static) = {
Stevenb3caf552018-03-27 15:04:47 -0700881 .path = "bond del",
882 .short_help = "bond del <slave-interface>",
Steven9cd2d7a2017-12-20 12:43:01 -0800883 .function = detach_interface_command_fn,
884};
885/* *INDENT-ON* */
886
887static void
888show_bond (vlib_main_t * vm)
889{
890 bond_main_t *bm = &bond_main;
891 bond_if_t *bif;
892
Steven318de5d2018-10-06 22:30:50 -0700893 vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
Steven9cd2d7a2017-12-20 12:43:01 -0800894 "interface name", "sw_if_index", "mode",
895 "load balance", "active slaves", "slaves");
896
897 /* *INDENT-OFF* */
898 pool_foreach (bif, bm->interfaces,
899 ({
Steven318de5d2018-10-06 22:30:50 -0700900 vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
Steven9cd2d7a2017-12-20 12:43:01 -0800901 format_bond_interface_name, bif->dev_instance,
902 bif->sw_if_index, format_bond_mode, bif->mode,
903 format_bond_load_balance, bif->lb,
904 vec_len (bif->active_slaves), vec_len (bif->slaves));
905 }));
906 /* *INDENT-ON* */
907}
908
909static void
910show_bond_details (vlib_main_t * vm)
911{
912 bond_main_t *bm = &bond_main;
913 bond_if_t *bif;
914 u32 *sw_if_index;
915
916 /* *INDENT-OFF* */
917 pool_foreach (bif, bm->interfaces,
918 ({
919 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
920 vlib_cli_output (vm, " mode: %U",
921 format_bond_mode, bif->mode);
922 vlib_cli_output (vm, " load balance: %U",
923 format_bond_load_balance, bif->lb);
Steven Luong2e1fa542020-01-06 15:14:46 -0800924 if (bif->gso)
925 vlib_cli_output (vm, " gso enable");
Steven9cd2d7a2017-12-20 12:43:01 -0800926 if (bif->mode == BOND_MODE_ROUND_ROBIN)
927 vlib_cli_output (vm, " last xmit slave index: %u",
928 bif->lb_rr_last_index);
929 vlib_cli_output (vm, " number of active slaves: %d",
930 vec_len (bif->active_slaves));
931 vec_foreach (sw_if_index, bif->active_slaves)
932 {
933 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
934 vnet_get_main (), *sw_if_index);
Steven Luonga1876b82019-08-20 16:58:00 -0700935 if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
936 {
937 slave_if_t *sif = bond_get_slave_by_sw_if_index (*sw_if_index);
938 if (sif)
939 vlib_cli_output (vm, " weight: %u, is_local_numa: %u, "
940 "sw_if_index: %u", sif->weight,
941 sif->is_local_numa, sif->sw_if_index);
942 }
Steven9cd2d7a2017-12-20 12:43:01 -0800943 }
944 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
945 vec_foreach (sw_if_index, bif->slaves)
946 {
947 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
948 vnet_get_main (), *sw_if_index);
949 }
950 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500951 vlib_cli_output (vm, " interface id: %d", bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800952 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
953 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
954 }));
955 /* *INDENT-ON* */
956}
957
958static clib_error_t *
959show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
960 vlib_cli_command_t * cmd)
961{
962 u8 details = 0;
963
964 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
965 {
966 if (unformat (input, "details"))
967 details = 1;
968 else
969 {
970 return clib_error_return (0, "unknown input `%U'",
971 format_unformat_error, input);
972 }
973 }
974
975 if (details)
976 show_bond_details (vm);
977 else
978 show_bond (vm);
979
980 return 0;
981}
982
983/* *INDENT-OFF* */
984VLIB_CLI_COMMAND (show_bond_command, static) = {
985 .path = "show bond",
986 .short_help = "show bond [details]",
987 .function = show_bond_fn,
988};
989/* *INDENT-ON* */
990
Steven Luonga1876b82019-08-20 16:58:00 -0700991void
992bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args)
993{
994 slave_if_t *sif;
995 bond_if_t *bif;
996 vnet_main_t *vnm;
997 u32 old_weight;
998
999 sif = bond_get_slave_by_sw_if_index (args->sw_if_index);
1000 if (!sif)
1001 {
1002 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1003 args->error = clib_error_return (0, "Interface not enslaved");
1004 return;
1005 }
1006 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
1007 if (!bif)
1008 {
1009 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1010 args->error = clib_error_return (0, "bond interface not found");
1011 return;
1012 }
1013 if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
1014 {
1015 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
1016 args->error =
1017 clib_error_return (0, "Weight valid for active-backup only");
1018 return;
1019 }
1020
1021 old_weight = sif->weight;
1022 sif->weight = args->weight;
1023 vnm = vnet_get_main ();
1024 /*
1025 * No need to sort the list if the affected slave is not up (not in active
1026 * slave set), active slave count is 1, or the current slave is already the
1027 * primary slave and new weight > old weight.
1028 */
1029 if (!vnet_sw_interface_is_up (vnm, sif->sw_if_index) ||
1030 (vec_len (bif->active_slaves) == 1) ||
1031 ((bif->active_slaves[0] == sif->sw_if_index) &&
1032 (sif->weight >= old_weight)))
1033 return;
1034
1035 bond_sort_slaves (bif);
1036}
1037
1038static clib_error_t *
1039bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
1040 vlib_cli_command_t * cmd)
1041{
1042 bond_set_intf_weight_args_t args = { 0 };
1043 u32 sw_if_index = (u32) ~ 0;
1044 unformat_input_t _line_input, *line_input = &_line_input;
1045 vnet_main_t *vnm = vnet_get_main ();
1046 u8 weight_enter = 0;
1047 u32 weight = 0;
1048
1049 /* Get a line of input. */
1050 if (!unformat_user (input, unformat_line_input, line_input))
1051 return clib_error_return (0, "Missing required arguments.");
1052
1053 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1054 {
1055 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1056 ;
1057 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
1058 &sw_if_index))
1059 ;
1060 else if (unformat (line_input, "weight %u", &weight))
1061 weight_enter = 1;
1062 else
1063 {
1064 clib_error_return (0, "unknown input `%U'", format_unformat_error,
1065 input);
1066 break;
1067 }
1068 }
1069
1070 unformat_free (line_input);
1071 if (sw_if_index == (u32) ~ 0)
1072 {
1073 args.rv = VNET_API_ERROR_INVALID_INTERFACE;
1074 clib_error_return (0, "Interface name is invalid!");
1075 }
1076 if (weight_enter == 0)
1077 {
1078 args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
1079 clib_error_return (0, "weight missing");
1080 }
1081
1082 args.sw_if_index = sw_if_index;
1083 args.weight = weight;
1084 bond_set_intf_weight (vm, &args);
1085
1086 return args.error;
1087}
1088
1089/* *INDENT-OFF* */
1090VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
1091 .path = "set interface bond",
1092 .short_help = "set interface bond <interface> | sw_if_index <idx>"
1093 " weight <value>",
1094 .function = bond_set_intf_cmd,
1095};
1096/* *INDENT-ON* */
1097
Steven9cd2d7a2017-12-20 12:43:01 -08001098clib_error_t *
1099bond_cli_init (vlib_main_t * vm)
1100{
1101 bond_main_t *bm = &bond_main;
1102
1103 bm->vlib_main = vm;
1104 bm->vnet_main = vnet_get_main ();
Steven0d883012018-05-11 11:06:23 -07001105 vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
Stevenc4e99c52018-09-27 20:06:26 -07001106 vec_validate_aligned (bm->per_thread_data,
1107 vlib_get_thread_main ()->n_vlib_mains - 1,
1108 CLIB_CACHE_LINE_BYTES);
Steven9cd2d7a2017-12-20 12:43:01 -08001109
1110 return 0;
1111}
1112
1113VLIB_INIT_FUNCTION (bond_cli_init);
1114
1115/*
1116 * fd.io coding-style-patch-verification: ON
1117 *
1118 * Local Variables:
1119 * eval: (c-set-style "gnu")
1120 * End:
1121 */