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