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_stream *s, int rx_len)
121 struct ofpbuf *rx = s->rxbuf;
122 int want_bytes, retval;
124 want_bytes = rx_len - rx->size;
125 ofpbuf_prealloc_tailroom(rx, want_bytes);
126 retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
129 return retval == want_bytes ? 0 : EAGAIN;
130 } else if (retval == 0) {
132 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
142 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
144 struct vconn_stream *s = vconn_stream_cast(vconn);
145 const struct ofp_header *oh;
148 /* Allocate new receive buffer if we don't have one. */
149 if (s->rxbuf == NULL) {
150 s->rxbuf = ofpbuf_new(1564);
153 /* Read ofp_header. */
154 if (s->rxbuf->size < sizeof(struct ofp_header)) {
155 int retval = vconn_stream_recv__(s, sizeof(struct ofp_header));
163 rx_len = ntohs(oh->length);
164 if (rx_len < sizeof(struct ofp_header)) {
165 VLOG_ERR_RL(&rl, "received too-short ofp_header (%zu bytes)",
168 } else if (s->rxbuf->size < rx_len) {
169 int retval = vconn_stream_recv__(s, rx_len);
181 vconn_stream_clear_txbuf(struct vconn_stream *s)
183 ofpbuf_delete(s->txbuf);
188 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
190 struct vconn_stream *s = vconn_stream_cast(vconn);
197 retval = stream_send(s->stream, buffer->data, buffer->size);
198 if (retval == buffer->size) {
199 ofpbuf_delete(buffer);
201 } else if (retval >= 0 || retval == -EAGAIN) {
202 leak_checker_claim(buffer);
205 ofpbuf_pull(buffer, retval);
214 vconn_stream_run(struct vconn *vconn)
216 struct vconn_stream *s = vconn_stream_cast(vconn);
223 retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
225 if (retval != -EAGAIN) {
226 VLOG_ERR_RL(&rl, "send: %s", strerror(-retval));
227 vconn_stream_clear_txbuf(s);
230 } else if (retval > 0) {
231 ofpbuf_pull(s->txbuf, retval);
232 if (!s->txbuf->size) {
233 vconn_stream_clear_txbuf(s);
240 vconn_stream_run_wait(struct vconn *vconn)
242 struct vconn_stream *s = vconn_stream_cast(vconn);
245 stream_send_wait(s->stream);
250 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
252 struct vconn_stream *s = vconn_stream_cast(vconn);
255 stream_connect_wait(s->stream);
260 stream_send_wait(s->stream);
262 /* Nothing to do: need to drain txbuf first.
263 * vconn_stream_run_wait() will arrange to wake up when there room
264 * to send data, so there's no point in calling poll_fd_wait()
265 * redundantly here. */
270 stream_recv_wait(s->stream);
278 /* Passive stream socket vconn. */
280 struct pvconn_pstream
282 struct pvconn pvconn;
283 struct pstream *pstream;
286 static struct pvconn_class pstream_pvconn_class;
288 static struct pvconn_pstream *
289 pvconn_pstream_cast(struct pvconn *pvconn)
291 return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
294 /* Creates a new pvconn named 'name' that will accept new connections using
295 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
297 * Returns 0 if successful, otherwise a positive errno value. (The current
298 * implementation never fails.) */
300 pvconn_pstream_listen(const char *name, char *suffix OVS_UNUSED,
301 struct pvconn **pvconnp)
303 struct pvconn_pstream *ps;
304 struct pstream *pstream;
307 error = pstream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
313 ps = xmalloc(sizeof *ps);
314 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name);
315 ps->pstream = pstream;
316 *pvconnp = &ps->pvconn;
321 pvconn_pstream_close(struct pvconn *pvconn)
323 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
324 pstream_close(ps->pstream);
329 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
331 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
332 struct stream *stream;
335 error = pstream_accept(ps->pstream, &stream);
337 if (error != EAGAIN) {
338 VLOG_DBG_RL(&rl, "%s: accept: %s",
339 pstream_get_name(ps->pstream), strerror(error));
344 *new_vconnp = vconn_stream_new(stream, 0);
349 pvconn_pstream_wait(struct pvconn *pvconn)
351 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
352 pstream_wait(ps->pstream);
355 /* Stream-based vconns and pvconns. */
357 #define DEFINE_VCONN_STREAM_CLASS(NAME) \
358 struct vconn_class NAME##_vconn_class = { \
361 vconn_stream_close, \
362 vconn_stream_connect, \
366 vconn_stream_run_wait, \
370 #define DEFINE_PVCONN_STREAM_CLASS(NAME) \
371 struct pvconn_class NAME##_pvconn_class = { \
373 pvconn_pstream_listen, \
374 pvconn_pstream_close, \
375 pvconn_pstream_accept, \
376 pvconn_pstream_wait \
379 static DEFINE_VCONN_STREAM_CLASS(stream);
380 static DEFINE_PVCONN_STREAM_CLASS(pstream);
382 DEFINE_VCONN_STREAM_CLASS(tcp);
383 DEFINE_PVCONN_STREAM_CLASS(ptcp);
385 DEFINE_VCONN_STREAM_CLASS(unix);
386 DEFINE_PVCONN_STREAM_CLASS(punix);
389 DEFINE_VCONN_STREAM_CLASS(ssl);
390 DEFINE_PVCONN_STREAM_CLASS(pssl);