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)
31 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
32 clib_spinlock_lock_if_init (&bif->lockp);
33 vec_foreach_index (i, bif->active_slaves)
35 p = *vec_elt_at_index (bif->active_slaves, i);
36 if (p == sif->sw_if_index)
38 vec_del1 (bif->active_slaves, i);
39 hash_unset (bif->active_slave_by_sw_if_index, sif->sw_if_index);
43 clib_spinlock_unlock_if_init (&bif->lockp);
47 bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
51 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
52 clib_spinlock_lock_if_init (&bif->lockp);
53 if (!hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index))
55 hash_set (bif->active_slave_by_sw_if_index, sif->sw_if_index,
57 vec_add1 (bif->active_slaves, sif->sw_if_index);
59 clib_spinlock_unlock_if_init (&bif->lockp);
63 bond_dump_ifs (bond_interface_details_t ** out_bondifs)
65 vnet_main_t *vnm = vnet_get_main ();
66 bond_main_t *bm = &bond_main;
68 vnet_hw_interface_t *hi;
69 bond_interface_details_t *r_bondifs = NULL;
70 bond_interface_details_t *bondif = NULL;
73 pool_foreach (bif, bm->interfaces,
74 vec_add2(r_bondifs, bondif, 1);
75 memset (bondif, 0, sizeof (*bondif));
76 bondif->sw_if_index = bif->sw_if_index;
77 hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
78 clib_memcpy(bondif->interface_name, hi->name,
79 MIN (ARRAY_LEN (bondif->interface_name) - 1,
80 strlen ((const char *) hi->name)));
81 bondif->mode = bif->mode;
83 bondif->active_slaves = vec_len (bif->active_slaves);
84 bondif->slaves = vec_len (bif->slaves);
88 *out_bondifs = r_bondifs;
94 bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
97 vnet_main_t *vnm = vnet_get_main ();
99 vnet_hw_interface_t *hi;
100 vnet_sw_interface_t *sw;
101 slave_interface_details_t *r_slaveifs = NULL;
102 slave_interface_details_t *slaveif = NULL;
103 u32 *sw_if_index = NULL;
106 bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
110 vec_foreach (sw_if_index, bif->slaves)
112 vec_add2 (r_slaveifs, slaveif, 1);
113 memset (slaveif, 0, sizeof (*slaveif));
114 sif = bond_get_slave_by_sw_if_index (*sw_if_index);
117 sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
118 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
119 clib_memcpy (slaveif->interface_name, hi->name,
120 MIN (ARRAY_LEN (slaveif->interface_name) - 1,
121 strlen ((const char *) hi->name)));
122 slaveif->sw_if_index = sif->sw_if_index;
123 slaveif->is_passive = sif->is_passive;
124 slaveif->is_long_timeout = sif->is_long_timeout;
127 *out_slaveifs = r_slaveifs;
133 bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
135 bond_main_t *bm = &bond_main;
136 vnet_main_t *vnm = vnet_get_main ();
138 vnet_hw_interface_t *sif_hw, *bif_hw;
140 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
141 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
143 bif->port_number_bitmap =
144 clib_bitmap_set (bif->port_number_bitmap,
145 ntohs (sif->actor_admin.port_number) - 1, 0);
146 hash_unset (bm->neighbor_by_sw_if_index, sif->sw_if_index);
147 vec_free (sif->last_marker_pkt);
148 vec_free (sif->last_rx_pkt);
149 vec_foreach_index (i, bif->slaves)
151 uword p = *vec_elt_at_index (bif->slaves, i);
152 if (p == sif->sw_if_index)
154 vec_del1 (bif->slaves, i);
159 if (bif_hw->l2_if_count)
161 ethernet_set_flags (vnm, sif_hw->hw_if_index, 0);
162 /* Allow ip packets to go directly to ip4-input etc */
163 ethernet_set_rx_redirect (vnm, sif_hw, 0);
166 bond_disable_collecting_distributing (vm, sif);
168 /* Put back the old mac */
169 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
170 sif->persistent_hw_address);
172 pool_put (bm->neighbors, sif);
174 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
175 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
179 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
181 bond_main_t *bm = &bond_main;
182 vnet_main_t *vnm = vnet_get_main ();
185 vnet_hw_interface_t *hw;
186 u32 *sif_sw_if_index;
191 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
192 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
193 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
195 bif = bond_get_master_by_dev_instance (hw->dev_instance);
197 vec_foreach (sif_sw_if_index, bif->slaves)
199 vec_add1 (s_list, sif_sw_if_index);
202 for (i = 0; i < vec_len (s_list); i++)
204 sif_sw_if_index = s_list[i];
205 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
207 bond_delete_neighbor (vm, bif, sif);
213 /* bring down the interface */
214 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
215 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
217 ethernet_delete_interface (vnm, bif->hw_if_index);
219 clib_bitmap_free (bif->port_number_bitmap);
220 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
221 for (thread_index = 0; thread_index < vlib_get_thread_main ()->n_vlib_mains;
224 vec_free (bif->per_thread_info[thread_index].frame);
226 vec_free (bif->per_thread_info);
227 memset (bif, 0, sizeof (*bif));
228 pool_put (bm->interfaces, bif);
234 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
236 bond_main_t *bm = &bond_main;
237 vnet_main_t *vnm = vnet_get_main ();
238 vnet_sw_interface_t *sw;
241 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
243 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
244 args->error = clib_error_return (0, "LACP plugin is not loaded");
247 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
249 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
250 args->error = clib_error_return (0, "Invalid mode");
253 if (args->lb > BOND_LB_L23)
255 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
256 args->error = clib_error_return (0, "Invalid load-balance");
259 pool_get (bm->interfaces, bif);
260 memset (bif, 0, sizeof (*bif));
261 bif->dev_instance = bif - bm->interfaces;
263 bif->mode = args->mode;
265 // Special load-balance mode used for rr and bc
266 if (bif->mode == BOND_MODE_ROUND_ROBIN)
267 bif->lb = BOND_LB_RR;
268 else if (bif->mode == BOND_MODE_BROADCAST)
269 bif->lb = BOND_LB_BC;
271 bif->use_custom_mac = args->hw_addr_set;
272 if (!args->hw_addr_set)
274 f64 now = vlib_time_now (vm);
276 rnd = (u32) (now * 1e6);
277 rnd = random_u32 (&rnd);
279 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
280 args->hw_addr[0] = 2;
281 args->hw_addr[1] = 0xfe;
283 memcpy (bif->hw_address, args->hw_addr, 6);
284 args->error = ethernet_register_interface
285 (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ ,
286 bif->hw_address /* ethernet address */ ,
287 &bif->hw_if_index, 0 /* flag change */ );
291 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
292 pool_put (bm->interfaces, bif);
296 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
297 bif->sw_if_index = sw->sw_if_index;
298 bif->group = bif->sw_if_index;
299 vec_validate_aligned (bif->per_thread_info,
300 vlib_get_thread_main ()->n_vlib_mains - 1,
301 CLIB_CACHE_LINE_BYTES);
302 if (vlib_get_thread_main ()->n_vlib_mains > 1)
303 clib_spinlock_init (&bif->lockp);
305 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
306 VNET_HW_INTERFACE_FLAG_LINK_UP);
308 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
311 args->sw_if_index = bif->sw_if_index;
314 static clib_error_t *
315 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
316 vlib_cli_command_t * cmd)
318 unformat_input_t _line_input, *line_input = &_line_input;
319 bond_create_if_args_t args = { 0 };
322 /* Get a line of input. */
323 if (!unformat_user (input, unformat_line_input, line_input))
324 return clib_error_return (0, "Missing required arguments.");
327 args.lb = BOND_LB_L2;
328 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
330 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
332 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
333 && unformat (line_input, "load-balance %U",
334 unformat_bond_load_balance, &args.lb))
336 else if (unformat (line_input, "hw-addr %U",
337 unformat_ethernet_address, args.hw_addr))
338 args.hw_addr_set = 1;
340 return clib_error_return (0, "unknown input `%U'",
341 format_unformat_error, input);
343 unformat_free (line_input);
345 if (mode_is_set == 0)
346 return clib_error_return (0, "Missing bond mode");
348 bond_create_if (vm, &args);
354 VLIB_CLI_COMMAND (bond_create_command, static) = {
355 .path = "create bond",
356 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
357 "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]",
358 .function = bond_create_command_fn,
362 static clib_error_t *
363 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
364 vlib_cli_command_t * cmd)
366 unformat_input_t _line_input, *line_input = &_line_input;
367 u32 sw_if_index = ~0;
368 vnet_main_t *vnm = vnet_get_main ();
371 /* Get a line of input. */
372 if (!unformat_user (input, unformat_line_input, line_input))
373 return clib_error_return (0, "Missing <interface>");
375 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
377 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
379 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
383 return clib_error_return (0, "unknown input `%U'",
384 format_unformat_error, input);
386 unformat_free (line_input);
388 if (sw_if_index == ~0)
389 return clib_error_return (0,
390 "please specify interface name or sw_if_index");
392 rv = bond_delete_if (vm, sw_if_index);
393 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
394 return clib_error_return (0, "not a bond interface");
396 return clib_error_return (0, "error on deleting bond interface");
402 VLIB_CLI_COMMAND (bond_delete__command, static) =
404 .path = "delete bond",
405 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
406 .function = bond_delete_command_fn,
411 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
413 bond_main_t *bm = &bond_main;
414 vnet_main_t *vnm = vnet_get_main ();
417 vnet_interface_main_t *im = &vnm->interface_main;
418 vnet_hw_interface_t *bif_hw, *sif_hw;
419 vnet_sw_interface_t *sw;
421 bif = bond_get_master_by_sw_if_index (args->group);
424 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
425 args->error = clib_error_return (0, "bond interface not found");
428 // make sure the interface is not already enslaved
429 if (bond_get_slave_by_sw_if_index (args->slave))
431 args->rv = VNET_API_ERROR_VALUE_EXIST;
432 args->error = clib_error_return (0, "interface was already enslaved");
435 sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
436 if (sif_hw->dev_class_index == bond_dev_class.index)
438 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
440 clib_error_return (0, "bond interface cannot be enslaved");
443 pool_get (bm->neighbors, sif);
444 memset (sif, 0, sizeof (*sif));
445 clib_spinlock_init (&sif->lockp);
446 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
447 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
448 sif->sw_if_index = sw->sw_if_index;
449 sif->hw_if_index = sw->hw_if_index;
450 sif->packet_template_index = (u8) ~ 0;
451 sif->is_passive = args->is_passive;
452 sif->group = args->group;
453 sif->bif_dev_instance = bif->dev_instance;
454 sif->mode = bif->mode;
456 sif->is_long_timeout = args->is_long_timeout;
457 if (args->is_long_timeout)
458 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
460 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
462 hash_set (bm->neighbor_by_sw_if_index, sif->sw_if_index,
463 sif - bm->neighbors);
464 vec_add1 (bif->slaves, sif->sw_if_index);
466 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
468 /* Save the old mac */
469 memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
470 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
471 if (bif->use_custom_mac)
473 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
478 // bond interface gets the mac address from the first slave
479 if (vec_len (bif->slaves) == 1)
481 memcpy (bif->hw_address, sif_hw->hw_address, 6);
482 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
487 // subsequent slaves gets the mac address of the bond interface
488 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
493 if (bif_hw->l2_if_count)
495 ethernet_set_flags (vnm, sif_hw->hw_if_index,
496 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
497 /* ensure all packets go to ethernet-input */
498 ethernet_set_rx_redirect (vnm, sif_hw, 1);
501 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
503 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
507 bond_enable_collecting_distributing (vm, sif);
510 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
511 sif_hw->hw_if_index, 1, 0, 0);
516 clib_error_return (0,
517 "Error encountered on input feature arc enable");
521 static clib_error_t *
522 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
523 vlib_cli_command_t * cmd)
525 bond_enslave_args_t args = { 0 };
526 unformat_input_t _line_input, *line_input = &_line_input;
527 vnet_main_t *vnm = vnet_get_main ();
529 /* Get a line of input. */
530 if (!unformat_user (input, unformat_line_input, line_input))
531 return clib_error_return (0, "Missing required arguments.");
535 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
537 if (unformat (line_input, "%U %U",
538 unformat_vnet_sw_interface, vnm, &args.group,
539 unformat_vnet_sw_interface, vnm, &args.slave))
541 else if (unformat (line_input, "passive"))
543 else if (unformat (line_input, "long-timeout"))
544 args.is_long_timeout = 1;
547 args.error = clib_error_return (0, "unknown input `%U'",
548 format_unformat_error, input);
552 unformat_free (line_input);
556 if (args.group == ~0)
557 return clib_error_return (0, "Missing bond interface");
558 if (args.slave == ~0)
559 return clib_error_return (0, "please specify valid slave interface name");
561 bond_enslave (vm, &args);
567 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
569 .short_help = "bond add <BondEthernetx> <slave-interface> "
570 "[passive] [long-timeout]",
571 .function = enslave_interface_command_fn,
576 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
581 sif = bond_get_slave_by_sw_if_index (args->slave);
584 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
585 args->error = clib_error_return (0, "interface was not enslaved");
588 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
589 bond_delete_neighbor (vm, bif, sif);
592 static clib_error_t *
593 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
594 vlib_cli_command_t * cmd)
596 bond_detach_slave_args_t args = { 0 };
597 unformat_input_t _line_input, *line_input = &_line_input;
598 vnet_main_t *vnm = vnet_get_main ();
600 /* Get a line of input. */
601 if (!unformat_user (input, unformat_line_input, line_input))
602 return clib_error_return (0, "Missing required arguments.");
605 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
607 if (unformat (line_input, "%U",
608 unformat_vnet_sw_interface, vnm, &args.slave))
612 args.error = clib_error_return (0, "unknown input `%U'",
613 format_unformat_error, input);
617 unformat_free (line_input);
621 if (args.slave == ~0)
622 return clib_error_return (0, "please specify valid slave interface name");
624 bond_detach_slave (vm, &args);
630 VLIB_CLI_COMMAND (detach_interface_command, static) = {
632 .short_help = "bond del <slave-interface>",
633 .function = detach_interface_command_fn,
638 show_bond (vlib_main_t * vm)
640 bond_main_t *bm = &bond_main;
643 vlib_cli_output (vm, "%-16s %-12s %-12s %-13s %-14s %s",
644 "interface name", "sw_if_index", "mode",
645 "load balance", "active slaves", "slaves");
648 pool_foreach (bif, bm->interfaces,
650 vlib_cli_output (vm, "%-16U %-12d %-12U %-13U %-14u %u",
651 format_bond_interface_name, bif->dev_instance,
652 bif->sw_if_index, format_bond_mode, bif->mode,
653 format_bond_load_balance, bif->lb,
654 vec_len (bif->active_slaves), vec_len (bif->slaves));
660 show_bond_details (vlib_main_t * vm)
662 bond_main_t *bm = &bond_main;
667 pool_foreach (bif, bm->interfaces,
669 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
670 vlib_cli_output (vm, " mode: %U",
671 format_bond_mode, bif->mode);
672 vlib_cli_output (vm, " load balance: %U",
673 format_bond_load_balance, bif->lb);
674 if (bif->mode == BOND_MODE_ROUND_ROBIN)
675 vlib_cli_output (vm, " last xmit slave index: %u",
676 bif->lb_rr_last_index);
677 vlib_cli_output (vm, " number of active slaves: %d",
678 vec_len (bif->active_slaves));
679 vec_foreach (sw_if_index, bif->active_slaves)
681 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
682 vnet_get_main (), *sw_if_index);
684 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
685 vec_foreach (sw_if_index, bif->slaves)
687 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
688 vnet_get_main (), *sw_if_index);
690 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
691 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
692 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
697 static clib_error_t *
698 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
699 vlib_cli_command_t * cmd)
703 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
705 if (unformat (input, "details"))
709 return clib_error_return (0, "unknown input `%U'",
710 format_unformat_error, input);
715 show_bond_details (vm);
723 VLIB_CLI_COMMAND (show_bond_command, static) = {
725 .short_help = "show bond [details]",
726 .function = show_bond_fn,
731 bond_cli_init (vlib_main_t * vm)
733 bond_main_t *bm = &bond_main;
736 bm->vnet_main = vnet_get_main ();
737 bm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));
742 VLIB_INIT_FUNCTION (bond_cli_init);
745 * fd.io coding-style-patch-verification: ON
748 * eval: (c-set-style "gnu")