Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pox.core import core
- import pox.openflow.libopenflow_01 as of
- logValues = core.getLogger()
- class Tutorial(object):
- def __init__(self, connection):
- self.mac_to_port = {}
- self.tohost = {}
- self.toprimarysw={}
- self.tosecondarysw={}
- self.toprimarysw[1]=((2,"10.0.0.2"),(2,"10.0.0.3"))
- self.toprimarysw[2]=((2,"10.0.0.1"),(3,"10.0.0.3"))
- self.toprimarysw[3]=((2,"10.0.0.1"),(2,"10.0.0.2"))
- self._dpid=self.connection.dpid
- self.connect_host()
- sel.connect_switch()
- def connect_host(self,packet_in=None):
- port, ip= self.tohost[self._dpid]
- log.warning("Installing flow switch " + self._dpid + " at port " + port + "dst " + ip + " src")
- msg = of.ofp_flow_mod()
- msg.match = of.ofp_match(nw_dst=ip,di_type=0x800)
- action = of.ofp_action_output(port = port)
- msg.actions.append(action)
- self.connection.send(msg)
- def connect_switch(self,packet_in=None):
- list= self.toprimarysw[self._dpid]
- for port, ip in list:
- log.warning("Installing flow switch switch %d dst %s.", self._dpid, port, ip)
- msg = of.ofp_flow_mod()
- msg.match = of.ofp_flow_mod()
- msg.match = of.ofp_match(nw_dst=ip, dl_type=0x800)
- action = of.ofp_action_output(port = port)
- msg.actions.append(action)
- self.connection.send(msg)
- def drop(self, event=None):
- if event is None:
- log.warning("Empty event calls for drop.")
- return
- msg = of.ofp_packet_out()
- msg.buffer_id = event.ofp.buffer_id
- msg.in_port = event.port
- self.connection.send(msg)
- def _handle_PacketIn (self, event):
- packet = event.parsed
- if not packet.parsed:
- log.warning("Incomplete packets are ignored.")
- return
- self.drop(event)
- self.ip_to_port= {}
- self.connection = connection
- connection.addListeners(self)
- #def connect_host(self,packet_in=None):
- def resend_packet(self, packet_in, out_port):
- msg = of.ofp_packet_out()
- msg.data = packet_in
- action = of.ofp_action_output(port=out_port)
- msg.actions.append(action)
- self.connection.send(msg)
- def act_like_hub(self, packet, packet_in):
- self.resend_packet(packet_in, of.OFPP_ALL)
- def act_like_switch(self, packet, packet_in):
- self.ip_to_port[packet.next.srcip] = packet_in.in_port
- if packet.next.dstip in self.ip_to_port:
- logValues.debug("Installing flow.")
- msg = of.ofp_flow_mod()
- msg.match = of.ofp_match.from_packet(packet)
- msg.idle_timeout = 10
- msg.hard_timeout = 30
- msg.data = packet_in
- msg.actions.append(of.ofp_action_output(port=self.ip_to_port[packet.next.dstip]))
- self.connection.send(msg)
- else:
- self.resend_packet(packet_in, of.OFPP_ALL)
- def _handle_PacketIn(self, event):
- packet = event.parsed # This is the parsed packet data.
- if not packet.parsed:
- logValues.warning("Ignoring incomplete packet")
- return
- packet_in = event.ofp # The actual ofp_packet_in message.
- self.act_like_switch(packet, packet_in)
- def launch():
- def start_switch(event):
- logValues.debug("Controlling %s" % (event.connection,))
- Tutorial((event.connection))
- core.openflow.addListenerByName("ConnectionUp", start_switch)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement