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