Skip to content

Commit ec3c098

Browse files
mcmanusdavem330
authored andcommitted
[TCP]: TCP_DEFER_ACCEPT updates - process as established
Change TCP_DEFER_ACCEPT implementation so that it transitions a connection to ESTABLISHED after handshake is complete instead of leaving it in SYN-RECV until some data arrvies. Place connection in accept queue when first data packet arrives from slow path. Benefits: - established connection is now reset if it never makes it to the accept queue - diagnostic state of established matches with the packet traces showing completed handshake - TCP_DEFER_ACCEPT timeouts are expressed in seconds and can now be enforced with reasonable accuracy instead of rounding up to next exponential back-off of syn-ack retry. Signed-off-by: Patrick McManus <mcmanus@ducksong.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e4c7884 commit ec3c098

9 files changed

Lines changed: 99 additions & 33 deletions

File tree

‎include/linux/tcp.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ static inline struct tcp_request_sock *tcp_rsk(const struct request_sock *req)
239239
return (struct tcp_request_sock *)req;
240240
}
241241

242+
struct tcp_deferred_accept_info {
243+
struct sock *listen_sk;
244+
struct request_sock *request;
245+
};
246+
242247
struct tcp_sock {
243248
/* inet_connection_sock has to be the first member of tcp_sock */
244249
struct inet_connection_sock inet_conn;
@@ -374,6 +379,8 @@ struct tcp_sock {
374379
unsigned int keepalive_intvl; /* time interval between keep alive probes */
375380
int linger2;
376381

382+
struct tcp_deferred_accept_info defer_tcp_accept;
383+
377384
unsigned long last_synq_overflow;
378385

379386
u32 tso_deferred;

‎include/net/request_sock.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ struct request_sock_queue {
115115
struct request_sock *rskq_accept_head;
116116
struct request_sock *rskq_accept_tail;
117117
rwlock_t syn_wait_lock;
118-
u8 rskq_defer_accept;
119-
/* 3 bytes hole, try to pack */
118+
u16 rskq_defer_accept;
119+
/* 2 bytes hole, try to pack */
120120
struct listen_sock *listen_opt;
121121
};
122122

‎include/net/tcp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ extern void tcp_time_wait(struct sock *sk, int state, int timeo);
139139
#define MAX_TCP_KEEPINTVL 32767
140140
#define MAX_TCP_KEEPCNT 127
141141
#define MAX_TCP_SYNCNT 127
142+
#define MAX_TCP_ACCEPT_DEFERRED 65535
142143

143144
#define TCP_SYNQ_INTERVAL (HZ/5) /* Period of SYNACK timer */
144145

‎net/ipv4/inet_connection_sock.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,7 @@ void inet_csk_reqsk_queue_prune(struct sock *parent,
414414
struct inet_connection_sock *icsk = inet_csk(parent);
415415
struct request_sock_queue *queue = &icsk->icsk_accept_queue;
416416
struct listen_sock *lopt = queue->listen_opt;
417-
int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
418-
int thresh = max_retries;
417+
int thresh = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
419418
unsigned long now = jiffies;
420419
struct request_sock **reqp, *req;
421420
int i, budget;
@@ -451,19 +450,15 @@ void inet_csk_reqsk_queue_prune(struct sock *parent,
451450
}
452451
}
453452

454-
if (queue->rskq_defer_accept)
455-
max_retries = queue->rskq_defer_accept;
456-
457453
budget = 2 * (lopt->nr_table_entries / (timeout / interval));
458454
i = lopt->clock_hand;
459455

460456
do {
461457
reqp=&lopt->syn_table[i];
462458
while ((req = *reqp) != NULL) {
463459
if (time_after_eq(now, req->expires)) {
464-
if ((req->retrans < (inet_rsk(req)->acked ? max_retries : thresh)) &&
465-
(inet_rsk(req)->acked ||
466-
!req->rsk_ops->rtx_syn_ack(parent, req))) {
460+
if (req->retrans < thresh &&
461+
!req->rsk_ops->rtx_syn_ack(parent, req)) {
467462
unsigned long timeo;
468463

469464
if (req->retrans++ == 0)

‎net/ipv4/tcp.c‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,15 +2105,12 @@ static int do_tcp_setsockopt(struct sock *sk, int level,
21052105
break;
21062106

21072107
case TCP_DEFER_ACCEPT:
2108-
icsk->icsk_accept_queue.rskq_defer_accept = 0;
2109-
if (val > 0) {
2110-
/* Translate value in seconds to number of
2111-
* retransmits */
2112-
while (icsk->icsk_accept_queue.rskq_defer_accept < 32 &&
2113-
val > ((TCP_TIMEOUT_INIT / HZ) <<
2114-
icsk->icsk_accept_queue.rskq_defer_accept))
2115-
icsk->icsk_accept_queue.rskq_defer_accept++;
2116-
icsk->icsk_accept_queue.rskq_defer_accept++;
2108+
if (val < 0) {
2109+
err = -EINVAL;
2110+
} else {
2111+
if (val > MAX_TCP_ACCEPT_DEFERRED)
2112+
val = MAX_TCP_ACCEPT_DEFERRED;
2113+
icsk->icsk_accept_queue.rskq_defer_accept = val;
21172114
}
21182115
break;
21192116

@@ -2295,8 +2292,7 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
22952292
val = (val ? : sysctl_tcp_fin_timeout) / HZ;
22962293
break;
22972294
case TCP_DEFER_ACCEPT:
2298-
val = !icsk->icsk_accept_queue.rskq_defer_accept ? 0 :
2299-
((TCP_TIMEOUT_INIT / HZ) << (icsk->icsk_accept_queue.rskq_defer_accept - 1));
2295+
val = icsk->icsk_accept_queue.rskq_defer_accept;
23002296
break;
23012297
case TCP_WINDOW_CLAMP:
23022298
val = tp->window_clamp;

‎net/ipv4/tcp_input.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4451,6 +4451,49 @@ static void tcp_urg(struct sock *sk, struct sk_buff *skb, struct tcphdr *th)
44514451
}
44524452
}
44534453

4454+
static int tcp_defer_accept_check(struct sock *sk)
4455+
{
4456+
struct tcp_sock *tp = tcp_sk(sk);
4457+
4458+
if (tp->defer_tcp_accept.request) {
4459+
int queued_data = tp->rcv_nxt - tp->copied_seq;
4460+
int hasfin = !skb_queue_empty(&sk->sk_receive_queue) ?
4461+
tcp_hdr((struct sk_buff *)
4462+
sk->sk_receive_queue.prev)->fin : 0;
4463+
4464+
if (queued_data && hasfin)
4465+
queued_data--;
4466+
4467+
if (queued_data &&
4468+
tp->defer_tcp_accept.listen_sk->sk_state == TCP_LISTEN) {
4469+
if (sock_flag(sk, SOCK_KEEPOPEN)) {
4470+
inet_csk_reset_keepalive_timer(sk,
4471+
keepalive_time_when(tp));
4472+
} else {
4473+
inet_csk_delete_keepalive_timer(sk);
4474+
}
4475+
4476+
inet_csk_reqsk_queue_add(
4477+
tp->defer_tcp_accept.listen_sk,
4478+
tp->defer_tcp_accept.request,
4479+
sk);
4480+
4481+
tp->defer_tcp_accept.listen_sk->sk_data_ready(
4482+
tp->defer_tcp_accept.listen_sk, 0);
4483+
4484+
sock_put(tp->defer_tcp_accept.listen_sk);
4485+
sock_put(sk);
4486+
tp->defer_tcp_accept.listen_sk = NULL;
4487+
tp->defer_tcp_accept.request = NULL;
4488+
} else if (hasfin ||
4489+
tp->defer_tcp_accept.listen_sk->sk_state != TCP_LISTEN) {
4490+
tcp_reset(sk);
4491+
return -1;
4492+
}
4493+
}
4494+
return 0;
4495+
}
4496+
44544497
static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen)
44554498
{
44564499
struct tcp_sock *tp = tcp_sk(sk);
@@ -4811,6 +4854,9 @@ int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
48114854

48124855
tcp_data_snd_check(sk);
48134856
tcp_ack_snd_check(sk);
4857+
4858+
if (tcp_defer_accept_check(sk))
4859+
return -1;
48144860
return 0;
48154861

48164862
csum_error:

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,14 @@ int tcp_v4_destroy_sock(struct sock *sk)
19201920
sk->sk_sndmsg_page = NULL;
19211921
}
19221922

1923+
if (tp->defer_tcp_accept.request) {
1924+
reqsk_free(tp->defer_tcp_accept.request);
1925+
sock_put(tp->defer_tcp_accept.listen_sk);
1926+
sock_put(sk);
1927+
tp->defer_tcp_accept.listen_sk = NULL;
1928+
tp->defer_tcp_accept.request = NULL;
1929+
}
1930+
19231931
atomic_dec(&tcp_sockets_allocated);
19241932

19251933
return 0;

‎net/ipv4/tcp_minisocks.c‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,8 @@ struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
571571
does sequence test, SYN is truncated, and thus we consider
572572
it a bare ACK.
573573
574-
If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
575-
bare ACK. Otherwise, we create an established connection. Both
576-
ends (listening sockets) accept the new incoming connection and try
577-
to talk to each other. 8-)
574+
Both ends (listening sockets) accept the new incoming
575+
connection and try to talk to each other. 8-)
578576
579577
Note: This case is both harmless, and rare. Possibility is about the
580578
same as us discovering intelligent life on another plant tomorrow.
@@ -642,13 +640,6 @@ struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
642640
if (!(flg & TCP_FLAG_ACK))
643641
return NULL;
644642

645-
/* If TCP_DEFER_ACCEPT is set, drop bare ACK. */
646-
if (inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
647-
TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
648-
inet_rsk(req)->acked = 1;
649-
return NULL;
650-
}
651-
652643
/* OK, ACK is valid, create big socket and
653644
* feed this segment to it. It will repeat all
654645
* the tests. THIS SEGMENT MUST MOVE SOCKET TO
@@ -687,7 +678,24 @@ struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
687678
inet_csk_reqsk_queue_unlink(sk, req, prev);
688679
inet_csk_reqsk_queue_removed(sk, req);
689680

690-
inet_csk_reqsk_queue_add(sk, req, child);
681+
if (inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
682+
TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
683+
684+
/* the accept queue handling is done is est recv slow
685+
* path so lets make sure to start there
686+
*/
687+
tcp_sk(child)->pred_flags = 0;
688+
sock_hold(sk);
689+
sock_hold(child);
690+
tcp_sk(child)->defer_tcp_accept.listen_sk = sk;
691+
tcp_sk(child)->defer_tcp_accept.request = req;
692+
693+
inet_csk_reset_keepalive_timer(child,
694+
inet_csk(sk)->icsk_accept_queue.rskq_defer_accept * HZ);
695+
} else {
696+
inet_csk_reqsk_queue_add(sk, req, child);
697+
}
698+
691699
return child;
692700

693701
listen_overflow:

‎net/ipv4/tcp_timer.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ static void tcp_keepalive_timer (unsigned long data)
481481
goto death;
482482
}
483483

484+
if (tp->defer_tcp_accept.request && sk->sk_state == TCP_ESTABLISHED) {
485+
tcp_send_active_reset(sk, GFP_ATOMIC);
486+
goto death;
487+
}
488+
484489
if (!sock_flag(sk, SOCK_KEEPOPEN) || sk->sk_state == TCP_CLOSE)
485490
goto out;
486491

0 commit comments

Comments
 (0)