Mininet script for generic Tree topology with custom bandwidth links

sinhayash picture sinhayash · Jul 14, 2015 · Viewed 11.7k times · Source

Previously I created mininet topology using the command:

sudo mn --topo tree,depth=2,fanout=5 --controller=remote,ip=10.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13, --link tc,bw=1,delay=10ms

I need to specify custom bw values for different links.

How do I make a tree in a generic way, specifying depth and fanout values as TreeNet mentioned here? I will need a setLink(int value, src, dest) to modify links of the tree created.

Till now I have this:

#!/usr/bin/python

from functools import partial
from mininet.cli import CLI
from mininet.link import TCLink
from mininet.log import setLogLevel
from mininet.net import Mininet
from mininet.node import OVSKernelSwitch
from mininet.node import RemoteController
from mininet.topo import Topo
from mininet.util import dumpNodeConnections


class MyNet( Topo ):    
    def __init__( self ):
        "Create custom topo."

        # Initialize topology
        Topo.__init__( self )

        # Add hosts
        h1 = self.addHost( 'h1' )
        h2 = self.addHost( 'h2' )
        ...
        h25 = self.addHost( 'h25' )

    # Add switches
        s1 = self.addSwitch( 's1' )
        ...
        s6 = self.addSwitch( 's6' )

        # Add links
        self.addLink( s2, s1 ,bw=10)
        self.addLink( s3, s1 ,bw=10)
        self.addLink( s4, s1 ,bw=10)
        self.addLink( s5, s1 ,bw=10)
        self.addLink( s6, s1 ,bw=10)

        self.addLink( h1, s2 ,bw=10)
        self.addLink( h2, s2 ,bw=10)
        self.addLink( h3, s2 ,bw=10)
        self.addLink( h4, s2 ,bw=10)
        self.addLink( h5, s2 ,bw=10)

        self.addLink( h6, s3 ,bw=10)
        self.addLink( h7, s3 ,bw=10)
        self.addLink( h8, s3 ,bw=10)
        self.addLink( h9, s3 ,bw=10)
        self.addLink( h10, s3 ,bw=10)

        self.addLink( h11, s4 ,bw=10)
        ...
        self.addLink( h25, s6 ,bw=10)

topos = { 'MyNet': ( lambda: MyNet() ) }

I am calling using:

#sudo mn --custom MyNet.py --topo MyNet --controller=remote,ip=10.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13 --link tc

Answer

Ehsan Ab picture Ehsan Ab · Jul 14, 2015

Here is what made quickly. It seems to be working. I got a little from topolib.py

The code uses recursion. The function addTree() is called inside itself.

__author__ = 'Ehsan'
from mininet.node import CPULimitedHost
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.log import setLogLevel, info
from mininet.node import RemoteController
from mininet.cli import CLI
from mininet.link import TCLink
"""
Instructions to run the topo:
    1. Go to directory where this fil is.
    2. run: sudo -E python <file name>
       In this case it is: sudo -E python Tree_Generic_Topo.py     
"""


class GenericTree(Topo):
    """Simple topology example."""

    def build( self, depth=1, fanout=2 ):
        # Numbering:  h1..N, s1..M
        self.hostNum = 1
        self.switchNum = 1

    def build( self, depth=1, fanout=2 ):
        # Numbering:  h1..N, s1..M
        self.hostNum = 1
        self.switchNum = 1
        # Build topology
        self.addTree(depth, fanout)

    def addTree( self, depth, fanout ):
        """Add a subtree starting with node n.
           returns: last node added"""
        isSwitch = depth > 0
        if isSwitch:
            node = self.addSwitch( 's%s' % self.switchNum )
            self.switchNum += 1
            for _ in range( fanout ):
                child = self.addTree( depth - 1, fanout )
                self.addLink( node, child )
        else:
            node = self.addHost( 'h%s' % self.hostNum )
            self.hostNum += 1
        return node


def run():
    c = RemoteController('c', '0.0.0.0', 6633)
    # Change the args of GenericTree() to your desired values. You could even get them from command line.
    net = Mininet(topo=GenericTree(depth=2, fanout=3), host=CPULimitedHost, controller=None)
    net.addController(c)
    net.start()

    # installStaticFlows( net )
    CLI(net)
    net.stop()

# if the script is run directly (sudo custom/optical.py):
if __name__ == '__main__':
    setLogLevel('info')
    run()