SDN on the fifth machine experiments

1. Visit the official website RYU RYU learn to install and develop introductory tutorial Submit your understanding of the tutorials code RYU controller.

1. Controller source installation RYU

sudo apt-get install python3-pip
git clone https://github.com/osrg/ryu.git
cd ryu
sudo pip3 install -r tools/pip-requires -i https://pypi.tuna.tsinghua.edu.cn/simple
sudo python3 setup.py install
Run the ryu-manager will complain

Presumably due ubuntu comes with python, and we use pip install, will start a problem ryu
解决方法:
cd ryu
pip3 install .
At this point you can start ryu normal


2. Learn to develop introductory tutorial RYU

  • The official tutorial describes realized what kind of switch function?

    The received packet to all ports.
  • What version of the OpenFlow controller setting switches support?

    Supports OpenFlow 1.0 version
  • The controller sets the switch how to handle a packet?

  1. Introducing the package from ryu
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
  • base

    app_manager.py its role is to manage RYU center applications. RYU for loading application, receives information transmitted from the APP over, but also to complete the message route.

    Its main function has app registration, cancellation, find, and defines RYUAPP base class that defines the basic attributes of RYUAPP. Comprising name, threads, events, event_handlers observers and other members, and a number corresponding to the basic functions. Such as: start (), stop () and the like.

    This file also defines AppManager base class for managing APP. Defines the load APP and other functions.
  • controller-- achieve interconnection and event processing between the controller and the switch

    controller folder in a number of very important documents, such as events.py, ofp_handler.py, controller.py and so on. Wherein controller.py defined OpenFlowController base class. A controller for defining OpenFlow for processing connected to the switch controller and the other events, but also can generate events and routing events. Define its event system, you can view events.py and ofp_events.py.

    Defines the basic handler in ofp_handler.py in (call it how it should handle handler???), Completed the basic, such as: shaking hands, and keep alive the error information processing and other functions. More should be defined as packet_in_handler in the app.

    在dpset.py文件中,定义了交换机端的一些消息,如端口状态信息等,用于描述和操作交换机。如添加端口,删除端口等操作。
  • ofproto

    在这个目录下,基本分为两类文件,一类是协议的数据结构定义,另一类是协议解析,也即数据包处理函数文件。如ofproto_v1_0.py是1.0版本的OpenFlow协议数据结构的定义,而ofproto_v1_0_parser.py则定义了1.0版本的协议编码和解码。
  1. 对交换机进行具体的编码设置
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)]
        out = ofp_parser.OFPPacketOut(
            datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
            actions=actions)
        dp.send_msg(out)
  • 设置交换机支持的OpenFlow版本号

    OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
  • packet_in_handler方法用于处理packet_in事件,表明当Ryu收到OpenFlow packet_in消息时,将产生事件
  • ev.msg:每一个事件类ev中都有msg成员,用于携带触发事件的数据包。
  • msg.datapath:已经格式化的msg其实就是一个packet_in报文,msg.datapath直接可以获得packet_in报文的datapath结构。datapath用于描述一个交换网桥。也是和控制器通信的实体单元。datapath.send_msg()函数用于发送数据到指定datapath。
  • datapath.ofproto对象是一个OpenFlow协议数据结构的对象,成员包含OpenFlow协议的数据结构,如动作类型OFPP_FLOOD。
  • datapath.ofp_parser则是一个按照OpenFlow解析的数据结构。
  • actions是一个列表,用于存放action list,可在其中添加动作。
  • 通过ofp_parser类,可以构造构造packet_out数据结构。括弧中填写对应字段的赋值即可。

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控制器

使用python脚本创建拓扑

from mininet.topo import Topo

class MyTopo(Topo):

    def __init__(self):

        # initilaize topology
        Topo.__init__(self)

        # add hosts and switches
        h1 = self.addHost('h1')
        h2 = self.addHost('h2')

        s1 = self.addSwitch('s1')

        # add links
        self.addLink(h1, s1, 1, 1)
        self.addLink(h2, s1, 1, 2)
        
topos = {'mytopo': (lambda: MyTopo())}


4.验证自学习交换机的功能,提交分析过程和验证结果

h1 ping h2

查看流表
sudo ovs-ofctl dump-flows s1

5.心得体会

由于我是在上机当天下午装的RYU,有了助教的博客帮助,总体上没有出现什么大的问题。跟前面几次上机实验相比,这一次的实验更加依赖于Python代码, 让我不得不感叹一下自己当初学的Python是有多差劲,是时候趁着这一学期,好好地补一补,结合实践地学习。

参考资料

Guess you like

Origin www.cnblogs.com/yumesinyo/p/11973223.html