mirror of
https://github.com/openvswitch/ovs
synced 2025-10-11 13:57:52 +00:00
xenserver: Update interface-reconfigure and vif integration scripts.
This patch updates the versions of the host integration scripts to what is present in the recent XCP update and the next release of XenServer (5.6). I think it probably works on 5.5 as well but I haven't verified that. These new versions support runtime selection of the Linux bridging vs vswitch through the toolstack. The patch includes the necessary reconfiguration (rewriting /etc/xensource/network.conf) to enable vswitch in the RPM %post stage. The RPM spec file integration is only lightly tested, seems to work for me. This commit is as provided by Ian Campbell, with a few minor tweaks: - Updated xenserver/automake.mk to distribute the added files. - Updated RPM spec file not to complain if the added files do not already exist (since they do not exist on XenServer 5.5.0). - Change location of dbcache back to /var/lib/openvswitch/dbcache so that this can coexist with our other internal builds. (Undoubtedly we will have to adopt the new location chosen by Ian eventually, however.)
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
# Copyright (C) 2009 Nicira Networks, Inc.
|
# Copyright (C) 2009, 2010 Nicira Networks, Inc.
|
||||||
#
|
#
|
||||||
# Copying and distribution of this file, with or without modification,
|
# Copying and distribution of this file, with or without modification,
|
||||||
# are permitted in any medium without royalty provided the copyright
|
# are permitted in any medium without royalty provided the copyright
|
||||||
@@ -6,18 +6,23 @@
|
|||||||
# without warranty of any kind.
|
# without warranty of any kind.
|
||||||
|
|
||||||
EXTRA_DIST += \
|
EXTRA_DIST += \
|
||||||
|
xenserver/LICENSE \
|
||||||
xenserver/README \
|
xenserver/README \
|
||||||
|
xenserver/automake.mk \
|
||||||
xenserver/etc_init.d_vswitch \
|
xenserver/etc_init.d_vswitch \
|
||||||
xenserver/etc_init.d_vswitch-xapi-update \
|
xenserver/etc_init.d_vswitch-xapi-update \
|
||||||
xenserver/etc_logrotate.d_vswitch \
|
xenserver/etc_logrotate.d_vswitch \
|
||||||
xenserver/etc_profile.d_vswitch.sh \
|
xenserver/etc_profile.d_vswitch.sh \
|
||||||
xenserver/etc_xapi.d_plugins_vswitch-cfg-update \
|
xenserver/etc_xapi.d_plugins_vswitch-cfg-update \
|
||||||
xenserver/etc_xensource_scripts_vif \
|
xenserver/etc_xensource_scripts_vif \
|
||||||
|
xenserver/opt_xensource_libexec_InterfaceReconfigure.py \
|
||||||
|
xenserver/opt_xensource_libexec_InterfaceReconfigureBridge.py \
|
||||||
|
xenserver/opt_xensource_libexec_InterfaceReconfigureVswitch.py \
|
||||||
xenserver/opt_xensource_libexec_interface-reconfigure \
|
xenserver/opt_xensource_libexec_interface-reconfigure \
|
||||||
xenserver/usr_lib_xsconsole_plugins-base_XSFeatureVSwitch.py \
|
xenserver/usr_lib_xsconsole_plugins-base_XSFeatureVSwitch.py \
|
||||||
xenserver/usr_sbin_brctl \
|
xenserver/usr_sbin_brctl \
|
||||||
xenserver/usr_sbin_xen-bugtool \
|
xenserver/usr_sbin_xen-bugtool \
|
||||||
xenserver/usr_share_vswitch_scripts_sysconfig.template \
|
|
||||||
xenserver/usr_share_vswitch_scripts_dump-vif-details \
|
xenserver/usr_share_vswitch_scripts_dump-vif-details \
|
||||||
xenserver/usr_share_vswitch_scripts_refresh-xs-network-uuids \
|
xenserver/usr_share_vswitch_scripts_refresh-xs-network-uuids \
|
||||||
|
xenserver/usr_share_vswitch_scripts_sysconfig.template \
|
||||||
xenserver/vswitch-xen.spec
|
xenserver/vswitch-xen.spec
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# Copyright (C) 2008,2009 Citrix Systems, Inc.
|
# Copyright (C) 2008,2009 Citrix Systems, Inc.
|
||||||
# Copyright (C) 2009 Nicira Networks, Inc.
|
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# This program is free software; you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Lesser General Public License as published
|
# it under the terms of the GNU Lesser General Public License as published
|
||||||
@@ -21,126 +20,215 @@
|
|||||||
|
|
||||||
# Keep other-config/ keys in sync with device.ml:vif_udev_keys
|
# Keep other-config/ keys in sync with device.ml:vif_udev_keys
|
||||||
|
|
||||||
|
BRCTL="/usr/sbin/brctl"
|
||||||
|
IP="/sbin/ip"
|
||||||
|
|
||||||
cfg_mod="/usr/bin/ovs-cfg-mod"
|
cfg_mod="/usr/bin/ovs-cfg-mod"
|
||||||
vsctl="/usr/bin/ovs-vsctl"
|
vsctl="/usr/bin/ovs-vsctl"
|
||||||
dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
|
|
||||||
service="/sbin/service"
|
service="/sbin/service"
|
||||||
|
|
||||||
TYPE=`echo ${XENBUS_PATH} | cut -f 2 -d '/'`
|
|
||||||
DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
|
|
||||||
DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
|
|
||||||
|
|
||||||
XAPI=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
|
|
||||||
HOTPLUG=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
|
|
||||||
PRIVATE=/xapi/${DOMID}/private/${TYPE}/${DEVID}
|
|
||||||
BRCTL=/usr/sbin/brctl
|
|
||||||
IP=/sbin/ip
|
|
||||||
|
|
||||||
|
|
||||||
handle_promiscuous()
|
handle_promiscuous()
|
||||||
{
|
{
|
||||||
local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous")
|
local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
|
||||||
if [ $? -eq 0 -a -n "${arg}" ] ; then
|
if [ $? -eq 0 -a -n "${arg}" ] ; then
|
||||||
case "${arg}" in
|
case $NETWORK_MODE in
|
||||||
true|on) logger -t script-vif "${vif}: Promiscuous ports are not supported via vSwitch." ;;
|
bridge)
|
||||||
*) ;;
|
case "${arg}" in
|
||||||
esac
|
true|on) echo 1 > /sys/class/net/${dev}/brport/promisc ;;
|
||||||
|
*) echo 0 > /sys/class/net/${dev}/brport/promisc ;;
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
vswitch)
|
||||||
|
logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch."
|
||||||
|
;;
|
||||||
|
esac
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_ethtool()
|
handle_ethtool()
|
||||||
{
|
{
|
||||||
local opt=$1
|
local opt=$1
|
||||||
local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}")
|
local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}" 2>/dev/null)
|
||||||
if [ $? -eq 0 -a -n "${arg}" ] ; then
|
if [ $? -eq 0 -a -n "${arg}" ] ; then
|
||||||
case "${arg}" in
|
case "${arg}" in
|
||||||
true|on) /sbin/ethtool -K "${vif}" "${opt}" on ;;
|
true|on) /sbin/ethtool -K "${dev}" "${opt}" on ;;
|
||||||
false|off) /sbin/ethtool -K "${vif}" "${opt}" off ;;
|
false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
|
||||||
*) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${vif}/${VIFUUID}" ;;
|
*) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_mtu()
|
handle_mtu()
|
||||||
{
|
{
|
||||||
local mtu=$(xenstore-read "${PRIVATE}/MTU")
|
local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
|
||||||
if [ $? -eq 0 -a -n "${mtu}" ]; then
|
if [ $? -eq 0 -a -n "${mtu}" ]; then
|
||||||
echo "${mtu}" > /sys/class/net/${vif}/mtu
|
echo "${mtu}" > /sys/class/net/${dev}/mtu
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handle_vswitch_vif_details()
|
||||||
|
{
|
||||||
|
local vif_details=
|
||||||
|
local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
|
||||||
|
if [ -n "${net_uuid}" ] ; then
|
||||||
|
vif_details="$vif_details --add=port.${dev}.net-uuid=${net_uuid}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
|
||||||
|
if [ -n "${address}" ] ; then
|
||||||
|
vif_details="$vif_details --add=port.${dev}.vif-mac=${address}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
|
||||||
|
if [ -n "${vif_uuid}" ] ; then
|
||||||
|
vif_details="$vif_details --add=port.${dev}.vif-uuid=${vif_uuid}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
|
||||||
|
if [ $? -eq 0 -a -n "${vm}" ] ; then
|
||||||
|
local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
|
||||||
|
fi
|
||||||
|
if [ -n "${vm_uuid}" ] ; then
|
||||||
|
vif_details="$vif_details --add=port.${dev}.vm-uuid=${vm_uuid}"
|
||||||
|
fi
|
||||||
|
echo ${vif_details}
|
||||||
|
}
|
||||||
|
|
||||||
add_to_bridge()
|
add_to_bridge()
|
||||||
{
|
{
|
||||||
local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
|
local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
|
||||||
if [ $? -ne 0 -o -z "${address}" ]; then
|
if [ $? -ne 0 -o -z "${address}" ]; then
|
||||||
logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
|
logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
local bridge=$(xenstore-read "${PRIVATE}/bridge")
|
local bridge=$(xenstore-read "${PRIVATE}/bridge")
|
||||||
if [ $? -ne 0 -o -z "${bridge}" ]; then
|
if [ $? -ne 0 -o -z "${bridge}" ]; then
|
||||||
logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
|
logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
logger -t scripts-vif "Adding ${vif} to ${bridge} with address ${address}"
|
logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
|
||||||
|
|
||||||
local VLAN_ID=$($vsctl br-to-vlan $bridge)
|
${IP} link set "${dev}" down || logger -t scripts-vif "Failed to ip link set ${dev} down"
|
||||||
local vid=
|
${IP} link set "${dev}" arp off || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
|
||||||
if [ "$VLAN_ID" -ne 0 ] ; then
|
${IP} link set "${dev}" multicast off || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
|
||||||
bridge=$($vsctl br-to-parent $bridge)
|
${IP} link set "${dev}" address "${address}" || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
|
||||||
vid="--add=vlan.${vif}.tag=${VLAN_ID}"
|
${IP} addr flush "${dev}" || logger -t scripts-vif "Failed to ip addr flush ${dev}"
|
||||||
fi
|
|
||||||
|
|
||||||
${IP} link set "${vif}" down || logger -t scripts-vif "Failed to ip link set ${vif} down"
|
case $NETWORK_MODE in
|
||||||
${IP} link set "${vif}" arp off || logger -t scripts-vif "Failed to ip link set ${vif} arp off"
|
bridge)
|
||||||
${IP} link set "${vif}" multicast off || logger -t scripts-vif "Failed to ip link set ${vif} multicast off"
|
${BRCTL} setfd "${bridge}" 0 || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
|
||||||
${IP} link set "${vif}" address "${address}" || logger -t scripts-vif "Failed to ip link set ${vif} address ${address}"
|
${BRCTL} addif "${bridge}" "${dev}" || logger -t scripts-vif "Failed to brctl addif ${bridge} ${dev}"
|
||||||
${IP} addr flush "${vif}" || logger -t scripts-vif "Failed to ip addr flush ${vif}"
|
;;
|
||||||
|
vswitch)
|
||||||
|
local VLAN_ID=$($vsctl br-to-vlan $bridge)
|
||||||
|
local vid=
|
||||||
|
if [ "$VLAN_ID" -ne 0 ] ; then
|
||||||
|
bridge=$($vsctl br-to-parent $bridge)
|
||||||
|
vid="--add=vlan.${dev}.tag=${VLAN_ID}"
|
||||||
|
fi
|
||||||
|
|
||||||
local vif_details=$($dump_vif_details $DOMID $DEVID)
|
if [ "$TYPE" = "vif" ] ; then
|
||||||
if [ $? -ne 0 -o -z "${vif_details}" ]; then
|
local vif_details=$(handle_vswitch_vif_details)
|
||||||
logger -t scripts-vif "Failed to retrieve vif details for vswitch"
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
$cfg_mod -F /etc/ovs-vswitchd.conf \
|
$cfg_mod -F /etc/ovs-vswitchd.conf \
|
||||||
--del-match="bridge.*.port=$vif" \
|
--del-match="bridge.*.port=${dev}" \
|
||||||
--del-match="vlan.$vif.trunks=*" \
|
--del-match="vlan.${dev}.trunks=*" \
|
||||||
--del-match="vlan.$vif.tag=*" \
|
--del-match="vlan.${dev}.tag=*" \
|
||||||
--del-match="port.$vif.[!0-9]*" \
|
--del-match="port.${dev}.[!0-9]*" \
|
||||||
--add="bridge.$bridge.port=$vif" \
|
--add="bridge.$bridge.port=${dev}" \
|
||||||
$vid $vif_details -c
|
$vid $vif_details -c
|
||||||
$service vswitch reload
|
$service vswitch reload
|
||||||
|
;;
|
||||||
${IP} link set "${vif}" up || logger -t scripts-vif "Failed to ip link set ${vif} up"
|
esac
|
||||||
|
|
||||||
|
${IP} link set "${dev}" up || logger -t scripts-vif "Failed to ip link set ${dev} up"
|
||||||
}
|
}
|
||||||
|
|
||||||
echo Called as "$@" "$TYPE" "$DOMID" "$DEVID" | logger -t scripts-vif
|
remove_from_bridge()
|
||||||
case "$1" in
|
{
|
||||||
online)
|
case $NETWORK_MODE in
|
||||||
handle_ethtool rx
|
bridge)
|
||||||
handle_ethtool tx
|
# Nothing to do
|
||||||
handle_ethtool sg
|
;;
|
||||||
handle_ethtool tso
|
vswitch)
|
||||||
handle_ethtool ufo
|
$cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
|
||||||
handle_ethtool gso
|
--del-match="bridge.*.port=${dev}" \
|
||||||
|
--del-match="vlan.${dev}.trunks=*" \
|
||||||
|
--del-match="vlan.${dev}.tag=*" \
|
||||||
|
--del-match="port.${dev}.[!0-9]*" -c
|
||||||
|
$service vswitch reload
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
handle_mtu
|
NETWORK_MODE=$(cat /etc/xensource/network.conf)
|
||||||
add_to_bridge
|
ACTION=$1
|
||||||
handle_promiscuous
|
TYPE=$2
|
||||||
|
|
||||||
xenstore-write "${HOTPLUG}/vif" "${vif}"
|
case $NETWORK_MODE in
|
||||||
xenstore-write "${HOTPLUG}/hotplug" "online"
|
bridge|vswitch) ;;
|
||||||
|
*)
|
||||||
# xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
|
logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
|
||||||
xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
|
exit 1
|
||||||
|
;;
|
||||||
;;
|
esac
|
||||||
remove)
|
|
||||||
xenstore-rm "${HOTPLUG}/hotplug"
|
case ${TYPE} in
|
||||||
vif=vif${DOMID}.${DEVID}
|
vif)
|
||||||
logger -t scripts-vif "${vif} has been removed"
|
DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
|
||||||
$cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
|
DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
|
||||||
--del-match="bridge.*.port=${vif}" \
|
dev=vif${DOMID}.${DEVID}
|
||||||
--del-match="vlan.${vif}.trunks=*" \
|
;;
|
||||||
--del-match="vlan.${vif}.tag=*" \
|
tap)
|
||||||
--del-match="port.${vif}.[!0-9]*" -c
|
dev=$INTERFACE
|
||||||
$service vswitch reload
|
DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
|
||||||
|
DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
logger -t scripts-vif "unknown interface type ${TYPE}"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
|
||||||
|
HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
|
||||||
|
PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
|
||||||
|
|
||||||
|
logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
|
||||||
|
case "${ACTION}" in
|
||||||
|
online)
|
||||||
|
if [ "${TYPE}" = "vif" ] ; then
|
||||||
|
handle_ethtool rx
|
||||||
|
handle_ethtool tx
|
||||||
|
handle_ethtool sg
|
||||||
|
handle_ethtool tso
|
||||||
|
handle_ethtool ufo
|
||||||
|
handle_ethtool gso
|
||||||
|
|
||||||
|
handle_mtu
|
||||||
|
add_to_bridge
|
||||||
|
handle_promiscuous
|
||||||
|
|
||||||
|
xenstore-write "${HOTPLUG}/vif" "${dev}"
|
||||||
|
xenstore-write "${HOTPLUG}/hotplug" "online"
|
||||||
|
|
||||||
|
# xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
|
||||||
|
xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
add)
|
||||||
|
if [ "${TYPE}" = "tap" ] ; then
|
||||||
|
add_to_bridge
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
remove)
|
||||||
|
if [ "${TYPE}" = "vif" ] ;then
|
||||||
|
xenstore-rm "${HOTPLUG}/hotplug"
|
||||||
|
fi
|
||||||
|
logger -t scripts-vif "${dev} has been removed"
|
||||||
|
remove_from_bridge
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
793
xenserver/opt_xensource_libexec_InterfaceReconfigure.py
Normal file
793
xenserver/opt_xensource_libexec_InterfaceReconfigure.py
Normal file
@@ -0,0 +1,793 @@
|
|||||||
|
# Copyright (c) 2008,2009 Citrix Systems, Inc.
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Lesser General Public License as published
|
||||||
|
# by the Free Software Foundation; version 2.1 only. with the special
|
||||||
|
# exception on linking described in file LICENSE.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Lesser General Public License for more details.
|
||||||
|
#
|
||||||
|
import syslog
|
||||||
|
import os
|
||||||
|
|
||||||
|
from xml.dom.minidom import getDOMImplementation
|
||||||
|
from xml.dom.minidom import parse as parseXML
|
||||||
|
|
||||||
|
#
|
||||||
|
# Logging.
|
||||||
|
#
|
||||||
|
|
||||||
|
def log(s):
|
||||||
|
syslog.syslog(s)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Exceptions.
|
||||||
|
#
|
||||||
|
|
||||||
|
class Error(Exception):
|
||||||
|
def __init__(self, msg):
|
||||||
|
Exception.__init__(self)
|
||||||
|
self.msg = msg
|
||||||
|
|
||||||
|
#
|
||||||
|
# Run external utilities
|
||||||
|
#
|
||||||
|
|
||||||
|
def run_command(command):
|
||||||
|
log("Running command: " + ' '.join(command))
|
||||||
|
rc = os.spawnl(os.P_WAIT, command[0], *command)
|
||||||
|
if rc != 0:
|
||||||
|
log("Command failed %d: " % rc + ' '.join(command))
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
#
|
||||||
|
# Configuration File Handling.
|
||||||
|
#
|
||||||
|
|
||||||
|
class ConfigurationFile(object):
|
||||||
|
"""Write a file, tracking old and new versions.
|
||||||
|
|
||||||
|
Supports writing a new version of a file and applying and
|
||||||
|
reverting those changes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__STATE = {"OPEN":"OPEN",
|
||||||
|
"NOT-APPLIED":"NOT-APPLIED", "APPLIED":"APPLIED",
|
||||||
|
"REVERTED":"REVERTED", "COMMITTED": "COMMITTED"}
|
||||||
|
|
||||||
|
def __init__(self, path):
|
||||||
|
dirname,basename = os.path.split(path)
|
||||||
|
|
||||||
|
self.__state = self.__STATE['OPEN']
|
||||||
|
self.__children = []
|
||||||
|
|
||||||
|
self.__path = os.path.join(dirname, basename)
|
||||||
|
self.__oldpath = os.path.join(dirname, "." + basename + ".xapi-old")
|
||||||
|
self.__newpath = os.path.join(dirname, "." + basename + ".xapi-new")
|
||||||
|
|
||||||
|
self.__f = open(self.__newpath, "w")
|
||||||
|
|
||||||
|
def attach_child(self, child):
|
||||||
|
self.__children.append(child)
|
||||||
|
|
||||||
|
def path(self):
|
||||||
|
return self.__path
|
||||||
|
|
||||||
|
def readlines(self):
|
||||||
|
try:
|
||||||
|
return open(self.path()).readlines()
|
||||||
|
except:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def write(self, args):
|
||||||
|
if self.__state != self.__STATE['OPEN']:
|
||||||
|
raise Error("Attempt to write to file in state %s" % self.__state)
|
||||||
|
self.__f.write(args)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.__state != self.__STATE['OPEN']:
|
||||||
|
raise Error("Attempt to close file in state %s" % self.__state)
|
||||||
|
|
||||||
|
self.__f.close()
|
||||||
|
self.__state = self.__STATE['NOT-APPLIED']
|
||||||
|
|
||||||
|
def changed(self):
|
||||||
|
if self.__state != self.__STATE['NOT-APPLIED']:
|
||||||
|
raise Error("Attempt to compare file in state %s" % self.__state)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def apply(self):
|
||||||
|
if self.__state != self.__STATE['NOT-APPLIED']:
|
||||||
|
raise Error("Attempt to apply configuration from state %s" % self.__state)
|
||||||
|
|
||||||
|
for child in self.__children:
|
||||||
|
child.apply()
|
||||||
|
|
||||||
|
log("Applying changes to %s configuration" % self.__path)
|
||||||
|
|
||||||
|
# Remove previous backup.
|
||||||
|
if os.access(self.__oldpath, os.F_OK):
|
||||||
|
os.unlink(self.__oldpath)
|
||||||
|
|
||||||
|
# Save current configuration.
|
||||||
|
if os.access(self.__path, os.F_OK):
|
||||||
|
os.link(self.__path, self.__oldpath)
|
||||||
|
os.unlink(self.__path)
|
||||||
|
|
||||||
|
# Apply new configuration.
|
||||||
|
assert(os.path.exists(self.__newpath))
|
||||||
|
os.link(self.__newpath, self.__path)
|
||||||
|
|
||||||
|
# Remove temporary file.
|
||||||
|
os.unlink(self.__newpath)
|
||||||
|
|
||||||
|
self.__state = self.__STATE['APPLIED']
|
||||||
|
|
||||||
|
def revert(self):
|
||||||
|
if self.__state != self.__STATE['APPLIED']:
|
||||||
|
raise Error("Attempt to revert configuration from state %s" % self.__state)
|
||||||
|
|
||||||
|
for child in self.__children:
|
||||||
|
child.revert()
|
||||||
|
|
||||||
|
log("Reverting changes to %s configuration" % self.__path)
|
||||||
|
|
||||||
|
# Remove existing new configuration
|
||||||
|
if os.access(self.__newpath, os.F_OK):
|
||||||
|
os.unlink(self.__newpath)
|
||||||
|
|
||||||
|
# Revert new configuration.
|
||||||
|
if os.access(self.__path, os.F_OK):
|
||||||
|
os.link(self.__path, self.__newpath)
|
||||||
|
os.unlink(self.__path)
|
||||||
|
|
||||||
|
# Revert to old configuration.
|
||||||
|
if os.access(self.__oldpath, os.F_OK):
|
||||||
|
os.link(self.__oldpath, self.__path)
|
||||||
|
os.unlink(self.__oldpath)
|
||||||
|
|
||||||
|
# Leave .*.xapi-new as an aid to debugging.
|
||||||
|
|
||||||
|
self.__state = self.__STATE['REVERTED']
|
||||||
|
|
||||||
|
def commit(self):
|
||||||
|
if self.__state != self.__STATE['APPLIED']:
|
||||||
|
raise Error("Attempt to commit configuration from state %s" % self.__state)
|
||||||
|
|
||||||
|
for child in self.__children:
|
||||||
|
child.commit()
|
||||||
|
|
||||||
|
log("Committing changes to %s configuration" % self.__path)
|
||||||
|
|
||||||
|
if os.access(self.__oldpath, os.F_OK):
|
||||||
|
os.unlink(self.__oldpath)
|
||||||
|
if os.access(self.__newpath, os.F_OK):
|
||||||
|
os.unlink(self.__newpath)
|
||||||
|
|
||||||
|
self.__state = self.__STATE['COMMITTED']
|
||||||
|
|
||||||
|
#
|
||||||
|
# Helper functions for encoding/decoding database attributes to/from XML.
|
||||||
|
#
|
||||||
|
|
||||||
|
def _str_to_xml(xml, parent, tag, val):
|
||||||
|
e = xml.createElement(tag)
|
||||||
|
parent.appendChild(e)
|
||||||
|
v = xml.createTextNode(val)
|
||||||
|
e.appendChild(v)
|
||||||
|
def _str_from_xml(n):
|
||||||
|
def getText(nodelist):
|
||||||
|
rc = ""
|
||||||
|
for node in nodelist:
|
||||||
|
if node.nodeType == node.TEXT_NODE:
|
||||||
|
rc = rc + node.data
|
||||||
|
return rc
|
||||||
|
return getText(n.childNodes).strip()
|
||||||
|
|
||||||
|
def _bool_to_xml(xml, parent, tag, val):
|
||||||
|
if val:
|
||||||
|
_str_to_xml(xml, parent, tag, "True")
|
||||||
|
else:
|
||||||
|
_str_to_xml(xml, parent, tag, "False")
|
||||||
|
def _bool_from_xml(n):
|
||||||
|
s = _str_from_xml(n)
|
||||||
|
if s == "True":
|
||||||
|
return True
|
||||||
|
elif s == "False":
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
raise Error("Unknown boolean value %s" % s)
|
||||||
|
|
||||||
|
def _strlist_to_xml(xml, parent, ltag, itag, val):
|
||||||
|
e = xml.createElement(ltag)
|
||||||
|
parent.appendChild(e)
|
||||||
|
for v in val:
|
||||||
|
c = xml.createElement(itag)
|
||||||
|
e.appendChild(c)
|
||||||
|
cv = xml.createTextNode(v)
|
||||||
|
c.appendChild(cv)
|
||||||
|
def _strlist_from_xml(n, ltag, itag):
|
||||||
|
ret = []
|
||||||
|
for n in n.childNodes:
|
||||||
|
if n.nodeName == itag:
|
||||||
|
ret.append(_str_from_xml(n))
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def _otherconfig_to_xml(xml, parent, val, attrs):
|
||||||
|
otherconfig = xml.createElement("other_config")
|
||||||
|
parent.appendChild(otherconfig)
|
||||||
|
for n,v in val.items():
|
||||||
|
if not n in attrs:
|
||||||
|
raise Error("Unknown other-config attribute: %s" % n)
|
||||||
|
_str_to_xml(xml, otherconfig, n, v)
|
||||||
|
def _otherconfig_from_xml(n, attrs):
|
||||||
|
ret = {}
|
||||||
|
for n in n.childNodes:
|
||||||
|
if n.nodeName in attrs:
|
||||||
|
ret[n.nodeName] = _str_from_xml(n)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
#
|
||||||
|
# Definitions of the database objects (and their attributes) used by interface-reconfigure.
|
||||||
|
#
|
||||||
|
# Each object is defined by a dictionary mapping an attribute name in
|
||||||
|
# the xapi database to a tuple containing two items:
|
||||||
|
# - a function which takes this attribute and encodes it as XML.
|
||||||
|
# - a function which takes XML and decocdes it into a value.
|
||||||
|
#
|
||||||
|
# other-config attributes are specified as a simple array of strings
|
||||||
|
|
||||||
|
_PIF_XML_TAG = "pif"
|
||||||
|
_VLAN_XML_TAG = "vlan"
|
||||||
|
_BOND_XML_TAG = "bond"
|
||||||
|
_NETWORK_XML_TAG = "network"
|
||||||
|
|
||||||
|
_ETHTOOL_OTHERCONFIG_ATTRS = ['ethtool-%s' % x for x in 'autoneg', 'speed', 'duplex', 'rx', 'tx', 'sg', 'tso', 'ufo', 'gso' ]
|
||||||
|
|
||||||
|
_PIF_OTHERCONFIG_ATTRS = [ 'domain', 'peerdns', 'defaultroute', 'mtu', 'static-routes' ] + \
|
||||||
|
[ 'bond-%s' % x for x in 'mode', 'miimon', 'downdelay', 'updelay', 'use_carrier' ] + \
|
||||||
|
_ETHTOOL_OTHERCONFIG_ATTRS
|
||||||
|
|
||||||
|
_PIF_ATTRS = { 'uuid': (_str_to_xml,_str_from_xml),
|
||||||
|
'management': (_bool_to_xml,_bool_from_xml),
|
||||||
|
'network': (_str_to_xml,_str_from_xml),
|
||||||
|
'device': (_str_to_xml,_str_from_xml),
|
||||||
|
'bond_master_of': (lambda x, p, t, v: _strlist_to_xml(x, p, 'bond_master_of', 'slave', v),
|
||||||
|
lambda n: _strlist_from_xml(n, 'bond_master_of', 'slave')),
|
||||||
|
'bond_slave_of': (_str_to_xml,_str_from_xml),
|
||||||
|
'VLAN': (_str_to_xml,_str_from_xml),
|
||||||
|
'VLAN_master_of': (_str_to_xml,_str_from_xml),
|
||||||
|
'VLAN_slave_of': (lambda x, p, t, v: _strlist_to_xml(x, p, 'VLAN_slave_of', 'master', v),
|
||||||
|
lambda n: _strlist_from_xml(n, 'VLAN_slave_Of', 'master')),
|
||||||
|
'ip_configuration_mode': (_str_to_xml,_str_from_xml),
|
||||||
|
'IP': (_str_to_xml,_str_from_xml),
|
||||||
|
'netmask': (_str_to_xml,_str_from_xml),
|
||||||
|
'gateway': (_str_to_xml,_str_from_xml),
|
||||||
|
'DNS': (_str_to_xml,_str_from_xml),
|
||||||
|
'MAC': (_str_to_xml,_str_from_xml),
|
||||||
|
'other_config': (lambda x, p, t, v: _otherconfig_to_xml(x, p, v, _PIF_OTHERCONFIG_ATTRS),
|
||||||
|
lambda n: _otherconfig_from_xml(n, _PIF_OTHERCONFIG_ATTRS)),
|
||||||
|
|
||||||
|
# Special case: We write the current value
|
||||||
|
# PIF.currently-attached to the cache but since it will
|
||||||
|
# not be valid when we come to use the cache later
|
||||||
|
# (i.e. after a reboot) we always read it as False.
|
||||||
|
'currently_attached': (_bool_to_xml, lambda n: False),
|
||||||
|
}
|
||||||
|
|
||||||
|
_VLAN_ATTRS = { 'uuid': (_str_to_xml,_str_from_xml),
|
||||||
|
'tagged_PIF': (_str_to_xml,_str_from_xml),
|
||||||
|
'untagged_PIF': (_str_to_xml,_str_from_xml),
|
||||||
|
}
|
||||||
|
|
||||||
|
_BOND_ATTRS = { 'uuid': (_str_to_xml,_str_from_xml),
|
||||||
|
'master': (_str_to_xml,_str_from_xml),
|
||||||
|
'slaves': (lambda x, p, t, v: _strlist_to_xml(x, p, 'slaves', 'slave', v),
|
||||||
|
lambda n: _strlist_from_xml(n, 'slaves', 'slave')),
|
||||||
|
}
|
||||||
|
|
||||||
|
_NETWORK_OTHERCONFIG_ATTRS = [ 'mtu', 'static-routes' ] + _ETHTOOL_OTHERCONFIG_ATTRS
|
||||||
|
|
||||||
|
_NETWORK_ATTRS = { 'uuid': (_str_to_xml,_str_from_xml),
|
||||||
|
'bridge': (_str_to_xml,_str_from_xml),
|
||||||
|
'PIFs': (lambda x, p, t, v: _strlist_to_xml(x, p, 'PIFs', 'PIF', v),
|
||||||
|
lambda n: _strlist_from_xml(n, 'PIFs', 'PIF')),
|
||||||
|
'other_config': (lambda x, p, t, v: _otherconfig_to_xml(x, p, v, _NETWORK_OTHERCONFIG_ATTRS),
|
||||||
|
lambda n: _otherconfig_from_xml(n, _NETWORK_OTHERCONFIG_ATTRS)),
|
||||||
|
}
|
||||||
|
|
||||||
|
#
|
||||||
|
# Database Cache object
|
||||||
|
#
|
||||||
|
|
||||||
|
_db = None
|
||||||
|
|
||||||
|
def db():
|
||||||
|
assert(_db is not None)
|
||||||
|
return _db
|
||||||
|
|
||||||
|
def db_init_from_cache(cache):
|
||||||
|
global _db
|
||||||
|
assert(_db is None)
|
||||||
|
_db = DatabaseCache(cache_file=cache)
|
||||||
|
|
||||||
|
def db_init_from_xenapi(session):
|
||||||
|
global _db
|
||||||
|
assert(_db is None)
|
||||||
|
_db = DatabaseCache(session_ref=session)
|
||||||
|
|
||||||
|
class DatabaseCache(object):
|
||||||
|
def __read_xensource_inventory(self):
|
||||||
|
filename = "/etc/xensource-inventory"
|
||||||
|
f = open(filename, "r")
|
||||||
|
lines = [x.strip("\n") for x in f.readlines()]
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
defs = [ (l[:l.find("=")], l[(l.find("=") + 1):]) for l in lines ]
|
||||||
|
defs = [ (a, b.strip("'")) for (a,b) in defs ]
|
||||||
|
|
||||||
|
return dict(defs)
|
||||||
|
def __pif_on_host(self,pif):
|
||||||
|
return self.__pifs.has_key(pif)
|
||||||
|
|
||||||
|
def __get_pif_records_from_xapi(self, session, host):
|
||||||
|
self.__pifs = {}
|
||||||
|
for (p,rec) in session.xenapi.PIF.get_all_records().items():
|
||||||
|
if rec['host'] != host:
|
||||||
|
continue
|
||||||
|
self.__pifs[p] = {}
|
||||||
|
for f in _PIF_ATTRS:
|
||||||
|
self.__pifs[p][f] = rec[f]
|
||||||
|
self.__pifs[p]['other_config'] = {}
|
||||||
|
for f in _PIF_OTHERCONFIG_ATTRS:
|
||||||
|
if not rec['other_config'].has_key(f): continue
|
||||||
|
self.__pifs[p]['other_config'][f] = rec['other_config'][f]
|
||||||
|
|
||||||
|
def __get_vlan_records_from_xapi(self, session):
|
||||||
|
self.__vlans = {}
|
||||||
|
for v in session.xenapi.VLAN.get_all():
|
||||||
|
rec = session.xenapi.VLAN.get_record(v)
|
||||||
|
if not self.__pif_on_host(rec['untagged_PIF']):
|
||||||
|
continue
|
||||||
|
self.__vlans[v] = {}
|
||||||
|
for f in _VLAN_ATTRS:
|
||||||
|
self.__vlans[v][f] = rec[f]
|
||||||
|
|
||||||
|
def __get_bond_records_from_xapi(self, session):
|
||||||
|
self.__bonds = {}
|
||||||
|
for b in session.xenapi.Bond.get_all():
|
||||||
|
rec = session.xenapi.Bond.get_record(b)
|
||||||
|
if not self.__pif_on_host(rec['master']):
|
||||||
|
continue
|
||||||
|
self.__bonds[b] = {}
|
||||||
|
for f in _BOND_ATTRS:
|
||||||
|
self.__bonds[b][f] = rec[f]
|
||||||
|
|
||||||
|
def __get_network_records_from_xapi(self, session):
|
||||||
|
self.__networks = {}
|
||||||
|
for n in session.xenapi.network.get_all():
|
||||||
|
rec = session.xenapi.network.get_record(n)
|
||||||
|
self.__networks[n] = {}
|
||||||
|
for f in _NETWORK_ATTRS:
|
||||||
|
if f == "PIFs":
|
||||||
|
# drop PIFs on other hosts
|
||||||
|
self.__networks[n][f] = [p for p in rec[f] if self.__pif_on_host(p)]
|
||||||
|
else:
|
||||||
|
self.__networks[n][f] = rec[f]
|
||||||
|
self.__networks[n]['other_config'] = {}
|
||||||
|
for f in _NETWORK_OTHERCONFIG_ATTRS:
|
||||||
|
if not rec['other_config'].has_key(f): continue
|
||||||
|
self.__networks[n]['other_config'][f] = rec['other_config'][f]
|
||||||
|
|
||||||
|
def __to_xml(self, xml, parent, key, ref, rec, attrs):
|
||||||
|
"""Encode a database object as XML"""
|
||||||
|
e = xml.createElement(key)
|
||||||
|
parent.appendChild(e)
|
||||||
|
if ref:
|
||||||
|
e.setAttribute('ref', ref)
|
||||||
|
|
||||||
|
for n,v in rec.items():
|
||||||
|
if attrs.has_key(n):
|
||||||
|
h,_ = attrs[n]
|
||||||
|
h(xml, e, n, v)
|
||||||
|
else:
|
||||||
|
raise Error("Unknown attribute %s" % n)
|
||||||
|
def __from_xml(self, e, attrs):
|
||||||
|
"""Decode a database object from XML"""
|
||||||
|
ref = e.attributes['ref'].value
|
||||||
|
rec = {}
|
||||||
|
for n in e.childNodes:
|
||||||
|
if n.nodeName in attrs:
|
||||||
|
_,h = attrs[n.nodeName]
|
||||||
|
rec[n.nodeName] = h(n)
|
||||||
|
return (ref,rec)
|
||||||
|
|
||||||
|
def __init__(self, session_ref=None, cache_file=None):
|
||||||
|
if session_ref and cache_file:
|
||||||
|
raise Error("can't specify session reference and cache file")
|
||||||
|
if cache_file == None:
|
||||||
|
import XenAPI
|
||||||
|
session = XenAPI.xapi_local()
|
||||||
|
|
||||||
|
if not session_ref:
|
||||||
|
log("No session ref given on command line, logging in.")
|
||||||
|
session.xenapi.login_with_password("root", "")
|
||||||
|
else:
|
||||||
|
session._session = session_ref
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
inventory = self.__read_xensource_inventory()
|
||||||
|
assert(inventory.has_key('INSTALLATION_UUID'))
|
||||||
|
log("host uuid is %s" % inventory['INSTALLATION_UUID'])
|
||||||
|
|
||||||
|
host = session.xenapi.host.get_by_uuid(inventory['INSTALLATION_UUID'])
|
||||||
|
|
||||||
|
self.__get_pif_records_from_xapi(session, host)
|
||||||
|
|
||||||
|
self.__get_vlan_records_from_xapi(session)
|
||||||
|
self.__get_bond_records_from_xapi(session)
|
||||||
|
self.__get_network_records_from_xapi(session)
|
||||||
|
finally:
|
||||||
|
if not session_ref:
|
||||||
|
session.xenapi.session.logout()
|
||||||
|
else:
|
||||||
|
log("Loading xapi database cache from %s" % cache_file)
|
||||||
|
|
||||||
|
xml = parseXML(cache_file)
|
||||||
|
|
||||||
|
self.__pifs = {}
|
||||||
|
self.__bonds = {}
|
||||||
|
self.__vlans = {}
|
||||||
|
self.__networks = {}
|
||||||
|
|
||||||
|
assert(len(xml.childNodes) == 1)
|
||||||
|
toplevel = xml.childNodes[0]
|
||||||
|
|
||||||
|
assert(toplevel.nodeName == "xenserver-network-configuration")
|
||||||
|
|
||||||
|
for n in toplevel.childNodes:
|
||||||
|
if n.nodeName == "#text":
|
||||||
|
pass
|
||||||
|
elif n.nodeName == _PIF_XML_TAG:
|
||||||
|
(ref,rec) = self.__from_xml(n, _PIF_ATTRS)
|
||||||
|
self.__pifs[ref] = rec
|
||||||
|
elif n.nodeName == _BOND_XML_TAG:
|
||||||
|
(ref,rec) = self.__from_xml(n, _BOND_ATTRS)
|
||||||
|
self.__bonds[ref] = rec
|
||||||
|
elif n.nodeName == _VLAN_XML_TAG:
|
||||||
|
(ref,rec) = self.__from_xml(n, _VLAN_ATTRS)
|
||||||
|
self.__vlans[ref] = rec
|
||||||
|
elif n.nodeName == _NETWORK_XML_TAG:
|
||||||
|
(ref,rec) = self.__from_xml(n, _NETWORK_ATTRS)
|
||||||
|
self.__networks[ref] = rec
|
||||||
|
else:
|
||||||
|
raise Error("Unknown XML element %s" % n.nodeName)
|
||||||
|
|
||||||
|
def save(self, cache_file):
|
||||||
|
|
||||||
|
xml = getDOMImplementation().createDocument(
|
||||||
|
None, "xenserver-network-configuration", None)
|
||||||
|
for (ref,rec) in self.__pifs.items():
|
||||||
|
self.__to_xml(xml, xml.documentElement, _PIF_XML_TAG, ref, rec, _PIF_ATTRS)
|
||||||
|
for (ref,rec) in self.__bonds.items():
|
||||||
|
self.__to_xml(xml, xml.documentElement, _BOND_XML_TAG, ref, rec, _BOND_ATTRS)
|
||||||
|
for (ref,rec) in self.__vlans.items():
|
||||||
|
self.__to_xml(xml, xml.documentElement, _VLAN_XML_TAG, ref, rec, _VLAN_ATTRS)
|
||||||
|
for (ref,rec) in self.__networks.items():
|
||||||
|
self.__to_xml(xml, xml.documentElement, _NETWORK_XML_TAG, ref, rec,
|
||||||
|
_NETWORK_ATTRS)
|
||||||
|
|
||||||
|
f = open(cache_file, 'w')
|
||||||
|
f.write(xml.toprettyxml())
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
def get_pif_by_uuid(self, uuid):
|
||||||
|
pifs = map(lambda (ref,rec): ref,
|
||||||
|
filter(lambda (ref,rec): uuid == rec['uuid'],
|
||||||
|
self.__pifs.items()))
|
||||||
|
if len(pifs) == 0:
|
||||||
|
raise Error("Unknown PIF \"%s\"" % uuid)
|
||||||
|
elif len(pifs) > 1:
|
||||||
|
raise Error("Non-unique PIF \"%s\"" % uuid)
|
||||||
|
|
||||||
|
return pifs[0]
|
||||||
|
|
||||||
|
def get_pifs_by_device(self, device):
|
||||||
|
return map(lambda (ref,rec): ref,
|
||||||
|
filter(lambda (ref,rec): rec['device'] == device,
|
||||||
|
self.__pifs.items()))
|
||||||
|
|
||||||
|
def get_pif_by_bridge(self, bridge):
|
||||||
|
networks = map(lambda (ref,rec): ref,
|
||||||
|
filter(lambda (ref,rec): rec['bridge'] == bridge,
|
||||||
|
self.__networks.items()))
|
||||||
|
if len(networks) == 0:
|
||||||
|
raise Error("No matching network \"%s\"" % bridge)
|
||||||
|
|
||||||
|
answer = None
|
||||||
|
for network in networks:
|
||||||
|
nwrec = self.get_network_record(network)
|
||||||
|
for pif in nwrec['PIFs']:
|
||||||
|
pifrec = self.get_pif_record(pif)
|
||||||
|
if answer:
|
||||||
|
raise Error("Multiple PIFs on host for network %s" % (bridge))
|
||||||
|
answer = pif
|
||||||
|
if not answer:
|
||||||
|
raise Error("No PIF on host for network %s" % (bridge))
|
||||||
|
return answer
|
||||||
|
|
||||||
|
def get_pif_record(self, pif):
|
||||||
|
if self.__pifs.has_key(pif):
|
||||||
|
return self.__pifs[pif]
|
||||||
|
raise Error("Unknown PIF \"%s\"" % pif)
|
||||||
|
def get_all_pifs(self):
|
||||||
|
return self.__pifs
|
||||||
|
def pif_exists(self, pif):
|
||||||
|
return self.__pifs.has_key(pif)
|
||||||
|
|
||||||
|
def get_management_pif(self):
|
||||||
|
""" Returns the management pif on host
|
||||||
|
"""
|
||||||
|
all = self.get_all_pifs()
|
||||||
|
for pif in all:
|
||||||
|
pifrec = self.get_pif_record(pif)
|
||||||
|
if pifrec['management']: return pif
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_network_record(self, network):
|
||||||
|
if self.__networks.has_key(network):
|
||||||
|
return self.__networks[network]
|
||||||
|
raise Error("Unknown network \"%s\"" % network)
|
||||||
|
|
||||||
|
def get_bond_record(self, bond):
|
||||||
|
if self.__bonds.has_key(bond):
|
||||||
|
return self.__bonds[bond]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_vlan_record(self, vlan):
|
||||||
|
if self.__vlans.has_key(vlan):
|
||||||
|
return self.__vlans[vlan]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
def ethtool_settings(oc):
|
||||||
|
settings = []
|
||||||
|
if oc.has_key('ethtool-speed'):
|
||||||
|
val = oc['ethtool-speed']
|
||||||
|
if val in ["10", "100", "1000"]:
|
||||||
|
settings += ['speed', val]
|
||||||
|
else:
|
||||||
|
log("Invalid value for ethtool-speed = %s. Must be 10|100|1000." % val)
|
||||||
|
if oc.has_key('ethtool-duplex'):
|
||||||
|
val = oc['ethtool-duplex']
|
||||||
|
if val in ["10", "100", "1000"]:
|
||||||
|
settings += ['duplex', 'val']
|
||||||
|
else:
|
||||||
|
log("Invalid value for ethtool-duplex = %s. Must be half|full." % val)
|
||||||
|
if oc.has_key('ethtool-autoneg'):
|
||||||
|
val = oc['ethtool-autoneg']
|
||||||
|
if val in ["true", "on"]:
|
||||||
|
settings += ['autoneg', 'on']
|
||||||
|
elif val in ["false", "off"]:
|
||||||
|
settings += ['autoneg', 'off']
|
||||||
|
else:
|
||||||
|
log("Invalid value for ethtool-autoneg = %s. Must be on|true|off|false." % val)
|
||||||
|
offload = []
|
||||||
|
for opt in ("rx", "tx", "sg", "tso", "ufo", "gso"):
|
||||||
|
if oc.has_key("ethtool-" + opt):
|
||||||
|
val = oc["ethtool-" + opt]
|
||||||
|
if val in ["true", "on"]:
|
||||||
|
offload += [opt, 'on']
|
||||||
|
elif val in ["false", "off"]:
|
||||||
|
offload += [opt, 'off']
|
||||||
|
else:
|
||||||
|
log("Invalid value for ethtool-%s = %s. Must be on|true|off|false." % (opt, val))
|
||||||
|
return settings,offload
|
||||||
|
|
||||||
|
def mtu_setting(oc):
|
||||||
|
if oc.has_key('mtu'):
|
||||||
|
try:
|
||||||
|
int(oc['mtu']) # Check that the value is an integer
|
||||||
|
return oc['mtu']
|
||||||
|
except ValueError, x:
|
||||||
|
log("Invalid value for mtu = %s" % oc['mtu'])
|
||||||
|
return None
|
||||||
|
|
||||||
|
#
|
||||||
|
# IP Network Devices -- network devices with IP configuration
|
||||||
|
#
|
||||||
|
def pif_ipdev_name(pif):
|
||||||
|
"""Return the ipdev name associated with pif"""
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
nwrec = db().get_network_record(pifrec['network'])
|
||||||
|
|
||||||
|
if nwrec['bridge']:
|
||||||
|
# TODO: sanity check that nwrec['bridgeless'] != 'true'
|
||||||
|
return nwrec['bridge']
|
||||||
|
else:
|
||||||
|
# TODO: sanity check that nwrec['bridgeless'] == 'true'
|
||||||
|
return pif_netdev_name(pif)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bare Network Devices -- network devices without IP configuration
|
||||||
|
#
|
||||||
|
|
||||||
|
def netdev_exists(netdev):
|
||||||
|
return os.path.exists("/sys/class/net/" + netdev)
|
||||||
|
|
||||||
|
def pif_netdev_name(pif):
|
||||||
|
"""Get the netdev name for a PIF."""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
if pif_is_vlan(pif):
|
||||||
|
return "%(device)s.%(VLAN)s" % pifrec
|
||||||
|
else:
|
||||||
|
return pifrec['device']
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bonded PIFs
|
||||||
|
#
|
||||||
|
def pif_is_bond(pif):
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
return len(pifrec['bond_master_of']) > 0
|
||||||
|
|
||||||
|
def pif_get_bond_masters(pif):
|
||||||
|
"""Returns a list of PIFs which are bond masters of this PIF"""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
bso = pifrec['bond_slave_of']
|
||||||
|
|
||||||
|
# bond-slave-of is currently a single reference but in principle a
|
||||||
|
# PIF could be a member of several bonds which are not
|
||||||
|
# concurrently attached. Be robust to this possibility.
|
||||||
|
if not bso or bso == "OpaqueRef:NULL":
|
||||||
|
bso = []
|
||||||
|
elif not type(bso) == list:
|
||||||
|
bso = [bso]
|
||||||
|
|
||||||
|
bondrecs = [db().get_bond_record(bond) for bond in bso]
|
||||||
|
bondrecs = [rec for rec in bondrecs if rec]
|
||||||
|
|
||||||
|
return [bond['master'] for bond in bondrecs]
|
||||||
|
|
||||||
|
def pif_get_bond_slaves(pif):
|
||||||
|
"""Returns a list of PIFs which make up the given bonded pif."""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
bmo = pifrec['bond_master_of']
|
||||||
|
if len(bmo) > 1:
|
||||||
|
raise Error("Bond-master-of contains too many elements")
|
||||||
|
|
||||||
|
if len(bmo) == 0:
|
||||||
|
return []
|
||||||
|
|
||||||
|
bondrec = db().get_bond_record(bmo[0])
|
||||||
|
if not bondrec:
|
||||||
|
raise Error("No bond record for bond master PIF")
|
||||||
|
|
||||||
|
return bondrec['slaves']
|
||||||
|
|
||||||
|
#
|
||||||
|
# VLAN PIFs
|
||||||
|
#
|
||||||
|
|
||||||
|
def pif_is_vlan(pif):
|
||||||
|
return db().get_pif_record(pif)['VLAN'] != '-1'
|
||||||
|
|
||||||
|
def pif_get_vlan_slave(pif):
|
||||||
|
"""Find the PIF which is the VLAN slave of pif.
|
||||||
|
|
||||||
|
Returns the 'physical' PIF underneath the a VLAN PIF @pif."""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
vlan = pifrec['VLAN_master_of']
|
||||||
|
if not vlan or vlan == "OpaqueRef:NULL":
|
||||||
|
raise Error("PIF is not a VLAN master")
|
||||||
|
|
||||||
|
vlanrec = db().get_vlan_record(vlan)
|
||||||
|
if not vlanrec:
|
||||||
|
raise Error("No VLAN record found for PIF")
|
||||||
|
|
||||||
|
return vlanrec['tagged_PIF']
|
||||||
|
|
||||||
|
def pif_get_vlan_masters(pif):
|
||||||
|
"""Returns a list of PIFs which are VLANs on top of the given pif."""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
vlans = [db().get_vlan_record(v) for v in pifrec['VLAN_slave_of']]
|
||||||
|
return [v['untagged_PIF'] for v in vlans if v and db().pif_exists(v['untagged_PIF'])]
|
||||||
|
|
||||||
|
#
|
||||||
|
# Datapath base class
|
||||||
|
#
|
||||||
|
|
||||||
|
class Datapath(object):
|
||||||
|
"""Object encapsulating the actions necessary to (de)configure the
|
||||||
|
datapath for a given PIF. Does not include configuration of the
|
||||||
|
IP address on the ipdev.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, pif):
|
||||||
|
self._pif = pif
|
||||||
|
|
||||||
|
def configure_ipdev(self, cfg):
|
||||||
|
"""Write ifcfg TYPE field for an IPdev, plus any type specific
|
||||||
|
fields to cfg
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def preconfigure(self, parent):
|
||||||
|
"""Prepare datapath configuration for PIF, but do not actually
|
||||||
|
apply any changes.
|
||||||
|
|
||||||
|
Any configuration files should be attached to parent.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def bring_down_existing(self):
|
||||||
|
"""Tear down any existing network device configuration which
|
||||||
|
needs to be undone in order to bring this PIF up.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def configure(self):
|
||||||
|
"""Apply the configuration prepared in the preconfigure stage.
|
||||||
|
|
||||||
|
Should assume any configuration files changed attached in
|
||||||
|
the preconfigure stage are applied and bring up the
|
||||||
|
necesary devices to provide the datapath for the
|
||||||
|
PIF.
|
||||||
|
|
||||||
|
Should not bring up the IPdev.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def post(self):
|
||||||
|
"""Called after the IPdev has been brought up.
|
||||||
|
|
||||||
|
Should do any final setup, including reinstating any
|
||||||
|
devices which were taken down in the bring_down_existing
|
||||||
|
hook.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def bring_down(self):
|
||||||
|
"""Tear down and deconfigure the datapath. Should assume the
|
||||||
|
IPdev has already been brought down.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def DatapathFactory(pif):
|
||||||
|
# XXX Need a datapath object for bridgeless PIFs
|
||||||
|
|
||||||
|
try:
|
||||||
|
network_conf = open("/etc/xensource/network.conf", 'r')
|
||||||
|
network_backend = network_conf.readline().strip()
|
||||||
|
network_conf.close()
|
||||||
|
except Exception, e:
|
||||||
|
raise Error("failed to determine network backend:" + e)
|
||||||
|
|
||||||
|
if network_backend == "bridge":
|
||||||
|
from InterfaceReconfigureBridge import DatapathBridge
|
||||||
|
return DatapathBridge(pif)
|
||||||
|
elif network_backend == "vswitch":
|
||||||
|
from InterfaceReconfigureVswitch import DatapathVswitch
|
||||||
|
return DatapathVswitch(pif)
|
||||||
|
else:
|
||||||
|
raise Error("unknown network backend %s" % network_backend)
|
497
xenserver/opt_xensource_libexec_InterfaceReconfigureBridge.py
Normal file
497
xenserver/opt_xensource_libexec_InterfaceReconfigureBridge.py
Normal file
@@ -0,0 +1,497 @@
|
|||||||
|
# Copyright (c) 2008,2009 Citrix Systems, Inc.
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Lesser General Public License as published
|
||||||
|
# by the Free Software Foundation; version 2.1 only. with the special
|
||||||
|
# exception on linking described in file LICENSE.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Lesser General Public License for more details.
|
||||||
|
#
|
||||||
|
from InterfaceReconfigure import *
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
sysfs_bonding_masters = "/sys/class/net/bonding_masters"
|
||||||
|
|
||||||
|
def open_pif_ifcfg(pif):
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
interface = pif_netdev_name(pif)
|
||||||
|
log("Configuring %s (%s)" % (interface, pifrec['MAC']))
|
||||||
|
|
||||||
|
f = ConfigurationFile("/etc/sysconfig/network-scripts/ifcfg-%s" % interface)
|
||||||
|
|
||||||
|
f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
|
||||||
|
(os.path.basename(f.path()), os.path.basename(sys.argv[0])))
|
||||||
|
f.write("XEMANAGED=yes\n")
|
||||||
|
f.write("DEVICE=%s\n" % interface)
|
||||||
|
f.write("ONBOOT=no\n")
|
||||||
|
|
||||||
|
return f
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bare Network Devices -- network devices without IP configuration
|
||||||
|
#
|
||||||
|
|
||||||
|
def netdev_down(netdev):
|
||||||
|
"""Bring down a bare network device"""
|
||||||
|
if not netdev_exists(netdev):
|
||||||
|
log("netdev: down: device %s does not exist, ignoring" % netdev)
|
||||||
|
return
|
||||||
|
run_command(["/sbin/ifdown", netdev])
|
||||||
|
|
||||||
|
def netdev_up(netdev, mtu=None):
|
||||||
|
"""Bring up a bare network device"""
|
||||||
|
#if not netdev_exists(netdev):
|
||||||
|
# raise Error("netdev: up: device %s does not exist" % netdev)
|
||||||
|
|
||||||
|
run_command(["/sbin/ifup", netdev])
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bonding driver
|
||||||
|
#
|
||||||
|
|
||||||
|
def load_bonding_driver():
|
||||||
|
log("Loading bonding driver")
|
||||||
|
run_command(["/sbin/modprobe", "bonding"])
|
||||||
|
try:
|
||||||
|
# bond_device_exists() uses the contents of sysfs_bonding_masters to work out which devices
|
||||||
|
# have already been created. Unfortunately the driver creates "bond0" automatically at
|
||||||
|
# modprobe init. Get rid of this now or our accounting will go wrong.
|
||||||
|
f = open(sysfs_bonding_masters, "w")
|
||||||
|
f.write("-bond0")
|
||||||
|
f.close()
|
||||||
|
except IOError, e:
|
||||||
|
log("Failed to load bonding driver: %s" % e)
|
||||||
|
|
||||||
|
def bonding_driver_loaded():
|
||||||
|
lines = open("/proc/modules").read().split("\n")
|
||||||
|
modules = [line.split(" ")[0] for line in lines]
|
||||||
|
return "bonding" in modules
|
||||||
|
|
||||||
|
def bond_device_exists(name):
|
||||||
|
f = open(sysfs_bonding_masters, "r")
|
||||||
|
bonds = f.readline().split()
|
||||||
|
f.close()
|
||||||
|
return name in bonds
|
||||||
|
|
||||||
|
def __create_bond_device(name):
|
||||||
|
|
||||||
|
if not bonding_driver_loaded():
|
||||||
|
load_bonding_driver()
|
||||||
|
|
||||||
|
if bond_device_exists(name):
|
||||||
|
log("bond master %s already exists, not creating" % name)
|
||||||
|
else:
|
||||||
|
log("Creating bond master %s" % name)
|
||||||
|
try:
|
||||||
|
f = open(sysfs_bonding_masters, "w")
|
||||||
|
f.write("+" + name)
|
||||||
|
f.close()
|
||||||
|
except IOError, e:
|
||||||
|
log("Failed to create %s: %s" % (name, e))
|
||||||
|
|
||||||
|
def create_bond_device(pif):
|
||||||
|
"""Ensures that a bond master device exists in the kernel."""
|
||||||
|
|
||||||
|
if not pif_is_bond(pif):
|
||||||
|
return
|
||||||
|
|
||||||
|
__create_bond_device(pif_netdev_name(pif))
|
||||||
|
|
||||||
|
def __destroy_bond_device(name):
|
||||||
|
if bond_device_exists(name):
|
||||||
|
retries = 10 # 10 * 0.5 seconds
|
||||||
|
while retries > 0:
|
||||||
|
retries = retries - 1
|
||||||
|
log("Destroying bond master %s (%d attempts remain)" % (name,retries))
|
||||||
|
try:
|
||||||
|
f = open(sysfs_bonding_masters, "w")
|
||||||
|
f.write("-" + name)
|
||||||
|
f.close()
|
||||||
|
retries = 0
|
||||||
|
except IOError, e:
|
||||||
|
time.sleep(0.5)
|
||||||
|
else:
|
||||||
|
log("bond master %s does not exist, not destroying" % name)
|
||||||
|
|
||||||
|
def destroy_bond_device(pif):
|
||||||
|
"""No, Mr. Bond, I expect you to die."""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
if not pif_is_bond(pif):
|
||||||
|
return
|
||||||
|
|
||||||
|
# If the bonding module isn't loaded then do nothing.
|
||||||
|
if not os.access(sysfs_bonding_masters, os.F_OK):
|
||||||
|
return
|
||||||
|
|
||||||
|
name = pif_netdev_name(pif)
|
||||||
|
|
||||||
|
__destroy_bond_device(name)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bridges
|
||||||
|
#
|
||||||
|
|
||||||
|
def pif_is_bridged(pif):
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
nwrec = db().get_network_record(pifrec['network'])
|
||||||
|
|
||||||
|
if nwrec['bridge']:
|
||||||
|
# TODO: sanity check that nwrec['bridgeless'] != 'true'
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
# TODO: sanity check that nwrec['bridgeless'] == 'true'
|
||||||
|
return False
|
||||||
|
|
||||||
|
def pif_bridge_name(pif):
|
||||||
|
"""Return the bridge name of a pif.
|
||||||
|
|
||||||
|
PIF must be a bridged PIF."""
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
nwrec = db().get_network_record(pifrec['network'])
|
||||||
|
|
||||||
|
if nwrec['bridge']:
|
||||||
|
return nwrec['bridge']
|
||||||
|
else:
|
||||||
|
raise Error("PIF %(uuid)s does not have a bridge name" % pifrec)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bring Interface up/down.
|
||||||
|
#
|
||||||
|
|
||||||
|
def bring_down_interface(pif, destroy=False):
|
||||||
|
"""Bring down the interface associated with PIF.
|
||||||
|
|
||||||
|
Brings down the given interface as well as any physical interfaces
|
||||||
|
which are bond slaves of this one. This is because they will be
|
||||||
|
required when the bond is brought up."""
|
||||||
|
|
||||||
|
def destroy_bridge(pif):
|
||||||
|
"""Bring down the bridge associated with a PIF."""
|
||||||
|
#if not pif_is_bridged(pif):
|
||||||
|
# return
|
||||||
|
bridge = pif_bridge_name(pif)
|
||||||
|
if not netdev_exists(bridge):
|
||||||
|
log("destroy_bridge: bridge %s does not exist, ignoring" % bridge)
|
||||||
|
return
|
||||||
|
log("Destroy bridge %s" % bridge)
|
||||||
|
netdev_down(bridge)
|
||||||
|
run_command(["/usr/sbin/brctl", "delbr", bridge])
|
||||||
|
|
||||||
|
def destroy_vlan(pif):
|
||||||
|
vlan = pif_netdev_name(pif)
|
||||||
|
if not netdev_exists(vlan):
|
||||||
|
log("vconfig del: vlan %s does not exist, ignoring" % vlan)
|
||||||
|
return
|
||||||
|
log("Destroy vlan device %s" % vlan)
|
||||||
|
run_command(["/sbin/vconfig", "rem", vlan])
|
||||||
|
|
||||||
|
if pif_is_vlan(pif):
|
||||||
|
interface = pif_netdev_name(pif)
|
||||||
|
log("bring_down_interface: %s is a VLAN" % interface)
|
||||||
|
netdev_down(interface)
|
||||||
|
|
||||||
|
if destroy:
|
||||||
|
destroy_vlan(pif)
|
||||||
|
destroy_bridge(pif)
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
|
slave = pif_get_vlan_slave(pif)
|
||||||
|
if db().get_pif_record(slave)['currently_attached']:
|
||||||
|
log("bring_down_interface: vlan slave is currently attached")
|
||||||
|
return
|
||||||
|
|
||||||
|
masters = pif_get_vlan_masters(slave)
|
||||||
|
masters = [m for m in masters if m != pif and db().get_pif_record(m)['currently_attached']]
|
||||||
|
if len(masters) > 0:
|
||||||
|
log("bring_down_interface: vlan slave has other masters")
|
||||||
|
return
|
||||||
|
|
||||||
|
log("bring_down_interface: no more masters, bring down vlan slave %s" % pif_netdev_name(slave))
|
||||||
|
pif = slave
|
||||||
|
else:
|
||||||
|
vlan_masters = pif_get_vlan_masters(pif)
|
||||||
|
log("vlan masters of %s - %s" % (db().get_pif_record(pif)['device'], [pif_netdev_name(m) for m in vlan_masters]))
|
||||||
|
if len([m for m in vlan_masters if db().get_pif_record(m)['currently_attached']]) > 0:
|
||||||
|
log("Leaving %s up due to currently attached VLAN masters" % pif_netdev_name(pif))
|
||||||
|
return
|
||||||
|
|
||||||
|
# pif is now either a bond or a physical device which needs to be brought down
|
||||||
|
|
||||||
|
# Need to bring down bond slaves first since the bond device
|
||||||
|
# must be up to enslave/unenslave.
|
||||||
|
bond_slaves = pif_get_bond_slaves_sorted(pif)
|
||||||
|
log("bond slaves of %s - %s" % (db().get_pif_record(pif)['device'], [pif_netdev_name(s) for s in bond_slaves]))
|
||||||
|
for slave in bond_slaves:
|
||||||
|
slave_interface = pif_netdev_name(slave)
|
||||||
|
if db().get_pif_record(slave)['currently_attached']:
|
||||||
|
log("leave bond slave %s up (currently attached)" % slave_interface)
|
||||||
|
continue
|
||||||
|
log("bring down bond slave %s" % slave_interface)
|
||||||
|
netdev_down(slave_interface)
|
||||||
|
# Also destroy the bridge associated with the slave, since
|
||||||
|
# it will carry the MAC address and possibly an IP address
|
||||||
|
# leading to confusion.
|
||||||
|
destroy_bridge(slave)
|
||||||
|
|
||||||
|
interface = pif_netdev_name(pif)
|
||||||
|
log("Bring interface %s down" % interface)
|
||||||
|
netdev_down(interface)
|
||||||
|
|
||||||
|
if destroy:
|
||||||
|
destroy_bond_device(pif)
|
||||||
|
destroy_bridge(pif)
|
||||||
|
|
||||||
|
def interface_is_up(pif):
|
||||||
|
try:
|
||||||
|
interface = pif_netdev_name(pif)
|
||||||
|
state = open("/sys/class/net/%s/operstate" % interface).read().strip()
|
||||||
|
return state == "up"
|
||||||
|
except:
|
||||||
|
return False # interface prolly doesn't exist
|
||||||
|
|
||||||
|
def bring_up_interface(pif):
|
||||||
|
"""Bring up the interface associated with a PIF.
|
||||||
|
|
||||||
|
Also bring up the interfaces listed in additional.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# VLAN on bond seems to need bond brought up explicitly, but VLAN
|
||||||
|
# on normal device does not. Might as well always bring it up.
|
||||||
|
if pif_is_vlan(pif):
|
||||||
|
slave = pif_get_vlan_slave(pif)
|
||||||
|
if not interface_is_up(slave):
|
||||||
|
bring_up_interface(slave)
|
||||||
|
|
||||||
|
interface = pif_netdev_name(pif)
|
||||||
|
|
||||||
|
create_bond_device(pif)
|
||||||
|
|
||||||
|
log("Bring interface %s up" % interface)
|
||||||
|
netdev_up(interface)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Datapath topology configuration.
|
||||||
|
#
|
||||||
|
|
||||||
|
def _configure_physical_interface(pif):
|
||||||
|
"""Write the configuration for a physical interface.
|
||||||
|
|
||||||
|
Writes the configuration file for the physical interface described by
|
||||||
|
the pif object.
|
||||||
|
|
||||||
|
Returns the open file handle for the interface configuration file.
|
||||||
|
"""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
f = open_pif_ifcfg(pif)
|
||||||
|
|
||||||
|
f.write("TYPE=Ethernet\n")
|
||||||
|
f.write("HWADDR=%(MAC)s\n" % pifrec)
|
||||||
|
|
||||||
|
settings,offload = ethtool_settings(pifrec['other_config'])
|
||||||
|
if len(settings):
|
||||||
|
f.write("ETHTOOL_OPTS=\"%s\"\n" % str.join(" ", settings))
|
||||||
|
if len(offload):
|
||||||
|
f.write("ETHTOOL_OFFLOAD_OPTS=\"%s\"\n" % str.join(" ", offload))
|
||||||
|
|
||||||
|
mtu = mtu_setting(pifrec['other_config'])
|
||||||
|
if mtu:
|
||||||
|
f.write("MTU=%s\n" % mtu)
|
||||||
|
|
||||||
|
return f
|
||||||
|
|
||||||
|
def pif_get_bond_slaves_sorted(pif):
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
# build a list of slave's pifs
|
||||||
|
slave_pifs = pif_get_bond_slaves(pif)
|
||||||
|
|
||||||
|
# Ensure any currently attached slaves are listed in the opposite order to the order in
|
||||||
|
# which they were attached. The first slave attached must be the last detached since
|
||||||
|
# the bond is using its MAC address.
|
||||||
|
try:
|
||||||
|
attached_slaves = open("/sys/class/net/%s/bonding/slaves" % pifrec['device']).readline().split()
|
||||||
|
for slave in attached_slaves:
|
||||||
|
pifs = [p for p in db().get_pifs_by_device(slave) if not pif_is_vlan(p)]
|
||||||
|
slave_pif = pifs[0]
|
||||||
|
slave_pifs.remove(slave_pif)
|
||||||
|
slave_pifs.insert(0, slave_pif)
|
||||||
|
except IOError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return slave_pifs
|
||||||
|
|
||||||
|
def _configure_bond_interface(pif):
|
||||||
|
"""Write the configuration for a bond interface.
|
||||||
|
|
||||||
|
Writes the configuration file for the bond interface described by
|
||||||
|
the pif object. Handles writing the configuration for the slave
|
||||||
|
interfaces.
|
||||||
|
|
||||||
|
Returns the open file handle for the bond interface configuration
|
||||||
|
file.
|
||||||
|
"""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
f = open_pif_ifcfg(pif)
|
||||||
|
|
||||||
|
if pifrec['MAC'] != "":
|
||||||
|
f.write("MACADDR=%s\n" % pifrec['MAC'])
|
||||||
|
|
||||||
|
for slave in pif_get_bond_slaves(pif):
|
||||||
|
s = _configure_physical_interface(slave)
|
||||||
|
s.write("MASTER=%(device)s\n" % pifrec)
|
||||||
|
s.write("SLAVE=yes\n")
|
||||||
|
s.close()
|
||||||
|
f.attach_child(s)
|
||||||
|
|
||||||
|
settings,offload = ethtool_settings(pifrec['other_config'])
|
||||||
|
if len(settings):
|
||||||
|
f.write("ETHTOOL_OPTS=\"%s\"\n" % str.join(" ", settings))
|
||||||
|
if len(offload):
|
||||||
|
f.write("ETHTOOL_OFFLOAD_OPTS=\"%s\"\n" % str.join(" ", offload))
|
||||||
|
|
||||||
|
mtu = mtu_setting(pifrec['other_config'])
|
||||||
|
if mtu:
|
||||||
|
f.write("MTU=%s\n" % mtu)
|
||||||
|
|
||||||
|
# The bond option defaults
|
||||||
|
bond_options = {
|
||||||
|
"mode": "balance-slb",
|
||||||
|
"miimon": "100",
|
||||||
|
"downdelay": "200",
|
||||||
|
"updelay": "31000",
|
||||||
|
"use_carrier": "1",
|
||||||
|
}
|
||||||
|
|
||||||
|
# override defaults with values from other-config whose keys being with "bond-"
|
||||||
|
oc = pifrec['other_config']
|
||||||
|
overrides = filter(lambda (key,val): key.startswith("bond-"), oc.items())
|
||||||
|
overrides = map(lambda (key,val): (key[5:], val), overrides)
|
||||||
|
bond_options.update(overrides)
|
||||||
|
|
||||||
|
# write the bond options to ifcfg-bondX
|
||||||
|
f.write('BONDING_OPTS="')
|
||||||
|
for (name,val) in bond_options.items():
|
||||||
|
f.write("%s=%s " % (name,val))
|
||||||
|
f.write('"\n')
|
||||||
|
return f
|
||||||
|
|
||||||
|
def _configure_vlan_interface(pif):
|
||||||
|
"""Write the configuration for a VLAN interface.
|
||||||
|
|
||||||
|
Writes the configuration file for the VLAN interface described by
|
||||||
|
the pif object. Handles writing the configuration for the master
|
||||||
|
interface if necessary.
|
||||||
|
|
||||||
|
Returns the open file handle for the VLAN interface configuration
|
||||||
|
file.
|
||||||
|
"""
|
||||||
|
|
||||||
|
slave = _configure_pif(pif_get_vlan_slave(pif))
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
f = open_pif_ifcfg(pif)
|
||||||
|
f.write("VLAN=yes\n")
|
||||||
|
|
||||||
|
settings,offload = ethtool_settings(pifrec['other_config'])
|
||||||
|
if len(settings):
|
||||||
|
f.write("ETHTOOL_OPTS=\"%s\"\n" % str.join(" ", settings))
|
||||||
|
if len(offload):
|
||||||
|
f.write("ETHTOOL_OFFLOAD_OPTS=\"%s\"\n" % str.join(" ", offload))
|
||||||
|
|
||||||
|
mtu = mtu_setting(pifrec['other_config'])
|
||||||
|
if mtu:
|
||||||
|
f.write("MTU=%s\n" % mtu)
|
||||||
|
|
||||||
|
f.attach_child(slave)
|
||||||
|
|
||||||
|
return f
|
||||||
|
|
||||||
|
def _configure_pif(pif):
|
||||||
|
"""Write the configuration for a PIF object.
|
||||||
|
|
||||||
|
Writes the configuration file the PIF and all dependent
|
||||||
|
interfaces (bond slaves and VLAN masters etc).
|
||||||
|
|
||||||
|
Returns the open file handle for the interface configuration file.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if pif_is_vlan(pif):
|
||||||
|
f = _configure_vlan_interface(pif)
|
||||||
|
elif pif_is_bond(pif):
|
||||||
|
f = _configure_bond_interface(pif)
|
||||||
|
else:
|
||||||
|
f = _configure_physical_interface(pif)
|
||||||
|
|
||||||
|
f.write("BRIDGE=%s\n" % pif_bridge_name(pif))
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
return f
|
||||||
|
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
class DatapathBridge(Datapath):
|
||||||
|
def __init__(self, pif):
|
||||||
|
Datapath.__init__(self, pif)
|
||||||
|
log("Configured for Bridge datapath")
|
||||||
|
|
||||||
|
def configure_ipdev(self, cfg):
|
||||||
|
if pif_is_bridged(self._pif):
|
||||||
|
cfg.write("TYPE=Bridge\n")
|
||||||
|
cfg.write("DELAY=0\n")
|
||||||
|
cfg.write("STP=off\n")
|
||||||
|
cfg.write("PIFDEV=%s\n" % pif_netdev_name(self._pif))
|
||||||
|
else:
|
||||||
|
cfg.write("TYPE=Ethernet\n")
|
||||||
|
|
||||||
|
def preconfigure(self, parent):
|
||||||
|
pf = _configure_pif(self._pif)
|
||||||
|
parent.attach_child(pf)
|
||||||
|
|
||||||
|
def bring_down_existing(self):
|
||||||
|
# Bring down any VLAN masters so that we can reconfigure the slave.
|
||||||
|
for master in pif_get_vlan_masters(self._pif):
|
||||||
|
name = pif_netdev_name(master)
|
||||||
|
log("action_up: bring down vlan master %s" % (name))
|
||||||
|
netdev_down(name)
|
||||||
|
|
||||||
|
# interface-reconfigure is never explicitly called to down a bond master.
|
||||||
|
# However, when we are called to up a slave it is implicit that we are destroying the master.
|
||||||
|
bond_masters = pif_get_bond_masters(self._pif)
|
||||||
|
for master in bond_masters:
|
||||||
|
log("action_up: bring down bond master %s" % (pif_netdev_name(master)))
|
||||||
|
# bring down master
|
||||||
|
bring_down_interface(master, destroy=True)
|
||||||
|
|
||||||
|
# No masters left - now its safe to reconfigure the slave.
|
||||||
|
bring_down_interface(self._pif)
|
||||||
|
|
||||||
|
def configure(self):
|
||||||
|
bring_up_interface(self._pif)
|
||||||
|
|
||||||
|
def post(self):
|
||||||
|
# Bring back any currently-attached VLAN masters
|
||||||
|
for master in [v for v in pif_get_vlan_masters(self._pif) if db().get_pif_record(v)['currently_attached']]:
|
||||||
|
name = pif_netdev_name(master)
|
||||||
|
log("action_up: bring up %s" % (name))
|
||||||
|
netdev_up(name)
|
||||||
|
|
||||||
|
def bring_down(self):
|
||||||
|
bring_down_interface(self._pif, destroy=True)
|
458
xenserver/opt_xensource_libexec_InterfaceReconfigureVswitch.py
Normal file
458
xenserver/opt_xensource_libexec_InterfaceReconfigureVswitch.py
Normal file
@@ -0,0 +1,458 @@
|
|||||||
|
# Copyright (c) 2008,2009 Citrix Systems, Inc.
|
||||||
|
# Copyright (c) 2009 Nicira Networks.
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Lesser General Public License as published
|
||||||
|
# by the Free Software Foundation; version 2.1 only. with the special
|
||||||
|
# exception on linking described in file LICENSE.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Lesser General Public License for more details.
|
||||||
|
#
|
||||||
|
from InterfaceReconfigure import *
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bare Network Devices -- network devices without IP configuration
|
||||||
|
#
|
||||||
|
|
||||||
|
def netdev_down(netdev):
|
||||||
|
"""Bring down a bare network device"""
|
||||||
|
if not netdev_exists(netdev):
|
||||||
|
log("netdev: down: device %s does not exist, ignoring" % netdev)
|
||||||
|
return
|
||||||
|
run_command(["/sbin/ifconfig", netdev, 'down'])
|
||||||
|
|
||||||
|
def netdev_up(netdev, mtu=None):
|
||||||
|
"""Bring up a bare network device"""
|
||||||
|
if not netdev_exists(netdev):
|
||||||
|
raise Error("netdev: up: device %s does not exist" % netdev)
|
||||||
|
|
||||||
|
if mtu:
|
||||||
|
mtu = ["mtu", mtu]
|
||||||
|
else:
|
||||||
|
mtu = []
|
||||||
|
|
||||||
|
run_command(["/sbin/ifconfig", netdev, 'up'] + mtu)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bridges
|
||||||
|
#
|
||||||
|
|
||||||
|
def pif_bridge_name(pif):
|
||||||
|
"""Return the bridge name of a pif.
|
||||||
|
|
||||||
|
PIF must not be a VLAN and must be a bridged PIF."""
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
|
||||||
|
if pif_is_vlan(pif):
|
||||||
|
raise Error("PIF %(uuid)s cannot be a bridge, VLAN is %(VLAN)s" % pifrec)
|
||||||
|
|
||||||
|
nwrec = db().get_network_record(pifrec['network'])
|
||||||
|
|
||||||
|
if nwrec['bridge']:
|
||||||
|
return nwrec['bridge']
|
||||||
|
else:
|
||||||
|
raise Error("PIF %(uuid)s does not have a bridge name" % pifrec)
|
||||||
|
|
||||||
|
#
|
||||||
|
# PIF miscellanea
|
||||||
|
#
|
||||||
|
|
||||||
|
def pif_currently_in_use(pif):
|
||||||
|
"""Determine if a PIF is currently in use.
|
||||||
|
|
||||||
|
A PIF is determined to be currently in use if
|
||||||
|
- PIF.currently-attached is true
|
||||||
|
- Any bond master is currently attached
|
||||||
|
- Any VLAN master is currently attached
|
||||||
|
"""
|
||||||
|
rec = db().get_pif_record(pif)
|
||||||
|
if rec['currently_attached']:
|
||||||
|
log("configure_datapath: %s is currently attached" % (pif_netdev_name(pif)))
|
||||||
|
return True
|
||||||
|
for b in pif_get_bond_masters(pif):
|
||||||
|
if pif_currently_in_use(b):
|
||||||
|
log("configure_datapath: %s is in use by BOND master %s" % (pif_netdev_name(pif),pif_netdev_name(b)))
|
||||||
|
return True
|
||||||
|
for v in pif_get_vlan_masters(pif):
|
||||||
|
if pif_currently_in_use(v):
|
||||||
|
log("configure_datapath: %s is in use by VLAN master %s" % (pif_netdev_name(pif),pif_netdev_name(v)))
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
#
|
||||||
|
# Datapath Configuration
|
||||||
|
#
|
||||||
|
|
||||||
|
def pif_datapath(pif):
|
||||||
|
"""Return the datapath PIF associated with PIF.
|
||||||
|
For a non-VLAN PIF, the datapath name is the bridge name.
|
||||||
|
For a VLAN PIF, the datapath name is the bridge name for the PIF's VLAN slave.
|
||||||
|
"""
|
||||||
|
if pif_is_vlan(pif):
|
||||||
|
return pif_datapath(pif_get_vlan_slave(pif))
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
nwrec = db().get_network_record(pifrec['network'])
|
||||||
|
if not nwrec['bridge']:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return pif
|
||||||
|
|
||||||
|
def datapath_get_physical_pifs(pif):
|
||||||
|
"""Return the PIFs for the physical network device(s) associated with a datapath PIF.
|
||||||
|
For a bond master PIF, these are the bond slave PIFs.
|
||||||
|
For a non-VLAN, non-bond master PIF, the PIF is its own physical device PIF.
|
||||||
|
|
||||||
|
A VLAN PIF cannot be a datapath PIF.
|
||||||
|
"""
|
||||||
|
if pif_is_vlan(pif):
|
||||||
|
# Seems like overkill...
|
||||||
|
raise Error("get-physical-pifs should not get passed a VLAN")
|
||||||
|
elif pif_is_bond(pif):
|
||||||
|
return pif_get_bond_slaves(pif)
|
||||||
|
else:
|
||||||
|
return [pif]
|
||||||
|
|
||||||
|
def datapath_deconfigure_physical(netdev):
|
||||||
|
# The use of [!0-9] keeps an interface of 'eth0' from matching
|
||||||
|
# VLANs attached to eth0 (such as 'eth0.123'), which are distinct
|
||||||
|
# interfaces.
|
||||||
|
return ['--del-match=bridge.*.port=%s' % netdev,
|
||||||
|
'--del-match=port.%s.[!0-9]*' % netdev,
|
||||||
|
'--del-match=bonding.*.slave=%s' % netdev,
|
||||||
|
'--del-match=iface.%s.[!0-9]*' % netdev]
|
||||||
|
|
||||||
|
def datapath_configure_bond(pif,slaves):
|
||||||
|
pifrec = db().get_pif_record(pif)
|
||||||
|
interface = pif_netdev_name(pif)
|
||||||
|
|
||||||
|
argv = ['--del-match=bonding.%s.[!0-9]*' % interface]
|
||||||
|
argv += ["--add=bonding.%s.slave=%s" % (interface, pif_netdev_name(slave))
|
||||||
|
for slave in slaves]
|
||||||
|
argv += ['--add=bonding.%s.fake-iface=true' % interface]
|
||||||
|
|
||||||
|
if pifrec['MAC'] != "":
|
||||||
|
argv += ['--add=port.%s.mac=%s' % (interface, pifrec['MAC'])]
|
||||||
|
|
||||||
|
# Bonding options.
|
||||||
|
bond_options = {
|
||||||
|
"mode": "balance-slb",
|
||||||
|
"miimon": "100",
|
||||||
|
"downdelay": "200",
|
||||||
|
"updelay": "31000",
|
||||||
|
"use_carrier": "1",
|
||||||
|
}
|
||||||
|
# override defaults with values from other-config whose keys
|
||||||
|
# being with "bond-"
|
||||||
|
oc = pifrec['other_config']
|
||||||
|
overrides = filter(lambda (key,val):
|
||||||
|
key.startswith("bond-"), oc.items())
|
||||||
|
overrides = map(lambda (key,val): (key[5:], val), overrides)
|
||||||
|
bond_options.update(overrides)
|
||||||
|
for (name,val) in bond_options.items():
|
||||||
|
argv += ["--add=bonding.%s.%s=%s" % (interface, name, val)]
|
||||||
|
return argv
|
||||||
|
|
||||||
|
def datapath_deconfigure_bond(netdev):
|
||||||
|
# The use of [!0-9] keeps an interface of 'eth0' from matching
|
||||||
|
# VLANs attached to eth0 (such as 'eth0.123'), which are distinct
|
||||||
|
# interfaces.
|
||||||
|
return ['--del-match=bonding.%s.[!0-9]*' % netdev,
|
||||||
|
'--del-match=port.%s.[!0-9]*' % netdev]
|
||||||
|
|
||||||
|
def datapath_deconfigure_ipdev(interface):
|
||||||
|
# The use of [!0-9] keeps an interface of 'eth0' from matching
|
||||||
|
# VLANs attached to eth0 (such as 'eth0.123'), which are distinct
|
||||||
|
# interfaces.
|
||||||
|
return ['--del-match=bridge.*.port=%s' % interface,
|
||||||
|
'--del-match=port.%s.[!0-9]*' % interface,
|
||||||
|
'--del-match=iface.%s.[!0-9]*' % interface,
|
||||||
|
'--del-match=vlan.%s.trunks=*' % interface,
|
||||||
|
'--del-match=vlan.%s.tag=*' % interface]
|
||||||
|
|
||||||
|
def datapath_modify_config(commands):
|
||||||
|
#log("modifying configuration:")
|
||||||
|
#for c in commands:
|
||||||
|
# log(" %s" % c)
|
||||||
|
|
||||||
|
rc = run_command(['/usr/bin/ovs-cfg-mod', '-vANY:console:emer',
|
||||||
|
'-F', '/etc/ovs-vswitchd.conf']
|
||||||
|
+ [c for c in commands if c[0] != '#'] + ['-c'])
|
||||||
|
if not rc:
|
||||||
|
raise Error("Failed to modify vswitch configuration")
|
||||||
|
run_command(['/sbin/service', 'vswitch', 'reload'])
|
||||||
|
return True
|
||||||
|
|
||||||
|
#
|
||||||
|
# Toplevel Datapath Configuration.
|
||||||
|
#
|
||||||
|
|
||||||
|
def configure_datapath(pif):
|
||||||
|
"""Bring up the datapath configuration for PIF.
|
||||||
|
|
||||||
|
Should be careful not to glitch existing users of the datapath, e.g. other VLANs etc.
|
||||||
|
|
||||||
|
Should take care of tearing down other PIFs which encompass common physical devices.
|
||||||
|
|
||||||
|
Returns a tuple containing
|
||||||
|
- A list containing the necessary cfgmod command line arguments
|
||||||
|
- A list of additional devices which should be brought up after
|
||||||
|
the configuration is applied.
|
||||||
|
"""
|
||||||
|
|
||||||
|
cfgmod_argv = []
|
||||||
|
extra_up_ports = []
|
||||||
|
|
||||||
|
bridge = pif_bridge_name(pif)
|
||||||
|
|
||||||
|
physical_devices = datapath_get_physical_pifs(pif)
|
||||||
|
|
||||||
|
# Determine additional devices to deconfigure.
|
||||||
|
#
|
||||||
|
# Given all physical devices which are part of this PIF we need to
|
||||||
|
# consider:
|
||||||
|
# - any additional bond which a physical device is part of.
|
||||||
|
# - any additional physical devices which are part of an additional bond.
|
||||||
|
#
|
||||||
|
# Any of these which are not currently in use should be brought
|
||||||
|
# down and deconfigured.
|
||||||
|
extra_down_bonds = []
|
||||||
|
extra_down_ports = []
|
||||||
|
for p in physical_devices:
|
||||||
|
for bond in pif_get_bond_masters(p):
|
||||||
|
if bond == pif:
|
||||||
|
log("configure_datapath: leaving bond %s up" % pif_netdev_name(bond))
|
||||||
|
continue
|
||||||
|
if bond in extra_down_bonds:
|
||||||
|
continue
|
||||||
|
if db().get_pif_record(bond)['currently_attached']:
|
||||||
|
log("configure_datapath: implicitly tearing down currently-attached bond %s" % pif_netdev_name(bond))
|
||||||
|
|
||||||
|
extra_down_bonds += [bond]
|
||||||
|
|
||||||
|
for s in pif_get_bond_slaves(bond):
|
||||||
|
if s in physical_devices:
|
||||||
|
continue
|
||||||
|
if s in extra_down_ports:
|
||||||
|
continue
|
||||||
|
if pif_currently_in_use(s):
|
||||||
|
continue
|
||||||
|
extra_down_ports += [s]
|
||||||
|
|
||||||
|
log("configure_datapath: bridge - %s" % bridge)
|
||||||
|
log("configure_datapath: physical - %s" % [pif_netdev_name(p) for p in physical_devices])
|
||||||
|
log("configure_datapath: extra ports - %s" % [pif_netdev_name(p) for p in extra_down_ports])
|
||||||
|
log("configure_datapath: extra bonds - %s" % [pif_netdev_name(p) for p in extra_down_bonds])
|
||||||
|
|
||||||
|
# Need to fully deconfigure any bridge which any of the:
|
||||||
|
# - physical devices
|
||||||
|
# - bond devices
|
||||||
|
# - sibling devices
|
||||||
|
# refers to
|
||||||
|
for brpif in physical_devices + extra_down_ports + extra_down_bonds:
|
||||||
|
if brpif == pif:
|
||||||
|
continue
|
||||||
|
b = pif_bridge_name(brpif)
|
||||||
|
#ifdown(b)
|
||||||
|
# XXX
|
||||||
|
netdev_down(b)
|
||||||
|
cfgmod_argv += ['# remove bridge %s' % b]
|
||||||
|
cfgmod_argv += ['--del-match=bridge.%s.*' % b]
|
||||||
|
|
||||||
|
for n in extra_down_ports:
|
||||||
|
dev = pif_netdev_name(n)
|
||||||
|
cfgmod_argv += ['# deconfigure sibling physical device %s' % dev]
|
||||||
|
cfgmod_argv += datapath_deconfigure_physical(dev)
|
||||||
|
netdev_down(dev)
|
||||||
|
|
||||||
|
for n in extra_down_bonds:
|
||||||
|
dev = pif_netdev_name(n)
|
||||||
|
cfgmod_argv += ['# deconfigure bond device %s' % dev]
|
||||||
|
cfgmod_argv += datapath_deconfigure_bond(dev)
|
||||||
|
netdev_down(dev)
|
||||||
|
|
||||||
|
for p in physical_devices:
|
||||||
|
dev = pif_netdev_name(p)
|
||||||
|
cfgmod_argv += ['# deconfigure physical port %s' % dev]
|
||||||
|
cfgmod_argv += datapath_deconfigure_physical(dev)
|
||||||
|
if len(physical_devices) > 1:
|
||||||
|
cfgmod_argv += ['# deconfigure bond %s' % pif_netdev_name(pif)]
|
||||||
|
cfgmod_argv += datapath_deconfigure_bond(pif_netdev_name(pif))
|
||||||
|
cfgmod_argv += ['--del-entry=bridge.%s.port=%s' % (bridge,pif_netdev_name(pif))]
|
||||||
|
cfgmod_argv += ['# configure bond %s' % pif_netdev_name(pif)]
|
||||||
|
cfgmod_argv += datapath_configure_bond(pif, physical_devices)
|
||||||
|
cfgmod_argv += ['--add=bridge.%s.port=%s' % (bridge,pif_netdev_name(pif)) ]
|
||||||
|
extra_up_ports += [pif_netdev_name(pif)]
|
||||||
|
else:
|
||||||
|
iface = pif_netdev_name(physical_devices[0])
|
||||||
|
cfgmod_argv += ['# add physical device %s' % iface]
|
||||||
|
cfgmod_argv += ['--add=bridge.%s.port=%s' % (bridge,iface) ]
|
||||||
|
|
||||||
|
return cfgmod_argv,extra_up_ports
|
||||||
|
|
||||||
|
def deconfigure_datapath(pif):
|
||||||
|
cfgmod_argv = []
|
||||||
|
|
||||||
|
bridge = pif_bridge_name(pif)
|
||||||
|
|
||||||
|
physical_devices = datapath_get_physical_pifs(pif)
|
||||||
|
|
||||||
|
log("deconfigure_datapath: bridge - %s" % bridge)
|
||||||
|
log("deconfigure_datapath: physical devices - %s" % [pif_netdev_name(p) for p in physical_devices])
|
||||||
|
|
||||||
|
for p in physical_devices:
|
||||||
|
dev = pif_netdev_name(p)
|
||||||
|
cfgmod_argv += ['# deconfigure physical port %s' % dev]
|
||||||
|
cfgmod_argv += datapath_deconfigure_physical(dev)
|
||||||
|
netdev_down(dev)
|
||||||
|
|
||||||
|
if len(physical_devices) > 1:
|
||||||
|
cfgmod_argv += ['# deconfigure bond %s' % pif_netdev_name(pif)]
|
||||||
|
cfgmod_argv += datapath_deconfigure_bond(pif_netdev_name(pif))
|
||||||
|
|
||||||
|
cfgmod_argv += ['# deconfigure bridge %s' % bridge]
|
||||||
|
cfgmod_argv += ['--del-match=bridge.%s.*' % bridge]
|
||||||
|
|
||||||
|
return cfgmod_argv
|
||||||
|
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
class DatapathVswitch(Datapath):
|
||||||
|
def __init__(self, pif):
|
||||||
|
Datapath.__init__(self, pif)
|
||||||
|
self._dp = pif_datapath(pif)
|
||||||
|
self._ipdev = pif_ipdev_name(pif)
|
||||||
|
|
||||||
|
if pif_is_vlan(pif) and not self._dp:
|
||||||
|
raise Error("Unbridged VLAN devices not implemented yet")
|
||||||
|
|
||||||
|
log("Configured for Vswitch datapath")
|
||||||
|
|
||||||
|
def configure_ipdev(self, cfg):
|
||||||
|
cfg.write("TYPE=Ethernet\n")
|
||||||
|
|
||||||
|
def preconfigure(self, parent):
|
||||||
|
cfgmod_argv = []
|
||||||
|
extra_ports = []
|
||||||
|
|
||||||
|
pifrec = db().get_pif_record(self._pif)
|
||||||
|
|
||||||
|
ipdev = self._ipdev
|
||||||
|
bridge = pif_bridge_name(self._dp)
|
||||||
|
c,e = configure_datapath(self._dp)
|
||||||
|
cfgmod_argv += c
|
||||||
|
extra_ports += e
|
||||||
|
|
||||||
|
cfgmod_argv += ['# configure xs-network-uuids']
|
||||||
|
cfgmod_argv += ['--del-match=bridge.%s.xs-network-uuids=*' % bridge]
|
||||||
|
|
||||||
|
for nwpif in db().get_pifs_by_device(db().get_pif_record(self._pif)['device']):
|
||||||
|
rec = db().get_pif_record(nwpif)
|
||||||
|
|
||||||
|
# When state is read from dbcache PIF.currently_attached
|
||||||
|
# is always assumed to be false... Err on the side of
|
||||||
|
# listing even detached networks for the time being.
|
||||||
|
#if nwpif != pif and not rec['currently_attached']:
|
||||||
|
# log("Network PIF %s not currently attached (%s)" % (rec['uuid'],pifrec['uuid']))
|
||||||
|
# continue
|
||||||
|
nwrec = db().get_network_record(rec['network'])
|
||||||
|
cfgmod_argv += ['--add=bridge.%s.xs-network-uuids=%s' % (bridge, nwrec['uuid'])]
|
||||||
|
|
||||||
|
cfgmod_argv += ["# deconfigure ipdev %s" % ipdev]
|
||||||
|
cfgmod_argv += datapath_deconfigure_ipdev(ipdev)
|
||||||
|
cfgmod_argv += ["# reconfigure ipdev %s" % ipdev]
|
||||||
|
cfgmod_argv += ['--add=bridge.%s.port=%s' % (bridge, ipdev)]
|
||||||
|
if bridge == ipdev:
|
||||||
|
cfgmod_argv += ['--add=bridge.%s.mac=%s' % (bridge, pifrec['MAC'])]
|
||||||
|
else:
|
||||||
|
cfgmod_argv += ['--add=iface.%s.mac=%s' % (ipdev, pifrec['MAC'])]
|
||||||
|
|
||||||
|
if pif_is_vlan(self._pif):
|
||||||
|
cfgmod_argv += ['--add=vlan.%s.tag=%s' % (ipdev, pifrec['VLAN'])]
|
||||||
|
cfgmod_argv += ['--add=iface.%s.internal=true' % (ipdev)]
|
||||||
|
cfgmod_argv += ['--add=iface.%s.fake-bridge=true' % (ipdev)]
|
||||||
|
|
||||||
|
self._cfgmod_argv = cfgmod_argv
|
||||||
|
self._extra_ports = extra_ports
|
||||||
|
|
||||||
|
def bring_down_existing(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def configure(self):
|
||||||
|
# Bring up physical devices. ovs-vswitchd initially enables or
|
||||||
|
# disables bond slaves based on whether carrier is detected
|
||||||
|
# when they are added, and a network device that is down
|
||||||
|
# always reports "no carrier".
|
||||||
|
physical_devices = datapath_get_physical_pifs(self._dp)
|
||||||
|
|
||||||
|
for p in physical_devices:
|
||||||
|
oc = db().get_pif_record(p)['other_config']
|
||||||
|
|
||||||
|
dev = pif_netdev_name(p)
|
||||||
|
|
||||||
|
mtu = mtu_setting(oc)
|
||||||
|
|
||||||
|
netdev_up(dev, mtu)
|
||||||
|
|
||||||
|
settings, offload = ethtool_settings(oc)
|
||||||
|
if len(settings):
|
||||||
|
run_command(['/sbin/ethtool', '-s', dev] + settings)
|
||||||
|
if len(offload):
|
||||||
|
run_command(['/sbin/ethtool', '-K', dev] + offload)
|
||||||
|
|
||||||
|
datapath_modify_config(self._cfgmod_argv)
|
||||||
|
|
||||||
|
def post(self):
|
||||||
|
for p in self._extra_ports:
|
||||||
|
log("action_up: bring up %s" % p)
|
||||||
|
netdev_up(p)
|
||||||
|
|
||||||
|
def bring_down(self):
|
||||||
|
cfgmod_argv = []
|
||||||
|
|
||||||
|
dp = self._dp
|
||||||
|
ipdev = self._ipdev
|
||||||
|
|
||||||
|
bridge = pif_bridge_name(dp)
|
||||||
|
|
||||||
|
#nw = db().get_pif_record(self._pif)['network']
|
||||||
|
#nwrec = db().get_network_record(nw)
|
||||||
|
#cfgmod_argv += ['# deconfigure xs-network-uuids']
|
||||||
|
#cfgmod_argv += ['--del-entry=bridge.%s.xs-network-uuids=%s' % (bridge,nwrec['uuid'])]
|
||||||
|
|
||||||
|
log("deconfigure ipdev %s on %s" % (ipdev,bridge))
|
||||||
|
cfgmod_argv += ["# deconfigure ipdev %s" % ipdev]
|
||||||
|
cfgmod_argv += datapath_deconfigure_ipdev(ipdev)
|
||||||
|
|
||||||
|
if pif_is_vlan(self._pif):
|
||||||
|
# If the VLAN's slave is attached, leave datapath setup.
|
||||||
|
slave = pif_get_vlan_slave(self._pif)
|
||||||
|
if db().get_pif_record(slave)['currently_attached']:
|
||||||
|
log("action_down: vlan slave is currently attached")
|
||||||
|
dp = None
|
||||||
|
|
||||||
|
# If the VLAN's slave has other VLANs that are attached, leave datapath setup.
|
||||||
|
for master in pif_get_vlan_masters(slave):
|
||||||
|
if master != self._pif and db().get_pif_record(master)['currently_attached']:
|
||||||
|
log("action_down: vlan slave has other master: %s" % pif_netdev_name(master))
|
||||||
|
dp = None
|
||||||
|
|
||||||
|
# Otherwise, take down the datapath too (fall through)
|
||||||
|
if dp:
|
||||||
|
log("action_down: no more masters, bring down slave %s" % bridge)
|
||||||
|
else:
|
||||||
|
# Stop here if this PIF has attached VLAN masters.
|
||||||
|
masters = [db().get_pif_record(m)['VLAN'] for m in pif_get_vlan_masters(self._pif) if db().get_pif_record(m)['currently_attached']]
|
||||||
|
if len(masters) > 0:
|
||||||
|
log("Leaving datapath %s up due to currently attached VLAN masters %s" % (bridge, masters))
|
||||||
|
dp = None
|
||||||
|
|
||||||
|
if dp:
|
||||||
|
cfgmod_argv += deconfigure_datapath(dp)
|
||||||
|
datapath_modify_config(cfgmod_argv)
|
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
# Spec file for vswitch and related programs.
|
# Spec file for vswitch and related programs.
|
||||||
|
|
||||||
# Copyright (C) 2009 Nicira Networks, Inc.
|
# Copyright (C) 2009, 2010 Nicira Networks, Inc.
|
||||||
#
|
#
|
||||||
# Copying and distribution of this file, with or without modification,
|
# Copying and distribution of this file, with or without modification,
|
||||||
# are permitted in any medium without royalty provided the copyright
|
# are permitted in any medium without royalty provided the copyright
|
||||||
@@ -69,6 +69,12 @@ install -m 755 xenserver/etc_xapi.d_plugins_vswitch-cfg-update \
|
|||||||
install -d -m 755 $RPM_BUILD_ROOT/usr/share/vswitch/scripts
|
install -d -m 755 $RPM_BUILD_ROOT/usr/share/vswitch/scripts
|
||||||
install -m 755 xenserver/opt_xensource_libexec_interface-reconfigure \
|
install -m 755 xenserver/opt_xensource_libexec_interface-reconfigure \
|
||||||
$RPM_BUILD_ROOT/usr/share/vswitch/scripts/interface-reconfigure
|
$RPM_BUILD_ROOT/usr/share/vswitch/scripts/interface-reconfigure
|
||||||
|
install -m 755 xenserver/opt_xensource_libexec_InterfaceReconfigure.py \
|
||||||
|
$RPM_BUILD_ROOT/usr/share/vswitch/scripts/InterfaceReconfigure.py
|
||||||
|
install -m 755 xenserver/opt_xensource_libexec_InterfaceReconfigureBridge.py \
|
||||||
|
$RPM_BUILD_ROOT/usr/share/vswitch/scripts/InterfaceReconfigureBridge.py
|
||||||
|
install -m 755 xenserver/opt_xensource_libexec_InterfaceReconfigureVswitch.py \
|
||||||
|
$RPM_BUILD_ROOT/usr/share/vswitch/scripts/InterfaceReconfigureVswitch.py
|
||||||
install -m 755 xenserver/etc_xensource_scripts_vif \
|
install -m 755 xenserver/etc_xensource_scripts_vif \
|
||||||
$RPM_BUILD_ROOT/usr/share/vswitch/scripts/vif
|
$RPM_BUILD_ROOT/usr/share/vswitch/scripts/vif
|
||||||
install -m 755 xenserver/usr_share_vswitch_scripts_dump-vif-details \
|
install -m 755 xenserver/usr_share_vswitch_scripts_dump-vif-details \
|
||||||
@@ -209,13 +215,16 @@ mkdir -p /usr/lib/vswitch/xs-original \
|
|||||||
|| printf "Could not create script backup directory.\n"
|
|| printf "Could not create script backup directory.\n"
|
||||||
for f in \
|
for f in \
|
||||||
/opt/xensource/libexec/interface-reconfigure \
|
/opt/xensource/libexec/interface-reconfigure \
|
||||||
|
/opt/xensource/libexec/InterfaceReconfigure.py \
|
||||||
|
/opt/xensource/libexec/InterfaceReconfigureBridge.py \
|
||||||
|
/opt/xensource/libexec/InterfaceReconfigureVswitch.py \
|
||||||
/etc/xensource/scripts/vif \
|
/etc/xensource/scripts/vif \
|
||||||
/usr/sbin/xen-bugtool \
|
/usr/sbin/xen-bugtool \
|
||||||
/usr/sbin/brctl
|
/usr/sbin/brctl
|
||||||
do
|
do
|
||||||
s=$(basename "$f")
|
s=$(basename "$f")
|
||||||
t=$(readlink "$f")
|
t=$(readlink "$f")
|
||||||
if [ "$t" != "/usr/share/vswitch/scripts/$s" ]; then
|
if [ -f "$f" ] && [ "$t" != "/usr/share/vswitch/scripts/$s" ]; then
|
||||||
mv "$f" /usr/lib/vswitch/xs-original/ \
|
mv "$f" /usr/lib/vswitch/xs-original/ \
|
||||||
|| printf "Could not save original XenServer $s script\n"
|
|| printf "Could not save original XenServer $s script\n"
|
||||||
ln -s "/usr/share/vswitch/scripts/$s" "$f" \
|
ln -s "/usr/share/vswitch/scripts/$s" "$f" \
|
||||||
@@ -239,6 +248,9 @@ for s in vswitch vswitch-xapi-update; do
|
|||||||
chkconfig $s on || printf "Could not enable $s init script."
|
chkconfig $s on || printf "Could not enable $s init script."
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Configure system to use vswitch
|
||||||
|
echo vswitch > /etc/xensource/network.conf
|
||||||
|
|
||||||
if [ "$1" = "1" ]; then # $1 = 2 for upgrade
|
if [ "$1" = "1" ]; then # $1 = 2 for upgrade
|
||||||
printf "\nYou MUST reboot the server NOW to complete the change to the\n"
|
printf "\nYou MUST reboot the server NOW to complete the change to the\n"
|
||||||
printf "the vSwitch. Attempts to modify networking on the server\n"
|
printf "the vSwitch. Attempts to modify networking on the server\n"
|
||||||
@@ -289,6 +301,9 @@ if [ "$1" = "0" ]; then # $1 = 1 for upgrade
|
|||||||
rm -f /etc/ovs-vswitchd.cacert
|
rm -f /etc/ovs-vswitchd.cacert
|
||||||
rm -f /var/lib/openvswitch/dbcache
|
rm -f /var/lib/openvswitch/dbcache
|
||||||
|
|
||||||
|
# Configure system to use bridge
|
||||||
|
echo bridge > /etc/xensource/network.conf
|
||||||
|
|
||||||
printf "\nYou MUST reboot the server now to complete the change to\n"
|
printf "\nYou MUST reboot the server now to complete the change to\n"
|
||||||
printf "standard Xen networking. Attempts to modify networking on the\n"
|
printf "standard Xen networking. Attempts to modify networking on the\n"
|
||||||
printf "server or any hosted VM will fail until after the reboot and\n"
|
printf "server or any hosted VM will fail until after the reboot and\n"
|
||||||
@@ -308,6 +323,15 @@ fi
|
|||||||
/usr/share/vswitch/scripts/dump-vif-details
|
/usr/share/vswitch/scripts/dump-vif-details
|
||||||
/usr/share/vswitch/scripts/refresh-xs-network-uuids
|
/usr/share/vswitch/scripts/refresh-xs-network-uuids
|
||||||
/usr/share/vswitch/scripts/interface-reconfigure
|
/usr/share/vswitch/scripts/interface-reconfigure
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigure.py
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigure.pyc
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigure.pyo
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigureBridge.py
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigureBridge.pyc
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigureBridge.pyo
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigureVswitch.py
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigureVswitch.pyc
|
||||||
|
/usr/share/vswitch/scripts/InterfaceReconfigureVswitch.pyo
|
||||||
/usr/share/vswitch/scripts/vif
|
/usr/share/vswitch/scripts/vif
|
||||||
/usr/share/vswitch/scripts/xen-bugtool
|
/usr/share/vswitch/scripts/xen-bugtool
|
||||||
/usr/share/vswitch/scripts/XSFeatureVSwitch.py
|
/usr/share/vswitch/scripts/XSFeatureVSwitch.py
|
||||||
|
Reference in New Issue
Block a user