xenserver: Fix vNetManager internal network compatibility with XS 5.5.0.
[cascardo/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 dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
28
29 handle_promiscuous()
30 {
31     local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
32     if [ $? -eq 0 -a -n "${arg}" ] ; then
33         case $NETWORK_MODE in
34             bridge)
35                 case "${arg}" in 
36                     true|on) echo 1 > /sys/class/net/${dev}/brport/promisc ;;
37                     *) echo 0 > /sys/class/net/${dev}/brport/promisc ;;
38                 esac
39                 ;;
40             vswitch)
41                 logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch."
42                 ;;
43         esac
44     fi
45 }
46
47 handle_ethtool()
48 {
49     local opt=$1
50     local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}" 2>/dev/null)
51     if [ $? -eq 0 -a -n "${arg}" ] ; then
52         case "${arg}" in
53             true|on)   /sbin/ethtool -K "${dev}" "${opt}" on ;;
54             false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
55             *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
56         esac
57     fi
58 }
59
60 handle_mtu()
61 {
62     local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
63     if [ $? -eq 0 -a -n "${mtu}" ]; then
64         logger -t scripts-vif "Setting ${dev} MTU ${mtu}"
65         ${IP} link set "${dev}" mtu ${mtu} || logger -t scripts-vif "Failed to ip link set ${dev} mtu ${mtu}. Error code $?"
66     fi
67 }
68
69 set_vif_external_id()
70 {
71     local key=$1
72     local value=$2
73
74     logger -t scripts-vif "vif${DOMID}.${DEVID} external-ids:\"${key}\"=\"${value}\""
75
76     echo "-- set interface vif${DOMID}.${DEVID} external-ids:\"${key}\"=\"${value}\""
77 }
78
79 handle_vswitch_vif_details()
80 {
81     local vif_details=
82     local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
83     if [ -n "${net_uuid}" ] ; then
84         set_vif_external_id "xs-network-uuid" "${net_uuid}"
85     fi
86
87     local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
88     if [ -n "${address}" ] ; then
89         set_vif_external_id "xs-vif-mac" "${address}"
90     fi
91
92     local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
93     if [ -n "${vif_uuid}" ] ; then
94         set_vif_external_id "xs-vif-uuid" "${vif_uuid}"
95     fi
96
97     local vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
98     if [ $? -eq 0 -a -n "${vm}" ] ; then
99         local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
100     fi
101     if [ -n "${vm_uuid}" ] ; then
102         set_vif_external_id "xs-vm-uuid" "${vm_uuid}"
103     fi
104 }
105
106 xs550_set_internal_network_uuid()
107 {
108     . /etc/xensource-inventory
109     if test "$PRODUCT_VERSION" = "5.5.0" || test "${BUILD_NUMBER%p}" -le 26131
110     then
111         # vNetManager needs to know the network UUID(s) associated with each
112         # datapath.  Normally interface-reconfigure adds them, but XAPI does
113         # not use interface-reconfigure for internal networks. Instead, XAPI
114         # calls the addbr ioctl internally, so we have to do it here instead
115         # for internal networks.  This is only acceptable because xapi is lazy
116         # about creating internal networks: it only creates one just before it
117         # adds the first vif to it.  There may still be a brief delay between
118         # the initial ovs-vswitchd connection to vNetManager and setting this
119         # configuration variable, but vNetManager can tolerate that.
120         local bridge=$1
121         local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
122         if [ -n "${net_uuid}" ] ; then
123             logger -t scripts-vif "${bridge} xs-network-uuids ${net_uuid}"
124             echo "-- br-set-external-id \"$bridge\" xs-network-uuids \"${net_uuid}\""
125         fi
126     else
127         # XAPI after 5.5.0 sets the network external ids itself, via ovs-vsctl.
128         :
129     fi
130 }
131
132 add_to_bridge()
133 {
134     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
135     if [ $? -ne 0 -o -z "${address}" ]; then
136         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
137         exit 1
138     fi
139     local bridge=$(xenstore-read "${PRIVATE}/bridge")
140     if [ $? -ne 0 -o -z "${bridge}" ]; then
141         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
142         exit 1
143     fi
144     logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
145
146     ${IP} link set "${dev}" down                        || logger -t scripts-vif "Failed to ip link set ${dev} down"
147     ${IP} link set "${dev}" arp off                     || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
148     ${IP} link set "${dev}" multicast off               || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
149     ${IP} link set "${dev}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
150     ${IP} addr flush "${dev}"                           || logger -t scripts-vif "Failed to ip addr flush ${dev}"
151
152     case $NETWORK_MODE in
153         bridge)
154             ${BRCTL} setfd "${bridge}" 0                        || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
155             ${BRCTL} addif "${bridge}" "${dev}"                 || logger -t scripts-vif "Failed to brctl addif ${bridge} ${dev}"
156             ;;
157         vswitch)
158             if [ "$TYPE" = "vif" ] ; then
159                 local vif_details=$(handle_vswitch_vif_details)
160             fi
161
162             $vsctl -- --if-exists del-port $dev -- add-port $bridge $dev $vif_details $(xs550_set_internal_network_uuid)
163             ;;
164     esac
165             
166     ${IP} link set "${dev}" up                          || logger -t scripts-vif "Failed to ip link set ${dev} up"
167 }
168
169 remove_from_bridge()
170 {
171     case $NETWORK_MODE in
172         bridge)
173             # Nothing to do
174             ;;
175         vswitch)
176             $vsctl del-port $bridge $dev
177             ;;
178     esac
179 }
180
181 NETWORK_MODE=$(cat /etc/xensource/network.conf)
182 ACTION=$1
183
184 # Older versions of XenServer do not pass in the type as an argument
185 if [[ $# -lt 2 ]]; then
186     TYPE=vif
187 else
188     TYPE=$2
189 fi
190
191 case $NETWORK_MODE in
192     bridge|vswitch) ;;
193     *)
194         logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
195         exit 1
196         ;;
197 esac
198
199 case ${TYPE} in
200     vif)
201         DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
202         DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
203         dev=vif${DOMID}.${DEVID}
204         ;;
205     tap)
206         dev=$INTERFACE
207         DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
208         DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
209         ;;
210     *)  
211         logger -t scripts-vif "unknown interface type ${TYPE}"
212         exit 1
213         ;;
214 esac
215
216 XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
217 HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
218 PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
219
220 logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
221 case "${ACTION}" in
222 online)
223         if [ "${TYPE}" = "vif" ] ; then
224             handle_ethtool rx
225             handle_ethtool tx
226             handle_ethtool sg
227             handle_ethtool tso
228             handle_ethtool ufo
229             handle_ethtool gso
230
231             handle_mtu
232             add_to_bridge
233             handle_promiscuous
234
235             xenstore-write "${HOTPLUG}/vif" "${dev}"
236             xenstore-write "${HOTPLUG}/hotplug" "online"
237
238             # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
239             xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
240         fi
241         ;;
242
243 add)
244         if [ "${TYPE}" = "tap" ] ; then
245             add_to_bridge
246         fi
247         ;;
248
249 remove)
250         if [ "${TYPE}" = "vif" ] ;then
251             xenstore-rm "${HOTPLUG}/hotplug"
252         fi
253         logger -t scripts-vif "${dev} has been removed"
254         remove_from_bridge
255         ;;
256 esac