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>
23 #include <vpp/stats/stat_segment.h>
26 bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
28 bond_main_t *bm = &bond_main;
32 u8 switching_active = 0;
34 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
35 clib_spinlock_lock_if_init (&bif->lockp);
36 vec_foreach_index (i, bif->active_slaves)
38 p = *vec_elt_at_index (bif->active_slaves, i);
39 if (p == sif->sw_if_index)
41 if ((bif->mode == BOND_MODE_ACTIVE_BACKUP) && (i == 0) &&
42 (vec_len (bif->active_slaves) > 1))
43 /* deleting the active slave for active-backup */
45 vec_del1 (bif->active_slaves, i);
46 if (sif->lacp_enabled && bif->numa_only)
48 /* For lacp mode, if we check it is a slave on local numa node,
49 bif->n_numa_slaves should be decreased by 1 becasue the first
50 bif->n_numa_slaves are all slaves on local numa node */
51 if (i < bif->n_numa_slaves)
54 ASSERT (bif->n_numa_slaves >= 0);
61 /* We get a new slave just becoming active */
63 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
64 BOND_SEND_GARP_NA, bif->hw_if_index);
65 clib_spinlock_unlock_if_init (&bif->lockp);
69 * return 1 if s2 is preferred.
70 * return -1 if s1 is preferred.
73 bond_slave_sort (void *a1, void *a2)
77 slave_if_t *sif1 = bond_get_slave_by_sw_if_index (*s1);
78 slave_if_t *sif2 = bond_get_slave_by_sw_if_index (*s2);
84 * sort entries according to preference rules:
87 * 3. current active slave (to prevent churning)
88 * 4. lowest sw_if_index (for deterministic behavior)
91 if (sif2->weight > sif1->weight)
93 if (sif2->weight < sif1->weight)
97 if (sif2->is_local_numa > sif1->is_local_numa)
99 if (sif2->is_local_numa < sif1->is_local_numa)
103 bif = bond_get_master_by_dev_instance (sif1->bif_dev_instance);
104 /* Favor the current active slave to avoid churning */
105 if (bif->active_slaves[0] == sif2->sw_if_index)
107 if (bif->active_slaves[0] == sif1->sw_if_index)
109 /* go for the tiebreaker as the last resort */
110 if (sif1->sw_if_index > sif2->sw_if_index)
112 if (sif1->sw_if_index < sif2->sw_if_index)
122 bond_sort_slaves (bond_if_t * bif)
124 bond_main_t *bm = &bond_main;
125 u32 old_active = bif->active_slaves[0];
127 vec_sort_with_function (bif->active_slaves, bond_slave_sort);
128 if (old_active != bif->active_slaves[0])
129 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
130 BOND_SEND_GARP_NA, bif->hw_if_index);
134 bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
137 bond_main_t *bm = &bond_main;
138 vnet_main_t *vnm = vnet_get_main ();
139 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
143 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
144 clib_spinlock_lock_if_init (&bif->lockp);
145 vec_foreach_index (i, bif->active_slaves)
147 p = *vec_elt_at_index (bif->active_slaves, i);
148 if (p == sif->sw_if_index)
152 if (sif->lacp_enabled && bif->numa_only && (vm->numa_node == hw->numa_node))
154 vec_insert_elts (bif->active_slaves, &sif->sw_if_index, 1,
156 bif->n_numa_slaves++;
159 vec_add1 (bif->active_slaves, sif->sw_if_index);
161 sif->is_local_numa = (vm->numa_node == hw->numa_node) ? 1 : 0;
162 if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
164 if (vec_len (bif->active_slaves) == 1)
165 /* First slave becomes active? */
166 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
167 BOND_SEND_GARP_NA, bif->hw_if_index);
169 bond_sort_slaves (bif);
173 clib_spinlock_unlock_if_init (&bif->lockp);
177 bond_dump_ifs (bond_interface_details_t ** out_bondifs)
179 vnet_main_t *vnm = vnet_get_main ();
180 bond_main_t *bm = &bond_main;
182 vnet_hw_interface_t *hi;
183 bond_interface_details_t *r_bondifs = NULL;
184 bond_interface_details_t *bondif = NULL;
187 pool_foreach (bif, bm->interfaces,
188 vec_add2(r_bondifs, bondif, 1);
189 clib_memset (bondif, 0, sizeof (*bondif));
190 bondif->id = bif->id;
191 bondif->sw_if_index = bif->sw_if_index;
192 hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
193 clib_memcpy(bondif->interface_name, hi->name,
194 MIN (ARRAY_LEN (bondif->interface_name) - 1,
195 vec_len ((const char *) hi->name)));
196 /* enforce by memset() above */
197 ASSERT(0 == bondif->interface_name[ARRAY_LEN (bondif->interface_name) - 1]);
198 bondif->mode = bif->mode;
199 bondif->lb = bif->lb;
200 bondif->numa_only = bif->numa_only;
201 bondif->active_slaves = vec_len (bif->active_slaves);
202 bondif->slaves = vec_len (bif->slaves);
206 *out_bondifs = r_bondifs;
212 bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
213 u32 bond_sw_if_index)
215 vnet_main_t *vnm = vnet_get_main ();
217 vnet_hw_interface_t *hi;
218 vnet_sw_interface_t *sw;
219 slave_interface_details_t *r_slaveifs = NULL;
220 slave_interface_details_t *slaveif = NULL;
221 u32 *sw_if_index = NULL;
224 bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
228 vec_foreach (sw_if_index, bif->slaves)
230 vec_add2 (r_slaveifs, slaveif, 1);
231 clib_memset (slaveif, 0, sizeof (*slaveif));
232 sif = bond_get_slave_by_sw_if_index (*sw_if_index);
235 sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
236 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
237 clib_memcpy (slaveif->interface_name, hi->name,
238 MIN (ARRAY_LEN (slaveif->interface_name) - 1,
239 vec_len ((const char *) hi->name)));
240 /* enforce by memset() above */
242 slaveif->interface_name[ARRAY_LEN (slaveif->interface_name) -
244 slaveif->sw_if_index = sif->sw_if_index;
245 slaveif->is_passive = sif->is_passive;
246 slaveif->is_long_timeout = sif->is_long_timeout;
247 slaveif->is_local_numa = sif->is_local_numa;
248 slaveif->weight = sif->weight;
251 *out_slaveifs = r_slaveifs;
257 * Manage secondary mac addresses when attaching/detaching a slave.
258 * If adding, copies any secondary addresses from master to slave
259 * If deleting, deletes the master's secondary addresses from the slave
263 bond_slave_add_del_mac_addrs (bond_if_t * bif, u32 sif_sw_if_index, u8 is_add)
265 vnet_main_t *vnm = vnet_get_main ();
266 ethernet_interface_t *b_ei;
267 mac_address_t *sec_mac;
268 vnet_hw_interface_t *s_hwif;
270 b_ei = ethernet_get_interface (ðernet_main, bif->hw_if_index);
271 if (!b_ei || !b_ei->secondary_addrs)
274 s_hwif = vnet_get_sup_hw_interface (vnm, sif_sw_if_index);
276 vec_foreach (sec_mac, b_ei->secondary_addrs)
277 vnet_hw_interface_add_del_mac_address (vnm, s_hwif->hw_if_index,
278 sec_mac->bytes, is_add);
282 bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
284 bond_main_t *bm = &bond_main;
285 vnet_main_t *vnm = vnet_get_main ();
287 vnet_hw_interface_t *sif_hw;
289 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
291 bif->port_number_bitmap =
292 clib_bitmap_set (bif->port_number_bitmap,
293 ntohs (sif->actor_admin.port_number) - 1, 0);
294 bm->slave_by_sw_if_index[sif->sw_if_index] = 0;
295 vec_free (sif->last_marker_pkt);
296 vec_free (sif->last_rx_pkt);
297 vec_foreach_index (i, bif->slaves)
299 uword p = *vec_elt_at_index (bif->slaves, i);
300 if (p == sif->sw_if_index)
302 vec_del1 (bif->slaves, i);
307 bond_disable_collecting_distributing (vm, sif);
309 vnet_feature_enable_disable ("device-input", "bond-input",
310 sif->sw_if_index, 0, 0, 0);
312 /* Put back the old mac */
313 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
314 sif->persistent_hw_address);
316 /* delete the bond's secondary/virtual mac addrs from the slave */
317 bond_slave_add_del_mac_addrs (bif, sif->sw_if_index, 0 /* is_add */ );
320 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
321 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
323 if (bif->mode == BOND_MODE_LACP)
325 stat_segment_deregister_state_counter
326 (bm->stats[bif->sw_if_index][sif->sw_if_index].actor_state);
327 stat_segment_deregister_state_counter
328 (bm->stats[bif->sw_if_index][sif->sw_if_index].partner_state);
331 pool_put (bm->neighbors, sif);
335 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
337 bond_main_t *bm = &bond_main;
338 vnet_main_t *vnm = vnet_get_main ();
341 vnet_hw_interface_t *hw;
342 u32 *sif_sw_if_index;
345 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
346 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
347 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
349 bif = bond_get_master_by_dev_instance (hw->dev_instance);
351 vec_append (s_list, bif->slaves);
352 vec_foreach (sif_sw_if_index, s_list)
354 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
356 bond_delete_neighbor (vm, bif, sif);
360 /* bring down the interface */
361 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
362 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
364 ethernet_delete_interface (vnm, bif->hw_if_index);
366 clib_bitmap_free (bif->port_number_bitmap);
367 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
368 hash_unset (bm->id_used, bif->id);
369 clib_memset (bif, 0, sizeof (*bif));
370 pool_put (bm->interfaces, bif);
376 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
378 bond_main_t *bm = &bond_main;
379 vnet_main_t *vnm = vnet_get_main ();
380 vnet_sw_interface_t *sw;
382 vnet_hw_interface_t *hw;
384 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
386 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
387 args->error = clib_error_return (0, "LACP plugin is not loaded");
390 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
392 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
393 args->error = clib_error_return (0, "Invalid mode");
396 if (args->lb > BOND_LB_L23)
398 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
399 args->error = clib_error_return (0, "Invalid load-balance");
402 pool_get (bm->interfaces, bif);
403 clib_memset (bif, 0, sizeof (*bif));
404 bif->dev_instance = bif - bm->interfaces;
407 bif->mode = args->mode;
408 bif->gso = args->gso;
410 // Adjust requested interface id
412 bif->id = bif->dev_instance;
413 if (hash_get (bm->id_used, bif->id))
415 args->rv = VNET_API_ERROR_INSTANCE_IN_USE;
416 pool_put (bm->interfaces, bif);
419 hash_set (bm->id_used, bif->id, 1);
421 // Special load-balance mode used for rr and bc
422 if (bif->mode == BOND_MODE_ROUND_ROBIN)
423 bif->lb = BOND_LB_RR;
424 else if (bif->mode == BOND_MODE_BROADCAST)
425 bif->lb = BOND_LB_BC;
426 else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
427 bif->lb = BOND_LB_AB;
429 bif->use_custom_mac = args->hw_addr_set;
430 if (!args->hw_addr_set)
432 f64 now = vlib_time_now (vm);
434 rnd = (u32) (now * 1e6);
435 rnd = random_u32 (&rnd);
437 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
438 args->hw_addr[0] = 2;
439 args->hw_addr[1] = 0xfe;
441 memcpy (bif->hw_address, args->hw_addr, 6);
442 args->error = ethernet_register_interface
443 (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ ,
444 bif->hw_address /* ethernet address */ ,
445 &bif->hw_if_index, 0 /* flag change */ );
449 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
450 hash_unset (bm->id_used, bif->id);
451 pool_put (bm->interfaces, bif);
455 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
456 bif->sw_if_index = sw->sw_if_index;
457 bif->group = bif->sw_if_index;
458 bif->numa_only = args->numa_only;
460 hw = vnet_get_hw_interface (vnm, bif->hw_if_index);
461 hw->flags |= (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
462 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
463 if (vlib_get_thread_main ()->n_vlib_mains > 1)
464 clib_spinlock_init (&bif->lockp);
466 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
467 VNET_HW_INTERFACE_FLAG_LINK_UP);
469 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
472 args->sw_if_index = bif->sw_if_index;
476 static clib_error_t *
477 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
478 vlib_cli_command_t * cmd)
480 unformat_input_t _line_input, *line_input = &_line_input;
481 bond_create_if_args_t args = { 0 };
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 args.lb = BOND_LB_L2;
492 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
494 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
496 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
497 && unformat (line_input, "load-balance %U",
498 unformat_bond_load_balance, &args.lb))
500 else if (unformat (line_input, "hw-addr %U",
501 unformat_ethernet_address, args.hw_addr))
502 args.hw_addr_set = 1;
503 else if (unformat (line_input, "id %u", &args.id))
505 else if (unformat (line_input, "gso"))
507 else if (unformat (line_input, "numa-only"))
509 if (args.mode == BOND_MODE_LACP)
512 return clib_error_return (0,
513 "Only lacp mode supports numa-only so far!");
516 return clib_error_return (0, "unknown input `%U'",
517 format_unformat_error, input);
519 unformat_free (line_input);
521 if (mode_is_set == 0)
522 return clib_error_return (0, "Missing bond mode");
524 bond_create_if (vm, &args);
527 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
528 vnet_get_main (), args.sw_if_index);
534 VLIB_CLI_COMMAND (bond_create_command, static) = {
535 .path = "create bond",
536 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
537 "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} "
538 "[hw-addr <mac-address>] [id <if-id>] [gso]",
539 .function = bond_create_command_fn,
543 static clib_error_t *
544 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
545 vlib_cli_command_t * cmd)
547 unformat_input_t _line_input, *line_input = &_line_input;
548 u32 sw_if_index = ~0;
549 vnet_main_t *vnm = vnet_get_main ();
552 /* Get a line of input. */
553 if (!unformat_user (input, unformat_line_input, line_input))
554 return clib_error_return (0, "Missing <interface>");
556 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
558 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
560 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
564 return clib_error_return (0, "unknown input `%U'",
565 format_unformat_error, input);
567 unformat_free (line_input);
569 if (sw_if_index == ~0)
570 return clib_error_return (0,
571 "please specify interface name or sw_if_index");
573 rv = bond_delete_if (vm, sw_if_index);
574 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
575 return clib_error_return (0, "not a bond interface");
577 return clib_error_return (0, "error on deleting bond interface");
583 VLIB_CLI_COMMAND (bond_delete__command, static) =
585 .path = "delete bond",
586 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
587 .function = bond_delete_command_fn,
592 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
594 bond_main_t *bm = &bond_main;
595 vnet_main_t *vnm = vnet_get_main ();
598 vnet_interface_main_t *im = &vnm->interface_main;
599 vnet_hw_interface_t *bif_hw, *sif_hw;
600 vnet_sw_interface_t *sw;
604 bif = bond_get_master_by_sw_if_index (args->group);
607 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
608 args->error = clib_error_return (0, "bond interface not found");
611 // make sure the interface is not already enslaved
612 if (bond_get_slave_by_sw_if_index (args->slave))
614 args->rv = VNET_API_ERROR_VALUE_EXIST;
615 args->error = clib_error_return (0, "interface was already enslaved");
618 sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
619 if (sif_hw->dev_class_index == bond_dev_class.index)
621 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
623 clib_error_return (0, "bond interface cannot be enslaved");
626 if (bif->gso && !(sif_hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO))
628 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
630 clib_error_return (0, "slave interface is not gso capable");
633 if (bif->mode == BOND_MODE_LACP)
635 u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index,
638 vec_validate (bm->stats, bif->sw_if_index);
639 vec_validate (bm->stats[bif->sw_if_index], args->slave);
641 args->error = stat_segment_register_state_counter
642 (name, &bm->stats[bif->sw_if_index][args->slave].actor_state);
643 if (args->error != 0)
645 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
650 vec_reset_length (name);
651 name = format (0, "/if/lacp/%u/%u/partner-state%c", bif->sw_if_index,
653 args->error = stat_segment_register_state_counter
654 (name, &bm->stats[bif->sw_if_index][args->slave].partner_state);
656 if (args->error != 0)
658 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
663 pool_get (bm->neighbors, sif);
664 clib_memset (sif, 0, sizeof (*sif));
665 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
666 /* port_enabled is both admin up and hw link up */
667 sif->port_enabled = vnet_sw_interface_is_up (vnm, sw->sw_if_index);
668 sif->sw_if_index = sw->sw_if_index;
669 sif->hw_if_index = sw->hw_if_index;
670 sif->packet_template_index = (u8) ~ 0;
671 sif->is_passive = args->is_passive;
672 sif->group = args->group;
673 sif->bif_dev_instance = bif->dev_instance;
674 sif->mode = bif->mode;
676 sif->is_long_timeout = args->is_long_timeout;
677 if (args->is_long_timeout)
678 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
680 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
682 vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
683 CLIB_CACHE_LINE_BYTES);
685 * sif - bm->neighbors may be 0
686 * Left shift it by 1 bit to distinguish the valid entry that we actually
687 * store from the null entries
689 bm->slave_by_sw_if_index[sif->sw_if_index] =
690 (uword) (((sif - bm->neighbors) << 1) | 1);
691 vec_add1 (bif->slaves, sif->sw_if_index);
693 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
695 /* Save the old mac */
696 memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
697 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
698 if (bif->use_custom_mac)
700 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
705 // bond interface gets the mac address from the first slave
706 if (vec_len (bif->slaves) == 1)
708 memcpy (bif->hw_address, sif_hw->hw_address, 6);
709 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
714 // subsequent slaves gets the mac address of the bond interface
715 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
720 /* if there are secondary/virtual mac addrs, propagate to the slave */
721 bond_slave_add_del_mac_addrs (bif, sif->sw_if_index, 1 /* is_add */ );
723 if (bif_hw->l2_if_count)
725 ethernet_set_flags (vnm, sif_hw->hw_if_index,
726 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
727 /* ensure all packets go to ethernet-input */
728 ethernet_set_rx_redirect (vnm, sif_hw, 1);
731 if (bif->mode == BOND_MODE_LACP)
733 if (bm->lacp_enable_disable)
734 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
736 else if (sif->port_enabled)
738 bond_enable_collecting_distributing (vm, sif);
741 vec_foreach_index (thread_index, bm->per_thread_data)
743 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
746 vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1,
747 CLIB_CACHE_LINE_BYTES);
749 vec_foreach_index (sif_if_index, ptd->per_port_queue)
751 ptd->per_port_queue[sif_if_index].n_buffers = 0;
755 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
756 sif->sw_if_index, 1, 0, 0);
761 clib_error_return (0,
762 "Error encountered on input feature arc enable");
766 static clib_error_t *
767 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
768 vlib_cli_command_t * cmd)
770 bond_enslave_args_t args = { 0 };
771 unformat_input_t _line_input, *line_input = &_line_input;
772 vnet_main_t *vnm = vnet_get_main ();
774 /* Get a line of input. */
775 if (!unformat_user (input, unformat_line_input, line_input))
776 return clib_error_return (0, "Missing required arguments.");
780 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
782 if (unformat (line_input, "%U %U",
783 unformat_vnet_sw_interface, vnm, &args.group,
784 unformat_vnet_sw_interface, vnm, &args.slave))
786 else if (unformat (line_input, "passive"))
788 else if (unformat (line_input, "long-timeout"))
789 args.is_long_timeout = 1;
792 args.error = clib_error_return (0, "unknown input `%U'",
793 format_unformat_error, input);
797 unformat_free (line_input);
801 if (args.group == ~0)
802 return clib_error_return (0, "Missing bond interface");
803 if (args.slave == ~0)
804 return clib_error_return (0, "please specify valid slave interface name");
806 bond_enslave (vm, &args);
812 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
814 .short_help = "bond add <BondEthernetx> <slave-interface> "
815 "[passive] [long-timeout]",
816 .function = enslave_interface_command_fn,
821 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
826 sif = bond_get_slave_by_sw_if_index (args->slave);
829 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
830 args->error = clib_error_return (0, "interface was not enslaved");
833 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
834 bond_delete_neighbor (vm, bif, sif);
837 static clib_error_t *
838 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
839 vlib_cli_command_t * cmd)
841 bond_detach_slave_args_t args = { 0 };
842 unformat_input_t _line_input, *line_input = &_line_input;
843 vnet_main_t *vnm = vnet_get_main ();
845 /* Get a line of input. */
846 if (!unformat_user (input, unformat_line_input, line_input))
847 return clib_error_return (0, "Missing required arguments.");
850 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
852 if (unformat (line_input, "%U",
853 unformat_vnet_sw_interface, vnm, &args.slave))
857 args.error = clib_error_return (0, "unknown input `%U'",
858 format_unformat_error, input);
862 unformat_free (line_input);
866 if (args.slave == ~0)
867 return clib_error_return (0, "please specify valid slave interface name");
869 bond_detach_slave (vm, &args);
875 VLIB_CLI_COMMAND (detach_interface_command, static) = {
877 .short_help = "bond del <slave-interface>",
878 .function = detach_interface_command_fn,
883 show_bond (vlib_main_t * vm)
885 bond_main_t *bm = &bond_main;
888 vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
889 "interface name", "sw_if_index", "mode",
890 "load balance", "active slaves", "slaves");
893 pool_foreach (bif, bm->interfaces,
895 vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
896 format_bond_interface_name, bif->dev_instance,
897 bif->sw_if_index, format_bond_mode, bif->mode,
898 format_bond_load_balance, bif->lb,
899 vec_len (bif->active_slaves), vec_len (bif->slaves));
905 show_bond_details (vlib_main_t * vm)
907 bond_main_t *bm = &bond_main;
912 pool_foreach (bif, bm->interfaces,
914 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
915 vlib_cli_output (vm, " mode: %U",
916 format_bond_mode, bif->mode);
917 vlib_cli_output (vm, " load balance: %U",
918 format_bond_load_balance, bif->lb);
920 vlib_cli_output (vm, " gso enable");
921 if (bif->mode == BOND_MODE_ROUND_ROBIN)
922 vlib_cli_output (vm, " last xmit slave index: %u",
923 bif->lb_rr_last_index);
924 vlib_cli_output (vm, " number of active slaves: %d",
925 vec_len (bif->active_slaves));
926 vec_foreach (sw_if_index, bif->active_slaves)
928 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
929 vnet_get_main (), *sw_if_index);
930 if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
932 slave_if_t *sif = bond_get_slave_by_sw_if_index (*sw_if_index);
934 vlib_cli_output (vm, " weight: %u, is_local_numa: %u, "
935 "sw_if_index: %u", sif->weight,
936 sif->is_local_numa, sif->sw_if_index);
939 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
940 vec_foreach (sw_if_index, bif->slaves)
942 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
943 vnet_get_main (), *sw_if_index);
945 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
946 vlib_cli_output (vm, " interface id: %d", bif->id);
947 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
948 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
953 static clib_error_t *
954 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
955 vlib_cli_command_t * cmd)
959 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
961 if (unformat (input, "details"))
965 return clib_error_return (0, "unknown input `%U'",
966 format_unformat_error, input);
971 show_bond_details (vm);
979 VLIB_CLI_COMMAND (show_bond_command, static) = {
981 .short_help = "show bond [details]",
982 .function = show_bond_fn,
987 bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args)
994 sif = bond_get_slave_by_sw_if_index (args->sw_if_index);
997 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
998 args->error = clib_error_return (0, "Interface not enslaved");
1001 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
1004 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1005 args->error = clib_error_return (0, "bond interface not found");
1008 if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
1010 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
1012 clib_error_return (0, "Weight valid for active-backup only");
1016 old_weight = sif->weight;
1017 sif->weight = args->weight;
1018 vnm = vnet_get_main ();
1020 * No need to sort the list if the affected slave is not up (not in active
1021 * slave set), active slave count is 1, or the current slave is already the
1022 * primary slave and new weight > old weight.
1024 if (!vnet_sw_interface_is_up (vnm, sif->sw_if_index) ||
1025 (vec_len (bif->active_slaves) == 1) ||
1026 ((bif->active_slaves[0] == sif->sw_if_index) &&
1027 (sif->weight >= old_weight)))
1030 bond_sort_slaves (bif);
1033 static clib_error_t *
1034 bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
1035 vlib_cli_command_t * cmd)
1037 bond_set_intf_weight_args_t args = { 0 };
1038 u32 sw_if_index = (u32) ~ 0;
1039 unformat_input_t _line_input, *line_input = &_line_input;
1040 vnet_main_t *vnm = vnet_get_main ();
1041 u8 weight_enter = 0;
1044 /* Get a line of input. */
1045 if (!unformat_user (input, unformat_line_input, line_input))
1046 return clib_error_return (0, "Missing required arguments.");
1048 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1050 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1052 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
1055 else if (unformat (line_input, "weight %u", &weight))
1059 clib_error_return (0, "unknown input `%U'", format_unformat_error,
1065 unformat_free (line_input);
1066 if (sw_if_index == (u32) ~ 0)
1068 args.rv = VNET_API_ERROR_INVALID_INTERFACE;
1069 clib_error_return (0, "Interface name is invalid!");
1071 if (weight_enter == 0)
1073 args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
1074 clib_error_return (0, "weight missing");
1077 args.sw_if_index = sw_if_index;
1078 args.weight = weight;
1079 bond_set_intf_weight (vm, &args);
1085 VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
1086 .path = "set interface bond",
1087 .short_help = "set interface bond <interface> | sw_if_index <idx>"
1089 .function = bond_set_intf_cmd,
1094 bond_cli_init (vlib_main_t * vm)
1096 bond_main_t *bm = &bond_main;
1099 bm->vnet_main = vnet_get_main ();
1100 vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
1101 vec_validate_aligned (bm->per_thread_data,
1102 vlib_get_thread_main ()->n_vlib_mains - 1,
1103 CLIB_CACHE_LINE_BYTES);
1108 VLIB_INIT_FUNCTION (bond_cli_init);
1111 * fd.io coding-style-patch-verification: ON
1114 * eval: (c-set-style "gnu")