diff --git a/examples/README.md b/examples/README.md index 96e30d75c6f7d69c062fee50bbd1080db4029cf3..45eabb434315053f91694324d8a06c6034b7b04b 100644 --- a/examples/README.md +++ b/examples/README.md @@ -56,6 +56,11 @@ This example shows how to use link and CPU limits. This example shows how to create a custom topology programatically by subclassing Topo, and how to run a series of tests on it. +### linuxrouter.py: + +This example shows how to create and configure a router in Mininet +that uses Linux IP forwarding. + #### miniedit.py: This example demonstrates creating a network via a graphical editor. diff --git a/examples/linuxrouter.py b/examples/linuxrouter.py new file mode 100755 index 0000000000000000000000000000000000000000..488ec3cef508e9fc7b0920d6187c9d9b46957b66 --- /dev/null +++ b/examples/linuxrouter.py @@ -0,0 +1,68 @@ +#!/usr/bin/python + +""" +linuxrouter.py: Example network with Linux IP router + +This example converts a Node into a router using IP forwarding +already built into Linux. + +The topology contains a router with three IP subnets: + - 192.168.1.0/24 (interface IP: 192.168.1.1) + - 172.16.0.0/12 (interface IP: 172.16.0.1) + - 10.0.0.0/8 (interface IP: 10.0.0.1) + + It also contains three hosts, one in each subnet: + - h1 (IP: 192.168.1.100) + - h2 (IP: 172.16.0.100) + - h3 (IP: 10.0.0.100) + + Routing entries can be added to the routing tables of the + hosts or router using the "ip route add" or "route add" command. + See the man pages for more details. + +""" + +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.node import Node +from mininet.log import setLogLevel, info +from mininet.cli import CLI +from mininet.util import irange + +class LinuxRouter( Node ): + "A Node with IP forwarding enabled." + + def config( self, **params ): + super( LinuxRouter, self).config( **params ) + # Enable forwarding on the router + self.cmd( 'sysctl net.ipv4.ip_forward=1' ) + + def terminate( self ): + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) + super( LinuxRouter, self ).terminate() + + +class NetworkTopo( Topo ): + "A simple topology of a router with three subnets (one host in each)." + + def build( self, n=2, h=1, **opts ): + router = self.addNode( 'r0', cls=LinuxRouter, ip='192.168.1.1/24' ) + h1 = self.addHost( 'h1', ip='192.168.1.100/24', defaultRoute='via 192.168.1.1' ) + h2 = self.addHost( 'h2', ip='172.16.0.100/12', defaultRoute='via 172.16.0.1' ) + h3 = self.addHost( 'h3', ip='10.0.0.100/8', defaultRoute='via 10.0.0.1' ) + self.addLink( h1, router, intfName2='r0-eth1', params2={ 'ip' : '192.168.1.1/24' } ) + self.addLink( h2, router, intfName2='r0-eth2', params2={ 'ip' : '172.16.0.1/12' } ) + self.addLink( h3, router, intfName2='r0-eth3', params2={ 'ip' : '10.0.0.1/8' } ) + +def run(): + topo = NetworkTopo() + net = Mininet( topo=topo, controller=None ) # no controller needed + net.start() + info( '*** Routing Table on Router\n' ) + print net[ 'r0' ].cmd( 'route' ) + CLI( net ) + net.stop() + +if __name__ == '__main__': + setLogLevel( 'info' ) + run() diff --git a/examples/test/test_linuxrouter.py b/examples/test/test_linuxrouter.py new file mode 100644 index 0000000000000000000000000000000000000000..60af201446756fcc743bd26130f26835d444bba7 --- /dev/null +++ b/examples/test/test_linuxrouter.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +""" +Test for linuxrouter.py +""" + +import unittest +import pexpect +from mininet.util import quietRun + +class testLinuxRouter( unittest.TestCase ): + + prompt = 'mininet>' + + def testPingall( self ): + "Test connectivity between hosts" + p = pexpect.spawn( 'python -m mininet.examples.linuxrouter' ) + p.expect( self.prompt ) + p.sendline( 'pingall' ) + p.expect ( '(\d+)% dropped' ) + percent = int( p.match.group( 1 ) ) if p.match else -1 + p.expect( self.prompt ) + p.sendline( 'exit' ) + p.wait() + self.assertEqual( percent, 0 ) + + def testRouterPing( self ): + "Test connectivity from h1 to router" + p = pexpect.spawn( 'python -m mininet.examples.linuxrouter' ) + p.expect( self.prompt ) + p.sendline( 'h1 ping -c 1 r0' ) + p.expect ( '(\d+)% packet loss' ) + percent = int( p.match.group( 1 ) ) if p.match else -1 + p.expect( self.prompt ) + p.sendline( 'exit' ) + p.wait() + self.assertEqual( percent, 0 ) + + def testTTL( self ): + "Verify that the TTL is decremented" + p = pexpect.spawn( 'python -m mininet.examples.linuxrouter' ) + p.expect( self.prompt ) + p.sendline( 'h1 ping -c 1 h2' ) + p.expect ( 'ttl=(\d+)' ) + ttl = int( p.match.group( 1 ) ) if p.match else -1 + p.expect( self.prompt ) + p.sendline( 'exit' ) + p.wait() + self.assertEqual( ttl, 63 ) # 64 - 1 + +if __name__ == '__main__': + unittest.main()