Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[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 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) echo 1 > /sys/class/net/${vif}/brport/promisc ;;
36             *) echo 0 > /sys/class/net/${vif}/brport/promisc ;;
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     vid=
75     if [ -e "/etc/openvswitch/br-$bridge" ]; then
76         . "/etc/openvswitch/br-$bridge"
77         if [ -n "$VLAN_SLAVE" -a -n "$VLAN_VID" ]; then
78             bridge=$VLAN_SLAVE
79             vid="--add=vlan.$vif.tag=$VLAN_VID"
80         fi
81     fi
82
83     ${IP} link set "${vif}" down                        || logger -t scripts-vif "Failed to ip link set ${vif} down"
84     ${IP} link set "${vif}" arp off                     || logger -t scripts-vif "Failed to ip link set ${vif} arp off"
85     ${IP} link set "${vif}" multicast off               || logger -t scripts-vif "Failed to ip link set ${vif} multicast off"
86     ${IP} link set "${vif}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${vif} address ${address}"
87     ${IP} addr flush "${vif}"                           || logger -t scripts-vif "Failed to ip addr flush ${vif}"
88
89     $cfg_mod -F /etc/ovs-vswitchd.conf \
90         --del-match="bridge.*.port=$vif" \
91         --del-match="vlan.$vif.[!0-9]*" \
92         --del-match="port.$vif.[!0-9]*" \
93         --add="bridge.$bridge.port=$vif" \
94         $vid -c
95     $service vswitch reload
96
97     ${IP} link set "${vif}" up                          || logger -t scripts-vif "Failed to ip link set ${vif} up"
98 }
99
100 echo Called as "$@" "$TYPE" "$DOMID" "$DEVID" | logger -t scripts-vif
101 case "$1" in
102 online)
103         handle_ethtool rx
104         handle_ethtool tx
105         handle_ethtool sg
106         handle_ethtool tso
107         handle_ethtool ufo
108         handle_ethtool gso
109
110         handle_mtu
111         add_to_bridge
112         handle_promiscuous
113
114         xenstore-write "${HOTPLUG}/vif" "${vif}"
115         xenstore-write "${HOTPLUG}/hotplug" "online"
116
117         # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
118         xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
119
120         ;;
121 remove)
122         xenstore-rm "${HOTPLUG}/hotplug"
123         vif=vif${DOMID}.${DEVID}
124         logger -t scripts-vif "${vif} has been removed"
125         $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
126             --del-match="bridge.*.port=${vif}" \
127             --del-match="vlan.${vif}.[!0-9]*" \
128             --del-match="port.${vif}.[!0-9]*" -c
129         ;;
130 esac