033e001cba1578a8d340afc5ab866a738b3db928
[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
20 handle_promiscuous()
21 {
22     local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
23     if [ $? -eq 0 -a -n "${arg}" ] ; then
24         case "${arg}" in 
25             true|on) logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch." ;;
26             *) ;;
27         esac
28     fi
29 }
30
31 handle_ethtool()
32 {
33     local opt=$1
34     local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}" 2>/dev/null)
35     if [ $? -eq 0 -a -n "${arg}" ] ; then
36         case "${arg}" in
37             true|on)   /sbin/ethtool -K "${dev}" "${opt}" on ;;
38             false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
39             *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
40         esac
41     fi
42 }
43
44 handle_mtu()
45 {
46     local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
47     if [ $? -eq 0 -a -n "${mtu}" ]; then
48         echo "${mtu}" > /sys/class/net/${dev}/mtu
49     fi
50 }
51
52 handle_vif_details()
53 {
54     local vif_details=
55     local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
56     if [ -n "${net_uuid}" ] ; then
57         vif_details="$vif_details --add=port.${dev}.net-uuid=${net_uuid}"
58     fi
59
60     local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
61     if [ -n "${address}" ] ; then
62         vif_details="$vif_details --add=port.${dev}.vif-mac=${address}"
63     fi
64
65     local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
66     if [ -n "${vif_uuid}" ] ; then
67         vif_details="$vif_details --add=port.${dev}.vif-uuid=${vif_uuid}"
68     fi
69
70     local vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
71     if [ $? -eq 0 -a -n "${vm}" ] ; then
72         local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
73     fi
74     if [ -n "${vm_uuid}" ] ; then
75         vif_details="$vif_details --add=port.${dev}.vm-uuid=${vm_uuid}"
76     fi
77     echo ${vif_details}
78 }
79
80 add_to_bridge()
81 {
82     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
83     if [ $? -ne 0 -o -z "${address}" ]; then
84         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
85         exit 1
86     fi
87     local bridge=$(xenstore-read "${PRIVATE}/bridge")
88     if [ $? -ne 0 -o -z "${bridge}" ]; then
89         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
90         exit 1
91     fi
92     logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
93
94     local VLAN_ID=$($vsctl br-to-vlan $bridge)
95     local vid=
96     if [ "$VLAN_ID" -ne 0 ] ; then
97         bridge=$($vsctl br-to-parent $bridge)
98         vid="--add=vlan.${dev}.tag=${VLAN_ID}"
99     fi
100
101     if [ "$type" = "vif" ] ; then
102         local vif_details=$(handle_vif_details)
103     fi
104
105     ${IP} link set "${dev}" down                        || logger -t scripts-vif "Failed to ip link set ${dev} down"
106     ${IP} link set "${dev}" arp off                     || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
107     ${IP} link set "${dev}" multicast off               || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
108     ${IP} link set "${dev}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
109     ${IP} addr flush "${dev}"                           || logger -t scripts-vif "Failed to ip addr flush ${dev}"
110
111     $cfg_mod -F /etc/ovs-vswitchd.conf \
112         --del-match="bridge.*.port=${dev}" \
113         --del-match="vlan.${dev}.[!0-9]*" \
114         --del-match="port.${dev}.[!0-9]*" \
115         --add="bridge.$bridge.port=${dev}" \
116         $vid $vif_details -c 
117     $service vswitch reload
118
119     ${IP} link set "${dev}" up                          || logger -t scripts-vif "Failed to ip link set ${dev} up"
120 }
121
122 type=$2
123
124 case ${type} in
125     vif)
126         DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
127         DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
128         dev=vif${DOMID}.${DEVID}
129         ;;
130     tap)
131         dev=$INTERFACE
132         DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
133         DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
134         ;;
135     *)  
136         logger -t scripts-vif "unknown interface type ${type}"
137         exit 1
138         ;;
139 esac
140
141 XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
142 HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
143 PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
144
145 echo Called as "$@" "$DOMID" "$DEVID" | logger -t scripts-vif
146 case "$1" in
147 online)
148         if [ "${type}" = "vif" ] ; then
149             handle_ethtool rx
150             handle_ethtool tx
151             handle_ethtool sg
152             handle_ethtool tso
153             handle_ethtool ufo
154             handle_ethtool gso
155
156             handle_mtu
157             add_to_bridge
158             handle_promiscuous
159
160             xenstore-write "${HOTPLUG}/vif" "${dev}"
161             xenstore-write "${HOTPLUG}/hotplug" "online"
162
163             # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
164             xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
165         fi
166         ;;
167
168 add)
169         if [ "${type}" = "tap" ] ; then
170             add_to_bridge
171         fi
172         ;;
173
174 remove)
175         if [ "${type}" = "vif" ] ;then
176             xenstore-rm "${HOTPLUG}/hotplug"
177         fi
178         logger -t scripts-vif "${dev} has been removed"
179         $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
180             --del-match="bridge.*.port=${dev}" \
181             --del-match="vlan.${dev}.[!0-9]*" \
182             --del-match="port.${dev}.[!0-9]*" -c
183         $service vswitch reload
184         ;;
185 esac