实验 3:Mininet 实验——测量路径的损耗率
一、实验目的
• 在实验 2 的基础上进一步熟悉 Mininet 自定义拓扑脚本,以及与损耗率相关的设定;初步了解 Mininet 安装时自带的 POX 控制器脚本编写,测试路径损耗率。
二、实验任务
• h0 向 h1 发送数据包,由于在 Mininet 脚本中设置了连接损耗率,在传输过程中会丢失一些包,本次实验的目的是展示如何通过控制器计算路径损耗速率(h0s0-s1-h1)。这里假设控制器预先知道网络拓扑。控制器将向 s0 和 s1 发送flow_stats_request,当控制器接收到来自 s0 的 response 时,将特定流的数据包数保存在 input_pkts 中,当控制器接收到来自 s1 的 response 时,将接收到特定流的数据包数保存在 output_pkts 中,差值就是丢失的数据包数量。 基于上述拓扑,编写 Mininet 脚本,设置特定的交换机间的路径损耗速率,然后编写 POX 控制器脚本,实现对路径的损耗率的测量。
三、实验步骤
1. 实验环境
安装了 Ubuntu 16.04.5 Desktop amd64 的虚拟机
2.实验过程
SDNLAB 实验参考资料:https://www.sdnlab.com/15100.html
(1)新建并编辑 pox 脚本 flowstat.py:
脚本flowstat.py如下,来自于参考资料。
#!/usr/bin/python # standard includes from pox.core import core from pox.lib.util import dpidToStr import pox.openflow.libopenflow_01 as of from pox.lib.addresses import IPAddr, EthAddr # include as part of the betta branch from pox.openflow.of_json import * from pox.lib.recoco import Timer import time log = core.getLogger() src_dpid = 0 dst_dpid = 0 input_pkts = 0 output_pkts = 0 def getTheTime(): #fuction to create a timestamp flock = time.localtime() then = "[%s-%s-%s" %(str(flock.tm_year),str(flock.tm_mon),str(flock.tm_mday)) if int(flock.tm_hour)<10: hrs = "0%s" % (str(flock.tm_hour)) else: hrs = str(flock.tm_hour) if int(flock.tm_min)<10: mins = "0%s" % (str(flock.tm_min)) else: mins = str(flock.tm_min) if int(flock.tm_sec)<10: secs = "0%s" % (str(flock.tm_sec)) else: secs = str(flock.tm_sec) then +="]%s.%s.%s" % (hrs,mins,secs) return then # handler for timer function that sends the requests to all the # switches connected to the controller. def _timer_func (): for connection in core.openflow._connections.values(): connection.send(of.ofp_stats_request(body=of.ofp_flow_stats_request())) connection.send(of.ofp_stats_request(body=of.ofp_port_stats_request())) log.debug("Sent %i flow/port stats request(s)", len(core.openflow._connections)) # handler to display flow statistics received in JSON format # structure of event.stats is defined by ofp_flow_stats() def _handle_flowstats_received (event): #stats = flow_stats_to_list(event.stats) #log.debug("FlowStatsReceived from %s: %s", dpidToStr(event.connection.dpid), stats) global src_dpid, dst_dpid, input_pkts, output_pkts #print "src_dpid=", dpidToStr(src_dpid), "dst_dpid=", dpidToStr(dst_dpid) for f in event.stats: if f.match.dl_type==0x0800 and f.match.nw_dst==IPAddr("192.168.123.2") and f.match.nw_tos==0x64 and event.connection.dpid==src_dpid: #print "input: ", f.byte_count, f.packet_count input_pkts = f.packet_count if f.match.dl_type==0x0800 and f.match.nw_dst==IPAddr("192.168.123.2") and f.match.nw_tos==0x64 and event.connection.dpid==dst_dpid: #print "output: ", f.byte_count, f.packet_count output_pkts = f.packet_count if input_pkts !=0: print getTheTime(), "Path Loss Rate =", (input_pkts-output_pkts)*1.0/input_pkts*100, "%" # handler to display port statistics received in JSON format def _handle_portstats_received (event): #print "\n<<<STATS-REPLY: Return PORT stats for Switch", event.connection.dpid,"at ",getTheTime() #for f in event.stats: #if int(f.port_no)<65534: #print " PortNo:", f.port_no, " Fwd's Pkts:", f.tx_packets, " Fwd's Bytes:", f.tx_bytes, " Rc'd Pkts:", f.rx_packets, " Rc's Bytes:", f.rx_bytes #print " PortNo:", f.port_no, " TxDrop:", f.tx_dropped, " RxDrop:", f.rx_dropped, " TxErr:", f.tx_errors, " RxErr:", f.rx_errors, " CRC:", f.rx_crc_err, " Coll:", f.collisions stats = flow_stats_to_list(event.stats) log.debug("PortStatsReceived from %s: %s", dpidToStr(event.connection.dpid), stats) def _handle_ConnectionUp (event): global src_dpid, dst_dpid print "ConnectionUp: ", dpidToStr(event.connection.dpid) for m in event.connection.features.ports: if m.name == "s0-eth0": src_dpid = event.connection.dpid elif m.name == "s1-eth0": dst_dpid = event.connection.dpid msg = of.ofp_flow_mod() msg.priority =1 msg.idle_timeout = 0 msg.match.in_port =1 msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =1 msg.idle_timeout = 0 msg.match.in_port =2 msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =10 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_tos = 0x64 msg.match.in_port=1 msg.match.nw_dst = "192.168.123.2" msg.actions.append(of.ofp_action_output(port = 2)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =10 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_tos = 0x64 msg.match.nw_dst = "192.168.123.1" msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) # main functiont to launch the module def launch (): # attach handsers to listners core.openflow.addListenerByName("FlowStatsReceived", _handle_flowstats_received) core.openflow.addListenerByName("PortStatsReceived", _handle_portstats_received) core.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp) # timer set to execute every five seconds Timer(1, _timer_func, recurring=True)
在 pox 安装目录下(Mininet 完整安装包含了 pox)执行以下命令运行 pox 脚本
$ ./pox.py flowstat
(2)编辑 Mininet 脚本 mymininet3.py
参照拓扑图,新建并编辑 Mininet 脚本 mymininet3.py,控制器因为安装在本机,所以需修改参考资料代码中的控制器地址为 127.0.0.1:6633。
再执行命令运行 Mininet 脚本 mymininet3.py
Ping 默认是每 1 秒钟测一次,ping 的结果会显示一个丢包率,这里的丢包率是根据 ping 不通的次数占总次数的百分比计算得到的。上图中由于一共 ping 了 20次,每次都能通,所以丢包率是 0。
观察 pox 侧的实时状态更新
平均丢包率为 0,结果符合 Mininet 脚本中设置的损耗率,也有可能出现负值,可以认为没有丢包。
如果修改代码中 s0 和 s1 之间链路的丢包率为 10。
四、实验心得
本次实验难度还好,把整个实验流程看懂后,做下来实验思路很清晰。实验主要是一些细节上的操作花了些时间,比如在pox里由于权限无法编写脚本flowstat.py,所以一开始将写好的代码放在sdn路径,咨询老师后,将脚本移动进去后即可。