Merge citrix branch into master.
[sliver-openvswitch.git] / xenserver / etc_xensource_scripts_vif
1 #!/bin/sh
2
3 # Copyright (C) 2008,2009 Citrix Systems, Inc.
4 # Copyright (C) 2009 Nicira Networks, Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published
8 # by the Free Software Foundation; version 2.1 only. with the special
9 # exception on linking described in file LICENSE.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Lesser General Public License for more details.
15
16 # CA-23900: Warning: when VIFs are added to windows guests with PV drivers the backend vif device is registered,
17 # unregistered and then registered again. This causes the udev event to fire twice and this script runs twice.
18 # Since the first invocation of the script races with the device unregistration, spurious errors are possible
19 # which will be logged but are safe to ignore since the second script invocation should complete the operation.
20 # Note that each script invocation is run synchronously from udev and so the scripts don't race with each other.
21
22 # Keep other-config/ keys in sync with device.ml:vif_udev_keys
23
24 cfg_mod="/usr/bin/ovs-cfg-mod"
25 vsctl="/usr/bin/ovs-vsctl"
26 dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
27 service="/sbin/service"
28
29 TYPE=`echo ${XENBUS_PATH} | cut -f 2 -d '/'`
30 DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
31 DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
32
33 XAPI=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
34 HOTPLUG=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
35 PRIVATE=/xapi/${DOMID}/private/${TYPE}/${DEVID}
36 BRCTL=/usr/sbin/brctl
37 IP=/sbin/ip
38
39
40 handle_promiscuous()
41 {
42     local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous")
43     if [ $? -eq 0 -a -n "${arg}" ] ; then
44         case "${arg}" in 
45             true|on) logger -t script-vif "${vif}: Promiscuous ports are not supported via vSwitch." ;;
46             *) ;;
47         esac
48     fi
49 }
50
51 handle_ethtool()
52 {
53     local opt=$1
54     local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}")
55     if [ $? -eq 0 -a -n "${arg}" ] ; then
56         case "${arg}" in
57             true|on)   /sbin/ethtool -K "${vif}" "${opt}" on ;;
58             false|off) /sbin/ethtool -K "${vif}" "${opt}" off ;;
59             *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${vif}/${VIFUUID}" ;;
60         esac
61     fi
62 }
63
64 handle_mtu()
65 {
66     local mtu=$(xenstore-read "${PRIVATE}/MTU")
67     if [ $? -eq 0 -a -n "${mtu}" ]; then
68         echo "${mtu}" > /sys/class/net/${vif}/mtu
69     fi
70 }
71
72 add_to_bridge()
73 {
74     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
75     if [ $? -ne 0 -o -z "${address}" ]; then
76         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
77     fi
78     local bridge=$(xenstore-read "${PRIVATE}/bridge")
79     if [ $? -ne 0 -o -z "${bridge}" ]; then
80         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
81     fi
82     logger -t scripts-vif "Adding ${vif} to ${bridge} with address ${address}"
83
84     local VLAN_ID=$($vsctl br-to-vlan $bridge)
85     local vid=
86     if [ "$VLAN_ID" -ne 0 ] ; then
87         bridge=$($vsctl br-to-parent $bridge)
88         vid="--add=vlan.${vif}.tag=${VLAN_ID}"
89     fi
90
91     ${IP} link set "${vif}" down                        || logger -t scripts-vif "Failed to ip link set ${vif} down"
92     ${IP} link set "${vif}" arp off                     || logger -t scripts-vif "Failed to ip link set ${vif} arp off"
93     ${IP} link set "${vif}" multicast off               || logger -t scripts-vif "Failed to ip link set ${vif} multicast off"
94     ${IP} link set "${vif}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${vif} address ${address}"
95     ${IP} addr flush "${vif}"                           || logger -t scripts-vif "Failed to ip addr flush ${vif}"
96
97     local vif_details=$($dump_vif_details $DOMID $DEVID)
98     if [ $? -ne 0 -o -z "${vif_details}" ]; then
99             logger -t scripts-vif "Failed to retrieve vif details for vswitch"
100     fi
101
102     $cfg_mod -F /etc/ovs-vswitchd.conf \
103         --del-match="bridge.*.port=$vif" \
104         --del-match="vlan.$vif.trunks=*" \
105         --del-match="vlan.$vif.tag=*" \
106         --del-match="port.$vif.[!0-9]*" \
107         --add="bridge.$bridge.port=$vif" \
108         $vid $vif_details -c 
109     $service vswitch reload
110
111     ${IP} link set "${vif}" up                          || logger -t scripts-vif "Failed to ip link set ${vif} up"
112 }
113
114 echo Called as "$@" "$TYPE" "$DOMID" "$DEVID" | logger -t scripts-vif
115 case "$1" in
116 online)
117         handle_ethtool rx
118         handle_ethtool tx
119         handle_ethtool sg
120         handle_ethtool tso
121         handle_ethtool ufo
122         handle_ethtool gso
123
124         handle_mtu
125         add_to_bridge
126         handle_promiscuous
127
128         xenstore-write "${HOTPLUG}/vif" "${vif}"
129         xenstore-write "${HOTPLUG}/hotplug" "online"
130
131         # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
132         xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
133
134         ;;
135 remove)
136         xenstore-rm "${HOTPLUG}/hotplug"
137         vif=vif${DOMID}.${DEVID}
138         logger -t scripts-vif "${vif} has been removed"
139         $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
140             --del-match="bridge.*.port=${vif}" \
141             --del-match="vlan.${vif}.trunks=*" \
142             --del-match="vlan.${vif}.tag=*" \
143             --del-match="port.${vif}.[!0-9]*" -c
144         $service vswitch reload
145         ;;
146 esac