Linux TCP Tuning

To change TCP settings in Linux, add the entries below to the file /etc/sysctl.conf, and then run sysctl -p. The system will also set these values at boot time.

# increase TCP maximum buffer size
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

# increase Linux autotuning TCP buffer limits
# min, default, and maximum number of bytes to use
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

Set the maximum buffer sizes to a value large enough to handle the longest, fastest network link you think that host will encounter (16MB in the example above). Windows does not require any modifications, as the default maximum TCP buffer size (GlobalMaxTcpWindowSize) is not defined. My TCP Tuning Guide web site has information on how to set the maximum buffer size for other OSes.

Tuning TCP for Linux 2.4 and 2.6

NB: Manually adjusting socket buffer sizes with setsockopt() disables autotuning. Application that are optimized for other operating systems may implicitly defeat Linux autotuning.

The following values (which are the defaults for 2.6.17 with more than 1 GByte of memory) would be reasonable for all paths with a 4MB BDP or smaller (you must be root):

	echo 1 > /proc/sys/net/ipv4/tcp_moderate_rcvbuf
       	echo 108544 > /proc/sys/net/core/wmem_max
       	echo 108544 > /proc/sys/net/core/rmem_max
       	echo "4096 87380 4194304" > /proc/sys/net/ipv4/tcp_rmem
       	echo "4096 16384 4194304" > /proc/sys/net/ipv4/tcp_wmem

    
All standard advanced TCP features are on by default. You can check them by: cat /proc/sys/net/ipv4/tcp_timestamps cat /proc/sys/net/ipv4/tcp_window_scaling cat /proc/sys/net/ipv4/tcp_sack Linux supports both /proc and sysctl (using alternate forms of the variable names) Eg. net.core.rmem_max) for inspecting and adjusting network tuning parameters. The following is a useful shortcut for inspecting all tcp parameters: sysctl -a | fgrep tcp To detect network errors and signaling connection problems, You can enable TCP keep alive feature. It will increase signaling bandwidth used, but as bandwidth utilized by signaling channels is low from its nature,the increase should not be significant.Moreover, you can control it using keep alive timeout. The problem is that most system use keep alive timeout of 7200 seconds, which means the system is notified about a dead connection after 2 hours. You probably want this time to be shorter, like one minute or so. On each operating system, the adjustment is done in a different way. After settings all parameters, it's recommended to check whether the feature works correctly - just make a test call and unplug a network cable at either side of the call. Then see if the call terminates after the configured timeout. Here are some hints: Linux systems: Use sysctl -A to get a list of available kernel variables and grep this list for net.ipv4 settings (sysctl -A | grep net.ipv4). There should exist the following variables: - net.ipv4.tcp_keepalive_time - time of connection inactivity after which The first keep alive request is sent - net.ipv4.tcp_keepalive_probes - number of keep alive requests retransmitted before the connection is considered broken - net.ipv4.tcp_keepalive_intvl - time interval between keep alive probes You can manipulate with these settings using the following command: sysctl -w net.ipv4.tcp_keepalive_time=60 net.ipv4.tcp_keepalive_probes=3 net.ipv4.tcp_keepalive_intvl=10 This sample command changes TCP keepalive timeout to 60 seconds with 3 probes, 10 seconds gap between each. With this, your application will detect dead TCP connections after 90 seconds (60 + 10 + 10 + 10).
You can decrease the net.ipv4.netfilter.ip_conntrack_tcp_timeout_established, by half, at least. You can decrease also the following. net.ipv4.netfilter.ip_conntrack_tcp_max_retrans = 3 net.ipv4.netfilter.ip_conntrack_tcp_be_liberal = 0 net.ipv4.netfilter.ip_conntrack_tcp_loose = 1 net.ipv4.netfilter.ip_conntrack_tcp_timeout_max_retrans = 300 net.ipv4.netfilter.ip_conntrack_tcp_timeout_close = 10 net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 120 net.ipv4.netfilter.ip_conntrack_tcp_timeout_last_ack = 30 net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait = 60 net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 120 net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 14400 net.ipv4.netfilter.ip_conntrack_tcp_timeout_syn_recv = 60 net.ipv4.netfilter.ip_conntrack_tcp_timeout_syn_sent = 120 sysctl -w net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=14400 (14400 = 4hours)