Example #1
0
 def setup_hostdev():
     """
     Setup test environment for hostdev
     """
     pf_pci = utils_sriov.get_pf_pci()
     sriov_base.setup_vf(pf_pci, params)
     vf_pci = utils_misc.wait_for(lambda: utils_sriov.get_vf_pci_id(pf_pci),
                                  30, 5)
     pci_to_addr = utils_sriov.pci_to_addr(vf_pci)
     if params.get('iface_dict'):
         params.update({'iface_dict': params.get('iface_dict') % pci_to_addr})
     elif params.get('hostdev_dict'):
         del pci_to_addr['type']
         params.update(
             {'hostdev_dict': params.get('hostdev_dict') % pci_to_addr})
Example #2
0
    def setup_max_vfs():
        """
        Setup for max_vfs case

        1. Check test environment
        2. Enable VFs
        3. Create networks
        """
        if not utils_misc.compare_qemu_version(4, 0, 0, False):
            test.cancel("This test is supported from qemu-kvm 4.0.0.")
        if len(pf_info) < 2:
            test.cancel("This test requires at least 2 PFs.")

        pf_id_list = get_pf_id_list(pf_info, driver)
        for pf_pci in pf_id_list:
            sriov_base.recover_vf(pf_pci, params)
            sriov_base.setup_vf(pf_pci, params)

        net_info = get_net_dict(pf_info)
        for pf_dev, net_name in net_info.items():
            create_network(net_name, pf_dev, params)
Example #3
0
    def test_vf():
        """
        Detach/Reattach a vf when it is used in guest

        1) Detach/reattach the device
        2) Add the device to VM
        3) Start the VM
        4) Check driver of the device
        5) Detach/reattach the device again
        """
        logging.info("Initialize the vfs.")
        sriov_base.setup_vf(pf_pci, params)
        vf_pci = utils_sriov.get_vf_pci_id(pf_pci)
        pf_name = utils_sriov.get_pf_info_by_pci(pf_pci).get('iface')
        vf_mac = utils_sriov.get_vf_mac(pf_name, is_admin=False)
        logging.debug("VF's mac: %s.", vf_mac)

        logging.info("Check the vf's driver, it should not be vfio-pci.")
        dev_name = utils_sriov.get_device_name(vf_pci)
        check_driver_from_xml(dev_name)

        logging.info("Detach and reattach the device and check vf's mac.")
        nodedev_test(dev_name, no_reset=True)
        utils_misc.wait_for(
            lambda: libvirt_vfio.check_vfio_pci(vf_pci, True, True), 10, 5)
        compare_vf_mac(pf_name, vf_mac)

        logging.info("Cold-plug the device into the VM.")
        add_hostdev_iface(vm, vf_pci)
        vm.start()
        check_hostdev_iface(vm.name)

        logging.info("Check the device info. It should be vfio-pci.")
        check_driver_from_xml(dev_name, status_error=True)
        nodedev_test(dev_name, True)

        logging.info("Destroy the vm, and check the vf's mac is recovered.")
        vm.destroy(gracefully=False)
        compare_vf_mac(pf_name, vf_mac)
Example #4
0
def run(test, params, env):
    """
    Test interfaces attached from network
    """
    def test_at_dt():
        """
        Test attach-detach interfaces
        """
        if not pf_status:
            logging.info("Set pf state to down.")
            pf_iface_obj = utils_net.Interface(pf_name)
            pf_iface_obj.down()

        logging.info("Define network - %s.", params.get("net_name"))
        create_network(params)

        logging.debug("Remove VM's interface devices.")
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        vm.start()
        vm_session = vm.wait_for_serial_login(timeout=240)

        logging.info("Hotplug an interface to VM.")
        iface_dict = {
            "model": "virtio",
            "source": {
                'network': params.get("net_name")
            }
        }
        iface = create_iface(iface_dict)
        res = virsh.attach_device(vm_name, iface.xml, debug=True)
        libvirt.check_exit_status(res, status_error)
        if not pf_status:
            logging.info("Set pf state to up then check again.")
            pf_iface_obj.up()
            virsh.attach_device(vm_name,
                                iface.xml,
                                debug=True,
                                ignore_status=False)
        libvirt_vmxml.check_guest_xml(vm.name, params["net_name"])
        sriov_base.check_vm_network_accessed(vm_session)

    def test_connection():
        """
        Test network connections

        1. Create a network
        2. Attach the interfaces and check network connections
        3. Check the network connections after detaching ifaces, restarting
            libvirtd and destroying the VM
        """
        vf_no = int(params.get("vf_no", "4"))
        net_name = params.get("net_name")
        iface_type = params.get("iface_type")

        logging.info("Define network - %s.", net_name)
        create_network(params)
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        libvirt_pcicontr.reset_pci_num(vm_name)
        vm.start()
        vm.wait_for_serial_login(timeout=240)

        logging.info("Attach 4 interfaces to the guest.")
        opts = ' '.join(
            ["network", net_name,
             params.get('attach_extra_opts', "")])
        for i in range(vf_no):
            virsh.attach_interface(vm_name,
                                   option=opts,
                                   debug=True,
                                   ignore_status=False)
            libvirt_network.check_network_connection(net_name, i + 1)

        logging.info("Try to attach one more interface.")
        res = virsh.attach_interface(vm_name, option=opts, debug=True)
        libvirt.check_exit_status(res, True)

        logging.info("Detach an interface.")
        vm_ifaces = [
            iface for iface in vm_xml.VMXML.new_from_dumpxml(
                vm_name).devices.by_device_tag("interface")
        ]
        mac_addr = vm_ifaces[0].get_mac_address()
        opts = ' '.join([iface_type, "--mac %s" % mac_addr])
        virsh.detach_interface(vm_name,
                               option=opts,
                               debug=True,
                               wait_for_event=True,
                               ignore_status=False)
        libvirt_network.check_network_connection(net_name, vf_no - 1)

        logging.info(
            "Restart libvirtd service and check the network connection.")
        utils_libvirtd.Libvirtd().restart()
        libvirt_network.check_network_connection(net_name, vf_no - 1)
        logging.info("Destroy the VM and check the network connection.")
        vm.destroy(gracefully=False)
        libvirt_network.check_network_connection(net_name, 0)

    test_case = params.get("test_case", "")
    run_test = eval("test_%s" % test_case)
    status_error = "yes" == params.get("status_error", "no")

    vm_name = params.get("main_vm", "avocado-vt-vm1")
    vm = env.get_vm(vm_name)
    pf_pci = utils_sriov.get_pf_pci()
    if not pf_pci:
        test.cancel("NO available pf found.")
    sriov_base.setup_vf(pf_pci, params)

    vf_pci = utils_sriov.get_vf_pci_id(pf_pci)
    params['vf_iface'] = utils_sriov.get_iface_name(vf_pci)
    pf_status = "active" == params.get("pf_status", "active")
    pf_name = utils_sriov.get_pf_info_by_pci(pf_pci).get('iface')
    params['pf_name'] = pf_name
    vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
    orig_config_xml = vmxml.copy()

    try:
        run_test()

    finally:
        logging.info("Recover test enviroment.")
        if not pf_status:
            pf_iface_obj = utils_net.Interface(pf_name)
            pf_iface_obj.up()
        sriov_base.recover_vf(pf_pci, params)
        if vm.is_alive():
            vm.destroy(gracefully=False)
        orig_config_xml.sync()
        libvirt_network.create_or_del_network(
            {"net_name": params.get("net_name")}, True)
def run(test, params, env):
    """
    SR-IOV: managed related test.
    """
    def start_vm(vm, test_login=False, destroy_vm=False):
        """
        Start up VM

        :param vm: The vm object
        :param test_login: Whether to login VM
        :param destroy_vm: Whether to destroy VM
        """
        if vm.is_alive():
            vm.destroy()
        vm.start()
        if test_login:
            vm.wait_for_serial_login(timeout=180).close()
        if destroy_vm:
            vm.destroy()

    def create_vf_pool():
        """
        Create VF pool
        """
        net_hostdev_dict = {
            "net_name": params.get("net_name"),
            "net_forward": params.get("net_forward"),
            "vf_list_attrs": "[%s]" % utils_sriov.pci_to_addr(vf_pci)
        }
        libvirt_network.create_or_del_network(net_hostdev_dict)

    def check_vm_iface_managed(vm_name, iface_dict):
        """
        Check 'managed' in VM's iface

        :param vm_name: Name of VM
        :param iface_dict: The parameters dict
        :raise: TestFail if not match
        """
        vm_iface_managed = [
            iface.get("managed") for iface in vm_xml.VMXML.new_from_dumpxml(
                vm_name).devices.by_device_tag("interface")
        ][0]
        expr_managed = "yes" if iface_dict.get("managed",
                                               "") == "yes" else None
        if vm_iface_managed != expr_managed:
            test.fail("Unable to get the expected managed! Actual: %s, "
                      "Expected: %s." % (vm_iface_managed, expr_managed))

    def test_networks():
        """
        Start vm with VF from VF Pool with "managed=no" or default setting

        1) Create VF pool
        2) Prepare device xml and hot-plug to the guest
        3) Detach the device from host
        4) Check the driver of device
        5) Start VM
        6) Destroy vm then check the driver
        7) Reattach the device to the host and check the driver
        """
        create_vf_pool()
        libvirt_vfio.check_vfio_pci(vf_pci, status_error=True)
        iface_dict = {
            "type": "network",
            "source": "{'network': '%s'}" % params.get("net_name")
        }
        libvirt.modify_vm_iface(vm.name, "update_iface", iface_dict)
        res = virsh.start(vm.name, debug=True)
        libvirt.check_exit_status(res, True)

        virsh.nodedev_detach(dev_name, debug=True, ignore_status=False)
        libvirt_vfio.check_vfio_pci(vf_pci)
        start_vm(vm, True, True)
        libvirt_vfio.check_vfio_pci(vf_pci)
        virsh.nodedev_reattach(dev_name, debug=True, ignore_status=False)
        libvirt_vfio.check_vfio_pci(vf_pci, status_error=True)

    def test_device_hotplug():
        """
        Hotplug/unplug VF with managed='no'

        1) Prepare a running guest
        2) Check the driver of vf on host
        3) Prepare a xml with "managed=no"and attach to guest
        4) Detach the device from host
        5) Check the driver of vf on host
        6) Attach the device to guest
        7) Check the interface of the guest
        8) Detach the device from guest and check the driver
        9) Reattach the device to the host and check the driver
        """
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        start_vm(vm)
        libvirt_vfio.check_vfio_pci(vf_pci, status_error=True)
        mac_addr = utils_net.generate_mac_address_simple()
        iface_dict = eval(
            params.get('iface_dict', '{"hostdev_addr": "%s"}') %
            utils_sriov.pci_to_addr(vf_pci))
        iface = interface.Interface("hostdev")
        iface.xml = libvirt.modify_vm_iface(vm.name, "get_xml", iface_dict)
        res = virsh.attach_device(vm_name, iface.xml, debug=True)
        libvirt.check_exit_status(res, True)
        virsh.nodedev_detach(dev_name, debug=True, ignore_status=False)
        libvirt_vfio.check_vfio_pci(vf_pci)
        virsh.attach_device(vm_name,
                            iface.xml,
                            debug=True,
                            ignore_status=False)

        check_vm_iface_managed(vm_name, iface_dict)
        vm.wait_for_serial_login().close()
        virsh.detach_device(vm_name,
                            iface.xml,
                            wait_remove_event=True,
                            debug=True,
                            ignore_status=False)
        libvirt_vfio.check_vfio_pci(vf_pci)
        virsh.nodedev_reattach(dev_name, debug=True, ignore_status=False)
        libvirt_vfio.check_vfio_pci(vf_pci, status_error=True)

    test_case = params.get("test_case", "")
    run_test = eval("test_%s" % test_case)

    vm_name = params.get("main_vm", "avocado-vt-vm1")
    vm = env.get_vm(vm_name)
    pf_pci = utils_sriov.get_pf_pci()
    if not pf_pci:
        test.cancel("NO available pf found.")
    default_vf = sriov_base.setup_vf(pf_pci, params)

    vf_pci = utils_sriov.get_vf_pci_id(pf_pci)
    dev_name = utils_sriov.get_device_name(vf_pci)
    vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
    orig_config_xml = vmxml.copy()

    try:
        run_test()

    finally:
        logging.info("Recover test enviroment.")
        sriov_base.recover_vf(pf_pci, params, default_vf)
        if vm.is_alive():
            vm.destroy(gracefully=False)
        orig_config_xml.sync()
        libvirt_network.create_or_del_network(
            {"net_name": params.get("net_name")}, True)
        virsh.nodedev_reattach(dev_name, debug=True)
def run(test, params, env):
    """
    Test when the PCI configuration file is in read-only mode
    """
    def test_vf_hotplug():
        """
        Hot-plug VF to VM

        """
        logging.info("Preparing a running guest...")
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        vm.start()
        vm_session = vm.wait_for_serial_login(timeout=180)

        logging.info("Attaching VF to the guest...")
        mac_addr = utils_net.generate_mac_address_simple()
        iface_dict = eval(
            params.get('iface_dict', '{"hostdev_addr": "%s"}') %
            utils_sriov.pci_to_addr(vf_pci))
        iface = interface.Interface("hostdev")
        iface.xml = libvirt.modify_vm_iface(vm.name, "get_xml", iface_dict)
        virsh.attach_device(vm_name,
                            iface.xml,
                            debug=True,
                            ignore_status=False)

        logging.info("Checking VF in the guest...")
        vm_iface_types = [
            iface.get_type_name() for iface in vm_xml.VMXML.new_from_dumpxml(
                vm_name).devices.by_device_tag("interface")
        ]
        if 'hostdev' not in vm_iface_types:
            test.fail('Unable to get hostdev interface!')
        if cmd_in_vm:
            if not utils_misc.wait_for(
                    lambda: not vm_session.cmd_status(cmd_in_vm), 30, 10):
                test.fail("Can not get the Virtual Function info on vm!")
        vm_session.close()

    libvirt_version.is_libvirt_feature_supported(params)

    test_case = params.get("test_case", "")
    run_test = eval("test_%s" % test_case)
    cmd_in_vm = params.get("cmd_in_vm")

    vm_name = params.get("main_vm", "avocado-vt-vm1")
    vm = env.get_vm(vm_name)
    pf_pci = utils_sriov.get_pf_pci()
    if not pf_pci:
        test.cancel("NO available pf found.")
    default_vf = sriov_base.setup_vf(pf_pci, params)
    vf_pci = utils_sriov.get_vf_pci_id(pf_pci)
    dev_name = utils_sriov.get_device_name(vf_pci)

    vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
    orig_config_xml = vmxml.copy()
    libvirtd = utils_libvirtd.Libvirtd('virtqemud')
    try:
        virsh.nodedev_detach(dev_name, debug=True, ignore_status=False)
        logging.info("Re-mounting sysfs with ro mode...")
        utils_misc.mount('/sys', '', None, 'remount,ro')
        libvirtd.restart()
        run_test()
    finally:
        logging.info("Recover test enviroment.")
        utils_misc.mount('/sys', '', None, 'remount,rw')
        sriov_base.recover_vf(pf_pci, params, default_vf)
        if vm.is_alive():
            vm.destroy(gracefully=False)
        orig_config_xml.sync()
        virsh.nodedev_reattach(dev_name, debug=True)
Example #7
0
def run(test, params, env):
    """
    Test VFs when PF is down
    """
    def setup_default():
        """
        Default setup
        """
        test.log.info("Set pf state to down.")
        pf_iface_obj = utils_net.Interface(pf_name)
        pf_iface_obj.down()

    def teardown_default():
        """
        Default cleanup
        """
        pf_iface_obj = utils_net.Interface(pf_name)
        pf_iface_obj.up()

    def test_at_dt():
        """
        Test attach-detach interfaces
        """
        options = '' if vm.is_alive() else '--config'
        iface_dict = eval(params.get('iface_dict')
                          % utils_sriov.pci_to_addr(vf_pci))
        iface = interface_base.create_iface('hostdev', iface_dict)
        result = virsh.attach_device(vm_name, iface.xml,
                                     flagstr=options, debug=True)
        if not start_vm:
            result = virsh.start(vm.name, debug=True)
        libvirt.check_exit_status(result, status_error)
        if error_msg:
            libvirt.check_result(result, error_msg)

    test_case = params.get("test_case", "")
    run_test = eval("test_%s" % test_case)
    start_vm = "yes" == params.get("start_vm")
    status_error = "yes" == params.get("status_error", "no")
    error_msg = params.get("error_msg")

    vm_name = params.get("main_vm", "avocado-vt-vm1")
    vm = env.get_vm(vm_name)
    pf_pci = utils_sriov.get_pf_pci()
    if not pf_pci:
        test.cancel("NO available pf found.")
    sriov_base.setup_vf(pf_pci, params)

    vf_pci = utils_sriov.get_vf_pci_id(pf_pci)
    pf_name = utils_sriov.get_pf_info_by_pci(pf_pci).get('iface')

    setup_test = eval("setup_%s" % test_case) if "setup_%s" % test_case in \
        locals() else setup_default
    teardown_test = eval("teardown_%s" % test_case) if "teardown_%s" % \
        test_case in locals() else teardown_default
    vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
    orig_config_xml = vmxml.copy()

    try:
        setup_test()
        run_test()

    finally:
        test.log.info("Recover test enviroment.")
        orig_config_xml.sync()
        teardown_test()
def run(test, params, env):
    """
    Test hostdev attach/detach
    """
    def setup_default():
        """
        Default setup
        """
        test.log.info("TEST_SETUP: Clear up VM interface(s).")
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')

    def teardown_default():
        """
        Default cleanup
        """
        test.log.info("TEST_TEARDOWN: Recover test enviroment.")
        orig_config_xml.sync()
        sriov_base.recover_vf(pf_pci, params, 0)

    def exec_test(vm, hostdev_dict, params):
        """
        Execute basic test

        :param vm: VM object
        :param hostdev_dict: Hostdev attrs
        :param params: Test parameters
        """
        start_vm = "yes" == params.get("start_vm")
        options = '' if vm.is_alive() else '--config'
        status_error = "yes" == params.get("status_error", "no")
        error_msg = params.get("error_msg")

        host_dev = create_dev(params, hostdev_dict)
        test.log.debug("Hostdev XML: %s.", host_dev)
        test.log.info("TEST_STEP1: Attach hostdev interface.")
        result = virsh.attach_device(vm_name,
                                     host_dev.xml,
                                     flagstr=options,
                                     debug=True)
        libvirt.check_exit_status(result, status_error)
        if error_msg:
            libvirt.check_result(result, error_msg)
        if status_error:
            return
        if not start_vm:
            vm.start()
            vm.wait_for_serial_login(timeout=240).close()

        test.log.info("TEST_STEP2: Check VM XML.")
        device_type = "interface" if params.get(
            'hostdev_iface_dict') else 'hostdev'
        check_points.comp_hostdev_xml(vm, device_type, hostdev_dict)

    def test_unassigned_address():
        """
        Cold/Hot plug hostdev interface with 'unassigned' address type
        """
        hostdev_dict = get_hostdev_dict(vf_pci, params)
        exec_test(vm, hostdev_dict, params)
        if params.get("status_error") == "yes":
            return
        test.log.info("Check if the VM is not using VF.")
        libvirt_vfio.check_vfio_pci(vf_pci)

        vm.cleanup_serial_console()
        vm.create_serial_console()
        vm_session = vm.wait_for_serial_login(timeout=240)
        p_iface = utils_net.get_remote_host_net_ifs(vm_session)[0]
        if p_iface:
            test.fail("There should be no interface, but got %s." % p_iface)

    def test_duplicated_cust_alias():
        """
        Hotplug hostdev interface with duplicate custom alias
        """
        vm.cleanup_serial_console()
        vm.create_serial_console()
        vm.wait_for_serial_login(timeout=240).close()
        alias_name = 'ua-' + str(uuid.uuid4())
        hostdev_dict = eval(
            params.get('hostdev_iface_dict') %
            (utils_sriov.pci_to_addr(vf_pci), alias_name))
        exec_test(vm, hostdev_dict, params)

        host_dev = vm_xml.VMXML.new_from_dumpxml(vm.name)\
            .devices.by_device_tag("interface")[0]
        test.log.info("TEST_STEP3: Hotplug another hostdev interface with the"
                      "same alias name")
        vf2_pci = utils_sriov.get_vf_pci_id(pf_pci, vf_index=1)
        hostdev_dict['hostdev_address']['attrs'] = utils_sriov.pci_to_addr(
            vf2_pci)
        host_dev2 = interface_base.create_iface('hostdev', hostdev_dict)
        result = virsh.attach_device(vm_name, host_dev2.xml, debug=True)
        libvirt.check_exit_status(result, True)

        test.log.info("TEST_STEP4: Detach the first hostdev interface.")
        virsh.detach_device(vm_name,
                            host_dev.xml,
                            wait_for_event=True,
                            debug=True,
                            ignore_status=False)

        test.log.info("TEST_STEP5: Attach the second hostdev interface again.")
        virsh.attach_device(vm_name,
                            host_dev2.xml,
                            debug=True,
                            ignore_status=False)
        check_points.comp_hostdev_xml(vm, "interface", hostdev_dict)

    libvirt_version.is_libvirt_feature_supported(params)

    test_case = params.get("test_case", "")
    run_test = eval("test_%s" % test_case)

    vm_name = params.get("main_vm", "avocado-vt-vm1")
    vm = env.get_vm(vm_name)
    pf_pci = utils_sriov.get_pf_pci()
    if not pf_pci:
        test.cancel("NO available pf found.")
    sriov_base.setup_vf(pf_pci, params)

    vf_pci = utils_sriov.get_vf_pci_id(pf_pci)

    setup_test = eval("setup_%s" % test_case) if "setup_%s" % test_case in \
        locals() else setup_default
    teardown_test = eval("teardown_%s" % test_case) if "teardown_%s" % \
        test_case in locals() else teardown_default
    vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
    orig_config_xml = vmxml.copy()

    try:
        test.log.info("TEST_CASE: %s",
                      run_test.__doc__.lstrip().split('\n\n')[0])
        setup_test()
        run_test()

    finally:
        teardown_test()