编者按:在网络性能评估中一个巨大的挑战就是如何生成真实的网络流量,还好可以通过程序来创造人工的网络流量,通过建立测试环境来模拟真实的状况。本文就以数据中心网络为目标场景,来在mininet仿真环境中尽可能地还原数据中心内部的真实流量情况。目前有两种常用的流量模型:
随机模型:主机向在网络中的另一任意主机以等概率发送数据包
概率模型:在网络中,编号为m的主机分别以概率pt 、pa 、pc 、向主机编号为(m+i)、(m+j)、(m+k)的主机发送数据包

data center two model in mininet realized
我们使用mininet中的iperf工具在网络中生成udp流量,iperf客户端传送数据流到iperf的服务端,由服务端接收并记录相关信 息。mininet自带控制台可供使用的命令虽然已经比较丰富,但却并未给出较为明确的api接口来支持用户自定义命令。在诸如数据中心这样复杂、网络节 点较多的仿真环境中做一些批处理的工作就需要非常大的,比如通过iperf在所有主机之间发生流量。所以我们需要将自定义命令添加到mininet中,在 mininet中完成新命令的拓展。
<a target="_blank"></a>
在mininet中进行功能拓展主要分为3步:
1. 修改mininet/net.py: net模块实现mininet类,是仿真平台mininet的主体类,该类完成节点添加配置、网络基本功能和一些选项功能的实现。我们需要将我们自定义的函数定义在该类中。
<code>class mininet(object): def function(self,**kwargs): #function code</code>
2. 修改mininet/cli.py: cli模块定义了cli类,为米你呢他提供命令行接口,用于解析用户输入的命令,之前定义的自定义命令需要在cli类中通过注册函数注册这条自定义命令。
<code>class cli(cmd): def do_function(self,line): #do_function code</code>
完成命令注册与绑定。
3. 修改bin/mn: 在完成命令注册与绑定后,需要在mininet执行解释器中注册命令与对应执行函数的映射关系。
<code>altspelling = {'pingall':'pingall', 'pingpair':'pingpair', 'iperfudp':'iperfudp', 'function':'function'}</code>
net.py和cli.py均在mininet/mininet目录,mn文件在在mininet/bin目录中。在代码修改完成后需要重新编译安装一遍mininet,即运行:
<code>$~/mininet/util/install.sh -n</code>
二、 两种流量模型在mininet中的实现
任意一台主机以等概率随机地向另外一台主机发起一条udp数据流。
首先,先在两个主机之间进行iperf测试,并且在server端记录,实现iperf_single函数:
<code>def iperf_single( self,hosts=none, udpbw='10m', period=60, port=5001): """run iperf between two hosts using udp. hosts: list of hosts; if none, uses opposite hosts returns: results two-element array of server and client speeds""" 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 +'&')</code>
着为mininet添加自定义命令iperfmulti,依次为每一台主机随机选择另一台主机作为iperf的服务器端,通过调用 iperf_single,自身以客户端身份按照指定参数发送udp流,服务器生成的报告以重定向的方式输出到文件中,使用iperfmulti命令,主 机随机地向另一台主机发起一条恒定带宽的udp数据流。
<code>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"</code>
修改mininet/cli.py
<code>def do_iperfmulti( self, line ): """multi iperf udp test between nodes""" 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, float(period)) else: error('invalid number of args: iperfmulti udpbw period\n' + 'udpbw examples: 1m 120\n')</code>
修改bin/mn
在mininet/bin目录下修改mn文件,将iperfpb加入到对应的列表中。
<code>altspelling = { 'pingall': 'pingall', 'pingpair': 'pingpair', 'iperfudp': 'iperfudp', 'iperfudp': 'iperfudp', 'iperfpb':'iperfpb' }</code>
最后,进入mininet/util目录,重新编译安装mininet
重启mininet,输入iperf,可用table补全iperfpb,从而可使用iperfpb进行流量随机模型的测试。
为mininet添加自定义命令iperfpb,依次为每一台主机(编号为m)分别以概率pt 、pa 、pc 向主机编号为(m+i)、(m+j)、(m+k)的主机发送数据包,通过调用iperf_single,自身以客户端身份按照指定参数发送udp流,服务 器生成的报告以重定向的方式输出到文件中,使用iperfpb命令,主机按概率向其他被选择的主机发起一条恒定带宽的udp数据流。
为完成以一定概率选择主机,我们需要实现一个概率选择函数randompick,这个函数可用于以不同的概率从一个列表中随机地选择一些元素。下面为randompick的实现过程:
<code>def random_pick( self, _list, probabilities): x = random.uniform(0,1) p = none cumulative_probability = 0.0 for item, item_probability in zip(_list, probabilities): cumulative_probability += item_probability p = item if x < cumulative_probability:break return p</code>
修改mininet/net.py
<code>base_port = 5001</code>
<code>server_list = []</code>
<code>client_list = []</code>
<code>client_list = [h for h in self.hosts]</code>
<code>cli_outs = []</code>
<code>ser_outs = []</code>
<code>host_list = []</code>
<code>host_list = [h for h in self.hosts]</code>
<code>pc = 1 - pt - pa</code>
<code>p_list = [pt,pa,pc]</code>
<code>_len = len(self.hosts)</code>
<code>for key in xrange(_len):</code>
<code>client = host_list[key]</code>
<code>access_host = [host_list[(key+i)%_len],host_list[(key+j)%_len],host_list[(key+k)%_len]]</code>
<code>server = self.random_pick(access_host,p_list)</code>
<code>server_list.append(server)</code>
<code>self.iperf_single(hosts = [client, server], udpbw=bw, port=base_port)</code>
<code>sleep(.05)</code>
<code>sleep(period)</code>
<code>print "test has done"</code>
<code>def do_iperfpb(self, line): """multi iperf udp test with probablity"""</code>
<code>args = line.split()</code>
<code>if len(args) == 1:</code>
<code>udpbw = args[ 0 ]</code>
<code>self.mn.iperfmulti(udpbw)</code>
<code>elif len(args) == 2:</code>
<code>period = args[ 1 ]</code>
<code>err = false</code>
<code>self.mn.iperfpb(udpbw, float(period))</code>
<code>else:</code>
<code>error('invalid number of args: iperfmulti udpbw period\n' +</code>
<code>'udpbw examples: 1m 120\n')</code>
<code>altspelling = { 'pingall': 'pingall',</code>
<code>'pingpair': 'pingpair',</code>
<code>'iperfudp': 'iperfudp',</code>
最后,进入mininet/util目录,重新编译安装mininet:
重启mininet,输入iperf,可用table补全iperfpb,从而可使用iperfpb进行流量的概率模型的测试。
原文发布时间:2015-04-30
本文来自云栖合作伙伴“linux中国”