https://blog.csdn.net/yzy1103203312/article/details/81092647
本文介紹了如何在KVM虛拟機平台上使用Pass-through和SR-IOV,至于Pass-through和SR-IOV的原理可以查閱其他文章。
所謂Pass-through技術是指可以将PCI/PCIe裝置繞過虛拟機平台直接配置設定給虛拟機使用,而SR-IOV裝置除了有一個實體功能 (Physical Function, PF)之外,還可以提供許多虛拟功能 (Virtual Function, VF)給虛拟機使用。
本文使用的環境是:CentOS 7 + Linux Kernel 3.10.0 + Intel 82599網卡
PCI Pass-through的使用
使用PCI Pass-through需要硬體的支援:
主機需要支援Intel VT-d 或者 AMD IOMMU技術
在 Linux 核心需要啟用 PCI Pass-through
編輯 /etc/default/grub 檔案,添加 intel_iommu=on 參數:
# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet intel_iommu=on"
GRUB_DISABLE_RECOVERY="true"
更新GRUB後重新開機使之生效:
# grub2-mkconfig -o /etc/grub2.cfg
要使用Pass-through,首先要找到裝置的BDF (Bus, Device, Function),BDF由三個或者四個數字組成: DDDD:bb:dd.f,其中:
DDDD is a 4-digit hex for the PCI domain. This is optional (if not included, it will be assumed to be 0000).
bb is a 2-digit hex of the PCI bus number.
dd is a 2-digit hex of the PCI device number.
f is a 1-digit decimal of the PCI function number.
使用lspci指令可以很友善的找到裝置的BDF:
# lspci | grep 82599
05:00.0 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
05:00.1 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
在配置設定pci裝置給虛拟機之前,首先要讓libvirt識别到pci裝置,libvirt使用了與BDF相似的标記,隻不過libvirt是用下劃線(_)來分隔裝置的BDF:
# virsh nodedev-list | grep 05_00
pci_0000_05_00_0
pci_0000_05_00_1
KVM是通過設定pci裝置的xml配置檔案給虛拟機配置設定的,是以我們需要事先編輯好pci裝置的xml檔案:
# virsh nodedev-dumpxml pci_0000_05_00_0
<device>
<name>pci_0000_05_00_0</name>
<path>/sys/devices/pci0000:00/0000:00:03.2/0000:05:00.0</path>
<parent>pci_0000_00_03_2</parent>
<driver>
<name>ixgbe</name>
</driver>
<capability type='pci'>
<domain>0</domain>
<bus>5</bus>
<slot>0</slot>
<function>0</function>
<product id='0x10fb'>82599ES 10-Gigabit SFI/SFP+ Network Connection</product>
<vendor id='0x8086'>Intel Corporation</vendor>
<capability type='virt_functions' maxCount='63'>
<address domain='0x0000' bus='0x05' slot='0x10' function='0x0'/>
<address domain='0x0000' bus='0x05' slot='0x10' function='0x2'/>
<address domain='0x0000' bus='0x05' slot='0x10' function='0x4'/>
<address domain='0x0000' bus='0x05' slot='0x10' function='0x6'/>
</capability>
<iommuGroup number='17'>
<address domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
</iommuGroup>
<numa node='0'/>
<pci-express>
<link validity='cap' port='2' speed='5' width='8'/>
<link validity='sta' speed='5' width='8'/>
</pci-express>
</capability>
</device>
在配置xml檔案時需要bus, slot 和 function等參數,建立一個新的xml檔案并複制這些參數:
# cat pci_0.xml
<interface type='hostdev' managed='yes'>
<source>
<address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
</source>
</interface>
注意:如果配置了pci裝置為 managed 模式,pci裝置配置設定給虛拟機時會自動從主機上分離,pci裝置從虛拟機上分離時會自動 歸還給主機。
接下來就可以配置設定指定的pci裝置給虛拟機了:
# virsh list
Id Name State
----------------------------------------------------
1 vm1 running
2 vm2 running
# virsh attach-device vm1 pci_0.xml --live --config
注意:使用--live 參數是将pci裝置attach到正在運作的虛拟機,使用 --config 參數是設定的同時更改虛拟機xml檔案,這樣就可以保證虛拟機重新開機後仍然生效。
檢視虛拟機的pci裝置:
# virsh dumpxml vm1 | grep interface -A8
<interface type='bridge'>
<mac address='52:54:00:56:28:68'/>
<source bridge='br0'/>
<target dev='vnet0'/>
<model type='virtio'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='hostdev' managed='yes'>
<mac address='52:54:00:01:9e:e3'/>
<driver name='vfio'/>
<source>
<address type='pci' domain='0x0000' bus='0x05' slot='0x10' function='0x0'/>
</source>
<alias name='hostdev0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/1'/>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
<alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/1'>
使用完後從虛拟機上分離:
# virsh detach-device vm1 pci_0.xml
使用SR-IOV
使用SR-IOV也需要硬體的支援,我使用的是Intel 82599網卡。
首先檢視硬體是否被Linux檢測到:
[email protected]:/home/ye# lspci | grep 82599
05:00.0 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
05:00.1 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
以及檢視該硬體的驅動:
[email protected]:/home/ye# ethtool -i p5p1
driver: ixgbe
version: 4.2.1-k
firmware-version: 0x61bd0001
bus-info: 0000:05:00.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
可以看到我的82599網卡的驅動是ixgbe,可以使用lsmod 指令檢視ixgbe驅動是否工作正常:
[email protected]:/home/ye# lsmod | grep ixgbe
ixgbevf 53248 0
ixgbe 274432 0
vxlan 45056 1 ixgbe
mdio 16384 1 ixgbe
dca 16384 2 igb,ixgbe
ptp 20480 3 igb,tg3,ixgbe
接下來就可以激活82599的虛拟功能(Virtual Functions)了,使用參數max_vfs 可以在加載子產品的時候指定裝置可以配置設定的最大虛拟功能(Virtual Functions)的數目。
首先移除ixgbe子產品:
# modprobe -r ixgbe
重新加載ixgbe子產品并使用max_vfs參數指定裝置可以配置設定虛拟功能的最大數目:
# modprobe ixgbe max_vfs=7
注意:激活VF也可以在啟動加載ixgb子產品時實作,編輯 /etc/default/grub 檔案, 添加參數 ixgbe.max_vfs=4 即可。
激活的虛拟功能:
[email protected]:/home/ye# lspci | grep 82599
05:00.0 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
05:00.1 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
05:10.0 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:10.1 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:10.2 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:10.3 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:10.4 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:10.5 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:10.6 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:10.7 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:11.0 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:11.1 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:11.2 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:11.3 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:11.4 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
05:11.5 Ethernet controller: Intel Corporation 82599 Ethernet Controller Virtual Function (rev 01)
激活後的虛拟功能就可以通過Pass-through方式配置設定給DomU使用啦!
參考文獻:
[1]. USING SR-IOV
[2]. PCI DEVICE ASSIGNMENT
---------------------
作者:為幸福寫歌
來源:CSDN
原文:https://blog.csdn.net/yzy1103203312/article/details/81092647
版權聲明:本文為部落客原創文章,轉載請附上博文連結!