2009-05-01 19:41:40 +05:30
|
|
|
# Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
|
2011-06-30 05:16:31 +05:30
|
|
|
# Released under the 2-clause BSD license.
|
2007-11-14 20:52:04 +05:30
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
bridge_depend()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
before interface macnet
|
2012-01-09 05:54:03 +05:30
|
|
|
program brctl
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
_config_vars="$_config_vars bridge bridge_add brctl"
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_is_bridge()
|
|
|
|
{
|
2010-12-12 01:56:31 +05:30
|
|
|
[ -d /sys/class/net/"${1:-${IFACE}}"/bridge ]
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
_is_bridge_port()
|
|
|
|
{
|
|
|
|
[ -d /sys/class/net/"${1:-${IFACE}}"/brport ]
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
_bridge_ports()
|
|
|
|
{
|
|
|
|
for x in /sys/class/net/"${1:-${IFACE}}"/brif/*; do
|
|
|
|
n=${x##*/}
|
|
|
|
echo $n
|
|
|
|
done
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
bridge_pre_start()
|
|
|
|
{
|
2010-12-12 01:56:31 +05:30
|
|
|
local brif= oiface="${IFACE}" e= x=
|
2010-12-12 06:13:05 +05:30
|
|
|
# ports is for static add
|
2007-10-29 15:15:49 +05:30
|
|
|
local ports="$(_get_array "bridge_${IFVAR}")"
|
2010-12-12 06:13:05 +05:30
|
|
|
# old config options
|
2007-10-29 15:15:49 +05:30
|
|
|
local opts="$(_get_array "brctl_${IFVAR}")"
|
2010-12-12 01:56:31 +05:30
|
|
|
# brif is used for dynamic add
|
2007-04-05 16:48:42 +05:30
|
|
|
eval brif=\$bridge_add_${IFVAR}
|
2011-01-17 15:19:07 +05:30
|
|
|
|
2010-12-12 06:13:05 +05:30
|
|
|
# we need a way to if the bridge exists in a variable name, not just the
|
|
|
|
# contents of a variable. Eg if somebody has only bridge_add_eth0='br0',
|
|
|
|
# with no other lines mentioning br0.
|
2010-12-12 01:56:31 +05:30
|
|
|
eval bridge_unset=\${bridge_${IFVAR}-y\}
|
|
|
|
eval brctl_unset=\${brctl_${IFVAR}-y\}
|
2011-01-17 15:19:07 +05:30
|
|
|
|
2011-02-05 18:54:11 +05:30
|
|
|
if [ -z "${brif}" -a "${brctl_unset}" = 'y' ]; then
|
|
|
|
if [ -z "${ports}" -a "${bridge_unset}" = "y" ]; then
|
2010-12-12 01:56:31 +05:30
|
|
|
#eerror "Misconfigured static bridge detected (see net.example)"
|
|
|
|
return 0
|
|
|
|
fi
|
2008-09-18 19:43:33 +05:30
|
|
|
fi
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2010-12-12 01:56:31 +05:30
|
|
|
# If the bridge was already up, we should clear it
|
|
|
|
[ "${bridge_unset}" != "y" ] && bridge_post_stop
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
(
|
2010-12-12 01:56:31 +05:30
|
|
|
# Normalize order of variables
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ -z "${ports}" -a -n "${brif}" ]; then
|
2010-12-12 01:56:31 +05:30
|
|
|
# Dynamic mode detected
|
2007-04-05 16:48:42 +05:30
|
|
|
ports="${IFACE}"
|
|
|
|
IFACE="${brif}"
|
2010-12-12 01:56:31 +05:30
|
|
|
IFVAR=$(shell_var "${IFACE}")
|
2007-04-05 16:48:42 +05:30
|
|
|
else
|
2010-12-12 01:56:31 +05:30
|
|
|
# Static mode detected
|
2007-04-05 16:48:42 +05:30
|
|
|
ports="${ports}"
|
|
|
|
metric=1000
|
|
|
|
fi
|
|
|
|
|
2010-12-12 01:56:31 +05:30
|
|
|
if ! _is_bridge ; then
|
2007-04-05 16:48:42 +05:30
|
|
|
ebegin "Creating bridge ${IFACE}"
|
2007-11-02 03:55:53 +05:30
|
|
|
if ! brctl addbr "${IFACE}"; then
|
2007-04-05 16:48:42 +05:30
|
|
|
eend 1
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2010-12-12 01:56:31 +05:30
|
|
|
# TODO: does this reset the bridge every time we add a interface to the
|
|
|
|
# bridge? We should probably NOT do that.
|
|
|
|
|
|
|
|
# Old configuration set mechanism
|
|
|
|
# Only a very limited subset of the options are available in the old
|
|
|
|
# configuration method. The sysfs interface is in the next block instead.
|
2007-10-30 17:08:23 +05:30
|
|
|
local IFS="$__IFS"
|
2007-10-29 15:15:49 +05:30
|
|
|
for x in ${opts}; do
|
|
|
|
unset IFS
|
2007-04-19 12:16:29 +05:30
|
|
|
set -- ${x}
|
|
|
|
x=$1
|
|
|
|
shift
|
|
|
|
set -- "${x}" "${IFACE}" "$@"
|
|
|
|
brctl "$@"
|
|
|
|
done
|
2007-10-30 17:08:23 +05:30
|
|
|
unset IFS
|
2007-04-19 12:16:29 +05:30
|
|
|
|
2010-12-12 01:56:31 +05:30
|
|
|
# New configuration set mechanism, matches bonding
|
|
|
|
for x in /sys/class/net/"${IFACE}"/bridge/*; do
|
|
|
|
[ -f "${x}" ] || continue
|
|
|
|
n=${x##*/}
|
|
|
|
eval s=\$${n}_${IFVAR}
|
|
|
|
if [ -n "${s}" ]; then
|
|
|
|
einfo "Setting ${n}: ${s}"
|
|
|
|
echo "${s}" >"${x}" || \
|
|
|
|
eerror "Failed to configure $n (${n}_${IFVAR})"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2007-11-02 03:55:53 +05:30
|
|
|
if [ -n "${ports}" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
einfo "Adding ports to ${IFACE}"
|
|
|
|
eindent
|
|
|
|
|
2010-12-12 01:56:31 +05:30
|
|
|
local BR_IFACE="${IFACE}"
|
2007-10-29 15:15:49 +05:30
|
|
|
for x in ${ports}; do
|
2007-04-05 16:48:42 +05:30
|
|
|
ebegin "${x}"
|
2007-11-02 03:55:53 +05:30
|
|
|
local IFACE="${x}"
|
2010-12-12 01:56:31 +05:30
|
|
|
local IFVAR=$(shell_var "${IFACE}")
|
|
|
|
if ! _exists "${IFACE}" ; then
|
|
|
|
eerror "Cannot add non-existent interface ${IFACE} to ${BR_IFACE}"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
# The interface is known to exist now
|
2007-11-02 03:55:53 +05:30
|
|
|
_set_flag promisc
|
|
|
|
_up
|
2010-12-12 01:56:31 +05:30
|
|
|
if ! brctl addif "${BR_IFACE}" "${x}"; then
|
2007-11-02 03:55:53 +05:30
|
|
|
_set_flag -promisc
|
2007-04-05 16:48:42 +05:30
|
|
|
eend 1
|
|
|
|
return 1
|
|
|
|
fi
|
2010-12-12 01:56:31 +05:30
|
|
|
# Per-interface bridge settings
|
|
|
|
for x in /sys/class/net/"${IFACE}"/brport/*; do
|
|
|
|
[ -f "${x}" ] || continue
|
|
|
|
n=${x##*/}
|
|
|
|
eval s=\$${n}_${IFVAR}
|
|
|
|
if [ -n "${s}" ]; then
|
|
|
|
einfo "Setting ${n}@${IFACE}: ${s}"
|
|
|
|
echo "${s}" >"${x}" || \
|
|
|
|
eerror "Failed to configure $n (${n}_${IFVAR})"
|
|
|
|
fi
|
|
|
|
done
|
2007-04-05 16:48:42 +05:30
|
|
|
eend 0
|
|
|
|
done
|
|
|
|
eoutdent
|
|
|
|
fi
|
2007-04-19 12:16:29 +05:30
|
|
|
) || return 1
|
2007-04-17 16:57:47 +05:30
|
|
|
|
|
|
|
# Bring up the bridge
|
|
|
|
_up
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
bridge_post_stop()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
local port= ports= delete=false extra=
|
|
|
|
|
2010-12-12 01:56:31 +05:30
|
|
|
if _is_bridge "${IFACE}"; then
|
2007-04-05 16:48:42 +05:30
|
|
|
ebegin "Destroying bridge ${IFACE}"
|
|
|
|
_down
|
2010-12-12 01:56:31 +05:30
|
|
|
for x in /sys/class/net/"${IFACE}"/brif/*; do
|
|
|
|
[ -s $x ] || continue
|
|
|
|
n=${x##*/}
|
|
|
|
ports="${ports} ${n}"
|
|
|
|
done
|
2007-04-05 16:48:42 +05:30
|
|
|
delete=true
|
|
|
|
iface=${IFACE}
|
|
|
|
eindent
|
|
|
|
else
|
2010-12-12 01:56:31 +05:30
|
|
|
# We are taking down an interface that is part of a bridge maybe
|
|
|
|
ports="${IFACE}"
|
|
|
|
local brport_dir="/sys/class/net/${IFACE}/brport"
|
|
|
|
[ -d ${brport_dir} ] || return 0
|
|
|
|
iface=$(readlink ${brport_dir}/bridge)
|
|
|
|
iface=${iface##*/}
|
2007-04-05 16:48:42 +05:30
|
|
|
[ -z "${iface}" ] && return 0
|
|
|
|
extra=" from ${iface}"
|
|
|
|
fi
|
|
|
|
|
2007-11-02 03:55:53 +05:30
|
|
|
for port in ${ports}; do
|
2007-04-05 16:48:42 +05:30
|
|
|
ebegin "Removing port ${port}${extra}"
|
2007-11-02 03:55:53 +05:30
|
|
|
local IFACE="${port}"
|
|
|
|
_set_flag -promisc
|
2007-04-05 16:48:42 +05:30
|
|
|
brctl delif "${iface}" "${port}"
|
|
|
|
eend $?
|
|
|
|
done
|
|
|
|
|
2007-11-02 03:55:53 +05:30
|
|
|
if ${delete}; then
|
2007-04-05 16:48:42 +05:30
|
|
|
eoutdent
|
|
|
|
brctl delbr "${iface}"
|
|
|
|
eend $?
|
|
|
|
fi
|
2011-01-17 15:19:07 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
return 0
|
|
|
|
}
|