Statistics
| Branch: | Tag: | Revision:

mininet / examples / linuxrouter.py @ e0bf8ece

History | View | Annotate | Download (2.19 KB)

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 topology contains a router with three IP subnets:
10
 - 192.168.1.0/24 (interface IP: 192.168.1.1)
11
 - 172.16.0.0/12 (interface IP: 172.16.0.1)
12
 - 10.0.0.0/8 (interface IP: 10.0.0.1)
13

14
 It also contains three hosts, one in each subnet:
15
 - h1 (IP: 192.168.1.100)
16
 - h2 (IP: 172.16.0.100)
17
 - h3 (IP: 10.0.0.100)
18

19
 Routing entries can be added to the routing tables of the 
20
 hosts or router using the "ip route add" or "route add" command.
21
 See the man pages for more details.
22

23
"""
24

    
25
from mininet.topo import Topo
26
from mininet.net import Mininet
27
from mininet.node import Node
28
from mininet.log import setLogLevel, info
29
from mininet.cli import CLI
30

    
31
class LinuxRouter( Node ):
32
    "A Node with IP forwarding enabled."
33

    
34
    def config( self, **params ):
35
        super( LinuxRouter, self).config( **params )
36
        # Enable forwarding on the router
37
        self.cmd( 'sysctl net.ipv4.ip_forward=1' )
38

    
39
    def terminate( self ):
40
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
41
        super( LinuxRouter, self ).terminate()
42

    
43

    
44
class NetworkTopo( Topo ):
45
    "A simple topology of a router with three subnets (one host in each)."
46

    
47
    def build( self, n=2, h=1, **opts ):
48
        router = self.addNode( 'r0', cls=LinuxRouter, ip='192.168.1.1/24' )
49
        h1 = self.addHost( 'h1', ip='192.168.1.100/24', defaultRoute='via 192.168.1.1' )
50
        h2 = self.addHost( 'h2', ip='172.16.0.100/12', defaultRoute='via 172.16.0.1' )
51
        h3 = self.addHost( 'h3', ip='10.0.0.100/8', defaultRoute='via 10.0.0.1' )
52
        self.addLink( h1, router, intfName2='r0-eth1', params2={ 'ip' : '192.168.1.1/24' } )
53
        self.addLink( h2, router, intfName2='r0-eth2', params2={ 'ip' : '172.16.0.1/12' } )
54
        self.addLink( h3, router, intfName2='r0-eth3', params2={ 'ip' : '10.0.0.1/8' } )
55

    
56
def run():
57
    topo = NetworkTopo()
58
    net = Mininet( topo=topo, controller=None ) # no controller needed
59
    net.start()
60
    info( '*** Routing Table on Router\n' )
61
    print net[ 'r0' ].cmd( 'route' )
62
    CLI( net )
63
    net.stop()
64

    
65
if __name__ == '__main__':
66
    setLogLevel( 'info' )
67
    run()