|
|
from mininet.topo import Topo from mininet.node import Node from mininet.net import Mininet from mininet.cli import CLI from mininet.link import TCIntf from enum import Enum
class NodeRoles(Enum): ROUTER = 'Router' SWITCH = 'Switch' HOST = 'Host'
class Router(Node):
def config(self, **params): super(Router, self).config(**params) self.cmd('sysctl net.ipv4.ip_forward=1') self.waitOutput()
def terminate(self): self.cmd('sysctl net.ipv4.ip_forward=0') self.waitOutput() super(Router, self).terminate()
def print_routing_table(self): print(self.name + ' - Routing Table') print(self.cmd('ip route'))
class CustomTopo(Topo):
__LINK_BANDWIDTH = 1
def __init__(self): Topo.__init__(self)
def build(self):
r1 = self.addNode('r1', cls=Router, ip=None, role=NodeRoles.ROUTER.value)
s1 = self.addSwitch('s1', role=NodeRoles.SWITCH.value) s2 = self.addSwitch('s2', role=NodeRoles.SWITCH.value)
h11 = self.addHost('h11', ip='10.0.1.11/24', # inNamespace=False, defaultRoute='via 10.0.1.254', role=NodeRoles.HOST.value) h12 = self.addHost('h12', ip='10.0.1.12/24', defaultRoute='via 10.0.1.254', role=NodeRoles.HOST.value)
h21 = self.addHost('h21', ip='10.0.2.21/24', defaultRoute='via 10.0.2.254', role=NodeRoles.HOST.value) h22 = self.addHost('h22', ip='10.0.2.22/24', defaultRoute='via 10.0.2.254', role=NodeRoles.HOST.value)
for node1, node2 in [(h11, s1), (h12, s1), (h21, s2), (h22, s2)]: self.addLink(node1, node2, cls1=TCIntf, cls2=TCIntf, intfName1=node1 + '-' + node2, intfName2=node2 + '-' + node1, params1={'bw': self.__LINK_BANDWIDTH}, params2={'bw': self.__LINK_BANDWIDTH})
self.addLink(r1, s1, cls1=TCIntf, cls2=TCIntf, intfName1=r1 + '-' + s1, intfName2=s1 + '-' + r1, params1={'ip': '10.0.1.254/24', 'bw': self.__LINK_BANDWIDTH}, params2={'bw': self.__LINK_BANDWIDTH})
self.addLink(r1, s2, cls1=TCIntf, cls2=TCIntf, intfName1=r1 + '-' + s2, intfName2=s2 + '-' + r1, params1={'ip': '10.0.2.254/24', 'bw': self.__LINK_BANDWIDTH}, params2={'bw': self.__LINK_BANDWIDTH})
def run(): topo = CustomTopo() net = Mininet(topo=topo) net.start()
for node in net.topo.nodes(): print(net[node].params['role']) net['r1'].print_routing_table()
CLI(net) net.stop()
if __name__ == '__main__': run()
|