1、 添加mac学习功能
2、给交换机下发流表
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): 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)
[root@kunpeng82 app]# ovs-ofctl dump-flows s1 cookie=0x0, duration=10.967s, table=0, n_packets=4, n_bytes=280, in_port="s1-eth1",dl_src=da:54:a5:07:16:8d,dl_dst=2e:25:f2:98:49:13 actions=output:"s1-eth2" cookie=0x0, duration=9.967s, table=0, n_packets=4, n_bytes=280, in_port="s1-eth2",dl_src=2e:25:f2:98:49:13,dl_dst=da:54:a5:07:16:8d actions=output:"s1-eth1" [root@kunpeng82 app]#
删除所有流表
[root@kunpeng82 app]# ovs-ofctl dump-flows s1 cookie=0x0, duration=401.384s, table=0, n_packets=19, n_bytes=1638, in_port="s1-eth1",dl_src=da:54:a5:07:16:8d,dl_dst=2e:25:f2:98:49:13 actions=output:"s1-eth2" cookie=0x0, duration=400.384s, table=0, n_packets=19, n_bytes=1638, in_port="s1-eth2",dl_src=2e:25:f2:98:49:13,dl_dst=da:54:a5:07:16:8d actions=output:"s1-eth1" [root@kunpeng82 app]# ovs-ofctl del-flows s1 in_port="s1-eth1" [root@kunpeng82 app]# ovs-ofctl del-flows s1 in_port="s1-eth2" [root@kunpeng82 app]# ovs-ofctl dump-flows s1 [root@kunpeng82 app]#
删除之后,h2 ping h1 ,mn会收到EventOFPPacketIn
EVENT ofp_event->SimpleSwitch EventOFPPacketIn packet in 1 2e:25:f2:98:49:13 da:54:a5:07:16:8d 2 EVENT ofp_event->SimpleSwitch EventOFPPacketIn packet in 1 da:54:a5:07:16:8d 2e:25:f2:98:49:13 1