2 * Copyright (c) 2008, 2009, 2010, 2011, 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.
22 #include <sys/types.h>
24 #include "fatal-signal.h"
26 #include "openflow/openflow.h"
27 #include "poll-loop.h"
28 #include "socket-util.h"
31 #include "vconn-provider.h"
35 VLOG_DEFINE_THIS_MODULE(vconn_stream);
37 /* Active stream socket vconn. */
42 struct stream *stream;
48 static const struct vconn_class stream_vconn_class;
50 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
52 static void vconn_stream_clear_txbuf(struct vconn_stream *);
55 vconn_stream_new(struct stream *stream, int connect_status,
56 uint32_t allowed_versions)
58 struct vconn_stream *s;
60 s = xmalloc(sizeof *s);
61 vconn_init(&s->vconn, &stream_vconn_class, connect_status,
62 stream_get_name(stream), allowed_versions);
70 /* Creates a new vconn that will send and receive data on a stream named 'name'
71 * and stores a pointer to the vconn in '*vconnp'.
73 * Returns 0 if successful, otherwise a positive errno value. */
75 vconn_stream_open(const char *name, uint32_t allowed_versions,
76 char *suffix OVS_UNUSED, struct vconn **vconnp, uint8_t dscp)
78 struct stream *stream;
81 error = stream_open_with_default_port(name, OFP_OLD_PORT, &stream, dscp);
83 error = stream_connect(stream);
84 if (!error || error == EAGAIN) {
85 *vconnp = vconn_stream_new(stream, error, allowed_versions);
94 static struct vconn_stream *
95 vconn_stream_cast(struct vconn *vconn)
97 return CONTAINER_OF(vconn, struct vconn_stream, vconn);
101 vconn_stream_close(struct vconn *vconn)
103 struct vconn_stream *s = vconn_stream_cast(vconn);
105 if ((vconn->error == EPROTO || s->n_packets < 1) && s->rxbuf) {
106 stream_report_content(s->rxbuf->data, s->rxbuf->size, STREAM_OPENFLOW,
107 THIS_MODULE, vconn_get_name(vconn));
110 stream_close(s->stream);
111 vconn_stream_clear_txbuf(s);
112 ofpbuf_delete(s->rxbuf);
117 vconn_stream_connect(struct vconn *vconn)
119 struct vconn_stream *s = vconn_stream_cast(vconn);
120 return stream_connect(s->stream);
124 vconn_stream_recv__(struct vconn_stream *s, int rx_len)
126 struct ofpbuf *rx = s->rxbuf;
127 int want_bytes, retval;
129 want_bytes = rx_len - rx->size;
130 ofpbuf_prealloc_tailroom(rx, want_bytes);
131 retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
134 return retval == want_bytes ? 0 : EAGAIN;
135 } else if (retval == 0) {
137 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
147 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
149 struct vconn_stream *s = vconn_stream_cast(vconn);
150 const struct ofp_header *oh;
153 /* Allocate new receive buffer if we don't have one. */
154 if (s->rxbuf == NULL) {
155 s->rxbuf = ofpbuf_new(1564);
158 /* Read ofp_header. */
159 if (s->rxbuf->size < sizeof(struct ofp_header)) {
160 int retval = vconn_stream_recv__(s, sizeof(struct ofp_header));
168 rx_len = ntohs(oh->length);
169 if (rx_len < sizeof(struct ofp_header)) {
170 VLOG_ERR_RL(&rl, "received too-short ofp_header (%d bytes)", rx_len);
172 } else if (s->rxbuf->size < rx_len) {
173 int retval = vconn_stream_recv__(s, rx_len);
186 vconn_stream_clear_txbuf(struct vconn_stream *s)
188 ofpbuf_delete(s->txbuf);
193 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
195 struct vconn_stream *s = vconn_stream_cast(vconn);
202 retval = stream_send(s->stream, buffer->data, buffer->size);
203 if (retval == buffer->size) {
204 ofpbuf_delete(buffer);
206 } else if (retval >= 0 || retval == -EAGAIN) {
209 ofpbuf_pull(buffer, retval);
218 vconn_stream_run(struct vconn *vconn)
220 struct vconn_stream *s = vconn_stream_cast(vconn);
223 stream_run(s->stream);
228 retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
230 if (retval != -EAGAIN) {
231 VLOG_ERR_RL(&rl, "send: %s", ovs_strerror(-retval));
232 vconn_stream_clear_txbuf(s);
235 } else if (retval > 0) {
236 ofpbuf_pull(s->txbuf, retval);
237 if (!s->txbuf->size) {
238 vconn_stream_clear_txbuf(s);
245 vconn_stream_run_wait(struct vconn *vconn)
247 struct vconn_stream *s = vconn_stream_cast(vconn);
249 stream_run_wait(s->stream);
251 stream_send_wait(s->stream);
256 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
258 struct vconn_stream *s = vconn_stream_cast(vconn);
261 stream_connect_wait(s->stream);
266 stream_send_wait(s->stream);
268 /* Nothing to do: need to drain txbuf first.
269 * vconn_stream_run_wait() will arrange to wake up when there room
270 * to send data, so there's no point in calling poll_fd_wait()
271 * redundantly here. */
276 stream_recv_wait(s->stream);
284 /* Passive stream socket vconn. */
286 struct pvconn_pstream
288 struct pvconn pvconn;
289 struct pstream *pstream;
292 static const struct pvconn_class pstream_pvconn_class;
294 static struct pvconn_pstream *
295 pvconn_pstream_cast(struct pvconn *pvconn)
297 return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
300 /* Creates a new pvconn named 'name' that will accept new connections using
301 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
303 * Returns 0 if successful, otherwise a positive errno value. (The current
304 * implementation never fails.) */
306 pvconn_pstream_listen(const char *name, uint32_t allowed_versions,
307 char *suffix OVS_UNUSED, struct pvconn **pvconnp,
310 struct pvconn_pstream *ps;
311 struct pstream *pstream;
314 error = pstream_open_with_default_port(name, OFP_OLD_PORT,
320 ps = xmalloc(sizeof *ps);
321 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name, allowed_versions);
322 ps->pstream = pstream;
323 *pvconnp = &ps->pvconn;
328 pvconn_pstream_close(struct pvconn *pvconn)
330 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
331 pstream_close(ps->pstream);
336 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
338 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
339 struct stream *stream;
342 error = pstream_accept(ps->pstream, &stream);
344 if (error != EAGAIN) {
345 VLOG_DBG_RL(&rl, "%s: accept: %s",
346 pstream_get_name(ps->pstream), ovs_strerror(error));
351 *new_vconnp = vconn_stream_new(stream, 0, pvconn->allowed_versions);
356 pvconn_pstream_wait(struct pvconn *pvconn)
358 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
359 pstream_wait(ps->pstream);
362 /* Stream-based vconns and pvconns. */
364 #define STREAM_INIT(NAME) \
368 vconn_stream_close, \
369 vconn_stream_connect, \
373 vconn_stream_run_wait, \
377 #define PSTREAM_INIT(NAME) \
380 pvconn_pstream_listen, \
381 pvconn_pstream_close, \
382 pvconn_pstream_accept, \
383 pvconn_pstream_wait \
386 static const struct vconn_class stream_vconn_class = STREAM_INIT("stream");
387 static const struct pvconn_class pstream_pvconn_class = PSTREAM_INIT("pstream");
389 const struct vconn_class tcp_vconn_class = STREAM_INIT("tcp");
390 const struct pvconn_class ptcp_pvconn_class = PSTREAM_INIT("ptcp");
392 const struct vconn_class unix_vconn_class = STREAM_INIT("unix");
393 const struct pvconn_class punix_pvconn_class = PSTREAM_INIT("punix");
396 const struct vconn_class ssl_vconn_class = STREAM_INIT("ssl");
397 const struct pvconn_class pssl_pvconn_class = PSTREAM_INIT("pssl");