netdev-dpdk: fix mbuf leaks
[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             openvswitch)
40                 logger -t script-vif "${dev}: Promiscuous ports are not supported via Open 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 vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
81     if [ $? -eq 0 -a -n "${vm}" ] ; then
82         local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
83     fi
84     if [ -n "${vm_uuid}" ] ; then
85         set_vif_external_id "xs-vm-uuid" "${vm_uuid}"
86     fi
87
88     local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
89     if [ -n "${vif_uuid}" ] ; then
90         set_vif_external_id "xs-vif-uuid" "${vif_uuid}"
91     fi
92
93     local vif_details=
94     local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
95     if [ -n "${net_uuid}" ] ; then
96         set_vif_external_id "xs-network-uuid" "${net_uuid}"
97     fi
98     local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
99     if [ -n "${address}" ] ; then
100         set_vif_external_id "attached-mac" "${address}"
101     fi
102 }
103
104 add_to_bridge()
105 {
106     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
107     if [ $? -ne 0 -o -z "${address}" ]; then
108         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
109         exit 1
110     fi
111     local bridge=$(xenstore-read "${PRIVATE}/bridge")
112     if [ $? -ne 0 -o -z "${bridge}" ]; then
113         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
114         exit 1
115     fi
116     logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
117
118     ${IP} link set "${dev}" down                        || logger -t scripts-vif "Failed to ip link set ${dev} down"
119     ${IP} link set "${dev}" arp off                     || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
120     ${IP} link set "${dev}" multicast off               || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
121     ${IP} link set "${dev}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
122     ${IP} addr flush "${dev}"                           || logger -t scripts-vif "Failed to ip addr flush ${dev}"
123
124     case $NETWORK_MODE in
125     bridge)
126         ${BRCTL} setfd "${bridge}" 0                    || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
127         ${BRCTL} addif "${bridge}" "${dev}"             || logger -t scripts-vif "Failed to brctl addif ${bridge} ${dev}"
128         ;;
129     openvswitch)
130         if [ "$TYPE" = "vif" ] ; then
131             local vif_details=$(handle_vswitch_vif_details $bridge)
132         fi
133
134         $vsctl --timeout=30 -- --if-exists del-port $dev -- add-port $bridge $dev $vif_details
135         ;;
136     esac
137         
138     ${IP} link set "${dev}" up                          || logger -t scripts-vif "Failed to ip link set ${dev} up"
139 }
140
141 remove_from_bridge()
142 {
143     case $NETWORK_MODE in
144     bridge)
145         # Nothing to do
146         ;;
147     openvswitch)
148         $vsctl --timeout=30 -- del-port $dev
149         ;;
150     esac
151 }
152
153 call_hook_script() {
154     local domid=$1
155     local action=$2
156     # Call the VIF hotplug hook if present
157     if [ -x /etc/xapi.d/vif-hotplug ]; then
158         local vm=$(xenstore-read "/local/domain/$domid/vm" 2>/dev/null)
159         if [ $? -eq 0 -a -n "${vm}" ] ; then
160             local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
161         fi
162         if [ -n "${vm_uuid}" ] ; then
163             logger -t scripts-vif "VM UUID ${vm_uuid}"
164         fi
165
166         local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
167         if [ -n "${vif_uuid}" ] ; then
168             logger -t scripts-vif "VIF UUID ${vif_uuid}"
169         fi
170         if [ -n "${vif_uuid}" -a -n "${vm_uuid}" ] ; then
171             logger -t scripts-vif "Calling VIF hotplug hook for VM ${vm_uuid}, VIF ${vif_uuid}"
172             /etc/xapi.d/vif-hotplug -action "${action}" -vifuuid "${vif_uuid}" -vmuuid "${vm_uuid}"
173         fi
174     fi
175 }
176
177 NETWORK_MODE=$(cat /etc/xensource/network.conf)
178 ACTION=$1
179
180 # Older versions of XenServer do not pass in the type as an argument
181 if [[ $# -lt 2 ]]; then
182     TYPE=vif
183 else
184     TYPE=$2
185 fi
186
187 case $NETWORK_MODE in
188     bridge|openvswitch) ;;
189     vswitch) NETWORK_MODE=openvswitch ;;
190     *)
191         logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
192         exit 1
193         ;;
194 esac
195
196 case ${TYPE} in
197     vif)
198         if [ -z ${XENBUS_PATH} ]; then
199             DOMID=$3
200             DEVID=$4
201         else
202             DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
203             DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
204         fi
205         dev=vif${DOMID}.${DEVID}
206         ;;
207     tap)
208         dev=$INTERFACE
209         DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
210         DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
211         ;;
212     *)  
213         logger -t scripts-vif "unknown interface type ${TYPE}"
214         exit 1
215         ;;
216 esac
217
218 XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
219 HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
220 PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
221
222 logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
223 case "${ACTION}" in
224 online)
225     if [ "${TYPE}" = "vif" ] ; then
226         handle_ethtool rx
227         handle_ethtool tx
228         handle_ethtool sg
229         handle_ethtool tso
230         handle_ethtool ufo
231         handle_ethtool gso
232
233         handle_mtu
234         add_to_bridge
235         handle_promiscuous
236
237         xenstore-write "${HOTPLUG}/vif" "${dev}"
238         xenstore-write "${HOTPLUG}/hotplug" "online"
239
240         # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
241         xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
242         call_hook_script $DOMID "${ACTION}"
243     fi
244     ;;
245
246 add)
247     if [ "${TYPE}" = "tap" ] ; then
248         add_to_bridge
249     fi
250     ;;
251
252 remove)
253     if [ "${TYPE}" = "vif" ] ;then
254         xenstore-rm "${HOTPLUG}/hotplug"
255         call_hook_script $DOMID "${ACTION}"
256     fi
257     logger -t scripts-vif "${dev} has been removed"
258     remove_from_bridge
259     ;;
260
261 move)
262     if [ "${TYPE}" = "vif" ] ;then
263         add_to_bridge
264     fi
265 esac