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"
51 #define THIS_MODULE VLM_vconn_stream
53 /* Active stream socket vconn. */
61 struct poll_waiter *tx_waiter;
64 static struct vconn_class stream_vconn_class;
67 new_stream_vconn(const char *name, int fd, int connect_status,
68 uint32_t ip, struct vconn **vconnp)
70 struct stream_vconn *s;
72 s = xmalloc(sizeof *s);
73 s->vconn.class = &stream_vconn_class;
74 s->vconn.connect_status = connect_status;
84 static struct stream_vconn *
85 stream_vconn_cast(struct vconn *vconn)
87 assert(vconn->class == &stream_vconn_class);
88 return CONTAINER_OF(vconn, struct stream_vconn, vconn);
92 stream_close(struct vconn *vconn)
94 struct stream_vconn *s = stream_vconn_cast(vconn);
95 poll_cancel(s->tx_waiter);
101 stream_connect(struct vconn *vconn)
103 struct stream_vconn *s = stream_vconn_cast(vconn);
104 return check_connection_completion(s->fd);
108 stream_recv(struct vconn *vconn, struct buffer **bufferp)
110 struct stream_vconn *s = stream_vconn_cast(vconn);
115 if (s->rxbuf == NULL) {
116 s->rxbuf = buffer_new(1564);
121 if (sizeof(struct ofp_header) > rx->size) {
122 want_bytes = sizeof(struct ofp_header) - rx->size;
124 struct ofp_header *oh = rx->data;
125 size_t length = ntohs(oh->length);
126 if (length < sizeof(struct ofp_header)) {
127 VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
130 want_bytes = length - rx->size;
137 buffer_prealloc_tailroom(rx, want_bytes);
139 retval = read(s->fd, buffer_tail(rx), want_bytes);
142 if (retval == want_bytes) {
143 if (rx->size > sizeof(struct ofp_header)) {
152 } else if (retval == 0) {
154 VLOG_ERR("connection dropped mid-packet");
160 return retval ? errno : EAGAIN;
165 stream_clear_txbuf(struct stream_vconn *s)
167 buffer_delete(s->txbuf);
173 stream_do_tx(int fd UNUSED, short int revents UNUSED, void *vconn_)
175 struct vconn *vconn = vconn_;
176 struct stream_vconn *s = stream_vconn_cast(vconn);
177 ssize_t n = write(s->fd, s->txbuf->data, s->txbuf->size);
179 if (errno != EAGAIN) {
180 VLOG_ERR("send: %s", strerror(errno));
181 stream_clear_txbuf(s);
185 buffer_pull(s->txbuf, n);
186 if (!s->txbuf->size) {
187 stream_clear_txbuf(s);
191 s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
195 stream_send(struct vconn *vconn, struct buffer *buffer)
197 struct stream_vconn *s = stream_vconn_cast(vconn);
204 retval = write(s->fd, buffer->data, buffer->size);
205 if (retval == buffer->size) {
206 buffer_delete(buffer);
208 } else if (retval >= 0 || errno == EAGAIN) {
211 buffer_pull(buffer, retval);
213 s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
221 stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
223 struct stream_vconn *s = stream_vconn_cast(vconn);
226 poll_fd_wait(s->fd, POLLOUT);
231 poll_fd_wait(s->fd, POLLOUT);
233 /* Nothing to do: need to drain txbuf first. */
238 poll_fd_wait(s->fd, POLLIN);
246 static struct vconn_class stream_vconn_class = {
248 .close = stream_close,
249 .connect = stream_connect,
255 /* Passive stream socket vconn. */
261 int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
265 static struct vconn_class pstream_vconn_class;
267 static struct pstream_vconn *
268 pstream_vconn_cast(struct vconn *vconn)
270 assert(vconn->class == &pstream_vconn_class);
271 return CONTAINER_OF(vconn, struct pstream_vconn, vconn);
275 new_pstream_vconn(const char *name, int fd,
276 int (*accept_cb)(int fd, const struct sockaddr *,
277 size_t sa_len, struct vconn **),
278 struct vconn **vconnp)
280 struct pstream_vconn *ps;
283 retval = set_nonblocking(fd);
289 if (listen(fd, 10) < 0) {
291 VLOG_ERR("%s: listen: %s", name, strerror(error));
296 ps = xmalloc(sizeof *ps);
297 ps->vconn.class = &pstream_vconn_class;
298 ps->vconn.connect_status = 0;
300 ps->accept_cb = accept_cb;
301 *vconnp = &ps->vconn;
306 pstream_close(struct vconn *vconn)
308 struct pstream_vconn *ps = pstream_vconn_cast(vconn);
314 pstream_accept(struct vconn *vconn, struct vconn **new_vconnp)
316 struct pstream_vconn *ps = pstream_vconn_cast(vconn);
317 struct sockaddr_storage ss;
318 socklen_t ss_len = sizeof ss;
322 new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
325 if (retval != EAGAIN) {
326 VLOG_DBG("accept: %s", strerror(retval));
331 retval = set_nonblocking(new_fd);
337 return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
342 pstream_wait(struct vconn *vconn, enum vconn_wait_type wait)
344 struct pstream_vconn *ps = pstream_vconn_cast(vconn);
345 assert(wait == WAIT_ACCEPT);
346 poll_fd_wait(ps->fd, POLLIN);
349 static struct vconn_class pstream_vconn_class = {
351 .close = pstream_close,
352 .accept = pstream_accept,