Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- from ryu.topology import event
- from ryu.lib.packet import ether_types
- DEFAULT_PRIORITY = 1
- FIRST_SWITCH_ID = 1
- SECOND_SWITCH_ID = 2
- THIRD_SWITCH_ID = 3
- FOURTH_SWITCH_ID = 4
- class ExampleApp(app_manager.RyuApp):
- OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
- def __init__(self, *args, **kwargs):
- super(ExampleApp, self).__init__(*args, **kwargs)
- self.datapaths = {}
- def add_flow(self, datapath, match, actions, priority,idle_timeout,hard_timeout):
- ofprotocol = datapath.ofproto
- parser = datapath.ofproto_parser
- instruction = [parser.OFPInstructionActions(ofprotocol.OFPIT_APPLY_ACTIONS, actions)]
- flow_modification_message = datapath.ofproto_parser.OFPFlowMod(datapath=datapath, match=match,
- idle_timeout=idle_timeout,
- hard_timeout=hard_timeout, priority=priority,
- instructions=instruction)
- datapath.send_msg(flow_modification_message)
- @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
- def switch_features_handler(self, ev):
- datapath = ev.msg.datapath
- ofproto = datapath.ofproto
- parser = datapath.ofproto_parser
- if datapath.id not in self.datapaths:
- self.datapaths[datapath.id] = datapath
- match = parser.OFPMatch()
- actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
- ofproto.OFPCML_NO_BUFFER)]
- self.add_flow(datapath, match, actions, 0,1000,1000)
- if datapath.id ==1:
- out_port=2
- actions = [parser.OFPActionOutput(out_port)]
- match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.1", ipv4_dst="10.0.0.2")
- self.add_flow(datapath,match,actions,DEFAULT_PRIORITY, 1000, 1000)
- out_port = 1
- actions = [parser.OFPActionOutput(out_port)]
- match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.2", ipv4_dst="10.0.0.1")
- self.add_flow(datapath, match, actions, DEFAULT_PRIORITY, 1000, 1000)
- if datapath.id ==2:
- out_port=2
- actions = [parser.OFPActionOutput(out_port)]
- match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.1", ipv4_dst="10.0.0.2")
- self.add_flow(datapath,match,actions,DEFAULT_PRIORITY, 1000, 1000)
- out_port=1
- actions = [parser.OFPActionOutput(out_port)]
- match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.2", ipv4_dst="10.0.0.1")
- self.add_flow(datapath,match,actions,DEFAULT_PRIORITY, 1000, 1000)
- if datapath.id == 5:
- out_port = 2
- actions = [parser.OFPActionOutput(out_port)]
- match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.1", ipv4_dst="10.0.0.2")
- self.add_flow(datapath, match, actions, DEFAULT_PRIORITY, 1000, 1000)
- out_port = 4
- actions = [parser.OFPActionOutput(out_port)]
- match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.2", ipv4_dst="10.0.0.1")
- self.add_flow(datapath, match, actions, DEFAULT_PRIORITY, 1000, 1000)
- @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
- def packet_in_handler(self, ev):
- datapath = ev.msg.datapath
- in_port = ev.msg.match['in_port']
- received_packet = packet.Packet(ev.msg.data)
- ethernet_frame = received_packet.get_protocol(ethernet.ethernet)
- if ethernet_frame.ethertype != ether_types.ETH_TYPE_IP:
- return
- destination = ethernet_frame.dst
- source = ethernet_frame.src
- self.logger.info("packet in %s %s %s %s", datapath.id, source, destination, in_port)
- @set_ev_cls(event.EventPortDelete)
- def delete_port(self, port):
- switch_id = port.port.dpid
- @set_ev_cls(event.EventPortAdd)
- def add_port(self, port):
- switch_id = port.port.dpid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement