【问题标题】:Creating a topology using python使用 python 创建拓扑
【发布时间】:2018-12-29 19:01:11
【问题描述】:

我想使用 python API 构建一个 mininet 拓扑。我的拓扑应该有 2 个控制器、3 个交换机,每个交换机将连接多个主机(连接的主机数分别为 2、4、1)。代码如下:

#!/usr/bin/python                                                                                                                                                                   

import time
from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info


def net():

    net = Mininet(controller=RemoteController, switch=OVSKernelSwitch)
    c1 = net.addController('c1', controller=RemoteController, ip="127.0.0.1", port=6653)
    c2 = net.addController('c2', controller=RemoteController, ip="127.0.0.1", port=7753)

    s_n = 3 #number of switches
    c_n = 2 #number of controllers
    hosts = []
    amount = [2, 4, 1] # number of hosts that each switch has

    info( "*** Creating switches\n" )
    switches = [ net.addSwitch( 's%s' % s ) for s in range( 1, s_n ) ]

    index1 = 0 
    L_in = 0
    index2 = 0

    info( "*** Creating hosts\n" )
    for s in switches:
        L_in = (L_in + 1)
        if L_in <= len(amount):

            index1 = (index2 + 1)
            index2 = (index1 + amount[L_in]) - 1
            hosts = [ net.addHost( 'h%s' % n ) for n in ( index1, index2 ) ]
            for h in hosts:
                net.addLink( s, h )
        else:
            break

    # Wire up switches
    last = None
    for switch in switches:
        if last:
            net.addLink( last, switch )
        last = switch

    net.build()
    c1.start()
    c2.start()

    for sw in switches[:c_n]:
        sw.start( [c1] )

    for sw in switches[c_n:]:   
        sw.start( [c2] )

    net.start()
    CLI( net )
    net.stop()


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

但是当我运行代码时,它不能正常工作。例如,我期望 3 个交换机,但创建了 2 个交换机。我预计有 7 台主机,但创建了 4 台主机。主机和交换机之间的连接也不正确。这是输出:

*** Creating switches
*** Creating hosts
*** Configuring hosts
h1 h4 h5 h5 
*** Starting controller
c1 c2 
*** Starting 2 switches
s1 s2 ...
*** Starting CLI:
mininet> net
h1 h1-eth0:s1-eth1
h4 h4-eth0:s1-eth2
h5 h5-eth0:s2-eth2
h5 h5-eth0:s2-eth2
s1 lo:  s1-eth1:h1-eth0 s1-eth2:h4-eth0 s1-eth3:s2-eth3
s2 lo:  s2-eth1:h5-eth0 s2-eth2:h5-eth0 s2-eth3:s1-eth3
c1
c2

【问题讨论】:

    标签: python mininet topology


    【解决方案1】:

    这里有很多问题。

    range(1, n)1 没有n-1 生成数字,而不是n

    您定义了函数net,它将影响模块net 的先前(导入?)定义。叫它make_net 什么的。

    L_in 这样的显式循环索引几乎总是一个坏主意,就像index2 这样的非描述性名称一样。

    这样的事情应该会给你这个想法:

    n_switches = 3
    hosts_per_switch = [2, 4, 1]
    
    switches = [addSwitch("s%d" % n + 1) for n in range(n_switches)]
    
    for (switch, number_of_hosts) in zip(switches, hosts_per_switch):  # Pair them.
        hosts = [addHost("h%d" % n + 1) for n in range(number_of_hosts)]
        for host in hosts:
            addLink(switch, host)
    

    【讨论】:

    • 谢谢。我明白了你的意思,我编辑了我的代码。虽然它似乎工作得更好,但每次将新主机添加到交换机时,数字都会被重置。我的意思是我希望有 (h1 h2 for s1), (h3 h4 h5 h6 for s2) 和 (h7 for s3), 但我有 (h1 h2 for s1), (h1 h2 h3 h4 for s2) 和 (h1 for s3) 在输出中,这使得为每个人分配 IP 变得困难。
    • 我通过在第二个 for 之后使用 z 来做到这一点。起初 z=0 但后来这个 z 等于前面的 number_of_hosts ,而不是在主机的 for 循环中的 n+1 ,我使用 z+n+1 。
    猜你喜欢
    • 2018-09-21
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    相关资源
    最近更新 更多