1# KVM Xen guest functional tests 2# 3# Copyright © 2021 Red Hat, Inc. 4# Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. 5# 6# Author: 7# David Woodhouse <dwmw2@infradead.org> 8# Alex Bennée <alex.bennee@linaro.org> 9# 10# SPDX-License-Identifier: GPL-2.0-or-later 11 12import os 13 14from qemu.machine import machine 15 16from avocado_qemu import LinuxSSHMixIn 17from avocado_qemu import QemuSystemTest 18from avocado_qemu import wait_for_console_pattern 19 20class KVMXenGuest(QemuSystemTest, LinuxSSHMixIn): 21 """ 22 :avocado: tags=arch:x86_64 23 :avocado: tags=machine:q35 24 :avocado: tags=accel:kvm 25 :avocado: tags=kvm_xen_guest 26 """ 27 28 KERNEL_DEFAULT = 'printk.time=0 root=/dev/xvda console=ttyS0' 29 30 kernel_path = None 31 kernel_params = None 32 33 # Fetch assets from the kvm-xen-guest subdir of my shared test 34 # images directory on fileserver.linaro.org where you can find 35 # build instructions for how they where assembled. 36 def get_asset(self, name, sha1): 37 base_url = ('https://fileserver.linaro.org/s/' 38 'kE4nCFLdQcoBF9t/download?' 39 'path=%2Fkvm-xen-guest&files=' ) 40 url = base_url + name 41 # use explicit name rather than failing to neatly parse the 42 # URL into a unique one 43 return self.fetch_asset(name=name, locations=(url), asset_hash=sha1) 44 45 def common_vm_setup(self): 46 # We also catch lack of KVM_XEN support if we fail to launch 47 self.require_accelerator("kvm") 48 49 self.vm.set_console() 50 51 self.vm.add_args("-accel", "kvm,xen-version=0x4000a,kernel-irqchip=split") 52 self.vm.add_args("-smp", "2") 53 54 self.kernel_path = self.get_asset("bzImage", 55 "367962983d0d32109998a70b45dcee4672d0b045") 56 self.rootfs = self.get_asset("rootfs.ext4", 57 "f1478401ea4b3fa2ea196396be44315bab2bb5e4") 58 59 def run_and_check(self): 60 self.vm.add_args('-kernel', self.kernel_path, 61 '-append', self.kernel_params, 62 '-drive', f"file={self.rootfs},if=none,format=raw,id=drv0", 63 '-device', 'xen-disk,drive=drv0,vdev=xvda', 64 '-device', 'virtio-net-pci,netdev=unet', 65 '-netdev', 'user,id=unet,hostfwd=:127.0.0.1:0-:22') 66 67 try: 68 self.vm.launch() 69 except machine.VMLaunchFailure as e: 70 if "Xen HVM guest support not present" in e.output: 71 self.cancel("KVM Xen support is not present " 72 "(need v5.12+ kernel with CONFIG_KVM_XEN)") 73 elif "Property 'kvm-accel.xen-version' not found" in e.output: 74 self.cancel("QEMU not built with CONFIG_XEN_EMU support") 75 else: 76 raise e 77 78 self.log.info('VM launched, waiting for sshd') 79 console_pattern = 'Starting dropbear sshd: OK' 80 wait_for_console_pattern(self, console_pattern, 'Oops') 81 self.log.info('sshd ready') 82 self.ssh_connect('root', '', False) 83 84 self.ssh_command('cat /proc/cmdline') 85 self.ssh_command('dmesg | grep -e "Grant table initialized"') 86 87 def test_kvm_xen_guest(self): 88 """ 89 :avocado: tags=kvm_xen_guest 90 """ 91 92 self.common_vm_setup() 93 94 self.kernel_params = (self.KERNEL_DEFAULT + 95 ' xen_emul_unplug=ide-disks') 96 self.run_and_check() 97 self.ssh_command('grep xen-pirq.*msi /proc/interrupts') 98 99 def test_kvm_xen_guest_nomsi(self): 100 """ 101 :avocado: tags=kvm_xen_guest_nomsi 102 """ 103 104 self.common_vm_setup() 105 106 self.kernel_params = (self.KERNEL_DEFAULT + 107 ' xen_emul_unplug=ide-disks pci=nomsi') 108 self.run_and_check() 109 self.ssh_command('grep xen-pirq.* /proc/interrupts') 110 111 def test_kvm_xen_guest_noapic_nomsi(self): 112 """ 113 :avocado: tags=kvm_xen_guest_noapic_nomsi 114 """ 115 116 self.common_vm_setup() 117 118 self.kernel_params = (self.KERNEL_DEFAULT + 119 ' xen_emul_unplug=ide-disks noapic pci=nomsi') 120 self.run_and_check() 121 self.ssh_command('grep xen-pirq /proc/interrupts') 122 123 def test_kvm_xen_guest_vapic(self): 124 """ 125 :avocado: tags=kvm_xen_guest_vapic 126 """ 127 128 self.common_vm_setup() 129 self.vm.add_args('-cpu', 'host,+xen-vapic') 130 self.kernel_params = (self.KERNEL_DEFAULT + 131 ' xen_emul_unplug=ide-disks') 132 self.run_and_check() 133 self.ssh_command('grep xen-pirq /proc/interrupts') 134 self.ssh_command('grep PCI-MSI /proc/interrupts') 135 136 def test_kvm_xen_guest_novector(self): 137 """ 138 :avocado: tags=kvm_xen_guest_novector 139 """ 140 141 self.common_vm_setup() 142 self.kernel_params = (self.KERNEL_DEFAULT + 143 ' xen_emul_unplug=ide-disks' + 144 ' xen_no_vector_callback') 145 self.run_and_check() 146 self.ssh_command('grep xen-platform-pci /proc/interrupts') 147 148 def test_kvm_xen_guest_novector_nomsi(self): 149 """ 150 :avocado: tags=kvm_xen_guest_novector_nomsi 151 """ 152 153 self.common_vm_setup() 154 155 self.kernel_params = (self.KERNEL_DEFAULT + 156 ' xen_emul_unplug=ide-disks pci=nomsi' + 157 ' xen_no_vector_callback') 158 self.run_and_check() 159 self.ssh_command('grep xen-platform-pci /proc/interrupts') 160 161 def test_kvm_xen_guest_novector_noapic(self): 162 """ 163 :avocado: tags=kvm_xen_guest_novector_noapic 164 """ 165 166 self.common_vm_setup() 167 self.kernel_params = (self.KERNEL_DEFAULT + 168 ' xen_emul_unplug=ide-disks' + 169 ' xen_no_vector_callback noapic') 170 self.run_and_check() 171 self.ssh_command('grep xen-platform-pci /proc/interrupts') 172