215 lines
7.6 KiB
Python
215 lines
7.6 KiB
Python
"""HW 10 experiment harness. You write the parts marked YOUR TASK.
|
|
|
|
Run inside the course container, from this folder:
|
|
|
|
python3 run_experiment.py one reno 1 # given: one 10 s flow (algorithm, loss %), printed
|
|
python3 run_experiment.py capture reno 0 # given: one flow, recorded at h1 and h3 -> *.pcap
|
|
python3 run_experiment.py sweep # 8 conditions x REPS runs -> results.csv
|
|
python3 run_experiment.py cwnd # one run per algorithm at 1% -> cwnd.csv
|
|
python3 run_experiment.py fairness cubic # two flows, 20 and 100 ms RTT -> fairness.csv
|
|
python3 run_experiment.py summary # results.csv -> your results table (any computer)
|
|
|
|
`topo.py` builds the network and `measure.py` runs and reads the flows; read
|
|
both before you start. `python3 selfcheck.py` checks your work on any computer.
|
|
"""
|
|
|
|
import csv
|
|
import statistics
|
|
import sys
|
|
|
|
BW = 10 # bottleneck, Mbit/s
|
|
DELAY = 20 # one-way delay, ms (base round trip 40 ms)
|
|
SECONDS = 10 # length of each transfer
|
|
REPS = 3 # repetitions of every condition
|
|
LOSSES = [0, 0.1, 1, 3] # percent
|
|
ALGORITHMS = ["reno", "cubic"]
|
|
FAIR_SECONDS = 30 # the two-flow runs need longer to settle
|
|
FAIR_RTTS = (20, 100) # round-trip times wanted for flow A and flow B, ms
|
|
|
|
|
|
def write_csv(path, rows):
|
|
"""Write a list of dicts to `path`, one column per key."""
|
|
with open(path, "w", newline="") as f:
|
|
w = csv.DictWriter(f, fieldnames=list(rows[0]))
|
|
w.writeheader()
|
|
w.writerows(rows)
|
|
print(f"wrote {len(rows)} rows to {path}")
|
|
|
|
|
|
def read_csv(path):
|
|
with open(path, newline="") as f:
|
|
return list(csv.DictReader(f))
|
|
|
|
|
|
def quiet():
|
|
from mininet.log import setLogLevel
|
|
setLogLevel("error")
|
|
|
|
|
|
def one(cc, loss):
|
|
"""Given: run one flow and print what measure.py reports, to see what you are working with."""
|
|
import measure
|
|
import topo
|
|
quiet()
|
|
net = topo.start(bw=BW, delay=DELAY, loss=loss)
|
|
try:
|
|
r = measure.Flow(net, "h1", "h3", seconds=SECONDS, cc=cc).start().finish()
|
|
finally:
|
|
net.stop()
|
|
print(f"{r['cc']}, {loss}% loss: {r['mbps']:.2f} Mbit/s, {r['retransmits']} retransmits, "
|
|
f"RTT {r['rtt_ms']:.1f} ms")
|
|
print("cwnd (segments), one sample every 0.5 s:")
|
|
print(" ".join(str(s["cwnd"]) for s in r["cwnd"][::5]))
|
|
|
|
|
|
def capture(cc, loss, seconds=5):
|
|
"""Given: run one flow and record it at the sender (h1) and the receiver (h3).
|
|
|
|
Writes capture-<cc>-loss<loss>-h1.pcap and -h3.pcap for Wireshark. Only the
|
|
first 96 bytes of each packet are kept: the IP and TCP headers, not the data.
|
|
At 0% loss every drop happens in the full bottleneck queue, so that capture
|
|
shows congestion alone; at 1% the link's random loss is mixed in.
|
|
References: https://www.tcpdump.org/manpages/tcpdump.1.html and
|
|
https://www.wireshark.org/docs/wsug_html_chunked/ChAdvTCPAnalysis.html
|
|
"""
|
|
import time
|
|
import measure
|
|
import topo
|
|
quiet()
|
|
net = topo.start(bw=BW, delay=DELAY, loss=loss)
|
|
name = f"capture-{cc}-loss{loss:g}"
|
|
dumps = []
|
|
try:
|
|
for host in ("h1", "h3"):
|
|
node = net.get(host)
|
|
dumps.append(node.popen(["tcpdump", "-i", f"{host}-eth0", "-s", "96", "-U", "-w",
|
|
f"{name}-{host}.pcap", "tcp"], stderr=open("/dev/null", "w")))
|
|
time.sleep(1) # tcpdump needs a moment before it sees packets
|
|
r = measure.Flow(net, "h1", "h3", seconds=seconds, cc=cc).start().finish()
|
|
time.sleep(1)
|
|
finally:
|
|
for d in dumps:
|
|
d.terminate()
|
|
d.wait()
|
|
net.stop()
|
|
print(f"{r['cc']}, {loss:g}% loss: {r['mbps']:.2f} Mbit/s, {r['retransmits']} retransmits")
|
|
print(f"wrote {name}-h1.pcap (sender) and {name}-h3.pcap (receiver)")
|
|
|
|
|
|
# ---------------------------------------------------------------- your work
|
|
|
|
def sweep():
|
|
"""Run every (algorithm, loss) condition REPS times; write results.csv.
|
|
|
|
One row per run, with at least these columns:
|
|
cc, loss, rep, mbps, retransmits, rtt_ms
|
|
"""
|
|
import measure
|
|
import topo
|
|
quiet()
|
|
rows = []
|
|
# YOUR TASK: for each loss in LOSSES, each algorithm in ALGORITHMS, and each
|
|
# repetition, start the network with topo.start(bw=BW, delay=DELAY, loss=...),
|
|
# run one measure.Flow from h1 to h3 for SECONDS, stop the network (always,
|
|
# even if the flow fails), and add a row. Print each result as you go: the
|
|
# sweep takes several minutes.
|
|
raise NotImplementedError("sweep")
|
|
write_csv("results.csv", rows)
|
|
|
|
|
|
def summarize(rows):
|
|
"""Turn the per-run rows into one line per condition.
|
|
|
|
`rows` are dicts as read back from results.csv, so every value is a string.
|
|
Return a list of dicts, sorted by loss then algorithm, each with:
|
|
cc, loss (float), n (runs), mean, min, max, stdev (of mbps)
|
|
stdev is the sample standard deviation (statistics.stdev), 0.0 for one run.
|
|
"""
|
|
# YOUR TASK
|
|
raise NotImplementedError("summarize")
|
|
|
|
|
|
def fairness_setup():
|
|
"""The (delay, (extra_A, extra_B)) to pass to topo.start for the two-flow runs.
|
|
|
|
A flow's round trip is 2 x delay + its extra_rtt (see topo.py). Choose
|
|
values so flow A's round trip is FAIR_RTTS[0] and flow B's is FAIR_RTTS[1].
|
|
"""
|
|
# YOUR TASK
|
|
raise NotImplementedError("fairness_setup")
|
|
|
|
|
|
def fairness(cc):
|
|
"""Run flow A (h1 -> h3) and flow B (h2 -> h4) at the same time, REPS times; write fairness.csv.
|
|
|
|
One row per flow per run: cc, rep, flow ("A" or "B"), rtt_ms (the target
|
|
round trip), mbps. Start both flows before finishing either one.
|
|
"""
|
|
import measure
|
|
import topo
|
|
quiet()
|
|
delay, extra = fairness_setup()
|
|
rows = []
|
|
# YOUR TASK: REPS times, start the network with bw=BW, delay=delay,
|
|
# extra_rtt=extra (no loss), run both flows for FAIR_SECONDS together,
|
|
# and add two rows.
|
|
raise NotImplementedError("fairness")
|
|
write_csv("fairness.csv", rows)
|
|
|
|
|
|
def cwnd():
|
|
"""One run per algorithm at 1% loss; write every cwnd sample to cwnd.csv.
|
|
|
|
One row per sample: cc, t, cwnd, ssthresh (empty when there is none).
|
|
"""
|
|
import measure
|
|
import topo
|
|
quiet()
|
|
rows = []
|
|
# YOUR TASK
|
|
raise NotImplementedError("cwnd")
|
|
write_csv("cwnd.csv", rows)
|
|
|
|
|
|
def summary():
|
|
"""Print the summary as a Markdown table to paste into your report."""
|
|
lines = summarize(read_csv("results.csv"))
|
|
print("| loss % | algorithm | runs | mean Mbit/s | min | max | stdev |")
|
|
print("| --: | :-- | --: | --: | --: | --: | --: |")
|
|
for s in lines:
|
|
print(f"| {s['loss']:g} | {s['cc']} | {s['n']} | {s['mean']:.2f} | {s['min']:.2f} | "
|
|
f"{s['max']:.2f} | {s['stdev']:.2f} |")
|
|
|
|
|
|
def main():
|
|
try:
|
|
run(sys.argv[1:] or ["help"])
|
|
except NotImplementedError as e:
|
|
sys.exit(f"{e}() is not written yet: see its YOUR TASK")
|
|
except ModuleNotFoundError as e:
|
|
if e.name != "mininet":
|
|
raise
|
|
sys.exit("this step needs Mininet: run it inside the course container (see README.md)")
|
|
except FileNotFoundError as e:
|
|
sys.exit(f"{e.filename} does not exist yet: run the step that writes it first")
|
|
|
|
|
|
def run(args):
|
|
if args[0] == "one" and len(args) == 3:
|
|
one(args[1], float(args[2]))
|
|
elif args[0] == "capture" and len(args) == 3:
|
|
capture(args[1], float(args[2]))
|
|
elif args[0] == "sweep":
|
|
sweep()
|
|
elif args[0] == "cwnd":
|
|
cwnd()
|
|
elif args[0] == "fairness":
|
|
fairness(args[1] if len(args) > 1 else "cubic")
|
|
elif args[0] == "summary":
|
|
summary()
|
|
else:
|
|
sys.exit(__doc__)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|