tcp_max_syn_backlog 和 somaxconn 有什么区别?

What is the difference between tcp_max_syn_backlog and somaxconn?

我在 Linux 上阅读了一些关于 TCP 实现的文章,但我很困惑,net.ipv4.tcp_max_syn_backlognet.core.somaxconn 之间有什么区别以及传递为 backlog listen() 系统调用的参数,它们之间的关系是什么。

P.S。我想要内核 4.15 的解释,因为我发现在这个问题上旧内核和新内核之间存在一些差异。

sysctl 是一个 API。所以你可以只读 Linux 内核 documentation for appropriate version:

tcp_max_syn_backlog - INTEGER
    Maximal number of remembered connection requests, which have not
    received an acknowledgment from connecting client.
    The minimal value is 128 for low memory machines, and it will
    increase in proportion to the memory of machine.
    If server suffers from overload, try increasing this number.

somaxconn - INTEGER
    Limit of socket listen() backlog, known in userspace as SOMAXCONN.
    Defaults to 128.  See also tcp_max_syn_backlog for additional tuning
    for TCP sockets.

让我们考虑一个 TCP-handshake.. tcp_max_syn_backlog represents the maximal number of connections in SYN_RECV queue. I.e. when your server received SYN, sent SYN-ACK and haven't received ACK yet. This is a separate queue of so-called "request sockets" - reqsk in code (i.e. not fully-fledged sockets, "request sockets" occupy less memory. In this state we can save some memory and not yet allocate a full socket because the full connection may not be at all in the future if ACK will not arrive). The value of this queue is affected (see ) by listen()backlog 参数并在内核中受到 tcp_max_syn_backlog 的限制。

somaxconn 表示 ESTABLISHED 队列的最大大小。这是另一个队列。
回想一下前面提到的 SYN_RECV 队列——您的服务器正在等待来自客户端的 ACK。当 ACK 到达时,内核粗略地说从“请求套接字”中生成成熟的大套接字并将其移至 ESTABLISHED 队列。然后你可以在这个套接字上做accept()。此队列也受 listen()backlog 参数影响,并受内核中的 somaxconn 限制。

有用链接:1, 2.