xref: /openbmc/qemu/tests/qemu-iotests/300 (revision 0e8e77d4)
1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 Red Hat, Inc.
4#
5# Tests for dirty bitmaps migration with node aliases
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21import os
22import random
23import re
24from typing import Dict, List, Optional, Union
25import iotests
26import qemu
27
28BlockBitmapMapping = List[Dict[str, Union[str, List[Dict[str, str]]]]]
29
30assert iotests.sock_dir is not None
31mig_sock = os.path.join(iotests.sock_dir, 'mig_sock')
32
33
34class TestDirtyBitmapMigration(iotests.QMPTestCase):
35    src_node_name: str = ''
36    dst_node_name: str = ''
37    src_bmap_name: str = ''
38    dst_bmap_name: str = ''
39
40    def setUp(self) -> None:
41        self.vm_a = iotests.VM(path_suffix='-a')
42        self.vm_a.add_blockdev(f'node-name={self.src_node_name},'
43                               'driver=null-co')
44        self.vm_a.launch()
45
46        self.vm_b = iotests.VM(path_suffix='-b')
47        self.vm_b.add_blockdev(f'node-name={self.dst_node_name},'
48                               'driver=null-co')
49        self.vm_b.add_incoming(f'unix:{mig_sock}')
50        self.vm_b.launch()
51
52        result = self.vm_a.qmp('block-dirty-bitmap-add',
53                               node=self.src_node_name,
54                               name=self.src_bmap_name)
55        self.assert_qmp(result, 'return', {})
56
57        # Dirty some random megabytes
58        for _ in range(9):
59            mb_ofs = random.randrange(1024)
60            self.vm_a.hmp_qemu_io(self.src_node_name, f'discard {mb_ofs}M 1M')
61
62        result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256',
63                               node=self.src_node_name,
64                               name=self.src_bmap_name)
65        self.bitmap_hash_reference = result['return']['sha256']
66
67        caps = [{'capability': name, 'state': True}
68                for name in ('dirty-bitmaps', 'events')]
69
70        for vm in (self.vm_a, self.vm_b):
71            result = vm.qmp('migrate-set-capabilities', capabilities=caps)
72            self.assert_qmp(result, 'return', {})
73
74    def tearDown(self) -> None:
75        self.vm_a.shutdown()
76        self.vm_b.shutdown()
77        try:
78            os.remove(mig_sock)
79        except OSError:
80            pass
81
82    def check_bitmap(self, bitmap_name_valid: bool) -> None:
83        result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256',
84                               node=self.dst_node_name,
85                               name=self.dst_bmap_name)
86        if bitmap_name_valid:
87            self.assert_qmp(result, 'return/sha256',
88                            self.bitmap_hash_reference)
89        else:
90            self.assert_qmp(result, 'error/desc',
91                            f"Dirty bitmap '{self.dst_bmap_name}' not found")
92
93    def migrate(self, bitmap_name_valid: bool = True,
94                migration_success: bool = True) -> None:
95        result = self.vm_a.qmp('migrate', uri=f'unix:{mig_sock}')
96        self.assert_qmp(result, 'return', {})
97
98        with iotests.Timeout(5, 'Timeout waiting for migration to complete'):
99            self.assertEqual(self.vm_a.wait_migration('postmigrate'),
100                             migration_success)
101            self.assertEqual(self.vm_b.wait_migration('running'),
102                             migration_success)
103
104        if migration_success:
105            self.check_bitmap(bitmap_name_valid)
106
107    def verify_dest_error(self, msg: Optional[str]) -> None:
108        """
109        Check whether the given error message is present in vm_b's log.
110        (vm_b is shut down to do so.)
111        If @msg is None, check that there has not been any error.
112        """
113        self.vm_b.shutdown()
114        if msg is None:
115            self.assertNotIn('qemu-system-', self.vm_b.get_log())
116        else:
117            self.assertIn(msg, self.vm_b.get_log())
118
119    @staticmethod
120    def mapping(node_name: str, node_alias: str,
121                bitmap_name: str, bitmap_alias: str) -> BlockBitmapMapping:
122        return [{
123            'node-name': node_name,
124            'alias': node_alias,
125            'bitmaps': [{
126                'name': bitmap_name,
127                'alias': bitmap_alias
128            }]
129        }]
130
131    def set_mapping(self, vm: iotests.VM, mapping: BlockBitmapMapping,
132                    error: Optional[str] = None) -> None:
133        """
134        Invoke migrate-set-parameters on @vm to set the given @mapping.
135        Check for success if @error is None, or verify the error message
136        if it is not.
137        On success, verify that "info migrate_parameters" on HMP returns
138        our mapping.  (Just to check its formatting code.)
139        """
140        result = vm.qmp('migrate-set-parameters',
141                        block_bitmap_mapping=mapping)
142
143        if error is None:
144            self.assert_qmp(result, 'return', {})
145
146            result = vm.qmp('human-monitor-command',
147                            command_line='info migrate_parameters')
148
149            m = re.search(r'^block-bitmap-mapping:\r?(\n  .*)*\n',
150                          result['return'], flags=re.MULTILINE)
151            hmp_mapping = m.group(0).replace('\r', '') if m else None
152
153            self.assertEqual(hmp_mapping, self.to_hmp_mapping(mapping))
154        else:
155            self.assert_qmp(result, 'error/desc', error)
156
157    @staticmethod
158    def to_hmp_mapping(mapping: BlockBitmapMapping) -> str:
159        result = 'block-bitmap-mapping:\n'
160
161        for node in mapping:
162            result += f"  '{node['node-name']}' -> '{node['alias']}'\n"
163
164            assert isinstance(node['bitmaps'], list)
165            for bitmap in node['bitmaps']:
166                result += f"    '{bitmap['name']}' -> '{bitmap['alias']}'\n"
167
168        return result
169
170
171class TestAliasMigration(TestDirtyBitmapMigration):
172    src_node_name = 'node0'
173    dst_node_name = 'node0'
174    src_bmap_name = 'bmap0'
175    dst_bmap_name = 'bmap0'
176
177    def test_migration_without_alias(self) -> None:
178        self.migrate(self.src_node_name == self.dst_node_name and
179                     self.src_bmap_name == self.dst_bmap_name)
180
181        # Check for error message on the destination
182        if self.src_node_name != self.dst_node_name:
183            self.verify_dest_error(f"Cannot find "
184                                   f"device={self.src_node_name} nor "
185                                   f"node_name={self.src_node_name}")
186        else:
187            self.verify_dest_error(None)
188
189    def test_alias_on_src_migration(self) -> None:
190        mapping = self.mapping(self.src_node_name, self.dst_node_name,
191                               self.src_bmap_name, self.dst_bmap_name)
192
193        self.set_mapping(self.vm_a, mapping)
194        self.migrate()
195        self.verify_dest_error(None)
196
197    def test_alias_on_dst_migration(self) -> None:
198        mapping = self.mapping(self.dst_node_name, self.src_node_name,
199                               self.dst_bmap_name, self.src_bmap_name)
200
201        self.set_mapping(self.vm_b, mapping)
202        self.migrate()
203        self.verify_dest_error(None)
204
205    def test_alias_on_both_migration(self) -> None:
206        src_map = self.mapping(self.src_node_name, 'node-alias',
207                               self.src_bmap_name, 'bmap-alias')
208
209        dst_map = self.mapping(self.dst_node_name, 'node-alias',
210                               self.dst_bmap_name, 'bmap-alias')
211
212        self.set_mapping(self.vm_a, src_map)
213        self.set_mapping(self.vm_b, dst_map)
214        self.migrate()
215        self.verify_dest_error(None)
216
217
218class TestNodeAliasMigration(TestAliasMigration):
219    src_node_name = 'node-src'
220    dst_node_name = 'node-dst'
221
222
223class TestBitmapAliasMigration(TestAliasMigration):
224    src_bmap_name = 'bmap-src'
225    dst_bmap_name = 'bmap-dst'
226
227
228class TestFullAliasMigration(TestAliasMigration):
229    src_node_name = 'node-src'
230    dst_node_name = 'node-dst'
231    src_bmap_name = 'bmap-src'
232    dst_bmap_name = 'bmap-dst'
233
234
235class TestLongBitmapNames(TestAliasMigration):
236    # Giving long bitmap names is OK, as long as there is a short alias for
237    # migration
238    src_bmap_name = 'a' * 512
239    dst_bmap_name = 'b' * 512
240
241    # Skip all tests that do not use the intermediate alias
242    def test_migration_without_alias(self) -> None:
243        pass
244
245    def test_alias_on_src_migration(self) -> None:
246        pass
247
248    def test_alias_on_dst_migration(self) -> None:
249        pass
250
251
252class TestBlockBitmapMappingErrors(TestDirtyBitmapMigration):
253    src_node_name = 'node0'
254    dst_node_name = 'node0'
255    src_bmap_name = 'bmap0'
256    dst_bmap_name = 'bmap0'
257
258    """
259    Note that mapping nodes or bitmaps that do not exist is not an error.
260    """
261
262    def test_non_injective_node_mapping(self) -> None:
263        mapping: BlockBitmapMapping = [
264            {
265                'node-name': 'node0',
266                'alias': 'common-alias',
267                'bitmaps': [{
268                    'name': 'bmap0',
269                    'alias': 'bmap-alias0'
270                }]
271            },
272            {
273                'node-name': 'node1',
274                'alias': 'common-alias',
275                'bitmaps': [{
276                    'name': 'bmap1',
277                    'alias': 'bmap-alias1'
278                }]
279            }
280        ]
281
282        self.set_mapping(self.vm_a, mapping,
283                         "Invalid mapping given for block-bitmap-mapping: "
284                         "The node alias 'common-alias' is used twice")
285
286    def test_non_injective_bitmap_mapping(self) -> None:
287        mapping: BlockBitmapMapping = [{
288            'node-name': 'node0',
289            'alias': 'node-alias0',
290            'bitmaps': [
291                {
292                    'name': 'bmap0',
293                    'alias': 'common-alias'
294                },
295                {
296                    'name': 'bmap1',
297                    'alias': 'common-alias'
298                }
299            ]
300        }]
301
302        self.set_mapping(self.vm_a, mapping,
303                         "Invalid mapping given for block-bitmap-mapping: "
304                         "The bitmap alias 'node-alias0'/'common-alias' is "
305                         "used twice")
306
307    def test_ambiguous_node_mapping(self) -> None:
308        mapping: BlockBitmapMapping = [
309            {
310                'node-name': 'node0',
311                'alias': 'node-alias0',
312                'bitmaps': [{
313                    'name': 'bmap0',
314                    'alias': 'bmap-alias0'
315                }]
316            },
317            {
318                'node-name': 'node0',
319                'alias': 'node-alias1',
320                'bitmaps': [{
321                    'name': 'bmap0',
322                    'alias': 'bmap-alias0'
323                }]
324            }
325        ]
326
327        self.set_mapping(self.vm_a, mapping,
328                         "Invalid mapping given for block-bitmap-mapping: "
329                         "The node name 'node0' is mapped twice")
330
331    def test_ambiguous_bitmap_mapping(self) -> None:
332        mapping: BlockBitmapMapping = [{
333            'node-name': 'node0',
334            'alias': 'node-alias0',
335            'bitmaps': [
336                {
337                    'name': 'bmap0',
338                    'alias': 'bmap-alias0'
339                },
340                {
341                    'name': 'bmap0',
342                    'alias': 'bmap-alias1'
343                }
344            ]
345        }]
346
347        self.set_mapping(self.vm_a, mapping,
348                         "Invalid mapping given for block-bitmap-mapping: "
349                         "The bitmap 'node0'/'bmap0' is mapped twice")
350
351    def test_migratee_node_is_not_mapped_on_src(self) -> None:
352        self.set_mapping(self.vm_a, [])
353        # Should just ignore all bitmaps on unmapped nodes
354        self.migrate(False)
355        self.verify_dest_error(None)
356
357    def test_migratee_node_is_not_mapped_on_dst(self) -> None:
358        self.set_mapping(self.vm_b, [])
359        self.migrate(False)
360        self.verify_dest_error(f"Unknown node alias '{self.src_node_name}'")
361
362    def test_migratee_bitmap_is_not_mapped_on_src(self) -> None:
363        mapping: BlockBitmapMapping = [{
364            'node-name': self.src_node_name,
365            'alias': self.dst_node_name,
366            'bitmaps': []
367        }]
368
369        self.set_mapping(self.vm_a, mapping)
370        # Should just ignore all unmapped bitmaps
371        self.migrate(False)
372        self.verify_dest_error(None)
373
374    def test_migratee_bitmap_is_not_mapped_on_dst(self) -> None:
375        mapping: BlockBitmapMapping = [{
376            'node-name': self.dst_node_name,
377            'alias': self.src_node_name,
378            'bitmaps': []
379        }]
380
381        self.set_mapping(self.vm_b, mapping)
382        self.migrate(False)
383        self.verify_dest_error(f"Unknown bitmap alias "
384                               f"'{self.src_bmap_name}' "
385                               f"on node '{self.dst_node_name}' "
386                               f"(alias '{self.src_node_name}')")
387
388    def test_unused_mapping_on_dst(self) -> None:
389        # Let the source not send any bitmaps
390        self.set_mapping(self.vm_a, [])
391
392        # Establish some mapping on the destination
393        self.set_mapping(self.vm_b, [])
394
395        # The fact that there is a mapping on B without any bitmaps
396        # being received should be fine, not fatal
397        self.migrate(False)
398        self.verify_dest_error(None)
399
400    def test_non_wellformed_node_alias(self) -> None:
401        alias = '123-foo'
402
403        mapping: BlockBitmapMapping = [{
404            'node-name': self.src_node_name,
405            'alias': alias,
406            'bitmaps': []
407        }]
408
409        self.set_mapping(self.vm_a, mapping,
410                         f"Invalid mapping given for block-bitmap-mapping: "
411                         f"The node alias '{alias}' is not well-formed")
412
413    def test_node_alias_too_long(self) -> None:
414        alias = 'a' * 256
415
416        mapping: BlockBitmapMapping = [{
417            'node-name': self.src_node_name,
418            'alias': alias,
419            'bitmaps': []
420        }]
421
422        self.set_mapping(self.vm_a, mapping,
423                         f"Invalid mapping given for block-bitmap-mapping: "
424                         f"The node alias '{alias}' is longer than 255 bytes")
425
426    def test_bitmap_alias_too_long(self) -> None:
427        alias = 'a' * 256
428
429        mapping = self.mapping(self.src_node_name, self.dst_node_name,
430                               self.src_bmap_name, alias)
431
432        self.set_mapping(self.vm_a, mapping,
433                         f"Invalid mapping given for block-bitmap-mapping: "
434                         f"The bitmap alias '{alias}' is longer than 255 "
435                         f"bytes")
436
437    def test_bitmap_name_too_long(self) -> None:
438        name = 'a' * 256
439
440        result = self.vm_a.qmp('block-dirty-bitmap-add',
441                               node=self.src_node_name,
442                               name=name)
443        self.assert_qmp(result, 'return', {})
444
445        self.migrate(False, False)
446
447        # Check for the error in the source's log
448        self.vm_a.shutdown()
449        self.assertIn(f"Cannot migrate bitmap '{name}' on node "
450                      f"'{self.src_node_name}': Name is longer than 255 bytes",
451                      self.vm_a.get_log())
452
453        # Expect abnormal shutdown of the destination VM because of
454        # the failed migration
455        try:
456            self.vm_b.shutdown()
457        except qemu.machine.AbnormalShutdown:
458            pass
459
460    def test_aliased_bitmap_name_too_long(self) -> None:
461        # Longer than the maximum for bitmap names
462        self.dst_bmap_name = 'a' * 1024
463
464        mapping = self.mapping(self.dst_node_name, self.src_node_name,
465                               self.dst_bmap_name, self.src_bmap_name)
466
467        # We would have to create this bitmap during migration, and
468        # that would fail, because the name is too long.  Better to
469        # catch it early.
470        self.set_mapping(self.vm_b, mapping,
471                         f"Invalid mapping given for block-bitmap-mapping: "
472                         f"The bitmap name '{self.dst_bmap_name}' is longer "
473                         f"than 1023 bytes")
474
475    def test_node_name_too_long(self) -> None:
476        # Longer than the maximum for node names
477        self.dst_node_name = 'a' * 32
478
479        mapping = self.mapping(self.dst_node_name, self.src_node_name,
480                               self.dst_bmap_name, self.src_bmap_name)
481
482        # During migration, this would appear simply as a node that
483        # cannot be found.  Still better to catch impossible node
484        # names early (similar to test_non_wellformed_node_alias).
485        self.set_mapping(self.vm_b, mapping,
486                         f"Invalid mapping given for block-bitmap-mapping: "
487                         f"The node name '{self.dst_node_name}' is longer "
488                         f"than 31 bytes")
489
490
491class TestCrossAliasMigration(TestDirtyBitmapMigration):
492    """
493    Swap aliases, both to see that qemu does not get confused, and
494    that we can migrate multiple things at once.
495
496    So we migrate this:
497      node-a.bmap-a -> node-b.bmap-b
498      node-a.bmap-b -> node-b.bmap-a
499      node-b.bmap-a -> node-a.bmap-b
500      node-b.bmap-b -> node-a.bmap-a
501    """
502
503    src_node_name = 'node-a'
504    dst_node_name = 'node-b'
505    src_bmap_name = 'bmap-a'
506    dst_bmap_name = 'bmap-b'
507
508    def setUp(self) -> None:
509        TestDirtyBitmapMigration.setUp(self)
510
511        # Now create another block device and let both have two bitmaps each
512        result = self.vm_a.qmp('blockdev-add',
513                               node_name='node-b', driver='null-co')
514        self.assert_qmp(result, 'return', {})
515
516        result = self.vm_b.qmp('blockdev-add',
517                               node_name='node-a', driver='null-co')
518        self.assert_qmp(result, 'return', {})
519
520        bmaps_to_add = (('node-a', 'bmap-b'),
521                        ('node-b', 'bmap-a'),
522                        ('node-b', 'bmap-b'))
523
524        for (node, bmap) in bmaps_to_add:
525            result = self.vm_a.qmp('block-dirty-bitmap-add',
526                                   node=node, name=bmap)
527            self.assert_qmp(result, 'return', {})
528
529    @staticmethod
530    def cross_mapping() -> BlockBitmapMapping:
531        return [
532            {
533                'node-name': 'node-a',
534                'alias': 'node-b',
535                'bitmaps': [
536                    {
537                        'name': 'bmap-a',
538                        'alias': 'bmap-b'
539                    },
540                    {
541                        'name': 'bmap-b',
542                        'alias': 'bmap-a'
543                    }
544                ]
545            },
546            {
547                'node-name': 'node-b',
548                'alias': 'node-a',
549                'bitmaps': [
550                    {
551                        'name': 'bmap-b',
552                        'alias': 'bmap-a'
553                    },
554                    {
555                        'name': 'bmap-a',
556                        'alias': 'bmap-b'
557                    }
558                ]
559            }
560        ]
561
562    def verify_dest_has_all_bitmaps(self) -> None:
563        bitmaps = self.vm_b.query_bitmaps()
564
565        # Extract and sort bitmap names
566        for node in bitmaps:
567            bitmaps[node] = sorted((bmap['name'] for bmap in bitmaps[node]))
568
569        self.assertEqual(bitmaps,
570                         {'node-a': ['bmap-a', 'bmap-b'],
571                          'node-b': ['bmap-a', 'bmap-b']})
572
573    def test_alias_on_src(self) -> None:
574        self.set_mapping(self.vm_a, self.cross_mapping())
575
576        # Checks that node-a.bmap-a was migrated to node-b.bmap-b, and
577        # that is enough
578        self.migrate()
579        self.verify_dest_has_all_bitmaps()
580        self.verify_dest_error(None)
581
582    def test_alias_on_dst(self) -> None:
583        self.set_mapping(self.vm_b, self.cross_mapping())
584
585        # Checks that node-a.bmap-a was migrated to node-b.bmap-b, and
586        # that is enough
587        self.migrate()
588        self.verify_dest_has_all_bitmaps()
589        self.verify_dest_error(None)
590
591
592if __name__ == '__main__':
593    iotests.main(supported_protocols=['file'])
594