xenserver: Remove dead store from interface-reconfigure.
[openvswitch] / xenserver / opt_xensource_libexec_interface-reconfigure
index 98bcf4c3e707556d4d81947ec9e7ef15e7bda00f..5681d02d2f6ea7b9abd598065046b0c1eb42e70e 100755 (executable)
 # 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)
 
-# XXX: --force-interface=all down
-
-# XXX: --force-interface rewrite
-
-# XXX: Sometimes this leaves "orphaned" datapaths, e.g. a datapath whose
-#      only port is the local port.  Should delete those.
-
-# XXX: This can leave crud in ovs-vswitchd.conf in this scenario:
-#      - Create bond in XenCenter.
-#      - Create VLAN on bond in XenCenter.
-#      - Attempt to delete bond in XenCenter (this will fail because there
-#        is a VLAN on the bond, although the error may not be reported
-#        until the next step)
-#      - Delete VLAN in XenCenter.
-#      - Delete bond in XenCenter.
-# At this point there will still be some configuration data for the bond
-# or the VLAN in ovs-vswitchd.conf.
-
 import XenAPI
 import os, sys, getopt, time, signal
 import syslog
@@ -71,8 +53,8 @@ output_directory = None
 db = None
 management_pif = None
 
-dbcache_file = "/etc/ovs-vswitch.dbcache"
-vswitch_config_dir = "/etc/openvswitch"
+vswitch_state_dir = "/var/lib/openvswitch/"
+dbcache_file = vswitch_state_dir + "dbcache"
 
 class Usage(Exception):
     def __init__(self, msg):
@@ -266,16 +248,12 @@ def get_netdev_tx_queue_len(device):
         return None
 
 def get_netdev_by_mac(mac):
-    maybe = None
     for device in os.listdir("/sys/class/net"):
         dev_mac = get_netdev_mac(device)
-        if dev_mac and mac.lower() == dev_mac.lower():
-            if get_netdev_tx_queue_len(device):
+        if (dev_mac and mac.lower() == dev_mac.lower() and
+            get_netdev_tx_queue_len(device)):
                 return device
-            if not maybe:
-                # Probably a datapath internal port.
-                maybe = device
-    return maybe
+    return None
 
 #
 # Helper functions for encoding/decoding database attributes to/from XML.
@@ -374,6 +352,12 @@ PIF_ATTRS = { 'uuid': (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),
             }
 
 PIF_OTHERCONFIG_ATTRS = [ 'domain', 'peerdns', 'defaultroute', 'mtu', 'static-routes' ] + \
@@ -454,7 +438,11 @@ class DatabaseCache(object):
             rec = session.xenapi.network.get_record(n)
             self.__networks[n] = {}
             for f in NETWORK_ATTRS:
-                self.__networks[n][f] = rec[f]
+                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
@@ -690,7 +678,7 @@ For a non-VLAN, non-bond master PIF, the PIF is its own physical device PIF.
     pifrec = db.get_pif_record(pif)
 
     if pifrec['VLAN'] != '-1':
-        return [get_vlan_slave_of_pif(pif)]
+        return get_physdev_pifs(get_vlan_slave_of_pif(pif))
     elif len(pifrec['bond_master_of']) != 0:
         return get_bond_slaves_of_pif(pif)
     else:
@@ -1135,9 +1123,9 @@ def action_up(pif):
 
     # /etc/xensource/scripts/vif needs to know where to add VIFs.
     if vlan_slave:
-        if not os.path.exists(vswitch_config_dir):
-            os.mkdir(vswitch_config_dir)
-        br = ConfigurationFile("br-%s" % bridge, vswitch_config_dir)
+        if not os.path.exists(vswitch_state_dir):
+            os.mkdir(vswitch_state_dir)
+        br = ConfigurationFile("br-%s" % bridge, vswitch_state_dir)
         br.write("VLAN_SLAVE=%s\n" % datapath)
         br.write("VLAN_VID=%s\n" % pifrec['VLAN'])
         br.close()
@@ -1237,7 +1225,7 @@ def action_up(pif):
         argv += ['--add=iface.%s.fake-bridge=true' % (ipdev)]
     else:
         try:
-            os.unlink("%s/br-%s" % (vswitch_config_dir, bridge))
+            os.unlink("%s/br-%s" % (vswitch_state_dir, bridge))
         except OSError:
             pass
     argv += ['--del-match=bridge.%s.xs-network-uuids=*' % datapath]
@@ -1503,7 +1491,6 @@ def main(argv=None):
                 raise Usage("Unknown action %s"  % action)
 
             # Save cache.
-            pifrec = db.get_pif_record(pif)
             db.save(dbcache_file)
         
     except Usage, err: