You are on page 1of 104

Data Link Layer

Data link Layer Design Issues

 Services interface to the network layer.

 Framing

 Dealing with transmission errors.(Error Control)

 Regulating the flow of data so that slow receivers

are not swamped by fast senders.(Flow Control)


Functions of the Data Link Layer
 Relationship between packets and
frames.
Services to Network Layer
 Transferring data between network
layers of machines
Types of Services

1. Unacknowledged Connectionless Service.


2. Acknowledged connectionless Service.

3. Acknowledged connection-oriented Service.


Services
 Unacknowledged connectionless service
Real-time traffic, e.g., speech, video
Most LANs, such as Ethernet

 Acknowledged connectionless service


Useful over unreliable channels
Each frame sent individually acknowledged
e.g., wireless systems, e.g. 802.11 (WiFi)
Services
 Acknowledged connection-oriented
service
Guarantees
Each frame sent is received without error
All frames sent are received in right order
Three phases:
Connection establishment
 Variables and counters initialization
Frame transmission
Connection release
 Variables, buffers, resources freed up
Framing
 Fact
Raw bit stream delivered by physical layer is
not error free

 Data link layer detects/corrects errors


Framing
Computing checksum
Handling error if any
Framing
 Approaches

1) Character Count.

2) Flag bytes with byte stuffing.

3) Starting and ending flags, with bit stuffing.


Character Count
 A field in header specifies number of characters in a
frame.
 Problem?
Example
The following character encoding is used in a data link protocol:
 A: 01000111;
 B: 11100011;
 FLAG: 01111110;
 ESC: 11100000
 Show the bit sequence transmitted (in binary) for the four-character
frame: A B ESC FLAG
 When Character count framing method is used:

Answer

00000101 01000111 11100011 11100000 01111110


Flag Bytes with Byte Stuffing
 A frame delimited by flag bytes
 Four examples of byte sequences before and after stuffing
Byte Stuffing / Character Stuffing
 A serious problem may easily happen that the flag byte's bit
pattern occurs in the data.
 One way to solve this problem is to have the sender's data
link layer insert a special escape byte (ESC) just before each
''accidental'' flag byte in the data.
 The data link layer on the receiving end removes the escape
byte before the data are given to the network layer.
 This technique is called byte stuffing or character stuffing.
 Thus, a framing flag byte can be distinguished from one in
the data by the absence or presence of an escape byte
before it.
Example
The following character encoding is used in a data link protocol:
 A: 01000111;
 B: 11100011;
 FLAG: 01111110;
 ESC: 11100000

Show the bit sequence transmitted (in binary) for the four-character frame: A
B ESC FLAG
When Flag bytes with byte stuffing framing method is used:

Answer
01111110 01000111 11100011 11100000 11100000

11100000 01111110 01111110


Flag Bits with Bit Stuffing
 Each frame begins and ends with special bit pattern (flag
byte): 01111110
 Problem: 6 consecutive 1s in data
 Solution: Bit Stuffing: inserting a 0 after 5 consecutive 1s

Original Data

After
Stuffing

After received
and destuffed
Example
 The following character encoding is used in a data link protocol:
 A: 01000111;
 B: 11100011;
 FLAG: 01111110;
 ESC: 11100000
 Show the bit sequence transmitted (in binary) for the four-character
frame: A B ESC FLAG
 When Starting and ending flag bytes, with bit stuffing framing method is
used:

Answer

01111110 01000111 11010001 11110000 0 011111010


0111110
Exercises
The following character encoding is used in a data link protocol:
A: 11010101; B: 10101001; FLAG: 01111110; ESC: 10100011
 Show the bit sequence transmitted (in binary) for the five-character
frame: A ESC B ESC FLAG
when each of the following framing methods are used:
(a) Flag bytes with byte stuffing.
(b) Starting and ending flag bytes, with bit stuffing.

ANSWER:

 a) 01111110 11010101 10100011 10100011 10101001


10100011 10100011 10100011 01111110 01111110

 b) 01111110 11010101 10100011 10101001 10100011


011111010 0111110
Exercises
1.Given the output after byte-stuffing:
FLAG A B ESC ESC C ESC ESC ESC FLAG ESC FLAG D F FLAG.
What is the original data?
ANSWER:
 A B ESC C ESC FLAG FLAG D F

2. Given the output after byte-stuffing:


FLAG A B ESC ESC C ESC ESC ESC FLAG ESC FLAG D FLAG.
What is the original data?
ANSWER:
 A B ESC C ESC FLAG FLAG D

3. A bit string, 0111101111101111110, needs to be transmitted at


the data link layer. What is the string actually transmitted after
bit stuffing?
ANSWER:
 The output is 011110111110011111010
Error Control
 Using acknowledgement
Positive- If the sender receives a positive acknowledgement about a
frame, it knows the frame has arrived safely.
Negative-On the other hand, a negative acknowledgement means that
something has gone wrong, and the frame must be transmitted again.
 Problem: In some cases, sender waits for acknowledgement
forever if a frame is lost for ever.
Solution: Timer
 Problem: Duplicate transmission (receiver will accept the
same frame two or more times)
Solution: Sequence number
Positive Acknowledgement
 Sender sends a message, waits for
acknowledgement from receiver, and
then sends next message
Reliability and Acknowledgement
 Case 1: no error  Case 2: data lost
Sender Receiver Sender Receiver

Time Data Time Data


X
Timeout
Ack.
Data

Ack.

Timeout and retransmission


Reliability and Acknowledgement
 Case 3: data error  Case 4: ack. lost
Sender Receiver Sender Receiver
Time Data Time Data

Timeout Error Timeout


X
Data Data

Ack. Ack.

Timeout and retransmission New problem? Duplicate


Solution: Sequence number
Flow Control
 Needed
 Problem
When frames are transmitted faster than
receiver can accept, frames will be lost
 Solution
Flow control by feedback mechanism
 The protocol contains well-defined rules
about when a sender may transmit the
next frame
Introduction
 Transmission impairments (errors)
Attenuation
Loss of energy as signal propagates
Delay Distortion
Components travel at different speeds
Noise
Unwanted energy from other sources
Attenuation, distortion, and noise

Attenuation

Delay
Distortion

Noise

©The McGraw-Hill Companies, Inc., 2004


Types of Errors
Single-bit error
Burst error
Error Correcting/Detecting Codes
 Error correction
Referred to as forward error correction
Detect and correct error
 Error detection
Detect error and request retransmission
 Redundancy added to message
 Codeword (n bits)
message + redundancy
(m bits) (r bits)
n = m + r
Code rate = m / n
Error Correcting/Detecting Codes
 Where are error correction/detection
used?
Correcting: physical layer and higher layers
Detecting: data link, network, and
transport
 Error correcting or error detecting?
Circumstances, application, etc.
Error-Correcting Codes
 Hamming codes (our focus)
 Binary convolution codes
GSM mobile phone system, satellite
communication, 802.11
 Reed-Solomon codes
DSL, data over cable, satellite communications
CDs, DVDs, Blue-ray disks
 Low-density parity check codes
Digital video broadcasting, 10 Gbps Ethernet,
power-line networks, 802.11
Hamming Distance
 The number of bit positions in which
two codewords differ is called the
Hamming distance .
 Its significance is that if two codewords
are a Hamming distance ‘d’ apart, it will
require ‘d’ single-bit errors to convert
one into the other
Error Correction
 Hamming Code
 Linear systematic block code
 For m-bit message we need r-bit redundancy, where
(m +r + 1)  2r  Why?
 Redundancy bits are placed in position of 2’s power
 Example: If m = 7, then r = 4
Hamming Code Example

7-bit data word "0110101“(d -data bits, p -parity bits)


Combination

p1 : bits 1, 3, 5, 7, 9, 11..

p2 : bits 2,3,6,7,10,11..

p4 : bits 4,5,6,7..

p8 : bits 8,9,10,11..
Hamming Code Example (Cont.)

•Assume the final bit gets corrupted and turned from 1 to 0


•Flag each parity bit as 1 when the even parity check fails
Hamming Code Example
 How to construct codeword
 message: m = 7 bits
 1. find r using the formula  r = 4
 2. place data bits in codeword
 3. calculate P bits: even parity

Sum should be even


P1+1+0+0+0+1  P1 = 0
P2+1+0+0+0+1  P2 = 0
P4+0+0+0  P4 = 0
P8+0+0+1  P8 = 1
Hamming Code Example (Cont’d)
 How to correct error and retrieve message
1. compute syndrome similar to codeword
construction  error position
2. flip the bit in the error position
3. remove P bits from codeword

Example of an (11, 7) Hamming code correcting a single-bit error


Use of a Hamming code to correct burst errors.
Error-Detecting Codes
 Parity
 Checksums
Internet
 Cyclic Redundancy Checks (CRCs)
 They are all linear systematic block
code
Error-Detecting Codes
 Parity bit
Added to data so that number of 1 bits in
codeword is
Even (even parity)
Odd (odd parity)
E.g., ASCII of ‘H’ is 1011010, its codeword
is
10110100
________ if even parity is used
10110101
________ if odd parity is used
Use Parity to Detect Burst Errors
 Organize a block into (k x n) matrix
 One parity for each column

 one row of parities at the bottom


 Transmit one row at a time
 Can detect burst errors of length  n
Example

 Interleaving of parity bits to detect a


burst error
Error-Detecting Code - CRC
 Bit stream is treated as polynomial w/ coefficients
0 and 1
 Example:
data: 10100111
polynomial:
degree7 = 7 5
x  x  x  x 1
2
 Modulo 2 arithmetic is used
Example: 10011011 11110000
+11001010 -10100110
01010001 01010110
XOR
CRC generator and checker
Error-Detecting Code - CRC
 Use generator polynomial G(x) to calculate
checksum R(x)
Frame: P(x) generator: G(x)
degree of G(x) = d

P(x)  x d R(x)
 Q(x) 
G(x) G(x)
Transmitted: checksummed frame P(x)·xd + R(x)
 It’s guaranteed that P(x)·xd + R(x) is divisible by
G(x)!!
Error-Detecting Code - CRC
 Receiver
Divides checksummed frame by G(x)
If remainder is zero
No error, CRC is removed
Otherwise
Error, the frame is discarded
CRC -
Example

Generator:
x4  x 1

0 0
Received 0 0 0 1 0
Error during transmission
0
0 1

CRC -
Example

Generator:
x4  x 1 0

0
0 0 0
1 0

0 01 0 ≠0
Frame discarded
CRC – Example 1
G(x) = x3  x2  1 = 1101 Generator
P(x) = x7  x4  x3  x = 10011010 Message
1101 10011010000 Message plus k
1101 zeros
k + 1 bit check 1001
sequence c, 1101
equivalent to a 1000
degree-k 1101 Result:
polynomial 1011
1101 Transmit message
1100
1101
followed by
1000 remainder:
Remainder 1101

m mod c 101 10011010101


Example 1 CRC Checking (No Errors)

G(x) = x3  x2  1 = 1101 Generator


P(x) = x10  x7  x6  x4  x2  1 = 10011010101 Received Message

1101 10011010101 Received


1101 message, no
k + 1 bit check 1001 errors
sequence c, 1101
equivalent to a 1000
degree-k 1101 Result:
polynomial 1011
1101 CRC test is passed
1100
1101
1101
Remainder 1101

m mod c 0
Example 1:CRC Checking (With Errors)

G(x) = x3  x2  1 = 1101 Generator


P(x) = x10  x7  x5  x4  x2  1 = 10010110101 Received Message

1101 10010110101 Received


1101 message
k + 1 bit check 1000 Two bit errors
sequence c, 1101
equivalent to a 1011
degree-k 1101 Result:
polynomial 1101
1101 CRC test failed
0101

Remainder
m mod c
Example 2 Calculating CRC
Example 2 :Checking CRC
Exercise

For G=110011 and P=11100011, find the CRC


or
For P(x)=x7+x6+x5+x+1, G(x)=x5+x4+x+1 calculate CRC.
CRC Properties
 Single error detection
 Double error detection w/ carefully
chosen G(x)
 Odd number error detection if (x + 1) is
a factor of G(x)
 Detect burst error length  r for r check
bits
 Can be implemented in hardware using
simple shift register circuit
Standard CRC Examples
• CRC-8 (for ATM header):
x8 + x 2 + x + 1
• CRC-32 (for LANs):
x32 + x26 + x23 + x22 + x16 + x12 + x11 +
x10 + x8 + x7 + x5 + x4 + x2 + x + 1
• CRC-16-(for Bluetooth, etc.):
x16 + x12 + x5 + 1
Elementary Data Link Protocols
 Key Assumptions
Network, data link, and physical layers are
independent processes communicating by
sending messages
Machine A wants to send a long stream of
data to machine B over a reliable,
connection-oriented service
Implementation of Physical, Data
Link, and Network Layers
Data Structures and Primitives
Data Structures and Primitives
Data Structures and Primitives
Unrestricted Simplex Protocol
 Utopia protocol
 Assumptions
Unidirectional data transmission
Transmitting/receiving network layers are
always ready
Processing time is ignored
Infinite buffer space
No errors
Unrestricted Simplex Protocol - Sender
Unrestricted Simplex Protocol - Receiver
Simplex Stop-and-Wait Protocol
 Assumptions
Unidirectional data transmission
Transmitting/receiving network layers are always
ready
Finite processing speed
Finite buffer capacity
No errors
 Problem: Sender sends too fast
 Stop-and-wait
Senders sends one frame and then waits for an
acknowledgement before processing
Simplex Stop-and-Wait Protocol - Sender
Simplex Stop-and-Wait Protocol - Receiver
Protocol for noisy channel
Simplex PAR Protocol
 For noisy channel
 Positive acknowledgement with
retransmission (PAR)
 Sender waits for a positive
acknowledgement before advancing to
the next data item
 Also Known as ARQ (Automatic Repeat
reQuest)
PAR Protocol
 Assumptions
Unidirectional data transmission
Transmitting/receiving network layers are
always ready
Finite processing speed
Finite buffer capacity
Errors, can be detected
 Timer + sequence number
Size (i.e., # bits) of sequence number?
PAR Protocol – Sender
PAR Protocol – Sender (Cont’d)
PAR Protocol – Receiver
Sliding window protocols
Data Frame transmission
 Unidirectional assumption in previous
elementary protocols
 Not general
 Full-duplex - approach 1
Two separate communication channels
Forward channel for data
Reverse channel for acknowledgement
 Problems: 1. reverse channel bandwidth wasted
2. cost
Sliding Window Protocols
 Full-duplex - approach 2
Same circuit for both directions
Data and acknowledgement are intermixed
How do we tell acknowledgement from data?
"kind" field telling data or acknowledgement
 Approach 3
Attaching acknowledgement to outgoing data
frames
 Piggybacking
Piggybacking
 Temporarily delaying transmission of outgoing
acknowledgement so that they can be hooked
onto the next outgoing data frame
 Advantage: higher channel bandwidth
utilization
 Complication:
How long to wait for a packet to piggyback?
If longer than sender timeout period then
sender retransmit
 Purpose of acknowledgement is lost
Piggybacking
 Solution for timing complexion
If a new packet arrives quickly
 Piggybacking
If no new packet arrives after a receiver ack
timeout
 Sending a separate acknowledgement
frame
Sliding Window Protocol
 We are going to study three
bidirectional sliding window protocols
(max sending window size, receiving
window size)
One-bit sliding window protocol (1, 1)
Go back N (>1, 1)
Selective repeat (>1, >1)
 Differ in efficiency, complexity, and
buffer requirements
Sliding Window Protocol
 Each outbound frame contains an n-bit
sequence number
Range: 0 - MAX_SEQ (MAX_SEQ = 2n - 1)
 At any instance of time
Sender maintains a set of sequence
numbers of frames permitted to send
These frames fall within sending window
Receiver maintains a set of sequence
numbers of frames permitted to accept
These frames fall within receiving window
Sliding Window Protocol
 Lower limit, upper limit, and size of two
windows need not be the same
 Fixed or variable size
 Requirements
Packets delivered to the receiver's network
layer must be in the same order that they
were passed to the data link layer on the
sending machine
Frames must be delivered by the physical
communication channel in the order in
which they were sent
Sending Window
 Contains frames can be sent or have
been sent but not yet acknowledged –
outstanding frames
 When a packet arrives from network
layer
Next highest sequence number assigned
Upper edge of window advanced by 1
 When an acknowledgement arrives
Lower edge of window advanced by 1
Sending Window
 If the maximum window size is n, n
buffers is needed to hold
unacknowledged frames
 Window full (maximum window size
reached)
 shut off network layer
Receiving Window

 Contains frames may be accepted


 Frame outside the window  discarded
 When a frame's sequence number
equals to lower edge
Passed to the network layer
Acknowledgement generated
Window rotated by 1
Receiving Window
 Contains frames may be accepted
 Always remains at initial size (different from
sending window)
 Size
=1 means frames only accepted in order
>1 not so
 Again, the order of packets fed to the
receiver’s network layer must be the same
as the order packets sent by the sender’s
network layer
Actually, 1-bit
sequence
number is
enough for
this example.
The purpose
of using 3-bit
is to
demonstrate
the idea of
sliding
A sliding window of size 1, with a 3-bit sequence number. window.
(a) Initially.
In many textbooks, an
(b) After the first frame has been sent. array of boxes are used to
(c) After the first frame has been received. represent the window.
(d) After the first acknowledgement has been received.
One Bit Sliding Window Protocol
 Sending window size = receiving
window size = 1
 Stop-and-wait
 Acknowledgement =

Sequence number of last frame


received w/o error*
 Problem of sender and receiver send
simultaneously
*: some protocols define the acknowledgement to be the
sequence number expected to receive
Case 1: normal case Case 2: simultaneous start

(a) Case 1: Normal case. (b) Case 2: Abnormal case.


The notation is (seq, ack, packet number). An asterisk indicates
where a network layer accepts a packet.
Problem with stop and wait protocols
 Example:
 50 Kbps bandwidth (satellite channel), 1000 bit frames, 500 msec round-
trip propagation delay

Receiver
Sender
first packet bit transmitted, t = 0
last packet bit transmitted, t = 20
500/2=250
1000 bits/50,000 bps=20 msec first packet bit arrives: 500/2=250
500

500/2=250
ACK arrives: 500+20=520

Line utilization = 20/520=3.85% !!  sender is blocked 96.15% of time !


Pipelining
 Solution for poor channel utilization:
Use larger frames, but the maximum size is limited by
the bit error rate of the channel. The larger the
frame, the higher the probability that it will become
damaged during transmission.
 Allowing the sender to send more than 1 frame, w
frames send before blocking (Pipelining)
 In Pipelining:
 Sender does not wait for each frame to be ACK'ed. Rather it
sends many frames with the assumption that they will arrive.
Must still get back ACKs for each frame.
 Provides more efficient use of transmit bandwidth, but error
handling is more complex.
 Value of w? times equal to round-trip time,
 For previous example, W=26 (Because 520/20=26)
Pipelining
 In Pipelining:
Sender after sending the wth frame, will get the
ACK of first frame.
 If error occurred:
What if 26 frames transmitted, and the second has an
error. Frames 3-26 will be ignored at receiver side?
Sender will have to retransmit. What are the
possibilities?
 Two strategies as solutions (depends on receive
Window size)
 Go-Back-N
 Selective-Repeat
Go-Back-N
 Go-Back-N:
Equivalent to receiver's window size = 1.
If receiver sees bad frames or missing
sequence numbers, subsequent frames are
discarded. No ACKs for discarded frames.
Go Back n Protocol
 Receiver discards all subsequent frames
following an error one, and send no
acknowledgement for those discarded
 Receiving window size = 1 (i.e., frames must
be accepted in the order they were sent)
 Sending window might get full
If so, re-transmitting unacknowledged frames
 Wasting a lot of bandwidth if error rate is
high
Go Back n Protocol Implementation
 Sender has to buffer unacknowledged
frames
 Acknowledge n means frames n,n-1,n-2,
... are acknowledged (i.e., received
correctly) and those buffers can be
released
 One timer for each outstanding frame in
sending window
Pipelining and error recovery. Effect of an error when (a) receiver's
window size is 1 and (b) receiver's window size is large
Selective-Repeat
 Selective-Repeat:
Receiver's window size larger than one.
Store all received frames after the bad one. ACK
only last one received in sequence.

 Use NAK (Negative ACK) when an error detected:


checksum error or out of sequence
Select Repeat Protocol
 Receiver stores correct frames following the bad
one
 Sender retransmits the bad one after noticing
 Receiver passes data to network layer and
acknowledge with the highest number
 Receiving window > 1 i.e., any frame within the
window may be accepted and buffered until all
the preceding one passed to the network layer
 Might need large memory
Negative Acknowledgement (NAK)
 SRP is often combined with NAK
 When error is suspected by receiver,
receiver request retransmission of a frame
Arrival of a damaged frame
Arrival of a frame other than the expected
 Does receiver keep track of NAK?
 What if NAK gets lost?
Selective Repeat with NAK

t
los
2,
k
Na
Select Repeat Protocol Implementation
 Receiver has a buffer for each sequence
number within receiving window
 Each buffer is associated with an
"arrived" bit
 Check whether sequence number of an
arriving frame within window or not
If so, accept and store
 Maximum window size = ? Can it be
MAX_SEQ ?
Select Repeat Protocol - Window Size
 Problem is caused by new and old
windows overlapped
 Solution
Window size=(MAX_SEQ+1)/2
E.g., if 4-bit window is used, MAX_SEQ = 15
 window size = (15+1)/2 = 8
 Number of buffers needed
= window size
Select Repeat Protocol

(a) Initial situation with a window size seven.


(b) After seven frames sent and received, but not acknowledged.
(c) Initial situation with a window size of four.
(d) After four frames sent and received, but not acknowledged.

You might also like