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, member_if_t * mif)
28 bond_main_t *bm = &bond_main;
32 u8 switching_active = 0;
34 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
35 clib_spinlock_lock_if_init (&bif->lockp);
36 vec_foreach_index (i, bif->active_members)
38 p = *vec_elt_at_index (bif->active_members, i);
39 if (p == mif->sw_if_index)
41 if ((bif->mode == BOND_MODE_ACTIVE_BACKUP) && (i == 0) &&
42 (vec_len (bif->active_members) > 1))
43 /* deleting the active member for active-backup */
45 vec_del1 (bif->active_members, i);
46 if (mif->lacp_enabled && bif->numa_only)
48 /* For lacp mode, if we check it is a member on local numa node,
49 bif->n_numa_members should be decreased by 1 becasue the first
50 bif->n_numa_members are all members on local numa node */
51 if (i < bif->n_numa_members)
53 bif->n_numa_members--;
54 ASSERT (bif->n_numa_members >= 0);
61 /* We get a new member 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_member_sort (void *a1, void *a2)
77 member_if_t *mif1 = bond_get_member_by_sw_if_index (*s1);
78 member_if_t *mif2 = bond_get_member_by_sw_if_index (*s2);
84 * sort entries according to preference rules:
87 * 3. current active member (to prevent churning)
88 * 4. lowest sw_if_index (for deterministic behavior)
91 if (mif2->weight > mif1->weight)
93 if (mif2->weight < mif1->weight)
97 if (mif2->is_local_numa > mif1->is_local_numa)
99 if (mif2->is_local_numa < mif1->is_local_numa)
103 bif = bond_get_bond_if_by_dev_instance (mif1->bif_dev_instance);
104 /* Favor the current active member to avoid churning */
105 if (bif->active_members[0] == mif2->sw_if_index)
107 if (bif->active_members[0] == mif1->sw_if_index)
109 /* go for the tiebreaker as the last resort */
110 if (mif1->sw_if_index > mif2->sw_if_index)
112 if (mif1->sw_if_index < mif2->sw_if_index)
122 bond_sort_members (bond_if_t * bif)
124 bond_main_t *bm = &bond_main;
125 u32 old_active = bif->active_members[0];
127 vec_sort_with_function (bif->active_members, bond_member_sort);
128 if (old_active != bif->active_members[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, member_if_t * mif)
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, mif->sw_if_index);
143 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
144 clib_spinlock_lock_if_init (&bif->lockp);
145 vec_foreach_index (i, bif->active_members)
147 p = *vec_elt_at_index (bif->active_members, i);
148 if (p == mif->sw_if_index)
152 if (mif->lacp_enabled && bif->numa_only && (vm->numa_node == hw->numa_node))
154 vec_insert_elts (bif->active_members, &mif->sw_if_index, 1,
155 bif->n_numa_members);
156 bif->n_numa_members++;
159 vec_add1 (bif->active_members, mif->sw_if_index);
161 mif->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_members) == 1)
165 /* First member 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_members (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_members = vec_len (bif->active_members);
202 bondif->members = vec_len (bif->members);
206 *out_bondifs = r_bondifs;
212 bond_dump_member_ifs (member_interface_details_t ** out_memberifs,
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 member_interface_details_t *r_memberifs = NULL;
220 member_interface_details_t *memberif = NULL;
221 u32 *sw_if_index = NULL;
224 bif = bond_get_bond_if_by_sw_if_index (bond_sw_if_index);
228 vec_foreach (sw_if_index, bif->members)
230 vec_add2 (r_memberifs, memberif, 1);
231 clib_memset (memberif, 0, sizeof (*memberif));
232 mif = bond_get_member_by_sw_if_index (*sw_if_index);
235 sw = vnet_get_sw_interface (vnm, mif->sw_if_index);
236 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
237 clib_memcpy (memberif->interface_name, hi->name,
238 MIN (ARRAY_LEN (memberif->interface_name) - 1,
239 vec_len ((const char *) hi->name)));
240 /* enforce by memset() above */
242 memberif->interface_name[ARRAY_LEN (memberif->interface_name)
244 memberif->sw_if_index = mif->sw_if_index;
245 memberif->is_passive = mif->is_passive;
246 memberif->is_long_timeout = mif->is_long_timeout;
247 memberif->is_local_numa = mif->is_local_numa;
248 memberif->weight = mif->weight;
251 *out_memberifs = r_memberifs;
257 * Manage secondary mac addresses when attaching/detaching a member.
258 * If adding, copy any secondary addresses from bond interface to member.
259 * If deleting, delete the bond interface's secondary addresses from the
263 bond_member_add_del_mac_addrs (bond_if_t * bif, u32 mif_sw_if_index,
266 vnet_main_t *vnm = vnet_get_main ();
267 ethernet_interface_t *b_ei;
268 ethernet_interface_address_t *sec_mac;
269 vnet_hw_interface_t *s_hwif;
271 b_ei = ethernet_get_interface (ðernet_main, bif->hw_if_index);
272 if (!b_ei || !b_ei->secondary_addrs)
275 s_hwif = vnet_get_sup_hw_interface (vnm, mif_sw_if_index);
277 vec_foreach (sec_mac, b_ei->secondary_addrs)
278 vnet_hw_interface_add_del_mac_address (vnm, s_hwif->hw_if_index,
279 sec_mac->mac.bytes, is_add);
283 bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, member_if_t * mif)
285 bond_main_t *bm = &bond_main;
286 vnet_main_t *vnm = vnet_get_main ();
288 vnet_hw_interface_t *mif_hw;
290 mif_hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
292 bif->port_number_bitmap =
293 clib_bitmap_set (bif->port_number_bitmap,
294 ntohs (mif->actor_admin.port_number) - 1, 0);
295 bm->member_by_sw_if_index[mif->sw_if_index] = 0;
296 vec_free (mif->last_marker_pkt);
297 vec_free (mif->last_rx_pkt);
298 vec_foreach_index (i, bif->members)
300 uword p = *vec_elt_at_index (bif->members, i);
301 if (p == mif->sw_if_index)
303 vec_del1 (bif->members, i);
308 bond_disable_collecting_distributing (vm, mif);
310 vnet_feature_enable_disable ("device-input", "bond-input",
311 mif->sw_if_index, 0, 0, 0);
313 /* Put back the old mac */
314 vnet_hw_interface_change_mac_address (vnm, mif_hw->hw_if_index,
315 mif->persistent_hw_address);
317 /* delete the bond's secondary/virtual mac addrs from the member */
318 bond_member_add_del_mac_addrs (bif, mif->sw_if_index, 0 /* is_add */ );
321 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
322 (*bm->lacp_enable_disable) (vm, bif, mif, 0);
324 if (bif->mode == BOND_MODE_LACP)
326 stat_segment_deregister_state_counter
327 (bm->stats[bif->sw_if_index][mif->sw_if_index].actor_state);
328 stat_segment_deregister_state_counter
329 (bm->stats[bif->sw_if_index][mif->sw_if_index].partner_state);
332 pool_put (bm->neighbors, mif);
336 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
338 bond_main_t *bm = &bond_main;
339 vnet_main_t *vnm = vnet_get_main ();
342 vnet_hw_interface_t *hw;
343 u32 *mif_sw_if_index;
346 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
347 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
348 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
350 bif = bond_get_bond_if_by_dev_instance (hw->dev_instance);
352 vec_append (s_list, bif->members);
353 vec_foreach (mif_sw_if_index, s_list)
355 mif = bond_get_member_by_sw_if_index (*mif_sw_if_index);
357 bond_delete_neighbor (vm, bif, mif);
361 /* bring down the interface */
362 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
363 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
365 ethernet_delete_interface (vnm, bif->hw_if_index);
367 clib_bitmap_free (bif->port_number_bitmap);
368 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
369 hash_unset (bm->id_used, bif->id);
370 clib_memset (bif, 0, sizeof (*bif));
371 pool_put (bm->interfaces, bif);
377 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
379 bond_main_t *bm = &bond_main;
380 vnet_main_t *vnm = vnet_get_main ();
381 vnet_sw_interface_t *sw;
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;
461 * Add GSO and Checksum offload flags if GSO is enabled on Bond
465 vnet_hw_if_set_caps (vnm, bif->hw_if_index,
466 VNET_HW_IF_CAP_TCP_GSO |
467 VNET_HW_IF_CAP_TX_TCP_CKSUM |
468 VNET_HW_IF_CAP_TX_UDP_CKSUM);
470 if (vlib_get_thread_main ()->n_vlib_mains > 1)
471 clib_spinlock_init (&bif->lockp);
473 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
474 VNET_HW_INTERFACE_FLAG_LINK_UP);
476 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
479 args->sw_if_index = bif->sw_if_index;
483 static clib_error_t *
484 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
485 vlib_cli_command_t * cmd)
487 unformat_input_t _line_input, *line_input = &_line_input;
488 bond_create_if_args_t args = { 0 };
491 /* Get a line of input. */
492 if (!unformat_user (input, unformat_line_input, line_input))
493 return clib_error_return (0, "Missing required arguments.");
497 args.lb = BOND_LB_L2;
499 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
501 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
503 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
504 && unformat (line_input, "load-balance %U",
505 unformat_bond_load_balance, &args.lb))
507 else if (unformat (line_input, "hw-addr %U",
508 unformat_ethernet_address, args.hw_addr))
509 args.hw_addr_set = 1;
510 else if (unformat (line_input, "id %u", &args.id))
512 else if (unformat (line_input, "gso"))
514 else if (unformat (line_input, "numa-only"))
516 if (args.mode == BOND_MODE_LACP)
519 return clib_error_return (0,
520 "Only lacp mode supports numa-only so far!");
523 return clib_error_return (0, "unknown input `%U'",
524 format_unformat_error, input);
526 unformat_free (line_input);
528 if (mode_is_set == 0)
529 return clib_error_return (0, "Missing bond mode");
531 bond_create_if (vm, &args);
534 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
535 vnet_get_main (), args.sw_if_index);
541 VLIB_CLI_COMMAND (bond_create_command, static) = {
542 .path = "create bond",
543 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
544 "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} "
545 "[hw-addr <mac-address>] [id <if-id>] [gso]",
546 .function = bond_create_command_fn,
550 static clib_error_t *
551 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
552 vlib_cli_command_t * cmd)
554 unformat_input_t _line_input, *line_input = &_line_input;
555 u32 sw_if_index = ~0;
556 vnet_main_t *vnm = vnet_get_main ();
559 /* Get a line of input. */
560 if (!unformat_user (input, unformat_line_input, line_input))
561 return clib_error_return (0, "Missing <interface>");
563 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
565 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
567 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
571 return clib_error_return (0, "unknown input `%U'",
572 format_unformat_error, input);
574 unformat_free (line_input);
576 if (sw_if_index == ~0)
577 return clib_error_return (0,
578 "please specify interface name or sw_if_index");
580 rv = bond_delete_if (vm, sw_if_index);
581 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
582 return clib_error_return (0, "not a bond interface");
584 return clib_error_return (0, "error on deleting bond interface");
590 VLIB_CLI_COMMAND (bond_delete__command, static) =
592 .path = "delete bond",
593 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
594 .function = bond_delete_command_fn,
599 bond_add_member (vlib_main_t * vm, bond_add_member_args_t * args)
601 bond_main_t *bm = &bond_main;
602 vnet_main_t *vnm = vnet_get_main ();
605 vnet_interface_main_t *im = &vnm->interface_main;
606 vnet_hw_interface_t *bif_hw, *mif_hw;
607 vnet_sw_interface_t *sw;
611 bif = bond_get_bond_if_by_sw_if_index (args->group);
614 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
615 args->error = clib_error_return (0, "bond interface not found");
618 // make sure the interface is not already added as member
619 if (bond_get_member_by_sw_if_index (args->member))
621 args->rv = VNET_API_ERROR_VALUE_EXIST;
622 args->error = clib_error_return
623 (0, "interface was already added as member");
626 mif_hw = vnet_get_sup_hw_interface (vnm, args->member);
627 if (mif_hw->dev_class_index == bond_dev_class.index)
629 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
631 clib_error_return (0, "bond interface cannot be added as member");
634 if (bif->gso && !(mif_hw->caps & VNET_HW_IF_CAP_TCP_GSO))
636 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
638 clib_error_return (0, "member interface is not gso capable");
641 if (bif->mode == BOND_MODE_LACP)
643 u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index,
646 vec_validate (bm->stats, bif->sw_if_index);
647 vec_validate (bm->stats[bif->sw_if_index], args->member);
649 args->error = stat_segment_register_state_counter
650 (name, &bm->stats[bif->sw_if_index][args->member].actor_state);
651 if (args->error != 0)
653 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
658 vec_reset_length (name);
659 name = format (0, "/if/lacp/%u/%u/partner-state%c", bif->sw_if_index,
661 args->error = stat_segment_register_state_counter
662 (name, &bm->stats[bif->sw_if_index][args->member].partner_state);
664 if (args->error != 0)
666 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
671 pool_get (bm->neighbors, mif);
672 clib_memset (mif, 0, sizeof (*mif));
673 sw = pool_elt_at_index (im->sw_interfaces, args->member);
674 /* port_enabled is both admin up and hw link up */
675 mif->port_enabled = vnet_sw_interface_is_up (vnm, sw->sw_if_index);
676 mif->sw_if_index = sw->sw_if_index;
677 mif->hw_if_index = sw->hw_if_index;
678 mif->packet_template_index = (u8) ~ 0;
679 mif->is_passive = args->is_passive;
680 mif->group = args->group;
681 mif->bif_dev_instance = bif->dev_instance;
682 mif->mode = bif->mode;
684 mif->is_long_timeout = args->is_long_timeout;
685 if (args->is_long_timeout)
686 mif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
688 mif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
690 vec_validate_aligned (bm->member_by_sw_if_index, mif->sw_if_index,
691 CLIB_CACHE_LINE_BYTES);
693 * mif - bm->neighbors may be 0
694 * Left shift it by 1 bit to distinguish the valid entry that we actually
695 * store from the null entries
697 bm->member_by_sw_if_index[mif->sw_if_index] =
698 (uword) (((mif - bm->neighbors) << 1) | 1);
699 vec_add1 (bif->members, mif->sw_if_index);
701 mif_hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
703 /* Save the old mac */
704 memcpy (mif->persistent_hw_address, mif_hw->hw_address, 6);
705 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
706 if (bif->use_custom_mac)
708 vnet_hw_interface_change_mac_address (vnm, mif_hw->hw_if_index,
713 // bond interface gets the mac address from the first member
714 if (vec_len (bif->members) == 1)
716 memcpy (bif->hw_address, mif_hw->hw_address, 6);
717 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
722 // subsequent members gets the mac address of the bond interface
723 vnet_hw_interface_change_mac_address (vnm, mif_hw->hw_if_index,
728 /* if there are secondary/virtual mac addrs, propagate to the member */
729 bond_member_add_del_mac_addrs (bif, mif->sw_if_index, 1 /* is_add */ );
731 if (bif_hw->l2_if_count)
732 ethernet_set_flags (vnm, mif_hw->hw_if_index,
733 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
735 ethernet_set_flags (vnm, mif_hw->hw_if_index,
736 /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
738 if (bif->mode == BOND_MODE_LACP)
740 if (bm->lacp_enable_disable)
741 (*bm->lacp_enable_disable) (vm, bif, mif, 1);
743 else if (mif->port_enabled)
745 bond_enable_collecting_distributing (vm, mif);
748 vec_foreach_index (thread_index, bm->per_thread_data)
750 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
753 vec_validate_aligned (ptd->per_port_queue, vec_len (bif->members) - 1,
754 CLIB_CACHE_LINE_BYTES);
756 vec_foreach_index (mif_if_index, ptd->per_port_queue)
758 ptd->per_port_queue[mif_if_index].n_buffers = 0;
762 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
763 mif->sw_if_index, 1, 0, 0);
768 clib_error_return (0,
769 "Error encountered on input feature arc enable");
773 static clib_error_t *
774 add_member_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
775 vlib_cli_command_t * cmd)
777 bond_add_member_args_t args = { 0 };
778 unformat_input_t _line_input, *line_input = &_line_input;
779 vnet_main_t *vnm = vnet_get_main ();
781 /* Get a line of input. */
782 if (!unformat_user (input, unformat_line_input, line_input))
783 return clib_error_return (0, "Missing required arguments.");
787 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
789 if (unformat (line_input, "%U %U",
790 unformat_vnet_sw_interface, vnm, &args.group,
791 unformat_vnet_sw_interface, vnm, &args.member))
793 else if (unformat (line_input, "passive"))
795 else if (unformat (line_input, "long-timeout"))
796 args.is_long_timeout = 1;
799 args.error = clib_error_return (0, "unknown input `%U'",
800 format_unformat_error, input);
804 unformat_free (line_input);
808 if (args.group == ~0)
809 return clib_error_return (0, "Missing bond interface");
810 if (args.member == ~0)
811 return clib_error_return (0,
812 "please specify valid member interface name");
814 bond_add_member (vm, &args);
820 VLIB_CLI_COMMAND (add_member_interface_command, static) = {
822 .short_help = "bond add <BondEthernetx> <member-interface> "
823 "[passive] [long-timeout]",
824 .function = add_member_interface_command_fn,
829 bond_detach_member (vlib_main_t * vm, bond_detach_member_args_t * args)
834 mif = bond_get_member_by_sw_if_index (args->member);
837 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
838 args->error = clib_error_return (0, "interface was not a member");
841 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
842 bond_delete_neighbor (vm, bif, mif);
845 static clib_error_t *
846 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
847 vlib_cli_command_t * cmd)
849 bond_detach_member_args_t args = { 0 };
850 unformat_input_t _line_input, *line_input = &_line_input;
851 vnet_main_t *vnm = vnet_get_main ();
853 /* Get a line of input. */
854 if (!unformat_user (input, unformat_line_input, line_input))
855 return clib_error_return (0, "Missing required arguments.");
858 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
860 if (unformat (line_input, "%U",
861 unformat_vnet_sw_interface, vnm, &args.member))
865 args.error = clib_error_return (0, "unknown input `%U'",
866 format_unformat_error, input);
870 unformat_free (line_input);
874 if (args.member == ~0)
875 return clib_error_return (0,
876 "please specify valid member interface name");
878 bond_detach_member (vm, &args);
884 VLIB_CLI_COMMAND (detach_interface_command, static) = {
886 .short_help = "bond del <member-interface>",
887 .function = detach_interface_command_fn,
892 show_bond (vlib_main_t * vm)
894 bond_main_t *bm = &bond_main;
897 vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
898 "interface name", "sw_if_index", "mode",
899 "load balance", "active members", "members");
902 pool_foreach (bif, bm->interfaces)
904 vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
905 format_bond_interface_name, bif->dev_instance,
906 bif->sw_if_index, format_bond_mode, bif->mode,
907 format_bond_load_balance, bif->lb,
908 vec_len (bif->active_members), vec_len (bif->members));
914 show_bond_details (vlib_main_t * vm)
916 bond_main_t *bm = &bond_main;
921 pool_foreach (bif, bm->interfaces)
923 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
924 vlib_cli_output (vm, " mode: %U",
925 format_bond_mode, bif->mode);
926 vlib_cli_output (vm, " load balance: %U",
927 format_bond_load_balance, bif->lb);
929 vlib_cli_output (vm, " gso enable");
930 if (bif->mode == BOND_MODE_ROUND_ROBIN)
931 vlib_cli_output (vm, " last xmit member index: %u",
932 bif->lb_rr_last_index);
933 vlib_cli_output (vm, " number of active members: %d",
934 vec_len (bif->active_members));
935 vec_foreach (sw_if_index, bif->active_members)
937 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
938 vnet_get_main (), *sw_if_index);
939 if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
941 member_if_t *mif = bond_get_member_by_sw_if_index (*sw_if_index);
943 vlib_cli_output (vm, " weight: %u, is_local_numa: %u, "
944 "sw_if_index: %u", mif->weight,
945 mif->is_local_numa, mif->sw_if_index);
948 vlib_cli_output (vm, " number of members: %d", vec_len (bif->members));
949 vec_foreach (sw_if_index, bif->members)
951 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
952 vnet_get_main (), *sw_if_index);
954 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
955 vlib_cli_output (vm, " interface id: %d", bif->id);
956 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
957 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
962 static clib_error_t *
963 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
964 vlib_cli_command_t * cmd)
968 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
970 if (unformat (input, "details"))
974 return clib_error_return (0, "unknown input `%U'",
975 format_unformat_error, input);
980 show_bond_details (vm);
988 VLIB_CLI_COMMAND (show_bond_command, static) = {
990 .short_help = "show bond [details]",
991 .function = show_bond_fn,
996 bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args)
1003 mif = bond_get_member_by_sw_if_index (args->sw_if_index);
1006 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1007 args->error = clib_error_return (0, "Interface not a member");
1010 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
1013 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1014 args->error = clib_error_return (0, "bond interface not found");
1017 if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
1019 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
1021 clib_error_return (0, "Weight valid for active-backup only");
1025 old_weight = mif->weight;
1026 mif->weight = args->weight;
1027 vnm = vnet_get_main ();
1029 * No need to sort the list if the affected member is not up (not in active
1030 * member set), active member count is 1, or the current member is already the
1031 * primary member and new weight > old weight.
1033 if (!vnet_sw_interface_is_up (vnm, mif->sw_if_index) ||
1034 (vec_len (bif->active_members) == 1) ||
1035 ((bif->active_members[0] == mif->sw_if_index) &&
1036 (mif->weight >= old_weight)))
1039 bond_sort_members (bif);
1042 static clib_error_t *
1043 bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
1044 vlib_cli_command_t * cmd)
1046 bond_set_intf_weight_args_t args = { 0 };
1047 u32 sw_if_index = (u32) ~ 0;
1048 unformat_input_t _line_input, *line_input = &_line_input;
1049 vnet_main_t *vnm = vnet_get_main ();
1050 u8 weight_enter = 0;
1053 /* Get a line of input. */
1054 if (!unformat_user (input, unformat_line_input, line_input))
1055 return clib_error_return (0, "Missing required arguments.");
1057 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1059 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1061 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
1064 else if (unformat (line_input, "weight %u", &weight))
1068 clib_error_return (0, "unknown input `%U'", format_unformat_error,
1074 unformat_free (line_input);
1075 if (sw_if_index == (u32) ~ 0)
1077 args.rv = VNET_API_ERROR_INVALID_INTERFACE;
1078 clib_error_return (0, "Interface name is invalid!");
1080 if (weight_enter == 0)
1082 args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
1083 clib_error_return (0, "weight missing");
1086 args.sw_if_index = sw_if_index;
1087 args.weight = weight;
1088 bond_set_intf_weight (vm, &args);
1094 VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
1095 .path = "set interface bond",
1096 .short_help = "set interface bond <interface> | sw_if_index <idx>"
1098 .function = bond_set_intf_cmd,
1103 bond_cli_init (vlib_main_t * vm)
1105 bond_main_t *bm = &bond_main;
1108 bm->vnet_main = vnet_get_main ();
1109 vec_validate_aligned (bm->member_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
1110 vec_validate_aligned (bm->per_thread_data,
1111 vlib_get_thread_main ()->n_vlib_mains - 1,
1112 CLIB_CACHE_LINE_BYTES);
1117 VLIB_INIT_FUNCTION (bond_cli_init);
1120 * fd.io coding-style-patch-verification: ON
1123 * eval: (c-set-style "gnu")