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", "features: +virgl +edid"
84        )
85
86    def test_vhost_user_vga_virgl(self):
87        # FIXME: should check presence of vhost-user-gpu, virgl, memfd etc
88        self.require_accelerator('kvm')
89
90        vug = pick_default_vug_bin()
91        if not vug:
92            self.skipTest("Could not find vhost-user-gpu")
93
94        kernel_path = self.ASSET_KERNEL.fetch()
95        initrd_path = self.ASSET_INITRD.fetch()
96
97        # Create socketpair to connect proxy and remote processes
98        qemu_sock, vug_sock = socket.socketpair(
99            socket.AF_UNIX, socket.SOCK_STREAM
100        )
101        os.set_inheritable(qemu_sock.fileno(), True)
102        os.set_inheritable(vug_sock.fileno(), True)
103
104        self._vug_log_path = os.path.join(
105            self.logdir, "vhost-user-gpu.log"
106        )
107        self._vug_log_file = open(self._vug_log_path, "wb")
108        self.log.info('Complete vhost-user-gpu.log file can be '
109                      'found at %s', self._vug_log_path)
110
111        vugp = subprocess.Popen(
112            [vug, "--virgl", "--fd=%d" % vug_sock.fileno()],
113            stdin=subprocess.DEVNULL,
114            stdout=self._vug_log_file,
115            stderr=subprocess.STDOUT,
116            shell=False,
117            close_fds=False,
118        )
119
120        self.vm.set_console()
121        self.vm.add_args("-cpu", "host")
122        self.vm.add_args("-m", "2G")
123        self.vm.add_args("-object", "memory-backend-memfd,id=mem,size=2G")
124        self.vm.add_args("-machine", "pc,memory-backend=mem,accel=kvm")
125        self.vm.add_args("-chardev", "socket,id=vug,fd=%d" % qemu_sock.fileno())
126        self.vm.add_args("-device", "vhost-user-vga,chardev=vug")
127        self.vm.add_args("-display", "egl-headless")
128        self.vm.add_args(
129            "-kernel",
130            kernel_path,
131            "-initrd",
132            initrd_path,
133            "-append",
134            self.KERNEL_COMMAND_LINE,
135        )
136        try:
137            self.vm.launch()
138        except:
139            # TODO: probably fails because we are missing the VirGL features
140            self.skipTest("VirGL not enabled?")
141        self.wait_for_console_pattern("as init process")
142        exec_command_and_wait_for_pattern(self, "/usr/sbin/modprobe virtio_gpu",
143                                          "features: +virgl +edid")
144        self.vm.shutdown()
145        qemu_sock.close()
146        vugp.terminate()
147        vugp.wait()
148
149if __name__ == '__main__':
150    QemuSystemTest.main()
151