119 lines
5.1 KiB
Python
119 lines
5.1 KiB
Python
"""A dumbbell network with one bottleneck you configure. Given; do not change it.
|
|
|
|
h1 --+ +-- h3
|
|
s1 ===== bottleneck (s1 -> s2) ===== s2
|
|
h2 --+ +-- h4
|
|
|
|
Flow A is h1 -> h3 and flow B is h2 -> h4. Both cross the bottleneck, so they
|
|
compete for it. Where each setting lives:
|
|
|
|
bandwidth, queue s1's port toward s2: packets leave at `bw` Mbit/s, and
|
|
at most `queue` packets wait for their turn; the next
|
|
one to arrive is dropped (a drop-tail queue)
|
|
delay one way, added in each direction (so the base round
|
|
trip is 2 x delay)
|
|
loss random loss on data packets after the bottleneck
|
|
(h1/h2 -> h3/h4 only; ACKs are not lost)
|
|
extra_rtt extra round-trip time for flow A and flow B, added on
|
|
the ACKs' way back to h1 and h2
|
|
|
|
Use it from Python (this is what run_experiment.py does):
|
|
|
|
import topo
|
|
net = topo.start(bw=10, delay=20, loss=1.0) # Mbit/s, ms, percent
|
|
...
|
|
net.stop()
|
|
|
|
or on its own, for a mininet> prompt to explore:
|
|
|
|
python3 topo.py --bw 10 --delay 20 --loss 1
|
|
|
|
Runs inside the course container. Mininet reference:
|
|
http://mininet.org/walkthrough/ ; netem, which every queue here uses:
|
|
https://man7.org/linux/man-pages/man8/tc-netem.8.html
|
|
"""
|
|
|
|
import argparse
|
|
|
|
from mininet.net import Mininet
|
|
from mininet.nodelib import LinuxBridge
|
|
from mininet.topo import Topo
|
|
|
|
IP = {"h1": "10.0.0.1", "h2": "10.0.0.2", "h3": "10.0.0.3", "h4": "10.0.0.4"}
|
|
|
|
|
|
class Dumbbell(Topo):
|
|
def build(self):
|
|
s1, s2 = self.addSwitch("s1"), self.addSwitch("s2")
|
|
for h in ("h1", "h2", "h3", "h4"):
|
|
self.addHost(h, ip=IP[h] + "/24")
|
|
# interface names are fixed so the tc commands below know where each queue is
|
|
self.addLink("h1", s1, intfName2="s1-h1")
|
|
self.addLink("h2", s1, intfName2="s1-h2")
|
|
self.addLink(s1, s2, intfName1="s1-s2", intfName2="s2-s1")
|
|
self.addLink("h3", s2, intfName2="s2-h3")
|
|
self.addLink("h4", s2, intfName2="s2-h4")
|
|
|
|
|
|
def configure(net, bw=10, delay=20, loss=0.0, queue=50, extra_rtt=(0, 0)):
|
|
"""Put the bottleneck and the delays in place. Safe to call again to change them."""
|
|
s1, s2 = net.get("s1", "s2")
|
|
|
|
def qdisc(node, dev, spec):
|
|
out = node.cmd(f"tc qdisc replace dev {dev} root {spec}")
|
|
if out.strip():
|
|
raise RuntimeError(f"tc on {dev}: {out.strip()}")
|
|
|
|
# the bottleneck. netem, not tbf: netem releases each packet from the sender's
|
|
# accounting as it queues it, so TCP sees the queue fill and overflow as it would
|
|
# at a real router. Behind tbf, Linux holds the sender back and the queue never fills.
|
|
qdisc(s1, "s1-s2", f"netem rate {bw}mbit limit {queue}")
|
|
# data direction, after the bottleneck: the one-way delay and the random loss
|
|
for port in ("s2-h3", "s2-h4"):
|
|
qdisc(s2, port, f"netem delay {delay}ms loss {loss}% limit 100000")
|
|
# ACK direction: the same one-way delay, then each flow's extra round-trip time
|
|
qdisc(s2, "s2-s1", f"netem delay {delay}ms limit 100000")
|
|
for port, extra in zip(("s1-h1", "s1-h2"), extra_rtt):
|
|
qdisc(s1, port, f"netem delay {extra}ms limit 100000")
|
|
|
|
|
|
def start(bw=10, delay=20, loss=0.0, queue=50, extra_rtt=(0, 0)):
|
|
"""Build the dumbbell, configure it, and return the running Mininet network."""
|
|
net = Mininet(topo=Dumbbell(), switch=LinuxBridge, controller=None, autoSetMacs=True)
|
|
net.start()
|
|
for node in net.hosts + net.switches:
|
|
for intf in node.intfList():
|
|
if intf.name != "lo":
|
|
# no segmentation offload: the bottleneck must see real 1500-byte packets
|
|
node.cmd(f"ethtool -K {intf.name} tso off gso off gro off 2>/dev/null")
|
|
configure(net, bw, delay, loss, queue, extra_rtt)
|
|
# the first packets on a new network can be lost while it settles; ping until each path answers
|
|
for src, dst in (("h1", "h3"), ("h2", "h4")):
|
|
for _ in range(5):
|
|
if " 0 received" not in net.get(src).cmd(f"ping -c 1 -W 2 {IP[dst]}"):
|
|
break
|
|
return net
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(description="Start the dumbbell with a mininet> prompt.")
|
|
p.add_argument("--bw", type=float, default=10, help="bottleneck rate, Mbit/s")
|
|
p.add_argument("--delay", type=float, default=20, help="one-way delay, ms")
|
|
p.add_argument("--loss", type=float, default=0.0, help="random data loss, percent")
|
|
p.add_argument("--queue", type=int, default=50, help="bottleneck queue, packets")
|
|
p.add_argument("--extra-rtt", type=float, nargs=2, default=(0, 0), metavar=("A", "B"),
|
|
help="extra round-trip time for flow A (h1) and flow B (h2), ms")
|
|
a = p.parse_args()
|
|
from mininet.cli import CLI
|
|
from mininet.log import setLogLevel
|
|
setLogLevel("error")
|
|
net = start(a.bw, a.delay, a.loss, a.queue, tuple(a.extra_rtt))
|
|
print("h1 -> h3 and h2 -> h4 cross the bottleneck. Try: h1 ping -c 3 h3")
|
|
try:
|
|
CLI(net)
|
|
finally:
|
|
net.stop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|