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;
140 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
142 bif->port_number_bitmap =
143 clib_bitmap_set (bif->port_number_bitmap,
144 ntohs (sif->actor_admin.port_number) - 1, 0);
145 hash_unset (bm->neighbor_by_sw_if_index, sif->sw_if_index);
146 vec_free (sif->last_marker_pkt);
147 vec_free (sif->last_rx_pkt);
148 vec_foreach_index (i, bif->slaves)
150 uword p = *vec_elt_at_index (bif->slaves, i);
151 if (p == sif->sw_if_index)
153 vec_del1 (bif->slaves, i);
158 bond_disable_collecting_distributing (vm, sif);
160 /* Put back the old mac */
161 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
162 sif->persistent_hw_address);
164 pool_put (bm->neighbors, sif);
166 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
167 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
171 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
173 bond_main_t *bm = &bond_main;
174 vnet_main_t *vnm = vnet_get_main ();
177 vnet_hw_interface_t *hw;
178 u32 *sif_sw_if_index;
183 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
184 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
185 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
187 bif = bond_get_master_by_dev_instance (hw->dev_instance);
189 vec_foreach (sif_sw_if_index, bif->slaves)
191 vec_add1 (s_list, sif_sw_if_index);
194 for (i = 0; i < vec_len (s_list); i++)
196 sif_sw_if_index = s_list[i];
197 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
199 bond_delete_neighbor (vm, bif, sif);
205 /* bring down the interface */
206 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
207 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
209 ethernet_delete_interface (vnm, bif->hw_if_index);
211 clib_bitmap_free (bif->port_number_bitmap);
212 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
213 for (thread_index = 0; thread_index < vlib_get_thread_main ()->n_vlib_mains;
216 vec_free (bif->per_thread_info[thread_index].frame);
218 vec_free (bif->per_thread_info);
219 memset (bif, 0, sizeof (*bif));
220 pool_put (bm->interfaces, bif);
226 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
228 bond_main_t *bm = &bond_main;
229 vnet_main_t *vnm = vnet_get_main ();
230 vnet_sw_interface_t *sw;
233 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
235 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
236 args->error = clib_error_return (0, "LACP plugin is not loaded");
239 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
241 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
242 args->error = clib_error_return (0, "Invalid mode");
245 if (args->lb > BOND_LB_L23)
247 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
248 args->error = clib_error_return (0, "Invalid load-balance");
251 pool_get (bm->interfaces, bif);
252 memset (bif, 0, sizeof (*bif));
253 bif->dev_instance = bif - bm->interfaces;
255 bif->mode = args->mode;
257 // Special load-balance mode used for rr and bc
258 if (bif->mode == BOND_MODE_ROUND_ROBIN)
259 bif->lb = BOND_LB_RR;
260 else if (bif->mode == BOND_MODE_BROADCAST)
261 bif->lb = BOND_LB_BC;
263 bif->use_custom_mac = args->hw_addr_set;
264 if (!args->hw_addr_set)
266 f64 now = vlib_time_now (vm);
268 rnd = (u32) (now * 1e6);
269 rnd = random_u32 (&rnd);
271 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
272 args->hw_addr[0] = 2;
273 args->hw_addr[1] = 0xfe;
275 memcpy (bif->hw_address, args->hw_addr, 6);
276 args->error = ethernet_register_interface
277 (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ ,
278 bif->hw_address /* ethernet address */ ,
279 &bif->hw_if_index, 0 /* flag change */ );
283 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
284 pool_put (bm->interfaces, bif);
288 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
289 bif->sw_if_index = sw->sw_if_index;
290 bif->group = bif->sw_if_index;
291 vec_validate_aligned (bif->per_thread_info,
292 vlib_get_thread_main ()->n_vlib_mains - 1,
293 CLIB_CACHE_LINE_BYTES);
294 if (vlib_get_thread_main ()->n_vlib_mains > 1)
295 clib_spinlock_init (&bif->lockp);
297 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
298 VNET_HW_INTERFACE_FLAG_LINK_UP);
300 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
303 args->sw_if_index = bif->sw_if_index;
306 static clib_error_t *
307 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
308 vlib_cli_command_t * cmd)
310 unformat_input_t _line_input, *line_input = &_line_input;
311 bond_create_if_args_t args = { 0 };
314 /* Get a line of input. */
315 if (!unformat_user (input, unformat_line_input, line_input))
316 return clib_error_return (0, "Missing required arguments.");
319 args.lb = BOND_LB_L2;
320 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
322 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
324 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
325 && unformat (line_input, "load-balance %U",
326 unformat_bond_load_balance, &args.lb))
328 else if (unformat (line_input, "hw-addr %U",
329 unformat_ethernet_address, args.hw_addr))
330 args.hw_addr_set = 1;
332 return clib_error_return (0, "unknown input `%U'",
333 format_unformat_error, input);
335 unformat_free (line_input);
337 if (mode_is_set == 0)
338 return clib_error_return (0, "Missing bond mode");
340 bond_create_if (vm, &args);
346 VLIB_CLI_COMMAND (bond_create_command, static) = {
347 .path = "create bond",
348 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
349 "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]",
350 .function = bond_create_command_fn,
354 static clib_error_t *
355 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
356 vlib_cli_command_t * cmd)
358 unformat_input_t _line_input, *line_input = &_line_input;
359 u32 sw_if_index = ~0;
360 vnet_main_t *vnm = vnet_get_main ();
363 /* Get a line of input. */
364 if (!unformat_user (input, unformat_line_input, line_input))
365 return clib_error_return (0, "Missing <interface>");
367 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
369 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
371 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
375 return clib_error_return (0, "unknown input `%U'",
376 format_unformat_error, input);
378 unformat_free (line_input);
380 if (sw_if_index == ~0)
381 return clib_error_return (0,
382 "please specify interface name or sw_if_index");
384 rv = bond_delete_if (vm, sw_if_index);
385 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
386 return clib_error_return (0, "not a bond interface");
388 return clib_error_return (0, "error on deleting bond interface");
394 VLIB_CLI_COMMAND (bond_delete__command, static) =
396 .path = "delete bond",
397 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
398 .function = bond_delete_command_fn,
403 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
405 bond_main_t *bm = &bond_main;
406 vnet_main_t *vnm = vnet_get_main ();
409 vnet_interface_main_t *im = &vnm->interface_main;
410 vnet_hw_interface_t *bif_hw, *sif_hw;
411 vnet_sw_interface_t *sw;
413 bif = bond_get_master_by_sw_if_index (args->group);
416 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
417 args->error = clib_error_return (0, "bond interface not found");
420 // make sure the interface is not already enslaved
421 if (bond_get_slave_by_sw_if_index (args->slave))
423 args->rv = VNET_API_ERROR_VALUE_EXIST;
424 args->error = clib_error_return (0, "interface was already enslaved");
427 sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
428 if (sif_hw->dev_class_index == bond_dev_class.index)
430 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
432 clib_error_return (0, "bond interface cannot be enslaved");
435 pool_get (bm->neighbors, sif);
436 memset (sif, 0, sizeof (*sif));
437 clib_spinlock_init (&sif->lockp);
438 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
439 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
440 sif->sw_if_index = sw->sw_if_index;
441 sif->hw_if_index = sw->hw_if_index;
442 sif->packet_template_index = (u8) ~ 0;
443 sif->is_passive = args->is_passive;
444 sif->group = args->group;
445 sif->bif_dev_instance = bif->dev_instance;
446 sif->mode = bif->mode;
448 sif->is_long_timeout = args->is_long_timeout;
449 if (args->is_long_timeout)
450 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
452 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
454 hash_set (bm->neighbor_by_sw_if_index, sif->sw_if_index,
455 sif - bm->neighbors);
456 vec_add1 (bif->slaves, sif->sw_if_index);
458 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
460 /* Save the old mac */
461 memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
462 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
463 if (bif->use_custom_mac)
465 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
470 // bond interface gets the mac address from the first slave
471 if (vec_len (bif->slaves) == 1)
473 memcpy (bif->hw_address, sif_hw->hw_address, 6);
474 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
479 // subsequent slaves gets the mac address of the bond interface
480 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
485 if (bif_hw->l2_if_count)
487 ethernet_set_flags (vnm, sif_hw->hw_if_index,
488 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
489 /* ensure all packets go to ethernet-input */
490 ethernet_set_rx_redirect (vnm, sif_hw, 1);
493 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
495 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
499 bond_enable_collecting_distributing (vm, sif);
502 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
503 sif_hw->hw_if_index, 1, 0, 0);
508 clib_error_return (0,
509 "Error encountered on input feature arc enable");
513 static clib_error_t *
514 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
515 vlib_cli_command_t * cmd)
517 bond_enslave_args_t args = { 0 };
518 unformat_input_t _line_input, *line_input = &_line_input;
519 vnet_main_t *vnm = vnet_get_main ();
521 /* Get a line of input. */
522 if (!unformat_user (input, unformat_line_input, line_input))
523 return clib_error_return (0, "Missing required arguments.");
527 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
529 if (unformat (line_input, "%U %U",
530 unformat_vnet_sw_interface, vnm, &args.group,
531 unformat_vnet_sw_interface, vnm, &args.slave))
533 else if (unformat (line_input, "passive"))
535 else if (unformat (line_input, "long-timeout"))
536 args.is_long_timeout = 1;
539 args.error = clib_error_return (0, "unknown input `%U'",
540 format_unformat_error, input);
544 unformat_free (line_input);
548 if (args.group == ~0)
549 return clib_error_return (0, "Missing bond interface");
550 if (args.slave == ~0)
551 return clib_error_return (0, "please specify valid slave interface name");
553 bond_enslave (vm, &args);
559 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
561 .short_help = "bond add <BondEthernetx> <slave-interface> "
562 "[passive] [long-timeout]",
563 .function = enslave_interface_command_fn,
568 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
573 sif = bond_get_slave_by_sw_if_index (args->slave);
576 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
577 args->error = clib_error_return (0, "interface was not enslaved");
580 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
581 bond_delete_neighbor (vm, bif, sif);
584 static clib_error_t *
585 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
586 vlib_cli_command_t * cmd)
588 bond_detach_slave_args_t args = { 0 };
589 unformat_input_t _line_input, *line_input = &_line_input;
590 vnet_main_t *vnm = vnet_get_main ();
592 /* Get a line of input. */
593 if (!unformat_user (input, unformat_line_input, line_input))
594 return clib_error_return (0, "Missing required arguments.");
597 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
599 if (unformat (line_input, "%U",
600 unformat_vnet_sw_interface, vnm, &args.slave))
604 args.error = clib_error_return (0, "unknown input `%U'",
605 format_unformat_error, input);
609 unformat_free (line_input);
613 if (args.slave == ~0)
614 return clib_error_return (0, "please specify valid slave interface name");
616 bond_detach_slave (vm, &args);
622 VLIB_CLI_COMMAND (detach_interface_command, static) = {
624 .short_help = "bond del <slave-interface>",
625 .function = detach_interface_command_fn,
630 show_bond (vlib_main_t * vm)
632 bond_main_t *bm = &bond_main;
635 vlib_cli_output (vm, "%-16s %-12s %-12s %-13s %-14s %s",
636 "interface name", "sw_if_index", "mode",
637 "load balance", "active slaves", "slaves");
640 pool_foreach (bif, bm->interfaces,
642 vlib_cli_output (vm, "%-16U %-12d %-12U %-13U %-14u %u",
643 format_bond_interface_name, bif->dev_instance,
644 bif->sw_if_index, format_bond_mode, bif->mode,
645 format_bond_load_balance, bif->lb,
646 vec_len (bif->active_slaves), vec_len (bif->slaves));
652 show_bond_details (vlib_main_t * vm)
654 bond_main_t *bm = &bond_main;
659 pool_foreach (bif, bm->interfaces,
661 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
662 vlib_cli_output (vm, " mode: %U",
663 format_bond_mode, bif->mode);
664 vlib_cli_output (vm, " load balance: %U",
665 format_bond_load_balance, bif->lb);
666 if (bif->mode == BOND_MODE_ROUND_ROBIN)
667 vlib_cli_output (vm, " last xmit slave index: %u",
668 bif->lb_rr_last_index);
669 vlib_cli_output (vm, " number of active slaves: %d",
670 vec_len (bif->active_slaves));
671 vec_foreach (sw_if_index, bif->active_slaves)
673 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
674 vnet_get_main (), *sw_if_index);
676 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
677 vec_foreach (sw_if_index, bif->slaves)
679 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
680 vnet_get_main (), *sw_if_index);
682 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
683 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
684 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
689 static clib_error_t *
690 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
691 vlib_cli_command_t * cmd)
695 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
697 if (unformat (input, "details"))
701 return clib_error_return (0, "unknown input `%U'",
702 format_unformat_error, input);
707 show_bond_details (vm);
715 VLIB_CLI_COMMAND (show_bond_command, static) = {
717 .short_help = "show bond [details]",
718 .function = show_bond_fn,
723 bond_cli_init (vlib_main_t * vm)
725 bond_main_t *bm = &bond_main;
728 bm->vnet_main = vnet_get_main ();
729 bm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));
734 VLIB_INIT_FUNCTION (bond_cli_init);
737 * fd.io coding-style-patch-verification: ON
740 * eval: (c-set-style "gnu")