2

The scenario is composed by two routers connected each other r1-r2 (I found LinuxRouter class in the examples given by Mininet). Connecting 3 hosts to r1, each one belonging to a different subnet, the net ping properly.

class LinuxRouter( Node ):

    def config( self, **params ):
        super( LinuxRouter, self).config( **params )
        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 ):

    def build( self, **_opts ):
        r1 = self.addNode( 'r1', cls=LinuxRouter, ip='192.168.1.1/24' )
        s1, s2, s3, s4= [ self.addSwitch( s ) for s in ( 's1', 's2', 's3','s4') ]

        self.addLink( s1, r1, intfName2='r1-eth1', params2={ 'ip' : '192.168.1.1/24' } )  
        self.addLink( s2, r1, intfName2='r1-eth2', params2={ 'ip' : '172.16.0.1/12' } )
        self.addLink( s3, r1, intfName2='r1-eth3', params2={ 'ip' : '10.0.0.1/8' } )
        self.addLink( s4, r1, intfName2='r1-eth4', params2={ 'ip' : '5.5.5.1/16' } )

        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' )
        r2 = self.addNode( 'r2',cls=LinuxRouter,ip='5.5.5.100/16', defaultRoute='via 5.5.5.1' )

        for h, s in [ (h1, s1), (h2, s2), (h3, s3) ]:
            self.addLink( h, s)
        self.addLink(r2,s4)

def run():
    topo = NetworkTopo()
    net = Mininet( topo=topo)
    net.start()
    CLI( net )
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    run()

If I try to add an host (let's call it h4) to r2 just like I did for the other hosts (adding also a switch, s4, just like r2--s4--h4), it's not able to ping. How can I manage it?

D.Apple
  • 21
  • 1
  • 1
  • 3
  • First of all, shouldn't `r2 = self.addNode( 'r1',cls=LinuxRouter,ip='5.5.5.100/16', defaultRoute='via 5.5.5.1' )` be changed to `r2 = self.addNode( 'r2',cls=LinuxRouter,ip='5.5.5.100/16', defaultRoute='via 5.5.5.1' )`? – Marievi Oct 06 '17 at 06:00
  • Yes sure, just a copy mistake. I'll edit it. – D.Apple Oct 06 '17 at 08:22

1 Answers1

6

Let's reduce the problem to the simplest topology you can have with 2 routers, 2 switches, and 2 hosts.

Topology Image created using this tool.

Here's Mininet's modified linuxrouter.py that creates this topology. (Removed default docstrings and comments and added new, relevant ones.)

This works using the following Mininet VM version: mininet-2.2.2-170321-ubuntu-14.04.4-server

#!/usr/bin/python
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


class LinuxRouter(Node):
    def config(self, **params):
        super(LinuxRouter, self).config(**params)
        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):
    def build(self, **_opts):
        # Add 2 routers in two different subnets
        r1 = self.addHost('r1', cls=LinuxRouter, ip='10.0.0.1/24')
        r2 = self.addHost('r2', cls=LinuxRouter, ip='10.1.0.1/24')

        # Add 2 switches
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')

        # Add host-switch links in the same subnet
        self.addLink(s1,
                     r1,
                     intfName2='r1-eth1',
                     params2={'ip': '10.0.0.1/24'})

        self.addLink(s2,
                     r2,
                     intfName2='r2-eth1',
                     params2={'ip': '10.1.0.1/24'})

        # Add router-router link in a new subnet for the router-router connection
        self.addLink(r1,
                     r2,
                     intfName1='r1-eth2',
                     intfName2='r2-eth2',
                     params1={'ip': '10.100.0.1/24'},
                     params2={'ip': '10.100.0.2/24'})

        # Adding hosts specifying the default route
        d1 = self.addHost(name='d1',
                          ip='10.0.0.251/24',
                          defaultRoute='via 10.0.0.1')
        d2 = self.addHost(name='d2',
                          ip='10.1.0.252/24',
                          defaultRoute='via 10.1.0.1')

        # Add host-switch links
        self.addLink(d1, s1)
        self.addLink(d2, s2)


def run():
    topo = NetworkTopo()
    net = Mininet(topo=topo)

    # Add routing for reaching networks that aren't directly connected
    info(net['r1'].cmd("ip route add 10.1.0.0/24 via 10.100.0.2 dev r1-eth2"))
    info(net['r2'].cmd("ip route add 10.0.0.0/24 via 10.100.0.1 dev r2-eth2"))

    net.start()
    CLI(net)
    net.stop()


if __name__ == '__main__':
    setLogLevel('info')
    run()

Recap: you have two networks 10.0.0.0/24 and 10.1.0.0/24 connected through 2 routers using the network 10.100.0.0/24.

Kohányi Róbert
  • 9,791
  • 4
  • 52
  • 81
edoardesd
  • 109
  • 1
  • 8