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