def reply_echo(self, xid): """Reply to echo request """ self.connection.structsend_xid( "ofp_header", 0, self.__messages.get_value("OFPT_ECHO_REPLY"), 0, xid) output.dbg("Reply echo of xid:" + str(xid), self.name)
def send_packet(self, inport, bufferid=None, packet="", xid=0, reason=None): """Send packet in Assume no match as reason, bufferid = 0xFFFFFFFF, and empty packet by default """ if (reason == None): reason = self.__messages.get_value("OFPR_NO_MATCH") if (bufferid == None): bufferid = int("0xFFFFFFFF", 16) pktin = self.__messages.pack( "ofp_packet_in", 0, self.__messages.get_value("OFPT_PACKET_IN"), 0, xid, #header bufferid, len(packet), inport, reason, 0) self.connection.structsend_raw(pktin + packet) output.dbg("Send packet ", self.name)
def send_echo(self, xid=0): """Send echo """ self.connection.structsend_xid( "ofp_header", 0, self.__messages.get_value("OFPT_ECHO_REQUEST"), 0, xid) output.dbg("Send echo", self.name)
def send_echo(self, xid=0): """Send echo """ self.connection.structsend_xid("ofp_header", 0, self.__messages.get_value("OFPT_ECHO_REQUEST"), 0, xid) output.dbg("Send echo", self.name)
def send_hello(self): """Send hello """ self.connection.structsend("ofp_hello", 0, self.__messages.get_value("OFPT_HELLO"), 0, 0) output.dbg("Send hello", self.name)
def reply_echo(self, xid): """Reply to echo request """ self.connection.structsend_xid("ofp_header", 0, self.__messages.get_value("OFPT_ECHO_REPLY"), 0, xid) output.dbg("Reply echo of xid:"+str(xid),self.name)
def send_hello(self): """Send hello """ self.connection.structsend("ofp_hello", 0, self.__messages.get_value("OFPT_HELLO"), 0, 0) output.dbg("Send hello",self.name)
def receive_openflow(self, packet): """Switch receive OpenFlow packet, and respond accordingly """ dic = self.__messages.peek_from_front("ofp_header", packet) if (dic["type"][0] == self.__messages.get_value("OFPT_HELLO")): output.dbg("Receive hello", self.name) elif (dic["type"][0] == self.__messages.get_value("OFPT_ECHO_REQUEST") ): self.reply_echo(dic["xid"][0]) elif (dic["type"][0] == self.__messages.get_value( "OFPT_FEATURES_REQUEST")): self.reply_features(dic["xid"][0]) elif (dic["type"][0] == self.__messages.get_value("OFPT_FLOW_MOD")): self.handle_flow_mod(packet) # James: elif (dic["type"][0] == self.__messages.get_value("OFPT_STATS_REQUEST") ): self.reply_stats(dic["xid"][0]) elif (dic["type"][0] == self.__messages.get_value( "OFPT_GET_CONFIG_REQUEST")): self.reply_get_config(dic["xid"][0]) else: output.dbg( "Unprocessed message " + self.parser.header_describe(dic), self.name)
def reply_get_config(self, xid): """Reply to get config request """ self.connection.structsend_xid( "ofp_switch_config", 0, self.__messages.get_value("OFPT_GET_CONFIG_REPLY"), 0, xid, 0, 0xFFFF) output.dbg("Reply get config of xid:" + str(xid), self.name)
def reply_get_config(self, xid): """Reply to get config request """ self.connection.structsend_xid("ofp_switch_config", 0, self.__messages.get_value("OFPT_GET_CONFIG_REPLY"), 0, xid, 0, 0xFFFF) output.dbg("Reply get config of xid:"+str(xid),self.name)
def reply_stats(self, xid): """Reply to stats request """ pkt = self.__messages.pack( "ofp_stats_reply", 0, self.__messages.get_value("OFPT_STATS_REPLY"), 0, xid, 0, 0) payload = "\0" * 1056 self.connection.structsend_raw(pkt + payload) output.dbg("Reply stats of xid:" + str(xid), self.name)
def reply_features(self, xid): """Reply to feature request """ self.connection.structsend_xid( "ofp_switch_features", 0, self.__messages.get_value("OFPT_FEATURES_REPLY"), 0, xid, self.datapath_id, self.n_buffers, self.n_tables, 0, 0, 0, self.capability.get_capability(self.__messages), self.capability.get_actions(self.__messages)) output.dbg("Replied features request of xid " + str(xid), self.name)
def reply_stats(self, xid): """Reply to stats request """ pkt = self.__messages.pack("ofp_stats_reply", 0, self.__messages.get_value("OFPT_STATS_REPLY"), 0, xid, 0, 0 ) payload = "\0" * 1056 self.connection.structsend_raw(pkt+payload) output.dbg("Reply stats of xid:"+str(xid),self.name)
def reply_features(self, xid): """Reply to feature request """ self.connection.structsend_xid("ofp_switch_features", 0, self.__messages.get_value("OFPT_FEATURES_REPLY"), 0, xid, self.datapath_id, self.n_buffers, self.n_tables,0,0,0, self.capability.get_capability(self.__messages), self.capability.get_actions(self.__messages)) output.dbg("Replied features request of xid "+str(xid), self.name)
def send_packet(self, inport, bufferid=None, packet="", xid=0, reason=None): """Send packet in Assume no match as reason, bufferid = 0xFFFFFFFF, and empty packet by default """ if (reason == None): reason = self.__messages.get_value("OFPR_NO_MATCH") if (bufferid == None): bufferid = int("0xFFFFFFFF",16) pktin = self.__messages.pack("ofp_packet_in", 0, self.__messages.get_value("OFPT_PACKET_IN"), 0, xid, #header bufferid, len(packet), inport, reason, 0) self.connection.structsend_raw(pktin+packet) output.dbg("Send packet ",self.name)
def receive_openflow(self, packet): """Switch receive OpenFlow packet, and respond accordingly """ dic = self.__messages.peek_from_front("ofp_header", packet) if (dic["type"][0] == self.__messages.get_value("OFPT_HELLO")): output.dbg("Receive hello", self.name) elif (dic["type"][0] == self.__messages.get_value("OFPT_ECHO_REQUEST")): self.reply_echo(dic["xid"][0]) elif (dic["type"][0] == self.__messages.get_value("OFPT_FEATURES_REQUEST")): self.reply_features(dic["xid"][0]) elif (dic["type"][0] == self.__messages.get_value("OFPT_FLOW_MOD")): self.handle_flow_mod(packet) # James: elif (dic["type"][0] == self.__messages.get_value("OFPT_STATS_REQUEST")): self.reply_stats(dic["xid"][0]) elif (dic["type"][0] == self.__messages.get_value("OFPT_GET_CONFIG_REQUEST")): self.reply_get_config(dic["xid"][0]) else: output.dbg("Unprocessed message "+self.parser.header_describe(dic), self.name)
while running: ctime = time.time() time.sleep(max(0,min(ntime-ctime,interval/10.0))) if ((ctime-starttime) <= duration): #Send packet if time's up if (ctime >= ntime): ntime += interval pkt.src = struct.pack("Q",pcount)[:6] ofnet.switches[swindex].send_packet(1,10,pkt.pack()+'A'*3) pcount += 1 swno += 1 if (swno >= len(ofnet.switches)): swno=0 #Process any received message (ofsw, msg) = ofnet.connections.msgreceive() while (msg != None): dic = ofmsg.peek_from_front("ofp_header", msg) if (dic["type"][0] == ofmsg.get_value("OFPT_FLOW_MOD")): output.dbg("Received flow mod") rcount += 1 ofsw.receive_openflow(msg) (ofsw, msg) = ofnet.connections.msgreceive() else: running = False output.info("Sent "+str(pcount)+" packets at rate "+\ str(float(pcount)/float(duration))+" and received "+\ str(rcount)+" back")
def handle_flow_mod(self, packet): """Handle flow mod: just print it here """ output.dbg(self.parser.flow_mod_describe(packet), self.name)