2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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.
22 #include <sys/types.h>
24 #include "fatal-signal.h"
25 #include "leak-checker.h"
27 #include "openflow/openflow.h"
28 #include "poll-loop.h"
29 #include "socket-util.h"
32 #include "vconn-provider.h"
36 VLOG_DEFINE_THIS_MODULE(vconn_stream
);
38 /* Active stream socket vconn. */
43 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
,
57 uint32_t allowed_versions
)
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
), allowed_versions
);
68 s
->vconn
.remote_ip
= stream_get_remote_ip(stream
);
69 s
->vconn
.remote_port
= stream_get_remote_port(stream
);
70 s
->vconn
.local_ip
= stream_get_local_ip(stream
);
71 s
->vconn
.local_port
= stream_get_local_port(stream
);
75 /* Creates a new vconn that will send and receive data on a stream named 'name'
76 * and stores a pointer to the vconn in '*vconnp'.
78 * Returns 0 if successful, otherwise a positive errno value. */
80 vconn_stream_open(const char *name
, uint32_t allowed_versions
,
81 char *suffix OVS_UNUSED
, struct vconn
**vconnp
, uint8_t dscp
)
83 struct stream
*stream
;
86 error
= stream_open_with_default_ports(name
, OFP_TCP_PORT
, OFP_SSL_PORT
,
89 error
= stream_connect(stream
);
90 if (!error
|| error
== EAGAIN
) {
91 *vconnp
= vconn_stream_new(stream
, error
, allowed_versions
);
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
);
111 if ((vconn
->error
== EPROTO
|| s
->n_packets
< 1) && s
->rxbuf
) {
112 stream_report_content(s
->rxbuf
->data
, s
->rxbuf
->size
, STREAM_OPENFLOW
,
113 THIS_MODULE
, vconn_get_name(vconn
));
116 stream_close(s
->stream
);
117 vconn_stream_clear_txbuf(s
);
118 ofpbuf_delete(s
->rxbuf
);
123 vconn_stream_connect(struct vconn
*vconn
)
125 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
126 return stream_connect(s
->stream
);
130 vconn_stream_recv__(struct vconn_stream
*s
, int rx_len
)
132 struct ofpbuf
*rx
= s
->rxbuf
;
133 int want_bytes
, retval
;
135 want_bytes
= rx_len
- rx
->size
;
136 ofpbuf_prealloc_tailroom(rx
, want_bytes
);
137 retval
= stream_recv(s
->stream
, ofpbuf_tail(rx
), want_bytes
);
140 return retval
== want_bytes
? 0 : EAGAIN
;
141 } else if (retval
== 0) {
143 VLOG_ERR_RL(&rl
, "connection dropped mid-packet");
153 vconn_stream_recv(struct vconn
*vconn
, struct ofpbuf
**bufferp
)
155 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
156 const struct ofp_header
*oh
;
159 /* Allocate new receive buffer if we don't have one. */
160 if (s
->rxbuf
== NULL
) {
161 s
->rxbuf
= ofpbuf_new(1564);
164 /* Read ofp_header. */
165 if (s
->rxbuf
->size
< sizeof(struct ofp_header
)) {
166 int retval
= vconn_stream_recv__(s
, sizeof(struct ofp_header
));
174 rx_len
= ntohs(oh
->length
);
175 if (rx_len
< sizeof(struct ofp_header
)) {
176 VLOG_ERR_RL(&rl
, "received too-short ofp_header (%d bytes)", rx_len
);
178 } else if (s
->rxbuf
->size
< rx_len
) {
179 int retval
= vconn_stream_recv__(s
, rx_len
);
192 vconn_stream_clear_txbuf(struct vconn_stream
*s
)
194 ofpbuf_delete(s
->txbuf
);
199 vconn_stream_send(struct vconn
*vconn
, struct ofpbuf
*buffer
)
201 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
208 retval
= stream_send(s
->stream
, buffer
->data
, buffer
->size
);
209 if (retval
== buffer
->size
) {
210 ofpbuf_delete(buffer
);
212 } else if (retval
>= 0 || retval
== -EAGAIN
) {
213 leak_checker_claim(buffer
);
216 ofpbuf_pull(buffer
, retval
);
225 vconn_stream_run(struct vconn
*vconn
)
227 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
230 stream_run(s
->stream
);
235 retval
= stream_send(s
->stream
, s
->txbuf
->data
, s
->txbuf
->size
);
237 if (retval
!= -EAGAIN
) {
238 VLOG_ERR_RL(&rl
, "send: %s", strerror(-retval
));
239 vconn_stream_clear_txbuf(s
);
242 } else if (retval
> 0) {
243 ofpbuf_pull(s
->txbuf
, retval
);
244 if (!s
->txbuf
->size
) {
245 vconn_stream_clear_txbuf(s
);
252 vconn_stream_run_wait(struct vconn
*vconn
)
254 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
256 stream_run_wait(s
->stream
);
258 stream_send_wait(s
->stream
);
263 vconn_stream_wait(struct vconn
*vconn
, enum vconn_wait_type wait
)
265 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
268 stream_connect_wait(s
->stream
);
273 stream_send_wait(s
->stream
);
275 /* Nothing to do: need to drain txbuf first.
276 * vconn_stream_run_wait() will arrange to wake up when there room
277 * to send data, so there's no point in calling poll_fd_wait()
278 * redundantly here. */
283 stream_recv_wait(s
->stream
);
291 /* Passive stream socket vconn. */
293 struct pvconn_pstream
295 struct pvconn pvconn
;
296 struct pstream
*pstream
;
299 static struct pvconn_class pstream_pvconn_class
;
301 static struct pvconn_pstream
*
302 pvconn_pstream_cast(struct pvconn
*pvconn
)
304 return CONTAINER_OF(pvconn
, struct pvconn_pstream
, pvconn
);
307 /* Creates a new pvconn named 'name' that will accept new connections using
308 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
310 * Returns 0 if successful, otherwise a positive errno value. (The current
311 * implementation never fails.) */
313 pvconn_pstream_listen(const char *name
, uint32_t allowed_versions
,
314 char *suffix OVS_UNUSED
, struct pvconn
**pvconnp
,
317 struct pvconn_pstream
*ps
;
318 struct pstream
*pstream
;
321 error
= pstream_open_with_default_ports(name
, OFP_TCP_PORT
, OFP_SSL_PORT
,
327 ps
= xmalloc(sizeof *ps
);
328 pvconn_init(&ps
->pvconn
, &pstream_pvconn_class
, name
, allowed_versions
);
329 ps
->pstream
= pstream
;
330 *pvconnp
= &ps
->pvconn
;
335 pvconn_pstream_close(struct pvconn
*pvconn
)
337 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
338 pstream_close(ps
->pstream
);
343 pvconn_pstream_accept(struct pvconn
*pvconn
, struct vconn
**new_vconnp
)
345 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
346 struct stream
*stream
;
349 error
= pstream_accept(ps
->pstream
, &stream
);
351 if (error
!= EAGAIN
) {
352 VLOG_DBG_RL(&rl
, "%s: accept: %s",
353 pstream_get_name(ps
->pstream
), strerror(error
));
358 *new_vconnp
= vconn_stream_new(stream
, 0, pvconn
->allowed_versions
);
363 pvconn_pstream_wait(struct pvconn
*pvconn
)
365 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
366 pstream_wait(ps
->pstream
);
369 /* Stream-based vconns and pvconns. */
371 #define STREAM_INIT(NAME) \
375 vconn_stream_close, \
376 vconn_stream_connect, \
380 vconn_stream_run_wait, \
384 #define PSTREAM_INIT(NAME) \
387 pvconn_pstream_listen, \
388 pvconn_pstream_close, \
389 pvconn_pstream_accept, \
390 pvconn_pstream_wait \
393 static struct vconn_class stream_vconn_class
= STREAM_INIT("stream");
394 static struct pvconn_class pstream_pvconn_class
= PSTREAM_INIT("pstream");
396 struct vconn_class tcp_vconn_class
= STREAM_INIT("tcp");
397 struct pvconn_class ptcp_pvconn_class
= PSTREAM_INIT("ptcp");
399 struct vconn_class unix_vconn_class
= STREAM_INIT("unix");
400 struct pvconn_class punix_pvconn_class
= PSTREAM_INIT("punix");
403 struct vconn_class ssl_vconn_class
= STREAM_INIT("ssl");
404 struct pvconn_class pssl_pvconn_class
= PSTREAM_INIT("pssl");