Esempio n. 1
0
    def test_pf():
        """
        Reattach/reset/detach a pci device when it is used in guest

        1) Detach/Reset/reattach the device
        2) Add the device to VM
        3) Start the VM
        4) Check driver of the device
        5) Detach/Reset/reattach the device again
        """
        dev_name = utils_sriov.get_device_name(pf_pci)
        check_driver_from_xml(dev_name)
        nodedev_test(dev_name)
        add_hostdev_device(vm_name, pf_pci)
        vm.start()
        check_hostdev_device(vm_name)
        check_driver_from_xml(dev_name, status_error=True)
        nodedev_test(dev_name, True)
Esempio n. 2
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)
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)