TCP
- Network Address Translation
- Sequence and Acknowledgement Numbers
- Reliable Transmission
- Windows
- TCP header
Network Address Translation
- IP addresses are expensive (a symptom of scarcity)
- multiple computers could share a single address as long as
they use different TCP/UDP ports, e.g. computer A uses local port
12345 to connect to the web server at port 80 of 1.2.3.4, and computer
B uses port 12346 to connect to the same web server
- the router from the Internet to A and B must be smart enough to
route packets based not only on IP address, but also on TCP or UDP port number
- how to coordinate multiple computers' use of source ports?
- in particular, both A and B might be running a web server on port 80
- answer: each computer actually has its own internal address,
e.g. 192.168.1.2, and communication on the internal network uses these
addresses
- the Network address translator rewrites IP and TCP/UDP headers for
outgoing packets to have the external IP address as the source
address, and a randomly selected port as the source port
- replies are translated in the opposite way to have the internal
address as the IP destination, and the correct internal port as the
destination port
- a table is maintained for each TCP connection and for recently used
UDP sessions
Transmission Control Protocol
- connection oriented
- reliable
- full duplex
- streaming
- point-to-point
- communication over IP
also:
- reliable startup
- reliable shutdown (all data received by each receiver)
- end-to-end (not on routers)
Retransmission
- each packet sent carries a sequence number
- sequence numbers vary independently in the two directions
- each packet received causes the receiver to send
an acknowledgement (ack) packet
- the sender starts a retransmission timer whenever it sends a
data packet (not an ack)
- if the packet or ack was lost, the timer expires
and the sender retransmits the data packet
- the receiver uses the sequence number to detect duplicate, out of
order, or old packets
- in TCP, (almost) every packet carries an ACK (piggyback acks)
Adaptive Retransmission
- TCP monitors the time from sending to receiving an ack
- the average of this time is a good estimate for the retransmission
timer
- also add in a factor (e.g. 4) times the deviation: |actual time -
average time|, so we won't retransmit unnecessarily
- if a packet was retransmitted, don't use that packet's "RTT":
- if the packet or ack was lost, the RTT would be measured from the
retransmit
- otherwise the ack could simply be the delayed ack from the original
packet
- more complicated if we retransmitted the packet more than once
- so only use RTTs from packets that were only sent once
Computation of Running Average
- use a factor alpha, e.g. alpha = 1/8 (it is nice if alpha
is a power of two -- why?)
- if our estimate is E and we get a new sample s, compute
new estimate
E' = alpha s + (1 - alpha) E
- example for round-trip time, E'RTT = 1/8 RTT + 7/8 ERTT
- example for deviation, E'dev = 1/8 |RTT - ERTT|
+ 7/8 Edev,
- TCP sets its retransmission timer to ERTT + 4 Edev
- if ERTT = 8 ms, Edev = 4 ms, the retransmission
time is set to 8ms+16ms = 24ms
- if with this example our
latest RTT is 16 ms, the new values
E'RTT and E'dev are: (in-class exercise)
TCP windows
- TCP sequence (and ack) numbers and window sizes are all in bytes,
allowing us to segment differently during retransmission, if so desired
- ack number n means we have received all bytes up to and including
sequence number n-1
- each ack also carries a window w: if a packet has ack n and
window w, the sender is allowed to send up to byte number n+w
before stopping
- the sequence number carried in a packet is the sequence number
of its first byte
- example: packet with sequence number 100 and 22 bytes numbered
100..121
- the acknowledgement sent (ack number 121) carries a window of 500
bytes
- the sender may send up to sequence number 621, inclusive
TCP acks
- in case of bidirectional data flow, we want to delay acks so they
can be piggybacked: carried for free
- in case of unidirectional data flow, we want to send acks as soon as
possible, so the window on the sender accurately reflects the state
of the receiver
- TCP usually doesn't know whether the data flow will
be uni- or bi-directional, so TCP delays all acks by a small amount or
until a second packet must be acked
- if additional buffer space becomes available (the application has
read some of the data), an ack might be sent just to update the window
- an ack by itself is unreliable and might get lost, so a sender
with more data to send must probe the window by sending 1-byte
packets until it gets a response
TCP header
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
TCP Header Format
TCP Header fields
- Source and Destination port: demultiplexing
- Sequence and acknowledgement: reliable delivery
- Data Offset: header size, options
- Window: flow control
- Checksum: correctness
- Urgent Pointer: "special place" in the data stream