TCP
Transport Control Protocol
Feature
- Connection-oriented, reliable
- Unicast only
- Stream oriented
- Error Control, Flow Control, Congestion Control
TCP Header
![](/note/computer-network/transport-layer/tcp/TCP%20Header_hua3d9dce47e5b42c0179df68bbfbdba33_46648_2c07b13bfee7ff6f325ab01a8c9f1462.png)
- Sequence number: Identifies the byte in the stream of data from the sending TCP to the receiving TCP that the first byte of data in this segment represents(offset)
- Acknowledgement Number: The next sequence number that the host wants to receive
- Flags
- URG: an urgent message is being carried.
- ACK: the acknowledgment number is valid.
- PSH: a notification from the sender to the receiver that it should pass all the data received to the application as soon as possible.
- RST: signals a request to reset the TCP connection.
- SYN: set when initiating a connection.
- FIN: set to terminate a connection.
TCP Connection
Establishment
![](/note/computer-network/transport-layer/tcp/TCP%20Establishment_hu66203eec0205b708c60acc647ae66ee5_47122_bc4e972b1f0574fe14e6437bda0603f8.png)
Termination
![](/note/computer-network/transport-layer/tcp/TCP%20Termination_hu032c00bd0e2850496a99e3cfa1605c5e_78513_c32abfc067aae8e1b4fe109ae7b6e521.png)
Error Control
Each data byte has a unique sequence number. The receiver uses cumulative acknowledgments(by default) to inform the sender of the last correctly received byte in order.
Error detection is performed in each layer of the TCP/IP stack by means of header checksums, and errored packets are dropped.
Selective acknowledgment (SACK)
SACK is used to report multiple lost segments. The receiver uses the TCP SACK option to acknowledge all segments that has been successfully received in the last window of segments, and the sender can retransmit more than one lost segment at a time.
![](/note/computer-network/transport-layer/tcp/SACK_hu8c5fb22782cc185c16bdd144bbae0fd8_41653_e3badf4c1f5caafc3e79b78ba4787a71.png)
In this example, left edge of SACK is seq=1126009, which means that seq = 1126009 is received, but seq = 1126009-1448 = 1124561 is lost, so the sender will retransmit this datagram.
Retransmisson Timer
TCP will also set a Timer to measure whether the sended message will be received. If no ack received when the timer expires, this segment will be retransmitted.
- The Timer value should be larger than but of the same order of magnitude as a measurement of the Round Trip Time (RTT).
- TCP continuously measure the RTT and updates the retransmission timer value, defined as Retransmission TimeOut (RTO), dynamically.
Interactive data flow
TCP supports interactive data flow for interactive user applications, such as telnet and ssh.
To reduce the number of small segments to be more efficient:
Delayed ACK
Set a Timer: goes off every K ms. Send all acks every K ms.
Nagle Algorithm
Set a Window: when reach MSS, all buffered bytes are sent in a single segment.
Congestion Control
Sliding window buffer for both sender(cwnd) and receiver(awnd/rwnd).
![](/note/computer-network/transport-layer/tcp/Congestion%20Sliding%20Window_hu8dfc25bd627be4d083284fd8d528f796_60626_fb7f10ac7835f50a8ff845a6e9d3b41b.png)
For sender:
Here is a graph to show that the process of congestion control:
![](/note/computer-network/transport-layer/tcp/Congestion%20Control_huaa8940d965e19db238c3014e96b6d1fc_93525_529b12dc80022e7085c9d23393d141ea.png)
Serveral phases:
- Slow Start(cwnd $\leq$ ssthresh):
- cwnd = cwnd + segsize(= MSS), when receives an ACK
- Exponentially increases
- When timeout, it enters this phase again
- Congestion Avoidance(cwnd > ssthresh):
- cwnd = cwnd + segsize x segsize / cwnd + segsize / 8
- Linearly increases
- Fast Recovery(Three duplicate ACKs are received)
- ssthresh = max [ 2 segsize, min (cwnd, awnd)/2 ]
- Retransmit a segment if allowed by the window size
For receiver:
Event | Action |
---|---|
In-order segment, already ACKed | delayed ACK, wait up to 500ms for another segment, if not, send ACK |
In-order segment, ACK pending | Immediately send single cumulative ACK |
Out-of-order, Gap detected | Immediately send duplicate ACK, indicating next expected ACK |
Partially or completely fills gap | Immediately send ACK, provided that segment starts at lower end of gap |