Global replace of Nicira Networks.
[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     char *unlink_path;
173 };
174
175 static 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                char *unlink_path, struct pstream **pstreamp)
203 {
204     struct fd_pstream *ps = xmalloc(sizeof *ps);
205     pstream_init(&ps->pstream, &fd_pstream_class, name);
206     ps->fd = fd;
207     ps->accept_cb = accept_cb;
208     ps->unlink_path = unlink_path;
209     *pstreamp = &ps->pstream;
210     return 0;
211 }
212
213 static void
214 pfd_close(struct pstream *pstream)
215 {
216     struct fd_pstream *ps = fd_pstream_cast(pstream);
217     close(ps->fd);
218     maybe_unlink_and_free(ps->unlink_path);
219     free(ps);
220 }
221
222 static int
223 pfd_accept(struct pstream *pstream, struct stream **new_streamp)
224 {
225     struct fd_pstream *ps = fd_pstream_cast(pstream);
226     struct sockaddr_storage ss;
227     socklen_t ss_len = sizeof ss;
228     int new_fd;
229     int retval;
230
231     new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
232     if (new_fd < 0) {
233         retval = errno;
234         if (retval != EAGAIN) {
235             VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
236         }
237         return retval;
238     }
239
240     retval = set_nonblocking(new_fd);
241     if (retval) {
242         close(new_fd);
243         return retval;
244     }
245
246     return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
247                          new_streamp);
248 }
249
250 static void
251 pfd_wait(struct pstream *pstream)
252 {
253     struct fd_pstream *ps = fd_pstream_cast(pstream);
254     poll_fd_wait(ps->fd, POLLIN);
255 }
256
257 static struct pstream_class fd_pstream_class = {
258     "pstream",
259     false,
260     NULL,
261     pfd_close,
262     pfd_accept,
263     pfd_wait
264 };
265 \f
266 /* Helper functions. */
267 static void
268 maybe_unlink_and_free(char *path)
269 {
270     if (path) {
271         fatal_signal_unlink_file_now(path);
272         free(path);
273     }
274 }