ia64/xen-unstable

changeset 14484:d86957cea8b8

Add VIF.runtime_properties dictionary, and use that to implement xm network-list
through the Xen-API. Implement xm network-attach and xm network-detach also.

Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
author Ewan Mellor <ewan@xensource.com>
date Tue Mar 20 15:12:07 2007 +0000 (2007-03-20)
parents ba1212ee7689
children fc918d15c981
files tools/python/xen/xend/XendAPI.py tools/python/xen/xend/XendDomainInfo.py tools/python/xen/xm/main.py
line diff
     1.1 --- a/tools/python/xen/xend/XendAPI.py	Tue Mar 20 14:04:57 2007 +0000
     1.2 +++ b/tools/python/xen/xend/XendAPI.py	Tue Mar 20 15:12:07 2007 +0000
     1.3 @@ -1656,21 +1656,19 @@ class XendAPI(object):
     1.4          xendom = XendDomain.instance()
     1.5          dominfo = xendom.get_vm_with_dev_uuid('vbd', vbd_ref)
     1.6          device = dominfo.get_dev_config_by_uuid('vbd', vbd_ref)
     1.7 -        devid = int(device['id'])
     1.8 -        device_sxps = dominfo.getDeviceSxprs('vbd')
     1.9 -
    1.10 -        log.debug("VBD_get_runtime_properties devid: %i device_sxps: %s",
    1.11 -                  devid, device_sxps)
    1.12 -        
    1.13 -        device_dicts  = [dict(device_sxp[1][1:]) for device_sxp in device_sxps]
    1.14  
    1.15 -        device_dict = [device_dict
    1.16 -                       for device_dict in device_dicts
    1.17 -                       if int(device_dict['virtual-device']) == devid][0]
    1.18 +        try:
    1.19 +            devid = int(device['id'])
    1.20 +            device_sxps = dominfo.getDeviceSxprs('vbd')
    1.21 +            device_dicts  = [dict(device_sxp[1][1:]) for device_sxp in device_sxps]
    1.22 +            device_dict = [device_dict
    1.23 +                           for device_dict in device_dicts
    1.24 +                           if int(device_dict['virtual-device']) == devid][0]
    1.25  
    1.26 -        log.debug("VBD_get_runtime_properties device_dict: %s", device_dict)
    1.27 -
    1.28 -        return xen_api_success(device_dict)
    1.29 +            return xen_api_success(device_dict)
    1.30 +        except Exception, exn:
    1.31 +            log.exception(exn)
    1.32 +            return xen_api_success({})
    1.33  
    1.34      # attributes (rw)
    1.35      def VBD_get_VM(self, session, vbd_ref):
    1.36 @@ -1732,7 +1730,8 @@ class XendAPI(object):
    1.37      # Xen API: Class VIF
    1.38      # ----------------------------------------------------------------
    1.39  
    1.40 -    VIF_attr_ro = ['metrics']
    1.41 +    VIF_attr_ro = ['metrics',
    1.42 +                   'runtime_properties']
    1.43      VIF_attr_rw = ['device',
    1.44                     'network',
    1.45                     'VM',
    1.46 @@ -1769,18 +1768,17 @@ class XendAPI(object):
    1.47      # class methods
    1.48      def VIF_create(self, session, vif_struct):
    1.49          xendom = XendDomain.instance()
    1.50 -        if xendom.is_valid_vm(vif_struct['VM']):
    1.51 -            dom = xendom.get_vm_by_uuid(vif_struct['VM'])
    1.52 -            try:
    1.53 -                vif_ref = dom.create_vif(vif_struct)
    1.54 -                xendom.managed_config_save(dom)                
    1.55 -                return xen_api_success(vif_ref)
    1.56 -            except XendError:
    1.57 -                return xen_api_error(XEND_ERROR_TODO)
    1.58 -        else:
    1.59 +        if not xendom.is_valid_vm(vif_struct['VM']):
    1.60              return xen_api_error(['HANDLE_INVALID', 'VM', vif_struct['VM']])
    1.61  
    1.62 -
    1.63 +        dom = xendom.get_vm_by_uuid(vif_struct['VM'])
    1.64 +        try:
    1.65 +            vif_ref = dom.create_vif(vif_struct)
    1.66 +            xendom.managed_config_save(dom)
    1.67 +            return xen_api_success(vif_ref)
    1.68 +        except XendError:
    1.69 +            return xen_api_error(XEND_ERROR_TODO)
    1.70 +          
    1.71      def VIF_destroy(self, session, vif_ref):
    1.72          xendom = XendDomain.instance()
    1.73          vm = xendom.get_vm_with_dev_uuid('vif', vif_ref)
    1.74 @@ -1818,6 +1816,27 @@ class XendAPI(object):
    1.75          vifs = reduce(lambda x, y: x + y, vifs)
    1.76          return xen_api_success(vifs)
    1.77  
    1.78 +    def VIF_get_runtime_properties(self, _, vif_ref):
    1.79 +        xendom = XendDomain.instance()
    1.80 +        dominfo = xendom.get_vm_with_dev_uuid('vif', vif_ref)
    1.81 +        device = dominfo.get_dev_config_by_uuid('vif', vif_ref)
    1.82 +        
    1.83 +        try:
    1.84 +            devid = int(device['id'])
    1.85 +        
    1.86 +            device_sxps = dominfo.getDeviceSxprs('vif')
    1.87 +            device_dicts = [dict(device_sxp[1][1:])
    1.88 +                            for device_sxp in device_sxps]
    1.89 +            
    1.90 +            device_dict = [device_dict
    1.91 +                       for device_dict in device_dicts
    1.92 +                       if int(device_dict['handle']) == devid][0]
    1.93 +            
    1.94 +            return xen_api_success(device_dict)
    1.95 +        
    1.96 +        except Exception, exn:
    1.97 +            log.exception(exn)
    1.98 +            return xen_api_success({})
    1.99      
   1.100      # Xen API: Class VIF_metrics
   1.101      # ----------------------------------------------------------------
     2.1 --- a/tools/python/xen/xend/XendDomainInfo.py	Tue Mar 20 14:04:57 2007 +0000
     2.2 +++ b/tools/python/xen/xend/XendDomainInfo.py	Tue Mar 20 15:12:07 2007 +0000
     2.3 @@ -2286,8 +2286,6 @@ class XendDomainInfo:
     2.4          @return: uuid of the device
     2.5          """
     2.6          xenapi_vbd['image'] = vdi_image_path
     2.7 -        log.debug('create_vbd: %s' % xenapi_vbd)
     2.8 -        dev_uuid = ''
     2.9          if vdi_image_path.startswith('tap'):
    2.10              dev_uuid = self.info.device_add('tap', cfg_xenapi = xenapi_vbd)
    2.11          else:
    2.12 @@ -2297,8 +2295,8 @@ class XendDomainInfo:
    2.13              raise XendError('Failed to create device')
    2.14  
    2.15          if self.state == XEN_API_VM_POWER_STATE_RUNNING:
    2.16 +            
    2.17              _, config = self.info['devices'][dev_uuid]
    2.18 -            dev_control = None
    2.19              
    2.20              if vdi_image_path.startswith('tap'):
    2.21                  dev_control = self.getDeviceController('tap')
    2.22 @@ -2349,9 +2347,21 @@ class XendDomainInfo:
    2.23              raise XendError('Failed to create device')
    2.24          
    2.25          if self.state == XEN_API_VM_POWER_STATE_RUNNING:
    2.26 +
    2.27              _, config = self.info['devices'][dev_uuid]
    2.28 -            config['devid'] = self.getDeviceController('vif').createDevice(config)
    2.29 -
    2.30 +            dev_control = self.getDeviceController('vif')
    2.31 +
    2.32 +            try:
    2.33 +                devid = dev_control.createDevice(config)
    2.34 +                dev_control.waitForDevice(devid)
    2.35 +                self.info.device_update(dev_uuid,
    2.36 +                                        cfg_xenapi = {'devid': devid})
    2.37 +            except Exception, exn:
    2.38 +                log.exception(exn)
    2.39 +                del self.info['devices'][dev_uuid]
    2.40 +                self.info['vif_refs'].remove(dev_uuid)
    2.41 +                raise            
    2.42 + 
    2.43          return dev_uuid
    2.44  
    2.45      def create_vtpm(self, xenapi_vtpm):
     3.1 --- a/tools/python/xen/xm/main.py	Tue Mar 20 14:04:57 2007 +0000
     3.2 +++ b/tools/python/xen/xm/main.py	Tue Mar 20 15:12:07 2007 +0000
     3.3 @@ -497,6 +497,10 @@ def get_default_SR():
     3.4              for sr_ref in server.xenapi.SR.get_all()
     3.5              if server.xenapi.SR.get_type(sr_ref) == "local"][0]
     3.6  
     3.7 +def get_default_Network():
     3.8 +    return [network_ref
     3.9 +            for network_ref in server.xenapi.network.get_all()][0]
    3.10 +
    3.11  def map2sxp(m):
    3.12      return [[k, m[k]] for k in m.keys()]
    3.13  
    3.14 @@ -1669,12 +1673,20 @@ def xm_network_list(args):
    3.15      (use_long, params) = arg_check_for_resource_list(args, "network-list")
    3.16  
    3.17      dom = params[0]
    3.18 +
    3.19 +    if serverType == SERVER_XEN_API:
    3.20 +        vif_refs = server.xenapi.VM.get_VIFs(get_single_vm(dom))
    3.21 +        vif_properties = \
    3.22 +            map(server.xenapi.VIF.get_runtime_properties, vif_refs)
    3.23 +        devs = map(lambda x: [x.get('handle'), map2sxp(x)], vif_properties)
    3.24 +    else:
    3.25 +        devs = server.xend.domain.getDeviceSxprs(dom, 'vif')
    3.26 +        
    3.27      if use_long:
    3.28 -        devs = server.xend.domain.getDeviceSxprs(dom, 'vif')
    3.29          map(PrettyPrint.prettyprint, devs)
    3.30      else:
    3.31          hdr = 0
    3.32 -        for x in server.xend.domain.getDeviceSxprs(dom, 'vif'):
    3.33 +        for x in devs:
    3.34              if hdr == 0:
    3.35                  print 'Idx BE     MAC Addr.     handle state evt-ch tx-/rx-ring-ref BE-path'
    3.36                  hdr = 1
    3.37 @@ -1699,7 +1711,7 @@ def xm_block_list(args):
    3.38          vbd_refs = server.xenapi.VM.get_VBDs(get_single_vm(dom))
    3.39          vbd_properties = \
    3.40              map(server.xenapi.VBD.get_runtime_properties, vbd_refs)
    3.41 -        devs = map(lambda x: [x['virtual-device'], map2sxp(x)], vbd_properties)
    3.42 +        devs = map(lambda x: [x.get('virtual-device'), map2sxp(x)], vbd_properties)
    3.43      else:
    3.44          devs = server.xend.domain.getDeviceSxprs(dom, 'vbd')
    3.45  
    3.46 @@ -1836,15 +1848,65 @@ def xm_network_attach(args):
    3.47      vif_params = ['type', 'mac', 'bridge', 'ip', 'script', \
    3.48                    'backend', 'vifname', 'rate', 'model']
    3.49  
    3.50 -    for a in args[1:]:
    3.51 -        vif_param = a.split("=")
    3.52 -        if len(vif_param) != 2 or vif_param[1] == '' or \
    3.53 -           vif_param[0] not in vif_params:
    3.54 -            err("Invalid argument: %s" % a)
    3.55 -            usage('network-attach')
    3.56 -        vif.append(vif_param)
    3.57 +    if serverType == SERVER_XEN_API:     
    3.58 +        vif_record = {
    3.59 +            "device":               "eth0",
    3.60 +            "network":              get_default_Network(),
    3.61 +            "VM":                   get_single_vm(dom),
    3.62 +            "MAC":                  "",
    3.63 +            "MTU":                  "",
    3.64 +            "qos_algorithm_type":   "",
    3.65 +            "qos_algorithm_params": {},
    3.66 +            "other_config":         {}
    3.67 +            }
    3.68  
    3.69 -    server.xend.domain.device_create(dom, vif)
    3.70 +        def set(keys, val):
    3.71 +            record = vif_record
    3.72 +            for key in keys[:-1]:
    3.73 +                record = record[key]
    3.74 +            record[keys[-1]] = val 
    3.75 +         
    3.76 +        vif_conv = {
    3.77 +            'type':
    3.78 +                lambda x: None,
    3.79 +            'mac':
    3.80 +                lambda x: set(['MAC'], x),
    3.81 +            'bridge':
    3.82 +                lambda x: set(['network'], get_net_from_bridge(x)),
    3.83 +            'ip':
    3.84 +                lambda x: set(['other_config', 'ip'], x),
    3.85 +            'script':
    3.86 +                lambda x: set(['other_config', 'script'], x),
    3.87 +            'backend':
    3.88 +                lambda x: set(['other_config', 'backend'], x),
    3.89 +            'vifname':
    3.90 +                lambda x: set(['device'], x),
    3.91 +            'rate':
    3.92 +                lambda x: set(['qos_algorithm_params', 'rate'], x),
    3.93 +            'model':
    3.94 +                lambda x: None
    3.95 +            }
    3.96 +            
    3.97 +        for a in args[1:]:
    3.98 +            vif_param = a.split("=")
    3.99 +            if len(vif_param) != 2 or vif_param[1] == '' or \
   3.100 +                   vif_param[0] not in vif_params:
   3.101 +                err("Invalid argument: %s" % a)
   3.102 +                usage('network-attach')   
   3.103 +            else:
   3.104 +                vif_conv[vif_param[0]](vif_param[1])
   3.105 +
   3.106 +        print str(vif_record)
   3.107 +        server.xenapi.VIF.create(vif_record)
   3.108 +    else:
   3.109 +        for a in args[1:]:
   3.110 +            vif_param = a.split("=")
   3.111 +            if len(vif_param) != 2 or vif_param[1] == '' or \
   3.112 +                   vif_param[0] not in vif_params:
   3.113 +                err("Invalid argument: %s" % a)
   3.114 +                usage('network-attach')
   3.115 +            vif.append(vif_param)
   3.116 +        server.xend.domain.device_create(dom, vif)
   3.117  
   3.118  
   3.119  def detach(args, command, deviceClass):
   3.120 @@ -1890,7 +1952,22 @@ def xm_block_detach(args):
   3.121          detach(args, 'block-detach', 'tap')
   3.122  
   3.123  def xm_network_detach(args):
   3.124 -    detach(args, 'network-detach', 'vif')
   3.125 +    if serverType == SERVER_XEN_API:
   3.126 +        arg_check(args, "xm_block_detach", 2, 3)
   3.127 +        dom = args[0]
   3.128 +        devid = args[1]
   3.129 +        vif_refs = server.xenapi.VM.get_VIFs(get_single_vm(dom))
   3.130 +        vif_refs = [vif_ref for vif_ref in vif_refs
   3.131 +                    if server.xenapi.VIF.\
   3.132 +                    get_runtime_properties(vif_ref)["handle"] == devid]
   3.133 +        if len(vif_refs) > 0:
   3.134 +            vif_ref = vif_refs[0]
   3.135 +            
   3.136 +            server.xenapi.VIF.destroy(vif_ref)
   3.137 +        else:
   3.138 +            print "Cannot find device '%s' in domain '%s'" % (devid,dom)
   3.139 +    else:
   3.140 +        detach(args, 'network-detach', 'vif')
   3.141  
   3.142  
   3.143  def xm_vnet_list(args):