[PATCH] bonding: Apply bonding settings when setting up bonding device



Adds a new function nm_system_apply_bonding_config() which applies
the parameters specified in the NMSettingBond object via sysfs.

Calls that function after creating/updating the bonding master
device.

If a parameter is not specified in the ifcfg the parameter will be
re-initialized to the default value. This may overwrite changes
which have been done manually via sysfs but it is the only reliable
way of setting up the bond.

Supported parameters for now:
 - mode (default: balance-rr)
 - miimon (default: 100)
 - updelay (default: 0)
 - downdelay (default: 0)
 - arp_interval (default: 0)
 - arp_ip_target (default: none)

Thomas Graf <tgraf redhat com>
---
 src/nm-system.c |   78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/nm-system.h |    1 +
 2 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/src/nm-system.c b/src/nm-system.c
index 53cd4f0..e075552 100644
--- a/src/nm-system.c
+++ b/src/nm-system.c
@@ -1185,6 +1185,82 @@ nm_system_device_set_priority (int ifindex,
 	}
 }
 
+static gboolean
+nm_system_set_bonding_attribute (const char *iface, const char *attr,
+				 const char *value)
+{
+	char file[FILENAME_MAX];
+	gboolean ret;
+
+	snprintf (file, sizeof(file), "/sys/class/net/%s/bonding/%s",
+		  iface, attr);
+
+	ret = nm_utils_do_sysctl (file, value);
+	if (!ret)
+		nm_log_warn (LOGD_HW, "(%s): failed to set bonding attribute "
+			     "'%s' to '%s'", iface, attr, value);
+
+	return ret;
+}
+
+static gboolean
+nm_system_set_bonding_attribute_int (const char *iface, const char *attr,
+				     guint32 value)
+{
+	char buf[128];
+
+	snprintf (buf, sizeof(buf), "%u", value);
+
+	return nm_system_set_bonding_attribute (iface, attr, buf);
+}
+
+gboolean
+nm_system_apply_bonding_config (NMSettingBond *s_bond)
+{
+	const char *name, *val;
+
+	name = nm_setting_bond_get_interface_name (s_bond);
+	g_assert (name); 
+
+	if ((val = nm_setting_bond_get_mode (s_bond)))
+		nm_system_set_bonding_attribute (name, "mode", val);
+
+	/*
+	 * FIXME:
+	 *
+	 * ifup-eth contains code to append targets if the value is prefixed
+	 * with '+':
+	 *
+	 *  if [ "${key}" = "arp_ip_target" -a "${value:0:1}" != "+" ]; then
+	 *  OLDIFS=$IFS;
+	 *  IFS=',';
+	 *  for arp_ip in $value; do
+	 *      if ! grep -q $arp_ip /sys/class/net/${DEVICE}/bonding/$key; then
+	 *          echo +$arp_ip > /sys/class/net/${DEVICE}/bonding/$key
+	 *      fi
+	 *  done
+	 *
+	 * Not sure if this is actually being used and it seems dangerous as
+	 * the result is pretty much unforeseeable.
+	 */
+	if ((val = nm_setting_bond_get_arp_ip_target (s_bond)))
+		nm_system_set_bonding_attribute (name, "arp_ip_target", val);
+
+	nm_system_set_bonding_attribute_int (name, "miimon",
+			nm_setting_bond_get_miimon (s_bond));
+
+	nm_system_set_bonding_attribute_int (name, "downdelay",
+			nm_setting_bond_get_downdelay (s_bond));
+
+	nm_system_set_bonding_attribute_int (name, "updelay",
+			nm_setting_bond_get_updelay (s_bond));
+
+	nm_system_set_bonding_attribute_int (name, "arp_interval",
+			nm_setting_bond_get_arp_interval (s_bond));
+
+	return TRUE;
+}
+
 /**
  * nm_system_add_bonding_master:
  * @setting: bonding setting
@@ -1213,6 +1289,8 @@ nm_system_add_bonding_master(NMSettingBond *setting)
 		return FALSE;
 	}
 
+	nm_system_apply_bonding_config (setting);
+
 	return TRUE;
 }
 
diff --git a/src/nm-system.h b/src/nm-system.h
index 339dfa7..7839a97 100644
--- a/src/nm-system.h
+++ b/src/nm-system.h
@@ -91,6 +91,7 @@ gboolean		nm_system_iface_set_mtu                 (int ifindex, guint32 mtu);
 
 gboolean		nm_system_iface_set_mac                 (int ifindex, const struct ether_addr *mac);
 
+gboolean		nm_system_apply_bonding_config	(NMSettingBond *s_bond);
 gboolean		nm_system_add_bonding_master	(NMSettingBond *setting);
 gboolean		nm_system_iface_enslave		(NMDevice *slave, NMDevice *master);
 gboolean		nm_system_iface_release		(NMDevice *slave, NMDevice *master);
-- 
1.7.6.4



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]