SDN之基於Ryu控制器的集線器實現

集線器

 

集線器,顧名思義是基於交換機實現的。在OpenVSwitch中,集線器的功能是:收到一個端口發入的數據包後,將該數據包泛洪到其他所有端口(使用的動作爲OFPP_FLOOD)

 

實現代碼

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)

來源:https://edu.sdnlab.com/training/376.html(SDNLAB-未來網絡學院)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章