7.6 虚拟机网卡多队列
虚拟机使用virtio系列网卡时(包括virtio-net/vhost-net/vhost-user), virtio本身支持一个设备多队列,使用多队列可以有效提升虚拟机的网络吞吐量。虚拟机多队列数量最好和vcpu数量保持一致,可以保证虚拟机中一个vcpu处理一个队列的数据。
下面以16个VCPU为例,配置virtio网卡为vhost方式,队列数量为16
1. libvirt配置多队列
<interface type='bridge'>
<mac address='52:54:00:c4:8f:52'/>
<source bridge='br0'/>
<model type='virtio'/>
<driver name='vhost' queues='16'/> //必须使用vhost或者virtio
<address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0'/>
</interface>
2. 虚拟机中使能多队列
# ethtool -l enp3s0
Channel parameters for enp3s0:
Pre-set maximums:
RX: 0
TX: 0
Other: 0
Combined: 16 #表示当前支持16个队列
Current hardware settings:
RX: 0
TX: 0
Other: 0
Combined: 1 #表示当前生效的是1个队列
使能多队列:
# ethtool -L eth0 combined 16 # 设置eth0当前使用16个队列
# ethtool -l enp3s0
Channel parameters for enp3s0:
Pre-set maximums:
RX: 0
TX: 0
Other: 0
Combined: 16
Current hardware settings:
RX: 0
TX: 0
Other: 0
Combined: 16 //当前生效16个队列
虚拟机中需要检查irqbalance服务状态,确保每个队列能够均匀分布到不同的vcpu上,也可以手动设置/proc/irq/{irqnum}/smp_affinity
绑定中断的亲和性
# systemctl status irqbalance
● irqbalance.service - irqbalance daemon
Loaded: loaded (/usr/lib/systemd/system/irqbalance.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-09-10 16:17:13 CST; 1 weeks 6 days ago
Main PID: 612 (irqbalance)
Tasks: 2 (limit: 103032)
Memory: 2.7M
CGroup: /system.slice/irqbalance.service
└─612 /usr/sbin/irqbalance --foreground
Nov 10 16:17:13 dev54 systemd[1]: Started irqbalance daemon.
# cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3 CPU4 CPU5 CPU6 CPU7 CPU8 CPU9 CPU10 CPU11 CPU12 CPU13 CPU14 CPU15
98: 102691 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 PCH-MSI-EXT 105 virtio1-input.0
99: 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 PCH-MSI-EXT 106 virtio1-output.0
100: 0 295358 0 0 0 0 0 0 0 0 0 0 0 0 0 0 PCH-MSI-EXT 107 virtio1-input.1
101: 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 PCH-MSI-EXT 108 virtio1-output.1
102: 0 0 306137 0 0 0 0 0 0 0 0 0 0 0 0 0
......
3. 物理机上vhost-net线程优化
当配置使用vhost-net类型网卡时,每一个队列在宿主机内核中会启动一个线程vhost-
# ps aux|grep vhost
root 14301 0.2 0.0 0 0 ? S 21:05 0:15 [vhost-14255]
root 14302 0.4 0.0 0 0 ? S 21:05 0:25 [vhost-14255]
...