Module 11 · 16 min read · Free chapter
Data link layer: framing, error detection, MAC and Ethernet
The data link layer frames bits, detects errors and shares the medium. Bit stuffing adds a 0 after five 1s, and a CRC appends the remainder of modulo-2 division. Stop-and-wait efficiency is 1/(1 + 2a). Slotted ALOHA peaks at 0.368, and CSMA/CD needs a minimum frame of 2 × propagation delay × bandwidth.
Start from zero: sending parcels you can check
Think of a dabba delivery service that sends tiffins on a conveyor belt. If the boxes were not taped at both ends, the worker at the far end could not tell where one order ends and the next begins. So each order is wrapped, with tape marking its start and its end.
The kitchen also writes the total weight on each parcel. At the other end the worker weighs it again, and if the weight is wrong, something fell out or got added, so the parcel is sent back. Only one parcel can go on the narrow belt at a time, so the kitchens must also take turns.
The data link layer does these same three jobs for one link. It wraps bits into frames with clear boundaries, it adds a check so that damaged frames are caught, and it decides who may send when many machines share one medium. It also controls how fast the sender sends, so that the receiver is not flooded.
Why it matters. Wires and radio waves make mistakes, and many computers share the same cable or Wi-Fi channel. Without these jobs, the layers above would receive garbage and collisions all the time.
Framing: byte stuffing and bit stuffing
The physical layer delivers only a stream of bits, so the receiver needs a way to find frame boundaries. One old method puts a character count at the start of each frame. But if that one count is damaged, the receiver loses track of every frame after it, so flags are used instead.
In byte stuffing, a special FLAG byte marks both ends of the frame. If the data itself contains a byte equal to FLAG, the sender puts an escape byte ESC before it. If the data contains ESC, that is also preceded by another ESC, and the receiver simply removes every ESC that comes before a FLAG or ESC.
Bit stuffing does the same at the bit level. The flag is 01111110, which has six 1s in a row. The sender inserts a 0 after every run of five consecutive 1s in the data, so six 1s never appear inside the data. The receiver removes any 0 that follows five 1s.
Worked example 1. Stuff the data 0110111111111100 (it has ten 1s in a row).
- After the first five 1s the sender inserts a 0, and after the next five 1s it inserts another 0.
- The result is 011011111011111000, which is 18 bits instead of 16.
- The frame on the wire is 01111110, then 011011111011111000, then 01111110.
Why it matters. Framing questions in GATE and IT officer papers are quick marks if you count carefully. The rule is about five 1s, and the inserted 0 is added even when the next data bit is already 0.
Error detection: parity, checksum, CRC and Hamming distance
Every error-detection method adds some extra bits computed from the data. The receiver recomputes them, and a mismatch means the frame was damaged. Methods differ in how many extra bits they use and which errors they catch.
Parity. With even parity, one bit is added so that the total number of 1s is even. A single flipped bit makes the count odd and is caught, and so is any odd number of flips. But two flipped bits leave the count even, so they slip through.
Checksum. The Internet checksum adds the data as 16-bit words in one's complement arithmetic, where any carry out of the top bit is added back at the bottom. The sender transmits the complement of the sum. The receiver adds all words including the checksum, and the result must be all 1s.
Worked example 2 (8-bit words for brevity). The data words are 10110011 and 11001010.
- Their sum is 1 01111101, and wrapping the carry back gives 01111110.
- The checksum is the complement, 10000001.
- The receiver adds 10110011 + 11001010 + 10000001 and gets 11111111, so it accepts the data.
Cyclic redundancy check (CRC). Treat the bits as a polynomial and pick a generator of degree r, which has bits. Append r zeros to the data, divide by the generator using modulo-2 division, where subtraction is XOR and there are no carries, and send the data followed by the r-bit remainder. The receiver divides the whole frame and accepts it only if the remainder is zero.
Worked example 3. Data 100100, generator 1101 (), so .
100100000 data followed by three 0s
1101 XOR -> 010000000
1101 XOR -> 001010000
1101 XOR -> 000111000
1101 XOR -> 000001100
1101 XOR -> 000000001
remainder = 001, transmitted frame = 100100001
A CRC with r check bits catches every burst error of length r or less, which is why it is used in Ethernet. It is very strong in practice, and it is far better than parity or a checksum for burst errors.
Hamming distance. The Hamming distance between two codewords is the number of bit positions where they differ. If the smallest distance between any two codewords of a code is d, the code can detect up to errors and correct up to errors. For example, the code {00000, 01011, 10101, 11110} has minimum distance 3, so it detects 2 errors and corrects 1.
To correct single-bit errors in m data bits, a Hamming code needs r check bits such that . For 7 data bits, works because , while fails because . So the codeword has 11 bits.
Why it matters. The CRC division, the checksum and the "detect , correct " rule are among the most repeated GATE numericals. Do the XOR division slowly, and always append r zeros, not .
Flow control: stop-and-wait, go-back-N and selective repeat
When a frame is lost or damaged, the sender must send it again. Protocols that do this with acknowledgements (ACKs) and timers are called ARQ (automatic repeat request) protocols. They also give flow control, because the sender cannot run too far ahead of the receiver.
Stop-and-wait. The sender sends one frame and waits for its ACK before sending the next. Let the transmission time be and the one-way propagation delay be , and write . One cycle takes , of which only is useful, so the efficiency is .
Worked example 4. Frames are 1,000 bits on a 1 Mbps link with = 20 ms.
- = 1 ms, so and the stop-and-wait efficiency is , about 2.4%.
- The useful throughput is only about 24 kbps on a 1 Mbps link.
- To keep the link fully busy, the sender needs a window of frames.
Sliding window. The sender may have up to W unacknowledged frames in flight, and the efficiency becomes . Frames carry sequence numbers, and with k bits the numbers run from 0 to and then wrap around.
Go-back-N. The receiver accepts frames only in order, so its window is 1. If a frame is lost, the receiver discards all later frames, and the sender, on timeout, resends the lost frame and every frame after it. With k-bit sequence numbers the sender window can be at most .
Selective repeat. The receiver buffers out-of-order frames and the sender resends only the frame that was lost. To avoid confusing an old frame with a new one after wrap-around, both windows can be at most .
| Protocol | Sender window | Receiver window | On a loss |
|---|---|---|---|
| Stop-and-wait | 1 | 1 | Resend that frame |
| Go-back-N | at most | 1 | Resend it and all later frames |
| Selective repeat | at most | at most | Resend only the lost frame |
Worked example 5. In example 4 we needed .
- For go-back-N, gives , since .
- For selective repeat, gives , since .
- With only 3-bit numbers, go-back-N can use , so the efficiency is , about 17%.
Why it matters. These formulas tie straight back to the bandwidth-delay product: is just the pipe size counted in frames. Every window-size and sequence-bit question in GATE is one of these three steps.
Medium access: ALOHA, CSMA and CSMA/CD
When many stations share one channel, two frames sent at the same time collide and both are lost. Medium access control (MAC) protocols decide who sends and what to do after a collision. The simplest are the ALOHA protocols.
Pure ALOHA. A station sends whenever it has a frame, and if no ACK comes, it waits a random time and tries again. A frame is safe only if no other frame starts within one frame time before or after it, so the vulnerable period is two frame times. If G is the average number of frames offered per frame time, the throughput is , with a maximum of , about 18.4%, at .
Slotted ALOHA. Time is cut into slots of one frame time, and a frame may start only at a slot boundary. The vulnerable period falls to one slot, so , with a maximum of , about 36.8%, at .
Worked example 6. Frames of 200 bits share a 200 kbps channel, and all stations together offer 1,000 frames per second.
- The frame time is 1 ms, so .
- Pure ALOHA gives , so about 135 frames per second get through.
- Slotted ALOHA gives , so about 368 frames per second get through.
CSMA. In carrier sense multiple access, a station first listens and sends only if the channel is idle. In 1-persistent CSMA it sends as soon as the channel becomes idle, in non-persistent CSMA it waits a random time before listening again, and in p-persistent CSMA it sends with probability p in the next slot. Collisions still happen, because a signal takes time to reach other stations.
Collision detection. Wired Ethernet runs CSMA/CD, which adds collision detection: a station keeps listening while it sends and stops at once if it hears a collision. To be sure of hearing it, the station must still be sending when the collision news comes back, which in the worst case takes twice the propagation delay. So the transmission time must be at least , and the minimum frame size is .
Worked example 7. A 1 Gbps network has a 1 km cable with signal speed 2 × 10⁸ m/s.
- .
- bits, which is 1,250 bytes.
- Classic 10 Mbps Ethernet fixed its minimum frame at 64 bytes, which is 512 bits or 51.2 μs.
After a collision, Ethernet uses binary exponential backoff. After the n-th collision in a row, the station picks K at random from 0 to , where , and waits bit times. Wi-Fi cannot hear collisions while sending, so it uses CSMA/CA, which tries to avoid them with random waits and acknowledgements.
Why it matters. The ALOHA maxima 0.184 and 0.368 and the CSMA/CD minimum frame rule are asked again and again. A faster link or a longer cable both raise the minimum frame size, which is why Gigabit Ethernet needed special rules for shared cables.
Ethernet frames, learning switches and spanning tree
Ethernet is the most common wired LAN technology. Every network card has a 48-bit MAC address, written as six pairs of hex digits such as 3C:52:82:1A:0F:7E. The address FF:FF:FF:FF:FF:FF is the broadcast address, which every station on the LAN accepts.
| Field | Size (bytes) | Purpose |
|---|---|---|
| Preamble | 7 | Alternating 1s and 0s to synchronise clocks |
| Start frame delimiter | 1 | Marks the start of the frame |
| Destination MAC | 6 | Receiver's address |
| Source MAC | 6 | Sender's address |
| Type or length | 2 | Which protocol is inside, such as IPv4 |
| Data | 46 to 1,500 | Payload, padded up to 46 if shorter |
| CRC | 4 | Error check over the frame |
Counting from the destination address to the CRC, a frame is 64 to 1,518 bytes long. The 64-byte minimum is exactly the 512 bits that CSMA/CD needed on classic 10 Mbps Ethernet. That is why a short payload is padded up to 46 bytes.
Modern Ethernet connects every computer to a switch, which is a many-port bridge. A switch learns where machines are by watching traffic, so no one has to configure it.
How a learning switch works.
- When a frame arrives, the switch records its source MAC address and the port it came in on.
- If the destination address is already in the table and on another port, the switch forwards the frame only to that port.
- If the destination is on the same port the frame came from, the switch drops it, which is called filtering.
- If the destination is unknown or is the broadcast address, the switch floods the frame to every port except the one it came from.
Engineers often connect switches in a loop for backup. But a broadcast frame on a loop would circle for ever and multiply, which is called a broadcast storm. The spanning tree protocol prevents this by electing the bridge with the lowest ID as root, letting every other bridge keep its best path to the root, and blocking the remaining redundant ports. If a link fails, the bridges run the election again and unblock a backup port.
Why it matters. "Which port does the switch send this frame to?" is a favourite question, and the answer always follows the four steps above. Remember that switches learn only from source addresses, never from destination addresses.
Descriptive lens: how written papers ask this
Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about the data link layer in four ways:
- Explain the jobs of the data link layer and the two sublayers, logical link control and MAC.
- Work out a CRC, a checksum or a bit-stuffing example step by step.
- Compare stop-and-wait, go-back-N and selective repeat, with efficiency and window limits.
- Describe the working of CSMA/CD, Ethernet framing and how a learning switch builds its table.
Practice questions (our own, not previous-year questions).
- Find the CRC for the data 1101011011 using the generator , and show how the receiver checks the frame. (10 marks)
- Compare go-back-N and selective repeat ARQ. Why must the selective repeat window be at most half the sequence number space? (10 marks)
- Explain the working of CSMA/CD and derive the minimum frame size. Find it for a 100 Mbps network with a 2 km cable and signal speed 2 × 10⁸ m/s. (15 marks)
Answer plan for question 3.
- Begin by explaining that CSMA/CD stations listen before sending and keep listening while sending.
- Describe the worst case, where the collision happens near the far station and its news takes another propagation delay to return.
- Conclude that the transmission time must be at least , so the minimum frame size is .
- Calculate for the given network.
- Then find bits, which is 250 bytes.
- End by noting that a faster link or a longer cable needs a bigger minimum frame, and mention binary exponential backoff.
Quick revision
- Bit stuffing inserts a 0 after every five consecutive 1s, and the flag is 01111110.
- One parity bit catches any odd number of errors but no even number.
- The checksum uses one's complement addition with the carry wrapped around.
- For a CRC, append r zeros for a generator of degree r and send the r-bit remainder.
- Minimum distance d detects errors and corrects errors.
- Stop-and-wait efficiency is , and a window W gives .
- Go-back-N allows a window of , and selective repeat allows .
- Pure ALOHA peaks at 0.184 and slotted ALOHA at 0.368.
- For collision detection, the CSMA/CD minimum frame size is 2 × propagation delay × bandwidth.
- An Ethernet frame is 64 to 1,518 bytes with 46 to 1,500 bytes of data and a 4-byte CRC.
- A switch learns from source addresses, floods unknown destinations, and the spanning tree protocol removes loops.