2 * Copyright (c) 2008, 2009, 2010, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "stream-fd.h"
23 #include <sys/socket.h>
24 #include <sys/types.h>
26 #include "fatal-signal.h"
27 #include "poll-loop.h"
28 #include "socket-util.h"
30 #include "stream-provider.h"
34 VLOG_DEFINE_THIS_MODULE(stream_fd
);
36 /* Active file descriptor stream. */
44 static const struct stream_class stream_fd_class
;
46 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 25);
48 static void maybe_unlink_and_free(char *path
);
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().
54 * Returns 0 if successful, otherwise a positive errno value. (The current
55 * implementation never fails.) */
57 new_fd_stream(const char *name
, int fd
, int connect_status
,
58 struct stream
**streamp
)
62 s
= xmalloc(sizeof *s
);
63 stream_init(&s
->stream
, &stream_fd_class
, connect_status
, name
);
65 *streamp
= &s
->stream
;
69 static struct stream_fd
*
70 stream_fd_cast(struct stream
*stream
)
72 stream_assert_class(stream
, &stream_fd_class
);
73 return CONTAINER_OF(stream
, struct stream_fd
, stream
);
77 fd_close(struct stream
*stream
)
79 struct stream_fd
*s
= stream_fd_cast(stream
);
85 fd_connect(struct stream
*stream
)
87 struct stream_fd
*s
= stream_fd_cast(stream
);
88 return check_connection_completion(s
->fd
);
92 fd_recv(struct stream
*stream
, void *buffer
, size_t n
)
94 struct stream_fd
*s
= stream_fd_cast(stream
);
97 retval
= read(s
->fd
, buffer
, n
);
98 return retval
>= 0 ? retval
: -errno
;
102 fd_send(struct stream
*stream
, const void *buffer
, size_t n
)
104 struct stream_fd
*s
= stream_fd_cast(stream
);
107 retval
= write(s
->fd
, buffer
, n
);
108 return (retval
> 0 ? retval
109 : retval
== 0 ? -EAGAIN
114 fd_wait(struct stream
*stream
, enum stream_wait_type wait
)
116 struct stream_fd
*s
= stream_fd_cast(stream
);
120 poll_fd_wait(s
->fd
, POLLOUT
);
124 poll_fd_wait(s
->fd
, POLLIN
);
132 static const struct stream_class stream_fd_class
= {
134 false, /* needs_probes */
136 fd_close
, /* close */
137 fd_connect
, /* connect */
145 /* Passive file descriptor stream. */
149 struct pstream pstream
;
151 int (*accept_cb
)(int fd
, const struct sockaddr
*, size_t sa_len
,
153 int (*set_dscp_cb
)(int fd
, uint8_t dscp
);
157 static const struct pstream_class fd_pstream_class
;
159 static struct fd_pstream
*
160 fd_pstream_cast(struct pstream
*pstream
)
162 pstream_assert_class(pstream
, &fd_pstream_class
);
163 return CONTAINER_OF(pstream
, struct fd_pstream
, pstream
);
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'.
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.
175 * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
176 * to fatal_signal_unlink_file_now() and freed with free().
178 * Returns 0 if successful, otherwise a positive errno value. (The current
179 * implementation never fails.) */
181 new_fd_pstream(const char *name
, int fd
,
182 int (*accept_cb
)(int fd
, const struct sockaddr
*sa
,
183 size_t sa_len
, struct stream
**streamp
),
184 int (*set_dscp_cb
)(int fd
, uint8_t dscp
),
185 char *unlink_path
, struct pstream
**pstreamp
)
187 struct fd_pstream
*ps
= xmalloc(sizeof *ps
);
188 pstream_init(&ps
->pstream
, &fd_pstream_class
, name
);
190 ps
->accept_cb
= accept_cb
;
191 ps
->set_dscp_cb
= set_dscp_cb
;
192 ps
->unlink_path
= unlink_path
;
193 *pstreamp
= &ps
->pstream
;
198 pfd_close(struct pstream
*pstream
)
200 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
202 maybe_unlink_and_free(ps
->unlink_path
);
207 pfd_accept(struct pstream
*pstream
, struct stream
**new_streamp
)
209 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
210 struct sockaddr_storage ss
;
211 socklen_t ss_len
= sizeof ss
;
215 new_fd
= accept(ps
->fd
, (struct sockaddr
*) &ss
, &ss_len
);
218 if (retval
!= EAGAIN
) {
219 VLOG_DBG_RL(&rl
, "accept: %s", ovs_strerror(retval
));
224 retval
= set_nonblocking(new_fd
);
230 return ps
->accept_cb(new_fd
, (const struct sockaddr
*) &ss
, ss_len
,
235 pfd_wait(struct pstream
*pstream
)
237 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
238 poll_fd_wait(ps
->fd
, POLLIN
);
242 pfd_set_dscp(struct pstream
*pstream
, uint8_t dscp
)
244 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
245 if (ps
->set_dscp_cb
) {
246 return ps
->set_dscp_cb(ps
->fd
, dscp
);
251 static const struct pstream_class fd_pstream_class
= {
261 /* Helper functions. */
263 maybe_unlink_and_free(char *path
)
266 fatal_signal_unlink_file_now(path
);