xenserver: Send VIF details to controller
[sliver-openvswitch.git] / xenserver / etc_xensource_scripts_vif
1 #!/bin/sh
2
3 # This file is based on /etc/xensource/script/vif from Citrix XenServer 5.0.0.
4 # The original file did not contain a copyright notice or license statement.
5 #
6 # Copyright (C) 2009 Nicira Networks, Inc.
7
8 # CA-23900: Warning: when VIFs are added to windows guests with PV drivers the backend vif device is registered,
9 # unregistered and then registered again. This causes the udev event to fire twice and this script runs twice.
10 # Since the first invocation of the script races with the device unregistration, spurious errors are possible
11 # which will be logged but are safe to ignore since the second script invocation should complete the operation.
12 # Note that each script invocation is run synchronously from udev and so the scripts don't race with each other.
13
14 # Keep other-config/ keys in sync with device.ml:vif_udev_keys
15
16 cfg_mod="/root/vswitch/bin/ovs-cfg-mod"
17 dump_vif_details="/root/vswitch/scripts/dump-vif-details"
18 service="/sbin/service"
19
20 TYPE=`echo ${XENBUS_PATH} | cut -f 2 -d '/'`
21 DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
22 DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
23
24 XAPI=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
25 HOTPLUG=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
26 PRIVATE=/xapi/${DOMID}/private/${TYPE}/${DEVID}
27 BRCTL=/usr/sbin/brctl
28 IP=/sbin/ip
29
30
31 handle_promiscuous()
32 {
33     local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous")
34     if [ $? -eq 0 -a -n "${arg}" ] ; then
35         case "${arg}" in 
36             true|on) echo 1 > /sys/class/net/${vif}/brport/promisc ;;
37             *) echo 0 > /sys/class/net/${vif}/brport/promisc ;;
38         esac
39     fi
40 }
41
42 handle_ethtool()
43 {
44     local opt=$1
45     local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}")
46     if [ $? -eq 0 -a -n "${arg}" ] ; then
47         case "${arg}" in
48             true|on)   /sbin/ethtool -K "${vif}" "${opt}" on ;;
49             false|off) /sbin/ethtool -K "${vif}" "${opt}" off ;;
50             *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${vif}/${VIFUUID}" ;;
51         esac
52     fi
53 }
54
55 handle_mtu()
56 {
57     local mtu=$(xenstore-read "${PRIVATE}/MTU")
58     if [ $? -eq 0 -a -n "${mtu}" ]; then
59         echo "${mtu}" > /sys/class/net/${vif}/mtu
60     fi
61 }
62
63 add_to_bridge()
64 {
65     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
66     if [ $? -ne 0 -o -z "${address}" ]; then
67         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
68     fi
69     local bridge=$(xenstore-read "${PRIVATE}/bridge")
70     if [ $? -ne 0 -o -z "${bridge}" ]; then
71         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
72     fi
73     logger -t scripts-vif "Adding ${vif} to ${bridge} with address ${address}"
74
75     vid=
76     if [ -e "/etc/openvswitch/br-$bridge" ]; then
77         . "/etc/openvswitch/br-$bridge"
78         if [ -n "$VLAN_SLAVE" -a -n "$VLAN_VID" ]; then
79             bridge=$VLAN_SLAVE
80             vid="--add=vlan.$vif.tag=$VLAN_VID"
81         fi
82     fi
83
84     ${IP} link set "${vif}" down                        || logger -t scripts-vif "Failed to ip link set ${vif} down"
85     ${IP} link set "${vif}" arp off                     || logger -t scripts-vif "Failed to ip link set ${vif} arp off"
86     ${IP} link set "${vif}" multicast off               || logger -t scripts-vif "Failed to ip link set ${vif} multicast off"
87     ${IP} link set "${vif}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${vif} address ${address}"
88     ${IP} addr flush "${vif}"                           || logger -t scripts-vif "Failed to ip addr flush ${vif}"
89
90     local vif_details=$($dump_vif_details $DOMID $DEVID)
91     if [ $? -ne 0 -o -z "${vif_details}" ]; then
92             logger -t scripts-vif "Failed to retrieve vif details for vswitch"
93     fi
94
95     $cfg_mod -F /etc/ovs-vswitchd.conf \
96         --del-match="bridge.*.port=$vif" \
97         --del-match="vlan.$vif.[!0-9]*" \
98         --del-match="port.$vif.[!0-9]*" \
99         --add="bridge.$bridge.port=$vif" \
100         $vid $vif_details -c >/tmp/j
101     $service vswitch reload
102
103     ${IP} link set "${vif}" up                          || logger -t scripts-vif "Failed to ip link set ${vif} up"
104 }
105
106 echo Called as "$@" "$TYPE" "$DOMID" "$DEVID" | logger -t scripts-vif
107 case "$1" in
108 online)
109         handle_ethtool rx
110         handle_ethtool tx
111         handle_ethtool sg
112         handle_ethtool tso
113         handle_ethtool ufo
114         handle_ethtool gso
115
116         handle_mtu
117         add_to_bridge
118         handle_promiscuous
119
120         xenstore-write "${HOTPLUG}/vif" "${vif}"
121         xenstore-write "${HOTPLUG}/hotplug" "online"
122
123         # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
124         xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
125
126         ;;
127 remove)
128         xenstore-rm "${HOTPLUG}/hotplug"
129         vif=vif${DOMID}.${DEVID}
130         logger -t scripts-vif "${vif} has been removed"
131         $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
132             --del-match="bridge.*.port=${vif}" \
133             --del-match="vlan.${vif}.[!0-9]*" \
134             --del-match="port.${vif}.[!0-9]*" -c
135         ;;
136 esac