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;
204 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
205 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
206 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
208 bif = bond_get_master_by_dev_instance (hw->dev_instance);
210 vec_foreach (sif_sw_if_index, bif->slaves)
212 vec_add1 (s_list, sif_sw_if_index);
215 for (i = 0; i < vec_len (s_list); i++)
217 sif_sw_if_index = s_list[i];
218 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
220 bond_delete_neighbor (vm, bif, sif);
226 /* bring down the interface */
227 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
228 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
230 ethernet_delete_interface (vnm, bif->hw_if_index);
232 clib_bitmap_free (bif->port_number_bitmap);
233 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
234 memset (bif, 0, sizeof (*bif));
235 pool_put (bm->interfaces, bif);
241 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
243 bond_main_t *bm = &bond_main;
244 vnet_main_t *vnm = vnet_get_main ();
245 vnet_sw_interface_t *sw;
248 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
250 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
251 args->error = clib_error_return (0, "LACP plugin is not loaded");
254 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
256 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
257 args->error = clib_error_return (0, "Invalid mode");
260 if (args->lb > BOND_LB_L23)
262 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
263 args->error = clib_error_return (0, "Invalid load-balance");
266 pool_get (bm->interfaces, bif);
267 memset (bif, 0, sizeof (*bif));
268 bif->dev_instance = bif - bm->interfaces;
270 bif->mode = args->mode;
272 // Special load-balance mode used for rr and bc
273 if (bif->mode == BOND_MODE_ROUND_ROBIN)
274 bif->lb = BOND_LB_RR;
275 else if (bif->mode == BOND_MODE_BROADCAST)
276 bif->lb = BOND_LB_BC;
277 else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
278 bif->lb = BOND_LB_AB;
280 bif->use_custom_mac = args->hw_addr_set;
281 if (!args->hw_addr_set)
283 f64 now = vlib_time_now (vm);
285 rnd = (u32) (now * 1e6);
286 rnd = random_u32 (&rnd);
288 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
289 args->hw_addr[0] = 2;
290 args->hw_addr[1] = 0xfe;
292 memcpy (bif->hw_address, args->hw_addr, 6);
293 args->error = ethernet_register_interface
294 (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ ,
295 bif->hw_address /* ethernet address */ ,
296 &bif->hw_if_index, 0 /* flag change */ );
300 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
301 pool_put (bm->interfaces, bif);
305 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
306 bif->sw_if_index = sw->sw_if_index;
307 bif->group = bif->sw_if_index;
308 if (vlib_get_thread_main ()->n_vlib_mains > 1)
309 clib_spinlock_init (&bif->lockp);
311 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
312 VNET_HW_INTERFACE_FLAG_LINK_UP);
314 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
317 args->sw_if_index = bif->sw_if_index;
320 static clib_error_t *
321 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
322 vlib_cli_command_t * cmd)
324 unformat_input_t _line_input, *line_input = &_line_input;
325 bond_create_if_args_t args = { 0 };
328 /* Get a line of input. */
329 if (!unformat_user (input, unformat_line_input, line_input))
330 return clib_error_return (0, "Missing required arguments.");
333 args.lb = BOND_LB_L2;
334 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
336 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
338 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
339 && unformat (line_input, "load-balance %U",
340 unformat_bond_load_balance, &args.lb))
342 else if (unformat (line_input, "hw-addr %U",
343 unformat_ethernet_address, args.hw_addr))
344 args.hw_addr_set = 1;
346 return clib_error_return (0, "unknown input `%U'",
347 format_unformat_error, input);
349 unformat_free (line_input);
351 if (mode_is_set == 0)
352 return clib_error_return (0, "Missing bond mode");
354 bond_create_if (vm, &args);
360 VLIB_CLI_COMMAND (bond_create_command, static) = {
361 .path = "create bond",
362 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
363 "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]",
364 .function = bond_create_command_fn,
368 static clib_error_t *
369 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
370 vlib_cli_command_t * cmd)
372 unformat_input_t _line_input, *line_input = &_line_input;
373 u32 sw_if_index = ~0;
374 vnet_main_t *vnm = vnet_get_main ();
377 /* Get a line of input. */
378 if (!unformat_user (input, unformat_line_input, line_input))
379 return clib_error_return (0, "Missing <interface>");
381 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
383 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
385 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
389 return clib_error_return (0, "unknown input `%U'",
390 format_unformat_error, input);
392 unformat_free (line_input);
394 if (sw_if_index == ~0)
395 return clib_error_return (0,
396 "please specify interface name or sw_if_index");
398 rv = bond_delete_if (vm, sw_if_index);
399 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
400 return clib_error_return (0, "not a bond interface");
402 return clib_error_return (0, "error on deleting bond interface");
408 VLIB_CLI_COMMAND (bond_delete__command, static) =
410 .path = "delete bond",
411 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
412 .function = bond_delete_command_fn,
417 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
419 bond_main_t *bm = &bond_main;
420 vnet_main_t *vnm = vnet_get_main ();
423 vnet_interface_main_t *im = &vnm->interface_main;
424 vnet_hw_interface_t *bif_hw, *sif_hw;
425 vnet_sw_interface_t *sw;
429 bif = bond_get_master_by_sw_if_index (args->group);
432 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
433 args->error = clib_error_return (0, "bond interface not found");
436 // make sure the interface is not already enslaved
437 if (bond_get_slave_by_sw_if_index (args->slave))
439 args->rv = VNET_API_ERROR_VALUE_EXIST;
440 args->error = clib_error_return (0, "interface was already enslaved");
443 sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
444 if (sif_hw->dev_class_index == bond_dev_class.index)
446 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
448 clib_error_return (0, "bond interface cannot be enslaved");
451 pool_get (bm->neighbors, sif);
452 memset (sif, 0, sizeof (*sif));
453 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
454 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
455 sif->sw_if_index = sw->sw_if_index;
456 sif->hw_if_index = sw->hw_if_index;
457 sif->packet_template_index = (u8) ~ 0;
458 sif->is_passive = args->is_passive;
459 sif->group = args->group;
460 sif->bif_dev_instance = bif->dev_instance;
461 sif->mode = bif->mode;
463 sif->is_long_timeout = args->is_long_timeout;
464 if (args->is_long_timeout)
465 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
467 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
469 vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
470 CLIB_CACHE_LINE_BYTES);
472 * sif - bm->neighbors may be 0
473 * Left shift it by 1 bit to distinguish the valid entry that we actually
474 * store from the null entries
476 bm->slave_by_sw_if_index[sif->sw_if_index] =
477 (uword) (((sif - bm->neighbors) << 1) | 1);
478 vec_add1 (bif->slaves, sif->sw_if_index);
480 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
482 /* Save the old mac */
483 memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
484 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
485 if (bif->use_custom_mac)
487 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
492 // bond interface gets the mac address from the first slave
493 if (vec_len (bif->slaves) == 1)
495 memcpy (bif->hw_address, sif_hw->hw_address, 6);
496 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
501 // subsequent slaves gets the mac address of the bond interface
502 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
507 if (bif_hw->l2_if_count)
509 ethernet_set_flags (vnm, sif_hw->hw_if_index,
510 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
511 /* ensure all packets go to ethernet-input */
512 ethernet_set_rx_redirect (vnm, sif_hw, 1);
515 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
517 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
521 bond_enable_collecting_distributing (vm, sif);
524 vec_foreach_index (thread_index, bm->per_thread_data)
526 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
529 vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1,
530 CLIB_CACHE_LINE_BYTES);
532 vec_foreach_index (sif_if_index, ptd->per_port_queue)
534 ptd->per_port_queue[sif_if_index].n_buffers = 0;
538 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
539 sif_hw->hw_if_index, 1, 0, 0);
544 clib_error_return (0,
545 "Error encountered on input feature arc enable");
549 static clib_error_t *
550 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
551 vlib_cli_command_t * cmd)
553 bond_enslave_args_t args = { 0 };
554 unformat_input_t _line_input, *line_input = &_line_input;
555 vnet_main_t *vnm = vnet_get_main ();
557 /* Get a line of input. */
558 if (!unformat_user (input, unformat_line_input, line_input))
559 return clib_error_return (0, "Missing required arguments.");
563 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
565 if (unformat (line_input, "%U %U",
566 unformat_vnet_sw_interface, vnm, &args.group,
567 unformat_vnet_sw_interface, vnm, &args.slave))
569 else if (unformat (line_input, "passive"))
571 else if (unformat (line_input, "long-timeout"))
572 args.is_long_timeout = 1;
575 args.error = clib_error_return (0, "unknown input `%U'",
576 format_unformat_error, input);
580 unformat_free (line_input);
584 if (args.group == ~0)
585 return clib_error_return (0, "Missing bond interface");
586 if (args.slave == ~0)
587 return clib_error_return (0, "please specify valid slave interface name");
589 bond_enslave (vm, &args);
595 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
597 .short_help = "bond add <BondEthernetx> <slave-interface> "
598 "[passive] [long-timeout]",
599 .function = enslave_interface_command_fn,
604 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
609 sif = bond_get_slave_by_sw_if_index (args->slave);
612 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
613 args->error = clib_error_return (0, "interface was not enslaved");
616 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
617 bond_delete_neighbor (vm, bif, sif);
620 static clib_error_t *
621 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
622 vlib_cli_command_t * cmd)
624 bond_detach_slave_args_t args = { 0 };
625 unformat_input_t _line_input, *line_input = &_line_input;
626 vnet_main_t *vnm = vnet_get_main ();
628 /* Get a line of input. */
629 if (!unformat_user (input, unformat_line_input, line_input))
630 return clib_error_return (0, "Missing required arguments.");
633 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
635 if (unformat (line_input, "%U",
636 unformat_vnet_sw_interface, vnm, &args.slave))
640 args.error = clib_error_return (0, "unknown input `%U'",
641 format_unformat_error, input);
645 unformat_free (line_input);
649 if (args.slave == ~0)
650 return clib_error_return (0, "please specify valid slave interface name");
652 bond_detach_slave (vm, &args);
658 VLIB_CLI_COMMAND (detach_interface_command, static) = {
660 .short_help = "bond del <slave-interface>",
661 .function = detach_interface_command_fn,
666 show_bond (vlib_main_t * vm)
668 bond_main_t *bm = &bond_main;
671 vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
672 "interface name", "sw_if_index", "mode",
673 "load balance", "active slaves", "slaves");
676 pool_foreach (bif, bm->interfaces,
678 vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
679 format_bond_interface_name, bif->dev_instance,
680 bif->sw_if_index, format_bond_mode, bif->mode,
681 format_bond_load_balance, bif->lb,
682 vec_len (bif->active_slaves), vec_len (bif->slaves));
688 show_bond_details (vlib_main_t * vm)
690 bond_main_t *bm = &bond_main;
695 pool_foreach (bif, bm->interfaces,
697 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
698 vlib_cli_output (vm, " mode: %U",
699 format_bond_mode, bif->mode);
700 vlib_cli_output (vm, " load balance: %U",
701 format_bond_load_balance, bif->lb);
702 if (bif->mode == BOND_MODE_ROUND_ROBIN)
703 vlib_cli_output (vm, " last xmit slave index: %u",
704 bif->lb_rr_last_index);
705 vlib_cli_output (vm, " number of active slaves: %d",
706 vec_len (bif->active_slaves));
707 vec_foreach (sw_if_index, bif->active_slaves)
709 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
710 vnet_get_main (), *sw_if_index);
712 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
713 vec_foreach (sw_if_index, bif->slaves)
715 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
716 vnet_get_main (), *sw_if_index);
718 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
719 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
720 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
725 static clib_error_t *
726 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
727 vlib_cli_command_t * cmd)
731 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
733 if (unformat (input, "details"))
737 return clib_error_return (0, "unknown input `%U'",
738 format_unformat_error, input);
743 show_bond_details (vm);
751 VLIB_CLI_COMMAND (show_bond_command, static) = {
753 .short_help = "show bond [details]",
754 .function = show_bond_fn,
759 bond_cli_init (vlib_main_t * vm)
761 bond_main_t *bm = &bond_main;
764 bm->vnet_main = vnet_get_main ();
765 vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
766 vec_validate_aligned (bm->per_thread_data,
767 vlib_get_thread_main ()->n_vlib_mains - 1,
768 CLIB_CACHE_LINE_BYTES);
773 VLIB_INIT_FUNCTION (bond_cli_init);
776 * fd.io coding-style-patch-verification: ON
779 * eval: (c-set-style "gnu")