在 Python 中使用 Mininet() class 创建拓扑时如何提及“--switch=ovsk,protocols=OpenFlow13”?

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

看下面的命令-

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

我想实现此拓扑使用Python代码

到目前为止,我在 python 中得到了以下几行,它们部分实现了上述命令的结果 -

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()

如何在此 python 代码中包含 switchprotocols 部分?

请参考mininet documentation

你的情况:

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()

我想我必须做一个新的 class MyTreeTopoTreeTopo class 相比有一个小的(但非常重要的)变化内置。

这是完整的工作代码 -

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()

备注:

  1. Mininet()的构造函数中,有一个参数叫switch,默认值为OVSKernelSwitch,这里不用提了。
  2. 使用 Python 2 和 运行 具有 root 权限的脚本 - sudo python2 script.py.
  3. 确保在执行前将此脚本粘贴到 mininet 文件夹中。
  4. 这段代码很可能会变得更通用,但就目前而言,它工作得非常好并且可以完成工作。