KVM PCI Passthrough and Omni-Path » History » Version 19
Brian Smith, 04/20/2018 05:15 PM
| 1 | 16 | Brian Smith | # KVM PCI Passthrough and Omni-Path |
|---|---|---|---|
| 2 | 1 | Brian Smith | |
| 3 | 16 | Brian Smith | A KVM guest can use OPA hardware when configured for PCI passthrough. This document is OPA and Debian-centric, but the concepts should apply to other Linux host operating systems and PCI devices. |
| 4 | 1 | Brian Smith | |
| 5 | ## BIOS Settings |
||
| 6 | |||
| 7 | 1. Intel VT must be enabled. |
||
| 8 | 4 | Brian Smith | 2. Integrated IO / IntelVT must be enabled. |
| 9 | 1 | Brian Smith | |
| 10 | ## Kernel Command Line |
||
| 11 | |||
| 12 | 9 | Brian Smith | Add this to the host's kernel command line and reboot the host: |
| 13 | |||
| 14 | 10 | Brian Smith | ``` |
| 15 | intel_iommu=on iommu=pt |
||
| 16 | ``` |
||
| 17 | 1 | Brian Smith | |
| 18 | 5 | Brian Smith | When configured properly, ```/sys/kernel/iommu_groups/``` will contain many subdirectories. If that path is empty, IOMMU is not working. |
| 19 | |||
| 20 | 1 | Brian Smith | ## Install KVM |
| 21 | |||
| 22 | ``` |
||
| 23 | 17 | Brian Smith | $ sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system virtinst libosinfo-bin |
| 24 | 1 | Brian Smith | $ sudo adduser YOU libvirt |
| 25 | $ sudo adduser YOU libvirt-qemu |
||
| 26 | $ sudo adduser YOU kvm |
||
| 27 | ``` |
||
| 28 | |||
| 29 | ## Disable hfi1 on host |
||
| 30 | |||
| 31 | The hfi1 driver must not be loaded on the host machine, in order to use PCI passthrough. In /etc/modprobe.d/hfi1.conf: |
||
| 32 | |||
| 33 | ``` |
||
| 34 | blacklist hfi1 |
||
| 35 | ``` |
||
| 36 | |||
| 37 | Also, there is no reason to have IFS installed on the host. The host machine should have no OPA functionality enabled. |
||
| 38 | |||
| 39 | ## Configure PCI Passthrough |
||
| 40 | |||
| 41 | The hfi1 device must be setup for PCI passthrough. Find the device's port in the output of lspci: |
||
| 42 | |||
| 43 | ``` |
||
| 44 | 19 | Brian Smith | $ lspci -vnn | grep Omni | cut -f1 '-d ' |
| 45 | 1 | Brian Smith | ``` |
| 46 | |||
| 47 | For the scripts below, prepend the port with 0000:, like "0000:80:02.0". |
||
| 48 | |||
| 49 | |||
| 50 | Use the following script, replace PCI_PORT with the port of the hfi1: |
||
| 51 | |||
| 52 | ``` |
||
| 53 | 15 | Brian Smith | |
| 54 | 1 | Brian Smith | #!/bin/bash |
| 55 | |||
| 56 | PCI_PORT=0000:80:02.0 |
||
| 57 | DEV_VENDOR=8086 |
||
| 58 | DEV_MODEL=24f0 |
||
| 59 | |||
| 60 | rmmod vfio_pci |
||
| 61 | 15 | Brian Smith | rmmod vfio |
| 62 | 1 | Brian Smith | echo "$PCI_PORT" > /sys/bus/pci/devices/$PCI_PORT/driver/unbind |
| 63 | modprobe vfio |
||
| 64 | modprobe vfio_pci |
||
| 65 | echo $DEV_VENDOR $DEV_MODEL > /sys/bus/pci/drivers/vfio-pci/new_id |
||
| 66 | ``` |
||
| 67 | |||
| 68 | 2 | Brian Smith | |
| 69 | 1 | Brian Smith | ## Create Guest |
| 70 | |||
| 71 | 16 | Brian Smith | While it is possible to manage guests for an unprivileged user, they get a non-functional network setup in the default config. |
| 72 | 3 | Brian Smith | |
| 73 | **Use virsh as root.** |
||
| 74 | 1 | Brian Smith | |
| 75 | ``` |
||
| 76 | $ systemctl start libvirtd |
||
| 77 | 8 | Brian Smith | $ virt-install --virt-type kvm --name GUEST_NAME \ |
| 78 | 1 | Brian Smith | --vcpus=4 --virt-type kvm --cdrom $HOME/kvm-guest/debian-8.7.0-amd64-DVD-1.iso \ |
| 79 | -v --os-variant debian8 \ |
||
| 80 | 8 | Brian Smith | --disk path=PATH_TO_CREATE_DISK,size=16 --memory 4096 --graphics vnc |
| 81 | 1 | Brian Smith | ``` |
| 82 | |||
| 83 | Connect a VNC client to a tunneled connection to the host. |
||
| 84 | |||
| 85 | From the workstation: |
||
| 86 | |||
| 87 | ``` |
||
| 88 | 14 | Brian Smith | $ ssh -L5910:localhost:5900 YOU@HOST |
| 89 | 1 | Brian Smith | ``` |
| 90 | |||
| 91 | Now connect a VNC client to localhost:5910 and complete the install. |
||
| 92 | |||
| 93 | ## Import Existing Disk to New Guest |
||
| 94 | |||
| 95 | To import an existing guest disk image, use the following command: |
||
| 96 | |||
| 97 | ``` |
||
| 98 | 7 | Brian Smith | $ sudo virt-install --virt-type kvm --name GUEST_NAME \ |
| 99 | 1 | Brian Smith | --vcpus=4 --virt-type kvm --import \ |
| 100 | -v --os-variant debian8 \ |
||
| 101 | --disk PATH_TO_DISK_IMAGE,device=disk,bus=virtio --memory 4096 --graphics vnc |
||
| 102 | 2 | Brian Smith | ``` |
| 103 | |||
| 104 | ## Connect to Guest, Configure DNS |
||
| 105 | |||
| 106 | The default network for KVM is 192.168.122.0/24 and the guest should be assigned a DHCP address when it boots. Use the VNC connection to execute ```$ ip addr``. ssh should be able to connect to the guest from the host. |
||
| 107 | 1 | Brian Smith | |
| 108 | 2 | Brian Smith | Unfortunately, dnsmasq doesn't appear to set the search domain properly. For Debian, configure a search domain in the guest's ```/etc/network/interfaces```. |
| 109 | |||
| 110 | ``` |
||
| 111 | allow-hotplug eth0 |
||
| 112 | iface eth0 inet dhcp |
||
| 113 | 16 | Brian Smith | dns-search MYDOMAIN |
| 114 | 1 | Brian Smith | ``` |
| 115 | 2 | Brian Smith | |
| 116 | 1 | Brian Smith | ## Configure Guest for PCI Passthrough |
| 117 | |||
| 118 | Shutdown the guest if it is running. |
||
| 119 | |||
| 120 | ``` |
||
| 121 | 16 | Brian Smith | $ virsh shutdown GUEST_NAME |
| 122 | 1 | Brian Smith | ``` |
| 123 | |||
| 124 | Look for the PCI device in virsh. Look for a pci device that matches the port found via lspci. |
||
| 125 | |||
| 126 | ``` |
||
| 127 | $ virsh nodedev-list --tree |
||
| 128 | ``` |
||
| 129 | |||
| 130 | Detach the device. Use the child device of the one that matches the device you found via lspci. |
||
| 131 | |||
| 132 | ``` |
||
| 133 | $ virsh nodedev-detach pci_0000_81_00_0 |
||
| 134 | ``` |
||
| 135 | |||
| 136 | Dump the device info. |
||
| 137 | |||
| 138 | ``` |
||
| 139 | $ virsh nodedev-dumpxml pci_0000_81_00_0 |
||
| 140 | ``` |
||
| 141 | |||
| 142 | 16 | Brian Smith | Convert bus, slot and function to hex. The printf utility may be used to do this. |
| 143 | 1 | Brian Smith | |
| 144 | 16 | Brian Smith | |
| 145 | ``` |
||
| 146 | $ printf %x VALUE |
||
| 147 | ``` |
||
| 148 | |||
| 149 | 1 | Brian Smith | Edit the guest and add a hostdev section: |
| 150 | |||
| 151 | ``` |
||
| 152 | 16 | Brian Smith | $ virsh edit GUEST_NAME |
| 153 | |||
| 154 | 1 | Brian Smith | <hostdev mode='subsystem' type='pci' managed='yes'> |
| 155 | <source> |
||
| 156 | <address domain='0x0000' bus='0x81' slot='0x0' function='0x0'/> |
||
| 157 | </source> |
||
| 158 | </hostdev> |
||
| 159 | ``` |
||
| 160 | |||
| 161 | 16 | Brian Smith | Boot the guest |
| 162 | 1 | Brian Smith | |
| 163 | 16 | Brian Smith | ``` |
| 164 | $ virsh start GUEST_NAME |
||
| 165 | ``` |
||
| 166 | 1 | Brian Smith | |
| 167 | 16 | Brian Smith | Upon booting the guest, the passthrough device should be present in the guest's lspci output. The passthrough device should be usable by the guest's kernel drivers. |
| 168 | |||
| 169 | **Note**: the PCI device may have different capabilities in the VM than it has on the physical host. Hopefully, the driver takes this into account. Refer to https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4c009af473b2026caaa26107e34d7cc68dad7756 for a patch that fixes one such problem in hfi1. Hope it helps. |
||
| 170 | |||
| 171 | 1 | Brian Smith | ## References |
| 172 | |||
| 173 | 1. https://wiki.debian.org/KVM |
||
| 174 | 2. https://jamielinux.com/docs/libvirt-networking-handbook/nat-based-network.html |
||
| 175 | 3. https://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM |
||
| 176 | 4. https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF |
||
| 177 | 5. https://wiki.debian.org/VGAPassthrough |
||
| 178 | |||
| 179 | ---- |
||
| 180 | |||
| 181 | 16 | Brian Smith | Brian T. Smith |
| 182 | Senior Technical Staff |
||
| 183 | System Fabric Works, Inc. |
||
| 184 | bsmith@systemfabricworks.com |