CN/Lesson 2 of 8/8 min read
Physical and Data Link Layers
Transmission media, encoding, framing, error detection (parity, checksum, CRC), Hamming code, flow control and ARQ protocols, MAC addresses, multiple access (ALOHA, CSMA/CD, CSMA/CA), Ethernet, switching, VLANs and ARP.
On this page
Physical layer
Moves raw bits over a medium. Defines voltages, timing, connectors, data rates.
Transmission Media
Guided (wired):
- Twisted pair (Cat5e, Cat6): cheap, up to 1 to 10 Gbps over 100 m; twisting reduces crosstalk.
- Coaxial cable: better shielding, used by cable TV/internet.
- Optical fiber: light pulses; highest bandwidth, long distances, immune to electromagnetic interference. Single-mode (long distance) vs multi-mode (short).
Unguided (wireless): radio waves (Wi-Fi, cellular), microwaves (point-to-point, satellite), infrared (short range).
Signals and encoding
- Line coding converts bits into signals: NRZ, Manchester (a transition in the middle of every bit, self-clocking, used by 10 Mbps Ethernet).
- Modulation (for analog carriers): ASK, FSK, PSK, QAM.
- Nyquist: max bit rate of a noiseless channel
= 2 x B x log2(L)(B bandwidth in Hz, L signal levels). - Shannon: capacity of a noisy channel
= B x log2(1 + SNR). - Multiplexing: share one link among many signals: FDM (frequency), TDM (time), WDM (wavelength, fiber).
Data link layer
Provides node-to-node delivery over a single link. Two sublayers:
- LLC (Logical Link Control): flow and error control, interface to the network layer.
- MAC (Media Access Control): addressing and access to the shared medium.
Services: framing, physical addressing, error control, flow control, access control.
Framing
Framing splits the bit stream into frames so the receiver knows where each starts and ends.
- Character count: header gives the frame length (fragile: one corrupted count desynchronizes everything).
- Byte stuffing: special flag bytes mark boundaries; an escape byte is inserted before any flag inside the data (PPP).
- Bit stuffing: flag is
01111110; the sender inserts a0after any five consecutive1s in the data, and the receiver removes it (HDLC).
Bit Stuffing
Data 011111110 becomes 0111110110 on the wire: a 0 stuffed after the fifth 1.
Error Detection
Parity Check
- Single parity bit: add one bit so the number of 1s is even (even parity). Detects all single-bit errors, misses any even number of flipped bits.
- 2D parity: parity per row and column; detects and can correct single-bit errors.
Checksum
Sum the data in 16-bit words using one's complement arithmetic and send the complement. The receiver sums everything including the checksum; the result should be all 1s. Used in IP, TCP and UDP headers. Weak against some patterns (reordered words).
CRC
Cyclic Redundancy Check treats data as a polynomial and divides it by an agreed generator polynomial using modulo-2 (XOR) division.
- Generator of degree
r(r + 1 bits). Appendrzeros to the data. - Divide by the generator using XOR; the remainder is the CRC.
- Send data + CRC. The receiver divides the whole frame; remainder 0 means no error detected.
Example: data 1101, generator 1011 (degree 3). Append 000: 1101000. XOR division gives remainder 001. Transmit 1101001.
CRC detects all single-bit errors, all double-bit errors (with a good generator), all odd numbers of errors (if the generator has the factor x + 1), and all burst errors of length <= r. Ethernet uses CRC-32 in its FCS trailer.
Error correction: Hamming Code
Hamming code adds parity bits at positions that are powers of 2 (1, 2, 4, 8, ...) so the receiver can locate and fix a single-bit error.
Number of parity bits r must satisfy 2^r >= m + r + 1 for m data bits. For 4 data bits, r = 3: the (7,4) Hamming code.
Each parity bit covers positions whose binary index has that bit set (P1 covers 1,3,5,7; P2 covers 2,3,6,7; P4 covers 4,5,6,7). The receiver recomputes the checks; the failed checks written as a binary number give the position of the flipped bit.
Hamming distance: to detect d errors you need minimum distance d + 1; to correct d errors you need 2d + 1.
Error correction on links (FEC) is used when retransmission is expensive: satellite, Wi-Fi, ECC memory.
Flow Control
Flow control stops a fast sender from overwhelming a slow receiver. Combined with retransmission on errors it is called ARQ (Automatic Repeat reQuest).
Stop-and-Wait
Send one frame, wait for the ACK, then send the next. Timeout triggers retransmission; sequence numbers 0 and 1 detect duplicates.
Efficiency: with a = propagation delay / transmission delay, efficiency = 1 / (1 + 2a). Terrible on long or fast links.
Sliding Window
Allow up to W unacknowledged frames in flight. With window W, efficiency = W / (1 + 2a) (capped at 1). To fully use the link, W >= 1 + 2a.
Go-Back-N
- Sender window
N, receiver window 1: the receiver accepts only in-order frames and discards others. - One cumulative ACK acknowledges everything up to a number.
- On timeout or loss, the sender retransmits that frame and everything after it.
- With
k-bit sequence numbers, max window =2^k - 1.
Simple receiver, but wastes bandwidth retransmitting good frames.
Selective Repeat
- Sender and receiver windows both
N; the receiver buffers out-of-order frames. - Only lost or corrupted frames are retransmitted (individual ACKs or NAKs).
- Max window =
2^(k-1)(half the sequence space) to avoid ambiguity between old and new frames.
More efficient on lossy links, more complex receiver. TCP uses a hybrid of these ideas (see TCP).
MAC Address
A MAC address is a 48-bit hardware address burned into the network interface card, written as 00:1A:2B:3C:4D:5E.
- First 24 bits: OUI (manufacturer). Last 24: device specific.
FF:FF:FF:FF:FF:FFis broadcast.- Used for delivery within one LAN. It changes at every hop, while the IP address stays end-to-end.
Multiple access protocols
When many nodes share one medium, who sends when?
ALOHA
- Pure ALOHA: send whenever you want; on collision, wait random time and resend. Max efficiency
1/(2e) ~ 18.4%. - Slotted ALOHA: send only at slot starts; vulnerable period halved. Max efficiency
1/e ~ 36.8%.
CSMA/CD
Carrier Sense Multiple Access with Collision Detection (classic shared Ethernet):
- Listen; if the medium is idle, transmit.
- While transmitting, detect collisions.
- On collision: send a jam signal, stop, wait using binary exponential backoff (random
0to2^k - 1slots after the k-th collision), retry.
Minimum frame size so collisions are detected while still sending: transmission time >= 2 x propagation delay. Ethernet's 64-byte minimum frame comes from this. Modern full-duplex switched Ethernet has no collisions, so CSMA/CD is effectively unused.
CSMA/CA
Collision Avoidance, used by Wi-Fi (802.11), because wireless nodes cannot detect collisions while transmitting and may not hear each other (hidden terminal problem). Wait for idle plus a random backoff, optionally reserve the channel with RTS/CTS, and require an ACK for every frame.
Controlled access alternatives: polling, token passing (Token Ring). Channelization: FDMA, TDMA, CDMA.
Ethernet
Ethernet (IEEE 802.3) is the dominant wired LAN technology.
Frame format:
| Preamble (7) | SFD (1) | Dest MAC (6) | Src MAC (6) | Type/Length (2) | Payload (46-1500) | FCS (4) |- Minimum frame 64 bytes, maximum 1518 bytes (payload up to 1500 = the MTU).
- Speeds: 10 Mbps up to 400 Gbps.
Switching
A switch learns which MAC addresses are on which port by reading the source MAC of incoming frames (MAC address table / CAM table).
- Known destination: forward out of that port only.
- Unknown destination or broadcast: flood to all other ports.
- Each port is its own collision domain; the whole switch (without VLANs) is one broadcast domain.
Spanning Tree Protocol
Redundant links between switches create loops, and broadcast frames would circulate forever (broadcast storms). Spanning Tree Protocol (STP, 802.1D) elects a root bridge and blocks redundant ports to make a loop-free tree, re-enabling them if an active link fails. RSTP converges faster.
VLAN
A VLAN (Virtual LAN) splits one physical switch into multiple logical broadcast domains (e.g. HR, Engineering, Guests).
- Improves security and reduces broadcast traffic.
- 802.1Q tagging adds a 4-byte tag with the VLAN ID on trunk links between switches; access ports belong to a single VLAN.
- Traffic between VLANs must go through a router or layer 3 switch.
ARP
ARP (Address Resolution Protocol) maps an IPv4 address to a MAC address on the local network.
- Host A wants to send to
192.168.1.20and checks its ARP cache. - On a miss it broadcasts: "Who has 192.168.1.20? Tell 192.168.1.10".
- The owner replies by unicast with its MAC.
- A caches the mapping for a while.
If the destination is on another network, A ARPs for the default gateway's MAC instead. ARP has no authentication, which enables ARP spoofing. IPv6 replaces ARP with Neighbor Discovery (NDP).
RARP (reverse, MAC to IP) is obsolete, replaced by DHCP.
Quick revision
- CRC = modulo-2 division by a generator; Ethernet uses CRC-32.
- Hamming: parity bits at powers of 2;
2^r >= m + r + 1. - Stop-and-wait efficiency
1/(1+2a); GBN window2^k - 1; SR window2^(k-1). - CSMA/CD for wired, CSMA/CA for Wi-Fi.
- Switches learn source MACs; VLANs split broadcast domains; ARP maps IP to MAC.