Catalli's threaded switch
[sliver-openvswitch.git] / lib / stream-fd.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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 "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     char *unlink_path;
45 };
46
47 static 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  * When '*streamp' is closed, then 'unlink_path' (if nonnull) will be passed to
58  * fatal_signal_unlink_file_now() and then freed with free().
59  *
60  * Returns 0 if successful, otherwise a positive errno value.  (The current
61  * implementation never fails.) */
62 int
63 new_fd_stream(const char *name, int fd, int connect_status,
64               char *unlink_path, struct stream **streamp)
65 {
66     struct stream_fd *s;
67
68     s = xmalloc(sizeof *s);
69     stream_init(&s->stream, &stream_fd_class, connect_status, name);
70     s->fd = fd;
71     s->unlink_path = unlink_path;
72     *streamp = &s->stream;
73     return 0;
74 }
75
76 static struct stream_fd *
77 stream_fd_cast(struct stream *stream)
78 {
79     stream_assert_class(stream, &stream_fd_class);
80     return CONTAINER_OF(stream, struct stream_fd, stream);
81 }
82
83 static void
84 fd_close(struct stream *stream)
85 {
86     struct stream_fd *s = stream_fd_cast(stream);
87     close(s->fd);
88     maybe_unlink_and_free(s->unlink_path);
89     free(s);
90 }
91
92 static int
93 fd_connect(struct stream *stream)
94 {
95     struct stream_fd *s = stream_fd_cast(stream);
96     return check_connection_completion(s->fd);
97 }
98
99 static ssize_t
100 fd_recv(struct stream *stream, void *buffer, size_t n)
101 {
102     struct stream_fd *s = stream_fd_cast(stream);
103     ssize_t retval = read(s->fd, buffer, n);
104     return retval >= 0 ? retval : -errno;
105 }
106
107 static ssize_t
108 fd_send(struct stream *stream, const void *buffer, size_t n)
109 {
110     struct stream_fd *s = stream_fd_cast(stream);
111     ssize_t retval = write(s->fd, buffer, n);
112     return (retval > 0 ? retval
113             : retval == 0 ? -EAGAIN
114             : -errno);
115 }
116
117 static void
118 fd_wait(struct stream *stream, enum stream_wait_type wait)
119 {
120     struct stream_fd *s = stream_fd_cast(stream);
121     switch (wait) {
122     case STREAM_CONNECT:
123     case STREAM_SEND:
124         poll_fd_wait(s->fd, POLLOUT);
125         break;
126
127     case STREAM_RECV:
128         poll_fd_wait(s->fd, POLLIN);
129         break;
130
131     default:
132         NOT_REACHED();
133     }
134 }
135
136 static struct stream_class stream_fd_class = {
137     "fd",                       /* name */
138     NULL,                       /* open */
139     fd_close,                   /* close */
140     fd_connect,                 /* connect */
141     fd_recv,                    /* recv */
142     fd_send,                    /* send */
143     NULL,                       /* run */
144     NULL,                       /* run_wait */
145     fd_wait,                    /* wait */
146 };
147 \f
148 /* Passive file descriptor stream. */
149
150 struct fd_pstream
151 {
152     struct pstream pstream;
153     int fd;
154     int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
155                      struct stream **);
156     char *unlink_path;
157 };
158
159 static struct pstream_class fd_pstream_class;
160
161 static struct fd_pstream *
162 fd_pstream_cast(struct pstream *pstream)
163 {
164     pstream_assert_class(pstream, &fd_pstream_class);
165     return CONTAINER_OF(pstream, struct fd_pstream, pstream);
166 }
167
168 /* Creates a new pstream named 'name' that will accept new socket connections
169  * on 'fd' and stores a pointer to the stream in '*pstreamp'.
170  *
171  * When a connection has been accepted, 'accept_cb' will be called with the new
172  * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
173  * accept_cb must return 0 if the connection is successful, in which case it
174  * must initialize '*streamp' to the new stream, or a positive errno value on
175  * error.  In either case accept_cb takes ownership of the 'fd' passed in.
176  *
177  * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
178  * to fatal_signal_unlink_file_now() and freed with free().
179  *
180  * Returns 0 if successful, otherwise a positive errno value.  (The current
181  * implementation never fails.) */
182 int
183 new_fd_pstream(const char *name, int fd,
184                int (*accept_cb)(int fd, const struct sockaddr *sa,
185                                 size_t sa_len, struct stream **streamp),
186                char *unlink_path, struct pstream **pstreamp)
187 {
188     struct fd_pstream *ps = xmalloc(sizeof *ps);
189     pstream_init(&ps->pstream, &fd_pstream_class, name);
190     ps->fd = fd;
191     ps->accept_cb = accept_cb;
192     ps->unlink_path = unlink_path;
193     *pstreamp = &ps->pstream;
194     return 0;
195 }
196
197 static void
198 pfd_close(struct pstream *pstream)
199 {
200     struct fd_pstream *ps = fd_pstream_cast(pstream);
201     close(ps->fd);
202     maybe_unlink_and_free(ps->unlink_path);
203     free(ps);
204 }
205
206 static int
207 pfd_accept(struct pstream *pstream, struct stream **new_streamp)
208 {
209     struct fd_pstream *ps = fd_pstream_cast(pstream);
210     struct sockaddr_storage ss;
211     socklen_t ss_len = sizeof ss;
212     int new_fd;
213     int retval;
214
215     new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
216     if (new_fd < 0) {
217         int retval = errno;
218         if (retval != EAGAIN) {
219             VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
220         }
221         return retval;
222     }
223
224     retval = set_nonblocking(new_fd);
225     if (retval) {
226         close(new_fd);
227         return retval;
228     }
229
230     return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
231                          new_streamp);
232 }
233
234 static void
235 pfd_wait(struct pstream *pstream)
236 {
237     struct fd_pstream *ps = fd_pstream_cast(pstream);
238     poll_fd_wait(ps->fd, POLLIN);
239 }
240
241 static struct pstream_class fd_pstream_class = {
242     "pstream",
243     NULL,
244     pfd_close,
245     pfd_accept,
246     pfd_wait
247 };
248 \f
249 /* Helper functions. */
250 static void
251 maybe_unlink_and_free(char *path)
252 {
253     if (path) {
254         fatal_signal_unlink_file_now(path);
255         free(path);
256     }
257 }