Exemplo n.º 1
0
 def create_interface():
     """
         Call different function to create interface according to the type
     """
     new_iface = Interface('network')
     if vf_type == "vf":
         new_iface = create_hostdev_interface(vf_addr, managed, model)
     if vf_type == "vf_pool":
         netxml = create_hostdev_network()
         virsh.net_define(netxml.xml, ignore_status=True)
         if not inactive_pool:
             virsh.net_start(netxml.name)
         new_iface = create_network_interface(netxml.name)
     if vf_type == "macvtap":
         new_iface = Interface('direct')
         new_iface.source = {"dev": vf_name, "mode": "passthrough"}
         new_iface.mac_address = utils_net.generate_mac_address_simple()
         new_iface.model = "virtio"
         if vlan_id:
             new_iface.vlan = new_iface.new_vlan(**vlan_id)
     if vf_type == "macvtap_network":
         netxml = create_macvtap_network()
         result = virsh.net_define(netxml.xml, ignore_status=True)
         virsh.net_start(netxml.name)
         new_iface = create_network_interface(netxml.name)
     return new_iface
Exemplo n.º 2
0
 def create_iface_xml(iface_mac):
     """
     Create interface xml file
     """
     iface = Interface(type_name=iface_type)
     source = ast.literal_eval(iface_source)
     if source:
         iface.source = source
     iface.model = iface_model if iface_model else "virtio"
     iface.mac_address = iface_mac
     driver_dict = {}
     driver_host = {}
     driver_guest = {}
     if iface_driver:
         driver_dict = ast.literal_eval(iface_driver)
     if iface_driver_host:
         driver_host = ast.literal_eval(iface_driver_host)
     if iface_driver_guest:
         driver_guest = ast.literal_eval(iface_driver_guest)
     iface.driver = iface.new_driver(driver_attr=driver_dict,
                                     driver_host=driver_host,
                                     driver_guest=driver_guest)
     if test_target:
         iface.target = {"dev": target_dev}
     logging.debug("Create new interface xml: %s", iface)
     return iface
Exemplo n.º 3
0
    def create_iface(iface_model, iface_source, **kwargs):
        """
        Create an interface to be attached to vm

        :param iface_model: model of the interface device
        :param iface_source: source of the interface device
        :param kwargs: other k-w args that needed to create device
        :return: the newly created interface object
        """
        iface = Interface('network')
        iface.model = iface_model
        iface.source = eval(iface_source)

        if 'mac' in kwargs:
            iface.mac_address = kwargs['mac']
        else:
            mac = utils_net.generate_mac_address_simple()
            iface.mac_address = mac

        if 'address' in kwargs:
            iface.address = iface.new_iface_address(
                attrs=eval(kwargs['address']))

        logging.debug('iface: %s', iface)
        return iface
Exemplo n.º 4
0
 def create_network_interface(name):
     """
         Create network type interface xml.
     """
     new_iface = Interface('network')
     new_iface.source = {'network': name}
     new_iface.model = "virtio"
     new_iface.mac_address = utils_net.generate_mac_address_simple()
     return new_iface
Exemplo n.º 5
0
 def create_network_interface(name):
     """
         Create network type interface xml.
     """
     new_iface = Interface('network')
     new_iface.source = {'network': name}
     new_iface.model = "virtio"
     new_iface.mac_address = utils_net.generate_mac_address_simple()
     return new_iface
Exemplo n.º 6
0
 def create_iface_xml():
     """
     Create interface xml file
     """
     iface = Interface("bridge")
     iface.source = eval("{'bridge':'virbr0'}")
     iface.model = "virtio"
     logging.debug("Create new interface xml: %s", iface)
     return iface
Exemplo n.º 7
0
 def create_iface_xml(mac):
     """
     Create interface xml file
     """
     iface = Interface(type_name=iface_type)
     iface.source = iface_source
     iface.model = iface_model if iface_model else "virtio"
     if iface_target:
         iface.target = {'dev': iface_target}
     iface.mac_address = mac
     logging.debug("Create new interface xml: %s", iface)
     return iface
Exemplo n.º 8
0
 def create_iface_xml(mac):
     """
     Create interface xml file
     """
     iface = Interface(type_name=iface_type)
     iface.source = iface_source
     iface.model = iface_model if iface_model else "virtio"
     if iface_target:
         iface.target = {'dev': iface_target}
     iface.mac_address = mac
     logging.debug("Create new interface xml: %s", iface)
     return iface
Exemplo n.º 9
0
        def create_iface_xml(mac):
            """
            Create interface xml file

            :param mac: The mac address of nic device
            """
            iface = Interface(type_name='network')
            iface.source = iface_source
            iface.model = iface_model
            iface.mac_address = mac
            logging.debug("Create new interface xml: %s", iface)
            return iface
Exemplo n.º 10
0
 def create_hostdev_interface(pci_id, managed, model):
     """
         Create hostdev type interface xml.
     """
     attrs = create_address_dict(pci_id)
     new_iface = Interface('hostdev')
     new_iface.managed = managed
     if model != "":
         new_iface.model = model
     new_iface.mac_address = utils_net.generate_mac_address_simple()
     new_iface.hostdev_address = new_iface.new_iface_address(
         **{"attrs": attrs})
     return new_iface
Exemplo n.º 11
0
 def create_hostdev_interface(pci_id, managed, model):
     """
         Create hostdev type interface xml.
     """
     attrs = create_address_dict(pci_id)
     new_iface = Interface('hostdev')
     new_iface.managed = managed
     if model != "":
         new_iface.model = model
     new_iface.mac_address = utils_net.generate_mac_address_simple()
     new_iface.hostdev_address = new_iface.new_iface_address(**{"attrs": attrs})
     chars = string.ascii_letters + string.digits + '-_'
     alias_name = 'ua-' + ''.join(random.choice(chars) for _ in list(range(64)))
     new_iface.alias = {'name': alias_name}
     return new_iface
Exemplo n.º 12
0
 def prepare_iface_xml(iface_bus, iface_slot):
     """
     Create interface xml file
     """
     iface_xml = Interface(type_name='bridge')
     iface_xml.source = {'bridge': 'virbr0'}
     iface_xml.model = "virtio"
     addr_dict = {
         'bus': iface_bus,
         'slot': iface_slot,
         'domain': '0x0000',
         'function': '0x0'
     }
     iface_xml.address = iface_xml.new_iface_address(type_name='pci',
                                                     **{"attrs": addr_dict})
     return iface_xml
Exemplo n.º 13
0
 def create_iface(iface_type, **kwargs):
     """
     Create a interface to be attached to vm
     """
     m_iface = Interface(iface_type)
     m_iface.mac_address = utils_net.generate_mac_address_simple()
     if 'base_if' in kwargs:
         m_iface.source = {'dev': kwargs['base_if'], 'mode': 'vepa'}
     if 'source_net' in kwargs:
         m_iface.source = {'network': kwargs['source_net']}
     if 'mtu' in kwargs:
         m_iface.mtu = {'size': kwargs['mtu']}
     if 'model_net' in kwargs:
         m_iface.model = kwargs['model_net']
     logging.debug(m_iface.get_xml())
     logging.debug(m_iface)
     return m_iface
Exemplo n.º 14
0
    def modify_iface_xml(br_name, nwfilter, vm_name):
        """
        Modify interface xml with the new bridge and the nwfilter

        :param br_name: bridge name
        :param nwfilter: nwfilter name
        :param vm_name: vm name
        """
        vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
        iface_xml = vmxml.get_devices('interface')[0]
        vmxml.del_device(iface_xml)
        iface_xml = Interface(type_name='bridge')
        iface_xml.source = {'bridge': br_name}
        iface_xml.model = 'virtio'
        iface_xml.filterref = iface_xml.new_filterref(name=nwfilter)
        logging.debug("new interface xml is: %s" % iface_xml)
        vmxml.add_device(iface_xml)
        vmxml.sync()
Exemplo n.º 15
0
 def create_iface(iface_type, **kwargs):
     """
     Create a interface to be attached to vm
     """
     m_iface = Interface(iface_type)
     m_iface.mac_address = utils_net.generate_mac_address_simple()
     if 'base_if' in kwargs:
         m_iface.source = {'dev': kwargs['base_if'],
                           'mode': 'vepa'}
     if 'source_net' in kwargs:
         m_iface.source = {'network': kwargs['source_net']}
     if 'mtu' in kwargs:
         m_iface.mtu = {'size': kwargs['mtu']}
     if 'model_net' in kwargs:
         m_iface.model = kwargs['model_net']
     logging.debug(m_iface.get_xml())
     logging.debug(m_iface)
     return m_iface
Exemplo n.º 16
0
 def create_iface_xml(iface_mac):
     """
     Create interface xml file
     """
     iface = Interface(type_name=iface_type)
     source = ast.literal_eval(iface_source)
     if source:
         iface.source = source
     iface.model = iface_model if iface_model else "virtio"
     iface.mac_address = iface_mac
     driver_dict = {}
     driver_host = {}
     driver_guest = {}
     if iface_driver:
         driver_dict = ast.literal_eval(iface_driver)
     if iface_driver_host:
         driver_host = ast.literal_eval(iface_driver_host)
     if iface_driver_guest:
         driver_guest = ast.literal_eval(iface_driver_guest)
     iface.driver = iface.new_driver(driver_attr=driver_dict,
                                     driver_host=driver_host,
                                     driver_guest=driver_guest)
     logging.debug("Create new interface xml: %s", iface)
     return iface
Exemplo n.º 17
0
 def create_interface():
     """
         Call different function to create interface according to the type
     """
     new_iface = Interface('network')
     if vf_type == "vf":
         new_iface = create_hostdev_interface(vf_addr, managed, model)
     if vf_type == "vf_pool":
         netxml = create_hostdev_network()
         virsh.net_define(netxml.xml, ignore_status=True)
         if not inactive_pool:
             virsh.net_start(netxml.name)
         new_iface = create_network_interface(netxml.name)
     if vf_type == "macvtap":
         new_iface = Interface('direct')
         new_iface.source = {"dev": vf_name, "mode": "passthrough"}
         new_iface.mac_address = utils_net.generate_mac_address_simple()
         new_iface.model = "virtio"
     if vf_type == "macvtap_network":
         netxml = create_macvtap_network()
         result = virsh.net_define(netxml.xml, ignore_status=True)
         virsh.net_start(netxml.name)
         new_iface = create_network_interface(netxml.name)
     return new_iface
Exemplo n.º 18
0
    def create_iface(iface_model, iface_source, **kwargs):
        """
        Create an interface to be attached to vm

        :param iface_model: model of the interface device
        :param iface_source: source of the interface device
        :param kwargs: other k-w args that needed to create device
        :return: the newly created interface object
        """
        iface = Interface('network')
        iface.model = iface_model
        iface.source = eval(iface_source)

        if 'mac' in kwargs:
            iface.mac_address = kwargs['mac']
        else:
            mac = utils_net.generate_mac_address_simple()
            iface.mac_address = mac

        if 'address' in kwargs:
            iface.address = iface.new_iface_address(attrs=eval(kwargs['address']))

        logging.debug('iface: %s', iface)
        return iface
Exemplo n.º 19
0
    def modify_iface_xml():
        """
        Modify interface xml options
        Two methods to modify domain interfae:
        1. modify guest xml, define it
        2. attach one interface for running guest

        :return: 0 for successful negative case
                 test.fail is fail for positive/negative case
                 None for successful positive case
        """
        if hotplug_iface:
            iface = Interface(iface_type)
        else:
            vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
            xml_devices = vmxml.devices
            iface_index = xml_devices.index(
                xml_devices.by_device_tag("interface")[0])
            iface = xml_devices[iface_index]

        if iface_type == 'network':
            iface.type_name = iface_type
            source = {iface_type: net_name}
        elif iface_type == 'bridge' and bridge_name:
            iface.type_name = iface_type
            source = {iface_type: bridge_name}
        elif iface_type == 'direct':
            iface.type_name = iface_type
            source = {'dev': interface, 'mode': 'bridge'}

        if source:
            del iface.source
            iface.source = source
        iface_model = params.get("iface_model", "virtio")
        iface.model = iface_model
        iface.coalesce = {'max': coalesce_value}
        if network_type == "ovsbridge" and iface_type == "bridge":
            iface.virtualport_type = "openvswitch"

        if not hotplug_iface:
            vmxml.devices = xml_devices
            vmxml.xmltreefile.write()
            try:
                vmxml.sync()
            except xcepts.LibvirtXMLError as details:
                if status_error:
                    # Expect error for negetive test
                    return 0
                else:
                    test.fail("Define guest: FAIL")
        else:
            if not vm.is_alive():
                vm.start()
                # Wait guest boot completely
                time.sleep(2)
            try:
                ret = virsh.attach_device(vm_name,
                                          iface.xml,
                                          ignore_status=False,
                                          debug=True)
            except process.CmdError as error:
                if status_error:
                    # Expect error for negetive test
                    return 0
                else:
                    test.fail("Define guest: FAIL")
Exemplo n.º 20
0
    def modify_iface_xml():
        """
        Modify interface xml options
        Two methods to modify domain interfae:
        1. modify guest xml, define it
        2. attach one interface for running guest

        :return: 0 for successful negative case
                 test.fail is fail for positive/negative case
                 None for successful positive case
        """
        if hotplug_iface:
            iface = Interface(iface_type)
        else:
            vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
            xml_devices = vmxml.devices
            iface_index = xml_devices.index(
                xml_devices.by_device_tag("interface")[0])
            iface = xml_devices[iface_index]

        if iface_type == 'network':
            iface.type_name = iface_type
            source = {iface_type: net_name}
        elif iface_type == 'bridge' and bridge_name:
            iface.type_name = iface_type
            source = {iface_type: bridge_name}
        elif iface_type == 'direct':
            iface.type_name = iface_type
            source = {'dev': interface, 'mode': 'bridge'}

        if source:
            del iface.source
            iface.source = source
        iface_model = params.get("iface_model", "virtio")
        iface.model = iface_model
        iface.coalesce = {'max': coalesce_value}
        if network_type == "ovsbridge" and iface_type == "bridge":
            iface.virtualport_type = "openvswitch"

        if not hotplug_iface:
            vmxml.devices = xml_devices
            vmxml.xmltreefile.write()
            try:
                vmxml.sync()
            except xcepts.LibvirtXMLError as details:
                if status_error:
                    # Expect error for negetive test
                    return 0
                else:
                    test.fail("Define guest: FAIL")
        else:
            if not vm.is_alive():
                vm.start()
                # Wait guest boot completely
                time.sleep(2)
            try:
                ret = virsh.attach_device(vm_name, iface.xml,
                                          ignore_status=False,
                                          debug=True)
            except process.CmdError as error:
                if status_error:
                    # Expect error for negetive test
                    return 0
                else:
                    test.fail("Define guest: FAIL")
Exemplo n.º 21
0
def run(test, params, env):
    """
    Test pci/pcie-to-pci bridge

    Hotplug interface to pci/pcie-to-pci bridge, then check xml and
    inside vm.
    Hotunplug interface, then check xml and inside vm
    """
    vm_name = params.get('main_vm')
    pci_model = params.get('pci_model', 'pci')
    hotplug = 'yes' == params.get('hotplug', 'no')

    pci_model_name = params.get('pci_model_name')
    pci_br_has_device = 'yes' == params.get('pci_br_has_device', 'no')
    sound_dev_model_type = params.get('sound_dev_model_type', '')
    sound_dev_address = params.get('sound_dev_address', '')
    iface_model = params.get('iface_model', '')
    iface_source = params.get('iface_source', '')

    vmxml = VMXML.new_from_inactive_dumpxml(vm_name)
    bkxml = vmxml.copy()
    vm = env.get_vm(vm_name)

    try:

        # Check if there is a pci/pcie-to-pci bridge, if so,
        # just use the existing pci/pcie-to-pci-bridge to test
        ori_pci_br = [
            dev for dev in vmxml.get_devices('controller')
            if dev.type == 'pci' and dev.model == pci_model
        ]

        # If there is not a pci/pcie-to-pci bridge to test,
        # create one and add to vm
        if not ori_pci_br:
            logging.info('No %s on vm, create one', pci_model)
            pci_bridge = Controller('pci')
            pci_bridge.model = pci_model
            pci_bridge.model_name = {'name': pci_model_name}

            vmxml.add_device(pci_bridge)
            vmxml.sync()
            logging.debug(virsh.dumpxml(vm_name))

        # Check if pci/pcie-to-pci bridge is successfully added
        vmxml = VMXML.new_from_inactive_dumpxml(vm_name)
        cur_pci_br = [
            dev for dev in vmxml.get_devices('controller')
            if dev.type == 'pci' and dev.model == pci_model
        ]
        if not cur_pci_br:
            test.error('Failed to add %s controller to vm xml' % pci_model)

        pci_br = cur_pci_br[0]
        logging.debug(pci_br)
        pci_br_index = pci_br.index

        # If test scenario requires another pci device on pci/pcie-to-pci
        # bridge before hotplug, add a sound device and make sure
        # the 'bus' is same with pci bridge index
        if pci_br_has_device:
            sound_dev = Sound()
            sound_dev.model_type = sound_dev_model_type
            sound_dev.address = eval(sound_dev_address % pci_br_index)
            logging.debug(sound_dev.address)
            vmxml.add_device(sound_dev)
            vmxml.sync()

        # Test hotplug scenario
        if hotplug:
            vm.start()
            vm.wait_for_login().close()

            # Create interface to be hotplugged
            logging.info('Create interface to be hotplugged')
            iface = Interface('network')
            iface.model = iface_model
            iface.source = eval(iface_source)
            mac = utils_net.generate_mac_address_simple()
            iface.mac_address = mac
            logging.debug(iface)

            result = virsh.attach_device(vm_name, iface.xml, debug=True)
            libvirt.check_exit_status(result)

            xml_after_attach = VMXML.new_from_dumpxml(vm_name)
            logging.debug(virsh.dumpxml(vm_name))

            # Check if the iface with given mac address is successfully attached
            iface_list = [
                iface for iface in xml_after_attach.get_devices('interface')
                if iface.mac_address == mac
            ]

            logging.debug('iface list after attach: %s', iface_list)
            if not iface_list:
                test.error('Failed to attach interface %s' % iface)

            # Check inside vm
            def check_inside_vm(session, expect=True):
                ip_output = session.cmd('ip a')
                logging.debug(ip_output)

                return expect if mac in ip_output else not expect

            session = vm.wait_for_serial_login()
            if not utils_misc.wait_for(lambda: check_inside_vm(session, True),
                                       timeout=60,
                                       step=5):
                test.fail('Check interface inside vm failed,'
                          'interface not successfully attached:'
                          'not found mac address %s' % mac)
            session.close()

            # Test hotunplug
            result = virsh.detach_device(vm_name, iface.xml, debug=True)
            libvirt.check_exit_status(result)

            logging.debug(virsh.dumpxml(vm_name))

            # Check if the iface with given mac address has been
            # successfully detached
            xml_after_detach = VMXML.new_from_dumpxml(vm_name)
            iface_list_after_detach = [
                iface for iface in xml_after_detach.get_devices('interface')
                if iface.mac_address == mac
            ]

            logging.debug('iface list after detach: %s',
                          iface_list_after_detach)
            if iface_list_after_detach:
                test.fail('Failed to detach device: %s', iface)

            # Check again inside vm
            session = vm.wait_for_serial_login()
            if not utils_misc.wait_for(lambda: check_inside_vm(session, False),
                                       timeout=60,
                                       step=5):
                test.fail('Check interface inside vm failed,'
                          'interface not successfully detached:'
                          'found mac address %s' % mac)
            session.close()

    finally:
        bkxml.sync()