/src/openvswitch/lib/stream-fd.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
64 | 0 | struct stream_fd *s; |
65 | |
|
66 | 0 | s = xmalloc(sizeof *s); |
67 | 0 | stream_init(&s->stream, &stream_fd_class, connect_status, name); |
68 | 0 | s->fd = fd; |
69 | 0 | s->fd_type = fd_type; |
70 | 0 | *streamp = &s->stream; |
71 | 0 | return 0; |
72 | 0 | } |
73 | | |
74 | | static struct stream_fd * |
75 | | stream_fd_cast(struct stream *stream) |
76 | 0 | { |
77 | 0 | stream_assert_class(stream, &stream_fd_class); |
78 | 0 | return CONTAINER_OF(stream, struct stream_fd, stream); |
79 | 0 | } |
80 | | |
81 | | static void |
82 | | fd_close(struct stream *stream) |
83 | 0 | { |
84 | 0 | struct stream_fd *s = stream_fd_cast(stream); |
85 | 0 | closesocket(s->fd); |
86 | 0 | free(s); |
87 | 0 | } |
88 | | |
89 | | static int |
90 | | fd_connect(struct stream *stream) |
91 | 0 | { |
92 | 0 | struct stream_fd *s = stream_fd_cast(stream); |
93 | 0 | int retval = check_connection_completion(s->fd); |
94 | 0 | if (retval == 0 && s->fd_type == AF_INET) { |
95 | 0 | setsockopt_tcp_nodelay(s->fd); |
96 | 0 | } |
97 | 0 | return retval; |
98 | 0 | } |
99 | | |
100 | | static ssize_t |
101 | | fd_recv(struct stream *stream, void *buffer, size_t n) |
102 | 0 | { |
103 | 0 | struct stream_fd *s = stream_fd_cast(stream); |
104 | 0 | ssize_t retval; |
105 | 0 | int error; |
106 | |
|
107 | 0 | retval = recv(s->fd, buffer, n, 0); |
108 | 0 | if (retval < 0) { |
109 | 0 | error = sock_errno(); |
110 | | #ifdef _WIN32 |
111 | | if (error == WSAEWOULDBLOCK) { |
112 | | error = EAGAIN; |
113 | | } |
114 | | #endif |
115 | 0 | if (error != EAGAIN) { |
116 | 0 | VLOG_DBG_RL(&rl, "recv: %s", sock_strerror(error)); |
117 | 0 | } |
118 | 0 | return -error; |
119 | 0 | } |
120 | 0 | return retval; |
121 | 0 | } |
122 | | |
123 | | static ssize_t |
124 | | fd_send(struct stream *stream, const void *buffer, size_t n) |
125 | 0 | { |
126 | 0 | struct stream_fd *s = stream_fd_cast(stream); |
127 | 0 | ssize_t retval; |
128 | 0 | int error; |
129 | |
|
130 | 0 | retval = send(s->fd, buffer, n, 0); |
131 | 0 | if (retval < 0) { |
132 | 0 | error = sock_errno(); |
133 | | #ifdef _WIN32 |
134 | | if (error == WSAEWOULDBLOCK) { |
135 | | error = EAGAIN; |
136 | | } |
137 | | #endif |
138 | 0 | if (error != EAGAIN) { |
139 | 0 | VLOG_DBG_RL(&rl, "send: %s", sock_strerror(error)); |
140 | 0 | } |
141 | 0 | return -error; |
142 | 0 | } |
143 | 0 | return (retval > 0 ? retval : -EAGAIN); |
144 | 0 | } |
145 | | |
146 | | static void |
147 | | fd_wait(struct stream *stream, enum stream_wait_type wait) |
148 | 0 | { |
149 | 0 | struct stream_fd *s = stream_fd_cast(stream); |
150 | 0 | switch (wait) { |
151 | 0 | case STREAM_CONNECT: |
152 | 0 | case STREAM_SEND: |
153 | 0 | poll_fd_wait(s->fd, POLLOUT); |
154 | 0 | break; |
155 | | |
156 | 0 | case STREAM_RECV: |
157 | 0 | poll_fd_wait(s->fd, POLLIN); |
158 | 0 | break; |
159 | | |
160 | 0 | default: |
161 | 0 | OVS_NOT_REACHED(); |
162 | 0 | } |
163 | 0 | } |
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 | | |
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 | 0 | { |
194 | 0 | pstream_assert_class(pstream, &fd_pstream_class); |
195 | 0 | return CONTAINER_OF(pstream, struct fd_pstream, pstream); |
196 | 0 | } |
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 | 0 | { |
220 | 0 | struct fd_pstream *ps = xmalloc(sizeof *ps); |
221 | 0 | pstream_init(&ps->pstream, &fd_pstream_class, name); |
222 | 0 | ps->fd = fd; |
223 | 0 | ps->accept_cb = accept_cb; |
224 | 0 | ps->unlink_path = unlink_path; |
225 | 0 | *pstreamp = &ps->pstream; |
226 | 0 | return 0; |
227 | 0 | } |
228 | | |
229 | | static void |
230 | | pfd_close(struct pstream *pstream) |
231 | 0 | { |
232 | 0 | struct fd_pstream *ps = fd_pstream_cast(pstream); |
233 | 0 | closesocket(ps->fd); |
234 | 0 | maybe_unlink_and_free(ps->unlink_path); |
235 | 0 | free(ps); |
236 | 0 | } |
237 | | |
238 | | static int |
239 | | pfd_accept(struct pstream *pstream, struct stream **new_streamp) |
240 | 0 | { |
241 | 0 | struct fd_pstream *ps = fd_pstream_cast(pstream); |
242 | 0 | struct sockaddr_storage ss; |
243 | 0 | socklen_t ss_len = sizeof ss; |
244 | 0 | int new_fd; |
245 | 0 | int retval; |
246 | |
|
247 | 0 | new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len); |
248 | 0 | if (new_fd < 0) { |
249 | 0 | retval = sock_errno(); |
250 | | #ifdef _WIN32 |
251 | | if (retval == WSAEWOULDBLOCK) { |
252 | | retval = EAGAIN; |
253 | | } |
254 | | #endif |
255 | 0 | if (retval != EAGAIN) { |
256 | 0 | VLOG_DBG_RL(&rl, "accept: %s", sock_strerror(retval)); |
257 | 0 | } |
258 | 0 | return retval; |
259 | 0 | } |
260 | | |
261 | 0 | retval = set_nonblocking(new_fd); |
262 | 0 | if (retval) { |
263 | 0 | closesocket(new_fd); |
264 | 0 | return retval; |
265 | 0 | } |
266 | | |
267 | 0 | return ps->accept_cb(new_fd, &ss, ss_len, new_streamp); |
268 | 0 | } |
269 | | |
270 | | static void |
271 | | pfd_wait(struct pstream *pstream) |
272 | 0 | { |
273 | 0 | struct fd_pstream *ps = fd_pstream_cast(pstream); |
274 | 0 | poll_fd_wait(ps->fd, POLLIN); |
275 | 0 | } |
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 | | |
286 | | /* Helper functions. */ |
287 | | static void |
288 | | maybe_unlink_and_free(char *path) |
289 | 0 | { |
290 | 0 | if (path) { |
291 | 0 | fatal_signal_unlink_file_now(path); |
292 | 0 | free(path); |
293 | 0 | } |
294 | 0 | } |