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                         '-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.command('device_del', id='rn1')
111        self.wait_for_crw_reports()
112        self.clear_guest_dmesg()
113        self.vm.command('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                    'cat /sys/bus/pci/devices/0005\:00\:00.0/subsystem_vendor',
125                    '0x1af4')
126        exec_command_and_wait_for_pattern(self,
127                    'cat /sys/bus/pci/devices/0005\:00\:00.0/subsystem_device',
128                    '0x0001')
129        # check fid propagation
130        exec_command_and_wait_for_pattern(self,
131                        'cat /sys/bus/pci/devices/000a\:00\:00.0/function_id',
132                        '0x0000000c')
133        # add another device
134        self.clear_guest_dmesg()
135        self.vm.command('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.command('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.command('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.command('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    @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
163    def test_s390x_fedora(self):
164
165        """
166        :avocado: tags=arch:s390x
167        :avocado: tags=machine:s390-ccw-virtio
168        :avocado: tags=device:virtio-gpu
169        :avocado: tags=device:virtio-crypto
170        :avocado: tags=device:virtio-net
171        """
172
173        kernel_url = ('https://archives.fedoraproject.org/pub/archive'
174                      '/fedora-secondary/releases/31/Server/s390x/os'
175                      '/images/kernel.img')
176        kernel_hash = 'b93d1efcafcf29c1673a4ce371a1f8b43941cfeb'
177        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
178
179        initrd_url = ('https://archives.fedoraproject.org/pub/archive'
180                      '/fedora-secondary/releases/31/Server/s390x/os'
181                      '/images/initrd.img')
182        initrd_hash = '3de45d411df5624b8d8ef21cd0b44419ab59b12f'
183        initrd_path_xz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
184        initrd_path = os.path.join(self.workdir, 'initrd-raw.img')
185        archive.lzma_uncompress(initrd_path_xz, initrd_path)
186
187        self.vm.set_console()
188        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + ' audit=0 '
189                              'rd.plymouth=0 plymouth.enable=0 rd.rescue')
190        self.vm.add_args('-nographic',
191                         '-smp', '4',
192                         '-m', '512',
193                         '-name', 'Some Guest Name',
194                         '-uuid', '30de4fd9-b4d5-409e-86a5-09b387f70bfa',
195                         '-kernel', kernel_path,
196                         '-initrd', initrd_path,
197                         '-append', kernel_command_line,
198                         '-device', 'zpci,uid=7,target=n',
199                         '-device', 'virtio-net-pci,id=n,mac=02:ca:fe:fa:ce:12',
200                         '-device', 'virtio-rng-ccw,devno=fe.1.9876',
201                         '-device', 'virtio-gpu-ccw,devno=fe.2.5432')
202        self.vm.launch()
203        self.wait_for_console_pattern('Entering emergency mode')
204
205        # Some tests to see whether the CLI options have been considered:
206        self.log.info("Test whether QEMU CLI options have been considered")
207        exec_command_and_wait_for_pattern(self,
208                        'while ! (dmesg | grep enP7p0s0) ; do sleep 1 ; done',
209                        'virtio_net virtio0 enP7p0s0: renamed')
210        exec_command_and_wait_for_pattern(self, 'lspci',
211                             '0007:00:00.0 Class 0200: Device 1af4:1000')
212        exec_command_and_wait_for_pattern(self,
213                             'cat /sys/class/net/enP7p0s0/address',
214                             '02:ca:fe:fa:ce:12')
215        exec_command_and_wait_for_pattern(self, 'lscss', '0.1.9876')
216        exec_command_and_wait_for_pattern(self, 'lscss', '0.2.5432')
217        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
218                             'processors    : 4')
219        exec_command_and_wait_for_pattern(self, 'grep MemTotal /proc/meminfo',
220                             'MemTotal:         499848 kB')
221        exec_command_and_wait_for_pattern(self, 'grep Name /proc/sysinfo',
222                             'Extended Name:   Some Guest Name')
223        exec_command_and_wait_for_pattern(self, 'grep UUID /proc/sysinfo',
224                             '30de4fd9-b4d5-409e-86a5-09b387f70bfa')
225
226        # Disable blinking cursor, then write some stuff into the framebuffer.
227        # QEMU's PPM screendumps contain uncompressed 24-bit values, while the
228        # framebuffer uses 32-bit, so we pad our text with some spaces when
229        # writing to the framebuffer. Since the PPM is uncompressed, we then
230        # can simply read the written "magic bytes" back from the PPM file to
231        # check whether the framebuffer is working as expected.
232        self.log.info("Test screendump of virtio-gpu device")
233        exec_command_and_wait_for_pattern(self,
234                        'while ! (dmesg | grep gpudrmfb) ; do sleep 1 ; done',
235                        'virtio_gpudrmfb frame buffer device')
236        exec_command_and_wait_for_pattern(self,
237            'echo -e "\e[?25l" > /dev/tty0', ':/#')
238        exec_command_and_wait_for_pattern(self, 'for ((i=0;i<250;i++)); do '
239            'echo " The  qu ick  fo x j ump s o ver  a  laz y d og" >> fox.txt;'
240            'done',
241            ':/#')
242        exec_command_and_wait_for_pattern(self,
243            'dd if=fox.txt of=/dev/fb0 bs=1000 oflag=sync,nocache ; rm fox.txt',
244            '12+0 records out')
245        with tempfile.NamedTemporaryFile(suffix='.ppm',
246                                         prefix='qemu-scrdump-') as ppmfile:
247            self.vm.command('screendump', filename=ppmfile.name)
248            ppmfile.seek(0)
249            line = ppmfile.readline()
250            self.assertEqual(line, b"P6\n")
251            line = ppmfile.readline()
252            self.assertEqual(line, b"1280 800\n")
253            line = ppmfile.readline()
254            self.assertEqual(line, b"255\n")
255            line = ppmfile.readline(256)
256            self.assertEqual(line, b"The quick fox jumps over a lazy dog\n")
257
258        # Hot-plug a virtio-crypto device and see whether it gets accepted
259        self.log.info("Test hot-plug virtio-crypto device")
260        self.clear_guest_dmesg()
261        self.vm.command('object-add', qom_type='cryptodev-backend-builtin',
262                        id='cbe0')
263        self.vm.command('device_add', driver='virtio-crypto-ccw', id='crypdev0',
264                        cryptodev='cbe0', devno='fe.0.2342')
265        exec_command_and_wait_for_pattern(self,
266                        'while ! (dmesg -c | grep Accelerator.device) ; do'
267                        ' sleep 1 ; done', 'Accelerator device is ready')
268        exec_command_and_wait_for_pattern(self, 'lscss', '0.0.2342')
269        self.vm.command('device_del', id='crypdev0')
270        self.vm.command('object-del', id='cbe0')
271        exec_command_and_wait_for_pattern(self,
272                        'while ! (dmesg -c | grep Start.virtcrypto_remove) ; do'
273                        ' sleep 1 ; done', 'Start virtcrypto_remove.')
274