IO虚拟化之设备透传

IO虚拟化实现的方式有很多种,有软件模拟、半虚拟化、设备直接分配、单根IO虚拟化。

PCI设备直接分配

设备直接分配 (Device assignment)也称为 Device Pass-Through。就是将宿主机host中的物理 PCI 设备直接分配给客户机guest使用,虚拟机独占这个PCI设备。在guest进行对应的IO操作时,避免 了VM Exit 陷入VMM 中,极大提高了性能。

在Intel平台上的Device assignment技术是VT-D(Intel Virtualization Technology for Directed I/O),是在VT-X的基础上对硬件辅助虚拟化的扩展。

下图(来自intel《vt-directed-io-spec》)是软件模拟io虚拟化和intel的VT-D的对比原理图: 

PCI设备直接分配实践

下面的例子是把host主机中个一个网卡透传给虚拟机使用。

(在intel平台上要开启VT-d,内核要设置intel_iommu=on。)

1.在host上查看网卡信息

# lspci
00:19.0 Ethernet controller: Intel Corporation Ethernet Connection I217-LM (rev 04)
04:00.0 Ethernet controller: Intel Corporation 82541PI Gigabit Ethernet Controller (rev 05)
# ls /sys/bus/pci/devices/0000:04:00.0/net/
eth1
# cat /sys/class/net/eth1/address
90:e2:ba:9f:7c:5a

2.把pci设备从host中分离

# virsh nodedev-list
pci_0000_04_00_0
# virsh nodedev-dettach pci_0000_04_00_0
Device pci_0000_04_00_0 detached

3.把pci设备分配给虚拟机

虚拟机xml:

<devices>
    <hostdev mode='subsystem' type='pci' managed='yes'>
      <source>
        <address domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
      </source>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x10' function='0x0'/>
    </hostdev>
</devices>

4.进入虚拟机查看pci是否进入

# lspci
00:10.0 Ethernet controller: Intel Corporation 82541PI Gigabit Ethernet Controller (rev 05)
# cat /sys/class/net/eth1/address
90:e2:ba:9f:7c:5a

可以看出虚拟机中的网卡就是原来host中的网卡。并且此时在host的/sys/class/net/中已经看不到这个网卡了。

5.把pci设备还给host:

# virsh nodedev-reattach pci_0000_04_00_0
原文地址:https://www.cnblogs.com/liujunjun/p/13354694.html