Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.56 KB | None | 0 0
  1. from pox.core import core
  2. import pox.openflow.libopenflow_01 as of
  3.  
  4. logValues = core.getLogger()
  5.  
  6. class Tutorial(object):
  7.  
  8.     def __init__(self, connection):
  9.         self.mac_to_port = {}
  10.         self.tohost = {}
  11.         self.toprimarysw={}
  12.         self.tosecondarysw={}
  13.  
  14.         self.toprimarysw[1]=((2,"10.0.0.2"),(2,"10.0.0.3"))
  15.         self.toprimarysw[2]=((2,"10.0.0.1"),(3,"10.0.0.3"))
  16.         self.toprimarysw[3]=((2,"10.0.0.1"),(2,"10.0.0.2"))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  17.  
  18.         self._dpid=self.connection.dpid
  19.         self.connect_host()
  20.         sel.connect_switch()
  21.  
  22.         def connect_host(self,packet_in=None):
  23.             port, ip= self.tohost[self._dpid]
  24.             log.warning("Installing flow switch " + self._dpid + " at port " + port + "dst " + ip + " src")
  25.             msg = of.ofp_flow_mod()
  26.             msg.match = of.ofp_match(nw_dst=ip,di_type=0x800)
  27.  
  28.  
  29.             action = of.ofp_action_output(port = port)
  30.             msg.actions.append(action)
  31.             self.connection.send(msg)
  32.         def connect_switch(self,packet_in=None):
  33.             list= self.toprimarysw[self._dpid]
  34.  
  35.             for port, ip in list:
  36.                 log.warning("Installing flow switch switch %d dst %s.", self._dpid, port, ip)
  37.                 msg = of.ofp_flow_mod()
  38.                 msg.match = of.ofp_flow_mod()
  39.  
  40.                 msg.match = of.ofp_match(nw_dst=ip, dl_type=0x800)
  41.  
  42.                 action = of.ofp_action_output(port = port)
  43.                 msg.actions.append(action)
  44.                 self.connection.send(msg)
  45.  
  46.         def drop(self, event=None):
  47.             if event is None:
  48.                 log.warning("Empty event calls for drop.")
  49.                 return
  50.             msg = of.ofp_packet_out()
  51.             msg.buffer_id = event.ofp.buffer_id
  52.             msg.in_port = event.port
  53.             self.connection.send(msg)
  54.  
  55.         def _handle_PacketIn (self, event):
  56.  
  57.             packet = event.parsed
  58.             if not packet.parsed:
  59.                 log.warning("Incomplete packets are ignored.")
  60.                 return
  61.  
  62.             self.drop(event)
  63.  
  64.  
  65.  
  66.         self.ip_to_port= {}
  67.  
  68.         self.connection = connection
  69.         connection.addListeners(self)
  70.        
  71.    
  72.     #def connect_host(self,packet_in=None):
  73.  
  74.     def resend_packet(self, packet_in, out_port):
  75.         msg = of.ofp_packet_out()
  76.         msg.data = packet_in
  77.  
  78.         action = of.ofp_action_output(port=out_port)
  79.         msg.actions.append(action)
  80.  
  81.         self.connection.send(msg)
  82.  
  83.     def act_like_hub(self, packet, packet_in):
  84.         self.resend_packet(packet_in, of.OFPP_ALL)
  85.  
  86.     def act_like_switch(self, packet, packet_in):
  87.        
  88.         self.ip_to_port[packet.next.srcip] = packet_in.in_port
  89.  
  90.  
  91.         if packet.next.dstip in self.ip_to_port:
  92.             logValues.debug("Installing flow.")
  93.             msg = of.ofp_flow_mod()
  94.             msg.match = of.ofp_match.from_packet(packet)
  95.             msg.idle_timeout = 10
  96.             msg.hard_timeout = 30
  97.             msg.data = packet_in
  98.             msg.actions.append(of.ofp_action_output(port=self.ip_to_port[packet.next.dstip]))
  99.  
  100.             self.connection.send(msg)
  101.  
  102.         else:
  103.             self.resend_packet(packet_in, of.OFPP_ALL)
  104.  
  105.  
  106.     def _handle_PacketIn(self, event):
  107.         packet = event.parsed  # This is the parsed packet data.
  108.         if not packet.parsed:
  109.             logValues.warning("Ignoring incomplete packet")
  110.             return
  111.  
  112.         packet_in = event.ofp  # The actual ofp_packet_in message.
  113.  
  114.  
  115.         self.act_like_switch(packet, packet_in)
  116.  
  117.  
  118. def launch():
  119.     def start_switch(event):
  120.         logValues.debug("Controlling %s" % (event.connection,))
  121.         Tutorial((event.connection))
  122.  
  123.     core.openflow.addListenerByName("ConnectionUp", start_switch)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement