81 lines
3.4 KiB
Python
81 lines
3.4 KiB
Python
"""Two ways to choose the window: Project 3's fixed number, and TCP Reno. Given; read it, do not change it.
|
|
|
|
sender.py asks a controller for the window before every send and tells it
|
|
what happened after every ACK and every timeout. The units are packets, not
|
|
bytes: cwnd = 4.0 means four packets may be in flight.
|
|
|
|
FixedWindow is what your Project 3 sender did: one number, chosen in advance,
|
|
that never reacts to anything. Reno reacts, using the rules of RFC 5681
|
|
sections 3.1 and 3.2, with one simplification: ssthresh becomes half of cwnd
|
|
rather than half of the data in flight. Both obey the receiver's rwnd.
|
|
|
|
References:
|
|
RFC 5681, sections 3.1 and 3.2: slow start, congestion avoidance, fast retransmit and fast recovery
|
|
Peterson and Davie, section 6.3: additive increase, multiplicative decrease, slow start, fast recovery
|
|
"""
|
|
|
|
|
|
class FixedWindow:
|
|
"""Project 3's sender: `size` packets in flight, whatever happens."""
|
|
|
|
def __init__(self, size=8):
|
|
self.cwnd = float(size)
|
|
self.ssthresh = float(size)
|
|
self.rwnd = 65535
|
|
self.in_recovery = False
|
|
|
|
def set_rwnd(self, rwnd):
|
|
self.rwnd = rwnd
|
|
|
|
def window(self):
|
|
return max(0, min(int(self.cwnd), self.rwnd))
|
|
|
|
def on_new_ack(self, newly_acked):
|
|
pass # nothing a fixed window learns from an ACK
|
|
|
|
def on_dup_ack(self, count):
|
|
return False # no fast retransmit: wait for the timer, as Go-Back-N did
|
|
|
|
def on_timeout(self):
|
|
pass # the window stays the same size after a loss
|
|
|
|
|
|
class Reno:
|
|
"""TCP Reno, counted in packets."""
|
|
|
|
def __init__(self, ssthresh=64):
|
|
self.cwnd = 1.0 # packets the network may hold for us
|
|
self.ssthresh = float(ssthresh)
|
|
self.rwnd = 65535 # packets the receiver has room for; set by every ACK
|
|
self.in_recovery = False # True between a fast retransmit and the ACK that ends it
|
|
|
|
def set_rwnd(self, rwnd):
|
|
self.rwnd = rwnd
|
|
|
|
def window(self):
|
|
"""The smaller of the two windows, as whole packets: congestion control and flow control together."""
|
|
return max(0, min(int(self.cwnd), self.rwnd))
|
|
|
|
def on_new_ack(self, newly_acked):
|
|
if self.in_recovery:
|
|
self.cwnd = self.ssthresh # the ACK that ends fast recovery: deflate
|
|
self.in_recovery = False
|
|
elif self.cwnd < self.ssthresh:
|
|
self.cwnd += newly_acked # slow start: one packet per ACK, so cwnd doubles each round trip
|
|
else:
|
|
self.cwnd += newly_acked / self.cwnd # congestion avoidance: about one packet per round trip
|
|
|
|
def on_dup_ack(self, count):
|
|
if count == 3: # three duplicates: one packet lost, later ones still arriving
|
|
self.ssthresh = max(self.cwnd / 2, 2.0)
|
|
self.cwnd = self.ssthresh + 3 # the three duplicates mean three packets have left the network
|
|
self.in_recovery = True
|
|
return True # resend the missing packet now
|
|
if count > 3 and self.in_recovery:
|
|
self.cwnd += 1 # each further duplicate: one more packet has left
|
|
return False
|
|
|
|
def on_timeout(self):
|
|
self.ssthresh = max(self.cwnd / 2, 2.0) # nothing is arriving, so the ACK clock has stopped
|
|
self.cwnd = 1.0
|
|
self.in_recovery = False
|