Mininet 系列實驗(六)
寫在前面
這次實驗遇到了非常多問題,非常非常多,花了很多時間去解決,還是有一些小問題沒有解決,但是基本上能完成實驗。建議先看完全文再開始做實驗。實驗內容
先看一下本次實驗的拓撲圖:
在該環境下,假設H1 ping H4,初始的路由規則是S1-S2-S5,一秒後,路由轉發規則變為S1-S3-S5,再過一秒,規則變為S1-S4-S5,然後再回到最初的轉發規則S1-S2-S5。通過這個循環調度的例子動態地改變交換機的轉發規則。參考
Mininet動態改變轉發規則實驗實驗環境
虛擬機: Oracle VM VirtualBox Ubuntu16.04LTS
PS:原文中說「本實驗需要兩臺虛擬機,分別安裝POX(4G)和支持OpenFlow1.3協議的Mininet。」而我只在自己的一臺機子上進行實驗,也許這就是出現如此多問題的根源?實驗步驟
1. ~/pox目錄下新建文件lab_controller.py
# cd pox
# vim lab_controller.py
腳本內容如下:
from pox.core import core import pox.openflow.libopenflow_01 as of from pox.lib.util import dpidToStr from pox.lib.addresses import IPAddr, EthAddr from pox.lib.packet.arp import arp from pox.lib.packet.ethernet import ethernet, ETHER_BROADCAST from pox.lib.packet.packet_base import packet_base from pox.lib.packet.packet_utils import * import pox.lib.packet as pkt from pox.lib.recoco import Timer import time log = core.getLogger() s1_dpid=0 s2_dpid=0 s3_dpid=0 s4_dpid=0 s5_dpid=0 s1_p1=0 s1_p4=0 s1_p5=0 s1_p6=0 s2_p1=0 s3_p1=0 s4_p1=0 pre_s1_p1=0 pre_s1_p4=0 pre_s1_p5=0 pre_s1_p6=0 pre_s2_p1=0 pre_s3_p1=0 pre_s4_p1=0 turn=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 = str(flock.tm_min) secs = "0%s" % (str(flock.tm_sec)) else: secs = str(flock.tm_sec) then +="]%s.%s.%s" % (hrs,mins,secs) return then def _timer_func (): global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid,turn #print getTheTime(), "sent the port stats request to s1_dpid" if turn==0: msg = of.ofp_flow_mod() msg.command=of.OFPFC_MODIFY_STRICT msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.4" msg.actions.append(of.ofp_action_output(port = 5)) core.openflow.getConnection(s1_dpid).send(msg) turn=1 return if turn==1: msg = of.ofp_flow_mod() msg.command=of.OFPFC_MODIFY_STRICT msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.4" msg.actions.append(of.ofp_action_output(port = 6)) core.openflow.getConnection(s1_dpid).send(msg) turn=2 return if turn==2: msg = of.ofp_flow_mod() msg.command=of.OFPFC_MODIFY_STRICT msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.4" msg.actions.append(of.ofp_action_output(port = 4)) turn=0 return def _handle_portstats_received (event): global s1_p1,s1_p4, s1_p5, s1_p6, s2_p1, s3_p1, s4_p1 global pre_s1_p1,pre_s1_p4, pre_s1_p5, pre_s1_p6, pre_s2_p1, pre_s3_p1, pre_s4_p1 if event.connection.dpid==s1_dpid: for f in event.stats: if int(f.port_no)<65534: if f.port_no==1: pre_s1_p1=s1_p1 s1_p1=f.rx_packets if f.port_no==4: pre_s1_p4=s1_p4 s1_p4=f.tx_packets #s1_p4=f.tx_bytes if f.port_no==5: pre_s1_p5=s1_p5 s1_p5=f.tx_packets if f.port_no==6: pre_s1_p6=s1_p6 s1_p6=f.tx_packets for f in event.stats: //非常非常非常坑,原文這一行行首多了一個空格,這裏已經是修改好的 if int(f.port_no)<65534: if f.port_no==1: pre_s2_p1=s2_p1 s2_p1=f.rx_packets #s2_p1=f.rx_bytes if event.connection.dpid==s3_dpid: for f in event.stats: if int(f.port_no)<65534: if f.port_no==1: pre_s3_p1=s3_p1 s3_p1=f.rx_packets if event.connection.dpid==s4_dpid: for f in event.stats: if int(f.port_no)<65534: if f.port_no==1: pre_s4_p1=s4_p1 s4_p1=f.rx_packets def _handle_ConnectionUp (event): global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid print "ConnectionUp: ",dpidToStr(event.connection.dpid) #remember the connection dpid for switch for m in event.connection.features.ports: if m.name == "s1-eth1": s1_dpid = event.connection.dpid print "s1_dpid=", s1_dpid elif m.name == "s2-eth1": s2_dpid = event.connection.dpid print "s2_dpid=", s2_dpid elif m.name == "s3-eth1": s3_dpid = event.connection.dpid elif m.name == "s4-eth1": s4_dpid = event.connection.dpid print "s4_dpid=", s4_dpid elif m.name == "s5-eth1": s5_dpid = event.connection.dpid print "s5_dpid=", s5_dpid if s1_dpid<>0 and s2_dpid<>0 and s3_dpid<>0 and s4_dpid<>0: Timer(1, _timer_func, recurring=True) def _handle_PacketIn(event): global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid packet=event.parsed if event.connection.dpid==s1_dpid: a=packet.find(‘arp‘) if a and a.protodst=="10.0.0.4": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=4)) event.connection.send(msg) if a and a.protodst=="10.0.0.5": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=5)) event.connection.send(msg) if a and a.protodst=="10.0.0.6": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=6)) event.connection.send(msg) if a and a.protodst=="10.0.0.1": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=1)) event.connection.send(msg) if a and a.protodst=="10.0.0.2": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=2)) event.connection.send(msg) if a and a.protodst=="10.0.0.3": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=3)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.1" msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.2" msg.actions.append(of.ofp_action_output(port = 2)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.3" msg.actions.append(of.ofp_action_output(port = 3)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 1 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.4" msg.actions.append(of.ofp_action_output(port = 4)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.5" msg.actions.append(of.ofp_action_output(port = 5)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.6" msg.actions.append(of.ofp_action_output(port = 6)) event.connection.send(msg) elif event.connection.dpid==s2_dpid: msg = of.ofp_flow_mod() msg.priority =10 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.in_port = 1 msg.match.dl_type=0x0806 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.in_port = 1 msg.match.dl_type=0x0800 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.in_port = 2 msg.match.dl_type=0x0806 msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =10 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.in_port = 2 msg.match.dl_type=0x0800 msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) elif event.connection.dpid==s3_dpid: msg = of.ofp_flow_mod() msg.priority =10 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.in_port = 1 msg.match.dl_type=0x0806 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.in_port = 1 msg.match.dl_type=0x0800 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.in_port = 2 msg.match.dl_type=0x0806 msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =10 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.in_port = 2 msg.match.dl_type=0x0800 msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) elif event.connection.dpid==s4_dpid: msg = of.ofp_flow_mod() msg.priority =10 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.in_port = 1 msg.match.dl_type=0x0806 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.in_port = 1 msg.match.dl_type=0x0800 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.in_port = 2 msg.match.dl_type=0x0806 msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =10 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.in_port = 2 msg.match.dl_type=0x0800 msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) elif event.connection.dpid==s5_dpid: a=packet.find(‘arp‘) if a and a.protodst=="10.0.0.4": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=4)) event.connection.send(msg) if a and a.protodst=="10.0.0.5": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=5)) event.connection.send(msg) if a and a.protodst=="10.0.0.6": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=6)) event.connection.send(msg) if a and a.protodst=="10.0.0.1": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=1)) event.connection.send(msg) if a and a.protodst=="10.0.0.2": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=2)) event.connection.send(msg) if a and a.protodst=="10.0.0.3": msg = of.ofp_packet_out(data=event.ofp) msg.actions.append(of.ofp_action_output(port=3)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.1" msg.actions.append(of.ofp_action_output(port = 1)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.2" msg.actions.append(of.ofp_action_output(port = 2)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.3" msg.actions.append(of.ofp_action_output(port = 3)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.4" msg.actions.append(of.ofp_action_output(port = 4)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.5" msg.actions.append(of.ofp_action_output(port = 5)) event.connection.send(msg) msg = of.ofp_flow_mod() msg.priority =100 msg.idle_timeout = 0 msg.hard_timeout = 0 msg.match.dl_type = 0x0800 msg.match.nw_dst = "10.0.0.6" msg.actions.append(of.ofp_action_output(port = 6)) event.connection.send(msg) def launch (): global start_time core.openflow.addListenerByName("PortStatsReceived",_handle_portstats_received) core.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp) core.openflow.addListenerByName("PacketIn",_handle_PacketIn)
這段代碼原文中是有問題的,如果執行會出現下面的錯誤:
2. 在~/mininet中創建文件mym.py
# cd mininet
# vim mym.py
腳本內容如下:
#!/usr/bin/python from mininet.topo import Topo from mininet.net import Mininet from mininet.node import CPULimitedHost from mininet.link import TCLink from mininet.util import dumpNodeConnections from mininet.log import setLogLevel from mininet.node import Controller from mininet.cli import CLI from functools import partial from mininet.node import RemoteController import os class MyTopo(Topo): "Single switch connected to n hosts." def __init__(self): Topo.__init__(self) s1=self.addSwitch(‘s1‘) s2=self.addSwitch(‘s2‘) s3=self.addSwitch(‘s3‘) s4=self.addSwitch(‘s4‘) s5=self.addSwitch(‘s5‘) h1=self.addHost(‘h1‘) h2=self.addHost(‘h2‘) h3=self.addHost(‘h3‘) h4=self.addHost(‘h4‘) h5=self.addHost(‘h5‘) h6=self.addHost(‘h6‘) self.addLink(h1, s1, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(h2, s1, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(h3, s1, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s1, s2, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s1, s3, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s1, s4, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s2, s5, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s3, s5, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s4, s5, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s5, h4, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s5, h5, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) self.addLink(s5, h6, bw=1, delay=‘10ms‘, loss=0, max_queue_size=1000, use_htb=True) def perfTest(): "Create network and run simple performance test" topo = MyTopo() net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink, controller=partial(RemoteController, ip=‘10.0.0.13‘, port=6633)) //這裏的 ip 改為本地 IP 地址,例如我的是 10.0.2.15 否則會連接不上控制器 net.start() print "Dumping host connections" dumpNodeConnections(net.hosts) h1,h2,h3=net.get(‘h1‘,‘h2‘,‘h3‘) h4,h5,h6=net.get(‘h4‘,‘h5‘,‘h6‘) h1.setMAC("0:0:0:0:0:1") h2.setMAC("0:0:0:0:0:2") h3.setMAC("0:0:0:0:0:3") h4.setMAC("0:0:0:0:0:4") h5.setMAC("0:0:0:0:0:5") h6.setMAC("0:0:0:0:0:6") CLI(net) net.stop() if __name__ == ‘__main__‘: setLogLevel(‘info‘) perfTest()
這裏又是一個坑,參照原文執行會得到以下錯誤:
因為做實驗的時候是在同一臺機子上進行,POX 控制器也是部署在本機上,因此在創建拓撲結構時要把控制器的 IP 地址改為本地的 IP 地址
查看 IP 地址:
ifconfig
3. 運行腳本
首先運行 lab_controller.py
# cd pox
# ./pox.py lab_controller
然後運行 mym.py
# cd mininet
# python mym.py
這時觀察 pox 控制臺,發現出現了一堆 warning 先不理...
在 mininet 中執行 h1 ping -i 0.1 h4,即每秒從 h1 傳送 10 個包到 h4
原文中提到「查看虛POX打印出來的結果顯示先從 s1_p4 (switch 1, port 4) 發出 10 個包,然後是從 s1_p5 ,接著從 s1_p6 ,
如此循環調度」但是實際過程中在 POX 控制臺並沒有看到這些信息,重復嘗試幾次之後,依然看不見。後來在網上看到 POX 是有圖形化界面的,也就是 poxdesk,之後利用 pox 的 Web 界面再次進行實驗。
poxdesk 安裝:
# cd ./pox/ext
# git clone https://github.com/MurphyMc/poxdesk
# cd poxdesk
# wget http://downloads.sourceforge.net/qooxdoo/qooxdoo-2.0.2-sdk.zip
# unzip qooxdoo-2.0.2-sdk.zip
# mv qooxdoo-2.0.2-sdk qx
# cd poxdesk
# ./generate.py
# cd ../../..
參考:SDN常用控制器安裝部署之POX篇
到這裏 poxdesk 就安裝好了。重做實驗的流程都不變,只是在執行 pox 腳本時代碼改為
./pox.py lab_controller web messenger messenger.log_service messenger.ajax_transport openflow.of_service poxdesk
//通用的命令為:./pox.py 腳本名 web messenger messenger.log_service messenger.ajax_transport openflow.of_service poxdesk
之後再運行 mininet 腳本
然後在瀏覽器中打開 http://pox-ip:8000/poxdesk 來訪問界面,其中pox-ip是本機 IP 地址,例如我的是10.0.2.15
依次點擊左下角pox->TableViewer->左上角Switch->第一個交換機00_00_00_00_00_01 觀察目的主機 h4 對應的那一行(也就是 nw_dst 為 10.0.0.4/32 的那一行)可以發現 OUTPUT一直在5、6兩個端口之間變動
這裏還是有一個問題,實驗開頭說路由規則會在三種情況之間變化,但是這樣看來只有兩種情況,不知道是什麽原因,個人推測應該問題應該出在 POX 腳本上
總結
1. 實驗過程中遇到的問題
- 因為之前的實驗在關閉 mininet 之後就可以了,開始實驗前我都沒有執行
「sudo mn -c」
然而這次實驗在運行 mininet 腳本之後,想要再次進行實驗必須先輸入 「sudo mn -c」,否則會報錯 - 在運行 pox 時會出現這樣的錯誤 「error: [Errno 98] Address already in ues」這是因為在上一次實驗結束的時候沒有關閉 pox 控制器。解決方法:重啟...
- 在 h1 ping h4 一段時間後會出現錯誤,具體是為什麽還沒有弄清
其他的問題在實驗步驟中都有提到了
2. 收獲
- 熟悉Mininet自定義拓撲腳本的編寫
- 熟悉編寫POX腳本動態改變轉發規則
這是 Mininet 系列實驗做到現在遇到最多問題的一次,主要是原文中代碼的錯誤本身就比較多。解決問題的過程遠比實驗本身重要的多,收獲也更多。
Mininet 系列實驗(六)