建一个简单的模型,使用一个单交换机,然后链接n个主机形成拓扑,然后对每个链路设置带宽,延迟时间,和丢包率。
这里就选择建一个单交换机和六个主机的作为例子。
创建py脚本生成拓扑:写一个类生成一个单交换机和六个主机的拓扑,并且给每个主机和交换机之间的连接赋予100m的带宽,5ms的延迟和1%的丢包率。然后通过pingall函数测试连接,iperf函数测试主机间的带宽,
1 from mininet.topo import Topo 2 from mininet.net import Mininet 3 from mininet.node import CPULimitedHost 4 from mininet.link import TCLink 5 from mininet.util import dumpNodeConnections 6 from mininet.log import setLogLevel 7 8 9 class SingleSwitchTopo(Topo): 10 def __init__(self, n=2, **opts): 11 Topo.__init__(self, **opts) 12 switch = self.addSwitch('s1') 13 for h in range(n): 14 # Each host gets 50%/n of system CPU 15 host = self.addHost('h%s' % (h + 1), cpu=.5/n) 16 # 100 Mbps, 5ms delay, 1% Loss, 1000 packet queue 17 self.addLink(host, switch, bw=100, delay='5ms', loss=1, 18 max_queue_size=1000, use_htb=True) 19 20 21 def perfTest(): 22 topo = SingleSwitchTopo(n=6) 23 net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink) 24 net.start() 25 print "Dumping host connections" 26 dumpNodeConnections(net.hosts) 27 print "Testing network connectivity" 28 net.pingAll() 29 print "Testing bandwidth between h1 and h4" 30 h1, h4 = net.get('h1', 'h4') 31 net.iperf((h1, h4)) 32 print "Testing bandwidth between h1 and h6" 33 h1, h6 = net.get('h1', 'h6') 34 net.iperf((h1, h6)) 35 net.stop() 36 37 38 if __name__ == '__main__': 39 setLogLevel('info') 40 perfTest()
ps:如果直接执行出现错误找不到py解释器的话,在代码开头加上#!/usr/bin/python。或者直接使用
1 sudo python 文件名.py
执行
结果如下图所示:
瞎搞一波,再试试两个交换机相互连接,然后分别下挂三个主机。link全部赋予10m带宽,5ms延迟和0%的丢包率。
1 from mininet.topo import Topo 2 from mininet.net import Mininet 3 from mininet.node import CPULimitedHost 4 from mininet.link import TCLink 5 from mininet.util import dumpNodeConnections 6 from mininet.log import setLogLevel 7 8 9 class DoubleSwitchTopo(Topo): 10 def __init__(self, n=2, **opts): 11 Topo.__init__(self, **opts) 12 switch1 = self.addSwitch('s1') 13 switch2 = self.addSwitch("s2") 14 self.addLink(switch1, switch2, bw=10, delay='5ms', 15 loss=0, max_queue_size=1000, use_htb=True) 16 for h in range(n/2): 17 host = self.addHost('h%s' % (h + 1), cpu=.5/n) 18 self.addLink(host, switch1, bw=10, delay='5ms', loss=0, 19 max_queue_size=1000, use_htb=True) 20 for h in range(n/2, n): 21 host = self.addHost('h%s' % (h + 1), cpu=.5/n) 22 self.addLink(host, switch2, bw=10, delay='5ms', loss=0, 23 max_queue_size=1000, use_htb=True) 24 25 26 def perfTest(): 27 topo = DoubleSwitchTopo(n=6) 28 net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink) 29 net.start() 30 print "Dumping host connections" 31 dumpNodeConnections(net.hosts) 32 print "Testing network connectivity" 33 net.pingAll() 34 print "Testing bandwidth between h1 and h3" 35 h1, h3 = net.get('h1', 'h3') 36 net.iperf((h1, h3)) 37 print "Testing bandwidth between h4 and h6" 38 h4, h6 = net.get('h4', 'h6') 39 net.iperf((h4, h6)) 40 net.stop() 41 42 43 if __name__ == '__main__': 44 setLogLevel('info') 45 perfTest()
结果如下图: