• 2019 SDN上机第5次作业


    1.浏览RYU官网学习RYU控制器的安装和RYU开发入门教程,提交你对于教程代码的理解,包括但不限于:

    描述官方教程实现了一个什么样的交换机功能?

    实现交换机在各个端口发送它所接收到的数据包的功能

    控制器设定交换机支持什么版本的OpenFlow?

    OpenFlow v1.0

    控制器设定了交换机如何处理数据包?

    官方代码:

    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER) 
        def packet_in_handler(self, ev): 
            msg = ev.msg 
            dp = msg.datapath
            ofp = dp.ofproto
            ofp_parser = dp.ofproto_parser
    
            actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)]
            out = ofp_parser.OFPPacketOut(
                datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
                actions=actions)
            dp.send_msg(out)
    

    其中
    packet_in_handler是新方法,它的作用是当Ryu收到OpenFlow packet_in消息时,将调用此方法。
    set_ev_cls:修饰器,告诉Ryu什么时间调用它要修饰的那个函数。
    ev.msg是表示packet_in数据结构的对象;
    msg.dp是代表数据路径(开关)的对象;
    dp.ofproto和dp.ofproto_parser是代表Ryu和交换机协商的OpenFlow协议的对象;
    OFPActionOutput类与packet_out消息一起使用,以指定要从中发送数据包的交换机端口。
    OFPPacketOut类用于构建packet_out消息;
    send_msg()让Ryu建立在线数据格式结构并发送给交换机

    2.根据官方教程和提供的示例代码(SimpleSwitch.py),将具有自学习功能的交换机代码(SelfLearning.py)补充完整

    代码已补充完整:

    from ryu.base import app_manager
    from ryu.controller import ofp_event
    from ryu.controller.handler import MAIN_DISPATCHER
    from ryu.controller.handler import set_ev_cls
    from ryu.ofproto import ofproto_v1_0
    
    from ryu.lib.mac import haddr_to_bin
    from ryu.lib.packet import packet
    from ryu.lib.packet import ethernet
    from ryu.lib.packet import ether_types
    
    
    class SimpleSwitch(app_manager.RyuApp):
        # TODO define OpenFlow 1.0 version for the switch
        OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
    
        def __init__(self, *args, **kwargs):
            super(SimpleSwitch, self).__init__(*args, **kwargs)
            self.mac_to_port = {}
    
        def add_flow(self, datapath, in_port, dst, src, actions):
            ofproto = datapath.ofproto
    
            match = datapath.ofproto_parser.OFPMatch(
                in_port=in_port,
                dl_dst=haddr_to_bin(dst), dl_src=haddr_to_bin(src))
    
            mod = datapath.ofproto_parser.OFPFlowMod(
                datapath=datapath, match=match, cookie=0,
                command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
                priority=ofproto.OFP_DEFAULT_PRIORITY,
                flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
    
            # TODO send modified message out
            datapath.send_msg(mod)
    
        @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
        def _packet_in_handler(self, ev):
            msg = ev.msg
            datapath = msg.datapath
            ofproto = datapath.ofproto
    
            pkt = packet.Packet(msg.data)
            eth = pkt.get_protocol(ethernet.ethernet)
    
            if eth.ethertype == ether_types.ETH_TYPE_LLDP:
                # ignore lldp packet
                return
            if eth.ethertype == ether_types.ETH_TYPE_IPV6:
                # ignore ipv6 packet
                return
    
            dst = eth.dst
            src = eth.src
            dpid = datapath.id
            self.mac_to_port.setdefault(dpid, {})
    
            self.logger.info("packet in DPID:%s MAC_SRC:%s MAC_DST:%s IN_PORT:%s", dpid, src, dst, msg.in_port)
    
            # learn a mac address to avoid FLOOD next time.
            self.mac_to_port[dpid][src] = msg.in_port
    
            if dst in self.mac_to_port[dpid]:
                out_port = self.mac_to_port[dpid][dst]
            else:
                out_port = ofproto.OFPP_FLOOD
    
            # TODO define the action for output
            actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
    
            # install a flow to avoid packet_in next time
            if out_port != ofproto.OFPP_FLOOD:
                self.logger.info("add flow s:DPID:%s Match:[ MAC_SRC:%s MAC_DST:%s IN_PORT:%s ], Action:[OUT_PUT:%s] ",
                                 dpid, src, dst, msg.in_port, out_port)
                self.add_flow(datapath, msg.in_port, dst, src, actions)
    
            data = None
            if msg.buffer_id == ofproto.OFP_NO_BUFFER:
                data = msg.data
    
            # TODO define the OpenFlow Packet Out
            out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
                                                       actions=actions, data=data)
            datapath.send_msg(out)
    
        print("PACKET_OUT...")
    

    3.在mininet创建一个最简拓扑,并连接RYU控制器

    from mininet.topo import Topo
    
    class Mytopo(Topo):
    
        def __init__(self):
    
            Topo.__init__(self)
    
            sw=self.addSwitch('s1')
    
            count=1
    
            for i in range(2):
    
                    host = self.addHost('h{}'.format(count))
    
                    self.addLink(host,sw,1,count)
    
                    count = count + 1
    
    topos = {'mytopo': (lambda:Mytopo())}
    

    输入以下命令

    sudo  mn  --custom  '/home/w/Desktop/test5/test5.py'   --topo mytopo --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10
    


    还没有连接ryu控制器的时候,查看s1的流表:

    未连接RYU控制器时查看s1的流表:
    

    打开另一个终端,输入以下命令

    ryu-manager SelfLearning.py
    

    h1 ping h2

    下发流表

     sudo ovs-ofctl dump-flows s1 
    

    开启RYU控制器的终端会显示如下信息,可以看到目标的MAC地址,源的MAC地址,进入端口,输出端口等数据

    5.写下你的实验体会

    第一步就出现了很多问题,升级了python后mininet不能使用了,弄了很久,重新安装了mininet发现ryu不能使用,又发现pip也不能使用了,弄了挺久的
    好在后面的操作比较简单,参考了老师给的资料之后成功完成,不过对实验还有不理解的地方,还要加倍努力

  • 相关阅读:
    [HDOJ3523]Image copy detection
    [HDOJ3526]Computer Assembling
    Ubuntu12.04 配置步骤
    const 详解
    ubuntu 12.04 源
    函数参数和数据成员同名
    友元
    静态数据 成员和静态函数
    成员指针
    内存泄露
  • 原文地址:https://www.cnblogs.com/xgsw/p/11969507.html
Copyright © 2020-2023  润新知