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 *);
54 static int count_fields(const char *);
57 vconn_stream_new(struct stream *stream, int connect_status)
59 struct vconn_stream *s;
61 s = xmalloc(sizeof *s);
62 vconn_init(&s->vconn, &stream_vconn_class, connect_status,
63 stream_get_name(stream));
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_, char *suffix OVS_UNUSED,
76 struct vconn **vconnp)
78 struct stream *stream;
82 if (!strncmp(name_, "tcp:", 4) && count_fields(name_) < 3) {
83 name = xasprintf("%s:%d", name_, OFP_TCP_PORT);
84 } else if (!strncmp(name_, "ssl:", 4) && count_fields(name_) < 3) {
85 name = xasprintf("%s:%d", name_, OFP_SSL_PORT);
87 name = xstrdup(name_);
89 error = stream_open(name, &stream);
92 if (error && error != EAGAIN) {
96 *vconnp = vconn_stream_new(stream, error);
100 static struct vconn_stream *
101 vconn_stream_cast(struct vconn *vconn)
103 return CONTAINER_OF(vconn, struct vconn_stream, vconn);
107 vconn_stream_close(struct vconn *vconn)
109 struct vconn_stream *s = vconn_stream_cast(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 *vconn, struct ofpbuf **bufferp)
126 struct vconn_stream *s = vconn_stream_cast(vconn);
131 if (s->rxbuf == NULL) {
132 s->rxbuf = ofpbuf_new(1564);
137 if (sizeof(struct ofp_header) > rx->size) {
138 want_bytes = sizeof(struct ofp_header) - rx->size;
140 struct ofp_header *oh = rx->data;
141 size_t length = ntohs(oh->length);
142 if (length < sizeof(struct ofp_header)) {
143 VLOG_ERR_RL(&rl, "received too-short ofp_header (%zu bytes)",
147 want_bytes = length - rx->size;
154 ofpbuf_prealloc_tailroom(rx, want_bytes);
156 retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
159 if (retval == want_bytes) {
160 if (rx->size > sizeof(struct ofp_header)) {
169 } else if (retval == 0) {
171 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
182 vconn_stream_clear_txbuf(struct vconn_stream *s)
184 ofpbuf_delete(s->txbuf);
189 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
191 struct vconn_stream *s = vconn_stream_cast(vconn);
198 retval = stream_send(s->stream, buffer->data, buffer->size);
199 if (retval == buffer->size) {
200 ofpbuf_delete(buffer);
202 } else if (retval >= 0 || retval == -EAGAIN) {
203 leak_checker_claim(buffer);
206 ofpbuf_pull(buffer, retval);
215 vconn_stream_run(struct vconn *vconn)
217 struct vconn_stream *s = vconn_stream_cast(vconn);
224 retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
226 if (retval != -EAGAIN) {
227 VLOG_ERR_RL(&rl, "send: %s", strerror(-retval));
228 vconn_stream_clear_txbuf(s);
231 } else if (retval > 0) {
232 ofpbuf_pull(s->txbuf, retval);
233 if (!s->txbuf->size) {
234 vconn_stream_clear_txbuf(s);
241 vconn_stream_run_wait(struct vconn *vconn)
243 struct vconn_stream *s = vconn_stream_cast(vconn);
246 stream_send_wait(s->stream);
251 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
253 struct vconn_stream *s = vconn_stream_cast(vconn);
256 stream_connect_wait(s->stream);
261 stream_send_wait(s->stream);
263 /* Nothing to do: need to drain txbuf first.
264 * vconn_stream_run_wait() will arrange to wake up when there room
265 * to send data, so there's no point in calling poll_fd_wait()
266 * redundantly here. */
271 stream_recv_wait(s->stream);
279 /* Passive stream socket vconn. */
281 struct pvconn_pstream
283 struct pvconn pvconn;
284 struct pstream *pstream;
287 static struct pvconn_class pstream_pvconn_class;
289 static struct pvconn_pstream *
290 pvconn_pstream_cast(struct pvconn *pvconn)
292 return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
295 /* Creates a new pvconn named 'name' that will accept new connections using
296 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
298 * Returns 0 if successful, otherwise a positive errno value. (The current
299 * implementation never fails.) */
301 pvconn_pstream_listen(const char *name_, char *suffix OVS_UNUSED,
302 struct pvconn **pvconnp)
304 struct pvconn_pstream *ps;
305 struct pstream *pstream;
309 if (!strncmp(name_, "ptcp:", 5) && count_fields(name_) < 2) {
310 name = xasprintf("%s:%d", name_, OFP_TCP_PORT);
311 } else if (!strncmp(name_, "pssl:", 5) && count_fields(name_) < 2) {
312 name = xasprintf("%s:%d", name_, OFP_SSL_PORT);
314 name = xstrdup(name_);
316 error = pstream_open(name, &pstream);
322 ps = xmalloc(sizeof *ps);
323 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name_);
324 ps->pstream = pstream;
325 *pvconnp = &ps->pvconn;
330 pvconn_pstream_close(struct pvconn *pvconn)
332 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
333 pstream_close(ps->pstream);
338 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
340 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
341 struct stream *stream;
344 error = pstream_accept(ps->pstream, &stream);
346 if (error != EAGAIN) {
347 VLOG_DBG_RL(&rl, "%s: accept: %s",
348 pstream_get_name(ps->pstream), strerror(error));
353 *new_vconnp = vconn_stream_new(stream, 0);
358 pvconn_pstream_wait(struct pvconn *pvconn)
360 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
361 pstream_wait(ps->pstream);
365 count_fields(const char *s_)
367 char *s, *field, *save_ptr;
372 for (field = strtok_r(s, ":", &save_ptr); field != NULL;
373 field = strtok_r(NULL, ":", &save_ptr)) {
381 /* Stream-based vconns and pvconns. */
383 #define DEFINE_VCONN_STREAM_CLASS(NAME) \
384 struct vconn_class NAME##_vconn_class = { \
387 vconn_stream_close, \
388 vconn_stream_connect, \
392 vconn_stream_run_wait, \
396 #define DEFINE_PVCONN_STREAM_CLASS(NAME) \
397 struct pvconn_class NAME##_pvconn_class = { \
399 pvconn_pstream_listen, \
400 pvconn_pstream_close, \
401 pvconn_pstream_accept, \
402 pvconn_pstream_wait \
405 static DEFINE_VCONN_STREAM_CLASS(stream);
406 static DEFINE_PVCONN_STREAM_CLASS(pstream);
408 DEFINE_VCONN_STREAM_CLASS(tcp);
409 DEFINE_PVCONN_STREAM_CLASS(ptcp);
411 DEFINE_VCONN_STREAM_CLASS(unix);
412 DEFINE_PVCONN_STREAM_CLASS(punix);
415 DEFINE_VCONN_STREAM_CLASS(ssl);
416 DEFINE_PVCONN_STREAM_CLASS(pssl);