ia64/xen-unstable

view tools/examples/network-bridge @ 7238:971e7c7411b3

Raise an exception if an error appears on the pipes to our children, and make
sure that the child's pipes are closed even under that exception. Move the
handling of POLLHUP to the end of the loop, so that we guarantee to read any
remaining data from the child if POLLHUP and POLLIN appear at the same time.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Thu Oct 06 10:13:11 2005 +0100 (2005-10-06)
parents e579bef0d237
children f98c8238e909
line source
1 #!/bin/sh
2 #============================================================================
3 # Default Xen network start/stop script.
4 # Xend calls a network script when it starts.
5 # The script name to use is defined in /etc/xen/xend-config.sxp
6 # in the network-script field.
7 #
8 # This script creates a bridge (default xen-br0), adds a device
9 # (default eth0) to it, copies the IP addresses from the device
10 # to the bridge and adjusts the routes accordingly.
11 #
12 # If all goes well, this should ensure that networking stays up.
13 # However, some configurations are upset by this, especially
14 # NFS roots. If the bridged setup does not meet your needs,
15 # configure a different script, for example using routing instead.
16 #
17 # Usage:
18 #
19 # network (start|stop|status) {VAR=VAL}*
20 #
21 # Vars:
22 #
23 # bridge The bridge to use (default xen-br0).
24 # netdev The interface to add to the bridge (default eth0).
25 # antispoof Whether to use iptables to prevent spoofing (default yes).
26 #
27 # start:
28 # Creates the bridge and enslaves netdev to it.
29 # Copies the IP addresses from netdev to the bridge.
30 # Deletes the routes to netdev and adds them on bridge.
31 #
32 # stop:
33 # Removes netdev from the bridge.
34 # Deletes the routes to bridge and adds them to netdev.
35 #
36 # status:
37 # Print ifconfig for netdev and bridge.
38 # Print routes.
39 #
40 #============================================================================
42 # Exit if anything goes wrong.
43 set -e
45 # First arg is the operation.
46 OP=$1
47 shift
49 # Pull variables in args in to environment.
50 for arg ; do export "${arg}" ; done
52 bridge=${bridge:-xen-br0}
53 netdev=${netdev:-eth0}
54 antispoof=${antispoof:-no}
56 echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof" >&2
58 # Gentoo doesn't have ifup/ifdown: define appropriate alternatives
59 which ifup >& /dev/null
60 if [ "$?" != 0 -a -e /etc/conf.d/net ]; then
61 ifup() {
62 /etc/init.d/net.$1 start
63 }
64 ifdown() {
65 /etc/init.d/net.$1 stop
66 }
67 fi
69 # Usage: transfer_addrs src dst
70 # Copy all IP addresses (including aliases) from device $src to device $dst.
71 transfer_addrs () {
72 local src=$1
73 local dst=$2
74 # Don't bother if $dst already has IP addresses.
75 if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
76 return
77 fi
78 # Address lines start with 'inet' and have the device in them.
79 # Replace 'inet' with 'ip addr add' and change the device name $src
80 # to 'dev $src'.
81 ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
82 s/inet/ip addr add/
83 s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
84 s/${src}/dev ${dst}/
85 " | sh -e
86 # Remove automatic routes on destionation device
87 ip route list | sed -ne "
88 /dev ${dst}\( \|$\)/ {
89 s/^/ip route del /
90 p
91 }" | sh -e
92 }
94 # Usage: del_addrs src
95 del_addrs () {
96 local src=$1
97 ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
98 s/inet/ip addr del/
99 s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@
100 s/${src}/dev ${src}/
101 " | sh -e
102 }
104 # Usage: transfer_routes src dst
105 # Get all IP routes to device $src, delete them, and
106 # add the same routes to device $dst.
107 # The original routes have to be deleted, otherwise adding them
108 # for $dst fails (duplicate routes).
109 transfer_routes () {
110 local src=$1
111 local dst=$2
112 # List all routes and grep the ones with $src in.
113 # Stick 'ip route del' on the front to delete.
114 # Change $src to $dst and use 'ip route add' to add.
115 ip route list | sed -ne "
116 /dev ${src}\( \|$\)/ {
117 h
118 s/^/ip route del /
119 P
120 g
121 s/${src}/${dst}/
122 s/^/ip route add /
123 P
124 d
125 }" | sh -e
126 }
128 # Usage: create_bridge bridge
129 create_bridge () {
130 local bridge=$1
132 # Don't create the bridge if it already exists.
133 if ! brctl show | grep -q ${bridge} ; then
134 brctl addbr ${bridge}
135 brctl stp ${bridge} off
136 brctl setfd ${bridge} 0
137 fi
138 ifconfig ${bridge} up
139 }
141 # Usage: add_to_bridge bridge dev
142 add_to_bridge () {
143 local bridge=$1
144 local dev=$2
145 # Don't add $dev to $bridge if it's already on a bridge.
146 if ! brctl show | grep -q ${dev} ; then
147 brctl addif ${bridge} ${dev}
148 fi
149 }
151 # Usage: antispoofing dev bridge
152 # Set the default forwarding policy for $dev to drop.
153 # Allow forwarding to the bridge.
154 antispoofing () {
155 local dev=$1
156 local bridge=$2
158 iptables -P FORWARD DROP
159 iptables -A FORWARD -m physdev --physdev-in ${dev} -j ACCEPT
160 }
162 # Usage: show_status dev bridge
163 # Print ifconfig and routes.
164 show_status () {
165 local dev=$1
166 local bridge=$2
168 echo '============================================================'
169 ifconfig ${dev}
170 ifconfig ${bridge}
171 echo ' '
172 ip route list
173 echo ' '
174 route -n
175 echo '============================================================'
176 }
178 op_start () {
179 if [ "${bridge}" == "null" ] ; then
180 return
181 fi
183 create_bridge ${bridge}
185 if ifconfig 2>/dev/null | grep -q veth0 ; then
186 return
187 fi
189 if ifconfig veth0 2>/dev/null | grep -q veth0 ; then
190 mac=`ifconfig ${netdev} | grep HWadd | sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
191 if ! ifdown ${netdev} ; then
192 # if ifup didn't work, see if we have an ip= on cmd line
193 if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline ;
194 then
195 kip=`sed -e 's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
196 kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline`
197 kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline`
198 ifconfig ${netdev} 0.0.0.0 down
199 fi
200 fi
201 ip link set ${netdev} name p${netdev}
202 ip link set veth0 name ${netdev}
203 ifconfig p${netdev} 0.0.0.0 -arp down
204 ifconfig p${netdev} hw ether fe:ff:ff:ff:ff:ff
205 ifconfig ${netdev} hw ether ${mac}
206 add_to_bridge ${bridge} vif0.0
207 add_to_bridge ${bridge} p${netdev}
208 ip link set ${bridge} up
209 ip link set vif0.0 up
210 ip link set p${netdev} up
211 if ! ifup ${netdev} ; then
212 if [ ${kip} ] ; then
213 # use the addresses we grocked from /proc/cmdline
214 ifconfig ${netdev} ${kip}
215 [ ${kmask} ] && ifconfig ${netdev} netmask ${kmask}
216 ifconfig ${netdev} up
217 [ ${kgate} ] && ip route add default via ${kgate}
218 fi
219 fi
220 else
221 # old style without veth0
222 transfer_addrs ${netdev} ${bridge}
223 transfer_routes ${netdev} ${bridge}
224 fi
226 if [ ${antispoof} == 'yes' ] ; then
227 antispoofing ${netdev} ${bridge}
228 fi
229 }
231 op_stop () {
232 if [ "${bridge}" == "null" ] ; then
233 return
234 fi
236 if ifconfig peth0 2>/dev/null | grep -q peth0 ; then
238 ifconfig vif0.0 down
239 mac=`ifconfig eth0 | grep HWadd | \
240 sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
241 ifconfig ${netdev} 0.0.0.0 down
242 ifconfig ${netdev} hw ether fe:ff:ff:ff:ff:ff
244 ifconfig p${netdev} down
245 ifconfig p${netdev} hw ether ${mac} arp
246 brctl delif ${bridge} p${netdev}
248 ip link set eth0 name veth0
249 ip link set peth0 name eth0
250 ifconfig ${bridge} down
251 brctl delbr ${bridge}
252 ifup eth0
254 else
255 transfer_routes ${bridge} ${netdev}
256 fi
257 }
259 case ${OP} in
260 start)
261 op_start
262 ;;
264 stop)
265 op_stop
266 ;;
268 status)
269 show_status ${netdev} ${bridge}
270 ;;
272 *)
273 echo 'Unknown command: ' ${OP} >&2
274 echo 'Valid commands are: start, stop, status' >&2
275 exit 1
276 esac