2 * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "stream-fd.h"
23 #include <sys/socket.h>
24 #include <sys/types.h>
26 #include "fatal-signal.h"
27 #include "openvswitch/poll-loop.h"
28 #include "socket-util.h"
30 #include "stream-provider.h"
32 #include "openvswitch/vlog.h"
34 VLOG_DEFINE_THIS_MODULE(stream_fd
);
36 /* Active file descriptor stream. */
45 static const struct stream_class stream_fd_class
;
47 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 25);
49 static void maybe_unlink_and_free(char *path
);
51 /* Creates a new stream named 'name' that will send and receive data on 'fd'
52 * and stores a pointer to the stream in '*streamp'. Initial connection status
53 * 'connect_status' is interpreted as described for stream_init(). 'fd_type'
54 * tells whether the socket is TCP or Unix domain socket.
56 * Takes ownership of 'name'.
58 * Returns 0 if successful, otherwise a positive errno value. (The current
59 * implementation never fails.) */
61 new_fd_stream(char *name
, int fd
, int connect_status
, int fd_type
,
62 struct stream
**streamp
)
66 s
= xmalloc(sizeof *s
);
67 stream_init(&s
->stream
, &stream_fd_class
, connect_status
, name
);
70 *streamp
= &s
->stream
;
74 static struct stream_fd
*
75 stream_fd_cast(struct stream
*stream
)
77 stream_assert_class(stream
, &stream_fd_class
);
78 return CONTAINER_OF(stream
, struct stream_fd
, stream
);
82 fd_close(struct stream
*stream
)
84 struct stream_fd
*s
= stream_fd_cast(stream
);
90 fd_connect(struct stream
*stream
)
92 struct stream_fd
*s
= stream_fd_cast(stream
);
93 int retval
= check_connection_completion(s
->fd
);
94 if (retval
== 0 && s
->fd_type
== AF_INET
) {
95 setsockopt_tcp_nodelay(s
->fd
);
101 fd_recv(struct stream
*stream
, void *buffer
, size_t n
)
103 struct stream_fd
*s
= stream_fd_cast(stream
);
107 retval
= recv(s
->fd
, buffer
, n
, 0);
109 error
= sock_errno();
111 if (error
== WSAEWOULDBLOCK
) {
115 if (error
!= EAGAIN
) {
116 VLOG_DBG_RL(&rl
, "recv: %s", sock_strerror(error
));
124 fd_send(struct stream
*stream
, const void *buffer
, size_t n
)
126 struct stream_fd
*s
= stream_fd_cast(stream
);
130 retval
= send(s
->fd
, buffer
, n
, 0);
132 error
= sock_errno();
134 if (error
== WSAEWOULDBLOCK
) {
138 if (error
!= EAGAIN
) {
139 VLOG_DBG_RL(&rl
, "send: %s", sock_strerror(error
));
143 return (retval
> 0 ? retval
: -EAGAIN
);
147 fd_wait(struct stream
*stream
, enum stream_wait_type wait
)
149 struct stream_fd
*s
= stream_fd_cast(stream
);
153 poll_fd_wait(s
->fd
, POLLOUT
);
157 poll_fd_wait(s
->fd
, POLLIN
);
165 static const struct stream_class stream_fd_class
= {
167 false, /* needs_probes */
169 fd_close
, /* close */
170 fd_connect
, /* connect */
178 /* Passive file descriptor stream. */
182 struct pstream pstream
;
184 int (*accept_cb
)(int fd
, const struct sockaddr_storage
*, size_t ss_len
,
189 static const struct pstream_class fd_pstream_class
;
191 static struct fd_pstream
*
192 fd_pstream_cast(struct pstream
*pstream
)
194 pstream_assert_class(pstream
, &fd_pstream_class
);
195 return CONTAINER_OF(pstream
, struct fd_pstream
, pstream
);
198 /* Creates a new pstream named 'name' that will accept new socket connections
199 * on 'fd' and stores a pointer to the stream in '*pstreamp'.
201 * When a connection has been accepted, 'accept_cb' will be called with the new
202 * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
203 * accept_cb must return 0 if the connection is successful, in which case it
204 * must initialize '*streamp' to the new stream, or a positive errno value on
205 * error. In either case accept_cb takes ownership of the 'fd' passed in.
207 * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
208 * to fatal_signal_unlink_file_now() and freed with free().
210 * Takes ownership of 'name'.
212 * Returns 0 if successful, otherwise a positive errno value. (The current
213 * implementation never fails.) */
215 new_fd_pstream(char *name
, int fd
,
216 int (*accept_cb
)(int fd
, const struct sockaddr_storage
*ss
,
217 size_t ss_len
, struct stream
**streamp
),
218 char *unlink_path
, struct pstream
**pstreamp
)
220 struct fd_pstream
*ps
= xmalloc(sizeof *ps
);
221 pstream_init(&ps
->pstream
, &fd_pstream_class
, name
);
223 ps
->accept_cb
= accept_cb
;
224 ps
->unlink_path
= unlink_path
;
225 *pstreamp
= &ps
->pstream
;
230 pfd_close(struct pstream
*pstream
)
232 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
234 maybe_unlink_and_free(ps
->unlink_path
);
239 pfd_accept(struct pstream
*pstream
, struct stream
**new_streamp
)
241 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
242 struct sockaddr_storage ss
;
243 socklen_t ss_len
= sizeof ss
;
247 new_fd
= accept(ps
->fd
, (struct sockaddr
*) &ss
, &ss_len
);
249 retval
= sock_errno();
251 if (retval
== WSAEWOULDBLOCK
) {
255 if (retval
!= EAGAIN
) {
256 VLOG_DBG_RL(&rl
, "accept: %s", sock_strerror(retval
));
261 retval
= set_nonblocking(new_fd
);
267 return ps
->accept_cb(new_fd
, &ss
, ss_len
, new_streamp
);
271 pfd_wait(struct pstream
*pstream
)
273 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
274 poll_fd_wait(ps
->fd
, POLLIN
);
277 static const struct pstream_class fd_pstream_class
= {
286 /* Helper functions. */
288 maybe_unlink_and_free(char *path
)
291 fatal_signal_unlink_file_now(path
);