1
CS242 Computer Networks Department of Computer Science Wellesley College
Connection-oriented transport TCP
TCP 11-2
Once through the door ... ... TCP directs data to the connection's
send buffer.
Periodically TCP "grabs" chunks of data and ships it
through the network layer portal*
*TCP specifications are very laid back about when this should happen stating: "Send that data in segments at its own convenience."
TCP 11-3
The man behind the curtain o TCP pairs each chunk of
client data* with a TCP header, forming a TCP segment.
o Segments are passed down the network layer, where they are separately encapsulated within network-layer IP datagrams and sent out over the network.
*Maximum Segment Size (MSS) is determined by OS. Common values are 1,500, 536, and 512 bytes.
Old friends
TCP 11-4
We have already discussed source and
destination port numbers ...
... as well as the Internet checksum
Chunk of data being sent
2
More TCP fields
TCP 11-5
Specifies the length of TCP header in 32-bit words (4 bits)
TCP header length is variable due to option field. Typically,
options field is empty, so length is usually 20 bytes.
Two rarely used bit fields*
TCP 11-6
Indicates upper layer of sending-side has marked some
data in segment as urgent; location of last byte of urgent
data indicated by Urgent Data Point field
Receiver should pass the data to upper layer immediately
*Sequence, acknowledgement, receive window, and the remaining bit fields are coming attractions.
TCP 11-7
Sequence and acknowledgment numbers o The sequence number for a segment is a byte-stream number
of the first byte in the segment. o The acknowledgement number that host A puts in its segment
is the sequence number of the next byte host A is expecting from host B.
o TCP only acknowledges bytes up to the first missing byte in the stream (cumulative acknowledgement).
TCP 11-8
Host B sends A a packet*
Seq # Ack # 0 5000 -------------------- Bytes 0 -- 999 of Host B’s file
Host A Host B
*Host A having already sent bytes 0 through 4999 of its data to host B.
Bytes 0 -- 4999
of Host A’s file
A’s Buffer B’s Buffer
We assume the initial sequence number was zero. In truth, both sides of a TCP connection randomly choose an initial sequence number.
3
TCP 11-9
Host A sends B next 1000 bytes + ACK
Seq # Ack # 5000 1000 --------------------- Bytes 5000 -- 5999 of Host A’s file
Bytes 0 -- 999
of Host B’s file
Host A Host B
A’s Buffer
Bytes 0 -- 4999
of Host A’s file
B’s Buffer
TCP 11-10
Host B sends A next 1000 bytes & ACK
Seq # Ack # 1000 6000 -------------------- Bytes 1000 -- 1999 of Host B’s file
Host A Host B
Bytes 0 -- 5999
of Host A’s file
B’s Buffer
Bytes 0 -- 999
of Host B’s file
A’s Buffer
TCP 11-11
B’s packet 1000 -- 1999 is lost
Host A Host B
Bytes 0 -- 5999
of Host A’s file
B’s Buffer
Bytes 0 -- 999
of Host B’s file
A’s Buffer
Seq # Ack # 2000 6000 -------------------- Bytes 2000 -- 2999 of Host B’s file
Seq
#
Ack
#
1000
600
0 --
----
----
----
----
--
Byte
s 10
00 -
- 19
99
of H
ost
B’s
file
TCP 11-12
Host A responds ACK 1000
Seq # Ack # 6000 1000 --------------------- Bytes 6000 -- 6999 of Host A’s file
Bytes 0 -- 999
of Host B’s file
Host A Host B
A’s Buffer
Bytes 0 -- 5999
of Host A’s file
B’s Buffer
Cumulative Ack: TCP only acknowledges
bytes up to the first missing byte
4
TCP 11-13
Host B will resend missing packet
Bytes 0 -- 999
of Host B’s file
Host A Host B
A’s Buffer
Bytes 0 -- 6999
of Host A’s file
B’s Buffer
Seq # Ack # 1000 7000 -------------------- Bytes 1000 -- 1999 of Host B’s file
TCP 11-14
What does A do with out-of-order packets?
Host A Host B
Bytes 0 -- 5999
of Host A’s file
B’s Buffer
Bytes 0 -- 999
of Host B’s file
A’s Buffer
Seq # Ack # 2000 6000 -------------------- Bytes 2000 -- 2999 of Host B’s file
Seq
#
Ack
#
1000
600
0 --
----
----
----
----
--
Byte
s 10
00 -
- 19
99
of H
ost
B’s
file
TCP RFCs are mum on this score Two Choices: 1. The receiver immediately
discards out-of-order segments;
2. The receiver keeps the out-of-order bytes and waits for the missing bytes to fill in the gaps.
TCP 11-15
*We've seen both in the previous slide set.
TCP 11-16
Telnet application over TCP
Acknowledgement for client-to-server is
"piggybacked" in a segment carrying server-to-client
data A segment with a sequence number, but no data; hum ...
5
TCP 11-17
Timeout o Like our rdt protocol, TCP uses a timeout/retransmit
mechanism to recover from lost segments.
o Timeout should be larger than the connection’s RTT, but
how much larger and how does TCP know the RRT in the first place.
TCP 11-18
o TCP takes one SampleRTT measurement at a time EstimatedRTT = (1-α)•EstimatedRTT + α•SampleRTT
Recommended alpha is 1/8
EstimatedRTT: An exponential weighted moving average
SampleRTT only measured for segments that have been transmitted once
TCP 11-19
o TCP takes one SampleRTT measurement at a time EstimatedRTT = (1-α)•EstimatedRTT + α•SampleRTT
EstimatedRTT: An exponential weighted moving average
The average puts more weight on recent samples than on older
samples
Weight given to past SampleRTT decay
exponentially over time
TCP 11-20
Should we use EstimatedRTT as our timeout value?
6
TCP 11-21
DevRTT o DevRTT estimates of how much SampleRTT bounces around
DevRTT = (1-β)•DevRTT + β•|SampleRTT-EstimatedRTT|
Recommended beta is 1/4
TCP 11-22
Setting and managing timeout interval o TCP’s method for determining the retransmission timeout
interval is !TimeoutInterval = EstimatedRTT + 4•DevRTT!
o When a timeout event occurs, TCP retransmits the not yet ACKed segment with the smallest sequence number and sets the timeout interval to twice the previous value.*
*Why in the world would TCP do that?
TCP 11-23
TCP sender (simplified)
NextSeqNum = InitialSeqNum SendBase = InitialSeqNum loop (forever) { switch(event) event: data received from application above create TCP segment with sequence number NextSeqNum if (timer currently not running) start timer pass segment to IP NextSeqNum = NextSeqNum + length(data)
event: timer timeout retransmit not-yet-acknowledged segment with smallest sequence number start timer event: ACK received, with ACK field value of y if (y > SendBase) { SendBase = y if (there are currently not-yet-acknowledged segments) start timer } } /* end of loop forever */
SendBase-1 is last cumulatively
ACKed byte
TCP 11-24
Retransmission due to lost ACK
7
TCP 11-25
Segment 100 not retransmitted
TCP 11-26
Cumulative ACK avoids retransmission
TCP 11-27
TCP ACK generation [RFC 1122, RFC 2581] Event at Receiver Arrival of in-order segment with expected seq #. All data up to expected seq # already ACKed Arrival of in-order segment with expected seq #. One other segment has ACK pending Arrival of out-of-order segment higher-than-expect seq. # . Gap detected Arrival of segment that partially or completely fills gap
TCP Receiver action Delayed ACK. Wait up to 500ms for next segment. If no next segment, send ACK Immediately send single cumulative ACK, ACKing both in-order segments Immediately send duplicate ACK, indicating seq. # of next expected byte Immediate send ACK, provided that segment starts at lower end of gap
TCP 11-28
Fast retransmittion o Timeout periods are often
pretty long. o If the sender receives
many duplicate ACKs for the same segment, even the dimmest watt bulb knows something is up.
8
TCP 11-29
event: ACK received, with ACK field value of y if (y > SendBase) { SendBase = y if (there are currently not-yet-acknowledged segments) start timer } else { increment count of dup ACKs received for y if (count of dup ACKs received for y is 3) { resend segment with sequence number y }
Fast retransmit algorithm
a duplicate ACK for already ACKed segment
fast retransmit
TCP 11-30
Flow control o TCP provides flow-control to eliminate the possibility of the
sender overflowing the receiver’s buffer LastByteRcvd - LastByteRead ≤ RcvBuffer
o The amount of spare room in buffer is stored in RcvWindow = RcvBuffer - [LastByteRcvd -
! ! ! ! ! !LastByteRead]!
*Remember TCP is full duplex, both sides maintain distinct receive windows.
TCP 11-31
Receiver informs sender of spare room o Recall TCP is full-duplex,
so while “sender” transmits data to “receiver”, the latter is posting its vacancy rate, RcvWindow, to “sender” in every segment.
o The sending host makes sure LastByteSent - !
!LastByteAcked !
! !≤ RcvWindow.!
TCP 11-32
Avoiding gridlock o There is one minor
technical problem with this scheme: Suppose the the B’s receive buffer fills, and RcvWindow = 0 is sent to “sender” A.
o Suppose further, that the B has no additional data to the “sender”.*
o So what's the problem?
9
TCP 11-33
Mr. Ed will only speak when he has something to say ...
o ... or when the other end's RevWindow is zero.
o The solution is TCP continues to send segments with one data byte when B's receive window is zero.
o These segments will be acknowledged by the receiver and eventually ...
TCP 11-34
Setting up a TCP connection
TCP 11-35
1. Client requests TCP connection: Hi
Client host sends TCP SYN segment to server specifying
initial sequence number ...
but sends no application-layer data.
...and setting the SYN bit,
TCP 11-36
Server host receives SYN, allocates the TCP buffers and variables to the
client TCP,
and replies with SYNACK by choosing its initial sequence
number,
setting its Acknowledgment field to the client_isn+1,
and setting the ACK and SYN bits (still no data)
2. The server replies: Hi!
10
TCP 11-37
Client host receives ACKSYN, allocates its own TCP buffers and variables to
the server TCP,
replies with SYN bit set to zero since connection is
established,
and may include data
3. Client responds: Got the time?
TCP 11-38
TCP three-way handshake
seq=client_isn+1
ack=server_isn+1
TCP 11-39
Closing a connection 1. Client end system sends
FIN control segment to server.
2. Server receives FIN, replies with ACK, begins closing.
3. Server sends its own FIN. 4. Client receives FIN,
replies with ACK. Enters timed wait to respond to additional FINs.
5. Server receives ACK. Connection closed.