ia64/xen-unstable

view tools/vnet/examples/network-vnet @ 3966:0b9ff7354a95

bitkeeper revision 1.1236.1.47 (4225ff68LvKUt73k78dKjnXxsnQt7A)

Fix tools build. Clean up barrier defs.
Signed-off-by: Jerone Young <jyoung5@us.ibm.com>
Signed-off-by: Keir Fraser <keir.fraser@cl.cam.ac.uk>
author kaf24@scramble.cl.cam.ac.uk
date Wed Mar 02 18:01:12 2005 +0000 (2005-03-02)
parents 0a4b76b6b5a0
children 6bad5eb72ce0 dfaf788ab18c
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:-yes}
56 echo "network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof"
58 # Usage: transfer_addrs src dst
59 # Copy all IP addresses (including aliases) from device $src to device $dst.
60 transfer_addrs () {
61 local src=$1
62 local dst=$2
63 # Don't bother if $dst already has IP addresses.
64 if ip addr show dev ${dst} | egrep -q '^ *inet' ; then
65 return
66 fi
67 # Address lines start with 'inet' and have the device in them.
68 # Replace 'inet' with 'ip addr add' and change the device name $src
69 # to 'dev $src'. Remove netmask as we'll add routes later.
70 ip addr show dev ${src} | egrep '^ *inet' | sed -e "
71 s/inet/ip addr add/
72 s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@
73 s/${src}/dev ${dst}/
74 " | sh -e
75 }
77 # Usage: transfer_routes src dst
78 # Get all IP routes to device $src, delete them, and
79 # add the same routes to device $dst.
80 # The original routes have to be deleted, otherwise adding them
81 # for $dst fails (duplicate routes).
82 transfer_routes () {
83 local src=$1
84 local dst=$2
85 # List all routes and grep the ones with $src in.
86 # Stick 'ip route del' on the front to delete.
87 # Change $src to $dst and use 'ip route add' to add.
88 ip route list | grep ${src} | sed -e "
89 h
90 s/^/ip route del /
91 P
92 g
93 s/${src}/${dst}/
94 s/^/ip route add /
95 P
96 d
97 " | sh -e
98 }
100 # Usage: create_bridge dev bridge
101 # Create bridge $bridge and add device $dev to it.
102 create_bridge () {
103 local dev=$1
104 local bridge=$2
106 # Don't create the bridge if it already exists.
107 if ! brctl show | grep -q ${bridge} ; then
108 brctl addbr ${bridge}
109 brctl stp ${bridge} off
110 brctl setfd ${bridge} 0
111 fi
112 ifconfig ${bridge} up
113 }
115 # Usage: antispoofing dev bridge
116 # Set the default forwarding policy for $dev to drop.
117 # Allow forwarding to the bridge.
118 antispoofing () {
119 local dev=$1
120 local bridge=$2
122 iptables -P FORWARD DROP
123 iptables -A FORWARD -m physdev --physdev-in ${dev} -j ACCEPT
124 }
126 # Usage: show_status dev bridge
127 # Print ifconfig and routes.
128 show_status () {
129 local dev=$1
130 local bridge=$2
132 echo '============================================================'
133 ifconfig ${dev}
134 ifconfig ${bridge}
135 echo ' '
136 ip route list
137 echo ' '
138 route -n
139 echo '============================================================'
140 }
142 # Insert the vnet module if it can be found and
143 # it's not already there.
144 vnet_insert () {
145 local module="vnet_module"
146 local mod_dir=/lib/modules/$(uname -r)/kernel
147 local mod_path="${mod_dir}/${module}"
148 local mod_obj=""
150 for ext in ".o" ".ko" ; do
151 f=${mod_path}${ext}
152 if [ -f ${f} ] ; then
153 mod_obj=$f
154 break
155 fi
156 done
157 if [ "${mod_obj}" == "" ] ; then
158 return
159 fi
160 if lsmod | grep -q ${module} ; then
161 echo "VNET: ${module} loaded"
162 else
163 echo "VNET: Loading ${module}..."
164 insmod ${mod_obj}
165 fi
166 }
168 op_start () {
169 if [ "${bridge}" == "null" ] ; then
170 return
171 fi
172 # Create the bridge and give it the interface IP addresses.
173 # Move the interface routes onto the bridge.
174 create_bridge ${netdev} ${bridge}
175 transfer_addrs ${netdev} ${bridge}
176 transfer_routes ${netdev} ${bridge}
177 # Don't add $dev to $bridge if it's already on a bridge.
178 if ! brctl show | grep -q ${netdev} ; then
179 brctl addif ${bridge} ${netdev}
180 fi
182 if [ ${antispoof} == 'yes' ] ; then
183 antispoofing ${netdev} ${bridge}
184 fi
186 vnet_insert
187 }
189 op_stop () {
190 if [ "${bridge}" == "null" ] ; then
191 return
192 fi
193 # Remove the interface from the bridge.
194 # Move the routes back to the interface.
195 brctl delif ${bridge} ${netdev}
196 transfer_routes ${bridge} ${netdev}
198 # It's not our place to be enabling forwarding...
199 }
201 case ${OP} in
202 start)
203 op_start
204 ;;
206 stop)
207 op_stop
208 ;;
210 status)
211 show_status ${netdev} ${bridge}
212 ;;
214 *)
215 echo 'Unknown command: ' ${OP}
216 echo 'Valid commands are: start, stop, status'
217 exit 1
218 esac