..标题:系统缓冲区调整 - TCP 和网络性能优化 .. slug:系统缓冲区调整 .. 日期: 2026-02-02 10:00:00 世界标准时间 .. 标签:网络、性能、调整、tcp、缓冲区 ..类别:文章 ..链接: .. 描述:了解和优化系统缓冲区以解决经常被误诊为网络问题的 TCP 性能问题 ..类型:文本
网络工程师经常遇到将 TCP 窗口或应用程序性能归咎于网络基础设施的情况。在执行大量数据包捕获、tcpdump 和网络分析后,通常会发现真正的瓶颈:客户端或服务器系统上的 NIC(网络接口卡)或操作系统级缓冲区已耗尽。
本文提供了适用于 Linux、Windows 和 macOS 的旧版(大约 2009 年)和当前版(2025-2026 年)缓冲区配置,以及在缓冲区耗尽成为严重问题之前识别缓冲区耗尽的诊断技术。
TCP 使用流量控制机制,其中接收方通告“窗口大小”,指示它可以接受多少数据。当系统缓冲区填满时,该窗口缩小为零,迫使发送方等待。这看起来是网络问题,但实际上是主机资源问题。
# Check current TCP buffer settings sysctl net.ipv4.tcp_rmem sysctl net.ipv4.tcp_wmem sysctl net.core.rmem_max sysctl net.core.wmem_max # Check NIC ring buffer sizes ethtool -g eth0 # Monitor socket buffer usage ss -tm # Check for TCP zero window events tcpdump -i any 'tcp[tcpflags] & tcp-push != 0' -vv # Check network statistics for buffer issues netstat -s | grep -i "buffer\|queue\|drop"
# Check TCP parameters
netsh interface tcp show global
# View network adapter buffer settings
Get-NetAdapterAdvancedProperty -Name "Ethernet" | Where-Object {$_.DisplayName -like "*buffer*"}
# Monitor TCP statistics
netstat -s -p tcp
# Check receive window auto-tuning
netsh interface tcp show global | findstr "Receive Window"
# Check current buffer settings sysctl kern.ipc.maxsockbuf sysctl net.inet.tcp.sendspace sysctl net.inet.tcp.recvspace # View network statistics netstat -s -p tcp # Monitor socket buffers netstat -an -p tcp
| 范围 | 遗产价值 (2009) | 描述 |
|---|---|---|
| net.core.rmem_default | 124928 (122KB) | 默认接收套接字缓冲区大小 |
| 网络核心.rmem_max | 131071 (128KB) | 最大接收套接字缓冲区大小 |
| net.core.wmem_default | 124928 (122KB) | 默认发送套接字缓冲区大小 |
| 网络核心.wmem_max | 131071 (128KB) | 最大发送套接字缓冲区大小 |
| net.ipv4.tcp_rmem | 4096 87380 174760 | TCP 接收缓冲区:最小、默认、最大(以字节为单位) |
| net.ipv4.tcp_wmem | 4096 16384 131072 | TCP 发送缓冲区:最小、默认、最大(以字节为单位) |
| net.ipv4.tcp_mem | 196608 262144 393216 | TCP内存页:低、压力、高 |
| net.core.netdev_max_backlog | 1000 | 输入队列中的最大数据包数 |
| 网络核心.optmem_max | 10240 (10KB) | 每个套接字的最大辅助缓冲区大小 |
| 范围 | 当前推荐值 | 描述 |
|---|---|---|
| net.core.rmem_default | 16777216 (16MB) | 默认接收套接字缓冲区大小 |
| 网络核心.rmem_max | 134217728 (128MB) | 最大接收套接字缓冲区大小 |
| net.core.wmem_default | 16777216 (16MB) | 默认发送套接字缓冲区大小 |
| 网络核心.wmem_max | 134217728 (128MB) | 最大发送套接字缓冲区大小 |
| net.ipv4.tcp_rmem | 4096 87380 134217728 | TCP 接收缓冲区:最小、默认、最大(最大 128MB) |
| net.ipv4.tcp_wmem | 4096 65536 134217728 | TCP 发送缓冲区:最小、默认、最大(最大 128MB) |
| net.ipv4.tcp_mem | 8388608 12582912 16777216 | TCP内存页:低、压力、高(64GB系统) |
| net.core.netdev_max_backlog | 250000 | 输入队列中的最大数据包 (10GbE+) |
| 网络核心.optmem_max | 65536 (64KB) | 每个套接字的最大辅助缓冲区大小 |
| net.ipv4.tcp_congestion_control | 伯伯 | 使用BBR拥塞控制(Google的算法) |
| net.ipv4.tcp_window_scaling | 1 | 启用 TCP 窗口缩放 (RFC 1323) |
| net.ipv4.tcp_timestamps | 1 | 启用 TCP 时间戳以更好地估计 RTT |
| net.ipv4.tcp_sack | 1 | 启用选择性确认 |
| net.ipv4.tcp_no_metrics_save | 1 | 禁用 TCP 指标缓存 |
将这些设置添加到/etc/sysctl.conf或创建一个新文件/etc/sysctl.d/99-network-tuning.conf:
# Network Buffer Tuning for High-Performance Applications # Optimized for 10GbE+ networks with RTT up to 300ms # Core socket buffer settings net.core.rmem_default = 16777216 net.core.rmem_max = 134217728 net.core.wmem_default = 16777216 net.core.wmem_max = 134217728 # TCP buffer settings net.ipv4.tcp_rmem = 4096 87380 134217728 net.ipv4.tcp_wmem = 4096 65536 134217728 net.ipv4.tcp_mem = 8388608 12582912 16777216 # Device buffer settings net.core.netdev_max_backlog = 250000 net.core.netdev_budget = 50000 net.core.netdev_budget_usecs = 5000 net.core.optmem_max = 65536 # TCP optimizations net.ipv4.tcp_congestion_control = bbr net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_timestamps = 1 net.ipv4.tcp_sack = 1 net.ipv4.tcp_no_metrics_save = 1 net.ipv4.tcp_moderate_rcvbuf = 1 # Apply with: sysctl -p /etc/sysctl.d/99-network-tuning.conf
# Check current ring buffer sizes ethtool -g eth0 # Set maximum ring buffer sizes (adjust based on NIC capabilities) ethtool -G eth0 rx 4096 tx 4096 # Make persistent by adding to /etc/network/interfaces or systemd service
| 范围 | 遗产价值 (2009) | 地点 |
|---|---|---|
| TCP窗口大小 | 65535 (64KB) | 注册表:HKLM\System\CurrentControlSet\Services\Tcpip\Parameters |
| TCP1323选项 | 0(禁用) | 默认情况下禁用窗口缩放 |
| 默认接收窗口 | 8192 (8KB) | 默认接收窗口 |
| 默认发送窗口 | 8192 (8KB) | 默认发送窗口 |
| 全局最大Tcp窗口大小 | 65535 (64KB) | 最大 TCP 窗口大小 |
| Tcp连接数 | 16777214 | 最大 TCP 连接数 |
现代 Windows 使用接收窗口自动调谐功能,可根据网络状况动态调整接收缓冲区。
| 特征 | 当前推荐设置 | 描述 |
|---|---|---|
| 自动调整电平 | 正常(或 10GbE+ 高度实验性) | 动态接收窗口调整 |
| 接收端缩放 (RSS) | 已启用 | 跨 CPU 分配网络处理 |
| 烟囱卸载 | 自动(或在现代 NIC 上禁用) | TCP 卸载到 NIC 硬件 |
| 网络DMA | 残疾人 | 直接内存访问(已弃用) |
| TCP 全局参数 | 请参阅下面的命令 | 系统范围的 TCP 设置 |
| 拥塞提供商 | CUBIC(或 NewReno 后备) | TCP拥塞控制算法 |
# Check current auto-tuning level netsh interface tcp show global # Enable auto-tuning (normal mode - default for most scenarios) netsh interface tcp set global autotuninglevel=normal # For high-bandwidth, high-latency networks (10GbE+, data center environments) netsh interface tcp set global autotuninglevel=experimental # For conservative tuning (if experimental causes issues) netsh interface tcp set global autotuninglevel=restricted # For very conservative tuning (not recommended for high-performance networks) netsh interface tcp set global autotuninglevel=highlyrestricted # Enable CUBIC congestion provider (Windows Server 2022/Windows 11+ only) netsh interface tcp set supplemental template=Internet congestionprovider=cubic # Note: Windows 10 and Server 2019 use Compound TCP or NewReno by default # CUBIC is not available on these older versions # Enable Receive-Side Scaling (RSS) netsh interface tcp set global rss=enabled # Set chimney offload (automatic is recommended) netsh interface tcp set global chimney=automatic # Disable NetDMA (recommended for modern systems) netsh interface tcp set global netdma=disabled # Enable Direct Cache Access (if supported) netsh interface tcp set global dca=enabled # Enable ECN (Explicit Congestion Notification) netsh interface tcp set global ecncapability=enabled # Set initial congestion window to 10 (RFC 6928) netsh interface tcp set global initialRto=3000
# View current adapter settings Get-NetAdapterAdvancedProperty -Name "Ethernet" # Increase receive buffers (adjust based on NIC) Set-NetAdapterAdvancedProperty -Name "Ethernet" -DisplayName "Receive Buffers" -DisplayValue 2048 # Increase transmit buffers Set-NetAdapterAdvancedProperty -Name "Ethernet" -DisplayName "Transmit Buffers" -DisplayValue 2048 # Enable Jumbo Frames (if network supports it) Set-NetAdapterAdvancedProperty -Name "Ethernet" -DisplayName "Jumbo Packet" -DisplayValue 9014 # Enable Large Send Offload (LSO) Set-NetAdapterAdvancedProperty -Name "Ethernet" -DisplayName "Large Send Offload V2 (IPv4)" -DisplayValue Enabled Set-NetAdapterAdvancedProperty -Name "Ethernet" -DisplayName "Large Send Offload V2 (IPv6)" -DisplayValue Enabled
# These settings are typically NOT needed on Windows 10/11 due to auto-tuning # Only modify if auto-tuning is disabled or problematic # Registry path: HKLM\System\CurrentControlSet\Services\Tcpip\Parameters # Maximum TCP window size (if auto-tuning disabled) # TcpWindowSize = 16777216 (16MB) - REG_DWORD # Enable window scaling (enabled by default on modern Windows) # Tcp1323Opts = 3 - REG_DWORD # Number of TCP Timed Wait Delay # TcpTimedWaitDelay = 30 - REG_DWORD (default 240)
| 范围 | 遗产价值 (2009) | 描述 |
|---|---|---|
| kern.ipc.maxsockbuf | 262144 (256KB) | 最大套接字缓冲区大小 |
| net.inet.tcp.sendspace | 32768 (32KB) | 默认 TCP 发送缓冲区 |
| net.inet.tcp.recvspace | 32768 (32KB) | 默认 TCP 接收缓冲区 |
| net.inet.tcp.autorcvbufmax | 131072 (128KB) | 最大自动调整接收缓冲区 |
| net.inet.tcp.autosndbufmax | 131072 (128KB) | 最大自动调整发送缓冲区 |
| 网络.inet.tcp.rfc1323 | 0(禁用) | TCP 窗口缩放 |
| 范围 | 当前推荐值 | 描述 |
|---|---|---|
| kern.ipc.maxsockbuf | 8388608 (8MB) | 最大套接字缓冲区大小 |
| net.inet.tcp.sendspace | 131072 (128KB) | 默认 TCP 发送缓冲区 |
| net.inet.tcp.recvspace | 131072 (128KB) | 默认 TCP 接收缓冲区 |
| net.inet.tcp.autorcvbufmax | 16777216 (16MB) | 最大自动调整接收缓冲区 |
| net.inet.tcp.autosndbufmax | 16777216 (16MB) | 最大自动调整发送缓冲区 |
| 网络.inet.tcp.rfc1323 | 1(启用) | 启用 TCP 窗口缩放 |
| 网络.inet.tcp.sack | 1(启用) | 启用选择性确认 |
| net.inet.tcp.mssdflt | 1440 | 默认 TCP 最大段大小 |
| net.inet.tcp.delayed_ack | 3 | 延迟 ACK 行为 |
# Check current settings sysctl kern.ipc.maxsockbuf sysctl net.inet.tcp.sendspace sysctl net.inet.tcp.recvspace sysctl net.inet.tcp.autorcvbufmax sysctl net.inet.tcp.autosndbufmax # Apply settings temporarily (until reboot) sudo sysctl -w kern.ipc.maxsockbuf=8388608 sudo sysctl -w net.inet.tcp.sendspace=131072 sudo sysctl -w net.inet.tcp.recvspace=131072 sudo sysctl -w net.inet.tcp.autorcvbufmax=16777216 sudo sysctl -w net.inet.tcp.autosndbufmax=16777216 sudo sysctl -w net.inet.tcp.rfc1323=1 sudo sysctl -w net.inet.tcp.sack=1 # Make settings persistent (create /etc/sysctl.conf) sudo tee /etc/sysctl.conf <<EOF kern.ipc.maxsockbuf=8388608 net.inet.tcp.sendspace=131072 net.inet.tcp.recvspace=131072 net.inet.tcp.autorcvbufmax=16777216 net.inet.tcp.autosndbufmax=16777216 net.inet.tcp.rfc1323=1 net.inet.tcp.sack=1 net.inet.tcp.mssdflt=1440 net.inet.tcp.delayed_ack=3 EOF # Note: On recent macOS versions, /etc/sysctl.conf may not be read automatically # Use a LaunchDaemon to apply settings at boot
# Create /Library/LaunchDaemons/com.local.sysctl.plist
sudo tee /Library/LaunchDaemons/com.local.sysctl.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.local.sysctl</string>
<key>ProgramArguments</key>
<array>
<string>/usr/sbin/sysctl</string>
<string>-w</string>
<string>kern.ipc.maxsockbuf=8388608</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
sudo chmod 644 /Library/LaunchDaemons/com.local.sysctl.plist
sudo launchctl load /Library/LaunchDaemons/com.local.sysctl.plist
# Server side iperf3 -s # Client side - test TCP throughput iperf3 -c server_ip -t 60 -i 5 -w 16M # Test with multiple parallel streams iperf3 -c server_ip -P 10 -t 60 # Test UDP performance iperf3 -c server_ip -u -b 1000M -t 60
# Capture and display TCP window sizes tcpdump -i any -n 'tcp' -vv | grep -i window # Save capture for Wireshark analysis tcpdump -i any -w /tmp/capture.pcap 'tcp port 443'
查找缓冲区问题的这些指标:
# Linux - Monitor network buffer statistics watch -n 1 'cat /proc/net/sockstat' watch -n 1 'ss -tm | grep -i mem' # Check for drops netstat -s | grep -i drop # Windows - Monitor TCP statistics netstat -e 1 # macOS - Monitor network statistics netstat -s -p tcp
要确定网络的最佳缓冲区大小,请计算带宽延迟积:
BDP = Bandwidth (bits/sec) × RTT (seconds) Example for 10 Gigabit Ethernet with 50ms RTT: BDP = 10,000,000,000 × 0.050 = 500,000,000 bits = 62.5 MB Buffer Size = BDP × 2 (for bidirectional traffic and headroom) Buffer Size = 62.5 MB × 2 = 125 MB This is why modern settings recommend 128MB maximum buffers.
| 工作负载类型 | 推荐的缓冲区大小 | 关键参数 |
|---|---|---|
| 网络服务器(低延迟) | 4-16MB | 更低的缓冲区、更多的连接、更快的响应 |
| 数据库服务器 | 16-32MB | 中等缓冲区,稳定的吞吐量 |
| 文件传输/备份 | 64-128MB | 最大缓冲区,高吞吐量优先级 |
| 视频流 | 32-64MB | 缓冲区大,投递率稳定 |
| 高性能计算/数据中心 | 128-256MB | 最大缓冲区、专门的拥塞控制 |
| 无线/移动 | 2-8MB | 保守的缓冲区、可变延迟处理 |
缓冲区耗尽是与网络相关的性能问题的常见根本原因。通过了解缓冲区大小从 2009 年的 128KB 限制到如今的 128MB 功能的演变,网络工程师可以快速识别并解决这些问题。
要点:
请记住:通过数据包分析显示 TCP 零窗口的“网络问题”实际上是主机系统资源问题。通过适当的缓冲区调整,您可以消除这些错误诊断并实现最佳性能。
最后更新时间:2026 年 2 月 2 日
作者:Baud9600技术团队