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