Skip to content

Commit

Permalink
net: tcp: When closing the connection send FIN without extra delays
Browse files Browse the repository at this point in the history
The earlier code was always queuing the FIN that is sent when
connection is closed. This caused long delay (200 ms) before the peer at
the other end noticed that the connection was actually closed.
Now check if there is nothing in the queue, then send the FIN
immediately. If there is some data in the queue, flush it when a valid
ack has been received.

Fixes #19678

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
  • Loading branch information
jukkar committed Oct 10, 2019
1 parent 36603a3 commit 6c4a515
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions subsys/net/ip/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,11 @@ int net_tcp_send_pkt(struct net_pkt *pkt)
return net_send_data(pkt);
}

static void flush_queue(struct net_context *context)
{
(void)net_tcp_send_data(context, NULL, NULL);
}

static void restart_timer(struct net_tcp *tcp)
{
if (!sys_slist_is_empty(&tcp->sent_list)) {
Expand Down Expand Up @@ -1134,6 +1139,11 @@ bool net_tcp_ack_received(struct net_context *ctx, u32_t ack)
*/
if (valid_ack) {
restart_timer(ctx->tcp);

/* Flush anything pending. This is important as if there
* is FIN waiting in the queue, it gets sent asap.
*/
flush_queue(ctx);
}

return true;
Expand Down Expand Up @@ -1377,6 +1387,7 @@ int net_tcp_recv(struct net_context *context, net_context_recv_cb_t cb,
static void queue_fin(struct net_context *ctx)
{
struct net_pkt *pkt = NULL;
bool flush = false;
int ret;

ret = net_tcp_prepare_segment(ctx->tcp, NET_TCP_FIN, NULL, 0,
Expand All @@ -1385,7 +1396,15 @@ static void queue_fin(struct net_context *ctx)
return;
}

if (sys_slist_is_empty(&ctx->tcp->sent_list)) {
flush = true;
}

net_tcp_queue_pkt(ctx, pkt);

if (flush) {
flush_queue(ctx);
}
}

int net_tcp_put(struct net_context *context)
Expand Down

0 comments on commit 6c4a515

Please sign in to comment.