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

一、L2Switch和POX的Hub模块有何不同

h1 ping h3

h1 ping h2

Hub和L2Switch模块都是洪泛转发,但L2Switch模块下发的流表无法查看,而Hub模块下发的流表可以查看。

二、提交修改过的L2xxxxxxxxx.py代码

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)

三、能够体现和验证修改的相关截图


四、个人总结

建立拓扑:sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10

通过Ryu图形界面查看网络拓扑:http://127.0.0.1:8080

运行L2Switch: ryu-manager L2Switch.py

使用 tcpdump 验证L2Switch:开启主机终端 xterm h2 h3 ,在h2主机终端中输入tcpdump -nn -i h2-eth0 ,在h3主机终端中输入tcpdump -nn -i h3-eth0 ,h1 ping h3

体现和验证修改:ryu-manager L2xxxxxxxxx.py ,mininet>dpctl dump-flows

posted @ 2022-10-22 02:19  飞舟鲫鱼  阅读(11)  评论(0编辑  收藏  举报