def test_check_msg_parser(self): (version, msg_type, msg_len, xid) = ofproto_parser.header(self.bufPacketIn) version = 0xff ofproto_parser.msg(self, version, msg_type, msg_len, xid, self.bufPacketIn)
def SBP_handler(self, ev): # parser the msg and handle the SBP message. # raise the event. # finish it in service or app. msg = ev.msg domain = msg.domain data = msg.data if CONF.sbp_proto_type == oxproto_v1_0.OXPS_OPENFLOW: buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE if len(data) == 0: return buf += data while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self.network_aware.fake_datapath, version, msg_type, msg_len, xid, buf) if msg: ev = oxp_event.sbp_to_oxp_msg_to_ev(msg) ev.domain = domain self.send_event_to_observers(ev, MAIN_DISPATCHER) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE
def SBP_handler(self, ev): # Parser the msg and raise an event. # Handle event in service or app. msg = ev.msg domain = msg.domain data = msg.data if domain.sbp_proto_type == oxproto_v1_0.OXPS_OPENFLOW: buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE if len(data) == 0: return buf += data while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) self.logger.debug('ofp msg %s cls %s', msg, msg.__class__) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self.fake_datapath, version, msg_type, msg_len, xid, buf) if msg: ev = oxp_event.sbp_to_oxp_msg_to_ev(msg) ev.domain = domain self.send_event_to_observers(ev, MAIN_DISPATCHER) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE
def _recv_loop(self): buf = bytearray() required_len = ofproto.OFP_HEADER_SIZE count = 0 while self.is_active: ret = self.socket.recv(required_len) if len(ret) == 0: self.is_active = False break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf) #LOG.debug('queue msg %s cls %s', msg, msg.__class__) self.ev_q.queue(ofp_event.ofp_msg_to_ev(msg)) buf = buf[required_len:] required_len = ofproto.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 gevent.sleep(0)
def blocking_read_sdn_message(self, block_read_written=False): ''' @returns an SDN message ''' reader_method = self._sdn_socket.blocking_read if block_read_written: reader_method = self._sdn_socket.blocking_read_written # first read openflow header msg_buffer = reader_method(OFP_HEADER_SIZE) while len(msg_buffer) != OFP_HEADER_SIZE: diff = OFP_HEADER_SIZE - len(msg_buffer) msg_buffer.extend(reader_method(diff)) # based on size of openflow header, decide how much more to # read (version, msg_type, msg_len, xid) = ofproto_parser.header(msg_buffer) while len(msg_buffer) != msg_len: diff = msg_len - len(msg_buffer) msg_buffer.extend(reader_method(diff)) msg = ofproto_parser.msg(_OF_1_0_DATAPATH, version, msg_type, msg_len, xid, str(msg_buffer)) msg.original_buffer = msg_buffer return msg
def handle_event(self, header, msg): #required_len = self.ofp.OFP_HEADER_SIZE ret = bytearray(msg) (version, msg_type, msg_len, xid) = ofproto_parser.header(ret) self.netide_xid = header[NetIDEOps.NetIDE_header['XID']] msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, ret) if msg: ev = ofp_event.ofp_msg_to_ev(msg) event_observers = self.ofp_brick.get_observers(ev,self.state) module_id = header[NetIDEOps.NetIDE_header['MOD_ID']] for key, value in self.channel.running_modules.iteritems(): if value == module_id and key in event_observers: module_brick = ryu.base.app_manager.lookup_service_brick(key) module_brick_handlers = module_brick.get_handlers(ev) for handler in module_brick_handlers: handler(ev) break # Sending the FENCE message to the Core only if self.netide_xid is not 0 if self.netide_xid is not 0: msg_to_send = NetIDEOps.netIDE_encode('NETIDE_FENCE', self.netide_xid, module_id, 0, "") self.channel.socket.send(msg_to_send) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler)] for handler in handlers: handler(ev) # Resetting netide_xid to zero self.netide_xid = 0
def _test_msg(self, name, wire_msg, json_str): json_dict = json.loads(json_str) # on-wire -> OFPxxx -> json (version, msg_type, msg_len, xid) = ofproto_parser.header(wire_msg) try: has_parser, has_serializer = implemented[version][msg_type] except KeyError: has_parser = True has_serializer = True dp = DummyDatapath(*self._ofp_versions[version]) if has_parser: msg = ofproto_parser.msg(dp, version, msg_type, msg_len, xid, wire_msg) json_dict2 = self._msg_to_jsondict(msg) # XXXdebug code open(('/tmp/%s.json' % name), 'wb').write(json.dumps(json_dict2)) eq_(json_dict, json_dict2) # json -> OFPxxx -> json msg2 = self._jsondict_to_msg(dp, json_dict) if has_serializer: msg2.serialize() eq_(self._msg_to_jsondict(msg2), json_dict) eq_(wire_msg, msg2.buf)
def _recv_loop(self): buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE count = 0 while self.is_active: ret = self.socket.recv(required_len) if len(ret) == 0: self.is_active = False break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf) # LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: self.principal_connection.receive_principal_message(msg) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def msg_parser (msg): msg_decoded = "" global datapaths (netide_version, netide_msg_type, netide_msg_len, netide_xid, netide_mod_id, netide_datapath) = NetIDEOps.netIDE_decode_header(msg) message_data = msg[NetIDEOps.NetIDE_Header_Size:] ret = bytearray(message_data) if len(ret) >= ofproto_common.OFP_HEADER_SIZE: (version, msg_type, msg_len, xid) = ofproto_parser.header(ret) msg_decoded = ofproto_parser.msg(netide_datapath, version, msg_type, msg_len, xid, ret) #print(msg_decoded) if str(msg_decoded).find("OFPSwitchFeatures", 0, len(str(msg_decoded))) != -1: datapath = msg_decoded.datapath #print(type(datapath)) if not datapath in datapaths: datapaths.append(datapath) return (0, msg_decoded) if str(msg_decoded).find("OFPPortStatsReply", 0, len(str(msg_decoded))) != -1: #print(msg_decoded) #print(str(msg_decoded).find("OFPPortStatsReplay", 0, len(str(msg_decoded)))) return (1, msg_decoded) if str(msg_decoded).find("OFPFlowStatsReply", 0, len(str(msg_decoded))) != -1: return (2, msg_decoded) if str(msg_decoded).find("OFPAggregateStatsReply", 0, len(str(msg_decoded))) != -1: return (3, msg_decoded) if str(msg_decoded).find("OFPQueueStatsReply", 0, len(str(msg_decoded))) != -1: return (4, msg_decoded) if str(msg_decoded).find("OFPTableStatsReply", 0, len(str(msg_decoded))) != -1: return (5, msg_decoded) else: return (0, msg_decoded) else: return (0, msg_decoded)
def blocking_read_sdn_message(self,block_read_written=False): ''' @returns an SDN message ''' reader_method = self._sdn_socket.blocking_read if block_read_written: reader_method = self._sdn_socket.blocking_read_written # first read openflow header msg_buffer = reader_method(OFP_HEADER_SIZE) while len(msg_buffer) != OFP_HEADER_SIZE: diff = OFP_HEADER_SIZE - len(msg_buffer) msg_buffer.extend(reader_method(diff)) # based on size of openflow header, decide how much more to # read (version, msg_type, msg_len, xid) = ofproto_parser.header(msg_buffer) while len(msg_buffer) != msg_len: diff = msg_len - len(msg_buffer) msg_buffer.extend(reader_method(diff)) msg = ofproto_parser.msg( _OF_1_0_DATAPATH, version, msg_type, msg_len, xid, str(msg_buffer)) msg.original_buffer = msg_buffer return msg
def testPacketIn(self): (version, msg_type, msg_len, xid) = ofproto_parser.header(self.bufPacketIn) msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, self.bufPacketIn) LOG.debug(msg) ok_(isinstance(msg, ofproto_v1_0_parser.OFPPacketIn))
def _on_message(self, data): (version, msg_type, msg_len, xid) = ofproto_parser.header(self._msg_header) assert len(data) == msg_len - ofproto_common.OFP_HEADER_SIZE msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, self._msg_header+data) logging.getLogger("openflow").debug("RECV %s %s" % (self.address, msg)) self.dispatch(msg) if not self.stream.closed(): self.stream.read_bytes(ofproto_common.OFP_HEADER_SIZE, stack_context.wrap(self._on_header))
def _recv_loop(self): buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE count = 0 while self.state != DEAD_DISPATCHER: ret = "" try: ret = self.socket.recv(required_len) except SocketTimeout: continue except ssl.SSLError: # eventlet throws SSLError (which is a subclass of IOError) # on SSL socket read timeout; re-try the loop in this case. continue except (EOFError, IOError): break if len(ret) == 0: break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf[:msg_len]) # LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [ handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler) ] for handler in handlers: handler(ev) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def _recv_loop(self): buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE count = 0 while self.state != DEAD_DISPATCHER: ret = "" try: ret = self.socket.recv(required_len) except SocketTimeout: continue except ssl.SSLError: # eventlet throws SSLError (which is a subclass of IOError) # on SSL socket read timeout; re-try the loop in this case. continue except (EOFError, IOError): break if len(ret) == 0: break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg( self, version, msg_type, msg_len, xid, buf[:msg_len]) # LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler)] for handler in handlers: handler(ev) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def _recv_loop(self): buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE count = 0 while True: ret = "" try: ret = self.socket.recv(required_len) except: # Hit socket timeout; decide what to do. if self.close_requested: pass else: continue if (len(ret) == 0) or (self.close_requested): self.socket.close() break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf[:msg_len]) # LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [ handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler) ] for handler in handlers: handler(ev) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def _recv_loop(self): buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE count = 0 while True: ret = "" try: ret = self.socket.recv(required_len) except: # Hit socket timeout; decide what to do. if self.close_requested: pass else: continue if (len(ret) == 0) or (self.close_requested): self.socket.close() break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg( self, version, msg_type, msg_len, xid, buf[:msg_len]) # LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler)] for handler in handlers: handler(ev) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def testFeaturesReply(self): (version, msg_type, msg_len, xid) = ofproto_parser.header(self.bufFeaturesReply) msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, self.bufFeaturesReply) LOG.debug(msg) ok_(isinstance(msg, ofproto_v1_0_parser.OFPSwitchFeatures)) LOG.debug(msg.ports[65534]) ok_(isinstance(msg.ports[1], ofproto_v1_0_parser.OFPPhyPort)) ok_(isinstance(msg.ports[2], ofproto_v1_0_parser.OFPPhyPort)) ok_(isinstance(msg.ports[65534], ofproto_v1_0_parser.OFPPhyPort))
def error_msg_handler(self, ev): err_msg = ev.msg #self.logger.info('OFPErrorMsg received: type=0x%02x code=0x%02x message=%s', msg.type, msg.code, utils.hex_array(msg.data)) if err_msg.type == ofproto_v1_3.OFPET_FLOW_MOD_FAILED and err_msg.code == ofproto_v1_3.OFPFMFC_TABLE_FULL: print 'caputred full table error message' self.rand_remove(err_msg.datapath) #parser = err_msg.datapath.ofproto_parser (version,msg_type,msg_len,xid)= ofproto_parser.header(err_msg.data) print 'version %s, msg_type %s, msg_len %s, xid %s, len %s' % (version, msg_type, msg_len, xid, len(err_msg.data)) msg = ofproto_parser.msg(err_msg.datapath, version,msg_type,msg_len,xid,err_msg.data) #controller will assign a new xid msg.xid=None error_msg.datapath.send_msg(msg)
def decode_sent_message(msg): (netide_version, netide_msg_type, netide_msg_len, netide_xid, netide_mod_id, netide_datapath) = NetIDEOps.netIDE_decode_header(msg) print(netide_version, netide_msg_type, netide_msg_len, netide_xid, netide_mod_id, netide_datapath) message_data = msg[NetIDEOps.NetIDE_Header_Size:] ret = bytearray(message_data) #print(len(message_data)) if len(ret) >= ofproto_common.OFP_HEADER_SIZE: #prrr = struct.unpack('BBHI', message_data) #print(prrr) (version, msg_type, msg_len, xid) = ofproto_parser.header(ret) print(version, msg_type, msg_len, xid) msg_decoded = ofproto_parser.msg(netide_datapath, version, msg_type, msg_len, xid, ret) print(msg_decoded)
def recv_ofp(self): logging.debug("%s", binascii.hexlify(self.data)) self.recv(ofproto_common.OFP_HEADER_SIZE) version, msg_type, msg_len, xid = ofproto_parser.header(self.data) self.recv(msg_len) msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, self.data[:msg_len]) self.data = self.data[msg_len:] return msg
def OF_error_msg_handler(self, event): msg = event.msg try: (version, msg_type, msg_len, xid) = ofproto_parser.header(msg.data) ryu_msg = ofproto_parser.msg( self._datapath, version, msg_type, msg_len - ofproto_common.OFP_HEADER_SIZE, xid, msg.data) LOG.error('OFPErrorMsg received: %s', ryu_msg) except Exception: LOG.error('Unrecognized OFPErrorMsg received: ' 'type=0x%(type)02x code=0x%(code)02x ' 'message=%(msg)s', {'type': msg.type, 'code': msg.code, 'msg': utils.hex_array(msg.data)})
def _test_msg(self, name, wire_msg, json_str): json_dict = json.loads(json_str) # on-wire -> OFPxxx -> json (version, msg_type, msg_len, xid) = ofproto_parser.header(wire_msg) try: has_parser, has_serializer = implemented[version][msg_type] except KeyError: has_parser = True has_serializer = True dp = ofproto_protocol.ProtocolDesc(version=version) if has_parser: msg = ofproto_parser.msg(dp, version, msg_type, msg_len, xid, wire_msg) json_dict2 = self._msg_to_jsondict(msg) # XXXdebug code open(('/tmp/%s.json' % name), 'wb').write(json.dumps(json_dict2)) eq_(json_dict, json_dict2) # json -> OFPxxx -> json msg2 = self._jsondict_to_msg(dp, json_dict) if has_serializer: msg2.serialize() eq_(self._msg_to_jsondict(msg2), json_dict) eq_(wire_msg, msg2.buf) # check if "len" "length" fields can be omitted def _remove(d, names): f = lambda x: _remove(x, names) if isinstance(d, list): return map(f, d) if isinstance(d, dict): d2 = {} for k, v in d.iteritems(): if k in names: continue d2[k] = f(v) return d2 return d json_dict3 = _remove(json_dict, ['len', 'length']) msg3 = self._jsondict_to_msg(dp, json_dict3) msg3.serialize() eq_(wire_msg, msg3.buf) msg2.serialize() eq_(wire_msg, msg2.buf)
def _test_msg(self, name, wire_msg, json_str): json_dict = json.loads(json_str) # on-wire -> OFPxxx -> json (version, msg_type, msg_len, xid) = ofproto_parser.header(wire_msg) try: has_parser, has_serializer = implemented[version][msg_type] except KeyError: has_parser = True has_serializer = True dp = DummyDatapath(*self._ofp_versions[version]) if has_parser: msg = ofproto_parser.msg(dp, version, msg_type, msg_len, xid, wire_msg) json_dict2 = self._msg_to_jsondict(msg) # XXXdebug code open(('/tmp/%s.json' % name), 'wb').write(json.dumps(json_dict2)) eq_(json_dict, json_dict2) # json -> OFPxxx -> json msg2 = self._jsondict_to_msg(dp, json_dict) if has_serializer: msg2.serialize() eq_(self._msg_to_jsondict(msg2), json_dict) eq_(wire_msg, msg2.buf) # check if "len" "length" fields can be omitted def _remove(d, names): f = lambda x: _remove(x, names) if isinstance(d, list): return map(f, d) if isinstance(d, dict): d2 = {} for k, v in d.iteritems(): if k in names: continue d2[k] = f(v) return d2 return d json_dict3 = _remove(json_dict, ['len', 'length']) msg3 = self._jsondict_to_msg(dp, json_dict3) msg3.serialize() eq_(wire_msg, msg3.buf) msg2.serialize() eq_(wire_msg, msg2.buf)
def handle_event(self, header, msg, of_proto): #required_len = self.ofp.OFP_HEADER_SIZE ret = bytearray(msg) (version, msg_type, msg_len, xid) = ofproto_parser.header(ret) self.netide_xid = header[NetIDEOps.NetIDE_header['XID']] msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, ret) if msg: ev = ofp_event.ofp_msg_to_ev(msg) event_observers = self.ofp_brick.get_observers(ev, self.state) module_id = header[NetIDEOps.NetIDE_header['MOD_ID']] for key, value in self.channel.running_modules.iteritems(): if value == module_id and key in event_observers: module_brick = ryu.base.app_manager.lookup_service_brick( key) module_brick_handlers = module_brick.get_handlers(ev) for handler in module_brick_handlers: handler(ev) break # Sending the FENCE message to the Core only if self.netide_xid is not 0 if self.netide_xid is not 0: msg_to_send = NetIDEOps.netIDE_encode('NETIDE_FENCE', self.netide_xid, module_id, 0, "") self.channel.socket.send(msg_to_send) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [ handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler) ] for handler in handlers: # we record that we received a feature reply from that specific device if msg_type == of_proto.OFPT_FEATURES_REPLY: self.datapath_init[msg.datapath.id] = True # we do not allow multipart messages until the feature_reply has been received (needed for OF1.3 or higher) if of_proto.OFP_VERSION >= 0x04: if msg_type == of_proto.OFPT_MULTIPART_REPLY and msg.datapath.id not in self.datapath_init: break handler(ev) # Resetting netide_xid to zero self.netide_xid = 0
def handle_event(self, header, msg, of_proto): #required_len = self.ofp.OFP_HEADER_SIZE ret = bytearray(msg) (version, msg_type, msg_len, xid) = ofproto_parser.header(ret) self.netide_xid = header[NetIDEOps.NetIDE_header['XID']] msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, ret) if msg: ev = ofp_event.ofp_msg_to_ev(msg) event_observers = self.ofp_brick.get_observers(ev,self.state) module_id = header[NetIDEOps.NetIDE_header['MOD_ID']] for key, value in self.channel.running_modules.iteritems(): if value == module_id and key in event_observers: module_brick = ryu.base.app_manager.lookup_service_brick(key) module_brick_handlers = module_brick.get_handlers(ev) for handler in module_brick_handlers: handler(ev) break # Sending the FENCE message to the Core only if self.netide_xid is not 0 if self.netide_xid is not 0: msg_to_send = NetIDEOps.netIDE_encode('NETIDE_FENCE', self.netide_xid, module_id, 0, "") self.channel.socket.send(msg_to_send) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler)] for handler in handlers: # we record that we received a feature reply from that specific device if msg_type == of_proto.OFPT_FEATURES_REPLY: self.datapath_init[msg.datapath.id] = True # we do not allow multipart messages until the feature_reply has been received (needed for OF1.3 or higher) if of_proto.OFP_VERSION >= 0x04: if msg_type == of_proto.OFPT_MULTIPART_REPLY and msg.datapath.id not in self.datapath_init: break handler(ev) # Resetting netide_xid to zero self.netide_xid = 0
def _recv_loop(self): buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE count = 0 while self.is_active: ret = self.socket.recv(required_len) if len(ret) == 0: self.is_active = False break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf) #LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) handlers = [ handler for handler in self.ofp_brick.get_handlers(ev) if self.state in handler.dispatchers ] for handler in handlers: handler(ev) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def _recv_loop(self): buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE count = 0 while self.is_active: ret = self.socket.recv(required_len) if len(ret) == 0: self.is_active = False break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf) #LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) handlers = [handler for handler in self.ofp_brick.get_handlers(ev) if self.state in handler.dispatchers] for handler in handlers: handler(ev) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def _recv_loop(self): buf = bytearray() required_len = ofproto.OFP_HEADER_SIZE while self.is_active: ret = self.socket.recv(ofproto.OFP_MSG_SIZE_MAX) if len(ret) == 0: self.is_active = False break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf) #LOG.debug('queue msg %s cls %s', msg, msg.__class__) self.recv_q.put(msg) buf = buf[required_len:] required_len = ofproto.OFP_HEADER_SIZE
def packet_in_handler(self,msg): version = ofproto.OFP_VERSION msg_type = ofproto.OFPT_PACKET_IN msg_len = ofproto.OFP_PACKET_IN_SIZE in_port = msg['inport'] buf = self.packet_to_network(msg) buf_len = len(buf) zfill = '\x00' * 1 # TODO: buffer_id is set to -1 since is not provided by the shim client data = self.to_hex_string(version,1) \ + self.to_hex_string(msg_type,1) \ + self.to_hex_string(msg_len,2) \ + self.to_hex_string(0,4) \ + self.to_hex_string(0xFFFFFFFF,4) \ + self.to_hex_string(msg_len + buf_len,2) \ + self.to_hex_string(in_port,2) \ + self.to_hex_string(ofproto.OFPR_NO_MATCH,1) \ + zfill \ + buf packet_in = ofproto_parser.msg(self, version, msg_type, msg_len, 0, data) print "Backend. Datapath: " , self.id, " packet_in:", packet_in self.register_event(packet_in)
def _test_msg(self, name, wire_msg, json_str): def bytes_eq(buf1, buf2): if buf1 != buf2: msg = 'EOF in either data' for i in range(0, min(len(buf1), len(buf2))): c1 = six.indexbytes(six.binary_type(buf1), i) c2 = six.indexbytes(six.binary_type(buf2), i) if c1 != c2: msg = 'differs at chr %d, %d != %d' % (i, c1, c2) break assert buf1 == buf2, "%r != %r, %s" % (buf1, buf2, msg) json_dict = json.loads(json_str) # on-wire -> OFPxxx -> json (version, msg_type, msg_len, xid) = ofproto_parser.header(wire_msg) try: has_parser, has_serializer = implemented[version][msg_type] except KeyError: has_parser = True has_serializer = True dp = ofproto_protocol.ProtocolDesc(version=version) if has_parser: try: msg = ofproto_parser.msg(dp, version, msg_type, msg_len, xid, wire_msg) json_dict2 = self._msg_to_jsondict(msg) except exception.OFPTruncatedMessage as e: json_dict2 = { 'OFPTruncatedMessage': self._msg_to_jsondict(e.ofpmsg) } # XXXdebug code open(('/tmp/%s.json' % name), 'w').write(json.dumps(json_dict2)) eq_(json_dict, json_dict2) if 'OFPTruncatedMessage' in json_dict2: return # json -> OFPxxx -> json xid = json_dict[list(json_dict.keys())[0]].pop('xid', None) msg2 = self._jsondict_to_msg(dp, json_dict) msg2.set_xid(xid) if has_serializer: msg2.serialize() eq_(self._msg_to_jsondict(msg2), json_dict) bytes_eq(wire_msg, msg2.buf) # check if "len" "length" fields can be omitted def _remove(d, names): f = lambda x: _remove(x, names) if isinstance(d, list): return list(map(f, d)) if isinstance(d, dict): d2 = {} for k, v in d.items(): if k in names: continue d2[k] = f(v) return d2 return d json_dict3 = _remove(json_dict, ['len', 'length']) msg3 = self._jsondict_to_msg(dp, json_dict3) msg3.set_xid(xid) msg3.serialize() bytes_eq(wire_msg, msg3.buf) msg2.serialize() bytes_eq(wire_msg, msg2.buf)
def _test_msg(self, name, wire_msg, json_str): def bytes_eq(buf1, buf2): if buf1 != buf2: msg = 'EOF in either data' for i in range(0, min(len(buf1), len(buf2))): c1 = six.indexbytes(six.binary_type(buf1), i) c2 = six.indexbytes(six.binary_type(buf2), i) if c1 != c2: msg = 'differs at chr %d, %d != %d' % (i, c1, c2) break assert buf1 == buf2, "%r != %r, %s" % (buf1, buf2, msg) json_dict = json.loads(json_str) # on-wire -> OFPxxx -> json (version, msg_type, msg_len, xid) = ofproto_parser.header(wire_msg) try: has_parser, has_serializer = implemented[version][msg_type] except KeyError: has_parser = True has_serializer = True dp = ofproto_protocol.ProtocolDesc(version=version) if has_parser: msg = ofproto_parser.msg(dp, version, msg_type, msg_len, xid, wire_msg) json_dict2 = self._msg_to_jsondict(msg) # XXXdebug code open(('/tmp/%s.json' % name), 'w').write(json.dumps(json_dict2)) eq_(json_dict, json_dict2) # json -> OFPxxx -> json xid = json_dict[list(json_dict.keys())[0]].pop('xid', None) msg2 = self._jsondict_to_msg(dp, json_dict) msg2.set_xid(xid) if has_serializer: msg2.serialize() eq_(self._msg_to_jsondict(msg2), json_dict) bytes_eq(wire_msg, msg2.buf) # check if "len" "length" fields can be omitted def _remove(d, names): f = lambda x: _remove(x, names) if isinstance(d, list): return list(map(f, d)) if isinstance(d, dict): d2 = {} for k, v in d.items(): if k in names: continue d2[k] = f(v) return d2 return d json_dict3 = _remove(json_dict, ['len', 'length']) msg3 = self._jsondict_to_msg(dp, json_dict3) msg3.set_xid(xid) msg3.serialize() bytes_eq(wire_msg, msg3.buf) msg2.serialize() bytes_eq(wire_msg, msg2.buf)
def _recv_loop(self): buf = bytearray() count = 0 min_read_len = remaining_read_len = ofproto_common.OFP_HEADER_SIZE while self.state != DEAD_DISPATCHER: try: read_len = min_read_len if remaining_read_len > min_read_len: read_len = remaining_read_len ret = self.socket.recv(read_len) except SocketTimeout: continue except ssl.SSLError: # eventlet throws SSLError (which is a subclass of IOError) # on SSL socket read timeout; re-try the loop in this case. continue except (EOFError, IOError): break if not ret: break buf += ret buf_len = len(buf) while buf_len >= min_read_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) if msg_len < min_read_len: # Someone isn't playing nicely; log it, and try something sane. LOG.debug( "Message with invalid length %s received from switch at address %s", msg_len, self.address) msg_len = min_read_len if buf_len < msg_len: remaining_read_len = (msg_len - buf_len) break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf[:msg_len]) # LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) def dispatchers(x): return x.callers[ev.__class__].dispatchers handlers = [ handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler) ] for handler in handlers: handler(ev) buf = buf[msg_len:] buf_len = len(buf) remaining_read_len = min_read_len # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def _recv_loop(self): buf = bytearray() count = 0 min_read_len = remaining_read_len = ofproto_common.OFP_HEADER_SIZE while self.state != DEAD_DISPATCHER: try: read_len = min_read_len if (remaining_read_len > min_read_len): read_len = remaining_read_len ret = self.socket.recv(read_len) except SocketTimeout: continue except ssl.SSLError: # eventlet throws SSLError (which is a subclass of IOError) # on SSL socket read timeout; re-try the loop in this case. continue except (EOFError, IOError): break if len(ret) == 0: break buf += ret buf_len = len(buf) while buf_len >= min_read_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) if (msg_len < min_read_len): # Someone isn't playing nicely; log it, and try something sane. LOG.debug("Message with invalid length %s received from switch at address %s", msg_len, self.address) msg_len = min_read_len if buf_len < msg_len: remaining_read_len = (msg_len - buf_len) break msg = ofproto_parser.msg( self, version, msg_type, msg_len, xid, buf[:msg_len]) # LOG.debug('queue msg %s cls %s', msg, msg.__class__) if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler)] for handler in handlers: handler(ev) buf = buf[msg_len:] buf_len = len(buf) remaining_read_len = min_read_len # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)
def _recv_loop(self): buf = bytearray() required_len = ofproto_common.OFP_HEADER_SIZE count = 0 while self.is_active: ret = self.socket.recv(required_len) if len(ret) == 0: self.is_active = False break buf += ret while len(buf) >= required_len: (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) required_len = msg_len if len(buf) < required_len: break msg = ofproto_parser.msg(self, version, msg_type, msg_len, xid, buf) # LOG.debug('queue msg %s cls %s', msg, msg.__class__) # -------------------------- Fujitsu code start ----------------------------- # For optical enhancing LOG.debug('receive data (dpid=%s) <%s> [%s]', str(self.id), self.msgtype_dict.setdefault(msg_type, 'unknown(%d)' % msg_type), binascii.hexlify(buf)) # debug mode (replace to flow_stats_reply message for debug) if CONF.dbg_flow_stats_reply_path is not None and isinstance(msg, self.ofproto_parser.OFPFlowStatsReply): buf_tmp = '' bin_len = 0 try: fl = open(CONF.dbg_flow_stats_reply_path) bin_data = '' for line in fl: bin_data += line bin_len = len(bin_data) // 2 buf_tmp = bytearray() for i in range(0,bin_len): buf_tmp.append(struct.pack("B", int(bin_data[i*2:i*2+2],16))) except: # not found data file. (default sequence) LOG.debug("_recv_loop : flow-stats-reply-file(%s) open error.", CONF.dbg_flow_stats_reply_path) if buf_tmp != '' : msg = ofproto_parser.msg(self, version, msg_type, bin_len, xid, buf_tmp) LOG.debug('replace receive data(TEST DATA:%s) (dpid=%s) <OFPT_MULTIPART_REPLY> [%s]', CONF.dbg_flow_stats_reply_path, str(self.id), binascii.hexlify(buf_tmp)) # -------------------------- Fujitsu code end ------------------------------- if msg: ev = ofp_event.ofp_msg_to_ev(msg) self.ofp_brick.send_event_to_observers(ev, self.state) dispatchers = lambda x: x.callers[ev.__class__].dispatchers handlers = [handler for handler in self.ofp_brick.get_handlers(ev) if self.state in dispatchers(handler)] for handler in handlers: handler(ev) buf = buf[required_len:] required_len = ofproto_common.OFP_HEADER_SIZE # We need to schedule other greenlets. Otherwise, ryu # can't accept new switches or handle the existing # switches. The limit is arbitrary. We need the better # approach in the future. count += 1 if count > 2048: count = 0 hub.sleep(0)