2 * Copyright (c) 2008, 2009, 2010 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. */
48 static struct stream_class stream_fd_class;
50 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
52 static void maybe_unlink_and_free(char *path);
54 /* Creates a new stream named 'name' that will send and receive data on 'fd'
55 * and stores a pointer to the stream in '*streamp'. Initial connection status
56 * 'connect_status' is interpreted as described for stream_init().
58 * When '*streamp' is closed, then 'unlink_path' (if nonnull) will be passed to
59 * fatal_signal_unlink_file_now() and then freed with free().
61 * Returns 0 if successful, otherwise a positive errno value. (The current
62 * implementation never fails.) */
64 new_fd_stream(const char *name, int fd, int connect_status,
65 char *unlink_path, struct stream **streamp)
69 s = xmalloc(sizeof *s);
70 stream_init(&s->stream, &stream_fd_class, connect_status, name);
72 s->unlink_path = unlink_path;
73 *streamp = &s->stream;
77 static struct stream_fd *
78 stream_fd_cast(struct stream *stream)
80 stream_assert_class(stream, &stream_fd_class);
81 return CONTAINER_OF(stream, struct stream_fd, stream);
85 fd_close(struct stream *stream)
87 struct stream_fd *s = stream_fd_cast(stream);
89 maybe_unlink_and_free(s->unlink_path);
94 fd_connect(struct stream *stream)
96 struct stream_fd *s = stream_fd_cast(stream);
97 return check_connection_completion(s->fd);
101 stream_flaky_recv, "simulate failure of fd stream recvs",
105 fd_recv(struct stream *stream, void *buffer, size_t n)
107 struct stream_fd *s = stream_fd_cast(stream);
110 if (STRESS(stream_flaky_recv)) {
114 retval = read(s->fd, buffer, n);
115 return retval >= 0 ? retval : -errno;
119 stream_flaky_send, "simulate failure of fd stream sends",
123 fd_send(struct stream *stream, const void *buffer, size_t n)
125 struct stream_fd *s = stream_fd_cast(stream);
128 if (STRESS(stream_flaky_send)) {
132 retval = write(s->fd, buffer, n);
133 return (retval > 0 ? retval
134 : retval == 0 ? -EAGAIN
139 fd_wait(struct stream *stream, enum stream_wait_type wait)
141 struct stream_fd *s = stream_fd_cast(stream);
145 poll_fd_wait(s->fd, POLLOUT);
149 poll_fd_wait(s->fd, POLLIN);
157 static struct stream_class stream_fd_class = {
160 fd_close, /* close */
161 fd_connect, /* connect */
169 /* Passive file descriptor stream. */
173 struct pstream pstream;
175 int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
180 static struct pstream_class fd_pstream_class;
182 static struct fd_pstream *
183 fd_pstream_cast(struct pstream *pstream)
185 pstream_assert_class(pstream, &fd_pstream_class);
186 return CONTAINER_OF(pstream, struct fd_pstream, pstream);
189 /* Creates a new pstream named 'name' that will accept new socket connections
190 * on 'fd' and stores a pointer to the stream in '*pstreamp'.
192 * When a connection has been accepted, 'accept_cb' will be called with the new
193 * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
194 * accept_cb must return 0 if the connection is successful, in which case it
195 * must initialize '*streamp' to the new stream, or a positive errno value on
196 * error. In either case accept_cb takes ownership of the 'fd' passed in.
198 * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
199 * to fatal_signal_unlink_file_now() and freed with free().
201 * Returns 0 if successful, otherwise a positive errno value. (The current
202 * implementation never fails.) */
204 new_fd_pstream(const char *name, int fd,
205 int (*accept_cb)(int fd, const struct sockaddr *sa,
206 size_t sa_len, struct stream **streamp),
207 char *unlink_path, struct pstream **pstreamp)
209 struct fd_pstream *ps = xmalloc(sizeof *ps);
210 pstream_init(&ps->pstream, &fd_pstream_class, name);
212 ps->accept_cb = accept_cb;
213 ps->unlink_path = unlink_path;
214 *pstreamp = &ps->pstream;
219 pfd_close(struct pstream *pstream)
221 struct fd_pstream *ps = fd_pstream_cast(pstream);
223 maybe_unlink_and_free(ps->unlink_path);
228 pfd_accept(struct pstream *pstream, struct stream **new_streamp)
230 struct fd_pstream *ps = fd_pstream_cast(pstream);
231 struct sockaddr_storage ss;
232 socklen_t ss_len = sizeof ss;
236 new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
239 if (retval != EAGAIN) {
240 VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
245 retval = set_nonblocking(new_fd);
251 return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
256 pfd_wait(struct pstream *pstream)
258 struct fd_pstream *ps = fd_pstream_cast(pstream);
259 poll_fd_wait(ps->fd, POLLIN);
262 static struct pstream_class fd_pstream_class = {
270 /* Helper functions. */
272 maybe_unlink_and_free(char *path)
275 fatal_signal_unlink_file_now(path);