2 * Copyright (c) 2008, 2009, 2010, 2012 Nicira Networks.
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"
24 #include <sys/socket.h>
25 #include <sys/types.h>
27 #include "fatal-signal.h"
28 #include "leak-checker.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
33 #include "stream-provider.h"
37 VLOG_DEFINE_THIS_MODULE(stream_fd
);
39 /* Active file descriptor stream. */
47 static const struct stream_class stream_fd_class
;
49 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 25);
51 static void maybe_unlink_and_free(char *path
);
53 /* Creates a new stream named 'name' that will send and receive data on 'fd'
54 * and stores a pointer to the stream in '*streamp'. Initial connection status
55 * 'connect_status' is interpreted as described for stream_init().
57 * Returns 0 if successful, otherwise a positive errno value. (The current
58 * implementation never fails.) */
60 new_fd_stream(const char *name
, int fd
, int connect_status
,
61 struct stream
**streamp
)
65 s
= xmalloc(sizeof *s
);
66 stream_init(&s
->stream
, &stream_fd_class
, connect_status
, name
);
68 *streamp
= &s
->stream
;
72 static struct stream_fd
*
73 stream_fd_cast(struct stream
*stream
)
75 stream_assert_class(stream
, &stream_fd_class
);
76 return CONTAINER_OF(stream
, struct stream_fd
, stream
);
80 fd_close(struct stream
*stream
)
82 struct stream_fd
*s
= stream_fd_cast(stream
);
88 fd_connect(struct stream
*stream
)
90 struct stream_fd
*s
= stream_fd_cast(stream
);
91 return check_connection_completion(s
->fd
);
95 stream_flaky_recv
, "simulate failure of fd stream recvs",
99 fd_recv(struct stream
*stream
, void *buffer
, size_t n
)
101 struct stream_fd
*s
= stream_fd_cast(stream
);
104 if (STRESS(stream_flaky_recv
)) {
108 retval
= read(s
->fd
, buffer
, n
);
109 return retval
>= 0 ? retval
: -errno
;
113 stream_flaky_send
, "simulate failure of fd stream sends",
117 fd_send(struct stream
*stream
, const void *buffer
, size_t n
)
119 struct stream_fd
*s
= stream_fd_cast(stream
);
122 if (STRESS(stream_flaky_send
)) {
126 retval
= write(s
->fd
, buffer
, n
);
127 return (retval
> 0 ? retval
128 : retval
== 0 ? -EAGAIN
133 fd_wait(struct stream
*stream
, enum stream_wait_type wait
)
135 struct stream_fd
*s
= stream_fd_cast(stream
);
139 poll_fd_wait(s
->fd
, POLLOUT
);
143 poll_fd_wait(s
->fd
, POLLIN
);
151 static const struct stream_class stream_fd_class
= {
154 fd_close
, /* close */
155 fd_connect
, /* connect */
163 /* Passive file descriptor stream. */
167 struct pstream pstream
;
169 int (*accept_cb
)(int fd
, const struct sockaddr
*, size_t sa_len
,
174 static struct pstream_class fd_pstream_class
;
176 static struct fd_pstream
*
177 fd_pstream_cast(struct pstream
*pstream
)
179 pstream_assert_class(pstream
, &fd_pstream_class
);
180 return CONTAINER_OF(pstream
, struct fd_pstream
, pstream
);
183 /* Creates a new pstream named 'name' that will accept new socket connections
184 * on 'fd' and stores a pointer to the stream in '*pstreamp'.
186 * When a connection has been accepted, 'accept_cb' will be called with the new
187 * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
188 * accept_cb must return 0 if the connection is successful, in which case it
189 * must initialize '*streamp' to the new stream, or a positive errno value on
190 * error. In either case accept_cb takes ownership of the 'fd' passed in.
192 * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
193 * to fatal_signal_unlink_file_now() and freed with free().
195 * Returns 0 if successful, otherwise a positive errno value. (The current
196 * implementation never fails.) */
198 new_fd_pstream(const char *name
, int fd
,
199 int (*accept_cb
)(int fd
, const struct sockaddr
*sa
,
200 size_t sa_len
, struct stream
**streamp
),
201 char *unlink_path
, struct pstream
**pstreamp
)
203 struct fd_pstream
*ps
= xmalloc(sizeof *ps
);
204 pstream_init(&ps
->pstream
, &fd_pstream_class
, name
);
206 ps
->accept_cb
= accept_cb
;
207 ps
->unlink_path
= unlink_path
;
208 *pstreamp
= &ps
->pstream
;
213 pfd_close(struct pstream
*pstream
)
215 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
217 maybe_unlink_and_free(ps
->unlink_path
);
222 pfd_accept(struct pstream
*pstream
, struct stream
**new_streamp
)
224 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
225 struct sockaddr_storage ss
;
226 socklen_t ss_len
= sizeof ss
;
230 new_fd
= accept(ps
->fd
, (struct sockaddr
*) &ss
, &ss_len
);
233 if (retval
!= EAGAIN
) {
234 VLOG_DBG_RL(&rl
, "accept: %s", strerror(retval
));
239 retval
= set_nonblocking(new_fd
);
245 return ps
->accept_cb(new_fd
, (const struct sockaddr
*) &ss
, ss_len
,
250 pfd_wait(struct pstream
*pstream
)
252 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
253 poll_fd_wait(ps
->fd
, POLLIN
);
256 static struct pstream_class fd_pstream_class
= {
264 /* Helper functions. */
266 maybe_unlink_and_free(char *path
)
269 fatal_signal_unlink_file_now(path
);