Module 2 -- Data Link Layer and Medium Access Sub Layer -- Computer Networks


Index

  1. Data Link Layer (Layer 2 of the OSI Model)
  2. Error Detection and Correction (with some error detection techniques)
  3. Flow Control
  4. 1. Stop-And-Wait ARQ
  5. 2. Go Back -N ARQ
  6. 3. Selective Repeat ARQ
  7. 4. Piggybacking
  8. Random Access And Multiple Access Protocols
  9. The ALOHA Protocol
  10. 1. Pure ALOHA
  11. 2. Slotted ALOHA
  12. 3. CSMA (Carrier Sense Multiple Access Protocol)
  13. CSMA/CD (Collision Detection)
  14. CSMA/CA (Collision Avoidance)

Data Link Layer (Layer 2 of the OSI Model)

https://www.youtube.com/watch?v=JRgmPco0KWI&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=22

The Data Link Layer (DLL) is the second layer in the OSI model and plays a vital role in ensuring reliable communication over a single link or hop in a network. It serves as an intermediary between the Physical Layer (which transmits raw bits) and the Network Layer (which handles end-to-end delivery across multiple networks). Here’s an in-depth look at its features and functionalities:


a. Hop-to-Hop (Node-to-Node) Delivery

b. Flow Control

c. Error Control

d. Access Control (Media Access Control)

e. Physical Addressing

f. Framing


2. Interplay with Other OSI Layers


3. Summary

The Data Link Layer is essential for:


Error Detection and Correction (with some error detection techniques)

Error Detection vs. Error Correction

Hamming Distance

What Is Hamming Distance?

Examples

Minimum Hamming Distance and Its Importance

Why Hamming Distance Matters


2. Error Detection Techniques

A. Single-Bit Parity Check

https://www.youtube.com/watch?v=U09cNsiYpc8&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=29

What Is Parity?

Types of Parity Bits

A parity bit is a bit appended to a data of binary bits to ensure that the total number of 1’s in the data is even or odd. Parity bits are used for error detection. There are two types of parity bits:

The m+1 Rule

How It Works (Using Even Parity as an Example)

Error Detection Capability

Cost and Simplicity

Some more types of errors (relevant in CRC):

Multiple Bit Error

A multiple-bit error is an error type that arises when more than one bit in a data transmission is affected. Although multiple-bit errors are relatively rare when compared to single-bit errors, they can still occur, particularly in high-noise or high-interference digital environments.

Pasted image 20250322140015.png

Burst Error

When several consecutive bits are flipped mistakenly in digital transmission, it creates a burst error. This error causes a sequence of consecutive incorrect values.

Pasted image 20250322140041.png


B. Cyclic Redundancy Check (CRC) and Modulo-2 Division

https://www.youtube.com/watch?v=5Q-Yv6_0Qcw&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=30

https://www.geeksforgeeks.org/modulo-2-binary-division/

CRC or Cyclic Redundancy Check is a method of detecting accidental changes/errors in the communication channel. 
CRC uses Generator Polynomial which is available on both sender and receiver side. An example generator polynomial is of the form like x3 + x + 1 . This generator polynomial represents key 1011. Another example is x2+1 that represents key 101.

n : Number of bits in data to be sent 
    from sender side.  
k : Number of bits in the key obtained 
    from generator polynomial.

1. Why CRC?


2. CRC Process Overview

a. Representing Data and Divisor

b. Determining the Number of Redundant Bits (r)

What is Redundant Bits?

Redundant bits are extra binary bits that are generated and added to the information-carrying bits of data transfer to ensure that no bits were lost during the data transfer. The number of redundant bits can be calculated using the following formula:

2rm+r+1

where m is the number of bits in input data, and r is the number of redundant bits.

Suppose the number of data bits is 7, then the number of redundant bits can be calculated using: = 247+4+1 . Thus, the number of redundant bits is 4.

c. Binary Division Using XOR

d. Creating the Codeword


3. Error Detection at the Receiver


4. Efficiency and Practical Considerations


How CRC is used

Sender Side (Generation of Encoded Data from Data and Generator Polynomial (or Key)):

  1. The binary data is first augmented by adding k-1 zeros in the end of the data
  2. Use modulo-2 binary division to divide binary data by the key and store remainder of division.
  3. Append the remainder at the end of the data to form the encoded data and send the same

Receiver Side (Check if there are errors introduced in transmission)
Perform modulo-2 division again and if the remainder is 0, then there are no errors.

In this article we will focus only on finding the remainder i.e. check word and the code word.

Modulo 2 Division:
The process of modulo-2 binary division is the same as the familiar division process we use for decimal numbers. Just that instead of subtraction, we use XOR here.

Worked out example

1. No error

Data word to be sent - 100100
Key - 1101 Or generator polynomial x3+x2+1

Sender Side:

Pasted image 20250322183016.png

Note what we added k1 zeroes at the end of the original data word to be sent. Here k = 4

So we have added 3 zeroes at the end of the original data word.

Therefore, the remainder is 001 and hence the encoded
data sent is 100100001.

Receiver Side:
Code word received at the receiver side 100100001.

Pasted image 20250322183453.png

Therefore, the remainder is all zeros. Hence, the
data received has no error.


2. Error in transmission

Data word to be sent - 100100
Key - 1101

Sender Side:

Pasted image 20250322183728.png

Therefore, the remainder is 001 and hence the
code word sent is 100100001.

Receiver Side
Let there be an error in transmission media
Code word received at the receiver side - 100000001

Pasted image 20250322183811.png

Since the remainder is not all zeroes, the error
is detected at the receiver side.


3. Error Correction Techniques

C. Hamming Code

https://www.youtube.com/watch?v=WdmGSWrcMvM


Algorithm of Hamming Code

https://www.youtube.com/watch?v=1A_NcXxdoCc (Error detection)
https://www.youtube.com/watch?v=wbH2VxzmoZk (Error correction)

Hamming Code is simply the use of extra parity bits to allow the identification of an error.

Step 1: Write the bit positions starting from 1 in binary form (1, 10, 11, 100, etc.).

Step 2: All the bit positions that are a power of 2 are marked as parity bits (1, 2, 4, 8, etc.).

Step 3: All the other bit positions are marked as data bits.

Step 4: Each data bit is included in a unique set of parity bits, as determined its bit position in binary form:

Step 5: Since we check for even parity set a parity bit to 1 if the total number of ones in the positions it checks is odd. Set a parity bit to 0 if the total number of ones in the positions it checks is even.


Summary


D. Block Coding

Block coding is a technique used in digital communications and error control where data is divided into fixed-size blocks (or words), and each block is encoded by adding extra bits (redundancy) to form a codeword. These extra bits enable the detection and, in many cases, the correction of errors that might occur during transmission. Here’s a detailed explanation:


1. Basic Concept


2. Types of Block Codes


3. Encoding and Decoding


4. Error Detection and Correction


5. Advantages and Trade-offs


6. Real-World Applications


Flow Control

Flow Control Overview

Some Flow control protocols :

1. Stop-And-Wait ARQ

https://www.geeksforgeeks.org/stop-and-wait-arq/

The Stop-and-Wait ARQ (Automatic Repeat Request) protocol is one of the simplest error control protocols used in data link layer communications. Its primary goal is to ensure that each frame is received correctly before the next one is transmitted. Here’s an in-depth look at how it works:


1. Operational Overview

Step-by-Step Process


2. Key Characteristics

Simplicity

Reliability

Flow Control


3. Throughput Considerations

Efficiency Formula

Throughput efficiency (the fraction of time used for transmitting useful data) is approximated by:

Efficiency=TfTf+2Tp​​

Implication of High Latency


4. Advantages and Disadvantages

Advantages

Disadvantages


Characteristics of Stop and Wait ARQ


Useful Terms in Stop and Wait Protocol

Propagation Delay = (Distance between routers) / (Velocity of propagation)


Simple Stop and Wait

At Sender

At Receiver

Pasted image 20250322221708.png

(After consumption) indicates that the packet sent to the receiver has been received and used successfully so the receiver sends (Ack) or acknowledgement to the sender, so that the sender can send the next packet.


5. Practical Example

Imagine a scenario where:

The efficiency would be calculated as:

Efficiency=55+2×20=54511.1%

This means only about 11.1% of the total communication time is used for actual data transmission, with the rest spent waiting for acknowledgments.


Useful Terms in Stop and Wait Protocol

Propagation Delay = (Distance between routers) / (Velocity of propagation)


Problems Associated with Stop and Wait

1. Lost Data

Assume the sender transmits the data packet and it is lost. The receiver has been waiting for the data for a long time. Because the data is not received by the receiver, it does not transmit an acknowledgment. The sender does not receive an acknowledgment, it will not send the next packet. This problem is caused by a loss of data.

Pasted image 20250322222041.png

2. Lost Acknowledgement

Assume the sender sends the data, which is also received by the receiver. The receiver sends an acknowledgment after receiving the packet. In this situation, the acknowledgement is lost in the network. The sender does not send the next data packet because it does not receive acknowledgement, under the stop and wait protocol, the next packet cannot be transmitted until the preceding packet’s acknowledgment is received.

Pasted image 20250322222135.png

3. Delayed Acknowledgement/Data

Assume the sender sends the data, which is also received by the receiver. The receiver then transmits the acknowledgment, which is received after the sender’s timeout period. After a timeout on the sender side, a long-delayed acknowledgement might be wrongly considered as acknowledgement of some other recent packet.


Stop and Wait for ARQ (Automatic Repeat Request) (Solutions to the above problems)

The above 3 problems are resolved by Stop and Wait for ARQ (Automatic Repeat Request) that does both error control and flow control.

Pasted image 20250322222214.png

1. Time Out

Timeout refers to the duration for which the sender waits for an acknowledgment (ACK) from the receiver after transmitting a data packet. If the sender does not receive an ACK within this timeout period, it assumes that the frame was lost or corrupted and retransmits the frame.

Pasted image 20250322222235.png


2. Sequence Number (Data)

In Stop-and-Wait ARQ, the sender assigns sequence numbers to each data frame it sends. This allows the receiver to identify and acknowledge each frame individually, ensuring reliable delivery of data packets. After sending a frame, the sender waits for an acknowledgment before sending the next frame.

Pasted image 20250322222353.png


3. Sequence Number(Acknowledgement)

Similarly, sequence numbers are also used in acknowledgments (ACKs) sent by the receiver to acknowledge received data frames. When the receiver successfully receives a data frame, it sends an ACK back to the sender, indicating the sequence number of the next expected frame. The sender uses this ACK to determine whether the transmission was successful and whether it can proceed to send the next frame.


2. Go Back -N ARQ

https://www.youtube.com/watch?v=YIX1NfaUpsU&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=24

https://www.geeksforgeeks.org/sliding-window-protocol-set-2-receiver-side/

Go-Back-N ARQ is a sliding window protocol used for error control in data link layer communications. It allows the sender to transmit multiple frames before needing an acknowledgment (ACK) for the first frame, thereby improving throughput compared to the Stop-and-Wait protocol.

Pasted image 20250323122903.png

In this picture we see that in a single N window 4 packets are sent, namely, 0, 1, 2, 3.

We see that packet 2 is lost in the network so it doesn't reach the receiver.

When packet 3 reaches the receiver it's discarded since the receiver still expects packet 2.

Packets 0 and 1 are successfully received by the receiver which then sends Ack packets as packets 4 and 5 back to the sender.

In the next window the lost packet 2 along with the discarded packet 3 is transmitted again along with packets 4 and 5.

Note that this window is re-transmitted again only after sender receives Ack for the first frame/packet which is how Go Back -N ARQ works.


1. Operational Overview

Sliding Window Concept

Handling Errors


2. Detailed Mechanism

Step-by-Step Process

  1. Transmission:
    The sender transmits a set of frames defined by the window size. Frames are numbered sequentially.

  2. Reception & Acknowledgment:
    The receiver checks each frame. It accepts frames in order and sends back a cumulative acknowledgment for the last correctly received frame in order.

  3. Error Occurrence:
    If a frame is found to be in error, the receiver discards it and any subsequent frames because they cannot be accepted out of order.

  4. Go-Back-N Action:
    The sender, upon noticing that the expected acknowledgment has not advanced (or detecting a duplicate ACK), retransmits the erroneous frame and every subsequent frame in the window.

Example Scenario


3. Advantages and Disadvantages

Advantages

Disadvantages


4. Throughput Consideration


5. When to Use Go-Back-N ARQ


3. Selective Repeat ARQ

https://www.youtube.com/watch?v=08y_Vrs1vHo&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=25

https://www.geeksforgeeks.org/sliding-window-protocol-set-3-selective-repeat/

Selective Repeat ARQ is an advanced error control protocol within the sliding window family. Unlike Go-Back-N, which retransmits all frames following an error, Selective Repeat ARQ retransmits only the individual frames that were received with errors. This selective retransmission can significantly improve throughput in environments where errors occur infrequently.

Pasted image 20250323132155.png

In this picture we see that that each frame contains two packets.

Frame 0 is sent first, starting with packet 0 to the receiver, followed by packet 1.
On receiving Frame 1 the receiver sends ACK to the sender that the frame has been received has been successfully received in the correct order.

Now frame 2 is sent from the sender but gets lost in the network.
But frame 3 is received successfully by the receiver, however since it didn't receive the preceding frame, it sends a NAK 2 or "Not acknowledged" to sender indicating that the frame 2 was not received by the receiver.

In the third section we see that the sender resends frame 2 along with a new frame lined up for sending as well after frame 2.

This way Selective repeat only resends the frames that are not received, thus saving time, computational and network resources.

1. Operational Overview

Sliding Window Concept

Independent Acknowledgments


2. Step-by-Step Process

  1. Transmission:

    • The sender transmits a sequence of frames within its window.

    • Each frame is assigned a sequence number to uniquely identify it.

  2. Reception & Buffering:

    • The receiver checks each incoming frame for errors.

    • If a frame is received correctly, an individual ACK is sent back for that frame.

    • Out-of-order frames are stored in the receiver’s buffer until all preceding frames are correctly received.

  3. Error Detection:

    • If the receiver detects an error in a frame (via CRC or other error-checking mechanisms), that frame is not acknowledged.
  4. Selective Retransmission:

    • The sender monitors ACKs. If an ACK for a specific frame is not received within a timeout period, only that particular frame is retransmitted.

    • This process continues until all frames are correctly acknowledged.

  5. Reordering at the Receiver:

    • Once the missing frames are received, the receiver reassembles the sequence in the correct order and delivers it to the higher layers.

3. Advantages and Disadvantages

Advantages

Disadvantages


4. Throughput Consideration


5. Example Scenario

Consider a sender with a window size of 4 transmitting frames numbered 1 to 4:


4. Piggybacking

https://www.geeksforgeeks.org/piggybacking-in-computer-networks/

Piggybacking is an optimization technique used in two-way communication protocols at the data link layer. It involves combining (or "piggybacking") an acknowledgment (ACK) with outgoing data instead of sending the acknowledgment as a separate frame.

Pasted image 20250323150552.png

1. Concept and Purpose


2. How Piggybacking Works

Step-by-Step Process:

  1. Receiving Data:

    • A sender transmits a data frame to a receiver.

    • The receiver processes the frame and prepares an acknowledgment.

  2. Delaying the ACK:

    • Instead of immediately sending a separate ACK frame, the receiver waits for a brief period.

    • This waiting period allows the receiver to determine if it has data to send back to the original sender.

  3. Combining Data and ACK:

    • If the receiver has data ready to transmit, it attaches (piggybacks) the ACK information within the header of the outgoing data frame.

    • The piggybacked frame thus serves a dual purpose: delivering data while simultaneously acknowledging the receipt of the earlier frame.

  4. Fallback to Standalone ACK:

    • If the receiver does not have any data to send within the waiting period, it will eventually send a standalone ACK frame to ensure the sender is informed of the successful reception.

3. Advantages of Piggybacking


4. Considerations and Limitations


5. Practical Example

Imagine two computers, A and B, communicating over a full-duplex link:


Random Access And Multiple Access Protocols

1. Overview of Multiple Access Protocols

Multiple access protocols manage how devices transmit data over a shared medium, reducing collisions (when two devices transmit at the same time) and ensuring fair access. They fall broadly into two categories:


The ALOHA Protocol

The ALOHA protocol was first developed at the University of Hawaii in the early 1970s for packet radio networks. However, it can be used in any situation where multiple devices share a common communication channel. This protocol allows devices to transmit data at any time, without a set schedule. This is known as a random access technique, and it is asynchronous because there is no coordination between devices.

There are two variants of the ALOHA Protocol.

Pasted image 20250323151943.png


1. Pure ALOHA

https://www.geeksforgeeks.org/what-is-pure-aloha/

https://www.youtube.com/watch?v=WYM9nFYnYAg&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=33

1. Operational Principle


2. Collision Window and Vulnerability Period

Pasted image 20250323152152.png


3. Handling Collisions


4. Throughput Efficiency

Explanation:
The efficiency S in Pure ALOHA can be modeled by the equation:

S=G . e2G

where G is the average number of transmission attempts (both new frames and retransmissions) per frame time. The maximum efficiency occurs when G=0.5


5. Advantages and Disadvantages

Advantages

Disadvantages


2. Slotted ALOHA

Slotted ALOHA is an enhancement over Pure ALOHA that introduces time synchronization to reduce the likelihood of collisions and improve throughput. In this version, the channel is divided into small, fixed-length time slots and users are only allowed to transmit data at the beginning of each time slot. This synchronization of transmissions reduces the chances of collisions between devices, increasing the overall efficiency of the network.

1. Fundamental Concept


2. How It Works

Transmission Process

  1. Frame Readiness:
    When a device has data to send, it waits until the start of the next time slot.

  2. Transmission at Slot Boundary:
    The device transmits its frame right at the beginning of the time slot.

  3. Collision Occurrence:

    • If two or more devices transmit in the same time slot, a collision occurs, and none of the frames in that slot are received correctly.

    • Since transmissions are synchronized to slot boundaries, collisions can only occur if more than one device transmits in the same slot—not partially overlapping as in Pure ALOHA.

Collision Handling

Pasted image 20250323153649.png


3. Throughput Efficiency


4. Advantages and Disadvantages

Advantages

Disadvantages


5. Comparison with Pure ALOHA

https://www.youtube.com/watch?v=ggdeb2_z240&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=34

Pasted image 20250323153936.png


6. Practical Considerations


3. CSMA (Carrier Sense Multiple Access Protocol)

https://www.youtube.com/watch?v=IftFvfSywCQ&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=35

https://www.geeksforgeeks.org/carrier-sense-multiple-access-csma/

Carrier Sense Multiple Access (CSMA) is an access control protocol used at the MAC layer that helps multiple nodes share a common communication channel efficiently.

Pasted image 20250323180212.png

In the diagram, starts sending the first bit of its frame at t1 and since C sees the channel idle at t2, starts sending its frame at t2. C detects A’s frame at t3 and aborts transmission. A detects C’s frame at t4 and aborts its transmission. Transmission time for C’s frame is, therefore, t3-t2 and for A’s frame is t4-t1

So, the frame transmission time (Tfr) should be at least twice the maximum propagation time (Tp). This can be deduced when the two stations involved in a collision are a maximum distance apart.

1. Basic Idea of CSMA


2. Variations of CSMA Protocols

a. 1-Persistent CSMA

b. 0-Persistent CSMA

c. P-Persistent CSMA


3. CSMA Variants in Practice


4. Key Takeaways


CSMA/CD (Collision Detection)

CSMA/CD—Carrier Sense Multiple Access with Collision Detection—is an enhancement over basic CSMA. It not only requires nodes to sense the medium before transmitting but also to monitor the medium during transmission to detect if a collision occurs.

1. Fundamentals of CSMA/CD


2. Timing and Collision Detection

Key Formula:
The transmission time (TT) must satisfy:

TT2×PD

Alternatively, if you express the transmission time in terms of message length (L) and bandwidth (R), then:

L2×PD×R

3. Collision Detection without Acknowledgements


4. How Does CSMA/CD Work?


How Does a Station Know if Its Data Collide?

Pasted image 20250323182137.png

Consider the above situation. Two stations, A & B. 
Propagation Time: Tp = 1 hr ( Signal takes 1 hr to go from A to B)


At time t=0, A transmits its data.  
        t= 30 mins : Collision occurs.

After the collision occurs, a collision signal is generated and sent to both A & B to inform the stations about the collision. Since the collision happened midway, the collision signal also takes 30 minutes to reach A & B.

Therefore, t=1 hr: A & B receive collision signals.

This collision signal is received by all the stations on that link.


How to Ensure that it is our Station’s Data that Collided?  (CA or Collision Avoidance)

For this, Transmission time (Tt) > Propagation Time (Tp) [Rough bound] 
This is because we want that before we transmit the last bit of our data from our station, we should at least be sure that some of the bits have already reached their destination. This ensures that the link is not busy and collisions will not occur. 
But, above is a loose bound. We have not taken the time taken by the collision signal to travel back to us. For this consider the worst-case scenario.

Consider the above system again.

Pasted image 20250323182252.png

At time t=0, A transmits its data.  
        t= 59:59 mins : Collision occurs

This collision occurs just before the data reaches B. Now the collision signal takes 59:59 minutes again to reach A. Hence, A receives the collision information approximately after 2 hours, that is, after 2 * Tp.

Hence, to ensure tighter bound, to detect the collision completely,
Tt > >= 2 * Tp

This is the maximum collision time that a system can take to detect if the collision was of its own data.


What should be the Minimum length of the Packet to be Transmitted?

Transmission Time = Tt = Length of the packet/ Bandwidth of the link 
[Number of bits transmitted by sender per second] 
Substituting above, we get, 
Length of the packet/ Bandwidth of the link>= 2 * Tp

Length of the packet >= 2 * Tp * Bandwidth of the link


Padding helps in cases where we do not have such long packets. We can pad extra characters to the end of our data to satisfy the above condition.

Features of Collision Detection in CSMA/CD

Advantages of CSMA/CD

Disadvantages of CSMA/CD


CSMA/CA (Collision Avoidance)

https://www.youtube.com/watch?v=reQ938TeFHM&list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_&index=37

🌐 Why CSMA/CA Exists

In wired networks, like Ethernet, we use CSMA/CD (Carrier Sense Multiple Access with Collision Detection). But in wireless networks (Wi-Fi, IEEE 802.11), we use CSMA/CA, because:


🚦 How CSMA/CA Works (Step-by-Step Gist)

  1. Sense the Channel (Carrier Sensing):

    • The device (node) checks if the channel is idle (no one else is talking).
    • If it's busy, it keeps waiting until it's free.
  2. Wait for a While (Inter Frame Space or IFS):

    • Even after the channel seems idle, the device waits a little longer
    • Why? Maybe another station is about to start sending, and this wait gives them a chance to go first — to avoid collision.
  3. Random Backoff (Contention Window):

    • The device picks a random number R in a growing window: 0 to (2^K - 1) where K is the number of previous failed attempts.
    • It waits for R * SlotTime before trying to send — this spreads out attempts among devices to reduce collision chances.
  4. RTS/CTS Handshake (optional but common in Wi-Fi):

    • Device sends an RTS (Ready To Send) signal to the access point.
    • Access point responds with CTS (Clear To Send).
    • Other devices hear the CTS and know they must stay silent for a while — this avoids hidden terminal issues.
  5. Data Transmission:

    • If the device gets CTS, it sends the actual data.
    • If it gets an ACK (acknowledgment), it knows the transmission was successful.
  6. If no ACK is received:

    • The device assumes a collision happened.
    • It increases K and tries again with a larger backoff.

🧠 Key Differences from CSMA/CD:

Feature CSMA/CD (Wired) CSMA/CA (Wireless)
Type Collision Detection Collision Avoidance
Medium Wired LAN Wireless LAN (Wi-Fi)
Collision Handling Detect collision via signal energy Avoid collisions via waiting & RTS/CTS
Acknowledgment Not always needed ACK is mandatory to confirm success
RTS/CTS Not used Often used to avoid hidden terminals

🧠 Simple Analogy

Imagine you're in a classroom: