ia64/xen-unstable

view tools/examples/network-bridge @ 8964:8946b6dcd49e

Fix x86_64 Xen build.

event_callback_cs and failsafe_callback_cs are x86_32 only.

Signed-off-by: Ian Campbell <Ian.Campbell@XenSource.com>
author Ian.Campbell@xensource.com
date Wed Feb 22 17:26:39 2006 +0000 (2006-02-22)
parents d1596fc2cbaa
children 32444375fd3d
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 xenbr${vifnum}), adds a device
9 # (default eth${vifnum}) 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-bridge (start|stop|status) {VAR=VAL}*
20 #
21 # Vars:
22 #
23 # vifnum Virtual device number to use (default 0). Numbers >=8
24 # require the netback driver to have nloopbacks set to a
25 # higher value than its default of 8.
26 # bridge The bridge to use (default xenbr${vifnum}).
27 # netdev The interface to add to the bridge (default eth${vifnum}).
28 # antispoof Whether to use iptables to prevent spoofing (default no).
29 #
30 # Internal Vars:
31 # pdev="p${netdev}"
32 # vdev="veth${vifnum}"
33 # vif0="vif0.${vifnum}"
34 #
35 # start:
36 # Creates the bridge
37 # Copies the IP and MAC addresses from netdev to vdev
38 # Renames netdev to be pdev
39 # Renames vdev to be netdev
40 # Enslaves pdev, vdev to bridge
41 #
42 # stop:
43 # Removes netdev from the bridge
44 # Transfers addresses, routes from netdev to pdev
45 # Renames netdev to vdev
46 # Renames pdev to netdev
47 # Deletes bridge
48 #
49 # status:
50 # Print addresses, interfaces, routes
51 #
52 #============================================================================
55 dir=$(dirname "$0")
56 . "$dir/xen-script-common.sh"
57 . "$dir/xen-network-common.sh"
59 findCommand "$@"
60 evalVariables "$@"
62 vifnum=${vifnum:-0}
63 bridge=${bridge:-xenbr${vifnum}}
64 netdev=${netdev:-$(ip route list default scope global| awk '{ print $NF }')}
65 netdev=${netdev:-eth${vifnum}}
66 antispoof=${antispoof:-no}
68 pdev="p${netdev}"
69 vdev="veth${vifnum}"
70 vif0="vif0.${vifnum}"
72 get_ip_info() {
73 addr_pfx=`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
74 gateway=`ip route show dev $1 | fgrep default | sed 's/default via //'`
75 }
77 do_ifup() {
78 if ! ifup $1 ; then
79 if [ ${addr_pfx} ] ; then
80 # use the info from get_ip_info()
81 ip addr flush $1
82 ip addr add ${addr_pfx} dev $1
83 ip link set dev $1 up
84 [ ${gateway} ] && ip route add default via ${gateway}
85 fi
86 fi
87 }
89 # Usage: transfer_addrs src dst
90 # Copy all IP addresses (including aliases) from device $src to device $dst.
91 transfer_addrs () {
92 local src=$1
93 local dst=$2
94 # Don't bother if $dst already has IP addresses.
95 if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
96 return
97 fi
98 # Address lines start with 'inet' and have the device in them.
99 # Replace 'inet' with 'ip addr add' and change the device name $src
100 # to 'dev $src'.
101 ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
102 s/inet/ip addr add/
103 s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
104 s/${src}/dev ${dst}/
105 " | sh -e
106 # Remove automatic routes on destination device
107 ip route list | sed -ne "
108 /dev ${dst}\( \|$\)/ {
109 s/^/ip route del /
110 p
111 }" | sh -e
112 }
114 # Usage: transfer_routes src dst
115 # Get all IP routes to device $src, delete them, and
116 # add the same routes to device $dst.
117 # The original routes have to be deleted, otherwise adding them
118 # for $dst fails (duplicate routes).
119 transfer_routes () {
120 local src=$1
121 local dst=$2
122 # List all routes and grep the ones with $src in.
123 # Stick 'ip route del' on the front to delete.
124 # Change $src to $dst and use 'ip route add' to add.
125 ip route list | sed -ne "
126 /dev ${src}\( \|$\)/ {
127 h
128 s/^/ip route del /
129 P
130 g
131 s/${src}/${dst}/
132 s/^/ip route add /
133 P
134 d
135 }" | sh -e
136 }
139 ##
140 # link_exists interface
141 #
142 # Returns 0 if the interface named exists (whether up or down), 1 otherwise.
143 #
144 link_exists()
145 {
146 if ip link show "$1" >/dev/null 2>/dev/null
147 then
148 return 0
149 else
150 return 1
151 fi
152 }
155 # Usage: create_bridge bridge
156 create_bridge () {
157 local bridge=$1
159 # Don't create the bridge if it already exists.
160 if ! brctl show | grep -q ${bridge} ; then
161 brctl addbr ${bridge}
162 brctl stp ${bridge} off
163 brctl setfd ${bridge} 0
164 fi
165 ip link set ${bridge} up
166 }
168 # Usage: add_to_bridge bridge dev
169 add_to_bridge () {
170 local bridge=$1
171 local dev=$2
172 # Don't add $dev to $bridge if it's already on a bridge.
173 if ! brctl show | grep -q ${dev} ; then
174 brctl addif ${bridge} ${dev}
175 fi
176 }
178 # Set the default forwarding policy for $dev to drop.
179 # Allow forwarding to the bridge.
180 antispoofing () {
181 iptables -P FORWARD DROP
182 iptables -F FORWARD
183 iptables -A FORWARD -m physdev --physdev-in ${pdev} -j ACCEPT
184 iptables -A FORWARD -m physdev --physdev-in ${vif0} -j ACCEPT
185 }
187 # Usage: show_status dev bridge
188 # Print ifconfig and routes.
189 show_status () {
190 local dev=$1
191 local bridge=$2
193 echo '============================================================'
194 ip addr show ${dev}
195 ip addr show ${bridge}
196 echo ' '
197 brctl show ${bridge}
198 echo ' '
199 ip route list
200 echo ' '
201 route -n
202 echo '============================================================'
203 }
205 op_start () {
206 if [ "${bridge}" = "null" ] ; then
207 return
208 fi
210 if ! link_exists "$vdev"; then
211 if link_exists "$pdev"; then
212 # The device is already up.
213 return
214 else
215 echo "
216 Link $vdev is missing.
217 This may be because you have reached the limit of the number of interfaces
218 that the loopback driver supports. If the loopback driver is a module, you
219 may raise this limit by passing it as a parameter (nloopbacks=<N>); if the
220 driver is compiled statically into the kernel, then you may set the parameter
221 using loopback.nloopbacks=<N> on the domain 0 kernel command line.
222 " >&2
223 exit 1
224 fi
225 fi
227 create_bridge ${bridge}
229 if link_exists "$vdev"; then
230 mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'`
231 preiftransfer ${netdev}
232 transfer_addrs ${netdev} ${vdev}
233 if ! ifdown ${netdev}; then
234 # If ifdown fails, remember the IP details.
235 get_ip_info ${netdev}
236 ip link set ${netdev} down
237 ip addr flush ${netdev}
238 fi
239 ip link set ${netdev} name ${pdev}
240 ip link set ${vdev} name ${netdev}
241 ip link set ${pdev} down arp off
242 ip link set ${pdev} addr fe:ff:ff:ff:ff:ff
243 ip addr flush ${pdev}
244 ip link set ${netdev} addr ${mac} arp on
245 add_to_bridge ${bridge} ${vif0}
246 ip link set ${bridge} up
247 ip link set ${vif0} up
248 ip link set ${pdev} up
249 add_to_bridge2 ${bridge} ${pdev}
250 do_ifup ${netdev}
251 else
252 # old style without ${vdev}
253 transfer_addrs ${netdev} ${bridge}
254 transfer_routes ${netdev} ${bridge}
255 fi
257 if [ ${antispoof} = 'yes' ] ; then
258 antispoofing
259 fi
260 }
262 op_stop () {
263 if [ "${bridge}" = "null" ]; then
264 return
265 fi
266 if ! link_exists "$bridge"; then
267 return
268 fi
270 if link_exists "$pdev"; then
271 ip link set dev ${vif0} down
272 mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'`
273 transfer_addrs ${netdev} ${pdev}
274 if ! ifdown ${netdev}; then
275 get_ip_info ${netdev}
276 fi
277 ip link set ${netdev} down arp off
278 ip link set ${netdev} addr fe:ff:ff:ff:ff:ff
279 ip link set ${pdev} down
280 ip addr flush ${netdev}
281 ip link set ${pdev} addr ${mac} arp on
283 brctl delif ${bridge} ${pdev}
284 brctl delif ${bridge} ${vif0}
285 ip link set ${bridge} down
287 ip link set ${netdev} name ${vdev}
288 ip link set ${pdev} name ${netdev}
289 do_ifup ${netdev}
290 else
291 transfer_routes ${bridge} ${netdev}
292 ip link set ${bridge} down
293 fi
294 brctl delbr ${bridge}
295 }
297 # adds $dev to $bridge but waits for $dev to be in running state first
298 add_to_bridge2() {
299 local bridge=$1
300 local dev=$2
301 local maxtries=10
303 echo -n "Waiting for ${dev} to negotiate link."
304 for i in `seq ${maxtries}` ; do
305 if ifconfig ${dev} | grep -q RUNNING ; then
306 break
307 else
308 echo -n '.'
309 sleep 1
310 fi
311 done
313 if [ ${i} -eq ${maxtries} ] ; then echo '(link isnt in running state)' ; fi
315 add_to_bridge ${bridge} ${dev}
316 }
318 case "$command" in
319 start)
320 op_start
321 ;;
323 stop)
324 op_stop
325 ;;
327 status)
328 show_status ${netdev} ${bridge}
329 ;;
331 *)
332 echo "Unknown command: $command" >&2
333 echo 'Valid commands are: start, stop, status' >&2
334 exit 1
335 esac