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);
67 s->vconn.remote_ip = stream_get_remote_ip(stream);
68 s->vconn.remote_port = stream_get_remote_port(stream);
69 s->vconn.local_ip = stream_get_local_ip(stream);
70 s->vconn.local_port = stream_get_local_port(stream);
74 /* Creates a new vconn that will send and receive data on a stream named 'name'
75 * and stores a pointer to the vconn in '*vconnp'.
77 * Returns 0 if successful, otherwise a positive errno value. */
79 vconn_stream_open(const char *name, uint32_t allowed_versions,
80 char *suffix OVS_UNUSED, struct vconn **vconnp, uint8_t dscp)
82 struct stream *stream;
85 error = stream_open_with_default_port(name, OFP_OLD_PORT, &stream, dscp);
87 error = stream_connect(stream);
88 if (!error || error == EAGAIN) {
89 *vconnp = vconn_stream_new(stream, error, allowed_versions);
98 static struct vconn_stream *
99 vconn_stream_cast(struct vconn *vconn)
101 return CONTAINER_OF(vconn, struct vconn_stream, vconn);
105 vconn_stream_close(struct vconn *vconn)
107 struct vconn_stream *s = vconn_stream_cast(vconn);
109 if ((vconn->error == EPROTO || s->n_packets < 1) && s->rxbuf) {
110 stream_report_content(s->rxbuf->data, s->rxbuf->size, STREAM_OPENFLOW,
111 THIS_MODULE, vconn_get_name(vconn));
114 stream_close(s->stream);
115 vconn_stream_clear_txbuf(s);
116 ofpbuf_delete(s->rxbuf);
121 vconn_stream_connect(struct vconn *vconn)
123 struct vconn_stream *s = vconn_stream_cast(vconn);
124 return stream_connect(s->stream);
128 vconn_stream_recv__(struct vconn_stream *s, int rx_len)
130 struct ofpbuf *rx = s->rxbuf;
131 int want_bytes, retval;
133 want_bytes = rx_len - rx->size;
134 ofpbuf_prealloc_tailroom(rx, want_bytes);
135 retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
138 return retval == want_bytes ? 0 : EAGAIN;
139 } else if (retval == 0) {
141 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
151 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
153 struct vconn_stream *s = vconn_stream_cast(vconn);
154 const struct ofp_header *oh;
157 /* Allocate new receive buffer if we don't have one. */
158 if (s->rxbuf == NULL) {
159 s->rxbuf = ofpbuf_new(1564);
162 /* Read ofp_header. */
163 if (s->rxbuf->size < sizeof(struct ofp_header)) {
164 int retval = vconn_stream_recv__(s, sizeof(struct ofp_header));
172 rx_len = ntohs(oh->length);
173 if (rx_len < sizeof(struct ofp_header)) {
174 VLOG_ERR_RL(&rl, "received too-short ofp_header (%d bytes)", rx_len);
176 } else if (s->rxbuf->size < rx_len) {
177 int retval = vconn_stream_recv__(s, rx_len);
190 vconn_stream_clear_txbuf(struct vconn_stream *s)
192 ofpbuf_delete(s->txbuf);
197 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
199 struct vconn_stream *s = vconn_stream_cast(vconn);
206 retval = stream_send(s->stream, buffer->data, buffer->size);
207 if (retval == buffer->size) {
208 ofpbuf_delete(buffer);
210 } else if (retval >= 0 || retval == -EAGAIN) {
213 ofpbuf_pull(buffer, retval);
222 vconn_stream_run(struct vconn *vconn)
224 struct vconn_stream *s = vconn_stream_cast(vconn);
227 stream_run(s->stream);
232 retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
234 if (retval != -EAGAIN) {
235 VLOG_ERR_RL(&rl, "send: %s", ovs_strerror(-retval));
236 vconn_stream_clear_txbuf(s);
239 } else if (retval > 0) {
240 ofpbuf_pull(s->txbuf, retval);
241 if (!s->txbuf->size) {
242 vconn_stream_clear_txbuf(s);
249 vconn_stream_run_wait(struct vconn *vconn)
251 struct vconn_stream *s = vconn_stream_cast(vconn);
253 stream_run_wait(s->stream);
255 stream_send_wait(s->stream);
260 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
262 struct vconn_stream *s = vconn_stream_cast(vconn);
265 stream_connect_wait(s->stream);
270 stream_send_wait(s->stream);
272 /* Nothing to do: need to drain txbuf first.
273 * vconn_stream_run_wait() will arrange to wake up when there room
274 * to send data, so there's no point in calling poll_fd_wait()
275 * redundantly here. */
280 stream_recv_wait(s->stream);
288 /* Passive stream socket vconn. */
290 struct pvconn_pstream
292 struct pvconn pvconn;
293 struct pstream *pstream;
296 static const struct pvconn_class pstream_pvconn_class;
298 static struct pvconn_pstream *
299 pvconn_pstream_cast(struct pvconn *pvconn)
301 return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
304 /* Creates a new pvconn named 'name' that will accept new connections using
305 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
307 * Returns 0 if successful, otherwise a positive errno value. (The current
308 * implementation never fails.) */
310 pvconn_pstream_listen(const char *name, uint32_t allowed_versions,
311 char *suffix OVS_UNUSED, struct pvconn **pvconnp,
314 struct pvconn_pstream *ps;
315 struct pstream *pstream;
318 error = pstream_open_with_default_port(name, OFP_OLD_PORT,
324 ps = xmalloc(sizeof *ps);
325 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name, allowed_versions);
326 ps->pstream = pstream;
327 *pvconnp = &ps->pvconn;
332 pvconn_pstream_close(struct pvconn *pvconn)
334 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
335 pstream_close(ps->pstream);
340 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
342 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
343 struct stream *stream;
346 error = pstream_accept(ps->pstream, &stream);
348 if (error != EAGAIN) {
349 VLOG_DBG_RL(&rl, "%s: accept: %s",
350 pstream_get_name(ps->pstream), ovs_strerror(error));
355 *new_vconnp = vconn_stream_new(stream, 0, pvconn->allowed_versions);
360 pvconn_pstream_wait(struct pvconn *pvconn)
362 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
363 pstream_wait(ps->pstream);
366 /* Stream-based vconns and pvconns. */
368 #define STREAM_INIT(NAME) \
372 vconn_stream_close, \
373 vconn_stream_connect, \
377 vconn_stream_run_wait, \
381 #define PSTREAM_INIT(NAME) \
384 pvconn_pstream_listen, \
385 pvconn_pstream_close, \
386 pvconn_pstream_accept, \
387 pvconn_pstream_wait \
390 static const struct vconn_class stream_vconn_class = STREAM_INIT("stream");
391 static const struct pvconn_class pstream_pvconn_class = PSTREAM_INIT("pstream");
393 const struct vconn_class tcp_vconn_class = STREAM_INIT("tcp");
394 const struct pvconn_class ptcp_pvconn_class = PSTREAM_INIT("ptcp");
396 const struct vconn_class unix_vconn_class = STREAM_INIT("unix");
397 const struct pvconn_class punix_pvconn_class = PSTREAM_INIT("punix");
400 const struct vconn_class ssl_vconn_class = STREAM_INIT("ssl");
401 const struct pvconn_class pssl_pvconn_class = PSTREAM_INIT("pssl");