2 * Copyright (c) 2008, 2009 Nicira Networks.
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 "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 struct vconn **vconnp)
59 struct stream_vconn *s;
61 s = xmalloc(sizeof *s);
62 vconn_init(&s->vconn, &stream_vconn_class, connect_status, name);
71 static struct stream_vconn *
72 stream_vconn_cast(struct vconn *vconn)
74 vconn_assert_class(vconn, &stream_vconn_class);
75 return CONTAINER_OF(vconn, struct stream_vconn, vconn);
79 stream_close(struct vconn *vconn)
81 struct stream_vconn *s = stream_vconn_cast(vconn);
82 poll_cancel(s->tx_waiter);
83 stream_clear_txbuf(s);
84 ofpbuf_delete(s->rxbuf);
90 stream_connect(struct vconn *vconn)
92 struct stream_vconn *s = stream_vconn_cast(vconn);
93 return check_connection_completion(s->fd);
97 stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
99 struct stream_vconn *s = stream_vconn_cast(vconn);
104 if (s->rxbuf == NULL) {
105 s->rxbuf = ofpbuf_new(1564);
110 if (sizeof(struct ofp_header) > rx->size) {
111 want_bytes = sizeof(struct ofp_header) - rx->size;
113 struct ofp_header *oh = rx->data;
114 size_t length = ntohs(oh->length);
115 if (length < sizeof(struct ofp_header)) {
116 VLOG_ERR_RL(&rl, "received too-short ofp_header (%zu bytes)",
120 want_bytes = length - rx->size;
127 ofpbuf_prealloc_tailroom(rx, want_bytes);
129 retval = read(s->fd, ofpbuf_tail(rx), want_bytes);
132 if (retval == want_bytes) {
133 if (rx->size > sizeof(struct ofp_header)) {
142 } else if (retval == 0) {
144 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
155 stream_clear_txbuf(struct stream_vconn *s)
157 ofpbuf_delete(s->txbuf);
163 stream_do_tx(int fd UNUSED, short int revents UNUSED, void *vconn_)
165 struct vconn *vconn = vconn_;
166 struct stream_vconn *s = stream_vconn_cast(vconn);
167 ssize_t n = write(s->fd, s->txbuf->data, s->txbuf->size);
169 if (errno != EAGAIN) {
170 VLOG_ERR_RL(&rl, "send: %s", strerror(errno));
171 stream_clear_txbuf(s);
175 ofpbuf_pull(s->txbuf, n);
176 if (!s->txbuf->size) {
177 stream_clear_txbuf(s);
181 s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
185 stream_send(struct vconn *vconn, struct ofpbuf *buffer)
187 struct stream_vconn *s = stream_vconn_cast(vconn);
194 retval = write(s->fd, buffer->data, buffer->size);
195 if (retval == buffer->size) {
196 ofpbuf_delete(buffer);
198 } else if (retval >= 0 || errno == EAGAIN) {
199 leak_checker_claim(buffer);
202 ofpbuf_pull(buffer, retval);
204 s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
212 stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
214 struct stream_vconn *s = stream_vconn_cast(vconn);
217 poll_fd_wait(s->fd, POLLOUT);
222 poll_fd_wait(s->fd, POLLOUT);
224 /* Nothing to do: need to drain txbuf first. */
229 poll_fd_wait(s->fd, POLLIN);
237 static struct vconn_class stream_vconn_class = {
240 stream_close, /* close */
241 stream_connect, /* connect */
242 stream_recv, /* recv */
243 stream_send, /* send */
244 stream_wait, /* wait */
247 /* Passive stream socket vconn. */
249 struct pstream_pvconn
251 struct pvconn pvconn;
253 int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
257 static struct pvconn_class pstream_pvconn_class;
259 static struct pstream_pvconn *
260 pstream_pvconn_cast(struct pvconn *pvconn)
262 pvconn_assert_class(pvconn, &pstream_pvconn_class);
263 return CONTAINER_OF(pvconn, struct pstream_pvconn, pvconn);
267 new_pstream_pvconn(const char *name, int fd,
268 int (*accept_cb)(int fd, const struct sockaddr *,
269 size_t sa_len, struct vconn **),
270 struct pvconn **pvconnp)
272 struct pstream_pvconn *ps = xmalloc(sizeof *ps);
273 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name);
275 ps->accept_cb = accept_cb;
276 *pvconnp = &ps->pvconn;
281 pstream_close(struct pvconn *pvconn)
283 struct pstream_pvconn *ps = pstream_pvconn_cast(pvconn);
289 pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
291 struct pstream_pvconn *ps = pstream_pvconn_cast(pvconn);
292 struct sockaddr_storage ss;
293 socklen_t ss_len = sizeof ss;
297 new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
300 if (retval != EAGAIN) {
301 VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
306 retval = set_nonblocking(new_fd);
312 return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
317 pstream_wait(struct pvconn *pvconn)
319 struct pstream_pvconn *ps = pstream_pvconn_cast(pvconn);
320 poll_fd_wait(ps->fd, POLLIN);
323 static struct pvconn_class pstream_pvconn_class = {