Skip to content

Instantly share code, notes, and snippets.

@samos123
Created September 29, 2014 05:11
Show Gist options
  • Save samos123/b50ca56e8a66009a6b48 to your computer and use it in GitHub Desktop.
Save samos123/b50ca56e8a66009a6b48 to your computer and use it in GitHub Desktop.
Using mininet to deeper understand openflow, wrote a simple sdn controller which works as l2 learning switch
"""
This component is for use with the OpenFlow tutorial.
This was modified to work as an l2 learning switch
It's quite similar to the one for NOX. Credit where credit due. :)
"""
from pox.core import core
import pox.openflow.libopenflow_01 as of
log = core.getLogger()
class Tutorial (object):
"""
A Tutorial object is created for each switch that connects.
A Connection object for that switch is passed to the __init__ function.
"""
def __init__ (self, connection):
# Keep track of the connection to the switch so that we can [54/118]
# send it messages!
self.connection = connection
# This binds our PacketIn event listener
connection.addListeners(self)
# Use this table to keep track of which ethernet address is on
# which switch port (keys are MACs, values are ports).
self.mac_to_port = {}
def resend_packet (self, packet_in, out_port):
"""
Instructs the switch to resend a packet that it had sent to us.
"packet_in" is the ofp_packet_in object the switch had sent to the
controller due to a table-miss.
"""
msg = of.ofp_packet_out()
msg.data = packet_in
# Add an action to send to the specified port
action = of.ofp_action_output(port = out_port)
msg.actions.append(action)
# Send message to switch
self.connection.send(msg)
def act_like_switch (self, packet, packet_in):
"""
Implement switch-like behavior.
"""
# Learn the port for the source MAC
self.mac_to_port[packet.src.toStr()] = packet_in.in_port
if packet.dst.toStr() in self.mac_to_port:
out_port = self.mac_to_port[packet.dst.toStr()]
log.debug("Installing flow from port %s to %s. Src: %s, dst: %s" % (
packet_in.in_port, out_port, packet.src, packet.dst))
msg = of.ofp_flow_mod()
msg.data = packet_in
msg.match = of.ofp_match.from_packet(packet)
msg.actions.append(of.ofp_action_output(port=out_port))
self.connection.send(msg)
else:
# Flood the packet out everything but the input port
# This part looks familiar, right?
log.debug("Flooding...")
self.resend_packet(packet_in, of.OFPP_ALL)
def _handle_PacketIn (self, event):
"""
Handles packet in messages from the switch.
"""
packet = event.parsed # This is the parsed packet data.
if not packet.parsed:
log.warning("Ignoring incomplete packet")
return
packet_in = event.ofp # The actual ofp_packet_in message.
self.act_like_switch(packet, packet_in)
def _handle_PacketIn (self, event):
"""
Handles packet in messages from the switch.
"""
packet = event.parsed # This is the parsed packet data.
if not packet.parsed:
log.warning("Ignoring incomplete packet")
return
packet_in = event.ofp # The actual ofp_packet_in message.
self.act_like_switch(packet, packet_in)
def launch ():
"""
Starts the component
"""
def start_switch (event):
log.debug("Controlling %s" % (event.connection,))
Tutorial(event.connection)
core.openflow.addListenerByName("ConnectionUp", start_switch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment