我是靠谱客的博主 无聊冷风,最近开发中收集的这篇文章主要介绍OVS实验笔记(二):Mininet上使用controller代替手动设置flow table.,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

在原先的拓扑基础上进行操作,和上文一样的topo:

有了前面的学习基础,这次我们不再手动去设置s1 s2 s3 s4的flow table了,而改用远程controller

mininet在创建topo时会自带一个controller:c0,xterm c0登录它

在上面使用以下命令安装ryu:

sudo apt-get update
sudo apt install python3-pip
pip3 install ryu

然后cd到ryu下的app文件夹下使用下面的命令运行写好的controller脚本(放在文章最后):

ryu-manager FourSW.py --verbose

(1)

这个过程可能用到cp 文件存储路径 ryu/app 来把文件复制到app文件夹里

(2)

把windows下的py文件直接拖到ubuntu里打开可能会产生空格不对的问题,然后运行时报错module not found,需要检查一下

controller代码如下:

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.ofproto import ether
from ryu.ofproto import inet

class ExampleSwitch13(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]

    def __init__(self, *args, **kwargs):
        super(ExampleSwitch13, 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
        parser = datapath.ofproto_parser

        # install the table-miss flow entry.
        match = parser.OFPMatch()
        actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)]
        self.add_flow(datapath, 0, match, actions)
        dpid = datapath.id
        print("******setting features for s" + str(dpid) + "******")
        self.settings_for_switches(datapath, dpid)

    #下发流表逻辑
    def add_flow(self, datapath, priority, match, actions):
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        # construct flow_mod message and send it.
        inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
        mod = parser.OFPFlowMod(datapath=datapath, priority=priority, match=match, instructions=inst)
        datapath.send_msg(mod)
        	
    #对icmp单独的转发规则:h1-s1-s3-s4-h2/h2-s4-s3-s1-h1
    def add_icmp_rules(self, datapath, in_port, out_port):
        priority = 20
        parser = datapath.ofproto_parser
        actions = [parser.OFPActionOutput(out_port)]
        match = parser.OFPMatch(eth_type =ether.ETH_TYPE_IP,ip_proto=inet.IPPROTO_ICMP,in_port= in_port)
        self.add_flow(datapath, priority, match, actions)


    #对所有流的初始化转发规则:h1-s1-s2-s4-h2/h2-s4-s2-s1-h1
    def add_default_rules(self, datapath, in_port, out_port):
        priority = 1
        parser = datapath.ofproto_parser
        actions = [parser.OFPActionOutput(out_port)]
        match = parser.OFPMatch(in_port = in_port)
        self.add_flow(datapath, priority, match, actions)



    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    def _pakcet_in_handler(self, ev):
        print("******getting a packet in message******")
        #如果发生了table-miss,就把对应的default forwarding安装上
        #如果miss的是icmp,就把对应的icmp forwarding安装上
        #先不管
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        dpid = datapath.id
        print("******setting flows for s" + str(dpid) + "******")
        self.settings_for_switches(datapath, dpid)


    def settings_for_switches(self, datapath, dpid):
        if dpid == 1:#s1
            #eth1进来的,默认发给eth
            self.add_default_rules(datapath, 1, 2)
            #eth2和eth3进来的,发给eth1
            self.add_default_rules(datapath, 2, 1)
            self.add_default_rules(datapath, 3, 1)
            #eth1进来的icmp,发给eth3
            self.add_icmp_rules(datapath, 1, 3)
        elif dpid == 2:#s2
            #eth1进来的,默认发给eth2
            self.add_default_rules(datapath, 1, 2)
            #eth2进来的,默认发给eth1
            self.add_default_rules(datapath, 2, 1)
        elif dpid == 3:#s3
            #eth1发来的,默认发给eth2
            self.add_default_rules(datapath, 1, 2)
            #eth2发来的,默认发给eth1
            self.add_default_rules(datapath, 2, 1)
        elif dpid == 4:#s4
            #eth1和eth2发来的,默认发给eth3
            self.add_default_rules(datapath, 1, 3)
            self.add_default_rules(datapath, 2, 3)
            #eth3发来的,默认发给eth1
            self.add_default_rules(datapath, 3, 1)
            #eth3发来的icmp, 发给eth2
            self.add_icmp_rules(datapath, 3, 2)

最后

以上就是无聊冷风为你收集整理的OVS实验笔记(二):Mininet上使用controller代替手动设置flow table.的全部内容,希望文章能够帮你解决OVS实验笔记(二):Mininet上使用controller代替手动设置flow table.所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(29)

评论列表共有 0 条评论

立即
投稿
返回
顶部