Beispiel #1
0
def main(args):
    def load_format_file(format_file):
        if format_file.endswith('.py'):
            format_file = format_file[:-3].replace("/", ".")
        config = __import__(format_file, globals(), locals(), ["*"])
        return config

    if args.format_file is not None:
        format_def = load_format_file(args.format_file)
    else:
        format_def = object()

    dp_trace = None
    if args.dp_trace_path is not None:
        dp_trace = Trace(args.dp_trace_path).dataplane_trace

    if hasattr(format_def, "fields"):
        fields = format_def.fields
    else:
        fields = default_fields

    for field in fields:
        if field not in field_formatters:
            raise ValueError("unknown field %s" % field)

    if hasattr(format_def, "filtered_classes"):
        filtered_classes = format_def.filtered_classes
    else:
        filtered_classes = default_filtered_classes

    stats = Stats()

    # all events are printed with a fixed number of lines, and (optionally)
    # separated by delimiter lines of the form:
    # ----------------------------------
    with open(args.input) as input_file:
        trace = parse(input_file)
        for event in trace:
            if type(event) not in filtered_classes:
                if dp_trace is not None and type(
                        event) == replay_events.TrafficInjection:
                    event.dp_event = dp_trace.pop(0)
                for field in fields:
                    field_formatters[field](event)
                stats.update(event)

        if check_for_violation_signature(trace, args.violation_signature):
            print "Violation occurs at end of trace: %s" % args.violation_signature
        elif args.violation_signature is not None:
            print("Violation does not occur at end of trace: %s",
                  args.violation_signature)
        print

    if args.stats:
        print "Stats: %s" % stats
def main(args):
  def load_format_file(format_file):
    if format_file.endswith('.py'):
      format_file = format_file[:-3].replace("/", ".")
    config = __import__(format_file, globals(), locals(), ["*"])
    return config

  if args.format_file is not None:
    format_def = load_format_file(args.format_file)
  else:
    format_def = object()

  dp_trace = None
  if args.dp_trace_path is not None:
    dp_trace = Trace(args.dp_trace_path).dataplane_trace

  if hasattr(format_def, "fields"):
    fields = format_def.fields
  else:
    fields = default_fields

  for field in fields:
    if field not in field_formatters:
      raise ValueError("unknown field %s" % field)

  if hasattr(format_def, "filtered_classes"):
    filtered_classes = format_def.filtered_classes
  else:
    filtered_classes = default_filtered_classes

  stats = Stats()

  # all events are printed with a fixed number of lines, and (optionally)
  # separated by delimiter lines of the form:
  # ----------------------------------
  with open(args.input) as input_file:
    trace = parse(input_file)
    for event in trace:
      if type(event) not in filtered_classes:
        if dp_trace is not None and type(event) == replay_events.TrafficInjection:
          event.dp_event = dp_trace.pop(0)
        for field in fields:
          field_formatters[field](event)
        stats.update(event)

    if check_for_violation_signature(trace, args.violation_signature):
      print "Violation occurs at end of trace: %s" % args.violation_signature
    elif args.violation_signature is not None:
      print ("Violation does not occur at end of trace: %s",
             args.violation_signature)
    print

  if args.stats:
    print "Stats: %s" % stats
def main(args):
  if args.dp_trace_path is None:
    args.dp_trace_path = os.path.dirname(args.input) + "/dataplane.trace"

  dp_trace = Trace(args.dp_trace_path).dataplane_trace

  event_logger = InputLogger()
  event_logger.open(results_dir="/tmp/events.trace")

  with open(args.input) as input_file:
    trace = parse(input_file)
    for event in trace:
      if type(event) == replay_events.TrafficInjection:
        event.dp_event = dp_trace.pop(0)
      event_logger.log_input_event(event)

    event_logger.output.close()
Beispiel #4
0
def main(args):
    if args.dp_trace_path is None:
        args.dp_trace_path = os.path.dirname(args.input) + "/dataplane.trace"

    dp_trace = Trace(args.dp_trace_path).dataplane_trace

    event_logger = InputLogger()
    event_logger.open(results_dir="/tmp/events.trace")

    with open(args.input) as input_file:
        trace = parse(input_file)
        for event in trace:
            if type(event) == replay_events.TrafficInjection:
                event.dp_event = dp_trace.pop(0)
            event_logger.log_input_event(event)

        event_logger.output.close()
Beispiel #5
0
    def bootstrap(self,
                  sync_callback,
                  boot_controllers=default_boot_controllers):
        '''Return a simulation object encapsulating the state of
       the system in its initial starting point:
       - boots controllers
       - connects switches to controllers

       May be invoked multiple times!
    '''
        def remove_monkey_patch():
            if hasattr(select, "_old_select"):
                # Revert the previous monkeypatch to allow the new true_sockets to
                # connect
                select.select = select._old_select
                socket.socket = socket._old_socket

        def initialize_io_loop():
            ''' boot the IOLoop (needed for the controllers) '''
            _io_master = IOMaster()
            # monkey patch time.sleep for all our friends
            _io_master.monkey_time_sleep()
            # tell sts.console to use our io_master
            msg.set_io_master(_io_master)
            return _io_master

        def wire_controller_patch_panel(controller_manager, create_io_worker):
            patch_panel = None
            if not self.interpose_on_controllers:
                return patch_panel
            # N.B. includes local controllers in network namespaces or VMs.
            remote_controllers = controller_manager.remote_controllers
            if len(remote_controllers) != 0:
                patch_panel = self.controller_patch_panel_class(
                    create_io_worker)
                for c in remote_controllers:
                    patch_panel.register_controller(c.cid, c.guest_eth_addr,
                                                    c.host_device)
            return patch_panel

        def instantiate_topology(create_io_worker):
            '''construct a clean topology object from topology_class and
      topology_params'''
            log.info("Creating topology...")
            # If you want to shoot yourself in the foot, feel free :)
            comma = "" if self._topology_params == "" else ","
            topology = eval(
                "%s(%s%screate_io_worker=create_io_worker)" %
                (self._topology_class.__name__, self._topology_params, comma))
            return topology

        # Instantiate the pieces needed for Simulation's constructor
        remove_monkey_patch()
        io_master = initialize_io_loop()
        sync_connection_manager = STSSyncConnectionManager(
            io_master, sync_callback)
        controller_manager = boot_controllers(self.controller_configs,
                                              self.snapshot_service,
                                              sync_connection_manager)
        controller_patch_panel = wire_controller_patch_panel(
            controller_manager, io_master.create_worker_for_socket)
        topology = instantiate_topology(io_master.create_worker_for_socket)
        patch_panel = self._patch_panel_class(topology.switches,
                                              topology.hosts,
                                              topology.get_connected_port)
        openflow_buffer = OpenFlowBuffer()
        dataplane_trace = None
        if self._dataplane_trace_path is not None:
            dataplane_trace = Trace(self._dataplane_trace_path, topology)
        if self._violation_persistence_threshold is not None:
            violation_tracker = ViolationTracker(
                self._violation_persistence_threshold)
        else:
            violation_tracker = ViolationTracker()

        simulation = Simulation(topology, controller_manager, dataplane_trace,
                                openflow_buffer, io_master,
                                controller_patch_panel, patch_panel,
                                sync_callback, self.multiplex_sockets,
                                violation_tracker,
                                self._kill_controllers_on_exit)
        self.current_simulation = simulation
        return simulation
Beispiel #6
0
    def bootstrap(self, sync_callback):
        '''Return a simulation object encapsulating the state of
       the system in its initial starting point:
       - boots controllers
       - connects switches to controllers

       May be invoked multiple times!

       Optional parameter:
       - switch_init_sleep_seconds: an integer, sleep time in seconds, to wait
             for switches to initialize their TCP connections with the
             controller(s). Defaults to False
    '''
        def initialize_io_loop():
            ''' boot the IOLoop (needed for the controllers) '''
            _io_master = IOMaster()
            # monkey patch time.sleep for all our friends
            _io_master.monkey_time_sleep()
            # tell sts.console to use our io_master
            msg.set_io_master(_io_master)
            return _io_master

        def boot_controllers(sync_connection_manager):
            # Boot the controllers
            controllers = []
            for c in self.controller_configs:
                controller = Controller(c, sync_connection_manager,
                                        self.snapshot_service)
                controller.start()
                log.info("Launched controller c%s: %s [PID %d]" % (str(
                    c.uuid), " ".join(c.expanded_cmdline), controller.pid))
                controllers.append(controller)
            return ControllerManager(controllers)

        def instantiate_topology():
            '''construct a clean topology object from topology_class and
      topology_params'''
            # If you want to shoot yourself in the foot, feel free :)
            topology = eval(
                "%s(%s)" %
                (self._topology_class.__name__, self._topology_params))
            return topology

        # Instantiate the pieces needed for Simulation's constructor
        io_master = initialize_io_loop()
        sync_connection_manager = STSSyncConnectionManager(
            io_master, sync_callback)
        controller_manager = boot_controllers(sync_connection_manager)
        topology = instantiate_topology()
        patch_panel = self._patch_panel_class(topology.switches,
                                              topology.hosts,
                                              topology.get_connected_port)
        god_scheduler = GodScheduler()
        dataplane_trace = None
        if self._dataplane_trace_path is not None:
            dataplane_trace = Trace(self._dataplane_trace_path, topology)

        simulation = Simulation(topology, controller_manager, dataplane_trace,
                                god_scheduler, io_master, patch_panel,
                                sync_callback)
        self.current_simulation = simulation

        # Connect to controllers
        # TODO(cs): somewhat hacky that we do this after instantiating Simulation
        def create_connection(controller_info, switch):
            ''' Connect switches to controllers. May raise a TimeoutError '''
            # TODO(cs): move this into a ConnectionFactory class
            socket = connect_socket_with_backoff(controller_info.address,
                                                 controller_info.port,
                                                 max_backoff_seconds=8)
            # Set non-blocking
            socket.setblocking(0)
            io_worker = DeferredIOWorker(
                io_master.create_worker_for_socket(socket))
            return DeferredOFConnection(io_worker, switch.dpid, god_scheduler)

        if self.switch_init_sleep_seconds:
            simulation.set_pass_through()

        # TODO(cs): this should block until all switches have finished
        # initializing with the controller
        topology.connect_to_controllers(self.controller_configs,
                                        create_connection=create_connection)

        if self.switch_init_sleep_seconds:
            log.debug("Waiting %f seconds for switch initialization" %
                      self.switch_init_sleep_seconds)
            time.sleep(self.switch_init_sleep_seconds)

        # Now unset pass-through mode
        if self.switch_init_sleep_seconds:
            simulation.unset_pass_through()

        return simulation
Beispiel #7
0
  def bootstrap(self, sync_callback=None, boot_controllers=default_boot_controllers):
    '''Return a simulation object encapsulating the state of
       the system in its initial starting point:
       - boots controllers
       - connects switches to controllers

       May be invoked multiple times!
    '''
    if sync_callback is None:
      sync_callback = ReplaySyncCallback(None)

    def initialize_io_loop():
      ''' boot the IOLoop (needed for the controllers) '''
      _io_master = IOMaster()
      # monkey patch time.sleep for all our friends
      _io_master.monkey_time_sleep()
      # tell sts.console to use our io_master
      msg.set_io_master(_io_master)
      return _io_master

    def wire_controller_patch_panel(controller_manager, create_io_worker):
      patch_panel = None
      if not self.interpose_on_controllers:
        return patch_panel
      # N.B. includes local controllers in network namespaces or VMs.
      remote_controllers = controller_manager.remote_controllers
      if len(remote_controllers) != 0:
        patch_panel = self.controller_patch_panel_class(create_io_worker)
        for c in remote_controllers:
          patch_panel.register_controller(c.cid, c.guest_eth_addr, c.host_device)
      return patch_panel

    def instantiate_topology(create_io_worker):
      '''construct a clean topology object from topology_class and
      topology_params'''
      log.info("Creating topology...")
      # If you want to shoot yourself in the foot, feel free :)
      comma = "" if self._topology_params == "" else ","
      topology = eval("%s(%s%screate_io_worker=create_io_worker)" %
                      (self._topology_class.__name__,
                       self._topology_params, comma))
      return topology

    def monkeypatch_select(multiplex_sockets, controller_manager):
      mux_select = None
      demuxers = []
      if multiplex_sockets:
        log.debug("Monkeypatching STS select")
        revert_select_monkeypatch()
        # Monkey patch select to use our deterministic version
        mux_select = MultiplexedSelect()
        for c in controller_manager.controller_configs:
          # Connect the true sockets
          true_socket = connect_socket_with_backoff(address=c.address, port=c.port)
          true_socket.setblocking(0)
          io_worker = mux_select.create_worker_for_socket(true_socket)
          demux = STSSocketDemultiplexer(io_worker, c.server_info)
          demuxers.append(demux)

        # Monkey patch select.select
        select._old_select = select.select
        select.select = mux_select.select

      return (mux_select, demuxers)

    # Instantiate the pieces needed for Simulation's constructor
    revert_select_monkeypatch()
    io_master = initialize_io_loop()
    sync_connection_manager = STSSyncConnectionManager(io_master,
                                                       sync_callback)
    controller_manager = boot_controllers(self.controller_configs,
                                          self.snapshot_service,
                                          sync_connection_manager,
                                          multiplex_sockets=self.multiplex_sockets)
    controller_patch_panel = wire_controller_patch_panel(controller_manager,
                                                         io_master.create_worker_for_socket)
    topology = instantiate_topology(io_master.create_worker_for_socket)
    patch_panel = self._patch_panel_class(topology.switches, topology.hosts,
                                          topology.get_connected_port)
    openflow_buffer = OpenFlowBuffer()
    dataplane_trace = None
    if self._dataplane_trace_path is not None:
      dataplane_trace = Trace(self._dataplane_trace_path, topology)
    if self._violation_persistence_threshold is not None:
      violation_tracker = ViolationTracker(self._violation_persistence_threshold)
    else:
      violation_tracker = ViolationTracker()

    # Connect up MuxSelect if enabled
    (mux_select, demuxers) = monkeypatch_select(self.multiplex_sockets,
                                                controller_manager)

    simulation = Simulation(topology, controller_manager, dataplane_trace,
                            openflow_buffer, io_master, controller_patch_panel,
                            patch_panel, sync_callback, mux_select, demuxers,
                            violation_tracker, self._kill_controllers_on_exit)
    if self.ignore_interposition:
      simulation.set_pass_through()
    self.current_simulation = simulation
    return simulation