Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / mininet / examples / linuxrouter.py
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         h1 = self.addHost( 'h1', ip='192.168.1.100/24',
69                            defaultRoute='via 192.168.1.1' )
70         h2 = self.addHost( 'h2', ip='172.16.0.100/12',
71                            defaultRoute='via 172.16.0.1' )
72         h3 = self.addHost( 'h3', ip='10.0.0.100/8',
73                            defaultRoute='via 10.0.0.1' )
74
75         for h, s in [ (h1, s1), (h2, s2), (h3, s3) ]:
76             self.addLink( h, s )
77
78
79 def run():
80     "Test linux router"
81     topo = NetworkTopo()
82     net = Mininet( topo=topo )  # controller is used by s1-s3
83     net.start()
84     info( '*** Routing Table on Router:\n' )
85     info( net[ 'r0' ].cmd( 'route' ) )
86     CLI( net )
87     net.stop()
88
89 if __name__ == '__main__':
90     setLogLevel( 'info' )
91     run()