From 9e341fbb562a4686c26f3e97b1b82b3c4ca987f2 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Tue, 12 Nov 2019 22:46:18 +0000 Subject: Initial support for bonding (link aggrigation) in rc.inet1. The bonding code introduces three new variables for use in rc.inet1.conf: BONDNICS[x]="" BONDMODE[x]="" BONDOPTS[x]="" The BONDNICS variable takes a list of interfaces which should be slaved to the bond. BONDMODE sets the mode of the bond. Useful options are 'balance-rr', 'active-backup' and '802.3ad'. A full list of options can be found in /usr/src/linux/Documentation/networking/bonding.txt and README.bonding. BONDOPTS are a pipe (|) separated list of options to apply to the bond after the interfaces have been added. Useful options are 'primary' (which is required when BONDMODE is 'active-backup', 'xmit_hash_policy' (which can be used with 'balance-rr' to choose the hash policy, 'lacp_rate' (which should be used with '802.3ad' mode), and 'miimon' (which should be used with all modes) See the kernel source documentation or README.bonding for more info. --- rc.inet1 | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 7 deletions(-) (limited to 'rc.inet1') diff --git a/rc.inet1 b/rc.inet1 index d8cbdfd..c19571f 100644 --- a/rc.inet1 +++ b/rc.inet1 @@ -175,6 +175,50 @@ br_close() { /sbin/ip link del ${IFNAME[$1]} } +# Function to create a bond. +bond_create() { + # Argument is 'i' - the position of this interface in the IFNAME array. + info_log "${IFNAME[$1]}: creating bond" + debug_log "/sbin/ip link add name ${IFNAME[$1]} type bond" + /sbin/ip link add name ${IFNAME[$1]} type bond + debug_log "/sbin/ip link set dev ${IFNAME[$1]} type bond mode ${BONDMODE[$1]}" + /sbin/ip link set dev ${IFNAME[$1]} type bond mode ${BONDMODE[$1]} + for BONDIF in ${BONDNICS[$1]}; do + debug_log "/sbin/ip link set dev $BONDIF down" + /sbin/ip link set dev $BONDIF down + debug_log "/sbin/ip address flush dev $BONDIF" + /sbin/ip address flush dev $BONDIF + debug_log "/sbin/ip link set $BONDIF master ${IFNAME[$1]}" + /sbin/ip link set $BONDIF master ${IFNAME[$1]} + debug_log "/sbin/ip link set dev $BONDIF up" + /sbin/ip link set dev $BONDIF up + done + while read -r -d \| BONDOPT; do + if [ -n "$BONDOPT" ]; then + debug_log "/sbin/ip link set dev ${IFNAME[$1]} type bond $BONDOPT" + /sbin/ip link set dev ${IFNAME[$1]} type bond $BONDOPT + fi + done <<<"${BONDOPTS[$1]}|" # The | on the end is required. +} + +# Function to destroy a bond. +bond_destroy() { + # Argument is 'i' - the position of this interface in the IFNAME array. + info_log "${IFNAME[$1]}: destroying bond" + debug_log "/sbin/ip link set dev ${IFNAME[$1]} down" + /sbin/ip link set dev ${IFNAME[$1]} down + debug_log "/sbin/ip address flush dev ${IFNAME[$1]}" + /sbin/ip address flush dev ${IFNAME[$1]} + for BONDIF in ${BONDNICS[$1]}; do + debug_log "/sbin/ip link set $BONDIF nomaster" + /sbin/ip link set $BONDIF nomaster + debug_log "/sbin/ip link set dev $BONDIF down" + /sbin/ip link set dev $BONDIF down + done + debug_log "/sbin/ip link del name ${IFNAME[$1]} type bond" + /sbin/ip link del name ${IFNAME[$1]} type bond +} + # Function to bring up a network interface. If the interface is # already up or does not yet exist (perhaps because the kernel driver # is not loaded yet), do nothing. @@ -194,10 +238,12 @@ if_up() { return fi info_log "${1}: configuring interface" - # If the interface is a bridge, then create it first. - # If you need to set hardware addresses for the underlying interfaces, - # configure the interfaces with IPs of 0.0.0.0 and set the MAC address - # in the usual way. Then, finally, define the bridge. + # If you need to set hardware addresses for the underlying interfaces in a + # bond or bridge, configure the interfaces with IPs of 0.0.0.0 and set the + # MAC address with HWADDR. Then, finally, define the bond or bridge. + # If the interface is a bond, create it. + [ -n "${BONDNICS[$i]}" ] && bond_create $i + # If the interface is a bridge, create it. [ -n "${BRNICS[$i]}" ] && br_open $i if [ -e /sys/class/net/${1%%[:.]*} ]; then # interface exists if ! /sbin/ip address show scope global dev ${1} 2>/dev/null | grep -Ewq '(inet|inet6)' || \ @@ -460,9 +506,9 @@ if_down() { cat /proc/sys/net/ipv6/conf/default/accept_ra >/proc/sys/net/ipv6/conf/$1/accept_ra fi # If the interface is a bridge, then destroy it now: - if [ -n "${BRNICS[$i]}" ]; then - br_close $i - fi + [ -n "${BRNICS[$i]}" ] && br_close $i + # If the interface is a bond, then destroy it now. + [ -n "${BONDNICS[$i]}" ] && bond_destroy $i # Take down VLAN interface, if configured. if echo "${1}" | grep -Fq .; then info_log "${1}: destroying VLAN interface" -- cgit v1.2.3