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                    r'echo dm_clear\ ' + str(self.dmesg_clear_count),
40                    r'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                         '-cpu', 'max,prno-trng=off',
70                         '-device', 'virtio-net-ccw,devno=fe.1.1111',
71                         '-device',
72                         'virtio-rng-ccw,devno=fe.2.0000,max_revision=0,id=rn1',
73                         '-device',
74                         'virtio-rng-ccw,devno=fe.3.1234,max_revision=2,id=rn2',
75                         '-device', 'zpci,uid=5,target=zzz',
76                         '-device', 'virtio-net-pci,id=zzz',
77                         '-device', 'zpci,uid=0xa,fid=12,target=serial',
78                         '-device', 'virtio-serial-pci,id=serial',
79                         '-device', 'virtio-balloon-ccw')
80        self.vm.launch()
81
82        shell_ready = "sh: can't access tty; job control turned off"
83        self.wait_for_console_pattern(shell_ready)
84        # first debug shell is too early, we need to wait for device detection
85        exec_command_and_wait_for_pattern(self, 'exit', shell_ready)
86
87        ccw_bus_ids="0.1.1111  0.2.0000  0.3.1234"
88        pci_bus_ids="0005:00:00.0  000a:00:00.0"
89        exec_command_and_wait_for_pattern(self, 'ls /sys/bus/ccw/devices/',
90                                          ccw_bus_ids)
91        exec_command_and_wait_for_pattern(self, 'ls /sys/bus/pci/devices/',
92                                          pci_bus_ids)
93        # check that the device at 0.2.0000 is in legacy mode, while the
94        # device at 0.3.1234 has the virtio-1 feature bit set
95        virtio_rng_features="00000000000000000000000000001100" + \
96                            "10000000000000000000000000000000"
97        virtio_rng_features_legacy="00000000000000000000000000001100" + \
98                                   "00000000000000000000000000000000"
99        exec_command_and_wait_for_pattern(self,
100                        'cat /sys/bus/ccw/devices/0.2.0000/virtio?/features',
101                        virtio_rng_features_legacy)
102        exec_command_and_wait_for_pattern(self,
103                        'cat /sys/bus/ccw/devices/0.3.1234/virtio?/features',
104                        virtio_rng_features)
105        # check that /dev/hwrng works - and that it's gone after ejecting
106        exec_command_and_wait_for_pattern(self,
107                        'dd if=/dev/hwrng of=/dev/null bs=1k count=10',
108                        '10+0 records out')
109        self.clear_guest_dmesg()
110        self.vm.cmd('device_del', id='rn1')
111        self.wait_for_crw_reports()
112        self.clear_guest_dmesg()
113        self.vm.cmd('device_del', id='rn2')
114        self.wait_for_crw_reports()
115        exec_command_and_wait_for_pattern(self,
116                        'dd if=/dev/hwrng of=/dev/null bs=1k count=10',
117                        'dd: /dev/hwrng: No such device')
118        # verify that we indeed have virtio-net devices (without having the
119        # virtio-net driver handy)
120        exec_command_and_wait_for_pattern(self,
121                                    'cat /sys/bus/ccw/devices/0.1.1111/cutype',
122                                    '3832/01')
123        exec_command_and_wait_for_pattern(self,
124                    r'cat /sys/bus/pci/devices/0005\:00\:00.0/subsystem_vendor',
125                    r'0x1af4')
126        exec_command_and_wait_for_pattern(self,
127                    r'cat /sys/bus/pci/devices/0005\:00\:00.0/subsystem_device',
128                    r'0x0001')
129        # check fid propagation
130        exec_command_and_wait_for_pattern(self,
131                    r'cat /sys/bus/pci/devices/000a\:00\:00.0/function_id',
132                    r'0x0000000c')
133        # add another device
134        self.clear_guest_dmesg()
135        self.vm.cmd('device_add', driver='virtio-net-ccw',
136                    devno='fe.0.4711', id='net_4711')
137        self.wait_for_crw_reports()
138        exec_command_and_wait_for_pattern(self, 'for i in 1 2 3 4 5 6 7 ; do '
139                    'if [ -e /sys/bus/ccw/devices/*4711 ]; then break; fi ;'
140                    'sleep 1 ; done ; ls /sys/bus/ccw/devices/',
141                    '0.0.4711')
142        # and detach it again
143        self.clear_guest_dmesg()
144        self.vm.cmd('device_del', id='net_4711')
145        self.vm.event_wait(name='DEVICE_DELETED',
146                           match={'data': {'device': 'net_4711'}})
147        self.wait_for_crw_reports()
148        exec_command_and_wait_for_pattern(self,
149                                          'ls /sys/bus/ccw/devices/0.0.4711',
150                                          'No such file or directory')
151        # test the virtio-balloon device
152        exec_command_and_wait_for_pattern(self, 'head -n 1 /proc/meminfo',
153                                          'MemTotal:         115640 kB')
154        self.vm.cmd('human-monitor-command', command_line='balloon 96')
155        exec_command_and_wait_for_pattern(self, 'head -n 1 /proc/meminfo',
156                                          'MemTotal:          82872 kB')
157        self.vm.cmd('human-monitor-command', command_line='balloon 128')
158        exec_command_and_wait_for_pattern(self, 'head -n 1 /proc/meminfo',
159                                          'MemTotal:         115640 kB')
160
161
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        # Unfortunately, this test is flaky, so we don't run it by default
232        if os.getenv('QEMU_TEST_FLAKY_TESTS'):
233            self.log.info("Test screendump of virtio-gpu device")
234            exec_command_and_wait_for_pattern(self,
235                        'while ! (dmesg | grep gpudrmfb) ; do sleep 1 ; done',
236                        'virtio_gpudrmfb frame buffer device')
237            exec_command_and_wait_for_pattern(self,
238                r'echo -e "\e[?25l" > /dev/tty0', ':/#')
239            exec_command_and_wait_for_pattern(self, 'for ((i=0;i<250;i++)); do '
240                'echo " The  qu ick  fo x j ump s o ver  a  laz y d og" >> fox.txt;'
241                'done',
242                ':/#')
243            exec_command_and_wait_for_pattern(self,
244                'dd if=fox.txt of=/dev/fb0 bs=1000 oflag=sync,nocache ; rm fox.txt',
245                '12+0 records out')
246            with tempfile.NamedTemporaryFile(suffix='.ppm',
247                                             prefix='qemu-scrdump-') as ppmfile:
248                self.vm.cmd('screendump', filename=ppmfile.name)
249                ppmfile.seek(0)
250                line = ppmfile.readline()
251                self.assertEqual(line, b"P6\n")
252                line = ppmfile.readline()
253                self.assertEqual(line, b"1280 800\n")
254                line = ppmfile.readline()
255                self.assertEqual(line, b"255\n")
256                line = ppmfile.readline(256)
257                self.assertEqual(line, b"The quick fox jumps over a lazy dog\n")
258        else:
259            self.log.info("Skipped flaky screendump of virtio-gpu device test")
260
261        # Hot-plug a virtio-crypto device and see whether it gets accepted
262        self.log.info("Test hot-plug virtio-crypto device")
263        self.clear_guest_dmesg()
264        self.vm.cmd('object-add', qom_type='cryptodev-backend-builtin',
265                    id='cbe0')
266        self.vm.cmd('device_add', driver='virtio-crypto-ccw', id='crypdev0',
267                    cryptodev='cbe0', devno='fe.0.2342')
268        exec_command_and_wait_for_pattern(self,
269                        'while ! (dmesg -c | grep Accelerator.device) ; do'
270                        ' sleep 1 ; done', 'Accelerator device is ready')
271        exec_command_and_wait_for_pattern(self, 'lscss', '0.0.2342')
272        self.vm.cmd('device_del', id='crypdev0')
273        self.vm.cmd('object-del', id='cbe0')
274        exec_command_and_wait_for_pattern(self,
275                        'while ! (dmesg -c | grep Start.virtcrypto_remove) ; do'
276                        ' sleep 1 ; done', 'Start virtcrypto_remove.')
277