注:本文只针对python运行mininet的topo文件的,对于使用sudo mn来运行的需要改代码,具体改法自行琢磨,或者可以私聊我,但是回复应该会慢。
安装sflow之前,需要安装java1.8+的环境
-
更新系统包列表: 执行命令
sudo apt-get update
来更新您的包列表。 -
安装 OpenJDK 8: 使用命令
sudo apt-get install openjdk-8-jdk
来安装 OpenJDK 8。这将安装 Java 开发工具包,包括 JRE 和 JDK。 -
验证安装: 安装完成后,您可以通过运行
java -version
和javac -version
来验证 Java 和 Java 编译器是否已正确安装。(亲测好使)
sflow的安装步骤详见链接,亲测可行sFlow的安装和使用 - 灰信网(软件开发博客聚合)
在mininet的topo文件中,在build下加入以下代码
def getIfInfo(ip):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((ip, 0))
ip = s.getsockname()[0]
ifconfig = check_output(['ifconfig'])
ifs = re.findall(r'^(\S+).*?inet addr:(\S+).*?', ifconfig, re.S|re.M)
print ("#######test######%s,%s".format(re.S,re.M))
for entry in ifs:
if entry[1] == ip:
return entry
def configSFlow(net,collector,ifname):
print ("*** Enabling sFlow:")
sflow = 'ovs-vsctl -- --id=@sflow create sflow agent=%s target=%s sampling=%s polling=%s --' % (ifname,collector,sampling,polling)
for s in net.switches:
sflow += ' -- set bridge %s sflow=@sflow' % s
print (' '.join([s.name for s in net.switches]))
quietRun(sflow)
def configHostsflow(net,collector,ifname):
print ("***enableing host sflow:")
sflow='ovs-vsctl -- --id=@sflow create sflow agent=%s target=%s sampling=%s polling=%s --' %(ifname,collector,sampling,polling)
for s in net.hosts:
sflow += ' --set bridge %s sflow=@sflow' % s
print (sflow)
print (' '.join([s.name for s in net.hosts]))
quietRun(sflow)
def sendTopology(net,agent,collector):
#Search for the network card of each switch and assign a value to parts
print ("*** Sending topology")
topo = {'nodes':{}, 'links':{}}
for s in net.switches:
topo['nodes'][s.name] = {'agent':agent, 'ports':{}}
path = '/sys/devices/virtual/net/'
for child in listdir(path):
parts = re.match('(^s[0-9]+)-(.*)', child)
if parts == None: continue
ifindex = open(path+child+'/ifindex').read().split('\n',1)[0]
topo['nodes'][parts.group(1)]['ports'][child] = {'ifindex': ifindex}
i = 0
for s1 in net.switches:
j = 0
for s2 in net.switches:
if j > i:
intfs = s1.connectionsTo(s2)
for intf in intfs:
s1ifIdx = topo['nodes'][s1.name]['ports'][intf[0].name]['ifindex']
s2ifIdx = topo['nodes'][s2.name]['ports'][intf[1].name]['ifindex']
linkName = '%s-%s' % (s1.name, s2.name)
topo['links'][linkName] = {'node1': s1.name, 'port1': intf[0].name, 'node2': s2.name, 'port2': intf[1].name}
j += 1
i += 1
put('http://'+collector+':8008/topology/json',data=dumps(topo))
在运行主函数中加入
collector = '127.0.0.1'
sampling = 10
polling = 10
在net.start()后加入
result = topo.getIfInfo(collector)
ifname = result[0]
agent = result[1]
topo.configSFlow(net, collector, ifname)
topo.configHostsflow(net, collector, ifname)
topo.sendTopology(net, agent, collector)
最后首先打开sflow,然后正常运行mininet即可连接