优化前需明确dropped发生的层级(链路层、网络层、传输层或应用层),常用工具:
ethtool -S eth0(查看网卡丢包计数,如rx_discard、tx_discard)、ip -s link show eth0(统计接收/发送丢包数)、netstat -i(查看接口错误率)。netstat -s(统计TCP/UDP丢包原因,如segments retransmitted表示重传)、ss -lnt(查看连接队列状态)、dmesg(查看内核日志中的丢包警告)。tc -s qdisc show dev eth0(检查QoS规则是否导致丢包)。将网卡中断分散到多个CPU核心,减少单核瓶颈。命令:ethtool -l eth0(查看队列数)、ethtool -L eth0 combined 8(设置队列数为8,建议与CPU核心数匹配)。
增大接收/发送缓冲区,避免流量突发时溢出。命令:ethtool -G eth0 rx 8192 tx 8192(设置为8192,突发流量可提升至16384以上)。
将TCP/UDP校验和计算交给网卡,减少CPU负载。命令:ethtool -K eth0 rx-checksum on tx-checksum on。
sysctl -w net.core.netdev_max_backlog=30000(默认1000易丢包)。sysctl -w net.core.somaxconn=8192(默认128太小,需与应用层listen()参数匹配)。sysctl -w net.ipv4.tcp_syncookies=1。根据网络带宽和延迟设置动态缓冲区,提升吞吐量。命令:
sysctl -w net.ipv4.tcp_rmem="4096 131072 16777216"(最小/默认/最大接收窗口)
sysctl -w net.ipv4.tcp_wmem="4096 16384 16777216"(发送窗口)。
BBR算法在高延迟网络(如跨机房)中表现更优,提升吞吐并减少丢包。命令:sysctl -w net.ipv4.tcp_congestion_control=bbr。
sysctl -w vm.swappiness=10(默认60太高,建议10以下)。sysctl -w net.ipv4.tcp_mem="16777216 16777216 16777216"(单位:字节,根据服务器内存调整)。sysctl -w net.ipv4.tcp_syn_retries=2(默认5次)。sysctl -w net.ipv4.tcp_tw_reuse=1(允许复用TIME_WAIT状态的连接)。sysctl -w net.ipv4.tcp_fastopen=3(客户端+服务端均需开启)。worker_connections 65535;(在nginx.conf中设置)。innodb_buffer_pool_size=4G(占物理内存的50%-70%),减少磁盘IO。使用tc工具标记关键业务流量(如HTTP端口80),给予更高优先级:
tc qdisc add dev eth0 root handle 1: htb default 30
tc class add dev eth0 parent 1: classid 1:1 htb rate 1Gbit ceil 1Gbit
tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dport 80 0xffff flowid 1:1
将网卡中断绑定到特定CPU核心,减少上下文切换。命令:
echo 0f > /proc/irq/$(cat /proc/interrupts | grep eth0 | awk -F: '{print $1}')/smp_affinity(将中断绑定到CPU0和1)。
watch -n 1 "ethtool -S eth0 | grep -E 'discard|error'"监控网卡丢包,nload -u M eth0监控带宽利用率。journalctl -u network.service查看网络服务日志,定位异常丢包原因。iperf3测试网络吞吐量,ping测试延迟,mtr分析路径丢包。以上优化需根据实际场景(如服务器角色、网络带宽、应用类型)调整参数,并通过监控验证效果。优化后需重启网络服务(systemctl restart network)或服务器使参数生效。