In computer networking, the Transmission Control Protocol (TCP) is a transport protocol. There are two core principles to TCP:

  • Connection-oriented — both client and server must handshake to allow packets to flow. After this, a persistent connection is created between the two processes, and data flow is bi-directional.
  • Reliable data transfer — the communicating processes can rely on TCP to deliver all data sent without error and in the proper order (no missing or duplicate bytes).
    • There is also congestion control built-in so that the sender doesn’t overwhelm the receiver.

TCP is used by most application layer protocols that require reliable full data transfer (like email, SSH, web accesses, and file transfer).

Specification

TCP sockets are identified by 4-tuples, which contain:

  • Source IP address
  • Source port number
  • Destination IP address
  • Destination port number

On the receiver-side, demux is done by using all 4 values to direct the segment to the appropriate socket. This allows segments to go to the same port in the receiving machine, but still be able to differentiate to the correct process.

The TCP congestion and flow control will set the window size, and pipeline the packets. How out-of-order segments are handled is up to the implementation.

Timeout

The TCP timeout value is set to be longer than an estimated RTT parameter. This estimation is predicted with a sample RTT value, which is the measured time from segment transmission until ACK receipt ignoring re-transmissions.

This is an exponential weighted moving average (EWMA), and essentially continuously updates itself for each data point. We typically set . We typically set the timeout interval as the estimated RTT plus a safety margin. With a large variation in estimated RTT, we want a larger safety margin.

The is the safety margin, the EWMA of the sample RTT’s deviation from the estimated RTT.

and we typically set .

TCP follows Karn’s algorithm. It sets two rules:

  • If a packet has to be retransmitted, do not use its ACK for the update of the timeout.
  • When a timeout happens, double the timeout value (do not use the previous formula).

Transmission

TCP’s reliable service is done with:

  • Cumulative ACKs
  • A single retransmission timer, where retransmissions are triggered by timeout events or duplicate ACKs.

The sender does a few steps:

  • Creates a segment with a sequence number.
  • Sequence number is byte-stream number of first data byte in segment (i.e., if the packet starts at byte 100, then the number is 100).
  • Start timer if it isn’t already running. The timer is essentially waiting for the oldest unACKed segment.
  • Then, in the event of a timeout:
    • The responsible segment is retransmitted.
    • Timer is restarted.
  • In the event of a received ACK, if the ACK is for previously unACKed segments:
    • Update what is known to be ACKed.
    • Start timer if there are still unACKed segments.

Connection management

To establish a connection, TCP specifies a 3-way handshake between both processes. This allows them to agree on connection parameters.

  • First, the client sends a segment that contains no application layer data. What distinguishes is that one flag in the segment’s header, the SYN bit, is set to 1 (so this segment is called a SYN segment).

Congestion management

tahoe/reno

In the slow-start phase, the maximum window size grows by the number of ACK received starting from 1 MSS. In practice, if we fill the window size for each send, then it’ll grow exponentially.

The edge case is where we don’t fill the window size. Suppose the window size is 16 MSS and we only send 8 MSS. In this case, the window size only grows to 16+8=24 MSS.

In the congestion avoidance phase, the max window size grows by 1 MSS for every window’s worth of ACK received. In other words, if we fill the window, then the max size will grow by 1. Otherwise, it won’t.

Network programming

Applications

We generally prefer TCP when we need its core principles:

  • Connection-oriented service, for when we want the context of a persistent connection.
  • Reliable service, for when we generally prefer reliability to speed, and don’t want to implement our own network partition tolerant logic.

These applications use TCP:

See also