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 vec_foreach_index (i, bif->active_slaves)
34 p = *vec_elt_at_index (bif->active_slaves, i);
35 if (p == sif->sw_if_index)
37 vec_del1 (bif->active_slaves, i);
38 hash_unset (bif->active_slave_by_sw_if_index, sif->sw_if_index);
45 bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
49 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
50 if (!hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index))
52 hash_set (bif->active_slave_by_sw_if_index, sif->sw_if_index,
54 vec_add1 (bif->active_slaves, sif->sw_if_index);
59 bond_dump_ifs (bond_interface_details_t ** out_bondifs)
61 vnet_main_t *vnm = vnet_get_main ();
62 bond_main_t *bm = &bond_main;
64 vnet_hw_interface_t *hi;
65 bond_interface_details_t *r_bondifs = NULL;
66 bond_interface_details_t *bondif = NULL;
69 pool_foreach (bif, bm->interfaces,
70 vec_add2(r_bondifs, bondif, 1);
71 memset (bondif, 0, sizeof (*bondif));
72 bondif->sw_if_index = bif->sw_if_index;
73 hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
74 clib_memcpy(bondif->interface_name, hi->name,
75 MIN (ARRAY_LEN (bondif->interface_name) - 1,
76 strlen ((const char *) hi->name)));
77 bondif->mode = bif->mode;
79 bondif->active_slaves = vec_len (bif->active_slaves);
80 bondif->slaves = vec_len (bif->slaves);
84 *out_bondifs = r_bondifs;
90 bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
93 vnet_main_t *vnm = vnet_get_main ();
95 vnet_hw_interface_t *hi;
96 vnet_sw_interface_t *sw;
97 slave_interface_details_t *r_slaveifs = NULL;
98 slave_interface_details_t *slaveif = NULL;
99 u32 *sw_if_index = NULL;
102 bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
106 vec_foreach (sw_if_index, bif->slaves)
108 vec_add2 (r_slaveifs, slaveif, 1);
109 memset (slaveif, 0, sizeof (*slaveif));
110 sif = bond_get_slave_by_sw_if_index (*sw_if_index);
113 sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
114 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
115 clib_memcpy (slaveif->interface_name, hi->name,
116 MIN (ARRAY_LEN (slaveif->interface_name) - 1,
117 strlen ((const char *) hi->name)));
118 slaveif->sw_if_index = sif->sw_if_index;
119 slaveif->is_passive = sif->is_passive;
120 slaveif->is_long_timeout = sif->is_long_timeout;
123 *out_slaveifs = r_slaveifs;
129 bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
131 bond_main_t *bm = &bond_main;
132 vnet_main_t *vnm = vnet_get_main ();
134 vnet_hw_interface_t *hw;
136 bif->port_number_bitmap =
137 clib_bitmap_set (bif->port_number_bitmap,
138 ntohs (sif->actor_admin.port_number) - 1, 0);
139 hash_unset (bm->neighbor_by_sw_if_index, sif->sw_if_index);
140 vec_free (sif->last_marker_pkt);
141 vec_free (sif->last_rx_pkt);
142 vec_foreach_index (i, bif->slaves)
144 uword p = *vec_elt_at_index (bif->slaves, i);
145 if (p == sif->sw_if_index)
147 vec_del1 (bif->slaves, i);
152 bond_disable_collecting_distributing (vm, sif);
154 /* Put back the old mac */
155 hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
156 vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
157 sif->persistent_hw_address);
159 pool_put (bm->neighbors, sif);
161 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
162 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
166 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
168 bond_main_t *bm = &bond_main;
169 vnet_main_t *vnm = vnet_get_main ();
172 vnet_hw_interface_t *hw;
173 u32 *sif_sw_if_index;
175 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
176 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
177 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
179 bif = bond_get_master_by_dev_instance (hw->dev_instance);
181 vec_foreach (sif_sw_if_index, bif->slaves)
183 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
185 bond_delete_neighbor (vm, bif, sif);
188 /* bring down the interface */
189 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
190 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
192 ethernet_delete_interface (vnm, bif->hw_if_index);
194 clib_bitmap_free (bif->port_number_bitmap);
195 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
196 memset (bif, 0, sizeof (*bif));
197 pool_put (bm->interfaces, bif);
203 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
205 bond_main_t *bm = &bond_main;
206 vnet_main_t *vnm = vnet_get_main ();
207 vnet_sw_interface_t *sw;
210 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
212 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
213 args->error = clib_error_return (0, "LACP plugin is not loaded");
216 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
218 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
219 args->error = clib_error_return (0, "Invalid mode");
222 if (args->lb > BOND_LB_L23)
224 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
225 args->error = clib_error_return (0, "Invalid load-balance");
228 pool_get (bm->interfaces, bif);
229 memset (bif, 0, sizeof (*bif));
230 bif->dev_instance = bif - bm->interfaces;
232 bif->mode = args->mode;
234 // Special load-balance mode used for rr and bc
235 if (bif->mode == BOND_MODE_ROUND_ROBIN)
236 bif->lb = BOND_LB_RR;
237 else if (bif->mode == BOND_MODE_BROADCAST)
238 bif->lb = BOND_LB_BC;
240 bif->use_custom_mac = args->hw_addr_set;
241 if (!args->hw_addr_set)
243 f64 now = vlib_time_now (vm);
245 rnd = (u32) (now * 1e6);
246 rnd = random_u32 (&rnd);
248 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
249 args->hw_addr[0] = 2;
250 args->hw_addr[1] = 0xfe;
252 memcpy (bif->hw_address, args->hw_addr, 6);
253 args->error = ethernet_register_interface
254 (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ ,
255 bif->hw_address /* ethernet address */ ,
256 &bif->hw_if_index, 0 /* flag change */ );
260 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
261 pool_put (bm->interfaces, bif);
265 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
266 bif->sw_if_index = sw->sw_if_index;
267 bif->group = bif->sw_if_index;
269 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
270 VNET_HW_INTERFACE_FLAG_LINK_UP);
272 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
275 args->sw_if_index = bif->sw_if_index;
278 static clib_error_t *
279 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
280 vlib_cli_command_t * cmd)
282 unformat_input_t _line_input, *line_input = &_line_input;
283 bond_create_if_args_t args = { 0 };
286 /* Get a line of input. */
287 if (!unformat_user (input, unformat_line_input, line_input))
288 return clib_error_return (0, "Missing required arguments.");
291 args.lb = BOND_LB_L2;
292 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
294 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
296 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
297 && unformat (line_input, "load-balance %U",
298 unformat_bond_load_balance, &args.lb))
300 else if (unformat (line_input, "hw-addr %U",
301 unformat_ethernet_address, args.hw_addr))
302 args.hw_addr_set = 1;
304 return clib_error_return (0, "unknown input `%U'",
305 format_unformat_error, input);
307 unformat_free (line_input);
309 if (mode_is_set == 0)
310 return clib_error_return (0, "Missing bond mode");
312 bond_create_if (vm, &args);
318 VLIB_CLI_COMMAND (bond_create_command, static) = {
319 .path = "create bond",
320 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
321 "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]",
322 .function = bond_create_command_fn,
326 static clib_error_t *
327 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
328 vlib_cli_command_t * cmd)
330 unformat_input_t _line_input, *line_input = &_line_input;
331 u32 sw_if_index = ~0;
332 vnet_main_t *vnm = vnet_get_main ();
335 /* Get a line of input. */
336 if (!unformat_user (input, unformat_line_input, line_input))
337 return clib_error_return (0, "Missing <interface>");
339 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
341 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
343 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
347 return clib_error_return (0, "unknown input `%U'",
348 format_unformat_error, input);
350 unformat_free (line_input);
352 if (sw_if_index == ~0)
353 return clib_error_return (0,
354 "please specify interface name or sw_if_index");
356 rv = bond_delete_if (vm, sw_if_index);
357 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
358 return clib_error_return (0, "not a bond interface");
360 return clib_error_return (0, "error on deleting bond interface");
366 VLIB_CLI_COMMAND (bond_delete__command, static) =
368 .path = "delete bond",
369 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
370 .function = bond_delete_command_fn,
375 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
377 bond_main_t *bm = &bond_main;
378 vnet_main_t *vnm = vnet_get_main ();
381 vnet_interface_main_t *im = &vnm->interface_main;
382 vnet_hw_interface_t *hw, *hw2;
383 vnet_sw_interface_t *sw;
385 bif = bond_get_master_by_sw_if_index (args->group);
388 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
389 args->error = clib_error_return (0, "bond interface not found");
392 // make sure the interface is not already enslaved
393 if (bond_get_slave_by_sw_if_index (args->slave))
395 args->rv = VNET_API_ERROR_VALUE_EXIST;
396 args->error = clib_error_return (0, "interface was already enslaved");
399 hw = vnet_get_sup_hw_interface (vnm, args->slave);
400 if (hw->dev_class_index == bond_dev_class.index)
402 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
404 clib_error_return (0, "bond interface cannot be enslaved");
407 pool_get (bm->neighbors, sif);
408 memset (sif, 0, sizeof (*sif));
409 clib_spinlock_init (&sif->lockp);
410 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
411 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
412 sif->sw_if_index = sw->sw_if_index;
413 sif->hw_if_index = sw->hw_if_index;
414 sif->packet_template_index = (u8) ~ 0;
415 sif->is_passive = args->is_passive;
416 sif->group = args->group;
417 sif->bif_dev_instance = bif->dev_instance;
418 sif->mode = bif->mode;
420 sif->is_long_timeout = args->is_long_timeout;
421 if (args->is_long_timeout)
422 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
424 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
426 hash_set (bm->neighbor_by_sw_if_index, sif->sw_if_index,
427 sif - bm->neighbors);
428 vec_add1 (bif->slaves, sif->sw_if_index);
430 hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
431 /* Save the old mac */
432 memcpy (sif->persistent_hw_address, hw->hw_address, 6);
433 if (bif->use_custom_mac)
435 vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
440 // bond interface gets the mac address from the first slave
441 if (vec_len (bif->slaves) == 1)
443 memcpy (bif->hw_address, hw->hw_address, 6);
444 hw2 = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
445 vnet_hw_interface_change_mac_address (vnm, hw2->hw_if_index,
450 // subsequent slaves gets the mac address of the bond interface
451 vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
456 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
458 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
462 bond_enable_collecting_distributing (vm, sif);
465 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
466 hw->hw_if_index, 1, 0, 0);
471 clib_error_return (0,
472 "Error encountered on input feature arc enable");
476 static clib_error_t *
477 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
478 vlib_cli_command_t * cmd)
480 bond_enslave_args_t args = { 0 };
481 unformat_input_t _line_input, *line_input = &_line_input;
482 vnet_main_t *vnm = vnet_get_main ();
484 /* Get a line of input. */
485 if (!unformat_user (input, unformat_line_input, line_input))
486 return clib_error_return (0, "Missing required arguments.");
490 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
492 if (unformat (line_input, "interface %U",
493 unformat_vnet_sw_interface, vnm, &args.slave))
495 else if (unformat (line_input, "to %U", unformat_vnet_sw_interface, vnm,
498 else if (unformat (line_input, "passive"))
500 else if (unformat (line_input, "long-timeout"))
501 args.is_long_timeout = 1;
504 args.error = clib_error_return (0, "unknown input `%U'",
505 format_unformat_error, input);
509 unformat_free (line_input);
513 if (args.group == ~0)
514 return clib_error_return (0, "Missing bond interface");
515 if (args.slave == ~0)
516 return clib_error_return (0, "please specify valid interface name");
518 bond_enslave (vm, &args);
524 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
526 .short_help = "enslave interface <interface> to <BondEthernetx> [passive] [long-timeout]",
527 .function = enslave_interface_command_fn,
532 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
537 sif = bond_get_slave_by_sw_if_index (args->slave);
540 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
541 args->error = clib_error_return (0, "interface was not enslaved");
544 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
545 bond_delete_neighbor (vm, bif, sif);
548 static clib_error_t *
549 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
550 vlib_cli_command_t * cmd)
552 bond_detach_slave_args_t args = { 0 };
553 unformat_input_t _line_input, *line_input = &_line_input;
554 vnet_main_t *vnm = vnet_get_main ();
556 /* Get a line of input. */
557 if (!unformat_user (input, unformat_line_input, line_input))
558 return clib_error_return (0, "Missing required arguments.");
561 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
563 if (unformat (line_input, "interface %U",
564 unformat_vnet_sw_interface, vnm, &args.slave))
568 args.error = clib_error_return (0, "unknown input `%U'",
569 format_unformat_error, input);
573 unformat_free (line_input);
577 if (args.slave == ~0)
578 return clib_error_return (0, "please specify valid interface name");
580 bond_detach_slave (vm, &args);
586 VLIB_CLI_COMMAND (detach_interface_command, static) = {
588 .short_help = "detach interface <interface>",
589 .function = detach_interface_command_fn,
594 show_bond (vlib_main_t * vm)
596 bond_main_t *bm = &bond_main;
599 vlib_cli_output (vm, "%-16s %-12s %-12s %-13s %-14s %s",
600 "interface name", "sw_if_index", "mode",
601 "load balance", "active slaves", "slaves");
604 pool_foreach (bif, bm->interfaces,
606 vlib_cli_output (vm, "%-16U %-12d %-12U %-13U %-14u %u",
607 format_bond_interface_name, bif->dev_instance,
608 bif->sw_if_index, format_bond_mode, bif->mode,
609 format_bond_load_balance, bif->lb,
610 vec_len (bif->active_slaves), vec_len (bif->slaves));
616 show_bond_details (vlib_main_t * vm)
618 bond_main_t *bm = &bond_main;
623 pool_foreach (bif, bm->interfaces,
625 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
626 vlib_cli_output (vm, " mode: %U",
627 format_bond_mode, bif->mode);
628 vlib_cli_output (vm, " load balance: %U",
629 format_bond_load_balance, bif->lb);
630 if (bif->mode == BOND_MODE_ROUND_ROBIN)
631 vlib_cli_output (vm, " last xmit slave index: %u",
632 bif->lb_rr_last_index);
633 vlib_cli_output (vm, " number of active slaves: %d",
634 vec_len (bif->active_slaves));
635 vec_foreach (sw_if_index, bif->active_slaves)
637 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
638 vnet_get_main (), *sw_if_index);
640 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
641 vec_foreach (sw_if_index, bif->slaves)
643 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
644 vnet_get_main (), *sw_if_index);
646 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
647 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
648 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
653 static clib_error_t *
654 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
655 vlib_cli_command_t * cmd)
659 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
661 if (unformat (input, "details"))
665 return clib_error_return (0, "unknown input `%U'",
666 format_unformat_error, input);
671 show_bond_details (vm);
679 VLIB_CLI_COMMAND (show_bond_command, static) = {
681 .short_help = "show bond [details]",
682 .function = show_bond_fn,
687 bond_cli_init (vlib_main_t * vm)
689 bond_main_t *bm = &bond_main;
692 bm->vnet_main = vnet_get_main ();
693 bm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));
698 VLIB_INIT_FUNCTION (bond_cli_init);
701 * fd.io coding-style-patch-verification: ON
704 * eval: (c-set-style "gnu")