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;
182 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
183 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
184 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
186 bif = bond_get_master_by_dev_instance (hw->dev_instance);
188 vec_foreach (sif_sw_if_index, bif->slaves)
190 vec_add1 (s_list, sif_sw_if_index);
193 for (i = 0; i < vec_len (s_list); i++)
195 sif_sw_if_index = s_list[i];
196 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
198 bond_delete_neighbor (vm, bif, sif);
204 /* bring down the interface */
205 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
206 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
208 ethernet_delete_interface (vnm, bif->hw_if_index);
210 clib_bitmap_free (bif->port_number_bitmap);
211 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
212 for (thread_index = 0; thread_index < vlib_get_thread_main ()->n_vlib_mains;
215 vec_free (bif->per_thread_info[thread_index].frame);
217 vec_free (bif->per_thread_info);
218 memset (bif, 0, sizeof (*bif));
219 pool_put (bm->interfaces, bif);
225 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
227 bond_main_t *bm = &bond_main;
228 vnet_main_t *vnm = vnet_get_main ();
229 vnet_sw_interface_t *sw;
232 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
234 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
235 args->error = clib_error_return (0, "LACP plugin is not loaded");
238 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
240 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
241 args->error = clib_error_return (0, "Invalid mode");
244 if (args->lb > BOND_LB_L23)
246 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
247 args->error = clib_error_return (0, "Invalid load-balance");
250 pool_get (bm->interfaces, bif);
251 memset (bif, 0, sizeof (*bif));
252 bif->dev_instance = bif - bm->interfaces;
254 bif->mode = args->mode;
256 // Special load-balance mode used for rr and bc
257 if (bif->mode == BOND_MODE_ROUND_ROBIN)
258 bif->lb = BOND_LB_RR;
259 else if (bif->mode == BOND_MODE_BROADCAST)
260 bif->lb = BOND_LB_BC;
262 bif->use_custom_mac = args->hw_addr_set;
263 if (!args->hw_addr_set)
265 f64 now = vlib_time_now (vm);
267 rnd = (u32) (now * 1e6);
268 rnd = random_u32 (&rnd);
270 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
271 args->hw_addr[0] = 2;
272 args->hw_addr[1] = 0xfe;
274 memcpy (bif->hw_address, args->hw_addr, 6);
275 args->error = ethernet_register_interface
276 (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ ,
277 bif->hw_address /* ethernet address */ ,
278 &bif->hw_if_index, 0 /* flag change */ );
282 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
283 pool_put (bm->interfaces, bif);
287 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
288 bif->sw_if_index = sw->sw_if_index;
289 bif->group = bif->sw_if_index;
290 vec_validate_aligned (bif->per_thread_info,
291 vlib_get_thread_main ()->n_vlib_mains - 1,
292 CLIB_CACHE_LINE_BYTES);
293 if (vlib_get_thread_main ()->n_vlib_mains > 1)
294 clib_spinlock_init (&bif->lockp);
296 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
297 VNET_HW_INTERFACE_FLAG_LINK_UP);
299 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
302 args->sw_if_index = bif->sw_if_index;
305 static clib_error_t *
306 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
307 vlib_cli_command_t * cmd)
309 unformat_input_t _line_input, *line_input = &_line_input;
310 bond_create_if_args_t args = { 0 };
313 /* Get a line of input. */
314 if (!unformat_user (input, unformat_line_input, line_input))
315 return clib_error_return (0, "Missing required arguments.");
318 args.lb = BOND_LB_L2;
319 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
321 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
323 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
324 && unformat (line_input, "load-balance %U",
325 unformat_bond_load_balance, &args.lb))
327 else if (unformat (line_input, "hw-addr %U",
328 unformat_ethernet_address, args.hw_addr))
329 args.hw_addr_set = 1;
331 return clib_error_return (0, "unknown input `%U'",
332 format_unformat_error, input);
334 unformat_free (line_input);
336 if (mode_is_set == 0)
337 return clib_error_return (0, "Missing bond mode");
339 bond_create_if (vm, &args);
345 VLIB_CLI_COMMAND (bond_create_command, static) = {
346 .path = "create bond",
347 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
348 "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]",
349 .function = bond_create_command_fn,
353 static clib_error_t *
354 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
355 vlib_cli_command_t * cmd)
357 unformat_input_t _line_input, *line_input = &_line_input;
358 u32 sw_if_index = ~0;
359 vnet_main_t *vnm = vnet_get_main ();
362 /* Get a line of input. */
363 if (!unformat_user (input, unformat_line_input, line_input))
364 return clib_error_return (0, "Missing <interface>");
366 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
368 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
370 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
374 return clib_error_return (0, "unknown input `%U'",
375 format_unformat_error, input);
377 unformat_free (line_input);
379 if (sw_if_index == ~0)
380 return clib_error_return (0,
381 "please specify interface name or sw_if_index");
383 rv = bond_delete_if (vm, sw_if_index);
384 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
385 return clib_error_return (0, "not a bond interface");
387 return clib_error_return (0, "error on deleting bond interface");
393 VLIB_CLI_COMMAND (bond_delete__command, static) =
395 .path = "delete bond",
396 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
397 .function = bond_delete_command_fn,
402 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
404 bond_main_t *bm = &bond_main;
405 vnet_main_t *vnm = vnet_get_main ();
408 vnet_interface_main_t *im = &vnm->interface_main;
409 vnet_hw_interface_t *hw, *hw2;
410 vnet_sw_interface_t *sw;
412 bif = bond_get_master_by_sw_if_index (args->group);
415 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
416 args->error = clib_error_return (0, "bond interface not found");
419 // make sure the interface is not already enslaved
420 if (bond_get_slave_by_sw_if_index (args->slave))
422 args->rv = VNET_API_ERROR_VALUE_EXIST;
423 args->error = clib_error_return (0, "interface was already enslaved");
426 hw = vnet_get_sup_hw_interface (vnm, args->slave);
427 if (hw->dev_class_index == bond_dev_class.index)
429 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
431 clib_error_return (0, "bond interface cannot be enslaved");
434 pool_get (bm->neighbors, sif);
435 memset (sif, 0, sizeof (*sif));
436 clib_spinlock_init (&sif->lockp);
437 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
438 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
439 sif->sw_if_index = sw->sw_if_index;
440 sif->hw_if_index = sw->hw_if_index;
441 sif->packet_template_index = (u8) ~ 0;
442 sif->is_passive = args->is_passive;
443 sif->group = args->group;
444 sif->bif_dev_instance = bif->dev_instance;
445 sif->mode = bif->mode;
447 sif->is_long_timeout = args->is_long_timeout;
448 if (args->is_long_timeout)
449 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
451 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
453 hash_set (bm->neighbor_by_sw_if_index, sif->sw_if_index,
454 sif - bm->neighbors);
455 vec_add1 (bif->slaves, sif->sw_if_index);
457 hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
458 /* Save the old mac */
459 memcpy (sif->persistent_hw_address, hw->hw_address, 6);
460 if (bif->use_custom_mac)
462 vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
467 // bond interface gets the mac address from the first slave
468 if (vec_len (bif->slaves) == 1)
470 memcpy (bif->hw_address, hw->hw_address, 6);
471 hw2 = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
472 vnet_hw_interface_change_mac_address (vnm, hw2->hw_if_index,
477 // subsequent slaves gets the mac address of the bond interface
478 vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
483 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
485 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
489 bond_enable_collecting_distributing (vm, sif);
492 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
493 hw->hw_if_index, 1, 0, 0);
498 clib_error_return (0,
499 "Error encountered on input feature arc enable");
503 static clib_error_t *
504 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
505 vlib_cli_command_t * cmd)
507 bond_enslave_args_t args = { 0 };
508 unformat_input_t _line_input, *line_input = &_line_input;
509 vnet_main_t *vnm = vnet_get_main ();
511 /* Get a line of input. */
512 if (!unformat_user (input, unformat_line_input, line_input))
513 return clib_error_return (0, "Missing required arguments.");
517 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
519 if (unformat (line_input, "%U %U",
520 unformat_vnet_sw_interface, vnm, &args.group,
521 unformat_vnet_sw_interface, vnm, &args.slave))
523 else if (unformat (line_input, "passive"))
525 else if (unformat (line_input, "long-timeout"))
526 args.is_long_timeout = 1;
529 args.error = clib_error_return (0, "unknown input `%U'",
530 format_unformat_error, input);
534 unformat_free (line_input);
538 if (args.group == ~0)
539 return clib_error_return (0, "Missing bond interface");
540 if (args.slave == ~0)
541 return clib_error_return (0, "please specify valid slave interface name");
543 bond_enslave (vm, &args);
549 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
551 .short_help = "bond add <BondEthernetx> <slave-interface> "
552 "[passive] [long-timeout]",
553 .function = enslave_interface_command_fn,
558 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
563 sif = bond_get_slave_by_sw_if_index (args->slave);
566 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
567 args->error = clib_error_return (0, "interface was not enslaved");
570 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
571 bond_delete_neighbor (vm, bif, sif);
574 static clib_error_t *
575 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
576 vlib_cli_command_t * cmd)
578 bond_detach_slave_args_t args = { 0 };
579 unformat_input_t _line_input, *line_input = &_line_input;
580 vnet_main_t *vnm = vnet_get_main ();
582 /* Get a line of input. */
583 if (!unformat_user (input, unformat_line_input, line_input))
584 return clib_error_return (0, "Missing required arguments.");
587 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
589 if (unformat (line_input, "%U",
590 unformat_vnet_sw_interface, vnm, &args.slave))
594 args.error = clib_error_return (0, "unknown input `%U'",
595 format_unformat_error, input);
599 unformat_free (line_input);
603 if (args.slave == ~0)
604 return clib_error_return (0, "please specify valid slave interface name");
606 bond_detach_slave (vm, &args);
612 VLIB_CLI_COMMAND (detach_interface_command, static) = {
614 .short_help = "bond del <slave-interface>",
615 .function = detach_interface_command_fn,
620 show_bond (vlib_main_t * vm)
622 bond_main_t *bm = &bond_main;
625 vlib_cli_output (vm, "%-16s %-12s %-12s %-13s %-14s %s",
626 "interface name", "sw_if_index", "mode",
627 "load balance", "active slaves", "slaves");
630 pool_foreach (bif, bm->interfaces,
632 vlib_cli_output (vm, "%-16U %-12d %-12U %-13U %-14u %u",
633 format_bond_interface_name, bif->dev_instance,
634 bif->sw_if_index, format_bond_mode, bif->mode,
635 format_bond_load_balance, bif->lb,
636 vec_len (bif->active_slaves), vec_len (bif->slaves));
642 show_bond_details (vlib_main_t * vm)
644 bond_main_t *bm = &bond_main;
649 pool_foreach (bif, bm->interfaces,
651 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
652 vlib_cli_output (vm, " mode: %U",
653 format_bond_mode, bif->mode);
654 vlib_cli_output (vm, " load balance: %U",
655 format_bond_load_balance, bif->lb);
656 if (bif->mode == BOND_MODE_ROUND_ROBIN)
657 vlib_cli_output (vm, " last xmit slave index: %u",
658 bif->lb_rr_last_index);
659 vlib_cli_output (vm, " number of active slaves: %d",
660 vec_len (bif->active_slaves));
661 vec_foreach (sw_if_index, bif->active_slaves)
663 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
664 vnet_get_main (), *sw_if_index);
666 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
667 vec_foreach (sw_if_index, bif->slaves)
669 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
670 vnet_get_main (), *sw_if_index);
672 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
673 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
674 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
679 static clib_error_t *
680 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
681 vlib_cli_command_t * cmd)
685 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
687 if (unformat (input, "details"))
691 return clib_error_return (0, "unknown input `%U'",
692 format_unformat_error, input);
697 show_bond_details (vm);
705 VLIB_CLI_COMMAND (show_bond_command, static) = {
707 .short_help = "show bond [details]",
708 .function = show_bond_fn,
713 bond_cli_init (vlib_main_t * vm)
715 bond_main_t *bm = &bond_main;
718 bm->vnet_main = vnet_get_main ();
719 bm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));
724 VLIB_INIT_FUNCTION (bond_cli_init);
727 * fd.io coding-style-patch-verification: ON
730 * eval: (c-set-style "gnu")