实验7:基于REST API的SDN北向应用实践
作者:互联网
实验7:基于REST API的SDN北向应用实践
基本实验
实验步骤1
-
利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;
-
首先运行ODL
-
在终端输入sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
-
在浏览器中访问http://127.0.0.1:8181/index.html查看拓扑
实验步骤2
- 编写Python程序,调用OpenDaylight的北向接口下发指令删除s1上的流表数据。
- 编写代码并命名为delete.py
- 在终端执行该代码删除流表数据
#delete.py
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。
- 编写代码并命名为puts.py以及ODL_flowtable.json文件
- mininet命令行内输入h1 ping h3 ,终端输入python3 puts.py执行该程序
#puts.py
import requests
from requests.auth import HTTPBasicAuth
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("./ODL_flowtable.json") as f:
jstr = f.read()
headers = {'Content-Type': 'application/json'}
res = requests.put(url, jstr, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print (res.content)
- ODL_flowtable.json
{
"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
- 在终端执行该代码获取流表数
#get.py
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)
执行结果
实验步骤5
- 编写Python程序,调用Ryu的北向接口,实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。
- 编写代码并命名为ryu_put.py以及flowtable.json
- 首先终端输入命令:ryu-manager ryu/ryu/app/gui_topology/gui_topology.py --observe-links 连接ryu控制器
- 在ryu/ryu/app/文件夹内运行终端输入命令:ryu-manager ryu.app.simple_switch_13 ryu.app.ofctl_rest 运行ryu
- 终端输入curl -X GET http://localhost:8080/stats/flow/1查看流表
- 终端输入命令:sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13建立拓扑
- mininet命令行下输入h1 ping h3
- 终端执行程序,产生中断
#ryu_put.py
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)
- flowtable.json
{
"dpid": 1,
"cookie": 1,
"cookie_mask": 1,
"table_id": 0,
"hard_timeout": 20,
"priority": 65535,
"flags": 1,
"match":{
"in_port":1
},
"actions":[
]
}
执行结果
实验步骤6
- 利用Mininet平台搭建下图所示网络拓扑,要求支持OpenFlow 1.3协议,主机名、交换机名以及端口对应正确。拓扑生成后需连接Ryu,且Ryu应能够提供REST API服务。
- 编写以下代码,命名为topo.py
#topo.py
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/ryu/app/gui_topology/gui_topology.py --observe-links 连接ryu控制器
- 在ryu/ryu/app/文件夹内运行终端输入命令:ryu-manager ryu.app.simple_switch_13 ryu.app.ofctl_rest 运行ryu
- 终端输入curl -X GET http://localhost:8080/stats/flow/1查看流表
- 终端输入命令:sudo sudo mn --custom topo.py --topo mytopo --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13建立拓扑
执行结果
实验步骤7
-
整理一个Shell脚本,参考Ryu REST API的文档,利用curl命令,实现和实验2相同的VLAN。
-
终端输入命令:curl -X DELETE http://localhost:8080/stats/flowentry/clear/1和curl -X DELETE http://localhost:8080/stats/flowentry/clear/2删除流表
-
编写shell脚本,并命名为VLAN.py
# 将主机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
- 运行脚本程序,可在mininet中通过pingall检验VLAN划分结果
执行结果
个人总结
实验难度:
- 较难
实验过程遇到的困难及解决办法
- 第一个遇见的困难是在下发硬超时流表时候,中断的产生忘记了要先ping测试。
- 第二个困难是运行了ryu,但是ping测试却显示各主机均不连通,上网查不到答案,之后看了同学的博客,发现了一样的问题,于是试着做了一遍发现成功了,
命令行需要运行curl -X GET http://localhost:8080/stats/flow/1 查看流表(虽然只是查看流表的操作,可是不知道为啥,运行了以后就能成功ping测试了,还是疑惑)
刚开始会提示要安装curl,如下图,输入命令sudo snap install curl即可完成安装
- 第三个困难,在利用自己代码建立拓扑时候,终端输入时候没有加上custom参数,导致一直连接出错。
- 第四个困难是在最后一个实验步骤一直无法得到正确的VLAN划分结果,查看同学的个人总结,发现是由于没有删除之前遗留的流表所致。
个人感想
- 经过本次实验,通过自己编程来调用ODL的北向接口实现部分功能,使我对基于REST API的SDN北向应用有了深刻的认识,也对流表遗留的危害有了深刻印象,
下次遇见类似问题,必能及时排查出来。对了curl命令部分运用也有了初步学习,对于虚拟网的实现有了更深层次的理解和掌握了。
标签:__,http,REST,port,API,SDN,ryu,type,curl 来源: https://www.cnblogs.com/lkf123/p/15456587.html