5 – TCP

Features

Needs to keep track of historical info (state), hence the requirement of sender/receiver buffers on both client and server (remember, TCP is bidirectional!).

Socket

A TCP socket is identified by 4-tuple: (source IP, source port, dest IP, dest port)

In TCP, the acknowledgement isn’t acking segments, but a stream of bytes.

We can combine sending info and acknowledgement in the same segment!

TCP Segment

How much app-layer data can a TCP segment carry?

Checksum

Same algorithm as UDP, over pseudo IP header, other TCP header fields and the TCP body itself.

Sequence/Acknowledgement number

Two separate values, each counting by bytes.

Q: Why is the sequence number randomized?

A: To improve security (prevent spoofing) and lessen the likelihood of swapping packets with the same seq# from various sessions between two hosts

Sequence number: Byte id that is a 32-bit integer that wraps around upon overflow.

Acknowledgement number: Last byte id received + 1.

Receive Window

Used if application is consuming at a slower rate than TCP socket is receiving, in which case we need to implement flow control.

Receive window specifies The number of bytes the receiver is willing to accept before the receive buffer overflows.

TCP 3-way handshake

RST, SYN, FIN

SYNbit: Indicator that this segment is for establishing TCP connection.

  1. Client sends TCP SYN message with initial sequence number $x$.
  2. Server sends TCP SYNACK message with initial seuqnce number $y$ and ACKnum $x+1$.
  3. When Client receives the SYNACK, both Client and Server have established (ESTAB) the server is live.

ACKbit: Indicator that this segment’s acknowledgement number is valid.

FINbit: One side informs the other that they want to close this connection (the other side will reply with an ACK). Means this side no longer sends out data, only acknowledgements.

  1. Client sends FIN message with sequence number $u$. Now client can no longer send (but can receive) data.
  2. Server sends ACK message with ACKnum $u+1$ and now Client waits for the server to close.
  3. Server sends FIN message with sequence number $v$. Now server can no longer send data.
  4. Client starts timer of (2 * Max Segment Lifetime) and sends ACK message with ACKnum $y+1$.
  5. When Server receives ACK, closes. When Client ends its timer, closes.

The FINbit packet might get lost. Hence we want to have this 2x bye. OTherwise the FINbit packet is resent.

TCP reliable data transfer summary

rdt aspects: pipelined segments, cumulative acks, single retransmission timer

retransmission triggered by: timeout events, duplicate acks. Retransmission only resends the oldest unacked segment.

cumulative acks: if ACK=100 has been lost but ACK=120 is received, the former packet will not be retransmitted.

TCP ACK generation details

The TCP receiver can wait up to 500ms (timeout value) for another segment if an in-order segment with expected sequence number and no pending ACK segments.

TCP time out value must be minimally longer than RTT.

Fast retransmit

If a segment is lost, there will be many duplicate ACKs from the pipeline (since last unACKed).

Hence if 4 ACKs with same ACKnum, resend oldest unacked segment, so no need to wait for timeout.

Out of order packets

Chance of wraparound and old packet using a consecutive ACK is received is less with a bigger range of sequence numbers.