]>
Commit | Line | Data |
---|---|---|
c34b65c7 | 1 | /* |
e731d71b | 2 | * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014 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" | |
c34b65c7 BP |
27 | #include "poll-loop.h" |
28 | #include "socket-util.h" | |
29 | #include "util.h" | |
30 | #include "stream-provider.h" | |
31 | #include "stream.h" | |
c34b65c7 | 32 | #include "vlog.h" |
5136ce49 | 33 | |
d98e6007 | 34 | VLOG_DEFINE_THIS_MODULE(stream_fd); |
c34b65c7 BP |
35 | |
36 | /* Active file descriptor stream. */ | |
37 | ||
38 | struct stream_fd | |
39 | { | |
40 | struct stream stream; | |
41 | int fd; | |
c34b65c7 BP |
42 | }; |
43 | ||
da327b18 | 44 | static const struct stream_class stream_fd_class; |
c34b65c7 BP |
45 | |
46 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25); | |
47 | ||
48 | static void maybe_unlink_and_free(char *path); | |
49 | ||
50 | /* Creates a new stream named 'name' that will send and receive data on 'fd' | |
51 | * and stores a pointer to the stream in '*streamp'. Initial connection status | |
52 | * 'connect_status' is interpreted as described for stream_init(). | |
53 | * | |
c34b65c7 BP |
54 | * Returns 0 if successful, otherwise a positive errno value. (The current |
55 | * implementation never fails.) */ | |
56 | int | |
57 | new_fd_stream(const char *name, int fd, int connect_status, | |
7921b912 | 58 | struct stream **streamp) |
c34b65c7 BP |
59 | { |
60 | struct stream_fd *s; | |
61 | ||
62 | s = xmalloc(sizeof *s); | |
63 | stream_init(&s->stream, &stream_fd_class, connect_status, name); | |
64 | s->fd = fd; | |
c34b65c7 BP |
65 | *streamp = &s->stream; |
66 | return 0; | |
67 | } | |
68 | ||
69 | static struct stream_fd * | |
70 | stream_fd_cast(struct stream *stream) | |
71 | { | |
72 | stream_assert_class(stream, &stream_fd_class); | |
73 | return CONTAINER_OF(stream, struct stream_fd, stream); | |
74 | } | |
75 | ||
76 | static void | |
77 | fd_close(struct stream *stream) | |
78 | { | |
79 | struct stream_fd *s = stream_fd_cast(stream); | |
80 | close(s->fd); | |
c34b65c7 BP |
81 | free(s); |
82 | } | |
83 | ||
84 | static int | |
85 | fd_connect(struct stream *stream) | |
86 | { | |
87 | struct stream_fd *s = stream_fd_cast(stream); | |
88 | return check_connection_completion(s->fd); | |
89 | } | |
90 | ||
91 | static ssize_t | |
92 | fd_recv(struct stream *stream, void *buffer, size_t n) | |
93 | { | |
94 | struct stream_fd *s = stream_fd_cast(stream); | |
cc01d0bb BP |
95 | ssize_t retval; |
96 | ||
cc01d0bb | 97 | retval = read(s->fd, buffer, n); |
c34b65c7 BP |
98 | return retval >= 0 ? retval : -errno; |
99 | } | |
100 | ||
101 | static ssize_t | |
102 | fd_send(struct stream *stream, const void *buffer, size_t n) | |
103 | { | |
104 | struct stream_fd *s = stream_fd_cast(stream); | |
cc01d0bb BP |
105 | ssize_t retval; |
106 | ||
cc01d0bb | 107 | retval = write(s->fd, buffer, n); |
c34b65c7 BP |
108 | return (retval > 0 ? retval |
109 | : retval == 0 ? -EAGAIN | |
110 | : -errno); | |
111 | } | |
112 | ||
113 | static void | |
114 | fd_wait(struct stream *stream, enum stream_wait_type wait) | |
115 | { | |
116 | struct stream_fd *s = stream_fd_cast(stream); | |
117 | switch (wait) { | |
118 | case STREAM_CONNECT: | |
119 | case STREAM_SEND: | |
120 | poll_fd_wait(s->fd, POLLOUT); | |
121 | break; | |
122 | ||
123 | case STREAM_RECV: | |
124 | poll_fd_wait(s->fd, POLLIN); | |
125 | break; | |
126 | ||
127 | default: | |
428b2edd | 128 | OVS_NOT_REACHED(); |
c34b65c7 BP |
129 | } |
130 | } | |
131 | ||
da327b18 | 132 | static const struct stream_class stream_fd_class = { |
c34b65c7 | 133 | "fd", /* name */ |
f1936eb6 | 134 | false, /* needs_probes */ |
c34b65c7 BP |
135 | NULL, /* open */ |
136 | fd_close, /* close */ | |
137 | fd_connect, /* connect */ | |
138 | fd_recv, /* recv */ | |
139 | fd_send, /* send */ | |
539e96f6 BP |
140 | NULL, /* run */ |
141 | NULL, /* run_wait */ | |
c34b65c7 BP |
142 | fd_wait, /* wait */ |
143 | }; | |
144 | \f | |
145 | /* Passive file descriptor stream. */ | |
146 | ||
147 | struct fd_pstream | |
148 | { | |
149 | struct pstream pstream; | |
150 | int fd; | |
e731d71b | 151 | int (*accept_cb)(int fd, const struct sockaddr_storage *, size_t ss_len, |
c34b65c7 | 152 | struct stream **); |
f89b7ce5 | 153 | int (*set_dscp_cb)(int fd, uint8_t dscp); |
c34b65c7 BP |
154 | char *unlink_path; |
155 | }; | |
156 | ||
112b76f6 | 157 | static const struct pstream_class fd_pstream_class; |
c34b65c7 BP |
158 | |
159 | static struct fd_pstream * | |
160 | fd_pstream_cast(struct pstream *pstream) | |
161 | { | |
162 | pstream_assert_class(pstream, &fd_pstream_class); | |
163 | return CONTAINER_OF(pstream, struct fd_pstream, pstream); | |
164 | } | |
165 | ||
166 | /* Creates a new pstream named 'name' that will accept new socket connections | |
167 | * on 'fd' and stores a pointer to the stream in '*pstreamp'. | |
168 | * | |
169 | * When a connection has been accepted, 'accept_cb' will be called with the new | |
170 | * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'. | |
171 | * accept_cb must return 0 if the connection is successful, in which case it | |
172 | * must initialize '*streamp' to the new stream, or a positive errno value on | |
173 | * error. In either case accept_cb takes ownership of the 'fd' passed in. | |
174 | * | |
175 | * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed | |
176 | * to fatal_signal_unlink_file_now() and freed with free(). | |
177 | * | |
178 | * Returns 0 if successful, otherwise a positive errno value. (The current | |
179 | * implementation never fails.) */ | |
180 | int | |
181 | new_fd_pstream(const char *name, int fd, | |
e731d71b AS |
182 | int (*accept_cb)(int fd, const struct sockaddr_storage *ss, |
183 | size_t ss_len, struct stream **streamp), | |
f89b7ce5 | 184 | int (*set_dscp_cb)(int fd, uint8_t dscp), |
c34b65c7 BP |
185 | char *unlink_path, struct pstream **pstreamp) |
186 | { | |
187 | struct fd_pstream *ps = xmalloc(sizeof *ps); | |
188 | pstream_init(&ps->pstream, &fd_pstream_class, name); | |
189 | ps->fd = fd; | |
190 | ps->accept_cb = accept_cb; | |
f89b7ce5 | 191 | ps->set_dscp_cb = set_dscp_cb; |
c34b65c7 BP |
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) { | |
2a022368 | 217 | retval = errno; |
c34b65c7 | 218 | if (retval != EAGAIN) { |
10a89ef0 | 219 | VLOG_DBG_RL(&rl, "accept: %s", ovs_strerror(retval)); |
c34b65c7 BP |
220 | } |
221 | return retval; | |
222 | } | |
223 | ||
224 | retval = set_nonblocking(new_fd); | |
225 | if (retval) { | |
226 | close(new_fd); | |
227 | return retval; | |
228 | } | |
229 | ||
e731d71b | 230 | return ps->accept_cb(new_fd, &ss, ss_len, new_streamp); |
c34b65c7 BP |
231 | } |
232 | ||
233 | static void | |
234 | pfd_wait(struct pstream *pstream) | |
235 | { | |
236 | struct fd_pstream *ps = fd_pstream_cast(pstream); | |
237 | poll_fd_wait(ps->fd, POLLIN); | |
238 | } | |
239 | ||
f89b7ce5 IY |
240 | static int |
241 | pfd_set_dscp(struct pstream *pstream, uint8_t dscp) | |
242 | { | |
243 | struct fd_pstream *ps = fd_pstream_cast(pstream); | |
244 | if (ps->set_dscp_cb) { | |
245 | return ps->set_dscp_cb(ps->fd, dscp); | |
246 | } | |
247 | return 0; | |
248 | } | |
249 | ||
112b76f6 | 250 | static const struct pstream_class fd_pstream_class = { |
c34b65c7 | 251 | "pstream", |
f1936eb6 | 252 | false, |
c34b65c7 BP |
253 | NULL, |
254 | pfd_close, | |
255 | pfd_accept, | |
f89b7ce5 IY |
256 | pfd_wait, |
257 | pfd_set_dscp, | |
c34b65c7 BP |
258 | }; |
259 | \f | |
260 | /* Helper functions. */ | |
261 | static void | |
262 | maybe_unlink_and_free(char *path) | |
263 | { | |
264 | if (path) { | |
265 | fatal_signal_unlink_file_now(path); | |
266 | free(path); | |
267 | } | |
268 | } |