The physical layer moves raw bits. The data link layer turns them into frames and makes delivery across one link dependable.
Functions of the Data Link Layer
The data link layer provides node-to-node delivery over a single link. Its functions are: framing, dividing the bit stream into frames; physical (MAC) addressing, adding the source and destination addresses of the link; error control, detecting (and sometimes correcting) errors and retransmitting damaged frames; flow control, preventing a fast sender from overwhelming a slow receiver; and access control, deciding which device may use a shared medium. In IEEE 802 standards, it is divided into two sublayers: Logical Link Control (LLC), which handles flow and error control and identifies the network-layer protocol, and Media Access Control (MAC), which handles addressing and access to the medium, using methods such as CSMA/CD in classic Ethernet and CSMA/CA in Wi-Fi.
Definition: Framing. The division of a stream of bits into frames, units with a header, data and trailer, so that the receiver can identify where each frame starts and ends.
Framing
A frame has a header (addresses and control information), the payload (data from the network layer) and a trailer (usually an error-checking code). The receiver must know where each frame begins and ends. Character count: the header states the number of characters in the frame; a single error in the count spoils every later frame. Byte (character) stuffing: each frame starts and ends with a special flag byte; if the flag appears in the data, an escape byte (ESC) is inserted before it, and the receiver removes it. Bit stuffing: the frame is marked by the flag 01111110; whenever the sender sees five consecutive 1s in the data, it inserts a 0, so the flag pattern never appears in the data; the receiver removes the stuffed 0s. HDLC uses bit stuffing. Physical-layer coding violations: special signal patterns that never occur in data mark the frame boundaries.
Services to the Network Layer
The data link layer may offer an unacknowledged connectionless service (as in Ethernet, where errors are rare and recovery is left to higher layers), an acknowledged connectionless service (as in Wi-Fi, where each frame is acknowledged because the wireless link is unreliable), or an acknowledged connection-oriented service.
Table 2.1: Framing methods
| Method | How the frame is marked | Weakness |
|---|---|---|
| Character count | Length field in header | One error spoils later frames |
| Byte stuffing | Flag bytes, escape inside data | Tied to 8-bit characters |
| Bit stuffing | Flag 01111110, 0 after five 1s | Extra bits added |
| Coding violation | Invalid signal patterns | Needs special encoding |
Example 2.1: Bit stuffing
Problem: Apply bit stuffing to the data 0110111111111100 and show the frame with flags.
Solution:
- Scan the data. After the first five consecutive 1s, insert a 0.
- Continue counting from zero: after the next five 1s, insert another 0.
- Stuffed data: 011011111011111000.
- Add the flags: 01111110 011011111011111000 01111110.
Result: Two 0 bits are stuffed, so the flag never appears inside the data.
def stuff(bits):
out, ones = [], 0
for b in bits:
out.append(b)
ones = ones + 1 if b == "1" else 0
if ones == 5:
out.append("0")
ones = 0
return "".join(out)
data = "0110111111111100"
s = stuff(data)
print("data :", data)
print("stuffed:", s)
print("frame :", "01111110", s, "01111110")
Output
data : 0110111111111100
stuffed: 011011111011111000
frame : 01111110 011011111011111000 01111110
Common mistakes
- Thinking the data link layer delivers end to end. It works hop by hop over one link.
- Forgetting to reset the count of 1s after stuffing a 0.
- Confusing LLC (flow and error control) with MAC (addressing and access).
Key takeaways
- Functions: framing, MAC addressing, error control, flow control and access control.
- Sublayers: LLC and MAC.
- Framing methods: character count, byte stuffing, bit stuffing and coding violations.
- Bit stuffing inserts a 0 after five consecutive 1s.
Practice questions
- Name the two sublayers of the data link layer.
- What flag does bit stuffing use?
- Why is byte stuffing needed?
Answers to the practice questions
- Logical Link Control (LLC) and Media Access Control (MAC).
-
- So that a flag byte appearing in the data is not mistaken for the end of the frame.
Sources
- Official BCA syllabus Computer Networks, course code 26BCA404DS02, Unit II
