Statistics
| Branch: | Tag: | Revision:

mininet / examples / linuxrouter.py @ 79f5d39d

History | View | Annotate | Download (2.76 KB)

1 8a987b9c Brian O'Connor
#!/usr/bin/python
2
3
"""
4
linuxrouter.py: Example network with Linux IP router
5 5a530af1 Bob Lantz

6 aa4dfda4 Brian O'Connor
This example converts a Node into a router using IP forwarding
7
already built into Linux.
8

9 6da3fcde Bob Lantz
The example topology creates a router and three IP subnets:
10 aa4dfda4 Brian O'Connor

11 6da3fcde Bob Lantz
    - 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 8a987b9c Brian O'Connor

15 6da3fcde Bob Lantz
Each subnet consists of a single host connected to
16
a single switch:
17 2c76ab71 Brian O'Connor

18 6da3fcde Bob Lantz
    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 79f5d39d Bob Lantz
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 8a987b9c Brian O'Connor
"""
29
30
from mininet.topo import Topo
31
from mininet.net import Mininet
32
from mininet.node import Node
33 2c76ab71 Brian O'Connor
from mininet.log import setLogLevel, info
34 8a987b9c Brian O'Connor
from mininet.cli import CLI
35
36 2c76ab71 Brian O'Connor
class LinuxRouter( Node ):
37 aa4dfda4 Brian O'Connor
    "A Node with IP forwarding enabled."
38 8a987b9c Brian O'Connor
39
    def config( self, **params ):
40 2c76ab71 Brian O'Connor
        super( LinuxRouter, self).config( **params )
41 8a987b9c Brian O'Connor
        # 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 2c76ab71 Brian O'Connor
        super( LinuxRouter, self ).terminate()
47 8a987b9c Brian O'Connor
48
49 aa4dfda4 Brian O'Connor
class NetworkTopo( Topo ):
50 6da3fcde Bob Lantz
    "A LinuxRouter connecting three IP subnets"
51 8a987b9c Brian O'Connor
52 49654212 Bob Lantz
    def build( self, **_opts ):
53 6da3fcde Bob Lantz
54 79f5d39d Bob Lantz
        defaultIP = '192.168.1.1/24'  # IP address for r0-eth1
55 6da3fcde Bob Lantz
        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 18aab5b7 Bob Lantz
        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 6da3fcde Bob Lantz
73 79f5d39d Bob Lantz
        for h, s in [ (h1, s1), (h2, s2), (h3, s3) ]:
74 6da3fcde Bob Lantz
            self.addLink( h, s )
75
76 8a987b9c Brian O'Connor
77
def run():
78 18aab5b7 Bob Lantz
    "Test linux router"
79 8a987b9c Brian O'Connor
    topo = NetworkTopo()
80 6da3fcde Bob Lantz
    net = Mininet( topo=topo )  # controller is used by s1-s3
81 8a987b9c Brian O'Connor
    net.start()
82 6da3fcde Bob Lantz
    info( '*** Routing Table on Router:\n' )
83 2c76ab71 Brian O'Connor
    print net[ 'r0' ].cmd( 'route' )
84 aa4dfda4 Brian O'Connor
    CLI( net )
85 8a987b9c Brian O'Connor
    net.stop()
86
87
if __name__ == '__main__':
88 aa4dfda4 Brian O'Connor
    setLogLevel( 'info' )
89 8a987b9c Brian O'Connor
    run()