SDN初步:Mininet(SDN测试平台)&Ryu(SDN控制器) |
您所在的位置:网站首页 › 网络控制器怎么用的 › SDN初步:Mininet(SDN测试平台)&Ryu(SDN控制器) |
Mininet:SDN测试平台&Ryu:SDN控制器
Mininet:SDN测试平台&Ryu:SDN控制器(均是在64位Ubuntu 16.04.7虚拟机中安装)64位Ubuntu 16.04.7光盘映像文件Mininet的安装Ryu的安装Mininet的使用Ryu的使用
Mininet:SDN测试平台&Ryu:SDN控制器(均是在64位Ubuntu 16.04.7虚拟机中安装)
64位Ubuntu 16.04.7光盘映像文件
链接:https://pan.baidu.com/s/1epMw5oBSBWZW1IeBi0VQWw 提取码:1234 Mininet的安装Step1(如果没有git需要安装,如果有则忽略): sudo apt install git 在获取Mininet源代码时,需要替换国内镜像,首先点击设置,接着如下操作,替换后请务必重启虚拟机 Step2(获取Mininet源代码): git clone http://github.com/mininet/mininet.git Step3(安装Mininet,保守起见,这选择完整安装,标签-a,该过程时间较久): mininet/util/install.sh -a Step4(测试安装是否成功): sudo mn --test pingall Step5(查看版本): mn --version Step1(检查python的版本): 需要使用python版本3.6以上,建议使用ubuntu20.04 LTS的虚拟机安装,自带的python版本就是可以安装ryu的 Step2(获取Ryu源代码): git clone https://github.com/osrg/ryu.git 这里只介绍一种比较简单的方法(鄙人也只会这种),就是通过miniedit这个可视化工具,能够快速对mininet上手 首先将目录切到miniedit.py下: cd mininet/mininet/examples 运行miniedit.py文件,记得要使用超级用户权限: sudo ./miniedit/py 接着就会出现以下界面: Ryu作为SDN的Controller,即数据控制平面,我们可以在其下编写多样的应用程序,比如像一个自学习交换机的实现,我们可以基于ryu编写代码,能够表现出与传统网络设施相同的功能 我们编写的代码需要放置在app文件夹下,首先切换目录: cd ryu/ryu/app ryu很好的一点,是基于python编写应用代码,例如如下的自学习交换机的代码实现: 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 from ryu.lib.packet import packet from ryu.lib.packet import ethernet class Learning_switch(app_manager.RyuApp): # Example Switch OFP_VERSIONS=[ofproto_v1_3.OFP_VERSION] def __init__(self, *args, **kwargs): super(Learning_switch, self).__init__(*args, **kwargs) self.mac_to_port={} @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 # get datapath id to identify openflow switch dpid=datapath.id #dpid=datapath id self.mac_to_port.setdefault(dpid,{}) # save info # parser and analyse the received packets pkt=packet.Packet(msg.data) eth_pkt=pkt.get_protocol(ethernet.ethernet) dst=eth_pkt.dst src=eth_pkt.src in_port=msg.match['in_port'] self.logger.info("packet in %s %s %s %s",dpid,src,dst,in_port) # learn the relationship between source mac address and ports to avoid Flood next time self.mac_to_port[dpid][src]=in_port # if the dst mac address exists, decide which port to send the packet # otherwise Flood (Don't know which port lead to the dest port) if dst in self.mac_to_port[dpid]: out_port=self.mac_to_port[dpid][dst] else: out_port=ofproto.OFPP_FLOOD # build actions actions=[ofp_parser.OFPActionOutput(out_port)] # install a new flow rule if out_port != ofproto.OFPP_FLOOD: match=ofp_parser.OFPMatch(in_port=in_port,eth_dst=dst) self.add_flow(datapath, 1, match, actions) # send a packet-out out=ofp_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions) datapath.send_msg(out)而运行的方法也很简单,如下命令行(一定要切到ryu/ryu/app下): ryu-manager learning_switch.py --verbose !!!编写的应用代码一定要放置在ryu/ryu/app文件夹下 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |