Merge citrix branch into master.
[cascardo/ovs.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
19 TYPE=`echo ${XENBUS_PATH} | cut -f 2 -d '/'`
20 DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
21 DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
22
23 XAPI=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
24 HOTPLUG=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
25 PRIVATE=/xapi/${DOMID}/private/${TYPE}/${DEVID}
26 BRCTL=/usr/sbin/brctl
27 IP=/sbin/ip
28
29
30 handle_promiscuous()
31 {
32     local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous")
33     if [ $? -eq 0 -a -n "${arg}" ] ; then
34         case "${arg}" in 
35             true|on) logger -t script-vif "${vif}: Promiscuous ports are not supported via vSwitch." ;;
36             *) ;;
37         esac
38     fi
39 }
40
41 handle_ethtool()
42 {
43     local opt=$1
44     local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}")
45     if [ $? -eq 0 -a -n "${arg}" ] ; then
46         case "${arg}" in
47             true|on)   /sbin/ethtool -K "${vif}" "${opt}" on ;;
48             false|off) /sbin/ethtool -K "${vif}" "${opt}" off ;;
49             *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${vif}/${VIFUUID}" ;;
50         esac
51     fi
52 }
53
54 handle_mtu()
55 {
56     local mtu=$(xenstore-read "${PRIVATE}/MTU")
57     if [ $? -eq 0 -a -n "${mtu}" ]; then
58         echo "${mtu}" > /sys/class/net/${vif}/mtu
59     fi
60 }
61
62 add_to_bridge()
63 {
64     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
65     if [ $? -ne 0 -o -z "${address}" ]; then
66         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
67     fi
68     local bridge=$(xenstore-read "${PRIVATE}/bridge")
69     if [ $? -ne 0 -o -z "${bridge}" ]; then
70         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
71     fi
72     logger -t scripts-vif "Adding ${vif} to ${bridge} with address ${address}"
73
74     local VLAN_ID=$($vsctl br-to-vlan $bridge)
75     local vid=
76     if [ "$VLAN_ID" -ne 0 ] ; then
77         bridge=$($vsctl br-to-parent $bridge)
78         vid="--add=vlan.${vif}.tag=${VLAN_ID}"
79     fi
80
81     ${IP} link set "${vif}" down                        || logger -t scripts-vif "Failed to ip link set ${vif} down"
82     ${IP} link set "${vif}" arp off                     || logger -t scripts-vif "Failed to ip link set ${vif} arp off"
83     ${IP} link set "${vif}" multicast off               || logger -t scripts-vif "Failed to ip link set ${vif} multicast off"
84     ${IP} link set "${vif}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${vif} address ${address}"
85     ${IP} addr flush "${vif}"                           || logger -t scripts-vif "Failed to ip addr flush ${vif}"
86
87     local vif_details=$($dump_vif_details $DOMID $DEVID)
88     if [ $? -ne 0 -o -z "${vif_details}" ]; then
89             logger -t scripts-vif "Failed to retrieve vif details for vswitch"
90     fi
91
92     $cfg_mod -F /etc/ovs-vswitchd.conf \
93         --del-match="bridge.*.port=$vif" \
94         --del-match="vlan.$vif.[!0-9]*" \
95         --del-match="port.$vif.[!0-9]*" \
96         --add="bridge.$bridge.port=$vif" \
97         $vid $vif_details -c 
98     $service vswitch reload
99
100     ${IP} link set "${vif}" up                          || logger -t scripts-vif "Failed to ip link set ${vif} up"
101 }
102
103 echo Called as "$@" "$TYPE" "$DOMID" "$DEVID" | logger -t scripts-vif
104 case "$1" in
105 online)
106         handle_ethtool rx
107         handle_ethtool tx
108         handle_ethtool sg
109         handle_ethtool tso
110         handle_ethtool ufo
111         handle_ethtool gso
112
113         handle_mtu
114         add_to_bridge
115         handle_promiscuous
116
117         xenstore-write "${HOTPLUG}/vif" "${vif}"
118         xenstore-write "${HOTPLUG}/hotplug" "online"
119
120         # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
121         xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
122
123         ;;
124 remove)
125         xenstore-rm "${HOTPLUG}/hotplug"
126         vif=vif${DOMID}.${DEVID}
127         logger -t scripts-vif "${vif} has been removed"
128         $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
129             --del-match="bridge.*.port=${vif}" \
130             --del-match="vlan.${vif}.[!0-9]*" \
131             --del-match="port.${vif}.[!0-9]*" -c
132         $service vswitch reload
133         ;;
134 esac