本文介绍了如何在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 listId    Name                           State
----------------------------------------------------1     vm1                            running2     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相关推荐

  1. OpenStack 企业私有云的若干需求(7):电信行业解决方案 NFV

    本文转自网络文章,内容均为非盈利,一切版权原作者所有. 文章内容仅代表原作者独立观点,不代表本账号立场,转载此文章在于个人学习收藏,传递更多知识. 如有侵权,马上删除. 原文作者:世民谈云计算(微信公 ...

  2. 华为dra路由方式分为relay和proxy_华为云计算(3)——网络虚拟化

    ​关注微信公众号IT小组,获取更多干货知识~ 一.网络虚拟化 网络虚拟化在介绍网络虚拟化之前,需要考虑一个问题--为什么需要网络虚拟化呢?之所以要对网络进行虚拟化,是因为在没有进行虚拟化之前,每个服务 ...

  3. Tungsten Fabric SDN — for Akraino Based Network Edges

    目录 文章目录 目录 Tungsten Fabric as SDN for Akraino Based Network Edges Deployment Tungsten Fabric as SDN ...

  4. OpenStack 的 SR-IOV 虚拟机热迁移

    目录 文章目录 目录 前言列表 前言 SR-IOV Pass-through 虚拟机热迁移的问题 基于 macvtap 层的 SR-IOV 虚拟机热迁移 Workaround SR-IOV Pass- ...

  5. 云计算 原理与实践期末复习

    本文按照老师ppt,不完全遵从书的章节顺序及内容,仅供参考 第一讲 云计算概述 1.云计算定义 云计算是一种无处不在.便捷且按需对一个共享的可配置计算资源(包括网络.服务器.存储.应用和服务)进行网络 ...

  6. 万兆电口网卡性能分析

    FM-ELX540BT2-T2 是一款PCI-E双口万兆双电口网卡,采用英特尔原装X540芯片,FM-ELX540BT2-T2是飞迈瑞克的一款新的电口万兆网卡,这也标志着万兆以太网进入更广阔的服务器市 ...

  7. CNA, FCoE, TOE, RDMA, iWARP, iSCSI等概念及 Chelsio T5 产品介绍

    鉴于研究所的需求,最近开始研究Chelsio T5(终结者5),本篇博文对相关技术的基础概念做了罗列,并给了一些扩展学习链接.后续自己将针对RDMA技术做进一步学习和研究! 核心基础概念 FCoE:以 ...

  8. python马尔可夫链_[译] 用 Python 实现马尔可夫链的初级教程

    马尔可夫链是通常用一组随机变量定义的数学系统,可以根据具体的概率规则进行状态转移.转移的集合满足 马尔可夫性质 ,也就是说,转移到任一特定状态的概率只取决于当前状态和所用时间,而与其之前的状态序列无关 ...

  9. 查看当前设备是否启用SR-IOV

    参考: 在 Windows Server 上為 Hyper-V 虛擬機配置 SR-IOV - HeyAdvice.net 虚拟化中的SR-IOV - 爱码网 Windows 使用PowerShell ...

  10. 华为云05之网络虚拟化

    华为云之网络虚拟化 网络虚拟化有三种实现方式: 1.直接是Domain0来完成网络虚拟化. 2.底层硬件层是一个智能的网卡(INIC),在VM侧装Tools,利用Tools的驱动来使用INIC,不过这 ...

最新文章

  1. ncbi查找目的基因序列_使用NCBI设计qPCR引物方法
  2. Python操作MySQL之SQLAlchemy
  3. explian执行计划
  4. oracle死锁trace,Oracle 学习之性能优化(十)锁
  5. PHP安全,防止远程非法提交
  6. unity如何检测内存泄漏_如何排查Java内存泄漏?看懂这一篇就够用了
  7. matlab2c使用c++实现matlab函数系列教程-prod函数
  8. [leetcode] 872. 叶子相似的树(周赛)
  9. windows DNS服务器的配置
  10. C语言题目设圆的半径为2.5,编程该圆的周长和面积。
  11. Redfish协议测试工具–Postman
  12. mysql 校对规则_MySQL:校对规则
  13. Power BI分解销售目标
  14. android 电池权限,Android M及以上提示用户将APP加入电池优化白名单
  15. 移动端h5调起高德地图、百度地图实现路线及路况查询
  16. php 字符串放到数组中,在PHP中将字符串转换为数组(Converting string into array in php)...
  17. 佐治亚理工学院计算机科学硕士,佐治亚理工学院
  18. 使用自然语言处理来检测电子邮件中的垃圾邮件
  19. 腾讯云大数据流计算 Oceanus 在 MySQL CDC Connector 的核心优化
  20. Java安装与环境配置(Java SE 14)

热门文章

  1. java定义一个圆类_2.8.1 如何定义一个圆的类
  2. 7-6 学生选课信息管理 分数 10
  3. 几十款收费软件现在免费了
  4. php 中insert into,php – INSERT INTO SQL Heredoc正确的语法
  5. 计算机学院迎接新生标语,大学迎接新生标语
  6. 对于Imbalance Data的总结
  7. 中兴U880 完美版2.3.7第三弹炫目登场,你还在为刷机而烦恼?来吧!它值得你拥有!!!!
  8. JAVA毕业设计HTML5“守护萌宠”网站设计与实现计算机源码+lw文档+系统+调试部署+数据库
  9. C#开发的OpenRA游戏的游戏界面内鼠标处理窗口
  10. 基于STM32+SIM800C+IoT开发平台设计的森林火灾预警系统(联动控制)