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