Ryu tutorial Addendum

A note that I had a hard time finding it in the ryu python openflow library because it seems to be listed in the Tutorial.

First of all, in ryu, we write it as an" event driven "application. The event is a derived class of ryu.event.EventBase.

When writing an openflow application, it is common to say "first". With ryu, I'm wondering what event to use at this time, but this seems to be good.

app.py


from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import set_ev_cls, MAIN_DISPATCHER

class App1(app_manager.RyuApp):
    @set_ev_cls(ofp_event.EventOFPStateChange, MAIN_DISPATCHER)
    def on_switch_ready(self, ev):
        assert isinstance(ev, ofp_event.EventOFPStateChange)
        print("switch ready")

Execution example. For example, on Ubuntu you can run it by including the ryu-bin package.

ryu-manager --config-file=/dev/null app.py

Event

Since ryu is event driven, we can't start without knowing the event. Under ryu.controller, which is used as standard, there are the following events. First of all, I will list them briefly. I will write a commentary later.

from represents the publisher side of the event and to represents the subscriber side.

ryu.controller.ofp_event

Events with a 1: 1 correspondence to relatively openflow protocol messages are handled.

datapath state notification system

The following is dynamically generated as a subclass of ʻEventOFPMsgBase`. All events are generated from recv loop.

port state Notification system. Issued after EventOFPPortStatus.

ryu.controller.dpset

openflow protocol An event that can handle the enable / disable of datapath and the state of the port at the next higher layer rather than directly. It can be used as a DPSet Service.

Event commentary

Taken together, when connecting to an openflow 1.3 switch, the following events are automatically played.

--TCP connection established

So the so-called on switch ready points are ʻEventOFPStateChange, MAIN_DISPATCHER, or ʻEventDP.

Service

Like the DPSet above, event sources can now be added as services. The service is identified by a string inside ryu. The following services exist in ryu. These are usually enabled automatically when you register and start using the event handler (set_ev_cls).

For example, to use ryu.controller.dpset:

app2.py


from ryu.base import app_manager
from ryu.controller import dpset, ofp_event
from ryu.controller.handler import set_ev_cls

class App2(app_manager.RyuApp):
    @set_ev_cls(dpset.EventDP)
    def on_dp_change(self, ev):
        print("datapath event", ev.enter)

The following example is also an ant as an on switch ready point using Service.

app3.py


import ryu.topology.event
from ryu.base import app_manager
from ryu.controller.handler import set_ev_cls

class App3(app_manager.RyuApp):
    @set_ev_cls(ryu.topology.event.EventSwitchEnter)
    def on_enter(self, ev):
        print("switch ready")

Dependent singleton

As a non-serviced form, you can also register and use a class that explicitly depends on RyuApp._CONTEXTS. Generated as a singleton at startup and passed via kwargs in RyuApp.__init__.

app4.py


from ryu.base import app_manager

class A(object):
   def __init__(self):
       print("init A")

class App4(app_manager.RyuApp):
    _CONTEXTS = dict(a=A)
    def __init__(self, *args, **kwargs):
        super(App4, self).__init__(*args, **kwargs)
        print(kwargs)

This example does not set a handler for openflow messages, so exit after initialization is complete. The ryu itself is an event hub, and the openflow protocol seems to have been created with the idea that it is one of the applications driven by events in the ʻofp_event` category.

Recommended Posts

Ryu tutorial Addendum
sqlalchemy tutorial
PyODE Tutorial 2
Python tutorial
PyODE Tutorial 1
PyODE Tutorial 3
TensorFlow tutorial tutorial