Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between tcp_max_syn_backlog and somaxconn?

I have been reading some articles about TCP implementation on Linux and I got confused, what is the difference between net.ipv4.tcp_max_syn_backlog and net.core.somaxconn and the backlog passed as parameter to listen() system call, and what is the relation between them.

P.S. I want explanation for kernel 4.15 because I found that there are some differences between oldest and newer kernels on this subject.

like image 975
Random Avatar asked Jun 29 '20 15:06

Random


People also ask

What is Tcp_max_syn_backlog?

tcp_max_syn_backlog : 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.

What is Somaxconn?

SOMAXCONN defines the maximum number you're allowed to pass to listen() which is 128 on my system. You can read more about it in the man page. Follow this answer to receive notifications.

What is net core Netdev_max_backlog?

net. core. netdev_max_backlogThis parameter sets the maximum size of the network interface's receive queue. The queue is used to store received frames after removing them from the network adapter's ring buffer.

What is net ipv4 Tcp_rmem?

net. ipv4. tcp_rmem. Contains three values that represent the minimum, default and maximum size of the TCP socket receive buffer. The minimum represents the smallest receive buffer size guaranteed, even under memory pressure.


1 Answers

sysctl is an API. So you can just read the Linux kernel 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.

Let's consider a 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 this post) by listen()'s backlog argument and limited by tcp_max_syn_backlog in kernel.

somaxconn represents the maximal size of ESTABLISHED queue. This is another queue.
Recall the previously mentioned SYN_RECV queue - your server is waiting for ACK from client. When the ACK arrives the kernel roughly speaking makes the big full-fledged socket from "request socket" and moves it to ESTABLISHED queue. Then you can do accept() on this socket. This queue is also affected by listen()'s backlog argument and limited by somaxconn in kernel.

Useful links: 1, 2.

like image 107
red0ct Avatar answered Oct 20 '22 00:10

red0ct