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
));
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_
, char *suffix OVS_UNUSED
,
80 struct vconn
**vconnp
)
82 struct stream
*stream
;
86 if (!strncmp(name_
, "tcp:", 4) && count_fields(name_
) < 3) {
87 name
= xasprintf("%s:%d", name_
, OFP_TCP_PORT
);
88 } else if (!strncmp(name_
, "ssl:", 4) && count_fields(name_
) < 3) {
89 name
= xasprintf("%s:%d", name_
, OFP_SSL_PORT
);
91 name
= xstrdup(name_
);
93 error
= stream_open(name
, &stream
);
96 if (error
&& error
!= EAGAIN
) {
100 *vconnp
= vconn_stream_new(stream
, error
);
104 static struct vconn_stream
*
105 vconn_stream_cast(struct vconn
*vconn
)
107 return CONTAINER_OF(vconn
, struct vconn_stream
, vconn
);
111 vconn_stream_close(struct vconn
*vconn
)
113 struct vconn_stream
*s
= vconn_stream_cast(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
*vconn
, struct ofpbuf
**bufferp
)
130 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
135 if (s
->rxbuf
== NULL
) {
136 s
->rxbuf
= ofpbuf_new(1564);
141 if (sizeof(struct ofp_header
) > rx
->size
) {
142 want_bytes
= sizeof(struct ofp_header
) - rx
->size
;
144 struct ofp_header
*oh
= rx
->data
;
145 size_t length
= ntohs(oh
->length
);
146 if (length
< sizeof(struct ofp_header
)) {
147 VLOG_ERR_RL(&rl
, "received too-short ofp_header (%zu bytes)",
151 want_bytes
= length
- rx
->size
;
158 ofpbuf_prealloc_tailroom(rx
, want_bytes
);
160 retval
= stream_recv(s
->stream
, ofpbuf_tail(rx
), want_bytes
);
163 if (retval
== want_bytes
) {
164 if (rx
->size
> sizeof(struct ofp_header
)) {
173 } else if (retval
== 0) {
175 VLOG_ERR_RL(&rl
, "connection dropped mid-packet");
186 vconn_stream_clear_txbuf(struct vconn_stream
*s
)
188 ofpbuf_delete(s
->txbuf
);
193 vconn_stream_send(struct vconn
*vconn
, struct ofpbuf
*buffer
)
195 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
202 retval
= stream_send(s
->stream
, buffer
->data
, buffer
->size
);
203 if (retval
== buffer
->size
) {
204 ofpbuf_delete(buffer
);
206 } else if (retval
>= 0 || retval
== -EAGAIN
) {
207 leak_checker_claim(buffer
);
210 ofpbuf_pull(buffer
, retval
);
219 vconn_stream_run(struct vconn
*vconn
)
221 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
228 retval
= stream_send(s
->stream
, s
->txbuf
->data
, s
->txbuf
->size
);
230 if (retval
!= -EAGAIN
) {
231 VLOG_ERR_RL(&rl
, "send: %s", strerror(-retval
));
232 vconn_stream_clear_txbuf(s
);
235 } else if (retval
> 0) {
236 ofpbuf_pull(s
->txbuf
, retval
);
237 if (!s
->txbuf
->size
) {
238 vconn_stream_clear_txbuf(s
);
245 vconn_stream_run_wait(struct vconn
*vconn
)
247 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
250 stream_send_wait(s
->stream
);
255 vconn_stream_wait(struct vconn
*vconn
, enum vconn_wait_type wait
)
257 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
260 stream_connect_wait(s
->stream
);
265 stream_send_wait(s
->stream
);
267 /* Nothing to do: need to drain txbuf first.
268 * vconn_stream_run_wait() will arrange to wake up when there room
269 * to send data, so there's no point in calling poll_fd_wait()
270 * redundantly here. */
275 stream_recv_wait(s
->stream
);
283 /* Passive stream socket vconn. */
285 struct pvconn_pstream
287 struct pvconn pvconn
;
288 struct pstream
*pstream
;
291 static struct pvconn_class pstream_pvconn_class
;
293 static struct pvconn_pstream
*
294 pvconn_pstream_cast(struct pvconn
*pvconn
)
296 return CONTAINER_OF(pvconn
, struct pvconn_pstream
, pvconn
);
299 /* Creates a new pvconn named 'name' that will accept new connections using
300 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
302 * Returns 0 if successful, otherwise a positive errno value. (The current
303 * implementation never fails.) */
305 pvconn_pstream_listen(const char *name_
, char *suffix OVS_UNUSED
,
306 struct pvconn
**pvconnp
)
308 struct pvconn_pstream
*ps
;
309 struct pstream
*pstream
;
313 if (!strncmp(name_
, "ptcp:", 5) && count_fields(name_
) < 2) {
314 name
= xasprintf("%s%d", name_
, OFP_TCP_PORT
);
315 } else if (!strncmp(name_
, "pssl:", 5) && count_fields(name_
) < 2) {
316 name
= xasprintf("%s%d", name_
, OFP_SSL_PORT
);
318 name
= xstrdup(name_
);
320 error
= pstream_open(name
, &pstream
);
326 ps
= xmalloc(sizeof *ps
);
327 pvconn_init(&ps
->pvconn
, &pstream_pvconn_class
, name_
);
328 ps
->pstream
= pstream
;
329 *pvconnp
= &ps
->pvconn
;
334 pvconn_pstream_close(struct pvconn
*pvconn
)
336 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
337 pstream_close(ps
->pstream
);
342 pvconn_pstream_accept(struct pvconn
*pvconn
, struct vconn
**new_vconnp
)
344 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
345 struct stream
*stream
;
348 error
= pstream_accept(ps
->pstream
, &stream
);
350 if (error
!= EAGAIN
) {
351 VLOG_DBG_RL(&rl
, "%s: accept: %s",
352 pstream_get_name(ps
->pstream
), strerror(error
));
357 *new_vconnp
= vconn_stream_new(stream
, 0);
362 pvconn_pstream_wait(struct pvconn
*pvconn
)
364 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
365 pstream_wait(ps
->pstream
);
369 count_fields(const char *s_
)
371 char *s
, *field
, *save_ptr
;
376 for (field
= strtok_r(s
, ":", &save_ptr
); field
!= NULL
;
377 field
= strtok_r(NULL
, ":", &save_ptr
)) {
385 /* Stream-based vconns and pvconns. */
387 #define DEFINE_VCONN_STREAM_CLASS(NAME) \
388 struct vconn_class NAME##_vconn_class = { \
391 vconn_stream_close, \
392 vconn_stream_connect, \
396 vconn_stream_run_wait, \
400 #define DEFINE_PVCONN_STREAM_CLASS(NAME) \
401 struct pvconn_class NAME##_pvconn_class = { \
403 pvconn_pstream_listen, \
404 pvconn_pstream_close, \
405 pvconn_pstream_accept, \
406 pvconn_pstream_wait \
409 static DEFINE_VCONN_STREAM_CLASS(stream
);
410 static DEFINE_PVCONN_STREAM_CLASS(pstream
);
412 DEFINE_VCONN_STREAM_CLASS(tcp
);
413 DEFINE_PVCONN_STREAM_CLASS(ptcp
);
415 DEFINE_VCONN_STREAM_CLASS(unix
);
416 DEFINE_PVCONN_STREAM_CLASS(punix
);
419 DEFINE_VCONN_STREAM_CLASS(ssl
);
420 DEFINE_PVCONN_STREAM_CLASS(pssl
);