2 * Copyright (c) 2008, 2009, 2010, 2011 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.
23 #include <sys/types.h>
25 #include "fatal-signal.h"
26 #include "leak-checker.h"
28 #include "openflow/openflow.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
33 #include "vconn-provider.h"
37 VLOG_DEFINE_THIS_MODULE(vconn_stream);
39 /* Active stream socket vconn. */
44 struct stream *stream;
50 static struct vconn_class stream_vconn_class;
52 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
54 static void vconn_stream_clear_txbuf(struct vconn_stream *);
57 vconn_stream_new(struct stream *stream, int connect_status,
58 uint32_t allowed_versions)
60 struct vconn_stream *s;
62 s = xmalloc(sizeof *s);
63 vconn_init(&s->vconn, &stream_vconn_class, connect_status,
64 stream_get_name(stream), allowed_versions);
69 s->vconn.remote_ip = stream_get_remote_ip(stream);
70 s->vconn.remote_port = stream_get_remote_port(stream);
71 s->vconn.local_ip = stream_get_local_ip(stream);
72 s->vconn.local_port = stream_get_local_port(stream);
76 /* Creates a new vconn that will send and receive data on a stream named 'name'
77 * and stores a pointer to the vconn in '*vconnp'.
79 * Returns 0 if successful, otherwise a positive errno value. */
81 vconn_stream_open(const char *name, uint32_t allowed_versions,
82 char *suffix OVS_UNUSED, struct vconn **vconnp, uint8_t dscp)
84 struct stream *stream;
87 error = stream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
90 error = stream_connect(stream);
91 if (!error || error == EAGAIN) {
92 *vconnp = vconn_stream_new(stream, error, allowed_versions);
101 static struct vconn_stream *
102 vconn_stream_cast(struct vconn *vconn)
104 return CONTAINER_OF(vconn, struct vconn_stream, vconn);
108 vconn_stream_close(struct vconn *vconn)
110 struct vconn_stream *s = vconn_stream_cast(vconn);
112 if ((vconn->error == EPROTO || s->n_packets < 1) && s->rxbuf) {
113 stream_report_content(s->rxbuf->data, s->rxbuf->size, STREAM_OPENFLOW,
114 THIS_MODULE, vconn_get_name(vconn));
117 stream_close(s->stream);
118 vconn_stream_clear_txbuf(s);
119 ofpbuf_delete(s->rxbuf);
124 vconn_stream_connect(struct vconn *vconn)
126 struct vconn_stream *s = vconn_stream_cast(vconn);
127 return stream_connect(s->stream);
131 vconn_stream_recv__(struct vconn_stream *s, int rx_len)
133 struct ofpbuf *rx = s->rxbuf;
134 int want_bytes, retval;
136 want_bytes = rx_len - rx->size;
137 ofpbuf_prealloc_tailroom(rx, want_bytes);
138 retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
141 return retval == want_bytes ? 0 : EAGAIN;
142 } else if (retval == 0) {
144 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
154 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
156 struct vconn_stream *s = vconn_stream_cast(vconn);
157 const struct ofp_header *oh;
160 /* Allocate new receive buffer if we don't have one. */
161 if (s->rxbuf == NULL) {
162 s->rxbuf = ofpbuf_new(1564);
165 /* Read ofp_header. */
166 if (s->rxbuf->size < sizeof(struct ofp_header)) {
167 int retval = vconn_stream_recv__(s, sizeof(struct ofp_header));
175 rx_len = ntohs(oh->length);
176 if (rx_len < sizeof(struct ofp_header)) {
177 VLOG_ERR_RL(&rl, "received too-short ofp_header (%d bytes)", rx_len);
179 } else if (s->rxbuf->size < rx_len) {
180 int retval = vconn_stream_recv__(s, rx_len);
193 vconn_stream_clear_txbuf(struct vconn_stream *s)
195 ofpbuf_delete(s->txbuf);
200 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
202 struct vconn_stream *s = vconn_stream_cast(vconn);
209 retval = stream_send(s->stream, buffer->data, buffer->size);
210 if (retval == buffer->size) {
211 ofpbuf_delete(buffer);
213 } else if (retval >= 0 || retval == -EAGAIN) {
214 leak_checker_claim(buffer);
217 ofpbuf_pull(buffer, retval);
226 vconn_stream_run(struct vconn *vconn)
228 struct vconn_stream *s = vconn_stream_cast(vconn);
231 stream_run(s->stream);
236 retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
238 if (retval != -EAGAIN) {
239 VLOG_ERR_RL(&rl, "send: %s", strerror(-retval));
240 vconn_stream_clear_txbuf(s);
243 } else if (retval > 0) {
244 ofpbuf_pull(s->txbuf, retval);
245 if (!s->txbuf->size) {
246 vconn_stream_clear_txbuf(s);
253 vconn_stream_run_wait(struct vconn *vconn)
255 struct vconn_stream *s = vconn_stream_cast(vconn);
257 stream_run_wait(s->stream);
259 stream_send_wait(s->stream);
264 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
266 struct vconn_stream *s = vconn_stream_cast(vconn);
269 stream_connect_wait(s->stream);
274 stream_send_wait(s->stream);
276 /* Nothing to do: need to drain txbuf first.
277 * vconn_stream_run_wait() will arrange to wake up when there room
278 * to send data, so there's no point in calling poll_fd_wait()
279 * redundantly here. */
284 stream_recv_wait(s->stream);
292 /* Passive stream socket vconn. */
294 struct pvconn_pstream
296 struct pvconn pvconn;
297 struct pstream *pstream;
300 static struct pvconn_class pstream_pvconn_class;
302 static struct pvconn_pstream *
303 pvconn_pstream_cast(struct pvconn *pvconn)
305 return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
308 /* Creates a new pvconn named 'name' that will accept new connections using
309 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
311 * Returns 0 if successful, otherwise a positive errno value. (The current
312 * implementation never fails.) */
314 pvconn_pstream_listen(const char *name, uint32_t allowed_versions,
315 char *suffix OVS_UNUSED, struct pvconn **pvconnp,
318 struct pvconn_pstream *ps;
319 struct pstream *pstream;
322 error = pstream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
328 ps = xmalloc(sizeof *ps);
329 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name, allowed_versions);
330 ps->pstream = pstream;
331 *pvconnp = &ps->pvconn;
336 pvconn_pstream_close(struct pvconn *pvconn)
338 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
339 pstream_close(ps->pstream);
344 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
346 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
347 struct stream *stream;
350 error = pstream_accept(ps->pstream, &stream);
352 if (error != EAGAIN) {
353 VLOG_DBG_RL(&rl, "%s: accept: %s",
354 pstream_get_name(ps->pstream), strerror(error));
359 *new_vconnp = vconn_stream_new(stream, 0, pvconn->allowed_versions);
364 pvconn_pstream_wait(struct pvconn *pvconn)
366 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
367 pstream_wait(ps->pstream);
370 /* Stream-based vconns and pvconns. */
372 #define STREAM_INIT(NAME) \
376 vconn_stream_close, \
377 vconn_stream_connect, \
381 vconn_stream_run_wait, \
385 #define PSTREAM_INIT(NAME) \
388 pvconn_pstream_listen, \
389 pvconn_pstream_close, \
390 pvconn_pstream_accept, \
391 pvconn_pstream_wait \
394 static struct vconn_class stream_vconn_class = STREAM_INIT("stream");
395 static struct pvconn_class pstream_pvconn_class = PSTREAM_INIT("pstream");
397 struct vconn_class tcp_vconn_class = STREAM_INIT("tcp");
398 struct pvconn_class ptcp_pvconn_class = PSTREAM_INIT("ptcp");
400 struct vconn_class unix_vconn_class = STREAM_INIT("unix");
401 struct pvconn_class punix_pvconn_class = PSTREAM_INIT("punix");
404 struct vconn_class ssl_vconn_class = STREAM_INIT("ssl");
405 struct pvconn_class pssl_pvconn_class = PSTREAM_INIT("pssl");