Ejemplo n.º 1
0
        def monkeypatch_select():
            log.debug("Monkeypatching STS select")
            mux_select = None
            demuxers = []
            if self.multiplex_sockets:
                revert_select_monkeypatch()
                revert_socket_monkeypatch()
                # Monkey patch select to use our deterministic version
                mux_select = MultiplexedSelect()
                for c in self.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)
                    mux_select.set_true_io_worker(io_worker)
                    demux = STSSocketDemultiplexer(io_worker, c.server_info)
                    demuxers.append(demux)

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

                def socket_patch(protocol, sock_type):
                    if sock_type == socket.SOCK_STREAM:
                        return STSMockSocket(protocol, sock_type)
                    else:
                        socket._old_socket(protocol, sock_type)

                socket.socket = socket_patch

            return (mux_select, demuxers)
Ejemplo n.º 2
0
    def monkeypatch_select():
      log.debug("Monkeypatching STS select")
      mux_select = None
      demuxers = []
      if self.multiplex_sockets:
        revert_select_monkeypatch()
        revert_socket_monkeypatch()
        # Monkey patch select to use our deterministic version
        mux_select = MultiplexedSelect()
        for c in self.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)
          mux_select.set_true_io_worker(io_worker)
          demux = STSSocketDemultiplexer(io_worker, c.server_info)
          demuxers.append(demux)

        # Monkey patch select.select
        select._old_select = select.select
        select.select = mux_select.select
        # Monkey patch socket.socket
        socket._old_socket = socket.socket
        def socket_patch(protocol, sock_type):
          if sock_type == socket.SOCK_STREAM:
            return STSMockSocket(protocol, sock_type)
          else:
            socket._old_socket(protocol, sock_type)
        socket.socket = socket_patch

      return (mux_select, demuxers)
Ejemplo n.º 3
0
    def fetchSnapshot(self, controller):
        from pox.lib.util import connect_socket_with_backoff
        import socket
        snapshotSocket = connect_socket_with_backoff('127.0.0.1', self.port)
        log.debug("Sending Request")
        snapshotSocket.send("{\"hello\":\"nommessenger\"}")
        snapshotSocket.send("{\"getnom\":0}", socket.MSG_WAITALL)
        log.debug("Receiving Results")
        jsonstr = ""
        while True:
            data = snapshotSocket.recv(1024)
            log.debug("%d byte packet received" % len(data))
            if not data: break
            jsonstr += data
            if len(data) != 1024: break
        snapshotSocket.close()

        jsonNOM = json.loads(jsonstr)  # (json string with the NOM)

        # Update local Snapshot object
        self.snapshot.switches = [
            self.myNOMDecoder.decode(s) for s in jsonNOM["switches"]
        ]
        self.snapshot.hosts = [
            self.myNOMDecoder.decode(h) for h in jsonNOM["hosts"]
        ]
        self.snapshot.links = [
            self.myNOMDecoder.decode(l) for l in jsonNOM["links"]
        ]
        self.snapshot.time = time.time()

        return self.snapshot
Ejemplo n.º 4
0
  def fetchSnapshot(self, controller):
    from pox.lib.util import connect_socket_with_backoff
    import socket
    snapshotSocket = connect_socket_with_backoff('127.0.0.1', self.port)
    log.debug("Sending Request")
    snapshotSocket.send("{\"hello\":\"nommessenger\"}")
    snapshotSocket.send("{\"getnom\":0}", socket.MSG_WAITALL)
    log.debug("Receiving Results")
    jsonstr = ""
    while True:
      data = snapshotSocket.recv(1024)
      log.debug("%d byte packet received" % len(data))
      if not data: break
      jsonstr += data
      if len(data) != 1024: break
    snapshotSocket.close()

    jsonNOM = json.loads(jsonstr) # (json string with the NOM)

    # Update local Snapshot object
    self.snapshot.switches = [self.myNOMDecoder.decode(s) for s in jsonNOM["switches"]]
    self.snapshot.hosts = [self.myNOMDecoder.decode(h) for h in jsonNOM["hosts"]]
    self.snapshot.links = [self.myNOMDecoder.decode(l) for l in jsonNOM["links"]]
    self.snapshot.time = time.time()

    return self.snapshot
Ejemplo n.º 5
0
  def connect(self, io_master):
    if self.mode != "tcp":
      raise RuntimeError("only tcp (active) mode supported by now")

    socket = connect_socket_with_backoff(self.host, self.port)
    self.io_delegate = SyncIODelegate(io_master, socket)
    self.speaker = STSSyncProtocolSpeaker(controller=self.controller,
        state_master=self.state_master, io_delegate=self.io_delegate)
Ejemplo n.º 6
0
    def connect(self, io_master):
        if self.mode != "tcp":
            raise RuntimeError("only tcp (active) mode supported by now")

        socket = connect_socket_with_backoff(self.host, self.port)
        self.io_delegate = SyncIODelegate(io_master, socket)
        self.speaker = STSSyncProtocolSpeaker(controller=self.controller,
                                              state_master=self.state_master,
                                              io_delegate=self.io_delegate)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
 def setup_client(self, num_socks, address):
   from pox.lib.util import connect_socket_with_backoff
   io_master = MultiplexedSelect()
   socket = connect_socket_with_backoff(address=address)
   io_worker = io_master.create_worker_for_socket(socket)
   io_master.set_true_io_worker(io_worker)
   demux = STSSocketDemultiplexer(io_worker, address)
   mock_socks = []
   for i in xrange(num_socks):
     mock_socket = STSMockSocket(None, None)
     mock_socket.connect(address)
     mock_socket.send(self.client_messages[i])
     mock_socks.append(mock_socket)
   io_master.select(mock_socks, mock_socks, [])
Ejemplo n.º 10
0
 def create_connection(controller_info, switch):
   ''' Connect switches to controllers. May raise a TimeoutError '''
   while controller_info.address == "__address__":
     log.debug("Waiting for controller address for %s..." % controller_info.label)
     time.sleep(5)
   # TODO(cs): move this into a ConnectionFactory class
   socket = connect_socket_with_backoff(controller_info.address,
                                        controller_info.port,
                                        max_backoff_seconds=1024)
   # Set non-blocking
   socket.setblocking(0)
   io_worker = DeferredIOWorker(self.io_master.create_worker_for_socket(socket))
   connection = DeferredOFConnection(io_worker, controller_info.cid, switch.dpid, self.openflow_buffer)
   return connection
Ejemplo n.º 11
0
 def __init__(self, create_io_worker):
   super(OVSControllerPatchPanel, self).__init__(create_io_worker)
   # Boot up POX.
   args = str("""./pox.py openflow.of_01 --address=127.0.0.1 --port=%d"""
              """         messenger.messenger --tcp_address=127.0.0.1 --tcp_port=%d """
              """         sts_of_forwarder.sts_of_forwarder """ %
              (self.of_port, self.messenger_port)).split()
   self.pox = subprocess.Popen(args, cwd="./pox/", preexec_fn=lambda: os.setsid())
   # Establish connection with POX messenger component.
   true_socket = connect_socket_with_backoff(address="127.0.0.1",
                                             port=self.messenger_port)
   self.json_worker = JSONIOWorker(create_io_worker(true_socket))
   # Send the handshake.
   self.json_worker.send({"sts_connection": ""})
   self._initialize_switch()
Ejemplo n.º 12
0
 def setup_client(self, num_socks, address):
     from pox.lib.util import connect_socket_with_backoff
     io_master = MultiplexedSelect()
     socket = connect_socket_with_backoff(address=address)
     io_worker = io_master.create_worker_for_socket(socket)
     io_master.set_true_io_worker(io_worker)
     # TODO(cs): unused variable demux
     demux = STSSocketDemultiplexer(io_worker, address)
     mock_socks = []
     for i in xrange(num_socks):
         mock_socket = STSMockSocket(None, None)
         mock_socket.connect(address)
         mock_socket.send(self.client_messages[i])
         mock_socks.append(mock_socket)
     io_master.select(mock_socks, mock_socks, [])
Ejemplo n.º 13
0
 def __init__(self, create_io_worker):
     super(OVSControllerPatchPanel, self).__init__(create_io_worker)
     # Boot up POX.
     args = str(
         """./pox.py openflow.of_01 --address=127.0.0.1 --port=%d"""
         """         messenger.messenger --tcp_address=127.0.0.1 --tcp_port=%d """
         """         sts_of_forwarder.sts_of_forwarder """ %
         (self.of_port, self.messenger_port)).split()
     self.pox = subprocess.Popen(args,
                                 cwd="./pox/",
                                 preexec_fn=lambda: os.setsid())
     # Establish connection with POX messenger component.
     true_socket = connect_socket_with_backoff(address="127.0.0.1",
                                               port=self.messenger_port)
     self.json_worker = JSONIOWorker(create_io_worker(true_socket))
     # Send the handshake.
     self.json_worker.send({"sts_connection": ""})
     self._initialize_switch()
Ejemplo n.º 14
0
  def snapshot_proceed(self):
    """
    Tell the previously fork()ed controller process to wake up, and connect
    a new socket to the fork()ed controller's (OpenFlow) port. Also
    de-registers the old controller process and registers the new controller
    process.

    Note that it is the responsibility of the caller to kill the previously
    fork()ed controller process.

    This method may block if the fork()ed process is not ready to proceed.

    Returns: a new socket connected to the woken controller.

    Pre: snapshot() has been invoked
    """
    self.log.info("Initiating snapshot proceed")
    # Check that the fork()ed controller is ready
    self.log.debug("Checking READY")
    # N.B. snapshot_socket is blocking
    response = self.snapshot_socket.recv(100)
    match = re.match(r"READY (?P<pid>\d+)", response)
    if not match:
      raise ValueError("Unknown response %s" % response)
    pid = int(match.group('pid'))

    # De-registers the old controller process and registers the new controller
    # process.
    self._unregister_proc(self.process)
    self.process = SnapshotPopen(pid)
    self._register_proc(self.process)

    # Send PROCEED
    self.log.debug("Sending PROCEED")
    self.snapshot_socket.send("PROCEED")

    # Reconnect
    self.log.debug("Connecting new mux socket")
    true_socket = connect_socket_with_backoff(address=self.config.address,
                                              port=self.config.port)
    true_socket.setblocking(0)
    self.log.debug("Finished snapshot proceed")
    return true_socket
Ejemplo n.º 15
0
    def snapshot_proceed(self):
        """
    Tell the previously fork()ed controller process to wake up, and connect
    a new socket to the fork()ed controller's (OpenFlow) port. Also
    de-registers the old controller process and registers the new controller
    process.

    Note that it is the responsibility of the caller to kill the previously
    fork()ed controller process.

    This method may block if the fork()ed process is not ready to proceed.

    Returns: a new socket connected to the woken controller.

    Pre: snapshot() has been invoked
    """
        self.log.info("Initiating snapshot proceed")
        # Check that the fork()ed controller is ready
        self.log.debug("Checking READY")
        # N.B. snapshot_socket is blocking
        response = self.snapshot_socket.recv(100)
        match = re.match(r"READY (?P<pid>\d+)", response)
        if not match:
            raise ValueError("Unknown response %s" % response)
        pid = int(match.group('pid'))

        # De-registers the old controller process and registers the new controller
        # process.
        self._unregister_proc(self.process)
        self.process = SnapshotPopen(pid)
        self._register_proc(self.process)

        # Send PROCEED
        self.log.debug("Sending PROCEED")
        self.snapshot_socket.send("PROCEED")

        # Reconnect
        self.log.debug("Connecting new mux socket")
        true_socket = connect_socket_with_backoff(address=self.config.address,
                                                  port=self.config.port)
        true_socket.setblocking(0)
        self.log.debug("Finished snapshot proceed")
        return true_socket
Ejemplo n.º 16
0
 def create_connection(self, controller_info, switch, max_backoff_seconds=1024):
   ''' Connect switches to controllers. May raise a TimeoutError '''
   # TODO(cs): move this into a ConnectionFactory class
   while controller_info.address == "__address__":
     log.debug("Waiting for controller address for %s..." % controller_info.label)
     time.sleep(5)
   if self.multiplex_sockets:
     socket_ctor = STSMockSocket
   else:
     socket_ctor = socket.socket
   sock = connect_socket_with_backoff(controller_info.address,
                                      controller_info.port,
                                      max_backoff_seconds=max_backoff_seconds,
                                      socket_ctor=socket_ctor)
   # Set non-blocking
   sock.setblocking(0)
   io_worker = DeferredIOWorker(self.io_master.create_worker_for_socket(sock))
   connection = DeferredOFConnection(io_worker, controller_info.cid, switch.dpid, self.openflow_buffer)
   return connection
Ejemplo n.º 17
0
 def setup_client(self, num_socks, address):
   try:
     from pox.lib.util import connect_socket_with_backoff
     io_master = MultiplexedSelect()
     socket = connect_socket_with_backoff(address=address)
     io_worker = io_master.create_worker_for_socket(socket)
     # TODO(cs): unused variable demux
     demux = STSSocketDemultiplexer(io_worker, address)
     mock_socks = []
     for i in xrange(num_socks):
       mock_socket = STSMockSocket(None, None)
       mock_socket.connect(address)
       mock_socket.send(self.client_messages[i])
       mock_socks.append(mock_socket)
     # Now flush messages
     while [ m for m in mock_socks if m.json_worker.io_worker._ready_to_send ] != []:
       io_master.select(mock_socks, mock_socks, [])
   except Exception as e:
     log.critical("Client died: %s" % e)
     raise e
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
    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)
Ejemplo n.º 20
0
 def setup_client(self, num_socks, address):
     try:
         from pox.lib.util import connect_socket_with_backoff
         io_master = MultiplexedSelect()
         socket = connect_socket_with_backoff(address=address)
         io_worker = io_master.create_worker_for_socket(socket)
         # TODO(cs): unused variable demux
         demux = STSSocketDemultiplexer(io_worker, address)
         mock_socks = []
         for i in xrange(num_socks):
             mock_socket = STSMockSocket(None, None)
             mock_socket.connect(address)
             mock_socket.send(self.client_messages[i])
             mock_socks.append(mock_socket)
         # Now flush messages
         while [
                 m for m in mock_socks
                 if m.json_worker.io_worker._ready_to_send
         ] != []:
             io_master.select(mock_socks, mock_socks, [])
     except Exception as e:
         log.critical("Client died: %s" % e)
         raise e
Ejemplo n.º 21
0
 def create_io_worker(switch, controller_info):
   controller_socket = connect_socket_with_backoff(controller_info.address, controller_info.port)
   # Set non-blocking
   controller_socket.setblocking(0)
   return io_worker_generator(controller_socket)
Ejemplo n.º 22
0
 def _check_snapshot_connect(self):
   if getattr(self.config, "snapshot_address", None):
     # N.B. snapshot_socket is intended to be blocking
     self.log.debug("Connecting snapshot socket")
     self.snapshot_socket = connect_socket_with_backoff(
       address=self.config.snapshot_address)
Ejemplo n.º 23
0
 def _check_snapshot_connect(self):
     if getattr(self.config, "snapshot_address", None):
         # N.B. snapshot_socket is intended to be blocking
         self.log.debug("Connecting snapshot socket")
         self.snapshot_socket = connect_socket_with_backoff(
             address=self.config.snapshot_address)