一、實驗目的
- 能夠編寫程序調用OpenDaylight REST API實現特定網絡功能;
- 能夠編寫程序調用Ryu REST API實現特定網絡功能。
二、實驗環境
- 下載虛擬機軟件Oracle VisualBox或VMware;
- 在虛擬機中安裝Ubuntu 20.04 Desktop amd64,并完整安裝Mininet、OpenDaylight(Carbon版本)、Postman和Ryu;
三、實驗要求
(一)基本要求
- OpenDaylight
(1) 利用Mininet平臺搭建下圖所示網絡拓撲,并連接OpenDaylight;
![]()
(2) 編寫Python程序,調用OpenDaylight的北向接口下發指令刪除s1上的流表數據。
- Delete.py
#!/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。
Interrupt.py
#!/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)
flowstable.jsoh
{
"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上活動的流表數。
get.py
#!/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)

- Ryu
(1) 編寫Python程序,調用Ryu的北向接口,實現上述OpenDaylight實驗拓撲上相同的硬超時流表下發。
- ryuflow.py
#!/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)
- flowtable2.json
{
"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服務

- topo.py
#!/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())}
打開控制器輸入以下命令
ryu-manager ryu.app.simple_switch_13 ryu.app.ofctl_rest

(3) 整理一個Shell腳本,參考Ryu REST API的文檔,利用curl命令,實現和實驗2相同的VLAN。
| VLAN_ID | Hosts |
|---|---|
| 0 | h1 h3 |
| 1 | h2 h4 |
- shell腳本
# 將主機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

運行shell文件之后

四、實驗總結
- 實驗難度:較難
- 遇到的問題以及解決辦法:在做odl的時候,由于以前一直沒有更改默認的python版本號,以前使用的python命令是python3,但是這次做的時候忘記了,導致一直使用python,打開python文件,結果一直錯誤,但是之后改用了python3 的打開方式之后,解決問題。還有就是在ryu控制器的時候,打開控制器后創建了拓撲但是無法ping通,之后我刪除了mininet的緩存,重新啟動虛擬機,再打開ryu控制器,結果一切順利
- 個人感受,自己以前做過的實驗,做完之后并沒有及時去復習,導致再次做實驗的時候,有些命令已經忘記了,還要繼續去翻找以前的文件,對于虛擬機的知識了解不夠,還需要多加學習。

浙公網安備 33010602011771號