1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
35 #include "vconn-stream.h"
41 #include <sys/types.h>
46 #include "poll-loop.h"
47 #include "socket-util.h"
49 #include "vconn-provider.h"
52 #define THIS_MODULE VLM_vconn_stream
54 /* Active stream socket vconn. */
62 struct poll_waiter *tx_waiter;
65 static struct vconn_class stream_vconn_class;
67 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
70 new_stream_vconn(const char *name, int fd, int connect_status,
71 uint32_t ip, struct vconn **vconnp)
73 struct stream_vconn *s;
75 s = xmalloc(sizeof *s);
76 vconn_init(&s->vconn, &stream_vconn_class, connect_status, ip, name);
85 static struct stream_vconn *
86 stream_vconn_cast(struct vconn *vconn)
88 assert(vconn->class == &stream_vconn_class);
89 return CONTAINER_OF(vconn, struct stream_vconn, vconn);
93 stream_close(struct vconn *vconn)
95 struct stream_vconn *s = stream_vconn_cast(vconn);
96 poll_cancel(s->tx_waiter);
102 stream_connect(struct vconn *vconn)
104 struct stream_vconn *s = stream_vconn_cast(vconn);
105 return check_connection_completion(s->fd);
109 stream_recv(struct vconn *vconn, struct buffer **bufferp)
111 struct stream_vconn *s = stream_vconn_cast(vconn);
116 if (s->rxbuf == NULL) {
117 s->rxbuf = buffer_new(1564);
122 if (sizeof(struct ofp_header) > rx->size) {
123 want_bytes = sizeof(struct ofp_header) - rx->size;
125 struct ofp_header *oh = rx->data;
126 size_t length = ntohs(oh->length);
127 if (length < sizeof(struct ofp_header)) {
128 VLOG_ERR_RL(&rl, "received too-short ofp_header (%zu bytes)",
132 want_bytes = length - rx->size;
139 buffer_prealloc_tailroom(rx, want_bytes);
141 retval = read(s->fd, buffer_tail(rx), want_bytes);
144 if (retval == want_bytes) {
145 if (rx->size > sizeof(struct ofp_header)) {
154 } else if (retval == 0) {
156 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
162 return retval ? errno : EAGAIN;
167 stream_clear_txbuf(struct stream_vconn *s)
169 buffer_delete(s->txbuf);
175 stream_do_tx(int fd UNUSED, short int revents UNUSED, void *vconn_)
177 struct vconn *vconn = vconn_;
178 struct stream_vconn *s = stream_vconn_cast(vconn);
179 ssize_t n = write(s->fd, s->txbuf->data, s->txbuf->size);
181 if (errno != EAGAIN) {
182 VLOG_ERR_RL(&rl, "send: %s", strerror(errno));
183 stream_clear_txbuf(s);
187 buffer_pull(s->txbuf, n);
188 if (!s->txbuf->size) {
189 stream_clear_txbuf(s);
193 s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
197 stream_send(struct vconn *vconn, struct buffer *buffer)
199 struct stream_vconn *s = stream_vconn_cast(vconn);
206 retval = write(s->fd, buffer->data, buffer->size);
207 if (retval == buffer->size) {
208 buffer_delete(buffer);
210 } else if (retval >= 0 || errno == EAGAIN) {
213 buffer_pull(buffer, retval);
215 s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
223 stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
225 struct stream_vconn *s = stream_vconn_cast(vconn);
228 poll_fd_wait(s->fd, POLLOUT);
233 poll_fd_wait(s->fd, POLLOUT);
235 /* Nothing to do: need to drain txbuf first. */
240 poll_fd_wait(s->fd, POLLIN);
248 static struct vconn_class stream_vconn_class = {
250 .close = stream_close,
251 .connect = stream_connect,
257 /* Passive stream socket vconn. */
263 int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
267 static struct vconn_class pstream_vconn_class;
269 static struct pstream_vconn *
270 pstream_vconn_cast(struct vconn *vconn)
272 assert(vconn->class == &pstream_vconn_class);
273 return CONTAINER_OF(vconn, struct pstream_vconn, vconn);
277 new_pstream_vconn(const char *name, int fd,
278 int (*accept_cb)(int fd, const struct sockaddr *,
279 size_t sa_len, struct vconn **),
280 struct vconn **vconnp)
282 struct pstream_vconn *ps;
285 retval = set_nonblocking(fd);
291 if (listen(fd, 10) < 0) {
293 VLOG_ERR("%s: listen: %s", name, strerror(error));
298 ps = xmalloc(sizeof *ps);
299 vconn_init(&ps->vconn, &pstream_vconn_class, 0, 0, name);
301 ps->accept_cb = accept_cb;
302 *vconnp = &ps->vconn;
307 pstream_close(struct vconn *vconn)
309 struct pstream_vconn *ps = pstream_vconn_cast(vconn);
315 pstream_accept(struct vconn *vconn, struct vconn **new_vconnp)
317 struct pstream_vconn *ps = pstream_vconn_cast(vconn);
318 struct sockaddr_storage ss;
319 socklen_t ss_len = sizeof ss;
323 new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
326 if (retval != EAGAIN) {
327 VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
332 retval = set_nonblocking(new_fd);
338 return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
343 pstream_wait(struct vconn *vconn, enum vconn_wait_type wait)
345 struct pstream_vconn *ps = pstream_vconn_cast(vconn);
346 assert(wait == WAIT_ACCEPT);
347 poll_fd_wait(ps->fd, POLLIN);
350 static struct vconn_class pstream_vconn_class = {
352 .close = pstream_close,
353 .accept = pstream_accept,