3. THE DATA LINK LAYER. From: Computer Networks, 3rd ed. by Andrew S.
Tanenbaum,. 1996 Prentice Hall. Page 2. 4. 3. 2. 1. 4. 3. 2. 1. 4. 3. 2. 1. 4. 3. 2. 1.
3 THE DATA LINK LAYER
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Host 1
Host 2
4
3
2
Virtual data path
1
Host 1
Host 2
4
4
4
3
3
3
2
2
2
1
1
1 Actual data path
(a)
(b)
Fig. 3-1. (a) Virtual communication. (b) Actual communication.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Router
Data link layer process
2
2
Routing process
3
2
2 Data link protocol
Frames Packets here here
Transmission line to a router
Fig. 3-2. Placement of the data link protocol.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
3
One character
Character count
(a) 5
1
2
3
4
5
Frame 1 5 characters
6
7
8
9
8
0
Frame 2 5 characters
1
2
3 4
5
6
8
7
Frame 3 8 characters
8
9
0
1
2
3
2
3
Frame 4 8 characters
Error
(b) 5
1
2
3
Frame 1
4
7
6
7
8
9
Frame 2 (Wrong)
8
0
1
2
3
4 5
6
8
7
8
9
0
1
Now a character count
Fig. 3-3. A character stream. (a) Without errors. (b) With one error.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
DLE
STX
A
DLE
B
DLE
ETX
(a)
DLE
STX
A
DLE
DLE
B
DLE
ETX
(b)
DLE
STX
A
DLE
Stuffed DLE
B
DLE
ETX
(c)
Fig. 3-4. (a) Data sent by the network layer. (b) Data after being character stuffed by the data link layer. (c) Data passed to the network layer on the receiving side.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
(a) 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0
(b) 0 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 0 0 1 0
Stuffed bits
(c) 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0
Fig. 3-5. Bit stuffing. (a) The original data. (b) The data as they appear on the line. (c) The data as they are stored in the receiver’s memory after destuffing.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Char.
H
a m m i n g
c o d e
ASCII
Check bits
1001000 00110010000 10111001001 1100001 11101010101 1101101 11101010101 1101101 01101011001 1101001 01101010110 1101110 11111001111 1100111 10011000000 0100000 11111000011 1100011 00101011111 1101111 11111001100 1100100 00111000101 1100101 Order of bit transmission
Fig. 3-6. Use of a Hamming code to correct burst errors.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Frame : 1101011011 Generator: 1 0 0 1 1 Message after appending 4 zero bits: 1 1 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 1 0 1 0 10011
1 1 0 1 0 1 1 0 1 1 0 0 0 0 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 1 1 0 1 0 0 1 1 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 1 0 0 1 1 0 1 1 1 0 0 0 0 0 0 1 1 1 0
Remainder
Transmitted frame: 1 1 0 1 0 1 1 0 1 1 1 1 1 0
Fig. 3-7. Calculation of the polynomial code checksum.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
#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 { frame kind kind; seq nr seq; seq nr ack; packet info; } frame;
/* frames are transported in this layer */ /* what kind of a frame is it? */ /* sequence number */ /* acknowledgement number */ /* the network layer packet */
/* 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); /* Go get an inbound frame from the physical layer and copy it to r. */ void from physical layer(frame *r); /* Pass the frame to the physical layer for transmission. */ void to physical layer(frame *s); /* Start the clock running and enable the timeout event. */ void start timer(seq nr k); /* Stop the clock and disable the timeout event. */ void stop timer(seq nr k); /* Start an auxiliary timer and enable the ack timeout event. */ void start ack timer(void); /* Stop the auxiliary timer and disable the ack timeout event. */ void stop ack timer(void); /* Allow the network layer to cause a network layer ready event. */ void enable network layer(void); /* Forbid the network layer from causing a network layer ready event. */ void disable network layer(void); /* Macro inc is expanded in-line: Increment k circularly. */ #define inc(k) if (k < MAX SEQ) k = k + 1; else k = 0 From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Fig. 3-8. Some definitions needed in the protocols to follow.
/* 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 fast. Consequently, the sender just sits in a loop pumping data out onto the line as fast as it can. */ typedef enum {frame arrival} event type; #include "protocol.h" void sender1(void) { frame s; packet buffer;
}
/* buffer for an outbound frame */ /* buffer for an outbound packet */
while (true) { from network layer(&buffer); /* go get something to send */ s.info = buffer; /* copy it into s for transmission */ to physical layer(&s); /* send it on its way */ } /* Tomorrow, and tomorrow, and tomorrow, Creeps in this petty pace from day to day To the last syllable of recorded time - Macbeth, V, v */
void receiver1(void) { frame r; event type event;
/* filled in by wait, but not used here */
while (true) { wait for event(&event); /* only possibility is frame arrival */ from physical layer(&r); /* go get the inbound frame */ to network layer(&r.info); /* pass the data to the network layer */ } }
Fig. 3-9. An unrestricted simplex protocol.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
/* 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. */ typedef enum {frame arrival} event type; #include "protocol.h" void sender2(void) { frame s; packet buffer; event type event;
/* buffer for an outbound frame */ /* buffer for an outbound packet */ /* frame arrival is the only possibility */
while (true) { from network layer(&buffer); /* go get something to send */ s.info = buffer; /* copy it into s for transmission */ to physical layer(&s); /* bye bye little frame */ wait for event(&event); /* do not proceed until given the go ahead */ } } void receiver2(void) { frame r, s; /* buffers for frames */ event type event; /* frame arrival is the only possibility */ while (true) { wait for event(&event); /* only possibility is frame arrival */ from physical layer(&r); /* go get the inbound frame */ to network layer(&r.info); /* pass the data to the network layer */ to physical layer(&s); /* send a dummy frame to awaken sender */ } }
Fig. 3-10. A simplex stop-and-wait protocol.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
/* Protocol 3 (par) allows unidirectional data flow over an unreliable channel. */ #define MAX SEQ 1 /* must be 1 for protocol 3 */ typedef enum {frame arrival, cksum err, timeout} event type; #include "protocol.h" void sender3(void) { seq nr next frame to send; frame s; packet buffer; event type event;
/* seq number of next outgoing frame */ /* scratch variable */ /* buffer for an outbound packet */
next frame to send = 0; /* initialize outbound sequence numbers */ from network layer(&buffer); /* fetch first packet */ while (true) { s.info = buffer; /* construct a frame for transmission */ s.seq = next frame to send; /* insert sequence number in frame */ to physical layer(&s); /* send it on its way */ start timer(s.seq); /* if (answer takes too long, time out */ wait for event(&event); /* frame arrival, cksum err, timeout */ if (event == frame arrival) { from physical layer(&s); /* get the acknowledgement */ if (s.ack == next frame to send) { from network layer(&buffer); /* get the next one to send */ inc(next frame to send); /* invert next frame to send */ } } } } void receiver3(void) { seq nr frame expected; frame r, s; event type event; frame expected = 0; while (true) { wait for event(&event); /* possibilities: frame arrival, cksum err */ if (event == frame arrival) { /* a valid frame has arrived. */ from physical layer(&r); /* go get the newly arrived frame */ if (r.seq == frame expected) { /* this is what we have been waiting for. */ to network layer(&r.info); /* pass the data to the network layer */ inc(frame expected); /* next time expect the other sequence nr */ } s.ack = 1 − frame expected; /* tell which frame is being acked */ to physical layer(&s); /* none of the fields are used */ } } }
Fig. 3-11. A positive acknowledgement/retransmission protocol. From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Sender
7
0
7
0
7
0
7
0
6
1
6
1
6
1
6
1
5
2
5
2
5
2
5
2
4
3
4
3
4
3
4
3
7
0
7
0
7
0
7
0
Receiver
6
1
6
1
6
1
6
1
5
2
5
2
5
2
5
2
4
3 (a)
4
3 (b)
4
3 (c)
4
3 (d)
Fig. 3-12. 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.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
/* Protocol 4 (sliding window) is bidirectional and is more robust than protocol 3. */ /* must be 1 for protocol 4 */ #define MAX SEQ 1 typedef enum {frame arrival, cksum err, timeout} event type; #include "protocol.h" void protocol4 (void) { seq nr next frame to send; /* 0 or 1 only */ seq nr frame expected; /* 0 or 1 only */ frame r, s; /* scratch variables */ packet buffer; /* current packet being sent */ event type event; next frame to send = 0; /* next frame on the outbound stream */ frame expected = 0; /* number of frame arriving frame expected */ from network layer(&buffer); /* fetch a packet from the network layer */ s.info = buffer; /* prepare to send the initial frame */ s.seq = next frame to send; /* insert sequence number into frame */ s.ack = 1 − frame expected; /* piggybacked ack */ to physical layer(&s); /* transmit the frame */ /* start the timer running */ start timer(s.seq); while (true) { wait for event(&event); /* frame arrival, cksum err, or timeout */ if (event == frame arrival) { /* a frame has arrived undamaged. */ from physical layer(&r); /* go get it */ if (r.seq == frame expected) { /* Handle inbound frame stream. */ to network layer(&r.info); /* pass packet to network layer */ inc(frame expected); /* invert sequence number expected next */ } if (r.ack == next frame to send) { /* handle outbound frame stream. */ from network layer(&buffer); /* fetch new pkt from network layer */ inc(next frame to send); /* invert sender’s sequence number */ } } s.info = buffer; s.seq = next frame to send; s.ack = 1 − frame expected; to physical layer(&s); start timer(s.seq); }
/* construct outbound frame */ /* insert sequence number into it */ /* seq number of last received frame */ /* transmit a frame */ /* start the timer running */
}
Fig. 3-13. A 1-bit sliding window protocol. From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
A sends (0, 1, A0) B gets (0, 1, A0)* B sends (0, 0, B0)
B sends (0, 1, B0) B gets (0, 1, A0)* B sends (0, 0, B0)
A gets (0, 0, B0)* A sends (1, 0, A1)
A gets (0, 1, B0)*
A sends (0, 0, A0)
B gets (1, 0, A1)* B sends (1, 1, B1)
B gets (0, 0, A0) B sends (1, 0, B1)
A gets (1, 1, B1)* A sends (0, 1, A2)
A gets (0, 0, B0) A sends (1, 0, A1)
B gets (0, 1, A2)* B sends (0, 0, B2) A gets (0, 0, B2)* A sends (1, 0, A3)
B gets (1, 0, A1)* B sends (1, 1, B1)
A gets (1, 0, B1)* A sends (1, 1, A1)
B gets (1, 0, A3)* B sends (1, 1, B3)
(a)
A sends (0, 1, A0)
Time
B gets (1, 1, A1) B sends (0, 1, B2)
(b)
Fig. 3-14. Two scenarios for protocol 4. The notation is (seq, ack, packet number). An asterisk indicates where a network layer accepts a packet.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Timeout interval 1
2
3
Ac k1
4
Ac
k0
0
0
1
5
6
7
8
2
3
4
5
E
Error
D
D
D
D
D
D
Ac
2
k
2
6
Ac
3
k
7
3
k
Ac
4
4
5
8
Ac
k
5
9
6
Ac
k
7
6
10
Ac
k
7
8
Frames discarded by data link layer Time (a) Timeout interval
0
1
2
3
0
Ac
k
0
1 Error
4
k
Ac
E
5
6
1
3
k
Ac
4
1
7
k
Ac
5
1
8
k
Ac
6
1
2
k
Ac
1
9
7
k
Ac
8
Buffered by data link layer
1
10
k
Ac
2
11
1
Ac
k
9
8
Ac
k
12
13
9
10
10
k Ac
11
14
12
Packets 2-8 passed to network layer
(b)
Fig. 3-15. (a) Effect of an error when the receiver window size is 1. (b) Effect of an error when the receiver window size is large.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
/* Protocol 5 (pipelining) allows multiple outstanding frames. The sender may transmit up to MAX SEQ frames without waiting for an ack. In addition, unlike the previous protocols, the network layer is not assumed to have a new packet all the time. Instead, the network layer causes a network layer ready event when there is a packet to send. */ #define MAX SEQ 7 /* should be 2ˆn − 1 */ typedef enum {frame arrival, cksum err, timeout, network layer ready} event type; #include "protocol.h" static boolean between(seq nr a, seq nr b, seq nr c) { /* Return true if (a 0
16
8
01111110
Address
Control
Data
Checksum
01111110
Fig. 3-24. Frame format for bit-oriented protocols.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Bits !
(a)
!
!
1
0
(b)
1
(c)
1
1
Seq
P/F
Next
P/F
Next
P/F
Modifier
Type
0
1
3
Type
3
Fig. 3-25. Control field of (a) an information frame, (b) a supervisory frame, (c) an unnumbered frame.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Internet provider's office
User's home
Modems
PC Client process using TCP/IP Dial-up telephone line
Modem TCP/IP connection using SLIP or PPP Router
Routing process
Fig. 3-26. A home personal computer acting as an Internet host.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
Bytes
1
1
1
1 or 2
Variable
2 or 4
1
Flag 01111110
Address 11111111
Control 00000011
Protocol
Payload
Checksum
Flag 01111110
Fig. 3-27. The PPP full frame format for unnumbered mode operation.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
&
' Carrier detected ( $
Both sides agree on options
Authentication ) successful %
Establish
Authenticate
Failed +
Dead
" &
' Carrier dropped
Network
Failed
Terminate
#
Open
Done *
NCP configuration
Fig. 3-28. A simplified phase diagram for bringing a line up and down.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
/, ,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.,-,-,-,-,-,-,-,-,-/ ,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / / / Direction / / Name Description /, ,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.,-,-,-,-,-,-,-,-,-/ ,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / / Configure-request / I → R / List of proposed options and values / /, ,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.,-,-,-,-,-,-,-,-,-/ ,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / /, ,-Configure-ack ,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.I ,-← ,-,-R ,-,-,-,-,-,-/ ,-All ,-,.,-options ,-,-,-,-,-,-,-are ,-,-,-,.accepted ,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / / / / / /, ,-Configure-nak ,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.I ,-← ,-,-R ,-,-,-,-,-,-/ ,-Some ,-,.,-,-,-,-options ,-,-,-,-,-,-,-are ,.,-,-,-not ,-,-,-accepted ,-,-,-,-,-,.,-,-,-,-,-,-,-, / / Configure-reject / I ← R / / ,/ ,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.,-,-,-,-,-,-,-,-,-/ ,-Some ,-,.,-,-,-,-options ,-,-,-,-,-,-,-are ,.,-,-not ,-,-,-,-negotiable ,-,-,-,-,-,.,-,-,-,-,-,-,-, / / Terminate-request/ I → R / Request to shut the line down / /, ,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.,-,-,-,-,-,-,-,-,-/ ,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / /, ,-Terminate-ack ,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.I ,-← ,-,-R ,-,-,-,-,-,-/ ,-OK, ,-,.,-,-line ,-,-,-,-shut ,-,-,-,-down ,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / / / / / /, ,-Code-reject ,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.I ,-← ,-,-R ,-,-,-,-,-,-/ ,-Unknown ,-,.,-,-,-,-,-,-,-request ,-,-,-,-,.,-,-received ,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / / Protocol-reject / I←R / Unknown protocol requested / /, ,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.,-,-,-,-,-,-,-,-,-/ ,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / / Echo-request / I→R / Please send this frame back / /, ,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.,-,-,-,-,-,-,-,-,-/ ,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / /, ,-Echo-reply ,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-/ ,-,.I ,-← ,-,-R ,-,-,-,-,-,-/ ,-Here ,-,.,-,-,-is ,-,-the ,-,-,-,-frame ,-,-,.,-,-back ,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-, / // // // // Discard-request I → R Just discard this frame (for testing) , ,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,-,-,-,-,.,-,-,-,-,-,-,-,
Fig. 3-29. The LCP packet types.
From: Computer Networks, 3rd ed. by Andrew S. Tanenbaum,
1996 Prentice Hall
;
Bit-by-bit : check
1
2
HEC
Correct
3 4
d ete cte d
HUNT 7
Incorre5 c6 t
HEC
0
PRESYNCH 9
8 detected