You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.8 KiB

  1. from mininet.topo import Topo
  2. from mininet.node import Node
  3. from mininet.net import Mininet
  4. from mininet.cli import CLI
  5. from mininet.link import TCIntf
  6. from enum import Enum
  7. class NodeRoles(Enum):
  8. ROUTER = 'Router'
  9. SWITCH = 'Switch'
  10. HOST = 'Host'
  11. class Router(Node):
  12. def config(self, **params):
  13. super(Router, self).config(**params)
  14. self.cmd('sysctl net.ipv4.ip_forward=1')
  15. self.waitOutput()
  16. def terminate(self):
  17. self.cmd('sysctl net.ipv4.ip_forward=0')
  18. self.waitOutput()
  19. super(Router, self).terminate()
  20. def print_routing_table(self):
  21. print(self.name + ' - Routing Table')
  22. print(self.cmd('ip route'))
  23. class CustomTopo(Topo):
  24. __LINK_BANDWIDTH = 1
  25. def __init__(self):
  26. Topo.__init__(self)
  27. def build(self):
  28. r1 = self.addNode('r1', cls=Router, ip=None,
  29. role=NodeRoles.ROUTER.value)
  30. s1 = self.addSwitch('s1', role=NodeRoles.SWITCH.value)
  31. s2 = self.addSwitch('s2', role=NodeRoles.SWITCH.value)
  32. h11 = self.addHost('h11', ip='10.0.1.11/24',
  33. # inNamespace=False,
  34. defaultRoute='via 10.0.1.254',
  35. role=NodeRoles.HOST.value)
  36. h12 = self.addHost('h12', ip='10.0.1.12/24',
  37. defaultRoute='via 10.0.1.254',
  38. role=NodeRoles.HOST.value)
  39. h21 = self.addHost('h21', ip='10.0.2.21/24',
  40. defaultRoute='via 10.0.2.254',
  41. role=NodeRoles.HOST.value)
  42. h22 = self.addHost('h22', ip='10.0.2.22/24',
  43. defaultRoute='via 10.0.2.254',
  44. role=NodeRoles.HOST.value)
  45. for node1, node2 in [(h11, s1), (h12, s1), (h21, s2), (h22, s2)]:
  46. self.addLink(node1, node2,
  47. cls1=TCIntf, cls2=TCIntf,
  48. intfName1=node1 + '-' + node2,
  49. intfName2=node2 + '-' + node1,
  50. params1={'bw': self.__LINK_BANDWIDTH},
  51. params2={'bw': self.__LINK_BANDWIDTH})
  52. self.addLink(r1, s1,
  53. cls1=TCIntf, cls2=TCIntf,
  54. intfName1=r1 + '-' + s1,
  55. intfName2=s1 + '-' + r1,
  56. params1={'ip': '10.0.1.254/24', 'bw': self.__LINK_BANDWIDTH},
  57. params2={'bw': self.__LINK_BANDWIDTH})
  58. self.addLink(r1, s2,
  59. cls1=TCIntf, cls2=TCIntf,
  60. intfName1=r1 + '-' + s2,
  61. intfName2=s2 + '-' + r1,
  62. params1={'ip': '10.0.2.254/24', 'bw': self.__LINK_BANDWIDTH},
  63. params2={'bw': self.__LINK_BANDWIDTH})
  64. def run():
  65. topo = CustomTopo()
  66. net = Mininet(topo=topo)
  67. net.start()
  68. for node in net.topo.nodes():
  69. print(net[node].params['role'])
  70. net['r1'].print_routing_table()
  71. CLI(net)
  72. net.stop()
  73. if __name__ == '__main__':
  74. run()