2012-05-02 15:21:36 -07:00
|
|
|
# Copyright (c) 2010, 2011, 2012 Nicira, Inc.
|
2010-08-25 10:26:40 -07:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at:
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
|
python: Convert dict iterators.
In Python 2, dict.items(), dict.keys(), and dict.values() returned a
list. dict.iteritems(), dict.iterkeys(), and dict.itervalues() returned
an iterator.
As of Python 3, dict.iteritems(), dict.itervalues(), and dict.iterkeys()
are gone. items(), keys(), and values() now return an iterator.
In the case where we want an iterator, we now use the six.iter*()
helpers. If we want a list, we explicitly create a list from the
iterator.
Signed-off-by: Russell Bryant <russell@ovn.org>
Acked-by: Ben Pfaff <blp@ovn.org>
2015-12-14 15:13:20 -05:00
|
|
|
import six
|
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
import ovs.poller
|
|
|
|
import ovs.socket_util
|
2011-09-24 17:53:30 -07:00
|
|
|
import ovs.vlog
|
|
|
|
|
|
|
|
vlog = ovs.vlog.Vlog("stream")
|
2010-08-25 10:26:40 -07:00
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2012-04-11 20:18:34 -07:00
|
|
|
def stream_or_pstream_needs_probes(name):
|
|
|
|
""" 1 if the stream or pstream specified by 'name' needs periodic probes to
|
2012-07-23 00:24:30 -07:00
|
|
|
verify connectivity. For [p]streams which need probes, it can take a long
|
2012-04-11 20:18:34 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
class Stream(object):
|
|
|
|
"""Bidirectional byte stream. Currently only Unix domain sockets
|
|
|
|
are implemented."""
|
|
|
|
|
|
|
|
# States.
|
|
|
|
__S_CONNECTING = 0
|
|
|
|
__S_CONNECTED = 1
|
|
|
|
__S_DISCONNECTED = 2
|
|
|
|
|
|
|
|
# Kinds of events that one might wait for.
|
|
|
|
W_CONNECT = 0 # Connect complete (success or failure).
|
|
|
|
W_RECV = 1 # Data received.
|
|
|
|
W_SEND = 2 # Send buffer room available.
|
|
|
|
|
2012-09-27 18:28:08 +09:00
|
|
|
_SOCKET_METHODS = {}
|
|
|
|
|
|
|
|
@staticmethod
|
2012-10-18 03:18:16 +09:00
|
|
|
def register_method(method, cls):
|
|
|
|
Stream._SOCKET_METHODS[method + ":"] = cls
|
2012-09-27 18:28:08 +09:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _find_method(name):
|
python: Convert dict iterators.
In Python 2, dict.items(), dict.keys(), and dict.values() returned a
list. dict.iteritems(), dict.iterkeys(), and dict.itervalues() returned
an iterator.
As of Python 3, dict.iteritems(), dict.itervalues(), and dict.iterkeys()
are gone. items(), keys(), and values() now return an iterator.
In the case where we want an iterator, we now use the six.iter*()
helpers. If we want a list, we explicitly create a list from the
iterator.
Signed-off-by: Russell Bryant <russell@ovn.org>
Acked-by: Ben Pfaff <blp@ovn.org>
2015-12-14 15:13:20 -05:00
|
|
|
for method, cls in six.iteritems(Stream._SOCKET_METHODS):
|
2012-09-27 18:28:08 +09:00
|
|
|
if name.startswith(method):
|
|
|
|
return cls
|
|
|
|
return None
|
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
@staticmethod
|
|
|
|
def is_valid_name(name):
|
|
|
|
"""Returns True if 'name' is a stream name in the form "TYPE:ARGS" and
|
2012-09-27 18:28:08 +09:00
|
|
|
TYPE is a supported stream type (currently only "unix:" and "tcp:"),
|
|
|
|
otherwise False."""
|
|
|
|
return bool(Stream._find_method(name))
|
2010-08-25 10:26:40 -07:00
|
|
|
|
2012-02-27 11:13:00 -08:00
|
|
|
def __init__(self, socket, name, status):
|
2010-08-25 10:26:40 -07:00
|
|
|
self.socket = socket
|
|
|
|
self.name = name
|
|
|
|
if status == errno.EAGAIN:
|
|
|
|
self.state = Stream.__S_CONNECTING
|
|
|
|
elif status == 0:
|
|
|
|
self.state = Stream.__S_CONNECTED
|
|
|
|
else:
|
|
|
|
self.state = Stream.__S_DISCONNECTED
|
|
|
|
|
|
|
|
self.error = 0
|
|
|
|
|
2012-09-27 18:28:08 +09:00
|
|
|
# Default value of dscp bits for connection between controller and manager.
|
|
|
|
# Value of IPTOS_PREC_INTERNETCONTROL = 0xc0 which is defined
|
|
|
|
# in <netinet/ip.h> is used.
|
|
|
|
IPTOS_PREC_INTERNETCONTROL = 0xc0
|
|
|
|
DSCP_DEFAULT = IPTOS_PREC_INTERNETCONTROL >> 2
|
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
@staticmethod
|
2012-09-27 18:28:08 +09:00
|
|
|
def open(name, dscp=DSCP_DEFAULT):
|
2010-08-25 10:26:40 -07:00
|
|
|
"""Attempts to connect a stream to a remote peer. 'name' is a
|
|
|
|
connection name in the form "TYPE:ARGS", where TYPE is an active stream
|
|
|
|
class's name and ARGS are stream class-specific. Currently the only
|
2012-09-27 18:28:08 +09:00
|
|
|
supported TYPEs are "unix" and "tcp".
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
Returns (error, stream): on success 'error' is 0 and 'stream' is the
|
|
|
|
new Stream, on failure 'error' is a positive errno value and 'stream'
|
|
|
|
is None.
|
|
|
|
|
|
|
|
Never returns errno.EAGAIN or errno.EINPROGRESS. Instead, returns 0
|
|
|
|
and a new Stream. The connect() method can be used to check for
|
|
|
|
successful connection completion."""
|
2012-09-27 18:28:08 +09:00
|
|
|
cls = Stream._find_method(name)
|
|
|
|
if not cls:
|
2010-08-25 10:26:40 -07:00
|
|
|
return errno.EAFNOSUPPORT, None
|
|
|
|
|
2012-09-27 18:28:08 +09:00
|
|
|
suffix = name.split(":", 1)[1]
|
2013-02-08 12:37:18 -08:00
|
|
|
if name.startswith("unix:"):
|
|
|
|
suffix = ovs.util.abs_file_name(ovs.dirs.RUNDIR, suffix)
|
2012-09-27 18:28:08 +09:00
|
|
|
error, sock = cls._open(suffix, dscp)
|
2010-08-25 10:26:40 -07:00
|
|
|
if error:
|
|
|
|
return error, None
|
|
|
|
else:
|
|
|
|
status = ovs.socket_util.check_connection_completion(sock)
|
2012-02-27 11:13:00 -08:00
|
|
|
return 0, Stream(sock, name, status)
|
2010-08-25 10:26:40 -07:00
|
|
|
|
2012-09-27 18:28:08 +09:00
|
|
|
@staticmethod
|
|
|
|
def _open(suffix, dscp):
|
|
|
|
raise NotImplementedError("This method must be overrided by subclass")
|
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
@staticmethod
|
2015-07-29 21:12:45 -05:00
|
|
|
def open_block(error_stream):
|
2010-08-25 10:26:40 -07:00
|
|
|
"""Blocks until a Stream completes its connection attempt, either
|
2011-08-23 10:50:47 -07:00
|
|
|
succeeding or failing. (error, stream) should be the tuple returned by
|
2010-08-25 10:26:40 -07:00
|
|
|
Stream.open(). Returns a tuple of the same form.
|
|
|
|
|
|
|
|
Typical usage:
|
2011-08-23 10:50:47 -07:00
|
|
|
error, stream = Stream.open_block(Stream.open("unix:/tmp/socket"))"""
|
2010-08-25 10:26:40 -07:00
|
|
|
|
2015-07-29 21:12:45 -05:00
|
|
|
# Py3 doesn't support tuple parameter unpacking - PEP 3113
|
|
|
|
error, stream = error_stream
|
2010-08-25 10:26:40 -07:00
|
|
|
if not error:
|
|
|
|
while True:
|
|
|
|
error = stream.connect()
|
|
|
|
if error != errno.EAGAIN:
|
|
|
|
break
|
|
|
|
stream.run()
|
|
|
|
poller = ovs.poller.Poller()
|
2012-07-02 10:34:32 -07:00
|
|
|
stream.run_wait(poller)
|
2010-08-25 10:26:40 -07:00
|
|
|
stream.connect_wait(poller)
|
|
|
|
poller.block()
|
|
|
|
assert error != errno.EINPROGRESS
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
if error and stream:
|
|
|
|
stream.close()
|
|
|
|
stream = None
|
|
|
|
return error, stream
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.socket.close()
|
|
|
|
|
|
|
|
def __scs_connecting(self):
|
|
|
|
retval = ovs.socket_util.check_connection_completion(self.socket)
|
|
|
|
assert retval != errno.EINPROGRESS
|
|
|
|
if retval == 0:
|
|
|
|
self.state = Stream.__S_CONNECTED
|
|
|
|
elif retval != errno.EAGAIN:
|
|
|
|
self.state = Stream.__S_DISCONNECTED
|
|
|
|
self.error = retval
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
"""Tries to complete the connection on this stream. If the connection
|
|
|
|
is complete, returns 0 if the connection was successful or a positive
|
|
|
|
errno value if it failed. If the connection is still in progress,
|
|
|
|
returns errno.EAGAIN."""
|
2012-11-21 22:09:55 -08:00
|
|
|
|
|
|
|
if self.state == Stream.__S_CONNECTING:
|
|
|
|
self.__scs_connecting()
|
|
|
|
|
|
|
|
if self.state == Stream.__S_CONNECTING:
|
|
|
|
return errno.EAGAIN
|
|
|
|
elif self.state == Stream.__S_CONNECTED:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
assert self.state == Stream.__S_DISCONNECTED
|
|
|
|
return self.error
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
def recv(self, n):
|
|
|
|
"""Tries to receive up to 'n' bytes from this stream. Returns a
|
|
|
|
(error, string) tuple:
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
- If successful, 'error' is zero and 'string' contains between 1
|
|
|
|
and 'n' bytes of data.
|
|
|
|
|
|
|
|
- On error, 'error' is a positive errno value.
|
|
|
|
|
|
|
|
- If the connection has been closed in the normal fashion or if 'n'
|
|
|
|
is 0, the tuple is (0, "").
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
The recv function will not block waiting for data to arrive. If no
|
|
|
|
data have been received, it returns (errno.EAGAIN, "") immediately."""
|
|
|
|
|
|
|
|
retval = self.connect()
|
|
|
|
if retval != 0:
|
|
|
|
return (retval, "")
|
|
|
|
elif n == 0:
|
|
|
|
return (0, "")
|
|
|
|
|
|
|
|
try:
|
|
|
|
return (0, self.socket.recv(n))
|
2015-07-29 21:12:45 -05:00
|
|
|
except socket.error as e:
|
2010-08-25 10:26:40 -07:00
|
|
|
return (ovs.socket_util.get_exception_errno(e), "")
|
|
|
|
|
|
|
|
def send(self, buf):
|
|
|
|
"""Tries to send 'buf' on this stream.
|
|
|
|
|
|
|
|
If successful, returns the number of bytes sent, between 1 and
|
|
|
|
len(buf). 0 is only a valid return value if len(buf) is 0.
|
|
|
|
|
|
|
|
On error, returns a negative errno value.
|
|
|
|
|
|
|
|
Will not block. If no bytes can be immediately accepted for
|
|
|
|
transmission, returns -errno.EAGAIN immediately."""
|
|
|
|
|
|
|
|
retval = self.connect()
|
|
|
|
if retval != 0:
|
|
|
|
return -retval
|
|
|
|
elif len(buf) == 0:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
try:
|
2015-12-17 09:45:58 -05:00
|
|
|
# Python 3 has separate types for strings and bytes. We must have
|
|
|
|
# bytes here.
|
2016-04-14 09:23:27 -04:00
|
|
|
if six.PY3 and not isinstance(buf, six.binary_type):
|
2015-12-17 09:45:58 -05:00
|
|
|
buf = six.binary_type(buf, 'utf-8')
|
2010-08-25 10:26:40 -07:00
|
|
|
return self.socket.send(buf)
|
2015-07-29 21:12:45 -05:00
|
|
|
except socket.error as e:
|
2010-08-25 10:26:40 -07:00
|
|
|
return -ovs.socket_util.get_exception_errno(e)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run_wait(self, poller):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def wait(self, poller, wait):
|
|
|
|
assert wait in (Stream.W_CONNECT, Stream.W_RECV, Stream.W_SEND)
|
|
|
|
|
|
|
|
if self.state == Stream.__S_DISCONNECTED:
|
|
|
|
poller.immediate_wake()
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.state == Stream.__S_CONNECTING:
|
|
|
|
wait = Stream.W_CONNECT
|
2011-08-23 11:16:57 -07:00
|
|
|
if wait == Stream.W_RECV:
|
2012-11-19 03:50:52 +09:00
|
|
|
poller.fd_wait(self.socket, ovs.poller.POLLIN)
|
2011-08-23 11:16:57 -07:00
|
|
|
else:
|
2012-11-19 03:50:52 +09:00
|
|
|
poller.fd_wait(self.socket, ovs.poller.POLLOUT)
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
def connect_wait(self, poller):
|
|
|
|
self.wait(poller, Stream.W_CONNECT)
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def recv_wait(self, poller):
|
|
|
|
self.wait(poller, Stream.W_RECV)
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def send_wait(self, poller):
|
|
|
|
self.wait(poller, Stream.W_SEND)
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def __del__(self):
|
|
|
|
# Don't delete the file: we might have forked.
|
|
|
|
self.socket.close()
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
class PassiveStream(object):
|
|
|
|
@staticmethod
|
|
|
|
def is_valid_name(name):
|
|
|
|
"""Returns True if 'name' is a passive stream name in the form
|
2016-05-18 18:29:13 +03:00
|
|
|
"TYPE:ARGS" and TYPE is a supported passive stream type (currently
|
|
|
|
"punix:" or "ptcp"), otherwise False."""
|
|
|
|
return name.startswith("punix:") | name.startswith("ptcp:")
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
def __init__(self, sock, name, bind_path):
|
|
|
|
self.name = name
|
|
|
|
self.socket = sock
|
|
|
|
self.bind_path = bind_path
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def open(name):
|
|
|
|
"""Attempts to start listening for remote stream connections. 'name'
|
|
|
|
is a connection name in the form "TYPE:ARGS", where TYPE is an passive
|
2016-05-18 18:29:13 +03:00
|
|
|
stream class's name and ARGS are stream class-specific. Currently the
|
|
|
|
supported values for TYPE are "punix" and "ptcp".
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
Returns (error, pstream): on success 'error' is 0 and 'pstream' is the
|
|
|
|
new PassiveStream, on failure 'error' is a positive errno value and
|
|
|
|
'pstream' is None."""
|
|
|
|
if not PassiveStream.is_valid_name(name):
|
|
|
|
return errno.EAFNOSUPPORT, None
|
|
|
|
|
|
|
|
bind_path = name[6:]
|
2013-02-08 12:37:18 -08:00
|
|
|
if name.startswith("punix:"):
|
|
|
|
bind_path = ovs.util.abs_file_name(ovs.dirs.RUNDIR, bind_path)
|
2016-05-18 18:29:13 +03:00
|
|
|
error, sock = ovs.socket_util.make_unix_socket(socket.SOCK_STREAM,
|
|
|
|
True, bind_path,
|
|
|
|
None)
|
|
|
|
if error:
|
|
|
|
return error, None
|
|
|
|
|
|
|
|
elif name.startswith("ptcp:"):
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
remote = name.split(':')
|
|
|
|
sock.bind((remote[1], int(remote[2])))
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise Exception('Unknown connection string')
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
sock.listen(10)
|
2015-07-29 21:12:45 -05:00
|
|
|
except socket.error as e:
|
2011-09-24 17:53:30 -07:00
|
|
|
vlog.err("%s: listen: %s" % (name, os.strerror(e.error)))
|
2010-08-25 10:26:40 -07:00
|
|
|
sock.close()
|
|
|
|
return e.error, None
|
|
|
|
|
|
|
|
return 0, PassiveStream(sock, name, bind_path)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Closes this PassiveStream."""
|
|
|
|
self.socket.close()
|
|
|
|
if self.bind_path is not None:
|
|
|
|
ovs.fatal_signal.unlink_file_now(self.bind_path)
|
|
|
|
self.bind_path = None
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
"""Tries to accept a new connection on this passive stream. Returns
|
|
|
|
(error, stream): if successful, 'error' is 0 and 'stream' is the new
|
|
|
|
Stream object, and on failure 'error' is a positive errno value and
|
|
|
|
'stream' is None.
|
|
|
|
|
|
|
|
Will not block waiting for a connection. If no connection is ready to
|
|
|
|
be accepted, returns (errno.EAGAIN, None) immediately."""
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
sock, addr = self.socket.accept()
|
|
|
|
ovs.socket_util.set_nonblocking(sock)
|
2016-05-18 18:29:13 +03:00
|
|
|
if (sock.family == socket.AF_UNIX):
|
|
|
|
return 0, Stream(sock, "unix:%s" % addr, 0)
|
|
|
|
return 0, Stream(sock, 'ptcp:%s:%s' % (addr[0],
|
|
|
|
str(addr[1])), 0)
|
2015-07-29 21:12:45 -05:00
|
|
|
except socket.error as e:
|
2010-08-25 10:26:40 -07:00
|
|
|
error = ovs.socket_util.get_exception_errno(e)
|
|
|
|
if error != errno.EAGAIN:
|
|
|
|
# XXX rate-limit
|
2011-09-24 17:53:30 -07:00
|
|
|
vlog.dbg("accept: %s" % os.strerror(error))
|
2010-08-25 10:26:40 -07:00
|
|
|
return error, None
|
|
|
|
|
|
|
|
def wait(self, poller):
|
2012-11-19 03:50:52 +09:00
|
|
|
poller.fd_wait(self.socket, ovs.poller.POLLIN)
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
# Don't delete the file: we might have forked.
|
|
|
|
self.socket.close()
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2011-09-26 16:02:14 -07:00
|
|
|
def usage(name):
|
|
|
|
return """
|
|
|
|
Active %s connection methods:
|
|
|
|
unix:FILE Unix domain socket named FILE
|
2012-09-27 18:28:08 +09:00
|
|
|
tcp:IP:PORT TCP socket to IP with port no of PORT
|
2011-09-26 16:02:14 -07:00
|
|
|
|
|
|
|
Passive %s connection methods:
|
|
|
|
punix:FILE Listen on Unix domain socket FILE""" % (name, name)
|
2012-09-27 18:28:08 +09:00
|
|
|
|
|
|
|
|
|
|
|
class UnixStream(Stream):
|
|
|
|
@staticmethod
|
|
|
|
def _open(suffix, dscp):
|
|
|
|
connect_path = suffix
|
2016-01-05 18:16:20 -05:00
|
|
|
return ovs.socket_util.make_unix_socket(socket.SOCK_STREAM,
|
|
|
|
True, None, connect_path)
|
2012-10-18 03:18:16 +09:00
|
|
|
Stream.register_method("unix", UnixStream)
|
2012-09-27 18:28:08 +09:00
|
|
|
|
|
|
|
|
|
|
|
class TCPStream(Stream):
|
|
|
|
@staticmethod
|
|
|
|
def _open(suffix, dscp):
|
|
|
|
error, sock = ovs.socket_util.inet_open_active(socket.SOCK_STREAM,
|
|
|
|
suffix, 0, dscp)
|
|
|
|
if not error:
|
|
|
|
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
|
|
|
return error, sock
|
2012-10-18 03:18:16 +09:00
|
|
|
Stream.register_method("tcp", TCPStream)
|