+void
+bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args)
+{
+ slave_if_t *sif;
+ bond_if_t *bif;
+ vnet_main_t *vnm;
+ u32 old_weight;
+
+ sif = bond_get_slave_by_sw_if_index (args->sw_if_index);
+ if (!sif)
+ {
+ args->rv = VNET_API_ERROR_INVALID_INTERFACE;
+ args->error = clib_error_return (0, "Interface not enslaved");
+ return;
+ }
+ bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
+ if (!bif)
+ {
+ args->rv = VNET_API_ERROR_INVALID_INTERFACE;
+ args->error = clib_error_return (0, "bond interface not found");
+ return;
+ }
+ if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
+ {
+ args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
+ args->error =
+ clib_error_return (0, "Weight valid for active-backup only");
+ return;
+ }
+
+ old_weight = sif->weight;
+ sif->weight = args->weight;
+ vnm = vnet_get_main ();
+ /*
+ * No need to sort the list if the affected slave is not up (not in active
+ * slave set), active slave count is 1, or the current slave is already the
+ * primary slave and new weight > old weight.
+ */
+ if (!vnet_sw_interface_is_up (vnm, sif->sw_if_index) ||
+ (vec_len (bif->active_slaves) == 1) ||
+ ((bif->active_slaves[0] == sif->sw_if_index) &&
+ (sif->weight >= old_weight)))
+ return;
+
+ bond_sort_slaves (bif);
+}
+
+static clib_error_t *
+bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ bond_set_intf_weight_args_t args = { 0 };
+ u32 sw_if_index = (u32) ~ 0;
+ unformat_input_t _line_input, *line_input = &_line_input;
+ vnet_main_t *vnm = vnet_get_main ();
+ u8 weight_enter = 0;
+ u32 weight = 0;
+
+ /* Get a line of input. */
+ if (!unformat_user (input, unformat_line_input, line_input))
+ return clib_error_return (0, "Missing required arguments.");
+
+ while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (line_input, "sw_if_index %d", &sw_if_index))
+ ;
+ else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
+ &sw_if_index))
+ ;
+ else if (unformat (line_input, "weight %u", &weight))
+ weight_enter = 1;
+ else
+ {
+ clib_error_return (0, "unknown input `%U'", format_unformat_error,
+ input);
+ break;
+ }
+ }
+
+ unformat_free (line_input);
+ if (sw_if_index == (u32) ~ 0)
+ {
+ args.rv = VNET_API_ERROR_INVALID_INTERFACE;
+ clib_error_return (0, "Interface name is invalid!");
+ }
+ if (weight_enter == 0)
+ {
+ args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
+ clib_error_return (0, "weight missing");
+ }
+
+ args.sw_if_index = sw_if_index;
+ args.weight = weight;
+ bond_set_intf_weight (vm, &args);
+
+ return args.error;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
+ .path = "set interface bond",
+ .short_help = "set interface bond <interface> | sw_if_index <idx>"
+ " weight <value>",
+ .function = bond_set_intf_cmd,
+};
+/* *INDENT-ON* */
+