1. 程式人生 > 其它 >第七次SDN實驗

第七次SDN實驗

1 利用Mininet平臺搭建下圖所示網路拓撲,並連線OpenDaylight

2 編寫Python程式,呼叫OpenDaylight的北向介面下發指令刪除s1上的流表資料

#!/usr/bin/python

import requests

from requests.auth import HTTPBasicAuth

def http_detele(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/'
with open('test.json') as f:
jstr = f.read()
resp = http_detele(url)
print(resp.content)

3 編寫Python程式,呼叫OpenDaylight的北向介面下發硬超時流表,實現拓撲內主機h1和h3網路中斷20s

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('206.json') as f:
jstr = f.read()
resp = http_put(url,jstr)
print (resp.content) 4 編寫Python程式,呼叫OpenDaylight的北向介面獲取s1上活動的流表數 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/operational/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/opendaylight-flow-table-statistics:flow-table-statistics
'
with open('206.json') as f:
jstr = f.read()
resp = http_get(url)
print(resp.content) 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服務

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('206-Ryu-1.json') as f:
jstr = f.read()
resp = http_post(url,jstr)
print (resp.content) 3 整理一個Shell指令碼,參考Ryu REST API的文件,利用curl命令,實現和實驗2相同的VLAN 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": 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": 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": 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

心得體會:本次實驗對我來說具有一定難度,實驗期間遇到的第一個問題是執行上一次實驗建立的 L2Switch.py 出現錯誤,報錯“AttributeError: module 'ryu.ofproto.ofproto_v1_0' has no attribute 'OFPET_EXPERIMENTER'”,始終未找到最終原因。最後改用安裝 Ryu 時自帶的simple_switch_13.py,成功啟動。

這次一下這麼大的程式碼量修改了許多次。但也用到了之前的許多知識如RYU的平臺,讓我認識到了如這些工具的重要性,也讓我更加熟悉了平臺的使用。