实验6:开源控制器实践——RYU
一、实验目的
1.能够独立部署RYU控制器;
2.能够理解RYU控制器实现软件定义的集线器原理;
3.能够理解RYU控制器实现软件定义的交换机原理。
二、实验环境
Ubuntu 20.04 Desktop amd64
三、实验要求
(一)基本要求
2.搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。
2.阅读Ryu文档的The First Application一节,运行当中的L2Switch,h1 ping h2或h3,在目标主机使用 tcpdump 验证L2Switch,分析L2Switch和POX的Hub模块有何不同。
3.编程修改L2Switch.py,另存为L2xxxxxxxxx.py,使之和POX的Hub模块的变得一致?(xxxxxxxxx为学号)
(二)进阶要求
1.阅读Ryu关于simple_switch.py和simple_switch_1x.py的实现,以simple_switch_13.py为例,完成其代码的注释工作,并回答下列问题:
a) 代码当中的mac_to_port的作用是什么?
b) simple_switch和simple_switch_13在dpid的输出上有何不同?
c) 相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?
d) simple_switch_13是如何实现流规则下发的?
e) switch_features_handler和_packet_in_handler两个事件在发送流规则的优先级上有何不同?
2.编程实现和ODL实验的一样的硬超时功能。
(三)实验报告
创建拓扑并启动Ryu控制器
创建L2Switch.py并运行icmp测试:
POX和RYU控制器HUB模块相同的是交换机接收到一个数据包时会同样泛洪转发给其他接口,而不同的是POX控制器在给交换机下发流表时可以查看流表项,而RYU不行。
RYU查看流表:
POX查看流表:
修改L2Switch.py并另存为L2212106662.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)
修改后RYU可以查看流表项:
个人总结:
总结:在更改完L2Switch代码后,因为代码里面没有携带Openflow的版本,所以在创建ryu拓扑是要把版本删除