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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <vlib/vlib.h>
20 #include <vlib/unix/unix.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/bonding/node.h>
25 bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
27 bond_main_t *bm = &bond_main;
31 u8 switching_active = 0;
33 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
34 clib_spinlock_lock_if_init (&bif->lockp);
35 vec_foreach_index (i, bif->active_slaves)
37 p = *vec_elt_at_index (bif->active_slaves, i);
38 if (p == sif->sw_if_index)
40 /* Are we disabling the very 1st slave? */
41 if (sif->sw_if_index == *vec_elt_at_index (bif->active_slaves, 0))
44 vec_del1 (bif->active_slaves, i);
45 hash_unset (bif->active_slave_by_sw_if_index, sif->sw_if_index);
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);
55 clib_spinlock_unlock_if_init (&bif->lockp);
59 bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
62 bond_main_t *bm = &bond_main;
64 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
65 clib_spinlock_lock_if_init (&bif->lockp);
66 if (!hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index))
68 hash_set (bif->active_slave_by_sw_if_index, sif->sw_if_index,
70 vec_add1 (bif->active_slaves, sif->sw_if_index);
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);
78 clib_spinlock_unlock_if_init (&bif->lockp);
82 bond_dump_ifs (bond_interface_details_t ** out_bondifs)
84 vnet_main_t *vnm = vnet_get_main ();
85 bond_main_t *bm = &bond_main;
87 vnet_hw_interface_t *hi;
88 bond_interface_details_t *r_bondifs = NULL;
89 bond_interface_details_t *bondif = NULL;
92 pool_foreach (bif, bm->interfaces,
93 vec_add2(r_bondifs, bondif, 1);
94 memset (bondif, 0, sizeof (*bondif));
95 bondif->sw_if_index = bif->sw_if_index;
96 hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
97 clib_memcpy(bondif->interface_name, hi->name,
98 MIN (ARRAY_LEN (bondif->interface_name) - 1,
99 strlen ((const char *) hi->name)));
100 bondif->mode = bif->mode;
101 bondif->lb = bif->lb;
102 bondif->active_slaves = vec_len (bif->active_slaves);
103 bondif->slaves = vec_len (bif->slaves);
107 *out_bondifs = r_bondifs;
113 bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
114 u32 bond_sw_if_index)
116 vnet_main_t *vnm = vnet_get_main ();
118 vnet_hw_interface_t *hi;
119 vnet_sw_interface_t *sw;
120 slave_interface_details_t *r_slaveifs = NULL;
121 slave_interface_details_t *slaveif = NULL;
122 u32 *sw_if_index = NULL;
125 bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
129 vec_foreach (sw_if_index, bif->slaves)
131 vec_add2 (r_slaveifs, slaveif, 1);
132 memset (slaveif, 0, sizeof (*slaveif));
133 sif = bond_get_slave_by_sw_if_index (*sw_if_index);
136 sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
137 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
138 clib_memcpy (slaveif->interface_name, hi->name,
139 MIN (ARRAY_LEN (slaveif->interface_name) - 1,
140 strlen ((const char *) hi->name)));
141 slaveif->sw_if_index = sif->sw_if_index;
142 slaveif->is_passive = sif->is_passive;
143 slaveif->is_long_timeout = sif->is_long_timeout;
146 *out_slaveifs = r_slaveifs;
152 bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
154 bond_main_t *bm = &bond_main;
155 vnet_main_t *vnm = vnet_get_main ();
157 vnet_hw_interface_t *sif_hw;
159 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
161 bif->port_number_bitmap =
162 clib_bitmap_set (bif->port_number_bitmap,
163 ntohs (sif->actor_admin.port_number) - 1, 0);
164 bm->slave_by_sw_if_index[sif->sw_if_index] = 0;
165 vec_free (sif->last_marker_pkt);
166 vec_free (sif->last_rx_pkt);
167 vec_foreach_index (i, bif->slaves)
169 uword p = *vec_elt_at_index (bif->slaves, i);
170 if (p == sif->sw_if_index)
172 vec_del1 (bif->slaves, i);
177 bond_disable_collecting_distributing (vm, sif);
179 vnet_feature_enable_disable ("device-input", "bond-input",
180 sif_hw->hw_if_index, 0, 0, 0);
182 /* Put back the old mac */
183 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
184 sif->persistent_hw_address);
186 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
187 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
189 pool_put (bm->neighbors, sif);
193 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
195 bond_main_t *bm = &bond_main;
196 vnet_main_t *vnm = vnet_get_main ();
199 vnet_hw_interface_t *hw;
200 u32 *sif_sw_if_index;
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;
209 bif = bond_get_master_by_dev_instance (hw->dev_instance);
211 vec_foreach (sif_sw_if_index, bif->slaves)
213 vec_add1 (s_list, sif_sw_if_index);
216 for (i = 0; i < vec_len (s_list); i++)
218 sif_sw_if_index = s_list[i];
219 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
221 bond_delete_neighbor (vm, bif, sif);
227 /* 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);
231 ethernet_delete_interface (vnm, bif->hw_if_index);
233 clib_bitmap_free (bif->port_number_bitmap);
234 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
235 for (thread_index = 0; thread_index < vlib_get_thread_main ()->n_vlib_mains;
238 vec_free (bif->per_thread_info[thread_index].frame);
240 vec_free (bif->per_thread_info);
241 memset (bif, 0, sizeof (*bif));
242 pool_put (bm->interfaces, bif);
248 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
250 bond_main_t *bm = &bond_main;
251 vnet_main_t *vnm = vnet_get_main ();
252 vnet_sw_interface_t *sw;
255 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
257 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
258 args->error = clib_error_return (0, "LACP plugin is not loaded");
261 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
263 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
264 args->error = clib_error_return (0, "Invalid mode");
267 if (args->lb > BOND_LB_L23)
269 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
270 args->error = clib_error_return (0, "Invalid load-balance");
273 pool_get (bm->interfaces, bif);
274 memset (bif, 0, sizeof (*bif));
275 bif->dev_instance = bif - bm->interfaces;
277 bif->mode = args->mode;
279 // Special load-balance mode used for rr and bc
280 if (bif->mode == BOND_MODE_ROUND_ROBIN)
281 bif->lb = BOND_LB_RR;
282 else if (bif->mode == BOND_MODE_BROADCAST)
283 bif->lb = BOND_LB_BC;
285 bif->use_custom_mac = args->hw_addr_set;
286 if (!args->hw_addr_set)
288 f64 now = vlib_time_now (vm);
290 rnd = (u32) (now * 1e6);
291 rnd = random_u32 (&rnd);
293 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
294 args->hw_addr[0] = 2;
295 args->hw_addr[1] = 0xfe;
297 memcpy (bif->hw_address, args->hw_addr, 6);
298 args->error = ethernet_register_interface
299 (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ ,
300 bif->hw_address /* ethernet address */ ,
301 &bif->hw_if_index, 0 /* flag change */ );
305 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
306 pool_put (bm->interfaces, bif);
310 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
311 bif->sw_if_index = sw->sw_if_index;
312 bif->group = bif->sw_if_index;
313 vec_validate_aligned (bif->per_thread_info,
314 vlib_get_thread_main ()->n_vlib_mains - 1,
315 CLIB_CACHE_LINE_BYTES);
316 if (vlib_get_thread_main ()->n_vlib_mains > 1)
317 clib_spinlock_init (&bif->lockp);
319 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
320 VNET_HW_INTERFACE_FLAG_LINK_UP);
322 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
325 args->sw_if_index = bif->sw_if_index;
328 static clib_error_t *
329 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
330 vlib_cli_command_t * cmd)
332 unformat_input_t _line_input, *line_input = &_line_input;
333 bond_create_if_args_t args = { 0 };
336 /* Get a line of input. */
337 if (!unformat_user (input, unformat_line_input, line_input))
338 return clib_error_return (0, "Missing required arguments.");
341 args.lb = BOND_LB_L2;
342 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
344 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
346 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
347 && unformat (line_input, "load-balance %U",
348 unformat_bond_load_balance, &args.lb))
350 else if (unformat (line_input, "hw-addr %U",
351 unformat_ethernet_address, args.hw_addr))
352 args.hw_addr_set = 1;
354 return clib_error_return (0, "unknown input `%U'",
355 format_unformat_error, input);
357 unformat_free (line_input);
359 if (mode_is_set == 0)
360 return clib_error_return (0, "Missing bond mode");
362 bond_create_if (vm, &args);
368 VLIB_CLI_COMMAND (bond_create_command, static) = {
369 .path = "create bond",
370 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
371 "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]",
372 .function = bond_create_command_fn,
376 static clib_error_t *
377 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
378 vlib_cli_command_t * cmd)
380 unformat_input_t _line_input, *line_input = &_line_input;
381 u32 sw_if_index = ~0;
382 vnet_main_t *vnm = vnet_get_main ();
385 /* Get a line of input. */
386 if (!unformat_user (input, unformat_line_input, line_input))
387 return clib_error_return (0, "Missing <interface>");
389 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
391 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
393 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
397 return clib_error_return (0, "unknown input `%U'",
398 format_unformat_error, input);
400 unformat_free (line_input);
402 if (sw_if_index == ~0)
403 return clib_error_return (0,
404 "please specify interface name or sw_if_index");
406 rv = bond_delete_if (vm, sw_if_index);
407 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
408 return clib_error_return (0, "not a bond interface");
410 return clib_error_return (0, "error on deleting bond interface");
416 VLIB_CLI_COMMAND (bond_delete__command, static) =
418 .path = "delete bond",
419 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
420 .function = bond_delete_command_fn,
425 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
427 bond_main_t *bm = &bond_main;
428 vnet_main_t *vnm = vnet_get_main ();
431 vnet_interface_main_t *im = &vnm->interface_main;
432 vnet_hw_interface_t *bif_hw, *sif_hw;
433 vnet_sw_interface_t *sw;
435 bif = bond_get_master_by_sw_if_index (args->group);
438 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
439 args->error = clib_error_return (0, "bond interface not found");
442 // make sure the interface is not already enslaved
443 if (bond_get_slave_by_sw_if_index (args->slave))
445 args->rv = VNET_API_ERROR_VALUE_EXIST;
446 args->error = clib_error_return (0, "interface was already enslaved");
449 sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
450 if (sif_hw->dev_class_index == bond_dev_class.index)
452 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
454 clib_error_return (0, "bond interface cannot be enslaved");
457 pool_get (bm->neighbors, sif);
458 memset (sif, 0, sizeof (*sif));
459 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
460 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
461 sif->sw_if_index = sw->sw_if_index;
462 sif->hw_if_index = sw->hw_if_index;
463 sif->packet_template_index = (u8) ~ 0;
464 sif->is_passive = args->is_passive;
465 sif->group = args->group;
466 sif->bif_dev_instance = bif->dev_instance;
467 sif->mode = bif->mode;
469 sif->is_long_timeout = args->is_long_timeout;
470 if (args->is_long_timeout)
471 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
473 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
475 vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
476 CLIB_CACHE_LINE_BYTES);
478 * sif - bm->neighbors may be 0
479 * Left shift it by 1 bit to distinguish the valid entry that we actually
480 * store from the null entries
482 bm->slave_by_sw_if_index[sif->sw_if_index] =
483 (uword) (((sif - bm->neighbors) << 1) | 1);
484 vec_add1 (bif->slaves, sif->sw_if_index);
486 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
488 /* Save the old mac */
489 memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
490 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
491 if (bif->use_custom_mac)
493 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
498 // bond interface gets the mac address from the first slave
499 if (vec_len (bif->slaves) == 1)
501 memcpy (bif->hw_address, sif_hw->hw_address, 6);
502 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
507 // subsequent slaves gets the mac address of the bond interface
508 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
513 if (bif_hw->l2_if_count)
515 ethernet_set_flags (vnm, sif_hw->hw_if_index,
516 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
517 /* ensure all packets go to ethernet-input */
518 ethernet_set_rx_redirect (vnm, sif_hw, 1);
521 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
523 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
527 bond_enable_collecting_distributing (vm, sif);
530 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
531 sif_hw->hw_if_index, 1, 0, 0);
536 clib_error_return (0,
537 "Error encountered on input feature arc enable");
541 static clib_error_t *
542 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
543 vlib_cli_command_t * cmd)
545 bond_enslave_args_t args = { 0 };
546 unformat_input_t _line_input, *line_input = &_line_input;
547 vnet_main_t *vnm = vnet_get_main ();
549 /* Get a line of input. */
550 if (!unformat_user (input, unformat_line_input, line_input))
551 return clib_error_return (0, "Missing required arguments.");
555 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
557 if (unformat (line_input, "%U %U",
558 unformat_vnet_sw_interface, vnm, &args.group,
559 unformat_vnet_sw_interface, vnm, &args.slave))
561 else if (unformat (line_input, "passive"))
563 else if (unformat (line_input, "long-timeout"))
564 args.is_long_timeout = 1;
567 args.error = clib_error_return (0, "unknown input `%U'",
568 format_unformat_error, input);
572 unformat_free (line_input);
576 if (args.group == ~0)
577 return clib_error_return (0, "Missing bond interface");
578 if (args.slave == ~0)
579 return clib_error_return (0, "please specify valid slave interface name");
581 bond_enslave (vm, &args);
587 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
589 .short_help = "bond add <BondEthernetx> <slave-interface> "
590 "[passive] [long-timeout]",
591 .function = enslave_interface_command_fn,
596 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
601 sif = bond_get_slave_by_sw_if_index (args->slave);
604 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
605 args->error = clib_error_return (0, "interface was not enslaved");
608 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
609 bond_delete_neighbor (vm, bif, sif);
612 static clib_error_t *
613 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
614 vlib_cli_command_t * cmd)
616 bond_detach_slave_args_t args = { 0 };
617 unformat_input_t _line_input, *line_input = &_line_input;
618 vnet_main_t *vnm = vnet_get_main ();
620 /* Get a line of input. */
621 if (!unformat_user (input, unformat_line_input, line_input))
622 return clib_error_return (0, "Missing required arguments.");
625 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
627 if (unformat (line_input, "%U",
628 unformat_vnet_sw_interface, vnm, &args.slave))
632 args.error = clib_error_return (0, "unknown input `%U'",
633 format_unformat_error, input);
637 unformat_free (line_input);
641 if (args.slave == ~0)
642 return clib_error_return (0, "please specify valid slave interface name");
644 bond_detach_slave (vm, &args);
650 VLIB_CLI_COMMAND (detach_interface_command, static) = {
652 .short_help = "bond del <slave-interface>",
653 .function = detach_interface_command_fn,
658 show_bond (vlib_main_t * vm)
660 bond_main_t *bm = &bond_main;
663 vlib_cli_output (vm, "%-16s %-12s %-12s %-13s %-14s %s",
664 "interface name", "sw_if_index", "mode",
665 "load balance", "active slaves", "slaves");
668 pool_foreach (bif, bm->interfaces,
670 vlib_cli_output (vm, "%-16U %-12d %-12U %-13U %-14u %u",
671 format_bond_interface_name, bif->dev_instance,
672 bif->sw_if_index, format_bond_mode, bif->mode,
673 format_bond_load_balance, bif->lb,
674 vec_len (bif->active_slaves), vec_len (bif->slaves));
680 show_bond_details (vlib_main_t * vm)
682 bond_main_t *bm = &bond_main;
687 pool_foreach (bif, bm->interfaces,
689 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
690 vlib_cli_output (vm, " mode: %U",
691 format_bond_mode, bif->mode);
692 vlib_cli_output (vm, " load balance: %U",
693 format_bond_load_balance, bif->lb);
694 if (bif->mode == BOND_MODE_ROUND_ROBIN)
695 vlib_cli_output (vm, " last xmit slave index: %u",
696 bif->lb_rr_last_index);
697 vlib_cli_output (vm, " number of active slaves: %d",
698 vec_len (bif->active_slaves));
699 vec_foreach (sw_if_index, bif->active_slaves)
701 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
702 vnet_get_main (), *sw_if_index);
704 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
705 vec_foreach (sw_if_index, bif->slaves)
707 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
708 vnet_get_main (), *sw_if_index);
710 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
711 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
712 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
717 static clib_error_t *
718 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
719 vlib_cli_command_t * cmd)
723 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
725 if (unformat (input, "details"))
729 return clib_error_return (0, "unknown input `%U'",
730 format_unformat_error, input);
735 show_bond_details (vm);
743 VLIB_CLI_COMMAND (show_bond_command, static) = {
745 .short_help = "show bond [details]",
746 .function = show_bond_fn,
751 bond_cli_init (vlib_main_t * vm)
753 bond_main_t *bm = &bond_main;
756 bm->vnet_main = vnet_get_main ();
757 vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
762 VLIB_INIT_FUNCTION (bond_cli_init);
765 * fd.io coding-style-patch-verification: ON
768 * eval: (c-set-style "gnu")