cs 356: introduction to computer networks lecture 19: … · 2019. 4. 26. · • two events...

53
CS 356: Introduction to Computer Networks Lecture 19: Transmission Control Protocol (TCP) Chap. 5.2 Xiaowei Yang [email protected]

Upload: others

Post on 11-Aug-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

CS 356: Introduction to Computer Networks

Lecture 19: Transmission Control

Protocol (TCP) Chap. 5.2

Xiaowei Yang [email protected]

Page 2: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Overview

•  TCP – Connection management – Flow control – When to transmit a segment – Adaptive retransmission – TCP options

•  Modern extensions

Page 3: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Transmission Control Protocol •  Connection-oriented protocol •  Provides a reliable unicast end-to-end byte stream

over an unreliable internetwork

TCP

IP Internetwork

Byt

e S

tream

Byt

e S

tream

TCP

Page 4: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

TCP performance is critical to business

Source: http://www.webperformancetoday.com/2011/11/23/case-study-slow-page-load-mobile-business-metrics/

Page 5: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Source: http://www.webperformancetoday.com/2012/02/28/4-awesome-slides-showing-how-page-speed-correlates-to-business-metrics-at-walmart-com/

Page 6: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Connection establishment/tear down

•  Active/passive open

•  Active/passive close, simultaneous close

Page 7: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

TCP state diagram

•  Two events trigger transitions: –  Packet

arrival –  Application

operations •  Half-close end

may still receive data

•  Time_Wait –  Must wait

because ACK may be lost

–  The other end may retransmit FIN

Page 8: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

TCP States in “Normal” Connection Lifetime

SYN (SeqNo = x)

SYN (SeqNo = y, AckNo = x + 1 )

(AckNo = y + 1 )

SYN_SENT(active open)

SYN_RCVD

ESTABLISHED

ESTABLISHED

FIN_WAIT_1(active close)

LISTEN(passive open)

FIN (SeqNo = m)

CLOSE_WAIT(passive close)

(AckNo = m+ 1 )

FIN (SeqNo = n )

(AckNo = n+1)LAST_ACK

FIN_WAIT_2

TIME_WAIT

CLOSED

Page 9: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

2MSL Wait State 2MSL= 2 * Maximum Segment Lifetime

2MSL Wait State = TIME_WAIT •  When TCP does an active close, and sends the final ACK, the connection must

stay in the TIME_WAIT state for twice the maximum segment lifetime. •  The socket pair (srcIP, srcPort, dstIP, dstPort) cannot be reused

•  Why? •  To prevent mixing packets

from two different incarnations of the same connection

FIN

ACK

ACK

FIN

A B

X Time_Wait

Page 10: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Resetting Connections •  Resetting connections is done by setting the

RST flag •  When is the RST flag set?

– Connection request arrives and no server process is waiting on the destination port

– Abort a connection causes the receiver to throw away buffered data

– Receiver does not acknowledge the RST segment – Abused in practice to block applications

Page 11: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Flow control

Page 12: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Sliding window revisited

•  Invariants –  LastByteAcked ≤ LastByteSent –  LastByteSent ≤ LastByteWritten –  LastByteRead < NextByteExpected –  NextByteExpected ≤ LastByteRcvd + 1

•  Limited sending buffer and Receiving buffer

Sender Window Size Receiver Window Size

Page 13: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Buffer Sizes vs Window Sizes

•  Maximum SWS ≤ MaxSndBuf •  Maximum RWS ≤ MaxRcvBuf –

((NextByteExpected-1) – LastByteRead)

Page 14: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

TCP Flow Control

•  Q: how does a receiver prevent a sender from overrunning its buffer?

•  A: use AdvertisedWindow

IP header TCP header TCP data

Sequence number (32 bits)

DATA

20 bytes 20 bytes

0 15 16 31

Source Port Number Destination Port Number

Acknowledgement number (32 bits)

window sizeheaderlength 0 Flags

Options (if any)

TCP checksum urgent pointer

20 bytes

Page 15: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Invariants for flow control

•  Receiver side: – LastByteRcvd – LastByteRead ≤ MaxRcvBuf – AdvertisedWindow = MaxRcvBuf –

((NextByteExpected - 1) – LastByteRead)

Page 16: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Invariants for flow control

•  Sender side: EffectiveWindow = AdvertisedWindow – (LastByteSent – LastByteAcked )

– LastByteWritten – LastByteAcked ≤ MaxSndBuf •  Sender process would be blocked if send buffer is full

Page 17: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end
Page 18: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Window probes •  What if a receiver advertises a window size of

zero? – Problem: Receiver can’t send more ACKs as sender

stops sending more data

•  Design choices – Receivers send duplicate ACKs when window opens – Sender sends periodic 1 byte probes

•  Why? – Keeping the receive side simple à Smart sender/dumb

receiver

Page 19: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

When to send a segment?

•  App writes bytes to a TCP socket •  TCP decides when to send a segment

•  Design choices when window opens: – Send whenever data available – Send when collected Maximum Segment Size data

•  Why? •  More efficient

Page 20: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Push flag

•  What if App is interactive, e.g. ssh? – App sets the PUSH flag – Flush the sent buffer

Page 21: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Silly Window Syndrome

•  Now considers flow control – Window opens, but does not have MSS bytes

•  Design choice 1: send all it has •  E.g., sender sends 1 byte, receiver acks 1, acks opens

the window by 1 byte, sender sends another 1 byte, and so on

Page 22: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Sending smaller segments

Page 23: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Silly Window Syndrome

Page 24: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

How to avoid Silly Window Syndrome

•  Receiver side – Do not advertise small window sizes – Min (MSS, MaxRecvBuf/2)

•  Sender side – Wait until it has a large segment to send – Q: How long should a sender wait?

Page 25: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Sender-Side Silly Window Syndrome avoidance

•  Nagle’s Algorithm – Self-clocking

•  Interactive applications may turn off Nagle’s algorithm using the TCP_NODELAY socket option

When app has data to send if data and window >= MSS send a full segment else if there is unACKed data buffer new data until ACK else send all the new data now

Page 26: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

TCP window management summary

•  Receiver uses AdvertisedWindow for flow control

•  Sender sends probes when AdvertisedWindow reaches zero

•  Silly Window Syndrome avoidance – Receiver: do not advertise small windows – Sender: Nagle’s algorithm

Page 27: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Overview

•  TCP – Connection management – Flow control – When to transmit a segment – Adaptive retransmission – TCP options

•  Modern extensions

Page 28: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

TCP Retransmission

•  A TCP sender retransmits a segment when it assumes that the segment has been lost

•  How does a TCP sender detect a segment loss? –  Timeout –  Duplicate ACKs (later)

Page 29: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

How to set the timer

•  Challenge: RTT unknown and variable

•  Too small – Results in unnecessary retransmissions

•  Too large – Long waiting time

Page 30: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Adaptive retransmission •  Estimate a RTO value based on round-trip time

(RTT) measurements

Segment 1

Segment 4

ACK for Segment 1

Segment 2Segment 3

ACK for Segment 2 + 3

Segment 5

ACK for Segment 4

ACK for Segment 5

RTT #1RTT #2

RTT #3

•  Implementation: one timer per connection

•  Q: Retransmitted segments?

Page 31: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Karn’s Algorithm

•  Ambiguity •  Solution: Karn’s Algorithm: –  Don’t update RTT on

any segments that have been retransmitted

segment

ACK

retransmissionof segment

Timeout !

RTT ? RTT ?

Page 32: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Setting the RTO value •  Uses an exponential moving average (a low-pass

filter) to estimate RTT (srtt) and variance of RTT (rttvar) –  The influence of past samples decrease exponentially

•  The RTT measurements are smoothed by the following estimators srtt and rttvar: srttn+1 = α RTT + (1- α ) srttn rttvarn+1 = β ( | RTT – srttn | ) + (1- β ) rttvarn RTOn+1 = srttn+1 + 4 rttvarn+1

–  The gains are set to α =1/4 and β =1/8 –  Negative power of 2 makes it efficient for implementation

Page 33: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Setting the RTO value (cont’d) •  Initial value for RTO:

–  Sender should set the initial value of RTO to RTO0 = 3 seconds

•  RTO calculation after first RTT measurements arrived

srtt1 = RTT rttvar1 = RTT / 2

RTO1 = srtt1 + 4 rttvarn+1 •  When a timeout occurs , the RTO value is doubled

RTOn+1 = max ( 2 RTOn , 64) seconds

This is called an exponential backoff

Page 34: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Overview

•  TCP – Connection management – Flow control – When to transmit a segment – Adaptive retransmission – TCP options

•  Modern extensions

Page 35: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

TCP header fields •  Options: (type, length, value) •  TCP hdrlen field tells how long options are

End ofOptions kind=0

1 byte

NOP(no operation) kind=1

1 byte

MaximumSegment Size kind=2

1 byte

len=4

1 byte

maximumsegment size

2 bytes

Window ScaleFactor kind=3

1 byte

len=3

1 byte

shift count

1 byte

Timestamp kind=8

1 byte

len=10

1 byte

timestamp value

4 bytes

timestamp echo reply

4 bytes

Page 36: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

TCP header fields •  Options:

– NOP is used to pad TCP header to multiples of 4 bytes

– Maximum Segment Size – Window Scale Options

•  Increases the TCP window from 16 to 32 bits, i.e., the window size is interpreted differently

•  This option can only be used in the SYN segment (first segment) during connection establishment time

– Timestamp Option •  Can be used for roundtrip measurements

Page 37: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Modern TCP extensions •  Timestamp

•  Window scaling factor •  Protection Against Wrapped Sequence Numbers (PAWS)

•  Selective Acknowledgement (SACK)

•  References –  http://www.ietf.org/rfc/rfc1323.txt –  http://www.ietf.org/rfc/rfc2018.txt

Page 38: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Improving RTT estimate

•  TCP timestamp option – Old design

•  One sample per RTT •  Using host timer

•  More samples to estimate – Timestamp option

•  Current TS, echo TS

Page 39: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Increase TCP window size

•  16-bit window size •  Maximum send window <= 65535B •  Suppose a RTT is 100ms •  Max TCP throughput = 65KB/100ms = 5Mbps •  Not good enough for modern high speed links!

IP header TCP header TCP data

Sequence number (32 bits)

DATA

20 bytes 20 bytes

0 15 16 31

Source Port Number Destination Port Number

Acknowledgement number (32 bits)

window sizeheaderlength 0 Flags

Options (if any)

TCP checksum urgent pointer

20 bytes

Page 40: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Protecting against Wraparound

Time until 32-bit sequence number space wraps around.

Page 41: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Solution: Window scaling option

•  All windows are treated as 32-bit •  Negotiating shift.cnt in SYN packets

–  Ignore if SYN flag not set •  Sending TCP

– Real available buffer >> self.shift.cnt à AdvertisedWindow

•  Receiving TCP: stores other.shift.cnt – AdvertisedWindow << other.shift.cnt à Maximum

Sending Window

Kind = 3

Length = 3 Shift.cnt Three bytes

Page 42: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Protect Against Wrapped Sequence Number

•  32-bit sequence number space •  Why sequence numbers may wrap around?

– High speed link – On an OC-45 (2.5Gbps), it takes 14 seconds <

2MSL

•  Solution: compare timestamps – Receiver keeps recent timestamp – Discard old timestamps

Page 43: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Selective Acknowledgement

•  More when we discuss congestion control

•  If there are holes, ack the contiguous received blocks to improve performance

Page 44: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Summary

•  Nitty-gritty details about TCP – Connection management – Flow control – When to transmit a segment – Adaptive retransmission – TCP options – Modern extensions – Next: Congestion Control

•  How does TCP keeps the pipe full?

Page 45: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Midterm statistics

Page 46: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Overall Scores

•  MAXIMUM –  96.5

•  MEAN –  79.79

•  MEDIAN –  82.0

•  STD DEV –  10.69

Page 47: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Quiz Score •  Use mean to evaluate •  Take Quiz 1 82.021739 •  Not Take Quiz 1 72.157895

•  Take Quiz 2 82.025000 •  Not Take Quiz 2 73.500000

•  Either 1 or 2 81.800000 •  Neither 1 nor 2 66.958333

Page 48: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

Survey results

Page 49: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

•  1. Has the course met your expectations of an undergraduate networking class so far? If not, please explain the areas where the course has not met your expectations.

Page 50: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

•  2. Are the lectures given a) at an appropriate speed; b) too fast; c) too slow? Please circle one answer.

Page 51: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

•  3. Do you have any suggestion on how the course materials might be improved?

Page 52: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

•  4. Do you have any suggestion on how the instructor might improve her teaching quality?

Page 53: CS 356: Introduction to Computer Networks Lecture 19: … · 2019. 4. 26. · • Two events trigger transitions: – Packet arrival – Application operations • Half-close end

•  5. Do you have any suggestion on how the TA might improve his teaching quality?