TCP Segment Structure

image

Maximum Segment Size (MSS)

MSS define the how many bytes of effective payload that a single TCP segment can carry.

image

ACK & Seq Number

  • Seq Number: The sequence number of the first byte in the segment’s data payload.
  • ACK Number: The sequence number of the next byte that the sender of the ACK expects to receive.

Note that both Seq Number and ACK Number are byte-oriented, not segment-oriented, and ACK Number is the next expected byte not the last received byte. The following diagram illustrates this concept: (Assume only A is sending data)

sequenceDiagram
    participant A as Host A (Sender)
    participant B as Host B (Receiver)

    Note over A,B: TCP Connection Established
    Note over A: Initial SEQ = x
    Note over B: Initial SEQ = y

    A->>B: [ACK], SEQ=x, ACK=y + 1, Payload: 10 bytes
    Note over B: receives bytes 0-9, expects SEQ = x + 10
    B->>A: [ACK], SEQ=y, ACK=x + 10 (Acknowledges bytes 0-9)
    Note over A: SEQ = x + 10

    A->>B: [ACK], SEQ=x+10, ACK=y+1, Payload: 15 bytes
    Note over B: Receives bytes 10-24, expected SEQ = x + 25
    B->>A: [ACK], SEQ=y, ACK=x+25 (Acknowledges bytes 10-24)

    Note over A,B: TCP Connection Termination

Connection Management

Three-way Handshake

Note that it is possible to bring data in the third segment of the handshake.

sequenceDiagram
    participant A as Host A
    participant B as Host B

    Note over A: Initial SEQ = X
    Note over B: Initial SEQ = Y

    A->>B: [SYN] SEQ = X
    Note right of B: receive X, expect X + 1 from A

    B->>A: [SYN, ACK] SEQ = Y, ACK = X + 1
    Note left of A: SEQ = X + 1, expect Y + 1 from B

    A->>B: [ACK] SEQ = X + 1, ACK = Y + 1, Payload: L bytes
    Note right of B: SEQ = Y + 1, expect X + 1 + L from A

    Note over A,B: Connection Established

Automatic Repeat reQuest (ARQ)

Go Back N

Go back N is a solution to packet loss in TCP pipelining. The basic idea is when packet loss happens. The sender will resend the entire window from the lost packet.

As for the sender side:

  • maintain a sending window of size 𝑁
  • can send up to 𝑁 packets within the window without waiting for ACK
  • if not receive any ACK after time 𝑡, resend the window

As for the receiver side:

  • send ACK with the highest in-order sequence number
  • discard out-of-order packets
    • For example [1], [x], [3], [4], although we receive [3], [4] we still need to discard it because we haven’t receive [2]. So we ACK 1

Tips

The convention of ACK number in go back N differs from standard TCP. Here, the ACK number indicates the last correctly received packet instead of the next expected packet.

sequenceDiagram
    participant A as Host A
    participant N as Network
    participant B as Host B

    Note over A,B: Window Size = 4

    A->>B: PKT 0
    A--xN: PKT 1
    A->>B: PKT 2
    A->>B: PKT 3

    Note over A: Reach window limit, stop sending
    Note over B: Receives PKT 0, 2, 3 and drops PKT 2,3

    B->>A: ACK=0

    Note over A: Retransmit from PKT 1
    A->>B: PKT 1
    A->>B: PKT 2
    A->>B: PKT 3

    Note over A: Window not full, can send new packets
    A->>B: PKT 4

It is also possible that sender receive ACKs out of window range due to network delay. In this case, the sender should ignore these ACKs.

sequenceDiagram
    participant S as Sender
    participant N as Network
    participant R as Receiver

    Note over S,R: Window: [0,1,2]

    S->>+R: Pkt 0
    S->>+R: Pkt 1
    S->>+R: Pkt 2

    Note over R: All packets received successfully
    R-->>N: ACK 0 (delayed in network)
    R-->>N: ACK 1 (delayed in network)
    R-->>N: ACK 2 (delayed in network)

    Note over S: Timeout, retransmit the whole window

    S->>R: Resend Pkt 0
    S--xN: Resend Pkt 1
    S--xN: Resend Pkt 2

    Note over R: Duplicate packets received
    R->>S: ACK 0 (immediate)

    Note over S: Receive ACK 0, Window advances: [1, 2, 3]
    S->>R: Pkt 3

    Note over N,S: Delayed ACKs finally arrive!
    N->>S: ACK 0
    Note over S: ACK 0 ignored (outside current window)