如何实现软件定义的自学习交换机?
经过控制器来实现自学习交换算法,而后指导数据平面实现交换机操做
控制器选用Ryu,数据平面经过Mininet模拟
在控制器上编程开发交换机应用,建立实验网络为验证方案作准备
运行程序,调试程序,验证程序
验证成功后,优化程序
交换机MAC地址表记录了统一网段中的各个主机对应交换机的端口和主机的MAC地址
当主机A要和主机B通讯时,初始交换机MAC表是空的,会先记录主机A的MAC地址和对应的交换机端口,而后查找交换机MAC中是否有目标MAC地址,没有找到,会向其余全部端口泛洪查找
泛洪,通知其余主机。主机C接收到数据包,发现不是本身的,则不处理,丢弃数据包。当主机B接收后,发现是找本身的,则能够进行消息通讯。交换机先进行MAC学习,记录主机B的MAC信息,再进行查表转发,单播发送给主机A
SDN中交换机不存储MAC表,(datapath)只存在流表。其地址学习操做由控制器(控制器中包含MAC 地址表)实现,以后控制器下发流表项给交换机
from ryu.base import app_manager from ryu.ofproto import ofproto_v1_3 from ryu.controller import ofp_event from ryu.controller.handler import set_ev_cls from ryu.controller.handler import CONFIG_DISPATCHER,MAIN_DISPATCHER from ryu.lib.packet import packet from ryu.lib.packet import ethernet class SelfLearnSwitch(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] #set openflow protocol version while we support def __init__(self,*args,**kwargs): super(SelfLearnSwitch,self).__init__(*args,**kwargs) #set a data construction to save MAC Address Table self.Mac_Port_Table={} @set_ev_cls(ofp_event.EventOFPSwitchFeatures) def switch_features_handler(self,ev): ''' manage the initial link, from switch to controller ''' #first parse event to get datapath and openflow protocol msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto ofp_parser = datapath.ofproto_parser self.logger.info("datapath: %s link to controller",datapath.id) #secondly set match and action match = ofp_parser.OFPMatch() #all data message match successful actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,ofproto.OFPCML_NO_BUFFER)] #set receive port and buffer for switch #add flow and send it to switch in add_flow self.add_flow(datapath,0,match,actions,"default flow entry") def add_flow(self,datapath,priority,match,actions,extra_info): """ add flow entry to switch """ #get open flow protocol infomation ofproto = datapath.ofproto ofp_parser = datapath.ofproto_parser #set instruction infomation from openflow protocol 1.3 inst = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions)] #set flow entry mod mod = ofp_parser.OFPFlowMod(datapath=datapath,priority=priority,match=match,instructions=inst) print("send "+extra_info) #send flow entry to switch datapath.send_msg(mod) @set_ev_cls(ofp_event.EventOFPPacketIn,MAIN_DISPATCHER) def packet_in_handler(self,ev): ''' manage infomation from switch ''' #first parser openflow protocol msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto ofp_parser = datapath.ofproto_parser #get datapath id from datapath, and save dpid into MAC table (default) dpid = datapath.id self.Mac_Port_Table.setdefault(dpid, {}) #analysize packet, get ethernet data, get host MAC info pkt = packet.Packet(msg.data) eth_pkt = pkt.get_protocol(ethernet.ethernet) dst = eth_pkt.dst src = eth_pkt.src #get switch port where host packet send in in_port = msg.match['in_port'] self.logger.info("Controller %s get packet, Mac address from: %s send to: %s , send from datapath: %s,in port is: %s" ,dpid,src,dst,dpid,in_port) #save src data into dictionary---MAC address table self.Mac_Port_Table[dpid][src] = in_port #query MAC address table to get destinction host`s port from current datapath #---first: find port to send packet #---second: not find port,so send packet by flood if dst in self.Mac_Port_Table[dpid]: Out_Port = self.Mac_Port_Table[dpid][dst] else: Out_Port = ofproto.OFPP_FLOOD #set match-action from above status actions = [ofp_parser.OFPActionOutput(Out_Port)] #add a new flow entry to switch by add_flow if Out_Port != ofproto.OFPP_FLOOD: #if Out_port == ofproto.OFPP_FLOOD ---> flow entry == default flow entry, it already exist match = ofp_parser.OFPMatch(in_port=in_port,eth_dst = dst) self.add_flow(datapath, 1, match, actions,"a new flow entry by specify port") self.logger.info("send packet to switch port: %s",Out_Port) #finally send the packet to datapath, to achive self_learn_switch Out = ofp_parser.OFPPacketOut(datapath=datapath,buffer_id=msg.buffer_id, in_port=in_port,actions=actions,data=msg.data) datapath.send_msg(Out)
from ryu.base import app_manager from ryu.ofproto import ofproto_v1_3 from ryu.controller import ofp_event from ryu.controller.handler import set_ev_cls from ryu.controller.handler import CONFIG_DISPATCHER,MAIN_DISPATCHER from ryu.lib.packet import packet from ryu.lib.packet import ethernet class SelfLearnSwitch(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] #set openflow protocol version while we support def __init__(self,*args,**kwargs): super(SelfLearnSwitch,self).__init__(*args,**kwargs) #set a data construction to save MAC Address Table self.Mac_Port_Table={} @set_ev_cls(ofp_event.EventOFPSwitchFeatures) def switch_features_handler(self,ev): ''' manage the initial link, from switch to controller ''' #first parse event to get datapath and openflow protocol msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto ofp_parser = datapath.ofproto_parser self.logger.info("datapath: %s link to controller",datapath.id) #secondly set match and action match = ofp_parser.OFPMatch() #all data message match successful actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,ofproto.OFPCML_NO_BUFFER)] #set receive port and buffer for switch #add flow and send it to switch in add_flow self.add_flow(datapath,0,match,actions,"default flow entry") def add_flow(self,datapath,priority,match,actions,extra_info): """ add flow entry to switch """ #get open flow protocol infomation ofproto = datapath.ofproto ofp_parser = datapath.ofproto_parser #set instruction infomation from openflow protocol 1.3 inst = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions)] #set flow entry mod mod = ofp_parser.OFPFlowMod(datapath=datapath,priority=priority,match=match,instructions=inst) print("send "+extra_info) #send flow entry to switch datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn,MAIN_DISPATCHER) def packet_in_handler(self,ev): ''' manage infomation from switch ''' #first parser openflow protocol 先解析OpenFlow协议信息 msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto ofp_parser = datapath.ofproto_parser #get datapath id from datapath, and save dpid into MAC table (default) 获取datapath(虚拟交换机的id),用dpid初始化一个键值 dpid = datapath.id self.Mac_Port_Table.setdefault(dpid, {}) #analysize packet, get ethernet data, get host MAC info 分析packert数据包,由于转发的包,都是基于以太网协议的,因此咱们须要用到以太网协议进行解析,获取源MAC和目的MAC pkt = packet.Packet(msg.data) eth_pkt = pkt.get_protocol(ethernet.ethernet) dst = eth_pkt.dst src = eth_pkt.src #get switch port where host packet send in 获取datapath的数据输入端口 in_port = msg.match['in_port'] self.logger.info("Controller %s get packet, Mac address from: %s send to: %s , send from datapath: %s,in port is: %s" ,dpid,src,dst,dpid,in_port) #打印调试信息 #save src data into dictionary---MAC address table 将源MAC地址保存,学习,放入MAC表中 self.Mac_Port_Table[dpid][src] = in_port #query MAC address table to get destinction host`s port from current datapath 查询MAC表,是否有目标MAC地址的键值 #---first: find port to send packet 若是找到,咱们则按照该端口发送 #---second: not find port,so send packet by flood 若是没有找到,咱们须要泛洪发送给下一个(或者下几个)交换机,依次查询 if dst in self.Mac_Port_Table[dpid]: Out_Port = self.Mac_Port_Table[dpid][dst] else: Out_Port = ofproto.OFPP_FLOOD #set match-action from above status 开始设置match-actions匹配动做 actions = [ofp_parser.OFPActionOutput(Out_Port)] #add a new flow entry to switch by add_flow 进行对应的流表项下发 《重点》 if Out_Port != ofproto.OFPP_FLOOD: match = ofp_parser.OFPMatch(in_port=in_port,eth_dst = dst) self.add_flow(datapath, 1, match, actions,"a new flow entry by specify port") self.logger.info("send packet to switch port: %s",Out_Port) #finally send the packet to datapath, to achive self_learn_switch 最后咱们将以前交换机发送上来的数据,从新发给交换机 Out = ofp_parser.OFPPacketOut(datapath=datapath,buffer_id=msg.buffer_id, in_port=in_port,actions=actions,data=msg.data) #咱们必须加上这个data,才能够将packet数据包发送回去《重点》否则会出错×××××× datapath.send_msg(Out)
网络可达,说明实现自学习交换机
class Packet(StringifyMixin): """A packet decoder/encoder class. An instance is used to either decode or encode a single packet. *data* is a bytearray to describe a raw datagram to decode. data是一个未加工的报文数据, 即msg.data直接从事件的msg中获取的数据 When decoding, a Packet object is iteratable. Iterated values are protocol (ethernet, ipv4, ...) headers and the payload. Protocol headers are instances of subclass of packet_base.PacketBase. The payload is a bytearray. They are iterated in on-wire order. *data* should be omitted when encoding a packet. """ # Ignore data field when outputting json representation. _base_attributes = ['data'] def __init__(self, data=None, protocols=None, parse_cls=ethernet.ethernet): 协议解析,默认是按照以太网协议 super(Packet, self).__init__() self.data = data if protocols is None: self.protocols = [] else: self.protocols = protocols if self.data: self._parser(parse_cls)
class Packet(StringifyMixin): def add_protocol(self, proto): """Register a protocol *proto* for this packet. This method is legal only when encoding a packet. When encoding a packet, register a protocol (ethernet, ipv4, ...) header to add to this packet. Protocol headers should be registered in on-wire order before calling self.serialize. """ self.protocols.append(proto) def get_protocols(self, protocol): """Returns a list of protocols that matches to the specified protocol. """ if isinstance(protocol, packet_base.PacketBase): protocol = protocol.__class__ assert issubclass(protocol, packet_base.PacketBase) return [p for p in self.protocols if isinstance(p, protocol)]
class ethernet(packet_base.PacketBase): """Ethernet header encoder/decoder class. An instance has the following attributes at least. MAC addresses are represented as a string like '08:60:6e:7f:74:e7'. __init__ takes the corresponding args in this order. ============== ==================== ===================== Attribute Description Example ============== ==================== ===================== dst destination address 'ff:ff:ff:ff:ff:ff' src source address '08:60:6e:7f:74:e7' ethertype ether type 0x0800 ============== ==================== ===================== """ _PACK_STR = '!6s6sH' _MIN_LEN = struct.calcsize(_PACK_STR) _MIN_PAYLOAD_LEN = 46 _TYPE = { 'ascii': [ 'src', 'dst' ] } def __init__(self, dst='ff:ff:ff:ff:ff:ff', src='00:00:00:00:00:00', ethertype=ether.ETH_TYPE_IP): super(ethernet, self).__init__() self.dst = dst self.src = src self.ethertype = ethertype @classmethod def parser(cls, buf): dst, src, ethertype = struct.unpack_from(cls._PACK_STR, buf) return (cls(addrconv.mac.bin_to_text(dst), addrconv.mac.bin_to_text(src), ethertype), ethernet.get_packet_type(ethertype), buf[ethernet._MIN_LEN:]) def serialize(self, payload, prev): # Append padding if the payload is less than 46 bytes long pad_len = self._MIN_PAYLOAD_LEN - len(payload) if pad_len > 0: payload.extend(b'\x00' * pad_len) return struct.pack(ethernet._PACK_STR, addrconv.mac.text_to_bin(self.dst), addrconv.mac.text_to_bin(self.src), self.ethertype) @classmethod def get_packet_type(cls, type_): """Override method for the ethernet IEEE802.3 Length/Type field (self.ethertype). If the value of Length/Type field is less than or equal to 1500 decimal(05DC hexadecimal), it means Length interpretation and be passed to the LLC sublayer.""" if type_ <= ether.ETH_TYPE_IEEE802_3: type_ = ether.ETH_TYPE_IEEE802_3 return cls._TYPES.get(type_)