Exemple #1
0
 def setup_save_restore_hostdev_iface_with_teaming():
     logging.info("Create hostdev network.")
     net_hostdev_fwd = params.get("net_hostdev_fwd",
                                  '{"mode": "hostdev", "managed": "yes"}')
     net_hostdev_dict = {
         "net_name": net_hostdev_name,
         "net_forward": net_hostdev_fwd,
         "net_forward_pf": '{"dev": "%s"}' % pf_name
     }
     libvirt_network.create_or_del_network(net_hostdev_dict)
     libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
     vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
     iface = interface.Interface("network")
     iface.xml = create_bridge_iface_xml(vm, mac_addr, params)
     vmxml.add_device(iface)
     iface.xml = create_hostdev_iface_xml(vm, mac_addr, params)
     vmxml.add_device(iface)
     vmxml.sync()
     logging.debug("VMXML after updating ifaces: %s.",
                   vm_xml.VMXML.new_from_dumpxml(vm_name))
     vm.start()
     vm_session = vm.wait_for_serial_login(timeout=240)
     ping_ip = get_ping_dest(vm_session, mac_addr)
     check_vm_network_accessed(vm_session,
                               ping_dest=ping_ip,
                               tcpdump_iface=bridge_name,
                               tcpdump_status_error=True)
Exemple #2
0
    def setup_test(dev_type):
        """
        Set up test

        1) Remove interface devices
        2) Setup test environment for a specific interface if needed
        3) Set VM attrs

        :param dev_type: interface type
        :return: An object of special test environment
        """
        logging.debug("Remove VM's interface devices.")
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        test_env_obj = None
        if dev_type == 'vdpa':
            test_env_obj = setup_vdpa()

        logging.debug("Update VM's settings.")
        vm_attrs = eval(params.get('vm_attrs', '{}'))
        vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
        vmxml.setup_attrs(**vm_attrs)
        vmxml.sync()
        logging.debug("Updated VM xml: %s.",
                      vm_xml.VMXML.new_from_dumpxml(vm_name))
        return test_env_obj
    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()
Exemple #4
0
    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)
Exemple #5
0
def add_hostdev_iface(vm, vf_pci):
    """
    Add hostdev device to VM

    :param vm: VM object
    :param vf_pci: PCI ID of a VF
    """
    libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
    iface_dict = {"type": "hostdev", "managed": "yes",
                  "hostdev_addr": str(utils_sriov.pci_to_addr(vf_pci))}
    libvirt.modify_vm_iface(vm.name, "update_iface", iface_dict)
 def setup_default():
     """
     Default setup
     """
     logging.debug("Remove VM's interface devices.")
     libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
     vm_attrs = eval(params.get('vm_attrs', '{}'))
     if vm_attrs:
         vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
         vmxml.setup_attrs(**vm_attrs)
         vmxml.sync()
Exemple #7
0
    def setup_save_restore_hostdev_device_with_teaming():
        logging.info("Start a VM with bridge iface and hostdev device.")
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
        iface = interface.Interface("network")
        iface.xml = create_bridge_iface_xml(vm, mac_addr, params)
        vmxml.add_device(iface)

        hostdev_dev = libvirt.create_hostdev_xml(vf_pci,
                                                 teaming=hostdev_teaming_dict)
        vmxml.add_device(hostdev_dev)
        vmxml.sync()
        vm.start()
        utils_sriov.set_vf_mac(pf_name, mac_addr)
        vm.wait_for_serial_login(timeout=240).close()
Exemple #8
0
    def setup_hotplug_hostdev_iface_with_teaming():
        logging.info("Create hostdev network.")
        net_hostdev_fwd = params.get("net_hostdev_fwd",
                                     '{"mode": "hostdev", "managed": "yes"}')
        net_hostdev_dict = {
            "net_name": net_hostdev_name,
            "net_forward": net_hostdev_fwd,
            "net_forward_pf": '{"dev": "%s"}' % pf_name
        }
        libvirt_network.create_or_del_network(net_hostdev_dict)

        logging.info("Clear up VM interface.")
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        vm.start()
        vm.wait_for_serial_login(timeout=180).close()
Exemple #9
0
    def prepare_vmxml(vm, vm_name, direct=False):
        """
        Ensure there is only 1 requested interface in the vmxml

        param vm: the test vm
        param vm_name: the vm'name
        param direct: True or False, if True, prepare vm xml with a direct type
                      interface(the tap device will be named as macvtap*);
                      if False , prepare vm xml with a network type interface
                      connected to default network(the tap device will be named
                      as vnet* automatically instead)
        :return: None
        """
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        iface_dict = prepare_iface_dict(direct)
        libvirt.modify_vm_iface(vm_name, 'update_iface', iface_dict)
Exemple #10
0
    def test_max_vfs():
        """
        Hotplug MAX VFs to guest

        1. Start vm with 64 vfio interfaces
        2. Check networks
        3. Try to hot plug the 65th hostdev interface
        4. Destroy vm and cold plug 1 hostdev interface
        """
        net_info = get_net_dict(pf_info)
        vf_no = int(params.get("vf_no", "63"))

        logging.debug("Remove VM's interface devices.")
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        compare_vm_iface(test, get_vm_iface_num(vm_name), 0)

        logging.info("Cold plug 64 interfaces to VM.")
        opts = "network %s --config" % list(net_info.values())[0]
        for i in range(vf_no):
            virsh.attach_interface(vm_name,
                                   opts,
                                   debug=True,
                                   ignore_status=False)
        net_name_2 = list(net_info.values())[1]
        opts = "network %s --config" % net_name_2
        virsh.attach_interface(vm_name, opts, debug=True, ignore_status=False)
        compare_vm_iface(test, get_vm_iface_num(vm_name), vf_no + 1)

        logging.info("Start VM and check networks.")
        vm.start()
        vm_session = vm.wait_for_serial_login(timeout=240)
        res = vm_session.cmd_status_output(
            'lspci |grep Ether')[1].strip().splitlines()
        compare_vm_iface(test, len(res), vf_no + 1)

        logging.info("Hot Plug the 65th iface.")
        opts_hotplug = "network %s" % net_name_2
        res = virsh.attach_interface(vm_name, opts_hotplug)
        libvirt.check_exit_status(res, True)

        logging.info("Destroy vm and cold plug the 65th hostdev interface.")
        vm.destroy()
        virsh.attach_interface(vm_name, opts, ignore_status=False)

        compare_vm_iface(test, get_vm_iface_num(vm_name), vf_no + 2)
        res = virsh.start(vm_name, debug=True)
        libvirt.check_exit_status(res, True)
Exemple #11
0
    def setup_test_virtio_mem():
        """
        Setup vmxml for test
        """
        set_num_huge_pages = params.get("set_num_huge_pages")
        if set_num_huge_pages:
            utils_memory.set_num_huge_pages(int(set_num_huge_pages))

        libvirt_vmxml.remove_vm_devices_by_type(vm, 'memory')
        vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
        vm_attrs = eval(params.get('vm_attrs', '{}'))
        vmxml.setup_attrs(**vm_attrs)
        vmxml.sync()

        if params.get("start_vm") == "yes":
            vm.start()
            vm.wait_for_login().close()
    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)
    def setup_test_virtio_mem():
        """
        Setup vmxml for test
        """
        set_num_huge_pages = params.get("set_num_huge_pages")
        if set_num_huge_pages:
            utils_memory.set_num_huge_pages(int(set_num_huge_pages))

        libvirt_vmxml.remove_vm_devices_by_type(vm, 'memory')
        vm_attrs = eval(params.get('vm_attrs', '{}'))
        vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
        vmxml.setup_attrs(**vm_attrs)
        vmxml.sync()
        mem_device_attrs = eval(params.get('mem_device_attrs', '{}'))
        mem_device = Memory()
        mem_device.setup_attrs(**mem_device_attrs)

        virsh.attach_device(vm_name,
                            mem_device.xml,
                            flagstr='--config',
                            debug=True,
                            ignore_status=False)
Exemple #14
0
    def setup_hotplug_hostdev_iface_with_teaming():
        logging.info("Create hostdev network.")
        net_hostdev_fwd = params.get("net_hostdev_fwd",
                                     '{"mode": "hostdev", "managed": "yes"}')
        net_hostdev_dict = {
            "net_name": net_hostdev_name,
            "net_forward": net_hostdev_fwd,
            "net_forward_pf": '{"dev": "%s"}' % pf_name
        }
        libvirt_network.create_or_del_network(net_hostdev_dict)

        logging.info("Clear up VM interface.")
        libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
        iface = interface.Interface("network")
        iface.xml = create_bridge_iface_xml(vm, mac_addr, params)
        virsh.attach_device(vm_name,
                            iface.xml,
                            flagstr='--persistent',
                            debug=True,
                            ignore_status=False)
        vm.start()
        vm.wait_for_serial_login(timeout=180).close()
Exemple #15
0
    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)
Exemple #16
0
def run(test, params, env):
    """
    Test openvswitch support for network.

    1.Prepare test environment,destroy or suspend a VM.
    2.Edit xml and start the domain.
    3.Perform test operation.
    4.Recover test environment.
    5.Confirm the test result.
    """
    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)

    # install openvswitch on host.
    if distro.detect().name == 'Ubuntu':
        pkg = "openvswitch-switch"
    else:
        pkg = "openvswitch"

    ovs_service = service.Factory.create_service(pkg)

    if not shutil.which('ovs-vsctl') and not utils_package.package_install(
            pkg):
        test.cancel("Failed to install dependency package %s" " on host" % pkg)
    if not ovs_service.status():
        logging.debug("Restart %s service.." % pkg)
        ovs_service.restart()

    def check_ovs_port(ifname, brname):
        """
        Check OVS port that created by libvirt
        """
        pg_name = params.get("porgroup_name", "").split()
        pg_vlan = params.get("portgroup_vlan", "").split()
        if_source = eval(iface_source)
        port_vlan = {}
        if "portgroup" in if_source:
            pg = if_source["portgroup"]
            for (name, vlan) in zip(pg_name, pg_vlan):
                if pg == name:
                    port_vlan = eval(vlan)
        # Check bridge name by port name
        _, bridge = utils_net.find_current_bridge(ifname)
        assert bridge == brname
        # Get port info from ovs-vsctl output
        cmd = "ovs-vsctl list port %s" % ifname
        output = process.run(cmd, shell=True).stdout_text
        logging.debug("ovs port output: %s", output)
        for line in output.splitlines():
            if line.count("tag"):
                tag_info = line.rsplit(':')
                if ("id" in port_vlan and tag_info[0] == "tag"):
                    assert port_vlan["id"] == tag_info[1]
            elif line.count("vlan_mode"):
                mode_info = line.rsplit(':')
                if ("nativeMode" in port_vlan and mode_info[0] == "vlan_mode"):
                    assert (port_vlan["nativeMode"] == "native-%s" %
                            mode_info[1])

    start_error = "yes" == params.get("start_error", "no")

    # network specific attributes.
    net_name = params.get("net_name", "default")
    net_bridge = params.get("net_bridge", "{'name':'virbr0'}")
    iface_source = params.get("iface_source", "{}")
    create_network = "yes" == params.get("create_network", "no")
    change_iface_option = "yes" == params.get("change_iface_option", "no")
    test_ovs_port = "yes" == params.get("test_ovs_port", "no")
    iface_inbound = params.get("iface_bandwidth_inbound")
    iface_outbound = params.get("iface_bandwidth_outbound")
    test_qos = "yes" == params.get("test_qos", "no")
    hotplug = "yes" == params.get("hotplug", "no")
    iface_type = params.get("iface_type", "bridge")
    iface_model = params.get("iface_model", "virtio")
    iface_virtualport = params.get("iface_virtualport")
    live_add_qos = "yes" == params.get("live_add_qos", 'no')

    libvirt_version.is_libvirt_feature_supported(params)
    # Destroy the guest first
    if vm.is_alive():
        vm.destroy(gracefully=False)

    # Back up xml file.
    netxml_backup = NetworkXML.new_from_net_dumpxml("default")
    iface_mac = vm_xml.VMXML.get_first_mac_by_name(vm_name)
    params["guest_mac"] = iface_mac
    vmxml_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)

    bridge_name = eval(net_bridge)['name']
    # Build the xml and run test.
    try:
        # Edit the network xml or create a new one.
        if create_network:
            # Try to add ovs bridge first
            if not utils_net.ovs_br_exists(bridge_name):
                utils_net.add_ovs_bridge(bridge_name)
            netxml = libvirt.create_net_xml(net_name, params)
            netxml.sync()
        # Edit the interface xml.
        if change_iface_option:
            # Try to add bridge if needed
            source = eval(iface_source)
            if source:
                if "bridge" in source:
                    if not utils_net.ovs_br_exists(source["bridge"]):
                        utils_net.add_ovs_bridge(source["bridge"])
            iface_dict = {
                'type': iface_type,
                'model': iface_model,
                'source': iface_source,
                'virtualport_type': iface_virtualport,
                'inbound': iface_inbound,
                'outbound': iface_outbound
            }
            if live_add_qos:
                iface_dict.pop('inbound')
                iface_dict.pop('outbound')
            if not hotplug:
                libvirt.modify_vm_iface(vm_name, 'update_iface', iface_dict)
            else:
                iface_attach_xml = os.path.join(data_dir.get_data_dir(),
                                                "iface_attach.xml")
                shutil.copyfile(
                    libvirt.modify_vm_iface(vm_name, 'get_xml', iface_dict),
                    iface_attach_xml)
                libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')

        try:
            # Start the VM.
            vm.start()
            if start_error:
                test.fail("VM started unexpectedly")
            if hotplug:
                virsh.attach_device(vm_name,
                                    iface_attach_xml,
                                    debug=True,
                                    ignore_status=False)
            iface_name = libvirt.get_ifname_host(vm_name, iface_mac)
            if test_ovs_port:
                check_ovs_port(iface_name, bridge_name)
            if live_add_qos:
                inbound_opt = ",".join(re.findall(r'[0-9]+', iface_inbound))
                outbount_opt = ",".join(re.findall(r'[0-9]+', iface_outbound))
                virsh.domiftune(vm_name,
                                iface_name,
                                inbound=inbound_opt,
                                outbound=outbount_opt,
                                debug=True,
                                ignore_status=False)
            if test_qos:
                iface_mac = vm_xml.VMXML.get_first_mac_by_name(vm_name)
                tap_name = libvirt.get_ifname_host(vm_name, iface_mac)
                logging.info("Test inbound:")
                res1 = utils_net.check_class_rules(
                    tap_name, "1:1", ast.literal_eval(iface_inbound))
                logging.info("Test outbound:")
                res2 = utils_net.check_filter_rules(
                    tap_name, ast.literal_eval(iface_outbound))
                if not res1 or not res2:
                    test.fail("Qos test fail!")

        except virt_vm.VMStartError as details:
            logging.info(str(details))
            if not start_error:
                test.fail('VM failed to start:\n%s' % details)

    finally:
        # Recover VM.
        if vm.is_alive():
            vm.destroy(gracefully=False)
        logging.info("Restoring network...")
        if net_name == "default":
            netxml_backup.sync()
        else:
            # Destroy and undefine new created network
            virsh.net_destroy(net_name)
            virsh.net_undefine(net_name)
        # Try to recovery ovs bridge
        if utils_net.ovs_br_exists(bridge_name):
            utils_net.del_ovs_bridge(bridge_name)
        if "iface_attach_xml" in locals():
            os.remove(iface_attach_xml)
        vmxml_backup.sync()
 def setup_default():
     """
     Default setup
     """
     test.log.info("TEST_SETUP: Clear up VM interface(s).")
     libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
Exemple #18
0
 def setup_hotplug_hostdev_device_with_teaming():
     libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')
     vm.start()
     vm.wait_for_serial_login(timeout=240).close()
Exemple #19
0
 def setup_default():
     """
     Default setup
     """
     logging.debug("Remove VM's interface devices.")
     libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')