xref: /openbmc/qemu/tests/qemu-iotests/148 (revision 250bc43a)
1#!/usr/bin/env python3
2#
3# Test the rate limit of QMP events
4#
5# Copyright (C) 2016 Igalia, S.L.
6# Author: Alberto Garcia <berto@igalia.com>
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
24
25imgs = (os.path.join(iotests.test_dir, 'quorum0.img'),
26        os.path.join(iotests.test_dir, 'quorum1.img'),
27        os.path.join(iotests.test_dir, 'quorum2.img'))
28
29img_conf = (os.path.join(iotests.test_dir, 'quorum0.conf'),
30            os.path.join(iotests.test_dir, 'quorum1.conf'),
31            os.path.join(iotests.test_dir, 'quorum2.conf'))
32
33event_rate = 1000000000
34sector_size = 512
35offset = 10
36
37class TestQuorumEvents(iotests.QMPTestCase):
38    read_pattern = 'quorum'
39
40    def create_blkdebug_file(self, blkdebug_file, bad_sector):
41        file = open(blkdebug_file, 'w')
42        file.write('''
43[inject-error]
44event = "read_aio"
45errno = "5"
46sector = "%d"
47''' % bad_sector)
48        file.close()
49
50    @iotests.skip_if_unsupported(['quorum'])
51    def setUp(self):
52        driveopts = ['driver=quorum', 'vote-threshold=2']
53        driveopts.append('read-pattern=%s' % self.read_pattern)
54        for i in range(len(imgs)):
55            iotests.qemu_img('create', '-f', iotests.imgfmt, imgs[i], '1M')
56            self.create_blkdebug_file(img_conf[i], i + offset)
57            driveopts.append('children.%d.driver=%s' % (i, iotests.imgfmt))
58            driveopts.append('children.%d.file.driver=blkdebug' % i)
59            driveopts.append('children.%d.file.config=%s' % (i, img_conf[i]))
60            driveopts.append('children.%d.file.image.filename=%s' % (i, imgs[i]))
61            driveopts.append('children.%d.node-name=img%d' % (i, i))
62        self.vm = iotests.VM()
63        self.vm.add_drive(None, opts = ','.join(driveopts))
64        self.vm.launch()
65
66    def tearDown(self):
67        self.vm.shutdown()
68        for i in range(len(imgs)):
69            os.remove(imgs[i])
70            os.remove(img_conf[i])
71
72    def do_check_event(self, node, sector = 0):
73        if node == None:
74            self.assertEqual(self.vm.get_qmp_event(), None)
75            return
76
77        for event in self.vm.get_qmp_events(wait=True):
78            if event['event'] == 'QUORUM_REPORT_BAD':
79                self.assert_qmp(event, 'data/node-name', node)
80                self.assert_qmp(event, 'data/sector-num', sector)
81
82    def testQuorum(self):
83        # Generate an error and get an event
84        self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
85                            (offset * sector_size, sector_size))
86        self.vm.qtest("clock_step 10")
87        self.do_check_event('img0', offset)
88
89        # I/O errors in the same child: only one event is emitted
90        delay = 10
91        for i in range(3):
92            self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
93                                (offset * sector_size, sector_size))
94            self.vm.qtest("clock_step %d" % delay)
95            self.do_check_event(None)
96
97        # Wait enough so the event is finally emitted
98        self.vm.qtest("clock_step %d" % (2 * event_rate))
99        self.do_check_event('img0', offset)
100
101        # I/O errors in the same child: all events are emitted
102        delay = 2 * event_rate
103        for i in range(3):
104            self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
105                                (offset * sector_size, sector_size))
106            self.vm.qtest("clock_step %d" % delay)
107            self.do_check_event('img0', offset)
108
109        # I/O errors in different children: all events are emitted
110        delay = 10
111        for i in range(len(imgs)):
112            self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
113                                ((offset + i) * sector_size, sector_size))
114            self.vm.qtest("clock_step %d" % delay)
115            # In fifo mode only errors in the first child are detected
116            if i > 0 and self.read_pattern == 'fifo':
117                self.do_check_event(None)
118            else:
119                self.do_check_event('img%d' % i, offset + i)
120
121        # I/O errors in different children: all events are emitted
122        delay = 2 * event_rate
123        for i in range(len(imgs)):
124            self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
125                                ((offset + i) * sector_size, sector_size))
126            self.vm.qtest("clock_step %d" % delay)
127            # In fifo mode only errors in the first child are detected
128            if i > 0 and self.read_pattern == 'fifo':
129                self.do_check_event(None)
130            else:
131                self.do_check_event('img%d' % i, offset + i)
132
133        # No more pending events
134        self.do_check_event(None)
135
136class TestFifoQuorumEvents(TestQuorumEvents):
137    read_pattern = 'fifo'
138
139if __name__ == '__main__':
140    iotests.verify_quorum()
141    iotests.main(supported_fmts=["raw"],
142                 supported_protocols=["file"])
143