You are on page 1of 60

Chapter 3

Data Link Layer


By Pavan Poudel
2
By Pavan Poudel

Functions of DLL
• Providing a well-defined service interface to the network
layer.
• Dealing with transmission errors.
• Regulating the flow of data so that slow receivers are not
swamped by fast senders.
3
By Pavan Poudel

Data Flow
• To the Network Layer, it looks as though the path to the new
machine happens at the DLL level, when it is really
happening at the physical level.
4
By Pavan Poudel

How the data is sent?


• Takes the packets of information from the Network Layer.
• Convert them into frames for transmission.
• Each frame holds the payload(packet) plus a header and a
trailer (overhead).
• Transmit frames over the physical layer.
5
By Pavan Poudel

DLL Services
• Framing
▫ Encapsulate datagram into frame, adding header(MAC address)
and trailer(error control bits)
• Link Access
▫ Channel access for shared medium
• Flow control
▫ Pacing between adjacent sending and receiving nodes
• Error Detection
▫ Signal attenuation, noise may cause error
▫ On detection of error, receiver sends signal to sender for
retransmission
• Error Correction
▫ Receiver identifies and corrects bit errors without resorting to
retransmission
6
By Pavan Poudel

DLL Services
• DLL can offer many different services which may vary from
system to system.
• Types of DLL services:
▫ Unacknowledged connectionless service
▫ Acknowledged connectionless service
▫ Acknowledged connection-oriented service
7
By Pavan Poudel

Unacknowledged Connectionless Service


• No acknowledgement from the receiving machine.
• No logical connection is set up between the two machines.
• The DLL will make no attempt to detect the loss of or recover
a lost frame.
• This service is useful for low error rate networks and for real-
time traffic where late data is worse than no data.
8
By Pavan Poudel

Acknowledged Connectionless Service


• The receiver acknowledges the arrival of each frame.
• If it hasn’t arrived correctly (or within the correct time) it can
be resent.
• This is a useful service when the connection is unreliable
(such as wireless).
• There is no requirement for such an acknowledgement
service to be implemented by the Data Link Layer.
9
By Pavan Poudel

Acknowledged Connection–oriented Service


• A connection is established between the two machines.
• The frames are then transmitted and each frame is
acknowledged.
• The frames are guaranteed to arrive only once and in order.
• This is the same as a “reliable” bit stream.
• The connection is released once the communication is
complete.
10
By Pavan Poudel

Framing
• The usual approach is for the data link layer to break the bit
stream up into discrete frames and compute the checksum for
each frame .
• When a frame arrives at the destination, the checksum is
recomputed. If the newly-computed checksum is different
from the one contained in the frame, the data link layer knows
that an error has occurred and takes steps to deal with it.
• Breaking the bit stream up into frames is more difficult than it
at first appears. One way to achieve this framing is to insert
time gaps between frames, much like the spaces between
words in ordinary text.
11
By Pavan Poudel

Framing Methods
• Character count
• Flag bytes with byte stuffing
• Starting and ending flags, with bit stuffing
12
By Pavan Poudel

Character Count Method


• Fields in header specify no. of characters in the frame.

A character stream. (a) Without errors. (b) With one error.


13
By Pavan Poudel

Character Count
• The trouble with this algorithm is that the count can be
garbled by a transmission error.
• For example, if the character count of 5 in the second frame
of (b) becomes a 7, the destination will get out of
synchronization and will be unable to locate the start of the
next frame.
• Even if the checksum is incorrect so the destination knows
that the frame is bad, it still has no way of telling where the
next frame starts.
• Sending a frame back to the source asking for a
retransmission does not help either, since the destination
does not know how many characters to skip over to get to the
start of the retransmission.
• For this reason, the character count method is rarely used
anymore.
14
By Pavan Poudel

Flag Byte with Byte Stuffing


• The second framing method gets around the problem of
resynchronization after an error by having each frame start
and end with special bytes.
• In the past, the starting and ending bytes were different, but in
recent years most protocols have used the same byte, called
a flag byte, as both the starting and ending delimiter, as
shown in Fig. (a) as FLAG.
• In this way, if the receiver ever loses synchronization, it can
just search for the flag byte to find the end of the current
frame.
• Two consecutive flag bytes indicate the end of one frame and
start of the next one.
15
By Pavan Poudel

Flag Byte with Byte Stuffing

(a) A frame delimited by flag bytes.


(b) Four examples of byte sequences before and after stuffing.
16
By Pavan Poudel

Byte Stuffing
• It may easily happen that the flag byte's bit pattern occurs in
the data. This situation will usually interfere with the framing.
• 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.
• Same is done if escape byte/character itself is present in
message.
• 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.
• Problem:
▫ two communicating machine might use 8-bit or 16-bit
characters
17
By Pavan Poudel

Starting and ending flags with Bit Stuffing


• The new technique allows data frames to contain an arbitrary
number of bits and allows character codes with an arbitrary
number of bits per character.
• It works like this. Each frame begins and ends with a special
bit pattern, 01111110 (in fact, a flag byte).
• Whenever the sender's data link layer encounters five
consecutive 1s in the data, it automatically stuffs a 0 bit into
the outgoing bit stream.
• This bit stuffing is analogous to byte stuffing, in which an
escape byte is stuffed into the outgoing character stream
before a flag byte in the data.
18
By Pavan Poudel

Bit Stuffing

Bit stuffing
(a) The original data.
(b) The data as they appear on the line.
(c) The data as they are stored in receiver’s memory after
destuffing.
19
By Pavan Poudel

Error Control
Types of Error:
• Content Error :
1. Error in bits of data
2. Eg: 0 is received in place of 1
• Flow integrity error:
1. Data blocks missing
2. Loss during transmission
• On the basis of no. of bits error
▫ Single bit error : error in only one bit
 10110 -> 10100
▫ Burst Error : error in two or more bits in a data unit
 11001010->11011001
▫ Extra bits are added in order to detect/correct errors.
20
By Pavan Poudel

Error Detecting Process


• Transmitter
▫ For a given frame, an error-detecting code (check bits) is
calculated from data bits
▫ Check bits are appended to data bits
• Receiver
▫ Separates incoming frame into data bits and check bits
▫ Calculates check bits from received data bits
▫ Compares calculated check bits against received check bits
▫ Detected error occurs if mismatch

• Parity Checking
• Checksum Error Detection
• Cyclic Redundancy Check(CRC)
21
By Pavan Poudel

Error Detecting Codes


• Parity Checking :
▫ Simplest technique MSB
▫ Adding a parity bit
▫ 8-bit word
Parity Bit Data Bit

▫ Even Parity : makes no. of 1’s in entire word even.


▫ Odd parity : makes no. of 1’s in entire word odd.

1 0 1 0 1 1 0 0 1 0 1 0 1 1 0 No. of 1’s = 4(Even)

Even Parity
1 0 1 0 1 1 0 1 1 0 1 0 1 1 0 No. of 1’s = 5(Odd)
Odd Parity
22
By Pavan Poudel

Parity Checking
Parity

1 0 0 1 0 1 0 1 1 0 1 1 0 1 0 1 Odd (i.e. Error)

Even Parity
1 0 1 1 1 1 0 1 Even (but Error)

• Limitations:
• Cannot detect error in 2,4 or 6 bits
• Cannot correct the error (error location unknown)
• Not suitable for burst error which are more common
23
By Pavan Poudel

Checksum Error Detection


• All the bytes of messages are added to generate a checksum
which is then sent after all the messages.
• Receiver separately calculates checksum and compares with
the one sent by sender.
• If both checksum matches, there is no error.
• Example:
▫ 10101101,00101101,10011101

10101101
00101101 • Limitations:
 Can only detect error but
11011010
 Not able to correct error
10011101
101110111

Ignored
24
By Pavan Poudel

Cyclic Redundancy Check (CRC)


• Type of polynomial code in which bit string is represented in
the form of polynomials with coefficients 0 and 1.
• Much powerful than parity and checksum
• Based on binary division
• Sequence of redundant bits called CRC are appended at the
end of data.
• Requirement of CRC
▫ It should be exactly one less bit than divisor
▫ Appending CRC to end of data unit should result in bit
sequence exactly divisible by the divisor.
25
By Pavan Poudel

CRC
CRC Generator: Data 0 0 ……0
• Append n 0’s to the data unit where n is
1 less than the number of bits in the pre-
decided divisor(n+1 bit). Divisor (n+1)bits

• Divide zero padded data by pre- Remainder


determined divisor(While dividing-
CRC
addition and subtraction must be modulo
n bits
2 operation (i.e. XOR).
• Obtain remainder  CRC
Data CRC
• This CRC will replace the n 0‘s
appended to data unit. Fig : CRC Generator
26
By Pavan Poudel

CRC
CRC checker Received Codeword
Data CRC
• Received codeword is divided by same
(n+1) bit divisor which was used at the
transmitter.
Divisor (n+1)bits
• If remainder is zero, then the received
codeword is error free and hence should
be accepted. Remainder
• But non-zero remainder indicates If remainder is 0 then
presence of errors and hence there is no error

corresponding codeword should be Fig: CRC checker


rejected.
27
By Pavan Poudel

CRC Example

• Here number of bits in


generator G(x) is 5. So we
need to append n(G(x)) -1
Zeros at the end of the
Frame.
• Perform XOR
• CRC Value is: 1110
• Generator = x4 + x1 + 1
=10011
28
By Pavan Poudel

CRC Example
• Let’s assume that we have
received the same bits i.e.
T(x) = 11010110111110.
Now let’s check the
remainder by using XOR
method with G(x).
• Here Remainder is Zero
so, the frame is transmitted
successfully.
29
By Pavan Poudel

Error Correcting Codes


• Error-correcting codes are widely used on wireless links,
which are notoriously noisy and error prone when compared
to copper wire or optical fibers.
• Without error-correcting codes, it would be hard to get
anything through.
• However, over copper wire or fiber, the error rate is much
lower, so error detection and retransmission is usually more
efficient there for dealing with the occasional error.
• Hamming Code is Example of error Correcting Code
30
By Pavan Poudel

Hamming Code
Dec. no. Binary P1 P2 P4 P8
1 0001 X
2 0010 X
3 0011 X X
4 0100 X
5 0101 X X
6 0110 X X
7 0111 X X X
8 1000 X
9 1001 X X
10 1010 X X
11 1011 X X X
12 1100 X X
13 1101 X X X
14 1110 X X X
15 1111 X X X X

• P1=(1,3,5,7) -> first least significant bit


• P2=(2,3,6,7) -> second least significant bit
• P4=(4,5,6,7) -> Third least significant bit
31
By Pavan Poudel

Hamming Code
32
By Pavan Poudel

Hamming Code
• Data To be encoded 10101101011

• After placing the data in the table we find that in positions 3,


6, 9, 10, 12, 14 and 15 we have a ‘1’. Using our previous
conversion table we obtain the binary representation for each
of these values.
• We then exclusive OR the resulting values (essentially setting
the parity bit to 1 if an odd # of 1’s else setting it to 0).
33
By Pavan Poudel

Hamming Code

• Encoded Data
34
By Pavan Poudel

Hamming Code
35
By Pavan Poudel

#Assignment
• Generate CRC code for data 110010101. The divisor is
10101. Also check whether there are errors in the
received codeword 1100101011011.
36
By Pavan Poudel

Flow Control
• Deals with the issue where sender sends data at higher rate
than receiver can receive
• One way is to implement buffer.
▫ Receiver should able to inform sender when the buffer is
full.
• Two approaches:
▫ Feedback-based flow control
 Feedback to the sender telling how receiver is doing
▫ Rate-based flow control
 Transfer rate is fixed by sender
 Not used often in DLL
37
By Pavan Poudel

Data Link Layer Protocols


• Unrestricted Simplex Protocol
• Simplex Stop and wait Protocol
• Simplex Protocol for Noisy Channel
• Duplex Protocol
• Piggybacking
• Sliding Window Protocol
38
By Pavan Poudel

Unrestricted Simplex Protocol


• Data are transmitted in one direction only.
• Both the transmitting and receiving network layers are always
ready.
• Processing time can be ignored. Infinite buffer space is
available.
• And best of all, the communication channel between the data
link layers never damages or loses frames.
• This thoroughly unrealistic protocol.
39
By Pavan Poudel

Simplex Stop-and-wait Protocol


• Simplest form of flow control.
• In Stop-and-Wait flow control, the receiver indicates its
readiness to receive data for each frame.
• Sender waits for ACK(Empty frame) for sometime
• If it do not receive ACK, retransmits the frame.
• Problem: if ACK get lost, same frame is received twice
• Operations:
▫ Sender: Transmit a single frame
▫ Receiver: Transmit acknowledgment (ACK)
▫ Goto 1.
40
By Pavan Poudel

Simplex Stop-and-wait Protocol

Figure shows an example of


communication using this
protocol. It is still very
simple. The sender sends
one frame and waits for
feedback from the receiver.
When the ACK arrives, the
sender sends the next frame.
Note that sending two
frames in the protocol
involves the sender in four
events and the receiver in
two events.
41
By Pavan Poudel

Major Drawbacks of stop and wait protocol


• Only one frame can be in transmitted at a time.
• This leads to inefficiency if propagation delay is much longer
than the transmission delay.
42
By Pavan Poudel

Simplex Protocol for Noisy Channel


• We must be able to deal with dropped and mangled frames
from a noisy channel.
▫ What do we do if the data arrives damaged?
▫ What do we do if the frame header is damaged?
▫ What do we do if the frame is dropped?
• The receiver must be able to determine if a frame is an
original or a retransmission.
• To do this, Sequential numbers are given to each frame
• If the receiver does not receive a frame, it can ask for
that specific frame to be retransmitted.
• If the frame arrives twice, the receiver can ignore the
second frame.
43
By Pavan Poudel

Duplex Protocol
• As opposed to sending information in only one direction, we
should make full use of the connection.
• Data is sent both ways
• We are already sending ACK back, so why not the data
• Use header to determine if it is data or ACK

• Piggybacking:
▫ Not to send separate frame for ACK rather, piggyback
ACK on next data frame
▫ Gives free ride to ACK and decrease overhead.
44
By Pavan Poudel

Sliding Window Flow Control


• Allows transmission of multiple frames.
• Sequence number ranging from 0 to 2k-1 (allowing k-bit field
for sequence number) is assigned to each frame.
• Sliding Window : An imaginary boxes at transmitter and
receiver sides. These window holds number of frames that
are allowed to be sent or received at a time.
• Operations:
▫ Sending Window
▫ Receiving Window
• Transmitter and receiver windows need not be of same size.
They may even sink or grow.
• Frames in sending window need to be stored in memory in
case, they need to be re-transmitted.
45
By Pavan Poudel

Sending Window
• At any instant, the sender is permitted to send frames with
sequence numbers in a certain range (the sending window)
46
By Pavan Poudel

Receiving Window
• The receiver maintains a receiving window corresponding to
the sequence numbers of frames that are accepted
47
By Pavan Poudel

How is flow control achieved ?


• Receiver can control the size of the sending window.
• By limiting the size of the sending window data flow from
sender to receiver can be limited.
48
By Pavan Poudel

ARQ Error Control


• Types of errors: Lost frames, damaged frames
• Most Error Control techniques are based on
▫ (1) Error Detection Scheme (e.g., Parity checks, CRC),and
▫ (2) Retransmission Scheme
• Error control schemes that involve error detection and
retransmission of lost or corrupted frames are referred to as
Automatic Repeat Request (ARQ) error control.

• The most common ARQ retransmission Schemes:


▫ Stop-and-Wait ARQ
▫ Go-Back-N ARQ
▫ Selective Repeat ARQ
• The protocol for sending ACKs in all ARQ protocols are based
on the sliding window.
49
By Pavan Poudel

Stop-and-wait ARQ
• Stop-and-Wait ARQ is an addition to the Stop-and-Wait flow
control protocol:
▫ Frames have 1-bit sequence numbers (SN = 0 or 1) i.e.
maximum window size = 1.
▫ Sender sends the frame and waits to get ACK.
▫ Receiver sends an ACK (1-SN) if frame SN is correctly
received.
▫ Sender waits for an ACK (1-SN) before transmitting the
next frame with sequence number 1-SN.
▫ Frame is retransmitted if transmitter receives negative
acknowledgement (NAK) or if no ACK is received for
certain time.
▫ Transmit next frame only after getting positive ACK for
previous frame.
50
By Pavan Poudel

Stop-and-wait protocol
Sender Receiver Sender Receiver

Data 0 Data 0

ACK 1 ACK 1

Data 1 Data 1

ACK 0 NAK Error in


frame 1
Data 1

ACK 0

Fig : Under Normal Condition Fig : For Damaged Frame


51
By Pavan Poudel

Stop-and-wait Protocol
Sender Receiver Sender Receiver
Data 0 Data 0

ACK 1 ACK 1

Data 1 Data 1
Time Out Lost
Time Out
ACK 0
Data 1 Lost

Data 1
ACK 0

ACK 0

Fig : When data is lost Fig : When ACK lost


52
By Pavan Poudel

One bit sliding window protocol

A sliding window of size 1,


(a) Initially.
(b) After the first frame has been sent.
(c) After the first frame has been received.
(d) After the first acknowledgement has been received.
53
By Pavan Poudel

Disadvantages of stop-and-wait ARQ


• Problem is in efficiency
• Only one frame is transmitted at a time
• Sender will have to wait at least one round trip time before
sending next
54
By Pavan Poudel

Go-Back-n ARQ
• The sender keeps track of the outstanding frames and
updates the variables and windows as the ACKs arrive
• Allow transmitter to continuously send enough frames (frame
in window) and waits for acknowledgement.
• If a frame is damaged or lost, all the frames since last frame
acknowledged are re-transmitted.
• Increased efficiency due to pipelining.
0 1 2 3 4 5 3 4 5 6

ACK ACK ACK NAK

0 1 2 3 4 5 3 4 5
Error
Discarded
55
By Pavan Poudel

Go-Back-n ARQ
Sender Receiver Sender Receiver

Data 0 Data 0

Data 1 Data 1

Lost
Data 2 Damaged Data 2
NAK 2 Frame
Retransmitted Discarded

Data 3 Data 3

Data 4 Data 4

Data 2
Retransmitted Data 2

Data 3 Data 3

Fig : When frame is damaged Fig : When data is lost


56
By Pavan Poudel

Disadvantages of Go-Back-n ARQ


• It transmit all the frames if one frame is damaged or lost.
• It continuously transmit frame until it does not receive NAK
• If a frame is damaged or lost, all the frames since last frame
acknowledged are re-transmitted.
57
By Pavan Poudel

Selective Repeat ARQ


• Similar to Go-Back-N ARQ. However, the sender only
retransmits frames for which a NAK is received
• Advantage over Go-Back-N:
▫ Fewer Retransmissions.
• Disadvantages:
▫ More complexity at sender and receiver
▫ Each frame must be acknowledged individually (no
cumulative acknowledgements)
▫ Receiver may receive frames out of sequence
58
By Pavan Poudel

Selective Repeat ARQ


• Window size of receiver is more than one so, it is able to frames
received after NAK has been sent until damaged frame has been
replaced.
• As frame are not accepted in order, it may introduce certain problem
(Reordering required).

1 2 3 4 5 6 7 3 8 9 10 Transmitter

NAK

Receiver 1 2 3 4 5 6 7 3 8 9 10
Damaged Retransmitted
59
By Pavan Poudel

Selective Repeat ARQ


60

Thank You !!!

You might also like