xref: /qemu/tests/functional/test_ppc64_hv.py (revision b07a5bb736ca08d55cc3ada8ca309943b55d4b70)
1c9cb4967SNicholas Piggin# Tests that specifically try to exercise hypervisor features of the
2c9cb4967SNicholas Piggin# target machines. powernv supports the Power hypervisor ISA, and
3c9cb4967SNicholas Piggin# pseries supports the nested-HV hypervisor spec.
4c9cb4967SNicholas Piggin#
5c9cb4967SNicholas Piggin# Copyright (c) 2023 IBM Corporation
6c9cb4967SNicholas Piggin#
7c9cb4967SNicholas Piggin# This work is licensed under the terms of the GNU GPL, version 2 or
8c9cb4967SNicholas Piggin# later.  See the COPYING file in the top-level directory.
9c9cb4967SNicholas Piggin
10c9cb4967SNicholas Pigginfrom avocado import skipIf, skipUnless
11c9cb4967SNicholas Pigginfrom avocado.utils import archive
12c9cb4967SNicholas Pigginfrom avocado_qemu import QemuSystemTest
13c9cb4967SNicholas Pigginfrom avocado_qemu import wait_for_console_pattern, exec_command
14c9cb4967SNicholas Pigginimport os
15c9cb4967SNicholas Pigginimport time
16c9cb4967SNicholas Pigginimport subprocess
17*b07a5bb7SNicholas Pigginfrom datetime import datetime
18c9cb4967SNicholas Piggin
19c9cb4967SNicholas Piggindeps = ["xorriso"] # dependent tools needed in the test setup/box.
20c9cb4967SNicholas Piggin
21c9cb4967SNicholas Piggindef which(tool):
22c9cb4967SNicholas Piggin    """ looks up the full path for @tool, returns None if not found
23c9cb4967SNicholas Piggin        or if @tool does not have executable permissions.
24c9cb4967SNicholas Piggin    """
25c9cb4967SNicholas Piggin    paths=os.getenv('PATH')
26c9cb4967SNicholas Piggin    for p in paths.split(os.path.pathsep):
27c9cb4967SNicholas Piggin        p = os.path.join(p, tool)
28c9cb4967SNicholas Piggin        if os.path.exists(p) and os.access(p, os.X_OK):
29c9cb4967SNicholas Piggin            return p
30c9cb4967SNicholas Piggin    return None
31c9cb4967SNicholas Piggin
32c9cb4967SNicholas Piggindef missing_deps():
33c9cb4967SNicholas Piggin    """ returns True if any of the test dependent tools are absent.
34c9cb4967SNicholas Piggin    """
35c9cb4967SNicholas Piggin    for dep in deps:
36c9cb4967SNicholas Piggin        if which(dep) is None:
37c9cb4967SNicholas Piggin            return True
38c9cb4967SNicholas Piggin    return False
39c9cb4967SNicholas Piggin
40c9cb4967SNicholas Piggin# Alpine is a light weight distro that supports QEMU. These tests boot
41c9cb4967SNicholas Piggin# that on the machine then run a QEMU guest inside it in KVM mode,
42c9cb4967SNicholas Piggin# that runs the same Alpine distro image.
43c9cb4967SNicholas Piggin# QEMU packages are downloaded and installed on each test. That's not a
44c9cb4967SNicholas Piggin# large download, but it may be more polite to create qcow2 image with
45c9cb4967SNicholas Piggin# QEMU already installed and use that.
4674eb04afSNicholas Piggin# XXX: The order of these tests seems to matter, see git blame.
4774eb04afSNicholas Piggin@skipIf(missing_deps(), 'dependencies (%s) not installed' % ','.join(deps))
48c9cb4967SNicholas Piggin@skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test sometimes gets stuck due to console handling problem')
49c9cb4967SNicholas Piggin@skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited')
50c9cb4967SNicholas Piggin@skipUnless(os.getenv('SPEED') == 'slow', 'runtime limited')
51c9cb4967SNicholas Pigginclass HypervisorTest(QemuSystemTest):
52c9cb4967SNicholas Piggin
53c9cb4967SNicholas Piggin    timeout = 1000
54c9cb4967SNicholas Piggin    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 console=hvc0 '
55c9cb4967SNicholas Piggin    panic_message = 'Kernel panic - not syncing'
56c9cb4967SNicholas Piggin    good_message = 'VFS: Cannot open root device'
57c9cb4967SNicholas Piggin
58c9cb4967SNicholas Piggin    def extract_from_iso(self, iso, path):
59c9cb4967SNicholas Piggin        """
60c9cb4967SNicholas Piggin        Extracts a file from an iso file into the test workdir
61c9cb4967SNicholas Piggin
62c9cb4967SNicholas Piggin        :param iso: path to the iso file
63c9cb4967SNicholas Piggin        :param path: path within the iso file of the file to be extracted
64c9cb4967SNicholas Piggin        :returns: path of the extracted file
65c9cb4967SNicholas Piggin        """
66c9cb4967SNicholas Piggin        filename = os.path.basename(path)
67c9cb4967SNicholas Piggin
68c9cb4967SNicholas Piggin        cwd = os.getcwd()
69c9cb4967SNicholas Piggin        os.chdir(self.workdir)
70c9cb4967SNicholas Piggin
71c9cb4967SNicholas Piggin        with open(filename, "w") as outfile:
72c9cb4967SNicholas Piggin            cmd = "xorriso -osirrox on -indev %s -cpx %s %s" % (iso, path, filename)
73c9cb4967SNicholas Piggin            subprocess.run(cmd.split(),
74c9cb4967SNicholas Piggin                           stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
75c9cb4967SNicholas Piggin
76c9cb4967SNicholas Piggin        os.chdir(cwd)
77c9cb4967SNicholas Piggin
78c9cb4967SNicholas Piggin        # Return complete path to extracted file.  Because callers to
79c9cb4967SNicholas Piggin        # extract_from_iso() specify 'path' with a leading slash, it is
80c9cb4967SNicholas Piggin        # necessary to use os.path.relpath() as otherwise os.path.join()
81c9cb4967SNicholas Piggin        # interprets it as an absolute path and drops the self.workdir part.
82c9cb4967SNicholas Piggin        return os.path.normpath(os.path.join(self.workdir, filename))
83c9cb4967SNicholas Piggin
84c9cb4967SNicholas Piggin    def setUp(self):
85c9cb4967SNicholas Piggin        super().setUp()
86c9cb4967SNicholas Piggin
87c9cb4967SNicholas Piggin        iso_url = ('https://dl-cdn.alpinelinux.org/alpine/v3.18/releases/ppc64le/alpine-standard-3.18.4-ppc64le.iso')
88c9cb4967SNicholas Piggin
89c9cb4967SNicholas Piggin        # Alpine use sha256 so I recalculated this myself
90c9cb4967SNicholas Piggin        iso_sha256 = 'c26b8d3e17c2f3f0fed02b4b1296589c2390e6d5548610099af75300edd7b3ff'
91c9cb4967SNicholas Piggin        iso_path = self.fetch_asset(iso_url, asset_hash=iso_sha256,
92c9cb4967SNicholas Piggin                                    algorithm = "sha256")
93c9cb4967SNicholas Piggin
94c9cb4967SNicholas Piggin        self.iso_path = iso_path
95c9cb4967SNicholas Piggin        self.vmlinuz = self.extract_from_iso(iso_path, '/boot/vmlinuz-lts')
96c9cb4967SNicholas Piggin        self.initramfs = self.extract_from_iso(iso_path, '/boot/initramfs-lts')
97c9cb4967SNicholas Piggin
98c9cb4967SNicholas Piggin    def do_start_alpine(self):
99c9cb4967SNicholas Piggin        self.vm.set_console()
100c9cb4967SNicholas Piggin        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE
101c9cb4967SNicholas Piggin        self.vm.add_args("-kernel", self.vmlinuz)
102c9cb4967SNicholas Piggin        self.vm.add_args("-initrd", self.initramfs)
103c9cb4967SNicholas Piggin        self.vm.add_args("-smp", "4", "-m", "2g")
104c9cb4967SNicholas Piggin        self.vm.add_args("-drive", f"file={self.iso_path},format=raw,if=none,id=drive0")
105c9cb4967SNicholas Piggin
106c9cb4967SNicholas Piggin        self.vm.launch()
107c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'Welcome to Alpine Linux 3.18')
108c9cb4967SNicholas Piggin        exec_command(self, 'root')
109c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'localhost login:')
110c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'You may change this message by editing /etc/motd.')
111*b07a5bb7SNicholas Piggin        # If the time is wrong, SSL certificates can fail.
112*b07a5bb7SNicholas Piggin        exec_command(self, 'date -s "' + datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S' + '"'))
113c9cb4967SNicholas Piggin        exec_command(self, 'setup-alpine -qe')
114c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'Updating repository indexes... done.')
115c9cb4967SNicholas Piggin
116c9cb4967SNicholas Piggin    def do_stop_alpine(self):
117c9cb4967SNicholas Piggin        exec_command(self, 'poweroff')
118c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'alpine:~#')
119c9cb4967SNicholas Piggin        self.vm.wait()
120c9cb4967SNicholas Piggin
121c9cb4967SNicholas Piggin    def do_setup_kvm(self):
122c9cb4967SNicholas Piggin        exec_command(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/main > /etc/apk/repositories')
123c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'alpine:~#')
124c9cb4967SNicholas Piggin        exec_command(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/community >> /etc/apk/repositories')
125c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'alpine:~#')
126c9cb4967SNicholas Piggin        exec_command(self, 'apk update')
127c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'alpine:~#')
128c9cb4967SNicholas Piggin        exec_command(self, 'apk add qemu-system-ppc64')
129c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'alpine:~#')
130c9cb4967SNicholas Piggin        exec_command(self, 'modprobe kvm-hv')
131c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'alpine:~#')
132c9cb4967SNicholas Piggin
133c9cb4967SNicholas Piggin    # This uses the host's block device as the source file for guest block
134c9cb4967SNicholas Piggin    # device for install media. This is a bit hacky but allows reuse of the
135c9cb4967SNicholas Piggin    # iso without having a passthrough filesystem configured.
136c9cb4967SNicholas Piggin    def do_test_kvm(self, hpt=False):
137c9cb4967SNicholas Piggin        if hpt:
138c9cb4967SNicholas Piggin            append = 'disable_radix'
139c9cb4967SNicholas Piggin        else:
140c9cb4967SNicholas Piggin            append = ''
141c9cb4967SNicholas Piggin        exec_command(self, 'qemu-system-ppc64 -nographic -smp 2 -m 1g '
142c9cb4967SNicholas Piggin                           '-machine pseries,x-vof=on,accel=kvm '
143c9cb4967SNicholas Piggin                           '-machine cap-cfpc=broken,cap-sbbc=broken,'
144c9cb4967SNicholas Piggin                                    'cap-ibs=broken,cap-ccf-assist=off '
145c9cb4967SNicholas Piggin                           '-drive file=/dev/nvme0n1,format=raw,readonly=on '
146c9cb4967SNicholas Piggin                           '-initrd /media/nvme0n1/boot/initramfs-lts '
147c9cb4967SNicholas Piggin                           '-kernel /media/nvme0n1/boot/vmlinuz-lts '
148c9cb4967SNicholas Piggin                           '-append \'usbcore.nousb ' + append + '\'')
149c9cb4967SNicholas Piggin        # Alpine 3.18 kernel seems to crash in XHCI USB driver.
150c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'Welcome to Alpine Linux 3.18')
151c9cb4967SNicholas Piggin        exec_command(self, 'root')
152c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'localhost login:')
153c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'You may change this message by editing /etc/motd.')
154c9cb4967SNicholas Piggin        exec_command(self, 'poweroff >& /dev/null')
155c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'localhost:~#')
156c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'reboot: Power down')
157c9cb4967SNicholas Piggin        time.sleep(1)
158c9cb4967SNicholas Piggin        exec_command(self, '')
159c9cb4967SNicholas Piggin        wait_for_console_pattern(self, 'alpine:~#')
160c9cb4967SNicholas Piggin
161c9cb4967SNicholas Piggin    def test_hv_pseries(self):
162c9cb4967SNicholas Piggin        """
163c9cb4967SNicholas Piggin        :avocado: tags=arch:ppc64
164c9cb4967SNicholas Piggin        :avocado: tags=machine:pseries
165c9cb4967SNicholas Piggin        :avocado: tags=accel:tcg
166c9cb4967SNicholas Piggin        """
167c9cb4967SNicholas Piggin        self.require_accelerator("tcg")
168c9cb4967SNicholas Piggin        self.vm.add_args("-accel", "tcg,thread=multi")
169c9cb4967SNicholas Piggin        self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0')
170c9cb4967SNicholas Piggin        self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on")
171c9cb4967SNicholas Piggin        self.do_start_alpine()
172c9cb4967SNicholas Piggin        self.do_setup_kvm()
173c9cb4967SNicholas Piggin        self.do_test_kvm()
174c9cb4967SNicholas Piggin        self.do_stop_alpine()
175c9cb4967SNicholas Piggin
176c9cb4967SNicholas Piggin    def test_hv_pseries_kvm(self):
177c9cb4967SNicholas Piggin        """
178c9cb4967SNicholas Piggin        :avocado: tags=arch:ppc64
179c9cb4967SNicholas Piggin        :avocado: tags=machine:pseries
180c9cb4967SNicholas Piggin        :avocado: tags=accel:kvm
181c9cb4967SNicholas Piggin        """
182c9cb4967SNicholas Piggin        self.require_accelerator("kvm")
183c9cb4967SNicholas Piggin        self.vm.add_args("-accel", "kvm")
184c9cb4967SNicholas Piggin        self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0')
185c9cb4967SNicholas Piggin        self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on,cap-ccf-assist=off")
186c9cb4967SNicholas Piggin        self.do_start_alpine()
187c9cb4967SNicholas Piggin        self.do_setup_kvm()
188c9cb4967SNicholas Piggin        self.do_test_kvm()
189c9cb4967SNicholas Piggin        self.do_stop_alpine()
190c9cb4967SNicholas Piggin
191c9cb4967SNicholas Piggin    def test_hv_powernv(self):
192c9cb4967SNicholas Piggin        """
193c9cb4967SNicholas Piggin        :avocado: tags=arch:ppc64
194c9cb4967SNicholas Piggin        :avocado: tags=machine:powernv
195c9cb4967SNicholas Piggin        :avocado: tags=accel:tcg
196c9cb4967SNicholas Piggin        """
197c9cb4967SNicholas Piggin        self.require_accelerator("tcg")
198c9cb4967SNicholas Piggin        self.vm.add_args("-accel", "tcg,thread=multi")
199c9cb4967SNicholas Piggin        self.vm.add_args('-device', 'nvme,bus=pcie.2,addr=0x0,serial=1234,drive=drive0',
200c9cb4967SNicholas Piggin                         '-device', 'e1000e,netdev=net0,mac=C0:FF:EE:00:00:02,bus=pcie.0,addr=0x0',
201c9cb4967SNicholas Piggin                         '-netdev', 'user,id=net0,hostfwd=::20022-:22,hostname=alpine')
202c9cb4967SNicholas Piggin        self.do_start_alpine()
203c9cb4967SNicholas Piggin        self.do_setup_kvm()
204c9cb4967SNicholas Piggin        self.do_test_kvm()
205c9cb4967SNicholas Piggin        self.do_test_kvm(True)
206c9cb4967SNicholas Piggin        self.do_stop_alpine()
207