ia64/xen-unstable

changeset 14968:55d7745bd377

[XEND] Fix up PIF class, make it work

signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
author Tom Wilkie <tom.wilkie@gmail.com>
date Fri Apr 27 12:49:17 2007 +0100 (2007-04-27)
parents 6049b33d10e1
children 499e50c3115f
files tools/python/xen/xend/XendPIF.py
line diff
     1.1 --- a/tools/python/xen/xend/XendPIF.py	Fri Apr 27 11:03:47 2007 +0100
     1.2 +++ b/tools/python/xen/xend/XendPIF.py	Fri Apr 27 12:49:17 2007 +0100
     1.3 @@ -93,7 +93,22 @@ def linux_set_mtu(iface, mtu):
     1.4  def _create_VLAN(dev, vlan):
     1.5      rc, _ = commands.getstatusoutput('vconfig add %s %d' %
     1.6                                       (dev, vlan))
     1.7 -    return rc == 0   
     1.8 +    if rc != 0:
     1.9 +        return False
    1.10 +
    1.11 +    rc, _ = commands.getstatusoutput('ifconfig %s.%d up' %
    1.12 +                                     (dev, vlan))
    1.13 +    return rc == 0
    1.14 +
    1.15 +def _destroy_VLAN(dev, vlan):
    1.16 +    rc, _ = commands.getstatusoutput('ifconfig %s.%d down' %
    1.17 +                                     (dev, vlan))
    1.18 +    if rc != 0:
    1.19 +        return False
    1.20 +                                     
    1.21 +    rc, _ = commands.getstatusoutput('vconfig rem %s.%d' %
    1.22 +                                     (dev, vlan))
    1.23 +    return rc == 0
    1.24  
    1.25  class XendPIF(XendBase):
    1.26      """Representation of a Physical Network Interface."""
    1.27 @@ -105,13 +120,13 @@ class XendPIF(XendBase):
    1.28          attrRO = ['network',
    1.29                    'host',
    1.30                    'metrics',
    1.31 -                  'device']
    1.32 +                  'device',
    1.33 +                  'VLAN']
    1.34          return XendBase.getAttrRO() + attrRO
    1.35      
    1.36      def getAttrRW(self):
    1.37          attrRW = ['MAC',
    1.38 -                  'MTU',
    1.39 -                  'VLAN']
    1.40 +                  'MTU']
    1.41          return XendBase.getAttrRW() + attrRW
    1.42  
    1.43      def getAttrInst(self):
    1.44 @@ -124,7 +139,8 @@ class XendPIF(XendBase):
    1.45  
    1.46      def getMethods(self):
    1.47          methods = ['plug',
    1.48 -                   'unplug']
    1.49 +                   'unplug',
    1.50 +                   'destroy']
    1.51          return XendBase.getMethods() + methods
    1.52  
    1.53      def getFuncs(self):
    1.54 @@ -189,12 +205,13 @@ class XendPIF(XendBase):
    1.55          ifs = [dev for dev, _1, _2 in linux_get_phy_ifaces()]
    1.56          if pif.get_VLAN() == -1:
    1.57              if pif.get_device() not in ifs:
    1.58 -                del pif
    1.59 -                del metrics
    1.60 +                pif.destroy()
    1.61 +                metrics.destroy()
    1.62                  return None
    1.63          else:
    1.64              if pif.get_interface_name() not in ifs:
    1.65                  _create_VLAN(pif.get_device(), pif.get_VLAN())
    1.66 +                pif.plug()
    1.67  
    1.68          return pif_uuid
    1.69  
    1.70 @@ -218,12 +235,12 @@ class XendPIF(XendBase):
    1.71              raise DeviceExistsError("%s.%d" % (device, vlan))
    1.72  
    1.73          # Check network ref is valid
    1.74 -        from xen.xend import XendNode
    1.75 -        network_uuids = XendNode.instance().networks
    1.76 -        if network_uuid in network_uuids:
    1.77 -            raise InvalidHandleError("Network", network_ref)
    1.78 +        from XendNetwork import XendNetwork
    1.79 +        if network_uuid not in XendNetwork.get_all():
    1.80 +            raise InvalidHandleError("Network", network_uuid)
    1.81  
    1.82          # Check host_ref is this host
    1.83 +        import XendNode
    1.84          if host_ref != XendNode.instance().get_uuid():
    1.85              raise InvalidHandleError("Host", host_ref)
    1.86  
    1.87 @@ -239,20 +256,18 @@ class XendPIF(XendBase):
    1.88              "device":  device,
    1.89              "MAC":     '',
    1.90              "MTU":     '',
    1.91 -            "network": network_ref,
    1.92 +            "network": network_uuid,
    1.93              "VLAN":    vlan
    1.94              }
    1.95  
    1.96          # Create instances
    1.97          metrics = XendPIFMetrics(metrics_uuid, pif_uuid)
    1.98          pif = XendPIF(record, pif_uuid, metrics_uuid)
    1.99 -        
   1.100 -        # Add it to list of PIFs
   1.101 -        XendNode.instance().pifs.append(pif_ref)        
   1.102  
   1.103 -        # Add it to network
   1.104 -        network.add_pif(pif_ref)
   1.105 +        # Not sure if they should be created plugged or not...
   1.106 +        pif.plug()
   1.107  
   1.108 +        XendNode.instance().save_PIFs()
   1.109          return pif_uuid
   1.110  
   1.111      create_phy  = classmethod(create_phy)
   1.112 @@ -269,6 +284,7 @@ class XendPIF(XendBase):
   1.113                                     "network")
   1.114          bridge_name = network.get_name_label()
   1.115  
   1.116 +        from xen.util import Brctl
   1.117          Brctl.vif_bridge_add({
   1.118              "bridge": bridge_name,
   1.119              "vif":    self.get_interface_name()
   1.120 @@ -276,7 +292,30 @@ class XendPIF(XendBase):
   1.121  
   1.122      def unplug(self):
   1.123          """Unplug the PIF from the network"""
   1.124 -        pass
   1.125 +        network = XendAPIStore.get(self.network,
   1.126 +                                   "network")
   1.127 +        bridge_name = network.get_name_label()
   1.128 +
   1.129 +        from xen.util import Brctl
   1.130 +        Brctl.vif_bridge_rem({
   1.131 +            "bridge": bridge_name,
   1.132 +            "vif":    self.get_interface_name()
   1.133 +            })
   1.134 +
   1.135 +    def destroy(self):
   1.136 +        # Figure out if this is a physical device
   1.137 +        if self.get_interface_name() == \
   1.138 +           self.get_device():
   1.139 +            raise PIFIsPhysical(self.get_uuid())
   1.140 +
   1.141 +        self.unplug()
   1.142 +
   1.143 +        if _destroy_VLAN(self.get_device(), self.get_VLAN()):
   1.144 +            XendBase.destroy(self)
   1.145 +            import XendNode
   1.146 +            XendNode.instance().save_PIFs()
   1.147 +        else:
   1.148 +            raise NetworkError("Unable to delete VLAN", self.get_uuid())
   1.149  
   1.150      def get_interface_name(self):
   1.151          if self.get_VLAN() == -1:
   1.152 @@ -310,6 +349,8 @@ class XendPIF(XendBase):
   1.153          success = linux_set_mac(self.device, new_mac)
   1.154          if success:
   1.155              self.MAC = new_mac
   1.156 +            import XendNode
   1.157 +            XendNode.instance().save_PIFs()
   1.158          return success
   1.159  
   1.160      def get_MTU(self):
   1.161 @@ -319,10 +360,9 @@ class XendPIF(XendBase):
   1.162          success = linux_set_mtu(self.device, new_mtu)
   1.163          if success:
   1.164              self.MTU = new_mtu
   1.165 +            import XendNode
   1.166 +            XendNode.instance().save_PIFs()
   1.167          return success
   1.168  
   1.169      def get_VLAN(self):
   1.170          return self.VLAN
   1.171 -
   1.172 -    def set_VLAN(self, VLAN):
   1.173 -        pass