90d328acc0c4fdd93f01a50b2ca8fb48bdd9ef94
[sliver-openvswitch.git] / lib / stream-fd.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "stream-fd.h"
19 #include <errno.h>
20 #include <poll.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include "fatal-signal.h"
27 #include "leak-checker.h"
28 #include "poll-loop.h"
29 #include "socket-util.h"
30 #include "stress.h"
31 #include "util.h"
32 #include "stream-provider.h"
33 #include "stream.h"
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(stream_fd);
37
38 /* Active file descriptor stream. */
39
40 struct stream_fd
41 {
42     struct stream stream;
43     int fd;
44 };
45
46 static const struct stream_class stream_fd_class;
47
48 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
49
50 static void maybe_unlink_and_free(char *path);
51
52 /* Creates a new stream named 'name' that will send and receive data on 'fd'
53  * and stores a pointer to the stream in '*streamp'.  Initial connection status
54  * 'connect_status' is interpreted as described for stream_init().
55  *
56  * Returns 0 if successful, otherwise a positive errno value.  (The current
57  * implementation never fails.) */
58 int
59 new_fd_stream(const char *name, int fd, int connect_status,
60               struct stream **streamp)
61 {
62     struct stream_fd *s;
63
64     s = xmalloc(sizeof *s);
65     stream_init(&s->stream, &stream_fd_class, connect_status, name);
66     s->fd = fd;
67     *streamp = &s->stream;
68     return 0;
69 }
70
71 static struct stream_fd *
72 stream_fd_cast(struct stream *stream)
73 {
74     stream_assert_class(stream, &stream_fd_class);
75     return CONTAINER_OF(stream, struct stream_fd, stream);
76 }
77
78 static void
79 fd_close(struct stream *stream)
80 {
81     struct stream_fd *s = stream_fd_cast(stream);
82     close(s->fd);
83     free(s);
84 }
85
86 static int
87 fd_connect(struct stream *stream)
88 {
89     struct stream_fd *s = stream_fd_cast(stream);
90     return check_connection_completion(s->fd);
91 }
92
93 STRESS_OPTION(
94     stream_flaky_recv, "simulate failure of fd stream recvs",
95     100, 0, -1, 0);
96
97 static ssize_t
98 fd_recv(struct stream *stream, void *buffer, size_t n)
99 {
100     struct stream_fd *s = stream_fd_cast(stream);
101     ssize_t retval;
102
103     if (STRESS(stream_flaky_recv)) {
104         return -EIO;
105     }
106
107     retval = read(s->fd, buffer, n);
108     return retval >= 0 ? retval : -errno;
109 }
110
111 STRESS_OPTION(
112     stream_flaky_send, "simulate failure of fd stream sends",
113     100, 0, -1, 0);
114
115 static ssize_t
116 fd_send(struct stream *stream, const void *buffer, size_t n)
117 {
118     struct stream_fd *s = stream_fd_cast(stream);
119     ssize_t retval;
120
121     if (STRESS(stream_flaky_send)) {
122         return -EIO;
123     }
124
125     retval = write(s->fd, buffer, n);
126     return (retval > 0 ? retval
127             : retval == 0 ? -EAGAIN
128             : -errno);
129 }
130
131 static void
132 fd_wait(struct stream *stream, enum stream_wait_type wait)
133 {
134     struct stream_fd *s = stream_fd_cast(stream);
135     switch (wait) {
136     case STREAM_CONNECT:
137     case STREAM_SEND:
138         poll_fd_wait(s->fd, POLLOUT);
139         break;
140
141     case STREAM_RECV:
142         poll_fd_wait(s->fd, POLLIN);
143         break;
144
145     default:
146         NOT_REACHED();
147     }
148 }
149
150 static const struct stream_class stream_fd_class = {
151     "fd",                       /* name */
152     false,                      /* needs_probes */
153     NULL,                       /* open */
154     fd_close,                   /* close */
155     fd_connect,                 /* connect */
156     fd_recv,                    /* recv */
157     fd_send,                    /* send */
158     NULL,                       /* run */
159     NULL,                       /* run_wait */
160     fd_wait,                    /* wait */
161 };
162 \f
163 /* Passive file descriptor stream. */
164
165 struct fd_pstream
166 {
167     struct pstream pstream;
168     int fd;
169     int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
170                      struct stream **);
171     int (*set_dscp_cb)(int fd, uint8_t dscp);
172     char *unlink_path;
173 };
174
175 static const struct pstream_class fd_pstream_class;
176
177 static struct fd_pstream *
178 fd_pstream_cast(struct pstream *pstream)
179 {
180     pstream_assert_class(pstream, &fd_pstream_class);
181     return CONTAINER_OF(pstream, struct fd_pstream, pstream);
182 }
183
184 /* Creates a new pstream named 'name' that will accept new socket connections
185  * on 'fd' and stores a pointer to the stream in '*pstreamp'.
186  *
187  * When a connection has been accepted, 'accept_cb' will be called with the new
188  * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
189  * accept_cb must return 0 if the connection is successful, in which case it
190  * must initialize '*streamp' to the new stream, or a positive errno value on
191  * error.  In either case accept_cb takes ownership of the 'fd' passed in.
192  *
193  * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
194  * to fatal_signal_unlink_file_now() and freed with free().
195  *
196  * Returns 0 if successful, otherwise a positive errno value.  (The current
197  * implementation never fails.) */
198 int
199 new_fd_pstream(const char *name, int fd,
200                int (*accept_cb)(int fd, const struct sockaddr *sa,
201                                 size_t sa_len, struct stream **streamp),
202                int (*set_dscp_cb)(int fd, uint8_t dscp),
203                char *unlink_path, struct pstream **pstreamp)
204 {
205     struct fd_pstream *ps = xmalloc(sizeof *ps);
206     pstream_init(&ps->pstream, &fd_pstream_class, name);
207     ps->fd = fd;
208     ps->accept_cb = accept_cb;
209     ps->set_dscp_cb = set_dscp_cb;
210     ps->unlink_path = unlink_path;
211     *pstreamp = &ps->pstream;
212     return 0;
213 }
214
215 static void
216 pfd_close(struct pstream *pstream)
217 {
218     struct fd_pstream *ps = fd_pstream_cast(pstream);
219     close(ps->fd);
220     maybe_unlink_and_free(ps->unlink_path);
221     free(ps);
222 }
223
224 static int
225 pfd_accept(struct pstream *pstream, struct stream **new_streamp)
226 {
227     struct fd_pstream *ps = fd_pstream_cast(pstream);
228     struct sockaddr_storage ss;
229     socklen_t ss_len = sizeof ss;
230     int new_fd;
231     int retval;
232
233     new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
234     if (new_fd < 0) {
235         retval = errno;
236         if (retval != EAGAIN) {
237             VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
238         }
239         return retval;
240     }
241
242     retval = set_nonblocking(new_fd);
243     if (retval) {
244         close(new_fd);
245         return retval;
246     }
247
248     return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
249                          new_streamp);
250 }
251
252 static void
253 pfd_wait(struct pstream *pstream)
254 {
255     struct fd_pstream *ps = fd_pstream_cast(pstream);
256     poll_fd_wait(ps->fd, POLLIN);
257 }
258
259 static int
260 pfd_set_dscp(struct pstream *pstream, uint8_t dscp)
261 {
262     struct fd_pstream *ps = fd_pstream_cast(pstream);
263     if (ps->set_dscp_cb) {
264         return ps->set_dscp_cb(ps->fd, dscp);
265     }
266     return 0;
267 }
268
269 static const struct pstream_class fd_pstream_class = {
270     "pstream",
271     false,
272     NULL,
273     pfd_close,
274     pfd_accept,
275     pfd_wait,
276     pfd_set_dscp,
277 };
278 \f
279 /* Helper functions. */
280 static void
281 maybe_unlink_and_free(char *path)
282 {
283     if (path) {
284         fatal_signal_unlink_file_now(path);
285         free(path);
286     }
287 }