def _load_ini(self): try: f = open(self.ini_filename) except IOError: dbg("no ini file: %s" % (self.ini_filename)) return {} info = {} current_file = None for line in f.readlines(): if len(line.strip()) == 0: continue # blank line dbg("loading line: %s" % (line,)) match = self.newfile.match(line) if match: current_file = match.group(1) if current_file not in info: info[current_file] = {} else: if '=' in line: attr, val = [s.strip() for s in line.split('=', 1)] if current_file is None: print "no current file to apply attr: %s" % (line,) else: info[current_file][attr] = val else: print "doesn't look like an attr to me: %s" % (line, ) f.close() return info
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 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_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_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 __getitem__(self, item): item_dirname = os.path.dirname(os.path.abspath(item)) if not item_dirname == self.dirname: raise ValueError("ini file for %s directory does not have information for files in %s" % (self.dirname, item_dirname)) item = os.path.basename(item) # dbg("ini getitem %s, all items = %s" % (item, self.ini_info)) if not item in self.ini_info: dbg("adding file: %s" % (item,)) self.ini_info[item] = {} return self.ini_info[item]
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 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) else: output.dbg("Unprocessed message "+self.parser.header_describe(dic), 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) 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)
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")