def processevent(self, event): """Process OpenFlow message for config reply @param event OpenFlow message event to process @return True """ if isinstance(event, ofevents.features_reply): # Get config getconfig = pyof.ofp_header() getconfig.type = pyof.OFPT_GET_CONFIG_REQUEST self.conn.send(event.sock, getconfig.pack()) elif isinstance(event, ofevents.config_reply): # Check if I should set config desired_flags = (event.config.flags | self.default_on_config_flags) & ~self.default_off_config_flags desired_miss_send_len = event.config.miss_send_len if self.default_miss_send_len != None: desired_miss_send_len = self.default_miss_send_len if (event.config.flags != desired_flags) or (desired_miss_send_len != event.config.miss_send_len): # Change config to desired output.dbg( "Set config to desired with flag %x " % desired_flags + "and miss_send_len " + str(desired_miss_send_len), self.__class__.__name__, ) setconfig = pyof.ofp_switch_config() setconfig.header.type = pyof.OFPT_SET_CONFIG setconfig.flags = desired_flags setconfig.miss_send_len = desired_miss_send_len self.conn.send(event.sock, setconfig.pack()) # Get config again after set getconfig = pyof.ofp_header() getconfig.type = pyof.OFPT_GET_CONFIG_REQUEST self.conn.send(event.sock, getconfig.pack()) else: # Remember config key = self.get_key(event.sock) mc.set(key, event.config) output.dbg("Updated config with key " + key, self.__class__.__name__) elif isinstance(event, comm.event): # Socket close, so remove config if event.event == comm.event.SOCK_CLOSE: key = self.get_key(event.sock) c = mc.get(key) if c != None: mc.delete(key) return True
def replyecho(self, msg): """Handle echo request """ sendmsg = pyopenflow.ofp_header() sendmsg.type = pyopenflow.OFPT_ECHO_REPLY sendmsg.xid = msg.header.xid self.send(sendmsg.pack())
def dohandshake(self, msg): """Function to carry out handshake Switch (hello) => hello + feature request Switch (feature reply) => DONE """ if (msg.header.type == pyopenflow.OFPT_HELLO): sendmsg = pyopenflow.ofp_hello() self.send(sendmsg.pack()) sendmsg = pyopenflow.ofp_header() sendmsg.type = pyopenflow.OFPT_FEATURES_REQUEST self.send(sendmsg.pack()) elif (msg.header.type == pyopenflow.OFPT_FEATURES_REPLY): switch_feature = pyopenflow.ofp_switch_features() switch_feature.unpack(msg.message) self.dpid = switch_feature.datapath_id output.info("Connected to switch %x" % self.dpid, self.__class__.__name__) self.handshake = True else: output.warn("Handshake should not handle message type "+\ pyopenflow.ofp_type[msg.header.type], self.__class__.__name__)
def __init__(self, sock, msg): """OpenFlow message event """ ##Header self.header = pyopenflow.ofp_header() self.header.unpack(msg) ##Connection message is received from self.sock = sock ##Message self.message = msg
def send(self, msg): """Send OpenFlow message """ header = pyopenflow.ofp_header() if (len(msg) < len(header)): output.warn("Cannot send OpenFlow of length "+str(len(msg))) else: remain = header.unpack(msg) header.length = len(msg) output.vdbg("Send message "+header.show().strip().replace("\n",";"), self.__class__.__name__) try: self.sock.send(header.pack()+remain) except socket.error: output.warn("Broken pipe, message not sent", self.__class__.__name__)
def __init__(self, sock, server): """Initialize """ comm.sockmanager.__init__(self, sock, server, 2048) ##Header object self.__header = pyopenflow.ofp_header()