Linux kernel network parameter tuning commands

ulimit -n

ulimit -n is used to view or set the maximum value of open file descriptors for the current user process. A file descriptor is an abstract representation used by the operating system to access files or other I/O resources. In Linux and Unix-like systems, each process can have a certain number of file descriptors open at the same time.

You can use the ulimit -n command to view the maximum number of file descriptors allowed to be opened by the current user process. If no special settings are made, this value usually has a default limit.

For example, run the following command to view the maximum number of file descriptors allowed to be opened by the current user process:

ulimit -n

The output displays a number indicating the current limit of the maximum number of file descriptors. If the output is "unlimited", it means that no specific limit is set and any number of file descriptors can be opened.

It should be noted that the ulimit -n command only takes effect on the current shell session. Re-opening a new terminal window after exiting the session will use the system's default limits. If you need to permanently change the maximum value of file descriptors, you need to set it in the system configuration file, such as modifying the configuration file under /etc/security/limits.conf or /etc/security/limits.d/, and then log in again to take effect. Please note, however, that you should be careful when modifying system limits to ensure that you do not negatively impact system stability and performance.

The results output by the ulimit -n command are only valid for the current user's process. Each user can independently set the maximum number of file descriptors allowed to be opened by his or her process, thus preventing one user's process from affecting other users' processes.

For an entire Linux server, the maximum number of file handles depends on several factors, including hardware configuration, kernel parameter settings, and system usage. The Linux system will limit the number of file handles on the entire server based on these configurations and parameters.
In Linux, the limit on the number of file handles mainly involves two parameters:

ulimit: used to limit the number of user-level file descriptors, which is a user-level resource limit.
fs.file-max: used to limit the number of file descriptors at the entire system level, which is a system-level resource limit.

The system-level file descriptor limit can be viewed and modified through the /proc/sys/fs/file-max file. Use the following command to view the file descriptor limit of the current system:

somaxconn Socket parameters

somaxconn is a parameter used to control the length of the server listening queue in Linux systems, also known as the TCP maximum number of connections parameter. It determines the upper limit of the maximum number of connections that the server can accept.
In Linux, when a client attempts to connect to the server, the server will put the connection in a queue waiting for processing. The somaxconn parameter defines the length of this queue, which is the maximum number of connections that can be queued for processing at the same time.
If the number of concurrent connections of the server is high and the server processes connections very quickly, the queue length may not be enough, causing the client connection to be rejected. To avoid this, you can increase the length of the queue by adjusting the somaxconn parameter, allowing more connections to wait for processing.

You can use the following command to view the somaxconn parameter value of the current system:

sysctl net.core.somaxconn

To temporarily modify the value of the somaxconn parameter, you can use the following command:

sysctl -w net.core.somaxconn=<new_value>

To permanently change the value of the somaxconn parameter, edit the /etc/sysctl.conf file and add or modify the following line:

net.core.somaxconn = <new_value>

Then use the following command to make the modifications effective:

sysctl -p

Note that modifying the somaxconn parameter requires administrator privileges. It is recommended that you exercise caution when adjusting this parameter to ensure that the server's hardware resources and application performance can support larger connection queues.

In Linux systems, somaxconn is a kernel parameter used to control the length of the server's listening queue. When a server program uses the listen() function to listen to a certain port, the kernel will create a queue for the listening socket to temporarily store connection requests initiated by the client that have not yet been processed by the server's accept() function. connect. The somaxconn parameter determines the maximum length of the listening queue, that is, the upper limit of the number of connection requests that can be queued for processing at the same time.
Give an example to explain the role of somaxconn:
Suppose there is a Web server listening on port 80 to handle HTTP requests from clients. When the server's processing capacity is limited and cannot handle a large number of connection requests in a timely manner, you can increase the length of the listening queue by increasing the somaxconn parameter, so that more connection requests can be temporarily stored in the queue to wait for processing. In this way, even if the server is busy, the new connection request will not be rejected immediately, but can wait temporarily until the server has processed the previous connection and then process the connection requests in the queue one by one.
If the somaxconn parameter is set too small, when the connection request exceeds the queue length, the new connection request will be directly rejected, and the client may receive a connection timeout or connection refused error.

netdev_max_backlog parameter

netdev_max_backlog. cat /proc/sys/net/core/netdev_max_backlog, this file indicates the maximum number of packets that are allowed to be sent to the queue when each network interface receives packets faster than the kernel can process them.

netdev_max_backlog is a parameter used to control the length of the network device queue in Linux systems. It is used to limit the maximum length of the receive queue of each network device (network card). When a network device receives packets, the kernel places the packets into the device's receive queue, where they are further processed by the system's network stack.

The function of this parameter is to prevent the network device receiving queue from being overloaded, that is, to prevent the influx of a large number of data packets in a short period of time, causing the queue length to be too long, and the system cannot process it in time, which may cause packet loss, increased delay, etc. Internet problem.
When the length of the network device's receive queue reaches the netdev_max_backlog value, newly arrived data packets will be discarded to prevent the queue from growing indefinitely. In this way, the system can process newly arrived data packets in a more timely manner, ensuring network stability and performance.
By default, the value of the netdev_max_backlog parameter is a dynamic value related to the system's memory size and number of CPUs. It will adaptively adjust according to the system's hardware configuration and usually does not require manual settings.

Suppose we have a Linux server running a network application that needs to handle a large number of network requests. We hope to observe the impact on network performance by adjusting the netdev_max_backlog parameter.
First, we can use the following command to view the netdev_max_backlog parameter values ​​of the current system:

sysctl net.core.netdev_max_backlog

Assume the output is:

net.core.netdev_max_backlog = 1000

This means that the current system's netdev_max_backlog parameter value is 1000.

Now, we temporarily adjust the value of the netdev_max_backlog parameter to 5000 to increase the length of the receive queue:

sysctl -w net.core.netdev_max_backlog=5000

Next, we can run our web application and simulate a large number of network requests being sent to the server. In this case, if the server's network processing capability is insufficient, request processing may be slowed down or packets may be lost.

By increasing the value ofnetdev_max_backlog parameter, we expand the length of the receive queue, allowing the server to handle more concurrent requests, reducing the risk of possible packet loss, thereby improving Network performance and stability.

tcp_max_syn_backlog parameter

cat /proc/sys/net/ipv4/tcp_max_syn_backlog. This file indicates the length of the SYN queue. The default is 1024.

tcp_max_syn_backlogIs a parameter used in Linux systems to control the maximum queue length of a semi-connection (SYN_RCVD state) during the TCP three-way handshake. During the TCP three-way handshake, after receiving the client's SYN packet, the server will put the corresponding semi-connection into a queue and wait for the completion of the three-way handshake.

When this queue is full, new connection requests will be discarded, which may cause the client connection to time out or the request to fail. To avoid this, you can increase the length of this queue by adjusting the tcp_max_syn_backlog parameter, allowing more semi-connections to wait in the queue to complete the handshake.

You can use the following command to view the tcp_max_syn_backlog parameter values ​​of the current system:

sysctl net.ipv4.tcp_max_syn_backlog

Assume the output is:

net.ipv4.tcp_max_syn_backlog = 1024

This means that the current system's tcp_max_syn_backlog parameter value is 1024.
You can temporarily adjust the value of the tcp_max_syn_backlog parameter using the following command:

sysctl -w net.ipv4.tcp_max_syn_backlog=2048

In this way, the value of the tcp_max_syn_backlog parameter is temporarily adjusted to 2048.
It should be noted that increasing the value of tcp_max_syn_backlog parameter can increase the number of half-connections that the system can accommodate during the three-way handshake, thus improving the system's ability to handle concurrent connection requests. . However, too large a value may also occupy too many memory resources.

Consider a Linux server with a default value of 128 for the tcp_max_syn_backlog parameter. This means that the server can only accommodate a maximum of 128 half-connections (SYN_RCVD state) during the TCP three-way handshake.

Now assume that a large number of clients initiate connection requests to the server at the same time, exceeding the default tcp_max_syn_backlog number. In this case, if the server cannot handle so many connection requests, it will cause some client connection requests to be discarded, resulting in connection timeout or request failure.

To solve this problem, you can adjust the value of the tcp_max_syn_backlog parameter to increase the number of half-connections the server can accommodate during the three-way handshake.

Guess you like

Origin blog.csdn.net/Octopus21/article/details/132124481