cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

virtio_check_params.py (5070B)


      1#
      2# Test virtio-scsi and virtio-blk queue settings for all machine types
      3#
      4# Copyright (c) 2019 Virtuozzo International GmbH
      5#
      6# This program is free software; you can redistribute it and/or modify
      7# it under the terms of the GNU General Public License as published by
      8# the Free Software Foundation; either version 2 of the License, or
      9# (at your option) any later version.
     10#
     11# This program is distributed in the hope that it will be useful,
     12# but WITHOUT ANY WARRANTY; without even the implied warranty of
     13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14# GNU General Public License for more details.
     15#
     16# You should have received a copy of the GNU General Public License
     17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
     18#
     19
     20import sys
     21import os
     22import re
     23import logging
     24
     25sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
     26from qemu.machine import QEMUMachine
     27from avocado_qemu import Test
     28from avocado import skip
     29
     30#list of machine types and virtqueue properties to test
     31VIRTIO_SCSI_PROPS = {'seg_max_adjust': 'seg_max_adjust'}
     32VIRTIO_BLK_PROPS = {'seg_max_adjust': 'seg-max-adjust'}
     33
     34DEV_TYPES = {'virtio-scsi-pci': VIRTIO_SCSI_PROPS,
     35             'virtio-blk-pci': VIRTIO_BLK_PROPS}
     36
     37VM_DEV_PARAMS = {'virtio-scsi-pci': ['-device', 'virtio-scsi-pci,id=scsi0'],
     38                 'virtio-blk-pci': ['-device',
     39                                    'virtio-blk-pci,id=scsi0,drive=drive0',
     40                                    '-drive',
     41                                    'driver=null-co,id=drive0,if=none']}
     42
     43
     44class VirtioMaxSegSettingsCheck(Test):
     45    @staticmethod
     46    def make_pattern(props):
     47        pattern_items = ['{0} = \w+'.format(prop) for prop in props]
     48        return '|'.join(pattern_items)
     49
     50    def query_virtqueue(self, vm, dev_type_name):
     51        query_ok = False
     52        error = None
     53        props = None
     54
     55        output = vm.command('human-monitor-command',
     56                            command_line = 'info qtree')
     57        props_list = DEV_TYPES[dev_type_name].values();
     58        pattern = self.make_pattern(props_list)
     59        res = re.findall(pattern, output)
     60
     61        if len(res) != len(props_list):
     62            props_list = set(props_list)
     63            res = set(res)
     64            not_found = props_list.difference(res)
     65            not_found = ', '.join(not_found)
     66            error = '({0}): The following properties not found: {1}'\
     67                     .format(dev_type_name, not_found)
     68        else:
     69            query_ok = True
     70            props = dict()
     71            for prop in res:
     72                p = prop.split(' = ')
     73                props[p[0]] = p[1]
     74        return query_ok, props, error
     75
     76    def check_mt(self, mt, dev_type_name):
     77        mt['device'] = dev_type_name # Only for the debug() call.
     78        logger = logging.getLogger('machine')
     79        logger.debug(mt)
     80        with QEMUMachine(self.qemu_bin) as vm:
     81            vm.set_machine(mt["name"])
     82            vm.add_args('-nodefaults')
     83            for s in VM_DEV_PARAMS[dev_type_name]:
     84                vm.add_args(s)
     85            try:
     86                vm.launch()
     87                query_ok, props, error = self.query_virtqueue(vm, dev_type_name)
     88            except:
     89                query_ok = False
     90                error = sys.exc_info()[0]
     91
     92        if not query_ok:
     93            self.fail('machine type {0}: {1}'.format(mt['name'], error))
     94
     95        for prop_name, prop_val in props.items():
     96            expected_val = mt[prop_name]
     97            self.assertEqual(expected_val, prop_val)
     98
     99    @staticmethod
    100    def seg_max_adjust_enabled(mt):
    101        # machine types >= 5.0 should have seg_max_adjust = true
    102        # others seg_max_adjust = false
    103        mt = mt.split("-")
    104
    105        # machine types with one line name and name like pc-x.x
    106        if len(mt) <= 2:
    107            return False
    108
    109        # machine types like pc-<chip_name>-x.x[.x]
    110        ver = mt[2]
    111        ver = ver.split(".");
    112
    113        # versions >= 5.0 goes with seg_max_adjust enabled
    114        major = int(ver[0])
    115
    116        if major >= 5:
    117            return True
    118        return False
    119
    120    @skip("break multi-arch CI")
    121    def test_machine_types(self):
    122        # collect all machine types except 'none', 'isapc', 'microvm'
    123        with QEMUMachine(self.qemu_bin) as vm:
    124            vm.launch()
    125            machines = [m['name'] for m in vm.command('query-machines')]
    126            vm.shutdown()
    127        machines.remove('none')
    128        machines.remove('isapc')
    129        machines.remove('microvm')
    130
    131        for dev_type in DEV_TYPES:
    132            # create the list of machine types and their parameters.
    133            mtypes = list()
    134            for m in machines:
    135                if self.seg_max_adjust_enabled(m):
    136                    enabled = 'true'
    137                else:
    138                    enabled = 'false'
    139                mtypes.append({'name': m,
    140                               DEV_TYPES[dev_type]['seg_max_adjust']: enabled})
    141
    142            # test each machine type for a device type
    143            for mt in mtypes:
    144                self.check_mt(mt, dev_type)