TCP/IP backlog


在linux中,当一个应用创建一个处于listen状态的socket时,会为这个socket指定两条队列。

  • SYN queue:存放处于syn_rec状态的socket,由内核参数tcp_max_syn_backlog指定。

  • accept queue:存放处于ESTABLISHED状态的socket,由内核参数somaxconn和应用本身指定,如:redis:tcp-backlog,取somaxconn和tcp-backlog中的最小值。

  • 在accept queue中的socket会被应用程序进行消费。


syn queue未满,而accept queue已满,此时:

1)当tcp第三次握手的ACK到达server,而此时accept queue已经满时,内核会丢失这个ACK包。

2)由于TCP的重传机制,sever端在超时时间内未收到ACK,server会重传SYN+ACK,client端重传ACK包。

3)在/proc/sys/net/ipv4/tcp_synack_retries设定的重试次数超过后还没正常的收到ACK包,就不再重试。


在监听状态的socket

State      Recv-Q Send-Q                                  Local Address:Port                                    Peer Address:Port 
LISTEN     0      100                                                 *:7000                                               *:*     
LISTEN     0      100                                                 *:7800                                               *:*     
LISTEN     0      50                                                  *:58201                                              *:*     
LISTEN     0      50                                                  *:44761                                              *:*     
LISTEN     0      100                                                 *:7001                                               *:*     
LISTEN     0      512                                                 *:6265                                               *:*     
LISTEN     0      50                                                  *:60026                                              *:*   
Recv-Q:处于accept queue的队列长度
Send-Q:accept queue能容纳的总长度


非监听状态的socket

State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port 
CLOSE-WAIT 1      0               10.11.11.66:45694         10.11.11.227:8800  
TIME-WAIT  0      0               10.13.13.66:9800           10.13.13.23:46128 
ESTAB      0      0               10.13.13.66:39618          10.13.13.82:6380  
ESTAB      0      0               10.13.13.66:7800           10.13.13.24:45343 
ESTAB      0      0               10.13.13.66:8201           10.13.13.24:54330 
ESTAB      0      0               10.13.13.66:9800           10.13.13.23:44531 
ESTAB      0      0               10.11.11.66:43312         10.11.11.183:6001  
ESTAB      0      0               10.13.13.66:45206          10.13.13.83:6380  
CLOSE-WAIT 1      0               10.11.11.66:52191         10.11.11.227:8800  
Recv-Q:The count of bytes not copied by the user program connected to this socket.
Send-Q:The count of bytes not acknowledged by the remote host.


参考文章:

http://veithen.github.io/2014/01/01/how-tcp-backlog-works-in-linux.html

http://blog.itpub.net/15480802/viewspace-1399303/


你可能感兴趣的:(tcp,backlog)