diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2012-03-08 18:15:43 +0000 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2012-03-08 18:20:25 +0000 |
commit | 0ca801493d28fc0f6a32edfbc5af8edb16987d5d (patch) | |
tree | e8236180464af9d5a39cc3f391b6f1e5e976bba0 /testing | |
parent | 87b35cc3a59287e2c1c540c6e263454992a27216 (diff) | |
download | aports-0ca801493d28fc0f6a32edfbc5af8edb16987d5d.tar.bz2 aports-0ca801493d28fc0f6a32edfbc5af8edb16987d5d.tar.xz |
testing/bonding: new aport
Scripts for network interface bonding
http://wiki.alpinelinux.org/wiki/Bonding
Diffstat (limited to 'testing')
-rw-r--r-- | testing/bonding/APKBUILD | 43 | ||||
-rwxr-xr-x | testing/bonding/bonding.post-down | 64 | ||||
-rwxr-xr-x | testing/bonding/bonding.pre-up | 135 | ||||
-rwxr-xr-x | testing/bonding/bonding.up | 45 |
4 files changed, 287 insertions, 0 deletions
diff --git a/testing/bonding/APKBUILD b/testing/bonding/APKBUILD new file mode 100644 index 000000000..055fc4285 --- /dev/null +++ b/testing/bonding/APKBUILD @@ -0,0 +1,43 @@ +# Contributor: Natanael Copa <ncopa@alpinelinux.org> +# Maintainer: Natanael Copa <ncopa@alpinelinux.org> + +# those scripts are based on debians ifenslave-2.6 scripts + +pkgname=bonding +pkgver=2.6 +pkgrel=0 +pkgdesc="Scripts for network interface bonding" +url="http://wiki.alpinelinux.org/wiki/Bonding" +arch="noarch" +license="GPL" +depends="" +makedepends="" +install="" +subpackages="" +source="bonding.pre-up bonding.up bonding.post-down" + +_builddir= +prepare() { + local i + cd "$_builddir" + for i in $source; do + case $i in + *.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;; + esac + done +} + +build() { + return 0 +} + +package() { + cd "$srcdir" + for i in pre-up up post-down; do + install -Dm755 bonding.$i "$pkgdir"/etc/network/if-$i.d/bonding + done +} + +md5sums="a3f404206bf13f770c4ab5f651f9d8b8 bonding.pre-up +68e64f483be3f8730a4c522f1e63e92d bonding.up +c5f6d07415118aa21d8e4730f9f18cab bonding.post-down" diff --git a/testing/bonding/bonding.post-down b/testing/bonding/bonding.post-down new file mode 100755 index 000000000..8dd4c07e9 --- /dev/null +++ b/testing/bonding/bonding.post-down @@ -0,0 +1,64 @@ +#!/bin/sh + +[ "$VERBOSITY" = 1 ] && set -x + +sysfs() +{ + # Called with : + # $1 = value to write. Won't write if $1 is empty. + # $2 = basename of the file in bonding/ to write to. + if [ "$1" ] ; then + echo "$1" > "/sys/class/net/$IFACE/master/bonding/$2" + return $? + fi + return 0 +} + +sysfs_remove_all() +{ + # Called with: + # $1 = target filename + read values < "/sys/class/net/$IFACE/bonding/$1" + for value in $values ; do + echo "-$value" > "/sys/class/net/$IFACE/bonding/$1" + done +} + +BOND_PARAMS="/sys/class/net/$IFACE/bonding" +IFSTATE=/var/run/ifstate + +# free $IFACE if it is currently enslaved to a bonding device. +if [ -f "/sys/class/net/$IFACE/master/bonding/slaves" ] ; then + echo "-$IFACE" > "/sys/class/net/$IFACE/master/bonding/slaves" + + # The first slave in bond-primary found in current slaves becomes the primary. + # If no slave in bond-primary is found, then primary does not change and might be undefined if just removed. + for slave in $IF_BOND_PRIMARY ; do + if grep -sq "\\<$slave\\>" "/sys/class/net/$IFACE/master/bonding/slaves" ; then + sysfs "$slave" primary + break + fi + done +fi + +# If $IFACE is not a master, exit. +[ ! -f "$BOND_PARAMS/slaves" ] && exit + +# Unset multivalue sysfs entries, so that re-enabling the interface later won't cause error. + +sysfs_remove_all arp_ip_target + +# Remove any slaves of $IFACE. + +[ "$VERBOSITY" = 1 ] && v=-v +read slaves < "$BOND_PARAMS/slaves" +for slave in $slaves ; do + # If $slave is currently up in $IFSTATE, then bring it down, to keep $IFSTATE consistent. + # This is supposed to have the side effect of freeing the interface. + grep -q "^$slave=" $IFSTATE && ifdown $v $slave + + # Anyway, ensure $slave is free. + if [ -f "/sys/class/net/$slave/master/bonding/slaves" ] ; then + echo "-$slave" > "$BOND_PARAMS/slaves" 2> /dev/null + fi +done diff --git a/testing/bonding/bonding.pre-up b/testing/bonding/bonding.pre-up new file mode 100755 index 000000000..f93fadde8 --- /dev/null +++ b/testing/bonding/bonding.pre-up @@ -0,0 +1,135 @@ +#!/bin/sh + +[ "$VERBOSITY" = 1 ] && set -x + +IFSTATE=/var/run/ifstate + +add_master() +{ + # Return if $BOND_MASTER is already a bonding interface. + [ -f "/sys/class/net/$BOND_MASTER/bonding/slaves" ] && return + + # If the bonding module is not yet loaded, load it. + if [ ! -r /sys/class/net/bonding_masters ]; then + modprobe -q bonding + fi + + # Create the master interface. + if ! grep -sq "\\<$BOND_MASTER\\>" /sys/class/net/bonding_masters; then + echo "+$BOND_MASTER" > /sys/class/net/bonding_masters + fi +} + +sysfs() +{ + # Called with : + # $1 = value to write. Won't write if $1 is empty. + # $2 = basename of the file in bonding/ to write to. + if [ "$1" ] ; then + echo "$1" > "/sys/class/net/$BOND_MASTER/bonding/$2" + return $? + fi + return 0 +} + +sysfs_add() +{ + # Called with : + # $1 = values to write. + # $2 = target filename. + for value in $1; do + # Do not add $1 to $2 if already present. + if ! grep -sq "\\<$value\\>" /sys/class/net/$BOND_MASTER/bonding/$2 + then + sysfs "+$value" "$2" + fi + done +} + +enslave_slaves() +{ + case "$BOND_SLAVES" in + none) + BOND_SLAVES="" + ;; + all) + BOND_SLAVES=`sed -ne 's/ *\(eth[^:]*\):.*/\1/p' /proc/net/dev` + AUTOIF="yes" + ;; + esac + + [ "$VERBOSITY" = 1 ] && v=-v + for slave in $BOND_SLAVES ; do + if ( [ "$AUTOIF" ] && grep -q "^$slave=" $IFSTATE ) ; then + echo "Not enslaving interface $slave since it is already configured" + else + # Ensure $slave is down. + ip link set "$slave" down 2>/dev/null + if ! sysfs_add "$slave" slaves 2>/dev/null ; then + echo "Failed to enslave $slave to $BOND_MASTER. Is $BOND_MASTER ready and a bonding interface ?" >&2 + else + # Bring up slave if it is the target of an allow-bondX stanza. + # This is usefull to bring up slaves that need extra setup. + if ifup -n "$slave" >/dev/null 2>&1; then + ifup $v "$slave" + fi + fi + fi + done +} + +setup_master() +{ + sysfs "$IF_BOND_MODE" mode + sysfs "$IF_BOND_MIIMON" miimon + sysfs "$IF_BOND_USE_CARRIER" use_carrier + sysfs "$IF_BOND_UPDELAY" updelay + sysfs "$IF_BOND_DOWNDELAY" downdelay + sysfs "$IF_BOND_ARP_INTERVAL" arp_interval + sysfs "$IF_BOND_ARP_VALIDATE" arp_validate + sysfs "$IF_BOND_FAIL_OVER_MAC" fail_over_mac + sysfs "$IF_BOND_XMIT_HASH_POLICY" xmit_hash_policy + sysfs "$IF_BOND_LACP_RATE" lacp_rate + sysfs_add "$IF_BOND_ARP_IP_TARGET" arp_ip_target +} + +setup_slaves() +{ + # The first slave in bond-primary found in current slaves becomes the primary. + # If no slave in bond-primary is found, then primary does not change. + for slave in $IF_BOND_PRIMARY ; do + if grep -sq "\\<$slave\\>" "/sys/class/net/$BOND_MASTER/bonding/slaves" ; then + sysfs "$slave" primary + break + fi + done + + if [ "$IF_BOND_ACTIVE_SLAVE" ] ; then + # Need to force interface up before. Bonding will refuse to activate a down interface. + ip link set "$IF_BOND_ACTIVE_SLAVE" up + sysfs "$IF_BOND_ACTIVE_SLAVE" active_slave + fi +} + +# Are there anything to do ? + +# Option slaves deprecated, replaced by bond-slaves, but still supported for backward compatibility. +IF_BOND_SLAVES=${IF_BOND_SLAVES:-$IF_SLAVES} + +if [ "$IF_BOND_MASTER" ] ; then + BOND_MASTER="$IF_BOND_MASTER" + BOND_SLAVES="$IFACE" +else + if [ "$IF_BOND_SLAVES" ] ; then + BOND_MASTER="$IFACE" + BOND_SLAVES="$IF_BOND_SLAVES" + fi +fi + +# Exit if nothing to do... +[ -z "$BOND_MASTER$BOND_SLAVES" ] && exit + +add_master +setup_master +enslave_slaves +setup_slaves diff --git a/testing/bonding/bonding.up b/testing/bonding/bonding.up new file mode 100755 index 000000000..7f27421cd --- /dev/null +++ b/testing/bonding/bonding.up @@ -0,0 +1,45 @@ +#!/bin/sh + +[ "$VERBOSITY" = 1 ] && set -x + +sysfs() +{ + # Called with : + # $1 = value to write. Won't write if $1 is empty. + # $2 = basename of the file in bonding/ to write to. + if [ "$1" ] ; then + echo "$1" > "/sys/class/net/$IFACE/master/bonding/$2" + return $? + fi + return 0 +} + +# If the stanza bond-give-a-chance is set for a slave interface, +# then force $IFACE to be the primary for some time, then restore primary to it previous value. + +# This stanza is designed to workaround a bug in wpa_supplicant, when used with bonding : + +# wpa_supplicant expect wifi authentication packets on the bond interface, but also send wifi authentication packets on the bond interface. +# If the active interface is not the wifi interface at the time wpa_supplicant try to authenticate, the wifi AP won't receive anything, causing the authentication to fail. + +# In order for the wifi authentication to succeed, one need to give a chance to the wifi interface to send authentication packets. +# "bond-give-a-chance 10" will set the wifi interface as the primary interface for 10 seconds, then restore the previous primary interface. +# This is supposed to be enought to give a chance to wifi to authenticate properly. + +if [ "$IF_BOND_GIVE_A_CHANCE" ] ; then + read primary < "/sys/class/net/$IFACE/master/bonding/primary" + # Set the temporary primary. + sysfs "$IFACE" primary + + # Wait for the link to be setup, but not longer that $IF_BOND_GIVE_A_CHANGE seconds. + while [ "$IF_BOND_GIVE_A_CHANCE" -gt 0 ] ; do + if ip link show $IFACE | grep -sq 'state UP'; then + break + fi + sleep 1 + IF_BOND_GIVE_A_CHANCE=`expr $IF_BOND_GIVE_A_CHANCE - 1` + done + + # Restore the previous primary. + sysfs "$primary" primary +fi |