From: Kurt Garloff Subject: Re: Re: network-bridge script reworked Date: Wed, 19 Oct 2005 14:22:01 +0200 Message-ID: <20051019122201.GF11567@tpkurt.pmc.nue.novell.com> References: <20051019093502.GP8705@tpkurt.tagung.uni-hamburg.de> <43562AC6.3030006@spamcop.net> <20051019115840.GD26910@leeni.uk.xensource.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1380497279==" Return-path: In-Reply-To: <20051019115840.GD26910@leeni.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Mime-version: 1.0 Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Ewan Mellor Cc: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org --===============1380497279== Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="6BvahUXLYAruDZOj" Content-Disposition: inline --6BvahUXLYAruDZOj Content-Type: multipart/mixed; boundary="aZoGpuMECXJckB41" Content-Disposition: inline --aZoGpuMECXJckB41 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi Ewan, On Wed, Oct 19, 2005 at 12:58:40PM +0100, Ewan Mellor wrote: > Just waiting for that script, Kurt ;-) Ouch it was measnt to be attached to the original message. Sorry. Best, --=20 Kurt Garloff, Director SUSE Labs, Novell Inc. --aZoGpuMECXJckB41 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=network-bridge Content-Transfer-Encoding: quoted-printable #!/bin/sh #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D # Default Xen network start/stop script. # Xend calls a network script when it starts. # The script name to use is defined in /etc/xen/xend-config.sxp # in the network-script field. # # This script creates a bridge (default xenbr0), adds a device # (default eth0) to it, copies the IP addresses from the device # to the bridge and adjusts the routes accordingly. # # If all goes well, this should ensure that networking stays up. # However, some configurations are upset by this, especially # NFS roots. If the bridged setup does not meet your needs, # configure a different script, for example using routing instead. # # Usage: # # network (start|stop|status) {VAR=3DVAL}* # # Vars: # # bridge The bridge to use (default xenbr0). # netdev The interface to add to the bridge (default eth0). # antispoof Whether to use iptables to prevent spoofing (default yes). # # start: # Creates the bridge and enslaves netdev to it. # Copies the IP addresses from netdev to the bridge. # Deletes the routes to netdev and adds them on bridge. # # stop: # Removes netdev from the bridge. # Deletes the routes to bridge and adds them to netdev. # # status: # Print ifconfig for netdev and bridge. # Print routes. # #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D # Gentoo doesn't have ifup/ifdown: define appropriate alternatives which ifup >& /dev/null if [ "$?" !=3D 0 -a -e /etc/conf.d/net ]; then ifup() { /etc/init.d/net.$1 start } ifdown() { /etc/init.d/net.$1 stop } fi # Exit if anything goes wrong. set -e=20 # First arg is the operation. OP=3D$1 shift # Pull variables in args in to environment. for arg ; do export "${arg}" ; done bridge=3D${bridge:-xenbr0} netdev=3D${netdev:-eth0} antispoof=3D${antispoof:-no} echo "*network $OP bridge=3D$bridge netdev=3D$netdev antispoof=3D$antispoof= " >&2 legacy_mask_to_prefix() { mask=3D$1 first=3D${mask%%.*} second=3D${mask#*.} third=3D${second#*.} fourth=3D${third#*.} second=3D${second%%.*} third=3D${third%%.*} declare -i INT FULLMASK BIT INT=3D$((((($first*256)+$second)*256+$third)*256+$fourth)) FULLMASK=3D4294967295 BIT=3D1 for bit in `seq 32 -1 0`; do if test $FULLMASK -eq $INT; then PREFIX=3D$bit; return; fi FULLMASK=3D$(($FULLMASK-$BIT)) BIT=3D$((BIT*2)) done echo "ERROR converting netmask $mask to prefix" exit 1 } # Usage: transfer_addrs src dst # Copy all IP addresses (including aliases) from device $src to device $dst. transfer_addrs () { local src=3D$1 local dst=3D$2 # Don't bother if $dst already has IP addresses. if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then return fi # Address lines start with 'inet' and have the device in them. # Replace 'inet' with 'ip addr add' and change the device name $src # to 'dev $src'. ip addr show dev ${src} | egrep '^ *inet ' | sed -e " s/inet/ip addr add/ s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@ s/${src}/dev ${dst}/ " | sh -e # Remove automatic routes on destionation device ip route list | sed -ne " /dev ${dst}\( \|$\)/ { s/^/ip route del / p }" | sh -e } # Usage: del_addrs src del_addrs () { local src=3D$1 ip addr show dev ${src} | egrep '^ *inet ' | sed -e " s/inet/ip addr del/ s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@ s/${src}/dev ${src}/ " | sh -e ip link set dev ${dst} up } # Usage: transfer_routes src dst # Get all IP routes to device $src, delete them, and # add the same routes to device $dst. # The original routes have to be deleted, otherwise adding them # for $dst fails (duplicate routes). transfer_routes () { local src=3D$1 local dst=3D$2 # List all routes and grep the ones with $src in. # Stick 'ip route del' on the front to delete. # Change $src to $dst and use 'ip route add' to add. ip route list | sed -ne " /dev ${src}\( \|$\)/ { h s/^/ip route del / P g s/${src}/${dst}/ s/^/ip route add / P d }" | sh -e } # Usage: create_bridge bridge create_bridge () { local bridge=3D$1 # Don't create the bridge if it already exists. if ! brctl show | grep -q ${bridge} ; then brctl addbr ${bridge} brctl stp ${bridge} off brctl setfd ${bridge} 0 fi ip link set ${bridge} up } # Usage: add_to_bridge bridge dev add_to_bridge () { local bridge=3D$1 local dev=3D$2 # Don't add $dev to $bridge if it's already on a bridge. if ! brctl show | grep -q ${dev} ; then brctl addif ${bridge} ${dev} fi } # Usage: antispoofing dev bridge # Set the default forwarding policy for $dev to drop. # Allow forwarding to the bridge. antispoofing () { local dev=3D$1 local bridge=3D$2 iptables -P FORWARD DROP iptables -A FORWARD -m physdev --physdev-in ${dev} -j ACCEPT } # Usage: show_status dev bridge # Print ifconfig and routes. show_status () { local dev=3D$1 local bridge=3D$2 =20 echo '=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D' ip addr show ${dev} ip addr show ${bridge} echo ' ' brctl show ${bridge} echo ' ' ip route list echo ' ' route -n echo '=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D' } op_start () { if [ "${bridge}" =3D=3D "null" ] ; then return fi create_bridge ${bridge} if ! ip link show 2>/dev/null | grep -q "^[0-9]*: veth0"; then return fi if ip link show veth0 2>/dev/null >/dev/null; then mac=3D`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(.= =2E:..:..:..:..:..\).*/\1/'` eval `/sbin/getcfg -d /etc/sysconfig/network/ -f ifcfg- -- ${netdev}` transfer_addrs ${netdev} veth0 if ! ifdown ${netdev}; then # if ifup didn't work, see if we have an ip=3D on cmd line if egrep 'ip=3D[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline; then kip=3D`sed -e 's!.*ip=3D\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /p= roc/cmdline` kmask=3D`sed -e 's!.*ip=3D[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdli= ne`=20 kgate=3D`sed -e 's!.*ip=3D[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` ip link set ${netdev} down ip addr flush ${netdev} fi fi ip link set ${netdev} name p${netdev} ip link set veth0 name ${netdev} ip link set p${netdev} down arp off ip link set p${netdev} addr fe:ff:ff:ff:ff:ff ip addr flush p${netdev} ip link set ${netdev} addr ${mac} arp on add_to_bridge ${bridge} vif0.0 add_to_bridge ${bridge} p${netdev} ip link set ${bridge} up ip link set vif0.0 up ip link set p${netdev} up=20 if ! ifup ${HWD_CONFIG_0} ${netdev} ; then if [ ${kip} ] ; then # use the addresses we grocked from /proc/cmdline if [ -z "${kmask}" ]; then=20 PREFIX=3D32;=20 else=20 legacy_to_prefix ${kmask}; fi ip addr add ${kip}/${PREFIX} dev ${netdev} ip link set dev ${netdev} up [ ${kgate} ] && ip route add default via ${kgate} fi fi else # old style without veth0 transfer_addrs ${netdev} ${bridge} transfer_routes ${netdev} ${bridge} fi if [ ${antispoof} =3D=3D 'yes' ] ; then antispoofing ${netdev} ${bridge} fi } op_stop () { if [ "${bridge}" =3D=3D "null" ]; then return fi if ! ip link show ${bridge} >/dev/null 2>&1; then return fi if ip link show p${netdev} 2>/dev/null >/dev/null; then ip link set dev vif0.0 down mac=3D`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(.= =2E:..:..:..:..:..\).*/\1/'` transfer_addrs ${netdev} p${netdev} ifdown ${netdev} ip link set ${netdev} down arp off ip link set ${netdev} addr fe:ff:ff:ff:ff:ff ip link set p${netdev} down ip addr flush ${netdev} ip link set p${netdev} addr ${mac} arp on brctl delif ${bridge} p${netdev} brctl delif ${bridge} vif0.0 ip link set ${bridge} down ip link set ${netdev} name veth0 ip link set p${netdev} name ${netdev} ifup ${netdev} else transfer_routes ${bridge} ${netdev} ip link set ${bridge} down fi brctl delbr ${bridge} } case ${OP} in start) op_start ;; =20 stop) op_stop ;; status) show_status ${netdev} ${bridge} ;; *) echo 'Unknown command: ' ${OP} >&2 echo 'Valid commands are: start, stop, status' >&2 exit 1 esac --aZoGpuMECXJckB41-- --6BvahUXLYAruDZOj Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) iD8DBQFDVjppxmLh6hyYd04RAjNwAJ4pdwqwXKtC1MyZopEU0VYgJZru3wCgxIBT Ls7r65kGflVcW4Qwe69ZLq0= =9NUz -----END PGP SIGNATURE----- --6BvahUXLYAruDZOj-- --===============1380497279== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --===============1380497279==--