首页 > 其他分享 >实验6:开源控制器实践——RYU

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

时间:2022-10-29 12:00:09浏览次数:76  
标签:控制器 self parser datapath msg 开源 ofproto RYU port

 

一、实验目的

  1. 能够独立部署RYU控制器;
  2. 能够理解RYU控制器实现软件定义的集线器原理;
  3. 能够理解RYU控制器实现软件定义的交换机原理。

二、实验环境

Ubuntu 20.04 Desktop amd64

三、实验要求

(一)基本要求

  1. 搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。

     

  2. 阅读Ryu文档的The First Application一节,运行当中的L2Switch,h1 ping h2或h3,在目标主机使用 tcpdump 验证L2Switch,分析L2Switch和POX的Hub模块有何不同。

    h1 ping h3

    h1 ping h2

    L2Switch.py代码

     1 from ryu.base import app_manager
     2 from ryu.controller import ofp_event
     3 from ryu.controller.handler import MAIN_DISPATCHER
     4 from ryu.controller.handler import set_ev_cls
     5 from ryu.ofproto import ofproto_v1_0
     6 
     7 class L2Switch(app_manager.RyuApp):
     8     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
     9 
    10     def __init__(self, *args, **kwargs):
    11         super(L2Switch, self).__init__(*args, **kwargs)
    12 
    13     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    14     def packet_in_handler(self, ev):
    15         msg = ev.msg
    16         dp = msg.datapath
    17         ofp = dp.ofproto
    18         ofp_parser = dp.ofproto_parser
    19 
    20         actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)]
    21 
    22         data = None
    23         if msg.buffer_id == ofp.OFP_NO_BUFFER:
    24              data = msg.data
    25 
    26         out = ofp_parser.OFPPacketOut(
    27             datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
    28             actions=actions, data = data)
    29         dp.send_msg(out)

    L2SwitchPOXHub模块的不同

    Ryu中,L2Switch下发的流表无法查看;而POX中Hub则可以查看。

    POX的Hub模块

     

  3. 编程修改L2Switch.py,另存为L212106668.py,使之和POX的Hub模块的变得一致?(xxxxxxxxx为学号)
     1 from ryu.base import app_manager
     2 from ryu.ofproto import ofproto_v1_3
     3 from ryu.controller import ofp_event
     4 from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
     5 from ryu.controller.handler import set_ev_cls
     6 
     7 class hub(app_manager.RyuApp):
     8     OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
     9 
    10     def __init__(self, *args, **kwargs):
    11         super(hub, self).__init__(*args, **kwargs)
    12 
    13     @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    14     def switch_feathers_handler(self, ev):
    15         datapath = ev.msg.datapath
    16         ofproto = datapath.ofproto
    17         ofp_parser = datapath.ofproto_parser
    18 
    19         # install flow table-miss flow entry
    20         match = ofp_parser.OFPMatch()
    21         actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)]
    22         # 1\OUTPUT PORT, 2\BUFF IN SWITCH?
    23         self.add_flow(datapath, 0, match, actions)
    24 
    25     def add_flow(self, datapath, priority, match, actions):
    26         # 1\ datapath for the switch, 2\priority for flow entry, 3\match field, 4\action for packet
    27         ofproto = datapath.ofproto
    28         ofp_parser = datapath.ofproto_parser
    29         # install flow
    30         inst = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
    31         mod = ofp_parser.OFPFlowMod(datapath=datapath, priority=priority, match=match, instructions=inst)
    32         datapath.send_msg(mod)
    33 
    34     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    35     def packet_in_handler(self, ev):
    36         msg = ev.msg
    37         datapath = msg.datapath
    38         ofproto = datapath.ofproto
    39         ofp_parser = datapath.ofproto_parser
    40         in_port = msg.match['in_port']  # get in port of the packet
    41 
    42         # add a flow entry for the packet
    43         match = ofp_parser.OFPMatch()
    44         actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
    45         self.add_flow(datapath, 1, match, actions)
    46 
    47         # to output the current packet. for install rules only output later packets
    48         out = ofp_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions)
    49         # buffer id: locate the buffered packet
    50         datapath.send_msg(out)

(二)进阶要求

  1. 阅读Ryu关于simple_switch.py和simple_switch_1x.py的实现,以simple_switch_13.py为例,完成其代码的注释工作,
      1 # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #    http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
     12 # implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 
     16 from ryu.base import app_manager
     17 from ryu.controller import ofp_event
     18 from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
     19 from ryu.controller.handler import set_ev_cls
     20 from ryu.ofproto import ofproto_v1_3
     21 from ryu.lib.packet import packet
     22 from ryu.lib.packet import ethernet
     23 from ryu.lib.packet import ether_types
     24 
     25 
     26 class SimpleSwitch13(app_manager.RyuApp):   # 继承
     27     # 定义openflow版本
     28     OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
     29 
     30     def __init__(self, *args, **kwargs):
     31         super(SimpleSwitch13, self).__init__(*args, **kwargs)   # py2.7版本的书写方式
     32         self.mac_to_port = {}  # 用字典存储MAC地址表
     33 
     34 
     35     # 监听事件,参数是事件名和状态
     36     @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
     37     # switch_features_handler函数是新增缺失流表项到流表中,当封包没有匹配到流表时,就触发packet_in
     38     def switch_features_handler(self, ev):
     39         datapath = ev.msg.datapath  # datapath可认为是交换机n上发生的事
     40         ofproto = datapath.ofproto  # openflow协议的版本
     41         parser = datapath.ofproto_parser  # openflow协议的解析器
     42 
     43         # install table-miss flow entry
     44         #
     45         # We specify NO BUFFER to max_len of the output action due to
     46         # OVS bug. At this moment, if we specify a lesser number, e.g.,
     47         # 128, OVS will send Packet-In with invalid buffer_id and
     48         # truncated packet data. In that case, we cannot output packets
     49         # correctly.  The bug has been fixed in OVS v2.1.0.
     50         match = parser.OFPMatch()  # 空的,为了匹配所有的封包
     51         # actions是为了转送到控制器端口,参数里是发往控制器端口,不进行缓存
     52         actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
     53                                           ofproto.OFPCML_NO_BUFFER)]
     54         self.add_flow(datapath, 0, match, actions)  # 添加流表
     55 
     56     def add_flow(self, datapath, priority, match, actions, buffer_id=None):
     57         # 获取交换机信息
     58         ofproto = datapath.ofproto
     59         parser = datapath.ofproto_parser
     60 
     61         # inst是instruction的缩写,第一个参数是马上执行动作,第二个参数是执行动作的对象
     62         inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
     63                                              actions)]
     64         # 判断是否存在buffer_id,并生成mod对象
     65         if buffer_id:
     66             mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
     67                                     priority=priority, match=match,
     68                                     instructions=inst)
     69         else:
     70             mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
     71                                     match=match, instructions=inst)
     72         datapath.send_msg(mod)   # 发送操作指令给交换机
     73 
     74 
     75     # 监听packet_in消息是否被触发,用来处理未知目的地的封包
     76     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
     77     def _packet_in_handler(self, ev):
     78         # If you hit this you might want to increase
     79         # the "miss_send_length" of your switch
     80         if ev.msg.msg_len < ev.msg.total_len:
     81             self.logger.debug("packet truncated: only %s of %s bytes",
     82                               ev.msg.msg_len, ev.msg.total_len)
     83         msg = ev.msg
     84         datapath = msg.datapath
     85         ofproto = datapath.ofproto
     86         parser = datapath.ofproto_parser
     87         in_port = msg.match['in_port']   # 获取源端口
     88 
     89         pkt = packet.Packet(msg.data)
     90         eth = pkt.get_protocols(ethernet.ethernet)[0]
     91 
     92         if eth.ethertype == ether_types.ETH_TYPE_LLDP:
     93             # ignore lldp packet
     94             return
     95         dst = eth.dst    # 获取目的端口
     96         src = eth.src    # 获取源端口
     97 
     98 
     99         dpid = format(datapath.id, "d").zfill(16)     # MAC地址表和每个交换机之间的识别,用dpid确认
    100         self.mac_to_port.setdefault(dpid, {})
    101 
    102         # 打印日志信息
    103         self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
    104 
    105         # learn a mac address to avoid FLOOD next time.
    106         self.mac_to_port[dpid][src] = in_port   # 学习MAC地址,避免下次泛洪
    107 
    108         if dst in self.mac_to_port[dpid]:   # 如果目标Mac地址已经被学习了,决定哪个从哪个端口发送数据包,否则范洪
    109             out_port = self.mac_to_port[dpid][dst]
    110         else:
    111             out_port = ofproto.OFPP_FLOOD
    112 
    113         actions = [parser.OFPActionOutput(out_port)]
    114 
    115         # 下发流表避免下次触发 packet in 事件
    116         # install a flow to avoid packet_in next time
    117         if out_port != ofproto.OFPP_FLOOD:
    118             match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
    119             # verify if we have a valid buffer_id, if yes avoid to send both
    120             # flow_mod & packet_out
    121             if msg.buffer_id != ofproto.OFP_NO_BUFFER:
    122                 self.add_flow(datapath, 1, match, actions, msg.buffer_id)
    123                 return
    124             else:
    125                 self.add_flow(datapath, 1, match, actions)
    126         data = None
    127         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
    128             data = msg.data
    129         # 构造一个pack_out消息然后发送
    130         out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
    131                                   in_port=in_port, actions=actions, data=data)
    132         datapath.send_msg(out)

     

    并回答下列问题:

    a) 代码当中的mac_to_port的作用是什么?
      保存mac地址到交换机端口的映射
    b) simple_switch和simple_switch_13在dpid的输出上有何不同?
      差别在于:simple_switch直接输出dpid,而simple_switch_13则在dpid前端填充0直至满16位
    c) 相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?
           switch_features_handler函数是新增缺失流表项到流表中,当封包没有匹配到流表时,就触发packet_in
    d) simple_switch_13是如何实现流规则下发的?
      在接收到packetin事件后,首先获取包学习,交换机信息,以太网信息,协议信息等。若以太网类型是  LLDP类型,则不予处理。如果不是,则获取源端口的目的端口和交换机id,先学习源地址对应的交换机的入端口,再查看是否已经学习目的mac地址,如果没有则进行洪泛转发。如果学习过该mac地址,则查看是否有buffer_id,如果有的话,则在添加流表信息时加上buffer_id,向交换机发送流表。
    e) switch_features_handler和_packet_in_handler两个事件在发送流规则的优先级上有何不同?

     switch_features_handler下发流表的优先级比_packet_in_handler的优先级高。

  2. 编程实现和ODL实验的一样的硬超时功能。
    # 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
    
    
    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, 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, hard_timeout=0):
            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, hard_timeout=hard_timeout)
            else:
                mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
                                        match=match, instructions=inst, hard_timeout=hard_timeout)
            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
    
            dpid = format(datapath.id, "d").zfill(16)
            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)]\
    
            actions_timeout=[]
    
            # 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
                hard_timeout=10
                if msg.buffer_id != ofproto.OFP_NO_BUFFER:
                    self.add_flow(datapath, 2, match,actions_timeout, msg.buffer_id,hard_timeout=10)
                    self.add_flow(datapath, 1, match, actions, msg.buffer_id)
                    return
                else:
                    self.add_flow(datapath, 2, match, actions_timeout, hard_timeout=10)
                    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-manager L2Switch.py一直报错不能执行,然后重启虚拟机后可以执行,发现应该是原先执行的ryu-manager ryu/ryu/app/gui_topology/gui_topology.py --observe-links这条命令没有退出。对比上次实验这回不用一直清除上一条拓扑,直接新建就可以了方便不少。通过本次实验我能够部署RYU控制器,顺利打开ryu的可视化图形界面,此外也进一步熟悉了之前tcpdump命令的用法,还了解到Hub和L2Switch的区别就在于下发流表是否可以查看,希望可以在之后的实验中能够更加深入地理解到它们的区别。

标签:控制器,self,parser,datapath,msg,开源,ofproto,RYU,port
From: https://www.cnblogs.com/huang0724/p/16837386.html

相关文章

  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopam......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境(一)基本要求下载虚拟机软......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopam......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境(一)基本要求下载虚拟机软......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环......
  • unity 使用动画器覆盖控制器(AnimatorOverrideController)快速创建新对象的动画控制器
    注释:假设你已经创建好了一个怪物对象的基础动画控制,此时需要在添加一个全新的敌人,你又懒得从新写一堆参数和代码,那么就可以使用这种重写控制器来快速生成控制器参数则使......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU(一)基本要求1、搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。sudomn--topo=single,3--mac......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验......