Statistics
| Branch: | Tag: | Revision:

mininet / examples / linuxrouter.py @ 79f5d39d

History | View | Annotate | Download (2.76 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 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
from mininet.topo import Topo
31
from mininet.net import Mininet
32
from mininet.node import Node
33
from mininet.log import setLogLevel, info
34
from mininet.cli import CLI
35

    
36
class LinuxRouter( Node ):
37
    "A Node with IP forwarding enabled."
38

    
39
    def config( self, **params ):
40
        super( LinuxRouter, self).config( **params )
41
        # Enable forwarding on the router
42
        self.cmd( 'sysctl net.ipv4.ip_forward=1' )
43

    
44
    def terminate( self ):
45
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
46
        super( LinuxRouter, self ).terminate()
47

    
48

    
49
class NetworkTopo( Topo ):
50
    "A LinuxRouter connecting three IP subnets"
51

    
52
    def build( self, **_opts ):
53

    
54
        defaultIP = '192.168.1.1/24'  # IP address for r0-eth1
55
        router = self.addNode( 'r0', cls=LinuxRouter, ip=defaultIP )
56

    
57
        s1, s2, s3 = [ self.addSwitch( s ) for s in 's1', 's2', 's3' ]
58

    
59
        self.addLink( s1, router, intfName2='r0-eth1',
60
                      params2={ 'ip' : defaultIP } )  # for clarity
61
        self.addLink( s2, router, intfName2='r0-eth2',
62
                      params2={ 'ip' : '172.16.0.1/12' } )
63
        self.addLink( s3, router, intfName2='r0-eth3',
64
                      params2={ 'ip' : '10.0.0.1/8' } )
65

    
66
        h1 = self.addHost( 'h1', ip='192.168.1.100/24',
67
                           defaultRoute='via 192.168.1.1' )
68
        h2 = self.addHost( 'h2', ip='172.16.0.100/12',
69
                           defaultRoute='via 172.16.0.1' )
70
        h3 = self.addHost( 'h3', ip='10.0.0.100/8',
71
                           defaultRoute='via 10.0.0.1' )
72

    
73
        for h, s in [ (h1, s1), (h2, s2), (h3, s3) ]:
74
            self.addLink( h, s )
75

    
76

    
77
def run():
78
    "Test linux router"
79
    topo = NetworkTopo()
80
    net = Mininet( topo=topo )  # controller is used by s1-s3
81
    net.start()
82
    info( '*** Routing Table on Router:\n' )
83
    print net[ 'r0' ].cmd( 'route' )
84
    CLI( net )
85
    net.stop()
86

    
87
if __name__ == '__main__':
88
    setLogLevel( 'info' )
89
    run()