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

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

一、实验目的

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

二、实验环境

Ubuntu 20.04 Desktop amd64

三、实验要求

(一)基本要求

  1. 搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。
    启动控制器,利用Web图形界面查看网络拓扑
    ryu-manager ryu/ryu/app/gui_topology/gui_topology.py --observe-links

  2. 阅读Ryu文档的The First Application一节,运行当中的L2Switch,h1 ping h2或h3,在目标主机使用 tcpdump 验证L2Switch,分析L2Switch和POX的Hub模块有何不同。
    (1)建立拓扑:sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10
    (2)运行Ryu:ryu-manager L2Switch.py ,验证L2Switch

    h1 ping h2:h2,h3都接收到icmp报文

     h1 ping h3:h2,h3都接收到icmp报文

    L2Switch和POX的Hub模块有何不同?

    L2Switch代码

     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)

    L2Switch模块

    POX的Hub模块

    所以可得 Ryu的L2Switch模块和POX的Hub模块都是采用洪泛转发,但是不同之处是:可以查看Hub模块下发的流表,无法查看L2Switch模块下发的流表。

  3. 编程修改L2Switch.py,另存为L2xxxxxxxxx.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_to_port的作用是保存mac地址到交换机端口的映射。
    b)simple_switch和simple_switch_13在dpid的输出上有何不同?
    答:simple_switch直接输出dpid,simple_switch_13对dpid进行了格式化,并填充为16位数字,会在不满16位的dpid前补0直到满16位。
    c)相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?
    答:实现了交换机以特性应答消息来响应特性请求的功能。
    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实验的一样的硬超时功能。
      1 from ryu.base import app_manager
      2 from ryu.controller import ofp_event
      3 from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
      4 from ryu.controller.handler import set_ev_cls
      5 from ryu.ofproto import ofproto_v1_3
      6 from ryu.lib.packet import packet
      7 from ryu.lib.packet import ethernet
      8 from ryu.lib.packet import ether_types
      9 
     10 
     11 class SimpleSwitch13(app_manager.RyuApp):
     12     OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
     13 
     14     def __init__(self, *args, **kwargs):
     15         super(SimpleSwitch13, self).__init__(*args, **kwargs)
     16         self.mac_to_port = {}
     17 
     18     @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
     19     def switch_features_handler(self, ev):
     20         datapath = ev.msg.datapath
     21         ofproto = datapath.ofproto
     22         parser = datapath.ofproto_parser
     23 
     24         match = parser.OFPMatch()
     25         actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
     26                                           ofproto.OFPCML_NO_BUFFER)]
     27         self.add_flow(datapath, 0, match, actions)
     28 
     29     def add_flow(self, datapath, priority, match, actions, buffer_id=None, hard_timeout=0):
     30         ofproto = datapath.ofproto
     31         parser = datapath.ofproto_parser
     32 
     33         inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
     34                                              actions)]
     35         if buffer_id:
     36             mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
     37                                     priority=priority, match=match,
     38                                     instructions=inst, hard_timeout=hard_timeout)
     39         else:
     40             mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
     41                                     match=match, instructions=inst, hard_timeout=hard_timeout)
     42         datapath.send_msg(mod)
     43 
     44     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
     45     def _packet_in_handler(self, ev):
     46         # If you hit this you might want to increase
     47         # the "miss_send_length" of your switch
     48         if ev.msg.msg_len < ev.msg.total_len:
     49             self.logger.debug("packet truncated: only %s of %s bytes",
     50                               ev.msg.msg_len, ev.msg.total_len)
     51         msg = ev.msg
     52         datapath = msg.datapath
     53         ofproto = datapath.ofproto
     54         parser = datapath.ofproto_parser
     55         in_port = msg.match['in_port']
     56 
     57         pkt = packet.Packet(msg.data)
     58         eth = pkt.get_protocols(ethernet.ethernet)[0]
     59 
     60         if eth.ethertype == ether_types.ETH_TYPE_LLDP:
     61             # ignore lldp packet
     62             return
     63         dst = eth.dst
     64         src = eth.src
     65 
     66         dpid = format(datapath.id, "d").zfill(16)
     67         self.mac_to_port.setdefault(dpid, {})
     68 
     69         self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
     70 
     71         # learn a mac address to avoid FLOOD next time.
     72         self.mac_to_port[dpid][src] = in_port
     73 
     74         if dst in self.mac_to_port[dpid]:
     75             out_port = self.mac_to_port[dpid][dst]
     76         else:
     77             out_port = ofproto.OFPP_FLOOD
     78 
     79         actions = [parser.OFPActionOutput(out_port)]\
     80 
     81         actions_timeout=[]
     82 
     83         # install a flow to avoid packet_in next time
     84         if out_port != ofproto.OFPP_FLOOD:
     85             match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
     86             # verify if we have a valid buffer_id, if yes avoid to send both
     87             # flow_mod & packet_out
     88             hard_timeout=10
     89             if msg.buffer_id != ofproto.OFP_NO_BUFFER:
     90                 self.add_flow(datapath, 2, match,actions_timeout, msg.buffer_id,hard_timeout=10)
     91                 self.add_flow(datapath, 1, match, actions, msg.buffer_id)
     92                 return
     93             else:
     94                 self.add_flow(datapath, 2, match, actions_timeout, hard_timeout=10)
     95                 self.add_flow(datapath, 1, match, actions)
     96         data = None
     97         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
     98             data = msg.data
     99 
    100         out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
    101                                   in_port=in_port, actions=actions, data=data)
    102         datapath.send_msg(out)


(三)个人总结

       在本次实验中,我已经能够独立部署RYU控制器;也大概能能够理解RYU控制器实现软件定义的集线器和交换机原理,不仅加深了理论知识,还提升了上机实践能力。但是在实验过程中,还是出现了很多问题,比如在lab6下面运行ryu-manager L2Switch.py后,拓扑图执行pingall操作却ping不通,最后,在同学的帮助下才发现需要关闭利用Web图形界面查看网络拓扑的控制器,因为这两个命令可能有冲突。对我而言,这次的实验难度还是挺大的,主要是要对比分析L2Switch和POX的Hub模块的不同,代码一直报错,最后在建立拓扑图的时候没有加上协议,才成功运行出来,所以对于理论还是要加强学习,这样实验才会更加顺利!

posted @ 2022-10-22 14:32  林小刀刀  阅读(40)  评论(0编辑  收藏  举报