Computer Science and IT‹›⌂ 0:00 studied

Module 11 · 16 min read · Free chapter

Data link layer: framing, error detection, MAC and Ethernet

In short

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.

What are you preparing for? We show the practice and previous year questions of your exam.

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 r+1r + 1 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 (x3+x2+1x^3 + x^2 + 1), so r=3r = 3.

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 d−1d - 1 errors and correct up to ⌊d−12⌋\lfloor \dfrac{d-1}{2} \rfloor 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 2r≥m+r+12^r \ge m + r + 1. For 7 data bits, r=4r = 4 works because 24=16≥122^4 = 16 \ge 12, while r=3r = 3 fails because 8<118 < 11. So the codeword has 11 bits.

Why it matters. The CRC division, the checksum and the "detect d−1d - 1, correct ⌊d−12⌋\lfloor \dfrac{d-1}{2} \rfloor" rule are among the most repeated GATE numericals. Do the XOR division slowly, and always append r zeros, not r+1r + 1.

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 TtT_t and the one-way propagation delay be TpT_p, and write a=TpTta = \dfrac{T_p}{T_t}. One cycle takes Tt+2TpT_t + 2T_p, of which only TtT_t is useful, so the efficiency is 11+2a\dfrac{1}{1 + 2a}.

Worked example 4. Frames are 1,000 bits on a 1 Mbps link with TpT_p = 20 ms.

  • TtT_t = 1 ms, so a=20a = 20 and the stop-and-wait efficiency is 141\dfrac{1}{41}, 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 W=1+2a=41W = 1 + 2a = 41 frames.

Sliding window. The sender may have up to W unacknowledged frames in flight, and the efficiency becomes min⁡(1,W1+2a)\min\left(1, \dfrac{W}{1 + 2a}\right). Frames carry sequence numbers, and with k bits the numbers run from 0 to 2k−12^k - 1 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 2k−12^k - 1.

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 2k−12^{k-1}.

Protocol Sender window Receiver window On a loss
Stop-and-wait 1 1 Resend that frame
Go-back-N at most 2k−12^k - 1 1 Resend it and all later frames
Selective repeat at most 2k−12^{k-1} at most 2k−12^{k-1} Resend only the lost frame

Worked example 5. In example 4 we needed W=41W = 41.

  • For go-back-N, 2k−1≥412^k - 1 \ge 41 gives k=6k = 6, since 63≥4163 \ge 41.
  • For selective repeat, 2k−1≥412^{k-1} \ge 41 gives k=7k = 7, since 64≥4164 \ge 41.
  • With only 3-bit numbers, go-back-N can use W=7W = 7, so the efficiency is 741\dfrac{7}{41}, about 17%.

Why it matters. These formulas tie straight back to the bandwidth-delay product: 1+2a1 + 2a 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 S=Ge−2GS = G e^{-2G}, with a maximum of 12e\dfrac{1}{2e}, about 18.4%, at G=0.5G = 0.5.

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 S=Ge−GS = G e^{-G}, with a maximum of 1e\dfrac{1}{e}, about 36.8%, at G=1G = 1.

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 G=1000×0.001=1G = 1000 \times 0.001 = 1.
  • Pure ALOHA gives S=1×e−2≈0.135S = 1 \times e^{-2} \approx 0.135, so about 135 frames per second get through.
  • Slotted ALOHA gives S=1×e−1≈0.368S = 1 \times e^{-1} \approx 0.368, 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 2Tp2T_p, and the minimum frame size is L=2×Tp×RL = 2 \times T_p \times R.

Worked example 7. A 1 Gbps network has a 1 km cable with signal speed 2 × 10⁸ m/s.

  • Tp=10002×108 s=5 μsT_p = \dfrac{1000}{2 \times 10^8}\ \text{s} = 5\ \mu\text{s}.
  • L=2×5×10−6×109=10,000L = 2 \times 5 \times 10^{-6} \times 10^9 = 10{,}000 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 2m−12^m - 1, where m=min⁡(n,10)m = \min(n, 10), and waits K×512K \times 512 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:

  1. Explain the jobs of the data link layer and the two sublayers, logical link control and MAC.
  2. Work out a CRC, a checksum or a bit-stuffing example step by step.
  3. Compare stop-and-wait, go-back-N and selective repeat, with efficiency and window limits.
  4. Describe the working of CSMA/CD, Ethernet framing and how a learning switch builds its table.

Practice questions (our own, not previous-year questions).

  1. Find the CRC for the data 1101011011 using the generator x4+x+1x^4 + x + 1, and show how the receiver checks the frame. (10 marks)
  2. Compare go-back-N and selective repeat ARQ. Why must the selective repeat window be at most half the sequence number space? (10 marks)
  3. 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 2×Tp2 \times T_p, so the minimum frame size is 2×Tp×R2 \times T_p \times R.
  • Calculate Tp=20002×108 s=10 μsT_p = \dfrac{2000}{2 \times 10^8}\ \text{s} = 10\ \mu\text{s} for the given network.
  • Then find L=2×10×10−6×108=2000L = 2 \times 10 \times 10^{-6} \times 10^8 = 2000 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 d−1d - 1 errors and corrects ⌊d−12⌋\lfloor \dfrac{d-1}{2} \rfloor errors.
  • Stop-and-wait efficiency is 11+2a\dfrac{1}{1 + 2a}, and a window W gives min⁡(1,W1+2a)\min\left(1, \dfrac{W}{1 + 2a}\right).
  • Go-back-N allows a window of 2k−12^k - 1, and selective repeat allows 2k−12^{k-1}.
  • 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.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. In byte stuffing, what is inserted before a FLAG byte that appears inside the data?

  2. Using the flag 01111110, the data 0110111111111100 after bit stuffing becomes:

  3. With 8-bit one's complement words 10110011 and 11001010, the checksum is:

  4. Data 100100 is sent with CRC generator 1101. The transmitted frame is:

  5. A CRC with a generator of degree 16 is guaranteed to detect all burst errors of length:

  6. A code has minimum Hamming distance 5. It can correct up to how many bit errors?

  7. How many check bits does a single-error-correcting Hamming code need for 7 data bits?

  8. Frames of 1,000 bits are sent on a 1 Mbps link with 20 ms one-way propagation delay. Stop-and-wait efficiency is about:

  9. With 3-bit sequence numbers, the maximum sender window for go-back-N and selective repeat is:

  10. A link needs a window of 41 frames for full use. The minimum sequence-number bits for selective repeat is:

  11. The maximum throughput of slotted ALOHA is about:

  12. A CSMA/CD network runs at 1 Gbps on a 1 km cable with signal speed 2 × 10⁸ m/s. The minimum frame size is:

  13. The size of an Ethernet MAC address is:

  14. A learning switch receives a frame whose destination MAC address is not in its table. It:

  15. Which topic does the GATE 2026 CS syllabus list under the data link layer?

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

How is a CRC calculated?

Append r zeros for a generator of degree r, divide using XOR, and attach the r-bit remainder to the data.

What window sizes do go-back-N and selective repeat allow?

With k-bit sequence numbers, go-back-N allows 2ᵏ − 1 and selective repeat allows 2ᵏ⁻¹.

What is the maximum throughput of ALOHA?

Pure ALOHA reaches 1/(2e), about 0.184, and slotted ALOHA reaches 1/e, about 0.368.

Why does Ethernet have a minimum frame size?

A station must still be sending when a collision's news returns, so transmission time must be at least twice the propagation delay.

More in this module: Computer networks

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Computer Networks and Internet Protocol (IIT Kharagpur, 106105183): data link layer, MAC and Ethernet