-# Copyright (c) 2010, 2011 Nicira Networks
+# Copyright (c) 2010, 2011, 2012 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# limitations under the License.
import errno
-import logging
import os
import select
import socket
import ovs.poller
import ovs.socket_util
+import ovs.vlog
+
+vlog = ovs.vlog.Vlog("stream")
+
+
+def stream_or_pstream_needs_probes(name):
+ """ 1 if the stream or pstream specified by 'name' needs periodic probes to
+ verify connectivity. For [p]streams which need probes, it can take a long
+ time to notice the connection was dropped. Returns 0 if probes aren't
+ needed, and -1 if 'name' is invalid"""
+
+ if PassiveStream.is_valid_name(name) or Stream.is_valid_name(name):
+ # Only unix and punix are supported currently.
+ return 0
+ else:
+ return -1
class Stream(object):
"""Bidirectional byte stream. Currently only Unix domain sockets
are implemented."""
- n_unix_sockets = 0
# States.
__S_CONNECTING = 0
False."""
return name.startswith("unix:")
- def __init__(self, socket, name, bind_path, status):
+ def __init__(self, socket, name, status):
self.socket = socket
self.name = name
- self.bind_path = bind_path
if status == errno.EAGAIN:
self.state = Stream.__S_CONNECTING
elif status == 0:
if not Stream.is_valid_name(name):
return errno.EAFNOSUPPORT, None
- Stream.n_unix_sockets += 1
- bind_path = "/tmp/stream-unix.%d.%d" % (os.getpid(),
- Stream.n_unix_sockets)
connect_path = name[5:]
error, sock = ovs.socket_util.make_unix_socket(socket.SOCK_STREAM,
- True, bind_path,
+ True, None,
connect_path)
if error:
return error, None
else:
status = ovs.socket_util.check_connection_completion(sock)
- return 0, Stream(sock, name, bind_path, status)
+ return 0, Stream(sock, name, status)
@staticmethod
def open_block((error, stream)):
break
stream.run()
poller = ovs.poller.Poller()
- stream.run_wait()
+ stream.run_wait(poller)
stream.connect_wait(poller)
poller.block()
assert error != errno.EINPROGRESS
def close(self):
self.socket.close()
- if self.bind_path is not None:
- ovs.fatal_signal.unlink_file_now(self.bind_path)
- self.bind_path = None
def __scs_connecting(self):
retval = ovs.socket_util.check_connection_completion(self.socket)
try:
sock.listen(10)
except socket.error, e:
- logging.error("%s: listen: %s" % (name, os.strerror(e.error)))
+ vlog.err("%s: listen: %s" % (name, os.strerror(e.error)))
sock.close()
return e.error, None
try:
sock, addr = self.socket.accept()
ovs.socket_util.set_nonblocking(sock)
- return 0, Stream(sock, "unix:%s" % addr, None, 0)
+ return 0, Stream(sock, "unix:%s" % addr, 0)
except socket.error, e:
error = ovs.socket_util.get_exception_errno(e)
if error != errno.EAGAIN:
# XXX rate-limit
- logging.debug("accept: %s" % os.strerror(error))
+ vlog.dbg("accept: %s" % os.strerror(error))
return error, None
def wait(self, poller):