2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "vconn-stream.h"
24 #include <sys/types.h>
26 #include "leak-checker.h"
28 #include "openflow/openflow.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
32 #include "vconn-provider.h"
36 #define THIS_MODULE VLM_vconn_stream
38 /* Active stream socket vconn. */
46 struct poll_waiter *tx_waiter;
49 static struct vconn_class stream_vconn_class;
51 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
53 static void stream_clear_txbuf(struct stream_vconn *);
56 new_stream_vconn(const char *name, int fd, int connect_status,
57 uint32_t ip, bool reconnectable, struct vconn **vconnp)
59 struct stream_vconn *s;
61 s = xmalloc(sizeof *s);
62 vconn_init(&s->vconn, &stream_vconn_class, connect_status, ip, name,
72 static struct stream_vconn *
73 stream_vconn_cast(struct vconn *vconn)
75 vconn_assert_class(vconn, &stream_vconn_class);
76 return CONTAINER_OF(vconn, struct stream_vconn, vconn);
80 stream_close(struct vconn *vconn)
82 struct stream_vconn *s = stream_vconn_cast(vconn);
83 poll_cancel(s->tx_waiter);
84 stream_clear_txbuf(s);
85 ofpbuf_delete(s->rxbuf);
91 stream_connect(struct vconn *vconn)
93 struct stream_vconn *s = stream_vconn_cast(vconn);
94 return check_connection_completion(s->fd);
98 stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
100 struct stream_vconn *s = stream_vconn_cast(vconn);
105 if (s->rxbuf == NULL) {
106 s->rxbuf = ofpbuf_new(1564);
111 if (sizeof(struct ofp_header) > rx->size) {
112 want_bytes = sizeof(struct ofp_header) - rx->size;
114 struct ofp_header *oh = rx->data;
115 size_t length = ntohs(oh->length);
116 if (length < sizeof(struct ofp_header)) {
117 VLOG_ERR_RL(&rl, "received too-short ofp_header (%zu bytes)",
121 want_bytes = length - rx->size;
128 ofpbuf_prealloc_tailroom(rx, want_bytes);
130 retval = read(s->fd, ofpbuf_tail(rx), want_bytes);
133 if (retval == want_bytes) {
134 if (rx->size > sizeof(struct ofp_header)) {
143 } else if (retval == 0) {
145 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
156 stream_clear_txbuf(struct stream_vconn *s)
158 ofpbuf_delete(s->txbuf);
164 stream_do_tx(int fd UNUSED, short int revents UNUSED, void *vconn_)
166 struct vconn *vconn = vconn_;
167 struct stream_vconn *s = stream_vconn_cast(vconn);
168 ssize_t n = write(s->fd, s->txbuf->data, s->txbuf->size);
170 if (errno != EAGAIN) {
171 VLOG_ERR_RL(&rl, "send: %s", strerror(errno));
172 stream_clear_txbuf(s);
176 ofpbuf_pull(s->txbuf, n);
177 if (!s->txbuf->size) {
178 stream_clear_txbuf(s);
182 s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
186 stream_send(struct vconn *vconn, struct ofpbuf *buffer)
188 struct stream_vconn *s = stream_vconn_cast(vconn);
195 retval = write(s->fd, buffer->data, buffer->size);
196 if (retval == buffer->size) {
197 ofpbuf_delete(buffer);
199 } else if (retval >= 0 || errno == EAGAIN) {
200 leak_checker_claim(buffer);
203 ofpbuf_pull(buffer, retval);
205 s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
213 stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
215 struct stream_vconn *s = stream_vconn_cast(vconn);
218 poll_fd_wait(s->fd, POLLOUT);
223 poll_fd_wait(s->fd, POLLOUT);
225 /* Nothing to do: need to drain txbuf first. */
230 poll_fd_wait(s->fd, POLLIN);
238 static struct vconn_class stream_vconn_class = {
241 stream_close, /* close */
242 stream_connect, /* connect */
243 stream_recv, /* recv */
244 stream_send, /* send */
245 stream_wait, /* wait */
248 /* Passive stream socket vconn. */
250 struct pstream_pvconn
252 struct pvconn pvconn;
254 int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
258 static struct pvconn_class pstream_pvconn_class;
260 static struct pstream_pvconn *
261 pstream_pvconn_cast(struct pvconn *pvconn)
263 pvconn_assert_class(pvconn, &pstream_pvconn_class);
264 return CONTAINER_OF(pvconn, struct pstream_pvconn, pvconn);
268 new_pstream_pvconn(const char *name, int fd,
269 int (*accept_cb)(int fd, const struct sockaddr *,
270 size_t sa_len, struct vconn **),
271 struct pvconn **pvconnp)
273 struct pstream_pvconn *ps;
276 retval = set_nonblocking(fd);
282 if (listen(fd, 10) < 0) {
284 VLOG_ERR("%s: listen: %s", name, strerror(error));
289 ps = xmalloc(sizeof *ps);
290 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name);
292 ps->accept_cb = accept_cb;
293 *pvconnp = &ps->pvconn;
298 pstream_close(struct pvconn *pvconn)
300 struct pstream_pvconn *ps = pstream_pvconn_cast(pvconn);
306 pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
308 struct pstream_pvconn *ps = pstream_pvconn_cast(pvconn);
309 struct sockaddr_storage ss;
310 socklen_t ss_len = sizeof ss;
314 new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
317 if (retval != EAGAIN) {
318 VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
323 retval = set_nonblocking(new_fd);
329 return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
334 pstream_wait(struct pvconn *pvconn)
336 struct pstream_pvconn *ps = pstream_pvconn_cast(pvconn);
337 poll_fd_wait(ps->fd, POLLIN);
340 static struct pvconn_class pstream_pvconn_class = {