libnm: sort "mode" in nm_setting_bond_get_option() first

Internally, the options are tracked in a hash table and of undefined
sort order. However, nm_setting_bond_get_option() always returns a stable
(sorted) order.

Move "mode" as first, because that is usually the most interesting option.

The effect is:

  $ nmcli -o connection show "$BOND_PROFILE"
  ...
  -bond.options:  arp_interval=5,arp_ip_target=192.168.7.7,arp_validate=active,mode=balance-rr,use_carrier=0
  +bond.options:  mode=balance-rr,arp_interval=5,arp_ip_target=192.168.7.7,arp_validate=active,use_carrier=0

This doesn't affect keyfile, which sorts the hash keys themself (and
doesn't treat the "mode" special).

This however does affect ifcfg-rh writer how it writes the BONDING_OPTS
variable. I think this change is fine and preferable.
This commit is contained in:
Thomas Haller 2020-02-19 16:16:02 +01:00
parent 5ccab333d0
commit ae1008b239
2 changed files with 15 additions and 2 deletions

View file

@ -197,6 +197,19 @@ nm_setting_bond_get_num_options (NMSettingBond *setting)
return g_hash_table_size (NM_SETTING_BOND_GET_PRIVATE (setting)->options);
}
static int
_get_option_sort (gconstpointer p_a, gconstpointer p_b, gpointer _unused)
{
const char *a = *((const char *const*) p_a);
const char *b = *((const char *const*) p_b);
NM_CMP_DIRECT (nm_streq (b, NM_SETTING_BOND_OPTION_MODE),
nm_streq (a, NM_SETTING_BOND_OPTION_MODE));
NM_CMP_DIRECT_STRCMP (a, b);
nm_assert_not_reached ();
return 0;
}
/**
* nm_setting_bond_get_option:
* @setting: the #NMSettingBond
@ -236,7 +249,7 @@ nm_setting_bond_get_option (NMSettingBond *setting,
return FALSE;
if (!G_UNLIKELY (priv->options_idx_cache))
priv->options_idx_cache = nm_utils_named_values_from_str_dict (priv->options, NULL);
priv->options_idx_cache = nm_utils_named_values_from_str_dict_with_sort (priv->options, NULL, _get_option_sort, NULL);
NM_SET_OUT (out_name, priv->options_idx_cache[idx].name);
NM_SET_OUT (out_value, priv->options_idx_cache[idx].value_str);

View file

@ -1,4 +1,4 @@
BONDING_OPTS="downdelay=5 miimon=100 mode=balance-rr updelay=10"
BONDING_OPTS="mode=balance-rr downdelay=5 miimon=100 updelay=10"
TYPE=Bond
BONDING_MASTER=yes
HWADDR=