sdn第六次实践作业

(一)

运行截图:
image

tcpdump验证过程:
image
(h2和h3都收到了h1的数据包)

分析:L2 Switch模块和POX的hub无太大区别,都是使用OFPP_FLOOD标记让数据包向所有端口发送,
分析代码:Hub模块有被动和主动两个方式,被动是处理每个数据包然后让他们泛洪,主动是给交换机下发流表,告诉每个交换机去泛洪。L2 Switch是直接构造一个数据去泛洪。

(二)进阶

simple_switch13的源代码与注释

from ryu.base import app_manager

from ryu.controller import ofp_event

from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER

from ryu.controller.handler import set_ev_cls

from ryu.ofproto import ofproto_v1_3

from ryu.lib.packet import packet

from ryu.lib.packet import ethernet

from ryu.lib.packet import ether_types





class SimpleSwitch13(app_manager.RyuApp):

    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] #标注版本号为1.3



    def __init__(self, *args, **kwargs): # 初始化类

        super(SimpleSwitch13, self).__init__(*args, **kwargs)

        self.mac_to_port = {} # 初始化mac_to_port数组



    @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()

        actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,

                                          ofproto.OFPCML_NO_BUFFER)]

        self.add_flow(datapath, 0, match, actions)



    def add_flow(self, datapath, priority, match, actions, buffer_id=None): # 增加流表

        ofproto = datapath.ofproto

        parser = datapath.ofproto_parser



        inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,

                                             actions)]

        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)

	# 构建一个流量调制信息并发送

    @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

        datapath = msg.datapath

        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:

            # ignore lldp packet

            return

        dst = eth.dst

        src = eth.src # 用packet library分析收到的数据包



        dpid = format(datapath.id, "d").zfill(16)# 获得Datapath ID以便于识别OpenFlow交换机

        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.

        # 记录mac地址以避免下次的消息范洪

        self.mac_to_port[dpid][src] = in_port



        if dst in self.mac_to_port[dpid]: # 如果目标Mac地址已经被学习了,决定哪个从哪个端口发送数据包。否则范洪

            out_port = self.mac_to_port[dpid][dst]

        else:

            out_port = ofproto.OFPP_FLOOD 

        actions = [parser.OFPActionOutput(out_port)] #构造action表



        # install a flow to avoid packet_in next time

        # 安装流表以避免下次pactet_in

        if out_port != ofproto.OFPP_FLOOD:

            match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)

            # verify if we have a valid buffer_id, if yes avoid to send both

            # 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

	# 构造一个pack_out消息然后发送

        out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,

                                  in_port=in_port, actions=actions, data=data)

        datapath.send_msg(out)

a) 代码当中的mac_to_port的作用是什么?
答:记录mac地址以避免下次的消息Flood

b) simple_switch和simple_switch_13在dpid的输出上有何不同?
答:simple_switch_13对dpid进行了格式化,并填充为16位数字

c) 相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?
答:安装无目标的流表条目

d) simple_switch_13是如何实现流规则下发的?
答:构造一个flow_mod消息并发送

e) switch_features_handler和_packet_in_handler两个事件在发送流规则的优先级上有何不同
答:switch_features_handler发送的priority=0,_packet_in_handler发送的流表的priority设置为1

心得体会

RYU的安装代码参考:

git clone https://github.com/faucetsdn/ryu.git
cd ryu
sudo pip3 install -r tools/pip-requires -i https://pypi.tuna.tsinghua.edu.cn/simple
sudo python3 setup.py install

L2Switch源码

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

class L2Switch(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]

    def __init__(self, *args, **kwargs):
        super(L2Switch, self).__init__(*args, **kwargs)

    @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)]

        data = None
        if msg.buffer_id == ofp.OFP_NO_BUFFER:
             data = msg.data

        out = ofp_parser.OFPPacketOut(
            datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
            actions=actions, data = data)
        dp.send_msg(out)

遇到困难:可视化拓扑图光秃秃的只有一个交换机
原因:本来就只能显示交换机。和ODL不一样。尴尬
L2Switch的代码似乎为了教学而写的简单。在文档下面也写了觉得这个L2Switch太蠢?可以自己写。。
进阶心得:反正就是找资料。不然还要理解源码还要理解ryu的各个组件有什么作用。

posted @ 2021-10-12 18:44  张牧歌  阅读(146)  评论(0编辑  收藏  举报