MacVTap和vhost-net技術原理
VM <----> 虛擬網卡 <----> 虛擬化層 <----> 內核網橋 <----> 物理網卡
vhost-net技術使虛擬機的網絡通信繞過用戶空間的虛擬化層,必須使用Virtio半虛擬化的網卡
MacVTap則是跳過內核的網橋
傳統橋接方案
<interface type='bridge'>
<mac address='00:16:42:34:45:6f'/>
<source bridge='br0'/>
<targetdev='tap0'/>
<model type='Virtio'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
vhost-net是對於Vritio的優化,Virtio本來是設計用於進行客戶系統的前端與VMM的後端通信,減少硬件虛擬化方式下根模式和非根模式的轉換
vhost-net是後端優化方案.
不使用vhost-net,進入CPU的根模式後,需要進入用戶態將數據發送到tap設備後,再次切入內核態,使用vhost-net方式後,進入內核態後不需要再進行內核態和用戶態的切換,進一步減少特權級切換的開銷.
MacVTap方案
> ip link add link eth1 name MacVTap0 type MacVTap
> ip link set MacVTap0 address 1a:34:34:34:45:42 up
> ip link show MacVTap0
<interface type='direct'>
<mac address='1a:343434:45:42'/>
<source dev='eth1' mode='bridge'/>
<model type='e1000'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
網卡的中斷與多隊列
Linux內核調用中斷處理程序的函數來處理中斷,由於中斷會頻繁發生,必須要將一些繁重且不非常重要的任務從中斷處理程序中剝離出來,這一部分在Linux稱爲下半部
3中處理下半部的方法
軟中斷
tasklet
工作隊列
高負載的網卡是軟中斷產生的大戶,很容易形成瓶頸.並且在相當長的時間裏是CPU0來處理
網卡多隊列 網卡的數據請求通過多個CPU來處理
RSS是硬件特性,實現多隊列,將 不同的流發到不同的CPU上, 同一數據流始終在同一個CPU上, 避免CPU的並行性和TCP的順序性發生衝突.
ls /sys/class/net/eth0/queues/
會看到系統中看到網卡對應多個發送和接收隊列
irqbalance 優化中斷分配
工作模式
performance
power-save
https://gist.github.com/syuu1228/4352382 網卡綁定中斷的腳本
多隊列Virtio網卡
網卡PCI PassThrough技術
> virsh nodedev -list -tree
> virsh nodedev-dumpxml.pci_0000_04_00_0
SR-IOV技術
the Single Root I/O Virtualization 是一個將PCI-E設備共享給虛擬機使用的標準.多用在網絡設備上,理論上也可以支持其他的PCI設備
SR-IOV技術是一種硬件解決方案,提供了一種從硬件上繞過系統的虛擬化層,並且使沒有虛擬機能有單獨的內存地址,中斷,DMA流
PFs physical functions 擁有全功能PCI-E
VFs Virtual Functions 只有輕量級的PCI-E 每個PF最多可以有64000個虛擬功能VF
網卡SR-IOV
需要先配置宿主機的PF,然後將子網卡通過網卡獨佔的方式供虛擬機使用.
modprobe igb
modprobe igb max_vfs=7
千兆網卡最多支持8個VF(0-7)
萬兆網卡最多支持64個VF
BIOS打開SR-IOV
modprobe -r igb
echo "option igb max_vfs=7" >> /etc/modprobe.d/igb.conf
lspci |grep 82576
虛擬機使用網卡獨佔的方式使用子網卡
virsh nodedev-list|grep 0b
Open vSwitch
> ovs-vsctl add-br br0 #創建網橋
> ovs-vsctl add-port br0 eth1
編輯虛擬機文件
<interface type='bridge'>
<mac address='xx:xx:xx:xx:xx:xx' />
<source bridge='br0'/>
<virtualport type='KVM'/>
<vlan>
<tag id='2'/>
</vlan>
<targetdev='tap1'/>
<model type='Virtio'/>
</interface>
ovs-vsctl show
>ovs-vsctl set port tap1 tag=3 通過改變虛擬機對應的tap網卡設置VLAN tag即可
ovs-vsctl add-br br0
ovs-vsctl add-bond br0 bond0 eth2 eth3 lacp=active
ovs-vsctl set port bond0 bond_mode=balance-slb
ovs-appctl bond/show bond0
連接網格open vSwitch網橋
1.利用VETH設備
ovs-vsctl add-br br0
ovs-vsctl add-br br1
ip link add name veth0 type veth peer name veth1
ovs-vsctl add-port br0 veth0
ovs-vsctl add-port br1 veth1
2.利用Open vSwitch的pacth類型的端口
ovs-vsctl add-br br0
ovs-vsctl add-br br1
ovs-vsctl add-port br0 patch-to-br0
ovs-vsctl set interface patch-to-br0 type=patch
ovs-vsctl set interface patch-to-br0 options:peer=patch-to-br1
ovs-vsctl add-port br1 patch-to-br1
ovs-vsctl set interface patch-to-br1 type=patch
ovs-vsctl set interface patch-to-br1 options:peer=patch-to-br0
宿主機多網卡綁定
cat /etc/modprobe.d/nicbond.conf
alias bond0 bonding
options bond0 mode=1 miimon=100
配置聚合接口
cd /etc/sysconfig/network-scripts
cat ifcfg-bond0
DEVICE=bond0
ONBOOT=YES
BRIDGE=br1
配置子網卡
cat ifcfg-enp4s0f0
HWADDR=
TYPE=Ethernet
BOOTPROTO=none
NAME=enp4s0f0
UUID=
ONBOOT=yes
MASTER=bond0
SLAVE=yes
配置虛擬網橋
cat ifcfg-br1
DEVICE=br1
TYPE=Bridge
IPADDR=192.168.20.200
NETMASK=255.255.255.0
ONBOOT=yes
內容從<深度實踐KVM>一書總結