ryu代码分析-openflow协议

协议定义文件

下图是ryu中关于openflow协议的定义文件,从图中可以看出ryu支持 1.0、1.2、1.3、1.5版本的协议,目前使用主流是1.3版本,我们公司在使用时也是1.3版本,所以主要分析1.3版本。


图片.png

ofproto_v1_3.py 和 ofproto_v1.3_parser.py的关系

ofproto_v1_3.py 中对协议的定义,ofproto_v1_3_parser.py在上层又进行了封装,我们在写自己的代码时使用的是封装之后的变量。

ofproto_v1_3.py的内容基本如下:


图片.png

其中,红框中的 "OFPT_PACET_IN",可以在 ofproto_v1.3_parser.py中找到如下图所示:


图片.png

所以,在我们需要知道ryu中需要使用协议中的哪个关键字可以在,ofproto_v1_3.py找到协议,并在ofproto_v1_3_parser.py 中找到使用方法。

以下是openflow1.3协议文档
https://www.opennetworking.org/wp-content/uploads/2014/10/openflow-spec-v1.3.0.pdf

如何编写自己逻辑

分析一下 simple_switch_13.py,我们写完自己的代码后格式应该与示例差不多,需要将代码启动,启动方法 ryu-manager simple_switch_13.py 或 直接修改 ryu/cmd/manager.py,红框是新写的代码。再执行 python manager.py


图片.png


from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types


class SimpleSwitch13(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]

    def __init__(self, *args, **kwargs):
        super(SimpleSwitch13, self).__init__(*args, **kwargs)
        self.mac_to_port = {}


     # set_ev_cls 中的 第一个参数 ofp_event.EventOFPSwitchFeatures通过ide没有办法找到,
     # 因为这个定义是在代码运行后生成的。
    # 这个给阅读代码带不方便
    # 1. 先在 ofproto_v1_3.py找到协议关键字
    # 2. 在ofproto_v1_3_parser.py 找到对应的class
    # 3. 在set_ev_cls装饰器传参数里直接在 第2步找到的class name前面加上"Event"
    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    def switch_features_handler(self, ev):
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        # install table-miss flow entry
        #
        # We specify NO BUFFER to max_len of the output action due to
        # OVS bug. At this moment, if we specify a lesser number, e.g.,
        # 128, OVS will send Packet-In with invalid buffer_id and
        # truncated packet data. In that case, we cannot output packets
        # correctly.  The bug has been fixed in OVS v2.1.0.
        match = parser.OFPMatch()
        actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                          ofproto.OFPCML_NO_BUFFER)]
        self.add_flow(datapath, 0, match, actions)

    def add_flow(self, datapath, priority, match, actions, buffer_id=None):
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                             actions)]
        if buffer_id:
            mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
                                    priority=priority, match=match,
                                    instructions=inst)
        else:
            mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
                                    match=match, instructions=inst)
        datapath.send_msg(mod)

    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    # @set_ev_cls(ofproto_v1_3.OFPT_PACKET_IN, MAIN_DISPATCHER)
    def _packet_in_handler(self, ev):
        # If you hit this you might want to increase
        # the "miss_send_length" of your switch
        if ev.msg.msg_len < ev.msg.total_len:
            self.logger.debug("packet truncated: only %s of %s bytes",
                              ev.msg.msg_len, ev.msg.total_len)
        msg = ev.msg
        datapath = msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        in_port = msg.match['in_port']

        print(msg)

        pkt = packet.Packet(msg.data)
        eth = pkt.get_protocols(ethernet.ethernet)[0]

        if eth.ethertype == ether_types.ETH_TYPE_LLDP:
            # ignore lldp packet
            return
        dst = eth.dst
        src = eth.src

        dpid = datapath.id
        self.mac_to_port.setdefault(dpid, {})

        self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)

        # learn a mac address to avoid FLOOD next time.
        self.mac_to_port[dpid][src] = in_port

        if dst in self.mac_to_port[dpid]:
            out_port = self.mac_to_port[dpid][dst]
        else:
            out_port = ofproto.OFPP_FLOOD

        actions = [parser.OFPActionOutput(out_port)]

        # install a flow to avoid packet_in next time
        if out_port != ofproto.OFPP_FLOOD:
            match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
            # verify if we have a valid buffer_id, if yes avoid to send both
            # flow_mod & packet_out
            if msg.buffer_id != ofproto.OFP_NO_BUFFER:
                self.add_flow(datapath, 1, match, actions, msg.buffer_id)
                return
            else:
                self.add_flow(datapath, 1, match, actions)
        data = None
        if msg.buffer_id == ofproto.OFP_NO_BUFFER:
            data = msg.data

        out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
                                  in_port=in_port, actions=actions, data=data)
        datapath.send_msg(out)

你可能感兴趣的:(ryu代码分析-openflow协议)