]> git.proxmox.com Git - mirror_ovs.git/blob - xenserver/etc_xensource_scripts_vif
Merge "master" into "next".
[mirror_ovs.git] / xenserver / etc_xensource_scripts_vif
1 #!/bin/sh
2
3 # Copyright (C) 2008,2009 Citrix Systems, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation; version 2.1 only. with the special
8 # exception on linking described in file LICENSE.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14
15 # CA-23900: Warning: when VIFs are added to windows guests with PV drivers the backend vif device is registered,
16 # unregistered and then registered again. This causes the udev event to fire twice and this script runs twice.
17 # Since the first invocation of the script races with the device unregistration, spurious errors are possible
18 # which will be logged but are safe to ignore since the second script invocation should complete the operation.
19 # Note that each script invocation is run synchronously from udev and so the scripts don't race with each other.
20
21 # Keep other-config/ keys in sync with device.ml:vif_udev_keys
22
23 BRCTL="/usr/sbin/brctl"
24 IP="/sbin/ip"
25
26 vsctl="/usr/bin/ovs-vsctl"
27 dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
28
29 handle_promiscuous()
30 {
31 local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
32 if [ $? -eq 0 -a -n "${arg}" ] ; then
33 case $NETWORK_MODE in
34 bridge)
35 case "${arg}" in
36 true|on) echo 1 > /sys/class/net/${dev}/brport/promisc ;;
37 *) echo 0 > /sys/class/net/${dev}/brport/promisc ;;
38 esac
39 ;;
40 vswitch)
41 logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch."
42 ;;
43 esac
44 fi
45 }
46
47 handle_ethtool()
48 {
49 local opt=$1
50 local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}" 2>/dev/null)
51 if [ $? -eq 0 -a -n "${arg}" ] ; then
52 case "${arg}" in
53 true|on) /sbin/ethtool -K "${dev}" "${opt}" on ;;
54 false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
55 *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
56 esac
57 fi
58 }
59
60 handle_mtu()
61 {
62 local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
63 if [ $? -eq 0 -a -n "${mtu}" ]; then
64 echo "${mtu}" > /sys/class/net/${dev}/mtu
65 fi
66 }
67
68 add_to_bridge()
69 {
70 local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
71 if [ $? -ne 0 -o -z "${address}" ]; then
72 logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
73 exit 1
74 fi
75 local bridge=$(xenstore-read "${PRIVATE}/bridge")
76 if [ $? -ne 0 -o -z "${bridge}" ]; then
77 logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
78 exit 1
79 fi
80 logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
81
82 ${IP} link set "${dev}" down || logger -t scripts-vif "Failed to ip link set ${dev} down"
83 ${IP} link set "${dev}" arp off || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
84 ${IP} link set "${dev}" multicast off || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
85 ${IP} link set "${dev}" address "${address}" || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
86 ${IP} addr flush "${dev}" || logger -t scripts-vif "Failed to ip addr flush ${dev}"
87
88 case $NETWORK_MODE in
89 bridge)
90 ${BRCTL} setfd "${bridge}" 0 || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
91 ${BRCTL} addif "${bridge}" "${dev}" || logger -t scripts-vif "Failed to brctl addif ${bridge} ${dev}"
92 ;;
93 vswitch)
94 local vif_details=$($dump_vif_details $DOMID $DEVID)
95 if [ $? -ne 0 -o -z "${vif_details}" ]; then
96 logger -t scripts-vif "Failed to retrieve vif details for vswitch"
97 fi
98
99 $vsctl add-port $bridge $dev $vif_details
100 ;;
101 esac
102
103 ${IP} link set "${dev}" up || logger -t scripts-vif "Failed to ip link set ${dev} up"
104 }
105
106 remove_from_bridge()
107 {
108 case $NETWORK_MODE in
109 bridge)
110 # Nothing to do
111 ;;
112 vswitch)
113 $vsctl del-port $bridge $dev
114 ;;
115 esac
116 }
117
118 NETWORK_MODE=$(cat /etc/xensource/network.conf)
119 ACTION=$1
120
121 # Older versions of XenServer do not pass in the type as an argument
122 if [[ $# -lt 2 ]]; then
123 TYPE=vif
124 else
125 TYPE=$2
126 fi
127
128 case $NETWORK_MODE in
129 bridge|vswitch) ;;
130 *)
131 logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
132 exit 1
133 ;;
134 esac
135
136 case ${TYPE} in
137 vif)
138 DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
139 DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
140 dev=vif${DOMID}.${DEVID}
141 ;;
142 tap)
143 dev=$INTERFACE
144 DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
145 DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
146 ;;
147 *)
148 logger -t scripts-vif "unknown interface type ${TYPE}"
149 exit 1
150 ;;
151 esac
152
153 XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
154 HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
155 PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
156
157 logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
158 case "${ACTION}" in
159 online)
160 if [ "${TYPE}" = "vif" ] ; then
161 handle_ethtool rx
162 handle_ethtool tx
163 handle_ethtool sg
164 handle_ethtool tso
165 handle_ethtool ufo
166 handle_ethtool gso
167
168 handle_mtu
169 add_to_bridge
170 handle_promiscuous
171
172 xenstore-write "${HOTPLUG}/vif" "${dev}"
173 xenstore-write "${HOTPLUG}/hotplug" "online"
174
175 # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
176 xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
177 fi
178 ;;
179
180 add)
181 if [ "${TYPE}" = "tap" ] ; then
182 add_to_bridge
183 fi
184 ;;
185
186 remove)
187 if [ "${TYPE}" = "vif" ] ;then
188 xenstore-rm "${HOTPLUG}/hotplug"
189 fi
190 logger -t scripts-vif "${dev} has been removed"
191 remove_from_bridge
192 ;;
193 esac