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