blob: bccbb2c036eb36a2f6cf9a041b741d3257e410f1 [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>
23
24void
25bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
26{
Steven9f781d82018-06-05 11:09:32 -070027 bond_main_t *bm = &bond_main;
Steven9cd2d7a2017-12-20 12:43:01 -080028 bond_if_t *bif;
29 int i;
30 uword p;
Steven9f781d82018-06-05 11:09:32 -070031 u8 switching_active = 0;
Steven9cd2d7a2017-12-20 12:43:01 -080032
33 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
Stevena005e7f2018-03-22 17:46:58 -070034 clib_spinlock_lock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -080035 vec_foreach_index (i, bif->active_slaves)
36 {
37 p = *vec_elt_at_index (bif->active_slaves, i);
38 if (p == sif->sw_if_index)
39 {
Steven9f781d82018-06-05 11:09:32 -070040 /* Are we disabling the very 1st slave? */
41 if (sif->sw_if_index == *vec_elt_at_index (bif->active_slaves, 0))
42 switching_active = 1;
43
Steven9cd2d7a2017-12-20 12:43:01 -080044 vec_del1 (bif->active_slaves, i);
45 hash_unset (bif->active_slave_by_sw_if_index, sif->sw_if_index);
Steven9f781d82018-06-05 11:09:32 -070046
47 /* We got a new slave just becoming active? */
48 if ((vec_len (bif->active_slaves) >= 1) &&
49 (bif->mode == BOND_MODE_ACTIVE_BACKUP) && switching_active)
50 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
51 BOND_SEND_GARP_NA, bif->hw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -080052 break;
53 }
54 }
Stevena005e7f2018-03-22 17:46:58 -070055 clib_spinlock_unlock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -080056}
57
58void
59bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
60{
61 bond_if_t *bif;
Steven9f781d82018-06-05 11:09:32 -070062 bond_main_t *bm = &bond_main;
Steven9cd2d7a2017-12-20 12:43:01 -080063
64 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
Stevena005e7f2018-03-22 17:46:58 -070065 clib_spinlock_lock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -080066 if (!hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index))
67 {
68 hash_set (bif->active_slave_by_sw_if_index, sif->sw_if_index,
69 sif->sw_if_index);
70 vec_add1 (bif->active_slaves, sif->sw_if_index);
Steven9f781d82018-06-05 11:09:32 -070071
72 /* First slave becomes active? */
73 if ((vec_len (bif->active_slaves) == 1) &&
74 (bif->mode == BOND_MODE_ACTIVE_BACKUP))
75 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
76 BOND_SEND_GARP_NA, bif->hw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -080077 }
Stevena005e7f2018-03-22 17:46:58 -070078 clib_spinlock_unlock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -080079}
80
81int
82bond_dump_ifs (bond_interface_details_t ** out_bondifs)
83{
84 vnet_main_t *vnm = vnet_get_main ();
85 bond_main_t *bm = &bond_main;
86 bond_if_t *bif;
87 vnet_hw_interface_t *hi;
88 bond_interface_details_t *r_bondifs = NULL;
89 bond_interface_details_t *bondif = NULL;
90
91 /* *INDENT-OFF* */
92 pool_foreach (bif, bm->interfaces,
93 vec_add2(r_bondifs, bondif, 1);
Dave Barachb7b92992018-10-17 10:38:51 -040094 clib_memset (bondif, 0, sizeof (*bondif));
Alexander Chernavinad9d5282018-12-13 09:08:09 -050095 bondif->id = bif->id;
Steven9cd2d7a2017-12-20 12:43:01 -080096 bondif->sw_if_index = bif->sw_if_index;
97 hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
98 clib_memcpy(bondif->interface_name, hi->name,
99 MIN (ARRAY_LEN (bondif->interface_name) - 1,
100 strlen ((const char *) hi->name)));
101 bondif->mode = bif->mode;
102 bondif->lb = bif->lb;
103 bondif->active_slaves = vec_len (bif->active_slaves);
104 bondif->slaves = vec_len (bif->slaves);
105 );
106 /* *INDENT-ON* */
107
108 *out_bondifs = r_bondifs;
109
110 return 0;
111}
112
113int
114bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
115 u32 bond_sw_if_index)
116{
117 vnet_main_t *vnm = vnet_get_main ();
118 bond_if_t *bif;
119 vnet_hw_interface_t *hi;
120 vnet_sw_interface_t *sw;
121 slave_interface_details_t *r_slaveifs = NULL;
122 slave_interface_details_t *slaveif = NULL;
123 u32 *sw_if_index = NULL;
124 slave_if_t *sif;
125
126 bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
127 if (!bif)
128 return 1;
129
130 vec_foreach (sw_if_index, bif->slaves)
131 {
132 vec_add2 (r_slaveifs, slaveif, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400133 clib_memset (slaveif, 0, sizeof (*slaveif));
Steven9cd2d7a2017-12-20 12:43:01 -0800134 sif = bond_get_slave_by_sw_if_index (*sw_if_index);
135 if (sif)
136 {
137 sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
138 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
139 clib_memcpy (slaveif->interface_name, hi->name,
140 MIN (ARRAY_LEN (slaveif->interface_name) - 1,
141 strlen ((const char *) hi->name)));
142 slaveif->sw_if_index = sif->sw_if_index;
143 slaveif->is_passive = sif->is_passive;
144 slaveif->is_long_timeout = sif->is_long_timeout;
145 }
146 }
147 *out_slaveifs = r_slaveifs;
148
149 return 0;
150}
151
152static void
153bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
154{
155 bond_main_t *bm = &bond_main;
156 vnet_main_t *vnm = vnet_get_main ();
157 int i;
Stevence2db6a2018-04-23 17:06:24 -0700158 vnet_hw_interface_t *sif_hw;
Steven4f8863b2018-04-12 19:36:19 -0700159
160 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800161
162 bif->port_number_bitmap =
163 clib_bitmap_set (bif->port_number_bitmap,
164 ntohs (sif->actor_admin.port_number) - 1, 0);
Steven0d883012018-05-11 11:06:23 -0700165 bm->slave_by_sw_if_index[sif->sw_if_index] = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800166 vec_free (sif->last_marker_pkt);
167 vec_free (sif->last_rx_pkt);
168 vec_foreach_index (i, bif->slaves)
169 {
170 uword p = *vec_elt_at_index (bif->slaves, i);
171 if (p == sif->sw_if_index)
172 {
173 vec_del1 (bif->slaves, i);
174 break;
175 }
176 }
177
178 bond_disable_collecting_distributing (vm, sif);
179
Steven5967baf2018-09-24 21:09:36 -0700180 vnet_feature_enable_disable ("device-input", "bond-input",
181 sif_hw->hw_if_index, 0, 0, 0);
182
Steven9cd2d7a2017-12-20 12:43:01 -0800183 /* Put back the old mac */
Steven4f8863b2018-04-12 19:36:19 -0700184 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800185 sif->persistent_hw_address);
186
Steven9cd2d7a2017-12-20 12:43:01 -0800187 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
188 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
Steven5967baf2018-09-24 21:09:36 -0700189
190 pool_put (bm->neighbors, sif);
Steven9cd2d7a2017-12-20 12:43:01 -0800191}
192
193int
194bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
195{
196 bond_main_t *bm = &bond_main;
197 vnet_main_t *vnm = vnet_get_main ();
198 bond_if_t *bif;
199 slave_if_t *sif;
200 vnet_hw_interface_t *hw;
201 u32 *sif_sw_if_index;
Steven70488ab2018-03-28 17:59:00 -0700202 u32 **s_list = 0;
203 u32 i;
Steven9cd2d7a2017-12-20 12:43:01 -0800204
205 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
206 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
207 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
208
209 bif = bond_get_master_by_dev_instance (hw->dev_instance);
210
211 vec_foreach (sif_sw_if_index, bif->slaves)
212 {
Steven70488ab2018-03-28 17:59:00 -0700213 vec_add1 (s_list, sif_sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800214 }
215
Steven70488ab2018-03-28 17:59:00 -0700216 for (i = 0; i < vec_len (s_list); i++)
217 {
218 sif_sw_if_index = s_list[i];
219 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
220 if (sif)
221 bond_delete_neighbor (vm, bif, sif);
222 }
223
224 if (s_list)
225 vec_free (s_list);
226
Steven9cd2d7a2017-12-20 12:43:01 -0800227 /* bring down the interface */
228 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
229 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
230
231 ethernet_delete_interface (vnm, bif->hw_if_index);
232
233 clib_bitmap_free (bif->port_number_bitmap);
234 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500235 hash_unset (bm->id_used, bif->id);
Dave Barachb7b92992018-10-17 10:38:51 -0400236 clib_memset (bif, 0, sizeof (*bif));
Steven9cd2d7a2017-12-20 12:43:01 -0800237 pool_put (bm->interfaces, bif);
238
239 return 0;
240}
241
242void
243bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
244{
245 bond_main_t *bm = &bond_main;
246 vnet_main_t *vnm = vnet_get_main ();
247 vnet_sw_interface_t *sw;
248 bond_if_t *bif;
249
250 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
251 {
252 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
253 args->error = clib_error_return (0, "LACP plugin is not loaded");
254 return;
255 }
256 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
257 {
258 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
259 args->error = clib_error_return (0, "Invalid mode");
260 return;
261 }
262 if (args->lb > BOND_LB_L23)
263 {
264 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
265 args->error = clib_error_return (0, "Invalid load-balance");
266 return;
267 }
268 pool_get (bm->interfaces, bif);
Dave Barachb7b92992018-10-17 10:38:51 -0400269 clib_memset (bif, 0, sizeof (*bif));
Steven9cd2d7a2017-12-20 12:43:01 -0800270 bif->dev_instance = bif - bm->interfaces;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500271 bif->id = args->id;
Steven9cd2d7a2017-12-20 12:43:01 -0800272 bif->lb = args->lb;
273 bif->mode = args->mode;
274
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500275 // Adjust requested interface id
276 if (bif->id == ~0)
277 bif->id = bif->dev_instance;
278 if (hash_get (bm->id_used, bif->id))
279 {
280 args->rv = VNET_API_ERROR_INSTANCE_IN_USE;
281 pool_put (bm->interfaces, bif);
282 return;
283 }
284 hash_set (bm->id_used, bif->id, 1);
285
Steven9cd2d7a2017-12-20 12:43:01 -0800286 // Special load-balance mode used for rr and bc
287 if (bif->mode == BOND_MODE_ROUND_ROBIN)
288 bif->lb = BOND_LB_RR;
289 else if (bif->mode == BOND_MODE_BROADCAST)
290 bif->lb = BOND_LB_BC;
Steven318de5d2018-10-06 22:30:50 -0700291 else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
292 bif->lb = BOND_LB_AB;
Steven9cd2d7a2017-12-20 12:43:01 -0800293
294 bif->use_custom_mac = args->hw_addr_set;
295 if (!args->hw_addr_set)
296 {
297 f64 now = vlib_time_now (vm);
298 u32 rnd;
299 rnd = (u32) (now * 1e6);
300 rnd = random_u32 (&rnd);
301
302 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
303 args->hw_addr[0] = 2;
304 args->hw_addr[1] = 0xfe;
305 }
306 memcpy (bif->hw_address, args->hw_addr, 6);
307 args->error = ethernet_register_interface
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500308 (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ ,
Steven9cd2d7a2017-12-20 12:43:01 -0800309 bif->hw_address /* ethernet address */ ,
310 &bif->hw_if_index, 0 /* flag change */ );
311
312 if (args->error)
313 {
314 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500315 hash_unset (bm->id_used, bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800316 pool_put (bm->interfaces, bif);
317 return;
318 }
319
320 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
321 bif->sw_if_index = sw->sw_if_index;
322 bif->group = bif->sw_if_index;
Stevena005e7f2018-03-22 17:46:58 -0700323 if (vlib_get_thread_main ()->n_vlib_mains > 1)
324 clib_spinlock_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -0800325
326 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
327 VNET_HW_INTERFACE_FLAG_LINK_UP);
328
329 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
330
331 // for return
332 args->sw_if_index = bif->sw_if_index;
333}
334
335static clib_error_t *
336bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
337 vlib_cli_command_t * cmd)
338{
339 unformat_input_t _line_input, *line_input = &_line_input;
340 bond_create_if_args_t args = { 0 };
341 u8 mode_is_set = 0;
342
343 /* Get a line of input. */
344 if (!unformat_user (input, unformat_line_input, line_input))
345 return clib_error_return (0, "Missing required arguments.");
346
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500347 args.id = ~0;
Steven9cd2d7a2017-12-20 12:43:01 -0800348 args.mode = -1;
349 args.lb = BOND_LB_L2;
350 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
351 {
352 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
353 mode_is_set = 1;
354 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
355 && unformat (line_input, "load-balance %U",
356 unformat_bond_load_balance, &args.lb))
357 ;
358 else if (unformat (line_input, "hw-addr %U",
359 unformat_ethernet_address, args.hw_addr))
360 args.hw_addr_set = 1;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500361 else if (unformat (line_input, "id %u", &args.id))
362 ;
Steven9cd2d7a2017-12-20 12:43:01 -0800363 else
364 return clib_error_return (0, "unknown input `%U'",
365 format_unformat_error, input);
366 }
367 unformat_free (line_input);
368
369 if (mode_is_set == 0)
370 return clib_error_return (0, "Missing bond mode");
371
372 bond_create_if (vm, &args);
373
374 return args.error;
375}
376
377/* *INDENT-OFF* */
378VLIB_CLI_COMMAND (bond_create_command, static) = {
379 .path = "create bond",
380 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500381 "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>] "
382 "[id <if-id>]",
Steven9cd2d7a2017-12-20 12:43:01 -0800383 .function = bond_create_command_fn,
384};
385/* *INDENT-ON* */
386
387static clib_error_t *
388bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
389 vlib_cli_command_t * cmd)
390{
391 unformat_input_t _line_input, *line_input = &_line_input;
392 u32 sw_if_index = ~0;
393 vnet_main_t *vnm = vnet_get_main ();
394 int rv;
395
396 /* Get a line of input. */
397 if (!unformat_user (input, unformat_line_input, line_input))
398 return clib_error_return (0, "Missing <interface>");
399
400 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
401 {
402 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
403 ;
404 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
405 vnm, &sw_if_index))
406 ;
407 else
408 return clib_error_return (0, "unknown input `%U'",
409 format_unformat_error, input);
410 }
411 unformat_free (line_input);
412
413 if (sw_if_index == ~0)
414 return clib_error_return (0,
415 "please specify interface name or sw_if_index");
416
417 rv = bond_delete_if (vm, sw_if_index);
418 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
419 return clib_error_return (0, "not a bond interface");
420 else if (rv != 0)
421 return clib_error_return (0, "error on deleting bond interface");
422
423 return 0;
424}
425
426/* *INDENT-OFF* */
427VLIB_CLI_COMMAND (bond_delete__command, static) =
428{
429 .path = "delete bond",
430 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
431 .function = bond_delete_command_fn,
432};
433/* *INDENT-ON* */
434
435void
436bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
437{
438 bond_main_t *bm = &bond_main;
439 vnet_main_t *vnm = vnet_get_main ();
440 bond_if_t *bif;
441 slave_if_t *sif;
442 vnet_interface_main_t *im = &vnm->interface_main;
Steven4f8863b2018-04-12 19:36:19 -0700443 vnet_hw_interface_t *bif_hw, *sif_hw;
Steven9cd2d7a2017-12-20 12:43:01 -0800444 vnet_sw_interface_t *sw;
Stevenc4e99c52018-09-27 20:06:26 -0700445 u32 thread_index;
446 u32 sif_if_index;
Steven9cd2d7a2017-12-20 12:43:01 -0800447
448 bif = bond_get_master_by_sw_if_index (args->group);
449 if (!bif)
450 {
451 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
452 args->error = clib_error_return (0, "bond interface not found");
453 return;
454 }
455 // make sure the interface is not already enslaved
456 if (bond_get_slave_by_sw_if_index (args->slave))
457 {
458 args->rv = VNET_API_ERROR_VALUE_EXIST;
459 args->error = clib_error_return (0, "interface was already enslaved");
460 return;
461 }
Steven4f8863b2018-04-12 19:36:19 -0700462 sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
463 if (sif_hw->dev_class_index == bond_dev_class.index)
Steven9cd2d7a2017-12-20 12:43:01 -0800464 {
465 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
466 args->error =
467 clib_error_return (0, "bond interface cannot be enslaved");
468 return;
469 }
470 pool_get (bm->neighbors, sif);
Dave Barachb7b92992018-10-17 10:38:51 -0400471 clib_memset (sif, 0, sizeof (*sif));
Steven9cd2d7a2017-12-20 12:43:01 -0800472 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
473 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
474 sif->sw_if_index = sw->sw_if_index;
475 sif->hw_if_index = sw->hw_if_index;
476 sif->packet_template_index = (u8) ~ 0;
477 sif->is_passive = args->is_passive;
478 sif->group = args->group;
479 sif->bif_dev_instance = bif->dev_instance;
480 sif->mode = bif->mode;
481
482 sif->is_long_timeout = args->is_long_timeout;
483 if (args->is_long_timeout)
484 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
485 else
486 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
487
Steven0d883012018-05-11 11:06:23 -0700488 vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
489 CLIB_CACHE_LINE_BYTES);
490 /*
491 * sif - bm->neighbors may be 0
492 * Left shift it by 1 bit to distinguish the valid entry that we actually
493 * store from the null entries
494 */
495 bm->slave_by_sw_if_index[sif->sw_if_index] =
496 (uword) (((sif - bm->neighbors) << 1) | 1);
Steven9cd2d7a2017-12-20 12:43:01 -0800497 vec_add1 (bif->slaves, sif->sw_if_index);
498
Steven4f8863b2018-04-12 19:36:19 -0700499 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
500
Steven9cd2d7a2017-12-20 12:43:01 -0800501 /* Save the old mac */
Steven4f8863b2018-04-12 19:36:19 -0700502 memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
503 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800504 if (bif->use_custom_mac)
505 {
Steven4f8863b2018-04-12 19:36:19 -0700506 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800507 bif->hw_address);
508 }
509 else
510 {
511 // bond interface gets the mac address from the first slave
512 if (vec_len (bif->slaves) == 1)
513 {
Steven4f8863b2018-04-12 19:36:19 -0700514 memcpy (bif->hw_address, sif_hw->hw_address, 6);
515 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
516 sif_hw->hw_address);
Steven9cd2d7a2017-12-20 12:43:01 -0800517 }
518 else
519 {
520 // subsequent slaves gets the mac address of the bond interface
Steven4f8863b2018-04-12 19:36:19 -0700521 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800522 bif->hw_address);
523 }
524 }
525
Steven4f8863b2018-04-12 19:36:19 -0700526 if (bif_hw->l2_if_count)
527 {
528 ethernet_set_flags (vnm, sif_hw->hw_if_index,
529 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
530 /* ensure all packets go to ethernet-input */
531 ethernet_set_rx_redirect (vnm, sif_hw, 1);
532 }
533
Stevene43278f2019-01-17 15:11:29 -0800534 if (bif->mode == BOND_MODE_LACP)
Steven9cd2d7a2017-12-20 12:43:01 -0800535 {
Stevene43278f2019-01-17 15:11:29 -0800536 if (bm->lacp_enable_disable)
537 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
Steven9cd2d7a2017-12-20 12:43:01 -0800538 }
Stevene43278f2019-01-17 15:11:29 -0800539 else if (sif->port_enabled &&
540 (sif_hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
Steven9cd2d7a2017-12-20 12:43:01 -0800541 {
542 bond_enable_collecting_distributing (vm, sif);
543 }
544
Stevenc4e99c52018-09-27 20:06:26 -0700545 vec_foreach_index (thread_index, bm->per_thread_data)
546 {
547 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
548 thread_index);
549
Damjan Marion69fdfee2018-10-06 14:33:18 +0200550 vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1,
Stevenc4e99c52018-09-27 20:06:26 -0700551 CLIB_CACHE_LINE_BYTES);
552
553 vec_foreach_index (sif_if_index, ptd->per_port_queue)
554 {
555 ptd->per_port_queue[sif_if_index].n_buffers = 0;
556 }
557 }
558
Steven9cd2d7a2017-12-20 12:43:01 -0800559 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
Steven4f8863b2018-04-12 19:36:19 -0700560 sif_hw->hw_if_index, 1, 0, 0);
Steven9cd2d7a2017-12-20 12:43:01 -0800561
562 if (args->rv)
563 {
564 args->error =
565 clib_error_return (0,
566 "Error encountered on input feature arc enable");
567 }
568}
569
570static clib_error_t *
571enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
572 vlib_cli_command_t * cmd)
573{
574 bond_enslave_args_t args = { 0 };
575 unformat_input_t _line_input, *line_input = &_line_input;
576 vnet_main_t *vnm = vnet_get_main ();
577
578 /* Get a line of input. */
579 if (!unformat_user (input, unformat_line_input, line_input))
580 return clib_error_return (0, "Missing required arguments.");
581
582 args.slave = ~0;
583 args.group = ~0;
584 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
585 {
Stevenb3caf552018-03-27 15:04:47 -0700586 if (unformat (line_input, "%U %U",
587 unformat_vnet_sw_interface, vnm, &args.group,
Steven9cd2d7a2017-12-20 12:43:01 -0800588 unformat_vnet_sw_interface, vnm, &args.slave))
589 ;
Steven9cd2d7a2017-12-20 12:43:01 -0800590 else if (unformat (line_input, "passive"))
591 args.is_passive = 1;
592 else if (unformat (line_input, "long-timeout"))
593 args.is_long_timeout = 1;
594 else
595 {
596 args.error = clib_error_return (0, "unknown input `%U'",
597 format_unformat_error, input);
598 break;
599 }
600 }
601 unformat_free (line_input);
602
603 if (args.error)
604 return args.error;
605 if (args.group == ~0)
606 return clib_error_return (0, "Missing bond interface");
607 if (args.slave == ~0)
Stevenb3caf552018-03-27 15:04:47 -0700608 return clib_error_return (0, "please specify valid slave interface name");
Steven9cd2d7a2017-12-20 12:43:01 -0800609
610 bond_enslave (vm, &args);
611
612 return args.error;
613}
614
615/* *INDENT-OFF* */
616VLIB_CLI_COMMAND (enslave_interface_command, static) = {
Stevenb3caf552018-03-27 15:04:47 -0700617 .path = "bond add",
618 .short_help = "bond add <BondEthernetx> <slave-interface> "
619 "[passive] [long-timeout]",
Steven9cd2d7a2017-12-20 12:43:01 -0800620 .function = enslave_interface_command_fn,
621};
622/* *INDENT-ON* */
623
624void
625bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
626{
627 bond_if_t *bif;
628 slave_if_t *sif;
629
630 sif = bond_get_slave_by_sw_if_index (args->slave);
631 if (!sif)
632 {
633 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
634 args->error = clib_error_return (0, "interface was not enslaved");
635 return;
636 }
637 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
638 bond_delete_neighbor (vm, bif, sif);
639}
640
641static clib_error_t *
642detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
643 vlib_cli_command_t * cmd)
644{
645 bond_detach_slave_args_t args = { 0 };
646 unformat_input_t _line_input, *line_input = &_line_input;
647 vnet_main_t *vnm = vnet_get_main ();
648
649 /* Get a line of input. */
650 if (!unformat_user (input, unformat_line_input, line_input))
651 return clib_error_return (0, "Missing required arguments.");
652
653 args.slave = ~0;
654 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
655 {
Stevenb3caf552018-03-27 15:04:47 -0700656 if (unformat (line_input, "%U",
Steven9cd2d7a2017-12-20 12:43:01 -0800657 unformat_vnet_sw_interface, vnm, &args.slave))
658 ;
659 else
660 {
661 args.error = clib_error_return (0, "unknown input `%U'",
662 format_unformat_error, input);
663 break;
664 }
665 }
666 unformat_free (line_input);
667
668 if (args.error)
669 return args.error;
670 if (args.slave == ~0)
Stevenb3caf552018-03-27 15:04:47 -0700671 return clib_error_return (0, "please specify valid slave interface name");
Steven9cd2d7a2017-12-20 12:43:01 -0800672
673 bond_detach_slave (vm, &args);
674
675 return args.error;
676}
677
678/* *INDENT-OFF* */
679VLIB_CLI_COMMAND (detach_interface_command, static) = {
Stevenb3caf552018-03-27 15:04:47 -0700680 .path = "bond del",
681 .short_help = "bond del <slave-interface>",
Steven9cd2d7a2017-12-20 12:43:01 -0800682 .function = detach_interface_command_fn,
683};
684/* *INDENT-ON* */
685
686static void
687show_bond (vlib_main_t * vm)
688{
689 bond_main_t *bm = &bond_main;
690 bond_if_t *bif;
691
Steven318de5d2018-10-06 22:30:50 -0700692 vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
Steven9cd2d7a2017-12-20 12:43:01 -0800693 "interface name", "sw_if_index", "mode",
694 "load balance", "active slaves", "slaves");
695
696 /* *INDENT-OFF* */
697 pool_foreach (bif, bm->interfaces,
698 ({
Steven318de5d2018-10-06 22:30:50 -0700699 vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
Steven9cd2d7a2017-12-20 12:43:01 -0800700 format_bond_interface_name, bif->dev_instance,
701 bif->sw_if_index, format_bond_mode, bif->mode,
702 format_bond_load_balance, bif->lb,
703 vec_len (bif->active_slaves), vec_len (bif->slaves));
704 }));
705 /* *INDENT-ON* */
706}
707
708static void
709show_bond_details (vlib_main_t * vm)
710{
711 bond_main_t *bm = &bond_main;
712 bond_if_t *bif;
713 u32 *sw_if_index;
714
715 /* *INDENT-OFF* */
716 pool_foreach (bif, bm->interfaces,
717 ({
718 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
719 vlib_cli_output (vm, " mode: %U",
720 format_bond_mode, bif->mode);
721 vlib_cli_output (vm, " load balance: %U",
722 format_bond_load_balance, bif->lb);
723 if (bif->mode == BOND_MODE_ROUND_ROBIN)
724 vlib_cli_output (vm, " last xmit slave index: %u",
725 bif->lb_rr_last_index);
726 vlib_cli_output (vm, " number of active slaves: %d",
727 vec_len (bif->active_slaves));
728 vec_foreach (sw_if_index, bif->active_slaves)
729 {
730 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
731 vnet_get_main (), *sw_if_index);
732 }
733 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
734 vec_foreach (sw_if_index, bif->slaves)
735 {
736 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
737 vnet_get_main (), *sw_if_index);
738 }
739 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500740 vlib_cli_output (vm, " interface id: %d", bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800741 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
742 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
743 }));
744 /* *INDENT-ON* */
745}
746
747static clib_error_t *
748show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
749 vlib_cli_command_t * cmd)
750{
751 u8 details = 0;
752
753 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
754 {
755 if (unformat (input, "details"))
756 details = 1;
757 else
758 {
759 return clib_error_return (0, "unknown input `%U'",
760 format_unformat_error, input);
761 }
762 }
763
764 if (details)
765 show_bond_details (vm);
766 else
767 show_bond (vm);
768
769 return 0;
770}
771
772/* *INDENT-OFF* */
773VLIB_CLI_COMMAND (show_bond_command, static) = {
774 .path = "show bond",
775 .short_help = "show bond [details]",
776 .function = show_bond_fn,
777};
778/* *INDENT-ON* */
779
780clib_error_t *
781bond_cli_init (vlib_main_t * vm)
782{
783 bond_main_t *bm = &bond_main;
784
785 bm->vlib_main = vm;
786 bm->vnet_main = vnet_get_main ();
Steven0d883012018-05-11 11:06:23 -0700787 vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
Stevenc4e99c52018-09-27 20:06:26 -0700788 vec_validate_aligned (bm->per_thread_data,
789 vlib_get_thread_main ()->n_vlib_mains - 1,
790 CLIB_CACHE_LINE_BYTES);
Steven9cd2d7a2017-12-20 12:43:01 -0800791
792 return 0;
793}
794
795VLIB_INIT_FUNCTION (bond_cli_init);
796
797/*
798 * fd.io coding-style-patch-verification: ON
799 *
800 * Local Variables:
801 * eval: (c-set-style "gnu")
802 * End:
803 */