一、实验目的
- 能够独立部署RYU控制器;
- 能够理解RYU控制器实现软件定义的集线器原理;
- 能够理解RYU控制器实现软件定义的交换机原理。
二、实验环境
Ubuntu 20.04 Desktop amd64
三、实验要求
3.1 基本要求
3.1.1 搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。
3.1.1.1 搭建SDN拓扑
sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk
3.1.1.2 连接Ryu控制器,查看网络拓扑
ryu-manager ryu/ryu/app/gui_topology/gui_topology.py --observe-links
3.1.2 阅读Ryu文档的The First Application一节,运行当中的L2Switch,h1 ping h2或h3,在目标主机使用 tcpdump 验证L2Switch,分析L2Switch和POX的Hub模块有何不同。
3.1.2.1 在lab6文件夹下创建L2Swicth.py
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0
class L2Switch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(L2Switch, self).__init__(*args, **kwargs)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
msg = ev.msg
dp = msg.datapath
ofp = dp.ofproto
ofp_parser = dp.ofproto_parser
actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)]
data = None
if msg.buffer_id == ofp.OFP_NO_BUFFER:
data = msg.data
out = ofp_parser.OFPPacketOut(
datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
actions=actions, data = data)
dp.send_msg(out)
3.1.2.2 运行当中的L2Switch,h1 ping h2或h3
- 运行当中的L2Switch
- 通过mininet交互式界面开启h1 h2 h3 终端
xterm h1 h2 h3
- h1 ping h2或h3
3.1.2.3 在目标主机使用 tcpdump 验证L2Switch,分析L2Switch和POX的Hub模块有何不同
- 使用 tcpdump 验证L2Switch
- L2Switch和POX的Hub模块的差异
Hub和L2Switch模块都是洪泛转发,但L2Switch模块下发的流表无法查看,而Hub模块下发的流表可以查看
3.1.3 编程修改L2Switch.py,另存为L2xxxxxxxxx.py,使之和POX的Hub模块的变得一致?(xxxxxxxxx为学号)
3.1.3.1 编程修改L2Switch.py,另存为L2212106651.py
from ryu.base import app_manager
from ryu.ofproto import ofproto_v1_3
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
class hub(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(hub, self).__init__(*args, **kwargs)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_feathers_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
# install flow table-miss flow entry
match = ofp_parser.OFPMatch()
actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)]
# 1\OUTPUT PORT, 2\BUFF IN SWITCH?
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions):
# 1\ datapath for the switch, 2\priority for flow entry, 3\match field, 4\action for packet
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
# install flow
inst = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
mod = ofp_parser.OFPFlowMod(datapath=datapath, priority=priority, match=match, instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
in_port = msg.match['in_port'] # get in port of the packet
# add a flow entry for the packet
match = ofp_parser.OFPMatch()
actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
self.add_flow(datapath, 1, match, actions)
# to output the current packet. for install rules only output later packets
out = ofp_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions)
# buffer id: locate the buffered packet
datapath.send_msg(out)
3.1.3.2 运行效果
- 运行L2xxxxxxxxx.py
- ping通信效果
- 查看流表信息
3.2 进阶要求
阅读Ryu关于simple_switch.py和simple_switch_1x.py的实现,以simple_switch_13.py为例,完成其代码的注释工作,并回答下列问题:
- 代码当中的mac_to_port的作用是什么?
- simple_switch和simple_switch_13在dpid的输出上有何不同?
- 相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?
- simple_switch_13是如何实现流规则下发的?
- switch_features_handler和_packet_in_handler两个事件在发送流规则的优先级上有何不同?
编程实现和ODL实验的一样的硬超时功能。
3.3 实验报告
3.3.1 请用Markdown排版;
3.3.2 所有实验相关代码文件(如有)保存在目录/home/用户名/学号/lab6/中;
3.3.3 基本要求需提交:
- 回答基础要求2中有何不同
Hub和L2Switch模块都是洪泛转发,但L2Switch模块下发的流表无法查看,而Hub模块下发的流表可以查看
- 提交修改过的L2xxxxxxxxx.py代码和能够体现和验证修改的相关截图;
from ryu.base import app_manager
from ryu.ofproto import ofproto_v1_3
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
class hub(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(hub, self).__init__(*args, **kwargs)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_feathers_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
# install flow table-miss flow entry
match = ofp_parser.OFPMatch()
actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)]
# 1\OUTPUT PORT, 2\BUFF IN SWITCH?
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions):
# 1\ datapath for the switch, 2\priority for flow entry, 3\match field, 4\action for packet
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
# install flow
inst = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
mod = ofp_parser.OFPFlowMod(datapath=datapath, priority=priority, match=match, instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
in_port = msg.match['in_port'] # get in port of the packet
# add a flow entry for the packet
match = ofp_parser.OFPMatch()
actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
self.add_flow(datapath, 1, match, actions)
# to output the current packet. for install rules only output later packets
out = ofp_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions)
# buffer id: locate the buffered packet
datapath.send_msg(out)
3.3.4 进阶要求为选做,有完成的同学请提交相关问题回答、代码和运行结果,交换机流表项截图,代码保存目录同要求2,有完成比未完成的上机分数更高。
3.3.5 个人总结,包括但不限于实验难度、实验过程遇到的困难及解决办法,个人感想,不少于200字。
- 本次实验主要是ryu的命令操作弄不明白,远程端口(网络页面端口8080)和远程控制器设置为一样的,导致端口检测错误,无法在图形界面显示控制器,后面通过交远程控制器的端口号修改成6633,实验得以进行。
- 在执行L2Switch.py的程序时,不能通过查看流表查询到空表而是出现有信息的流表。经查阅分析,此次的流表是因为上一次实验所构建的拓扑残留下的流表,使用命令dpctl del-flows删除流表,再重新操作后才实现。