def switch_handler(self, address, fd, events): if events & io_loop.READ: data = self.sock_sw.recv(1024) if data == '': print "switch disconnected" io_loop.remove_handler(self.fd_sw) print "closing connection to controller" self.sock_con.close() io_loop.remove_handler(self.fd_con) else: rmsg = of.ofp_header(data[0:8]) if rmsg.type == 6: print "OFPT_FEATURES_REPLY" #Actually,we just need to change here. header = of.ofp_header(data[0:8]) print "ofp_features_reply.xid ", header.xid msg = of.ofp_features_reply(data[8:32]) #all sw type should make the convertion. Because our protocol need to use in all nets. msg_port = data[32:] msg = header/msg/msg_port self.dpid=msg.datapath_id #record the dpid data = convert.of2ofc(msg, self.buffer, self.dpid) elif rmsg.type == 10: pkt_in_msg = of.ofp_packet_in(data[8:18]) pkt_parsed = of.Ether(data[18:]) self.counter+=1 #[port + id+ dpid] --> [buffer_id + pkt_in_msg] if isinstance(pkt_parsed.payload, of.IP) or isinstance(pkt_parsed.payload.payload, of.IP): self.buffer[(pkt_in_msg.in_port, self.counter, self.dpid)] = [pkt_in_msg.buffer_id, rmsg/pkt_in_msg/pkt_parsed] # bind buffer id with in port rmsg.xid = self.counter # use the counter to check the buffer data = rmsg/pkt_in_msg/pkt_parsed elif rmsg.type ==11: match = ofc.ofp_match(data[12:48]) #data[8:12]is wildcards for flow in self.flow_cache: if match == ofc.ofp_match(str(flow[1])[12:48]): self.flow_cache.remove(flow) #delete the flow elif rmsg.type == 17: print "stats_reply" ,len(data) body = data[8:] reply_header = of.ofp_stats_reply(body[:4]) if reply_header.type == 1 and len(data)>91: reply_body_match = ofc.ofp_match(body[12:48]) reply_body_data2 = ofc.ofp_flow_stats_data(body[48:92]) if reply_body_data2.byte_count == 0 and reply_body_data2.packet_count == 0: #it is a junck flow,delete it! for flow in self.flow_cache: if reply_body_match == ofc.ofp_match(str(flow[1])[12:48]): self.flow_cache.remove(flow) io_loop.update_handler(self.fd_con, io_loop.WRITE) self.queue_con.put(str(data)) if events & io_loop.WRITE: try: next_msg = self.queue_sw.get_nowait() except Queue.Empty: #print "%s queue empty" % str(address) io_loop.update_handler(self.fd_sw, io_loop.READ) else: #print 'sending "%s" to %s' % (of.ofp_type[of.ofp_header(next_msg).type], self.sock_sw.getpeername()) self.sock_sw.send(next_msg)
18 okay "OFPT_BARRIER_REQUEST", 19 okay "OFPT_BARRIER_REPLY", 20 okay "OFPT_QUEUE_GET_CONFIG_REQUEST", 21 okay "OFPT_QUEUE_GET_CONFIG_REPLY" 24 oaky "OFPT_CFEATURES_REPLY" 0xfe oaky "OFPT_CPORT_STATUS" 0xff okay "OFPT_CFLOW_MO """ if __name__ == '__main__': import convert import libopenflow as of #a = ofp_cfeatures_reply() #b = ofp_phy_cport() #c = ofp_phy_cport() #d=ofp_header(type =24,length = 32+78*2)/ofp_cfeatures_reply(n_cports=2)/ofp_phy_cport()/sup_wave_port_bandwidth(num_lmda=4)/ofp_phy_cport()/sup_wave_port_bandwidth(num_lmda=5) #d.show() #a.n_cports=2 #a.OFPC_TABLE_STATS=100 #a.OFPST_T_OTN = 1 #a.SUPP_SW_GRAN = 129 #print a.SUPP_SW_GRAN buffer = {} dpid = 8 e = of.ofp_header(type =6)/of.ofp_features_reply(datapath_id=1)/of.ofp_phy_port(port_no=1)/of.ofp_phy_port(port_no=2)\ /of.ofp_phy_port(port_no=3)/of.ofp_phy_port(port_no=4)/of.ofp_phy_port(port_no=5) f = convert.of2ofc(e, buffer, dpid) f.show() p = str(f)
def switch_handler(self, address, fd, events): if events & io_loop.READ: data = self.sock_sw.recv(1024) if data == '': print "switch disconnected" io_loop.remove_handler(self.fd_sw) print "closing connection to controller" self.sock_con.close() io_loop.remove_handler(self.fd_con) else: rmsg = of.ofp_header(data[0:8]) if rmsg.type == 6: print "OFPT_FEATURES_REPLY" #Actually,we just need to change here. header = of.ofp_header(data[0:8]) print "ofp_features_reply.xid ", header.xid msg = of.ofp_features_reply( data[8:32] ) #all sw type should make the convertion. Because our protocol need to use in all nets. msg_port = data[32:] msg = header / msg / msg_port self.dpid = msg.datapath_id #record the dpid data = convert.of2ofc(msg, self.buffer, self.dpid) elif rmsg.type == 10: pkt_in_msg = of.ofp_packet_in(data[8:18]) pkt_parsed = of.Ether(data[18:]) self.counter += 1 #[port + id+ dpid] --> [buffer_id + pkt_in_msg] if isinstance(pkt_parsed.payload, of.IP) or isinstance( pkt_parsed.payload.payload, of.IP): self.buffer[(pkt_in_msg.in_port, self.counter, self.dpid)] = [ pkt_in_msg.buffer_id, rmsg / pkt_in_msg / pkt_parsed ] # bind buffer id with in port rmsg.xid = self.counter # use the counter to check the buffer data = rmsg / pkt_in_msg / pkt_parsed elif rmsg.type == 11: match = ofc.ofp_match(data[12:48]) #data[8:12]is wildcards for flow in self.flow_cache: if match == ofc.ofp_match(str(flow[1])[12:48]): self.flow_cache.remove(flow) #delete the flow elif rmsg.type == 17: print "stats_reply", len(data) body = data[8:] reply_header = of.ofp_stats_reply(body[:4]) if reply_header.type == 1 and len(data) > 91: reply_body_match = ofc.ofp_match(body[12:48]) reply_body_data2 = ofc.ofp_flow_stats_data(body[48:92]) if reply_body_data2.byte_count == 0 and reply_body_data2.packet_count == 0: #it is a junck flow,delete it! for flow in self.flow_cache: if reply_body_match == ofc.ofp_match( str(flow[1])[12:48]): self.flow_cache.remove(flow) io_loop.update_handler(self.fd_con, io_loop.WRITE) self.queue_con.put(str(data)) if events & io_loop.WRITE: try: next_msg = self.queue_sw.get_nowait() except Queue.Empty: #print "%s queue empty" % str(address) io_loop.update_handler(self.fd_sw, io_loop.READ) else: #print 'sending "%s" to %s' % (of.ofp_type[of.ofp_header(next_msg).type], self.sock_sw.getpeername()) self.sock_sw.send(next_msg)