软件定义网络-实验 3:Mininet 实验——测量路径的损耗率

 

一、实验目的
在实验 2 的基础上进一步熟悉 Mininet 自定义拓扑脚本,以及与损耗率相关的设
定;初步了解 Mininet 安装时自带的 POX 控制器脚本编写,测试路径损耗率。
二、实验任务


h0 向 h1 发送数据包,由于在 Mininet 脚本中设置了连接损耗率,在传输过程中
会丢失一些包,本次实验的目的是展示如何通过控制器计算路径损耗速率(h0
s0-s1-h1)。这里假设控制器预先知道网络拓扑。控制器将向 s0 和 s1 发送
flow_stats_request,当控制器接收到来自 s0 的 response 时,将特定流的数据包
数保存在 input_pkts 中,当控制器接收到来自 s1 的 response 时,将接收到特定
流的数据包数保存在 output_pkts 中,差值就是丢失的数据包数量。
基于上述拓扑,编写 Mininet 脚本,设置特定的交换机间的路径损耗速率,然后
编写 POX 控制器脚本,实现对路径的损耗率的测量。
三、实验步骤
1. 实验环境
安装了 Ubuntu 18.04.5 Desktop amd64 的虚拟机
2. 实验过程
SDNLAB 实验参考资料:https://www.sdnlab.com/15100.html
(1)新建并编辑 pox 脚本 flowstat.py:
在 pox 安装目录下(Mininet 完整安装包含了 pox)执行以下命令运行 pox 脚本
$ ./pox.py flowstat

现在一起看下 flowstat.py 的关键代码:
第 7 行开始,让 h0 ping h1,监测 s0 和 s1 之间的链路。

如果匹配到以太网类型的包头(0x0800),并且数据包的目的 IP 地址是192.168.123.2(对照后面 Mininet 的脚本发现是 h1), 并且连接到控制器的数据平面设备 id 是 s0(h0 ping h1,链路 s0-s1 上数据包是从 s0 流向 s1,s0 为源,s1 为目的地), 执行 input_pkts = f.packet_count,把数据包数量存入input_pkts;

同理,如果连接到控制器的数据平面设备 id 是 s1,执行 output_pkts = f.packet_count,把数据包数量存入 output_pkts。 

最后求 input_pkts 和 output_pkts 的差值。一般情况下差值为正,说明链路上数据包有损耗。 

 1 def _handle_flowstats_received (event): 
 2    #stats = flow_stats_to_list(event.stats) 
 3    #log.debug("FlowStatsReceived from %s: %s", dpidToStr(event.connection.dpid), stats) 
 4    global src_dpid, dst_dpid, input_pkts, output_pkts 
 5    #print "src_dpid=", dpidToStr(src_dpid), "dst_dpid=", dpidToStr(dst_dpid) 
 6    for f in event.stats: 
 7      if f.match.dl_type==0x0800 and f.match.nw_dst==IPAddr("192.168.123.2") and f.match.nw_tos==0x64 
 8 and event.connection.dpid==src_dpid:  
 9        #print "input: ", f.byte_count, f.packet_count 
10        input_pkts = f.packet_count 
11      if f.match.dl_type==0x0800 and f.match.nw_dst==IPAddr("192.168.123.2") and f.match.nw_tos==0x64 
12 and event.connection.dpid==dst_dpid: 
13        #print "output: ", f.byte_count, f.packet_count   
14        output_pkts = f.packet_count 
15        if input_pkts !=0: 
16          print getTheTime(), "Path Loss Rate =", (input_pkts-output_pkts)*1.0/input_pkts*100, "%" 

以下为完整代码

  1 #!/usr/bin/python
  2 # Copyright 2012 William Yu
  3 # wyu@ateneo.edu
  4 #
  5 # This file is part of POX.
  6 #
  7 # POX is free software: you can redistribute it and/or modify
  8 # it under the terms of the GNU General Public License as published by
  9 # the Free Software Foundation, either version 3 of the License, or
 10 # (at your option) any later version.
 11 #
 12 # POX is distributed in the hope that it will be useful,
 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15 # GNU General Public License for more details.
 16 #
 17 # You should have received a copy of the GNU General Public License
 18 # along with POX. If not, see <http://www.gnu.org/licenses/>.
 19 #
 20  
 21 """
 22 This is a demonstration file created to show how to obtain flow 
 23 and port statistics from OpenFlow 1.0-enabled switches. The flow
 24 statistics handler contains a summary of web-only traffic.
 25 """
 26  
 27 # standard includes
 28 from pox.core import core
 29 from pox.lib.util import dpidToStr
 30 import pox.openflow.libopenflow_01 as of
 31 from pox.lib.addresses import IPAddr, EthAddr
 32  
 33 # include as part of the betta branch
 34 from pox.openflow.of_json import *
 35 from pox.lib.recoco import Timer
 36 import time
 37  
 38 log = core.getLogger()
 39  
 40 src_dpid = 0
 41 dst_dpid = 0
 42 input_pkts = 0
 43 output_pkts = 0
 44  
 45 def getTheTime():  #fuction to create a timestamp
 46   flock = time.localtime()
 47   then = "[%s-%s-%s" %(str(flock.tm_year),str(flock.tm_mon),str(flock.tm_mday))
 48   
 49   if int(flock.tm_hour)<10:
 50     hrs = "0%s" % (str(flock.tm_hour))
 51   else:
 52     hrs = str(flock.tm_hour)
 53   if int(flock.tm_min)<10:
 54     mins = "0%s" % (str(flock.tm_min))
 55   else:
 56     mins = str(flock.tm_min)
 57   if int(flock.tm_sec)<10:
 58     secs = "0%s" % (str(flock.tm_sec))
 59   else:
 60     secs = str(flock.tm_sec)
 61   then +="]%s.%s.%s" % (hrs,mins,secs)
 62   return then
 63  
 64 # handler for timer function that sends the requests to all the
 65 # switches connected to the controller.
 66 def _timer_func ():
 67   for connection in core.openflow._connections.values():
 68     connection.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))
 69     connection.send(of.ofp_stats_request(body=of.ofp_port_stats_request()))
 70   log.debug("Sent %i flow/port stats request(s)", len(core.openflow._connections))
 71  
 72 # handler to display flow statistics received in JSON format
 73 # structure of event.stats is defined by ofp_flow_stats()
 74 def _handle_flowstats_received (event):
 75    #stats = flow_stats_to_list(event.stats)
 76    #log.debug("FlowStatsReceived from %s: %s", dpidToStr(event.connection.dpid), stats)
 77    global src_dpid, dst_dpid, input_pkts, output_pkts
 78    #print "src_dpid=", dpidToStr(src_dpid), "dst_dpid=", dpidToStr(dst_dpid)
 79    for f in event.stats:
 80      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: 
 81        #print "input: ", f.byte_count, f.packet_count
 82        input_pkts = f.packet_count
 83      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:
 84        #print "output: ", f.byte_count, f.packet_count  
 85        output_pkts = f.packet_count
 86        if input_pkts !=0:
 87          print getTheTime(), "Path Loss Rate =", (input_pkts-output_pkts)*1.0/input_pkts*100, "%"
 88  
 89 # handler to display port statistics received in JSON format
 90 def _handle_portstats_received (event):
 91    #print "\n<<<STATS-REPLY: Return PORT stats for Switch", event.connection.dpid,"at ",getTheTime()
 92    #for f in event.stats:
 93       #if int(f.port_no)<65534:
 94         #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
 95         #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 
 96   stats = flow_stats_to_list(event.stats)
 97   log.debug("PortStatsReceived from %s: %s", dpidToStr(event.connection.dpid), stats)
 98  
 99 def _handle_ConnectionUp (event):
100   global src_dpid, dst_dpid
101   print "ConnectionUp: ", dpidToStr(event.connection.dpid)
102   for m in event.connection.features.ports:
103     if m.name == "s0-eth0":
104       src_dpid = event.connection.dpid
105     elif m.name == "s1-eth0":
106       dst_dpid = event.connection.dpid
107  
108   msg = of.ofp_flow_mod()
109   msg.priority =1
110   msg.idle_timeout = 0
111   msg.match.in_port =1
112   msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL))
113   event.connection.send(msg)
114  
115   msg = of.ofp_flow_mod()
116   msg.priority =1
117   msg.idle_timeout = 0
118   msg.match.in_port =2
119   msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL))
120   event.connection.send(msg)
121  
122   msg = of.ofp_flow_mod()
123   msg.priority =10
124   msg.idle_timeout = 0
125   msg.hard_timeout = 0
126   msg.match.dl_type = 0x0800
127   msg.match.nw_tos = 0x64
128   msg.match.in_port=1
129   msg.match.nw_dst = "192.168.123.2"
130   msg.actions.append(of.ofp_action_output(port = 2))
131   event.connection.send(msg)
132  
133   msg = of.ofp_flow_mod()
134   msg.priority =10
135   msg.idle_timeout = 0
136   msg.hard_timeout = 0
137   msg.match.dl_type = 0x0800
138   msg.match.nw_tos = 0x64
139   msg.match.nw_dst = "192.168.123.1"
140   msg.actions.append(of.ofp_action_output(port = 1))
141   event.connection.send(msg)
142     
143 # main functiont to launch the module
144 def launch ():
145   # attach handsers to listners
146   core.openflow.addListenerByName("FlowStatsReceived", 
147     _handle_flowstats_received) 
148   core.openflow.addListenerByName("PortStatsReceived", 
149     _handle_portstats_received) 
150   core.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp)
151  
152   # timer set to execute every five seconds
153   Timer(1, _timer_func, recurring=True)

(2)编辑 Mininet 脚本 mymininet3.py
参照拓扑图,新建并编辑 Mininet 脚本 mymininet3.py,控制器因为安装在本机,
所以需修改参考资料代码中的控制器地址为 127.0.0.1:6633。 设置 s0 和 s1 之间链路的丢包率为 0 

 1 #!/usr/bin/python
 2  
 3 from mininet.net import Mininet
 4 from mininet.node import Node
 5 from mininet.link import TCLink
 6 from mininet.log import  setLogLevel, info
 7 from threading import Timer
 8 from mininet.util import quietRun
 9 from time import sleep
10  
11 def myNet(cname='controller', cargs='-v ptcp:'):
12     "Create network from scratch using Open vSwitch."
13     info( "*** Creating nodes\n" )
14     controller = Node( 'c0', inNamespace=False )
15     switch = Node( 's0', inNamespace=False )
16     switch1 = Node( 's1', inNamespace=False )
17     h0 = Node( 'h0' )
18     h1 = Node( 'h1' )
19     
20     info( "*** Creating links\n" )
21     linkopts0=dict(bw=100, delay='1ms', loss=0)
22     linkopts1=dict(bw=100, delay='1ms', loss=0)
23     link0=TCLink( h0, switch, **linkopts0)
24     link1 = TCLink( switch, switch1, **linkopts1)     
25     link2 = TCLink( h1, switch1, **linkopts0)
26     #print link0.intf1, link0.intf2
27     link0.intf2.setMAC("0:0:0:0:0:1")
28     link1.intf1.setMAC("0:0:0:0:0:2")
29     link1.intf2.setMAC("0:1:0:0:0:1") 
30     link2.intf2.setMAC("0:1:0:0:0:2")
31  
32     info( "*** Configuring hosts\n" )
33     h0.setIP( '192.168.123.1/24' )
34     h1.setIP( '192.168.123.2/24' )
35        
36     info( "*** Starting network using Open vSwitch\n" )
37     switch.cmd( 'ovs-vsctl del-br dp0' )
38     switch.cmd( 'ovs-vsctl add-br dp0' )
39     switch1.cmd( 'ovs-vsctl del-br dp1' )
40     switch1.cmd( 'ovs-vsctl add-br dp1' )
41  
42     controller.cmd( cname + ' ' + cargs + '&' )     
43     for intf in switch.intfs.values():
44         print intf
45         print switch.cmd( 'ovs-vsctl add-port dp0 %s' % intf )
46     for intf in switch1.intfs.values():
47         print intf
48         print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf )
49    
50     # Note: controller and switch are in root namespace, and we
51     # can connect via loopback interface
52     switch.cmd( 'ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633' )
53     switch1.cmd( 'ovs-vsctl set-controller dp1 tcp:127.0.0.1:6633' )
54   
55     info( '*** Waiting for switch to connect to controller' )
56     while 'is_connected' not in quietRun( 'ovs-vsctl show' ):
57         sleep( 1 )
58         info( '.' )
59     info( '\n' )
60  
61     #info( "*** Running test\n" )
62     h0.cmdPrint( 'ping -Q 0x64 -c 20 ' + h1.IP() )
63     
64     sleep( 1 ) 
65     info( "*** Stopping network\n" )
66     controller.cmd( 'kill %' + cname )
67     switch.cmd( 'ovs-vsctl del-br dp0' )
68     switch.deleteIntfs()
69     switch1.cmd( 'ovs-vsctl del-br dp1' )
70     switch1.deleteIntfs()
71     info( '\n' )
72  
73 if __name__ == '__main__':
74     setLogLevel( 'info' )
75     info( '*** Scratch network demo (kernel datapath)\n' )
76     Mininet.init()
77     myNet()

再执行命令运行 Mininet 脚本 mymininet3.py
$ sudo python mymininet3.py

 Ping 默认是每 1 秒钟测一次,ping 的结果会显示一个丢包率,这里的丢包率是根据 ping 不通的次数占总次数的百分比计算得到的。上图中由于一共 ping 了 20次,每次都能通,所以丢包率是 0。 

观察 pox 侧的实时状态更新 

平均丢包率为 0,结果符合 Mininet 脚本中设置的损耗率,也有可能出现负值,可以认为没有丢包。 

如果修改代码中 s0 和 s1 之间链路的丢包率为 10。

info( "*** Creating links\n" )    
linkopts0=dict(bw=100, delay='1ms', loss=0)     linkopts1=dict(bw=100, delay='1ms', loss=10)     
link0=TCLink( h0, switch, **linkopts0)    
link1 = TCLink( switch, switch1, **linkopts1)          
link2 = TCLink( h1, switch1, **linkopts0) 

重新运行 Mininet 脚本 mymininet3.py,20 秒时间的 ping 过程中有 icmp_seq 为2/4/14/16/19/20 共 6 次 ping 不通,所以丢包率计算为 30%。

 POX 端重新测试,会发现出现丢包现象,但是实际测量出的丢包率会有浮动,链路的性能总体受到了限制。

posted @ 2020-09-16 21:16  Lipchordia  阅读(277)  评论(0编辑  收藏  举报