让你的服务器网卡跑满100G

1,742 阅读3分钟

服务器100G网卡测试

验证服务器跑满100G网卡可行性,和网卡性能优化思路

服务器硬件信息

cpu

$ sudo lscpu
CPU(s):                             128
Model name:                         AMD EPYC 7713 64-Core Processor

网卡

$ sudo lspci |grep -i eth
01:00.0 Ethernet controller: Mellanox Technologies MT27710 Family [ConnectX-4 Lx]
01:00.1 Ethernet controller: Mellanox Technologies MT27710 Family [ConnectX-4 Lx]
41:00.0 Ethernet controller: Mellanox Technologies MT27710 Family [ConnectX-4 Lx]
41:00.1 Ethernet controller: Mellanox Technologies MT27710 Family [ConnectX-4 Lx]
42:00.0 Ethernet controller: Mellanox Technologies MT27710 Family [ConnectX-4 Lx]
42:00.1 Ethernet controller: Mellanox Technologies MT27710 Family [ConnectX-4 Lx]
81:00.0 Ethernet controller: Mellanox Technologies MT2894 Family [ConnectX-6 Lx]
81:00.1 Ethernet controller: Mellanox Technologies MT2894 Family [ConnectX-6 Lx]

# ethtool bond0
Settings for bond0:
……
	Speed: 100000Mb/s
	Duplex: Full
	Auto-negotiation: off
	Port: Other
	PHYAD: 0
	Transceiver: internal
	Link detected: yes

使用iperf对100G网卡进行压测

被测试的机器(7713)

sudo iperf3 -s -p 25001 -B 10.196.160.240

压测客户端机器

sudo iperf3 -c 10.196.160.240  -p 25001 -P 10 -i1 -t 100

直接开测

因为只使用了一台客户端测试服务器,且bond模式使用的layer2+3,根据mac和ip来做hash,导致永远只走一个网口,网卡流量只能跑到10G。修改为layer3+4后,网卡流量可以跑到18G多

$ cat /proc/net/bonding/bond0 |grep "Transmit Hash Policy"
Transmit Hash Policy: layer2+3 (2)
$ cd /sys/class/net/bond0/bonding/
$ echo 'layer3+4' > xmit_hash_policy

现在继续增加客户端

在2台流量40G时候网卡就出现了丢包,所以需要优化一下系统参数

image-20231020145810744.png

image-20231020150003903.png

网卡性能优化

调整Ring Buffer 大小,因为是RX丢包,我们调整一下RX相关的参数

$ sudo ethtool -g enos0
Ring parameters for enos0:
Pre-set maximums:
RX:		8192
RX Mini:	n/a
RX Jumbo:	n/a
TX:		8192
Current hardware settings:
RX:		1024
RX Mini:	n/a
RX Jumbo:	n/a
TX:		1024

# enos0、enos1、enos2、enos3分别修改
$ sudo ip link set enos0 down
$ sudo ethtool -G enos0 rx 4096
$ sudo ip link set enos0 up

$ sudo ethtool -g enos0
Current hardware settings:
RX:		4096

# 固化配置
$ sudo vi /etc/network/interfaces
auto bond0.3306
iface bond0.3306 inet static
vlan_raw_device bond0
address xxx
netmask 255.255.255.0
  pre-up /sbin/ethtool -G enos0 rx 4096
  pre-up /sbin/ethtool -G enos1 rx 4096
  pre-up /sbin/ethtool -G enos2 rx 4096
  pre-up /sbin/ethtool -G enos3 rx 4096

打散网卡的irq绑核

# 查看网卡中断绑定的cpu
for i in $(cat /proc/interrupts | grep enos[0-9]- | awk '{print $1}' | cut -d ':' -f1); do sudo cat /proc/irq/$i/smp_affinity_list; done

可以通过irqbalance来自动绑定,或者自己写脚本将中断打散

sudo apt-get install irqbalance
sudo systemctl start irqbalance
sudo systemctl enable irqbalance

# 如果想手动重新分配中断,请使用以下命令
sudo irqbalance -d

继续增加客户端测试,直接跑满

image-20231020173242808.png

image-20231020173827077.png

测试Nginx的流量

按照之前的优化方法测试nginx的性能,发现下载流量可以跑满网卡,但是上传流量无法跑满。观察cpu使用情况,经常会有单cpu跑满的情况,我们要做的就是继续优化中断绑核,把对应的网卡绑带到自身的numa上。

image-20231106233927121.png

AMDCPU需要在bios下nps:amd cbs ->DF Common Options ->memory addressing ->NUMA nodes per socket开启numa。

$ lscpu
Socket(s):                          1
NUMA node(s):                       2 # 2个numa,因为我们只有2张网卡
Vendor ID:                          AuthenticAMD
CPU family:                         25
Model:                              1
Model name:                         AMD EPYC 7713 64-Core Processor
NUMA node0 CPU(s):                  0-31,64-95
NUMA node1 CPU(s):                  32-63,96-127

根据网卡绑定中断

# 查看网卡的pci号
$ sudo ethtool -i enos0
driver: mlx5_core
version: 5.8-2.0.3
firmware-version: 26.36.1010 (MT_0000000546)
expansion-rom-version:
bus-info: 0000:81:00.1
# 根据pci查看numa情况
$ lspci -s 81:00.1 -vv
01:00.0 Ethernet controller: Mellanox Technologies MT27710 Family [ConnectX-4 Lx]
	NUMA node: 0 # 网卡pci插槽在numa0,所以我们需要把81:00.1的网卡中断绑定到0-31,64-95核上

# 绑定irq
$ set_irq_affinity.sh  0-31,64-95 enos0
$ set_irq_affinity.sh  0-31,64-95 enos1
$ set_irq_affinity.sh  32-63,96-127 enos2
$ set_irq_affinity.sh  32-63,96-127 enos3

这个set_irq_affinity脚本可以在intel的驱动里面获取

总结

因为测试的机器数量较少,我们要充分利用测试机的网卡,所以需要调整bond的hash策略。其实影响网卡收发性能的关键主要就是Ring Buffer和网卡软中断绑定。

优化的大致思路

  • 尽量使用较新的网卡驱动

  • 优化网卡队列,适当增大队列

  • 优化网卡中断,虽然irqbalance可以快速帮助我们打散中断,但是在极致环境下需要考虑到numa的影响

  • 内核参数优化,例如tcp缓冲区配置

    net.ipv4.tcp_rmem = 8192 87380 16777216
    net.ipv4.tcp_wmem = 8192 65536 16777216
    net.ipv4.tcp_mem = 8388608 12582912 16777216 net.ipv4.tcp_window_scaling=1
    

附上内核参数,供参考

sysctl::addition_setting:
  kernel.pid_max: 2821360
  kernel.threads-max: 2821360
  vm.max_map_count: 5642720
  net.ipv4.ip_forward: 1
  net.ipv4.tcp_tw_reuse: 1
  net.ipv4.neigh.default.gc_thresh1: 2048
  net.ipv4.neigh.default.gc_thresh2: 8192
  net.ipv4.neigh.default.gc_thresh3: 16384
  net.ipv4.ip_local_port_range: "12000 65535"
  vm.swappiness: 0
  net.ipv4.conf.default.rp_filter : 0
  net.ipv4.conf.all.rp_filter: 0
  net.ipv4.conf.lo.rp_filter : 0
  net.netfilter.nf_conntrack_max: 8388608