2 * Copyright (c) 2008, 2009, 2010 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.
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 #define THIS_MODULE VLM_vconn_stream
39 /* Active stream socket vconn. */
44 struct stream *stream;
49 static struct vconn_class stream_vconn_class;
51 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
53 static void vconn_stream_clear_txbuf(struct vconn_stream *);
56 vconn_stream_new(struct stream *stream, int connect_status)
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));
66 s->vconn.remote_ip = stream_get_remote_ip(stream);
67 s->vconn.remote_port = stream_get_remote_port(stream);
68 s->vconn.local_ip = stream_get_local_ip(stream);
69 s->vconn.local_port = stream_get_local_port(stream);
73 /* Creates a new vconn that will send and receive data on a stream named 'name'
74 * and stores a pointer to the vconn in '*vconnp'.
76 * Returns 0 if successful, otherwise a positive errno value. */
78 vconn_stream_open(const char *name, char *suffix OVS_UNUSED,
79 struct vconn **vconnp)
81 struct stream *stream;
84 error = stream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
87 if (error && error != EAGAIN) {
91 *vconnp = vconn_stream_new(stream, error);
95 static struct vconn_stream *
96 vconn_stream_cast(struct vconn *vconn)
98 return CONTAINER_OF(vconn, struct vconn_stream, vconn);
102 vconn_stream_close(struct vconn *vconn)
104 struct vconn_stream *s = vconn_stream_cast(vconn);
105 stream_close(s->stream);
106 vconn_stream_clear_txbuf(s);
107 ofpbuf_delete(s->rxbuf);
112 vconn_stream_connect(struct vconn *vconn)
114 struct vconn_stream *s = vconn_stream_cast(vconn);
115 return stream_connect(s->stream);
119 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
121 struct vconn_stream *s = vconn_stream_cast(vconn);
126 if (s->rxbuf == NULL) {
127 s->rxbuf = ofpbuf_new(1564);
132 if (sizeof(struct ofp_header) > rx->size) {
133 want_bytes = sizeof(struct ofp_header) - rx->size;
135 struct ofp_header *oh = rx->data;
136 size_t length = ntohs(oh->length);
137 if (length < sizeof(struct ofp_header)) {
138 VLOG_ERR_RL(&rl, "received too-short ofp_header (%zu bytes)",
142 want_bytes = length - rx->size;
149 ofpbuf_prealloc_tailroom(rx, want_bytes);
151 retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
154 if (retval == want_bytes) {
155 if (rx->size > sizeof(struct ofp_header)) {
164 } else if (retval == 0) {
166 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
177 vconn_stream_clear_txbuf(struct vconn_stream *s)
179 ofpbuf_delete(s->txbuf);
184 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
186 struct vconn_stream *s = vconn_stream_cast(vconn);
193 retval = stream_send(s->stream, buffer->data, buffer->size);
194 if (retval == buffer->size) {
195 ofpbuf_delete(buffer);
197 } else if (retval >= 0 || retval == -EAGAIN) {
198 leak_checker_claim(buffer);
201 ofpbuf_pull(buffer, retval);
210 vconn_stream_run(struct vconn *vconn)
212 struct vconn_stream *s = vconn_stream_cast(vconn);
219 retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
221 if (retval != -EAGAIN) {
222 VLOG_ERR_RL(&rl, "send: %s", strerror(-retval));
223 vconn_stream_clear_txbuf(s);
226 } else if (retval > 0) {
227 ofpbuf_pull(s->txbuf, retval);
228 if (!s->txbuf->size) {
229 vconn_stream_clear_txbuf(s);
236 vconn_stream_run_wait(struct vconn *vconn)
238 struct vconn_stream *s = vconn_stream_cast(vconn);
241 stream_send_wait(s->stream);
246 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
248 struct vconn_stream *s = vconn_stream_cast(vconn);
251 stream_connect_wait(s->stream);
256 stream_send_wait(s->stream);
258 /* Nothing to do: need to drain txbuf first.
259 * vconn_stream_run_wait() will arrange to wake up when there room
260 * to send data, so there's no point in calling poll_fd_wait()
261 * redundantly here. */
266 stream_recv_wait(s->stream);
274 /* Passive stream socket vconn. */
276 struct pvconn_pstream
278 struct pvconn pvconn;
279 struct pstream *pstream;
282 static struct pvconn_class pstream_pvconn_class;
284 static struct pvconn_pstream *
285 pvconn_pstream_cast(struct pvconn *pvconn)
287 return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
290 /* Creates a new pvconn named 'name' that will accept new connections using
291 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
293 * Returns 0 if successful, otherwise a positive errno value. (The current
294 * implementation never fails.) */
296 pvconn_pstream_listen(const char *name, char *suffix OVS_UNUSED,
297 struct pvconn **pvconnp)
299 struct pvconn_pstream *ps;
300 struct pstream *pstream;
303 error = pstream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
309 ps = xmalloc(sizeof *ps);
310 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name);
311 ps->pstream = pstream;
312 *pvconnp = &ps->pvconn;
317 pvconn_pstream_close(struct pvconn *pvconn)
319 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
320 pstream_close(ps->pstream);
325 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
327 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
328 struct stream *stream;
331 error = pstream_accept(ps->pstream, &stream);
333 if (error != EAGAIN) {
334 VLOG_DBG_RL(&rl, "%s: accept: %s",
335 pstream_get_name(ps->pstream), strerror(error));
340 *new_vconnp = vconn_stream_new(stream, 0);
345 pvconn_pstream_wait(struct pvconn *pvconn)
347 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
348 pstream_wait(ps->pstream);
351 /* Stream-based vconns and pvconns. */
353 #define DEFINE_VCONN_STREAM_CLASS(NAME) \
354 struct vconn_class NAME##_vconn_class = { \
357 vconn_stream_close, \
358 vconn_stream_connect, \
362 vconn_stream_run_wait, \
366 #define DEFINE_PVCONN_STREAM_CLASS(NAME) \
367 struct pvconn_class NAME##_pvconn_class = { \
369 pvconn_pstream_listen, \
370 pvconn_pstream_close, \
371 pvconn_pstream_accept, \
372 pvconn_pstream_wait \
375 static DEFINE_VCONN_STREAM_CLASS(stream);
376 static DEFINE_PVCONN_STREAM_CLASS(pstream);
378 DEFINE_VCONN_STREAM_CLASS(tcp);
379 DEFINE_PVCONN_STREAM_CLASS(ptcp);
381 DEFINE_VCONN_STREAM_CLASS(unix);
382 DEFINE_PVCONN_STREAM_CLASS(punix);
385 DEFINE_VCONN_STREAM_CLASS(ssl);
386 DEFINE_PVCONN_STREAM_CLASS(pssl);