diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index b3198f4c0b..f55a7dac03 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -3213,3 +3213,64 @@ nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_typ return FALSE; } } + +typedef struct { + const char *str; + const char *num; +} BondMode; + +static 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: + * @mode: bonding mode as a numeric value + * + * Convert bonding mode from integer value to descriptive name. + * See https://www.kernel.org/doc/Documentation/networking/bonding.txt for + * available modes. + * + * Returns: bonding mode string, or NULL on error +*/ + +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; +} + +/** + * nm_utils_bond_mode_string_to_int: + * @mode: bonding mode as string + * + * Convert bonding mode from string representation to numeric value. + * See https://www.kernel.org/doc/Documentation/networking/bonding.txt for + * available modes. + * The @mode string can be either a descriptive name or a number (as string). + * + * Returns: numeric bond mode, or -1 on error +*/ +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 ( strcmp (mode, bond_mode_table[i].str) == 0 + || strcmp (mode, bond_mode_table[i].num) == 0) + return i; + } + return -1; +} diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index 8c22cfd168..7393040696 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -15,7 +15,7 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2005 - 2013 Red Hat, Inc. + * Copyright 2005 - 2014 Red Hat, Inc. */ #ifndef __NM_UTILS_H__ @@ -185,6 +185,9 @@ gboolean nm_utils_ipaddr_valid (int family, const char *ip); gboolean nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_type); +int nm_utils_bond_mode_string_to_int (const char *mode); +const char *nm_utils_bond_mode_int_to_string (int mode); + G_END_DECLS #endif /* __NM_UTILS_H__ */ diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 82bcdb9f07..322d553560 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -754,6 +754,8 @@ global: nm_state_get_type; nm_utils_ap_mode_security_valid; nm_utils_bin2hexstr; + nm_utils_bond_mode_int_to_string; + nm_utils_bond_mode_string_to_int; nm_utils_check_virtual_device_compatibility; nm_utils_escape_ssid; nm_utils_file_is_certificate;