Merge master branch into xs5.7.
[sliver-openvswitch.git] / xenserver / etc_xensource_scripts_vif
1 #!/bin/sh
2
3 # Copyright (C) 2008,2009 Citrix Systems, Inc.
4 # Copyright (C) 2009 Nicira Networks, Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published
8 # by the Free Software Foundation; version 2.1 only. with the special
9 # exception on linking described in file LICENSE.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Lesser General Public License for more details.
15
16 # CA-23900: Warning: when VIFs are added to windows guests with PV drivers the backend vif device is registered,
17 # unregistered and then registered again. This causes the udev event to fire twice and this script runs twice.
18 # Since the first invocation of the script races with the device unregistration, spurious errors are possible
19 # which will be logged but are safe to ignore since the second script invocation should complete the operation.
20 # Note that each script invocation is run synchronously from udev and so the scripts don't race with each other.
21
22 # Keep other-config/ keys in sync with device.ml:vif_udev_keys
23
24 cfg_mod="/usr/bin/ovs-cfg-mod"
25 vsctl="/usr/bin/ovs-vsctl"
26 dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
27 service="/sbin/service"
28 IP="/sbin/ip"
29 vif_on_internal_bridge="/usr/share/vswitch/scripts/vif-on-internal-bridge"
30
31 handle_promiscuous()
32 {
33     local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
34     if [ $? -eq 0 -a -n "${arg}" ] ; then
35         case "${arg}" in 
36             true|on) logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch." ;;
37             *) ;;
38         esac
39     fi
40 }
41
42 handle_ethtool()
43 {
44     local opt=$1
45     local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}" 2>/dev/null)
46     if [ $? -eq 0 -a -n "${arg}" ] ; then
47         case "${arg}" in
48             true|on)   /sbin/ethtool -K "${dev}" "${opt}" on ;;
49             false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
50             *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
51         esac
52     fi
53 }
54
55 handle_mtu()
56 {
57     local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
58     if [ $? -eq 0 -a -n "${mtu}" ]; then
59         echo "${mtu}" > /sys/class/net/${dev}/mtu
60     fi
61 }
62
63 handle_vif_details()
64 {
65     local vif_details=
66     local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
67     if [ -n "${net_uuid}" ] ; then
68         vif_details="$vif_details --add=port.${dev}.net-uuid=${net_uuid}"
69     fi
70
71     local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
72     if [ -n "${address}" ] ; then
73         vif_details="$vif_details --add=port.${dev}.vif-mac=${address}"
74     fi
75
76     local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
77     if [ -n "${vif_uuid}" ] ; then
78         vif_details="$vif_details --add=port.${dev}.vif-uuid=${vif_uuid}"
79     fi
80
81     local vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
82     if [ $? -eq 0 -a -n "${vm}" ] ; then
83         local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
84     fi
85     if [ -n "${vm_uuid}" ] ; then
86         vif_details="$vif_details --add=port.${dev}.vm-uuid=${vm_uuid}"
87     fi
88
89     # vNetManager needs to know the network UUID(s) associated with
90     # each datapath.  Normally interface-reconfigure adds them, but
91     # interface-reconfigure never gets called for internal networks
92     # (xapi does the addbr ioctl internally), so we have to do it
93     # here instead for internal networks.  This is only acceptable
94     # because xapi is lazy about creating internal networks: it
95     # only creates one just before it adds the first vif to it.
96     # There may still be a brief delay between the initial
97     # ovs-vswitchd connection to vNetManager and setting this
98     # configuration variable, but vNetManager can tolerate that.
99     local internal=$(${vif_on_internal_bridge} ${DOMID} ${DEVID})
100     if [ "$internal" = "true" ]; then
101         local bridge=$(xenstore-read "${PRIVATE}/bridge" 2>/dev/null)
102         local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
103         local key="bridge.${bridge}.xs-network-uuids"
104
105         vif_details="$vif_details --del-match=${key}=*"
106         vif_details="$vif_details --add=${key}=${net_uuid}"
107     fi
108
109     echo ${vif_details}
110 }
111
112 add_to_bridge()
113 {
114     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
115     if [ $? -ne 0 -o -z "${address}" ]; then
116         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
117         exit 1
118     fi
119     local bridge=$(xenstore-read "${PRIVATE}/bridge")
120     if [ $? -ne 0 -o -z "${bridge}" ]; then
121         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
122         exit 1
123     fi
124     logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
125
126     local VLAN_ID=$($vsctl br-to-vlan $bridge)
127     local vid=
128     if [ "$VLAN_ID" -ne 0 ] ; then
129         bridge=$($vsctl br-to-parent $bridge)
130         vid="--add=vlan.${vif}.tag=${VLAN_ID}"
131     fi
132
133     if [ "$type" = "vif" ] ; then
134         local vif_details=$(handle_vif_details)
135     fi
136
137     ${IP} link set "${dev}" down                        || logger -t scripts-vif "Failed to ip link set ${dev} down"
138     ${IP} link set "${dev}" arp off                     || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
139     ${IP} link set "${dev}" multicast off               || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
140     ${IP} link set "${dev}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
141     ${IP} addr flush "${dev}"                           || logger -t scripts-vif "Failed to ip addr flush ${dev}"
142
143     $cfg_mod -F /etc/ovs-vswitchd.conf \
144         --del-match="bridge.*.port=${dev}" \
145         --del-match="vlan.${dev}.trunks=*" \
146         --del-match="vlan.${dev}.tags=*" \
147         --del-match="port.${dev}.[!0-9]*" \
148         --add="bridge.$bridge.port=${dev}" \
149         $vid $vif_details -c 
150     $service vswitch reload
151
152     ${IP} link set "${dev}" up                          || logger -t scripts-vif "Failed to ip link set ${dev} up"
153 }
154
155 type=$2
156
157 case ${type} in
158     vif)
159         DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
160         DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
161         dev=vif${DOMID}.${DEVID}
162         ;;
163     tap)
164         dev=$INTERFACE
165         DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
166         DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
167         ;;
168     *)  
169         logger -t scripts-vif "unknown interface type ${type}"
170         exit 1
171         ;;
172 esac
173
174 XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
175 HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
176 PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
177
178 echo Called as "$@" "$DOMID" "$DEVID" | logger -t scripts-vif
179 case "$1" in
180 online)
181         if [ "${type}" = "vif" ] ; then
182             handle_ethtool rx
183             handle_ethtool tx
184             handle_ethtool sg
185             handle_ethtool tso
186             handle_ethtool ufo
187             handle_ethtool gso
188
189             handle_mtu
190             add_to_bridge
191             handle_promiscuous
192
193             xenstore-write "${HOTPLUG}/vif" "${dev}"
194             xenstore-write "${HOTPLUG}/hotplug" "online"
195
196             # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
197             xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
198         fi
199         ;;
200
201 add)
202         if [ "${type}" = "tap" ] ; then
203             add_to_bridge
204         fi
205         ;;
206
207 remove)
208         if [ "${type}" = "vif" ] ;then
209             xenstore-rm "${HOTPLUG}/hotplug"
210         fi
211         logger -t scripts-vif "${dev} has been removed"
212         $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
213             --del-match="bridge.*.port=${dev}" \
214             --del-match="vlan.${dev}.trunks=*" \
215             --del-match="vlan.${dev}.tag=*" \
216             --del-match="port.${dev}.[!0-9]*" -c
217         $service vswitch reload
218         ;;
219 esac