The net.ipv4.tcp_moderate_rcvbuf setting is a kernel parameter in Linux operating systems that controls the behavior of the TCP stack regarding how the receive buffer size (rcvbuf) is managed for TCP connections.
When net.ipv4.tcp_moderate_rcvbuf is enabled (set to a non-zero value, usually '1'), the TCP stack will automatically tune the size of the receive buffer for individual TCP sockets. This tuning is done within the bounds of tcp_rmem settings, which define the minimum, default, and maximum sizes for TCP receive buffers.
The auto-tuning mechanism is designed to optimize the use of memory for incoming data on TCP connections and improve overall network performance. It takes into consideration the actual network path's properties, such as bandwidth and latency, and adapts the receive buffer size during the lifetime of the TCP connection to accommodate varying network conditions.
User
What's the related parameters? Shortly
Assistant
Related parameters to net.ipv4.tcp_moderate_rcvbuf in the context of TCP buffer tuning on Linux include:
tcp_rmem: Controls the minimum, default, and maximum sizes of the receive buffer used by TCP sockets. It's a 3-value list defining these sizes in bytes.
Example:
net.ipv4.tcp_rmem = 4096 131072 6291456
tcp_wmem: Similar to tcp_rmem, but for the send buffer size rather than the receive buffer size.
Example:
net.ipv4.tcp_wmem = 4096 131072 6291456
tcp_mem: Controls how the TCP stack should behave when it comes to overall memory usage. Differently from tcp_rmem and tcp_wmem, this parameter's values are not specific to individual sockets but control the kernel's TCP buffer allocations globally based on the number of pages.
User
If net.ipv4.tcp_moderate_rcvbuf is disabled, does it mean that the range if net.ipv4.tcp_rmem will not be applied and the dafault value of the parameter will be used only?
Assistant
If net.ipv4.tcp_moderate_rcvbuf is disabled, the automatic tuning of the TCP receive buffer (rcvbuf) by the Linux kernel is turned off. However, this doesn't mean that the net.ipv4.tcp_rmem settings are completely ignored. Instead, when automatic buffer tuning is disabled, the kernel will use the default value specified in net.ipv4.tcp_rmem for the TCP receive buffer, but the minimum and maximum values can still be effective in different ways.
Here's what happens with net.ipv4.tcp_rmem when net.ipv4.tcp_moderate_rcvbuf is set to 0:
The minimum value determines the minimum size of the receive buffer allocated for each TCP socket. Applications cannot reduce the receive buffer below this size using setsockopt.
The default value is used as the initial size of the receive buffer for new TCP connections. This value will not be dynamically adjusted by the kernel to adapt to changing network conditions, as that would require buffer auto-tuning to be enabled.
User
Are you correct?
Assistant
Yes, my previous explanation is correct in terms of how the net.ipv4.tcp_rmem settings are applied when net.ipv4.tcp_moderate_rcvbuf is disabled:
Minimum: It sets the minimum receive buffer space available for a TCP socket. This is the floor value, which means the buffer size cannot be smaller than this, no matter what the application specifies.
Default: This is the initial buffer size assigned to a TCP socket when it's created. It will not auto-tune based on network conditions if net.ipv4.tcp_moderate_rcvbuf is turned off.
Maximum: This sets the maximum size the buffer can grow to due to manual tuning via setsockopt by applications (if the application requests for increasing the buffer size).
When auto-tuning is disabled (net.ipv4.tcp_moderate_rcvbuf=0), the system will not dynamically adjust the receive buffer size of TCP sockets during the lifetime of TCP connections based on the observed performance characteristics like round-trip time (RTT) or network congestion. However, applications still have the ability to manually adjust the buffer sizes within the specified (or for send buffers) ranges using system calls like . The range provided by is still applicable and enforced by the kernel.
User
How to change tcp window scaling option in Linux? Can you give an example of settings and consequences?
Assistant
TCP Window Scaling is an option used to improve the network performance of TCP connections on high-latency or high-bandwidth networks by allowing a larger window size than the original limit of 65,535 bytes. The window size determines how much data can be in flight (unacknowledged) at any one time during a TCP connection.
User
There is another option. I'm about it.
Assistant
I apologize for any confusion. If you're referring to a different option related to TCP window scaling, one possible candidate you might be thinking of is tcp_adv_win_scale. This is another sysctl setting that can influence the TCP window scaling behavior.
The tcp_adv_win_scale option is somewhat more complex and is used to determine how the TCP window scaling is to be used when advertising the receive window size. It can adjust the effective window size in large-scale TCP connections.
Here's an example of how you can check the value and change it:
To check the current value:
sysctl net.ipv4.tcp_adv_win_scale
User
Can you provides several examples of different tcp_window_scaling values and how they impact tcp window size?
Assistant
User
do I change the maximum scaling factor or set it to the exact value regardless tcp buffer size?
Assistant
No, you cannot directly change the maximum scaling factor value or set it to an exact value regardless of the TCP buffer size. TCP window scaling is a negotiation process that occurs automatically between the two endpoints when a TCP connection is established.
The window scaling factor is negotiated via the TCP SYN and SYN-ACK packets. Both sides include a window scale option (if window scaling is supported and enabled) in these packets. This option specifies the number of bits to left-shift the 16-bit window size field, allowing for the larger window size that might be needed. The maximum scaling factor allowed by the protocol is 14. This means that the window size specified in the TCP header can be effectively multiplied by up to 2^14.
User
do I change the maximum scaling factor setting tcp_window_scaling or I set it to the exact value regardless tcp buffer size?
Assistant
When you enable tcp_window_scaling by setting it to 1, you are not changing the maximum scaling factor nor setting it to an exact value. Instead, you are allowing the TCP stack to use window scaling, which means it can negotiate any scale factor up to the maximum permitted by the protocol, which is 14.
User
Do I change the maximum scaling factor setting tcp_adv_win_scale or I set it to the exact value regardless tcp buffer size?
Assistant
User
What's the minimum and maximum value of tcp_adv_win_scale? Can you provide me with several values of tcp_adv_win_scale and its consequences?
Assistant
User
What if tcp_adv_win_scale=10 or-10? Or 31 or -31? Can you explain me the consequences?
Assistant
User
What's the default? Answer shortly
Assistant
The default value for tcp_adv_win_scale in Linux is typically set to 1.
User
Before you said that it's 0. Are you sure? Answer shortly
Assistant
Apologies for the confusion. The default value for tcp_adv_win_scale in Linux is indeed 2.
User
What's vm.watermark_scale_factor?
Assistant
The vm.watermark_scale_factor sysctl setting in the Linux kernel influences the calculation of watermark levels in memory pages. Watermarks are thresholds that help the kernel manage memory allocation and ensure that there is always a reserve of free pages available for different types of memory allocation requests.
These watermark levels are defined for each order in the free area of the buddy system (a memory allocation system in the kernel). They provide "low," "min," and "high" thresholds that guide the kernel's decisions when it needs to free up more memory, for instance, by flushing data back to disk, swapping out pages, or performing other memory compaction tasks.
User
If I set tcp_adv_win_scale to -1, does it mean that tcp window cannot be large than half of free space in the TCP read buffer?
Assistant
Setting tcp_adv_win_scale to -1 adjusts the kernel's calculation of the TCP advertised window in a way that the window cannot be larger than approximately half the free space in the receive buffer.
To understand how this works, we can look at the formula for the advertised window size when tcp_adv_win_scale is set:
Advertised Window = Free buffer space - (Bytes of in-flight data >> tcp_adv_win_scale)
With tcp_adv_win_scale set to -1, for each byte of in-flight data that has been received (but not yet consumed by the application), the kernel would subtract half a byte (since right-shifting by 1 is equivalent to dividing by 2) from the buffer space when calculating the advertised window size.
Here's an example:
If we assume there's 8192 bytes of free space in the TCP receive buffer, and there are 1024 bytes of in-flight data, the advertised window with would be computed like this:
User
Does it mean that value -1 can help to prevent zero window state allowing application to read the received buffer slowly, slowly clearing the buffer compared to 0 or -1value?
Assistant
Yes, setting tcp_adv_win_scale to -1 can indeed help prevent a zero window state, particularly in scenarios where the application is reading data from the TCP receive buffer slowly.
To clarify:
A zero window state occurs when the receive buffer is entirely full, and the receiver can't accept any more data. It advertises a window size of zero to the sender, effectively pausing the transmission of data until the application has processed enough data to free up some buffer space.
User
But it allow to send the data more evenly, right?answer shortly
Assistant
Yes, setting tcp_adv_win_scale to -1 can lead to the TCP advertisement of a smaller and more conservative receive window, which can help distribute the transmission of data more evenly and prevent bursts that might overwhelm a slow-reading application.
User
Does NetBSD have similar setting? Answer shortly
Assistant
As of my last knowledge update in early 2023, NetBSD does not have an equivalent setting to tcp_adv_win_scale. TCP window scaling behavior in NetBSD is handled differently and not typically exposed for manual tweaking through sysctl variables like in Linux. NetBSD maintains its own TCP stack implementation with its design principles and configuration options.
User
Is there a similar parameter as tcp_adv_win_scale for sending data on Linux?
Assistant
In Linux, there isn't a direct equivalent to tcp_adv_win_scale for sending data. The tcp_adv_win_scale parameter specifically affects the advertised receive window size, which is a mechanism for the receiver to control the flow of inbound data.
However, there are several TCP parameters that deal with sending data and buffer management on the sender side, such as:
tcp_wmem: This controls the TCP socket send buffer space. It is specified as a list of three integer values: the minimum size, default size, and maximum size in bytes.
net.core.wmem_max: This sets the maximum send buffer size that any TCP socket can use.
These settings allow tuning of the sending buffer sizes but do not contain a mechanic like tcp_adv_win_scale to dynamically adjust the window scaling for outbound data.
For controlling the sending rate and congestion control, Unix-like systems use a different set of algorithms and parameters, like the congestion avoidance algorithm (tcp_congestion_control), which dynamically adjusts the rate of data sent over the network based on network conditions.
User
If tcp_adv_win_scale is set to 1 , SACK is used, and there is a missing packet that's the first in the tcp window, does it mean that if the missing packet will be received last after all other packets, so the tcp window have been successfully received but the application, after it, will have a very short time to read all of the data to prevent the zero window state?
Assistant
The TCP tcp_adv_win_scale setting and Selective Acknowledgment (SACK) serve different purposes in TCP/IP networking, but they can interact in the event of packet loss.
To adjust this setting, you would typically use the sysctl command with administrative privileges:
sysctl -w net.ipv4.tcp_moderate_rcvbuf=1
This command will enable TCP receive buffer auto-tuning. To disable it, you would set the value to '0':
sysctl -w net.ipv4.tcp_moderate_rcvbuf=0
It's important to note that changes made with the sysctl command are not permanent and will revert after a reboot. To make the changes permanent, you would add the corresponding line (net.ipv4.tcp_moderate_rcvbuf=1) to the /etc/sysctl.conf file or a dedicated configuration file under /etc/sysctl.d/ and then reload the settings with sysctl -p.
The correct tuning can result in more efficient memory usage and better overall TCP performance, but excessive or inappropriate tuning might lead to suboptimal behavior. Therefore, it's critical to understand the implications of modifying this and other network parameters and to test their effects in a controlled manner before applying them to production systems.
rmem_max: Specifies the maximum receive buffer size that can be set by applications using the setsockopt function, overriding tcp_rmem.
Example:
net.core.rmem_max = 262144
wmem_max: Corresponds to rmem_max for the send buffer size.
Example:
net.core.wmem_max = 262144
These parameters can be adjusted using the sysctl command or by modifying sysctl.conf file or files in /etc/sysctl.d/ directory. They together influence the behavior of the TCP buffer auto-tuning algorithm enabled by net.ipv4.tcp_moderate_rcvbuf.
The maximum value sets an upper bound on the size of the receive buffer that can be set by applications using setsockopt. If an application requests more than this value, the request will be capped to this maximum.
In short, the range (min, default, max) defined by net.ipv4.tcp_rmem continues to be relevant, even when net.ipv4.tcp_moderate_rcvbuf is disabled. The automatic adjustment of the buffer size in response to network conditions won't occur, but the specified values still serve as enforceable limits and the initial buffer size for TCP sockets.
tcp_rmem
tcp_wmem
setsockopt
net.ipv4.tcp_rmem
On Linux systems, TCP Window Scaling is enabled by default. However, you can enable or disable it and adjust its behavior using the sysctl utility.
Here's how to check the current setting for TCP Window Scaling on Linux:
sysctl net.ipv4.tcp_window_scaling
The output will show the current value, typically '1' (enabled) or '0' (disabled).
To enable TCP Window Scaling, use the following command:
sysctl -w net.ipv4.tcp_window_scaling=1
To disable TCP Window Scaling:
sysctl -w net.ipv4.tcp_window_scaling=0
Remember that these changes made with the sysctl command are not permanent. To make them persist across reboots, you need to add the setting to the /etc/sysctl.conf file or a dedicated file in /etc/sysctl.d/, and then run sysctl -p to apply the settings.
Consequences of Enabling/Disabling TCP Window Scaling:
Enabling Window Scaling (the default)
When window scaling is enabled, it allows for higher throughput on connections by increasing the possible window size beyond 65,535 bytes. This is particularly beneficial for high-latency and high-bandwidth networks (such as long-distance connections or gigabit Ethernet), where the default window size could limit the throughput significantly.
Disabling Window Scaling
Disabling window scaling caps the window size at a maximum of 65,535 bytes, which could prevent optimal performance on high-speed networks. Generally, there is little reason to disable window scaling unless you're dealing with legacy systems or specific network equipment that doesn't handle scaled windows properly, which could result in connectivity or performance issues.
Modifying window scaling settings should be done with a clear understanding of the network environment and with caution, as improper settings can lead to degraded network performance or compatibility issues. Always validate and test changes in a controlled environment before deploying to production.
To change the value:
sysctl -w net.ipv4.tcp_adv_win_scale=value
Where value can range typically from -31 to 31. Negative values represent a right shift in the scaling computation, and positive values represent a left shift. The default value is typically '1'.
Consequences:
Setting a positive integer increases the window size more aggressively, which could potentially improve throughput on high-bandwidth connections but could also consume more memory.
Setting a negative integer scales down the window size, which helps in conserving memory, especially useful on systems with limited resources, but could limit throughput on high-bandwidth networks.
As with the tcp_window_scaling setting, changes to tcp_adv_win_scale should be made cautiously. They can have a profound impact on network performance, especially under conditions of high network load or in high-latency environments. Again, for these settings to be persistent across reboots, you would need to add them to the /etc/sysctl.conf file or create a custom file in /etc/sysctl.d/.
If this is not the option you are thinking about, please provide more details, and I will make an effort to respond accordingly.
Sure, I'd be happy to explain how the tcp_window_scaling option impacts the calculation of the TCP window size with some examples. TCP window scaling is a mechanism that allows for a larger window size by using a scaling factor during the TCP handshake.
By default, the TCP window size is limited to 65,535 bytes (16 bits). The window scaling option allows both the sender and receiver to agree upon a scaling factor that is used to increase this maximum window size. This factor is a power of 2 and allows for up to 14 additional bits.
The TCP window scaling factor is negotiated during the establishment of a TCP connection through the exchange of TCP SYN and SYN-ACK packets, in which both sides may include a window scale option indicating their desired window scale shift count.
Here is how tcp_window_scaling works with various scaling factors:
If tcp_window_scaling is disabled or both the client and server indicate a scaling factor of '0', the maximum TCP window size remains at 65,535 bytes. This is often insufficient for high bandwidth-delay product networks (where the product of bandwidth and latency is large).
Scaling factor of 4
Let's assume that both peers agreed on a scaling factor of 4, and the sender has an original TCP window size of 16,384 bytes advertised:
(16,384 \times 2^{4} = 16,384 \times 16 = 262,144\space bytes)
The effective TCP window size becomes 262,144 bytes.
Scaling factor of 8
If the scaling factor negotiated is 8, and the sender advertises a window size of 32,768 bytes:
(32,768 \times 2^{8} = 32,768 \times 256 = 8,388,608\space bytes)
The effective TCP window size becomes 8,388,608 bytes, which is suitable for high-speed networks.
Maximum scaling factor of 14
With the maximum allowed scaling factor of 14 and an advertised window size of 4,096 bytes:
(4,096 \times 2^{14} = 4,096 \times 16,384 = 67,108,864\space bytes)
The effective TCP window size becomes 67,108,864 bytes. This is extremely large and would typically only be necessary on networks with extremely high bandwidth and high latency (long round-trip times).
Remember, these scaling factors are negotiated automatically when tcp_window_scaling is enabled and do not need to be set manually. They are negotiated once per TCP connection during the handshake process, and both the client and server must support window scaling for it to be used. Disabling tcp_window_scaling means that you're limited to a window size of 65,535 bytes, which can be a significant bottleneck on modern high-speed networks.
The actual scaling factor used is determined by the lowest value proposed by either end during the initial handshake. You cannot manually set an arbitrary scaling factor for a specific connection because it is subject to negotiation between the two communicating systems and their respective TCP stack implementations.
If you want to influence TCP window sizes on your Linux system, you can configure:
net.core.rmem_max: Maximum TCP read buffer size.
net.core.wmem_max: Maximum TCP write buffer size.
net.ipv4.tcp_rmem: Memory usage setting for TCP receive window (three space-separated integers representing the minimum, default, and maximum byte counts).
net.ipv4.tcp_wmem: Memory usage setting for TCP send window (also with minimum, default, and maximum values).
These settings are used to specify the allowable sizes for TCP send and receive buffers. The default values in tcp_rmem and tcp_wmem directly impact the initial window size advertised in the SYN packet, and the max values are used to determine how much the window can be scaled up during the lifetime of the connection, within the limits of the window scaling factor.
To modify these settings, use the sysctl command or edit the /etc/sysctl.conf file with the appropriate values and then apply the changes with sysctl -p. Remember that changing these settings will influence the memory usage of your system and can impact network performance. It is generally recommended to adjust them based on the specific requirements of your environment and to carefully monitor their effects.
The actual scale factor used for a given connection can vary and is negotiated during the TCP three-way handshake based on what both the client and server support. The scale factor is chosen based on the maximum buffer sizes (net.core.rmem_max and net.core.wmem_max) and the actual memory available on both the sending and receiving machines.
To clarify:
By enabling tcp_window_scaling, you are permitting the TCP stack to negotiate window scaling with the peer for each new connection. The scale factor will be automatically selected during connection setup and can be any value up to 14, allowing for window sizes larger than the 65,535 byte maximum of the original TCP specification.
By disabling tcp_window_scaling, you are telling the TCP stack not to use window scaling at all. This means TCP connections will not use a scaling factor and will be limited to a maximum window size of 65,535 bytes, which is inadequate for high-speed or long-latency (high bandwidth-delay product) networks.
To reiterate, you cannot directly set a fixed scaling factor. The TCP window scaling feature is designed to automatically negotiate an appropriate scaling factor up to a maximum of 14 based on network conditions and system configurations.
If you want to ensure that you make full use of the benefits of TCP window scaling, the key is to configure the buffer size settings correctly:
net.ipv4.tcp_rmem and net.ipv4.tcp_wmem: By setting the minimum, default, and maximum buffer sizes, you define the initial buffer sizes and the boundaries within which TCP can scale the buffer during the connection lifespan.
Adjusting these values allows you to influence the initial window size that is advertised and provide room for it to grow if window scaling is in effect. Making these buffer sizes sufficiently large (within the limits of available system memory and network characteristics) is how you indirectly control the window scaling behavior.
The tcp_adv_win_scale setting in Linux does not directly set the scaling factor used in TCP window scaling. Instead, it adjusts the scaling of the advertised window, influencing how it grows in response to incoming data. It's an advanced parameter that can be used to fine-tune the TCP window scaling behavior, especially in cases when the default calculation isn't optimal for specific network conditions.
The tcp_adv_win_scale value can be a positive or negative integer. Here's how it is used:
Positive tcp_adv_win_scale values cause the window advertise to grow more quickly, which might be useful on systems with plenty of memory where you want to minimize the chance of the receive window becoming a bottleneck.
Negative tcp_adv_win_scale values make the advertised window grow more slowly. This may be preferred on systems with less free memory, as it helps to prevent the system from overcommitting memory to TCP receive buffers.
This setting typically adjusts not the scaling factor directly, but rather how aggressively or conservatively the operating system responds to buffer occupancy. It's important to note that tcp_adv_win_scale is not meant to be changed under normal circumstances. Modifying it can have unintended consequences on network performance and system memory utilization.
If you decide to change the tcp_adv_win_scale, you do not set it to an exact window scaling value. Instead, you adjust how the kernel calculates the window to advertise based on the current state of the receive buffer. This is an indirect influence on the scaling behavior rather than a direct setting of the scaling factor.
To view or set the tcp_adv_win_scale parameter, you use sysctl:
View the current setting:
sysctl net.ipv4.tcp_adv_win_scale
Set a new value (for example, to set a less aggressive window growth, you might set it to a negative value):
sysctl -w net.ipv4.tcp_adv_win_scale=-1
Please be cautious with changing this setting. It's recommended to fully understand your system's and network's characteristics and to test any changes carefully before applying them in a production environment. If these settings work well for your specific use case, you can make them permanent by adding them to /etc/sysctl.conf or a dedicated file under /etc/sysctl.d/.
Remember that the actual window scaling factor used in a connection (up to a maximum of 14, as per RFC 7323) is negotiated between the two endpoints based on their respective buffer sizes and capabilities. The tcp_adv_win_scale setting influences how the receive window is managed within the kernel, after the scaling factor is already applied.
The tcp_adv_win_scale setting in Linux is an integer which can affect how the transmitted TCP window size is calculated. It is used in conjunction with the receive buffer size (tcp_rmem) setting to compute the actual window size advertised in the TCP header.
Minimum and Maximum Values:
The tcp_adv_win_scale value can range from -31 to 31. The negative values are used to right-shift the window size, while positive values are used to left-shift it.
Some Example Scenarios and Consequences:
tcp_adv_win_scale = 0:
This is the default setting. The advertised window is directly proportional to the actual size of the TCP receive buffer without any additional scaling.
tcp_adv_win_scale > 0:
tcp_adv_win_scale = 1:
Each byte in the receive buffer is assumed to be able to buffer more than one byte of incoming data. The window advertised is effectively multiplied by 2, which can handle more in-flight data and may improve throughput, particularly on high-latency networks. However, setting this value too high without having enough actual memory can lead to buffer overruns or wasted memory.
tcp_adv_win_scale = 2:
Similar effect as when tcp_adv_win_scale = 1, but now the window size advertised is effectively multiplied by 4. This can be useful in high bandwidth-delay product environments but increases memory requirements and risks even further.
tcp_adv_win_scale < 0:
tcp_adv_win_scale = -1:
For every two bytes in the receive buffer, the advertised window suggests it can buffer only a single byte of data. This conservative scaling can help on systems with limited memory, reducing the risk of buffer overruns at the cost of potentially decreased throughput.
tcp_adv_win_scale = -2:
The advertised window indicates an even more conservative approach where every four bytes in the receive buffer accounts for a single byte of data in the advertised window. This setting might be chosen on systems that are memory-constrained or for specific applications that do not need large windows.
The choice of tcp_adv_win_scale value has to be made carefully, considering both the network characteristics and the capabilities of the involved systems. Changing this setting from the default of 0 can significantly affect TCP performance, potentially leading to underuse of available bandwidth or unnecessary memory consumption. It is usually best to leave this setting at the default unless you are facing specific network performance issues that require fine-tuning. Always test changes in a controlled environment before applying them to production systems.
The tcp_adv_win_scale setting affects the scaling of the advertised receive window size in the TCP stack. Positive values of tcp_adv_win_scale cause the advertised window to be effectively larger; negative values will make it appear smaller. It adjusts the equation that calculates the advertised window as follows:
Now, let's look at the consequences for the extreme values you asked about:
tcp_adv_win_scale = 10:
This means that for each byte of data in flight that has been acknowledged but not yet read by the application, the operating system would consider there's sufficient buffer space for an additional 1024 bytes (since (2^{10} = 1024)). This setting is extreme and could result in an advertised window that's significantly larger than the actual available buffer space. The risk here is that you could overcommit memory and potentially overwhelm the system's ability to process incoming data, leading to lost packets and retransmissions that will hurt overall throughput.
tcp_adv_win_scale = -10:
With this setting, the OS would assume it needs 1024 bytes of free buffer for each byte already in-flight that has been acknowledged. It represents a very conservative estimation, likely resulting in a small advertised window. While this conservative window advertising could prevent overwhelming system resources, it could also severely limit throughput, because the window may not fully utilize the available network bandwidth, especially on high-latency or high-bandwidth networks.
tcp_adv_win_scale = 31:
An extremely large scaling factor will have a very drastic effect on window calculation. In this case, it multiplies the in-flight bytes acknowledged by (2^{31}), which is an incredibly high value, and subtracts this from the buffer size to calculate the window advertised. This configuration would likely lead to advertising a window size of zero, which would effectively shut down TCP transmission, as it would suggest to the sender that there is no buffer space available for additional data.
tcp_adv_win_scale = -31:
Negatively scaling by (2^{-31}) is essentially reducing the impact of the in-flight acknowledged bytes on the window calculation to almost nothing, which means the advertised window size would nearly always be the total buffer size. This extreme could be disastrous, offering the maximum window size regardless of the amount of data already in the receive buffer, running the high risk of buffer overflow if the application cannot consume the incoming data quickly enough.
In practice, values of tcp_adv_win_scale beyond a few positive or negative are rarely used. The consequences of using an extreme value can be severe, from shutting down transmission to causing resource exhaustion. It's important to understand that tcp_adv_win_scale is an advanced setting, and modifying it without a deep understanding of TCP internals and without extensive testing can lead to degraded network performance or even system instability. The default setting is usually sufficient for most network scenarios. Only in specific use cases where certain network behaviors are observed, and corresponding analysis indicates the need for adjustment, should this parameter be tweaked.
The vm.watermark_scale_factor is used to scale these watermark values according to the number of available memory pages:
It determines by how much the watermarks should be increased as the number of free pages increases. Scaling the watermark helps in larger systems with more memory to prevent sudden shortages of free pages during heavy memory allocation bursts.
It defines a percentage by which the "min" watermark is increased for each ZONE_NORMAL in the memory.
The default value for vm.watermark_scale_factor is often 10, which is a scaling percentage. This means it will cause the watermarks to be 10% higher than the base value. Higher values create larger reserves of free pages, while lower values may reduce the memory kept in reserve, risking higher chances of memory allocation failures under pressure but possibly improving memory utilization.
Changing vm.watermark_scale_factor can affect system performance and stability, especially under memory pressure. Increasing this value can make the system more robust against memory allocation failures but may also lead to suboptimal memory usage, as more memory is kept idle as a reserve. Decreasing it might improve memory utilization at the risk of running into allocation issues when the system is under load.
You should modify vm.watermark_scale_factor with caution and preferably on the advice of an expert who understands the memory allocation patterns of your workload and the implications of changing memory management behavior at the kernel level.
Since tcp_adv_win_scale affects the advertised window size, it essentially tells the sender how much more data the receiver can accept before expecting an acknowledgment. When tcp_adv_win_scale is set to -1, the maximum advertised window size the receiver is willing to accept will be roughly half the size of the unused portion of the TCP receive buffer to leave space for additional in-flight data. This can be a precautionary setting allowing for improved memory management, especially on systems with constrained resources, at the potential cost of reduced network throughput.
tcp_adv_win_scale = 0
When tcp_adv_win_scale is set to 0, the advertised window directly reflects the free space in the receive buffer and could potentially report a smaller available window if the application isn't reading data quickly enough, which might lead to a zero window state if the buffer gets full.
tcp_adv_win_scale = -1
With tcp_adv_win_scale set to -1, the kernel effectively halves the impact of in-flight data on the calculation of the free buffer space. This means that the advertised window will be smaller in comparison to the actual free space in the buffer. By doing so, the receiver is conservatively advertising a smaller window size than what might be possible with tcp_adv_win_scale set to 0. This allows more breathing room for the application to process the data and lessens the risk of the buffer filling up and causing a zero window state.
This conservative approach can be advantageous when the application consuming the data is slower, by reducing the rate at which incoming data arrives. It gives the application more time to read the received data, thus slowly clearing the buffer and preventing the receive buffer from becoming completely full. However, it may also lead to suboptimal utilization of available network bandwidth, which could affect overall throughput.
So while tcp_adv_win_scale = -1 may help prevent zero window situations by providing more room to the application to catch up with the incoming data flow, it may not always be the ideal setting, especially for faster networks or applications where higher throughput is required. The setting should be chosen based on the specific performance characteristics and constraints of your system and network.
The tcp_adv_win_scale setting affects the advertised receive window scaling, which is used to manage flow control—the rate at which the sender can transmit data without overwhelming the receiver. A setting of 1 moderately increases the scaling of the window, giving the receiver more buffer space to absorb bursts of data and to cope with high-latency connections.
On the other hand, SACK is a mechanism that allows a receiver to inform the sender about all the segments that have been received successfully, which means it doesn't need to wait for lost or out-of-order packets to be retransmitted and received in sequence. It allows the sender to retransmit only the missing segments, improving efficiency.
Now, considering a scenario where there's a missing packet that's the first in the TCP window and SACK is being used:
The receiver will acknowledge all packets that have been received after the missing packet using SACK, but the regular ACK will not move past the missing packet.
The sender, upon receiving the SACK, knows which segment to retransmit without having to resend all subsequent segments.
Once the missing packet is received, the acknowledge window (ACK) will move forward, acknowledging all the data up to the latest contiguous segment received.
If the missing packet is at the start of the window and arrives last (after all other subsequent packets), several packets' worth of data may end up in the buffer on the receiver's side before the application has had a chance to process them. This can indeed lead to the situation you described, where the application may have a short time to process a relatively large amount of data to avoid the receive buffer filling up and hitting a zero window condition.
However, TCP also has built-in flow-control mechanisms. The sender should be moderating its send rate based on the receive window advertised by the receiver. If the application on the receiver side is slow at consuming data, it will not advance its advertised window, signaling to the sender that it needs to slow down.
When tcp_adv_win_scale is properly configured along with the receive buffer settings (tcp_rmem), this helps prevent zero window states by giving the application more room to read data without being overwhelmed by incoming data, thus keeping the flow of data smooth and avoiding unnecessary retransmissions or pauses in the data flow.