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