实验六

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

 

一、基础实验

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


(2)L2Switch和POX的Hub模块的区别



hub模块是通过向交换机添加流表项来实现的对数据包的转发操作,而L2Switch则是通过控制器接收交换机发送来的消息,生成对应的消息并发送给交换机,进而通过该消息指导交换机对数据包的转发

(3)L2212006199.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_features_handler(self, ev):
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

    #install the table-miss flow entry
        match = ofp_parser.OFPMatch()
        actions=[ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,ofproto.OFPCML_NO_BUFFER)]
        self.add_flow(datapath, 0, match, actions)

    def add_flow(self, datapath, priority, match, actions):
        #add a flow entry, and install it into datapath
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

        #contruct a flow_mod msg and sent it
        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']

        #contruct a flow entry
        match = ofp_parser.OFPMatch()
        actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_FLOOD)]

        #install flow_mod to a avoid packet_ining next time
        self.add_flow(datapath, 1, match, actions)

        out = ofp_parser.OFPPacterOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port,actions=actions)
        datapath.send_msg(out)

二、进阶实验

(1)阅读Ryu关于simple_switch.py和simple_switch_1x.py的实现,以simple_switch_13.py为例,完成其代码的注释工作,并回答下列问题:

a) 代码当中的mac_to_port的作用是什么?

获取并存放端口的mac地址

b) simple_switch和simple_switch_13在dpid的输出上有何不同?

simple_switch_13:dpid = format(datapath.id, "d").zfill(16) 调用后在数值基础上从左开始填充0,直到总位数达到16位
simple_switch:dpid = datapath.id 直接调用datapath中的成员参数 
因此二者之间的区别在于数据的格式不同

c) 相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?

功能 回复留言
交换机使用此消息来回复属性请求消息

d) simple_switch_13是如何实现流规则下发的?

def send_flow_mod(self, datapath):
    ofp = datapath.ofproto
    ofp_parser = datapath.ofproto_parser

    cookie = cookie_mask = 0
    table_id = 0
    idle_timeout = hard_timeout = 0
    priority = 32768
    buffer_id = ofp.OFP_NO_BUFFER
    match = ofp_parser.OFPMatch(in_port=1, eth_dst='ff:ff:ff:ff:ff:ff')
    actions = [ofp_parser.OFPActionOutput(ofp.OFPP_NORMAL, 0)]
    inst = [ofp_parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS,
                                             actions)]
    req = ofp_parser.OFPFlowMod(datapath, cookie, cookie_mask,
                                table_id, ofp.OFPFC_ADD,
                                idle_timeout, hard_timeout,
                                priority, buffer_id,
                                ofp.OFPP_ANY, ofp.OFPG_ANY,
                                ofp.OFPFF_SEND_FLOW_REM,
                                match, inst)
    datapath.send_msg(req)
posted @ 2022-10-30 20:55  JKL1234  阅读(16)  评论(0)    收藏  举报