debuggers.hg

view tools/hotplug/Linux/vif-common.sh @ 21990:0232bc7c9544

tools/hotplug, Use udev rules instead of qemu script to setup the bridge.
From: Anthony PERARD <anthony.perard@citrix.com>

This patch adds a second argument to vif-bridge script. It can be "vif"
or "tap". "vif" give the default behavior and "tap" just add the
interface to the found bridge when the action is "add".

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
tools/hotplug/Linux/vif-bridge | 20 ++++++---
tools/hotplug/Linux/vif-common.sh | 70 ++++++++++++++++++++++++---------
tools/hotplug/Linux/xen-backend.rules | 5 +-
tools/libxl/libxl.c | 6 +-
4 files changed, 70 insertions(+), 31 deletions(-)
author Anthony Perard <anthony.perard@citrix.com>
date Mon Aug 09 16:46:02 2010 +0100 (2010-08-09)
parents 57033bc8cd30
children b0fe8260cefa
line source
1 #
2 # Copyright (c) 2005 XenSource Ltd.
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of version 2.1 of the GNU Lesser General Public
6 # License as published by the Free Software Foundation.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #
19 dir=$(dirname "$0")
20 . "$dir/xen-hotplug-common.sh"
21 . "$dir/xen-network-common.sh"
23 findCommand "$@"
25 if [ "$command" != "online" ] &&
26 [ "$command" != "offline" ] &&
27 [ "$command" != "add" ] &&
28 [ "$command" != "remove" ]
29 then
30 log err "Invalid command: $command"
31 exit 1
32 fi
35 # Parameters may be read from the environment, the command line arguments, and
36 # the store, with overriding in that order. The environment is given by the
37 # driver, the command line is given by the Xend global configuration, and
38 # store details are given by the per-domain or per-device configuration.
40 evalVariables "$@"
42 # Older versions of Xen do not pass in the type as an argument,
43 # so the default value is vif.
44 : ${type_if:=vif}
46 case "$type_if" in
47 vif)
48 dev=$vif
49 ;;
50 tap)
51 dev=$INTERFACE
52 ;;
53 *)
54 log err "unknown interface type $type_if"
55 exit 1
56 ;;
57 esac
59 case "$command" in
60 online | offline)
61 test "$type_if" != vif && exit 0
62 ;;
63 add | remove)
64 test "$type_if" != tap && exit 0
65 ;;
66 esac
69 if [ "$type_if" = vif ]; then
70 # Check presence of compulsory args.
71 XENBUS_PATH="${XENBUS_PATH:?}"
72 vif="${vif:?}"
74 vifname=$(xenstore_read_default "$XENBUS_PATH/vifname" "")
75 if [ "$vifname" ]
76 then
77 if [ "$command" == "online" ] && ! ip link show "$vifname" >&/dev/null
78 then
79 do_or_die ip link set "$vif" name "$vifname"
80 fi
81 vif="$vifname"
82 fi
83 elif [ "$type_if" = tap ]; then
84 # Check presence of compulsory args.
85 : ${INTERFACE:?}
87 # Get xenbus_path from device name.
88 # The name is built like that: "tap${domid}.${devid}".
89 dev_=${dev#tap}
90 domid=${dev_%.*}
91 devid=${dev_#*.}
93 XENBUS_PATH="/local/domain/0/backend/vif/$domid/$devid"
94 fi
96 ip=${ip:-}
97 ip=$(xenstore_read_default "$XENBUS_PATH/ip" "$ip")
99 frob_iptable()
100 {
101 if [ "$command" == "online" ]
102 then
103 local c="-I"
104 else
105 local c="-D"
106 fi
108 iptables "$c" FORWARD -m physdev --physdev-in "$vif" "$@" -j ACCEPT \
109 2>/dev/null &&
110 iptables "$c" FORWARD -m state --state RELATED,ESTABLISHED -m physdev \
111 --physdev-out "$vif" -j ACCEPT 2>/dev/null
113 if [ "$command" == "online" -a $? -ne 0 ]
114 then
115 log err "iptables setup failed. This may affect guest networking."
116 fi
117 }
120 ##
121 # Add or remove the appropriate entries in the iptables. With antispoofing
122 # turned on, we have to explicitly allow packets to the interface, regardless
123 # of the ip setting. If ip is set, then we additionally restrict the packets
124 # to those coming from the specified networks, though we allow DHCP requests
125 # as well.
126 #
127 handle_iptable()
128 {
129 # Check for a working iptables installation. Checking for the iptables
130 # binary is not sufficient, because the user may not have the appropriate
131 # modules installed. If iptables is not working, then there's no need to do
132 # anything with it, so we can just return.
133 if ! iptables -L -n >&/dev/null
134 then
135 return
136 fi
138 claim_lock "iptables"
140 if [ "$ip" != "" ]
141 then
142 local addr
143 for addr in $ip
144 do
145 frob_iptable -s "$addr"
146 done
148 # Always allow the domain to talk to a DHCP server.
149 frob_iptable -p udp --sport 68 --dport 67
150 else
151 # No IP addresses have been specified, so allow anything.
152 frob_iptable
153 fi
155 release_lock "iptables"
156 }
159 ##
160 # ip_of interface
161 #
162 # Print the IP address currently in use at the given interface, or nothing if
163 # the interface is not up.
164 #
165 ip_of()
166 {
167 ip addr show "$1" | awk "/^.*inet.*$1\$/{print \$2}" | sed -n '1 s,/.*,,p'
168 }
171 ##
172 # dom0_ip
173 #
174 # Print the IP address of the interface in dom0 through which we are routing.
175 # This is the IP address on the interface specified as "netdev" as a parameter
176 # to these scripts, or eth0 by default. This function will call fatal if no
177 # such interface could be found.
178 #
179 dom0_ip()
180 {
181 local nd=${netdev:-eth0}
182 local result=$(ip_of "$nd")
183 if [ -z "$result" ]
184 then
185 fatal
186 "$netdev is not up. Bring it up or specify another interface with " \
187 "netdev=<if> as a parameter to $0."
188 fi
189 echo "$result"
190 }