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