add ofproto 1.3 coverage, check key-error and attribute-error.

This commit is contained in:
Josh Bailey 2021-06-06 21:52:17 +00:00
parent 34f33491ff
commit 98d4913d32
6 changed files with 65 additions and 51 deletions

View File

@ -317,7 +317,8 @@ class Datapath(ofproto_protocol.ProtocolDesc):
self.state = state self.state = state
ev = ofp_event.EventOFPStateChange(self) ev = ofp_event.EventOFPStateChange(self)
ev.state = state ev.state = state
self.ofp_brick.send_event_to_observers(ev, state) if self.ofp_brick is not None:
self.ofp_brick.send_event_to_observers(ev, state)
# Low level socket handling layer # Low level socket handling layer
@_deactivate @_deactivate
@ -362,16 +363,17 @@ class Datapath(ofproto_protocol.ProtocolDesc):
# LOG.debug('queue msg %s cls %s', msg, msg.__class__) # LOG.debug('queue msg %s cls %s', msg, msg.__class__)
if msg: if msg:
ev = ofp_event.ofp_msg_to_ev(msg) ev = ofp_event.ofp_msg_to_ev(msg)
self.ofp_brick.send_event_to_observers(ev, self.state) if self.ofp_brick is not None:
self.ofp_brick.send_event_to_observers(ev, self.state)
def dispatchers(x): def dispatchers(x):
return x.callers[ev.__class__].dispatchers return x.callers[ev.__class__].dispatchers
handlers = [handler for handler in handlers = [handler for handler in
self.ofp_brick.get_handlers(ev) if self.ofp_brick.get_handlers(ev) if
self.state in dispatchers(handler)] self.state in dispatchers(handler)]
for handler in handlers: for handler in handlers:
handler(ev) handler(ev)
buf = buf[msg_len:] buf = buf[msg_len:]
buf_len = len(buf) buf_len = len(buf)

View File

@ -149,6 +149,7 @@ def register_service(service):
there are applications consuming OFP events. there are applications consuming OFP events.
""" """
frame = inspect.currentframe() frame = inspect.currentframe()
m_name = frame.f_back.f_globals['__name__'] if frame is not None:
m = sys.modules[m_name] m_name = frame.f_back.f_globals['__name__']
m._SERVICE_NAME = service m = sys.modules[m_name]
m._SERVICE_NAME = service

View File

@ -50,7 +50,7 @@ class MacToPortTable(object):
return self.mac_to_port[dpid].get(mac) return self.mac_to_port[dpid].get(mac)
def mac_list(self, dpid, port): def mac_list(self, dpid, port):
return [mac for (mac, port_) in self.mac_to_port.get(dpid).items() return [mac for (mac, port_) in self.mac_to_port.get(dpid, {}).items()
if port_ == port] if port_ == port]
def mac_del(self, dpid, mac): def mac_del(self, dpid, mac):

View File

@ -47,7 +47,7 @@ def register_switch_address(addr, interval=None):
def _retry_loop(): def _retry_loop():
# Delays registration if ofp_handler is not started yet # Delays registration if ofp_handler is not started yet
while True: while True:
if ofp_handler.controller is not None: if ofp_handler is not None and ofp_handler.controller is not None:
for a, i in _TMP_ADDRESSES.items(): for a, i in _TMP_ADDRESSES.items():
ofp_handler.controller.spawn_client_loop(a, i) ofp_handler.controller.spawn_client_loop(a, i)
hub.sleep(1) hub.sleep(1)
@ -69,6 +69,6 @@ def unregister_switch_address(addr):
""" """
ofp_handler = app_manager.lookup_service_brick(ofp_event.NAME) ofp_handler = app_manager.lookup_service_brick(ofp_event.NAME)
# Do nothing if ofp_handler is not started yet # Do nothing if ofp_handler is not started yet
if ofp_handler.controller is None: if ofp_handler is None or ofp_handler.controller is None:
return return
ofp_handler.controller.stop_client_loop(addr) ofp_handler.controller.stop_client_loop(addr)

View File

@ -1754,7 +1754,7 @@ class OFPMatchField(StringifyMixin):
(value, mask) = struct.unpack_from(pack_str, buf, offset + 4) (value, mask) = struct.unpack_from(pack_str, buf, offset + 4)
else: else:
(value,) = struct.unpack_from(cls.pack_str, buf, offset + 4) (value,) = struct.unpack_from(cls.pack_str, buf, offset + 4)
return cls(header, value, mask) return cls(header, value, mask) # pytype: disable=wrong-arg-count
def serialize(self, buf, offset): def serialize(self, buf, offset):
if ofproto.oxm_tlv_header_extract_hasmask(self.header): if ofproto.oxm_tlv_header_extract_hasmask(self.header):
@ -2773,8 +2773,9 @@ class OFPFlowMod(MsgBase):
try: try:
while offset < msg_len: while offset < msg_len:
i = OFPInstruction.parser(buf, offset) i = OFPInstruction.parser(buf, offset)
instructions.append(i) if i is not None:
offset += i.len instructions.append(i)
offset += i.len
except exception.OFPTruncatedMessage as e: except exception.OFPTruncatedMessage as e:
instructions.append(e.ofpmsg) instructions.append(e.ofpmsg)
msg.instructions = instructions msg.instructions = instructions
@ -2805,7 +2806,9 @@ class OFPInstruction(StringifyMixin):
def parser(cls, buf, offset): def parser(cls, buf, offset):
(type_, len_) = struct.unpack_from('!HH', buf, offset) (type_, len_) = struct.unpack_from('!HH', buf, offset)
cls_ = cls._INSTRUCTION_TYPES.get(type_) cls_ = cls._INSTRUCTION_TYPES.get(type_)
return cls_.parser(buf, offset) if cls_ is not None:
return cls_.parser(buf, offset)
return None
@OFPInstruction.register_instruction_type([ofproto.OFPIT_GOTO_TABLE]) @OFPInstruction.register_instruction_type([ofproto.OFPIT_GOTO_TABLE])
@ -3551,7 +3554,7 @@ class OFPActionExperimenter(OFPAction):
data = buf[(offset + ofproto.OFP_ACTION_EXPERIMENTER_HEADER_SIZE data = buf[(offset + ofproto.OFP_ACTION_EXPERIMENTER_HEADER_SIZE
): offset + len_] ): offset + len_]
if experimenter == ofproto_common.NX_EXPERIMENTER_ID: if experimenter == ofproto_common.NX_EXPERIMENTER_ID:
obj = NXAction.parse(data) # noqa obj = NXAction.parse(data) # pytype: disable=name-error # noqa
else: else:
obj = OFPActionExperimenterUnknown(experimenter, data) obj = OFPActionExperimenterUnknown(experimenter, data)
obj.len = len_ obj.len = len_
@ -3932,22 +3935,23 @@ class OFPMultipartReply(MsgBase):
ofproto.OFP_MULTIPART_REPLY_PACK_STR, six.binary_type(buf), ofproto.OFP_MULTIPART_REPLY_PACK_STR, six.binary_type(buf),
ofproto.OFP_HEADER_SIZE) ofproto.OFP_HEADER_SIZE)
stats_type_cls = cls._STATS_MSG_TYPES.get(type_) stats_type_cls = cls._STATS_MSG_TYPES.get(type_)
msg = super(OFPMultipartReply, stats_type_cls).parser( msg = super(OFPMultipartReply, stats_type_cls).parser( # pytype: disable=attribute-error
datapath, version, msg_type, msg_len, xid, buf) datapath, version, msg_type, msg_len, xid, buf)
msg.type = type_ msg.type = type_
msg.flags = flags msg.flags = flags
offset = ofproto.OFP_MULTIPART_REPLY_SIZE if stats_type_cls is not None:
body = [] offset = ofproto.OFP_MULTIPART_REPLY_SIZE
while offset < msg_len: body = []
b = stats_type_cls.cls_stats_body_cls.parser(msg.buf, offset) while offset < msg_len:
body.append(b) b = stats_type_cls.cls_stats_body_cls.parser(msg.buf, offset)
offset += b.length if hasattr(b, 'length') else b.len body.append(b)
offset += b.length if hasattr(b, 'length') else b.len
if stats_type_cls.cls_body_single_struct: if stats_type_cls.cls_body_single_struct:
msg.body = body[0] msg.body = body[0]
else: else:
msg.body = body msg.body = body
return msg return msg
@ -4577,12 +4581,13 @@ class OFPGroupStats(StringifyMixin):
group_stats = cls(*group) group_stats = cls(*group)
group_stats.bucket_stats = [] group_stats.bucket_stats = []
total_len = group_stats.length + offset if group_stats.length is not None:
offset += ofproto.OFP_GROUP_STATS_SIZE total_len = group_stats.length + offset
while total_len > offset: offset += ofproto.OFP_GROUP_STATS_SIZE
b = OFPBucketCounter.parser(buf, offset) while total_len > offset:
group_stats.bucket_stats.append(b) b = OFPBucketCounter.parser(buf, offset)
offset += ofproto.OFP_BUCKET_COUNTER_SIZE group_stats.bucket_stats.append(b)
offset += ofproto.OFP_BUCKET_COUNTER_SIZE
return group_stats return group_stats
@ -5770,7 +5775,7 @@ class ONFFlowMonitorRequest(StringifyMixin):
match_len = match.length match_len = match.length
match_hdr_len = ofproto.OFP_MATCH_SIZE - 4 # exclude pad[4] match_hdr_len = ofproto.OFP_MATCH_SIZE - 4 # exclude pad[4]
# strip ofp_match header and trailing padding # strip ofp_match header and trailing padding
bin_match = bytes(bin_match)[match_hdr_len:match_len] bin_match = bytearray(bin_match)[match_hdr_len:match_len]
self.match_len = len(bin_match) self.match_len = len(bin_match)
buf = bytearray() buf = bytearray()
@ -5936,14 +5941,16 @@ class OFPQueueProp(OFPQueuePropHeader):
ofproto.OFP_QUEUE_PROP_HEADER_PACK_STR, ofproto.OFP_QUEUE_PROP_HEADER_PACK_STR,
buf, offset) buf, offset)
cls_ = cls._QUEUE_PROP_PROPERTIES.get(property_) cls_ = cls._QUEUE_PROP_PROPERTIES.get(property_)
p = cls_.parser(buf, offset + ofproto.OFP_QUEUE_PROP_HEADER_SIZE) if cls_ is not None:
p.property = property_ p = cls_.parser(buf, offset + ofproto.OFP_QUEUE_PROP_HEADER_SIZE)
p.len = len_ p.property = property_
if property_ == ofproto.OFPQT_EXPERIMENTER: p.len = len_
rest = buf[offset + ofproto.OFP_QUEUE_PROP_EXPERIMENTER_SIZE: if property_ == ofproto.OFPQT_EXPERIMENTER:
offset + len_] rest = buf[offset + ofproto.OFP_QUEUE_PROP_EXPERIMENTER_SIZE:
p.parse_experimenter_data(rest) offset + len_]
return p p.parse_experimenter_data(rest)
return p
return None
@OFPQueueProp.register_property(ofproto.OFPQT_MIN_RATE, @OFPQueueProp.register_property(ofproto.OFPQT_MIN_RATE,
@ -6017,9 +6024,10 @@ class OFPPacketQueue(StringifyMixin):
properties = [] properties = []
while length < len_: while length < len_:
queue_prop = OFPQueueProp.parser(buf, offset) queue_prop = OFPQueueProp.parser(buf, offset)
properties.append(queue_prop) if queue_prop is not None:
offset += queue_prop.len properties.append(queue_prop)
length += queue_prop.len offset += queue_prop.len
length += queue_prop.len
o = cls(queue_id, port, properties) o = cls(queue_id, port, properties)
o.len = len_ o.len = len_
return o return o
@ -6342,6 +6350,10 @@ class OFPSetAsync(MsgBase):
self.flow_removed_mask[0], self.flow_removed_mask[1]) self.flow_removed_mask[0], self.flow_removed_mask[1])
class OFPBundleProp(OFPPropBase):
_TYPES = {}
@_register_exp_type(ofproto_common.ONF_EXPERIMENTER_ID, @_register_exp_type(ofproto_common.ONF_EXPERIMENTER_ID,
ofproto.ONF_ET_BUNDLE_CONTROL) ofproto.ONF_ET_BUNDLE_CONTROL)
class ONFBundleCtrlMsg(OFPExperimenter): class ONFBundleCtrlMsg(OFPExperimenter):

View File

@ -59,10 +59,9 @@ console_scripts =
[pytype] [pytype]
inputs = inputs =
ryu/controller/ ryu/controller/
ryu/ofproto/ofproto_v1_3*
disable = disable =
attribute-error
import-error import-error
key-error
module-attr module-attr
keep-going = keep-going =
1 1