xref: /qemu/tests/avocado/virtio-gpu.py (revision 73b49878)
1# virtio-gpu tests
2#
3# This work is licensed under the terms of the GNU GPL, version 2 or
4# later.  See the COPYING file in the top-level directory.
5
6
7from avocado_qemu import BUILD_DIR
8from avocado_qemu import QemuSystemTest
9from avocado_qemu import wait_for_console_pattern
10from avocado_qemu import exec_command_and_wait_for_pattern
11from avocado_qemu import is_readable_executable_file
12
13from qemu.utils import kvm_available
14
15import os
16import socket
17import subprocess
18
19
20def pick_default_vug_bin():
21    relative_path = "./contrib/vhost-user-gpu/vhost-user-gpu"
22    if is_readable_executable_file(relative_path):
23        return relative_path
24
25    bld_dir_path = os.path.join(BUILD_DIR, relative_path)
26    if is_readable_executable_file(bld_dir_path):
27        return bld_dir_path
28
29
30class VirtioGPUx86(QemuSystemTest):
31    """
32    :avocado: tags=virtio-gpu
33    :avocado: tags=arch:x86_64
34    :avocado: tags=cpu:host
35    """
36
37    KERNEL_COMMAND_LINE = "printk.time=0 console=ttyS0 rdinit=/bin/bash"
38    KERNEL_URL = (
39        "https://archives.fedoraproject.org/pub/archive/fedora"
40        "/linux/releases/33/Everything/x86_64/os/images"
41        "/pxeboot/vmlinuz"
42    )
43    KERNEL_HASH = '1433cfe3f2ffaa44de4ecfb57ec25dc2399cdecf'
44    INITRD_URL = (
45        "https://archives.fedoraproject.org/pub/archive/fedora"
46        "/linux/releases/33/Everything/x86_64/os/images"
47        "/pxeboot/initrd.img"
48    )
49    INITRD_HASH = 'c828d68a027b53e5220536585efe03412332c2d9'
50
51    def wait_for_console_pattern(self, success_message, vm=None):
52        wait_for_console_pattern(
53            self,
54            success_message,
55            failure_message="Kernel panic - not syncing",
56            vm=vm,
57        )
58
59    def test_virtio_vga_virgl(self):
60        """
61        :avocado: tags=device:virtio-vga-gl
62        """
63        # FIXME: should check presence of virtio, virgl etc
64        self.require_accelerator('kvm')
65
66        kernel_path = self.fetch_asset(self.KERNEL_URL, self.KERNEL_HASH)
67        initrd_path = self.fetch_asset(self.INITRD_URL, self.INITRD_HASH)
68
69        self.vm.set_console()
70        self.vm.add_args("-m", "2G")
71        self.vm.add_args("-machine", "pc,accel=kvm")
72        self.vm.add_args("-device", "virtio-vga-gl")
73        self.vm.add_args("-display", "egl-headless")
74        self.vm.add_args(
75            "-kernel",
76            kernel_path,
77            "-initrd",
78            initrd_path,
79            "-append",
80            self.KERNEL_COMMAND_LINE,
81        )
82        try:
83            self.vm.launch()
84        except:
85            # TODO: probably fails because we are missing the VirGL features
86            self.cancel("VirGL not enabled?")
87
88        self.wait_for_console_pattern("as init process")
89        exec_command_and_wait_for_pattern(
90            self, "/usr/sbin/modprobe virtio_gpu", ""
91        )
92        self.wait_for_console_pattern("features: +virgl +edid")
93
94    def test_vhost_user_vga_virgl(self):
95        """
96        :avocado: tags=device:vhost-user-vga
97        """
98        # FIXME: should check presence of vhost-user-gpu, virgl, memfd etc
99        self.require_accelerator('kvm')
100
101        vug = pick_default_vug_bin()
102        if not vug:
103            self.cancel("Could not find vhost-user-gpu")
104
105        kernel_path = self.fetch_asset(self.KERNEL_URL, self.KERNEL_HASH)
106        initrd_path = self.fetch_asset(self.INITRD_URL, self.INITRD_HASH)
107
108        # Create socketpair to connect proxy and remote processes
109        qemu_sock, vug_sock = socket.socketpair(
110            socket.AF_UNIX, socket.SOCK_STREAM
111        )
112        os.set_inheritable(qemu_sock.fileno(), True)
113        os.set_inheritable(vug_sock.fileno(), True)
114
115        self._vug_log_path = os.path.join(
116            self.logdir, "vhost-user-gpu.log"
117        )
118        self._vug_log_file = open(self._vug_log_path, "wb")
119        self.log.info('Complete vhost-user-gpu.log file can be '
120                      'found at %s', self._vug_log_path)
121
122        vugp = subprocess.Popen(
123            [vug, "--virgl", "--fd=%d" % vug_sock.fileno()],
124            stdin=subprocess.DEVNULL,
125            stdout=self._vug_log_file,
126            stderr=subprocess.STDOUT,
127            shell=False,
128            close_fds=False,
129        )
130
131        self.vm.set_console()
132        self.vm.add_args("-m", "2G")
133        self.vm.add_args("-object", "memory-backend-memfd,id=mem,size=2G")
134        self.vm.add_args("-machine", "pc,memory-backend=mem,accel=kvm")
135        self.vm.add_args("-chardev", "socket,id=vug,fd=%d" % qemu_sock.fileno())
136        self.vm.add_args("-device", "vhost-user-vga,chardev=vug")
137        self.vm.add_args("-display", "egl-headless")
138        self.vm.add_args(
139            "-kernel",
140            kernel_path,
141            "-initrd",
142            initrd_path,
143            "-append",
144            self.KERNEL_COMMAND_LINE,
145        )
146        try:
147            self.vm.launch()
148        except:
149            # TODO: probably fails because we are missing the VirGL features
150            self.cancel("VirGL not enabled?")
151        self.wait_for_console_pattern("as init process")
152        exec_command_and_wait_for_pattern(self, "/usr/sbin/modprobe virtio_gpu",
153                                          "features: +virgl +edid")
154        self.vm.shutdown()
155        qemu_sock.close()
156        vugp.terminate()
157        vugp.wait()
158