Mininet拓扑学习

Mininet /Mininet API给出了相当丰富的功能,本篇对各函数总结归纳。


拓扑结构

  • Top
  • build()
  • addSwitch()
  • addHost()
  • addLink()
  • start()
  • stop()
  • net.hosts

from mininet.topo import Topo
from mininet.net import Mininet
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')
# Python's range(N) generates 0..N-1
for h in range(n):
host = self.addHost('h%s' % (h + 1))
self.addLink(host, switch)

def simpleTest():
"Create and test a simple network"
topo = SingleSwitchTopo(n=4)
net = Mininet(topo)
net.start()
net.stop()

if __name__ == '__main__':
# Tell mininet to print useful information
setLogLevel('info')
simpleTest()

网络工具

  • pingAll(): 测试网络所有节点的连通性,注意保证节点都在net中,否则不测试。
  • dumpNodeConnections():截获节点之间的连通并进行分析。
  • iperf(): 测试两个节点之间的网络性能,返回网速。

性能设定(以参数方法实现)

  • self.addHost(name, cpu=f):

    可以指定一个小数,意义是让CPU分配一定比例(设定的百分数)的CPU资源给虚拟主机。
  • self.addLink(node1, node2, bw=10, delay='5ms', max_queue_size=1000, loss=10, use_htb=True):

    以bw(bandwidth),delay,以及loss的值建立一个双向连接,最大等待队列长度为1000且启用HTB算法(提高带宽利用率且限制P2P海量软件的下载)。

#!/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()

指令

  • cmd()

由于每一个host主机都是作为bash shell来进行模拟的,所以理论上可以支持所有终端指令

h1 = net.get('h1')
result = h1.cmd('ifconfig')
print result