1
votes

I need to identify a control packet from Python RYU-controller. In other words: How I can to do the following instruction?

If (I receive a OFPT_PACKET_OUT msg from ryu-controller)
   do something (for example all control traffic must mirroring to an output port)

and How can I match this rule?

I saw in OpenFlow v1.3 specification that there is a ofproto.OFPP_CONTROLLER reserved port: How can I use it as an ingress port?

From OFv1.3 spec.: "OFPP_CONTROLLER: Represents the control channel with the OpenFlow controller. Can be used as an ingress port or as an output port.

When used as an output port, encapsulate the packet in a packet-in message and send it using the OpenFlow protocol.

When used as an ingress port, identify a packet originating from the controller."

Thanks for the help.

1
I need to identify only OFPT_PACKET_OUT messages from controllerMaurizio Marrocco

1 Answers

1
votes

Regarding the first part of your question, let's see a basic Layer 2 Switch that simply floods the incoming packets to all output ports:

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 L2Switch(app_manager.RyuApp):
    def __init__(self, *args, **kwargs):
        super(L2Switch, 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 last two statements are

out = ofp_parser.OFPPacketOut(
    datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
    actions=actions)
dp.send_msg(out)

These statements generate a packet_out message, however, I don't think there's a corresponding event that is raised for a packet_out message (Like a packet_in message generates the EventOFPPacketIn event which can be detected in code, and a method can be attached to it). I haven't used Ryu API much, but I think the reason is simple. A packet_out message is sent via the code itself, and you can simply add a few more lines of code after the lines generating this event. These few lines can execute whatever you want to do upon the generation of a packet_out message. For example, in the above code, you can just add the lines mirroring control traffic to a specific port after the dp.send_msg(out) line. Correct/edit my answer if I'm wrong.