xenserver: Update interface-reconfigure and vif integration scripts.
authorIan Campbell <Ian.Campbell@citrix.com>
Fri, 22 Jan 2010 20:55:37 +0000 (12:55 -0800)
committerBen Pfaff <blp@nicira.com>
Mon, 25 Jan 2010 18:36:08 +0000 (10:36 -0800)
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.)

xenserver/automake.mk
xenserver/etc_xensource_scripts_vif
xenserver/opt_xensource_libexec_InterfaceReconfigure.py [new file with mode: 0644]
xenserver/opt_xensource_libexec_InterfaceReconfigureBridge.py [new file with mode: 0644]
xenserver/opt_xensource_libexec_InterfaceReconfigureVswitch.py [new file with mode: 0644]
xenserver/opt_xensource_libexec_interface-reconfigure
xenserver/vswitch-xen.spec

index 221925cda5508900d616b55b82095d5ad6e06caa..1b930454d795884362553b67b97a4bb0c6651058 100644 (file)
@@ -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,
 # are permitted in any medium without royalty provided the copyright
@@ -6,18 +6,23 @@
 # without warranty of any kind.
 
 EXTRA_DIST += \
+       xenserver/LICENSE \
        xenserver/README \
+       xenserver/automake.mk \
        xenserver/etc_init.d_vswitch \
        xenserver/etc_init.d_vswitch-xapi-update \
        xenserver/etc_logrotate.d_vswitch \
        xenserver/etc_profile.d_vswitch.sh \
        xenserver/etc_xapi.d_plugins_vswitch-cfg-update \
        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/usr_lib_xsconsole_plugins-base_XSFeatureVSwitch.py \
        xenserver/usr_sbin_brctl \
        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_refresh-xs-network-uuids \
+       xenserver/usr_share_vswitch_scripts_sysconfig.template \
        xenserver/vswitch-xen.spec
index c649e926b0e436e8623645253f15358c0b57afbc..91131598b1b56047fb6d6744ac5a89413ed8e251 100755 (executable)
@@ -1,7 +1,6 @@
 #!/bin/sh
 
 # 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
 # it under the terms of the GNU Lesser General Public License as published
 
 # 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"
 vsctl="/usr/bin/ovs-vsctl"
-dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
 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()
 {
-    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
-        case "${arg}" in 
-            true|on) logger -t script-vif "${vif}: Promiscuous ports are not supported via vSwitch." ;;
-            *) ;;
-        esac
+       case $NETWORK_MODE in
+           bridge)
+               case "${arg}" in 
+                   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
 }
 
 handle_ethtool()
 {
     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
         case "${arg}" in
-            true|on)   /sbin/ethtool -K "${vif}" "${opt}" on ;;
-            false|off) /sbin/ethtool -K "${vif}" "${opt}" off ;;
-            *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${vif}/${VIFUUID}" ;;
+            true|on)   /sbin/ethtool -K "${dev}" "${opt}" on ;;
+            false|off) /sbin/ethtool -K "${dev}" "${opt}" off ;;
+            *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${dev}/${VIFUUID}" ;;
         esac
     fi
 }
 
 handle_mtu()
 {
-    local mtu=$(xenstore-read "${PRIVATE}/MTU")
+    local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
     if [ $? -eq 0 -a -n "${mtu}" ]; then
-       echo "${mtu}" > /sys/class/net/${vif}/mtu
+       echo "${mtu}" > /sys/class/net/${dev}/mtu
     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()
 {
     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
     if [ $? -ne 0 -o -z "${address}" ]; then
        logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
+       exit 1
     fi
     local bridge=$(xenstore-read "${PRIVATE}/bridge")
     if [ $? -ne 0 -o -z "${bridge}" ]; then
        logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
+       exit 1
     fi
-    logger -t scripts-vif "Adding ${vif} to ${bridge} with address ${address}"
+    logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"
+
+    ${IP} link set "${dev}" down                        || logger -t scripts-vif "Failed to ip link set ${dev} down"
+    ${IP} link set "${dev}" arp off                     || logger -t scripts-vif "Failed to ip link set ${dev} arp off"
+    ${IP} link set "${dev}" multicast off               || logger -t scripts-vif "Failed to ip link set ${dev} multicast off"
+    ${IP} link set "${dev}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${dev} address ${address}"
+    ${IP} addr flush "${dev}"                           || logger -t scripts-vif "Failed to ip addr flush ${dev}"
+
+    case $NETWORK_MODE in
+       bridge)
+           ${BRCTL} setfd "${bridge}" 0                        || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
+           ${BRCTL} addif "${bridge}" "${dev}"                 || logger -t scripts-vif "Failed to brctl addif ${bridge} ${dev}"
+           ;;
+       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
+
+           if [ "$TYPE" = "vif" ] ; then
+               local vif_details=$(handle_vswitch_vif_details)
+           fi
+
+           $cfg_mod -F /etc/ovs-vswitchd.conf \
+               --del-match="bridge.*.port=${dev}" \
+               --del-match="vlan.${dev}.trunks=*" \
+               --del-match="vlan.${dev}.tag=*" \
+               --del-match="port.${dev}.[!0-9]*" \
+               --add="bridge.$bridge.port=${dev}" \
+               $vid $vif_details -c 
+           $service vswitch reload
+           ;;
+    esac
+           
+    ${IP} link set "${dev}" up                          || logger -t scripts-vif "Failed to ip link set ${dev} up"
+}
 
-    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.${vif}.tag=${VLAN_ID}"
-    fi
+remove_from_bridge()
+{
+    case $NETWORK_MODE in
+       bridge)
+           # Nothing to do
+           ;;
+       vswitch)
+           $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
+               --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
+}
 
-    ${IP} link set "${vif}" down                        || logger -t scripts-vif "Failed to ip link set ${vif} down"
-    ${IP} link set "${vif}" arp off                     || logger -t scripts-vif "Failed to ip link set ${vif} arp off"
-    ${IP} link set "${vif}" multicast off               || logger -t scripts-vif "Failed to ip link set ${vif} multicast off"
-    ${IP} link set "${vif}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${vif} address ${address}"
-    ${IP} addr flush "${vif}"                           || logger -t scripts-vif "Failed to ip addr flush ${vif}"
+NETWORK_MODE=$(cat /etc/xensource/network.conf)
+ACTION=$1
+TYPE=$2
 
-    local vif_details=$($dump_vif_details $DOMID $DEVID)
-    if [ $? -ne 0 -o -z "${vif_details}" ]; then
-           logger -t scripts-vif "Failed to retrieve vif details for vswitch"
-    fi
+case $NETWORK_MODE in
+    bridge|vswitch) ;;
+    *)
+       logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
+       exit 1
+       ;;
+esac
 
-    $cfg_mod -F /etc/ovs-vswitchd.conf \
-        --del-match="bridge.*.port=$vif" \
-        --del-match="vlan.$vif.trunks=*" \
-        --del-match="vlan.$vif.tag=*" \
-        --del-match="port.$vif.[!0-9]*" \
-        --add="bridge.$bridge.port=$vif" \
-        $vid $vif_details -c 
-    $service vswitch reload
+case ${TYPE} in
+    vif)
+       DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
+       DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
+       dev=vif${DOMID}.${DEVID}
+       ;;
+    tap)
+       dev=$INTERFACE
+       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
 
-    ${IP} link set "${vif}" up                          || logger -t scripts-vif "Failed to ip link set ${vif} up"
-}
+XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
+HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
+PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
 
-echo Called as "$@" "$TYPE" "$DOMID" "$DEVID" | logger -t scripts-vif
-case "$1" in
+logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
+case "${ACTION}" in
 online)
-       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" "${vif}"
-       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"
+       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)
-       xenstore-rm "${HOTPLUG}/hotplug"
-       vif=vif${DOMID}.${DEVID}
-       logger -t scripts-vif "${vif} has been removed"
-       $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
-           --del-match="bridge.*.port=${vif}" \
-           --del-match="vlan.${vif}.trunks=*" \
-           --del-match="vlan.${vif}.tag=*" \
-           --del-match="port.${vif}.[!0-9]*" -c
-       $service vswitch reload
+       if [ "${TYPE}" = "vif" ] ;then
+           xenstore-rm "${HOTPLUG}/hotplug"
+       fi
+       logger -t scripts-vif "${dev} has been removed"
+       remove_from_bridge
        ;;
 esac
diff --git a/xenserver/opt_xensource_libexec_InterfaceReconfigure.py b/xenserver/opt_xensource_libexec_InterfaceReconfigure.py
new file mode 100644 (file)
index 0000000..229f3b9
--- /dev/null
@@ -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)
diff --git a/xenserver/opt_xensource_libexec_InterfaceReconfigureBridge.py b/xenserver/opt_xensource_libexec_InterfaceReconfigureBridge.py
new file mode 100644 (file)
index 0000000..0fa9322
--- /dev/null
@@ -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)
diff --git a/xenserver/opt_xensource_libexec_InterfaceReconfigureVswitch.py b/xenserver/opt_xensource_libexec_InterfaceReconfigureVswitch.py
new file mode 100644 (file)
index 0000000..6f093a4
--- /dev/null
@@ -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)
index 2b6f62b46dd9ddf1be560ecbe73b5740e283e29b..4455731f43415f278851b880f80010ca9be14ea5 100755 (executable)
@@ -1,7 +1,6 @@
 #!/usr/bin/python
 #
 # 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
 
     %(command-name)s <PIF> up
     %(command-name)s <PIF> down
-    %(command-name)s [<PIF>] rewrite
+    %(command-name)s rewrite
     %(command-name)s --force <BRIDGE> up
     %(command-name)s --force <BRIDGE> down
-    %(command-name)s --force <BRIDGE> rewrite --device=<INTERFACE> <CONFIG>
-    %(command-name)s --force all down
+    %(command-name)s --force <BRIDGE> rewrite --device=<INTERFACE> --mac=<MAC-ADDRESS> <CONFIG>
 
     where <PIF> is one of:
        --session <SESSION-REF> --pif <PIF-REF>
        --mode=static --ip=<IPADDR> --netmask=<NM> [--gateway=<GW>]
 
   Options:
-    --session          A session reference to use to access the xapi DB
+    --session           A session reference to use to access the xapi DB
     --pif               A PIF reference within the session.
     --pif-uuid          The UUID of a PIF.
     --force             An interface name.
 """
 
-#
-# Undocumented parameters for test & dev:
-#
-#  --output-directory=<DIR>    Write configuration to <DIR>. Also disables actually
-#                               raising/lowering the interfaces
-#
-#
-#
 # Notes:
 # 1. Every pif belongs to exactly one network
 # 2. Every network has zero or one pifs
 # 3. A network may have an associated bridge, allowing vifs to be attached
 # 4. A network may be bridgeless (there's no point having a bridge over a storage pif)
 
-import XenAPI
-import os, sys, getopt, time, signal
+from InterfaceReconfigure import *
+
+import os, sys, getopt
 import syslog
 import traceback
 import re
 import random
-from xml.dom.minidom import getDOMImplementation
-from xml.dom.minidom import parse as parseXML
 
-output_directory = None
-
-db = None
 management_pif = None
 
-vswitch_state_dir = "/var/lib/openvswitch/"
-dbcache_file = vswitch_state_dir + "dbcache"
+dbcache_file = "/var/lib/openvswitch/dbcache"
 
 #
-# Debugging and Logging.
+# Logging.
 #
 
-def debug_mode():
-    return output_directory is not None
-
-def log(s):
-    if debug_mode():
-        print >>sys.stderr, s
-    else:
-        syslog.syslog(s)
-
 def log_pif_action(action, pif):
-    pifrec = db.get_pif_record(pif)
+    pifrec = db().get_pif_record(pif)
     rec = {}
     rec['uuid'] = pifrec['uuid']
     rec['ip_configuration_mode'] = pifrec['ip_configuration_mode']
@@ -91,15 +67,6 @@ def log_pif_action(action, pif):
     rec['message'] = "Bring %(action)s PIF %(uuid)s" % rec
     log("%(message)s: %(pif_netdev_name)s configured as %(ip_configuration_mode)s" % rec)
 
-
-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
-
 #
 # Exceptions.
 #
@@ -109,521 +76,6 @@ class Usage(Exception):
         Exception.__init__(self)
         self.msg = msg
 
-class Error(Exception):
-    def __init__(self, msg):
-        Exception.__init__(self)
-        self.msg = msg
-
-#
-# 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, fname, path="/etc/sysconfig/network-scripts"):
-
-        self.__state = self.__STATE['OPEN']
-        self.__fname = fname
-        self.__children = []
-
-        if debug_mode():
-            dirname = output_directory
-        else:
-            dirname = path
-
-        self.__path    = os.path.join(dirname, fname)
-        self.__oldpath = os.path.join(dirname, "." + fname + ".xapi-old")
-        self.__newpath = os.path.join(dirname, "." + fname + ".xapi-new")
-        self.__unlink = False
-
-        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 unlink(self):
-        if self.__state != self.__STATE['OPEN']:
-            raise Error("Attempt to unlink file in state %s" % self.__state)
-        self.__unlink = True
-        self.__f.close()
-        self.__state = self.__STATE['NOT-APPLIED']
-
-    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.__fname)
-
-        # 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))
-        if not self.__unlink:
-            os.link(self.__newpath, self.__path)
-        else:
-            pass # implicit unlink of original file
-
-        # 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.__fname)
-
-        # 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.__fname)
-
-        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)),
-                }
-
-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:
-            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\" (get_pif_record)" % 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_all_networks(self):
-        return self.__networks
-
-    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
-
 #
 # Boot from Network filesystem or device.
 #
@@ -634,7 +86,7 @@ def check_allowed(pif):
     Used to prevent system PIFs (such as network root disk) from being interfered with.
     """
 
-    pifrec = db.get_pif_record(pif)
+    pifrec = db().get_pif_record(pif)
     try:
         f = open("/proc/ardence")
         macline = filter(lambda x: x.startswith("HWaddr:"), f.readlines())
@@ -652,48 +104,12 @@ def check_allowed(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']
-
-def netdev_down(netdev):
-    """Bring down a bare network device"""
-    if debug_mode():
-        return
-    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 debug_mode():
-        return
-    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)
-
 def netdev_remap_name(pif, already_renamed=[]):
     """Check whether 'pif' exists and has the correct MAC.
     If not, try to find a device with the correct MAC and rename it.
     'already_renamed' is used to avoid infinite recursion.
     """
-    
+
     def read1(name):
         file = None
         try:
@@ -731,7 +147,7 @@ def netdev_remap_name(pif, already_renamed=[]):
         if not run_command(['/sbin/ip', 'link', 'set', old_name, 'name', new_name]):
             raise Error("Could not rename %s to %s" % (old_name, new_name))
 
-    pifrec = db.get_pif_record(pif)
+    pifrec = db().get_pif_record(pif)
     device = pifrec['device']
     mac = pifrec['MAC']
 
@@ -764,205 +180,39 @@ def netdev_remap_name(pif, already_renamed=[]):
 # 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)
-
 def ifdown(netdev):
     """Bring down a network interface"""
-    if debug_mode():
-        return
     if not netdev_exists(netdev):
         log("ifdown: device %s does not exist, ignoring" % netdev)
         return
     if not os.path.exists("/etc/sysconfig/network-scripts/ifcfg-%s" % netdev):
-        log("ifdown: device %s exists but ifcfg %s does not" % (netdev,netdev))
-        netdev_down(netdev)
+        log("ifdown: device %s exists but ifcfg-%s does not" % (netdev,netdev))
+        run_command(["/sbin/ifconfig", netdev, 'down'])
+        return
     run_command(["/sbin/ifdown", netdev])
 
 def ifup(netdev):
     """Bring up a network interface"""
-    if debug_mode():
-        return
-    if not netdev_exists(netdev):
-        raise Error("ifup: device %s does not exist, ignoring" % netdev)
     if not os.path.exists("/etc/sysconfig/network-scripts/ifcfg-%s" % netdev):
         raise Error("ifup: device %s exists but ifcfg-%s does not" % (netdev,netdev))
     run_command(["/sbin/ifup", netdev])
 
 #
-# 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)
+def pif_rename_physical_devices(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'])
+        pif = pif_get_vlan_slave(pif)
 
-    if nwrec['bridge']:
-        return nwrec['bridge']
+    if pif_is_bond(pif):
+        pifs = pif_get_bond_slaves(pif)
     else:
-        raise Error("PIF %(uuid)s does not have a bridge name" % pifrec)
+        pifs = [pif]
 
-#
-# 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
-
-#
-#
-#
-
-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
-
-#
-# Bonded PIFs
-#
-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'])]
+    for pif in pifs:
+        netdev_remap_name(pif)
 
 #
 # IP device configuration
@@ -981,7 +231,6 @@ def ipdev_configure_static_routes(interface, oc, f):
           172.16.0.0/15 via 192.168.0.3 dev xenbr1
           172.18.0.0/16 via 192.168.0.4 dev xenbr1
     """
-    fname = "route-%s" % interface
     if oc.has_key('static-routes'):
         # The key is present - extract comma seperates entries
         lines = oc['static-routes'].split(',')
@@ -989,7 +238,7 @@ def ipdev_configure_static_routes(interface, oc, f):
         # The key is not present, i.e. there are no static routes
         lines = []
 
-    child = ConfigurationFile(fname)
+    child = ConfigurationFile("/etc/sysconfig/network-scripts/route-%s" % interface)
     child.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
             (os.path.basename(child.path()), os.path.basename(sys.argv[0])))
 
@@ -1009,7 +258,7 @@ def ipdev_open_ifcfg(pif):
 
     log("Writing network configuration for %s" % ipdev)
 
-    f = ConfigurationFile("ifcfg-%s" % ipdev)
+    f = ConfigurationFile("/etc/sysconfig/network-scripts/ifcfg-%s" % ipdev)
 
     f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
             (os.path.basename(f.path()), os.path.basename(sys.argv[0])))
@@ -1019,7 +268,7 @@ def ipdev_open_ifcfg(pif):
 
     return f
 
-def ipdev_configure_network(pif):
+def ipdev_configure_network(pif, dp):
     """Write the configuration file for a network.
 
     Writes configuration derived from the network object into the relevant
@@ -1031,11 +280,11 @@ def ipdev_configure_network(pif):
 
     params:
         pif:  Opaque_ref of pif
-        f :   ConfigurationFile(/path/to/ifcfg) to which we append network configuration
+        dp:   Datapath object
     """
 
-    pifrec = db.get_pif_record(pif)
-    nwrec = db.get_network_record(pifrec['network'])
+    pifrec = db().get_pif_record(pif)
+    nwrec = db().get_network_record(pifrec['network'])
 
     ipdev = pif_ipdev_name(pif)
 
@@ -1048,7 +297,8 @@ def ipdev_configure_network(pif):
     if pifrec.has_key('other_config'):
         oc = pifrec['other_config']
 
-    f.write("TYPE=Ethernet\n")
+    dp.configure_ipdev(f)
+
     if pifrec['ip_configuration_mode'] == "DHCP":
         f.write("BOOTPROTO=dhcp\n")
         f.write("PERSISTENT_DHCLIENT=yes\n")
@@ -1081,8 +331,7 @@ def ipdev_configure_network(pif):
     if oc and oc.has_key('domain'):
         f.write("DOMAIN='%s'\n" % oc['domain'].replace(',', ' '))
 
-    # We only allow one ifcfg-* to have PEERDNS=yes and there can be
-    # only one GATEWAYDEV in /etc/sysconfig/network.
+    # There can be only one DNSDEV and one GATEWAYDEV in /etc/sysconfig/network.
     #
     # The peerdns pif will be the one with
     # pif::other-config:peerdns=true, or the mgmt pif if none have
@@ -1092,37 +341,35 @@ def ipdev_configure_network(pif):
     # pif::other-config:defaultroute=true, or the mgmt pif if none
     # have this set.
 
-    # Work out which pif on this host should be the one with
-    # PEERDNS=yes and which should be the GATEWAYDEV
+    # Work out which pif on this host should be the DNSDEV and which
+    # should be the GATEWAYDEV
     #
-    # Note: we prune out the bond master pif (if it exists).  This is
+    # Note: we prune out the bond master pif (if it exists). This is
     # because when we are called to bring up an interface with a bond
     # master, it is implicit that we should bring down that master.
-    pifs_on_host = [ __pif for __pif in db.get_all_pifs() if
-                     not __pif in pif_get_bond_masters(pif) ]
-    other_pifs_on_host = [ __pif for __pif in pifs_on_host if __pif != pif ]
 
-    peerdns_pif = None
-    defaultroute_pif = None
+    pifs_on_host = [p for p in db().get_all_pifs() if not p in pif_get_bond_masters(pif)]
 
     # loop through all the pifs on this host looking for one with
     #   other-config:peerdns = true, and one with
     #   other-config:default-route=true
+    peerdns_pif = None
+    defaultroute_pif = None
     for __pif in pifs_on_host:
-        __pifrec = db.get_pif_record(__pif)
+        __pifrec = db().get_pif_record(__pif)
         __oc = __pifrec['other_config']
         if __oc.has_key('peerdns') and __oc['peerdns'] == 'true':
             if peerdns_pif == None:
                 peerdns_pif = __pif
             else:
                 log('Warning: multiple pifs with "peerdns=true" - choosing %s and ignoring %s' % \
-                        (db.get_pif_record(peerdns_pif)['device'], __pifrec['device']))
+                        (db().get_pif_record(peerdns_pif)['device'], __pifrec['device']))
         if __oc.has_key('defaultroute') and __oc['defaultroute'] == 'true':
             if defaultroute_pif == None:
                 defaultroute_pif = __pif
             else:
                 log('Warning: multiple pifs with "defaultroute=true" - choosing %s and ignoring %s' % \
-                        (db.get_pif_record(defaultroute_pif)['device'], __pifrec['device']))
+                        (db().get_pif_record(defaultroute_pif)['device'], __pifrec['device']))
 
     # If no pif is explicitly specified then use the mgmt pif for
     # peerdns/defaultroute.
@@ -1131,370 +378,67 @@ def ipdev_configure_network(pif):
     if defaultroute_pif == None:
         defaultroute_pif = management_pif
 
-    # Update all the other network's ifcfg files and ensure
-    # consistency.
-    for __pif in other_pifs_on_host:
-        __f = ipdev_open_ifcfg(__pif)
-        peerdns_line_wanted = 'PEERDNS=%s\n' % ((__pif == peerdns_pif) and 'yes' or 'no')
-        lines =  __f.readlines()
-
-        if not peerdns_line_wanted in lines:
-            # the PIF selected for DNS has changed and as a result this ifcfg file needs rewriting
-            for line in lines:
-                if not line.lstrip().startswith('PEERDNS'):
-                    __f.write(line)
-            log("Setting %s in %s" % (peerdns_line_wanted.strip(), __f.path()))
-            __f.write(peerdns_line_wanted)
-            __f.close()
-            f.attach_child(__f)
-
-        else:
-            # There is no need to change this ifcfg file.  So don't attach_child.
-            pass
-
-    # ... and for this pif too
-    f.write('PEERDNS=%s\n' % ((pif == peerdns_pif) and 'yes' or 'no'))
-
-    # Update gatewaydev
-    fnetwork = ConfigurationFile("network", "/etc/sysconfig")
-    for line in fnetwork.readlines():
-        if line.lstrip().startswith('GATEWAY') :
-            continue
-        fnetwork.write(line)
-    if defaultroute_pif:
-        gatewaydev = pif_ipdev_name(defaultroute_pif)
-        if not gatewaydev:
-            gatewaydev = pif_netdev_name(defaultroute_pif)
-        fnetwork.write('GATEWAYDEV=%s\n' % gatewaydev)
-    fnetwork.close()
-    f.attach_child(fnetwork)
-
-    return f
-
-#
-# Datapath Configuration
-#
-
-def pif_datapath(pif):
-    """Return the OpenFlow datapath name 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']:
-        raise Error("datapath PIF cannot be bridgeless")
-    else:
-        return pif
+    is_dnsdev = peerdns_pif == pif
+    is_gatewaydev = defaultroute_pif == pif
+
+    if is_dnsdev or is_gatewaydev:
+        fnetwork = ConfigurationFile("/etc/sysconfig/network")
+        for line in fnetwork.readlines():
+            if is_dnsdev and line.lstrip().startswith('DNSDEV='):
+                fnetwork.write('DNSDEV=%s\n' % ipdev)
+                is_dnsdev = False
+            elif is_gatewaydev and line.lstrip().startswith('GATEWAYDEV='):
+                fnetwork.write('GATEWAYDEV=%s\n' % ipdev)
+                is_gatewaydev = False
+            else:
+                fnetwork.write(line)
 
-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.
+        if is_dnsdev:
+            fnetwork.write('DNSDEV=%s\n' % ipdev)
+        if is_gatewaydev:
+            fnetwork.write('GATEWAYDEV=%s\n' % ipdev)
 
-A VLAN PIF cannot be a datapath PIF.
-"""
-    pifrec = db.get_pif_record(pif)
+        fnetwork.close()
+        f.attach_child(fnetwork)
 
-    if pif_is_vlan(pif):
-        raise Error("get-physical-pifs should not get passed a VLAN")
-    elif len(pifrec['bond_master_of']) != 0:
-        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):
-    if debug_mode():
-        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
+    return f
 
 #
-# Toplevel Datapath Configuration.
+# Toplevel actions
 #
 
-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)
+def action_up(pif, force):
+    pifrec = db().get_pif_record(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)
-        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)
-
-    # Check the MAC address of each network device and remap if
-    # necessary to make names match our expectations.
-    for p in physical_devices:
-        netdev_remap_name(p)
-
-    # Bring up physical devices early, because 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".
-    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)
-
-    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])
+    ipdev = pif_ipdev_name(pif)
+    dp = DatapathFactory(pif)
 
-    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)
+    log("action_up: %s" % ipdev)
 
-    if len(physical_devices) > 1:
-        cfgmod_argv += ['# deconfigure bond %s' % pif_netdev_name(pif)]
-        cfgmod_argv += datapath_deconfigure_bond(pif_netdev_name(pif))
+    f = ipdev_configure_network(pif, dp)
 
-    cfgmod_argv += ['# deconfigure bridge %s' % bridge]
-    cfgmod_argv += ['--del-match=bridge.%s.*' % bridge]
-    
-    return cfgmod_argv
+    dp.preconfigure(f)
 
-#
-# Toplevel actions
-#
+    f.close()
 
-def action_up(pif):
-    pifrec = db.get_pif_record(pif)
-    cfgmod_argv = []
-    extra_ports = []
+    pif_rename_physical_devices(pif)
 
-    ipdev = pif_ipdev_name(pif)
-    dp = pif_datapath(pif)
-    bridge = pif_bridge_name(dp)
+    # if we are not forcing the interface up then attempt to tear down
+    # any existing devices which might interfere with brinign this one
+    # up.
+    if not force:
+        ifdown(ipdev)
 
-    log("action_up: %s on bridge %s" % (ipdev, bridge))
-    
-    ifdown(ipdev)
-
-    if dp:
-        c,e = configure_datapath(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(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)]
-
-    f = ipdev_configure_network(pif)
-    f.close()
+        dp.bring_down_existing()
 
-    # /etc/xensource/scripts/vif needs to know where to add VIFs.
-    if pif_is_vlan(pif):
-        if not bridge:
-            raise Error("Unbridged VLAN devices not implemented yet")
-        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)]
-        
-    # Apply updated configuration.
     try:
         f.apply()
 
-        datapath_modify_config(cfgmod_argv)
+        dp.configure()
 
         ifup(ipdev)
 
-        for p in extra_ports:
-            netdev_up(p)
+        dp.post()
 
         # Update /etc/issue (which contains the IP address of the management interface)
         os.system("/sbin/update-issue")
@@ -1506,74 +450,106 @@ def action_up(pif):
         raise
 
 def action_down(pif):
-    pifrec = db.get_pif_record(pif)
-    cfgmod_argv = []
-
     ipdev = pif_ipdev_name(pif)
-    dp = pif_datapath(pif)
-    bridge = pif_bridge_name(dp)
-    
-    log("action_down: %s on bridge %s" % (ipdev, bridge))
-
-    ifdown(ipdev)
+    dp = DatapathFactory(pif)
 
-    if dp:
-        nw = db.get_pif_record(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("action_down: %s" % ipdev)
 
-        log("deconfigure ipdev %s on %s" % (ipdev,bridge))
-        cfgmod_argv += ["# deconfigure ipdev %s" % ipdev]
-        cfgmod_argv += datapath_deconfigure_ipdev(ipdev)
+    ifdown(ipdev)
 
-    f = ipdev_open_ifcfg(pif)
-    f.unlink()
+    dp.bring_down()
 
-    if pif_is_vlan(pif):
-        br = ConfigurationFile("br-%s" % bridge, vswitch_state_dir)
-        br.unlink()
-        f.attach_child(br)
-
-        # If the VLAN's slave is attached, leave datapath setup.
-        slave = pif_get_vlan_slave(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 != 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(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
+# This is useful for reconfiguring the mgmt interface after having lost connectivity to the pool master
+def action_force_rewrite(bridge, config):
+    def getUUID():
+        import subprocess
+        uuid,_ = subprocess.Popen(['uuidgen'], stdout = subprocess.PIPE).communicate()
+        return uuid.strip()
 
-    if dp:
-        cfgmod_argv += deconfigure_datapath(dp)
+    # Notes:
+    # 1. that this assumes the interface is bridged
+    # 2. If --gateway is given it will make that the default gateway for the host
 
+    # extract the configuration
     try:
-        f.apply()
+        mode = config['mode']
+        mac = config['mac']
+        interface = config['device']
+    except:
+        raise Usage("Please supply --mode, --mac and --device")
 
-        datapath_modify_config(cfgmod_argv)
+    if mode == 'static':
+        try:
+            netmask = config['netmask']
+            ip = config['ip']
+        except:
+            raise Usage("Please supply --netmask and --ip")
+        try:
+            gateway = config['gateway']
+        except:
+            gateway = None
+    elif mode != 'dhcp':
+        raise Usage("--mode must be either static or dhcp")
 
-        f.commit()
-    except Error, e:
-        log("action_down failed to apply changes: %s" % e.msg)
-        f.revert()
-        raise
+    if config.has_key('vlan'):
+        is_vlan = True
+        vlan_slave, vlan_vid = config['vlan'].split('.')
+    else:
+        is_vlan = False
+
+    if is_vlan:
+        raise Error("Force rewrite of VLAN not implemented")
+
+    log("Configuring %s using %s configuration" % (bridge, mode))
+
+    f = ConfigurationFile(dbcache_file)
+
+    pif_uuid = getUUID()
+    network_uuid = getUUID()
+
+    f.write('<?xml version="1.0" ?>\n')
+    f.write('<xenserver-network-configuration>\n')
+    f.write('\t<pif ref="OpaqueRef:%s">\n' % pif_uuid)
+    f.write('\t\t<network>OpaqueRef:%s</network>\n' % network_uuid)
+    f.write('\t\t<management>True</management>\n')
+    f.write('\t\t<uuid>%sPif</uuid>\n' % interface)
+    f.write('\t\t<bond_slave_of>OpaqueRef:NULL</bond_slave_of>\n')
+    f.write('\t\t<bond_master_of/>\n')
+    f.write('\t\t<VLAN_slave_of/>\n')
+    f.write('\t\t<VLAN_master_of>OpaqueRef:NULL</VLAN_master_of>\n')
+    f.write('\t\t<VLAN>-1</VLAN>\n')
+    f.write('\t\t<device>%s</device>\n' % interface)
+    f.write('\t\t<MAC>%s</MAC>\n' % mac)
+    f.write('\t\t<other_config/>\n')
+    if mode == 'dhcp':
+        f.write('\t\t<ip_configuration_mode>DHCP</ip_configuration_mode>\n')
+        f.write('\t\t<IP></IP>\n')
+        f.write('\t\t<netmask></netmask>\n')
+        f.write('\t\t<gateway></gateway>\n')
+        f.write('\t\t<DNS></DNS>\n')
+    elif mode == 'static':
+        f.write('\t\t<ip_configuration_mode>Static</ip_configuration_mode>\n')
+        f.write('\t\t<IP>%s</IP>\n' % ip)
+        f.write('\t\t<netmask>%s</netmask>\n' % netmask)
+        if gateway is not None:
+            f.write('\t\t<gateway>%s</gateway>\n' % gateway)
+        f.write('\t\t<DNS></DNS>\n')
+    else:
+        raise Error("Unknown mode %s" % mode)
+    f.write('\t</pif>\n')
+
+    f.write('\t<network ref="OpaqueRef:%s">\n' % network_uuid)
+    f.write('\t\t<uuid>InitialManagementNetwork</uuid>\n')
+    f.write('\t\t<PIFs>\n')
+    f.write('\t\t\t<PIF>OpaqueRef:%s</PIF>\n' % pif_uuid)
+    f.write('\t\t</PIFs>\n')
+    f.write('\t\t<bridge>%s</bridge>\n' % bridge)
+    f.write('\t\t<other_config/>\n')
+    f.write('\t</network>\n')
+    f.write('</xenserver-network-configuration>\n')
 
-def action_rewrite(pif):
-    f = ipdev_configure_network(pif)
     f.close()
+
     try:
         f.apply()
         f.commit()
@@ -1582,11 +558,8 @@ def action_rewrite(pif):
         f.revert()
         raise
 
-def action_force_rewrite(bridge, config):
-    raise Error("Force rewrite is not implemented yet.")
-
 def main(argv=None):
-    global output_directory, management_pif
+    global management_pif
 
     session = None
     pif_uuid = None
@@ -1601,13 +574,12 @@ def main(argv=None):
     try:
         try:
             shortops = "h"
-            longops = [ "output-directory=",
-                        "pif=", "pif-uuid=",
+            longops = [ "pif=", "pif-uuid=",
                         "session=",
                         "force=",
                         "force-interface=",
                         "management",
-                        "device=", "mode=", "ip=", "netmask=", "gateway=",
+                        "mac=", "device=", "mode=", "ip=", "netmask=", "gateway=",
                         "help" ]
             arglist, args = getopt.gnu_getopt(argv[1:], shortops, longops)
         except getopt.GetoptError, msg:
@@ -1616,9 +588,7 @@ def main(argv=None):
         force_rewrite_config = {}
 
         for o,a in arglist:
-            if o == "--output-directory":
-                output_directory = a
-            elif o == "--pif":
+            if o == "--pif":
                 pif = a
             elif o == "--pif-uuid":
                 pif_uuid = a
@@ -1628,15 +598,15 @@ def main(argv=None):
                 force_interface = a
             elif o == "--management":
                 force_management = True
-            elif o in ["--device", "--mode", "--ip", "--netmask", "--gateway"]:
+            elif o in ["--mac", "--device", "--mode", "--ip", "--netmask", "--gateway"]:
                 force_rewrite_config[o[2:]] = a
             elif o == "-h" or o == "--help":
                 print __doc__ % {'command-name': os.path.basename(argv[0])}
                 return 0
 
-        if not debug_mode():
-            syslog.openlog(os.path.basename(argv[0]))
-            log("Called as " + str.join(" ", argv))
+        syslog.openlog(os.path.basename(argv[0]))
+        log("Called as " + str.join(" ", argv))
+
         if len(args) < 1:
             raise Usage("Required option <action> not present")
         if len(args) > 1:
@@ -1650,19 +620,17 @@ def main(argv=None):
         # backwards compatibility
         if action == "rewrite-configuration": action = "rewrite"
 
-        if output_directory and ( session or pif ):
-            raise Usage("--session/--pif cannot be used with --output-directory")
         if ( session or pif ) and pif_uuid:
             raise Usage("--session/--pif and --pif-uuid are mutually exclusive.")
         if ( session and not pif ) or ( not session and pif ):
             raise Usage("--session and --pif must be used together.")
         if force_interface and ( session or pif or pif_uuid ):
             raise Usage("--force is mutually exclusive with --session, --pif and --pif-uuid")
-        if force_interface == "all" and action != "down":
-            raise Usage("\"--force all\" only valid for down action")
         if len(force_rewrite_config) and not (force_interface and action == "rewrite"):
             raise Usage("\"--force rewrite\" needed for --device, --mode, --ip, --netmask, and --gateway")
-
+        if (action == "rewrite") and (pif or pif_uuid ):
+            raise Usage("rewrite action does not take --pif or --pif-uuid")
+        
         global db
         if force_interface:
             log("Force interface %s %s" % (force_interface, action))
@@ -1670,26 +638,23 @@ def main(argv=None):
             if action == "rewrite":
                 action_force_rewrite(force_interface, force_rewrite_config)
             elif action in ["up", "down"]:
-                if action == "down" and force_interface == "all":
-                    raise Error("Force all interfaces down not implemented yet")
-
-                db = DatabaseCache(cache_file=dbcache_file)
-                pif = db.get_pif_by_bridge(force_interface)
-                management_pif = db.get_management_pif()
+                db_init_from_cache(dbcache_file)
+                pif = db().get_pif_by_bridge(force_interface)
+                management_pif = db().get_management_pif()
 
                 if action == "up":
-                    action_up(pif)
+                    action_up(pif, True)
                 elif action == "down":
                     action_down(pif)
             else:
                 raise Error("Unknown action %s"  % action)
         else:
-            db = DatabaseCache(session_ref=session)
+            db_init_from_xenapi(session)
 
             if pif_uuid:
-                pif = db.get_pif_by_uuid(pif_uuid)
+                pif = db().get_pif_by_uuid(pif_uuid)
 
-            if action == "rewrite" and not pif:
+            if action == "rewrite":
                 pass
             else:
                 if not pif:
@@ -1701,8 +666,8 @@ def main(argv=None):
                 else:
                     # pif is not going to be the management pif.
                     # Search DB cache for pif on same host with management=true
-                    pifrec = db.get_pif_record(pif)
-                    management_pif = db.get_management_pif()
+                    pifrec = db().get_pif_record(pif)
+                    management_pif = db().get_management_pif()
 
                 log_pif_action(action, pif)
 
@@ -1710,16 +675,14 @@ def main(argv=None):
                     return 0
 
                 if action == "up":
-                    action_up(pif)
+                    action_up(pif, False)
                 elif action == "down":
                     action_down(pif)
-                elif action == "rewrite":
-                    action_rewrite(pif)
                 else:
                     raise Error("Unknown action %s"  % action)
 
             # Save cache.
-            db.save(dbcache_file)
+            db().save(dbcache_file)
 
     except Usage, err:
         print >>sys.stderr, err.msg
@@ -1741,7 +704,6 @@ if __name__ == "__main__":
         for exline in err:
             log(exline)
 
-    if not debug_mode():
-        syslog.closelog()
+    syslog.closelog()
 
     sys.exit(rc)
index d8e32e48f5cef210c5fea99839a3a8f870d31a31..f6c94570182250dd463c2b3897e29e2f898c9d1b 100644 (file)
@@ -1,6 +1,6 @@
 # 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,
 # 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 -m 755 xenserver/opt_xensource_libexec_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 \
              $RPM_BUILD_ROOT/usr/share/vswitch/scripts/vif
 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"
 for f in \
     /opt/xensource/libexec/interface-reconfigure \
+    /opt/xensource/libexec/InterfaceReconfigure.py \
+    /opt/xensource/libexec/InterfaceReconfigureBridge.py \
+    /opt/xensource/libexec/InterfaceReconfigureVswitch.py \
     /etc/xensource/scripts/vif \
     /usr/sbin/xen-bugtool \
     /usr/sbin/brctl
 do
     s=$(basename "$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/ \
             || printf "Could not save original XenServer $s script\n"
         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."
 done
 
+# Configure system to use vswitch
+echo vswitch > /etc/xensource/network.conf
+
 if [ "$1" = "1" ]; then    # $1 = 2 for upgrade
     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"
@@ -289,6 +301,9 @@ if [ "$1" = "0" ]; then     # $1 = 1 for upgrade
     rm -f /etc/ovs-vswitchd.cacert
     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 "standard Xen networking.  Attempts to modify networking on the\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/refresh-xs-network-uuids
 /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/xen-bugtool
 /usr/share/vswitch/scripts/XSFeatureVSwitch.py