第七次sdn
阿新 • • 發佈:2021-10-27
一、實驗目的
- 1.能夠編寫程式呼叫OpenDaylight REST API實現特定網路功能;
- 2.能夠編寫程式呼叫Ryu REST API實現特定網路功能。
二、實驗環境
- 1.下載虛擬機器軟體Oracle VisualBox或VMware;
- 2.在虛擬機器中安裝Ubuntu 20.04 Desktop amd64,並完整安裝Mininet、OpenDaylight(Carbon版本)、Postman和Ryu;
三、實驗要求
(一)基本要求
- 1.OpenDaylight
- (1) 利用Mininet平臺搭建下圖所示網路拓撲,並連線OpenDaylight;
- (2) 編寫Python程式,呼叫OpenDaylight的北向介面下發指令刪除s1上的流表資料。
點選檢視程式碼
#!/usr/bin/python import requests from requests.auth import HTTPBasicAuth def http_delete(url): url= url headers = {'Content-Type':'application/json'} resp = requests.delete(url, headers=headers, auth=HTTPBasicAuth('admin', 'admin')) return resp if __name__ == "__main__": url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/' resp = http_delete(url) print(resp.content)
- (3) 編寫Python程式,呼叫OpenDaylight的北向介面下發硬超時流表,實現拓撲內主機h1和h3網路中斷20s。
點選檢視程式碼
#!/usr/bin/python import requests from requests.auth import HTTPBasicAuth def http_put(url,jstr): url= url headers = {'Content-Type':'application/json'} resp = requests.put(url,jstr,headers=headers,auth=HTTPBasicAuth('admin', 'admin')) return resp if __name__ == "__main__": url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1' with open("flowstable.json") as f: jstr = f.read() resp = http_put(url,jstr) print (resp.content)
點選檢視程式碼
{
"flow": [
{
"id": "1",
"match": {
"in-port": "1",
"ethernet-match": {
"ethernet-type": {
"type": "0x0800"
}
},
"ipv4-destination": "10.0.0.3/32"
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"order": "0",
"drop-action": {}
}
]
}
}
]
},
"flow-name": "flow1",
"priority": "65535",
"hard-timeout": "20",
"cookie": "2",
"table_id": "0"
}
]
}
- (4) 編寫Python程式,呼叫OpenDaylight的北向介面獲取s1上活動的流表數。
點選檢視程式碼
#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
if __name__ == "__main__":
url = 'http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/opendaylight-flow-table-statistics:flow-table-statistics'
headers = {'Content-Type': 'application/json'}
res = requests.get(url,headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print (res.content)
點選檢視程式碼
#!/usr/bin/python
import requests
if __name__ == "__main__":
url = 'http://127.0.0.1:8080/stats/flowentry/add'
with open("./flowtable.json") as f:
jstr = f.read()
headers = {'Content-Type': 'application/json'}
res = requests.post(url, jstr, headers=headers)
print (res.content)
點選檢視程式碼
{
"dpid": 1,
"cookie": 1,
"cookie_mask": 1,
"table_id": 0,
"hard_timeout": 20,
"priority": 65535,
"flags": 1,
"match":{
"in_port":1
},
"actions":[
{
"type":"OUTPUT",
"port": 2
}
]
}
- (2) 利用Mininet平臺搭建下圖所示網路拓撲,要求支援OpenFlow 1.3協議,主機名、交換機名以及埠對應正確。拓撲生成後需連線Ryu,且Ryu應能夠提供REST API服務。
點選檢視程式碼
#!/usr/bin/env python
from mininet.topo import Topo
class MyTopo(Topo):
def __init__(self):
# initilaize topology
Topo.__init__(self)
self.addSwitch("s1")
self.addSwitch("s2")
self.addHost("h1")
self.addHost("h2")
self.addHost("h3")
self.addHost("h4")
self.addLink("s1", "h1")
self.addLink("s1", "h2")
self.addLink("s2", "h3")
self.addLink("s2", "h4")
self.addLink("s1", "s2")
topos = {'mytopo': (lambda: MyTopo())}
結果:
- (3) 整理一個Shell指令碼,參考Ryu REST API的文件,利用curl命令,實現和實驗2相同的VLAN。
|VLAN_ID|Hosts|
|:--|:--|
|0|h1 h3|
|1|h2 h4|
點選檢視程式碼
# 將主機1,2傳送來的資料包打上vlan標記
curl -X POST -d '{
"dpid": 1,
"priority": 1,
"match":{
"in_port": 1
},
"actions":[
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4096
},
{
"type": "OUTPUT",
"port": 3
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 1,
"priority": 1,
"match":{
"in_port": 2
},
"actions":[
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4097
},
{
"type": "OUTPUT",
"port": 3
}
]
}' http://localhost:8080/stats/flowentry/add
# 將主機3,4傳送來的資料包取出vlan標記
curl -X POST -d '{
"dpid": 1,
"priority": 1,
"match":{
"vlan_vid": 0
},
"actions":[
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type": "OUTPUT",
"port": 1
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 1,
"priority": 1,
"match":{
"vlan_vid": 1
},
"actions":[
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type": "OUTPUT",
"port": 2
}
]
}' http://localhost:8080/stats/flowentry/add
# 將主機3,4傳送來的資料包打上vlan標記
curl -X POST -d '{
"dpid": 2,
"priority": 1,
"match":{
"in_port": 1
},
"actions":[
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4096
},
{
"type": "OUTPUT",
"port": 3
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 2,
"priority": 1,
"match":{
"in_port": 2
},
"actions":[
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4097
},
{
"type": "OUTPUT",
"port": 3
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 2,
"priority": 1,
"match":{
"vlan_vid": 0
},
"actions":[
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type": "OUTPUT",
"port": 1
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 2,
"priority": 1,
"match":{
"vlan_vid": 1
},
"actions":[
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type": "OUTPUT",
"port": 2
}
]
}' http://localhost:8080/stats/flowentry/add
四、實驗總結
- 難度:難度感覺較大,有很多都沒有做出來結果,系統一直報錯。
- 遇到的問題:
- odl使用出現了問題,發生了不知道什麼問題。之後一直就有問題,只好再次重新安裝。回看之前的部落格裡odl操作,慢慢解決。
- 建立topo的命令與很多都忘記了,回看之前的pdf才知道怎麼操作。
- 在ryu控制器的時候,開啟控制器後建立了拓撲但是無法ping通,刪除了mininet的快取,重新啟動虛擬機器,終於成功。
- 實驗心得:
比較細緻的實驗做起來比之前的難多了,再加上很多基礎知識不是很理解,所以就進步很緩慢。寫程式碼也只能從同學的部落格裡貼來改寫一些做實驗,自己還是太菜啦。希望之後的大作業不要拖後腿。