-
Notifications
You must be signed in to change notification settings - Fork 0
OpenFlow in Ryu
The communication processing and a handling of the foundational OpenFlow protocol, e.g. hello, echo, is implemented in Ryu framework. A user Ryu application can implement a handling of necessary OpenFlow event.
An event handler is called with an event object as an argument. The event
object is representation of the OpenFlow message which Ryu received from
OpenFlow switch. The object is a subclass of
ryu.controller.ofp_event.EventOFPMsgBase
class.
class EventOFPMsgBase(event.EventBase):
def __init__(self, msg):
super(EventOFPMsgBase, self).__init__()
self.msg = msg
Each event class is created dynamically from the corresponding OpenFlow message class. Because of that, you cannot find the code of an event class explicitly.
The name of event class is 'Event' + corresponding message class name.
For example, the event class for OFPPacketIn
message is EventOFPPacketIn
.
The msg
attribute of the event class holds the message class instance
which created it.
Each OpenFlow message class is a subclass of
ryu.ofproto.ofproto_parser.MsgBase
class. The MsgBase class is defined as
follows, so every OpenFlow message class have those attributes.
class MsgBase(object):
def __init__(self, datapath):
self.datapath = datapath
self.version = None
self.msg_type = None
self.msg_len = None
self.xid = None
self.buf = None
Attribute | Description |
---|---|
datapath | A ryu.controller.controller.Datapath instance associated with the OpenFlow switch |
version | OpenFlow protocol version |
msg_type | Type of OpenFlow message |
msg_len | Length of the message |
xid | Transaction id |
buf | Raw data |
The following example is handling EventOFPPacketIn and sending OFPPacketOut.
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
class MySwitch(app_manager.RyuApp):
def __init__(self, *args, **kwargs):
super(MySwitch, self).__init__(*args, **kwargs)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
msg = ev.msg
dp = msg.datapath
ofp = dp.ofproto
ofp_parser = dp.ofproto_parser
actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)]
out = ofp_parser.OFPPacketOut(
datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
actions=actions)
dp.send_msg(out)
The following page describes the detail of OpenFlow messages.
OpenFlow V1.0 messages
OpenFlow V1.2 messages
OpenFlow V1.3 messages