topology.py 3.09 KB
#!/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 ):
    "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) :
    def __init__(self) :
        self.routerIPlist = ['10.0.1.1/24', '10.0.2.1/24', '10.0.3.1/24']
        self.n = 10 # n > 3
        self.routerlist = [0] * self.n
        self.switchlist = [0] * self.n
        self.hostlist = [0] * self.n
        Topo.__init__(self)

    def CreateRouterIP(self) :
        default = '10.0.'
        for i in range(3, self.n) :
            router_ip = default + str(i+1) + '.1/24'
            self.routerIPlist.append(router_ip)
        
    def CreateRouter(self):
        for i in range(0, self.n):
            name = 'r' + str(i)
            self.routerlist[i] = self.addNode(name, cls=LinuxRouter, ip=self.routerIPlist[i])

    def CreateSwitch(self) :
        for i in range(0, self.n):
            name = 's'+str(i)
            self.switchlist[i] = self.addSwitch(name)

    def Createlink(self):
        # router <-> switch
        for i in range(0, self.n):
            name2 = 'r'+str(i)+'-eth1'
            self.addLink(self.switchlist[i], self.routerlist[i], intfName2=name2 , params2={'ip':self.routerIPlist[i]})

        # router <-> router
        self.addLink(self.routerlist[0], self.routerlist[1], intfName1='r0-eth0', intfName2='r1-eth0', params1={'ip':'10.100.0.1/24'}, params2={'ip':'10.100.0.2/24'})

        cnt = 1
        for i in range(2, self.n):
            name1 = 'r'+str(i-1)+'-eth2'
            name2 = 'r'+str(i)+'-eth0'
            ip1 = '10.10'+str(cnt)+'.0.1/24'
            ip2 = '10.10'+str(cnt)+'.0.2/24'
            cnt += 1
            self.addLink(self.routerlist[i-1], self.routerlist[i], intfName1=name1, intfName2=name2, params1={'ip':ip1}, params2={'ip':ip2})

    def CreateHost(self) :
        for i in range(0, self.n) :
            name = 'h'+str(i)
            ip = '10.0.'+str(i+1)+'.100/24'
            defaultroute = 'via 10.0.'+str(i+1)+'.1'
            self.hostlist[i] = self.addHost(name, ip=ip, defaultRoute=defaultroute)

    def build(self, **_opts) :
        self.CreateRouterIP()
        self.CreateRouter()
        self.CreateSwitch()
        self.Createlink()
        self.CreateHost()

        # switch <-> host
        for h, s in zip(self.hostlist, self.switchlist):
            self.addLink(h, s)


def run() :
    topo = NetworkTopo()
    net = Mininet(topo=topo)
    net.start()
    n = 10 # n > 3
    router = [0] * n

    for i in range(0, n):
        name = 'r'+str(i)
        router[i] = net.get(name)
        router[i].cmd('python3 RIP.py&')

        info(net[name].cmd('route'))

    CLI(net)
    net.stop()

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