When writing Ryu applications, you can often find yourself repeatedly looking up multiple reference guides to find appropriate bitmasks and flag names even when doing something as simple as configuring a port. Following up on my previous Quick Tip, here we have a couple of utility functions that you can use to easily bring ports up and down inside a Ryu app itself. This is just a simple example, but it demonstrates that it is often a good idea to write your own utility functions that you may use in several places. This not only make your code cleaner and easier to read, but also save time in the future by not having to repeat previous work.

These functions can be used anywhere in a Ryu application where the datapath and port description is available. Here is an example where the first port reported by the switch in a OFPPortDescStatsReply message will be set administratively down:

How It Works

In this example, a function is defined to generate a message that can be sent to the switch to set or unset the OFPPC_PORT_DOWN configuration flag on a particular port. This results in the port being “administratively down” when set (see OpenFlow 1.3.4 spec section 7.2.1 Port Structures  under enum ofp_port_config, p.50).

This generates a OFPT_PORT_MOD message (OF 1.3.4 section 7.3.4.3 Port Modification Message, p. 79) using Ryu’s OpenFlow parser API’s ryu.ofproto.ofproto_v1_3_parser.OFPPortMod class. This message requires a port number, hardware address (as a sanity check), a new configuration value, a bitmask to mark what should be changed in the configuration, and the port advertisement (if it is to be set). As of this writing, ofagentapp requires that the advertise field be set to the previously observed value, so it is included in the code above. Setting the advertise field this way mirrors Ryu’s default action when sending port modification requests through the REST interface.

The mask is always set to the OFPPC_PORT_DOWN flag since this is the setting we want to affect. The config is set to the same value if set_port_down is True.

Our message-generating function above is then used in two functions that can be called to directly set or unset a port as administratively down:

These simply call getPortAdminModMsg to create the appropriate message and immediately send them through the specified datapath.

I hope this article helps in your quest to prototype and build fantastic Ryu controller applications.

Happy Coding!

-Leo

Full Listing

Example application that sets the first port reported as administratively down.

Download: portmgmt-0.0.1.tar