pythonnetwork-programmingsdnmininetopenflow

How to mention "--switch=ovsk,protocols=OpenFlow13" while making a topology using Mininet() class in Python?


Look at the following command -

$ sudo mn --controller=remote,ip=127.0.0.1,port=6653 --switch=ovsk,protocols=OpenFlow13 --topo=tree,depth=2,fanout=4

I want to achieve this topology using Python code.

So far I have got the following lines in python which achieve the above command's result partially -

from mininet.net import Mininet
from mininet.node import RemoteController
from mininet.topolib import TreeTopo

controller = RemoteController('c0', ip='127.0.0.1', port=6653)
topo = TreeTopo(2,4)
net = Mininet(topo=topo, controller=controller)
net.start()

How do I include the switch and protocols part in this python code?


Solution

  • I figured that I'll have to make a new class MyTreeTopo with a small (but very crucial) change as compared to TreeTopo class which comes built-in.

    Here's the complete working code -

    from mininet.topo import Topo
    from mininet.net import Mininet
    from mininet.cli import CLI
    from mininet.node import RemoteController
    
    
    class MyTreeTopo( Topo ):
        """
            Custom definition of TreeTopo where protocols of switch is OpenFlow13.
            It is copied from TreeTopo class with slight change.
        """
        "Topology for a tree network with a given depth and fanout."
    
        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, protocols="OpenFlow13" )  # This line has been modified
                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
    
    
    if __name__ == "__main__":
        #creating topology and connecting to contoller
        controller = RemoteController('c0', ip='127.0.0.1', port=6653)
        topo = MyTreeTopo(depth=2, fanout=4)
        net = Mininet(topo=topo, controller=controller)
        net.start()
    
        #IF you want to start the CLI
        CLI(net)
        #If you want to clean the environment
        net.stop()
    

    Notes:

    1. In the constructor of Mininet(), there is a parameter called switch whose default value is OVSKernelSwitch, so we do not need to mention it.
    2. Use Python 2 and run the script with root privileges - sudo python2 script.py.
    3. Make sure to paste this script inside the folder of mininet before executing.
    4. This code could most likely be made more generic, but for now, it works perfectly fine and does the job.