【博文推薦】KVM虛擬化網(wǎng)絡(luò)優(yōu)化技術(shù)總結(jié)
一個(gè)完整的數(shù)據(jù)包從虛擬機(jī)到物理機(jī)的路徑是:
虛擬機(jī)--QEMU虛擬網(wǎng)卡--虛擬化層--內(nèi)核網(wǎng)橋--物理網(wǎng)卡
KVM的網(wǎng)絡(luò)優(yōu)化方案,總的來說,就是讓虛擬機(jī)訪問物理網(wǎng)卡的層數(shù)更少,直至對(duì)物理網(wǎng)卡的單獨(dú)占領(lǐng),和物理機(jī)一樣的使用物理網(wǎng)卡,達(dá)到和物理機(jī)一樣的網(wǎng)絡(luò)性能。
方案一 全虛擬化網(wǎng)卡和virtio
Virtio與全虛擬化網(wǎng)卡區(qū)別
全虛擬化網(wǎng)卡是虛擬化層完全模擬出來的網(wǎng)卡,半虛擬化網(wǎng)卡通過驅(qū)動(dòng)對(duì)操作系統(tǒng)做了改造;
viritio簡(jiǎn)單的說,就是告訴虛擬機(jī),hi 你就是在一個(gè)虛擬化平臺(tái)上跑,咱們一起做一些改動(dòng),讓你在虛擬化平臺(tái)上獲得更好的性能;
關(guān)于virtio的使用場(chǎng)景
因 為windows虛擬機(jī)使用viritio有網(wǎng)絡(luò)閃斷的情況發(fā)生,windows 虛擬機(jī)如果網(wǎng)絡(luò)壓力不高,建議使用e1000這樣的全虛擬化網(wǎng)卡,如果網(wǎng)絡(luò)壓力比較高,建議使用SRIVO或者PCI Device Assignment這樣的技術(shù);viritio也在不斷的演進(jìn),希望windows的閃斷的問題越來越少。
KVM天生就是為linux系統(tǒng)設(shè)計(jì)的,linux系統(tǒng)請(qǐng)放心大膽的使用viritio驅(qū)動(dòng);
#p#
方案二 vhost_net macvtap技術(shù)
vhost_net使虛擬機(jī)的網(wǎng)絡(luò)通訊直接繞過用戶空間的虛擬化層,直接可以和內(nèi)核通訊,從而提供虛擬機(jī)的網(wǎng)絡(luò)性能;
macvtap則是跳過內(nèi)核的網(wǎng)橋;
使用vhost_net,必須使用virtio半虛擬化網(wǎng)卡;
vhost_net虛擬機(jī)xml文件配置,
- <interface type='bridge'>
- <mac address=''/>
- <source bridge='br0'/>
- <model type='virtio'/>
- <driver name="vhost"/>
- <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
- </interface>
如果不使用vhost_net,則為
- <driver name="qemu"/>
macvtap 虛擬機(jī)xml配置
- <interface type='direct'>
- <mac address='00:16:3e:d5:d6:77'/>
- <source dev='lo' mode='bridge'/>
- <model type='e1000'/>
- <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
- </interface>
注意:macvtap在windows虛擬機(jī)上性能很差,不建議使用
vhost_net macvtap比較
macvlan的功能是給同一個(gè)物理網(wǎng)卡配置多個(gè)MAC地址,這樣可以在軟件商配置多個(gè)以太網(wǎng)口,屬于物理層的功能。
macvtap是用來替代TUN/TAP和Bridge內(nèi)核模塊,macvtap是基于macvlan這個(gè)模塊,提供TUN/TAP中tap設(shè)備使用的接口,
使用macvtap以太網(wǎng)口的虛擬機(jī)能夠通過tap設(shè)備接口,直接將數(shù)據(jù)傳遞到內(nèi)核中對(duì)應(yīng)的macvtap以太網(wǎng)口。
vhost-net是對(duì)于virtio的優(yōu)化,virtio本來是設(shè)計(jì)用于進(jìn)行客戶系統(tǒng)的前端與VMM的后端通信,減少硬件虛擬化方式下根模式個(gè)非根模式的切換。
而是用vhost-net后,可以進(jìn)一步進(jìn)入CPU的根模式后,需要進(jìn)入用戶態(tài)將數(shù)據(jù)發(fā)送到tap設(shè)備后再次切入內(nèi)核態(tài)的開銷,而是進(jìn)入內(nèi)核態(tài)后不需要在進(jìn)行內(nèi)核態(tài)用戶態(tài)的切換,進(jìn)一步減少這種特權(quán)級(jí)的切換,說vhost-net屬于哪個(gè)層不準(zhǔn)確,而是屬于進(jìn)行二層網(wǎng)絡(luò)數(shù)據(jù)傳遞的優(yōu)化。
#p#
方案三 虛擬機(jī)網(wǎng)卡獨(dú)占
網(wǎng)卡passthrough在虛擬機(jī)的配置方法
1 使用lcpci 設(shè)備查看pci設(shè)備信息
- 04:00.0 Ethernet controller: Intel Corporation 82571EB Gigabit Ethernet Controller (rev 06)
- 04:00.1 Ethernet controller: Intel Corporation 82571EB Gigabit Ethernet Controller (rev 06)
也可以使用virsh nodedev-list –tree得到信息
- +- pci_0000_00_07_0
- | |
- | +- pci_0000_04_00_0
- | | |
- | | +- net_p1p1_00_1b_21_88_69_dc
- | |
- | +- pci_0000_04_00_1
- | |
- | +- net_p1p2_00_1b_21_88_69_dd
2 使用virsh nodedev-dumxml pci_0000_04_00_0得到xml配置信息
- [root@]# virsh nodedev-dumpxml pci_0000_04_00_0
- <device>
- <name>pci_0000_04_00_0</name>
- <parent>pci_0000_00_07_0</parent>
- <driver>
- <name>e1000e</name>
- </driver>
- <capability type='pci'>
- <domain>0</domain>
- <bus>4</bus>
- <slot>0</slot>
- <function>0</function>
- <product id='0x105e'>82571EB Gigabit Ethernet Controller</product>
- <vendor id='0x8086'>Intel Corporation</vendor>
- </capability>
- </device>
3 編輯虛擬機(jī)xml文件,加入pci設(shè)備信息
- <hostdev mode='subsystem' type='pci' managed='yes'>
- <source>
- <address domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
- </source>
- </hostdev>
Domain bus slot function信息從dumpxml出的xml文件獲取,define虛擬機(jī),然后開啟虛擬機(jī)就可以,注意以為附件上去的是物理設(shè)備,需要在系統(tǒng)里面安裝相應(yīng)的驅(qū)動(dòng)。
#p#
方案四 SR-IVO技術(shù)
SRIOV的原理
SR-IVO 是the single root I/O virtualization 的簡(jiǎn)寫,是一個(gè)將PCIe共享給虛擬機(jī)使用的標(biāo)準(zhǔn),目前用在網(wǎng)絡(luò)設(shè)備上比較多,理論上也可以支持其他的PCI設(shè)備,SRIOV需要硬件的支持。
以下內(nèi)容來自oracle網(wǎng)站,鏈接為
http://docs.oracle.com/cd/E38902_01/html/E38873/glbzi.html
物理功能 (Physical Function, PF)
用 于支持 SR-IOV 功能的 PCI 功能,如 SR-IOV 規(guī)范中定義。PF 包含 SR-IOV 功能結(jié)構(gòu),用于管理 SR-IOV 功能。PF 是全功能的 PCIe 功能,可以像其他任何 PCIe 設(shè)備一樣進(jìn)行發(fā)現(xiàn)、管理和處理。PF 擁有完全配置資源,可以用于配置或控制 PCIe 設(shè)備。
虛擬功能 (Virtual Function, VF)
與物理功能關(guān)聯(lián)的一種功能。VF 是一種輕量級(jí) PCIe 功能,可以與物理功能以及與同一物理功能關(guān)聯(lián)的其他 VF 共享一個(gè)或多個(gè)物理資源。VF 僅允許擁有用于其自身行為的配置資源。
每 個(gè) SR-IOV 設(shè)備都可有一個(gè)物理功能 (Physical Function, PF),并且每個(gè) PF 最多可有 64,000 個(gè)與其關(guān)聯(lián)的虛擬功能 (Virtual Function, VF)。PF 可以通過寄存器創(chuàng)建 VF,這些寄存器設(shè)計(jì)有專用于此目的的屬性。
一 旦在 PF 中啟用了 SR-IOV,就可以通過 PF 的總線、設(shè)備和功能編號(hào)(路由 ID)訪問各個(gè) VF 的 PCI 配置空間。每個(gè) VF 都具有一個(gè) PCI 內(nèi)存空間,用于映射其寄存器集。VF 設(shè)備驅(qū)動(dòng)程序?qū)拇嫫骷M(jìn)行操作以啟用其功能,并且顯示為實(shí)際存在的 PCI 設(shè)備。創(chuàng)建 VF 后,可以直接將其指定給 IO 來賓域或各個(gè)應(yīng)用程序(如裸機(jī)平臺(tái)上的 Oracle Solaris Zones)。此功能使得虛擬功能可以共享物理設(shè)備,并在沒有 CPU 和虛擬機(jī)管理程序軟件開銷的情況下執(zhí)行 I/O。
SR-IOV 的優(yōu)點(diǎn)
SR-IOV 標(biāo)準(zhǔn)允許在 IO 來賓域之間高效共享 PCIe 設(shè)備。SR-IOV 設(shè)備可以具有數(shù)百個(gè)與某個(gè)物理功能 (Physical Function, PF) 關(guān)聯(lián)的虛擬功能 (Virtual Function, VF)。VF 的創(chuàng)建可由 PF 通過設(shè)計(jì)用來開啟 SR-IOV 功能的寄存器以動(dòng)態(tài)方式進(jìn)行控制。缺省情況下,SR-IOV 功能處于禁用狀態(tài),PF 充當(dāng)傳統(tǒng) PCIe 設(shè)備。
具有 SR-IOV 功能的設(shè)備可以利用以下優(yōu)點(diǎn):
1.性能-從虛擬機(jī)環(huán)境直接訪問硬件。
2.成本降低-節(jié)省的資本和運(yùn)營(yíng)開銷包括:
- 節(jié)能
- 減少了適配器數(shù)量
- 簡(jiǎn)化了布線
- 減少了交換機(jī)端口
SRIOV的使用
啟動(dòng)SRIVO內(nèi)核模塊
modprobe igb
激活虛擬功能VF
modprobe igb max_vfs=7
千兆網(wǎng)卡最多支持8個(gè)vf0-7,千兆網(wǎng)卡目前支持比較好的是INTEL I350, 82576S雖然也支持SRIOV但是只支持虛擬機(jī)是linux的情況,windows系統(tǒng)不支持;
萬兆網(wǎng)卡最多支持64個(gè)vg0-63,intel的新新一代萬兆網(wǎng)卡都支持SRIOV x520 x540等;
如果需要重新設(shè)置vf 可以刪除模塊在重新加載
modprobe -r igb
將配置***寫入配置文件
echo "options igb max_vfs=7" >>/etc/modprobe.d/igb.conf
通過lspci命令可以看多主網(wǎng)卡和子網(wǎng)卡
- # lspci | grep 82576
- 0b:00.0 Ethernet controller: Intel Corporation 82576 Gigabit Network Connection (rev 01)
- 0b:00.1 Ethernet controller: Intel Corporation 82576 Gigabit Network Connection (rev 01)
- 0b:10.0 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:10.1 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:10.2 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:10.3 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:10.4 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:10.5 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:10.6 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:10.7 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:11.0 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:11.1 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:11.2 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:11.3 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:11.4 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
- 0b:11.5 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
虛擬機(jī)可以聽過pci網(wǎng)卡獨(dú)占的方式使用子網(wǎng)卡;
- <interface type='hostdev' managed='yes'>
- <source>
- <address type='pci' domain='0' bus='11' slot='16' function='0'/>
- </source>
- </interface>
#p#
方案五 網(wǎng)卡多隊(duì)列
centos 7開始支持virtio網(wǎng)卡多隊(duì)列,可以大大提高虛擬機(jī)網(wǎng)絡(luò)性能,配置方法如下:
虛擬機(jī)的xml網(wǎng)卡配置
- <interface type='network'>
- <source network='default'/> <model type='virtio'/> <driver name='vhost' queues='N'/> </interface>
N 1 - 8 最多支持8個(gè)隊(duì)列
在虛擬機(jī)上執(zhí)行以下命令開啟多隊(duì)列網(wǎng)卡
- #ethtool -L eth0 combined M
N 1 - 8 最多支持8個(gè)隊(duì)列
在虛擬機(jī)上執(zhí)行以下命令開啟多隊(duì)列網(wǎng)卡
- #ethtool -L eth0 combined M
KVM網(wǎng)絡(luò)優(yōu)化方案?jìng)€(gè)人認(rèn)為以硬件為主,硬件上萬兆+SRIOV的方案會(huì)越來越普及,但是在線遷移的問題有待解決。
本文出自:http://xiaoli110.blog.51cto.com/1724/1558984