admin管理员组文章数量:1568354
本文介绍了如何在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检测到:
root@ccrfox141:/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)
以及查看该硬件的驱动:
root@ccrfox141:/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驱动是否工作正常:
root@ccrfox141:/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 即可。
激活的虚拟功能:
root@ccrfox141:/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
版权声明:本文标题:KVM使用Pass-through和SR-IOV 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1725805385a1043966.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论