Lec22 Networking2
Lec22 Networking2
Networking II
Other
subnets
subnet1
Transcontinental
Router Link
Router
Other
subnets Router subnet3
subnet2
Router Router
• Contributions to Latency
– Wire latency: depends on speed of light in wire or fiber
» about 1–1.5 ns/foot
– Router latency: depends on internals of router
» Could be < 1 ms (for a good router)
» Question: can router handle full wire throughput?
4/17/06 Joseph CS162 ©UCB Spring 2006 Lec 22.6
Goals for Today
• Networking
– Reliable Messaging
» TCP windowing and congestion avoidance
– Two-phase commit
ack
• How to ensure transmission of packets?
– Detect garbling at receiver via checksum, discard if bad
– Receiver acknowledges (by sending “ack”) when packet
received properly at destination
– Timeout at sender: if no ack, retransmit
• Some questions:
– If the sender doesn’t get an ack, does that mean the
receiver didn’t get the original message?
» No
– What if ack gets dropped? Or if message gets delayed?
» Sender doesn’t get ack, retransmits. Receiver gets
message twice, acks each.
4/17/06 Joseph CS162 ©UCB Spring 2006 Lec 22.11
How to deal with message duplication
• Solution: put sequence number in message to identify
re-transmitted packets
– Receiver checks for duplicate #’s; Discard if detected
• Requirements:
– Sender keeps copy of unack’ed messages
» Easy: only need to buffer messages
– Receiver tracks possible duplicate messages
» Hard: when ok to forget about received message?
• Alternating-bit protocol:
– Send one message at a time; don’t send A B
Pkt #
next message until ack received 0
– Sender keeps last message; receiver c k #0
tracks sequence # of last message received A
Pkt #
• Pros: simple, small overhead 1
• Con: Poor performance c k #1
A
– Wire can hold multiple messages; want to Pkt #
fill up at (wire latency × throughput) 0
• Con: doesn’t work if network can delay c k #0
A
or duplicate messages arbitrarily
4/17/06 Joseph CS162 ©UCB Spring 2006 Lec 22.12
Better messaging: Window-based acknowledgements
• Window based protocol (TCP): A B
– Send up to N packets without ack pkt
# 0
» Allows pipelining of packets N=5
» Window size (N) < queue at destination pk
Queue
– Each packet has sequence number t#
4
» Receiver acknowledges each packet #0
» Ack says “received all packets up k
ac
to sequence number X”/send more #4
k
• Acks serve dual purpose: ac
– Reliability: Confirming packet received
– Flow Control: Receiver ready for packet
» Remaining space in queue at receiver
can be returned with ACK
• What if packet gets garbled/dropped?
– Sender will timeout waiting for ack packet
» Resend missing packets⇒ Receiver gets packets out of order!
– Should receiver discard packets that arrive out of order?
» Simple, but poor performance
– Alternative: Keep copy until sender fills in missing pieces?
» Reduces # of retransmits, but more complex
• What if ack gets garbled/dropped?
– Timeout and resend just the un-acknowledged packets
4/17/06 Joseph CS162 ©UCB Spring 2006 Lec 22.13
Administrivia
• Projects:
– Project 3 code due tomorrow
– Project 4 design document due May 1st
• Final Exam
– May 18th
Seq:100
Seq:140
Seq:190
Seq:230
Seq:260
Seq:300
Seq:340
Seq:380
Size:40
Size:50
Size:40
Size:30
Size:40
Size:40
Size:40
Size:20
A:100/300
Seq:100 A:140/260
Seq:140 A:190/210
Seq:230 A:190/140
Seq:260 A:190/100
Seq:300 A:190/60
Seq:340 A:380/20
Seq:380
4/17/06 Joseph CS162 ©UCB Spring 2006
A:400/0
Lec 22.17
Selective Acknowledgement Option (SACK)
Sequence Number
Sequence Number
Ack Number
Ack Number
(20 bytes)
(20 bytes)
IP Header
IP Header
TCP Header TCP Header
• Vanilla TCP Acknowledgement
– Every message encodes Sequence number and Ack
– Can include data for forward stream and/or ack for
reverse stream
• Selective Acknowledgement
– Acknowledgement information includes not just one
number, but rather ranges of received packets
– Must be specially negotiated at beginning of TCP setup
» Not widely in use (although in Windows since Windows 98)
4/17/06 Joseph CS162 ©UCB Spring 2006 Lec 22.18
Congestion Avoidance
• Congestion
– How long should timeout be for re-sending messages?
» Too long→wastes time if message lost
» Too short→retransmit even though ack will arrive shortly
– Stability problem: more congestion ⇒ ack is delayed ⇒
unnecessary timeout ⇒ more traffic ⇒ more congestion
» Closely related to window size at sender: too big means
putting too much data into network
• How does the sender’s window size get chosen?
– Must be less than receiver’s advertised buffer size
– Try to match the rate of sending packets with the rate
that the slowest link can accommodate
– Sender uses an adaptive algorithm to decide size of N
» Goal: fill network between sender and receiver
» Basic technique: slowly increase size of window until
acknowledgements start being delayed/lost
• TCP solution: “slow start” (start sending slowly)
– If no timeout, slowly increase window size (throughput)
– Timeout ⇒ congestion, so cut window size in half
– “Additive Increase, Multiplicative Decrease”
4/17/06 Joseph CS162 ©UCB Spring 2006 Lec 22.19
Sequence-Number Initialization
• How do you choose an initial sequence number?
– When machine boots, ok to start with sequence #0?
» No: could send two messages with same sequence #!
» Receiver might end up discarding valid packets, or duplicate
ack from original transmission might hide lost packet
– Also, if it is possible to predict sequence numbers, might
be possible for attacker to hijack TCP connection
• Some ways of choosing an initial sequence number:
– Time to live: each packet has a deadline.
» If not delivered in X seconds, then is dropped
» Thus, can re-use sequence numbers if wait for all packets
in flight to be delivered or to expire
– Epoch #: uniquely identifies which set of sequence
numbers are currently being used
» Epoch # stored on disk, Put in every message
» Epoch # incremented on crash and/or when run out of
sequence #
– Pseudo-random increment to previous sequence number
» Used by several protocol implementations
4/17/06 Joseph CS162 ©UCB Spring 2006 Lec 22.20
BREAK
Use of TCP: Sockets
• Socket: an abstraction of a network I/O queue
– Embodies one side of a communication channel
» Same interface regardless of location of other end
» Could be local machine (called “UNIX socket”) or remote
machine (called “network socket”)
– First introduced in 4.2 BSD UNIX: big innovation at time
» Now most operating systems provide some notion of socket
• Using Sockets for Client-Server (C/C++ interface):
– On server: set up “server-socket”
» Create socket, Bind to protocol (TCP), local address, port
» Call listen(): tells server socket to accept incoming requests
» Perform multiple accept() calls on socket to accept incoming
connection request
» Each successful accept() returns a new socket for a new
connection; can pass this off to handler thread
– On client:
» Create socket, Bind to protocol (TCP), remote address, port
» Perform connect() on socket to make connection
» If connect() successful, have socket connected to server
client:
// Makes socket, binds addr/port, calls connect()
Socket sock = new Socket(“169.229.60.38”,6013);
BufferedReader bin =
new BufferedReader(
new InputStreamReader(sock.getInputStream));
String line;
while ((line = bin.readLine())!=null)
System.out.println(line);
sock.close();
4/17/06 Joseph CS162 ©UCB Spring 2006 Lec 22.23
Distributed Applications
• How do you actually program a distributed application?
– Need to synchronize multiple threads, running on
different machines
» No shared memory, so cannot use test&set
Receive
Send Network