SDN第七次上機實驗
實驗7:基於REST API的SDN北向應用實踐
一、實驗目的
- 能夠編寫程式呼叫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上的流表資料。
#!/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('h_timeout.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
import json
from requests.auth import HTTPBasicAuth
def http_get(url):
url= url
headers = {'Content-Type':'application/json'}
resp = requests.get(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/flow-node-inventory:table/0'
resp = http_get(url)
res = json.loads(resp.text)
# print(resp.text)
print(len(res['flow-node-inventory:table'][0]['flow']))
-
Ryu
(1) 編寫Python程式,呼叫Ryu的北向介面,實現上述OpenDaylight實驗拓撲上相同的硬超時流表下發。#!/usr/bin/python import requests from requests.auth import HTTPBasicAuth def http_post(url,jstr): url= url headers = {'Content-Type':'application/json'} resp = requests.post(url,jstr,headers=headers) return resp if __name__ == "__main__": url='http://127.0.0.1:8080/stats/flowentry/add' with open('ryu_htimeout.json') as f: jstr = f.read() resp = http_post(url,jstr) print (resp.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服務。
(3) 整理一個Shell指令碼,參考Ryu REST API的文件,利用curl命令,實現和實驗2相同的VLAN。
VLAN_ID | Hosts |
---|---|
0 | h1 h3 |
1 | h2 h4 |
curl -X POST -d '{
"dpid": 1,
"match": {
"in_port": 1
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4096
},
{
"type":"OUTPUT",
"port": 3
}
],
"priority":5
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '
{
"dpid": 1,
"priority":5,
"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": 1,
"match": {
"vlan_vid": 0
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type":"OUTPUT",
"port": 1
}
],
"priority":5
}
' http://localhost:8080/stats/flowentry/add
curl -X POST -d'
{
"dpid": 1,
"match": {
"vlan_vid": 1
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type":"OUTPUT",
"port": 2
}
],
"priority":5
}
' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 2,
"match": {
"in_port": 1
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4096
},
{
"type":"OUTPUT",
"port": 3
}
],
"priority":5
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '
{
"dpid": 2,
"match": {
"in_port": 2
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4097
},
{
"type":"OUTPUT",
"port": 3
}
],
"priority":5
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '
{
"dpid": 2,
"match": {
"vlan_vid": 0
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type":"OUTPUT",
"port": 1
}
],
"priority":5
}
' http://localhost:8080/stats/flowentry/add
curl -X POST -d'
{
"dpid": 2,
"match": {
"vlan_vid": 1
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type":"OUTPUT",
"port": 2
}
],
"priority":5
}' http://localhost:8080/stats/flowentry/add
(二)進階要求
程式設計實現基本要求第2部分Ryu(3)中的VLAN劃分。
(三)實驗報告
- 請用Markdown排版;
- 將所有本實驗相關檔案儲存在目錄/home/使用者名稱/學號/lab7/中;
- (一)只需要提交實現相應Python程式碼和執行結果截圖,其餘文字請勿贅述;
- (二)不做必須要求,有完成的同學請提交Python程式碼和執行結果,檔案儲存目錄參照要求2。
- 個人總結,包括但不限於實驗難度、實驗過程遇到的困難及解決辦法,個人感想,不少於200字。
(四)心得體會
-
本次實驗對我來說具有一定難度,實驗期間遇到的第一個問題是執行上一次實驗建立的 L2Switch.py 出現錯誤,報錯“AttributeError: module 'ryu.ofproto.ofproto_v1_0' has no attribute 'OFPET_EXPERIMENTER'”,始終未找到最終原因。最後改用安裝 Ryu 時自帶的simple_switch_13.py,成功啟動。
-
第二個問題是啟動 simple_switch_13.py 時沒有一起執行 ofctl_rest.py 開啟控制器的北向介面,導致下發流表失敗。
-
通過本次實驗學習我瞭解瞭如何正確使用 python 向指定 url 傳送請求來呼叫 REST API,學習
了相關 API 的使用。