1.实验拓扑
实验拓扑图如下:
搭建代码如下:
创建py文件,并编写py代码,代码如下:
from mininet.topo import Topo from mininet.net import Mininet from mininet.node import RemoteController,CPULimitedHost from mininet.link import TCLink from mininet.util import dumpNodeConnections class Mytopo(Topo): def __init__(self): Topo.__init__(self) s=[] for i in range(2): sw = self.addSwitch('s{}'.format(i+1)) s.append(sw) count=1 for sw in s[0:2]: for i in range(3): host = self.addHost('h{}'.format(count)) self.addLink(sw,host) count += 1 self.addLink(s[0],s[1]) topos = {'mytopo': (lambda:Mytopo())}
搭建结果如下
使用net查看拓扑
使用pingall发现都不连通
进入文件夹的ryu文件夹下的app,通过以下的命令连接ryu控制器,结果如下图
ryu-manager ofctl_rest.py
2.使用Ryu的REST API下发流表实现和第2次实验同样的VLAN
按照实验二编写如下脚本,脚本为all.sh,内容如下:
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 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 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 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 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 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 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 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 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 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 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 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
使用sudo sh '/home/ubuntu/Desktop/all.sh' 运行脚本,可以发现ryu控制器已经接受了流表项,结果如下
查看s1和s2的流表,如下如所示:
然后pingall,发现可以ping的通了,由此下发流表成功,得到了和实验二一样的结果