diff options
-rw-r--r-- | libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c | 58 | ||||
-rw-r--r-- | libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h | 3 | ||||
-rw-r--r-- | libnm-core/nm-utils.c | 30 |
3 files changed, 56 insertions, 35 deletions
diff --git a/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c b/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c index 65cd8ba5d7..1d24d8fee8 100644 --- a/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c +++ b/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c @@ -36,18 +36,19 @@ _nm_setting_bond_remove_options_arp_interval(NMSettingBond *s_bond) nm_setting_bond_remove_option(s_bond, NM_SETTING_BOND_OPTION_ARP_IP_TARGET); } +/*****************************************************************************/ + NM_UTILS_STRING_TABLE_LOOKUP_DEFINE( _nm_setting_bond_mode_from_string, NMBondMode, - { g_return_val_if_fail(name, NM_BOND_MODE_UNKNOWN); }, + { + G_STATIC_ASSERT_EXPR(_NM_BOND_MODE_NUM <= 9); + + if (name && name[0] < '0' + _NM_BOND_MODE_NUM && name[0] >= '0' && name[1] == '\0') { + return name[0] - '0'; + } + }, { return NM_BOND_MODE_UNKNOWN; }, - {"0", NM_BOND_MODE_ROUNDROBIN}, - {"1", NM_BOND_MODE_ACTIVEBACKUP}, - {"2", NM_BOND_MODE_XOR}, - {"3", NM_BOND_MODE_BROADCAST}, - {"4", NM_BOND_MODE_8023AD}, - {"5", NM_BOND_MODE_TLB}, - {"6", NM_BOND_MODE_ALB}, {"802.3ad", NM_BOND_MODE_8023AD}, {"active-backup", NM_BOND_MODE_ACTIVEBACKUP}, {"balance-alb", NM_BOND_MODE_ALB}, @@ -56,6 +57,47 @@ NM_UTILS_STRING_TABLE_LOOKUP_DEFINE( {"balance-xor", NM_BOND_MODE_XOR}, {"broadcast", NM_BOND_MODE_BROADCAST}, ); +const char * +_nm_setting_bond_mode_to_string(int mode) +{ + static const char *const modes[] = { + [NM_BOND_MODE_8023AD] = "802.3ad", + [NM_BOND_MODE_ACTIVEBACKUP] = "active-backup", + [NM_BOND_MODE_ALB] = "balance-alb", + [NM_BOND_MODE_BROADCAST] = "broadcast", + [NM_BOND_MODE_ROUNDROBIN] = "balance-rr", + [NM_BOND_MODE_TLB] = "balance-tlb", + [NM_BOND_MODE_XOR] = "balance-xor", + }; + + G_STATIC_ASSERT(G_N_ELEMENTS(modes) == _NM_BOND_MODE_NUM); + + if (NM_MORE_ASSERT_ONCE(5)) { + char sbuf[100]; + int i; + NMBondMode m; + + for (i = 0; i < (int) G_N_ELEMENTS(modes); i++) { + nm_assert(modes[i]); + nm_assert(i == _nm_setting_bond_mode_from_string(modes[i])); + nm_assert(i == _nm_setting_bond_mode_from_string(nm_sprintf_buf(sbuf, "%d", i))); + } + nm_assert(NM_BOND_MODE_UNKNOWN == _nm_setting_bond_mode_from_string(NULL)); + nm_assert(NM_BOND_MODE_UNKNOWN == _nm_setting_bond_mode_from_string("")); + for (i = -2; i < ((int) G_N_ELEMENTS(modes)) + 20; i++) { + if (i < 0 || i >= G_N_ELEMENTS(modes)) + m = NM_BOND_MODE_UNKNOWN; + else + m = i; + nm_assert(m == _nm_setting_bond_mode_from_string(nm_sprintf_buf(sbuf, "%d", i))); + } + } + + if (mode >= 0 && mode < (int) G_N_ELEMENTS(modes)) + return modes[mode]; + return NULL; +} + /*****************************************************************************/ gboolean diff --git a/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h b/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h index 5d672fedb3..025d684776 100644 --- a/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h +++ b/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h @@ -65,11 +65,14 @@ typedef enum { NM_BOND_MODE_8023AD = 4, NM_BOND_MODE_TLB = 5, NM_BOND_MODE_ALB = 6, + _NM_BOND_MODE_NUM, } NMBondMode; NMBondMode _nm_setting_bond_mode_from_string(const char *str); +const char *_nm_setting_bond_mode_to_string(int mode); + /*****************************************************************************/ static inline guint32 diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 3aa4b4f90a..cc39be25bd 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -4942,20 +4942,7 @@ nm_utils_check_virtual_device_compatibility(GType virtual_type, GType other_type } } -typedef struct { - const char *str; - const char *num; -} BondMode; - -static const BondMode bond_mode_table[] = { - [0] = {"balance-rr", "0"}, - [1] = {"active-backup", "1"}, - [2] = {"balance-xor", "2"}, - [3] = {"broadcast", "3"}, - [4] = {"802.3ad", "4"}, - [5] = {"balance-tlb", "5"}, - [6] = {"balance-alb", "6"}, -}; +/*****************************************************************************/ /** * nm_utils_bond_mode_int_to_string: @@ -4972,9 +4959,7 @@ static const BondMode bond_mode_table[] = { const char * nm_utils_bond_mode_int_to_string(int mode) { - if (mode >= 0 && mode < G_N_ELEMENTS(bond_mode_table)) - return bond_mode_table[mode].str; - return NULL; + return _nm_setting_bond_mode_to_string(mode); } /** @@ -4993,16 +4978,7 @@ nm_utils_bond_mode_int_to_string(int mode) int nm_utils_bond_mode_string_to_int(const char *mode) { - int i; - - if (!mode || !*mode) - return -1; - - for (i = 0; i < G_N_ELEMENTS(bond_mode_table); i++) { - if (NM_IN_STRSET(mode, bond_mode_table[i].str, bond_mode_table[i].num)) - return i; - } - return -1; + return _nm_setting_bond_mode_from_string(mode); } /*****************************************************************************/ |