Skip to main content

Sequence Number Wrap-Around

2.1 Background​

Avoiding reuse of sequence numbers within the same connection is simple in principle: enforce a segment lifetime shorter than the time it takes to cycle the sequence space, whose size is effectively 2**31.

More specifically, if the maximum effective bandwidth at which TCP is able to transmit over a particular path is B bytes per second, then the following constraint must be satisfied for error-free operation:

          2**31 / B  > MSL (secs)                                    [1]

The following table shows the value for Twrap = 2**31/B in seconds, for some important values of the bandwidth B:

           Network       B*8          B         Twrap
bits/sec bytes/sec secs
_______ _______ ______ ______
ARPANET 56kbps 7KBps 3*10**5 (~3.6 days)
DS1 1.5Mbps 190KBps 10**4 (~3 hours)
Ethernet 10Mbps 1.25MBps 1700 (~30 mins)
DS3 45Mbps 5.6MBps 380
FDDI 100Mbps 12.5MBps 170
Gigabit 1Gbps 125MBps 17

It is clear why wrap-around of the sequence space was not a problem for 56kbps packet switching or even 10Mbps Ethernets. On the other hand, at DS3 and FDDI speeds, Twrap is comparable to the 2 minute MSL assumed by the TCP specification [Postel81]. Moving towards gigabit speeds, Twrap becomes too small for reliable enforcement by the Internet TTL mechanism.

The 16-bit window field of TCP limits the effective bandwidth B to 2**16/RTT, where RTT is the round-trip time in seconds [McKenzie89]. If the RTT is large enough, this limits B to a value that meets the constraint [1] for a large MSL value. For example, consider a transcontinental backbone with an RTT of 60ms (set by the laws of physics). With the bandwidth*delay product limited to 64KB by the TCP window size, B is then limited to 1.1MBps, no matter how high the theoretical transfer rate of the path. This corresponds to cycling the sequence number space in Twrap= 2000 secs, which is safe in today's Internet.

Based on this reasoning, an earlier RFC [McKenzie89] has cautioned that expanding the TCP window space as proposed in RFC-1072 will lead to sequence wrap-around and hence to possible data corruption. We believe that this is mis-identifying the culprit, which is not the larger window but rather the high bandwidth.

For example, consider a (very large) FDDI LAN with a diameter of 10km. Using the speed of light, we can compute the RTT across the ring as (210**4)/(310**8) = 67 microseconds, and the delay*bandwidth product is then 833 bytes. A TCP connection across this LAN using a window of only 833 bytes will run at the full 100mbps and can wrap the sequence space in about 3 minutes, very close to the MSL of TCP. Thus, high speed alone can cause a reliability problem with sequence number wrap-around, even without extended windows.

An "obvious" fix for the problem of cycling the sequence space is to increase the size of the TCP sequence number field. For example, the sequence number field (and also the acknowledgment field) could be expanded to 64 bits. However, the proposals for making such a change while maintaining compatibility with current TCP have tended towards complexity and ugliness.

This memo proposes a simple solution to the problem, using the TCP echo options defined in RFC-1072. Section 2.2 which follows describes the original use of these options to carry timestamps in order to measure RTT accurately. Section 2.3 proposes a method of using these same timestamps to reject old duplicate segments that could corrupt an open TCP connection. Section 3 discusses the application of this mechanism to avoiding old duplicates from previous incarnations.

2.2 TCP Timestamps​

RFC-1072 defined two TCP options, Echo and Echo Reply. Echo carries a 32-bit number, and the receiver of the option must return this same value to the source host in an Echo Reply option.

RFC-1072 furthermore describes the use of these options to contain 32-bit timestamps, for measuring the RTT. A TCP sending data would include Echo options containing the current clock value. The receiver would echo these timestamps in returning segments (generally, ACK segments). The difference between a timestamp from an Echo Reply option and the current time would then measure the RTT at the sender.

This mechanism was designed to solve the following problem: almost all TCP implementations base their RTT measurements on a sample of only one packet per window. If we look at RTT estimation as a signal processing problem (which it is), a data signal at some frequency (the packet rate) is being sampled at a lower frequency (the window rate). Unfortunately, this lower sampling frequency violates Nyquist's criteria and may introduce "aliasing" artifacts into the estimated RTT [Hamming77].

A good RTT estimator with a conservative retransmission timeout calculation can tolerate the aliasing when the sampling frequency is "close" to the data frequency. For example, with a window of 8 packets, the sample rate is 1/8 the data frequency -- less than an order of magnitude different. However, when the window is tens or hundreds of packets, the RTT estimator may be seriously in error, resulting in spurious retransmissions.

A solution to the aliasing problem that actually simplifies the sender substantially (since the RTT code is typically the single biggest protocol cost for TCP) is as follows: the will sender place a timestamp in each segment and the receiver will reflect these timestamps back in ACK segments. Then a single subtract gives the sender an accurate RTT measurement for every ACK segment (which will correspond to every other data segment, with a sensible receiver). RFC-1072 defined a timestamp echo option for this purpose.

It is vitally important to use the timestamp echo option with big windows; otherwise, the door is opened to some dangerous instabilities due to aliasing. Furthermore, the option is probably useful for all TCP's, since it simplifies the sender.

2.3 Avoiding Old Duplicate Segments​

Timestamps carried from sender to receiver in TCP Echo options can also be used to prevent data corruption caused by sequence number wrap-around, as this section describes.

2.3.1 Basic Algorithm​

Assume that every received TCP segment contains a timestamp. The basic idea is that a segment received with a timestamp that is earlier than the timestamp of the most recently accepted segment can be discarded as an old duplicate. More specifically, the following processing is to be performed on normal incoming segments:

         R1)  If the timestamp in the arriving segment timestamp is less
than the timestamp of the most recently received in-
sequence segment, treat the arriving segment as not
acceptable:

If SEG.LEN > 0, send an acknowledgement in reply as specified in RFC-793 page 69, and drop the segment; otherwise, just silently drop the segment.*


*Sending an ACK segment in reply is not strictly necessary, since the case can only arise when a later in-order segment has already been received. However, for consistency and simplicity, we suggest treating a timestamp failure the same way TCP treats any other unacceptable segment.

         R2)  If the segment is outside the window, reject it (normal
TCP processing)
         R3)  If an arriving segment is in-sequence (i.e, at the left
window edge), accept it normally and record its timestamp.
         R4)  Otherwise, treat the segment as a normal in-window, out-
of-sequence TCP segment (e.g., queue it for later delivery
to the user).

Steps R2-R4 are the normal TCP processing steps specified by RFC-793, except that in R3 the latest timestamp is set from each in-sequence segment that is accepted. Thus, the latest timestamp recorded at the receiver corresponds to the left edge of the window and only advances when the left edge moves [Jacobson88].

It is important to note that the timestamp is checked only when a segment first arrives at the receiver, regardless of whether it is in-sequence or is queued. Consider the following example.

Suppose the segment sequence: A.1, B.1, C.1, ..., Z.1 has been sent, where the letter indicates the sequence number and the digit represents the timestamp. Suppose also that segment B.1 has been lost. The highest in-sequence timestamp is 1 (from A.1), so C.1, ..., Z.1 are considered acceptable and are queued. When B is retransmitted as segment B.2 (using the latest timestamp), it fills the hole and causes all the segments through Z to be acknowledged and passed to the user. The timestamps of the queued segments are not inspected again at this time, since they have already been accepted. When B.2 is accepted, the receivers's current timestamp is set to 2.

This rule is vital to allow reasonable performance under loss. A full window of data is in transit at all times, and after a loss a full window less one packet will show up out-of-sequence to be queued at the receiver (e.g., up to ~2**30 bytes of data); the timestamp option must not result in discarding this data.

In certain unlikely circumstances, the algorithm of rules R1-R4 could lead to discarding some segments unnecessarily, as shown in the following example:

Suppose again that segments: A.1, B.1, C.1, ..., Z.1 have been sent in sequence and that segment B.1 has been lost. Furthermore, suppose delivery of some of C.1, ... Z.1 is delayed until AFTER the retransmission B.2 arrives at the receiver. These delayed segments will be discarded unnecessarily when they do arrive, since their timestamps are now out of date.

This case is very unlikely to occur. If the retransmission was triggered by a timeout, some of the segments C.1, ... Z.1 must have been delayed longer than the RTO time. This is presumably an unlikely event, or there would be many spurious timeouts and retransmissions. If B's retransmission was triggered by the "fast retransmit" algorithm, i.e., by duplicate ACK's, then the queued segments that caused these ACK's must have been received already.

Even if a segment was delayed past the RTO, the selective acknowledgment (SACK) facility of RFC-1072 will cause the delayed packets to be retransmitted at the same time as B.2, avoiding an extra RTT and therefore causing a very small performance penalty.

We know of no case with a significant probability of occurrence in which timestamps will cause performance degradation by unnecessarily discarding segments.

2.3.2 Header Prediction​

"Header prediction" [Jacobson90] is a high-performance transport protocol implementation technique that is is most important for high-speed links. This technique optimizes the code for the most common case: receiving a segment correctly and in order. Using header prediction, the receiver asks the question, "Is this segment the next in sequence?" This question can be answered in fewer machine instructions than the question, "Is this segment within the window?"

Adding header prediction to our timestamp procedure leads to the following sequence for processing an arriving TCP segment:

         H1)  Check timestamp (same as step R1 above)
         H2)  Do header prediction: if segment is next in sequence and
if there are no special conditions requiring additional
processing, accept the segment, record its timestamp, and
skip H3.
H3) Process the segment normally, as specified in RFC-793.

This includes dropping segments that are outside the window and possibly sending acknowledgments, and queueing in-window, out-of-sequence segments.

However, the timestamp check in step H1 is very unlikely to fail, and it is a relatively expensive operation since it requires interval arithmetic on a finite field. To perform this check on every single segment seems like poor implementation engineering, defeating the purpose of header prediction. Therefore, we suggest that an implementor interchange H1 and H2, i.e., perform header prediction FIRST, performing H1 and H3 only if header prediction fails. We believe that this change might gain 5-10% in performance on high-speed networks.

This reordering does raise a theoretical hazard: a segment from 2**32 bytes in the past may arrive at exactly the wrong time and be accepted mistakenly by the header-prediction step. We make the following argument to show that the probability of this failure is negligible.

If all segments are equally likely to show up as old duplicates, then the probability of an old duplicate exactly matching the left window edge is the maximum segment size (MSS) divided by the size of the sequence space. This ratio must be less than 2**-16, since MSS must be < 216; for example, it will be (212)/(232) = 2-20 for an FDDI link. However, the older a segment is, the less likely it is to be retained in the Internet, and under any reasonable model of segment lifetime the probability of an old duplicate exactly at the left window edge must be much smaller than 2**16.

The 16 bit TCP checksum also allows a basic unreliability of one part in 2**16. A protocol mechanism whose reliability exceeds the reliability of the TCP checksum should be considered "good enough", i.e., it won't contribute significantly to the overall error rate. We therefore believe we can ignore the problem of an old duplicate being accepted by doing header prediction before checking the timestamp.

2.3.3 Timestamp Frequency​

It is important to understand that the receiver algorithm for timestamps does not involve clock synchronization with the sender. The sender's clock is used to stamp the segments, and the sender uses this fact to measure RTT's. However, the receiver treats the timestamp as simply a monotone-increasing serial number, without any necessary connection to its clock. From the receiver's viewpoint, the timestamp is acting as a logical extension of the high-order bits of the sequence number.

However, the receiver algorithm dpes place some requirements on the frequency of the timestamp "clock":

         (a)  Timestamp clock must not be "too slow".

It must tick at least once for each 231 bytes sent. In fact, in order to be useful to the sender for round trip timing, the clock should tick at least once per window's worth of data, and even with the RFC-1072 window extension, 231 bytes must be at least two windows.

To make this more quantitative, any clock faster than 1 tick/sec will reject old duplicate segments for link speeds of ~2 Gbps; a 1ms clock will work up to link speeds of 2 Tbps (10**12 bps!).

         (b)  Timestamp clock must not be "too fast".

Its cycling time must be greater than MSL seconds. Since the clock (timestamp) is 32 bits and the worst-case MSL is 255 seconds, the maximum acceptable clock frequency is one tick every 59 ns.

However, since the sender is using the timestamp for RTT calculations, the timestamp doesn't need to have much more resolution than the granularity of the retransmit timer, e.g., tens or hundreds of milliseconds.

Thus, both limits are easily satisfied with a reasonable clock rate in the range 1-100ms per tick.

Using the timestamp option relaxes the requirements on MSL for avoiding sequence number wrap-around. For example, with a 1 ms timestamp clock, the 32-bit timestamp will wrap its sign bit in 25 days. Thus, it will reject old duplicates on the same connection as long as MSL is 25 days or less. This appears to be a very safe figure. If the timestamp has 10 ms resolution, the MSL requirement is boosted to 250 days. An MSL of 25 days or longer can probably be assumed by the gateway system without requiring precise MSL enforcement by the TTL value in the IP layer.