]> Git Repo - qemu.git/blob - tests/acceptance/virtio_version.py
contrib/elf2dmp: fix printf format
[qemu.git] / tests / acceptance / virtio_version.py
1 """
2 Check compatibility of virtio device types
3 """
4 # Copyright (c) 2018 Red Hat, Inc.
5 #
6 # Author:
7 #  Eduardo Habkost <[email protected]>
8 #
9 # This work is licensed under the terms of the GNU GPL, version 2 or
10 # later.  See the COPYING file in the top-level directory.
11 import sys
12 import os
13
14 sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "scripts"))
15 from qemu import QEMUMachine
16 from avocado_qemu import Test
17
18 # Virtio Device IDs:
19 VIRTIO_NET = 1
20 VIRTIO_BLOCK = 2
21 VIRTIO_CONSOLE = 3
22 VIRTIO_RNG = 4
23 VIRTIO_BALLOON = 5
24 VIRTIO_RPMSG = 7
25 VIRTIO_SCSI = 8
26 VIRTIO_9P = 9
27 VIRTIO_RPROC_SERIAL = 11
28 VIRTIO_CAIF = 12
29 VIRTIO_GPU = 16
30 VIRTIO_INPUT = 18
31 VIRTIO_VSOCK = 19
32 VIRTIO_CRYPTO = 20
33
34 PCI_VENDOR_ID_REDHAT_QUMRANET = 0x1af4
35
36 # Device IDs for legacy/transitional devices:
37 PCI_LEGACY_DEVICE_IDS = {
38     VIRTIO_NET:     0x1000,
39     VIRTIO_BLOCK:   0x1001,
40     VIRTIO_BALLOON: 0x1002,
41     VIRTIO_CONSOLE: 0x1003,
42     VIRTIO_SCSI:    0x1004,
43     VIRTIO_RNG:     0x1005,
44     VIRTIO_9P:      0x1009,
45     VIRTIO_VSOCK:   0x1012,
46 }
47
48 def pci_modern_device_id(virtio_devid):
49     return virtio_devid + 0x1040
50
51 def devtype_implements(vm, devtype, implements):
52     return devtype in [d['name'] for d in vm.command('qom-list-types', implements=implements)]
53
54 def get_pci_interfaces(vm, devtype):
55     interfaces = ('pci-express-device', 'conventional-pci-device')
56     return [i for i in interfaces if devtype_implements(vm, devtype, i)]
57
58 class VirtioVersionCheck(Test):
59     """
60     Check if virtio-version-specific device types result in the
61     same device tree created by `disable-modern` and
62     `disable-legacy`.
63
64     :avocado: enable
65     :avocado: tags=x86_64
66     """
67
68     # just in case there are failures, show larger diff:
69     maxDiff = 4096
70
71     def run_device(self, devtype, opts=None, machine='pc'):
72         """
73         Run QEMU with `-device DEVTYPE`, return device info from `query-pci`
74         """
75         with QEMUMachine(self.qemu_bin) as vm:
76             vm.set_machine(machine)
77             if opts:
78                 devtype += ',' + opts
79             vm.add_args('-device', '%s,id=devfortest' % (devtype))
80             vm.add_args('-S')
81             vm.launch()
82
83             pcibuses = vm.command('query-pci')
84             alldevs = [dev for bus in pcibuses for dev in bus['devices']]
85             devfortest = [dev for dev in alldevs
86                           if dev['qdev_id'] == 'devfortest']
87             return devfortest[0], get_pci_interfaces(vm, devtype)
88
89
90     def assert_devids(self, dev, devid, non_transitional=False):
91         self.assertEqual(dev['id']['vendor'], PCI_VENDOR_ID_REDHAT_QUMRANET)
92         self.assertEqual(dev['id']['device'], devid)
93         if non_transitional:
94             self.assertTrue(0x1040 <= dev['id']['device'] <= 0x107f)
95             self.assertGreaterEqual(dev['id']['subsystem'], 0x40)
96
97     def check_all_variants(self, qemu_devtype, virtio_devid):
98         """Check if a virtio device type and its variants behave as expected"""
99         # Force modern mode:
100         dev_modern, _ = self.run_device(qemu_devtype,
101                                        'disable-modern=off,disable-legacy=on')
102         self.assert_devids(dev_modern, pci_modern_device_id(virtio_devid),
103                            non_transitional=True)
104
105         # <prefix>-non-transitional device types should be 100% equivalent to
106         # <prefix>,disable-modern=off,disable-legacy=on
107         dev_1_0, nt_ifaces = self.run_device('%s-non-transitional' % (qemu_devtype))
108         self.assertEqual(dev_modern, dev_1_0)
109
110         # Force transitional mode:
111         dev_trans, _ = self.run_device(qemu_devtype,
112                                       'disable-modern=off,disable-legacy=off')
113         self.assert_devids(dev_trans, PCI_LEGACY_DEVICE_IDS[virtio_devid])
114
115         # Force legacy mode:
116         dev_legacy, _ = self.run_device(qemu_devtype,
117                                        'disable-modern=on,disable-legacy=off')
118         self.assert_devids(dev_legacy, PCI_LEGACY_DEVICE_IDS[virtio_devid])
119
120         # No options: default to transitional on PC machine-type:
121         no_opts_pc, generic_ifaces = self.run_device(qemu_devtype)
122         self.assertEqual(dev_trans, no_opts_pc)
123
124         #TODO: check if plugging on a PCI Express bus will make the
125         #      device non-transitional
126         #no_opts_q35 = self.run_device(qemu_devtype, machine='q35')
127         #self.assertEqual(dev_modern, no_opts_q35)
128
129         # <prefix>-transitional device types should be 100% equivalent to
130         # <prefix>,disable-modern=off,disable-legacy=off
131         dev_trans, trans_ifaces = self.run_device('%s-transitional' % (qemu_devtype))
132         self.assertEqual(dev_trans, dev_trans)
133
134         # ensure the interface information is correct:
135         self.assertIn('conventional-pci-device', generic_ifaces)
136         self.assertIn('pci-express-device', generic_ifaces)
137
138         self.assertIn('conventional-pci-device', nt_ifaces)
139         self.assertIn('pci-express-device', nt_ifaces)
140
141         self.assertIn('conventional-pci-device', trans_ifaces)
142         self.assertNotIn('pci-express-device', trans_ifaces)
143
144
145     def test_conventional_devs(self):
146         self.check_all_variants('virtio-net-pci', VIRTIO_NET)
147         # virtio-blk requires 'driver' parameter
148         #self.check_all_variants('virtio-blk-pci', VIRTIO_BLOCK)
149         self.check_all_variants('virtio-serial-pci', VIRTIO_CONSOLE)
150         self.check_all_variants('virtio-rng-pci', VIRTIO_RNG)
151         self.check_all_variants('virtio-balloon-pci', VIRTIO_BALLOON)
152         self.check_all_variants('virtio-scsi-pci', VIRTIO_SCSI)
153         # virtio-9p requires 'fsdev' parameter
154         #self.check_all_variants('virtio-9p-pci', VIRTIO_9P)
155
156     def check_modern_only(self, qemu_devtype, virtio_devid):
157         """Check if a modern-only virtio device type behaves as expected"""
158         # Force modern mode:
159         dev_modern, _ = self.run_device(qemu_devtype,
160                                        'disable-modern=off,disable-legacy=on')
161         self.assert_devids(dev_modern, pci_modern_device_id(virtio_devid),
162                            non_transitional=True)
163
164         # No options: should be modern anyway
165         dev_no_opts, ifaces = self.run_device(qemu_devtype)
166         self.assertEqual(dev_modern, dev_no_opts)
167
168         self.assertIn('conventional-pci-device', ifaces)
169         self.assertIn('pci-express-device', ifaces)
170
171     def test_modern_only_devs(self):
172         self.check_modern_only('virtio-vga', VIRTIO_GPU)
173         self.check_modern_only('virtio-gpu-pci', VIRTIO_GPU)
174         self.check_modern_only('virtio-mouse-pci', VIRTIO_INPUT)
175         self.check_modern_only('virtio-tablet-pci', VIRTIO_INPUT)
176         self.check_modern_only('virtio-keyboard-pci', VIRTIO_INPUT)
This page took 0.035455 seconds and 4 git commands to generate.