ia64/xen-unstable

view tools/examples/vif-nat @ 8202:ccb923727f93

Fix vif-net script for setups where eth0 has no IP address.
Use the IP address associated with the backend network interface
for connections from dom0 to the guest connected to this network
interface.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Fri Dec 02 22:15:58 2005 +0100 (2005-12-02)
parents e47e953c9e10
children b20641d24272
line source
1 #!/bin/sh
2 #============================================================================
3 # /etc/xen/vif-nat
4 #
5 # Script for configuring a vif in routed-nat mode.
6 # The hotplugging system will call this script if it is specified either in
7 # the device configuration given to Xend, or the default Xend configuration
8 # in /etc/xen/xend-config.sxp. If the script is specified in neither of those
9 # places, then vif-bridge is the default.
10 #
11 # Usage:
12 # vif-nat (add|remove|online|offline)
13 #
14 # Environment vars:
15 # vif vif interface name (required).
16 # XENBUS_PATH path to this device's details in the XenStore (required).
17 #
18 # Parameters:
19 # dhcp Whether to alter the local DHCP configuration to include this
20 # new host (default no).
21 #
22 # Read from the store:
23 # ip list of IP networks for the vif, space-separated (default given in
24 # this script).
25 #============================================================================
28 dir=$(dirname "$0")
29 . "$dir/vif-common.sh"
31 dhcp=${dhcp:-no}
33 if [ "$dhcp" != 'no' ]
34 then
35 dhcpd_conf_file=$(find_dhcpd_conf_file)
36 dhcpd_init_file=$(find_dhcpd_init_file)
37 if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ]
38 then
39 echo 'Failed to find dhcpd configuration or init file.' >&2
40 exit 1
41 fi
42 fi
45 ip_from_dom()
46 {
47 local domid=$(echo "$XENBUS_PATH" | sed -n 's#.*/\([0-9]*\)/[0-9]*$#\1#p')
48 local vifid=$(echo "$XENBUS_PATH" | sed -n 's#.*/[0-9]*/\([0-9]*\)$#\1#p')
50 local domid1=$(( $domid / 256 ))
51 local domid2=$(( $domid % 256 ))
52 vifid=$(( $vifid + 1 ))
54 echo "10.$domid1.$domid2.$vifid/16"
55 }
58 routing_ip()
59 {
60 echo $(echo $1 | awk -F. '{print $1"."$2"."$3"."$4 + 127}')
61 }
64 dotted_quad()
65 {
66 echo\
67 $(( ($1 & 0xFF000000) >> 24))\
68 .$(( ($1 & 0x00FF0000) >> 16))\
69 .$(( ($1 & 0x0000FF00) >> 8 ))\
70 .$(( $1 & 0x000000FF ))
71 }
74 if [ "$ip" == "" ]
75 then
76 ip=$(ip_from_dom)
77 fi
79 router_ip=$(routing_ip "$ip")
81 # Split the given IP/bits pair.
82 vif_ip=`echo ${ip} | awk -F/ '{print $1}'`
83 bits=`echo ${ip} | awk -F/ '{print $2}'`
85 # Convert $bits and $vif_ip to integers, mask appropriately to get a network
86 # address, and convert them both to dotted quads.
88 intmask=$(( (0xFFFFFFFF << (32 - $bits)) & 0xFFFFFFFF ))
89 vif_int=$(( $(echo "((($vif_ip" | sed -e 's#\.#)\*256\+#g') ))
91 netmask=$(dotted_quad $intmask)
92 network=$(dotted_quad $(( $vif_int & $intmask )) )
95 dhcp_remove_entry()
96 {
97 local tmpfile=$(mktemp)
98 grep -v "host Xen-${vif/./-}" "$dhcpd_conf_file" >"$tmpfile"
99 if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
100 then
101 rm "$tmpfile"
102 else
103 mv "$tmpfile" "$dhcpd_conf_file"
104 fi
105 }
108 dhcp_up()
109 {
110 dhcp_remove_entry
111 mac=$(xenstore_read "$XENBUS_PATH/mac")
112 echo >>"$dhcpd_conf_file" \
113 "host Xen-${vif/./-} { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; }"
115 "$dhcpd_init_file" restart
116 }
119 dhcp_down()
120 {
121 dhcp_remove_entry
122 "$dhcpd_init_file" restart || true # We need to ignore failure because
123 # ISC dhcpd 3 borks if there is nothing
124 # for it to do, which is the case if
125 # the outgoing interface is not
126 # configured to offer leases and there
127 # are no vifs.
128 }
131 case "$command" in
132 online)
133 if ip route | grep -q "dev $vif"
134 then
135 log debug "$vif already up"
136 exit 0
137 fi
139 do_or_die ip link set "$vif" up arp on
140 do_or_die ip addr add "$router_ip" dev "$vif"
141 do_or_die ip route add "$vif_ip" dev "$vif" src "$router_ip"
142 echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
143 [ "$dhcp" != 'no' ] && dhcp_up
144 ;;
145 offline)
146 [ "$dhcp" != 'no' ] && dhcp_down
147 ifconfig "$vif" down || true
148 ;;
149 esac
152 handle_iptable
154 log debug "Successful vif-nat $command for $vif."
155 if [ "$command" == "online" ]
156 then
157 success
158 fi