Mod3 TCP
Mod3 TCP
Transmission
Control Protocol
(TCP)
Types of data deliveries
PositionofTCPinTCP/IPprotocolsuite
TCP
SERVICES
Stream delivery
TCP allows the sending process to deliver data as a stream of bytes and
allows the receiving process to obtain data as a stream of bytes.
Onewaytoimplementabufferistouseacirculararray
of1bytelocations.
Figure 15.3 Sending and receiving buffers
Stream of bytes
At the sender, the buffer has
three chambers.
The white section contains
empty chambers that can be
filled by the sending process
(producer).
The colored area holds bytes that
The shaded have been sent but not yet
area contains acknowledged.
bytes to be The TCP sender keeps these
bytes in the buffer until it
sent by the receives an acknowledgment.
sending TCP.
TCP segments
TCP groups a number of bytes together into a
packet called a segment.
TCP adds a header to each segment and delivers
the segment to the IP layer for transmission.
The segments are encapsulated in an IP
datagram and transmitted.
The segments are not necessarily the same size.
In reality segments carry hundreds, if not
thousands, of bytes.
Figure 15.4 TCP segments
Segment N Segment 1
H H
Providing Reliability with Acknowledgments and Retransmissions
Send packet 1
Receive packet 1
Send ACK 1
Receive ACK 1
Send packet 2
Receive packet 2
Send ACK 2
Receive ACK 2
Packet Loss
(Timeout)
Send
packet 1
Receive
packet 1
Send ACK 1
Receive
ACK 1
ACK Delayed
Receive Receive
ACK 1 packet
(Discard)
1
Send ACK 1
Receive
(Discard)
ACK 1
TCP segment format
Header
Source port address: This is a 16-bit field that defines the
port number of the application program in the host that is
sending the segment.
20
TCP header fields
Flagbits:
URG: Urgentpointerisvalid
Ifthebitisset,thefollowingbytescontainanurgent
messageintherange:
SeqNo<=urgentmessage<=SeqNo+urgentpointer
ACK:AcknowledgementNumberisvalid
PSH:PUSHFlag
Notificationfromsendertothereceiverthatthereceiver
shouldpassalldatathatithastotheapplication.
Normallysetbysenderwhenthesendersbufferisempty
21
TCP header fields
Flagbits:
RST:Resettheconnection
Theflagcausesthereceivertoresettheconnection
ReceiverofaRSTterminatestheconnectionandindicates
higherlayerapplicationaboutthereset
1 => Tells the receiver to abort the conn.
SYN:Synchronizesequencenumbers
Sentinthefirstpacketwheninitiatingaconnection
FIN:Senderisfinishedwithsending
Usedforclosingaconnection
BothsidesofaconnectionmustsendaFIN
22
Window size. This field defines the window size
of the sending TCP in bytes.
Each byte of data has a position in the stream. However, there are
occasions in which an application program needs to send urgent
bytes, some bytes that need to be treated in a special way by the
application at the other end.
The solution is to send a segment with the URG bit set. The
sending application program tells the sending TCP that the piece
of data is urgent.
The sending TCP creates a segment and inserts the urgent data at
the beginning of the segment. The rest of the segment can contain
normal data from the buffer.
TCP segment format
Numbering System
Flow Control
Error Control
Congestion Control
Byte Number
Note
Solution
The following shows the sequence number for
each segment:
Example
ImagineaTCPconnectionistransferring
a file of 6000 bytes. The first byte is
numbered10010.Whatarethesequence
numbersforeachsegmentifdataissentin
five segments with the first four segments
carrying 1,000 bytes and the last segment
carrying2,000bytes?
The following shows the sequence number for
eachsegment:
Segment1 10,010(10,010to11,009)
Segment211,010(11,010to12,009)
Segment3 12,010(12,010to13,009)
Segment4 13,010(13,010to14,009)
Segment514,010(14,010to16,009)
Note
IMPORTANT: An acknowledgment
confirms receipt for all
unacknowledged data that has
a smaller sequence number
than given in the AckNo field
36
37
unacknowledged data with a smaller sequence number
100
ACK
o= 9 0
SeqN s
te
10 by
An acknowledgment confirms the receipt of all
TCP has cumulative acknowledgements:
o= 8 0
SeqN s
Cumulative Acknowledgements
te
10 by 0
o= 7
SeqN s
70
te ACK
10 by
o= 6 0
SeqN s
te
10 by
o= 5 0
SeqN s
te
10 by
o= 4 0
SeqN s 40
te ACK
10 by
o=30
SeqN s
te
10 by
o=20
SeqN s 20
te
10 by ACK
o=10
SeqN s 10
e
10 byt
ACK
o=0
Se q N s
te
10 by
B
A
Consider hosts A and B communicating over a TCP connection.
Assume that the initial sequence number for each of A and B is 0.
TCP
Application-layer data
header
IP
header
Frame
header
TCP payload
IP payload
Data-link layer payload
A TCP CONNECTION
TCP is connection-oriented. A connection-oriented transport
protocol establishes a virtual path between the source and
destination. All of the segments belonging to a message are
then sent over this virtual path.
40
Connection establishment
TCP transmits data in full-duplex mode.
(1) The client first sends a SYN segment, in which only SYN flag is
set. A SYN segment cannot carry data, but it consumes one
sequence number. The sequence number is called the initial
(2) After the server receives the SYN segment, it sends a SYN+ACK
sequence number (ISN).
segment back .The segment has 2 flag bits set: SYN and ACK, and
also contains receiver window size which indicates the maximal
number of bytes can be sent. A SYN+ACK segment cannot carry
data, but does consume one sequence number.
45
TCP Connection
Establishment
TCP uses a three-way handshake to open a connection:
(1) ACTIVE OPEN: Client sends a segment with
SYN bit set *
port number of client
46
Three-Way Handshake
aida.poly.edu mng.poly.edu
47
Figure 15.9 Connection establishment using three-way handshake
Passive
Active open
open
Connection
opened
Means no data !
seq: 8001 if piggybacking
Figure 15.10 Data Transfer
Send
request
Receive
Send
request
Receive
Send
request
Connection Termination
Figure 15.11 Connection termination using three-way handshake
Note
client server
SYN
SYN +ACK
ACK
data
ack
FIN
ACK
FIN
ACK
Note
X
Figure 15.18 Simultaneous open
Figure 15.19 Simultaneous close
ex
Figure 15.20 Denying a connection
Figure 15.21 Aborting a connection
TCP Module Algorithm
Transmission Control Block TCBs
Timers
Main Module
Input Processing Module
Output Processing Module
67
68
69
70
71
72
73
74
75
What is
Flow/Congestion/Error
Flow Control: Algorithms to prevent that the sender
Control ? overruns the receiver with information?
Congestion Control: Algorithms to prevent that the sender
overloads the network
--The flow control protocol must not be too slow (cant let sender send 1
byte and wait for acknowledgement)
The flow control protocol must make sure that receiver does not get
overwhelmed with data (cant let sender send all of its data without
worrying about acknowledgements)
Sender could sent everything up to and including byte 211 but this
could overflow the receiver and force retransmission
The sender must adjust itself to the number of locations available at the
receiver
window_size
Receiver Window is 7
Senderbufferandsenderwindow
Flow control is achieved if the sender creates a window with a size less than
or equal to the receiver window
Sender windows contains bytes sent and not acked and those that can be
sent
Situation can be relayed to sender and the Sender can expand its window
size
Here receiver has acked 2 bytes 203-204(expecting byte 205) and set win
size = 10
Sender has sent 205-209 and added 212-215 and set window size to 10 like
receiver
Shrinkingthesenderwindow
If the sender has already send 2 more bytes when it receives the news and
has received 3 more bytes from sending process, then the window will be
Closing Sender Window
Occurs when receiver buffer totally
full
Receiver Window size is 0
Relayed to sender who closes window
Left and right window boundaries overlap
Sender cannot send until receiver
announces a nonzero window value
InTCP,thesenderwindow
sizeistotallycontrolled
bythereceiverwindowvalue.
However,theactualwindowsize
canbesmallerifthereis
congestioninthenetwork.
SomePointsaboutTCPsSlidingWindows:
1.Thesourcedoesnothavetosenda
fullwindowsworthofdata.
2.Thesizeofthewindowcanbeincreased
ordecreasedbythedestination.
3.Thedestinationcansendan
acknowledgmentatanytime.
Flow Control: Two Approaches
Solution
The value of rwnd = 5,000 1,000 = 4,000. Host B can receive
only 4,000 bytes of data before overflowing its buffer. Host B
advertises this value in its next segment to A.
Figure 7 Sliding window
cwnd rwnd
Example
the server receives a packet with an acknowledgment value of 202 and
an rwnd of 9. The host has already sent bytes 203, 204, and 205. The
value of cwnd is still 20. Show the new window.
Solution
the size of the window has not been changed. The
acknowledgment value, 202, declares that bytes 200 and 201
have been received and the sender needs not worry about
them; the window can slide over them.
Example
the sender receives a packet with an acknowledgment value
of 206 and an rwnd of 12. The host has not sent any new
bytes. The value of cwnd is still 20. Show the new window.
Solution
The value of rwnd is less than cwnd, so the size of the
window is 12. Figure shows the new window. Note that the
window has been opened from the right by 7 and closed from
the left by 4; the size of the window has increased.
6. A window holds bytes 2001 to 5000. The next byte to be sent is 3001.
Draw a figure to show the situation of the window after the following
events.
(a) An ACK segment with the acknowledgement number 2500 and
window size advertisement 4000 is received.
(b) A segment carrying 1000 bytes is sent.
6.
Silly Window Syndrome
Serious problems can arise in the sliding
window operation when:
Sending application creates data slowly, or
Receiving applications consumes data
slowly (or both)
sender initiated silly window syndrome is
solved with Nagles algorithm
receiver initiated silly window syndrome is
solved with delayed ACKs
Silly Window Syndrome
TCP should wait until it has more data before it sends a 1-byte
segment. But how long should it wait to assemble data?
Nagles Algorithm:
1.The sending TCP sends the first piece of data it receives from
the sending application even if it is only 1 byte.
Silly Window Syndrome - What happens if the receiving TCP has a buffer
size of 1000 bytes and the sending TCP has just sent 1000 bytes. The
receiving buffer is now full so the receiver tells the sender to stop (window
size = 0).
The receiver now reads 1 byte of data, processes it, and sends a window
size of 1 (because now there is one space in the input buffer). The sender
gets the window size and sends 1 byte. This procedure continues.
Clarks Solution - Acknowledge receipt right away, but dont change the
window size until you have at least half the buffer space available.
Or, delay the ack until there is a decent amount of buffer space available.
105
ERROR CONTROL
TCP provides reliability using error control, which detects corrupted,
lost, out-of-order, and duplicated segments.
107
Figure 12.25 Normal operation
108
TCP Error Control
Rule 4: When a
segment arrives with a
sequence number
higher than expected,
the receiver
immediately sends an
ACK announcing the
sequence number it
expected. This leads to
fast retransmission
111
Retransmission
The heart of the error control mechanism is the
retransmission of segments.
TCP maintains a Retransmission Timer for each
connection
The timer is started during a transmission. A timeout
causes a retransmission
When a segment is sent, it is stored in a queue until it
is acknowledged.
When the retransmission timer expires or when the
sender receives three duplicate ACKs for the first
segment in the queue, that segment is retransmitted.
TCP Congestion Control
In the slow start phase, the sender starts with a slow rate of
transmission, but increases the rate rapidly to reach a
threshold.
1
cwnd TCP has a slow
RTT start phase. In
2 this phase, the
cwnd size of the
congestion
RTT window
4 increases
cwnd exponentially
until it reaches a
threshold.
RTT
8
cwnd
Slow Start Example
CWND is incremented by 1 for each received ACK (exponential
increase)
In practice: in Slow Start, the CWND doubles in each
RTT (Round Trip Time)
for each ACK, the cwnd is increased by only 1 MSS. Hence, if three
segments are acknowledged accummlatively, the size of the cwnd
increases by only 1 MSS, not 3 MSS
Why Call it Slow Start ?
The original version of TCP suggested that the
sender transmit as much as the Advertised Window
permitted.
Routers may not be able to cope with this burst of
transmissions.
Slow start is slower than the above version --
ensures that a transmission burst does not happen
at once.
Figure 17 Congestion avoidance, additive increase
After the cwnd reaches the threshold, the size of the congestion
window increases additively until the congestion is detected
Start cwnd=1
After 1 RTT cwnd=2
After 2 RTTs cwnd =3
.
ssthres
h
Roundtrip times
Congestion Detection: Multiplicative Decrease
If congestion occurs, the congestion window size must be
decreased.
The only way a sender can guess that congestion has
occurred is the need to retransmit a segment. This is a
major assumption made by TCP.
Retransmission is needed to recover a missing packet which
is assumed to have been dropped (i.e., lost) by a router that
had so many incoming packets, that had to drop the missing
segment
Congestion Detection: Multiplicative
Decrease
c. It starts the congestion avoidance phase.
TCP Congestion policy summary
Figure 15.37 Congestion example