2019 SDN上机第6次作业
1.实验拓扑
- 实验拓扑
要求:使用Python脚本完成拓扑搭建,并连接ryu控制器
(1)python拓扑代码
from mininet.topo import Topo
class MyTopo(Topo):
def __init__(self):
# initilaize topology
Topo.__init__(self)
# add hosts and switches
h1 = self.addHost('h1')
h2 = self.addHost('h2')
h3 = self.addHost('h3')
h4 = self.addHost('h4')
h5 = self.addHost('h5')
h6 = self.addHost('h6')
s1 = self.addSwitch('s1')
s2 = self.addSwitch('s2')
# add links
self.addLink(h1, s1, 1, 1)
self.addLink(h2, s1, 1, 2)
self.addLink(h3, s1, 1, 3)
self.addLink(s1, s2, 4, 4)
self.addLink(h4, s2, 1, 1)
self.addLink(h5, s2, 1, 2)
self.addLink(h6, s2, 1, 3)
topos = {'mytopo': (lambda: MyTopo())}
(2)终端输入命令建立拓扑
sudo mn --custom exp6.py --topo mytopo --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
(3)输入net命令查看拓扑
(4)输入pingall命令查看连通性
(5)进入ryu文件夹目录下的app文件夹,在app文件夹目录打开终端,通过以下命令连接ryu控制器
2.使用Ryu的REST API下发流表实现和第2次实验同样的VLAN
参考资料
REST API:可以在线学习中国大学MOOC 《软件定义网络》第六讲 北向接口
Ryu控制器的API文档:ryu.app.ofctl_rest
Ryu的拓扑展示
助教博客:基于RYU restful api实现的VLAN网络虚拟化要求
编写脚本,一键执行下发流表。
#交换机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
(1)保存为.sh文件
(2)输入./sh运行 【ps:需要安装crul (sudo apt-get install curl)】
3.对比两种方法,写出你的实验体会
实验二的方法使用ovs命令直接在交换机上下发流表实现交换机,步骤繁琐,但适用于对个别交换机特殊更改的时候使用。
这次实验的方法使用ryu的rest api,用shell脚本下发流表的方式更简单易做,便与修改,可多次执行,更灵活好用,也具有软件定义网络,“软件”的概念。