direct-io.hg

changeset 6142:de0127d6e06b

New 'network-bridge' script that uses ifdown/ifup to configure the interface rather than trying to transfer addresses and routes. (Suggested by Gerd Knorr)

Although Li Ge has had problems with this script he had trouble with the previous too. I think this new one should work for more people, particularly those using DHCP on their primary dom0 interface.

Debugging is currently enabled ("sh -x") so a log of the script's execution is output to /var/log/xend.log

Signed-off-by: ian@xensource.com
author iap10@freefall.cl.cam.ac.uk
date Sat Aug 13 22:02:54 2005 +0000 (2005-08-13)
parents 050bf85fb870
children 48da5e506589 6f6ed4689795
files linux-2.6-xen-sparse/include/linux/highmem.h linux-2.6-xen-sparse/mm/highmem.c tools/examples/network-bridge tools/examples/xend-config.sxp
line diff
     3.1 --- a/tools/examples/network	Sat Aug 13 21:52:46 2005 +0000
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,246 +0,0 @@
     3.4 -#!/bin/sh
     3.5 -#============================================================================
     3.6 -# Default Xen network start/stop script.
     3.7 -# Xend calls a network script when it starts.
     3.8 -# The script name to use is defined in /etc/xen/xend-config.sxp
     3.9 -# in the network-script field.
    3.10 -#
    3.11 -# This script creates a bridge (default xen-br0), adds a device
    3.12 -# (default eth0) to it, copies the IP addresses from the device
    3.13 -# to the bridge and adjusts the routes accordingly.
    3.14 -#
    3.15 -# If all goes well, this should ensure that networking stays up.
    3.16 -# However, some configurations are upset by this, especially
    3.17 -# NFS roots. If the bridged setup does not meet your needs,
    3.18 -# configure a different script, for example using routing instead.
    3.19 -#
    3.20 -# Usage:
    3.21 -#
    3.22 -# network (start|stop|status) {VAR=VAL}*
    3.23 -#
    3.24 -# Vars:
    3.25 -#
    3.26 -# bridge     The bridge to use (default xen-br0).
    3.27 -# netdev     The interface to add to the bridge (default eth0).
    3.28 -# antispoof  Whether to use iptables to prevent spoofing (default yes).
    3.29 -#
    3.30 -# start:
    3.31 -# Creates the bridge and enslaves netdev to it.
    3.32 -# Copies the IP addresses from netdev to the bridge.
    3.33 -# Deletes the routes to netdev and adds them on bridge.
    3.34 -#
    3.35 -# stop:
    3.36 -# Removes netdev from the bridge.
    3.37 -# Deletes the routes to bridge and adds them to netdev.
    3.38 -#
    3.39 -# status:
    3.40 -# Print ifconfig for netdev and bridge.
    3.41 -# Print routes.
    3.42 -#
    3.43 -#============================================================================
    3.44 -
    3.45 -# Exit if anything goes wrong.
    3.46 -set -e 
    3.47 -
    3.48 -# First arg is the operation.
    3.49 -OP=$1
    3.50 -shift
    3.51 -
    3.52 -# Pull variables in args in to environment.
    3.53 -for arg ; do export "${arg}" ; done
    3.54 -
    3.55 -bridge=${bridge:-xen-br0}
    3.56 -netdev=${netdev:-eth0}
    3.57 -antispoof=${antispoof:-yes}
    3.58 -
    3.59 -echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof" >&2
    3.60 -
    3.61 -# Usage: transfer_addrs src dst
    3.62 -# Copy all IP addresses (including aliases) from device $src to device $dst.
    3.63 -transfer_addrs () {
    3.64 -    local src=$1
    3.65 -    local dst=$2
    3.66 -    # Don't bother if $dst already has IP addresses.
    3.67 -    if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
    3.68 -        return
    3.69 -    fi
    3.70 -    # Address lines start with 'inet' and have the device in them.
    3.71 -    # Replace 'inet' with 'ip addr add' and change the device name $src
    3.72 -    # to 'dev $src'.
    3.73 -    ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
    3.74 -s/inet/ip addr add/
    3.75 -s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
    3.76 -s/${src}/dev ${dst}/
    3.77 -" | sh -e
    3.78 -    # Remove automatic routes on destionation device
    3.79 -    ip route list | sed -ne "
    3.80 -/dev ${dst}\( \|$\)/ {
    3.81 -  s/^/ip route del /
    3.82 -  p
    3.83 -}" | sh -e
    3.84 -}
    3.85 -
    3.86 -# Usage: del_addrs src
    3.87 -del_addrs () {
    3.88 -    local src=$1
    3.89 -    ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
    3.90 -s/inet/ip addr del/
    3.91 -s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@
    3.92 -s/${src}/dev ${src}/
    3.93 -" | sh -e
    3.94 -}
    3.95 -
    3.96 -# Usage: transfer_routes src dst
    3.97 -# Get all IP routes to device $src, delete them, and
    3.98 -# add the same routes to device $dst.
    3.99 -# The original routes have to be deleted, otherwise adding them
   3.100 -# for $dst fails (duplicate routes).
   3.101 -transfer_routes () {
   3.102 -    local src=$1
   3.103 -    local dst=$2
   3.104 -    # List all routes and grep the ones with $src in.
   3.105 -    # Stick 'ip route del' on the front to delete.
   3.106 -    # Change $src to $dst and use 'ip route add' to add.
   3.107 -    ip route list | sed -ne "
   3.108 -/dev ${src}\( \|$\)/ {
   3.109 -  h
   3.110 -  s/^/ip route del /
   3.111 -  P
   3.112 -  g
   3.113 -  s/${src}/${dst}/
   3.114 -  s/^/ip route add /
   3.115 -  P
   3.116 -  d
   3.117 -}" | sh -e
   3.118 -}
   3.119 -
   3.120 -# Usage: create_bridge bridge
   3.121 -create_bridge () {
   3.122 -    local bridge=$1
   3.123 -
   3.124 -    # Don't create the bridge if it already exists.
   3.125 -    if ! brctl show | grep -q ${bridge} ; then
   3.126 -        brctl addbr ${bridge}
   3.127 -        brctl stp ${bridge} off
   3.128 -        brctl setfd ${bridge} 0
   3.129 -    fi
   3.130 -    ifconfig ${bridge} up
   3.131 -}
   3.132 -
   3.133 -# Usage: add_to_bridge bridge dev
   3.134 -add_to_bridge () {
   3.135 -    local bridge=$1
   3.136 -    local dev=$2
   3.137 -    # Don't add $dev to $bridge if it's already on a bridge.
   3.138 -    if ! brctl show | grep -q ${dev} ; then
   3.139 -        brctl addif ${bridge} ${dev}
   3.140 -    fi
   3.141 -}
   3.142 -
   3.143 -# Usage: antispoofing dev bridge
   3.144 -# Set the default forwarding policy for $dev to drop.
   3.145 -# Allow forwarding to the bridge.
   3.146 -antispoofing () {
   3.147 -    local dev=$1
   3.148 -    local bridge=$2
   3.149 -
   3.150 -    iptables -P FORWARD DROP
   3.151 -    iptables -A FORWARD -m physdev --physdev-in ${dev} -j ACCEPT
   3.152 -}
   3.153 -
   3.154 -# Usage: show_status dev bridge
   3.155 -# Print ifconfig and routes.
   3.156 -show_status () {
   3.157 -    local dev=$1
   3.158 -    local bridge=$2
   3.159 -    
   3.160 -    echo '============================================================'
   3.161 -    ifconfig ${dev}
   3.162 -    ifconfig ${bridge}
   3.163 -    echo ' '
   3.164 -    ip route list
   3.165 -    echo ' '
   3.166 -    route -n
   3.167 -    echo '============================================================'
   3.168 -}
   3.169 -
   3.170 -op_start () {
   3.171 -    if [ "${bridge}" == "null" ] ; then
   3.172 -        return
   3.173 -    fi
   3.174 -
   3.175 -    create_bridge ${bridge}
   3.176 -
   3.177 -    if ifconfig 2>/dev/null | grep -q veth0 ; then
   3.178 -        return
   3.179 -    fi
   3.180 -
   3.181 -    if ifconfig veth0 2>/dev/null | grep -q veth0 ; then
   3.182 -        # Propagate MAC address and ARP responsibilities to virtual interface.
   3.183 -        mac=`ifconfig ${netdev} | grep HWadd | sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
   3.184 -        ifconfig veth0 down
   3.185 -        ifconfig veth0 hw ether ${mac}
   3.186 -        ifconfig veth0 arp up
   3.187 -        transfer_addrs ${netdev} veth0
   3.188 -        transfer_routes ${netdev} veth0
   3.189 -        del_addrs ${netdev}
   3.190 -        ifconfig ${netdev} -arp down
   3.191 -        ifconfig ${netdev} hw ether fe:ff:ff:ff:ff:ff up
   3.192 -        # Bring up second half of virtual device and attach it to the bridge.
   3.193 -        ifconfig vif0.0 up
   3.194 -        add_to_bridge ${bridge} vif0.0
   3.195 -    else
   3.196 -        transfer_addrs ${netdev} ${bridge}
   3.197 -        transfer_routes ${netdev} ${bridge}
   3.198 -    fi
   3.199 -
   3.200 -    # Attach the real interface to the bridge.
   3.201 -    add_to_bridge ${bridge} ${netdev}
   3.202 -    
   3.203 -    if [ ${antispoof} == 'yes' ] ; then
   3.204 -        antispoofing ${netdev} ${bridge}
   3.205 -    fi
   3.206 -}
   3.207 -
   3.208 -op_stop () {
   3.209 -    if [ "${bridge}" == "null" ] ; then
   3.210 -        return
   3.211 -    fi
   3.212 -
   3.213 -    brctl delif ${bridge} ${netdev}
   3.214 -
   3.215 -    if ifconfig veth0 2>/dev/null | grep -q veth0 ; then
   3.216 -        brctl delif ${bridge} vif0.0
   3.217 -        ifconfig vif0.0 down
   3.218 -        mac=`ifconfig veth0 | grep HWadd | sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
   3.219 -        ifconfig ${netdev} down
   3.220 -        ifconfig ${netdev} hw ether ${mac}
   3.221 -        ifconfig ${netdev} arp up
   3.222 -        transfer_addrs veth0 ${netdev}
   3.223 -        transfer_routes veth0 ${netdev}
   3.224 -        del_addrs veth0
   3.225 -        ifconfig veth0 -arp down
   3.226 -        ifconfig veth0 hw ether 00:00:00:00:00:00
   3.227 -    else
   3.228 -        transfer_routes ${bridge} ${netdev}
   3.229 -    fi
   3.230 -}
   3.231 -
   3.232 -case ${OP} in
   3.233 -  start)
   3.234 -        op_start
   3.235 -        ;;
   3.236 -    
   3.237 -    stop)
   3.238 -        op_stop
   3.239 -        ;;
   3.240 -
   3.241 -    status)
   3.242 -        show_status ${netdev} ${bridge}
   3.243 -       ;;
   3.244 -
   3.245 -    *)
   3.246 -       echo 'Unknown command: ' ${OP} >&2
   3.247 -       echo 'Valid commands are: start, stop, status' >&2
   3.248 -       exit 1
   3.249 -esac
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/examples/network-bridge	Sat Aug 13 22:02:54 2005 +0000
     4.3 @@ -0,0 +1,260 @@
     4.4 +#!/bin/sh -x
     4.5 +#============================================================================
     4.6 +# Default Xen network start/stop script.
     4.7 +# Xend calls a network script when it starts.
     4.8 +# The script name to use is defined in /etc/xen/xend-config.sxp
     4.9 +# in the network-script field.
    4.10 +#
    4.11 +# This script creates a bridge (default xen-br0), adds a device
    4.12 +# (default eth0) to it, copies the IP addresses from the device
    4.13 +# to the bridge and adjusts the routes accordingly.
    4.14 +#
    4.15 +# If all goes well, this should ensure that networking stays up.
    4.16 +# However, some configurations are upset by this, especially
    4.17 +# NFS roots. If the bridged setup does not meet your needs,
    4.18 +# configure a different script, for example using routing instead.
    4.19 +#
    4.20 +# Usage:
    4.21 +#
    4.22 +# network (start|stop|status) {VAR=VAL}*
    4.23 +#
    4.24 +# Vars:
    4.25 +#
    4.26 +# bridge     The bridge to use (default xen-br0).
    4.27 +# netdev     The interface to add to the bridge (default eth0).
    4.28 +# antispoof  Whether to use iptables to prevent spoofing (default yes).
    4.29 +#
    4.30 +# start:
    4.31 +# Creates the bridge and enslaves netdev to it.
    4.32 +# Copies the IP addresses from netdev to the bridge.
    4.33 +# Deletes the routes to netdev and adds them on bridge.
    4.34 +#
    4.35 +# stop:
    4.36 +# Removes netdev from the bridge.
    4.37 +# Deletes the routes to bridge and adds them to netdev.
    4.38 +#
    4.39 +# status:
    4.40 +# Print ifconfig for netdev and bridge.
    4.41 +# Print routes.
    4.42 +#
    4.43 +#============================================================================
    4.44 +
    4.45 +# Exit if anything goes wrong.
    4.46 +set -e 
    4.47 +
    4.48 +# First arg is the operation.
    4.49 +OP=$1
    4.50 +shift
    4.51 +
    4.52 +# Pull variables in args in to environment.
    4.53 +for arg ; do export "${arg}" ; done
    4.54 +
    4.55 +bridge=${bridge:-xen-br0}
    4.56 +netdev=${netdev:-eth0}
    4.57 +antispoof=${antispoof:-yes}
    4.58 +
    4.59 +echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof" >&2
    4.60 +
    4.61 +# Usage: transfer_addrs src dst
    4.62 +# Copy all IP addresses (including aliases) from device $src to device $dst.
    4.63 +transfer_addrs () {
    4.64 +    local src=$1
    4.65 +    local dst=$2
    4.66 +    # Don't bother if $dst already has IP addresses.
    4.67 +    if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
    4.68 +        return
    4.69 +    fi
    4.70 +    # Address lines start with 'inet' and have the device in them.
    4.71 +    # Replace 'inet' with 'ip addr add' and change the device name $src
    4.72 +    # to 'dev $src'.
    4.73 +    ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
    4.74 +s/inet/ip addr add/
    4.75 +s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
    4.76 +s/${src}/dev ${dst}/
    4.77 +" | sh -e
    4.78 +    # Remove automatic routes on destionation device
    4.79 +    ip route list | sed -ne "
    4.80 +/dev ${dst}\( \|$\)/ {
    4.81 +  s/^/ip route del /
    4.82 +  p
    4.83 +}" | sh -e
    4.84 +}
    4.85 +
    4.86 +# Usage: del_addrs src
    4.87 +del_addrs () {
    4.88 +    local src=$1
    4.89 +    ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
    4.90 +s/inet/ip addr del/
    4.91 +s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@
    4.92 +s/${src}/dev ${src}/
    4.93 +" | sh -e
    4.94 +}
    4.95 +
    4.96 +# Usage: transfer_routes src dst
    4.97 +# Get all IP routes to device $src, delete them, and
    4.98 +# add the same routes to device $dst.
    4.99 +# The original routes have to be deleted, otherwise adding them
   4.100 +# for $dst fails (duplicate routes).
   4.101 +transfer_routes () {
   4.102 +    local src=$1
   4.103 +    local dst=$2
   4.104 +    # List all routes and grep the ones with $src in.
   4.105 +    # Stick 'ip route del' on the front to delete.
   4.106 +    # Change $src to $dst and use 'ip route add' to add.
   4.107 +    ip route list | sed -ne "
   4.108 +/dev ${src}\( \|$\)/ {
   4.109 +  h
   4.110 +  s/^/ip route del /
   4.111 +  P
   4.112 +  g
   4.113 +  s/${src}/${dst}/
   4.114 +  s/^/ip route add /
   4.115 +  P
   4.116 +  d
   4.117 +}" | sh -e
   4.118 +}
   4.119 +
   4.120 +# Usage: create_bridge bridge
   4.121 +create_bridge () {
   4.122 +    local bridge=$1
   4.123 +
   4.124 +    # Don't create the bridge if it already exists.
   4.125 +    if ! brctl show | grep -q ${bridge} ; then
   4.126 +        brctl addbr ${bridge}
   4.127 +        brctl stp ${bridge} off
   4.128 +        brctl setfd ${bridge} 0
   4.129 +    fi
   4.130 +    ifconfig ${bridge} up
   4.131 +}
   4.132 +
   4.133 +# Usage: add_to_bridge bridge dev
   4.134 +add_to_bridge () {
   4.135 +    local bridge=$1
   4.136 +    local dev=$2
   4.137 +    # Don't add $dev to $bridge if it's already on a bridge.
   4.138 +    if ! brctl show | grep -q ${dev} ; then
   4.139 +        brctl addif ${bridge} ${dev}
   4.140 +    fi
   4.141 +}
   4.142 +
   4.143 +# Usage: antispoofing dev bridge
   4.144 +# Set the default forwarding policy for $dev to drop.
   4.145 +# Allow forwarding to the bridge.
   4.146 +antispoofing () {
   4.147 +    local dev=$1
   4.148 +    local bridge=$2
   4.149 +
   4.150 +    iptables -P FORWARD DROP
   4.151 +    iptables -A FORWARD -m physdev --physdev-in ${dev} -j ACCEPT
   4.152 +}
   4.153 +
   4.154 +# Usage: show_status dev bridge
   4.155 +# Print ifconfig and routes.
   4.156 +show_status () {
   4.157 +    local dev=$1
   4.158 +    local bridge=$2
   4.159 +    
   4.160 +    echo '============================================================'
   4.161 +    ifconfig ${dev}
   4.162 +    ifconfig ${bridge}
   4.163 +    echo ' '
   4.164 +    ip route list
   4.165 +    echo ' '
   4.166 +    route -n
   4.167 +    echo '============================================================'
   4.168 +}
   4.169 +
   4.170 +op_start () {
   4.171 +    if [ "${bridge}" == "null" ] ; then
   4.172 +        return
   4.173 +    fi
   4.174 +
   4.175 +    create_bridge ${bridge}
   4.176 +
   4.177 +    if ifconfig 2>/dev/null | grep -q veth0 ; then
   4.178 +        return
   4.179 +    fi
   4.180 +
   4.181 +    if ifconfig veth0 2>/dev/null | grep -q veth0 ; then
   4.182 +	mac=`ifconfig ${netdev} | grep HWadd | sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
   4.183 +	if ! ifdown ${netdev} ; then
   4.184 +		# if ifup didn't work, see if we have an ip= on cmd line
   4.185 +		if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline ; 
   4.186 +		then
   4.187 +                        kip=`sed -e 's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
   4.188 +                        kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` 
   4.189 +                        kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline`
   4.190 +			ifconfig ${netdev} 0.0.0.0 down
   4.191 +		fi
   4.192 +	fi
   4.193 +	ip link set ${netdev} name p${netdev}
   4.194 +	ip link set veth0 name eth0
   4.195 +	ifconfig p${netdev} -arp down
   4.196 +	ifconfig p${netdev} hw ether fe:ff:ff:ff:ff:ff
   4.197 +	ifconfig ${netdev} hw ether ${mac}
   4.198 +	add_to_bridge ${bridge} vif0.0
   4.199 +	add_to_bridge ${bridge} p${netdev}
   4.200 +	ip link set vif0.0 up
   4.201 +	ip link set p${netdev} up
   4.202 +	if ! ifup ${netdev} ; then
   4.203 +		if [ ${kip} ] ; then
   4.204 +			# use the addresses we grocked from /proc/cmdline	
   4.205 +			ifconfig ${netdev} ${kip} 
   4.206 +			[ ${kmask} ] && ifconfig ${netdev} netmask ${kmask} 
   4.207 +			ifconfig ${netdev} up
   4.208 +			[ ${kgate} ] && ip route add default via ${kgate}	
   4.209 +		fi
   4.210 +        fi
   4.211 +    else
   4.212 +	# old style without veth0
   4.213 +	transfer_addrs ${netdev} ${bridge}
   4.214 +        transfer_routes ${netdev} ${bridge}
   4.215 +    fi
   4.216 +    
   4.217 +    if [ ${antispoof} == 'yes' ] ; then
   4.218 +        antispoofing ${netdev} ${bridge}
   4.219 +    fi
   4.220 +}
   4.221 +
   4.222 +op_stop () {
   4.223 +    if [ "${bridge}" == "null" ] ; then
   4.224 +        return
   4.225 +    fi
   4.226 +
   4.227 +    brctl delif ${bridge} ${netdev}
   4.228 +
   4.229 +    if ifconfig veth0 2>/dev/null | grep -q veth0 ; then
   4.230 +        brctl delif ${bridge} vif0.0
   4.231 +        ifconfig vif0.0 down
   4.232 +        mac=`ifconfig veth0 | grep HWadd | sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
   4.233 +        ifconfig ${netdev} down
   4.234 +        ifconfig ${netdev} hw ether ${mac}
   4.235 +        ifconfig ${netdev} arp up
   4.236 +        transfer_addrs veth0 ${netdev}
   4.237 +        transfer_routes veth0 ${netdev}
   4.238 +        del_addrs veth0
   4.239 +        ifconfig veth0 -arp down
   4.240 +        ifconfig veth0 hw ether 00:00:00:00:00:00
   4.241 +    else
   4.242 +        transfer_routes ${bridge} ${netdev}
   4.243 +    fi
   4.244 +}
   4.245 +
   4.246 +case ${OP} in
   4.247 +  start)
   4.248 +        op_start
   4.249 +        ;;
   4.250 +    
   4.251 +    stop)
   4.252 +        op_stop
   4.253 +        ;;
   4.254 +
   4.255 +    status)
   4.256 +        show_status ${netdev} ${bridge}
   4.257 +       ;;
   4.258 +
   4.259 +    *)
   4.260 +       echo 'Unknown command: ' ${OP} >&2
   4.261 +       echo 'Valid commands are: start, stop, status' >&2
   4.262 +       exit 1
   4.263 +esac
     5.1 --- a/tools/examples/xend-config.sxp	Sat Aug 13 21:52:46 2005 +0000
     5.2 +++ b/tools/examples/xend-config.sxp	Sat Aug 13 22:02:54 2005 +0000
     5.3 @@ -28,7 +28,7 @@
     5.4  
     5.5  ## Use the following if VIF traffic is bridged.
     5.6  # The script used to start/stop networking for xend.
     5.7 -(network-script    network)
     5.8 +(network-script    network-bridge)
     5.9  # The default bridge that virtual interfaces should be connected to.
    5.10  (vif-bridge        xen-br0)
    5.11  # The default script used to control virtual interfaces.