When writing controller applications, it is often useful to determine when a port status changes. For example, when a port’s physical link is removed, flows and internal tables may need to be modified as part of a network layout learning algorithm. Thankfully, subscribing to these events is quite simple.

To get started quickly, download our portwatch.py example application (full source listing and download below) and put it in the apps/ folder of your working Simple Switch Quick Start for Ryu environment and run ryu-manager with the new example application alongside the existing simple_switch_13.py application. If you are using the local Ryu installation (see the link above), the following command will run it:

Once the switch connects, you should see a full list of ports listed:

And when ports are configured, you should see update messages:

In this case, the State changed with the OFPPS_LINK_DOWN flag removed.

How It Works

These events are triggered by asynchronous messages sent by the switch. There are several async messages used in any controller application, especially error and packet-in messages. The messages we want to receive for updates on port status are the OFPT_PORT_STATUS messages. These will provide a reason (added, deleted, modified) and the current port information. In order to make sure our controller receives these updates, the switch configuration for async messages may need to be set at the beginning of the switch’s connection to the controller.

All async message configuration (with the exception of error messages) can be configured with the OFPT_SET_ASYNC message. See the OpenFlow 1.3.4 specification under section 7.3.10 Set Asynchronous Configuration Message for more details. As of this writing, the OF-DPA 2.0 agent application does not handle the OFPT_SET_ASYNC message for configuration, but OFPT_PORT_STATUS messages are enabled by default. Our example application will still include the appropriate configuration message to ensure future versions of OF-DPA 2.0  agents are configured properly.

To process the messages, just add a method to your RyuApp that is called on the ofp_event.EventOFPPortStatus event on the MAIN_DISPATCHER. A small example is given in the Ryu documentation for ryu.ofproto.ofproto_v1_3_parser.OFPPortStatus. Here is our example in portwatch.py:

We also have a simple port_to_string method to pretty print some port information, especially the actual flags for  the state and config values in the provided port desc.

Feel free to use this application while debugging your own Ryu applications.

Happy Coding!

-Leo

Full Listing

Download from the Marketplace: portwatch-0.0.2.tar