| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # INTEL_IOMMU Functional tests |
| 4 | # |
| 5 | # Copyright (c) 2021 Red Hat, Inc. |
| 6 | # |
| 7 | # Author: |
| 8 | # Eric Auger <eric.auger@redhat.com> |
| 9 | # |
| 10 | # This work is licensed under the terms of the GNU GPL, version 2 or |
| 11 | # later. See the COPYING file in the top-level directory. |
| 12 | |
| 13 | from qemu_test import LinuxKernelTest, Asset, exec_command_and_wait_for_pattern |
| 14 | |
| 15 | |
| 16 | class IntelIOMMU(LinuxKernelTest): |
| 17 | |
| 18 | ASSET_KERNEL = Asset( |
| 19 | ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases' |
| 20 | '/31/Server/x86_64/os/images/pxeboot/vmlinuz'), |
| 21 | 'd4738d03dbbe083ca610d0821d0a8f1488bebbdccef54ce33e3adb35fda00129') |
| 22 | |
| 23 | ASSET_INITRD = Asset( |
| 24 | ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases' |
| 25 | '/31/Server/x86_64/os/images/pxeboot/initrd.img'), |
| 26 | '277cd6c7adf77c7e63d73bbb2cded8ef9e2d3a2f100000e92ff1f8396513cd8b') |
| 27 | |
| 28 | ASSET_DISKIMAGE = Asset( |
| 29 | ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases' |
| 30 | '/31/Cloud/x86_64/images/Fedora-Cloud-Base-31-1.9.x86_64.qcow2'), |
| 31 | 'e3c1b309d9203604922d6e255c2c5d098a309c2d46215d8fc026954f3c5c27a0') |
| 32 | |
| 33 | DEFAULT_KERNEL_PARAMS = ('root=/dev/vda1 console=ttyS0 net.ifnames=0 ' |
| 34 | 'quiet rd.rescue ') |
| 35 | GUEST_PORT = 8080 |
| 36 | IOMMU_ADDON = ',iommu_platform=on,disable-modern=off,disable-legacy=on' |
| 37 | kernel_path = None |
| 38 | initrd_path = None |
| 39 | kernel_params = None |
| 40 | |
| 41 | def add_common_args(self, path): |
| 42 | self.vm.add_args('-drive', f'file={path},if=none,id=drv0,snapshot=on') |
| 43 | self.vm.add_args('-device', 'virtio-blk-pci,bus=pcie.0,' + |
| 44 | 'drive=drv0,id=virtio-disk0,bootindex=1,' |
| 45 | 'werror=stop,rerror=stop' + self.IOMMU_ADDON) |
| 46 | self.vm.add_args('-device', 'virtio-gpu-pci' + self.IOMMU_ADDON) |
| 47 | |
| 48 | self.vm.add_args('-netdev', |
| 49 | 'user,id=n1,hostfwd=tcp:127.0.0.1:0-:%d' % |
| 50 | self.GUEST_PORT) |
| 51 | self.vm.add_args('-device', |
| 52 | 'virtio-net-pci,netdev=n1' + self.IOMMU_ADDON) |
| 53 | |
| 54 | self.vm.add_args('-device', 'virtio-rng-pci,rng=rng0') |
| 55 | self.vm.add_args('-object', |
| 56 | 'rng-random,id=rng0,filename=/dev/urandom') |
| 57 | self.vm.add_args("-m", "1G") |
| 58 | self.vm.add_args("-accel", "kvm") |
| 59 | |
| 60 | def common_vm_setup(self): |
| 61 | self.set_machine('q35') |
| 62 | self.require_accelerator("kvm") |
| 63 | self.require_netdev('user') |
| 64 | |
| 65 | self.kernel_path = self.ASSET_KERNEL.fetch() |
| 66 | self.initrd_path = self.ASSET_INITRD.fetch() |
| 67 | image_path = self.ASSET_DISKIMAGE.fetch() |
| 68 | self.add_common_args(image_path) |
| 69 | self.kernel_params = self.DEFAULT_KERNEL_PARAMS |
| 70 | |
| 71 | def run_and_check(self): |
| 72 | if self.kernel_path: |
| 73 | self.vm.add_args('-kernel', self.kernel_path, |
| 74 | '-append', self.kernel_params, |
| 75 | '-initrd', self.initrd_path) |
| 76 | self.vm.set_console() |
| 77 | self.vm.launch() |
| 78 | self.wait_for_console_pattern('Entering emergency mode.') |
| 79 | prompt = '# ' |
| 80 | self.wait_for_console_pattern(prompt) |
| 81 | |
| 82 | # Copy a file (checked later), umount afterwards to drop disk cache: |
| 83 | exec_command_and_wait_for_pattern(self, 'mount /dev/vda1 /sysroot', |
| 84 | prompt) |
| 85 | filename = '/boot/initramfs-5.3.7-301.fc31.x86_64.img' |
| 86 | exec_command_and_wait_for_pattern(self, (f'cp /sysroot{filename}' |
| 87 | ' /sysroot/root/data'), |
| 88 | prompt) |
| 89 | exec_command_and_wait_for_pattern(self, 'umount /sysroot', prompt) |
| 90 | |
| 91 | # Switch from initrd to the cloud image filesystem: |
| 92 | exec_command_and_wait_for_pattern(self, 'mount /dev/vda1 /sysroot', |
| 93 | prompt) |
| 94 | exec_command_and_wait_for_pattern(self, |
| 95 | ('for d in dev proc sys run ; do ' |
| 96 | 'mount -o bind /$d /sysroot/$d ; done'), prompt) |
| 97 | exec_command_and_wait_for_pattern(self, 'chroot /sysroot', prompt) |
| 98 | |
| 99 | # Checking for IOMMU enablement: |
| 100 | self.log.info("Checking whether IOMMU has been enabled...") |
| 101 | exec_command_and_wait_for_pattern(self, 'cat /proc/cmdline', |
| 102 | 'intel_iommu=on') |
| 103 | self.wait_for_console_pattern(prompt) |
| 104 | exec_command_and_wait_for_pattern(self, 'dmesg | grep DMAR:', |
| 105 | 'IOMMU enabled') |
| 106 | self.wait_for_console_pattern(prompt) |
| 107 | exec_command_and_wait_for_pattern(self, |
| 108 | 'find /sys/kernel/iommu_groups/ -type l', |
| 109 | 'devices/0000:00:') |
| 110 | self.wait_for_console_pattern(prompt) |
| 111 | |
| 112 | # Check hard disk device via sha256sum: |
| 113 | self.log.info("Checking hard disk...") |
| 114 | hashsum = '0dc7472f879be70b2f3daae279e3ae47175ffe249691e7d97f47222b65b8a720' |
| 115 | exec_command_and_wait_for_pattern(self, 'sha256sum ' + filename, |
| 116 | hashsum) |
| 117 | self.wait_for_console_pattern(prompt) |
| 118 | exec_command_and_wait_for_pattern(self, 'sha256sum /root/data', |
| 119 | hashsum) |
| 120 | self.wait_for_console_pattern(prompt) |
| 121 | |
| 122 | # Check virtio-net via HTTP: |
| 123 | exec_command_and_wait_for_pattern(self, 'dhclient eth0', prompt) |
| 124 | self.check_http_download(filename, hashsum, self.GUEST_PORT) |
| 125 | |
| 126 | def test_intel_iommu(self): |
| 127 | self.common_vm_setup() |
| 128 | self.vm.add_args('-device', 'intel-iommu,intremap=on') |
| 129 | self.vm.add_args('-machine', 'kernel_irqchip=split') |
| 130 | self.kernel_params += 'intel_iommu=on' |
| 131 | self.run_and_check() |
| 132 | |
| 133 | def test_intel_iommu_strict(self): |
| 134 | self.common_vm_setup() |
| 135 | self.vm.add_args('-device', 'intel-iommu,intremap=on') |
| 136 | self.vm.add_args('-machine', 'kernel_irqchip=split') |
| 137 | self.kernel_params += 'intel_iommu=on,strict' |
| 138 | self.run_and_check() |
| 139 | |
| 140 | def test_intel_iommu_strict_cm(self): |
| 141 | self.common_vm_setup() |
| 142 | self.vm.add_args('-device', 'intel-iommu,intremap=on,caching-mode=on') |
| 143 | self.vm.add_args('-machine', 'kernel_irqchip=split') |
| 144 | self.kernel_params += 'intel_iommu=on,strict' |
| 145 | self.run_and_check() |
| 146 | |
| 147 | def test_intel_iommu_pt(self): |
| 148 | self.common_vm_setup() |
| 149 | self.vm.add_args('-device', 'intel-iommu,intremap=on') |
| 150 | self.vm.add_args('-machine', 'kernel_irqchip=split') |
| 151 | self.kernel_params += 'intel_iommu=on iommu=pt' |
| 152 | self.run_and_check() |
| 153 | |
| 154 | if __name__ == '__main__': |
| 155 | LinuxKernelTest.main() |