Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 51

CHAPTER 2: DATA LINK LAYER

 2.1 Data link layer


 2.2 Error detection

 2.3 Error correction

 2.4 Flow Control


LINK LAYER: INTRODUCTION “link”
Some terminology:
 hosts and routers are nodes
 communication channels that
connect adjacent nodes along
communication path are links
 wired links
 wireless links
 LANs

 layer-2 packet is a frame,


encapsulates datagram

data-link layer has responsibility of


transferring datagram from one node 5-2
to adjacent node over a link
LINK LAYER
 Datagram transferred by different link
protocols over different links:
e.g., Ethernet on first link, frame relay on
intermediate links, 802.11 on last link
 Each link protocol provides different
services
e.g., may or may not provide reliable data
transfer over the link

5-3
LINK LAYER SERVICES
 Framing, link access:
 encapsulate datagram into frame, adding header, trailer
 channel access if shared medium
 “MAC” addresses used in frame headers to identify source,
destination
 different from IP address!
 Reliable delivery between adjacent nodes
 seldom used on low bit error link (fiber, some twisted pair)
 wireless links: high error rates

5-4
LINK LAYER SERVICES
 Flow Control:
 pacing between adjacent sending and receiving nodes
 Error Detection:
 errors caused by signal attenuation, noise.
 receiver detects presence of errors:
 signals sender for retransmission or drops frame
 Error Correction:
 receiver identifies and corrects bit error(s) without resorting to
retransmission
 Half-duplex and full-duplex
 with half duplex, nodes at both ends of link can transmit, but not
at same time 5-5
ADAPTORS COMMUNICATING
datagram
link layer protocol rcving
sending node
node
frame frame
adapter adapter

 link layer implemented in  sending side:


“adaptor” (NIC)  encapsulates datagram in a frame
 adds error checking bits, reliable data
 Ethernet card, PCMCI card,
transfer, flow control, etc.
802.11 card
 receiving side
 NIC contains
 looks for errors, rdt, flow control, etc
 RAM,
 extracts datagram, passes to receiving
 DSP chips,
node
 a host bus interface and
 adapter is semi-autonomous 5-6
 a link interface
ERROR DETECTION AND CORRECTION
 There are two strategies for dealing with errors.
 detecting errors- adding redundant information
 correcting errors- appending error detecting code

 On reliable channels, such as fiber, error detecting code


is used.
 retransmit occasional error block
 On channels such as wireless links with many errors,
error correcting mechanism is used.
 no need of retransmission- which itself may be in error
ERROR DETECTION
EDC= Error Detection and Correction bits (redundancy)
D = Data protected by error checking, may include header fields

 Error detection not 100% reliable!


 protocol may miss some errors, but rarely
 larger EDC field yields better detection and correction

5-8
PARITY CHECKING
Two Dimensional Bit Parity:
Single Bit Parity: Detect and correct single bit errors
Detect single bit errors

0 0

5-9
INTERNET CHECKSUM
Goal: detect “errors” (e.g., flipped bits) in transmitted segment
(note: used at transport layer only)

Sender: Receiver:
 treat segment contents as  compute checksum of received
sequence of 16-bit integers segment
 checksum: addition (1’s
 check if computed checksum
complement sum) of equals checksum field value:
 NO - error detected
segment contents
 YES - no error detected. But
 sender puts checksum
maybe errors nonetheless.
value into checksum field
CYCLIC REDUNDANCY CHECK
 view data bits, D, as a binary number
 choose r+1 bit pattern (generator), G
 goal: choose r CRC bits, R, such that
 <D,R> exactly divisible by G (modulo 2)
 receiver knows G, divides <D,R> by G. If non-zero remainder: error
detected!
 can detect all burst errors less than r+1 bits

 widely used in practice (ATM, HDLC)

5-11
CRC EXAMPLE
Want:
D.2r XOR R = nG
equivalently: R = remainder[ D] .2r
G
D.2r = nG XOR R
equivalently:
if we divide D.2r by G,
want remainder R

5-12
EXAMPLE
 Generator = 1001
 Message = 101110

 Then determine the transmitted frame by applying CRC


error detection code.
ERROR CORRECTION
 Frame consists of m data bits and n redundant bits.
 The total length, N = m + n, an N-bit codeword.

 Hamming distance of code:


 Minimum distance between any two code words in a code
 Two codewords,
10001001
10110001
differs by 3 bits (Hamming distance of 3).
 To correct d-bit errors, we need distance of 2d + 1.

 To detect d-bit errors, we need distance of d + 1.


EXAMPLE
0000000000
0000011111
1111100000
1111111111
 Hamming distance = 5, it can correct 2 bit errors, and detect 4-
bit errors.
 If 0000000111 received
 has to be 0000011111
 provided double bit errors.
 If a triple error changes 0000000000 into 0000000111, the
error will not be corrected.
HAMMING CODES
 Richard W. Hamming published a general method for
constructing error-correcting codes with a minimum distance
of 3.
 The Hamming code is capable of correcting only one bit error.

 A Hamming code word consists of m message bits and n


Hamming bits.
HAMMING CODES
 To design a code with m message bits and n hamming bits and
allow all single errors to be corrected, each of the 2m legal
messages has m + n illegal codewords at a distance 1 from it.
 Formed by inverting each of the m + n bits in the (m + n)-bit
codeword from it.
 Thus, each of the 2m legal messages requires m + n + 1 bit
patterns dedicated to it.
 Since the total number of bit patterns is 2m+n, we must have (m
+ n + 1) 2m ≤ 2m+n.
This becomes (m + n + 1) ≤ 2n.
 Given m, this puts a lower limit on the number of hamming
bits needed to correct single errors.
 The Hamming bits should not be grouped together.
 They are parity bits, which cover groups of bits throughout the
message.
 They can be placed in different ways.

 Consider the following arrangement in which the Hamming


bits are located at integer powers of 2.
 (Start with 2^0 =1, so h1 is placed at bit coding position 1, and
2^1=2, so h2 is placed at bit position 2, and 2^2=4, so h3 is
placed at bit position 4, and so on…..).
 If we have a 7 bit message, (7 + n + 1) ≤ 2n , n = 4

 So the 4 Hamming bits are placed in positions 1, 2, 4 and 8.


HAMMING ENCODING
 We need to determine the value of the Hamming bits for a
given binary message.
 Consider transmitting the 7-bit ASCII code corresponding to
the letter “K”
The following process is used to compute the Hamming bits:
 Express all the bit positions that contain a value of “1” as
binary numbers.
 In this case, positions 11, 7, 5, and 3.
 XOR these binary numbers together.
 The resulting four bit binary number correspond the values of
the Hamming bits.
 So the result is the 4 bits of Hamming code, which is
interleaved with the data into the specific positions we have
chosen, the MSB is h4 and the LSB is h1.
HAMMING DECODING
Decoding follows a similar process:
 Express all the bit positions in the received codeword
that contain a value of “1” as binary numbers (not
including the Hamming bits)
 In this case, positions 11, 7, 5, and 3.
 XOR these binary numbers together with 4-bit string
formed by the Hamming bits (1010)
 If no error is present, the resultant will be all zeros.
 Hamming decoding (no errors)
 From the previous example (positions 11, 7, 5, 3)
 Hamming decoding (with errors)
 Let’s consider the case in which a single bit error occurs.

 Say an error occurs in bit position 7

and the following codeword is received.


Apply the Hamming
decoding process.
A “1” is present in bit
positions 11, 5 and 3
and the Hamming bits
are 1010.

 The resulting 4 bit number reveals the bit position of the error.
(0111 => bit position 7)
 In this example using an (11, 7) Hamming code, with a
minimum distance of 3, this code can correct all single bit error
FLOW CONTROL
 Flow control refers to a set of procedures used to restrict the amount
of data that the sender can send before waiting for acknowledgment.
 Flow control is one of the most important duties of the data link layer.

 The flow of data must not be allowed to overwhelm the receiver.

 Any receiving device has a limited;


 speed to process incoming data
 amount of memory to store incoming data
 The receiver must inform the sender before those limits are reached
so that the transmitting device send fewer frames or stop.
 The rate of incoming data processing is slower than the rate of
transmission.
 Each receiving device has a buffer reserved for storing incoming data
until they are processed.
STOP-AND-WAIT PROTOCOL
 The sender sends one frame, stops until it receives
confirmation from the receiver and then sends the next
frame.
 Unidirectional communication for data frames is used,
but auxiliary ACK frames travel from the other direction.
SENDER SIDE
while (true) //Repeat forever
canSend = true //Allow the first frame to go
{
WaitForEvent(); // Sleep until an event occurs
if (Event (RequestToSend) AND canSend )
{
GetData();
MakeFrame();
SendFrame(); //Send the data frame
canSend=false; //cannot send until ACK arrives
}
WaitForEvent(); // Sleep until an event occurs
if (Event (ArrivalNotification)) // An ACK has arrived
{
ReceiveFrame(); //Receive the ACK frame
canSend = true;
}
}
RECEIVER SIDE
while (true)
{
WaitForEvent(); // Sleep until an event occurs
if(Event(ArrivalNotification)}//Data frame arrives
{
ReceiveFrame();
ExtractData();
Deliver(data); //Deliver data to network layer
SendFrame(); //Send an ACK frame
}
}
STOP-AND-WAIT AUTOMATIC
REPEAT REQUEST
 Stop-and-Wait ARQ adds an error control mechanism to the
Stop-and-Wait Protocol.
 To detect and correct corrupted frames, we add redundancy bits
to the data frame.
 When the frame arrives, it is checked and if it is corrupted, it is
silently discarded.
 The detection of errors in this protocol is manifested by the
silence of the receiver.
 The received frame could be the correct one, or a duplicate, or
a frame out of order.
 The solution is to number the frames.
STOP-AND-WAIT ARQ
 When a data frame that is out of order is received, the frames
were either lost or duplicated.
 The receiver does not respond when there is an error.

 The sender keeps a copy of the sent frame and starts a timer.

 If the timer expires and there is no ACK for the sent frame, the
frame is resent.
 An ACK frame can also be corrupted and lost.

 It needs redundancy bits and a sequence number field.

 The sender discards a corrupted ACK frame or ignores an out-


of-order one.
SEQUENCE NUMBERS
 A field is added to the data frame to hold the sequence
number.
 The sequence numbers can wrap around.

 For example, an m bits sequence numbers ranges from 0


to 2m -1.
 Assume we have used x as a sequence number; we only
need to use x + 1 after that.
 Assume that the sender has sent the frame numbered x.
SEQUENCE NUMBERS
Three things can happen.
1. The frame arrives without error; the receiver sends an
acknowledgment.
 The ACK arrives at the sender causes to send the next frame
numbered x + 1.
2. The frame arrives without error; the receiver sends an
ACK, but corrupted or lost.
 The sender resends the duplicate frame x after the time-out.
 The receiver can recognize this fact because it expects frame
x + 1 so it retransmits the ACK again.
3. The frame is corrupted or never arrives at the receiver;
the sender resends the frame x after the time-out.
SEQUENCE NUMBERS
 The sequence numbers x and x + 1 help the receiver to
distinguish between case 1 and case 2.
 In case 1, the frame can be numbered x again because frames x
and x + 1 are acknowledged.
 In cases 2 and 3, the new frame is x + 1.

 If only x and x + 1 are needed, we can let x = 0 and x + 1 = 1.

 This means that the sequence is 0, 1, 0, 1, 0, and so on.

 The ACK numbers tell the sequence number of the next frame
expected by the receiver.
 If frame 0 has arrived without error, the receiver sends an ACK
1.
 If frame 1 has arrived without error, the receiver sends an ACK
0.
Sender
Sn = 0; // Frame 0 should be sent first
canSend = true; // Allow the first request to go
while (true)
{
WaitForEvent();
if (Event (RequestToSend) AND canSend)
{
GetData () ;
MakeFrame (Sn) ; //The seqNo is Sn
StoreFrame(Sn); // keep copy
SendFrame(Sn) ;
StartTimer();
Sn = Sn + 1;
canSend = false;
}
WaitForEvent();
if (Event (ArrivaINotification) //An ACK has arrived
{
ReceiveFrame(ackNo); //Receive the ACK frame Sn
if(not corrupted AND ackNo==Sn) //Valid ACK
{
Stoptime();
PurgeFrame(Sn-l); //Copy is not needed
canSend = true;
}
}
if (Event (TimeOut)) // The timer expired
{
StartTimer();
ResendFrame(Sn-l); //Resend a copy check
}
}
Receiver
Rn = 0; // Frame 0 expected to arrive first
while (true)
{WaitForEvent(); // Sleep until an event occurs
if(Event(Arriva1Notification)) //Data frame arrives
{ReceiveFrame();
if(corrupted(frame))
sleep();
if (seqNo == Rn) //Valid data frame
{ExtractData();
De1iverData(); //Deliver data
Rn = Rn + 1;}
SendFrame (Rn) ; //Send an ACK
} }
EFFICIENCY
 The Stop-and-Wait ARQ is very inefficient if our channel
is having large bandwidth and longer round-trip delay.
 The product of these two is called the bandwidth delay
product.
 We can think of the channel as a pipe.

 The bandwidth-delay product then is the volume of the


pipe in bits.
 The pipe is always there. If we do not use it, we are
inefficient.
 The bandwidth-delay product is a measure of the number
of bits we can send out of our system while waiting for
news from the receiver.
EXAMPLE 1
 Assume that, in a Stop-and-Wait ARQ system, the bandwidth
of the line is 1 Mbps, and 1 bit takes 20 ms to make a round
trip. What is the bandwidth-delay product? If the system data
frames are 1000 bits in length, what is the utilization
percentage of the link?
 Solution
 The bandwidth-delay product is
 1x106x20x10-3 = 20,000 bits
 The system can send 20,000 bits during the time it takes for the
data to go from the sender to the receiver and then back again.
 However, the system sends only 1000 bits.
 The link utilization is only 1000/20,000, or 5 percent.
 For a link with a high bandwidth or long delay, the use of Stop-and-
Wait ARQ wastes the capacity of the link.
EXAMPLE 2
 What is the utilization percentage of the link in the above
Example if we have a protocol that can send up to 15
frames before stopping and worrying about the
acknowledgments?
 Solution
 bandwidth-delay product is still 20,000 bits.
 It can send 15 frames or 15,000 bits during a round trip.
 The utilization is 15,000/20,000, or 75 percent.
SELECTIVE REPEAT AUTOMATIC
REPEAT REQUEST
 The Selective Repeat ARQ uses two windows:
a send window and
 a receive window.

 The receiver and sender windows are of the same size.


 If m = 4, the sequence numbers go from 0 to 15, and the
window size will be 8.
 This Protocol allows as many frames as the size of the receive
window to arrive out of order and be kept until there is a set of
in-order frames to be delivered to the network layer.
 The receiver will not deliver packets out of order to the
network layer.
WINDOW SIZES
 The size of the sender and receiver windows must be at most
one half of 2m
 For an example, we choose m = 2, which means the size of the
window is 2m /2, or 2.
 If the size of the window is 2 and all ACKs are lost, the timer
for frame 0 expires and frame 0 is resent.
 However, the window of the receiver is now expecting frame 2,
not frame 0, so this duplicate frame is correctly discarded.
 When the size of the window is 3 and all ACKs are lost, the
sender sends a duplicate of frame 0. However, the window of
the receiver expects to receive frame 0, so it accepts frame 0,
not as a duplicate, but as the first frame in the next cycle.
 This is clearly an error.
A comparison of a window size of 2 and 3
Sender Selective Repeat ARQ Algorithm:
Sw = 2m-1;
Sf = 0;
Sn = 0;
while (true)
{
WaitForEvent();
if(Event(RequestToSend)) // there is a packet to send
{
if(Sn-Sf >= Sw) //if window is full
Sleep();
GetData();
MakeFrame(Sn);
StoreFrame(Sn);
SendFrame(Sn);
Sn = Sn + 1;
StartTimer(Sn);
}
if(Event(ArrivalNotfication)) // ACK
arrive while(Sf <ackNo)
{ {
purge(Sf);
Receive(frame); // receive ACK or
StopTimer(Sf);
NAK
Sf = Sf + 1;
if(corrupted(frame)) }
}
Sleep();
}
if(FrameType == NAK)
if(nakNo between Sf and Sn) if(Event(TimeOut(t)))
{ {
resend(nakNo); StartTimer(t);
StartTimer(nakNo); SendFrame(t);
} }
if(FrameType == ACK) }
if(ackNo between Sf and Sn)
{
Receiver -Selective Repeat ARQ Algorithm:
Rn = 0;
NakSent = false;
AckNeeded = false;
Repeat(for all slots)
Marked(slot) = false;
while(true)
{
WaitForEvent();
if(Event(ArrivalNotification))
{
Receive(Frame);
if(corrupted(Frame)) && (NOT NakSent)
{
SendNAK(Rn);
NakSent = true;
Sleep();
}
if(seqNo <>Rn) && (NOT NakSent) if(AckNeeded)
{ {
SentNAK(Rn); SendAck(Rn);
NakSent = true; AckNeeded = false;
if((seqNo in window) && (!Marked(seqNo))) NakSent = false;
{ }
StoreFrame(seqNo); }
Marked(seqNo) = true; }
while(Marked(Rn)) }
{ }
DeliverData(Rn);
Purge(Rn);
Rn = Rn + 1;
AckNeeded = true;
}

Selective Repeat behaves when frame 1 is lost

You might also like