fork download
  1. #!/usr/bin/python
  2.  
  3. """
  4. linuxrouter.py: Example network with Linux IP router
  5.  
  6. This example converts a Node into a router using IP forwarding
  7. already built into Linux.
  8.  
  9. The example topology creates a router and three IP subnets:
  10.  
  11. - 192.168.1.0/24 (r0-eth1, IP: 192.168.1.1)
  12. - 172.16.0.0/12 (r0-eth2, IP: 172.16.0.1)
  13. - 10.0.0.0/8 (r0-eth3, IP: 10.0.0.1)
  14.  
  15. Each subnet consists of a single host connected to
  16. a single switch:
  17.  
  18. r0-eth1 - s1-eth1 - h1-eth0 (IP: 192.168.1.100)
  19. r0-eth2 - s2-eth1 - h2-eth0 (IP: 172.16.0.100)
  20. r0-eth3 - s3-eth1 - h3-eth0 (IP: 10.0.0.100)
  21.  
  22. The example relies on default routing entries that are
  23. automatically created for each router interface, as well
  24. as 'defaultRoute' parameters for the host interfaces.
  25.  
  26. Additional routes may be added to the router or hosts by
  27. executing 'ip route' or 'route' commands on the router or hosts.
  28. """
  29.  
  30.  
  31. from mininet.topo import Topo
  32. from mininet.net import Mininet
  33. from mininet.node import Node
  34. from mininet.log import setLogLevel, info
  35. from mininet.cli import CLI
  36.  
  37.  
  38. class LinuxRouter( Node ):
  39. "A Node with IP forwarding enabled."
  40.  
  41. def config( self, **params ):
  42. super( LinuxRouter, self).config( **params )
  43. # Enable forwarding on the router
  44. self.cmd( 'sysctl net.ipv4.ip_forward=1' )
  45.  
  46. def terminate( self ):
  47. self.cmd( 'sysctl net.ipv4.ip_forward=0' )
  48. super( LinuxRouter, self ).terminate()
  49.  
  50.  
  51. class NetworkTopo( Topo ):
  52. "A LinuxRouter connecting three IP subnets"
  53.  
  54. def build( self, **_opts ):
  55.  
  56. defaultIP = '192.168.1.1/24' # IP address for r0-eth1
  57. router = self.addNode( 'r0', cls=LinuxRouter, ip=defaultIP )
  58.  
  59. s1, s2, s3 = [ self.addSwitch( s ) for s in ( 's1', 's2', 's3' ) ]
  60.  
  61. self.addLink( s1, router, intfName2='r0-eth1',
  62. params2={ 'ip' : defaultIP } ) # for clarity
  63. self.addLink( s2, router, intfName2='r0-eth2',
  64. params2={ 'ip' : '172.16.0.1/12' } )
  65. self.addLink( s3, router, intfName2='r0-eth3',
  66. params2={ 'ip' : '10.0.0.1/8' } )
  67.  
  68. h1x1 = self.addHost( 'h1x1', ip='192.168.1.101/24', mac='00:00:00:00:00:11',
  69. defaultRoute='via 192.168.1.1' )
  70. h1x2 = self.addHost( 'h1x2', ip='192.168.1.102/24', mac='00:00:00:00:00:12',
  71. defaultRoute='via 192.168.1.1' )
  72. h2x1 = self.addHost( 'h2x1', ip='172.16.0.101/12', mac='00:00:00:00:00:21',
  73. defaultRoute='via 172.16.0.1' )
  74. h2x2 = self.addHost( 'h2x2', ip='172.16.0.102/12', mac='00:00:00:00:00:22',
  75. defaultRoute='via 172.16.0.1' )
  76. h3x1 = self.addHost( 'h3x1', ip='10.0.0.101/8', mac='00:00:00:00:00:31',
  77. defaultRoute='via 10.0.0.1' )
  78. h3x2 = self.addHost( 'h3x2', ip='10.0.0.102/8', mac='00:00:00:00:00:32',
  79. defaultRoute='via 10.0.0.1' )
  80.  
  81. for h, s in [ (h1x1, s1), (h1x2, s1), (h2x1, s2), (h2x2, s2), (h3x1, s3), (h3x2, s3) ]:
  82. self.addLink( h, s )
  83.  
  84.  
  85. def run():
  86. "Test linux router"
  87. topo = NetworkTopo()
  88. net = Mininet( topo=topo ) # controller is used by s1-s3
  89. net.start()
  90. info( '*** Routing Table on Router:\n' )
  91. info( net[ 'r0' ].cmd( 'route' ) )
  92. CLI( net )
  93. net.stop()
  94.  
  95. if __name__ == '__main__':
  96. setLogLevel( 'info' )
  97. run()
  98. # your code goes here
Runtime error #stdin #stdout #stderr 0.01s 7308KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 31, in <module>
ImportError: No module named mininet.topo