UCB CS168 Notes Chapter 3 Transport

Last updated on April 17, 2026 pm

This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to original Chinese version. I am continuously working to improve the translation.

Transport Layer Design Principles

Shortcomings of the network layer:

  1. IP is best-effort, and packets may encounter a series of problems (dropped, corrupted, reordered, delayed, duplicated)
  2. Requiring programmers to consider packet splitting and merging at the coding level is too complex

Therefore, the goals of the transport layer:

  • de-multiplexing: at the network layer, we use the IP Header to decide whether to go to TCP or UDP; at the transport layer, we determine which port to use
  • abstraction
  • reliability
  • congestion-control and flow-control: preventing overload

We define:

  • at-least-once delivery: receiving a packet once is considered success; if there are duplicates, it is still considered success;
  • exactly-once delivery: built on top of at-least-once delivery.

bytestream abstraction:

Through the network layer, the originally abstract packets can become an ordered bytestream; the sender and receiver (at the application layer) do not need to worry about reliability issues such as data loss, because these are guaranteed by the transport layer.

The transport layer provides two protocols:

  • TCP:

    • de-multiplexing and reliability
    • bytestream
    • abstraction
  • UDP:

    • de-multiplexing only
    • datagram
    • abstraction
    • does not provide reliability guarantees; the application layer handles it itself

    Messages in UDP are limited to a single packet. If an application needs to send larger messages, it is responsible for splitting and reassembling them itself. Note that UDP still performs de-multiplexing via port numbers.

The transport layer is implemented only on end hosts, not on intermediate devices.

TCP Design

We first consider how to implement at-least-once delivery.

Deliver a Single Packet

RTT (Round-Trip-Time):

When the receiver receives a packet, it sends an ack (acknowledgment) to confirm receipt.

The sender maintains a Timer; if it times out (no ack received within the specified time), the packet is retransmitted to solve the packet loss problem.

If the ack is also lost, it is not a big problem, because we are doing at-least-once delivery, so we can just keep retransmitting.

Ideally, the RTT setting should be close to one round-trip time, but in practice it is difficult to measure.

If bits are corrupted: a checksum is added to the transport layer Header (different from the IP layer checksum); if there is a bad checksum, do nothing (meaning: do not send a so-called negative acknowledgment).

Considering all the dropped, delayed, duplicated, corrupted problems, we find that this simple protocol for a single packet can already handle them.

Reliably Delivering Multiple Packets

Compared with the protocol in the previous subsection, we only need to handle the re-ordered problem.

Solution: assign a sequence number to each packet, so even if packets are reordered, the end-host can reorder them.

This is not a stop-and-wait protocol; we send many packets in parallel, rather than waiting for the next ack before retransmitting each time.

If a packet is in the process of being sent, we call it in-flight.

Window

Sending one packet at a time is too slow, but sending all packets at once would overwhelm the network. To solve this problem, we define a W, stipulating that at most W packets may be in-flight at a time.

To make full use of W, we need to satisfy three constraints:

Filling the Pipe

To fully utilize the bandwidth, $W = RTT \times 每秒允许发送的最大数据包数$

Flow Control

The receiver may receive out-of-order packets, but to satisfy the Bytestream Abstraction, we must buffer packets in memory until delivery.

However, the buffer size is limited, so Flow control ensures that the recipient's buffer does not run out of memory.

To this end, we let the receiver tell the sender how much space remains in its buffer. The amount of space remaining in the receiver's buffer is called the advertised window advertised window.

When the sender learns the size of the advertised window, it adjusts its own window accordingly.

Congestion Control

Since the Internet is asynchronous, the bandwidth of a link is unlikely to be used by only a single connection.

This algorithm is actually quite complex; there will be a dedicated section later introducing congestion control algorithms.

Smarter Acknowledgments

Three approaches:

  • Each time an ack is sent, attach the sequence numbers of all packets that have been confirmed as received
  • Each time an ack is sent, first write the contiguous sequence, then write some additional discrete values
  • Each time an ack is sent, send only a single number representing the highest cumulative acknowledgment number, and discard the extra list — TCP actually adopts this approach.

For example, if [1, 2, 4, 5, 6] is received, the ack sent will be #2, because the longest contiguous sequence starting from 1 goes up to 2.

Detecting Loss

Waiting for the timer to expire is too slow. Set a k: if K subsequent packets after the lost packet are acknowledged, we consider that packet lost (even if the timer has not yet expired).

For the cumulative acks used by TCP, we check whether the same ack appears k times; if so, we consider the packet lost.

However, there can be ambiguity: for example, with k=3, we might get seven or eight duplicate acks because many packets were lost, and we have no way of knowing exactly which ones were lost.

TCP Implementation

TCP Segments

The previous discussion mainly relied on the packet abstraction, but applications mainly rely on the bytestream abstraction, so we think about the TCP protocol from another angle — from the perspective of bytes.

We write the bytes in a column, and take segments from it, i.e., filling bytes into a segment, corresponding to a packet as we discussed earlier.

Filling a segment:

  • Either fill it up to the maximum segment size;
  • Each time we start filling a new segment, start a Timer; if it expires, send the segment even if it is not yet full.
  • Do not confuse that Timer with the Timer that decides when to retransmit byte data.

Before sending a segment, prepend a TCP Header.

The TCP segment, with a TCP header and IP header on top, is sometimes called a TCP/IP packet.

MSS (Maximum Segment Size) = MTU (Maximum Transmission Unit) - IP Header Size - TCP Header Size

Sequence Numbers

How to number segments:

  • First there is an ISN (Initial Sequence Number)
  • For each TCP connection, the ISN is randomly and cryptographically generated
  • The first byte is marked as ISN+1, the next byte as ISN+2, the next as ISN+3, and so on
  • The sequence number of the first byte serves as the representative number of the packet
  • The ack sent back is the number of the next byte after the end of the segment (which can be understood as a left-closed, right-open interval)
  • More precisely, the ack means: I have received all bytes with numbers less than this number

Since TCP needs to store state information, each byte stream is called a connection or session, so TCP is a connection-oriented protocol. Unlike the third layer (network layer), where each packet can be handled independently, TCP requires both parties to establish a connection and initialize state before sending data. TCP also needs a mechanism to tear down the connection to free the memory allocated for state on both end hosts.

TCP is Full Duplex

An important property of TCP: the two end hosts send information in both directions (recall the model we built earlier, where one side always sends and the other receives).

Each packet contains both the data being sent and the ack.

TCP Handshake

As mentioned earlier, TCP requires a process of establishing and tearing down connections.

When we create a new connection, both parties need to agree on two starting ISNs (one for each direction).

Perform a three-way handshake to agree on the ISNs in both directions:

  1. First handshake: a SYN message containing the Client's ISN (meaning the Client-to-Server byte stream will start counting from this ISN);
  2. Second handshake: a SYN-ACK message containing the Server's ISN (same meaning as the first handshake), and also containing an ack, indicating successful receipt of the Client's ISN;
  3. Third handshake: an ACK message, where the Client confirms receipt of the Server's ISN.

After the three-way handshake, both sides can send messages to each other.

End Connections

Two ways:

Normal Way

  • Sending FIN means the sender will no longer send messages, but can continue to receive
  • After receiving FIN, sending ACK merely confirms receipt of the message
  • After both sides do this, the connection is terminated.

Abnormal Way

To end the connection unilaterally, send an RST packet, indicating: no more data will be sent or received.

RST packets are usually used when a host encounters an error and can no longer send or receive packets. Note that if an RST occurs and the end host crashes and loses its state, any data in transit will be lost.

If the other side continues to send data, repeatedly send RST.

A simplified TCP state diagram:

Piggybacking

Piggybacked acknowledgments

When the receiver receives a packet, if it has no data to send, the receiver has two options:

  • Send the ack immediately, without any data attached;
  • Wait until there is data to send, then send the ack together with the new data — Piggybacking

Whether data actually gets piggybacked depends on many factors:

  • TCP runs on top of the OS, separate from the application layer, and does not know when the application has information to send;
  • The application layer runs on the Bytestream Abstraction, so it is even less likely to consider Piggybacking;
  • Considering the OS's scheduling mechanisms, implementing this is also very complex
  • Therefore, we often do not piggyback.

An example where data is always piggybacked is the SYN-ACK packet during the handshake. This is unrelated to the issues discussed above, because the three-way handshake is performed by the OS.

Sliding Window

  • In the previous TCP discussion based on the packet abstraction, we considered the window to be the maximum number of packets in-flight at any moment, which could be non-contiguous;
  • Now, we define the window as the maximum number of contiguous bytes that can be transmitted at any given moment.
  • Left side of the window: the first unacknowledged byte
  • Right side of the window: left side + W

Even if some bytes in the middle have been acked, we can never send bytes beyond the window; the only way we can send more bytes is for the window to slide to the right, i.e., when the acknowledgment number increases (the bytes on the left side of the window are acknowledged).

The size of W:

  • flow control && congestion control
  • In the end, it is the minimum of the two.

Detecting Loss and Re-sending Data

Satisfy one of the following two conditions:

  • Timer is up (the sent data has not received the corresponding ack within a certain period of time)
    • In byte-abstraction-based TCP, we consider there to be only a single unique Timer, corresponding to the left side of the window. If it times out, the leftmost segment is retransmitted
    • Each time a new ack arrives, the Timer is reset
  • Receiving k duplicate acks

TCP Header

Some explanations:

  • advertised window is used for flow control and congestion control
  • Flags have 8 bits that can be set; we previously covered four of them: SYN ACK FIN RST

UCB CS168 Notes Chapter 3 Transport
https://blog.wendain.ing/en/posts/cs168-note-3/
Author
wendaining
Posted on
April 16, 2026
Updated on
April 17, 2026
Licensed under