Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.37 KB | None | 0 0
  1. from ryu.base import app_manager
  2. from ryu.controller import ofp_event
  3. from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
  4. from ryu.controller.handler import set_ev_cls
  5. from ryu.ofproto import ofproto_v1_3
  6. from ryu.lib.packet import packet
  7. from ryu.lib.packet import ethernet
  8. from ryu.topology import event
  9. from ryu.lib.packet import ether_types
  10.  
  11. DEFAULT_PRIORITY = 1
  12. FIRST_SWITCH_ID = 1
  13. SECOND_SWITCH_ID = 2
  14. THIRD_SWITCH_ID = 3
  15. FOURTH_SWITCH_ID = 4
  16.  
  17.  
  18. class ExampleApp(app_manager.RyuApp):
  19.     OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
  20.  
  21.     def __init__(self, *args, **kwargs):
  22.         super(ExampleApp, self).__init__(*args, **kwargs)
  23.         self.datapaths = {}
  24.  
  25.     def add_flow(self, datapath, match, actions, priority,idle_timeout,hard_timeout):
  26.         ofprotocol = datapath.ofproto
  27.         parser = datapath.ofproto_parser
  28.         instruction = [parser.OFPInstructionActions(ofprotocol.OFPIT_APPLY_ACTIONS, actions)]
  29.         flow_modification_message = datapath.ofproto_parser.OFPFlowMod(datapath=datapath, match=match,
  30.                                                                        idle_timeout=idle_timeout,
  31.                                                                        hard_timeout=hard_timeout, priority=priority,
  32.                                                                        instructions=instruction)
  33.         datapath.send_msg(flow_modification_message)
  34.  
  35.  
  36.  
  37.     @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
  38.     def switch_features_handler(self, ev):
  39.  
  40.         datapath = ev.msg.datapath
  41.         ofproto = datapath.ofproto
  42.         parser = datapath.ofproto_parser
  43.  
  44.         if datapath.id not in self.datapaths:
  45.             self.datapaths[datapath.id] = datapath
  46.  
  47.         match = parser.OFPMatch()
  48.         actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
  49.                                           ofproto.OFPCML_NO_BUFFER)]
  50.         self.add_flow(datapath, match, actions, 0,1000,1000)
  51.  
  52.         if datapath.id ==1:
  53.             out_port=2
  54.             actions = [parser.OFPActionOutput(out_port)]
  55.             match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.1", ipv4_dst="10.0.0.2")
  56.             self.add_flow(datapath,match,actions,DEFAULT_PRIORITY, 1000, 1000)
  57.  
  58.             out_port = 1
  59.             actions = [parser.OFPActionOutput(out_port)]
  60.             match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.2", ipv4_dst="10.0.0.1")
  61.             self.add_flow(datapath, match, actions, DEFAULT_PRIORITY, 1000, 1000)
  62.  
  63.  
  64.         if datapath.id ==2:
  65.             out_port=2
  66.             actions = [parser.OFPActionOutput(out_port)]
  67.             match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.1", ipv4_dst="10.0.0.2")
  68.             self.add_flow(datapath,match,actions,DEFAULT_PRIORITY, 1000, 1000)
  69.  
  70.             out_port=1
  71.             actions = [parser.OFPActionOutput(out_port)]
  72.             match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.2", ipv4_dst="10.0.0.1")
  73.             self.add_flow(datapath,match,actions,DEFAULT_PRIORITY, 1000, 1000)
  74.  
  75.         if datapath.id == 5:
  76.             out_port = 2
  77.             actions = [parser.OFPActionOutput(out_port)]
  78.             match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.1", ipv4_dst="10.0.0.2")
  79.             self.add_flow(datapath, match, actions, DEFAULT_PRIORITY, 1000, 1000)
  80.  
  81.             out_port = 4
  82.             actions = [parser.OFPActionOutput(out_port)]
  83.             match = parser.OFPMatch(eth_type=0x0800, ipv4_src="10.0.0.2", ipv4_dst="10.0.0.1")
  84.             self.add_flow(datapath, match, actions, DEFAULT_PRIORITY, 1000, 1000)
  85.  
  86.     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
  87.     def packet_in_handler(self, ev):
  88.  
  89.         datapath = ev.msg.datapath
  90.  
  91.         in_port = ev.msg.match['in_port']
  92.  
  93.         received_packet = packet.Packet(ev.msg.data)
  94.         ethernet_frame = received_packet.get_protocol(ethernet.ethernet)
  95.  
  96.         if ethernet_frame.ethertype != ether_types.ETH_TYPE_IP:
  97.             return
  98.  
  99.         destination = ethernet_frame.dst
  100.         source = ethernet_frame.src
  101.  
  102.         self.logger.info("packet in %s %s %s %s", datapath.id, source, destination, in_port)
  103.  
  104.     @set_ev_cls(event.EventPortDelete)
  105.     def delete_port(self, port):
  106.         switch_id = port.port.dpid
  107.  
  108.     @set_ev_cls(event.EventPortAdd)
  109.     def add_port(self, port):
  110.         switch_id = port.port.dpid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement