Data Link Layer Fundamentals
The data link layer is the second layer of the OSI reference model, sitting above the physical layer and below the network layer. Its core task is to provide reliable and efficient data transfer between adjacent nodes — the two nodes connected directly by a physical link.
The physical layer only transmits a raw bit stream and is not concerned with the meaning or structure of those bits. The data link layer organizes the bit stream into frames, and uses mechanisms such as error control and flow control to turn a physical link that may corrupt data into a data link that appears error-free to the network layer.
Definition
A definition is a precise description of what a concept, a term, or an object means. Understanding definitions is the foundation of learning any field, and every concept has its own exact meaning.
A data link is formed when a physical link is combined with the hardware and software that implement a data transmission protocol. A data link is a logical concept, while a physical link is a physical one.
1. Framing
The IP datagrams (packets) handed down by the network layer are encapsulated into frames following a defined format. Control information is added to the head and tail of the frame so that the receiver can identify the start and end of each frame within the received bit stream.
The central problem framing must solve is: how does the receiver distinguish the boundaries of one frame inside a continuous bit stream?
2. Error Control
Noise and interference on a physical link can flip bits during transmission (a 0 becoming a 1, or a 1 becoming a 0). Error control covers two things:
- Error detection: finding out whether transmission produced errors
- Error correction: finding errors and repairing them automatically
Common error-detection codes include parity check and cyclic redundancy check (CRC); the common error-correction code is Hamming code.
3. Flow Control
The sender’s transmission rate is limited so that it does not exceed the receiver’s ability to receive and process, which prevents the receiver’s buffer from overflowing and losing data. The common mechanism is the sliding window protocol.
4. Reliable Transmission
Acknowledgment (ACK) and retransmission mechanisms ensure that data reaches the receiver correctly and in order. When a frame is corrupted or lost, the sender transmits it again.
Not every data link layer protocol provides reliable transmission. Ethernet, for example, does not offer a reliable transmission service and leaves that responsibility to the layer above (TCP). Protocols such as PPP and HDLC do provide reliable transmission.
5. Medium Access Control (MAC)
When several nodes share the same broadcast channel, as in Ethernet, rules are needed to govern how nodes access the channel and avoid collisions. This is the responsibility of the medium access control sublayer (the MAC sublayer).
Framing works by adding special markers at the start and end of a frame so that the receiver can identify the frame boundaries. Four framing methods are commonly used.
1. Character Count
A field in the frame header states the number of bytes in the frame. By reading this count field, the receiver learns the length of the frame and therefore where the frame ends.
Advantages: simple, no padding required Disadvantages: if the count field itself is corrupted in transit, the receiver cannot identify the frame boundary correctly, and every subsequent frame becomes misaligned (frame synchronization is lost)
This method was used in DECNET’s DDCMP protocol.
2. Character Stuffing
Special control characters serve as the start and end markers of a frame. For example:
FLAG(a flag byte such as0x7E) delimits the frame- when the data contains a byte identical to
FLAG, an escape characterESC(such as0x7D) is inserted before it - if the data contains
ESC, anotherESCis inserted before it
After receiving an ESC, the receiver knows the next byte is data rather than a control character, and deletes the ESC.
Advantages: recovery from errors is possible (finding the FLAG again restores synchronization) Disadvantages: depends on a specific character set and is therefore coupled to the character encoding; stuffing and destuffing add processing overhead
This method was used in the early BISYNC protocol and in PPP (asynchronous mode).
3. Bit Stuffing
A special bit pattern (such as 01111110, that is, six consecutive 1 bits) serves as the frame delimiter.
Sender: whenever five consecutive 1 bits appear in the data, a 0 bit is inserted after them. The data therefore never contains six consecutive 1 bits.
Receiver: whenever five consecutive 1 bits arrive, the next bit is checked:
- if it is 0, the 0 is removed (restoring the data)
- if it is 1, the pattern is the frame delimiter (
01111110)
Bit stuffing is the most widely used framing method today; both HDLC and Ethernet use it. It does not depend on a character set and recovers synchronization quickly after an error.
4. Physical Layer Violations
Encoding patterns that are unused in the physical layer encoding are used as frame delimiters. In Manchester encoding, for example, every bit has a transition in its middle (high to low means 1, low to high means 0), so the absence of a transition is a violation and can mark the start and end of a frame.
Advantages: no padding, and the data is unaffected Disadvantages: depends on a specific physical layer encoding, so it is not general-purpose
This method was used in LANs such as 802.5 Token Ring.
Parity Check
Parity check is the simplest error-detection code. A parity bit is appended after the data bits so that the number of 1 bits in the whole codeword (data bits plus parity bit) is odd (odd parity) or even (even parity).
Odd parity: the parity bit makes the total number of 1 bits odd Even parity: the parity bit makes the total number of 1 bits even
For example, the data 1011001 contains four 1 bits:
- odd parity: the parity bit is 1 (4+1=5, odd), and the codeword is
10110011 - even parity: the parity bit is 0 (4+0=4, even), and the codeword is
10110010
The receiver counts the 1 bits in the received codeword; if the agreed parity is not satisfied, transmission went wrong.
Detection capability: it detects an odd number of bit errors but not an even number, so its detection capability is weak and it is generally used where requirements are modest (such as ASCII transmission).
Cyclic Redundancy Check (CRC)
CRC is the most widely used error-detection code today: it has strong detection capability and high computational efficiency, and almost every modern network protocol (Ethernet, PPP, USB, and so on) uses it.
The Basic Idea of CRC
Treat the data to be sent as the coefficients of a polynomial . Choose a generator polynomial (agreed in advance by both ends), perform modulo-2 division of by , and the remainder is the CRC code. The remainder is appended to the data and sent.
The receiver performs modulo-2 division of the received data (including the code) by the same . A remainder of 0 means no error; a non-zero remainder means an error occurred.
The Steps of CRC Computation
Let the data be bits and let the generator polynomial have degree (that is, has bits):
- Left shift: shift the data left by bits (append zero bits to the end of the data) to obtain
- Modulo-2 division: divide by using modulo-2 arithmetic (exclusive OR) to obtain the -bit remainder
- Append: append the remainder to the data , giving the transmitted codeword
The key point of modulo-2 division: subtraction is exclusive OR (XOR) with no borrowing. For example, .
A CRC Computation Example
Problem: let the data be (6 bits) and the generator polynomial be , that is, (4 bits, ). Find the CRC code.
Solution:
- Shift the data left by 3 bits:
- Perform modulo-2 division by :
110101 (quotient, not needed)
___________
1101|101001000
1101
----
1110
1101
----
0111
0000
----
1110
1101
----
0110
0000
----
1100
1101
----
001 (remainder R = 001)
- The remainder is , so the CRC code is
001 - The transmitted codeword is
After the receiver obtains 101001001, division by 1101 leaves a remainder of 0, so the frame arrived without error.
Common Generator Polynomials
| Name | Generator Polynomial | Application |
|---|---|---|
| CRC-8 | ATM | |
| CRC-16 | IBM SDLC | |
| CRC-CCITT | X.25, V.41 | |
| CRC-32 | Ethernet, ZIP |
Detection capability of CRC: it detects all single-bit errors, all double-bit errors, all odd numbers of bit errors, and all burst errors of length at most . For burst errors longer than , the undetected error rate is . The undetected error rate of CRC-32 is extremely low (about ), which is why it is used in protocols such as Ethernet.
Hamming Code
Hamming code is an error-correction code proposed by Richard Hamming in 1950. It not only detects errors but can also correct single-bit errors.
Basic Principle
Several parity bits are inserted among the data bits, and each parity bit performs a parity check over a specific set of bits. When a single-bit error occurs, the results of several parity groups point to the position of the error, which allows the error to be located and corrected.
Encoding Rules for Hamming Code
Let the data consist of bits and the parity bits consist of bits. The following must hold:
The parity bits are placed at the positions that are powers of two (that is, positions 1, 2, 4, 8, …), and the data bits occupy the remaining positions.
Each parity bit (located at position ) checks all positions whose binary index has a 1 in the -th bit.
A Hamming Code Example
Problem: the data is 1011 (4 bits). Find the Hamming code.
Solution:
-
Determine the number of parity bits: and we need . For , , so the condition holds. Hence parity bits, and the total length is .
-
Position layout (parity bits at positions 1, 2, and 4):
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Content | P1 | P2 | D1 | P3 | D2 | D3 | D4 |
| Data | ? | ? | 1 | ? | 0 | 1 | 1 |
-
Compute the parity bits (even parity):
- (position 1) covers positions 1, 3, 5, 7: , that is, , so
- (position 2) covers positions 2, 3, 6, 7: , that is, , so
- (position 4) covers positions 4, 5, 6, 7: , that is, , so
-
The Hamming code is:
0 1 1 0 0 1 1(positions 1 through 7)
Error Correction with Hamming Code
After the receiver obtains the codeword, it checks each parity group and combines the results into a binary number (the correction factor). A correction factor of 0 means there is no error. A non-zero correction factor is the position number of the erroneous bit, and inverting that bit corrects the error.
For example, suppose 0110011 is received and bit 5 is corrupted, giving 0110111:
- Parity group 1 (positions 1, 3, 5, 7): (error)
- Parity group 2 (positions 2, 3, 6, 7): (correct)
- Parity group 3 (positions 4, 5, 6, 7): (error)
- Correction factor = , so bit 5 is in error and is corrected by inversion.
Example 1: CRC Computation
The data to be sent is 1101011011 and the generator polynomial is (that is, $G = 10011`). Find the CRC code and the transmitted codeword.
Approach: shift the data left by the degree of the generator polynomial, divide modulo 2, and append the remainder.
Steps:
- The data is (10 bits) and (5 bits, )
- Shift the data left by 4 bits:
- Perform modulo-2 division by :
11010110110000 ÷ 10011
10011
-----
10011
10011
-----
00001
00000
-----
00011
00000
-----
00110
00000
-----
01100
00000
-----
11000
10011
-----
10110
10011
-----
01010
00000
-----
1010 (remainder R = 1010, 4 bits)- The CRC code is
1010 - The transmitted codeword is
11010110111010
Answer: the CRC code is 1010 and the transmitted codeword is 11010110111010.
Example 2: Hamming Code Encoding
The data is 1001 (4 bits). Using even parity, find the Hamming code.
Approach: place the parity bits at the powers of two, then solve each parity group for even parity.
Steps:
- , (), so the total length is
- Position layout:
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Content | P1 | P2 | D1 | P3 | D2 | D3 | D4 |
| Data | ? | ? | 1 | ? | 0 | 0 | 1 |
-
Compute the parity bits (even parity):
- (positions 1, 3, 5, 7):
- (positions 2, 3, 6, 7):
- (positions 4, 5, 6, 7):
-
The Hamming code is:
0 0 1 1 0 0 1
Answer: the Hamming code is 0011001.
Example 3: Analyzing a Framing Method
A data link layer protocol uses bit stuffing with the flag pattern 01111110. If the sender’s data is 011111101111101111110, write the actual bit sequence transmitted after bit stuffing.
Approach: walk through the data and insert a 0 after every run of five consecutive 1 bits, then wrap the result in the flag pattern.
Steps:
- Original data:
011111101111101111110 - Bit stuffing rule: whenever five consecutive 1 bits appear, insert a 0 after them
- Process the data segment by segment:
01111→ unchanged- next
110: the preceding01111(four 1 bits) plus this1gives five consecutive 1 bits → insert a 0 after the fifth 1 →0111110 - next:
1011111→ five consecutive 1 bits appear again → insert a 0 →10111110 - next:
01111110→ five consecutive 1 bits → insert a 0 →011111010
- Stuffed data:
011111010111110011111010 - Add the leading and trailing flag
01111110: Actually transmitted:01111110 011111010111110011111010 01111110
Answer: the transmitted bit sequence is 0111111001111101011111001111101001111110 (flags at both ends, stuffed data in between).
Exercise 1
Briefly describe the main functions of the data link layer, and explain why framing is needed.
Approach: list the five functions, then explain what the physical layer fails to provide.
Steps:
The main functions of the data link layer are:
- Framing: encapsulate the packets handed down by the network layer into frames, adding a header and trailer so that the receiver can identify frame boundaries within the bit stream.
- Error control: detect and correct transmission errors using error-detection codes (parity check, CRC) and error-correction codes (Hamming code).
- Flow control: control the sending rate so that the receiver’s buffer cannot overflow.
- Reliable transmission: use acknowledgment and retransmission to ensure that data arrives reliably.
- Medium access control: resolve access conflicts on a shared channel.
Why framing is needed: the physical layer transmits a raw bit stream with no structure and no boundaries. Without framing, the receiver cannot know where a piece of data begins or ends, and cannot run error detection over it. Framing organizes the bit stream into independent units (frames), which is the foundation of error control and reliable transmission.
Answer: the data link layer provides framing, error control, flow control, reliable transmission, and medium access control. Framing is needed because the physical layer provides only a raw bit stream, and framing is what makes error detection and reliable transmission possible.
Exercise 2
The data is 1101 and the generator polynomial is (that is, ). Find the CRC code.
Approach: shift the data left by 3 bits, then divide modulo 2 by 1011.
Steps:
- The data is (4 bits) and (4 bits, )
- Shift left by 3 bits:
1101000 - Modulo-2 division:
1101000 ÷ 1011
1011
----
1100
1011
----
1110
1011
----
101 (remainder)- The remainder is
Answer: the CRC code is 101.
| Term | Abbreviation | Explanation |
|---|---|---|
| Data Link Layer | DLL | Second layer of the OSI model |
| Frame | - | The protocol data unit of the data link layer |
| Framing | - | Organizing a bit stream into frames |
| Error Control | - | Detecting and correcting transmission errors |
| Flow Control | - | Controlling the sending rate |
| Cyclic Redundancy Check | CRC | A widely used error-detection code |
| Hamming Code | - | An error-correction code that fixes single-bit errors |
| Parity Check | - | The simplest error-detection code |
| Bit Stuffing | - | A framing method used by protocols such as HDLC |
| Medium Access Control | MAC | Access control on a shared channel |
| Generator Polynomial | - | The polynomial used in CRC computation |
