Skip to content

Commit dca43c7

Browse files
hkchudavem330
authored andcommitted
tcp: Add TCP_USER_TIMEOUT socket option.
This patch provides a "user timeout" support as described in RFC793. The socket option is also needed for the the local half of RFC5482 "TCP User Timeout Option". TCP_USER_TIMEOUT is a TCP level socket option that takes an unsigned int, when > 0, to specify the maximum amount of time in ms that transmitted data may remain unacknowledged before TCP will forcefully close the corresponding connection and return ETIMEDOUT to the application. If 0 is given, TCP will continue to use the system default. Increasing the user timeouts allows a TCP connection to survive extended periods without end-to-end connectivity. Decreasing the user timeouts allows applications to "fail fast" if so desired. Otherwise it may take upto 20 minutes with the current system defaults in a normal WAN environment. The socket option can be made during any state of a TCP connection, but is only effective during the synchronized states of a connection (ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, or LAST-ACK). Moreover, when used with the TCP keepalive (SO_KEEPALIVE) option, TCP_USER_TIMEOUT will overtake keepalive to determine when to close a connection due to keepalive failure. The option does not change in anyway when TCP retransmits a packet, nor when a keepalive probe will be sent. This option, like many others, will be inherited by an acceptor from its listener. Signed-off-by: H.K. Jerry Chu <hkchu@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 409456b commit dca43c7

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

include/linux/tcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ enum {
105105
#define TCP_COOKIE_TRANSACTIONS 15 /* TCP Cookie Transactions */
106106
#define TCP_THIN_LINEAR_TIMEOUTS 16 /* Use linear timeouts for thin streams*/
107107
#define TCP_THIN_DUPACK 17 /* Fast retrans. after 1 dupack */
108+
#define TCP_USER_TIMEOUT 18 /* How long for loss retry before timeout */
108109

109110
/* for TCP_INFO socket option */
110111
#define TCPI_OPT_TIMESTAMPS 1

include/net/inet_connection_sock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ struct inet_connection_sock {
125125
int probe_size;
126126
} icsk_mtup;
127127
u32 icsk_ca_priv[16];
128+
u32 icsk_user_timeout;
128129
#define ICSK_CA_PRIV_SIZE (16 * sizeof(u32))
129130
};
130131

net/ipv4/tcp.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,12 @@ static int do_tcp_setsockopt(struct sock *sk, int level,
23912391
err = tp->af_specific->md5_parse(sk, optval, optlen);
23922392
break;
23932393
#endif
2394-
2394+
case TCP_USER_TIMEOUT:
2395+
/* Cap the max timeout in ms TCP will retry/retrans
2396+
* before giving up and aborting (ETIMEDOUT) a connection.
2397+
*/
2398+
icsk->icsk_user_timeout = msecs_to_jiffies(val);
2399+
break;
23952400
default:
23962401
err = -ENOPROTOOPT;
23972402
break;
@@ -2610,6 +2615,10 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
26102615
case TCP_THIN_DUPACK:
26112616
val = tp->thin_dupack;
26122617
break;
2618+
2619+
case TCP_USER_TIMEOUT:
2620+
val = jiffies_to_msecs(icsk->icsk_user_timeout);
2621+
break;
26132622
default:
26142623
return -ENOPROTOOPT;
26152624
}

net/ipv4/tcp_timer.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
138138
* retransmissions with an initial RTO of TCP_RTO_MIN.
139139
*/
140140
static bool retransmits_timed_out(struct sock *sk,
141-
unsigned int boundary)
141+
unsigned int boundary,
142+
unsigned int timeout)
142143
{
143-
unsigned int timeout, linear_backoff_thresh;
144-
unsigned int start_ts;
144+
unsigned int linear_backoff_thresh, start_ts;
145145

146146
if (!inet_csk(sk)->icsk_retransmits)
147147
return false;
@@ -151,14 +151,15 @@ static bool retransmits_timed_out(struct sock *sk,
151151
else
152152
start_ts = tcp_sk(sk)->retrans_stamp;
153153

154-
linear_backoff_thresh = ilog2(TCP_RTO_MAX/TCP_RTO_MIN);
155-
156-
if (boundary <= linear_backoff_thresh)
157-
timeout = ((2 << boundary) - 1) * TCP_RTO_MIN;
158-
else
159-
timeout = ((2 << linear_backoff_thresh) - 1) * TCP_RTO_MIN +
160-
(boundary - linear_backoff_thresh) * TCP_RTO_MAX;
154+
if (likely(timeout == 0)) {
155+
linear_backoff_thresh = ilog2(TCP_RTO_MAX/TCP_RTO_MIN);
161156

157+
if (boundary <= linear_backoff_thresh)
158+
timeout = ((2 << boundary) - 1) * TCP_RTO_MIN;
159+
else
160+
timeout = ((2 << linear_backoff_thresh) - 1) * TCP_RTO_MIN +
161+
(boundary - linear_backoff_thresh) * TCP_RTO_MAX;
162+
}
162163
return (tcp_time_stamp - start_ts) >= timeout;
163164
}
164165

@@ -174,7 +175,7 @@ static int tcp_write_timeout(struct sock *sk)
174175
dst_negative_advice(sk);
175176
retry_until = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries;
176177
} else {
177-
if (retransmits_timed_out(sk, sysctl_tcp_retries1)) {
178+
if (retransmits_timed_out(sk, sysctl_tcp_retries1, 0)) {
178179
/* Black hole detection */
179180
tcp_mtu_probing(icsk, sk);
180181

@@ -187,14 +188,16 @@ static int tcp_write_timeout(struct sock *sk)
187188

188189
retry_until = tcp_orphan_retries(sk, alive);
189190
do_reset = alive ||
190-
!retransmits_timed_out(sk, retry_until);
191+
!retransmits_timed_out(sk, retry_until, 0);
191192

192193
if (tcp_out_of_resources(sk, do_reset))
193194
return 1;
194195
}
195196
}
196197

197-
if (retransmits_timed_out(sk, retry_until)) {
198+
if (retransmits_timed_out(sk, retry_until,
199+
(1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV) ? 0 :
200+
icsk->icsk_user_timeout)) {
198201
/* Has it gone just too far? */
199202
tcp_write_err(sk);
200203
return 1;
@@ -436,7 +439,7 @@ void tcp_retransmit_timer(struct sock *sk)
436439
icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
437440
}
438441
inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
439-
if (retransmits_timed_out(sk, sysctl_tcp_retries1 + 1))
442+
if (retransmits_timed_out(sk, sysctl_tcp_retries1 + 1, 0))
440443
__sk_dst_reset(sk);
441444

442445
out:;
@@ -556,7 +559,14 @@ static void tcp_keepalive_timer (unsigned long data)
556559
elapsed = keepalive_time_elapsed(tp);
557560

558561
if (elapsed >= keepalive_time_when(tp)) {
559-
if (icsk->icsk_probes_out >= keepalive_probes(tp)) {
562+
/* If the TCP_USER_TIMEOUT option is enabled, use that
563+
* to determine when to timeout instead.
564+
*/
565+
if ((icsk->icsk_user_timeout != 0 &&
566+
elapsed >= icsk->icsk_user_timeout &&
567+
icsk->icsk_probes_out > 0) ||
568+
(icsk->icsk_user_timeout == 0 &&
569+
icsk->icsk_probes_out >= keepalive_probes(tp))) {
560570
tcp_send_active_reset(sk, GFP_ATOMIC);
561571
tcp_write_err(sk);
562572
goto out;

0 commit comments

Comments
 (0)