分类: 系统运维
2015-08-25 11:56:14
The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later re-attempt at connection succeeds.
and a very important note
If the backlog argument is greater than the value in
/proc/sys/net/core/somaxconn
, then it is silently truncated to that value; the default value in this file is 128. In kernels before 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value 128.
We were dropping packets since the backlog queue was filling up. Worse, clients will wait 3 seconds before re-sending the SYN, and then 9 seconds if that SYN doesn’t get through again.
Another symptom we saw when looking at /var/log/messages was this message showing up
[84440.731929] possible SYN flooding on port 80. Sending cookies.
Were we being SYN flooded? Not an unreasonable thing to expect with the servers exposed to the internet, but it turns out this message can send you looking in the wrong direction. Couldn’t we just turn off After fixing the backlog issue, it was time to review our existing sysctl settings. We’ve had some tunings in place for a while but it had been some time since they were reviewed to ensure they still made sense for us. There’s a lot of bad information out on the web on tuning TCP settings under sysctl that people just blindly apply to their servers. Often times these resources don’t bother explaining why they are setting a certain sysctl parameter and just give you a file to put in place and tell you this will give you the best performance. You should be sure you fully understand any value you are changing under sysctl. You can seriously affect the performance of your server with the wrong values or certain options even enabled in the wrong environments. The TCP man page and TCP/IP Illustrated: The Implementation, Vol 2 were great resources in helping to understand these parameters.
Our current sysctl modifications as they stand today are as follows (included with comments), Disclaimer: please don’t just use these settings on your servers without understanding them first
# Max receive buffer size (8 Mb)
net.core.rmem_max=8388608
# Max send buffer size (8 Mb)
net.core.wmem_max=8388608# Default receive buffer size
net.core.rmem_default=65536
# Default send buffer size
net.core.wmem_default=65536# The first value tells the kernel the minimum receive/send buffer for each TCP connection,
# and this buffer is always allocated to a TCP socket,
# even under high pressure on the system. …
# The second value specified tells the kernel the default receive/send buffer
# allocated for each TCP socket. This value overrides the /proc/sys/net/core/rmem_default
# value used by other protocols. … The third and last value specified
# in this variable specifies the maximum receive/send buffer that can be allocated for a TCP socket.
# Note: The kernel will auto tune these values between the min-max range
# If for some reason you wanted to change this behavior, disable net.ipv4.tcp_moderate_rcvbuf
net.ipv4.tcp_rmem=8192 873800 8388608
net.ipv4.tcp_wmem=4096 655360 8388608# Units are in page size (default page size is 4 kb)
# These are global variables affecting total pages for TCP
# sockets
# 8388608 * 4 = 32 GB
# low pressure high
# When mem allocated by TCP exceeds “pressure”, kernel will put pressure on TCP memory
# We set all these values high to basically prevent any mem pressure from ever occurring
# on our TCP sockets
net.ipv4.tcp_mem=8388608 8388608 8388608# Increase max number of sockets allowed in TIME_WAIT
net.ipv4.tcp_max_tw_buckets=6000000# Increase max half-open connections.
net.ipv4.tcp_max_syn_backlog=65536# Increase max TCP orphans
# These are sockets which have been closed and no longer have a file handle attached to them
net.ipv4.tcp_max_orphans=262144# Max listen queue backlog
# make sure to increase nginx backlog as well if changed
net.core.somaxconn = 16384# Max number of packets that can be queued on interface input
# If kernel is receiving packets faster than can be processed
# this queue increases
net.core.netdev_max_backlog = 16384# Only retry creating TCP connections twice
# Minimize the time it takes for a connection attempt to fail
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2# Timeout closing of TCP connections after 7 seconds
net.ipv4.tcp_fin_timeout = 7# Avoid falling back to slow start after a connection goes idle
# keeps our cwnd large with the keep alive connections
net.ipv4.tcp_slow_start_after_idle = 0