from mininet.topo import Topo from mininet.net import Mininet from mininet.node import RemoteController from mininet.link import TCLink from mininet.util import dumpNodeConnections class MyTopo(Topo): def __init__(self): super(MyTopo,self).__init__() # add host Host1 = self.addHost('h1') Host2 = self.addHost('h2') Host3 = self.addHost('h3') switch1 = self.addSwitch('s1') switch2 = self.addSwitch('s2') self.addLink(Host1,switch1) self.addLink(Host2,switch1) self.addLink(Host3,switch2) self.addLink(switch1,switch2) topos = {"mytopo":(lambda:MyTopo())}
ryu-manager simple_switch.py 注意,该控制器py文件在app目录下
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # implied. # See the License for the specific language governing permissions and # limitations under the License. """ An OpenFlow 1.0 L2 learning switch implementation. """ 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): 不一样与以前的Ryu实验,这里面没有在交换机初始链接时下发默认流表...待思考 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) 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 dst = eth.dst src = eth.src dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) self.logger.info("packet in %s %s %s %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 actions = [datapath.ofproto_parser.OFPActionOutput(out_port)] # install a flow to avoid packet_in next time if out_port != ofproto.OFPP_FLOOD: self.add_flow(datapath, msg.in_port, dst, src, actions) data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data 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) @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER) def _port_status_handler(self, ev): msg = ev.msg reason = msg.reason port_no = msg.desc.port_no ofproto = msg.datapath.ofproto if reason == ofproto.OFPPR_ADD: self.logger.info("port added %s", port_no) elif reason == ofproto.OFPPR_DELETE: self.logger.info("port deleted %s", port_no) elif reason == ofproto.OFPPR_MODIFY: self.logger.info("port modified %s", port_no) else: self.logger.info("Illeagal port state %s %s", port_no, reason)
sudo mn --custom tree_topt.py --topo=mytopo --controller=remote,ip=127.0.0.1,port=6633
dpctl dump-flows 查看静态流表
dpctl del-flows 删除全部交换机中的流表
dpctl add-flow in_port=1,actions=output:2 添加流表项到全部交换机,注意:通常是成对添加,实现双方通讯
sh ovs-ofctl del-flows s1 in_port=2 删除指定交换机的,匹配in_port=2的流表
dpctl del-flows in_port=1 删除全部交换机中符合in_port=1的流表
dpctl add-flow in_port=2,actions=drop 添加丢弃数据包的流表项
从新启动Ryu和Mininet,直接查看交换机中是否有流表.
已解决。可是交换机是如何设置默认流表当不知道packet如何处理的时候发生给控制器?若是这是默认动做,那么咱们以前Ryu实验中为什么要实现 @set_ev_cls(ofp_event.EventOFPSwitchFeatures,CONFIG_DISPATCHER) def switch_features_handler(self,ev): ?????
通过启动hub.py在控制器上,进行测试,发现会进入switch_features_handler,而且会下发默认流表---因此说,咱们能够不用设置这个默认流表也能够,可是这个函数中,咱们能够设置一些其余的流表进行控制---因此说仍是比较有用的
因为没有流表,全部ping操做不可达
为全部交换机添加端口1和端口2的操做---两个交换机公共操做html
dpctl add-flow in_port=1,actions=output:2
dpctl add-flow in_port=2,actions=output:1
为交换机之间端口提供交互---只操做s1(由于只有s1有端口3)node
sh ovs-ofctl add-flow s1 in_port=1,actions=output:2,3
sh ovs-ofctl add-flow s1 in_port=3,actions=output:1,2
sh ovs-ofctl add-flow s1 in_port=2,actions=output:1,3
实验结果显示express
mininet> dpctl add-flow in_port=1,actions=output:2,3 mininet> dpctl add-flow in_port=2,actions=output:1,3 mininet> dpctl add-flow in_port=3,actions=output:1,2
mininet> sh ovs-ofctl del-flows s2 in_port=1 删除原有流表 mininet> sh ovs-ofctl add-flow s2 in_port=1,actions=drop 添加丢弃流表