2

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?

2 Answers2

0

Please refer to mininet documentation

in your case:

from mininet.net import Mininet
from mininet.node import RemoteController, OVSSwitch
from mininet.topolib import TreeTopo
from mininet.cli import CLI

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

#IF you want to start the CLI
CLI(net)
#If you want to clean the environment
net.stop()
Giuseppe
  • 658
  • 1
  • 6
  • 14
  • Hi! Thank you for your answer, but this isn't working as expected. I am getting the following error from the remote ONOS server - `Error while processing message from switch [/127.0.0.1:39702 DPID[00:00:00:00:00:00:00:02]]state WAIT_METER_FEATURES_REPLY io.netty.handler.codec.DecoderException: org.projectfloodlight.openflow.exceptions.OFParseError: Wrong length: Expected=40(40), got=32` Note that this is the same error that I'd get if I didn't include the **--switch=ovsk,protocols=OpenFlow13** when running mininet from the terminal. – Kshitiz Srivastava Feb 16 '21 at 18:07
  • can you try with port 6633? – Giuseppe Feb 18 '21 at 14:25
  • I tried with port 6633 but got the same error that I mentioned in the first comment. – Kshitiz Srivastava Feb 19 '21 at 15:21
0

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.
Dharman
  • 30,962
  • 25
  • 85
  • 135