Add Scenario classes

This commit is contained in:
Wojciech Kozlowski 2019-04-06 17:14:52 +02:00
parent 83204c6584
commit d6ef2fbfd9
8 changed files with 116 additions and 29 deletions

View File

@ -7,9 +7,10 @@ from mininet.cli import CLI
from router import Router from router import Router
from topology.two_nodes.topo import NetTopo as TwoNodes from topology.two_nodes.topo import NetTopo as TwoNodes
from scenario import Basic
def run(topo): def run(topo, scenario):
"""Start a network scenario. """Start a network scenario.
""" """
@ -18,20 +19,33 @@ def run(topo):
os.system("mn -c >/dev/null 2>&1") os.system("mn -c >/dev/null 2>&1")
os.system("killall -9 zebra staticd > /dev/null 2>&1") os.system("killall -9 zebra staticd > /dev/null 2>&1")
net = Mininet(topo=topo(), switch=Router) net = Mininet(topo=topo, switch=Router)
net.start() net.start()
scenario.setup(net)
# WARNING: FRR can get confused unless all daemons on each node are started
# together.
for node in net.switches: for node in net.switches:
# Start Zebra (routing table daemon) if node in scenario.routers:
node.cmd("/usr/lib/frr/zebra" # Enable IP forwarding
" -f %s/zebra/%s.conf" node.cmd("sysctl -w net.ipv4.ip_forward=1")
" -d" node.waitOutput()
" -i /tmp/%s-zebra.pid"
" > /tmp/%s-zebra.out 2>&1"
% (topo.topo_dir, node.name, node.name, node.name))
node.waitOutput()
if node.name.startswith('h'): if node in scenario.zebra:
# Start Zebra (routing table daemon)
node.cmd("/usr/lib/frr/zebra"
" -f %s/zebra/%s.conf"
" -d"
" -i /tmp/%s-zebra.pid"
" > /tmp/%s-zebra.out 2>&1"
% (topo.topo_dir, node.name, node.name, node.name))
node.waitOutput()
# Delete spare loopback address for convenience
node.cmd("ip addr del 127.0.0.1/8 dev lo")
node.waitOutput()
if node in scenario.staticd:
# Start static route daemon # Start static route daemon
node.cmd("/usr/lib/frr/staticd" node.cmd("/usr/lib/frr/staticd"
" -f %s/staticd/%s.conf" " -f %s/staticd/%s.conf"
@ -41,19 +55,10 @@ def run(topo):
% (topo.topo_dir, node.name, node.name, node.name)) % (topo.topo_dir, node.name, node.name, node.name))
node.waitOutput() node.waitOutput()
if node.name.startswith('R'):
# Enable IP forwarding
node.cmd("sysctl -w net.ipv4.ip_forward=1")
node.waitOutput()
# Delete spare loopback address for convenience
node.cmd("ip addr del 127.0.0.1/8 dev lo")
node.waitOutput()
CLI(net) CLI(net)
net.stop() net.stop()
os.system("killall -9 zebra staticd") os.system("killall -9 zebra staticd")
if __name__ == "__main__": if __name__ == "__main__":
run(TwoNodes) run(TwoNodes(), Basic())

82
scenario.py Normal file
View File

@ -0,0 +1,82 @@
class Scenario(object):
"""Class that describes a network scenario. A scenario defines which nodes
are routers and which are hosts as well as which nodes need to start which
daemons.
"""
def __init__(self):
self._routers = None
self._hosts = None
self._zebra = None
self._staticd = None
@property
def routers(self):
"""Set of nodes that are routers.
"""
return self._routers
@property
def hosts(self):
"""Set of nodes that are hosts.
"""
return self._hosts
@property
def zebra(self):
"""Set of nodes that should run zebra.
"""
return self._zebra
@property
def staticd(self):
"""Set of nodes that should run staticd.
"""
return self._staticd
def _routers_and_hosts(self, net):
"""Separate nodes into routers and hosts based on their names.
"""
self._routers = set()
self._hosts = set()
for node in net.switches:
if node.name.startswith('R'):
self._routers.add(node)
else:
self._hosts.add(node)
def setup(self, net):
"""Setup the scenario.
"""
self._routers_and_hosts(net)
self._zebra = set()
self._staticd = set()
class Plain(Scenario):
"""In a Plain scenario no daemons are run.
"""
class Basic(Scenario):
"""In a Basic scenario, all nodes run zebra, and all hosts run staticd to
setup a default route.
"""
def __init__(self):
Scenario.__init__(self)
def setup(self, net):
self._routers_and_hosts(net)
self._zebra = self._routers.union(self._hosts)
self._staticd = self._hosts

View File

@ -1,7 +1,7 @@
! -*- zebra -*- ! -*- staticd -*-
hostname h1_1 hostname h1_1
ip route 0.0.0.0/0 10.1.0.254 ip route 0.0.0.0/0 10.1.0.254
log file /tmp/h1_1-staticd.log log file /tmp/h1_1-staticd.log debugging

View File

@ -1,7 +1,7 @@
! -*- zebra -*- ! -*- staticd -*-
hostname h2_1 hostname h2_1
ip route 0.0.0.0/0 10.2.0.254 ip route 0.0.0.0/0 10.2.0.254
log file /tmp/h2_1-staticd.log log file /tmp/h2_1-staticd.log debugging

View File

@ -11,4 +11,4 @@ interface R1-eth1
interface R1-eth2 interface R1-eth2
ip address 10.1.0.254/24 ip address 10.1.0.254/24
log file /tmp/R1-zebra.log log file /tmp/R1-zebra.log debugging

View File

@ -11,4 +11,4 @@ interface R2-eth1
interface R2-eth2 interface R2-eth2
ip address 10.2.0.254/24 ip address 10.2.0.254/24
log file /tmp/R2-zebra.log log file /tmp/R2-zebra.log debugging

View File

@ -5,4 +5,4 @@ hostname h1_1
interface h1_1-eth1 interface h1_1-eth1
ip address 10.1.0.1/24 ip address 10.1.0.1/24
log file /tmp/h1_1-zebra.log log file /tmp/h1_1-zebra.log debugging

View File

@ -5,4 +5,4 @@ hostname h2_1
interface h2_1-eth1 interface h2_1-eth1
ip address 10.2.0.1/24 ip address 10.2.0.1/24
log file /tmp/h2_1-zebra.log log file /tmp/h2_1-zebra.log debugging