目的:创建多数据中心拓扑 iperf使用 通过程序生成真实网络流量
1.mininet创建fattree拓扑:sudo mn --custom fattree.py --topo mytopo --controller=remote,ip=192.168.231.131,port=6653
from mininet.topo import Topo from mininet.net import Mininet from mininet.node import RemoteController,CPULimitedHost from mininet.link import TCLink from mininet.util import dumpNodeConnections class MyTopo( Topo ): "Simple topology example." def __init__( self ): "Create custom topo." # Initialize topology Topo.__init__( self ) L1 = 2 L2 = L1 * 2 L3 = L2 c = [] a = [] e = [] # add core ovs for i in range( L1 ): sw = self.addSwitch( ‘c{}‘.format( i + 1 ) ) c.append( sw ) # add aggregation ovs for i in range( L2 ): sw = self.addSwitch( ‘a{}‘.format( L1 +i + 1 ) ) a.append( sw ) # add edge ovs for i in range( L3 ): sw = self.addSwitch( ‘e{}‘.format( L1 +L2 + i + 1 ) ) e.append( sw ) # add links between core and aggregation ovs for i in range( L1 ): sw1 = c[i] for sw2 in a[i/2::L1/2]: self.addLink( sw2, sw1 ) # add links between aggregation and edge ovs for i in range( 0, L2, 2 ): for sw1 in a[i:i+2]: for sw2 in e[i:i+2]: self.addLink( sw2, sw1 ) #add hosts and its links with edge ovs count = 1 for sw1 in e: for i in range(2): host = self.addHost( ‘h{}‘.format( count ) ) self.addLink( sw1, host ) count += 1 topos = { ‘mytopo‘: ( lambda: MyTopo() ) }
2.开启ryu:sudo ryu-manager --verbose simple_switch_stp_13.py ofctl_rest.py rest_topology.py
注意!:这里为了防止广播风暴,使用了STP协议simple_switch_stp_13.py。
3.先pingall,打流。
4.测试h1与其它主机之间的带宽。
但是mininet里没有所有主机间的带宽测试,需要自定义命令去实现该功能。
5.流量随机模型:所有主机间的随机带宽测试
mininet自定义命令的步骤:
① mininet/net.py定义iperf_single()函数
在两个主机间iperf udp测试,并且在server端记录,实现iperf_single()函数
def iperf_single( self,hosts=None, udpBw=5, period=5, port=5001): """""" if not hosts: return else: assert len( hosts ) == 2 client, server = hosts filename = client.name+‘.out‘ filenameserver = server.name+‘.out‘ delayname = client.name+‘-‘+server.name+‘.delay‘ output( ‘***testing bandwidth between ‘ ) output( "%s and %s\n" % ( client.name, server.name ) ) iperfArgs = ‘iperf -u‘ print "****start server******" server.cmd( iperfArgs + ‘-s‘ + ‘ >> /home/zhao/log/‘ + ‘server-‘+filenameserver + ‘&‘) print "****start client******" client.cmd(iperfArgs + ‘-t ‘ + str(period) + ‘ -c ‘ + server.IP() + ‘ -b ‘+str(udpBw)+‘M‘+ ‘ >> /home/zhao/log/‘+ ‘client-‘ + filename +‘&‘)
②net.py中添加自定义的iperfmulti()函数
依次为每一台主机随机选择另一台主机作为iperf的服务器端,通过调用iperf_single,自身以客户端按照指定参数发送UDP流。
def iperf_single( self,hosts=None, udpBw=‘10M‘, period=60, port=5001): if not hosts: return else: assert len( hosts ) == 2 client, server = hosts filename = client.name[1:] + ‘.out‘ output( ‘*** Iperf: testing bandwidth between ‘ ) output( "%s and %s\n" % ( client.name, server.name ) ) iperfArgs = ‘iperf -u ‘ bwArgs = ‘-b ‘ + udpBw + ‘ ‘ print "***start server***" server.cmd( iperfArgs + ‘-s -i 1‘ + ‘ > /home/zg/log/‘ + filename + ‘&‘) print "***start client***" client.cmd( iperfArgs + ‘-t ‘+ str(period) + ‘ -c ‘ + server.IP() + ‘ ‘ + bwArgs +‘ > /home/zg/log/‘ + ‘client‘ + filename +‘&‘)
如图所示,其中目录需要是真实存在的路径
③mininet/cli.py中注册iperfmulti命令
解析用户输入的命令,net.py定义的iperfmulti命令需要在CLI类中注册。
def do_iperfmulti( self, line ): args=line.split() if len(args) == 1: udpBw = args[ 0 ] self.mn.iperfMulti(udpBw) elif len(args)== 2: udpBw = args[ 0 ] period = args[ 1 ] err = False self.mn.iperfMulti(udpBw,int(period)) else: error(‘invalid number of args: iperfmulti udpBw \n‘ +‘udpBw examples: 1M\n‘)
④bin/mn中加入iperfmulti可执行命令
将iperfmulti加入到对应的列表中。
def iperfMulti(self, bw, period=60): base_port = 5001 server_list = [] client_list = [h for h in self.hosts] host_list = [] host_list = [h for h in self.hosts] cli_outs = [] ser_outs = [] _len = len(host_list) for i in xrange(0, _len): client = host_list[i] server = client while( server == client ): server = random.choice(host_list) server_list.append(server) self.iperf_single(hosts = [client, server], udpBw=bw, period= period, port=base_port) sleep(.05) base_port += 1 sleep(period) print "test has done"
如图所示。
⑤重新编译mininet
进入mininet/util目录,重新编译安装mininet.
./install.sh -n
=================================================================
重新创建网络,如mn,输入iperf,可用table补全iperfmulti,从而可使用iperfmulti进行流量随机模型的测试。
使用iperfmulti 命令
命令格式:iperfmulti <带宽>
如 图示
随后在/home/zg/log/目录下生成 4个文件
分别对应server1,server2,client1,client2的带宽测试输出文件。输出4个文件是由于作者使用了mininet默认的网络拓扑做实验,网络中只有两台主机,这两台主机分别各当了一次server、client,因此共有两组输出文件。
查看iperf测试结果,例如:gedit 1.out