a)搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。
b)运行当中的L2Switch,h1 ping h2或h3,在目标主机使用 tcpdump 验证L2Switch
h1 ping h2
c)分析L2Switch和POX的Hub模块有何不同
由上述验证结果可知,二者相同之处在于:Hub和L2Switch都是洪泛发送ICMP报⽂,所以在h2和h3可以看到它们都有抓到数据包;而不同之处在于:L2Switch无法查看下发的流表,⽽Hub可以查看。
d)修改L2Switch.py,另存为L2xxxxxxxxx.py,使之和POX的Hub模块的变得一致
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3 # openflow版本:1.3
class L2Switch(app_manager.RyuApp): # 定义一个L2Switch类
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
match = ofp_parser.OFPMatch()
actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions): # 添加流表
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
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): # packet_in_handler方法用于处理packet_in事件
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
in_port = msg.match['in_port']
match = ofp_parser.OFPMatch()
actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
self.add_flow(datapath, 1, match, actions)
out = ofp_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions) # 构造packet_out数据结构
datapath.send_msg(out)
标签:控制器,parser,datapath,actions,ofp,开源,L2Switch,ofproto,RYU
From: https://www.cnblogs.com/zxy-0402/p/16842581.html