ia64/xen-unstable

changeset 13187:8036efeefbd1

Change the way that networks and PIFs relate to each other -- the PIF holds a
reference to the network (and the host) not the other way around. Persist only
the necessary details for the network (not the set of PIFs and VIFs).

Fix the plumbing of the network class inside XendAPI, and implement a few more
functions.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Mon Dec 25 16:26:51 2006 +0000 (2006-12-25)
parents eec06ba7afaa
children cd7cb95eacc3
files tools/python/xen/xend/XendAPI.py tools/python/xen/xend/XendNetwork.py tools/python/xen/xend/XendNode.py tools/python/xen/xend/XendPIF.py
line diff
     1.1 --- a/tools/python/xen/xend/XendAPI.py	Mon Dec 25 16:24:15 2006 +0000
     1.2 +++ b/tools/python/xen/xend/XendAPI.py	Mon Dec 25 16:26:51 2006 +0000
     1.3 @@ -451,15 +451,31 @@ class XendAPI:
     1.4          return xen_api_error(XEND_ERROR_UNSUPPORTED)
     1.5  
     1.6  
     1.7 -    # Xen API: Class Network
     1.8 +    # Xen API: Class network
     1.9      # ----------------------------------------------------------------
    1.10  
    1.11 -    Network_attr_ro = ['VIFs', 'PIFs']
    1.12 -    Network_attr_rw = ['name_label',
    1.13 +    network_attr_ro = ['VIFs', 'PIFs']
    1.14 +    network_attr_rw = ['name_label',
    1.15                         'name_description',
    1.16                         'default_gateway',
    1.17                         'default_netmask']
    1.18  
    1.19 +    def _get_network(self, ref):
    1.20 +        return XendNode.instance().get_network(ref)
    1.21 +
    1.22 +    def network_get_all(self, session):
    1.23 +        return xen_api_success(XendNode.instance().get_network_refs())
    1.24 +
    1.25 +    def network_get_record(self, session, ref):
    1.26 +        return xen_api_success(
    1.27 +            XendNode.instance().get_network(ref).get_record())
    1.28 +
    1.29 +    def network_get_VIFs(self, _, ref):
    1.30 +        return xen_api_success(self._get_network(ref).get_VIF_UUIDs())
    1.31 +
    1.32 +    def network_get_PIFs(self, session, ref):
    1.33 +        return xen_api_success(self._get_network(ref).get_PIF_UUIDs())
    1.34 +
    1.35      # Xen API: Class PIF
    1.36      # ----------------------------------------------------------------
    1.37  
     2.1 --- a/tools/python/xen/xend/XendNetwork.py	Mon Dec 25 16:24:15 2006 +0000
     2.2 +++ b/tools/python/xen/xend/XendNetwork.py	Mon Dec 25 16:26:51 2006 +0000
     2.3 @@ -12,7 +12,6 @@
     2.4  # License along with this library; if not, write to the Free Software
     2.5  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     2.6  #============================================================================
     2.7 -# Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
     2.8  # Copyright (c) 2006 Xensource Inc.
     2.9  #============================================================================
    2.10  
    2.11 @@ -22,7 +21,8 @@ import re
    2.12  import struct
    2.13  import socket
    2.14  
    2.15 -from xen.xend.XendRoot import instance as xendroot
    2.16 +import XendNode
    2.17 +from XendLogging import log
    2.18  
    2.19  IP_ROUTE_RE = r'^default via ([\d\.]+) dev (\w+)'
    2.20  
    2.21 @@ -63,57 +63,37 @@ class XendNetwork:
    2.22          self.name_description = description
    2.23          self.default_gateway = gateway
    2.24          self.default_netmask = netmask
    2.25 -        self.vifs = {}
    2.26 -        self.pifs = {}
    2.27 -
    2.28 -    def get_name_label(self):
    2.29 -        return self.name_label
    2.30  
    2.31      def set_name_label(self, new_name):
    2.32          self.name_label = new_name
    2.33  
    2.34 -    def get_name_description(self):
    2.35 -        return self.name_description
    2.36 -
    2.37      def set_name_description(self, new_desc):
    2.38          self.name_description = new_desc
    2.39  
    2.40 -    def get_default_gateway(self):
    2.41 -        return self.default_gateway
    2.42 -
    2.43      def set_default_gateway(self, new_gateway):
    2.44          if re.search('^\d+\.\d+\.\d+\.\d+$', new_gateway):
    2.45              self.default_gateway = new_gateway
    2.46  
    2.47 -    def get_default_netmask(self):
    2.48 -        return self.default_netmask
    2.49 -
    2.50      def set_default_netmask(self, new_netmask):
    2.51          if re.search('^\d+\.\d+\.\d+\.\d+$', new_netmask):
    2.52              self.default_netmask = new_netmask
    2.53  
    2.54 -    def add_pif(self, pif):
    2.55 -        self.pifs[pif.get_uuid()] = pif
    2.56 -
    2.57 -    def remove_pif(self, pif_uuid):
    2.58 -        if pif_uuid in self.pifs:
    2.59 -            del self.pifs[pif_uuid]
    2.60 +    def get_VIF_UUIDs(self):
    2.61 +        return []
    2.62  
    2.63 -    def add_vif(self, vif):
    2.64 -        self.vifs[vif.get_uuid()] = vif
    2.65 +    def get_PIF_UUIDs(self):
    2.66 +        return [x.uuid for x in XendNode.instance().pifs.values()
    2.67 +                if x.network == self]
    2.68  
    2.69 -    def remove_vif(self, vif_uuid):
    2.70 -        if vif_uuid in self.vifs:
    2.71 -            del self.vifs[vif_uuid]
    2.72 -        
    2.73 -    def get_record(self):
    2.74 -        return {
    2.75 +    def get_record(self, transient = True):
    2.76 +        result = {
    2.77              'uuid': self.uuid,
    2.78              'name_label': self.name_label,
    2.79              'name_description': self.name_description,
    2.80              'default_gateway': self.default_gateway,
    2.81              'default_netmask': self.default_netmask,
    2.82 -            'VIFs': self.vifs.keys(),
    2.83 -            'PIFs': self.pifs.keys()
    2.84          }
    2.85 -           
    2.86 +        if transient:
    2.87 +            result['VIFs'] = self.get_VIF_UUIDs()
    2.88 +            result['PIFs'] = self.get_PIF_UUIDs()
    2.89 +        return result
     3.1 --- a/tools/python/xen/xend/XendNode.py	Mon Dec 25 16:24:15 2006 +0000
     3.2 +++ b/tools/python/xen/xend/XendNode.py	Mon Dec 25 16:26:51 2006 +0000
     3.3 @@ -83,20 +83,6 @@ class XendNode:
     3.4          self.pifs = {}
     3.5          self.networks = {}
     3.6  
     3.7 -        # initialise PIFs
     3.8 -        saved_pifs = self.state_store.load_state('pif')
     3.9 -        if saved_pifs:
    3.10 -            for pif_uuid, pif in saved_pifs.items():
    3.11 -                self.pifs[pif_uuid] = XendPIF(pif_uuid,
    3.12 -                                              pif['name'],
    3.13 -                                              pif['MTU'],
    3.14 -                                              pif['MAC'])
    3.15 -        else:
    3.16 -            for name, mtu, mac in linux_get_phy_ifaces():
    3.17 -                pif_uuid = uuid.createString()
    3.18 -                pif = XendPIF(pif_uuid, name, mtu, mac)
    3.19 -                self.pifs[pif_uuid] = pif
    3.20 -
    3.21          # initialise networks
    3.22          saved_networks = self.state_store.load_state('network')
    3.23          if saved_networks:
    3.24 @@ -106,17 +92,31 @@ class XendNode:
    3.25                                  network.get('name_description', ''),
    3.26                                  network.get('default_gateway', ''),
    3.27                                  network.get('default_netmask', ''))
    3.28 -                
    3.29 -                for pif_uuid in network.get('PIFs', {}).keys():
    3.30 -                    pif = self.pifs.get(pif_uuid)
    3.31 -                    if pif:
    3.32 -                        self.networks.pifs[pif_uuid] = pif
    3.33          else:
    3.34              gateway, netmask = linux_get_default_network()
    3.35              net_uuid = uuid.createString()
    3.36              net = XendNetwork(net_uuid, 'net0', '', gateway, netmask)
    3.37              self.networks[net_uuid] = net
    3.38  
    3.39 +        # initialise PIFs
    3.40 +        saved_pifs = self.state_store.load_state('pif')
    3.41 +        if saved_pifs:
    3.42 +            for pif_uuid, pif in saved_pifs.items():
    3.43 +                if pif['network'] in self.networks:
    3.44 +                    network = self.networks[pif['network']]
    3.45 +                    self.pifs[pif_uuid] = XendPIF(pif_uuid,
    3.46 +                                                  pif['name'],
    3.47 +                                                  pif['MTU'],
    3.48 +                                                  pif['MAC'],
    3.49 +                                                  network,
    3.50 +                                                  self)
    3.51 +        else:
    3.52 +            for name, mtu, mac in linux_get_phy_ifaces():
    3.53 +                network = self.networks.values()[0]
    3.54 +                pif_uuid = uuid.createString()
    3.55 +                pif = XendPIF(pif_uuid, name, mtu, mac, network, self)
    3.56 +                self.pifs[pif_uuid] = pif
    3.57 +
    3.58          # initialise storage
    3.59          saved_sr = self.state_store.load_state('sr')
    3.60          if saved_sr and len(saved_sr) == 1:
    3.61 @@ -125,7 +125,6 @@ class XendNode:
    3.62          else:
    3.63              sr_uuid = uuid.createString()
    3.64              self.sr = XendStorageRepository(sr_uuid)
    3.65 -        self.save()
    3.66  
    3.67      def save(self):
    3.68          # save state
    3.69 @@ -133,10 +132,10 @@ class XendNode:
    3.70                                     'name_description':self.desc}}
    3.71          self.state_store.save_state('host',host_record)
    3.72          self.state_store.save_state('cpu', self.cpus)
    3.73 -        pif_records = dict([(k, v.get_record())
    3.74 +        pif_records = dict([(k, v.get_record(transient = False))
    3.75                              for k, v in self.pifs.items()])
    3.76          self.state_store.save_state('pif', pif_records)
    3.77 -        net_records = dict([(k, v.get_record())
    3.78 +        net_records = dict([(k, v.get_record(transient = False))
    3.79                              for k, v in self.networks.items()])
    3.80          self.state_store.save_state('network', net_records)
    3.81  
    3.82 @@ -164,6 +163,9 @@ class XendNode:
    3.83      def is_valid_cpu(self, cpu_ref):
    3.84          return (cpu_ref in self.cpus)
    3.85  
    3.86 +    def is_valid_network(self, network_ref):
    3.87 +        return (network_ref in self.networks)
    3.88 +
    3.89      #
    3.90      # Storage Repo
    3.91      #
    3.92 @@ -233,7 +235,17 @@ class XendNode:
    3.93      def get_host_cpu_load(self, host_cpu_ref):
    3.94          return 0.0
    3.95  
    3.96 +
    3.97 +    #
    3.98 +    # Network Functions
    3.99 +    #
   3.100      
   3.101 +    def get_network_refs(self):
   3.102 +        return self.networks.keys()
   3.103 +
   3.104 +    def get_network(self, network_ref):
   3.105 +        return self.networks[network_ref]
   3.106 +
   3.107  
   3.108      #
   3.109      # Getting host information.
   3.110 @@ -318,5 +330,5 @@ def instance():
   3.111          inst
   3.112      except:
   3.113          inst = XendNode()
   3.114 +        inst.save()
   3.115      return inst
   3.116 -
     4.1 --- a/tools/python/xen/xend/XendPIF.py	Mon Dec 25 16:24:15 2006 +0000
     4.2 +++ b/tools/python/xen/xend/XendPIF.py	Mon Dec 25 16:26:51 2006 +0000
     4.3 @@ -12,7 +12,6 @@
     4.4  # License along with this library; if not, write to the Free Software
     4.5  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     4.6  #============================================================================
     4.7 -# Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
     4.8  # Copyright (c) 2006 Xensource Inc.
     4.9  #============================================================================
    4.10  
    4.11 @@ -100,29 +99,18 @@ def same_dir_rename(old_path, new_path):
    4.12  class XendPIF:
    4.13      """Representation of a Physical Network Interface."""
    4.14      
    4.15 -    def __init__(self, uuid, name, mtu, mac):
    4.16 +    def __init__(self, uuid, name, mtu, mac, network, host):
    4.17          self.uuid = uuid
    4.18          self.name = name
    4.19          self.mac = mac
    4.20          self.mtu = mtu
    4.21          self.vlan = ''
    4.22 -        self.network = None
    4.23 +        self.network = network
    4.24 +        self.host = host
    4.25  
    4.26      def set_name(self, new_name):
    4.27          self.name = new_name
    4.28              
    4.29 -    def get_name(self):
    4.30 -        return self.name
    4.31 -
    4.32 -    def get_uuid(self):
    4.33 -        return self.uuid
    4.34 -
    4.35 -    def get_mac(self):
    4.36 -        return self.mac
    4.37 -
    4.38 -    def get_vlan(self):
    4.39 -        return self.vlan
    4.40 -
    4.41      def set_mac(self, new_mac):
    4.42          success = linux_set_mac(new_mac)
    4.43          if success:
    4.44 @@ -141,17 +129,14 @@ class XendPIF:
    4.45      def get_io_write_kbs(self):
    4.46          return 0.0
    4.47  
    4.48 -    def get_record(self):
    4.49 -        if self.network:
    4.50 -            network_uuid = self.network.get_uuid()
    4.51 -        else:
    4.52 -            network_uuid = None
    4.53 -
    4.54 -        return {'name': self.name,
    4.55 -                'MAC': self.mac,
    4.56 -                'MTU': self.mtu,
    4.57 -                'VLAN': self.vlan,
    4.58 -                'network': network_uuid,
    4.59 -                'io_read_kbs': self.get_io_read_kbs(),
    4.60 -                'io_write_kbs': self.get_io_write_kbs()}
    4.61 -                
    4.62 +    def get_record(self, transient = True):
    4.63 +        result = {'name': self.name,
    4.64 +                  'MAC': self.mac,
    4.65 +                  'MTU': self.mtu,
    4.66 +                  'VLAN': self.vlan,
    4.67 +                  'host': self.host.uuid,
    4.68 +                  'network': self.network.uuid}
    4.69 +        if transient:
    4.70 +            result['io_read_kbs'] = self.get_io_read_kbs()
    4.71 +            result['io_write_kbs'] = self.get_io_write_kbs()
    4.72 +        return result