ia64/xen-unstable

changeset 19778:a4036225c168

xend: pass-through: Move pci conversion functions to pci.py

Move dev_dict_to_sxp() into XendSXPDev.py. Move
pci_convert_dict_to_sxp() and pci_convert_sxp_to_dict() to pci.py,
where other similar functions live. This makes these functions
accessible outside of the XendConfig class.

Signed-off-by: Simon Horman <horms@verge.net.au>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Jun 17 07:36:47 2009 +0100 (2009-06-17)
parents 60588f1f055f
children bc7715954087
files tools/python/xen/util/pci.py tools/python/xen/xend/XendConfig.py tools/python/xen/xend/XendDomainInfo.py tools/python/xen/xend/XendSXPDev.py
line diff
     1.1 --- a/tools/python/xen/util/pci.py	Wed Jun 17 07:35:51 2009 +0100
     1.2 +++ b/tools/python/xen/util/pci.py	Wed Jun 17 07:36:47 2009 +0100
     1.3 @@ -14,8 +14,10 @@ import struct
     1.4  import time
     1.5  import threading
     1.6  from xen.util import utils
     1.7 +from xen.xend import uuid
     1.8  from xen.xend import sxp
     1.9  from xen.xend.XendConstants import AUTO_PHP_SLOT
    1.10 +from xen.xend.XendSXPDev import dev_dict_to_sxp
    1.11  
    1.12  PROC_PCI_PATH = '/proc/bus/pci/devices'
    1.13  PROC_PCI_NUM_RESOURCES = 7
    1.14 @@ -139,6 +141,79 @@ def pci_opts_list_to_sxp(list):
    1.15  def pci_opts_list_from_sxp(dev):
    1.16      return map(lambda x: sxp.children(x)[0], sxp.children(dev, 'opts'))
    1.17  
    1.18 +def pci_convert_dict_to_sxp(dev, state, sub_state = None):
    1.19 +    pci_sxp = ['pci', dev_dict_to_sxp(dev), ['state', state]]
    1.20 +    if sub_state != None:
    1.21 +        pci_sxp.append(['sub_state', sub_state])
    1.22 +    return pci_sxp
    1.23 +
    1.24 +def pci_convert_sxp_to_dict(dev_sxp):
    1.25 +    """Convert pci device sxp to dict
    1.26 +    @param dev_sxp: device configuration
    1.27 +    @type  dev_sxp: SXP object (parsed config)
    1.28 +    @return: dev_config
    1.29 +    @rtype: dictionary
    1.30 +    """
    1.31 +    # Parsing the device SXP's. In most cases, the SXP looks
    1.32 +    # like this:
    1.33 +    #
    1.34 +    # [device, [vif, [mac, xx:xx:xx:xx:xx:xx], [ip 1.3.4.5]]]
    1.35 +    #
    1.36 +    # However, for PCI devices it looks like this:
    1.37 +    #
    1.38 +    # [device, [pci, [dev, [domain, 0], [bus, 0], [slot, 1], [func, 2]]]
    1.39 +    #
    1.40 +    # It seems the reasoning for this difference is because
    1.41 +    # pciif.py needs all the PCI device configurations at
    1.42 +    # the same time when creating the devices.
    1.43 +    #
    1.44 +    # To further complicate matters, Xen 2.0 configuration format
    1.45 +    # uses the following for pci device configuration:
    1.46 +    #
    1.47 +    # [device, [pci, [domain, 0], [bus, 0], [dev, 1], [func, 2]]]
    1.48 +
    1.49 +    # For PCI device hotplug support, the SXP of PCI devices is
    1.50 +    # extendend like this:
    1.51 +    #
    1.52 +    # [device, [pci, [dev, [domain, 0], [bus, 0], [slot, 1], [func, 2],
    1.53 +    #                      [vslot, 0]],
    1.54 +    #                [state, 'Initialising']]]
    1.55 +    #
    1.56 +    # 'vslot' shows the virtual hotplug slot number which the PCI device
    1.57 +    # is inserted in. This is only effective for HVM domains.
    1.58 +    #
    1.59 +    # state 'Initialising' indicates that the device is being attached,
    1.60 +    # while state 'Closing' indicates that the device is being detached.
    1.61 +    #
    1.62 +    # The Dict looks like this:
    1.63 +    #
    1.64 +    # { devs: [{domain: 0, bus: 0, slot: 1, func: 2, vslot: 0}],
    1.65 +    #   states: ['Initialising'] }
    1.66 +
    1.67 +    dev_config = {}
    1.68 +
    1.69 +    pci_devs = []
    1.70 +    for pci_dev in sxp.children(dev_sxp, 'dev'):
    1.71 +        pci_dev_info = dict(pci_dev[1:])
    1.72 +        if 'opts' in pci_dev_info:
    1.73 +            pci_dev_info['opts'] = pci_opts_list_from_sxp(pci_dev)
    1.74 +        # append uuid to each pci device that does't already have one.
    1.75 +        if not pci_dev_info.has_key('uuid'):
    1.76 +            dpci_uuid = pci_dev_info.get('uuid', uuid.createString())
    1.77 +            pci_dev_info['uuid'] = dpci_uuid
    1.78 +        pci_devs.append(pci_dev_info)
    1.79 +    dev_config['devs'] = pci_devs
    1.80 +
    1.81 +    pci_states = []
    1.82 +    for pci_state in sxp.children(dev_sxp, 'state'):
    1.83 +        try:
    1.84 +            pci_states.append(pci_state[1])
    1.85 +        except IndexError:
    1.86 +            raise XendError("Error reading state while parsing pci sxp")
    1.87 +    dev_config['states'] = pci_states
    1.88 +
    1.89 +    return dev_config
    1.90 +
    1.91  def parse_hex(val):
    1.92      try:
    1.93          if isinstance(val, types.StringTypes):
     2.1 --- a/tools/python/xen/xend/XendConfig.py	Wed Jun 17 07:35:51 2009 +0100
     2.2 +++ b/tools/python/xen/xend/XendConfig.py	Wed Jun 17 07:36:47 2009 +0100
     2.3 @@ -36,7 +36,8 @@ from xen.xend.xenstore.xstransact import
     2.4  from xen.xend.server.BlktapController import blktap_disk_types
     2.5  from xen.xend.server.netif import randomMAC
     2.6  from xen.util.blkif import blkdev_name_to_number, blkdev_uname_to_file
     2.7 -from xen.util.pci import pci_opts_list_from_sxp
     2.8 +from xen.util.pci import pci_opts_list_from_sxp, pci_convert_sxp_to_dict
     2.9 +from xen.xend.XendSXPDev import dev_dict_to_sxp
    2.10  from xen.util import xsconstants
    2.11  import xen.util.auxbin
    2.12  
    2.13 @@ -1295,7 +1296,7 @@ class XendConfig(dict):
    2.14                  pci_devs_uuid = sxp.child_value(config, 'uuid',
    2.15                                                  uuid.createString())
    2.16  
    2.17 -                pci_dict = self.pci_convert_sxp_to_dict(config)
    2.18 +                pci_dict = pci_convert_sxp_to_dict(config)
    2.19                  pci_devs = pci_dict['devs']
    2.20  
    2.21                  # create XenAPI DPCI objects.
    2.22 @@ -1605,79 +1606,6 @@ class XendConfig(dict):
    2.23  
    2.24          return ''
    2.25  
    2.26 -    def pci_convert_dict_to_sxp(self, dev, state, sub_state = None):
    2.27 -        pci_sxp = ['pci', self.dev_dict_to_sxp(dev), ['state', state]]
    2.28 -        if sub_state != None:
    2.29 -            pci_sxp.append(['sub_state', sub_state])
    2.30 -        return pci_sxp
    2.31 -
    2.32 -    def pci_convert_sxp_to_dict(self, dev_sxp):
    2.33 -        """Convert pci device sxp to dict
    2.34 -        @param dev_sxp: device configuration
    2.35 -        @type  dev_sxp: SXP object (parsed config)
    2.36 -        @return: dev_config
    2.37 -        @rtype: dictionary
    2.38 -        """
    2.39 -        # Parsing the device SXP's. In most cases, the SXP looks
    2.40 -        # like this:
    2.41 -        #
    2.42 -        # [device, [vif, [mac, xx:xx:xx:xx:xx:xx], [ip 1.3.4.5]]]
    2.43 -        #
    2.44 -        # However, for PCI devices it looks like this:
    2.45 -        #
    2.46 -        # [device, [pci, [dev, [domain, 0], [bus, 0], [slot, 1], [func, 2]]]
    2.47 -        #
    2.48 -        # It seems the reasoning for this difference is because
    2.49 -        # pciif.py needs all the PCI device configurations at
    2.50 -        # the same time when creating the devices.
    2.51 -        #
    2.52 -        # To further complicate matters, Xen 2.0 configuration format
    2.53 -        # uses the following for pci device configuration:
    2.54 -        #
    2.55 -        # [device, [pci, [domain, 0], [bus, 0], [dev, 1], [func, 2]]]
    2.56 -
    2.57 -        # For PCI device hotplug support, the SXP of PCI devices is
    2.58 -        # extendend like this:
    2.59 -        #
    2.60 -        # [device, [pci, [dev, [domain, 0], [bus, 0], [slot, 1], [func, 2],
    2.61 -        #                      [vslot, 0]],
    2.62 -        #                [state, 'Initialising']]]
    2.63 -        #
    2.64 -        # 'vslot' shows the virtual hotplug slot number which the PCI device
    2.65 -        # is inserted in. This is only effective for HVM domains.
    2.66 -        #
    2.67 -        # state 'Initialising' indicates that the device is being attached,
    2.68 -        # while state 'Closing' indicates that the device is being detached.
    2.69 -        #
    2.70 -        # The Dict looks like this:
    2.71 -        #
    2.72 -        # { devs: [{domain: 0, bus: 0, slot: 1, func: 2, vslot: 0}],
    2.73 -        #   states: ['Initialising'] }
    2.74 -
    2.75 -        dev_config = {}
    2.76 -
    2.77 -        pci_devs = []
    2.78 -        for pci_dev in sxp.children(dev_sxp, 'dev'):
    2.79 -            pci_dev_info = dict(pci_dev[1:])
    2.80 -            if 'opts' in pci_dev_info:
    2.81 -                pci_dev_info['opts'] = pci_opts_list_from_sxp(pci_dev)
    2.82 -            # append uuid to each pci device that does't already have one.
    2.83 -            if not pci_dev_info.has_key('uuid'):
    2.84 -                dpci_uuid = pci_dev_info.get('uuid', uuid.createString())
    2.85 -                pci_dev_info['uuid'] = dpci_uuid
    2.86 -            pci_devs.append(pci_dev_info)
    2.87 -        dev_config['devs'] = pci_devs 
    2.88 -
    2.89 -        pci_states = []
    2.90 -        for pci_state in sxp.children(dev_sxp, 'state'):
    2.91 -            try:
    2.92 -                pci_states.append(pci_state[1])
    2.93 -            except IndexError:
    2.94 -                raise XendError("Error reading state while parsing pci sxp")
    2.95 -        dev_config['states'] = pci_states
    2.96 -
    2.97 -        return dev_config
    2.98 -
    2.99      def vscsi_convert_sxp_to_dict(self, dev_sxp):
   2.100          """Convert vscsi device sxp to dict
   2.101          @param dev_sxp: device configuration
   2.102 @@ -1848,7 +1776,7 @@ class XendConfig(dict):
   2.103              dev_type, dev_info = self['devices'][dev_uuid]
   2.104  
   2.105              if dev_type == 'pci': # Special case for pci
   2.106 -                pci_dict = self.pci_convert_sxp_to_dict(config)
   2.107 +                pci_dict = pci_convert_sxp_to_dict(config)
   2.108                  pci_devs = pci_dict['devs']
   2.109  
   2.110                  # destroy existing XenAPI DPCI objects
   2.111 @@ -1971,15 +1899,6 @@ class XendConfig(dict):
   2.112          result.extend([u for u in target['devices'].keys() if u not in result])
   2.113          return result
   2.114  
   2.115 -    # This includes a generic equivalent of pci_opts_list_to_sxp()
   2.116 -    def dev_dict_to_sxp(self, dev):
   2.117 -        def f((key, val)):
   2.118 -            if isinstance(val, types.ListType):
   2.119 -                return map(lambda x: [key, x], val)
   2.120 -            return [[key, val]]
   2.121 -        dev_sxp = ['dev'] + reduce(lambda x, y: x + y, map(f, dev.items()))
   2.122 -        return dev_sxp
   2.123 -
   2.124      def all_devices_sxpr(self, target = None):
   2.125          """Returns the SXPR for all devices in the current configuration."""
   2.126          sxprs = []
   2.127 @@ -2002,7 +1921,7 @@ class XendConfig(dict):
   2.128                      if dev_info.has_key('backend'):
   2.129                          sxpr.append(['backend', dev_info['backend']])
   2.130                  for pci_dev_info in dev_info['devs']:
   2.131 -                    sxpr.append(self.dev_dict_to_sxp(pci_dev_info))
   2.132 +                    sxpr.append(dev_dict_to_sxp(pci_dev_info))
   2.133                  sxprs.append((dev_type, sxpr))
   2.134              else:
   2.135                  sxpr = self.device_sxpr(dev_type = dev_type,
     3.1 --- a/tools/python/xen/xend/XendDomainInfo.py	Wed Jun 17 07:35:51 2009 +0100
     3.2 +++ b/tools/python/xen/xend/XendDomainInfo.py	Wed Jun 17 07:36:47 2009 +0100
     3.3 @@ -40,7 +40,9 @@ from xen.util.blkif import blkdev_uname_
     3.4  import xen.util.xsm.xsm as security
     3.5  from xen.util import xsconstants
     3.6  from xen.util.pci import serialise_pci_opts, pci_opts_list_to_sxp, \
     3.7 -                         pci_dict_to_bdf_str, pci_dict_to_xc_str, pci_dict_cmp
     3.8 +                         pci_dict_to_bdf_str, pci_dict_to_xc_str, \
     3.9 +                         pci_convert_sxp_to_dict, pci_convert_dict_to_sxp, \
    3.10 +                         pci_dict_cmp
    3.11  
    3.12  from xen.xend import balloon, sxp, uuid, image, arch
    3.13  from xen.xend import XendOptions, XendNode, XendConfig
    3.14 @@ -616,8 +618,8 @@ class XendDomainInfo:
    3.15          pci_conf = self.info['devices'][dev_uuid][1]
    3.16          pci_devs = pci_conf['devs']
    3.17          request = map(lambda x:
    3.18 -                      self.info.pci_convert_dict_to_sxp(x, 'Initialising',
    3.19 -                                                        'Booting'), pci_devs)
    3.20 +                      pci_convert_dict_to_sxp(x, 'Initialising', 'Booting'),
    3.21 +                      pci_devs)
    3.22  
    3.23          for i in request:
    3.24                  self.pci_device_configure(i)
    3.25 @@ -815,7 +817,7 @@ class XendDomainInfo:
    3.26              raise XendError("Cannot detach when pci platform does not exist")
    3.27  
    3.28          pci_dev = sxp.children(dev_sxp, 'dev')[0]
    3.29 -        dev_config = self.info.pci_convert_sxp_to_dict(dev_sxp)
    3.30 +        dev_config = pci_convert_sxp_to_dict(dev_sxp)
    3.31          dev = dev_config['devs'][0]
    3.32                  
    3.33          # Do HVM specific processing
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/python/xen/xend/XendSXPDev.py	Wed Jun 17 07:36:47 2009 +0100
     4.3 @@ -0,0 +1,14 @@
     4.4 +#!/usr/bin/env python
     4.5 +#
     4.6 +# Helper functions for dealing with the sxp representation of devices
     4.7 +
     4.8 +import types
     4.9 +
    4.10 +# This includes a generic equivalent of pci_opts_list_to_sxp()
    4.11 +def dev_dict_to_sxp(dev):
    4.12 +    def f((key, val)):
    4.13 +        if isinstance(val, types.ListType):
    4.14 +            return map(lambda x: [key, x], val)
    4.15 +        return [[key, val]]
    4.16 +    dev_sxp = ['dev'] + reduce(lambda x, y: x + y, map(f, dev.items()))
    4.17 +    return dev_sxp