| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Functional test that boots an s390x Linux guest with ccw and PCI devices |
| 4 | # attached and checks whether the devices are recognized by Linux |
| 5 | # |
| 6 | # Copyright (c) 2020 Red Hat, Inc. |
| 7 | # |
| 8 | # Author: |
| 9 | # Cornelia Huck <cohuck@redhat.com> |
| 10 | # |
| 11 | # This work is licensed under the terms of the GNU GPL, version 2 or |
| 12 | # later. See the COPYING file in the top-level directory. |
| 13 | |
| 14 | import os |
| 15 | import tempfile |
| 16 | |
| 17 | from qemu_test import QemuSystemTest, Asset |
| 18 | from qemu_test import exec_command |
| 19 | from qemu_test import exec_command_and_wait_for_pattern |
| 20 | from qemu_test import wait_for_console_pattern |
| 21 | |
| 22 | |
| 23 | class S390CCWVirtioMachine(QemuSystemTest): |
| 24 | KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 ' |
| 25 | |
| 26 | timeout = 120 |
| 27 | |
| 28 | ASSET_BUSTER_KERNEL = Asset( |
| 29 | ('https://snapshot.debian.org/archive/debian/' |
| 30 | '20201126T092837Z/dists/buster/main/installer-s390x/' |
| 31 | '20190702+deb10u6/images/generic/kernel.debian'), |
| 32 | 'd411d17c39ae7ad38d27534376cbe88b68b403c325739364122c2e6f1537e818') |
| 33 | ASSET_BUSTER_INITRD = Asset( |
| 34 | ('https://snapshot.debian.org/archive/debian/' |
| 35 | '20201126T092837Z/dists/buster/main/installer-s390x/' |
| 36 | '20190702+deb10u6/images/generic/initrd.debian'), |
| 37 | '836bbd0fe6a5ca81274c28c2b063ea315ce1868660866e9b60180c575fef9fd5') |
| 38 | |
| 39 | ASSET_F31_KERNEL = Asset( |
| 40 | ('https://archives.fedoraproject.org/pub/archive' |
| 41 | '/fedora-secondary/releases/31/Server/s390x/os' |
| 42 | '/images/kernel.img'), |
| 43 | '480859574f3f44caa6cd35c62d70e1ac0609134e22ce2a954bbed9b110c06e0b') |
| 44 | ASSET_F31_INITRD = Asset( |
| 45 | ('https://archives.fedoraproject.org/pub/archive' |
| 46 | '/fedora-secondary/releases/31/Server/s390x/os' |
| 47 | '/images/initrd.img'), |
| 48 | '04c46095b2c49020b1c2327158898b7db747e4892ae319726192fb949716aa9c') |
| 49 | |
| 50 | def wait_for_console_pattern(self, success_message, vm=None): |
| 51 | wait_for_console_pattern(self, success_message, |
| 52 | failure_message='Kernel panic - not syncing', |
| 53 | vm=vm) |
| 54 | |
| 55 | def wait_for_crw_reports(self): |
| 56 | exec_command_and_wait_for_pattern(self, |
| 57 | 'while ! (dmesg -c | grep CRW) ; do sleep 1 ; done', |
| 58 | 'CRW reports') |
| 59 | |
| 60 | dmesg_clear_count = 1 |
| 61 | def clear_guest_dmesg(self): |
| 62 | exec_command_and_wait_for_pattern(self, 'dmesg -c > /dev/null; ' |
| 63 | r'echo dm_clear\ ' + str(self.dmesg_clear_count), |
| 64 | r'dm_clear ' + str(self.dmesg_clear_count)) |
| 65 | self.dmesg_clear_count += 1 |
| 66 | |
| 67 | def test_s390x_devices(self): |
| 68 | self.set_machine('s390-ccw-virtio') |
| 69 | |
| 70 | kernel_path = self.ASSET_BUSTER_KERNEL.fetch() |
| 71 | initrd_path = self.ASSET_BUSTER_INITRD.fetch() |
| 72 | |
| 73 | self.vm.set_console() |
| 74 | kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + |
| 75 | 'console=sclp0 root=/dev/ram0 BOOT_DEBUG=3') |
| 76 | self.vm.add_args('-nographic', |
| 77 | '-kernel', kernel_path, |
| 78 | '-initrd', initrd_path, |
| 79 | '-append', kernel_command_line, |
| 80 | '-cpu', 'max,prno-trng=off', |
| 81 | '-device', 'virtio-net-ccw,devno=fe.1.1111', |
| 82 | '-device', |
| 83 | 'virtio-rng-ccw,devno=fe.2.0000,max_revision=0,id=rn1', |
| 84 | '-device', |
| 85 | 'virtio-rng-ccw,devno=fe.3.1234,max_revision=2,id=rn2', |
| 86 | '-device', 'zpci,uid=5,target=zzz', |
| 87 | '-device', 'virtio-net-pci,id=zzz', |
| 88 | '-device', 'zpci,uid=0xa,fid=12,target=serial', |
| 89 | '-device', 'virtio-serial-pci,id=serial', |
| 90 | '-device', 'virtio-balloon-ccw') |
| 91 | self.vm.launch() |
| 92 | |
| 93 | shell_ready = "sh: can't access tty; job control turned off" |
| 94 | self.wait_for_console_pattern(shell_ready) |
| 95 | # first debug shell is too early, we need to wait for device detection |
| 96 | exec_command_and_wait_for_pattern(self, 'exit', shell_ready) |
| 97 | |
| 98 | ccw_bus_ids="0.1.1111 0.2.0000 0.3.1234" |
| 99 | pci_bus_ids="0005:00:00.0 000a:00:00.0" |
| 100 | exec_command_and_wait_for_pattern(self, 'ls /sys/bus/ccw/devices/', |
| 101 | ccw_bus_ids) |
| 102 | exec_command_and_wait_for_pattern(self, 'ls /sys/bus/pci/devices/', |
| 103 | pci_bus_ids) |
| 104 | # check that the device at 0.2.0000 is in legacy mode, while the |
| 105 | # device at 0.3.1234 has the virtio-1 feature bit set |
| 106 | virtio_rng_features="00000000000000000000000000001100" + \ |
| 107 | "10000000000000000000000000000000" |
| 108 | virtio_rng_features_legacy="00000000000000000000000000001100" + \ |
| 109 | "00000000000000000000000000000000" |
| 110 | exec_command_and_wait_for_pattern(self, |
| 111 | 'cat /sys/bus/ccw/devices/0.2.0000/virtio?/features', |
| 112 | virtio_rng_features_legacy) |
| 113 | exec_command_and_wait_for_pattern(self, |
| 114 | 'cat /sys/bus/ccw/devices/0.3.1234/virtio?/features', |
| 115 | virtio_rng_features) |
| 116 | # check that /dev/hwrng works - and that it's gone after ejecting |
| 117 | exec_command_and_wait_for_pattern(self, |
| 118 | 'dd if=/dev/hwrng of=/dev/null bs=1k count=10', |
| 119 | '10+0 records out') |
| 120 | self.clear_guest_dmesg() |
| 121 | self.vm.cmd('device_del', id='rn1') |
| 122 | self.wait_for_crw_reports() |
| 123 | self.clear_guest_dmesg() |
| 124 | self.vm.cmd('device_del', id='rn2') |
| 125 | self.wait_for_crw_reports() |
| 126 | exec_command_and_wait_for_pattern(self, |
| 127 | 'dd if=/dev/hwrng of=/dev/null bs=1k count=10', |
| 128 | 'dd: /dev/hwrng: No such device') |
| 129 | # verify that we indeed have virtio-net devices (without having the |
| 130 | # virtio-net driver handy) |
| 131 | exec_command_and_wait_for_pattern(self, |
| 132 | 'cat /sys/bus/ccw/devices/0.1.1111/cutype', |
| 133 | '3832/01') |
| 134 | exec_command_and_wait_for_pattern(self, |
| 135 | r'cat /sys/bus/pci/devices/0005\:00\:00.0/subsystem_vendor', |
| 136 | r'0x1af4') |
| 137 | exec_command_and_wait_for_pattern(self, |
| 138 | r'cat /sys/bus/pci/devices/0005\:00\:00.0/subsystem_device', |
| 139 | r'0x1100') |
| 140 | # check fid propagation |
| 141 | exec_command_and_wait_for_pattern(self, |
| 142 | r'cat /sys/bus/pci/devices/000a\:00\:00.0/function_id', |
| 143 | r'0x0000000c') |
| 144 | # add another device |
| 145 | self.clear_guest_dmesg() |
| 146 | self.vm.cmd('device_add', driver='virtio-net-ccw', |
| 147 | devno='fe.0.4711', id='net_4711') |
| 148 | self.wait_for_crw_reports() |
| 149 | exec_command_and_wait_for_pattern(self, 'for i in 1 2 3 4 5 6 7 ; do ' |
| 150 | 'if [ -e /sys/bus/ccw/devices/*4711 ]; then break; fi ;' |
| 151 | 'sleep 1 ; done ; ls /sys/bus/ccw/devices/', |
| 152 | '0.0.4711') |
| 153 | # and detach it again |
| 154 | self.clear_guest_dmesg() |
| 155 | self.vm.cmd('device_del', id='net_4711') |
| 156 | self.vm.event_wait(name='DEVICE_DELETED', |
| 157 | match={'data': {'device': 'net_4711'}}) |
| 158 | self.wait_for_crw_reports() |
| 159 | exec_command_and_wait_for_pattern(self, |
| 160 | 'ls /sys/bus/ccw/devices/0.0.4711', |
| 161 | 'No such file or directory') |
| 162 | # test the virtio-balloon device |
| 163 | exec_command_and_wait_for_pattern(self, 'head -n 1 /proc/meminfo', |
| 164 | 'MemTotal: 115640 kB') |
| 165 | self.vm.cmd('human-monitor-command', command_line='balloon 96') |
| 166 | exec_command_and_wait_for_pattern(self, 'head -n 1 /proc/meminfo', |
| 167 | 'MemTotal: 82872 kB') |
| 168 | self.vm.cmd('human-monitor-command', command_line='balloon 128') |
| 169 | exec_command_and_wait_for_pattern(self, 'head -n 1 /proc/meminfo', |
| 170 | 'MemTotal: 115640 kB') |
| 171 | |
| 172 | |
| 173 | def test_s390x_fedora(self): |
| 174 | self.set_machine('s390-ccw-virtio') |
| 175 | |
| 176 | kernel_path = self.ASSET_F31_KERNEL.fetch() |
| 177 | |
| 178 | initrd_path = self.uncompress(self.ASSET_F31_INITRD, format="xz") |
| 179 | |
| 180 | self.vm.set_console() |
| 181 | kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + ' audit=0 ' |
| 182 | 'rd.plymouth=0 plymouth.enable=0 rd.rescue') |
| 183 | self.vm.add_args('-nographic', |
| 184 | '-smp', '4', |
| 185 | '-m', '512', |
| 186 | '-name', 'Some Guest Name', |
| 187 | '-uuid', '30de4fd9-b4d5-409e-86a5-09b387f70bfa', |
| 188 | '-kernel', kernel_path, |
| 189 | '-initrd', initrd_path, |
| 190 | '-append', kernel_command_line, |
| 191 | '-device', 'zpci,uid=7,target=n', |
| 192 | '-device', 'virtio-net-pci,id=n,mac=02:ca:fe:fa:ce:12', |
| 193 | '-device', 'virtio-rng-ccw,devno=fe.1.9876', |
| 194 | '-device', 'virtio-gpu-ccw,devno=fe.2.5432') |
| 195 | self.vm.launch() |
| 196 | self.wait_for_console_pattern('Kernel command line: %s' |
| 197 | % kernel_command_line) |
| 198 | self.wait_for_console_pattern('Entering emergency mode') |
| 199 | |
| 200 | # Some tests to see whether the CLI options have been considered: |
| 201 | self.log.info("Test whether QEMU CLI options have been considered") |
| 202 | exec_command_and_wait_for_pattern(self, |
| 203 | 'while ! (dmesg | grep enP7p0s0) ; do sleep 1 ; done', |
| 204 | 'virtio_net virtio0 enP7p0s0: renamed') |
| 205 | exec_command_and_wait_for_pattern(self, 'lspci', |
| 206 | '0007:00:00.0 Class 0200: Device 1af4:1041') |
| 207 | exec_command_and_wait_for_pattern(self, |
| 208 | 'cat /sys/class/net/enP7p0s0/address', |
| 209 | '02:ca:fe:fa:ce:12') |
| 210 | exec_command_and_wait_for_pattern(self, 'lscss', '0.1.9876') |
| 211 | exec_command_and_wait_for_pattern(self, 'lscss', '0.2.5432') |
| 212 | exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', |
| 213 | 'processors : 4') |
| 214 | exec_command_and_wait_for_pattern(self, 'grep MemTotal /proc/meminfo', |
| 215 | 'MemTotal: 499848 kB') |
| 216 | exec_command_and_wait_for_pattern(self, 'grep Name /proc/sysinfo', |
| 217 | 'Extended Name: Some Guest Name') |
| 218 | exec_command_and_wait_for_pattern(self, 'grep UUID /proc/sysinfo', |
| 219 | '30de4fd9-b4d5-409e-86a5-09b387f70bfa') |
| 220 | |
| 221 | # Disable blinking cursor, then write some stuff into the framebuffer. |
| 222 | # QEMU's PPM screendumps contain uncompressed 24-bit values, while the |
| 223 | # framebuffer uses 32-bit, so we pad our text with some spaces when |
| 224 | # writing to the framebuffer. Since the PPM is uncompressed, we then |
| 225 | # can simply read the written "magic bytes" back from the PPM file to |
| 226 | # check whether the framebuffer is working as expected. |
| 227 | # Unfortunately, this test is flaky, so we don't run it by default |
| 228 | if os.getenv('QEMU_TEST_FLAKY_TESTS'): |
| 229 | self.log.info("Test screendump of virtio-gpu device") |
| 230 | exec_command_and_wait_for_pattern(self, |
| 231 | 'while ! (dmesg | grep gpudrmfb) ; do sleep 1 ; done', |
| 232 | 'virtio_gpudrmfb frame buffer device') |
| 233 | exec_command_and_wait_for_pattern(self, |
| 234 | r'echo -e "\e[?25l" > /dev/tty0', ':/#') |
| 235 | exec_command_and_wait_for_pattern(self, 'for ((i=0;i<250;i++)); do ' |
| 236 | 'echo " The qu ick fo x j ump s o ver a laz y d og" >> fox.txt;' |
| 237 | 'done', |
| 238 | ':/#') |
| 239 | exec_command_and_wait_for_pattern(self, |
| 240 | 'dd if=fox.txt of=/dev/fb0 bs=1000 oflag=sync,nocache ; rm fox.txt', |
| 241 | '12+0 records out') |
| 242 | with tempfile.NamedTemporaryFile(suffix='.ppm', |
| 243 | prefix='qemu-scrdump-') as ppmfile: |
| 244 | self.vm.cmd('screendump', filename=ppmfile.name) |
| 245 | ppmfile.seek(0) |
| 246 | line = ppmfile.readline() |
| 247 | self.assertEqual(line, b"P6\n") |
| 248 | line = ppmfile.readline() |
| 249 | self.assertEqual(line, b"1280 800\n") |
| 250 | line = ppmfile.readline() |
| 251 | self.assertEqual(line, b"255\n") |
| 252 | line = ppmfile.readline(256) |
| 253 | self.assertEqual(line, b"The quick fox jumps over a lazy dog\n") |
| 254 | else: |
| 255 | self.log.info("Skipped flaky screendump of virtio-gpu device test") |
| 256 | |
| 257 | # Hot-plug a virtio-crypto device and see whether it gets accepted |
| 258 | self.log.info("Test hot-plug virtio-crypto device") |
| 259 | self.clear_guest_dmesg() |
| 260 | self.vm.cmd('object-add', qom_type='cryptodev-backend-builtin', |
| 261 | id='cbe0') |
| 262 | self.vm.cmd('device_add', driver='virtio-crypto-ccw', id='crypdev0', |
| 263 | cryptodev='cbe0', devno='fe.0.2342') |
| 264 | exec_command_and_wait_for_pattern(self, |
| 265 | 'while ! (dmesg -c | grep Accelerator.device) ; do' |
| 266 | ' sleep 1 ; done', 'Accelerator device is ready') |
| 267 | exec_command_and_wait_for_pattern(self, 'lscss', '0.0.2342') |
| 268 | self.vm.cmd('device_del', id='crypdev0') |
| 269 | self.vm.cmd('object-del', id='cbe0') |
| 270 | exec_command_and_wait_for_pattern(self, |
| 271 | 'while ! (dmesg -c | grep Start.virtcrypto_remove) ; do' |
| 272 | ' sleep 1 ; done', 'Start virtcrypto_remove.') |
| 273 | |
| 274 | # Test SCLP event Control-Program Identification (CPI) |
| 275 | cpi = '/sys/firmware/cpi/' |
| 276 | sclpcpi = '/machine/sclp/s390-sclp-event-facility/sclpcpi' |
| 277 | self.log.info("Test SCLP event CPI") |
| 278 | exec_command(self, 'echo TESTVM > ' + cpi + 'system_name') |
| 279 | exec_command(self, 'echo LINUX > ' + cpi + 'system_type') |
| 280 | exec_command(self, 'echo TESTPLEX > ' + cpi + 'sysplex_name') |
| 281 | exec_command(self, 'echo 0x001a000000060b00 > ' + cpi + 'system_level') |
| 282 | exec_command_and_wait_for_pattern(self, |
| 283 | 'echo 1 > ' + cpi + 'set', ':/#') |
| 284 | try: |
| 285 | event = self.vm.event_wait('SCLP_CPI_INFO_AVAILABLE') |
| 286 | except TimeoutError: |
| 287 | self.fail('Timed out waiting for the SCLP_CPI_INFO_AVAILABLE event') |
| 288 | ts = self.vm.cmd('qom-get', path=sclpcpi, property='timestamp') |
| 289 | self.assertNotEqual(ts, 0) |
| 290 | name = self.vm.cmd('qom-get', path=sclpcpi, property='system_name') |
| 291 | self.assertEqual(name.strip(), 'TESTVM') |
| 292 | typ = self.vm.cmd('qom-get', path=sclpcpi, property='system_type') |
| 293 | self.assertEqual(typ.strip(), 'LINUX') |
| 294 | sysplex = self.vm.cmd('qom-get', path=sclpcpi, property='sysplex_name') |
| 295 | self.assertEqual(sysplex.strip(), 'TESTPLEX') |
| 296 | level = self.vm.cmd('qom-get', path=sclpcpi, property='system_level') |
| 297 | self.assertEqual(level, 0x001a000000060b00) |
| 298 | |
| 299 | if __name__ == '__main__': |
| 300 | QemuSystemTest.main() |