标签:应用 建立 主机 control 常用 detail pat div entry
首先源码,解析部分如下,同时可以参考RYU_BOOK上的解释说明
from ryu.base import app_manager from ryu.controller import ofp_event from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER from ryu.controller.handler import set_ev_cls from ryu.ofproto import ofproto_v1_3 from ryu.lib.packet import packet from ryu.lib.packet import ethernet class SimpleSwitch13(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]//OpenFlow1.3版本 def __init__(self, *args, **kwargs): super(SimpleSwitch13, self).__init__(*args, **kwargs) self.mac_to_port = {}\\MAC位址表,用于存放MAC地址和端口之间的映射 \\@set_ev_cls表示事件修饰符,任何一个OpenFlow讯息都会产生一个对应的事件,事件类别的名称规则为 \\ryu.controller.ofp_event.EventOFP+<OpenFlow讯息名称> \\第二个参数表示交换机的状态。表示在CONFIG_DISPATCHER的交换机状态下,接收到交换机的\\SwitchFeatures讯息就会执行对应事件处理**_handle()函数。 @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER) def switch_features_handler(self, ev): \\datapath是交换机实体 datapath = ev.msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser # install table-miss flow entry # # We specify NO BUFFER to max_len of the output action due to # OVS bug. At this moment, if we specify a lesser number, e.g., # 128, OVS will send Packet-In with invalid buffer_id and # truncated packet data. In that case, we cannot output packets # correctly. \\为交换机的流表新增Table-miss Flow Entry项,用于封包在无法匹配到流表项后与之匹配。 \\match表示匹配约束,为空则说明可以匹配所有封包。action表示匹配成功后执行的操作,此处 \\为向Controller端口发送最大数据长度的封包。OFPCML_NO_BUFFER表示不需要在交换机上缓存封包 \\将封包整体发给controller。优先级为0,即最低的优先权。 match = parser.OFPMatch() actions = [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 parser = datapath.ofproto_parser \\Entry的Instruction项,指定为output action中的动作,OFPIT_APPLY_ACTIONS表示动作立即执行 inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)] \\FlowMod讯息的类别为OFPFlowMod,使用FlowMod所产生的实体透过datapath.send_msg()来发送、、\\FlowMod讯息给OpenFlow交换机。 mod = parser.OFPFlowMod(datapath=datapath, priority=priority, match=match, instructions=inst) datapath.send_msg(mod) \\交换机的一般状态下接收交换机的packet-In讯息所作处理 @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER) def _packet_in_handler(self, ev): msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match[‘in_port‘]\\表示封包进入交换器待转发的端口号 pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] \\获取目的和源Mac地址 dst = eth.dst src = eth.src \\获取OpenFlow交换器的标识ID dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port) \\每个datapath建立一个mac位址表,将host的mac地址与交换机对应的端口号进行映射。 # learn a mac address to avoid FLOOD next time. self.mac_to_port[dpid][src] = in_port \\判断目的Mac地址是否存在于Mac位址表中,若不存在则进行洪泛 if dst in self.mac_to_port[dpid]: out_port = self.mac_to_port[dpid][dst] else: out_port = ofproto.OFPP_FLOOD actions = [parser.OFPActionOutput(out_port)] # install a flow to avoid packet_in next time \\添加交换机中的流表项 if out_port != ofproto.OFPP_FLOOD: match = parser.OFPMatch(in_port=in_port, eth_dst=dst) self.add_flow(datapath, 1, match, actions) data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data \\将Packet-Out讯息对应的类别OFPPacketOut的实体发送给交换机 out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=data) datapath.send_msg(out)
标签:应用 建立 主机 control 常用 detail pat div entry
原文地址:https://www.cnblogs.com/manmanchanglu/p/11898626.html