Custom TCP Retransmission Rules
Background
TCP packet retransmissions comply with the exponential backoff principle. In a low-quality network, the packet arrival rate is low and the latency is high. To address this issue, custom TCP retransmission rules can be created by setting parameters. You can specify the number of linear backoffs, maximum number of retransmissions, and maximum retransmission interval, to increase the packet arrival rate and reduce the latency in a low-quality network.
Parameter Description
- Use sysctl to enable or disable customization of TCP retransmission rules.
net.ipv4.tcp_sock_retrans_policy_custom determines whether to enable customization of TCP retransmission rules.
0: Disable customization of TCP retransmission rules.
1: Enable customization of TCP retransmission rules.
sysctl -w net.ipv4.tcp_sock_retrans_policy_custom=1
- Use setsockopt to create TCP retransmission rules for specified sockets.
- Commands:
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); int getsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
optname passes enumerated options. optval passes the start address of the structure.
- Enumerated options:
TCP_SOCK_RETRANS_POLICY_CUSTOM 100
Structure:struct tcp_sock_retrans_policy { uint8_t tcp_linear_timeouts_times; /* number of times linear backoff */ uint8_t tcp_retries_max; /* maximum retransmission times */ uint8_t tcp_rto_max; /* maximum RTO time, unit:second */ uint8_t unused; };
Where:
- tcp_linear_timeouts_times indicates the number of linear backoffs. The value ranges from 0 to 32.
0: The default rule (exponential backoff) is used.
Non-0: A non-0 value indicates a custom number of linear backoffs.
- Exponential backoff: When packet retransmission occurs, the RTO value doubles.
- Linear backoff: When packet retransmission occurs, the RTO value remains unchanged.
- tcp_retries_max indicates the maximum number of retransmissions. The value ranges from 0 to 64.
0: The maximum number of retransmissions (15 by default) defined in the system will be used.
Non-0: A non-0 value indicates a custom maximum number of retransmissions.
The value of tcp_retries_max must be larger than the minimum number of retransmissions (3 by default) defined by the sysctl option net.ipv4.tcp_retries1.
- tcp_rto_max indicates the maximum retransmission interval, in seconds. The value must be 0 or an integer ranging from 20 to 120.
0: The default maximum retransmission interval (120s) is used.
Non-0: A non-0 value indicates a custom maximum retransmission interval.
- tcp_linear_timeouts_times indicates the number of linear backoffs. The value ranges from 0 to 32.
- Commands:
How to Use
- Enable customization of TCP retransmission rules. Set net.ipv4.tcp_sock_retrans_policy_custom to 1.
[root@localhost ~]# sysctl -w net.ipv4.tcp_sock_retrans_policy_custom=1
Set the number of linear backoffs to 4, the maximum number of retransmissions to 10, and the maximum retransmission interval to 20s.
tcp_sock_retrans_policy policy = {0}; policy.tcp_linear_timeouts_times = 4; policy.tcp_retries_max = 10; policy.tcp_rto_max = 20; setsockopt(sockfd, SOL_TCP, TCP_SOCK_RETRANS_POLICY_CUSTOM, (const void*)&policy, sizeof(struct tcp_sock_retrans_policy));
- Restore to the default rule.
tcp_sock_retrans_policy policy = {0}; setsockopt(sockfd, SOL_TCP, TCP_SOCK_RETRANS_POLICY_CUSTOM, (const void*)&policy, sizeof(struct tcp_sock_retrans_policy));
- Disable customization of TCP retransmission rules.
[root@localhost ~]# sysctl -w net.ipv4.tcp_sock_retrans_policy_custom=0
Customization of TCP retransmission rules is modified in the HCE 2.0 kernel. To use this function in glibc of HCE 2.0, you need to add the TCP_SOCK_RETRANS_POLICY_CUSTOM macro and the tcp_sock_retrans_policy structure to the user-mode code.
Constraints
- This function is only valid for TCP connections in the ESTABLISHED state.
- IPv4 and IPv6 are supported. Due to historical reasons, TCP-related sysctl options are under net.ipv4. The sysctl options starting with net.ipv4.tcp are valid for both IPv4 and IPv6.
- An application-level TCP retransmission rule has a higher priority than other global TCP retransmission settings.
- Custom TCP retransmission rules may cause high system loads. Create custom rules based on available system resources. No more than 6 linear backoffs are recommended.
- If the maximum number of retransmissions is less than the value of net.ipv4.tcp_retries1 (default: 3), network detection cannot be triggered. As a result, packets may fail to be sent in some network change scenarios.
- If the maximum number of retransmissions is not 0, the value must be no smaller than the number of linear backoffs. Otherwise, an error will be returned.
- If the maximum retransmission interval is specified but the maximum number of retransmissions is not, the latter may be greater than the value of net.ipv4.tcp_retries2 (default: 15). The value of net.ipv4.tcp_retries2 (R2 for short) indicates the maximum number of retransmissions or the maximum retransmission interval. According to RFC 6069, if R2 is used to describe the number of retransmissions, it must be converted into the retransmission interval. To prevent TCP disconnection from occurring too early after conversion is enabled, the default value of TCP_RTO_MAX (120s) instead of the custom maximum retransmission interval is used for the conversion. As a result, the actual number of retransmissions is greater than R2.
- According to the TCP-TLP algorithm, the tail packet that is not acknowledged may be selected by the Loss Probe timer for retransmission to trigger fast retransmission. This retransmission is not counted in the number of retransmissions defined by the custom rule.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot