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