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