4 # Test cases for blockdev + IOThread interactions
6 # Copyright (C) 2019 Red Hat, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from iotests import qemu_img, QemuStorageDaemon
27 image_len = 64 * 1024 * 1024
29 # Test for RHBZ#1782175
30 class TestDirtyBitmapIOThread(iotests.QMPTestCase):
31 drive0_img = os.path.join(iotests.test_dir, 'drive0.img')
32 images = { 'drive0': drive0_img }
35 for name in self.images:
36 qemu_img('create', '-f', iotests.imgfmt,
37 self.images[name], str(image_len))
39 self.vm = iotests.VM()
40 self.vm.add_object('iothread,id=iothread0')
42 for name in self.images:
43 self.vm.add_blockdev('driver=file,filename=%s,node-name=file_%s'
44 % (self.images[name], name))
45 self.vm.add_blockdev('driver=qcow2,file=file_%s,node-name=%s'
49 self.vm.qmp('x-blockdev-set-iothread',
50 node_name='drive0', iothread='iothread0',
55 for name in self.images:
56 os.remove(self.images[name])
58 def test_add_dirty_bitmap(self):
60 'block-dirty-bitmap-add',
66 self.assert_qmp(result, 'return', {})
69 # Test for RHBZ#1746217 & RHBZ#1773517
70 class TestNBDMirrorIOThread(iotests.QMPTestCase):
71 nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
72 drive0_img = os.path.join(iotests.test_dir, 'drive0.img')
73 mirror_img = os.path.join(iotests.test_dir, 'mirror.img')
74 images = { 'drive0': drive0_img, 'mirror': mirror_img }
77 for name in self.images:
78 qemu_img('create', '-f', iotests.imgfmt,
79 self.images[name], str(image_len))
81 self.vm_src = iotests.VM(path_suffix='src')
82 self.vm_src.add_object('iothread,id=iothread0')
83 self.vm_src.add_blockdev('driver=file,filename=%s,node-name=file0'
85 self.vm_src.add_blockdev('driver=qcow2,file=file0,node-name=drive0')
87 self.vm_src.qmp('x-blockdev-set-iothread',
88 node_name='drive0', iothread='iothread0',
91 self.vm_tgt = iotests.VM(path_suffix='tgt')
92 self.vm_tgt.add_object('iothread,id=iothread0')
93 self.vm_tgt.add_blockdev('driver=file,filename=%s,node-name=file0'
95 self.vm_tgt.add_blockdev('driver=qcow2,file=file0,node-name=drive0')
97 self.vm_tgt.qmp('x-blockdev-set-iothread',
98 node_name='drive0', iothread='iothread0',
102 self.vm_src.shutdown()
103 self.vm_tgt.shutdown()
104 for name in self.images:
105 os.remove(self.images[name])
107 def test_nbd_mirror(self):
108 result = self.vm_tgt.qmp(
112 'data': { 'path': self.nbd_sock }
115 self.assert_qmp(result, 'return', {})
117 result = self.vm_tgt.qmp(
122 self.assert_qmp(result, 'return', {})
124 result = self.vm_src.qmp(
127 target='nbd+unix:///drive0?socket=' + self.nbd_sock,
133 self.assert_qmp(result, 'return', {})
135 self.vm_src.event_wait(name="BLOCK_JOB_READY")
138 # Test for RHBZ#1779036
139 class TestExternalSnapshotAbort(iotests.QMPTestCase):
140 drive0_img = os.path.join(iotests.test_dir, 'drive0.img')
141 snapshot_img = os.path.join(iotests.test_dir, 'snapshot.img')
142 images = { 'drive0': drive0_img, 'snapshot': snapshot_img }
145 for name in self.images:
146 qemu_img('create', '-f', iotests.imgfmt,
147 self.images[name], str(image_len))
149 self.vm = iotests.VM()
150 self.vm.add_object('iothread,id=iothread0')
151 self.vm.add_blockdev('driver=file,filename=%s,node-name=file0'
153 self.vm.add_blockdev('driver=qcow2,file=file0,node-name=drive0')
155 self.vm.qmp('x-blockdev-set-iothread',
156 node_name='drive0', iothread='iothread0',
161 for name in self.images:
162 os.remove(self.images[name])
164 def test_external_snapshot_abort(self):
165 # Use a two actions transaction with a bogus values on the second
166 # one to trigger an abort of the transaction.
167 result = self.vm.qmp('transaction', actions=[
169 'type': 'blockdev-snapshot-sync',
170 'data': { 'node-name': 'drive0',
171 'snapshot-file': self.snapshot_img,
172 'snapshot-node-name': 'snap1',
173 'mode': 'absolute-paths',
177 'type': 'blockdev-snapshot-sync',
178 'data': { 'node-name': 'drive0',
179 'snapshot-file': '/fakesnapshot',
180 'snapshot-node-name': 'snap2',
181 'mode': 'absolute-paths',
186 # Crashes on failure, we expect this error.
187 self.assert_qmp(result, 'error/class', 'GenericError')
190 # Test for RHBZ#1782111
191 class TestBlockdevBackupAbort(iotests.QMPTestCase):
192 drive0_img = os.path.join(iotests.test_dir, 'drive0.img')
193 drive1_img = os.path.join(iotests.test_dir, 'drive1.img')
194 snap0_img = os.path.join(iotests.test_dir, 'snap0.img')
195 snap1_img = os.path.join(iotests.test_dir, 'snap1.img')
196 images = { 'drive0': drive0_img,
197 'drive1': drive1_img,
202 for name in self.images:
203 qemu_img('create', '-f', iotests.imgfmt,
204 self.images[name], str(image_len))
206 self.vm = iotests.VM()
207 self.vm.add_object('iothread,id=iothread0')
208 self.vm.add_device('virtio-scsi,iothread=iothread0')
210 for name in self.images:
211 self.vm.add_blockdev('driver=file,filename=%s,node-name=file_%s'
212 % (self.images[name], name))
213 self.vm.add_blockdev('driver=qcow2,file=file_%s,node-name=%s'
216 self.vm.add_device('scsi-hd,drive=drive0')
217 self.vm.add_device('scsi-hd,drive=drive1')
222 for name in self.images:
223 os.remove(self.images[name])
225 def test_blockdev_backup_abort(self):
226 # Use a two actions transaction with a bogus values on the second
227 # one to trigger an abort of the transaction.
228 result = self.vm.qmp('transaction', actions=[
230 'type': 'blockdev-backup',
231 'data': { 'device': 'drive0',
237 'type': 'blockdev-backup',
238 'data': { 'device': 'drive1',
244 # Hangs on failure, we expect this error.
245 self.assert_qmp(result, 'error/class', 'GenericError')
247 # Test for RHBZ#2033626
248 class TestYieldingAndTimers(iotests.QMPTestCase):
249 sock = os.path.join(iotests.sock_dir, 'nbd.sock')
253 self.create_nbd_export()
255 # Simple VM with an NBD block device connected to the NBD export
256 # provided by the QSD, and an (initially unused) iothread
257 self.vm = iotests.VM()
258 self.vm.add_object('iothread,id=iothr')
259 self.vm.add_blockdev('nbd,node-name=nbd,server.type=unix,' +
260 f'server.path={self.sock},export=exp,' +
261 'reconnect-delay=1,open-timeout=1')
266 self.stop_nbd_export()
269 def test_timers_with_blockdev_del(self):
270 # The NBD BDS will have had an active open timer, because setUp() gave
271 # a positive value for @open-timeout. It should be gone once the BDS
273 # (But there used to be a bug where it remained active, which will
274 # become important below.)
276 # Stop and restart the NBD server, and do some I/O on the client to
277 # trigger a reconnect and start the reconnect delay timer
278 self.stop_nbd_export()
279 self.create_nbd_export()
281 result = self.vm.qmp('human-monitor-command',
282 command_line='qemu-io nbd "write 0 512"')
283 self.assert_qmp(result, 'return', '')
285 # Reconnect is done, so the reconnect delay timer should be gone.
286 # (This is similar to how the open timer should be gone after open,
287 # and similarly there used to be a bug where it was not gone.)
289 # Delete the BDS to see whether both timers are gone. If they are not,
290 # they will remain active, fire later, and then access freed data.
291 # (Or, with "block/nbd: Assert there are no timers when closed"
292 # applied, the assertions added in that patch will fail.)
293 result = self.vm.qmp('blockdev-del', node_name='nbd')
294 self.assert_qmp(result, 'return', {})
296 # Give the timers some time to fire (both have a timeout of 1 s).
297 # (Sleeping in an iotest may ring some alarm bells, but note that if
298 # the timing is off here, the test will just always pass. If we kill
299 # the VM too early, then we just kill the timers before they can fire,
300 # thus not see the error, and so the test will pass.)
303 def test_yield_in_iothread(self):
304 # Move the NBD node to the I/O thread; the NBD block driver should
305 # attach the connection's QIOChannel to that thread's AioContext, too
306 result = self.vm.qmp('x-blockdev-set-iothread',
307 node_name='nbd', iothread='iothr')
308 self.assert_qmp(result, 'return', {})
310 # Do some I/O that will be throttled by the QSD, so that the network
311 # connection hopefully will yield here. When it is resumed, it must
312 # then be resumed in the I/O thread's AioContext.
313 result = self.vm.qmp('human-monitor-command',
314 command_line='qemu-io nbd "read 0 128K"')
315 self.assert_qmp(result, 'return', '')
317 def create_nbd_export(self):
318 assert self.qsd is None
320 # Export a throttled null-co BDS: Reads are throttled (max 64 kB/s),
322 self.qsd = QemuStorageDaemon(
324 'throttle-group,id=thrgr,x-bps-read=65536,x-bps-read-max=65536',
327 'null-co,node-name=null,read-zeroes=true',
330 'throttle,node-name=thr,file=null,throttle-group=thrgr',
333 f'addr.type=unix,addr.path={self.sock}',
336 'nbd,id=exp,node-name=thr,name=exp,writable=true'
339 def stop_nbd_export(self):
343 if __name__ == '__main__':
344 iotests.main(supported_fmts=['qcow2'],
345 supported_protocols=['file'],
346 unsupported_imgopts=['compat'])