configure: Silence check for broken strtok_r().
[sliver-openvswitch.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 cfg_mod="/usr/bin/ovs-cfg-mod"
27 vsctl="/usr/bin/ovs-vsctl"
28 service="/sbin/service"
29
30 handle_promiscuous()
31 {
32     local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
33     if [ $? -eq 0 -a -n "${arg}" ] ; then
34         case $NETWORK_MODE in
35             bridge)
36                 case "${arg}" in 
37                     true|on) echo 1 > /sys/class/net/${dev}/brport/promisc ;;
38                     *) echo 0 > /sys/class/net/${dev}/brport/promisc ;;
39                 esac
40                 ;;
41             vswitch)
42                 logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch."
43                 ;;
44         esac
45     fi
46 }
47
48 handle_ethtool()
49 {
50     local opt=$1
51     local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}" 2>/dev/null)
52     if [ $? -eq 0 -a -n "${arg}" ] ; then
53         case "${arg}" in
54             true|on)   /sbin/ethtool -K "${dev}" "${opt}" on ;;
55             false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
56             *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
57         esac
58     fi
59 }
60
61 handle_mtu()
62 {
63     local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
64     if [ $? -eq 0 -a -n "${mtu}" ]; then
65         echo "${mtu}" > /sys/class/net/${dev}/mtu
66     fi
67 }
68
69 handle_vswitch_vif_details()
70 {
71     local vif_details=
72     local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
73     if [ -n "${net_uuid}" ] ; then
74         vif_details="$vif_details --add=port.${dev}.net-uuid=${net_uuid}"
75     fi
76
77     local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
78     if [ -n "${address}" ] ; then
79         vif_details="$vif_details --add=port.${dev}.vif-mac=${address}"
80     fi
81
82     local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
83     if [ -n "${vif_uuid}" ] ; then
84         vif_details="$vif_details --add=port.${dev}.vif-uuid=${vif_uuid}"
85     fi
86
87     local vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
88     if [ $? -eq 0 -a -n "${vm}" ] ; then
89         local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
90     fi
91     if [ -n "${vm_uuid}" ] ; then
92         vif_details="$vif_details --add=port.${dev}.vm-uuid=${vm_uuid}"
93     fi
94     echo ${vif_details}
95 }
96
97 add_to_bridge()
98 {
99     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
100     if [ $? -ne 0 -o -z "${address}" ]; then
101         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
102         exit 1
103     fi
104     local bridge=$(xenstore-read "${PRIVATE}/bridge")
105     if [ $? -ne 0 -o -z "${bridge}" ]; then
106         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
107         exit 1
108     fi
109     logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
110
111     ${IP} link set "${dev}" down                        || logger -t scripts-vif "Failed to ip link set ${dev} down"
112     ${IP} link set "${dev}" arp off                     || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
113     ${IP} link set "${dev}" multicast off               || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
114     ${IP} link set "${dev}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
115     ${IP} addr flush "${dev}"                           || logger -t scripts-vif "Failed to ip addr flush ${dev}"
116
117     case $NETWORK_MODE in
118         bridge)
119             ${BRCTL} setfd "${bridge}" 0                        || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
120             ${BRCTL} addif "${bridge}" "${dev}"                 || logger -t scripts-vif "Failed to brctl addif ${bridge} ${dev}"
121             ;;
122         vswitch)
123             local VLAN_ID=$($vsctl br-to-vlan $bridge)
124             local vid=
125             if [ "$VLAN_ID" -ne 0 ] ; then
126                 bridge=$($vsctl br-to-parent $bridge)
127                 vid="--add=vlan.${dev}.tag=${VLAN_ID}"
128             fi
129
130             if [ "$TYPE" = "vif" ] ; then
131                 local vif_details=$(handle_vswitch_vif_details)
132             fi
133
134             $cfg_mod -F /etc/ovs-vswitchd.conf \
135                 --del-match="bridge.*.port=${dev}" \
136                 --del-match="vlan.${dev}.trunks=*" \
137                 --del-match="vlan.${dev}.tag=*" \
138                 --del-match="port.${dev}.[!0-9]*" \
139                 --add="bridge.$bridge.port=${dev}" \
140                 $vid $vif_details -c 
141             $service vswitch reload
142             ;;
143     esac
144             
145     ${IP} link set "${dev}" up                          || logger -t scripts-vif "Failed to ip link set ${dev} up"
146 }
147
148 remove_from_bridge()
149 {
150     case $NETWORK_MODE in
151         bridge)
152             # Nothing to do
153             ;;
154         vswitch)
155             $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
156                 --del-match="bridge.*.port=${dev}" \
157                 --del-match="vlan.${dev}.trunks=*" \
158                 --del-match="vlan.${dev}.tag=*" \
159                 --del-match="port.${dev}.[!0-9]*" -c
160             $service vswitch reload
161             ;;
162     esac
163 }
164
165 NETWORK_MODE=$(cat /etc/xensource/network.conf)
166 ACTION=$1
167 TYPE=$2
168
169 case $NETWORK_MODE in
170     bridge|vswitch) ;;
171     *)
172         logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
173         exit 1
174         ;;
175 esac
176
177 case ${TYPE} in
178     vif)
179         DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
180         DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
181         dev=vif${DOMID}.${DEVID}
182         ;;
183     tap)
184         dev=$INTERFACE
185         DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
186         DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
187         ;;
188     *)  
189         logger -t scripts-vif "unknown interface type ${TYPE}"
190         exit 1
191         ;;
192 esac
193
194 XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
195 HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
196 PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
197
198 logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
199 case "${ACTION}" in
200 online)
201         if [ "${TYPE}" = "vif" ] ; then
202             handle_ethtool rx
203             handle_ethtool tx
204             handle_ethtool sg
205             handle_ethtool tso
206             handle_ethtool ufo
207             handle_ethtool gso
208
209             handle_mtu
210             add_to_bridge
211             handle_promiscuous
212
213             xenstore-write "${HOTPLUG}/vif" "${dev}"
214             xenstore-write "${HOTPLUG}/hotplug" "online"
215
216             # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
217             xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
218         fi
219         ;;
220
221 add)
222         if [ "${TYPE}" = "tap" ] ; then
223             add_to_bridge
224         fi
225         ;;
226
227 remove)
228         if [ "${TYPE}" = "vif" ] ;then
229             xenstore-rm "${HOTPLUG}/hotplug"
230         fi
231         logger -t scripts-vif "${dev} has been removed"
232         remove_from_bridge
233         ;;
234 esac