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 *hw;
140 bif->port_number_bitmap =
141 clib_bitmap_set (bif->port_number_bitmap,
142 ntohs (sif->actor_admin.port_number) - 1, 0);
143 hash_unset (bm->neighbor_by_sw_if_index, sif->sw_if_index);
144 vec_free (sif->last_marker_pkt);
145 vec_free (sif->last_rx_pkt);
146 vec_foreach_index (i, bif->slaves)
148 uword p = *vec_elt_at_index (bif->slaves, i);
149 if (p == sif->sw_if_index)
151 vec_del1 (bif->slaves, i);
156 bond_disable_collecting_distributing (vm, sif);
158 /* Put back the old mac */
159 hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
160 vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
161 sif->persistent_hw_address);
163 pool_put (bm->neighbors, sif);
165 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
166 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
170 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
172 bond_main_t *bm = &bond_main;
173 vnet_main_t *vnm = vnet_get_main ();
176 vnet_hw_interface_t *hw;
177 u32 *sif_sw_if_index;
180 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
181 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
182 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
184 bif = bond_get_master_by_dev_instance (hw->dev_instance);
186 vec_foreach (sif_sw_if_index, bif->slaves)
188 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
190 bond_delete_neighbor (vm, bif, sif);
193 /* bring down the interface */
194 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
195 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
197 ethernet_delete_interface (vnm, bif->hw_if_index);
199 clib_bitmap_free (bif->port_number_bitmap);
200 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
201 for (thread_index = 0; thread_index < vlib_get_thread_main ()->n_vlib_mains;
204 vec_free (bif->per_thread_info[thread_index].frame);
206 vec_free (bif->per_thread_info);
207 memset (bif, 0, sizeof (*bif));
208 pool_put (bm->interfaces, bif);
214 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
216 bond_main_t *bm = &bond_main;
217 vnet_main_t *vnm = vnet_get_main ();
218 vnet_sw_interface_t *sw;
221 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
223 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
224 args->error = clib_error_return (0, "LACP plugin is not loaded");
227 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
229 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
230 args->error = clib_error_return (0, "Invalid mode");
233 if (args->lb > BOND_LB_L23)
235 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
236 args->error = clib_error_return (0, "Invalid load-balance");
239 pool_get (bm->interfaces, bif);
240 memset (bif, 0, sizeof (*bif));
241 bif->dev_instance = bif - bm->interfaces;
243 bif->mode = args->mode;
245 // Special load-balance mode used for rr and bc
246 if (bif->mode == BOND_MODE_ROUND_ROBIN)
247 bif->lb = BOND_LB_RR;
248 else if (bif->mode == BOND_MODE_BROADCAST)
249 bif->lb = BOND_LB_BC;
251 bif->use_custom_mac = args->hw_addr_set;
252 if (!args->hw_addr_set)
254 f64 now = vlib_time_now (vm);
256 rnd = (u32) (now * 1e6);
257 rnd = random_u32 (&rnd);
259 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
260 args->hw_addr[0] = 2;
261 args->hw_addr[1] = 0xfe;
263 memcpy (bif->hw_address, args->hw_addr, 6);
264 args->error = ethernet_register_interface
265 (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ ,
266 bif->hw_address /* ethernet address */ ,
267 &bif->hw_if_index, 0 /* flag change */ );
271 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
272 pool_put (bm->interfaces, bif);
276 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
277 bif->sw_if_index = sw->sw_if_index;
278 bif->group = bif->sw_if_index;
279 vec_validate_aligned (bif->per_thread_info,
280 vlib_get_thread_main ()->n_vlib_mains - 1,
281 CLIB_CACHE_LINE_BYTES);
282 if (vlib_get_thread_main ()->n_vlib_mains > 1)
283 clib_spinlock_init (&bif->lockp);
285 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
286 VNET_HW_INTERFACE_FLAG_LINK_UP);
288 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
291 args->sw_if_index = bif->sw_if_index;
294 static clib_error_t *
295 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
296 vlib_cli_command_t * cmd)
298 unformat_input_t _line_input, *line_input = &_line_input;
299 bond_create_if_args_t args = { 0 };
302 /* Get a line of input. */
303 if (!unformat_user (input, unformat_line_input, line_input))
304 return clib_error_return (0, "Missing required arguments.");
307 args.lb = BOND_LB_L2;
308 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
310 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
312 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
313 && unformat (line_input, "load-balance %U",
314 unformat_bond_load_balance, &args.lb))
316 else if (unformat (line_input, "hw-addr %U",
317 unformat_ethernet_address, args.hw_addr))
318 args.hw_addr_set = 1;
320 return clib_error_return (0, "unknown input `%U'",
321 format_unformat_error, input);
323 unformat_free (line_input);
325 if (mode_is_set == 0)
326 return clib_error_return (0, "Missing bond mode");
328 bond_create_if (vm, &args);
334 VLIB_CLI_COMMAND (bond_create_command, static) = {
335 .path = "create bond",
336 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
337 "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]",
338 .function = bond_create_command_fn,
342 static clib_error_t *
343 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
344 vlib_cli_command_t * cmd)
346 unformat_input_t _line_input, *line_input = &_line_input;
347 u32 sw_if_index = ~0;
348 vnet_main_t *vnm = vnet_get_main ();
351 /* Get a line of input. */
352 if (!unformat_user (input, unformat_line_input, line_input))
353 return clib_error_return (0, "Missing <interface>");
355 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
357 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
359 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
363 return clib_error_return (0, "unknown input `%U'",
364 format_unformat_error, input);
366 unformat_free (line_input);
368 if (sw_if_index == ~0)
369 return clib_error_return (0,
370 "please specify interface name or sw_if_index");
372 rv = bond_delete_if (vm, sw_if_index);
373 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
374 return clib_error_return (0, "not a bond interface");
376 return clib_error_return (0, "error on deleting bond interface");
382 VLIB_CLI_COMMAND (bond_delete__command, static) =
384 .path = "delete bond",
385 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
386 .function = bond_delete_command_fn,
391 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
393 bond_main_t *bm = &bond_main;
394 vnet_main_t *vnm = vnet_get_main ();
397 vnet_interface_main_t *im = &vnm->interface_main;
398 vnet_hw_interface_t *hw, *hw2;
399 vnet_sw_interface_t *sw;
401 bif = bond_get_master_by_sw_if_index (args->group);
404 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
405 args->error = clib_error_return (0, "bond interface not found");
408 // make sure the interface is not already enslaved
409 if (bond_get_slave_by_sw_if_index (args->slave))
411 args->rv = VNET_API_ERROR_VALUE_EXIST;
412 args->error = clib_error_return (0, "interface was already enslaved");
415 hw = vnet_get_sup_hw_interface (vnm, args->slave);
416 if (hw->dev_class_index == bond_dev_class.index)
418 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
420 clib_error_return (0, "bond interface cannot be enslaved");
423 pool_get (bm->neighbors, sif);
424 memset (sif, 0, sizeof (*sif));
425 clib_spinlock_init (&sif->lockp);
426 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
427 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
428 sif->sw_if_index = sw->sw_if_index;
429 sif->hw_if_index = sw->hw_if_index;
430 sif->packet_template_index = (u8) ~ 0;
431 sif->is_passive = args->is_passive;
432 sif->group = args->group;
433 sif->bif_dev_instance = bif->dev_instance;
434 sif->mode = bif->mode;
436 sif->is_long_timeout = args->is_long_timeout;
437 if (args->is_long_timeout)
438 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
440 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
442 hash_set (bm->neighbor_by_sw_if_index, sif->sw_if_index,
443 sif - bm->neighbors);
444 vec_add1 (bif->slaves, sif->sw_if_index);
446 hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
447 /* Save the old mac */
448 memcpy (sif->persistent_hw_address, hw->hw_address, 6);
449 if (bif->use_custom_mac)
451 vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
456 // bond interface gets the mac address from the first slave
457 if (vec_len (bif->slaves) == 1)
459 memcpy (bif->hw_address, hw->hw_address, 6);
460 hw2 = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
461 vnet_hw_interface_change_mac_address (vnm, hw2->hw_if_index,
466 // subsequent slaves gets the mac address of the bond interface
467 vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
472 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
474 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
478 bond_enable_collecting_distributing (vm, sif);
481 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
482 hw->hw_if_index, 1, 0, 0);
487 clib_error_return (0,
488 "Error encountered on input feature arc enable");
492 static clib_error_t *
493 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
494 vlib_cli_command_t * cmd)
496 bond_enslave_args_t args = { 0 };
497 unformat_input_t _line_input, *line_input = &_line_input;
498 vnet_main_t *vnm = vnet_get_main ();
500 /* Get a line of input. */
501 if (!unformat_user (input, unformat_line_input, line_input))
502 return clib_error_return (0, "Missing required arguments.");
506 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
508 if (unformat (line_input, "interface %U",
509 unformat_vnet_sw_interface, vnm, &args.slave))
511 else if (unformat (line_input, "to %U", unformat_vnet_sw_interface, vnm,
514 else if (unformat (line_input, "passive"))
516 else if (unformat (line_input, "long-timeout"))
517 args.is_long_timeout = 1;
520 args.error = clib_error_return (0, "unknown input `%U'",
521 format_unformat_error, input);
525 unformat_free (line_input);
529 if (args.group == ~0)
530 return clib_error_return (0, "Missing bond interface");
531 if (args.slave == ~0)
532 return clib_error_return (0, "please specify valid interface name");
534 bond_enslave (vm, &args);
540 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
542 .short_help = "enslave interface <interface> to <BondEthernetx> [passive] [long-timeout]",
543 .function = enslave_interface_command_fn,
548 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
553 sif = bond_get_slave_by_sw_if_index (args->slave);
556 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
557 args->error = clib_error_return (0, "interface was not enslaved");
560 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
561 bond_delete_neighbor (vm, bif, sif);
564 static clib_error_t *
565 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
566 vlib_cli_command_t * cmd)
568 bond_detach_slave_args_t args = { 0 };
569 unformat_input_t _line_input, *line_input = &_line_input;
570 vnet_main_t *vnm = vnet_get_main ();
572 /* Get a line of input. */
573 if (!unformat_user (input, unformat_line_input, line_input))
574 return clib_error_return (0, "Missing required arguments.");
577 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
579 if (unformat (line_input, "interface %U",
580 unformat_vnet_sw_interface, vnm, &args.slave))
584 args.error = clib_error_return (0, "unknown input `%U'",
585 format_unformat_error, input);
589 unformat_free (line_input);
593 if (args.slave == ~0)
594 return clib_error_return (0, "please specify valid interface name");
596 bond_detach_slave (vm, &args);
602 VLIB_CLI_COMMAND (detach_interface_command, static) = {
604 .short_help = "detach interface <interface>",
605 .function = detach_interface_command_fn,
610 show_bond (vlib_main_t * vm)
612 bond_main_t *bm = &bond_main;
615 vlib_cli_output (vm, "%-16s %-12s %-12s %-13s %-14s %s",
616 "interface name", "sw_if_index", "mode",
617 "load balance", "active slaves", "slaves");
620 pool_foreach (bif, bm->interfaces,
622 vlib_cli_output (vm, "%-16U %-12d %-12U %-13U %-14u %u",
623 format_bond_interface_name, bif->dev_instance,
624 bif->sw_if_index, format_bond_mode, bif->mode,
625 format_bond_load_balance, bif->lb,
626 vec_len (bif->active_slaves), vec_len (bif->slaves));
632 show_bond_details (vlib_main_t * vm)
634 bond_main_t *bm = &bond_main;
639 pool_foreach (bif, bm->interfaces,
641 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
642 vlib_cli_output (vm, " mode: %U",
643 format_bond_mode, bif->mode);
644 vlib_cli_output (vm, " load balance: %U",
645 format_bond_load_balance, bif->lb);
646 if (bif->mode == BOND_MODE_ROUND_ROBIN)
647 vlib_cli_output (vm, " last xmit slave index: %u",
648 bif->lb_rr_last_index);
649 vlib_cli_output (vm, " number of active slaves: %d",
650 vec_len (bif->active_slaves));
651 vec_foreach (sw_if_index, bif->active_slaves)
653 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
654 vnet_get_main (), *sw_if_index);
656 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
657 vec_foreach (sw_if_index, bif->slaves)
659 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
660 vnet_get_main (), *sw_if_index);
662 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
663 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
664 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
669 static clib_error_t *
670 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
671 vlib_cli_command_t * cmd)
675 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
677 if (unformat (input, "details"))
681 return clib_error_return (0, "unknown input `%U'",
682 format_unformat_error, input);
687 show_bond_details (vm);
695 VLIB_CLI_COMMAND (show_bond_command, static) = {
697 .short_help = "show bond [details]",
698 .function = show_bond_fn,
703 bond_cli_init (vlib_main_t * vm)
705 bond_main_t *bm = &bond_main;
708 bm->vnet_main = vnet_get_main ();
709 bm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));
714 VLIB_INIT_FUNCTION (bond_cli_init);
717 * fd.io coding-style-patch-verification: ON
720 * eval: (c-set-style "gnu")