mininet 如何创建有不同带宽的链路

被问道如何在mininet中创建一个有不同带宽的链路的时候,于是我就总结了一下我知道的方法:

在总结的时候,分别参考了如下的网址:

https://github.com/mininet/mininet/wiki/Introduction-to-Mininet

http://stackoverflow.com/questions/30496161/how-to-set-bandwidth-on-mininet-custom-topology


我总结的方法有以下的三种:

第一种,在创建的网络拓扑的时候,使用 --link 这个选项:

sudo mn --topo linear,10 --link tc,bw=10

这一条命令,就可以创建出一个拓扑是线性的,并且每一条连接都是10m的带宽。

这种创建的方法就是很简单,但是不能够模拟一种情况,不同的链路的带宽不同。对与做网络均衡负载的同学,就不能够适用

第二种,适用 python 脚本来进行创建一个 mininet网络。 使用这种脚本的方法是 python <your_mininet_creation_script.py> 就会创建出一个mininet的拓扑出来了。这种方法,如果要连接外部的控制器的时候,或许也要在里面写,或许也可以在mn的命令行中设置。我那时候在里面进行连接外部控制器的时候,总是出错,就没有继续用这种方式。我没有试过在脚本里面创建拓扑,然后在命令行中连接外部控制器这种情况。

下面附上这种脚本的一个范例,取自于 https://github.com/mininet/mininet/wiki/Introduction-to-Mininet

#!/usr/bin/python

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import CPULimitedHost
from mininet.link import TCLink
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel

class SingleSwitchTopo( Topo ):
    "Single switch connected to n hosts."
    def build( self, n=2 ):
    switch = self.addSwitch( 's1' )
    for h in range(n):
        # Each host gets 50%/n of system CPU
        host = self.addHost( 'h%s' % (h + 1),
                         cpu=.5/n )
        # 10 Mbps, 5ms delay, 2% loss, 1000 packet queue
        self.addLink( host, switch, bw=10, delay='5ms', loss=2,
                          max_queue_size=1000, use_htb=True )

def perfTest():
    "Create network and run simple performance test"
    topo = SingleSwitchTopo( n=4 )
    net = Mininet( topo=topo,
               host=CPULimitedHost, link=TCLink )
    net.start()
    print "Dumping host connections"
    dumpNodeConnections( net.hosts )
    print "Testing network connectivity"
    net.pingAll()
    print "Testing bandwidth between h1 and h4"
    h1, h4 = net.get( 'h1', 'h4' )
    net.iperf( (h1, h4) )
    net.stop()

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

第三种,是我自己也在用的方法,是适用 mininet walkthrough 中的创建拓扑的方法:脚本的适用方法是

sudo mn --custom <your_mininet_topo_creation_script.py> --topo <topo_class_in_your_script>

但是直接在你的脚本中添加语句

self.addLink( host, switch, bw=10, delay='5ms', loss=2, max_queue_size=1000, use_htb=True )

会导致出现没有 bw 这个参数的错误。这是因为没有适用 TCLink class。解决的方法,是在

http://stackoverflow.com/questions/30496161/how-to-set-bandwidth-on-mininet-custom-topology

中得到的启示。只要运行的时候,加上 --link 选项就可以,完整的运行的命令是

sudo mn --custom <your_mininet_topo_creation_script.py> --topo <topo_class_in_your_script> --link tc

OK, That is the end. Happy hacking.

原文地址:https://www.cnblogs.com/hwy89289709/p/6890218.html