xref: /openbmc/qemu/tests/qemu-iotests/151 (revision 1da79ecc)
1#!/usr/bin/env python3
2# group: rw
3#
4# Tests for active mirroring
5#
6# Copyright (C) 2018 Red Hat, Inc.
7#
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.
12#
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.
17#
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/>.
20#
21
22import os
23import iotests
24from iotests import qemu_img
25
26source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
27target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
28
29class TestActiveMirror(iotests.QMPTestCase):
30    image_len = 128 * 1024 * 1024 # MB
31    potential_writes_in_flight = True
32
33    def setUp(self):
34        qemu_img('create', '-f', iotests.imgfmt, source_img, '128M')
35        qemu_img('create', '-f', iotests.imgfmt, target_img, '128M')
36
37        blk_source = {'id': 'source',
38                      'if': 'none',
39                      'node-name': 'source-node',
40                      'driver': iotests.imgfmt,
41                      'file': {'driver': 'file',
42                               'filename': source_img}}
43
44        blk_target = {'node-name': 'target-node',
45                      'driver': iotests.imgfmt,
46                      'file': {'driver': 'file',
47                               'filename': target_img}}
48
49        self.vm = iotests.VM()
50        self.vm.add_drive_raw(self.vm.qmp_to_opts(blk_source))
51        self.vm.add_blockdev(self.vm.qmp_to_opts(blk_target))
52        self.vm.add_device('virtio-blk,drive=source')
53        self.vm.launch()
54
55    def tearDown(self):
56        self.vm.shutdown()
57
58        if not self.potential_writes_in_flight:
59            self.assertTrue(iotests.compare_images(source_img, target_img),
60                            'mirror target does not match source')
61
62        os.remove(source_img)
63        os.remove(target_img)
64
65    def doActiveIO(self, sync_source_and_target):
66        # Fill the source image
67        self.vm.hmp_qemu_io('source',
68                            'write -P 1 0 %i' % self.image_len);
69
70        # Start some background requests
71        for offset in range(1 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
72            self.vm.hmp_qemu_io('source', 'aio_write -P 2 %i 1M' % offset)
73        for offset in range(2 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
74            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
75
76        # Start the block job
77        result = self.vm.qmp('blockdev-mirror',
78                             job_id='mirror',
79                             filter_node_name='mirror-node',
80                             device='source-node',
81                             target='target-node',
82                             sync='full',
83                             copy_mode='write-blocking')
84        self.assert_qmp(result, 'return', {})
85
86        # Start some more requests
87        for offset in range(3 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
88            self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
89        for offset in range(4 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
90            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
91
92        # Wait for the READY event
93        self.wait_ready(drive='mirror')
94
95        # Now start some final requests; all of these (which land on
96        # the source) should be settled using the active mechanism.
97        # The mirror code itself asserts that the source BDS's dirty
98        # bitmap will stay clean between READY and COMPLETED.
99        for offset in range(5 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
100            self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
101        for offset in range(6 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
102            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
103
104        if sync_source_and_target:
105            # If source and target should be in sync after the mirror,
106            # we have to flush before completion
107            self.vm.hmp_qemu_io('source', 'aio_flush')
108            self.potential_writes_in_flight = False
109
110        self.complete_and_wait(drive='mirror', wait_ready=False)
111
112    def testActiveIO(self):
113        self.doActiveIO(False)
114
115    def testActiveIOFlushed(self):
116        self.doActiveIO(True)
117
118    def testUnalignedActiveIO(self):
119        # Fill the source image
120        result = self.vm.hmp_qemu_io('source', 'write -P 1 0 2M')
121
122        # Start the block job (very slowly)
123        result = self.vm.qmp('blockdev-mirror',
124                             job_id='mirror',
125                             filter_node_name='mirror-node',
126                             device='source-node',
127                             target='target-node',
128                             sync='full',
129                             copy_mode='write-blocking',
130                             buf_size=(1048576 // 4),
131                             speed=1)
132        self.assert_qmp(result, 'return', {})
133
134        # Start an unaligned request to a dirty area
135        result = self.vm.hmp_qemu_io('source', 'write -P 2 %i 1' % (1048576 + 42))
136
137        # Let the job finish
138        result = self.vm.qmp('block-job-set-speed', device='mirror', speed=0)
139        self.assert_qmp(result, 'return', {})
140        self.complete_and_wait(drive='mirror')
141
142        self.potential_writes_in_flight = False
143
144
145if __name__ == '__main__':
146    iotests.main(supported_fmts=['qcow2', 'raw'],
147                 supported_protocols=['file'])
148