]> git.proxmox.com Git - mirror_ovs.git/blob - xenserver/etc_xensource_scripts_vif
vswitchd: Make names of Bridge external_ids generic.
[mirror_ovs.git] / xenserver / etc_xensource_scripts_vif
1 #!/bin/sh
2
3 # Copyright (C) 2008,2009 Citrix Systems, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation; version 2.1 only. with the special
8 # exception on linking described in file LICENSE.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14
15 # CA-23900: Warning: when VIFs are added to windows guests with PV drivers the backend vif device is registered,
16 # unregistered and then registered again. This causes the udev event to fire twice and this script runs twice.
17 # Since the first invocation of the script races with the device unregistration, spurious errors are possible
18 # which will be logged but are safe to ignore since the second script invocation should complete the operation.
19 # Note that each script invocation is run synchronously from udev and so the scripts don't race with each other.
20
21 # Keep other-config/ keys in sync with device.ml:vif_udev_keys
22
23 BRCTL="/usr/sbin/brctl"
24 IP="/sbin/ip"
25
26 vsctl="/usr/bin/ovs-vsctl"
27
28 # XAPI before build 29381 (approximately) did not provide some of the
29 # data in XenStore that we rely on.
30 . /etc/xensource-inventory
31 if test "$PRODUCT_VERSION" = "5.5.0" || test "${BUILD_NUMBER%p}" -le 26131
32 then
33 xs550=true
34 else
35 xs550=false
36 fi
37
38 handle_promiscuous()
39 {
40 local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
41 if [ $? -eq 0 -a -n "${arg}" ] ; then
42 case $NETWORK_MODE in
43 bridge)
44 case "${arg}" in
45 true|on) echo 1 > /sys/class/net/${dev}/brport/promisc ;;
46 *) echo 0 > /sys/class/net/${dev}/brport/promisc ;;
47 esac
48 ;;
49 vswitch)
50 logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch."
51 ;;
52 esac
53 fi
54 }
55
56 handle_ethtool()
57 {
58 local opt=$1
59 local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}" 2>/dev/null)
60 if [ $? -eq 0 -a -n "${arg}" ] ; then
61 case "${arg}" in
62 true|on) /sbin/ethtool -K "${dev}" "${opt}" on ;;
63 false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
64 *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
65 esac
66 fi
67 }
68
69 handle_mtu()
70 {
71 local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
72 if [ $? -eq 0 -a -n "${mtu}" ]; then
73 logger -t scripts-vif "Setting ${dev} MTU ${mtu}"
74 ${IP} link set "${dev}" mtu ${mtu} || logger -t scripts-vif "Failed to ip link set ${dev} mtu ${mtu}. Error code $?"
75 fi
76 }
77
78 set_vif_external_id()
79 {
80 local key=$1
81 local value=$2
82
83 logger -t scripts-vif "vif${DOMID}.${DEVID} external-ids:\"${key}\"=\"${value}\""
84
85 echo "-- set interface vif${DOMID}.${DEVID} external-ids:\"${key}\"=\"${value}\""
86 }
87
88 handle_vswitch_vif_details()
89 {
90 local vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
91 if [ $? -eq 0 -a -n "${vm}" ] ; then
92 local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
93 fi
94 if [ -n "${vm_uuid}" ] ; then
95 set_vif_external_id "vm-uuid" "${vm_uuid}"
96 fi
97
98 local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
99 if $xs550 && [ -z "${vif_uuid}" ] && [ -n "${vm_uuid}" ]; then
100 vif_uuid=$(xe vif-list --minimal vm-uuid="${vm_uuid}" device=$DEVID)
101 fi
102 if [ -n "${vif_uuid}" ] ; then
103 set_vif_external_id "vif-uuid" "${vif_uuid}"
104 fi
105
106 local vif_details=
107 local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
108 if $xs550 && [ -z "${net_uuid}" ] && [ -n "${vif_uuid}" ]; then
109 net_uuid=$(xe vif-param-get uuid="${vif_uuid}" param-name=network-uuid)
110 fi
111 if [ -n "${net_uuid}" ] ; then
112 set_vif_external_id "network-uuid" "${net_uuid}"
113 fi
114
115 local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
116 if [ -n "${address}" ] ; then
117 set_vif_external_id "vif-mac" "${address}"
118 fi
119 }
120
121 xs550_set_internal_network_uuid()
122 {
123 if $xs550; then
124 # vNetManager needs to know the network UUID(s) associated with each
125 # datapath. Normally interface-reconfigure adds them, but XAPI does
126 # not use interface-reconfigure for internal networks. Instead, XAPI
127 # calls the addbr ioctl internally, so we have to do it here instead
128 # for internal networks. This is only acceptable because xapi is lazy
129 # about creating internal networks: it only creates one just before it
130 # adds the first vif to it. There may still be a brief delay between
131 # the initial ovs-vswitchd connection to vNetManager and setting this
132 # configuration variable, but vNetManager can tolerate that.
133 local bridge=$1
134 local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
135 if [ -n "${net_uuid}" ] ; then
136 logger -t scripts-vif "${bridge} network-uuids ${net_uuid}"
137 echo "-- br-set-external-id $bridge network-uuids ${net_uuid}"
138 fi
139 fi
140 }
141
142 add_to_bridge()
143 {
144 local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
145 if [ $? -ne 0 -o -z "${address}" ]; then
146 logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
147 exit 1
148 fi
149 local bridge=$(xenstore-read "${PRIVATE}/bridge")
150 if [ $? -ne 0 -o -z "${bridge}" ]; then
151 logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
152 exit 1
153 fi
154 logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
155
156 ${IP} link set "${dev}" down || logger -t scripts-vif "Failed to ip link set ${dev} down"
157 ${IP} link set "${dev}" arp off || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
158 ${IP} link set "${dev}" multicast off || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
159 ${IP} link set "${dev}" address "${address}" || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
160 ${IP} addr flush "${dev}" || logger -t scripts-vif "Failed to ip addr flush ${dev}"
161
162 case $NETWORK_MODE in
163 bridge)
164 ${BRCTL} setfd "${bridge}" 0 || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
165 ${BRCTL} addif "${bridge}" "${dev}" || logger -t scripts-vif "Failed to brctl addif ${bridge} ${dev}"
166 ;;
167 vswitch)
168 if [ "$TYPE" = "vif" ] ; then
169 local vif_details=$(handle_vswitch_vif_details)
170 fi
171
172 $vsctl -- --if-exists del-port $dev -- add-port $bridge $dev $vif_details $(xs550_set_internal_network_uuid $bridge)
173 ;;
174 esac
175
176 ${IP} link set "${dev}" up || logger -t scripts-vif "Failed to ip link set ${dev} up"
177 }
178
179 remove_from_bridge()
180 {
181 case $NETWORK_MODE in
182 bridge)
183 # Nothing to do
184 ;;
185 vswitch)
186 # If ovs-brcompatd is running, it might already have deleted the
187 # port. Use --if-exists to suppress the error that would otherwise
188 # arise in that case.
189 $vsctl -- --if-exists del-port $dev
190 ;;
191 esac
192 }
193
194 NETWORK_MODE=$(cat /etc/xensource/network.conf)
195 ACTION=$1
196
197 # Older versions of XenServer do not pass in the type as an argument
198 if [[ $# -lt 2 ]]; then
199 TYPE=vif
200 else
201 TYPE=$2
202 fi
203
204 case $NETWORK_MODE in
205 bridge|vswitch) ;;
206 *)
207 logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
208 exit 1
209 ;;
210 esac
211
212 case ${TYPE} in
213 vif)
214 DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
215 DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
216 dev=vif${DOMID}.${DEVID}
217 ;;
218 tap)
219 dev=$INTERFACE
220 DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
221 DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
222 ;;
223 *)
224 logger -t scripts-vif "unknown interface type ${TYPE}"
225 exit 1
226 ;;
227 esac
228
229 XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
230 HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
231 PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
232
233 logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
234 case "${ACTION}" in
235 online)
236 if [ "${TYPE}" = "vif" ] ; then
237 handle_ethtool rx
238 handle_ethtool tx
239 handle_ethtool sg
240 handle_ethtool tso
241 handle_ethtool ufo
242 handle_ethtool gso
243
244 handle_mtu
245 add_to_bridge
246 handle_promiscuous
247
248 xenstore-write "${HOTPLUG}/vif" "${dev}"
249 xenstore-write "${HOTPLUG}/hotplug" "online"
250
251 # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
252 xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
253 fi
254 ;;
255
256 add)
257 if [ "${TYPE}" = "tap" ] ; then
258 add_to_bridge
259 fi
260 ;;
261
262 remove)
263 if [ "${TYPE}" = "vif" ] ;then
264 xenstore-rm "${HOTPLUG}/hotplug"
265 fi
266 logger -t scripts-vif "${dev} has been removed"
267 remove_from_bridge
268 ;;
269 esac