3 # Copyright (C) 2008,2009 Citrix Systems, Inc.
4 # Copyright (C) 2009 Nicira Networks, Inc.
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.
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.
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.
22 # Keep other-config/ keys in sync with device.ml:vif_udev_keys
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"
29 vif_on_internal_bridge="/usr/share/vswitch/scripts/vif-on-internal-bridge"
33 local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
34 if [ $? -eq 0 -a -n "${arg}" ] ; then
36 true|on) logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch." ;;
45 local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}" 2>/dev/null)
46 if [ $? -eq 0 -a -n "${arg}" ] ; then
48 true|on) /sbin/ethtool -K "${dev}" "${opt}" on ;;
49 false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
50 *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
57 local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
58 if [ $? -eq 0 -a -n "${mtu}" ]; then
59 echo "${mtu}" > /sys/class/net/${dev}/mtu
66 local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
67 if [ -n "${net_uuid}" ] ; then
68 vif_details="$vif_details --add=port.${dev}.net-uuid=${net_uuid}"
71 local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
72 if [ -n "${address}" ] ; then
73 vif_details="$vif_details --add=port.${dev}.vif-mac=${address}"
76 local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
77 if [ -n "${vif_uuid}" ] ; then
78 vif_details="$vif_details --add=port.${dev}.vif-uuid=${vif_uuid}"
81 local vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
82 if [ $? -eq 0 -a -n "${vm}" ] ; then
83 local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
85 if [ -n "${vm_uuid}" ] ; then
86 vif_details="$vif_details --add=port.${dev}.vm-uuid=${vm_uuid}"
89 # vNetManager needs to know the network UUID(s) associated with
90 # each datapath. Normally interface-reconfigure adds them, but
91 # interface-reconfigure never gets called for internal networks
92 # (xapi does the addbr ioctl internally), so we have to do it
93 # here instead for internal networks. This is only acceptable
94 # because xapi is lazy about creating internal networks: it
95 # only creates one just before it adds the first vif to it.
96 # There may still be a brief delay between the initial
97 # ovs-vswitchd connection to vNetManager and setting this
98 # configuration variable, but vNetManager can tolerate that.
99 local internal=$(${vif_on_internal_bridge} ${DOMID} ${DEVID})
100 if [ "$internal" = "true" ]; then
101 local bridge=$(xenstore-read "${PRIVATE}/bridge" 2>/dev/null)
102 local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
103 local key="bridge.${bridge}.xs-network-uuids"
105 vif_details="$vif_details --del-match=${key}=*"
106 vif_details="$vif_details --add=${key}=${net_uuid}"
114 local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
115 if [ $? -ne 0 -o -z "${address}" ]; then
116 logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
119 local bridge=$(xenstore-read "${PRIVATE}/bridge")
120 if [ $? -ne 0 -o -z "${bridge}" ]; then
121 logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
124 logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
126 local VLAN_ID=$($vsctl br-to-vlan $bridge)
128 if [ "$VLAN_ID" -ne 0 ] ; then
129 bridge=$($vsctl br-to-parent $bridge)
130 vid="--add=vlan.${vif}.tag=${VLAN_ID}"
133 if [ "$type" = "vif" ] ; then
134 local vif_details=$(handle_vif_details)
137 ${IP} link set "${dev}" down || logger -t scripts-vif "Failed to ip link set ${dev} down"
138 ${IP} link set "${dev}" arp off || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
139 ${IP} link set "${dev}" multicast off || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
140 ${IP} link set "${dev}" address "${address}" || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
141 ${IP} addr flush "${dev}" || logger -t scripts-vif "Failed to ip addr flush ${dev}"
143 $cfg_mod -F /etc/ovs-vswitchd.conf \
144 --del-match="bridge.*.port=${dev}" \
145 --del-match="vlan.${dev}.trunks=*" \
146 --del-match="vlan.${dev}.tags=*" \
147 --del-match="port.${dev}.[!0-9]*" \
148 --add="bridge.$bridge.port=${dev}" \
150 $service vswitch reload
152 ${IP} link set "${dev}" up || logger -t scripts-vif "Failed to ip link set ${dev} up"
159 DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
160 DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
161 dev=vif${DOMID}.${DEVID}
165 DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
166 DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
169 logger -t scripts-vif "unknown interface type ${type}"
174 XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
175 HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
176 PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
178 echo Called as "$@" "$DOMID" "$DEVID" | logger -t scripts-vif
181 if [ "${type}" = "vif" ] ; then
193 xenstore-write "${HOTPLUG}/vif" "${dev}"
194 xenstore-write "${HOTPLUG}/hotplug" "online"
196 # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
197 xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
202 if [ "${type}" = "tap" ] ; then
208 if [ "${type}" = "vif" ] ;then
209 xenstore-rm "${HOTPLUG}/hotplug"
211 logger -t scripts-vif "${dev} has been removed"
212 $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
213 --del-match="bridge.*.port=${dev}" \
214 --del-match="vlan.${dev}.trunks=*" \
215 --del-match="vlan.${dev}.tag=*" \
216 --del-match="port.${dev}.[!0-9]*" -c
217 $service vswitch reload