We define a listener method that makes the OpenFlow switches act as a simple hub. Then we call from the launch(), which is invoked at the start of the application.
Listing 10.6 gives a simple FTP connection test as follows:
#!/usr/bin/env python # Python Network Programming Cookbook, Second Edition -- Chapter - 10 # This program is optimized for Python 2.7.12. # It may run on any other version with/without modifications. # Adopted from https://github.com/noxrepo/pox/blob/carp/pox/forwarding/hub.py # For more examples and tutorials: # https://github.com/noxrepo/pox/tree/carp/pox from pox.core import core import pox.openflow.libopenflow_01 as of from pox.lib.util import dpidToStr log = core.getLogger() # The listener definition: A simple and stupid hub. def _handle_ConnectionUp (event): msg = of.ofp_flow_mod() msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD)) event.connection.send(msg) # log the action. log.info("Hubifying %s", dpidToStr(event.dpid)) # When the application is launched with POX. def launch (): #Add a listener (defined above) to the pox.core openflow. core.openflow.addListenerByName("ConnectionUp",
_handle_ConnectionUp) log.info("Hub is running.")
The execution of this recipe prints the following output:
$ ./pox.py log.level --DEBUG 10_6_sdn_pox POX 0.2.0 (carp) / Copyright 2011-2013 James McCauley, et al. INFO:10_6_sdn_pox:Hub is running. DEBUG:core:POX 0.2.0 (carp) going up... DEBUG:core:Running on CPython (2.7.12/Nov 19 2016 06:48:10) DEBUG:core:Platform is Linux-4.8.0-58-generic-x86_64-with-Ubuntu-16.04-xenial INFO:core:POX 0.2.0 (carp) is up. DEBUG:openflow.of_01:Listening on 0.0.0.0:6633