Merge branch 'master' of ssh://github.com/openvswitch/ovs into master_new
[cascardo/ovs.git] / python / ovs / socket_util.py
1 # Copyright (c) 2010, 2012, 2014, 2015 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import errno
16 import os
17 import os.path
18 import random
19 import socket
20 import sys
21
22 import ovs.fatal_signal
23 import ovs.poller
24 import ovs.vlog
25
26 vlog = ovs.vlog.Vlog("socket_util")
27
28
29 def make_short_name(long_name):
30     if long_name is None:
31         return None
32     long_name = os.path.abspath(long_name)
33     long_dirname = os.path.dirname(long_name)
34     tmpdir = os.getenv('TMPDIR', '/tmp')
35     for x in xrange(0, 1000):
36         link_name = \
37             '%s/ovs-un-py-%d-%d' % (tmpdir, random.randint(0, 10000), x)
38         try:
39             os.symlink(long_dirname, link_name)
40             ovs.fatal_signal.add_file_to_unlink(link_name)
41             return os.path.join(link_name, os.path.basename(long_name))
42         except OSError as e:
43             if e.errno != errno.EEXIST:
44                 break
45     raise Exception("Failed to create temporary symlink")
46
47
48 def free_short_name(short_name):
49     if short_name is None:
50         return
51     link_name = os.path.dirname(short_name)
52     ovs.fatal_signal.unlink_file_now(link_name)
53
54
55 def make_unix_socket(style, nonblock, bind_path, connect_path, short=False):
56     """Creates a Unix domain socket in the given 'style' (either
57     socket.SOCK_DGRAM or socket.SOCK_STREAM) that is bound to 'bind_path' (if
58     'bind_path' is not None) and connected to 'connect_path' (if 'connect_path'
59     is not None).  If 'nonblock' is true, the socket is made non-blocking.
60
61     Returns (error, socket): on success 'error' is 0 and 'socket' is a new
62     socket object, on failure 'error' is a positive errno value and 'socket' is
63     None."""
64
65     try:
66         sock = socket.socket(socket.AF_UNIX, style)
67     except socket.error as e:
68         return get_exception_errno(e), None
69
70     try:
71         if nonblock:
72             set_nonblocking(sock)
73         if bind_path is not None:
74             # Delete bind_path but ignore ENOENT.
75             try:
76                 os.unlink(bind_path)
77             except OSError as e:
78                 if e.errno != errno.ENOENT:
79                     return e.errno, None
80
81             ovs.fatal_signal.add_file_to_unlink(bind_path)
82             sock.bind(bind_path)
83
84             try:
85                 if sys.hexversion >= 0x02060000:
86                     os.fchmod(sock.fileno(), 0o700)
87                 else:
88                     os.chmod("/dev/fd/%d" % sock.fileno(), 0o700)
89             except OSError as e:
90                 pass
91         if connect_path is not None:
92             try:
93                 sock.connect(connect_path)
94             except socket.error as e:
95                 if get_exception_errno(e) != errno.EINPROGRESS:
96                     raise
97         return 0, sock
98     except socket.error as e:
99         sock.close()
100         if (bind_path is not None and
101             os.path.exists(bind_path)):
102             ovs.fatal_signal.unlink_file_now(bind_path)
103         eno = ovs.socket_util.get_exception_errno(e)
104         if (eno == "AF_UNIX path too long" and
105             os.uname()[0] == "Linux"):
106             short_connect_path = None
107             short_bind_path = None
108             connect_dirfd = None
109             bind_dirfd = None
110             # Try workaround using /proc/self/fd
111             if connect_path is not None:
112                 dirname = os.path.dirname(connect_path)
113                 basename = os.path.basename(connect_path)
114                 try:
115                     connect_dirfd = os.open(dirname,
116                                             os.O_DIRECTORY | os.O_RDONLY)
117                 except OSError as err:
118                     return get_exception_errno(err), None
119                 short_connect_path = "/proc/self/fd/%d/%s" % (connect_dirfd,
120                                                               basename)
121
122             if bind_path is not None:
123                 dirname = os.path.dirname(bind_path)
124                 basename = os.path.basename(bind_path)
125                 try:
126                     bind_dirfd = os.open(dirname, os.O_DIRECTORY | os.O_RDONLY)
127                 except OSError as err:
128                     return get_exception_errno(err), None
129                 short_bind_path = "/proc/self/fd/%d/%s" % (bind_dirfd,
130                                                            basename)
131
132             try:
133                 return make_unix_socket(style, nonblock, short_bind_path,
134                                         short_connect_path)
135             finally:
136                 if connect_dirfd is not None:
137                     os.close(connect_dirfd)
138                 if bind_dirfd is not None:
139                     os.close(bind_dirfd)
140         elif (eno == "AF_UNIX path too long"):
141             if short:
142                 return get_exception_errno(e), None
143             short_bind_path = None
144             try:
145                 short_bind_path = make_short_name(bind_path)
146                 short_connect_path = make_short_name(connect_path)
147             except:
148                 free_short_name(short_bind_path)
149                 return errno.ENAMETOOLONG, None
150             try:
151                 return make_unix_socket(style, nonblock, short_bind_path,
152                                         short_connect_path, short=True)
153             finally:
154                 free_short_name(short_bind_path)
155                 free_short_name(short_connect_path)
156         else:
157             return get_exception_errno(e), None
158
159
160 def check_connection_completion(sock):
161     p = ovs.poller.SelectPoll()
162     p.register(sock, ovs.poller.POLLOUT)
163     pfds = p.poll(0)
164     if len(pfds) == 1:
165         revents = pfds[0][1]
166         if revents & ovs.poller.POLLERR:
167             try:
168                 # The following should raise an exception.
169                 socket.send("\0", socket.MSG_DONTWAIT)
170
171                 # (Here's where we end up if it didn't.)
172                 # XXX rate-limit
173                 vlog.err("poll return POLLERR but send succeeded")
174                 return errno.EPROTO
175             except socket.error as e:
176                 return get_exception_errno(e)
177         else:
178             return 0
179     else:
180         return errno.EAGAIN
181
182
183 def is_valid_ipv4_address(address):
184     try:
185         socket.inet_pton(socket.AF_INET, address)
186     except AttributeError:
187         try:
188             socket.inet_aton(address)
189         except socket.error:
190             return False
191     except socket.error:
192         return False
193
194     return True
195
196
197 def inet_parse_active(target, default_port):
198     address = target.split(":")
199     if len(address) >= 2:
200         host_name = ":".join(address[0:-1]).lstrip('[').rstrip(']')
201         port = int(address[-1])
202     else:
203         if default_port:
204             port = default_port
205         else:
206             raise ValueError("%s: port number must be specified" % target)
207         host_name = address[0]
208     if not host_name:
209         raise ValueError("%s: bad peer name format" % target)
210     return (host_name, port)
211
212
213 def inet_open_active(style, target, default_port, dscp):
214     address = inet_parse_active(target, default_port)
215     try:
216         is_addr_inet = is_valid_ipv4_address(address[0])
217         if is_addr_inet:
218             sock = socket.socket(socket.AF_INET, style, 0)
219             family = socket.AF_INET
220         else:
221             sock = socket.socket(socket.AF_INET6, style, 0)
222             family = socket.AF_INET6
223     except socket.error as e:
224         return get_exception_errno(e), None
225
226     try:
227         set_nonblocking(sock)
228         set_dscp(sock, family, dscp)
229         try:
230             sock.connect(address)
231         except socket.error as e:
232             if get_exception_errno(e) != errno.EINPROGRESS:
233                 raise
234         return 0, sock
235     except socket.error as e:
236         sock.close()
237         return get_exception_errno(e), None
238
239
240 def get_exception_errno(e):
241     """A lot of methods on Python socket objects raise socket.error, but that
242     exception is documented as having two completely different forms of
243     arguments: either a string or a (errno, string) tuple.  We only want the
244     errno."""
245     if type(e.args) == tuple:
246         return e.args[0]
247     else:
248         return errno.EPROTO
249
250
251 null_fd = -1
252
253
254 def get_null_fd():
255     """Returns a readable and writable fd for /dev/null, if successful,
256     otherwise a negative errno value.  The caller must not close the returned
257     fd (because the same fd will be handed out to subsequent callers)."""
258     global null_fd
259     if null_fd < 0:
260         try:
261             null_fd = os.open("/dev/null", os.O_RDWR)
262         except OSError as e:
263             vlog.err("could not open /dev/null: %s" % os.strerror(e.errno))
264             return -e.errno
265     return null_fd
266
267
268 def write_fully(fd, buf):
269     """Returns an (error, bytes_written) tuple where 'error' is 0 on success,
270     otherwise a positive errno value, and 'bytes_written' is the number of
271     bytes that were written before the error occurred.  'error' is 0 if and
272     only if 'bytes_written' is len(buf)."""
273     bytes_written = 0
274     if len(buf) == 0:
275         return 0, 0
276     while True:
277         try:
278             retval = os.write(fd, buf)
279             assert retval >= 0
280             if retval == len(buf):
281                 return 0, bytes_written + len(buf)
282             elif retval == 0:
283                 vlog.warn("write returned 0")
284                 return errno.EPROTO, bytes_written
285             else:
286                 bytes_written += retval
287                 buf = buf[:retval]
288         except OSError as e:
289             return e.errno, bytes_written
290
291
292 def set_nonblocking(sock):
293     try:
294         sock.setblocking(0)
295     except socket.error as e:
296         vlog.err("could not set nonblocking mode on socket: %s"
297                  % os.strerror(get_exception_errno(e)))
298
299
300 def set_dscp(sock, family, dscp):
301     if dscp > 63:
302         raise ValueError("Invalid dscp %d" % dscp)
303
304     val = dscp << 2
305     if family == socket.AF_INET:
306         sock.setsockopt(socket.IPPROTO_IP, socket.IP_TOS, val)
307     elif family == socket.AF_INET6:
308         sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, val)
309     else:
310         raise ValueError('Invalid family %d' % family)