Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 36.61 KB | None | 0 0
  1. # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #    http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15.  
  16. """
  17. Two OpenFlow 1.0 L3 Static Routers and two OpenFlow 1.0 L2 learning switches.
  18. """
  19.  
  20.  
  21. from ryu.base import app_manager
  22. from ryu.controller import ofp_event
  23. from ryu.controller.handler import MAIN_DISPATCHER
  24. from ryu.controller.handler import set_ev_cls
  25. from ryu.ofproto import ofproto_v1_0
  26. from ryu.lib.mac import haddr_to_bin
  27. from ryu.lib.packet import packet
  28. from ryu.lib.packet import ethernet
  29. from ryu.lib.packet import arp
  30. from ryu.lib.packet import ipv4
  31. from ryu.lib.packet import ether_types
  32.  
  33. """
  34. fill in the code here (optional)
  35. """
  36. BCAST = "FF:FF:FF:FF:FF:FF"
  37. LAN1 = "192.168.1.0"
  38. LAN2 = "192.168.2.0"
  39. R1_IP_LEFT = "192.168.1.1"
  40. R1_MAC_LEFT = "00:00:00:00:01:01"
  41. R1_MAC_RIGHT = "00:00:00:00:03:01"
  42. R2_IP_RIGHT = "192.168.2.1"
  43. R2_MAC_LEFT = "00:00:00:00:03:02"
  44. R2_MAC_RIGHT = "00:00:00:00:02:01"
  45.  
  46.  
  47. class SimpleSwitch(app_manager.RyuApp):
  48.     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
  49.  
  50.     def __init__(self, *args, **kwargs):
  51.         super(SimpleSwitch, self).__init__(*args, **kwargs)
  52.         self.mac_to_port = {}
  53.  
  54.     def add_flow(self, datapath, match, actions):
  55.         ofproto = datapath.ofproto
  56.  
  57.         mod = datapath.ofproto_parser.OFPFlowMod(
  58.             datapath=datapath, match=match, cookie=0,
  59.             command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
  60.             priority=ofproto.OFP_DEFAULT_PRIORITY,
  61.             flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
  62.         datapath.send_msg(mod)
  63.  
  64.     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
  65.     def _packet_in_handler(self, ev):
  66.         msg = ev.msg
  67.         datapath = msg.datapath
  68.         dpid = datapath.id
  69.         ofproto = datapath.ofproto
  70.  
  71.         pkt = packet.Packet(msg.data)
  72.         eth = pkt.get_protocol(ethernet.ethernet)
  73.  
  74.         dst = eth.dst
  75.         src = eth.src
  76.  
  77.         self.mac_to_port.setdefault(dpid, {})
  78.  
  79.         self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
  80.  
  81.         # learn a mac address to avoid FLOOD next time.
  82.         self.mac_to_port[dpid][src] = msg.in_port
  83.  
  84.         if dpid == 0x1A:
  85.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  86.                 arpPkt = pkt.get_protocol(arp.arp)
  87.         if arpPkt.opcode == 1 and arpPkt.dst_ip == R1_IP_LEFT:
  88.             self.send_arp_reply(datapath,R1_MAC_LEFT,R1_IP_LEFT,src,arpPkt.src_ip,msg.in_port)
  89.                 return
  90.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  91.         ipPkt = pkt.get_protocol(ipv4.ipv4)
  92.         src_ip = ipPkt.src
  93.         dst_ip = ipPkt.dst
  94.         if msg.in_port == 1:
  95.             if dst_ip[:10] == LAN1[:10]:
  96.                 match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 1,nw_dst_mask = 24,nw_dst = LAN1)
  97.                 actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R1_MAC_LEFT),datapath.ofproto_parser.OFPActionSetDlDst(BCAST),datapath.ofproto_parser.OFPActionOutput(2)]
  98.         elif msg.in_port == 2:
  99.             if dst_ip[:10] == LAN2[:10]:
  100.                 match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 2,nw_dst_mask = 24,nw_dst = LAN2)
  101.                                 actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R1_MAC_RIGHT),datapath.ofproto_parser.OFPActionSetDlDst(R2_MAC_LEFT),datapath.ofproto_parser.OFPActionOutput(1)]
  102.                
  103.         self.add_flow(datapath,match,actions)
  104.         out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath,buffer_id=0xffffffff,in_port=msg.in_port,actions=actions,data=msg.data)
  105.         datapath.send_msg(out)
  106.         return
  107.             return
  108.         if dpid == 0x1B:
  109.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  110.         arpPkt = pkt.get_protocol(arp.arp)
  111.                 if arpPkt.opcode == 1 and arpPkt.dst_ip == R2_IP_RIGHT:
  112.                         self.send_arp_reply(datapath,R2_MAC_RIGHT,R2_IP_RIGHT,src,arpPkt.src_ip,msg.in_port)
  113.                 return
  114.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  115.         if msg.in_port == 1:
  116.             match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 1,nw_dst_mask = 24,nw_dst = LAN2)
  117.             actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R2_MAC_RIGHT),datapath.ofproto_parser.OFPActionSetDlDst(BCAST),datapath.ofproto_parser.OFPActionOutput(2)]
  118.         elif msg.in_port == 2:
  119.             match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 2,nw_dst_mask = 24,nw_dst = LAN1)
  120.             actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R2_MAC_LEFT),datapath.ofproto_parser.OFPActionSetDlDst(R1_MAC_RIGHT),datapath.ofproto_parser.OFPActionOutput(1)]
  121.  
  122.         self.add_flow(datapath,match,actions)
  123.         out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath,buffer_id=0xffffffff,in_port=msg.in_port,actions=actions,data=msg.data)
  124.         datapath.send_msg(out)
  125.                 return
  126.             return
  127.                  
  128.         if dst in self.mac_to_port[dpid]:
  129.             out_port = self.mac_to_port[dpid][dst]
  130.         else:
  131.             out_port = ofproto.OFPP_FLOOD
  132.  
  133.         match = datapath.ofproto_parser.OFPMatch(
  134.             in_port=msg.in_port, dl_dst=haddr_to_bin(dst))
  135.  
  136.         actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
  137.  
  138.         # install a flow to avoid packet_in next time
  139.         if out_port != ofproto.OFPP_FLOOD:
  140.             self.add_flow(datapath, match, actions)
  141.  
  142.         data = None
  143.         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
  144.             data = msg.data
  145.  
  146.         out = datapath.ofproto_parser.OFPPacketOut(
  147.             datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
  148.             actions=actions, data=data)
  149.         datapath.send_msg(out)
  150.  
  151.     def send_arp_reply(self, datapath, srcMac, srcIp, dstMac, dstIp, outPort):
  152.         e = ethernet.ethernet(dstMac, srcMac, ether_types.ETH_TYPE_ARP)
  153.         a = arp.arp(1, 0x0800, 6, 4, 2, srcMac, srcIp, dstMac, dstIp)
  154.         p = packet.Packet()
  155.         p.add_protocol(e)
  156.         p.add_protocol(a)
  157.         p.serialize()
  158.  
  159.         actions = [datapath.ofproto_parser.OFPActionOutput(outPort, 0)]
  160.         out = datapath.ofproto_parser.OFPPacketOut(
  161.             datapath=datapath,
  162.             buffer_id=0xffffffff,
  163.             in_port=datapath.ofproto.OFPP_CONTROLLER,
  164.             actions=actions,
  165.             data=p.data)
  166.         datapath.send_msg(out)
  167.  
  168.     @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
  169.     def _port_status_handler(self, ev):
  170.         msg = ev.msg
  171.         reason = msg.reason
  172.         port_no = msg.desc.port_no
  173.  
  174.         ofproto = msg.datapath.ofproto
  175.         if reason == ofproto.OFPPR_ADD:
  176.             self.logger.info("port added %s", port_no)
  177.         elif reason == ofproto.OFPPR_DELETE:
  178.             self.logger.info("port deleted %s", port_no)
  179.         elif reason == ofproto.OFPPR_MODIFY:
  180.             self.logger.info("port modified %s", port_no)
  181.         else:
  182.             self.logger.info("Illeagal port state %s %s", port_no, reason)
  183.  
  184. ###########################################################
  185.  
  186. # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
  187. #
  188. # Licensed under the Apache License, Version 2.0 (the "License");
  189. # you may not use this file except in compliance with the License.
  190. # You may obtain a copy of the License at
  191. #
  192. #    http://www.apache.org/licenses/LICENSE-2.0
  193. #
  194. # Unless required by applicable law or agreed to in writing, software
  195. # distributed under the License is distributed on an "AS IS" BASIS,
  196. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  197. # implied.
  198. # See the License for the specific language governing permissions and
  199. # limitations under the License.
  200.  
  201. """
  202. Two OpenFlow 1.0 L3 Static Routers and two OpenFlow 1.0 L2 learning switches.
  203. """
  204.  
  205.  
  206. from ryu.base import app_manager
  207. from ryu.controller import ofp_event
  208. from ryu.controller.handler import MAIN_DISPATCHER
  209. from ryu.controller.handler import set_ev_cls
  210. from ryu.ofproto import ofproto_v1_0
  211. from ryu.lib.mac import haddr_to_bin
  212. from ryu.lib.packet import packet
  213. from ryu.lib.packet import ethernet
  214. from ryu.lib.packet import arp
  215. from ryu.lib.packet import ipv4
  216. from ryu.lib.packet import tcp
  217. from ryu.lib.packet import udp
  218. from ryu.lib.packet import ether_types
  219. import time
  220. """
  221. fill in the code here (optional)
  222. """
  223. BCAST = "FF:FF:FF:FF:FF:FF"
  224. LAN1 = "192.168.1.0"
  225. LAN2 = "192.168.2.0"
  226. PUBLIC_IP = "200.0.0.0"
  227. PUBLIC_MAC = "00:00:00:00:04:02"
  228. R1_IP_LEFT = "192.168.1.1"
  229. R1_MAC_LEFT = "00:00:00:00:01:01"
  230. R1_MAC_RIGHT = "00:00:00:00:03:01"
  231. R1_MAC_PUBLIC = "00:00:00:00:04:01"
  232. R1_IP_PUBLIC = "200.0.0.1"
  233. R2_IP_RIGHT = "192.168.2.1"
  234. R2_MAC_LEFT = "00:00:00:00:03:02"
  235. R2_MAC_RIGHT = "00:00:00:00:02:01"
  236. nat_table = {}
  237. nat_ports = 49160
  238.  
  239.  
  240.  
  241. class SimpleSwitch(app_manager.RyuApp):
  242.     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
  243.  
  244.     def __init__(self, *args, **kwargs):
  245.         super(SimpleSwitch, self).__init__(*args, **kwargs)
  246.         self.mac_to_port = {}
  247.  
  248.     def add_flow(self, datapath, match, actions):
  249.         ofproto = datapath.ofproto
  250.  
  251.         mod = datapath.ofproto_parser.OFPFlowMod(
  252.             datapath=datapath, match=match, cookie=0,
  253.             command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
  254.             priority=ofproto.OFP_DEFAULT_PRIORITY,
  255.             flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
  256.         datapath.send_msg(mod)
  257.  
  258.     def out_port(self,ip,port):
  259.     global nat_ports, nat_table
  260.     for p in nat_table:
  261.         if nat_table[p][0] == ip and nat_table[p][1] == port:
  262.             return p
  263.  
  264.     nat_ports = nat_ports + 1
  265.     nat_table[nat_ports] = [ip,port]
  266.     return nat_ports
  267.    
  268.     def find_host(self,port):
  269.     global nat_table,nat_ports
  270.  
  271.     if port in nat_table:
  272.     #   res = nat_table[port]
  273.     #   del nat_table[port]
  274.         return nat_table[port]
  275.     else:
  276.         return -1,-1
  277.    
  278.        
  279.     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
  280.     def _packet_in_handler(self, ev):
  281.     global nat_table
  282.         msg = ev.msg
  283.         datapath = msg.datapath
  284.         dpid = datapath.id
  285.         ofproto = datapath.ofproto
  286.  
  287.         pkt = packet.Packet(msg.data)
  288.         eth = pkt.get_protocol(ethernet.ethernet)
  289.  
  290.         dst = eth.dst
  291.         src = eth.src
  292.  
  293.         self.mac_to_port.setdefault(dpid, {})
  294.  
  295.         self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
  296.  
  297.         # learn a mac address to avoid FLOOD next time.
  298.         self.mac_to_port[dpid][src] = msg.in_port
  299.  
  300.         if dpid == 0x1A:
  301.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  302.                 arpPkt = pkt.get_protocol(arp.arp)
  303.         if arpPkt.opcode == 1 and arpPkt.dst_ip == R1_IP_LEFT:
  304.             self.send_arp_reply(datapath,R1_MAC_LEFT,R1_IP_LEFT,src,arpPkt.src_ip,msg.in_port)
  305.         elif arpPkt.opcode == 1 and arpPkt.dst_ip == R1_IP_PUBLIC:
  306.             self.send_arp_reply(datapath,R1_MAC_PUBLIC,R1_IP_PUBLIC,src,arpPkt.src_ip,msg.in_port)
  307.                 return
  308.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  309.         ipPkt = pkt.get_protocol(ipv4.ipv4)
  310.         src_ip = ipPkt.src
  311.         dst_ip = ipPkt.dst
  312.         print(ipPkt.proto)
  313.         if ipPkt.proto == 6:  
  314.             tpkt = pkt.get_protocol(tcp.tcp)
  315.             src_port = tpkt.src_port
  316.                         dst_port = tpkt.dst_port
  317.         #   print(src_port,dst_port)               
  318.         elif ipPkt.proto == 17:
  319.             tpkt = pkt.get_protocol(udp.udp)
  320.             src_port = tpkt.src_port
  321.             dst_port = tpkt.dst_port
  322.         #   print(src_port,dst_port)
  323.         if dst_ip[:8] == PUBLIC_IP[:8]:
  324.             new_port = self.out_port(src_ip,src_port)
  325.             print("---",nat_table)
  326.             match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,nw_dst_mask = 24,nw_dst = PUBLIC_IP)#change port
  327.             actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R1_MAC_PUBLIC),datapath.ofproto_parser.OFPActionSetDlDst(PUBLIC_MAC),datapath.ofproto_parser.OFPActionSetTpSrc(new_port),datapath.ofproto_parser.OFPActionOutput(3)]
  328.         else:
  329.             if msg.in_port == 1:
  330.                 if dst_ip[:10] == LAN1[:10]:
  331.                     match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 1,nw_dst_mask = 24,nw_dst = LAN1)
  332.                     actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R1_MAC_LEFT),datapath.ofproto_parser.OFPActionSetDlDst(BCAST),datapath.ofproto_parser.OFPActionOutput(2)]
  333.             elif msg.in_port == 2:
  334.                 if dst_ip[:10] == LAN2[:10]:
  335.                     match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 2,nw_dst_mask = 24,nw_dst = LAN2)
  336.                     actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R1_MAC_RIGHT),datapath.ofproto_parser.OFPActionSetDlDst(R2_MAC_LEFT),datapath.ofproto_parser.OFPActionOutput(1)]
  337.                     elif msg.in_port == 3:
  338.                 [dst_ip,port] = self.find_host(dst_port)
  339.                 print(dst_ip,port,"->",dst_port)
  340.                 print(nat_table)
  341.                 if dst_ip == -1 or port == -1:
  342.                     return
  343.                 if dst_ip[:10] == LAN1[:10]:
  344.                     out_port = 2
  345.                     srcMAC = R1_MAC_LEFT
  346.                     match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 3,nw_dst_mask = 24,nw_dst = LAN1)
  347.                 elif dst_ip[:10] == LAN2[:10]:
  348.                     out_port = 1
  349.                     srcMAC = R1_MAC_RIGHT
  350.                     match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 3,nw_dst_mask = 24,nw_dst = LAN2)
  351.  
  352.  
  353.                 actions = [datapath.ofproto_parser.OFPActionSetDlSrc(srcMAC),datapath.ofproto_parser.OFPActionSetDlDst(BCAST),datapath.ofproto_parser.OFPActionSetTpDst(port),datapath.ofproto_parser.OFPActionSetNwDst(dst_ip),datapath.ofproto_parser.OFPActionOutput(out_port)]
  354.  
  355.         self.add_flow(datapath,match,actions)
  356.         out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath,buffer_id=0xffffffff,in_port=msg.in_port,actions=actions,data=msg.data)
  357.         datapath.send_msg(out)
  358.         return
  359.             return
  360.         if dpid == 0x1B:
  361.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  362.         arpPkt = pkt.get_protocol(arp.arp)
  363.                 if arpPkt.opcode == 1 and arpPkt.dst_ip == R2_IP_RIGHT:
  364.                         self.send_arp_reply(datapath,R2_MAC_RIGHT,R2_IP_RIGHT,src,arpPkt.src_ip,msg.in_port)
  365.                 return
  366.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  367.         if msg.in_port == 1:
  368.             match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 1,nw_dst_mask = 24,nw_dst = LAN2)
  369.             actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R2_MAC_RIGHT),datapath.ofproto_parser.OFPActionSetDlDst(BCAST),datapath.ofproto_parser.OFPActionOutput(2)]
  370.         elif msg.in_port == 2:
  371.             ipPkt = pkt.get_protocol(ipv4.ipv4)
  372.             if ipPkt.dst[:8] == PUBLIC_IP[:8]:
  373.                 match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 2,nw_dst_mask = 24,nw_dst = PUBLIC_IP)
  374.             else:
  375.                 match = datapath.ofproto_parser.OFPMatch(dl_type = 0x0800,in_port = 2,nw_dst_mask = 24,nw_dst = LAN1)
  376.             actions = [datapath.ofproto_parser.OFPActionSetDlSrc(R2_MAC_LEFT),datapath.ofproto_parser.OFPActionSetDlDst(R1_MAC_RIGHT),datapath.ofproto_parser.OFPActionOutput(1)]
  377.  
  378.         self.add_flow(datapath,match,actions)
  379.         out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath,buffer_id=0xffffffff,in_port=msg.in_port,actions=actions,data=msg.data)
  380.         datapath.send_msg(out)
  381.                 return
  382.             return
  383.                  
  384.         if dst in self.mac_to_port[dpid]:
  385.             out_port = self.mac_to_port[dpid][dst]
  386.         else:
  387.             out_port = ofproto.OFPP_FLOOD
  388.  
  389.         match = datapath.ofproto_parser.OFPMatch(
  390.             in_port=msg.in_port, dl_dst=haddr_to_bin(dst))
  391.  
  392.         actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
  393.  
  394.         # install a flow to avoid packet_in next time
  395.         if out_port != ofproto.OFPP_FLOOD:
  396.             self.add_flow(datapath, match, actions)
  397.  
  398.         data = None
  399.         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
  400.             data = msg.data
  401.  
  402.         out = datapath.ofproto_parser.OFPPacketOut(
  403.             datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
  404.             actions=actions, data=data)
  405.         datapath.send_msg(out)
  406.    
  407.  
  408.     def send_arp_reply(self, datapath, srcMac, srcIp, dstMac, dstIp, outPort):
  409.         e = ethernet.ethernet(dstMac, srcMac, ether_types.ETH_TYPE_ARP)
  410.         a = arp.arp(1, 0x0800, 6, 4, 2, srcMac, srcIp, dstMac, dstIp)
  411.         p = packet.Packet()
  412.         p.add_protocol(e)
  413.         p.add_protocol(a)
  414.         p.serialize()
  415.  
  416.         actions = [datapath.ofproto_parser.OFPActionOutput(outPort, 0)]
  417.         out = datapath.ofproto_parser.OFPPacketOut(
  418.             datapath=datapath,
  419.             buffer_id=0xffffffff,
  420.             in_port=datapath.ofproto.OFPP_CONTROLLER,
  421.             actions=actions,
  422.             data=p.data)
  423.         datapath.send_msg(out)
  424.  
  425.     @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
  426.     def _port_status_handler(self, ev):
  427.         msg = ev.msg
  428.         reason = msg.reason
  429.         port_no = msg.desc.port_no
  430.  
  431.         ofproto = msg.datapath.ofproto
  432.         if reason == ofproto.OFPPR_ADD:
  433.             self.logger.info("port added %s", port_no)
  434.         elif reason == ofproto.OFPPR_DELETE:
  435.             self.logger.info("port deleted %s", port_no)
  436.         elif reason == ofproto.OFPPR_MODIFY:
  437.             self.logger.info("port modified %s", port_no)
  438.         else:
  439.             self.logger.info("Illeagal port state %s %s", port_no, reason)
  440. ###################################################################3
  441. # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
  442. #
  443. # Licensed under the Apache License, Version 2.0 (the "License");
  444. # you may not use this file except in compliance with the License.
  445. # You may obtain a copy of the License at
  446. #
  447. #    http://www.apache.org/licenses/LICENSE-2.0
  448. #
  449. # Unless required by applicable law or agreed to in writing, software
  450. # distributed under the License is distributed on an "AS IS" BASIS,
  451. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  452. # implied.
  453. # See the License for the specific language governing permissions and
  454. # limitations under the License.
  455.  
  456. """
  457. Two OpenFlow 1.0 L3 Static Routers and two OpenFlow 1.0 L2 learning switches.
  458. """
  459.  
  460.  
  461. from ryu.base import app_manager
  462. from ryu.controller import ofp_event
  463. from ryu.controller.handler import MAIN_DISPATCHER
  464. from ryu.controller.handler import set_ev_cls
  465. from ryu.ofproto import ofproto_v1_0
  466. from ryu.lib.mac import haddr_to_bin
  467. from ryu.lib.packet import packet
  468. from ryu.lib.packet import ethernet
  469. from ryu.lib.packet import arp
  470. from ryu.lib.packet import ipv4
  471. from ryu.lib.packet import ether_types
  472.  
  473. from ryu.lib.packet.arp import arp
  474.  
  475. """
  476. fill in the code here for any used constant (optional)
  477. """
  478.  
  479. class SimpleSwitch(app_manager.RyuApp):
  480.     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
  481.  
  482.     multicast_groups = (("01:00:5e:00:00:01", "01:00:5e:00:00:02"))
  483.     multicast_groups_IP = (("239.0.0.1", "239.0.0.2"))
  484.  
  485.     def __init__(self, *args, **kwargs):
  486.         super(SimpleSwitch, self).__init__(*args, **kwargs)
  487.         self.mac_to_port = {}
  488.  
  489.     def add_flow(self, datapath, match, actions):
  490.         ofproto = datapath.ofproto
  491.  
  492.         mod = datapath.ofproto_parser.OFPFlowMod(
  493.             datapath=datapath, match=match, cookie=0,
  494.             command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
  495.             priority=ofproto.OFP_DEFAULT_PRIORITY,
  496.             flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
  497.         datapath.send_msg(mod)
  498.  
  499.     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
  500.     def _packet_in_handler(self, ev):
  501.         msg = ev.msg
  502.         datapath = msg.datapath
  503.         dpid = datapath.id
  504.         ofproto = datapath.ofproto
  505.  
  506.         pkt = packet.Packet(msg.data)
  507.         eth = pkt.get_protocol(ethernet.ethernet)
  508.  
  509.         dst = eth.dst
  510.         src = eth.src
  511.  
  512.         self.mac_to_port.setdefault(dpid, {})
  513.  
  514.         self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
  515.  
  516.         # learn a mac address to avoid FLOOD next time.
  517.         self.mac_to_port[dpid][src] = msg.in_port
  518.  
  519.         if dpid == 0x1A:
  520.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  521.                 arpPacket = pkt.get_protocol(arp)
  522.                 arp_dstIp = arpPacket.dst_ip
  523.                 dstIp = arpPacket.src_ip
  524.                 srcIp = arpPacket.dst_ip
  525.                 dstMac = eth.src
  526.  
  527.  
  528.                 if arp_dstIp == "192.168.1.2":
  529.                     srcMac = "00:00:00:00:01:02"
  530.                     outPort = 1
  531.                 elif arp_dstIp == "192.168.1.3":
  532.                     srcMac = "00:00:00:00:01:03"
  533.                     outPort = 1
  534.                 else:
  535.                     srcMac = "00:00:00:00:01:01"
  536.                     outPort = 2
  537.  
  538.  
  539.                 self.send_arp_reply(datapath, srcMac, srcIp, dstMac, dstIp, outPort)
  540.  
  541.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  542.  
  543.                 ipv4Packet = pkt.get_protocol(ipv4.ipv4)
  544.                 srcIp = ipv4Packet.src
  545.                 dstIp = ipv4Packet.dst
  546.  
  547.                 print("INSIDE ROUTER 1")
  548.                 print(dstIp, eth.dst)
  549.  
  550.                 if dstIp == "192.168.2.2" or dstIp == "192.168.2.3" or dstIp in self.multicast_groups_IP:
  551.                     actions_modify_headers = [
  552.                         datapath.ofproto_parser.OFPActionSetDlDst("00:00:00:00:03:02"),
  553.                         datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:03:01"),
  554.                         datapath.ofproto_parser.OFPActionOutput(1)
  555.                     ]
  556.                 else:
  557.                     actions_modify_headers = [
  558.                         datapath.ofproto_parser.OFPActionSetDlDst("FF:FF:FF:FF:FF:FF"),
  559.                         datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:01:01"),
  560.                         datapath.ofproto_parser.OFPActionOutput(2)
  561.                     ]
  562.  
  563.                     match = datapath.ofproto_parser.OFPMatch(dl_type=0x0800, nw_dst="192.168.1.0", nw_dst_mask=24)
  564.                     self.add_flow(datapath, match, actions_modify_headers)
  565.  
  566.  
  567.                 data = None
  568.                 if msg.buffer_id == ofproto.OFP_NO_BUFFER:
  569.                     data = msg.data
  570.  
  571.                 out = datapath.ofproto_parser.OFPPacketOut(
  572.                     datapath=datapath, buffer_id=msg.buffer_id,in_port=msg.in_port,
  573.                     actions=actions_modify_headers, data=data)
  574.                 datapath.send_msg(out)
  575.             return
  576.  
  577.         if dpid == 0x1B:
  578.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  579.                 arpPacket = pkt.get_protocol(arp)
  580.                 arp_dstIp = arpPacket.dst_ip
  581.                 dstIp = arpPacket.src_ip
  582.                 srcIp = arpPacket.dst_ip
  583.                 dstMac = eth.src
  584.  
  585.                 if arp_dstIp == "192.168.2.2":
  586.                     srcMac = "00:00:00:00:02:02"
  587.                     outPort = 1
  588.                 elif arp_dstIp == "192.168.2.3":
  589.                     srcMac = "00:00:00:00:02:03"
  590.                     outPort = 1
  591.                 else:
  592.                     srcMac = "00:00:00:00:02:01"
  593.                     outPort = 2
  594.  
  595.                 self.send_arp_reply(datapath, srcMac, srcIp, dstMac, dstIp, outPort)
  596.  
  597.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  598.  
  599.                 ipv4Packet = pkt.get_protocol(ipv4.ipv4)
  600.                 srcIp = ipv4Packet.src
  601.                 dstIp = ipv4Packet.dst
  602.  
  603.                 print("INSIDE ROUTER 2")
  604.                 print(dstIp, eth.dst)
  605.  
  606.                 if dstIp == "192.168.1.2" or dstIp == "192.168.1.3":
  607.                     actions_modify_headers = [
  608.                         datapath.ofproto_parser.OFPActionSetDlDst("00:00:00:00:03:01"),
  609.                         datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:03:02"),
  610.                         datapath.ofproto_parser.OFPActionOutput(1)
  611.                     ]
  612.                 else:
  613.                     actions_modify_headers = [
  614.                         datapath.ofproto_parser.OFPActionSetDlDst("FF:FF:FF:FF:FF:FF"),
  615.                         datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:02:01"),
  616.                         datapath.ofproto_parser.OFPActionOutput(2)
  617.                     ]
  618.  
  619.                     match = datapath.ofproto_parser.OFPMatch(dl_type=0x0800, in_port=1, nw_dst="192.168.2.0", nw_dst_mask=24)
  620.                     self.add_flow(datapath, match, actions_modify_headers)
  621.  
  622.  
  623.                 data = None
  624.                 if msg.buffer_id == ofproto.OFP_NO_BUFFER:
  625.                     data = msg.data
  626.  
  627.                 out = datapath.ofproto_parser.OFPPacketOut(
  628.                     datapath=datapath, buffer_id=msg.buffer_id,in_port=msg.in_port,
  629.                     actions=actions_modify_headers, data=data)
  630.                 datapath.send_msg(out)
  631.             return
  632.  
  633.         ### SWITCHES ###
  634.  
  635.         print("IN SWITCH WITH DST")
  636.         print(dst)
  637.         print("#" * 20)
  638.         if dst in self.mac_to_port[dpid]:
  639.             out_port = self.mac_to_port[dpid][dst]
  640.         elif dst in self.multicast_groups:
  641.             out_port = 1
  642.         else:
  643.             out_port = ofproto.OFPP_FLOOD
  644.         print(out_port)
  645.         print("#" * 20)
  646.  
  647.         match = datapath.ofproto_parser.OFPMatch(
  648.             in_port=msg.in_port, dl_dst=haddr_to_bin(dst))
  649.  
  650.         actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
  651.  
  652.         # install a flow to avoid packet_in next time
  653.         if out_port != ofproto.OFPP_FLOOD:
  654.             self.add_flow(datapath, match, actions)
  655.  
  656.         data = None
  657.         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
  658.             data = msg.data
  659.  
  660.         out = datapath.ofproto_parser.OFPPacketOut(
  661.             datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
  662.             actions=actions, data=data)
  663.         datapath.send_msg(out)
  664.  
  665.     def send_arp_reply(self, datapath, srcMac, srcIp, dstMac, dstIp, outPort):
  666.         e = ethernet.ethernet(dstMac, srcMac, ether_types.ETH_TYPE_ARP)
  667.         a = arp(1, 0x0800, 6, 4, 2, srcMac, srcIp, dstMac, dstIp)
  668.         p = packet.Packet()
  669.         p.add_protocol(e)
  670.         p.add_protocol(a)
  671.         p.serialize()
  672.  
  673.         actions = [datapath.ofproto_parser.OFPActionOutput(outPort, 0)]
  674.         out = datapath.ofproto_parser.OFPPacketOut(
  675.             datapath=datapath,
  676.             buffer_id=0xffffffff,
  677.             in_port=datapath.ofproto.OFPP_CONTROLLER,
  678.             actions=actions,
  679.             data=p.data)
  680.         datapath.send_msg(out)
  681.  
  682.     @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
  683.     def _port_status_handler(self, ev):
  684.         msg = ev.msg
  685.         reason = msg.reason
  686.         port_no = msg.desc.port_no
  687.  
  688.         ofproto = msg.datapath.ofproto
  689.         if reason == ofproto.OFPPR_ADD:
  690.             self.logger.info("port added %s", port_no)
  691.         elif reason == ofproto.OFPPR_DELETE:
  692.             self.logger.info("port deleted %s", port_no)
  693.         elif reason == ofproto.OFPPR_MODIFY:
  694.             self.logger.info("port modified %s", port_no)
  695.         else:
  696.             self.logger.info("Illeagal port state %s %s", port_no, reason)
  697.  
  698. ##########################################################
  699.  
  700. # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
  701. #
  702. # Licensed under the Apache License, Version 2.0 (the "License");
  703. # you may not use this file except in compliance with the License.
  704. # You may obtain a copy of the License at
  705. #
  706. #    http://www.apache.org/licenses/LICENSE-2.0
  707. #
  708. # Unless required by applicable law or agreed to in writing, software
  709. # distributed under the License is distributed on an "AS IS" BASIS,
  710. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  711. # implied.
  712. # See the License for the specific language governing permissions and
  713. # limitations under the License.
  714.  
  715. """
  716. Two OpenFlow 1.0 L3 Static Routers and two OpenFlow 1.0 L2 learning switches.
  717. """
  718.  
  719.  
  720. from ryu.base import app_manager
  721. from ryu.controller import ofp_event
  722. from ryu.controller.handler import MAIN_DISPATCHER
  723. from ryu.controller.handler import set_ev_cls
  724. from ryu.ofproto import ofproto_v1_0
  725. from ryu.lib.mac import haddr_to_bin
  726. from ryu.lib.packet import packet
  727. from ryu.lib.packet import ethernet
  728. from ryu.lib.packet import arp
  729. from ryu.lib.packet import ipv4
  730. from ryu.lib.packet import ether_types
  731.  
  732. r1_dp = "0x1A"
  733. r1_ip="192.168.1.1"
  734. r1_eth1_mac="00:00:00:00:01:01"
  735. r1_eth2_mac="00:00:00:00:03:01"
  736.  
  737.  
  738. r2_dp ="0x1B"
  739. r2_ip ="192.168.2.1"
  740. r2_eth1_mac="00:00:00:00:03:02"
  741. r2_eth2_mac="00:00:00:00:02:01"
  742.  
  743. h1_mac="00:00:00:00:01:02"
  744. h1_ip="192.168.1.2"
  745.  
  746. h2_mac="00:00:00:00:02:02"
  747. h2_ip="192.168.2.2"
  748.  
  749. h3_mac="00:00:00:00:02:03"
  750. h3_ip="192.168.2.3"
  751.  
  752. h4_mac="00:00:00:00:01:03"
  753. h4_ip="192.168.1.3"
  754.  
  755. leftlan="192.168.1"
  756. rightlan="192.168.2"
  757.  
  758. class SimpleSwitch(app_manager.RyuApp):
  759.     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
  760.  
  761.     def __init__(self, *args, **kwargs):
  762.         super(SimpleSwitch, self).__init__(*args, **kwargs)
  763.         self.mac_to_port = {}
  764.  
  765.     def add_flow(self, datapath, match, actions):
  766.         ofproto = datapath.ofproto
  767.  
  768.         mod = datapath.ofproto_parser.OFPFlowMod(
  769.             datapath=datapath, match=match, cookie=0,
  770.             command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
  771.             priority=ofproto.OFP_DEFAULT_PRIORITY,
  772.             flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
  773.         datapath.send_msg(mod)
  774.  
  775.     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
  776.     def _packet_in_handler(self, ev):
  777.         msg = ev.msg
  778.         datapath = msg.datapath
  779.         dpid = datapath.id
  780.         ofproto = datapath.ofproto
  781.  
  782.         pkt = packet.Packet(msg.data)
  783.         eth = pkt.get_protocol(ethernet.ethernet)
  784.  
  785.         dst = eth.dst
  786.         src = eth.src
  787.  
  788.         self.mac_to_port.setdefault(dpid, {})
  789.  
  790.         self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
  791.  
  792.         # learn a mac address to avoid FLOOD next time.
  793.         self.mac_to_port[dpid][src] = msg.in_port
  794.  
  795.         if dpid == 0x1A:
  796.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  797.                 p_arp = pkt.get_protocol(arp.arp)
  798.                 arp_dstIp = p_arp.dst_ip
  799.                 arp_srcIp = p_arp.src_ip
  800.  
  801.                 if p_arp.opcode == 1:
  802.                    
  803.                     if arp_dstIp == r1_ip:
  804.                         self.send_arp_reply(datapath,r1_eth1_mac,r1_ip,p_arp.src_mac,arp_srcIp,msg.in_port)
  805.                         return
  806.                 return
  807.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  808.                 pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
  809.                 pkt_mac = pkt.get_protocol(ethernet.ethernet)
  810.                 if leftlan in pkt_ipv4.dst:
  811.                     match = datapath.ofproto_parser.OFPMatch(dl_type=0x800,nw_dst_mask=24,nw_dst="192.168.1.0")
  812.                     actions = [datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:01:01"),datapath.ofproto_parser.OFPActionSetDlDst("ff:ff:ff:ff:ff:ff"),datapath.ofproto_parser.OFPActionOutput(2)]
  813.                     self.add_flow(datapath, match ,actions)
  814.                     return
  815.                 elif rightlan in pkt_ipv4.dst:
  816.                     match = datapath.ofproto_parser.OFPMatch(dl_type=0x800,nw_dst_mask=24,nw_dst="192.168.2.0")
  817.                     actions = [datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:03:01"),datapath.ofproto_parser.OFPActionSetDlDst("00:00:00:00:03:02"),datapath.ofproto_parser.OFPActionOutput(1)]
  818.                     self.add_flow(datapath, match ,actions)
  819.  
  820.                 return
  821.             return
  822.         if dpid == 0x1B:
  823.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  824.                p_arp = pkt.get_protocol(arp.arp)
  825.                if p_arp.opcode == 1:
  826.                    arp_dstIp = p_arp.dst_ip
  827.                    if arp_dstIp == r2_ip:
  828.                        arp_srcIp = p_arp.src_ip
  829.                        self.send_arp_reply(datapath,r2_eth1_mac,r2_ip,p_arp.src_mac,arp_srcIp,msg.in_port)
  830.                        return
  831.                return
  832.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  833.                 pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
  834.                 pkt_mac = pkt.get_protocol(ethernet.ethernet)
  835.                 if rightlan in pkt_ipv4.dst:
  836.                     match = datapath.ofproto_parser.OFPMatch(dl_type=0x800,nw_dst_mask=24,nw_dst="192.168.2.0")
  837.                     actions = [datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:02:01"),datapath.ofproto_parser.OFPActionSetDlDst("ff:ff:ff:ff:ff:ff"),datapath.ofproto_parser.OFPActionOutput(2)]
  838.                     self.add_flow(datapath, match ,actions)
  839.                     return
  840.                 elif leftlan in pkt_ipv4.dst:
  841.                     match = datapath.ofproto_parser.OFPMatch(dl_type=0x800,nw_dst_mask=24,nw_dst="192.168.1.0")
  842.                     actions = [datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:03:02"),datapath.ofproto_parser.OFPActionSetDlDst("00:00:00:00:03:01"),datapath.ofproto_parser.OFPActionOutput(1)]
  843.                     self.add_flow(datapath, match ,actions)
  844.  
  845.                 return
  846.             return      
  847.         if dpid==0x1:
  848.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  849.                 p_arp = pkt.get_protocol(arp.arp)
  850.                 arp_dstIp = p_arp.dst_ip
  851.                 arp_srcIp = p_arp.src_ip
  852.  
  853.                 if p_arp.opcode == 1:
  854.  
  855.                     if arp_dstIp == h1_ip:
  856.                         self.send_arp_reply(datapath,h1_mac,h1_ip,p_arp.src_mac,arp_srcIp,msg.in_port)
  857.                         return
  858.                     elif arp_dstIp == h2_ip:
  859.                         self.send_arp_reply(datapath,h2_mac,h2_ip,p_arp.src_mac,arp_srcIp,msg.in_port)
  860.                         return
  861.  
  862.                 return
  863.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  864.                 pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
  865.                 pkt_mac = pkt.get_protocol(ethernet.ethernet)
  866.                
  867.                 match = datapath.ofproto_parser.OFPMatch(dl_type=0x800,nw_dst_mask=24,nw_dst="192.168.1.0")
  868.                 actions = [datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:03:02"),datapath.ofproto_parser.OFPActionSetDlDst("00:00:00:00:03:01"),datapath.ofproto_parser.OFPActionOutput(1)]
  869.                 self.add_flow(datapath, match ,actions)
  870.  
  871.             return
  872.         if dpid==0x2:
  873.             if eth.ethertype == ether_types.ETH_TYPE_ARP: # this packet is ARP packet
  874.                 p_arp = pkt.get_protocol(arp.arp)
  875.                 arp_dstIp = p_arp.dst_ip
  876.                 arp_srcIp = p_arp.src_ip
  877.  
  878.                 if p_arp.opcode == 1:
  879.  
  880.                     if arp_dstIp == h3_ip:
  881.                         self.send_arp_reply(datapath,h3_mac,h3_ip,p_arp.src_mac,arp_srcIp,msg.in_port)
  882.                         return
  883.                     elif arp_dstIp == h4_ip:
  884.                         self.send_arp_reply(datapath,h4_mac,h4_ip,p_arp.src_mac,arp_srcIp,msg.in_port)
  885.                         return
  886.  
  887.                 return
  888.             elif eth.ethertype == ether_types.ETH_TYPE_IP: # this packet is IP packet
  889.                 pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
  890.                 pkt_mac = pkt.get_protocol(ethernet.ethernet)
  891.                 if leftlan in pkt_ipv4.dst:
  892.                     match = datapath.ofproto_parser.OFPMatch(dl_type=0x800,nw_dst_mask=24,nw_dst="192.168.1.0")
  893.                     actions = [datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:01:01"),datapath.ofproto_parser.OFPActionSetDlDst("ff:ff:ff:ff:ff:ff"),datapath.ofproto_parser.OFPActionOutput(2)]
  894.                     self.add_flow(datapath, match ,actions)
  895.                     return
  896.                 elif rightlan in pkt_ipv4.dst:
  897.                     match = datapath.ofproto_parser.OFPMatch(dl_type=0x800,nw_dst_mask=24,nw_dst="192.168.2.0")
  898.                     actions = [datapath.ofproto_parser.OFPActionSetDlSrc("00:00:00:00:03:01"),datapath.ofproto_parser.OFPActionSetDlDst("00:00:00:00:03:02"),datapath.ofproto_parser.OFPActionOutput(1)]
  899.                     self.add_flow(datapath, match ,actions)
  900.  
  901.                 return
  902.             return
  903.  
  904.                  
  905.         if dst in self.mac_to_port[dpid]:
  906.             out_port = self.mac_to_port[dpid][dst]
  907.         else:
  908.             out_port = ofproto.OFPP_FLOOD
  909.  
  910.         match = datapath.ofproto_parser.OFPMatch(
  911.             in_port=msg.in_port, dl_dst=haddr_to_bin(dst))
  912.  
  913.         actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
  914.  
  915.         # install a flow to avoid packet_in next time
  916.         if out_port != ofproto.OFPP_FLOOD:
  917.             self.add_flow(datapath, match, actions)
  918.  
  919.         data = None
  920.         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
  921.             data = msg.data
  922.  
  923.         out = datapath.ofproto_parser.OFPPacketOut(
  924.             datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
  925.             actions=actions, data=data)
  926.         datapath.send_msg(out)
  927.  
  928.     def send_arp_reply(self, datapath, srcMac, srcIp, dstMac, dstIp, outPort):
  929.         e = ethernet.ethernet(dstMac, srcMac, ether_types.ETH_TYPE_ARP)
  930.         a = arp.arp(1, 0x0800, 6, 4, 2, srcMac, srcIp, dstMac, dstIp)
  931.         p = packet.Packet()
  932.         p.add_protocol(e)
  933.         p.add_protocol(a)
  934.         p.serialize()
  935.  
  936.         actions = [datapath.ofproto_parser.OFPActionOutput(outPort, 0)]
  937.         out = datapath.ofproto_parser.OFPPacketOut(
  938.             datapath=datapath,
  939.             buffer_id=0xffffffff,
  940.             in_port=datapath.ofproto.OFPP_CONTROLLER,
  941.             actions=actions,
  942.             data=p.data)
  943.         datapath.send_msg(out)
  944.  
  945.     @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
  946.     def _port_status_handler(self, ev):
  947.         msg = ev.msg
  948.         reason = msg.reason
  949.         port_no = msg.desc.port_no
  950.  
  951.         ofproto = msg.datapath.ofproto
  952.         if reason == ofproto.OFPPR_ADD:
  953.             self.logger.info("port added %s", port_no)
  954.         elif reason == ofproto.OFPPR_DELETE:
  955.             self.logger.info("port deleted %s", port_no)
  956.         elif reason == ofproto.OFPPR_MODIFY:
  957.             self.logger.info("port modified %s", port_no)
  958.         else:
  959.             self.logger.info("Illeagal port state %s %s", port_no, reason)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement