1. 程式人生 > 實用技巧 >IO虛擬化之裝置透傳

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