Skip to content

Data Link Layer

3.1 Framing

The data link layer receives a bit stream and divides it into manageable frames.

Framing methods:

  1. Byte count: First field specifies frame length. Vulnerable to corrupted count.
  2. Flag bytes: Special flag byte (0x7E for HDLC) marks start/end. Byte stuffing with escape byte (0x7D) prevents ambiguity.
  3. Bit-oriented flags: Flag pattern 01111110. Bit stuffing: insert 0 after five consecutive 1S in the data.
  4. Clock-based: Manchester encoding provides self-clocking and frame boundaries.

3.2 Error Detection and Correction

Error types: Single-bit errors (one bit flipped), burst errors (multiple consecutive bits).

Hamming distance. d(x,y)d(x, y) is the number of bit positions where codewords xx and yy differ. The minimum distance dmind_{\min} determines capability:

  • Detect up to dmin1d_{\min} - 1 errors.
  • Correct up to (dmin1)/2\lfloor(d_{\min} - 1)/2\rfloor errors.

Hamming code. Adds rr parity bits to mm data bits where 2rm+r+12^r \geq m + r + 1. Each parity bit Covers positions whose binary representation has a 1 in a specific bit position.

Example (Hamming(7,4)). 4 data bits, 3 parity bits. dmin=3d_{\min} = 3: detects 2 errors, corrects 1.

Worked Example: Hamming(7,4) Encoding and Error Correction

Encoding. Transmit data bits d1d2d3d4=1011d_1 d_2 d_3 d_4 = 1011.

Codeword positions (1-indexed): p1  p2  d1  p3  d2  d3  d4p_1\; p_2\; d_1\; p_3\; d_2\; d_3\; d_4. Place data bits: position 3 = 1, position 5 = 0, position 6 = 1, position 7 = 1.

Parity bits cover positions whose binary index has a 1 in the corresponding bit:

p1p_1 covers positions with bit 0 set: {1, 3, 5, 7}. p2p_2 covers positions with bit 1 set: {2, 3, 6, 7}. p3p_3 covers positions with bit 2 set: {4, 5, 6, 7}.

Calculate parity bits (even parity):

p1p_1: positions 1, 3, 5, 7 \to data at 3, 5, 7 are 1, 0, 1. Sum = 0 (even) \to p1=0p_1 = 0.

p2p_2: positions 2, 3, 6, 7 \to data at 3, 6, 7 are 1, 1, 1. Sum = 1 (odd) \to p2=1p_2 = 1.

p3p_3: positions 4, 5, 6, 7 \to data at 5, 6, 7 are 0, 1, 1. Sum = 0 (even) \to p3=0p_3 = 0.

Transmitted codeword: 0  1  1  0  0  1  10\;1\;1\;0\;0\;1\;1

Error correction. Suppose bit 6 is flipped during transmission. Received word: 0  1  1  0  0  0  10\;1\;1\;0\;0\;0\;1

Compute syndrome bits:

S1S_1: check positions 1, 3, 5, 7 \to 0+1+0+1=00 + 1 + 0 + 1 = 0 (even, OK).

S2S_2: check positions 2, 3, 6, 7 \to 1+1+0+1=11 + 1 + 0 + 1 = 1 (odd, error).

S3S_3: check positions 4, 5, 6, 7 \to 0+0+0+1=10 + 0 + 0 + 1 = 1 (odd, error).

Syndrome = S3S2S1=1102=6S_3 S_2 S_1 = 110_2 = 6. The error is at position 6. Flip bit 6: Corrected word = 0  1  1  0  0  1  10\;1\;1\;0\;0\;1\;1.

Answer: The Hamming code detects and corrects the single-bit error at position 6.

Cyclic Redundancy Check (CRC). A polynomial code widely used (Ethernet, Wi-Fi, USB).

  1. Append rr zero bits to the message (rr = degree of generator polynomial G(x)G(x)).
  2. Divide the augmented message polynomial by G(x)G(x) using modulo-2 arithmetic.
  3. Append the remainder (CRC) to the original message.
  4. Receiver divides by G(x)G(x); zero remainder means no error.

Theorem 3.1. CRC detects all single-bit errors, all double-bit errors (if G(x)G(x) has factor (x+1)(x+1)), all odd-numbered errors, and all burst errors of length r\leq r.

Worked Example: CRC Polynomial Division

Compute the CRC for message 110100 using generator G(x)=x3+x+1G(x) = x^3 + x + 1 (binary 1011Degree r=3r = 3).

Step 1: Append r=3r = 3 zero bits \to augmented message: 110100000.

Step 2: Perform modulo-2 (XOR) polynomial division.

M(x)=x5+x4+x2,G(x)=x3+x+1M(x) = x^5 + x^4 + x^2, \quad G(x) = x^3 + x + 1

Maug(x)=x8+x7+x5M_{\mathrm{aug}(x) = x^8 + x^7 + x^5}

Division steps:

  1. x8/x3=x5x^8 / x^3 = x^5. Multiply: x5G(x)=x8+x6+x5x^5 G(x) = x^8 + x^6 + x^5. Subtract (XOR): (x8+x7+x5)+(x8+x6+x5)=x7+x6(x^8 + x^7 + x^5) + (x^8 + x^6 + x^5) = x^7 + x^6.

  2. x7/x3=x4x^7 / x^3 = x^4. Multiply: x4G(x)=x7+x5+x4x^4 G(x) = x^7 + x^5 + x^4. Subtract: (x7+x6)+(x7+x5+x4)=x6+x5+x4(x^7 + x^6) + (x^7 + x^5 + x^4) = x^6 + x^5 + x^4.

  3. x6/x3=x3x^6 / x^3 = x^3. Multiply: x3G(x)=x6+x4+x3x^3 G(x) = x^6 + x^4 + x^3. Subtract: (x6+x5+x4)+(x6+x4+x3)=x5+x3(x^6 + x^5 + x^4) + (x^6 + x^4 + x^3) = x^5 + x^3.

  4. x5/x3=x2x^5 / x^3 = x^2. Multiply: x2G(x)=x5+x3+x2x^2 G(x) = x^5 + x^3 + x^2. Subtract: (x5+x3)+(x5+x3+x2)=x2(x^5 + x^3) + (x^5 + x^3 + x^2) = x^2.

  5. Degree of x2x^2 (which is 2) is less than degree of G(x)G(x) (which is 3). Stop.

Remainder: x2x^2 = binary 100.

Step 3: Append remainder to original message. Transmitted codeword: 110100100.

Verification. The receiver divides 110100100 by 1011. Since x2x^2 was chosen as the remainder, (x8+x7+x5)+x2=(x5+x4+x3+x2)G(x)(x^8 + x^7 + x^5) + x^2 = (x^5 + x^4 + x^3 + x^2) \cdot G(x)So the division yields remainder 0, Confirming no error.

3.3 MAC Protocols

ALOHA. Transmit whenever ready; if collision, wait random time and retransmit.

  • Pure ALOHA: Throughput S=Ge2GS = G e^{-2G}Maximum at G=0.5G = 0.5: Smax=1/(2e)18.4%S_{\max} = 1/(2e) \approx 18.4\%.
  • Slotted ALOHA: Time divided into slots; transmit at slot boundary. S=GeGS = G e^{-G}Maximum at G=1G = 1: Smax=1/e36.8%S_{\max} = 1/e \approx 36.8\%.

CSMA (Carrier Sense Multiple Access). Listen before transmitting.

  • 1-persistent: If busy, wait; transmit immediately when idle. High collision probability.
  • Non-persistent: If busy, wait random time before sensing again. Lower collision, higher delay.
  • p-persistent: If busy, wait until idle, then transmit with probability pp.

CSMA/CD (Collision Detection). Used in wired Ethernet (802.3). Transmit and listen simultaneously; If collision detected, send jam signal and wait random backoff.

  • Binary exponential backoff: After nn-th collision, choose random k0,1,,2min(n,10)1k \in \\{0, 1, \ldots, 2^{\min(n,10)} - 1\\} and wait K×512K \times 512 bit times.
  • Minimum frame size: Must be at least 2τ2\tau where τ\tau is the round-trip propagation delay. For 10 Mbps Ethernet with τ=51.2  μs\tau = 51.2\;\mu\mathrm{s}: minimum frame = 64 bytes.

CSMA/CD collision analysis. The sender must still be transmitting when a collision signal returns From the farthest point on the network. The worst-case round-trip propagation time is 2τ2\tauWhere τ=d/v\tau = d/v (dd is the maximum cable length, vv is the signal propagation speed, 2×1082 \times 10^8 m/s in copper). The minimum frame size is therefore:

Lmin=R×2τ=2RdvL_{\min} = R \times 2\tau = \frac{2Rd}{v}

Where RR is the data rate.

Worked Example: CSMA/CD Minimum Frame Size

A 100 Mbps Ethernet network uses a maximum cable length of 2 km. Signal propagation speed is 2×1082 \times 10^8 m/s. Calculate the minimum frame size.

One-way propagation delay: τ=dv=20002×108=10  μs\tau = \frac{d}{v} = \frac{2000}{2 \times 10^8} = 10\;\mu\mathrm{s}

Worst case: collision occurs at the far end, signal must travel back. Total time is 2τ=20  μs2\tau = 20\;\mu\mathrm{s}.

The sender must still be transmitting after 2τ2\tau: Lmin=R×2τ=100×106×20×106=2000  bits=250  bytesL_{\min} = R \times 2\tau = 100 \times 10^6 \times 20 \times 10^{-6} = 2000\;\mathrm{bits} = 250\;\mathrm{bytes}

Answer: The minimum frame size is 250 bytes (2000 bits). Any frame shorter than this risks an Undetected collision.

CSMA/CA (Collision Avoidance). Used in wireless (802.11). Cannot detect collisions while Transmitting (half-duplex radio). Uses RTS/CTS handshake and inter-frame spacing (IFS).

Wi-Fi (IEEE 802.11). Wireless LAN standards operating in the 2.4 GHz, 5 GHz, and 6 GHz bands.

StandardFrequencyMax PHY RateChannel Width
802.11a5 GHz54 Mbps20 MHz
802.11b2.4 GHz11 Mbps22 MHz
802.11g2.4 GHz54 Mbps20 MHz
802.11n2.4/5 GHz600 Mbps20/40 MHz
802.11ac5 GHz6.93 Gbps20-160 MHz
802.11ax2.4/5/69.6 Gbps20-160 MHz

Key 802.11 mechanisms:

  • DCF (Distributed Coordination Function): CSMA/CA with random backoff. IFS priorities: SIFS (shortest, for ACKs/CTS), PIFS (PCF), DIFS (standard data), EIFS (after error).
  • RTS/CTS: Sender sends Request To Send, receiver replies with Clear To Send. Reduces hidden terminal problem.
  • RTS/CTS pseudocode:
void wifi_send(frame *f) {
while (channel_busy()) wait(DIFS);
send_RTS();
wait_for_CTS(timeout);
if (received_CTS()) {
wait(SIFS);
send_data(f);
wait(SIFS);
wait_for_ACK(timeout);
if (!received_ACK()) {
backoff = choose_random(0, 2^min(n,10) - 1) * slot_time;
wait(backoff);
retry_count++;
}
} else {
backoff = choose_random(0, 2^min(n,10) - 1) * slot_time;
wait(backoff);
}
}
  • MIMO: Multiple-Input Multiple-Output. Uses multiple antennas for spatial multiplexing (simultaneous streams) and diversity (reliability). 802.11n supports up to 4x4 MIMO.
  • OFDM/OFDMA: Orthogonal Frequency-Division Multiplexing divides the channel into subcarriers. OFDMA (802.11ax) allows scheduling different clients on different subcarriers simultaneously.
  • BSS (Basic Service Set): An access point (AP) and its associated stations. ESS (Extended Service Set): multiple BSSs connected by a distribution system.

3.4 Ethernet (IEEE 802.3)

Frame format:

FieldSizeDescription
Preamble7 bytesAlternating 1s and 0s for sync
SFD1 byteStart of frame delimiter (10101011)
Dest MAC6 bytesDestination MAC address
Src MAC6 bytesSource MAC address
Type/Len2 bytesEtherType or frame length
Payload46-1500 BData
FCS4 bytesFrame check sequence (CRC-32)

MAC address: 48-bit globally unique address. First 24 bits: OUI (organisation). Last 24 bits: Device-specific. Broadcast: FF:FF:FF:FF:FF:FF. Multicast: least significant bit of the first Octet is 1.

Ethernet evolution:

StandardSpeedMediumMax Segment
10BASE-T10 MbpsCat3+ UTP100 m
100BASE-TX100 MbpsCat5+ UTP100 m
1000BASE-T1 GbpsCat5e+ UTP100 m
10GBASE-T10 GbpsCat6a+ UTP100 m
10GBASE-SR10 GbpsMulti-mode fibre26-300 m
10GBASE-LR10 GbpsSingle-mode fibre10 km
100GBASE-SR4100 GbpsMulti-mode fibre70-100 m
400GBASE-DR4400 GbpsSingle-mode fibre500 m

Theorem 3.3 (CSMA/CD efficiency). The maximum efficiency of CSMA/CD is:

η=11+5a\eta = \frac{1}{1 + 5a}

Where a=τ/Tfa = \tau / T_f is the ratio of propagation delay to frame transmission time.

Proof. In the worst case, a collision wastes 2τ2\tau time and one frame transmission time TfT_f. The useful time per cycle is TfT_f. The total cycle time is Tf+2τT_f + 2\tau in the worst case, but On average (with the binary exponential backoff), the effective overhead is approximately 5τ5\tau. Thus η=Tf/(Tf+5τ)=1/(1+5a)\eta = T_f / (T_f + 5\tau) = 1 / (1 + 5a). \blacksquare

When a1a \ll 1 (large frames or short distances), efficiency approaches 1. When aa approaches 1 (short frames or long distances), efficiency drops significantly. This is why minimum frame sizes Are imposed.

3.5 VLANs

A Virtual LAN (VLAN) logically segments a physical LAN into separate broadcast domains at layer 2, Without requiring separate physical infrastructure.

802.1Q frame tagging. A 4-byte tag is inserted after the source MAC address:

FieldSizeDescription
TPID16 bitsTag Protocol Identifier (0x8100)
PCP3 bitsPriority Code Point (802.1p)
DEI1 bitDrop Eligible Indicator
VLAN ID (VID)12 bitsVLAN identifier (1-4094)

Trunking. Links between switches that carry traffic for multiple VLANs. Frames are tagged with Their VLAN ID on trunk links.

Benefits: Security (isolation between VLANs), broadcast containment, flexible network management, Reduced cost (no need for separate switches per segment).

3.6 Switching

Circuit switching. A dedicated path is established for the entire duration. Used in traditional Telephone networks. Constant bandwidth but inefficient for bursty data.

Packet switching. Data divided into packets, each routed independently.

  • Datagram: Each packet routed independently (IP). No setup; may arrive out of order.
  • Virtual circuit: Logical path established before data transfer (ATM, MPLS). Ordered delivery.

Theorem 3.2. Packet switching achieves higher utilisation than circuit switching for bursty Traffic.

Proof. Circuit switching reserves the peak rate per connection. Packet switching uses statistical Multiplexing: the sum of peak rates can exceed link capacity as long as the average aggregate rate Does not. \blacksquare

Switch forwarding methods. A layer-2 switch can forward frames using two strategies:

AspectStore-and-ForwardCut-Through
OperationReceives entire frame firstReads destination MAC only
LatencyL/R+dpropL/R + d_{\mathrm{prop}} per hopLh/R+dpropL_h/R + d_{\mathrm{prop}} per hop
Error detectionCan check FCS before forwardCannot check FCS
Memory requirementMust buffer full frameOnly needs header buffer
Use caseGeneral-purpose switchingLow-latency environments

Where LL is the full frame length, LhL_h is the header length (14 bytes for Ethernet), RR is the Link rate, and dpropd_{\mathrm{prop}} is the propagation delay.

For a path through nn switches:

\mathrm{Store}\mathrm{-and\mathrm}{-forward\;latency} = n \cdot \frac{L}{R} + d_{\mathrm{total}}

\mathrm{Cut}\mathrm{-through}\;latency} = \frac{L}{R} + (n-1) \cdot \frac{L_h}{R} + d_{\mathrm{total}

Worked Example: Switching Latency Comparison

A 1500-byte frame traverses 3 store-and-forward switches on 1 Gbps links. Each link has 5 μ\muS Propagation delay.

Store-and-forward:

Latency=3×1500×8109+3×5×106=36  μs+15  μs=51  μs\mathrm{Latency} = 3 \times \frac{1500 \times 8}{10^9} + 3 \times 5 \times 10^{-6} = 36\;\mu\mathrm{s} + 15\;\mu\mathrm{s} = 51\;\mu\mathrm{s}

Cut-through:

Latency=1500×8109+2×14×8109+3×5×106=12  μs+0.224  μs+15  μs=27.2  μs\mathrm{Latency} = \frac{1500 \times 8}{10^9} + 2 \times \frac{14 \times 8}{10^9} + 3 \times 5 \times 10^{-6} = 12\;\mu\mathrm{s} + 0.224\;\mu\mathrm{s} + 15\;\mu\mathrm{s} = 27.2\;\mu\mathrm{s}

Answer: Cut-through saves approximately 23.8 μ\muS (47% reduction) for this scenario, but it Cannot detect corrupted frames before forwarding them.