实验6:开源控制器实践——RYU
一、实验目的
能够独立部署RYU控制器;
能够理解RYU控制器实现软件定义的集线器原理;
能够理解RYU控制器实现软件定义的交换机原理。
二、实验环境
Ubuntu 20.04 Desktop amd64
三、实验要求
(一)基本要求
搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑,建立拓扑并连接RYU控制器
通过Ryu的图形界面查看网络拓扑
阅读Ryu文档的The First Application一节,运行当中的L2Switch,h1 ping h2或h3,在目标主机使用 tcpdump 验证L2Switch,分析L2Switch和POX的Hub模块有何不同。
启动L2Switch模块后查看下发流表,pox下查看拓扑流表
二者都采用洪泛转发,所以在h2和h3可以看到都有抓到数据包。但不同之处在于:Ryu中,L2Switch下发的流表无法查看;而POX中Hub则可以查看。
编程修改L2Switch.py,另存为L2xxxxxxxxx.py,使之和POX的Hub模块的变得一致?(xxxxxxxxx为学号)
L2212106692.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)
(二)进阶要求 略
(三)个人总结
前面的实验来说基础的操作不会有问题,但是后面的为L2Switch添加流表的操作较为难,需要借助查阅资料来完成。实验的基础部分难度比较简单主要是要对比分析出了:RYU的L2Switch模块和POX的Hub模块都采用洪泛转发,但不同之处在于:可以在pox的Hub模块运行时查看流表,而无法在ryu的L2Switch模块运行时查看到流表。在L2Switch模块查看流表时报错,通过找资料发现,应该先打开L2Switch模块在启动ryu控制器,构建拓扑图,改正后也成功的ping通,也实现了无法查看流表的效果。在本次实验中,通过阅读RYU文档并查看相关模块的源代码,了解了RYU控制器的工作原理,并比较了RYU的L2Switch模块与POX的Hub模块的异同,还是有所收获的