2019 SDN上机第6次作业
1.实验拓扑
- 实验拓扑
python代码:
from mininet.topo import Topo
class Topo2( Topo ):
def __init__( self ):
# 初始化拓扑
Topo.__init__( self )
# 添加两台SDN交换机和六台交换机
h1 = self.addHost('h1')
h2 = self.addHost('h2')
h3 = self.addHost('h3')
h4 = self.addHost('h4')
h5 = self.addHost('h5')
h6 = self.addHost('h6')
sw1 = self.addSwitch('s1')
sw2 = self.addSwitch('s2')
# 添加连接
self.addLink(h1,sw1,1,1)
self.addLink(h2,sw1,1,2)
self.addLink(h3,sw1,1,3)
self.addLink(sw1,sw2,4,4)
self.addLink(h4,sw2,1,1)
self.addLink(h5,sw2,1,2)
self.addLink(h6,sw2,1,3)
topos = { 'mytopo': ( lambda: Topo2() ) }
- 运行:
sudo mn --custom mytopo.py --topo mytopo --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
2.使用Ryu的REST API下发流表实现和第2次实验同样的VLAN
- 在mininet中进行pingall测试:
- 运行gui_topology.py
注意这里需要进入到RYU中的app文件夹里找到gui_topology文件夹下的gui_topology.py
ryu-manager gui_topology.py
- 根据其所给的网页打开(ctrl+鼠标左键),可看到如下界面:
- 在文本编辑器新建一个文档,写入以下代码
#首先配置交换机s1-----------------------------------------------
#端口1发来的数据
curl -X POST -d '{
"dpid": 1,
"priority":1,
"match":{
"in_port":1
},
"actions":[
{
"type": "PUSH_VLAN", # s1将从端口1发来的数据包打上vlan_tag
"ethertype": 33024 # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧
},
{
"type": "SET_FIELD",
"field": "vlan_vid", # 设置VLAN ID
"value": 4096 # 设置vlan_id的值,VLAN_ID = 0 (0x1000)
},
{
"type": "OUTPUT",
"port": 4
}
]
}' http://127.0.0.1:8080/stats/flowentry/add
#端口号2发来数据
curl -X POST -d '{
"dpid": 1,
"priority":1,
"match":{
"in_port":2
},
"actions":[
{
"type": "PUSH_VLAN", # s1将从端口2发来的数据包打上vlan_tag
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4097 # 设置vlan_id的值,VLAN_ID = 1 (0x1001)
},
{
"type": "OUTPUT",
"port": 4
}
]
}' http://127.0.0.1:8080/stats/flowentry/add
#端口号3发来数据
curl -X POST -d '{
"dpid": 1,
"priority":1,
"match":{
"in_port":3
},
"actions":[
{
"type": "PUSH_VLAN", # s1将从端口3发来的数据包打上vlan_tag
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4098 # 设置vlan_id的值,VLAN_ID = 2 (0x1002)
},
{
"type": "OUTPUT",
"port": 4
}
]
}' http://127.0.0.1:8080/stats/flowentry/add
#对收到的包进行分流
curl -X POST -d '{
"dpid": 1,
"priority":1,
"match":{
"dl_vlan": "0"
},
"actions":[
{
"type": "POP_VLAN", # 将 dl_vlan=0 的包出去 vlan_tag
},
{
"type": "OUTPUT",
"port": 1 # 向端口1转发
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 1,
"priority":1,
"match":{
"dl_vlan": "1"
},
"actions":[
{
"type": "POP_VLAN", # 将 dl_vlan=1 的包出去 vlan_tag
},
{
"type": "OUTPUT",
"port": 2 # 向端口2转发
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 1,
"priority":1,
"match":{
"dl_vlan": "2"
},
"actions":[
{
"type": "POP_VLAN", # 将 dl_vlan=2 的包出去 vlan_tag
},
{
"type": "OUTPUT",
"port": 3 # 向端口3转发
}
]
}' http://localhost:8080/stats/flowentry/add
#然后配置交换机s2-----------------------------------------------
#端口号1发来数据
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": 4
}
]
}' http://127.0.0.1:8080/stats/flowentry/add
#端口号2发来数据
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": 4
}
]
}' http://127.0.0.1:8080/stats/flowentry/add
#端口号3发来数据
curl -X POST -d '{
"dpid": 2,
"priority":1,
"match":{
"in_port":3
},
"actions":[
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4098
},
{
"type": "OUTPUT",
"port": 4
}
]
}' http://127.0.0.1:8080/stats/flowentry/add
#对收到的包进行分流
curl -X POST -d '{
"dpid": 2,
"priority":1,
"match":{
"dl_vlan": "0"
},
"actions":[
{
"type": "POP_VLAN",
},
{
"type": "OUTPUT",
"port": 1
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 2,
"priority":1,
"match":{
"dl_vlan": "1"
},
"actions":[
{
"type": "POP_VLAN",
},
{
"type": "OUTPUT",
"port": 2
}
]
}' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 2,
"priority":1,
"match":{
"dl_vlan": "2"
},
"actions":[
{
"type": "POP_VLAN",
},
{
"type": "OUTPUT",
"port": 3
}
]
}' http://localhost:8080/stats/flowentry/add
以上为s1和s2要下发的流表
- 保存后缀为.sh文件
- 在运行脚本文件之前需要安装curl:
sudo apt-get install curl
- 在mininet和ryu-manager都在运行的情况下,运行脚本:
./flow.sh
- 可在网页中点击对应的交换机查看流表
- 再次进行pingall测试
3.对比两种方法,写出你的实验体会
相比来说,gui_topology.py可以用脚本实现自动化可能更高一些,在有脚本的前提下,对于一些行业之外的人可能更友好,但是脚本中出现错误的话,也不好纠正。而在Open vSwitch直接下发流表可能需要专业人士来使用,因为需要记的指令格式和变量较多,显得比较繁琐。