1.实验拓扑
(1)实验拓扑
(2)使用Python脚本完成拓扑搭建
Python脚本如下:
from mininet.topo import Topo class MyTopo( Topo ): "Simple topology example." def __init__( self ): # Initialize topology Topo.__init__( self ) sw1 = self.addSwitch('s1') sw2 = self.addSwitch('s2') h1 = self.addHost('h1') h2 = self.addHost('h2') h3 = self.addHost('h3') h4 = self.addHost('h4') h5 = self.addHost('h5') h6 = self.addHost('h6') 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: MyTopo() ) }
输入如下命令,创建拓扑:
sudo mn --custom ./kkk.py --topo mytopo --controller=remote,ip=127.0.0.1,port=6653 --switch ovsk,protocols=OpenFlow13
(3)连接ryu控制器
在对应文件中打开终端,输入如下命令,连接ryu控制器:
ryu-manager ofctl_rest.py
输入pingall测试连通性:
输入net,查看各主机与交换机之间的端口情况:
2.使用Ryu的REST API下发流表实现和第2次实验同样的VLAN
(1)编写脚本
s1脚本如下:
#端口号1发来数据 curl -X POST -d '{ "dpid": 1, "priority":1, "match":{ "in_port":1 }, "actions":[ { "type": "PUSH_VLAN", # s1将从主机发来的数据包打上vlan_tag "ethertype": 33024 # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧 }, { "type": "SET_FIELD", "field": "vlan_vid", # 设置VLAN ID "value": 4096 # 设置vlan_id的值 }, { "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将从主机发来的数据包打上vlan_tag "ethertype": 33024 # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧 }, { "type": "SET_FIELD", "field": "vlan_vid", # 设置VLAN ID "value": 4097 # 设置vlan_id的值 }, { "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将从主机发来的数据包打上vlan_tag "ethertype": 33024 # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧 }, { "type": "SET_FIELD", "field": "vlan_vid", # 设置VLAN ID "value": 4098 # 设置vlan_id的值 }, { "type": "OUTPUT", "port": 4 } ] }' http://127.0.0.1:8080/stats/flowentry/add #向端口1转发 curl -X POST -d '{ "dpid": 1, "priority":1, "match":{ "dl_vlan": "0" }, "actions":[ { "type": "POP_VLAN", # 给进入交换机的包去除 vlan_tag }, { "type": "OUTPUT", "port": 1 } ] }' http://localhost:8080/stats/flowentry/add #向端口2转发 curl -X POST -d '{ "dpid": 1, "priority":1, "match":{ "dl_vlan": "1" }, "actions":[ { "type": "POP_VLAN", # 给进入交换机的包去除 vlan_tag }, { "type": "OUTPUT", "port": 2 } ] }' http://localhost:8080/stats/flowentry/add #向端口3转发 curl -X POST -d '{ "dpid": 1, "priority":1, "match":{ "dl_vlan": "2" }, "actions":[ { "type": "POP_VLAN", # 给进入交换机的包去除 vlan_tag }, { "type": "OUTPUT", "port": 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", # s1将从主机发来的数据包打上vlan_tag "ethertype": 33024 # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧 }, { "type": "SET_FIELD", "field": "vlan_vid", # 设置VLAN ID "value": 4096 # 设置vlan_id的值 }, { "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", # s1将从主机发来的数据包打上vlan_tag "ethertype": 33024 # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧 }, { "type": "SET_FIELD", "field": "vlan_vid", # 设置VLAN ID "value": 4097 # 设置vlan_id的值 }, { "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", # s1将从主机发来的数据包打上vlan_tag "ethertype": 33024 # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧 }, { "type": "SET_FIELD", "field": "vlan_vid", # 设置VLAN ID "value": 4098 # 设置vlan_id的值 }, { "type": "OUTPUT", "port": 4 } ] }' http://127.0.0.1:8080/stats/flowentry/add #向端口1转发 curl -X POST -d '{ "dpid": 2, "priority":1, "match":{ "dl_vlan": "0" }, "actions":[ { "type": "POP_VLAN", # 给进入交换机的包去除 vlan_tag }, { "type": "OUTPUT", "port": 1 } ] }' http://localhost:8080/stats/flowentry/add #向端口2转发 curl -X POST -d '{ "dpid": 2, "priority":1, "match":{ "dl_vlan": "1" }, "actions":[ { "type": "POP_VLAN", # 给进入交换机的包去除 vlan_tag }, { "type": "OUTPUT", "port": 2 } ] }' http://localhost:8080/stats/flowentry/add #向端口3转发 curl -X POST -d '{ "dpid": 2, "priority":1, "match":{ "dl_vlan": "2" }, "actions":[ { "type": "POP_VLAN", # 给进入交换机的包去除 vlan_tag }, { "type": "OUTPUT", "port": 3 } ] }' http://localhost:8080/stats/flowentry/add
直接在终端执行:
s1和s2下发流表结果:
(2)使用如下命令,查看流表:
sudo ovs-ofctl -O OpenFlow13 dump-flows s1 sudo ovs-ofctl -O OpenFlow13 dump-flows s2
(3)再次输入pingall测试连通性:
3.对比两种方法,写出你的实验体会
这次实验做下来感觉还蛮顺利的,使用RYU控制器,整个实验过程还是比较有条理的,没有过多繁琐的操作。
对比起来,直接在Open vSwitch下发流表命令行过于繁琐,并且不能直观体现流表内容,而采用Ryu控制器的REST API下发的流表是直接编辑json格式的流表,界面会更清楚且容易理解,但是二者在思想和效果上是一致的。