A network is
$$\text{Network}=\text{Nodes}+\text{Links}+\text{Protocols},$$logical 逻辑上(transparent)
physical 实现
Remember that
$$\text{Computer Network}=\text{Protocol Stack}+\text{Addressing}+\text{Routing},$$base on which we build the Internet to connect everything.
Architecture & Layering
The network architecture, equivalently, the protocol stack, organizes communication into layers, each providing services to the layer above while hiding implementation details from the layer below.
A simple example is the OSI model, which partitions the communication process into seven layers:
$$\text{Physical}\to\text{Data Link}\to\text{Network}\to\text{Transport}\to\text{Session}\to\text{Presentation}\to\text{Application}.$$The OSI model provides a conceptual framework; it does not define specific protocols. Now consider the TCP/IP model, which condenses these into four layers:
$$\text{Network Access}\to\text{Internet}\to\text{Transport}\to\text{Application}.$$If we examine the Internet layer, IP provides a best-effort, connectionless datagram service — no guarantees of delivery, order, or integrity. This illustrates that architectural choices define the service model, while protocols implement it. The former is determined by design philosophy (e.g., the end-to-end principle), while the latter is determined by engineering constraints.
Density of Protocol Headers
Physical Layer
Physical layer is responsible for transmitting raw bits over a physical medium
$$b_1b_2\cdots b_n \xrightarrow{\text{modulation}} s(t) \xrightarrow{\text{channel}} r(t) \xrightarrow{\text{demodulation}} \hat{b}_1\hat{b}_2\cdots\hat{b}_n,$$with the concept of bandwidth (maximum data rate) and latency (propagation delay), where each signal faces attenuation, noise, and distortion. Fundamental concepts include
Bandwidth/Throughput/LatencySignal-to-Noise Ratio (SNR)Modulation/Demodulation/Encoding
A word on units: b (bit) vs. B (Byte). Bandwidth and data rate are always expressed in bits per second (bps) — the physical layer transmits symbols that encode bits, not bytes. Storage and packet sizes use bytes.
$$1\text{ B} = 8\text{ b},\quad 1\text{ KB} = 10^3\text{ B},\quad 1\text{ KiB} = 2^{10}\text{ B}.$$Network speeds use decimal prefixes (M $=10^6$, G $=10^9$), so a $100$ Mbps link delivers at most $12.5$ MB/s at the application level — and less after protocol overhead. Memory and OS contexts typically use binary prefixes (Mi $=2^{20}$, Gi $=2^{30}$), which is why a “4 GB” file may not fit in a router’s $4$ GiB buffer.
Generally speaking, Shannon’s theorem is more likely to illustrate the fundamental limit of a physical channel.
Shannon’s Theorem
The maximum channel capacity $C$ (in $\text{bps}$) for a bandlimited channel with Gaussian noise:
$$C = B\cdot\log_2\left(1+\frac{S}{N}\right)$$where $B$ is bandwidth in $\text{Hz}$ and $S/N$ is the signal-to-noise power ratio (not in $\text{dB}$).
It is a theoretical upper bound; achieving it requires optimal coding. Practical systems approach this limit with advanced coding schemes (e.g., LDPC, Turbo codes).
Nyquist Criterion
For a noiseless channel, Nyquist gives:
$$C = 2B\cdot\log_2 V$$where $V$ is the number of discrete signal levels. For a binary signal ($V=2$), $C=2B$. Combining Nyquist and Shannon yields the practical constraint:
$$\text{Data Rate} \le \min(2B\log_2 V,\; B\log_2(1+S/N)).$$Transmission Media
| Medium | Bandwidth | Distance | Interference |
|---|---|---|---|
| Twisted Pair (Cat 5e/6) | up to 1 Gbps / 10 Gbps | ~100 m | Moderate |
| Coaxial Cable | up to 10 Gbps | ~1 km | Low |
| Fiber Optic (Single-mode) | up to 400 Gbps+ | ~100 km | Negligible |
| Wireless (Wi-Fi 6/7) | up to 9.6 Gbps / 46 Gbps | ~100 m | High |
Encoding Schemes
Given a bit stream, we encode it into a signal waveform. Examples include:
- NRZ (Non-Return to Zero): $1\to\text{high}$, $0\to\text{low}$. Simple but suffers from baseline wander with long runs.
- NRZI: flip on $1$, stay on $0$. Solves consecutive $1$s but not $0$s.
- Manchester: $1\to\text{high-low}$, $0\to\text{low-high}$. Self-clocking (guaranteed mid-bit transition). Efficiency = $50\%$.
- 4B/5B: encode every 4 data bits into 5 code bits chosen to guarantee sufficient transitions, then transmit with NRZI. Efficiency = $80\%$. Used in Fast Ethernet (100BASE-TX).
Switching: The Network Core
Before diving into the data link layer, we must understand how data is transferred through the network core — the mesh of routers and switches that interconnect end systems. The fundamental question is: given a message to be sent from source $A$ to destination $B$ across a network of intermediate nodes, how should the network transfer the data?
There are three canonical answers, forming a historical progression:
$$\text{Circuit Switching} \to \text{Message Switching} \to \text{Packet Switching}.$$Circuit Switching
Circuit switching establishes a dedicated, end-to-end communication path (a circuit) between the source and destination before any data is transmitted. The circuit reserves resources (bandwidth, buffer space) along the entire path, and these resources remain allocated for the duration of the communication, regardless of whether data is actively being sent.
$$A \xrightarrow{\text{reserved path}} N_1 \xrightarrow{\text{reserved path}} N_2 \xrightarrow{\text{reserved path}} \cdots \xrightarrow{\text{reserved path}} B.$$This is exactly how the traditional Public Switched Telephone Network (PSTN) works: when you dial a number, the network establishes a physical (or virtual) circuit through a series of switches; voice samples flow along this dedicated path; when you hang up, the circuit is torn down and resources are released.
How Circuits Are Established
Circuit switching operates in three phases:
- Circuit Establishment: the source sends a setup request; each switch along the path allocates a dedicated channel (a time slot in TDM or a frequency band in FDM) and forwards the request; an acknowledgment travels back to the source. This is essentially a signaling protocol (e.g., SS7 in telephony).
- Data Transfer: data flows along the reserved path at a guaranteed constant rate. No addressing is needed during transfer — the switches know which channel belongs to which circuit.
- Circuit Teardown: either party signals release; all switches along the path free the reserved resources.
Multiplexing on a Single Link
A physical link can host multiple circuits simultaneously through:
FDM (Frequency Division Multiplexing): the link’s frequency spectrum is partitioned into disjoint bands, each assigned to one circuit. For $n$ circuits sharing a link of bandwidth $B$, each gets $B/n$ Hz (plus guard bands). Radio and TV broadcasting, ADSL, and early analog telephony use FDM.
$$\underbrace{|\text{Circuit }1|\text{Circuit }2|\cdots|\text{Circuit }n|}_{\text{Frequency}}$$TDM (Time Division Multiplexing): time is divided into fixed-length frames, and each frame is partitioned into $n$ slots, one per circuit. Each circuit periodically gets its slot. For a link of rate $R$, each circuit effectively gets $R/n$ bps. Modern digital telephony (T1/E1, SONET/SDH) uses TDM.
$$\underbrace{|1|2|\cdots|n|1|2|\cdots|n|}_{\text{Time}}$$Analysis of Circuit Switching
Suppose $n$ circuits share a link of rate $R$. If $k$ users are active simultaneously ($k\le n$), each gets exactly $R/k$ bps (often fixed at $R/n$ in practice).
- Advantages: guaranteed bandwidth, constant delay (no queuing), in-order delivery, no per-packet overhead once the circuit is set up. Ideal for real-time, constant-bit-rate applications (voice, video conferencing).
- Disadvantages:
- Inefficient for bursty traffic. Resource reservation is wasteful if the circuit is idle (e.g., pauses in conversation — the “silent period” problem). In a phone call, about $50\%$ of the time is silence.
- Long setup delay before data transfer can begin ($\sim$ seconds for international calls). This overhead is disproportionate for short messages.
- Vulnerability to failure. If any switch or link along the circuit fails, the entire circuit is broken and must be re-established.
- Stateful: each switch must maintain per-circuit state, limiting scalability.
Message Switching
Message switching eliminates the need for a dedicated circuit. Instead, the source sends the entire message (as a single unit) to the first switch. The switch receives the message in its entirety, stores it, checks for errors, selects the next hop (routing), and forwards it. This store-and-forward process repeats at each intermediate node until the message reaches its destination.
$$A \xrightarrow{\text{message}} N_1 \xrightarrow[\text{store}]{\text{\& forward}} N_2 \xrightarrow[\text{store}]{\text{\& forward}} \cdots \xrightarrow{\text{message}} B.$$Telegraph networks historically used message switching, and email systems (SMTP store-and-forward relays) still follow this paradigm.
Store-and-Forward Delay
Consider a message of $L$ bits sent over a path of $h$ hops, each link having rate $R$ bps (and ignoring propagation, processing, and queuing delays for simplicity). At each hop, the entire message must be received before forwarding can begin:
$$T_{\text{total}} = h\cdot\frac{L}{R} = h\cdot T_{\text{trans}}.$$For $h=3$ hops, a $L=7.5\times 10^6$ bit message ($\approx 1$ MB) over $R=1.5$ Mbps links would take:
$$T_{\text{total}} = 3 \times \frac{7.5\times 10^6}{1.5\times 10^6} = 3 \times 5 = 15\text{ s}.$$Example (unified scenario, see end of section): $L=6\times 10^6$ bits, $h=3$, $R=2$ Mbps, $D_{\text{prop}}=10$ ms/hop, $D_{\text{proc}}=1$ ms/switch. Message switching total delay: $3\times 3.0 + 3\times 0.01 + 2\times 0.001 = 9.032$ s. The transmission component alone ($9$ s) is $3\times$ circuit/packet switching because each hop must receive the entire message before forwarding.
- Advantages: no circuit setup delay, dynamic routing (can adapt to link failures), efficient for messages with varying destinations (no reservation overhead).
- Disadvantages:
- Enormous buffering requirement at each switch — must store the full message (potentially gigabytes).
- Hop-by-hop delay accumulates multiplicatively with path length (unlike circuit switching, where delay is only propagation).
- Head-of-line blocking: a long message blocks subsequent messages sharing the same link; a short interactive message behind a large file transfer suffers severe delay.
- Error handling is expensive: a single bit error requires retransmission of the entire message across the entire path.
Packet Switching
Packet switching is the synthesis that resolves the weaknesses of both circuit and message switching, and it is the foundation of the modern Internet. The key insight is to break the message into small, fixed-maximum-size packets (typically $\sim 1500$ bytes). Each packet is independently switched through the network via the store-and-forward principle, but because packets are small, they interleave on links and pipelining hides latency.
$$\text{Message } M = [\underbrace{P_1}_{\text{hdr}+\text{payload}}\mid\underbrace{P_2}_{\text{hdr}+\text{payload}}\mid\cdots\mid\underbrace{P_k}_{\text{hdr}+\text{payload}}].$$$$A \xrightarrow{P_3,P_2,P_1} N_1 \xrightarrow{P_3,P_2,P_1} N_2 \xrightarrow{P_3,P_2,P_1} \cdots \xrightarrow{P_3,P_2,P_1} B$$$$\text{(packets interleaved across shared links)}$$How Packet Switching Works
Each packet carries a header containing the destination address (and other control information). Each switch (router):
- Receives the packet.
- Examines the header — the destination address determines the output interface.
- Stores the packet in a queue for the output link (if the link is busy).
- When the link becomes available, transmits the packet.
Crucially, routers do not maintain per-connection state (in the basic datagram model) — they are stateless with respect to end-to-end flows. This is the statistical multiplexing principle: resources are allocated on demand, aggregated over many flows, achieving much higher utilization than circuit switching for bursty traffic.
Pipelining and Delay Analysis
Consider the same message of $L$ bits, broken into $k$ packets each of size $L/k$ (plus header overhead, ignored here), over $h$ hops of rate $R$. Because packets are small, they can be pipelined: while the first packet is being forwarded from $N_1$ to $N_2$, the second packet can be transmitted from $A$ to $N_1$:
$$T_{\text{total}} = \underbrace{k\cdot\frac{L/k}{R}}_{\text{source transmits all packets}} + \underbrace{(h-1)\cdot\frac{L/k}{R}}_{\text{last packet traverses remaining hops}} = \frac{L}{R} + (h-1)\frac{L}{kR}.$$As $k\to\infty$ (infinitely fine-grained packets), $T_{\text{total}} \to L/R$ — only one transmission delay, plus propagation. In practice, $k$ is finite but still yields a dramatic improvement over message switching:
For the earlier example ($L=7.5\times 10^6$ bits, $h=3$, $R=1.5$ Mbps) with $k=500$ packets of $1500$ bytes ($1.2\times 10^4$ bits):
$$T_{\text{total}} = \frac{7.5\times 10^6}{1.5\times 10^6} + 2\times\frac{1.2\times 10^4}{1.5\times 10^6} = 5 + 0.016 = 5.016\text{ s},$$vs. message switching’s $15$ s — a $3\times$ speedup.
Example (unified scenario): $L=6\times 10^6$ bits, $h=3$, $R=2$ Mbps, $D_{\text{prop}}=10$ ms/hop, $D_{\text{proc}}=1$ ms/switch, $k=500$ packets of $12000$ bits. Packet switching total: $3.0 + 2\times 0.006 + 3\times 0.01 + 2\times 0.001 = 3.044$ s — only $14$ ms more than circuit switching’s data-transfer phase, dramatically better than message switching’s $9.032$ s.
Two Flavors: Datagram vs. Virtual Circuit
Packet switching comes in two variants, differing in whether the network maintains per-flow state:
| Datagram (IP) | Virtual Circuit (ATM, MPLS) | |
|---|---|---|
| Connection setup | None (each packet independent) | Required (VC setup phase) |
| Addressing | Full destination address in each packet | Short VC identifier (VCI) per packet |
| Routing | Per-packet (stateless) | Per-flow (stateful), fixed path |
| Switch failure | Packets re-route around failure | VC broken; must be re-established |
| Ordering | Not guaranteed (packets may take different paths) | Guaranteed (all packets follow same path) |
| QoS | Difficult (per-packet basis) | Easier (resources reserved per VC) |
Datagram (Connectionless): this is how IP works. Each packet is routed independently; successive packets between the same hosts may follow different paths. Routers maintain forwarding tables (prefix-based), but no per-connection state. Robust to failures, but QoS is challenging.
Virtual Circuit (Connection-Oriented): a setup phase establishes a path and assigns a VCI (Virtual Circuit Identifier) at each link. During data transfer, each packet carries only the VCI (short, local to that link), and switches forward based on (input interface, input VCI) $\to$ (output interface, output VCI) mappings. This resembles circuit switching, but resources are not exclusively reserved — statistical multiplexing still applies. ATM and MPLS use this model.
Comparison of the Three Switching Paradigms
| Circuit Switching | Message Switching | Packet Switching | |
|---|---|---|---|
| Resource allocation | Dedicated (reserved) | On-demand (no reservation) | On-demand (statistical multiplexing) |
| Setup phase | Yes (seconds) | No | Datagram: No / VC: Yes |
| Per-hop state | Yes (circuit ID) | No | Datagram: No / VC: Yes |
| Addressing overhead | None during transfer | Full address in header | Full address (datagram) or VCI (VC) |
| Store-and-forward delay | None (cut-through) | $h\cdot L/R$ | $L/R + (h-1)L/(kR)$ |
| Buffer requirement | None | Full message | Small (packet-sized) |
| Efficiency for bursty traffic | Poor | Good (but head-of-line blocking) | Excellent |
| Delay jitter | Constant (ideal) | Very high and variable | Variable (queuing) |
| Failure resilience | Circuit breaks | Re-route around failure | Datagram: robust / VC: re-establish |
| Example | PSTN, ISDN, SONET/SDH | Telegram, SMTP | Internet (IP), Ethernet, Wi-Fi, ATM |
Delay Example: Circuit vs. Message vs. Packet Switching
Consider a file of $L = 750\text{ KB}$ sent over $h=3$ hops
$$\texttt{source} \to \texttt{R1} \to \texttt{R2} \to \texttt{destination},$$each link at $R = 2\text{ Mbps}$, with per-hop propagation $D_{\text{prop}} = 10\text{ ms}$ and per-switch processing $D_{\text{proc}} = 1\text{ ms}$. Circuit setup signaling takes $S = 200 \text{ ms}$ round-trip. For packet switching, the file is split into $k = 500$ packets of $1500\text{ B}$ each. Assume zero queuing delay (no competing traffic).
Circuit Switching:
$$D_{\text{circuit}} = S + D_{\text{trans}} + h\cdot D_{\text{prop}} = 0.2 + 3.0 + 3\times 0.01 = 3.23\text{ s}.$$No per-hop transmission or processing during data transfer — the circuit is a pre-established pipe. The dominant term is $D_{\text{trans}}$ ($3.0\text{ s}$); setup ($0.2\text{ s}$) is a one-time tax.
Message Switching:
$$\begin{aligned} D_{\text{message}} &= h\cdot D_{\text{trans}} + h\cdot D_{\text{prop}} + (h-1)\cdot D_{\text{proc}} \\ &= 3\times 3.0 + 3\times 0.01 + 2\times 0.001 \\ &= 9.0 + 0.03 + 0.002 = 9.032\text{ s}. \end{aligned}$$Each hop must receive the entire $6$ Mbit message before forwarding — the store-and-forward tax multiplies $D_{\text{trans}}$ by $h$, dwarfing all other terms.
Packet Switching:
$$\begin{aligned} D_{\text{packet}} &= D_{\text{trans}} + (h-1)\frac{P}{R} + h\cdot D_{\text{prop}} + (h-1)\cdot D_{\text{proc}} \\ &= 3.0 + 2\times\frac{12000}{2\times 10^6} + 3\times 0.01 + 2\times 0.001 \\ &= 3.0 + 0.012 + 0.03 + 0.002 = 3.044\text{ s}. \end{aligned}$$The store-and-forward penalty applies only to a single packet ($12$ ms extra), not the whole message. Pipelining brings the total within $14$ ms of circuit switching’s data phase — without any setup overhead.
Summary:
| Circuit | Message | Packet | |
|---|---|---|---|
| $D_{\text{trans}}$ | $3.000$ s | $9.000$ s | $3.012$ s |
| $D_{\text{prop}}$ | $0.030$ s | $0.030$ s | $0.030$ s |
| $D_{\text{proc}}$ | $0$ | $0.002$ s | $0.002$ s |
| Setup | $0.200$ s | $0$ | $0$ |
| Total | $3.230$ s | $9.032$ s | $3.044$ s |
Data Link Layer
The data link layer is to send frames over a single link (point-to-point or broadcast). It provides a reliable link service to the network layer above, abstracting away the physical layer’s imperfections.
$$\text{Data Link Layer} = \text{Frames} + \text{Link}+ \text{Protocols},$$It transforms the raw bit stream of the physical layer into discrete frames:
$$\texttt{frame}=[\texttt{header}\mid\texttt{payload}\mid\texttt{trailer}],$$and combines the physical links with protocols to form a logical link(circuit, bus, star and so on) between nodes. The data link layer is responsible for framing, addressing, error detection/correction, and flow control. It ensures that frames are delivered reliably and in order over the physical medium.
Framing Methods
- Byte count — first field gives frame length. Vulnerable: one corrupted count desynchronizes everything.
- Byte stuffing (flag bytes) — special start/end flag bytes (e.g.,
0x7Ein PPP); escape byte0x7Dprecedes data bytes that match the flag. If0x7Eappears in data, send0x7D 0x5E. - Bit stuffing — e.g., HDLC: a frame starts and ends with the bit pattern
01111110($6$ consecutive $1$s). The sender inserts (stuffs) a $0$ after every five consecutive $1$s in the data; the receiver removes the $0$ following five consecutive $1$s. This ensures the flag pattern never occurs in the payload. $$011111\underbrace{0}_{\text{stuffed}}10 \implies \text{data: }01111110 \text{ won't appear.}$$
Error Detection
Given a frame of $m$ data bits, append a $r$-bit check code. In general, we want to detect/correct errors using Hamming distance: the number of bit positions where two codewords differ.
To detect $d$ errors: $\text{Hamming distance} \ge d+1$ (HDU, Hamming Distance Upperbound).
To correct $c$ errors: $\text{Hamming distance} \ge 2c+1$.
- Parity bit — single parity bit (appended $0$ or $1$ to make the total number of $1$s even/odd). Hamming distance $=2$, detects all odd numbers of bit errors, but no even numbers.
- CRC (Cyclic Redundancy Check) — treats bit strings as polynomials over $\mathbb{F}_2$. Append an $r$-bit remainder $R(x)$ such that the transmitted $T(x) = x^rM(x) + R(x)$ is divisible by a generator polynomial $G(x)$ of degree $r$. At the receiver, divide by $G(x)$; a non-zero remainder indicates errors. $$\frac{T(x)}{G(x)}\equiv 0 \pmod{2}.$$ Common generator polynomials: CRC-32 ($G(x)=x^{32}+x^{26}+x^{23}+x^{22}+x^{16}+x^{12}+x^{11}+x^{10}+x^8+x^7+x^5+x^4+x^2+x+1$), used in Ethernet. CRC detects all burst errors of length $\le r$, all odd numbers of bit errors, and burst errors longer than $r$ with probability $1-2^{-r}$.
- Checksum — Internet checksum: split data into 16-bit words, sum (one’s complement), take one’s complement of the sum. Simple but weaker than CRC.
Hamming Code
For error correction, Hamming codes use $r$ parity bits to cover $m$ data bits such that:
$$2^r \ge m + r + 1.$$Each parity bit $p_i$ at position $2^{i-1}$ checks bits whose binary index has the $i$-th bit set. The syndrome (computed at receiver) directly indicates the position of a single-bit error.
Flow Control & Stop-and-Wait
In Stop-and-Wait, the sender transmits one frame and waits for an acknowledgment (ACK) before sending the next. If no ACK arrives within a timeout, the frame is retransmitted.
Channel utilization $U$ (with negligible processing time):
$$U = \frac{T_{\text{frame}}}{T_{\text{frame}} + 2\cdot T_{\text{prop}}} = \frac{1}{1+2a},$$where $a = T_{\text{prop}}/T_{\text{frame}}$. Utilization degrades badly for high-bandwidth, high-delay links.
Sliding Window Protocols
To improve utilization, the sender may transmit up to $W$ frames before receiving an ACK (pipelining).
Go-Back-N — the receiver discards all out-of-order frames; upon a timeout, the sender retransmits all frames from the lost one onward.
$$\text{Window size }W \le 2^n - 1\text{ (where }n\text{ bits for seq. number),}$$otherwise ambiguity between new and retransmitted frames.
Selective Repeat — the receiver buffers out-of-order frames and ACKs individually. The sender retransmits only lost frames.
$$\text{Window size }W \le 2^{n-1},\quad \text{typically }W_{\text{send}} = W_{\text{recv}} = 2^{n-1}.$$MAC Sublayer: Medium Access Control
The MAC sublayer governs access to a shared broadcast medium.
ALOHA:
- Pure ALOHA: transmit whenever; if collision, wait a random time and retry. Maximum throughput $S = G e^{-2G} \to 1/(2e)\approx 18.4\%$ at $G=0.5$.
- Slotted ALOHA: time is divided into slots; stations transmit only at slot boundaries. Throughput $S = G e^{-G} \to 1/e \approx 36.8\%$ at $G=1$.
CSMA/CD (Ethernet, IEEE 802.3):
- Carrier Sense: listen before transmitting. If idle, transmit; if busy, wait.
- Collision Detection: while transmitting, monitor the medium. If collision detected, send a jam signal and stop.
- Binary Exponential Backoff: after $k$ collisions, wait a random time in $\{0,1,\ldots,2^k-1\}$ slots ($k\le 10$), then retry. After $16$ collisions, give up.
To detect collisions reliably, Ethernet imposes a minimum frame length:
$$T_{\text{frame}}^{\min} \ge 2\cdot T_{\text{prop}}^{\max},$$i.e., $64$ bytes at $10$ Mbps over $2500$ m (with $4$ repeaters). At higher speeds, either reduce distance or increase minimum frame size (Gigabit Ethernet pads to $512$ bytes using carrier extension).
| |
CSMA/CA (Wi-Fi, IEEE 802.11): Wi-Fi cannot reliably detect collisions (Hidden Terminal Problem), so it avoids them:
- Sense the channel.
- If idle for DIFS (Distributed Inter-Frame Space), transmit.
- Else wait until idle, then start a random backoff counter. Decrement while idle; freeze while busy. Transmit when counter reaches $0$.
- Receiver sends ACK after SIFS (Short IFS). If no ACK, sender assumes collision and doubles the contention window.
Optional RTS/CTS exchange: sender sends Request to Send; receiver replies with Clear to Send. Neighbors hearing RTS/CTS defer (Virtual Carrier Sense via NAV). This addresses the Hidden Terminal Problem.
Ethernet Frame Format
$$\texttt{frame} = [\underbrace{\texttt{Preamble}}_{7\text{B}}\mid\underbrace{\texttt{SFD}}_{1\text{B}\ 10101011}\mid\underbrace{\texttt{Dst MAC}}_{6\text{B}}\mid\underbrace{\texttt{Src MAC}}_{6\text{B}}\mid\underbrace{\texttt{Type/Len}}_{2\text{B}}\mid\underbrace{\texttt{Payload}}_{46-1500\text{B}}\mid\underbrace{\texttt{FCS (CRC-32)}}_{4\text{B}}].$$The Inter-Frame Gap (IFG) of $96$ bit times separates consecutive frames.
Bridges & Switches
A switch (multi-port bridge) connects LAN segments and forwards frames selectively using a MAC address table (a.k.a. forwarding database). It learns source MAC addresses and their incoming ports; unknown destinations are flooded. Loops are eliminated by the Spanning Tree Protocol (STP, IEEE 802.1D).
- Cut-through switching: forward as soon as destination MAC is read ($6$ bytes). Low latency, but propagates errors.
- Store-and-forward: receive entire frame, verify CRC, then forward. Higher latency, but error filtering.
VLAN (IEEE 802.1Q)
A Virtual LAN partitions a physical switch into separate broadcast domains. Frames are tagged with a $4$-byte 802.1Q tag (including a $12$-bit VLAN ID), inserted between the source MAC and Type/Length fields. Trunk links carry traffic for multiple VLANs between switches.
Network Layer
The network layer provides end-to-end packet delivery across multiple hops (routers). Its core functions:
- Routing: determine the path from source to destination (control plane).
- Forwarding: move packets from an input interface to the appropriate output interface (data plane).
The Internet’s network layer is built around the Internet Protocol (IP), which provides a connectionless, best-effort datagram service.
IPv4
An IPv4 address is $32$ bits, written in dotted-decimal notation:
$$\underbrace{192.168.1.1}_{\text{4 octets, each }0\text{--}255}$$IPv4 Header ($20$–$60$ bytes, typically $20$):
$$\texttt{header} = [\underbrace{\texttt{VER+IHL}}_{1\text{B}}\mid\underbrace{\texttt{DSCP+ECN}}_{1\text{B}}\mid\underbrace{\texttt{Total Len}}_{2\text{B}}\mid\underbrace{\texttt{ID}}_{2\text{B}}\mid\underbrace{\texttt{Flags+FragOff}}_{2\text{B}}$$$$\mid\underbrace{\texttt{TTL}}_{1\text{B}}\mid\underbrace{\texttt{Protocol}}_{1\text{B}}\mid\underbrace{\texttt{Hdr Checksum}}_{2\text{B}}\mid\underbrace{\texttt{Src IP}}_{4\text{B}}\mid\underbrace{\texttt{Dst IP}}_{4\text{B}}\mid\underbrace{\texttt{Options}}_{0\text{--}40\text{B}}].$$Key fields:
- TTL (Time To Live): decremented at each hop; packet discarded when $0$, prevents infinite loops.
- Protocol: identifies the transport-layer protocol (TCP = $6$, UDP = $17$, ICMP = $1$).
- Fragmentation Offset: when a packet is too large for a link’s MTU, the router fragments it. The destination reassembles using
ID,Flags (MF bit), andFragment Offset. The offset is in $8$-byte units, so fragmentation occurs at $8$-byte boundaries.
The header checksum covers only the IP header (not payload), recalculated at each hop since TTL changes.
IP Addressing & Subnetting
Classful addressing (historical):
$$A: 0.0.0.0-127.255.255.255,\quad /8,\quad 2^{24}-2\text{ hosts}$$$$B: 128.0.0.0-191.255.255.255,\quad /16,\quad 2^{16}-2\text{ hosts}$$$$C: 192.0.0.0-223.255.255.255,\quad /24,\quad 2^8-2\text{ hosts}$$$$D: 224.0.0.0-239.255.255.255,\quad \text{multicast}$$$$E: 240.0.0.0-255.255.255.255,\quad \text{reserved}$$CIDR (Classless Inter-Domain Routing): uses prefix notation $a.b.c.d/n$, where $n$ is the network prefix length and the remaining $32-n$ bits identify the host. Subnet mask: $n$ ones followed by $32-n$ zeros.
$$\text{Network Address} = \text{IP Address} \land \text{Subnet Mask}.$$The number of usable hosts in a $/n$ subnet is
$$N_{\text{hosts}} = 2^{32-n} - 2,$$subtracting the network address (all host bits $0$) and the broadcast address (all host bits $1$).
Special addresses:
0.0.0.0/8— “this” network (source only)127.0.0.0/8— loopback169.254.0.0/16— link-local (APIPA)10.0.0.0/8,172.16.0.0/12,192.168.0.0/16— private (RFC 1918)255.255.255.255— limited broadcast
NAT (Network Address Translation)
NAT translates private IP addresses to a public IP at the network boundary. It maintains a translation table mapping private (IP, port) pairs to (public IP, NAT-assigned port). Outgoing packets have their source address/port rewritten; incoming packets are demultiplexed by port number. This provides address reuse and a basic firewall, but breaks the end-to-end principle and complicates protocols that embed IP addresses in payloads.
IPv6
IPv6 address is $128$ bits, written as eight groups of four hex digits separated by colons:
$$\texttt{2001:0db8:0000:0000:0000:ff00:0042:8329} \implies \texttt{2001:db8::ff00:42:8329}.$$Double colon :: compresses consecutive zero groups (once per address). The header is a fixed $40$ bytes:
Key differences from IPv4:
- No header checksum (redundant with link-layer and transport-layer checksums).
- No fragmentation by routers (uses Path MTU Discovery; if needed, sender fragments).
- Extension headers replace the Options field.
- Neighbor Discovery Protocol (NDP) replaces ARP.
- Stateless address autoconfiguration (SLAAC).
Routing Algorithms
The routing problem: given a graph $G=(V,E)$ with edge weights $w(u,v)$, find a path of minimum cost from source $s$ to destination $t$.
Distance-Vector (Bellman–Ford based, e.g., RIP): Each router maintains a vector of distances to all destinations. Routers exchange their distance vectors with neighbors. Updates follow the Bellman–Ford equation:
$$d_x(y) = \min_{v\in\text{neighbors}(x)} \left[w(x,v) + d_v(y)\right].$$Converges to correct distances but suffers from the count-to-infinity problem (bad news travels slowly). Mitigations: split horizon (don’t advertise a route back to the neighbor it was learned from), poison reverse (advertise with $\infty$), triggered updates, maximum hop count (RIP: $15$).
| |
Link-State (Dijkstra-based, e.g., OSPF): Each router floods its link states (neighbors and costs) throughout the entire network via Link State Advertisements (LSAs). Once every router has the full topology, each independently runs Dijkstra’s algorithm to compute the shortest-path tree rooted at itself.
Dijkstra’s Algorithm for Routing
| |
- RIP uses distance-vector; metric = hop count; updates every $30$s.
- OSPF uses link-state; metric = configurable cost; flooded LSAs; hierarchical areas (area $0$ is backbone).
- BGP (Border Gateway Protocol) routes between autonomous systems (ASes) using path-vector: each route advertisement carries the full AS path. Policy-based routing, not purely shortest-path.
ARP (Address Resolution Protocol)
Maps an IP address to a MAC address within a LAN. A host broadcasts an ARP request: “Who has $192.168.1.1$?” The target replies with its MAC address. Results are cached in the ARP table (entries timeout to handle changes). For destinations outside the LAN, the sender uses the default gateway’s MAC address.
Transport Layer
The transport layer provides logical communication between application processes running on different hosts. The Internet offers two transport protocols:
| TCP | UDP | |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Reliable (ACK, retransmit) | Unreliable (best-effort) |
| Ordering | In-order byte stream | No ordering guarantee |
| Congestion control | Yes | No |
| Flow control | Yes (sliding window) | No |
| Overhead | Higher (header $20$–$60$B) | Lower (header $8$B) |
| Use cases | Web, email, file transfer | DNS, streaming, VoIP, gaming |
UDP (User Datagram Protocol)
UDP is minimal: it adds only source/destination ports, length, and a checksum over a pseudo-header + UDP segment. No handshake, no state, no retransmission.
$$\texttt{segment}=[\underbrace{\texttt{Src Port}}_{\text{2B}}\mid\underbrace{\texttt{Dst Port}}_{\text{2B}}\mid\underbrace{\texttt{Length}}_{\text{2B}}\mid\underbrace{\texttt{Checksum}}_{\text{2B}}\mid\underbrace{\texttt{Payload}}_{\text{variable}}].$$The UDP checksum covers the UDP header, payload, and a pseudo-header containing source/destination IPs, protocol number, and UDP length. This lets the receiver verify that the datagram arrived at the correct destination.
TCP (Transmission Control Protocol)
TCP provides a reliable, in-order, byte-stream abstraction between two endpoints. Its segment structure:
$$\texttt{segment}=[\underbrace{\texttt{Src Port}}_{\text{2B}}\mid\underbrace{\texttt{Dst Port}}_{\text{2B}}\mid\underbrace{\texttt{Seq\#}}_{\text{4B}}\mid\underbrace{\texttt{ACK\#}}_{\text{4B}}$$$$\mid\underbrace{\texttt{HdrLen+Flags}}_{\text{2B}}\mid\underbrace{\texttt{WinSize}}_{\text{2B}}\mid\underbrace{\texttt{Checksum}}_{\text{2B}}\mid\underbrace{\texttt{UrgPtr}}_{\text{2B}}\mid\underbrace{\texttt{Options}}_{\text{0--40B}}\mid\underbrace{\texttt{Payload}}_{\text{variable}}].$$Key fields:
- Sequence number: byte offset of the first data byte in this segment (excluding SYN/FIN, which consume one sequence number each).
- Acknowledgment number: next expected byte from the other side (cumulative ACK).
- Flags: SYN (synchronize, open), FIN (finish, close), RST (reset), ACK, PSH (push), URG (urgent).
- Window size: receiver’s advertised window for flow control.
TCP Connection Management
The three-way handshake establishes a connection:
$$\text{Client}\xrightarrow{\text{SYN, seq=}x}\text{Server}$$$$\text{Client}\xleftarrow{\text{SYN+ACK, seq=}y,\ \text{ack=}x+1}\text{Server}$$$$\text{Client}\xrightarrow{\text{ACK, seq=}x+1,\ \text{ack=}y+1}\text{Server}$$Each side chooses an Initial Sequence Number (ISN) independently (randomized to prevent sequence number attacks). After the handshake, both directions are open and can transfer data simultaneously (full-duplex).
Connection teardown uses a four-way handshake, as each direction closes independently (half-close possible):
$$\text{A}\xrightarrow{\text{FIN, seq=}u}\text{B}$$$$\text{A}\xleftarrow{\text{ACK, ack=}u+1}\text{B}$$$$\text{A}\xleftarrow{\text{FIN, seq=}v}\text{B}$$$$\text{A}\xrightarrow{\text{ACK, ack=}v+1}\text{B}$$After sending the final ACK, A enters TIME_WAIT state for $2\times\text{MSL}$ (Maximum Segment Lifetime, typically $2$ minutes) to ensure any delayed FIN retransmissions from B are handled. This prevents old segments from a previous incarnation from interfering with a new connection to the same (IP, port) pair.
TCP Reliability
TCP ensures reliability through:
- Cumulative ACK: ACK number $n$ acknowledges all bytes up to $n-1$. If segments arrive out-of-order, the receiver repeats the last in-order ACK (duplicate ACK).
- Timeout and Retransmission: TCP estimates RTT and sets a timeout. Jacobson’s algorithm computes: $$\text{SRTT} = (1-\alpha)\cdot\text{SRTT} + \alpha\cdot\text{SampleRTT},\quad \alpha=1/8$$ $$\text{DevRTT} = (1-\beta)\cdot\text{DevRTT} + \beta\cdot|\text{SampleRTT}-\text{SRTT}|,\quad \beta=1/4$$ $$\text{RTO} = \text{SRTT} + 4\cdot\text{DevRTT}.$$
- Fast Retransmit: if the sender receives $3$ duplicate ACKs for the same sequence number, it retransmits the missing segment without waiting for the timeout. This handles isolated packet loss efficiently.
TCP Flow Control
TCP uses a sliding window to prevent the sender from overflowing the receiver’s buffer:
$$\text{EffectiveWindow} = \text{AdvertisedWindow} - (\text{LastByteSent} - \text{LastByteAcked}).$$The receiver’s advertised window (WinSize in the header) tells the sender how much free buffer space is available. If the advertised window is $0$, the sender pauses and sends periodic window probes (1-byte segments) to detect when the window reopens.
TCP Congestion Control
TCP maintains a congestion window (cwnd) to limit the number of unacknowledged bytes in flight:
The canonical TCP congestion control (Reno) operates in phases:
- Slow Start:
cwndstarts at $1$ MSS (Maximum Segment Size) and doubles every RTT (increases by $1$ MSS per ACK). Exponentially probes available bandwidth untilcwndreaches ssthresh (Slow Start Threshold) or a loss is detected. - Congestion Avoidance:
cwndincreases linearly (by roughly $1$ MSS per RTT:cwnd += MSS * MSS / cwndper ACK). - Loss Detection:
- Timeout (severe congestion):
ssthresh = cwnd/2,cwnd = 1MSS, re-enter Slow Start. - Triple Duplicate ACK (Fast Recovery):
ssthresh = cwnd/2,cwnd = ssthresh + 3*MSS, enter Fast Recovery. For each additional duplicate ACK,cwnd += MSS. Upon receiving a new ACK,cwnd = ssthresh, enter Congestion Avoidance.
- Timeout (severe congestion):
| |
Other variants: CUBIC (Linux default) uses a cubic function instead of AIMD, achieving better utilization on high-BDP (Bandwidth-Delay Product) paths. BBR (Bottleneck Bandwidth and RTT, Google) models the path rather than reacting to loss.
Application Layer
The application layer is where network applications and their protocols reside. Common architectures:
- Client-Server: a persistent server serves many clients (Web, email).
- P2P (Peer-to-Peer): peers communicate directly, with no central server (BitTorrent, blockchain).
- Hybrid: mix of both (Skype originally).
DNS (Domain Name System)
DNS maps human-readable domain names to IP addresses (and vice versa). It is a hierarchical, distributed database.
$$\texttt{www.example.com} \xrightarrow{\text{DNS lookup}} 93.184.216.34.$$The namespace is a tree:
$$\texttt{root(.)}\to \begin{cases} \texttt{com} \to \texttt{example} \to \texttt{www}\\ \texttt{org}\\ \texttt{cn} \to \texttt{edu} \to \texttt{tsinghua}\\ \ldots \end{cases}$$Resource Records (RRs) are the database entries, stored as $(\text{Name}, \text{Type}, \text{Class}, \text{TTL}, \text{Value})$:
| Type | Meaning | Value |
|---|---|---|
| A | IPv4 address | 32-bit IP |
| AAAA | IPv6 address | 128-bit IP |
| CNAME | Canonical name | another domain name |
| NS | Name server | domain name of authoritative name server |
| MX | Mail exchanger | priority + mail server domain |
| PTR | Pointer (reverse) | domain name (for reverse lookups) |
| SOA | Start of Authority | admin info for the zone |
Resolution process:
- Client sends query to local DNS resolver.
- Resolver iteratively queries root → TLD → authoritative servers (iterative), or forwards to another resolver (recursive).
- Results are cached along the entire path, respecting TTL.
DNS uses UDP port $53$ (TCP for large responses or zone transfers). The protocol has a $16$-bit transaction ID to match queries with responses.
HTTP (Hypertext Transfer Protocol)
HTTP is the foundation of the World Wide Web. It follows a request-response model over TCP (typically port $80$, or $443$ for HTTPS/TLS).
Request format:
$$\texttt{METHOD /path HTTP/1.1}\backslash\texttt{r}\backslash\texttt{n}$$$$\texttt{Header-Name: value}\backslash\texttt{r}\backslash\texttt{n}$$$$\ldots$$$$\backslash\texttt{r}\backslash\texttt{n}$$$$\texttt{[optional body]}$$Common methods: GET, POST, PUT, DELETE, HEAD, OPTIONS.
Response format:
$$\texttt{HTTP/1.1 200 OK}\backslash\texttt{r}\backslash\texttt{n}$$$$\texttt{Content-Type: text/html}\backslash\texttt{r}\backslash\texttt{n}$$$$\texttt{Content-Length: 1234}\backslash\texttt{r}\backslash\texttt{n}$$$$\ldots$$$$\backslash\texttt{r}\backslash\texttt{n}$$$$\texttt{[body]}$$Status codes: $1\text{xx}$ (informational), $2\text{xx}$ (success, e.g., $200$ OK), $3\text{xx}$ (redirection, e.g., $301$ Moved Permanently), $4\text{xx}$ (client error, e.g., $404$ Not Found), $5\text{xx}$ (server error).
HTTP/1.1 features: persistent connections (reuse TCP for multiple requests), pipelining (send multiple requests without waiting for responses — rarely used due to head-of-line blocking), chunked transfer encoding, virtual hosting (Host header).
HTTP/2: binary framing, multiplexed streams (solves head-of-line blocking), header compression (HPACK), server push.
HTTP/3: runs over QUIC (UDP-based) instead of TCP, with built-in TLS 1.3, $0$-RTT handshake, per-stream flow control, and no head-of-line blocking across streams.
Cookies: HTTP is stateless; cookies add state. The server sends Set-Cookie: name=value, and the client echoes it in subsequent requests via Cookie: name=value. Cookies enable sessions, personalization, and tracking.
HTTPS (HTTP over TLS): TLS provides confidentiality (symmetric encryption with session keys), integrity (MAC), and authentication (certificates + PKI). The TLS handshake:
- Client sends
ClientHello(supported cipher suites, random nonce). - Server replies
ServerHello(chosen cipher suite, random nonce), sends certificate chain,ServerHelloDone. - Client generates Pre-Master Secret, encrypts it with server’s public key (from certificate), sends
ClientKeyExchange. Both derive session keys from the two nonces + pre-master secret. - Both switch to encrypted communication (
ChangeCipherSpec+Finished).
TLS 1.3 reduces this to $1$-RTT (or $0$-RTT for resumed sessions).
Socket Programming
A socket is the interface between an application and the transport layer, identified by (IP address, port number). Sockets abstract the underlying protocol (TCP/UDP) into file-descriptor-like objects.
TCP Server (C):
| |
TCP Client (C):
| |
Network Performance Metrics
Delay decomposes into four components:
$$D_{\text{total}} = D_{\text{proc}} + D_{\text{queue}} + D_{\text{trans}} + D_{\text{prop}}.$$- Processing delay ($D_{\text{proc}}$): header parsing, error checking, lookup ($\mu$s scale).
- Queuing delay ($D_{\text{queue}}$): time spent waiting in buffers. Highly variable, depends on traffic intensity $\rho = \frac{\lambda L}{R}$, where $\lambda$ is packet arrival rate, $L$ is packet length (bits), $R$ is link rate (bps). When $\rho\to 1$, queuing delay $\to\infty$.
- Transmission delay ($D_{\text{trans}}$): $L/R$, the time to push all bits into the link.
- Propagation delay ($D_{\text{prop}}$): $d/s$, distance divided by signal propagation speed ($s\approx 2\times 10^8$ m/s in copper/fiber).
The Pipe Capacity is the number of bits that can be physically in flight on a link at any instant — i.e., the product of the link rate and the one-way propagation delay:
$$\text{Capacity} = \text{Bandwidth} \times D_{\text{prop}}.$$This is how many bits the link “holds” at a snapshot. But from the sender’s perspective, after pushing the last bit into the link, it must wait for an ACK to travel back before sending more — the full round-trip time is the relevant interval. Hence the Bandwidth-Delay Product (BDP) is defined with RTT:
$$\text{BDP} = \text{Bandwidth} \times \text{RTT} = 2\cdot\text{Capacity}\quad(\text{if symmetric and processing is negligible}).$$BDP is the minimum window size (in bits) required for the sender to keep the pipe continuously busy. If the window is smaller than BDP, throughput is capped by the window rather than the link rate.
Throughput is the effective data rate observed by an application. For a path of $n$ links with rates $R_1,R_2,\ldots,R_n$:
$$\text{Throughput} \le \min\{R_1,R_2,\ldots,R_n\}.$$The bottleneck link determines the end-to-end throughput.
Delay $\times$ Bandwidth rule: to keep a link fully utilized, the sender must have at least BDP bytes of unacknowledged data in flight (or equivalently, the window must be at least BDP). If the window is too small, throughput is limited by:
$$\text{Throughput}_{\max} \le \frac{\text{Window}}{\text{RTT}}.$$Exercises
Ex.1.1. A message of $L=6\times 10^6$ bits is to be sent from $A$ to $B$ over $h=4$ hops, each link having rate $R=2$ Mbps. Compare the total delivery time (ignoring propagation and processing delays) under:
- Circuit switching (setup time $S=500$ ms, ignore teardown).
- Message switching.
- Packet switching with packet size $P=1500$ bytes ($1.2\times 10^4$ bits).
Ex.1.2. Consider a $1$ Mbps link shared by $N$ users. Each user is active $10\%$ of the time, and when active, requires $100$ kbps. Find the maximum $N$ such that:
- All users can be simultaneously supported under circuit switching (FDM, equal allocation).
- The probability of more than $10$ users being active simultaneously is $< 0.001$ under packet switching. (Use the binomial distribution; a normal approximation is acceptable.)
Ex.1.3. A channel has bandwidth $B = 3\text{ MHz}$ and SNR $= 30\text{ dB}$. Compute the maximum achievable data rate according to Shannon’s theorem.
Ex.1.4. Show that for a Stop-and-Wait protocol with frame size $L$ bits, link rate $R$ bps, and one-way propagation delay $D$ seconds, the maximum throughput is
$$T_{\max} = \frac{L}{L/R + 2D}.$$If $L=1000$ bits, $R=1$ Mbps, and $D=50$ ms, what is the utilization?
Ex.2.1. Given the data bit stream $101111110011111010$, show the transmitted bit stream after bit stuffing in HDLC (flag = $01111110$).
Ex.2.2. CRC: Let $M(x) = x^9 + x^8 + x^5 + x^4$ (data), and $G(x) = x^3 + 1$ (generator). Find the CRC remainder $R(x)$ and the transmitted codeword $T(x)$.
Ex.2.3. A slotted ALOHA network has $N$ stations, each transmitting in a slot with probability $p$. Derive the throughput $S$ as a function of $N$ and $p$, and show that $\max S = 1/e$ as $N\to\infty$.
Ex.3.1. Given an IPv4 address $172.20.150.46/27$, find:
- The subnet mask in dotted-decimal notation.
- The network address.
- The broadcast address.
- The number of usable hosts.
Ex.3.2. Show that in distance-vector routing, the count-to-infinity problem can cause a routing loop of length $\ge 2$ to persist for an unbounded number of update rounds if no mitigation is used.
Ex.3.3. Consider the graph $G$ with vertices $\{A,B,C,D,E\}$ and weighted undirected edges:
$$(A,B,2),\ (A,C,5),\ (B,C,1),\ (B,D,4),\ (C,D,1),\ (C,E,7),\ (D,E,3).$$Run Dijkstra’s algorithm from $A$ and write out the distance and predecessor arrays after each iteration.
Ex.4.1. A TCP connection has RTT $= 100$ ms, MSS $= 1460$ bytes, and no packet loss. Approximately how many RTTs are needed for cwnd to reach $100$ KB starting from $1$ MSS in Slow Start? What if we start from Congestion Avoidance?
Ex.4.2. Show that the total number of segments transmitted in a three-way handshake followed by a four-way teardown (each FIN separately ACKed) is minimal for reliable connection setup and teardown.
Ex.4.3. Given the TCP sequence of cwnd values (in MSS): $1,2,4,8,16,32,33,34,35,36,18,19,20,\ldots$, identify:
- The Slow Start phase.
- The Congestion Avoidance phase.
- When a triple-duplicate-ACK event occurred and the
ssthreshvalue set.
Ex.5.1. A DNS resolver caches an A record with TTL $= 3600$s. At $t=0$, a client queries for the domain and the resolver must do a full iterative lookup. At $t=1800$, another client queries the same domain. Describe the difference in resolution time and explain the role of TTL.
Ex.5.2. Design a simple HTTP/1.1 request pipelining scenario: a client sends requests for a base HTML page and $3$ embedded images on a persistent connection with RTT $= 50$ ms, and each response takes $20$ ms transmission time. Compute the total page load time without pipelining vs. with pipelining vs. with HTTP/2 multiplexing. Ignore parsing and rendering delays.