Spring原始碼筆記
一、實驗目的
在實驗 2 的基礎上進一步熟悉 Mininet 自定義拓撲指令碼,以及與損耗率相關的設
定;初步瞭解 Mininet 安裝時自帶的 POX 控制器指令碼編寫,測試路徑損耗率。
二、實驗任務
h0 向 h1 傳送資料包,由於在 Mininet 指令碼中設定了連線損耗率,在傳輸過程中
會丟失一些包,本次實驗的目的是展示如何通過控制器計算路徑損耗速率(h0
s0-s1-h1)。這裡假設控制器預先知道網路拓撲。控制器將向 s0 和 s1 傳送
flow_stats_request,當控制器接收到來自 s0 的 response 時,將特定流的資料包
數儲存在 input_pkts 中,當控制器接收到來自 s1 的 response 時,將接收到特定
基於上述拓撲,編寫 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 # [email protected] 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 端重新測試,會發現出現丟包現象,但是實際測量出的丟包率會有浮動,鏈路的效能總體受到了限制。