This patch introduces a new option "--with-timestamp" which enable to get timestamp when Ryu received the OpenFlow message.
Suggested-by: Matthew Hayes <matthew_john_ha...@hotmail.com> Signed-off-by: IWASE Yusuke <iwase.yusu...@gmail.com> --- ryu/cmd/manager.py | 4 ++++ ryu/controller/ofp_event.py | 42 ++++++++++++++++++++++++++++-------------- ryu/flags.py | 3 +++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/ryu/cmd/manager.py b/ryu/cmd/manager.py index c801131..b9568fb 100755 --- a/ryu/cmd/manager.py +++ b/ryu/cmd/manager.py @@ -32,6 +32,7 @@ from ryu import flags # to load registered option definitions from ryu import version from ryu.app import wsgi from ryu.base.app_manager import AppManager +from ryu.controller import ofp_event CONF = cfg.CONF @@ -87,6 +88,9 @@ def main(args=None, prog=None): with open(CONF.pid_file, 'w') as pid_file: pid_file.write(str(os.getpid())) + if CONF.with_timestamp: + ofp_event.generate_ofp_msg_ev_classes(with_timestamp=True) + app_lists = CONF.app_lists + CONF.app # keep old behavior, run ofp if no application is specified. if not app_lists: diff --git a/ryu/controller/ofp_event.py b/ryu/controller/ofp_event.py index 6b1c8b3..8808da3 100644 --- a/ryu/controller/ofp_event.py +++ b/ryu/controller/ofp_event.py @@ -19,10 +19,10 @@ OpenFlow event definitions. """ import inspect +import time from ryu.controller import handler from ryu import ofproto -from ryu import utils from . import event @@ -51,6 +51,17 @@ class EventOFPMsgBase(event.EventBase): self.msg = msg +class EventOFPMsgBaseWithTimestamp(EventOFPMsgBase): + """ + The base class of OpenFlow event class with timestamp. + + This class takes timestamp when instantiated and store it as ``timestamp`` + attribute. The other attributes are the same with ``EventOFPMsgBase``. + """ + def __init__(self, msg): + super(EventOFPMsgBaseWithTimestamp, self).__init__(msg=msg) + self.timestamp = time.time() + # # Create ofp_event type corresponding to OFP Msg # @@ -71,32 +82,35 @@ def ofp_msg_to_ev_cls(msg_cls): return _OFP_MSG_EVENTS[name] -def _create_ofp_msg_ev_class(msg_cls): +def _create_ofp_msg_ev_class(msg_cls, with_timestamp=False): name = _ofp_msg_name_to_ev_name(msg_cls.__name__) - # print 'creating ofp_event %s' % name - if name in _OFP_MSG_EVENTS: + if not with_timestamp and name in _OFP_MSG_EVENTS: return - cls = type(name, (EventOFPMsgBase,), - dict(__init__=lambda self, msg: - super(self.__class__, self).__init__(msg))) + _base_cls = EventOFPMsgBase + if with_timestamp: + _base_cls = EventOFPMsgBaseWithTimestamp + + cls = type(name, (_base_cls,), {}) globals()[name] = cls _OFP_MSG_EVENTS[name] = cls -def _create_ofp_msg_ev_from_module(ofp_parser): - # print mod +def _create_ofp_msg_ev_from_module(ofp_parser, with_timestamp=False): for _k, cls in inspect.getmembers(ofp_parser, inspect.isclass): if not hasattr(cls, 'cls_msg_type'): continue - _create_ofp_msg_ev_class(cls) + _create_ofp_msg_ev_class(cls, with_timestamp) + + +def generate_ofp_msg_ev_classes(with_timestamp=False): + for ofp_mods in ofproto.get_ofp_modules().values(): + ofp_parser = ofp_mods[1] + _create_ofp_msg_ev_from_module(ofp_parser, with_timestamp) -for ofp_mods in ofproto.get_ofp_modules().values(): - ofp_parser = ofp_mods[1] - # print 'loading module %s' % ofp_parser - _create_ofp_msg_ev_from_module(ofp_parser) +generate_ofp_msg_ev_classes() class EventOFPStateChange(event.EventBase): diff --git a/ryu/flags.py b/ryu/flags.py index 40dc724..0708e0c 100644 --- a/ryu/flags.py +++ b/ryu/flags.py @@ -55,6 +55,9 @@ CONF.register_cli_opts([ cfg.BoolOpt( 'explicit-drop', default=True, help='link discovery: explicitly drop lldp packet in'), + cfg.BoolOpt( + 'with-timestamp', default=False, + help='enable timestamps when receiving OpenFlow messages'), ]) # group='default' # Note: The following option should not be exposed to CLI, because these -- 2.7.4 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Ryu-devel mailing list Ryu-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ryu-devel