最小拓扑,1 台交换机下挂 2 台主机
$ sudo mn --topo minimal
简单拓扑,1 台交换机下挂 n 台主机,此处 n=3,n=2 即为最小拓扑
$ sudo mn --topo single,3
线性拓扑,交换机连成一线,每台交换机下挂 1 台主机,此处有 3 台交换机 3 台主机
$ sudo mn --topo linear,3
树形拓扑,基于深度 depth 和扇出 fanout,此处均为 2
$ sudo mn --topo tree, fanout=2,depth=2
执行命令:
$ nano mytopo.py // 复制 Python 代码到 py 文件中
$ sudo python mytopo.py // 执行 py 文件
# coding=UTF-8
from mininet.net import Mininet
from mininet.node import CPULimitedHost
from mininet.link import TCLink
net = Mininet(host=CPULimitedHost, link=TCLink) # 如不限制性能,参数为空
# 创建网络节点
c0 = net.addController()
h1 = net.addHost(‘h1‘, cpu=0.5)
h2 = net.addHost(‘h2‘, cpu=0.5)
h3 = net.addHost(‘h3‘)
h4 = net.addHost(‘h4‘)
s1 = net.addSwitch(‘s1‘)
s2 = net.addSwitch(‘s2‘)
# 创建节点间的链路
net.addLink(h1, s1, bw=10, delay=‘5ms‘,max_queue_size=1000, loss=10, use_htb=True)
net.addLink(h3, s1)
net.addLink(h2, s2, bw=10, delay=‘5ms‘,max_queue_size=1000, loss=10, use_htb=True)
net.addLink(h4, s2)
net.addLink(s1, s2)
# 配置主机 ip
h1.setIP(‘10.0.0.1‘, 24)
h2.setIP(‘10.0.0.2‘, 24)
h3.setIP(‘10.0.0.3‘, 24)
h4.setIP(‘10.0.0.4‘, 24)
net.start()
net.pingAll()
net.stop()
修改之前的 Python 程序,使之可用 iPerf 测试网络拓扑中的指定主机之间的带宽。
# coding=UTF-8
#!/usr/bin/python
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
def IperfTest():
net = Mininet(host=CPULimitedHost, link=TCLink)
c0 = net.addController()
h1 = net.addHost(‘h1‘, cpu=0.5)
h2 = net.addHost(‘h2‘, cpu=0.5)
h3 = net.addHost(‘h3‘)
h4 = net.addHost(‘h4‘)
s1 = net.addSwitch(‘s1‘)
s2 = net.addSwitch(‘s2‘)
net.addLink(h1, s1, bw=10, delay=‘5ms‘,max_queue_size=1000, loss=0, use_htb=True)
net.addLink(h3, s1)
net.addLink(h2, s2, bw=10, delay=‘5ms‘,max_queue_size=1000, loss=0, use_htb=True)
net.addLink(h4, s2)
net.addLink(s1, s2)
h1.setIP(‘10.0.0.1‘, 24)
h2.setIP(‘10.0.0.2‘, 24)
h3.setIP(‘10.0.0.3‘, 24)
h4.setIP(‘10.0.0.4‘, 24)
net.start()
print "Dumping host connections"
dumpNodeConnections(net.hosts)
print "Testing network connectivity"
net.pingAll()
print "Testing bandwidth"
h1, h2, h3, h4 = net.get(‘h1‘, ‘h2‘, ‘h3‘, ‘h4‘)
net.iperf((h1, h3))
net.iperf((h2, h4))
net.stop()
if __name__==‘__main__‘:
setLogLevel(‘info‘) #print the log when Configuring hosts, starting switches and controller
IperfTest()
原文:https://www.cnblogs.com/GKQ2020/p/13670282.html