实验六:开源控制器实践——RYU

任务一

完成Ryu控制器的安装。
SDN第六次上机作业_初始化

任务二

搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器。
SDN第六次上机作业_请求超时_02

任务三

通过Ryu的图形界面查看网络拓扑。
SDN第六次上机作业_初始化_03

任务四

阅读Ryu文档的The First Application一节,运行并使用 tcpdump 验证L2Switch,分析和POX的Hub模块有何不同。

  • h1 ping h2
    SDN第六次上机作业_初始化_04

  • h1 ping h3
    SDN第六次上机作业_mac地址_05

  • 分析和POX的Hub模块有何不同
    此应用程序使用OFPP_FLOOD标志表示应在所有端口发送数据包。pox的hub也是对所以端口发送数据包。
    只是集线器hub跟switch所基于转发的形式有所不同而已

进阶任务

阅读Ryu关于simple_switch.py和simple_switch_1x.py的实现,以simple_switch_13.py为例,完成其代码的注释工作,并回答下列问题:
a) 代码当中的mac_to_port的作用是什么?
b) simple_switch和simple_switch_13在dpid的输出上有何不同?
c) 相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?
d) simple_switch_13是如何实现流规则下发的?
e) switch_features_handler和_packet_in_handler两个事件在发送流规则的优先级上有何不同?

  • 代码注释

# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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
# 继承ryu.base.app_manager.RyuApp

class SimpleSwitch13(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]  # 版本号为1.3

    def __init__(self, *args, **kwargs):  # 初始化类
        super(SimpleSwitch13, self).__init__(*args, **kwargs)
        self.mac_to_port = {}  # 初始化mac_to_port数组

    @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)
    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']

        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  # 用packet library分析收到的数据包

        dpid = format(datapath.id, "d").zfill(16)  # 获得Datapath ID以便于识别OpenFlow交换机
        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  # 记录mac地址以避免下次的消息范洪

        if dst in self.mac_to_port[dpid]:  # 如果目标Mac地址已经被学习了,决定哪个从哪个端口发送数据包。否则范洪
            out_port = self.mac_to_port[dpid][dst]
        else:
            out_port = ofproto.OFPP_FLOOD

        actions = [parser.OFPActionOutput(out_port)]  # 构造action表

        # 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
        # 构造一个pack_out消息然后发送
        out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
                                  in_port=in_port, actions=actions, data=data)
        datapath.send_msg(out)
  • 问题

a) 代码当中的mac_to_port的作用是什么?

  • 学习mac地址,保存mac地址到交换机端口的映射,就不使用洪泛转发

b) simple_switch和simple_switch_13在dpid的输出上有何不同?

  • simple_switch_13对dpid进行了格式化,并填充为16位数字

c) 相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?

  • 安装无目标的流表条目

d) simple_switch_13是如何实现流规则下发的?

  • 收到包后进行学习,如果mac地址存在则进行转发,不存在就使用洪泛转发

e) switch_features_handler和_packet_in_handler两个事件在发送流规则的优先级上有何不同?

  • switch_features_handler发送的priority=0,_packet_in_handler发送的流表的priority设置为1

总结

  • ryu下载时一直请求超时,问了同学他们就一直试,试到可以为止;
  • 在写L2Switch.py文件的时候,用vim一直写不进去,后面在别的地方写完用代码移进去,但是打不开,报错模块找不到,一开始以为是python版本的问题,后面发现也不是,一直没找到方法 ,换了个虚拟机重新安装一遍就好了,感觉是在安装的时候哪一步没搞好导致的,还没找到具体原因;
  • 进阶任务还是有难度,看得头很晕,找资料、参考已完成同学的博客才勉强完成。