1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3 2eaf1e85dSHanna Reitz# group: rw 39b8c59e7SSergio Lopez# 49b8c59e7SSergio Lopez# Test cases for blockdev + IOThread interactions 59b8c59e7SSergio Lopez# 69b8c59e7SSergio Lopez# Copyright (C) 2019 Red Hat, Inc. 79b8c59e7SSergio Lopez# 89b8c59e7SSergio Lopez# This program is free software; you can redistribute it and/or modify 99b8c59e7SSergio Lopez# it under the terms of the GNU General Public License as published by 109b8c59e7SSergio Lopez# the Free Software Foundation; either version 2 of the License, or 119b8c59e7SSergio Lopez# (at your option) any later version. 129b8c59e7SSergio Lopez# 139b8c59e7SSergio Lopez# This program is distributed in the hope that it will be useful, 149b8c59e7SSergio Lopez# but WITHOUT ANY WARRANTY; without even the implied warranty of 159b8c59e7SSergio Lopez# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 169b8c59e7SSergio Lopez# GNU General Public License for more details. 179b8c59e7SSergio Lopez# 189b8c59e7SSergio Lopez# You should have received a copy of the GNU General Public License 199b8c59e7SSergio Lopez# along with this program. If not, see <http://www.gnu.org/licenses/>. 209b8c59e7SSergio Lopez# 219b8c59e7SSergio Lopez 229b8c59e7SSergio Lopezimport os 23eaf1e85dSHanna Reitzimport time 249b8c59e7SSergio Lopezimport iotests 25eaf1e85dSHanna Reitzfrom iotests import qemu_img, QemuStorageDaemon 269b8c59e7SSergio Lopez 279b8c59e7SSergio Lopezimage_len = 64 * 1024 * 1024 289b8c59e7SSergio Lopez 299b8c59e7SSergio Lopez# Test for RHBZ#1782175 309b8c59e7SSergio Lopezclass TestDirtyBitmapIOThread(iotests.QMPTestCase): 319b8c59e7SSergio Lopez drive0_img = os.path.join(iotests.test_dir, 'drive0.img') 329b8c59e7SSergio Lopez images = { 'drive0': drive0_img } 339b8c59e7SSergio Lopez 349b8c59e7SSergio Lopez def setUp(self): 359b8c59e7SSergio Lopez for name in self.images: 369b8c59e7SSergio Lopez qemu_img('create', '-f', iotests.imgfmt, 379b8c59e7SSergio Lopez self.images[name], str(image_len)) 389b8c59e7SSergio Lopez 399b8c59e7SSergio Lopez self.vm = iotests.VM() 409b8c59e7SSergio Lopez self.vm.add_object('iothread,id=iothread0') 419b8c59e7SSergio Lopez 429b8c59e7SSergio Lopez for name in self.images: 439b8c59e7SSergio Lopez self.vm.add_blockdev('driver=file,filename=%s,node-name=file_%s' 449b8c59e7SSergio Lopez % (self.images[name], name)) 459b8c59e7SSergio Lopez self.vm.add_blockdev('driver=qcow2,file=file_%s,node-name=%s' 469b8c59e7SSergio Lopez % (name, name)) 479b8c59e7SSergio Lopez 489b8c59e7SSergio Lopez self.vm.launch() 499b8c59e7SSergio Lopez self.vm.qmp('x-blockdev-set-iothread', 509b8c59e7SSergio Lopez node_name='drive0', iothread='iothread0', 519b8c59e7SSergio Lopez force=True) 529b8c59e7SSergio Lopez 539b8c59e7SSergio Lopez def tearDown(self): 549b8c59e7SSergio Lopez self.vm.shutdown() 559b8c59e7SSergio Lopez for name in self.images: 569b8c59e7SSergio Lopez os.remove(self.images[name]) 579b8c59e7SSergio Lopez 589b8c59e7SSergio Lopez def test_add_dirty_bitmap(self): 59*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd( 609b8c59e7SSergio Lopez 'block-dirty-bitmap-add', 619b8c59e7SSergio Lopez node='drive0', 629b8c59e7SSergio Lopez name='bitmap1', 639b8c59e7SSergio Lopez persistent=True, 649b8c59e7SSergio Lopez ) 659b8c59e7SSergio Lopez 669b8c59e7SSergio Lopez 679b8c59e7SSergio Lopez# Test for RHBZ#1746217 & RHBZ#1773517 689b8c59e7SSergio Lopezclass TestNBDMirrorIOThread(iotests.QMPTestCase): 699b8c59e7SSergio Lopez nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock') 709b8c59e7SSergio Lopez drive0_img = os.path.join(iotests.test_dir, 'drive0.img') 719b8c59e7SSergio Lopez mirror_img = os.path.join(iotests.test_dir, 'mirror.img') 729b8c59e7SSergio Lopez images = { 'drive0': drive0_img, 'mirror': mirror_img } 739b8c59e7SSergio Lopez 749b8c59e7SSergio Lopez def setUp(self): 759b8c59e7SSergio Lopez for name in self.images: 769b8c59e7SSergio Lopez qemu_img('create', '-f', iotests.imgfmt, 779b8c59e7SSergio Lopez self.images[name], str(image_len)) 789b8c59e7SSergio Lopez 799b8c59e7SSergio Lopez self.vm_src = iotests.VM(path_suffix='src') 809b8c59e7SSergio Lopez self.vm_src.add_object('iothread,id=iothread0') 819b8c59e7SSergio Lopez self.vm_src.add_blockdev('driver=file,filename=%s,node-name=file0' 829b8c59e7SSergio Lopez % (self.drive0_img)) 839b8c59e7SSergio Lopez self.vm_src.add_blockdev('driver=qcow2,file=file0,node-name=drive0') 849b8c59e7SSergio Lopez self.vm_src.launch() 859b8c59e7SSergio Lopez self.vm_src.qmp('x-blockdev-set-iothread', 869b8c59e7SSergio Lopez node_name='drive0', iothread='iothread0', 879b8c59e7SSergio Lopez force=True) 889b8c59e7SSergio Lopez 899b8c59e7SSergio Lopez self.vm_tgt = iotests.VM(path_suffix='tgt') 909b8c59e7SSergio Lopez self.vm_tgt.add_object('iothread,id=iothread0') 919b8c59e7SSergio Lopez self.vm_tgt.add_blockdev('driver=file,filename=%s,node-name=file0' 929b8c59e7SSergio Lopez % (self.mirror_img)) 939b8c59e7SSergio Lopez self.vm_tgt.add_blockdev('driver=qcow2,file=file0,node-name=drive0') 949b8c59e7SSergio Lopez self.vm_tgt.launch() 959b8c59e7SSergio Lopez self.vm_tgt.qmp('x-blockdev-set-iothread', 969b8c59e7SSergio Lopez node_name='drive0', iothread='iothread0', 979b8c59e7SSergio Lopez force=True) 989b8c59e7SSergio Lopez 999b8c59e7SSergio Lopez def tearDown(self): 1009b8c59e7SSergio Lopez self.vm_src.shutdown() 1019b8c59e7SSergio Lopez self.vm_tgt.shutdown() 1029b8c59e7SSergio Lopez for name in self.images: 1039b8c59e7SSergio Lopez os.remove(self.images[name]) 1049b8c59e7SSergio Lopez 1059b8c59e7SSergio Lopez def test_nbd_mirror(self): 106*b6aed193SVladimir Sementsov-Ogievskiy self.vm_tgt.cmd( 1079b8c59e7SSergio Lopez 'nbd-server-start', 1089b8c59e7SSergio Lopez addr={ 1099b8c59e7SSergio Lopez 'type': 'unix', 1109b8c59e7SSergio Lopez 'data': { 'path': self.nbd_sock } 1119b8c59e7SSergio Lopez } 1129b8c59e7SSergio Lopez ) 1139b8c59e7SSergio Lopez 114*b6aed193SVladimir Sementsov-Ogievskiy self.vm_tgt.cmd( 1159b8c59e7SSergio Lopez 'nbd-server-add', 1169b8c59e7SSergio Lopez device='drive0', 1179b8c59e7SSergio Lopez writable=True 1189b8c59e7SSergio Lopez ) 1199b8c59e7SSergio Lopez 120*b6aed193SVladimir Sementsov-Ogievskiy self.vm_src.cmd( 1219b8c59e7SSergio Lopez 'drive-mirror', 1229b8c59e7SSergio Lopez device='drive0', 1239b8c59e7SSergio Lopez target='nbd+unix:///drive0?socket=' + self.nbd_sock, 1249b8c59e7SSergio Lopez sync='full', 1259b8c59e7SSergio Lopez mode='existing', 1269b8c59e7SSergio Lopez speed=64*1024*1024, 1279b8c59e7SSergio Lopez job_id='j1' 1289b8c59e7SSergio Lopez ) 1299b8c59e7SSergio Lopez 1309b8c59e7SSergio Lopez self.vm_src.event_wait(name="BLOCK_JOB_READY") 1319b8c59e7SSergio Lopez 1329b8c59e7SSergio Lopez 1339b8c59e7SSergio Lopez# Test for RHBZ#1779036 1349b8c59e7SSergio Lopezclass TestExternalSnapshotAbort(iotests.QMPTestCase): 1359b8c59e7SSergio Lopez drive0_img = os.path.join(iotests.test_dir, 'drive0.img') 1369b8c59e7SSergio Lopez snapshot_img = os.path.join(iotests.test_dir, 'snapshot.img') 1379b8c59e7SSergio Lopez images = { 'drive0': drive0_img, 'snapshot': snapshot_img } 1389b8c59e7SSergio Lopez 1399b8c59e7SSergio Lopez def setUp(self): 1409b8c59e7SSergio Lopez for name in self.images: 1419b8c59e7SSergio Lopez qemu_img('create', '-f', iotests.imgfmt, 1429b8c59e7SSergio Lopez self.images[name], str(image_len)) 1439b8c59e7SSergio Lopez 1449b8c59e7SSergio Lopez self.vm = iotests.VM() 1459b8c59e7SSergio Lopez self.vm.add_object('iothread,id=iothread0') 1469b8c59e7SSergio Lopez self.vm.add_blockdev('driver=file,filename=%s,node-name=file0' 1479b8c59e7SSergio Lopez % (self.drive0_img)) 1489b8c59e7SSergio Lopez self.vm.add_blockdev('driver=qcow2,file=file0,node-name=drive0') 1499b8c59e7SSergio Lopez self.vm.launch() 1509b8c59e7SSergio Lopez self.vm.qmp('x-blockdev-set-iothread', 1519b8c59e7SSergio Lopez node_name='drive0', iothread='iothread0', 1529b8c59e7SSergio Lopez force=True) 1539b8c59e7SSergio Lopez 1549b8c59e7SSergio Lopez def tearDown(self): 1559b8c59e7SSergio Lopez self.vm.shutdown() 1569b8c59e7SSergio Lopez for name in self.images: 1579b8c59e7SSergio Lopez os.remove(self.images[name]) 1589b8c59e7SSergio Lopez 1599b8c59e7SSergio Lopez def test_external_snapshot_abort(self): 1609b8c59e7SSergio Lopez # Use a two actions transaction with a bogus values on the second 1619b8c59e7SSergio Lopez # one to trigger an abort of the transaction. 1629b8c59e7SSergio Lopez result = self.vm.qmp('transaction', actions=[ 1639b8c59e7SSergio Lopez { 1649b8c59e7SSergio Lopez 'type': 'blockdev-snapshot-sync', 1659b8c59e7SSergio Lopez 'data': { 'node-name': 'drive0', 1669b8c59e7SSergio Lopez 'snapshot-file': self.snapshot_img, 1679b8c59e7SSergio Lopez 'snapshot-node-name': 'snap1', 1689b8c59e7SSergio Lopez 'mode': 'absolute-paths', 1699b8c59e7SSergio Lopez 'format': 'qcow2' } 1709b8c59e7SSergio Lopez }, 1719b8c59e7SSergio Lopez { 1729b8c59e7SSergio Lopez 'type': 'blockdev-snapshot-sync', 1739b8c59e7SSergio Lopez 'data': { 'node-name': 'drive0', 1749b8c59e7SSergio Lopez 'snapshot-file': '/fakesnapshot', 1759b8c59e7SSergio Lopez 'snapshot-node-name': 'snap2', 1769b8c59e7SSergio Lopez 'mode': 'absolute-paths', 1779b8c59e7SSergio Lopez 'format': 'qcow2' } 1789b8c59e7SSergio Lopez }, 1799b8c59e7SSergio Lopez ]) 1809b8c59e7SSergio Lopez 1819b8c59e7SSergio Lopez # Crashes on failure, we expect this error. 1829b8c59e7SSergio Lopez self.assert_qmp(result, 'error/class', 'GenericError') 1839b8c59e7SSergio Lopez 1849b8c59e7SSergio Lopez 1859b8c59e7SSergio Lopez# Test for RHBZ#1782111 1869b8c59e7SSergio Lopezclass TestBlockdevBackupAbort(iotests.QMPTestCase): 1879b8c59e7SSergio Lopez drive0_img = os.path.join(iotests.test_dir, 'drive0.img') 1889b8c59e7SSergio Lopez drive1_img = os.path.join(iotests.test_dir, 'drive1.img') 1899b8c59e7SSergio Lopez snap0_img = os.path.join(iotests.test_dir, 'snap0.img') 1909b8c59e7SSergio Lopez snap1_img = os.path.join(iotests.test_dir, 'snap1.img') 1919b8c59e7SSergio Lopez images = { 'drive0': drive0_img, 1929b8c59e7SSergio Lopez 'drive1': drive1_img, 1939b8c59e7SSergio Lopez 'snap0': snap0_img, 1949b8c59e7SSergio Lopez 'snap1': snap1_img } 1959b8c59e7SSergio Lopez 1969b8c59e7SSergio Lopez def setUp(self): 1979b8c59e7SSergio Lopez for name in self.images: 1989b8c59e7SSergio Lopez qemu_img('create', '-f', iotests.imgfmt, 1999b8c59e7SSergio Lopez self.images[name], str(image_len)) 2009b8c59e7SSergio Lopez 2019b8c59e7SSergio Lopez self.vm = iotests.VM() 2029b8c59e7SSergio Lopez self.vm.add_object('iothread,id=iothread0') 2039b8c59e7SSergio Lopez self.vm.add_device('virtio-scsi,iothread=iothread0') 2049b8c59e7SSergio Lopez 2059b8c59e7SSergio Lopez for name in self.images: 2069b8c59e7SSergio Lopez self.vm.add_blockdev('driver=file,filename=%s,node-name=file_%s' 2079b8c59e7SSergio Lopez % (self.images[name], name)) 2089b8c59e7SSergio Lopez self.vm.add_blockdev('driver=qcow2,file=file_%s,node-name=%s' 2099b8c59e7SSergio Lopez % (name, name)) 2109b8c59e7SSergio Lopez 2119b8c59e7SSergio Lopez self.vm.add_device('scsi-hd,drive=drive0') 2129b8c59e7SSergio Lopez self.vm.add_device('scsi-hd,drive=drive1') 2139b8c59e7SSergio Lopez self.vm.launch() 2149b8c59e7SSergio Lopez 2159b8c59e7SSergio Lopez def tearDown(self): 2169b8c59e7SSergio Lopez self.vm.shutdown() 2179b8c59e7SSergio Lopez for name in self.images: 2189b8c59e7SSergio Lopez os.remove(self.images[name]) 2199b8c59e7SSergio Lopez 2209b8c59e7SSergio Lopez def test_blockdev_backup_abort(self): 2219b8c59e7SSergio Lopez # Use a two actions transaction with a bogus values on the second 2229b8c59e7SSergio Lopez # one to trigger an abort of the transaction. 2239b8c59e7SSergio Lopez result = self.vm.qmp('transaction', actions=[ 2249b8c59e7SSergio Lopez { 2259b8c59e7SSergio Lopez 'type': 'blockdev-backup', 2269b8c59e7SSergio Lopez 'data': { 'device': 'drive0', 2279b8c59e7SSergio Lopez 'target': 'snap0', 2289b8c59e7SSergio Lopez 'sync': 'full', 2299b8c59e7SSergio Lopez 'job-id': 'j1' } 2309b8c59e7SSergio Lopez }, 2319b8c59e7SSergio Lopez { 2329b8c59e7SSergio Lopez 'type': 'blockdev-backup', 2339b8c59e7SSergio Lopez 'data': { 'device': 'drive1', 2349b8c59e7SSergio Lopez 'target': 'snap1', 2359b8c59e7SSergio Lopez 'sync': 'full' } 2369b8c59e7SSergio Lopez }, 2379b8c59e7SSergio Lopez ]) 2389b8c59e7SSergio Lopez 2399b8c59e7SSergio Lopez # Hangs on failure, we expect this error. 2409b8c59e7SSergio Lopez self.assert_qmp(result, 'error/class', 'GenericError') 2419b8c59e7SSergio Lopez 242eaf1e85dSHanna Reitz# Test for RHBZ#2033626 243eaf1e85dSHanna Reitzclass TestYieldingAndTimers(iotests.QMPTestCase): 244eaf1e85dSHanna Reitz sock = os.path.join(iotests.sock_dir, 'nbd.sock') 245eaf1e85dSHanna Reitz qsd = None 246eaf1e85dSHanna Reitz 247eaf1e85dSHanna Reitz def setUp(self): 248eaf1e85dSHanna Reitz self.create_nbd_export() 249eaf1e85dSHanna Reitz 250eaf1e85dSHanna Reitz # Simple VM with an NBD block device connected to the NBD export 2518cfbe929SHanna Reitz # provided by the QSD, and an (initially unused) iothread 252eaf1e85dSHanna Reitz self.vm = iotests.VM() 2538cfbe929SHanna Reitz self.vm.add_object('iothread,id=iothr') 254eaf1e85dSHanna Reitz self.vm.add_blockdev('nbd,node-name=nbd,server.type=unix,' + 255eaf1e85dSHanna Reitz f'server.path={self.sock},export=exp,' + 256eaf1e85dSHanna Reitz 'reconnect-delay=1,open-timeout=1') 257eaf1e85dSHanna Reitz 258eaf1e85dSHanna Reitz self.vm.launch() 259eaf1e85dSHanna Reitz 260eaf1e85dSHanna Reitz def tearDown(self): 261eaf1e85dSHanna Reitz self.stop_nbd_export() 262eaf1e85dSHanna Reitz self.vm.shutdown() 263eaf1e85dSHanna Reitz 264eaf1e85dSHanna Reitz def test_timers_with_blockdev_del(self): 265eaf1e85dSHanna Reitz # The NBD BDS will have had an active open timer, because setUp() gave 266eaf1e85dSHanna Reitz # a positive value for @open-timeout. It should be gone once the BDS 267eaf1e85dSHanna Reitz # has been opened. 268eaf1e85dSHanna Reitz # (But there used to be a bug where it remained active, which will 269eaf1e85dSHanna Reitz # become important below.) 270eaf1e85dSHanna Reitz 271eaf1e85dSHanna Reitz # Stop and restart the NBD server, and do some I/O on the client to 272eaf1e85dSHanna Reitz # trigger a reconnect and start the reconnect delay timer 273eaf1e85dSHanna Reitz self.stop_nbd_export() 274eaf1e85dSHanna Reitz self.create_nbd_export() 275eaf1e85dSHanna Reitz 276eaf1e85dSHanna Reitz result = self.vm.qmp('human-monitor-command', 277eaf1e85dSHanna Reitz command_line='qemu-io nbd "write 0 512"') 278eaf1e85dSHanna Reitz self.assert_qmp(result, 'return', '') 279eaf1e85dSHanna Reitz 280eaf1e85dSHanna Reitz # Reconnect is done, so the reconnect delay timer should be gone. 281eaf1e85dSHanna Reitz # (This is similar to how the open timer should be gone after open, 282eaf1e85dSHanna Reitz # and similarly there used to be a bug where it was not gone.) 283eaf1e85dSHanna Reitz 284eaf1e85dSHanna Reitz # Delete the BDS to see whether both timers are gone. If they are not, 285eaf1e85dSHanna Reitz # they will remain active, fire later, and then access freed data. 286eaf1e85dSHanna Reitz # (Or, with "block/nbd: Assert there are no timers when closed" 287eaf1e85dSHanna Reitz # applied, the assertions added in that patch will fail.) 288*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('blockdev-del', node_name='nbd') 289eaf1e85dSHanna Reitz 290eaf1e85dSHanna Reitz # Give the timers some time to fire (both have a timeout of 1 s). 291eaf1e85dSHanna Reitz # (Sleeping in an iotest may ring some alarm bells, but note that if 292eaf1e85dSHanna Reitz # the timing is off here, the test will just always pass. If we kill 293eaf1e85dSHanna Reitz # the VM too early, then we just kill the timers before they can fire, 294eaf1e85dSHanna Reitz # thus not see the error, and so the test will pass.) 295eaf1e85dSHanna Reitz time.sleep(2) 296eaf1e85dSHanna Reitz 2978cfbe929SHanna Reitz def test_yield_in_iothread(self): 2988cfbe929SHanna Reitz # Move the NBD node to the I/O thread; the NBD block driver should 2998cfbe929SHanna Reitz # attach the connection's QIOChannel to that thread's AioContext, too 300*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('x-blockdev-set-iothread', 3018cfbe929SHanna Reitz node_name='nbd', iothread='iothr') 3028cfbe929SHanna Reitz 3038cfbe929SHanna Reitz # Do some I/O that will be throttled by the QSD, so that the network 3048cfbe929SHanna Reitz # connection hopefully will yield here. When it is resumed, it must 3058cfbe929SHanna Reitz # then be resumed in the I/O thread's AioContext. 3068cfbe929SHanna Reitz result = self.vm.qmp('human-monitor-command', 3078cfbe929SHanna Reitz command_line='qemu-io nbd "read 0 128K"') 3088cfbe929SHanna Reitz self.assert_qmp(result, 'return', '') 3098cfbe929SHanna Reitz 310eaf1e85dSHanna Reitz def create_nbd_export(self): 311eaf1e85dSHanna Reitz assert self.qsd is None 312eaf1e85dSHanna Reitz 3138cfbe929SHanna Reitz # Export a throttled null-co BDS: Reads are throttled (max 64 kB/s), 3148cfbe929SHanna Reitz # writes are not. 315eaf1e85dSHanna Reitz self.qsd = QemuStorageDaemon( 3168cfbe929SHanna Reitz '--object', 3178cfbe929SHanna Reitz 'throttle-group,id=thrgr,x-bps-read=65536,x-bps-read-max=65536', 3188cfbe929SHanna Reitz 319eaf1e85dSHanna Reitz '--blockdev', 320eaf1e85dSHanna Reitz 'null-co,node-name=null,read-zeroes=true', 321eaf1e85dSHanna Reitz 3228cfbe929SHanna Reitz '--blockdev', 3238cfbe929SHanna Reitz 'throttle,node-name=thr,file=null,throttle-group=thrgr', 3248cfbe929SHanna Reitz 325eaf1e85dSHanna Reitz '--nbd-server', 326eaf1e85dSHanna Reitz f'addr.type=unix,addr.path={self.sock}', 327eaf1e85dSHanna Reitz 328eaf1e85dSHanna Reitz '--export', 3298cfbe929SHanna Reitz 'nbd,id=exp,node-name=thr,name=exp,writable=true' 330eaf1e85dSHanna Reitz ) 331eaf1e85dSHanna Reitz 332eaf1e85dSHanna Reitz def stop_nbd_export(self): 333eaf1e85dSHanna Reitz self.qsd.stop() 334eaf1e85dSHanna Reitz self.qsd = None 335eaf1e85dSHanna Reitz 3369b8c59e7SSergio Lopezif __name__ == '__main__': 3379b8c59e7SSergio Lopez iotests.main(supported_fmts=['qcow2'], 338b30b8077SVladimir Sementsov-Ogievskiy supported_protocols=['file'], 339b30b8077SVladimir Sementsov-Ogievskiy unsupported_imgopts=['compat']) 340