]> git.proxmox.com Git - mirror_ovs.git/blame - lib/stream-fd.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / lib / stream-fd.c
CommitLineData
c34b65c7 1/*
2b123371 2 * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014, 2015 Nicira, Inc.
c34b65c7
BP
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"
c34b65c7
BP
19#include <errno.h>
20#include <poll.h>
21#include <stdlib.h>
22#include <string.h>
3762274e 23#include <sys/socket.h>
c34b65c7
BP
24#include <sys/types.h>
25#include <unistd.h>
26#include "fatal-signal.h"
fd016ae3 27#include "openvswitch/poll-loop.h"
c34b65c7
BP
28#include "socket-util.h"
29#include "util.h"
30#include "stream-provider.h"
31#include "stream.h"
e6211adc 32#include "openvswitch/vlog.h"
5136ce49 33
d98e6007 34VLOG_DEFINE_THIS_MODULE(stream_fd);
c34b65c7
BP
35
36/* Active file descriptor stream. */
37
38struct stream_fd
39{
40 struct stream stream;
41 int fd;
b7cefbf7 42 int fd_type;
c34b65c7
BP
43};
44
da327b18 45static const struct stream_class stream_fd_class;
c34b65c7
BP
46
47static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
48
49static 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
b7cefbf7
GS
53 * 'connect_status' is interpreted as described for stream_init(). 'fd_type'
54 * tells whether the socket is TCP or Unix domain socket.
c34b65c7 55 *
b7636967
BP
56 * Takes ownership of 'name'.
57 *
c34b65c7
BP
58 * Returns 0 if successful, otherwise a positive errno value. (The current
59 * implementation never fails.) */
60int
b7636967 61new_fd_stream(char *name, int fd, int connect_status, int fd_type,
7921b912 62 struct stream **streamp)
c34b65c7
BP
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;
b7cefbf7 69 s->fd_type = fd_type;
c34b65c7
BP
70 *streamp = &s->stream;
71 return 0;
72}
73
74static struct stream_fd *
75stream_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
81static void
82fd_close(struct stream *stream)
83{
84 struct stream_fd *s = stream_fd_cast(stream);
c48f691f 85 closesocket(s->fd);
c34b65c7
BP
86 free(s);
87}
88
89static int
90fd_connect(struct stream *stream)
91{
92 struct stream_fd *s = stream_fd_cast(stream);
b7cefbf7
GS
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;
c34b65c7
BP
98}
99
100static ssize_t
101fd_recv(struct stream *stream, void *buffer, size_t n)
102{
103 struct stream_fd *s = stream_fd_cast(stream);
cc01d0bb 104 ssize_t retval;
c48f691f
GS
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;
c34b65c7
BP
121}
122
123static ssize_t
124fd_send(struct stream *stream, const void *buffer, size_t n)
125{
126 struct stream_fd *s = stream_fd_cast(stream);
cc01d0bb 127 ssize_t retval;
c48f691f
GS
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) {
2b123371 139 VLOG_DBG_RL(&rl, "send: %s", sock_strerror(error));
c48f691f
GS
140 }
141 return -error;
142 }
143 return (retval > 0 ? retval : -EAGAIN);
c34b65c7
BP
144}
145
146static void
147fd_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:
428b2edd 161 OVS_NOT_REACHED();
c34b65c7
BP
162 }
163}
164
da327b18 165static const struct stream_class stream_fd_class = {
c34b65c7 166 "fd", /* name */
f1936eb6 167 false, /* needs_probes */
c34b65c7
BP
168 NULL, /* open */
169 fd_close, /* close */
170 fd_connect, /* connect */
171 fd_recv, /* recv */
172 fd_send, /* send */
539e96f6
BP
173 NULL, /* run */
174 NULL, /* run_wait */
c34b65c7
BP
175 fd_wait, /* wait */
176};
177\f
178/* Passive file descriptor stream. */
179
180struct fd_pstream
181{
182 struct pstream pstream;
183 int fd;
e731d71b 184 int (*accept_cb)(int fd, const struct sockaddr_storage *, size_t ss_len,
c34b65c7
BP
185 struct stream **);
186 char *unlink_path;
187};
188
112b76f6 189static const struct pstream_class fd_pstream_class;
c34b65c7
BP
190
191static struct fd_pstream *
192fd_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 *
b7636967
BP
210 * Takes ownership of 'name'.
211 *
c34b65c7
BP
212 * Returns 0 if successful, otherwise a positive errno value. (The current
213 * implementation never fails.) */
214int
b7636967 215new_fd_pstream(char *name, int fd,
e731d71b
AS
216 int (*accept_cb)(int fd, const struct sockaddr_storage *ss,
217 size_t ss_len, struct stream **streamp),
c34b65c7
BP
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
229static void
230pfd_close(struct pstream *pstream)
231{
232 struct fd_pstream *ps = fd_pstream_cast(pstream);
c48f691f 233 closesocket(ps->fd);
c34b65c7
BP
234 maybe_unlink_and_free(ps->unlink_path);
235 free(ps);
236}
237
238static int
239pfd_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) {
c48f691f
GS
249 retval = sock_errno();
250#ifdef _WIN32
251 if (retval == WSAEWOULDBLOCK) {
252 retval = EAGAIN;
253 }
254#endif
c34b65c7 255 if (retval != EAGAIN) {
c48f691f 256 VLOG_DBG_RL(&rl, "accept: %s", sock_strerror(retval));
c34b65c7
BP
257 }
258 return retval;
259 }
260
261 retval = set_nonblocking(new_fd);
262 if (retval) {
c48f691f 263 closesocket(new_fd);
c34b65c7
BP
264 return retval;
265 }
266
e731d71b 267 return ps->accept_cb(new_fd, &ss, ss_len, new_streamp);
c34b65c7
BP
268}
269
270static void
271pfd_wait(struct pstream *pstream)
272{
273 struct fd_pstream *ps = fd_pstream_cast(pstream);
274 poll_fd_wait(ps->fd, POLLIN);
275}
276
112b76f6 277static const struct pstream_class fd_pstream_class = {
c34b65c7 278 "pstream",
f1936eb6 279 false,
c34b65c7
BP
280 NULL,
281 pfd_close,
282 pfd_accept,
f89b7ce5 283 pfd_wait,
c34b65c7
BP
284};
285\f
286/* Helper functions. */
287static void
288maybe_unlink_and_free(char *path)
289{
290 if (path) {
291 fatal_signal_unlink_file_now(path);
292 free(path);
293 }
294}