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

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

时间:2022-10-21 00:58:33浏览次数:57  
标签:控制器 self parser datapath ofproto 开源 msg RYU port

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

一、实验目的

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

二、实验环境

Ubuntu 20.04 Desktop amd64

三、实验要求

(一)基本要求
搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。
image
image
image

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

(1)运行并使用 tcpdump 验证L2Switch

创建L2Switch.py文件,并保存在/home/用户名/032002101/lab6/中
image
image
h1 ping h2
image

h1 ping h3
image

分析L2Switch和POX的Hub模块有何不同。

h1 ping h2时h3也能收到数据包,h1 ping h3时h2也能收到数据包,说明L2Switch模块的功能同hub模块,都采用洪泛转发,但L2Switch模块下发的流表无法查看,而Hub模块下发的流表可以查看

编程修改L2Switch.py,另存为L2032002101.py,使之和POX的Hub模块的变得一致?
def __init__(self, *args, **kwargs):
    super(SimpleSwitch13, self).__init__(*args, **kwargs)
    self.mac_to_port = {}  # 定义保存mac地址到端口的一个映射

# 处理SwitchFeatures事件
@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()  # match指流表项匹配,OFPMatch()指不匹配任何信息
    actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                  ofproto.OFPCML_NO_BUFFER)]
    self.add_flow(datapath, 0, match, actions)

# add_flow()增加流表项
def add_flow(self, datapath, priority, match, actions, buffer_id=None): # datapath:指定的 Switch;priority:此规则的优先权;match:此规则的 Match 条件;actions:动作
    # 获取交换机信息
    ofproto = datapath.ofproto
    parser = datapath.ofproto_parser
    # 对action进行包装
    inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                     actions)]
    # 判断是否存在buffer_id,并生成mod对象
    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)

# 处理PacketIn事件
@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    # ev.msg 是代表packet_in data structure对象
    datapath = msg.datapath
    # dp. ofproto 和 dp.ofproto_parser 是代表 Ryu 和交换机谈判的 OpenFlow 协议的对象
    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:
        # 忽略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

    # 查看是否已经学习过该目的mac地址
    if dst in self.mac_to_port[dpid]:  # 如果目的地址存在于mac_to_port中
        out_port = self.mac_to_port[dpid][dst]
    # 没有学习过该目的mac地址
    else:
        out_port = ofproto.OFPP_FLOOD  # OFPP_FLOOD标志表示应在所有端口发送数据包,即洪泛

    actions = [parser.OFPActionOutput(out_port)]

    # 下发流表避免下次触发 packet in 事件
    if out_port != ofproto.OFPP_FLOOD:
        match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
        # 验证我们是否有一个有效的buffer_id,如果是避免发送两个
        # 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

    # 发送Packet_out数据包
    out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
                          in_port=in_port, actions=actions, data=data)
    # 发送流表
    datapath.send_msg(out)`

image

个人总结

本次实验偏难,花费的时间也较多,通过这次实验学习了一些ryu的基础知识,过ryu的使用,明白了ryu与pox转发的流表的区别,pox是直接向交换机发送流表项的,而ryu要经过处理packet_in事件后,才向交换机下发流表,稍微理解了ryu和pox的一些区别,对ryu的理解还不是很深入还需要更多的学习。

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

相关文章

  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 实验5:开源控制器实践——POX
    基础要求只需要提交h1pingh2、h2和h3的tcpdump抓包结果截图,外加L2_learning模块代码流程图,其余文字请勿赘述;h1pingh2h1pingh3结论:无论h1pingh2还是h1ping......
  • .Net Core WebApi 控制器自动创建文件夹上传图片
    ///<summary>///异步图片或文件上传///</summary>///<paramname="formFile"></param>///<returns></returns>[Http......
  • 实验五:开源控制器实践——POX
    (一)基本要求1、POX的forwarding.hubh1pingh2h1pingh3h2pingh3结论:将数据包广播转发2、POX的forwarding.l2_learningh1pingh2h1pingh3h2pingh3......
  • 实验5:开源控制器实践——POX
    (一)基本要求:1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,控制器使用部署于本地的POX(默认监听6633端口)1)生成拓扑:sudomn--topo=single,3--mac--controller=remote,ip......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......
  • 测试开发jmeter forEach控制器
    测试开发jmeterforEach控制器 forEach控制器的使用场景:主要是对大量数据轮询就行接口请求 forEach控制器的使用前提:将数据进行参数化测试开发jmeterforEach控制器的......
  • 经纬恒润新一代差速四驱域控制器成功量产
        作为本土先进的汽车电子系统供应商,经纬恒润在汽车动力传动领域也有着丰富的研发和生产经验。目前,公司已推出了三代差速锁电子控制单元,均满足ISO26262ASIL-B功......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的1.能够理解POX控制器的工作原理;2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;3.......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......