Skip to content

Commit

Permalink
[tcp] Send TCP keepalives on idle established connections
Browse files Browse the repository at this point in the history
In some circumstances, intermediate devices may lose state in a way
that temporarily prevents the successful delivery of packets from a
TCP peer.  For example, a firewall may drop a NAT forwarding table
entry.

Since iPXE spends most of its time downloading files (and hence purely
receiving data, sending only TCP ACKs), this can easily happen in a
situation in which there is no reason for iPXE's TCP stack to generate
any retransmissions.  The temporary loss of connectivity can therefore
effectively become permanent.

Work around this problem by sending TCP keepalives after a period of
inactivity on an established connection.

TCP keepalives usually send a single garbage byte in sequence number
space that has already been ACKed by the peer.  Since we do not need
to elicit a response from the peer, we instead send pure ACKs (with no
garbage data) in order to keep the transmit code path simple.

Originally-implemented-by: Ladi Prosek <lprosek@redhat.com>
Debugged-by: Ladi Prosek <lprosek@redhat.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jun 13, 2016
1 parent 5c2a959 commit 188789e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/include/ipxe/tcp.h
Expand Up @@ -378,6 +378,14 @@ struct tcp_options {
*/
#define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )

/**
* TCP keepalive period
*
* We send keepalive ACKs after this period of inactivity has elapsed
* on an established connection.
*/
#define TCP_KEEPALIVE_DELAY ( 15 * TICKS_PER_SEC )

/**
* TCP maximum header length
*
Expand Down
38 changes: 38 additions & 0 deletions src/net/tcp.c
Expand Up @@ -113,6 +113,8 @@ struct tcp_connection {
struct process process;
/** Retransmission timer */
struct retry_timer timer;
/** Keepalive timer */
struct retry_timer keepalive;
/** Shutdown (TIME_WAIT) timer */
struct retry_timer wait;

Expand Down Expand Up @@ -177,6 +179,7 @@ static struct profiler tcp_xfer_profiler __profiler = { .name = "tcp.xfer" };
static struct process_descriptor tcp_process_desc;
static struct interface_descriptor tcp_xfer_desc;
static void tcp_expired ( struct retry_timer *timer, int over );
static void tcp_keepalive_expired ( struct retry_timer *timer, int over );
static void tcp_wait_expired ( struct retry_timer *timer, int over );
static struct tcp_connection * tcp_demux ( unsigned int local_port );
static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
Expand Down Expand Up @@ -284,6 +287,7 @@ static int tcp_open ( struct interface *xfer, struct sockaddr *peer,
intf_init ( &tcp->xfer, &tcp_xfer_desc, &tcp->refcnt );
process_init_stopped ( &tcp->process, &tcp_process_desc, &tcp->refcnt );
timer_init ( &tcp->timer, tcp_expired, &tcp->refcnt );
timer_init ( &tcp->keepalive, tcp_keepalive_expired, &tcp->refcnt );
timer_init ( &tcp->wait, tcp_wait_expired, &tcp->refcnt );
tcp->prev_tcp_state = TCP_CLOSED;
tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
Expand Down Expand Up @@ -380,6 +384,7 @@ static void tcp_close ( struct tcp_connection *tcp, int rc ) {
/* Remove from list and drop reference */
process_del ( &tcp->process );
stop_timer ( &tcp->timer );
stop_timer ( &tcp->keepalive );
stop_timer ( &tcp->wait );
list_del ( &tcp->list );
ref_put ( &tcp->refcnt );
Expand All @@ -394,6 +399,9 @@ static void tcp_close ( struct tcp_connection *tcp, int rc ) {
if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );

/* Stop keepalive timer */
stop_timer ( &tcp->keepalive );

/* If we have no data remaining to send, start sending FIN */
if ( list_empty ( &tcp->tx_queue ) &&
! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_FIN ) ) ) {
Expand Down Expand Up @@ -801,6 +809,32 @@ static void tcp_expired ( struct retry_timer *timer, int over ) {
}
}

/**
* Keepalive timer expired
*
* @v timer Keepalive timer
* @v over Failure indicator
*/
static void tcp_keepalive_expired ( struct retry_timer *timer,
int over __unused ) {
struct tcp_connection *tcp =
container_of ( timer, struct tcp_connection, keepalive );

DBGC ( tcp, "TCP %p sending keepalive\n", tcp );

/* Reset keepalive timer */
start_timer_fixed ( &tcp->keepalive, TCP_KEEPALIVE_DELAY );

/* Send keepalive. We do this only to preserve or restore
* state in intermediate devices (e.g. firewall NAT tables);
* we don't actually care about eliciting a response to verify
* that the peer is still alive. We therefore send just a
* pure ACK, to keep our transmit path simple.
*/
tcp->flags |= TCP_ACK_PENDING;
tcp_xmit ( tcp );
}

/**
* Shutdown timer expired
*
Expand Down Expand Up @@ -1105,6 +1139,10 @@ static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
/* Update window size */
tcp->snd_win = win;

/* Hold off (or start) the keepalive timer, if applicable */
if ( ! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_FIN ) ) )
start_timer_fixed ( &tcp->keepalive, TCP_KEEPALIVE_DELAY );

/* Ignore ACKs that don't actually acknowledge any new data.
* (In particular, do not stop the retransmission timer; this
* avoids creating a sorceror's apprentice syndrome when a
Expand Down

0 comments on commit 188789e

Please sign in to comment.