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

基本要求

  1. 搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。
生成拓扑
sudo mn --topo=single,3 --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10<details>
启动控制器
ryu-manager ryu/ryu/app/gui_topology/gui_topology.py --observe-links<details>
  1. 阅读Ryu文档的The First Application一节,运行当中的L2Switch,h1 ping h2或h3,在目标主机使用 tcpdump 验证L2Switch,分析L2Switch和POX的Hub模块有何不同。


开启主机终端

mininet> xterm h1 h2 h3

抓取数据包

tcpdump -nn -i h2-eth0
tcpdump -nn -i h3-eth0

查看流表的命令

dpctl dump-flows

结论:都采用洪泛转发,L2Switch模块运行时不能查看流表,原因是 dp.send_msg(out) 发送的包里面没有流表项, Hub模块运行时可以查看流表,发送的是数据流表. 因此 dpctl dump-flows 可以查看

  1. 编程修改L2Switch.py,另存为L2xxxxxxxxx.py,使之和POX的Hub模块的变得一致?(xxxxxxxxx为学号)
from ryu.base import app_manager
from ryu.ofproto import ofproto_v1_3
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
 
 
class hub(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
 
    def __init__(self, *args, **kwargs):
        super(hub, self).__init__(*args, **kwargs)
 
    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    def switch_feathers_handler(self, ev):
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser
 
        # install flow table-miss flow entry
        match = ofp_parser.OFPMatch()
        actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)]
        # 1\OUTPUT PORT, 2\BUFF IN SWITCH?
        self.add_flow(datapath, 0, match, actions)
 
    def add_flow(self, datapath, priority, match, actions):
        # 1\ datapath for the switch, 2\priority for flow entry, 3\match field, 4\action for packet
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser
        # install flow
        inst = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
        mod = ofp_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):
        msg = ev.msg
        datapath = msg.datapath
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser
        in_port = msg.match['in_port']  # get in port of the packet
 
        # add a flow entry for the packet
        match = ofp_parser.OFPMatch()
        actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
        self.add_flow(datapath, 1, match, actions)
 
        # to output the current packet. for install rules only output later packets
        out = ofp_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions)
        # buffer id: locate the buffered packet
        datapath.send_msg(out)

个人总结

看着PPT的话前面难度不大,但是图形查看器和Ryu不能同时开,否则无法ping通,非常的奇怪,我在这里浪费了一个小时的时间. 最后在舍友的提醒下才关掉图形界面,才继续下一个操作.
下一个操作和上一个实验基本相同,只要跟着做就行了. 在最后修改代码阶段,查看了其他同学的代码,但因为OpenFlow的版本太低,出现了无法ping通的情况. 最后是查了百度之后,重新更新了mininet,发现不是版本的问题. 而是构建拓扑时使用的OpenFlow版本是1.0的原因,修改成1.3后ping通.
莫名其妙的问题很多,也就浪费了很多时间,做了很久.

posted on 2022-10-28 22:51  zzzxxzxzxzxzxz  阅读(169)  评论(0编辑  收藏  举报

导航