0%

NIC 使用 sr-iov

SR-IOV 网卡直通

SR-IOV是一种基于硬件的虚拟化方案。用来使一个PCIe的物理设备,能虚拟出多个设备。

SR-IOV 中的两种新功能类型是:

  • 物理功能 (Physical Function, PF)
    包括管理SR-IOV功能在内的所有PCIe function

  • 虚拟功能 (Virtual Function, VF)
    VF 是一种轻量级 PCIe 功能,VF 仅允许拥有用于其自身行为的配置资源。

每个 SR-IOV 设备都可有一个物理功能 (Physical Function, PF),并且每个 PF 最多可有 64,000 个与其关联的虚拟功能 (Virtual Function, VF)。PF 可以通过寄存器创建 VF,这些寄存器设计有专用于此目的的属性。

一旦在 PF 中启用了 SR-IOV,就可以通过 PF 的总线、设备和功能编号(路由 ID)访问各个 VF 的 PCI 配置空间。每个 VF 都具有一个 PCI 内存空间,用于映射其寄存器集。VF 设备驱动程序对寄存器集进行操作以启用其功能,并且显示为实际存在的 PCI 设备。创建 VF 后,可以直接将其指定给 IO 来宾域或各个应用程序(如裸机平台上的 Oracle Solaris Zones)。此功能使得虚拟功能可以共享物理设备,并在没有 CPU 和虚拟机管理程序软件开销的情况下执行 I/O。

CentOS 7 开启

开启VF大概需要这几步:

  1. 硬件支持
  2. BIOS 开启 SR-IOV 、VT
  3. 开启IOMMU
  4. 配置PF

全文基于 Intel CPU

查看CPU是否支持虚拟化

1
egrep -c '(vmx|svm)' /proc/cpuinfo

如果 >0则支持

然后在 BIOS 中开启 enable VTIntel VT-d

启用IOMMU

修改/etc/default/grub
GRUB_CMDLINE_LINUX后面添加intel_iommu=on

1
GRUB_CMDLINE_LINUX="crashkernel=auto biosdevname=0 net.ifnames=0 rhgb quiet intel_iommu=on"

然后生成引导&重启

1
2
grub2-mkconfig  -o /boot/grub2/grub.cfg
reboot now

重启后检查IOMMU是否启用

1
2
3
4
[root@localhost ~]# dmesg | grep -e DMAR -e IOMMU
...
[ 0.000000] DMAR: IOMMU enabled
...

查看网卡支持虚拟的数量

1
cat /sys/class/net/eth0/device/sriov_totalvfs

创建虚拟网卡

eth0为例

1
echo '7' > /sys/class/net/eth0/device/sriov_numvfs

然后检查是否创建成功

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# lspci|grep Ethernet
01:00.0 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
01:00.1 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
01:10.0 Ethernet controller: Intel Corporation I350 Ethernet Controller Virtual Function (rev 01)
01:10.4 Ethernet controller: Intel Corporation I350 Ethernet Controller Virtual Function (rev 01)
01:11.0 Ethernet controller: Intel Corporation I350 Ethernet Controller Virtual Function (rev 01)
01:11.4 Ethernet controller: Intel Corporation I350 Ethernet Controller Virtual Function (rev 01)
01:12.0 Ethernet controller: Intel Corporation I350 Ethernet Controller Virtual Function (rev 01)
01:12.4 Ethernet controller: Intel Corporation I350 Ethernet Controller Virtual Function (rev 01)
01:13.0 Ethernet controller: Intel Corporation I350 Ethernet Controller Virtual Function (rev 01)

这个配置非持久化