You are on page 1of 48

Data Link Layer

Data Link Layer Design Issues


It has various functions:
• Providing well defined interface to the network layer
• Dealing with transmission errors
• Regulating the flow of data
• The DLL takes the packets from the network layer
and encapsulates them into frames.
Services to the Network Layer
• Unacknowledged Connectionless Service
• Acknowledged Connectionless Service
• Acknowledged Connection Oriented Service
Framing
• The physical layer accepts a raw bit stream and
delivers it to the DLL.
• It is possible that the number of bits received
may be less than, equal to or more than the ones
transmitted.
• It is also possible that some of the bit values may
have changed.
• Hence the DLL is responsible for detecting and
correcting such errors.
• Also the DLL breaks up the bit stream it receives
from the physical layer into frames.
Framing
• There are four methods to identify the start of
a new frame:
1. Byte count
2. Flag bytes with byte stuffing
3. Flag bits with bit stuffing
4. Physical Layer coding violations
Byte Count (Character Count)

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


Flag Bytes With Byte Stuffing

(a) A frame delimited by flag bytes.


(b) Four examples of byte sequences before and after stuffing.
Flag Bits With 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.
Error Control
• For ensuring reliable delivery of data:
1. Positive/Negative Acknowledgement
2. Timers
3. Sequence Numbers
Flow Control
• What happens when a sender wants to transmit
frames faster than the receiver can accept?
• In such situations flow control is required. Two
approaches are used:
1. Feedback based flow control: Receiver sends back
information to the sender giving it permission to
send more data.
2. Rate based flow control: Built-in mechanism that
limits the transmission rate at sender.
Error Detection and Correction
• Error detection is the detection of errors
caused by noise or other impairments during
transmission from the transmitter to the
receiver.
• Error Correction is the detection of errors and
reconstruction of the original, error free data.
• Two types of errors:
1. Single bit error
2. Burst Error
Error Detection and Correction
• Two ways to deal with errors:
1. Adding enough information for the receiver to
know what the transmitted data must be.
2. Adding enough information for the receiver to
know simply that an error has occurred.
• The former strategy uses error correcting code. It
is also known as FEC (Forward Error Correction).
• The latter one is known as error detecting code.
Error Correcting Codes
• Different error correcting codes are:
1. Hamming Code
2. Binary Convolution Code
3. Reed Solomon Code
4. Low density Parity Check Code
Hamming Code
• A frame consists of m data(i.e. message) bits
and r redundant(i.e. check) bits.
• Total length of the block be n (i.e. n=m+r)
• An n bit unit containing data and check bits is
referred to as an n-bit codeword. Also known
as (n,m) hamming code.
• Note: The r redundant bits are also known as
parity bits.
Parity Bits
• To detect errors in the data communication and
processing, an additional bit is sometimes added
to a binary codeword to define its parity.
• A parity bit is an extra bit included to make the
total number of 1s in the resulting code either
even/odd.
• Two types of parity bit:
1. Even Parity
2. Odd Parity
Hamming Code
• The code rate(or rate) is the fraction of the
code word that carries information i.e. not
redundant.
• Find the number of bits by which these two
code words differ:10001001,10110001.
• The number of bits by which the two code
words differ is called Hamming Distance.
• Parity bits are always power of 2.
Hamming Code
• Ex1: For (11,7) Hamming Code, we have
Msg=1000001. Find the redundant bits.
Hamming Code
• Ex1: For (11,7) Hamming Code, we have Msg=1000001.
Find the redundant bits.
• Ex2: Find the parity bits and codeword to be sent for
the Msg=0110101.
• Ex3: For Hamming code (6,3), find the parity bits and
codeword to be sent for the messages:
Msg1=000
Msg2=001
Msg3=010
Msg4=011
Msg5=111
Convolutional Codes
• Also known as NASA Convolutional codes.
• Encoder processes a sequence of input bits and produces
a sequence of output bits.
• Output depends on the current and previous input bits
since the encoder has memory.
• The number of previous bits on which the output
depends is called the constraint length.
• Convolutional codes are specified in terms of their rate
and constraint length.
• It is not a block code.
• These codes are used in GSM Phones, 802.11 and
satellite communication.
Convolutional Codes
• The fig. below shows the NASA convolutional codes for
code rate=1/2 and constraint length, k=7.
• Each input bit produces two output bits i.e. 1 input bit=2
output bits.
• Each time a bit is given as input the values in the registers
are shifted to the right.
Reed Solomon Code
• They are linear block codes which operate on
block codes(m bit symbols) rather than bits.
• Used for satellite communication as well as
on CDs/DVDs and Blu-ray discs.
• In presence of 32 redundant symbols, it can
correct upto 16 symbol errors.
• Reed solomon codes are generally used in
combination with convolutional codes.
Low Density Parity Check(LDPC) Code
• They are linear block codes.
• Excellent ECC.
• Used in digital video broadcasting, 802.11
and 10Gbps Ethernet.
Error Detecting Codes
1. Parity
2. Checksum
3. Cyclic Redundancy Check
Parity Bits
• To detect errors in the data communication and
processing, an additional bit is sometimes added
to a binary codeword to define its parity.
• A parity bit is an extra bit included to make the
total number of 1s in the resulting code either
even/odd.
• Two types of parity bit:
1. Even Parity
2. Odd Parity
Parity Bit
• Single parity bit can only detect a single bit error
in the block.
• It can not provide protection against burst errors.
• In order to do so, we use the technique of
Interleaving.
• Interleaving is a general technique to convert a
code that detects (or corrects) isolated errors into
a code that detects (or corrects) burst errors.
Parity Bit
• Compute parity bit for all n columns and send all
data bits as k rows, sending the rows from top to
bottom and bits in a row from left to right.

Interleaving of parity bits to detect a burst error.


Checksum
• It means a group of parity bits associated with a
message.
• Checksum can also be calculated based on the
data bits and is usually placed at the end of the
message.
• Checksum is generally the complement of sum
function.
• Errors may be detected by summing the entire
received codeword, both data bits and checksum.
Cyclic Redundancy Check (CRC) or
Polynomial Code

Example calculation of the CRC.


Elementary Data Link Protocols
• Few assumptions to be made before beginning with the protocols:
• The data structures used here are:
1. Boolean: can take True/False values.
2. Seq_nr: integer used to number the frames.
3. Packet: Unit of Info exchanged between network and data link layer.
4. A frame is composed of four fields:
kind - tells if the frame contains any data or not.
Seq & Ack – for sequence number and Ack respectively.
Info - contains a single packet.
The kind, Seq, Ack and Info fields are collectively called frame header.
Elementary Data Link Protocols
#define MAX_PKT 1024 /* determines packet size in bytes */
typedef enum {false, true} boolean; /* boolean type */
typedef unsigned int seq_nr; /* sequence or ack numbers */
typedef struct {unsigned char data[MAX_PKT];} packet;/* packet definition */
typedef enum {data, ack, nak} frame_kind; /* frame_kind definition */

typedef struct { /* frames are transported in this layer */


frame_kind kind; /* what kind of frame is it? */
seq_nr seq; /* sequence number */
seq_nr ack; /* acknowledgement number */
packet info; /* the network layer packet */
} frame;

/* Wait for an event to happen; return its type in event. */


void wait_for_event(event_type *event);

/* Fetch a packet from the network layer for transmission on the channel. */
void from_network_layer(packet *p);

/* Deliver information from an inbound frame to the network layer. */


void to_network_layer(packet *p);
Elementary Data Link Protocols
void from_physical_layer(frame *r); /* Go get an inbound frame from the physical layer and copy it to r. */

void to_physical_layer(frame *s); /* Pass the frame to the physical layer for transmission. */

void start_timer(seq_nr k); /* Start the clock running and enable the timeout event. */

void stop_timer(seq_nr k); /* Stop the clock and disable the timeout event. */

void start_ack_timer(void); /* Start an auxiliary timer and enable the ack_timeout event. */

void stop_ack_timer(void); /* Stop the auxiliary timer and disable the ack_timeout event. */
A Utopian Simplex Protocol
Assume:
• Data Transmission is in one direction only(Sensed
to Receiver)
• Communication channel is error free.
• Receiver processes all inputs quickly and infinite
buffer space is available.
• No loss of frames/no error/flow control.
• No Sequence number or Ack number are used
here.
A Utopian Simplex Protocol
/* Protocol 1 (Utopia) provides for data transmission in one direction only, from sender to receiver. The
communication channel is assumed to be error free and the receiver is assumed to be able to process
all the input infinitely quickly. Consequently, the sender just sits in a loop pumping data out onto the
line as fast as it can. */

void sender1(void) { void receiver1(void) {


frame s; /* buffer for an outbound frame frame r;
*/ event_type event; /* filled in
packet buffer; /* buffer for an outbound by wait, but not used here */
packet */
while (true) {
while (true) { wait_for_event(&event); /*
from_network_layer(&buffer); /* go get only possibility is frame_arrival */
something to send */ from_physical_layer(&r); /*
s.info = buffer; /* copy it go get the inbound frame */
into s for transmission */ to_network_layer(&r.info); /* pass
to_physical_layer(&s); /* send it on the data to the network layer */
its way */ }
} }
}
A Simplex Stop and Wait Protocol for
an Error Free Channel
• Deals with the problems of preventing the
sender from flooding the receiver with frames
faster than the receiver can process.
• Assumes:
1. Communication channel is half duplex.
2. Channel will be error free.
3. Receiver has only finite buffer capacity and
finite processing speed.
/* Protocol 2 (Stop-and-wait) also provides for a one-directional flow of data from sender to
receiver. The communication channel is once again assumed to be error free, as in protocol 1.
However, this time the receiver has only a finite buffer capacity and a finite processing speed, so the
protocol must explicitly prevent the sender from flooding the receiver with data faster than it can be
handled. */

void sender2(void) { void receiver2(void) {


frame s; /* buffer for an outbound frame frame r, s; /* buffers for frames */
*/ event_type event; /* frame_arrival is the
packet buffer; /* buffer for an outbound only possibility */
packet */
event_type event; /* frame_arrival is the while (true) {
only possibility */ wait_for_event(&event); /* only
possibility is frame_arrival */
while (true) { from_physical_layer(&r); /* go get the
from_network_layer(&buffer); /* go get inbound frame */
something to send */ to_network_layer(&r.info); /* pass the
s.info = buffer; /* copy it into s for data to the network layer */
transmission */ to_physical_layer(&s); /* send a
to_physical_layer(&s); /* bye-bye little dummy frame to awaken sender */
frame */ }
wait_for_event(&event); /* do not }
proceed until given the go ahead */
}
}
A Simplex Stop and Wait Protocol for
a Noisy Channel
Consider that the frames may be damaged or lost then:
1. If the frame is damaged then the receiver hardware will
detect this when it computes the checksum.
2. If the frame is damaged such that the checksum is
correct, this protocol fails.(Least chances)
• Goal of the DLL is to have error free communication
between network layer processes.
• When A sends a packet to B, if the packet gets lost on the
way then B will not be able to know about it. So we use
timers, if the packet is not delivered before the timer
goes off then the packet is resend.
A Simplex Stop and Wait Protocol for
a Noisy Channel
• Now consider A sends a frame to B. After receiving it
correctly B sends back an Ack to A. But the Ack gets lost
and the timer goes off at A. So it resends the frame again
to B.
• Duplicate frames keep arriving at B and is processed
accordingly. Again when the Ack is sent, it gets lost on the
way.
• Hence Sequence number is needed so that the receiver
can distinguish between a new frame and a
retransmission.
• Protocols in which the sender waits for a positive Ack
before advancing to the next data item are often called
ARQ (Automatic Repeat Request) or PAR (Positive Ack
with Retransmission).
/* Protocol 3 (PAR) allows unidirectional data flow over an unreliable channel. */
void sender3(void) { void receiver3(void) {
seq_nr next_frame_to_send; /* seq number of next seq_nr frame_expected;
outgoing frame */ frame r, s;
frame s; /* scratch variable */ event_type event;
packet buffer; /* buffer for an outbound packet */ frame_expected = 0;
event_type event; while (true) {
next_frame_to_send = 0; /* initialize outbound wait_for_event(&event); /* possibilities:
sequence numbers */ frame_arrival, cksum_err */
from_network_layer(&buffer); if (event == frame_arrival) { /* a valid frame has
while (true) { arrived */
s.info = buffer; /* construct a frame for transmission */ from_physical_layer(&r); /* go get the newly
s.seq = next_frame_to_send; /* insert sequence arrived frame */
number in frame */ if (r.seq == frame_expected) { /* this is what
to_physical_layer(&s); we have been waiting for */
start_timer(s.seq); /* if answer takes too long, time to_network_layer(&r.info); /* pass the data to
out */ the network layer */
wait_for_event(&event); /* frame_arrival, cksum_err, inc(frame_expected); /* next time expect the
timeout */ other sequence nr */
if (event == frame_arrival) { }
from_physical_layer(&s); /* get the ack*/ s.ack = 1 - frame_expected; /* tell which
if (s.ack == next_frame_to_send) { frame is being acked */
stop_timer(s.ack); /* turn the timer off */ to_physical_layer(&s); /* send ack */
from_network_layer(&buffer); }}}
}}}}
Sliding Window Protocols
• Different links for data in both directions can
result into waste of resources.
• So use one link for data transmission in both the
directions.
• Hence data frames are mixed with the Ack frames
from A to B.
• By looking at the kind field in the header, the
receiver can tell whether it is a data frame or not.
• Piggybacking is the technique of temporarily
delaying outgoing ack so that they can be hooked
onto next outgoing frame.
Sliding Window Protocols
Advantages of Piggybacking:
• Better use of available bandwidth.
• Piggyback field only costs 1 bit in the frame
header whereas a separate frame needs a
header, Ack and checksum.
Limitations of Piggybacking:
• How long should receiver take to piggyback Ack if
a timeout occurs at sender?
• How long should the DLL wait for the packet on
which to piggyback the Ack?
Sliding Window Protocols
• Three types:
1. One bit sliding window protocol
2. Go-back-N
3. Selective Repeat
• Outgoing frames contain a sequence no. ranging from 0 to 2m-1.
• Sender maintains a set of sequence nos. corresponding to frames it is
permitted to send. These frames are said to fall within the sending
window (next_frame_to_send).
• Receiver also maintains a set of sequence nos. corresponding to frames
it is permitted to receive. These frames are said to fall within the
receiving window (frame_expected).
• The size of the sender window and receiver window may be different.
Sliding Window Protocol

A sliding window of size 1, with a 3-bit sequence number.


(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.
A One-Bit Sliding Window Protocol

Two scenarios for protocol 4. (a) Normal case.


(b) Abnormal case. The notation is (seq, ack, packet
number).
A Protocol using Go-Back-N
• Consider a 50 Mbps satellite channel with 500ms RTT. Lets
assume we are using protocol 4 to send 1000 bit frames.
• At t=0ms, sender sends frame.
• At t=20ms, sender has completely sent frame.
• At t=270ms, frame arrives at receiver.
• At t= 520ms, Ack arrives back at sender.
• It shows that the sender was blocked 96% of the time.
• Result is Waste of Bandwidth for sending a short frame.
• This problem arises because the sender waits for Ack before
sending another frame.
• If we allow sender to transmit more than 1 frames before
blocking, we get better efficiency.
A Protocol using Go-Back-N
• Let us find the value of w i.e. maximum number of
frames a sender can transmit before getting blocked.
• If Bandwidth=50 kbps and one way transit time=250ms
then
• Bandwidth-Delay Product=B*D =50*250*10-3
• Hence, Bandwidth-Delay Product=12.5 kbits
• Sender’s Max. Window size=2BD+1 =26 frames
• Link Utilization: It is the fraction of time that the
sender is not blocked.
• Link Utilization <= (w / (1+2BD) )
A Protocol using Go-Back-N
• A phenomenon of keeping multiple frames
outstanding is known as Pipelining.
• If a frame in the middle of a long stream gets
damaged, then the receiver may accept several
frames succeeding it even before the sender finds
out something is wrong.
• If a damaged frame arrives at the receiver, discard it
but what to do with the correct frames following it?
• Two approaches:
1. Go-Back-N
2. Selective Repeat
Go back N and Selective Repeat

Pipelining and error recovery. Effect on an error when (a) Receiver’s


window size is 1. (b) Receiver’s window size is large.
Go back N and Selective Repeat
• Cumulative Acknowledgement: When an ack
comes in for frame n, frames (n-1), (n-2) and
so on are also automatically acknowledged.

You might also like