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"
26 #include "openflow/openflow.h"
27 #include "poll-loop.h"
28 #include "socket-util.h"
31 #include "vconn-provider.h"
35 VLOG_DEFINE_THIS_MODULE(vconn_stream
);
37 /* Active stream socket vconn. */
42 struct stream
*stream
;
48 static const struct vconn_class stream_vconn_class
;
50 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 25);
52 static void vconn_stream_clear_txbuf(struct vconn_stream
*);
55 vconn_stream_new(struct stream
*stream
, int connect_status
,
56 uint32_t allowed_versions
)
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
), allowed_versions
);
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
, uint32_t allowed_versions
,
80 char *suffix OVS_UNUSED
, struct vconn
**vconnp
, uint8_t dscp
)
82 struct stream
*stream
;
85 error
= stream_open_with_default_ports(name
, OFP_TCP_PORT
, OFP_SSL_PORT
,
88 error
= stream_connect(stream
);
89 if (!error
|| error
== EAGAIN
) {
90 *vconnp
= vconn_stream_new(stream
, error
, allowed_versions
);
99 static struct vconn_stream
*
100 vconn_stream_cast(struct vconn
*vconn
)
102 return CONTAINER_OF(vconn
, struct vconn_stream
, vconn
);
106 vconn_stream_close(struct vconn
*vconn
)
108 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
110 if ((vconn
->error
== EPROTO
|| s
->n_packets
< 1) && s
->rxbuf
) {
111 stream_report_content(s
->rxbuf
->data
, s
->rxbuf
->size
, STREAM_OPENFLOW
,
112 THIS_MODULE
, vconn_get_name(vconn
));
115 stream_close(s
->stream
);
116 vconn_stream_clear_txbuf(s
);
117 ofpbuf_delete(s
->rxbuf
);
122 vconn_stream_connect(struct vconn
*vconn
)
124 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
125 return stream_connect(s
->stream
);
129 vconn_stream_recv__(struct vconn_stream
*s
, int rx_len
)
131 struct ofpbuf
*rx
= s
->rxbuf
;
132 int want_bytes
, retval
;
134 want_bytes
= rx_len
- rx
->size
;
135 ofpbuf_prealloc_tailroom(rx
, want_bytes
);
136 retval
= stream_recv(s
->stream
, ofpbuf_tail(rx
), want_bytes
);
139 return retval
== want_bytes
? 0 : EAGAIN
;
140 } else if (retval
== 0) {
142 VLOG_ERR_RL(&rl
, "connection dropped mid-packet");
152 vconn_stream_recv(struct vconn
*vconn
, struct ofpbuf
**bufferp
)
154 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
155 const struct ofp_header
*oh
;
158 /* Allocate new receive buffer if we don't have one. */
159 if (s
->rxbuf
== NULL
) {
160 s
->rxbuf
= ofpbuf_new(1564);
163 /* Read ofp_header. */
164 if (s
->rxbuf
->size
< sizeof(struct ofp_header
)) {
165 int retval
= vconn_stream_recv__(s
, sizeof(struct ofp_header
));
173 rx_len
= ntohs(oh
->length
);
174 if (rx_len
< sizeof(struct ofp_header
)) {
175 VLOG_ERR_RL(&rl
, "received too-short ofp_header (%d bytes)", rx_len
);
177 } else if (s
->rxbuf
->size
< rx_len
) {
178 int retval
= vconn_stream_recv__(s
, rx_len
);
191 vconn_stream_clear_txbuf(struct vconn_stream
*s
)
193 ofpbuf_delete(s
->txbuf
);
198 vconn_stream_send(struct vconn
*vconn
, struct ofpbuf
*buffer
)
200 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
207 retval
= stream_send(s
->stream
, buffer
->data
, buffer
->size
);
208 if (retval
== buffer
->size
) {
209 ofpbuf_delete(buffer
);
211 } else if (retval
>= 0 || retval
== -EAGAIN
) {
214 ofpbuf_pull(buffer
, retval
);
223 vconn_stream_run(struct vconn
*vconn
)
225 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
228 stream_run(s
->stream
);
233 retval
= stream_send(s
->stream
, s
->txbuf
->data
, s
->txbuf
->size
);
235 if (retval
!= -EAGAIN
) {
236 VLOG_ERR_RL(&rl
, "send: %s", strerror(-retval
));
237 vconn_stream_clear_txbuf(s
);
240 } else if (retval
> 0) {
241 ofpbuf_pull(s
->txbuf
, retval
);
242 if (!s
->txbuf
->size
) {
243 vconn_stream_clear_txbuf(s
);
250 vconn_stream_run_wait(struct vconn
*vconn
)
252 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
254 stream_run_wait(s
->stream
);
256 stream_send_wait(s
->stream
);
261 vconn_stream_wait(struct vconn
*vconn
, enum vconn_wait_type wait
)
263 struct vconn_stream
*s
= vconn_stream_cast(vconn
);
266 stream_connect_wait(s
->stream
);
271 stream_send_wait(s
->stream
);
273 /* Nothing to do: need to drain txbuf first.
274 * vconn_stream_run_wait() will arrange to wake up when there room
275 * to send data, so there's no point in calling poll_fd_wait()
276 * redundantly here. */
281 stream_recv_wait(s
->stream
);
289 /* Passive stream socket vconn. */
291 struct pvconn_pstream
293 struct pvconn pvconn
;
294 struct pstream
*pstream
;
297 static const struct pvconn_class pstream_pvconn_class
;
299 static struct pvconn_pstream
*
300 pvconn_pstream_cast(struct pvconn
*pvconn
)
302 return CONTAINER_OF(pvconn
, struct pvconn_pstream
, pvconn
);
305 /* Creates a new pvconn named 'name' that will accept new connections using
306 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
308 * Returns 0 if successful, otherwise a positive errno value. (The current
309 * implementation never fails.) */
311 pvconn_pstream_listen(const char *name
, uint32_t allowed_versions
,
312 char *suffix OVS_UNUSED
, struct pvconn
**pvconnp
,
315 struct pvconn_pstream
*ps
;
316 struct pstream
*pstream
;
319 error
= pstream_open_with_default_ports(name
, OFP_TCP_PORT
, OFP_SSL_PORT
,
325 ps
= xmalloc(sizeof *ps
);
326 pvconn_init(&ps
->pvconn
, &pstream_pvconn_class
, name
, allowed_versions
);
327 ps
->pstream
= pstream
;
328 *pvconnp
= &ps
->pvconn
;
333 pvconn_pstream_close(struct pvconn
*pvconn
)
335 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
336 pstream_close(ps
->pstream
);
341 pvconn_pstream_accept(struct pvconn
*pvconn
, struct vconn
**new_vconnp
)
343 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
344 struct stream
*stream
;
347 error
= pstream_accept(ps
->pstream
, &stream
);
349 if (error
!= EAGAIN
) {
350 VLOG_DBG_RL(&rl
, "%s: accept: %s",
351 pstream_get_name(ps
->pstream
), strerror(error
));
356 *new_vconnp
= vconn_stream_new(stream
, 0, pvconn
->allowed_versions
);
361 pvconn_pstream_wait(struct pvconn
*pvconn
)
363 struct pvconn_pstream
*ps
= pvconn_pstream_cast(pvconn
);
364 pstream_wait(ps
->pstream
);
367 /* Stream-based vconns and pvconns. */
369 #define STREAM_INIT(NAME) \
373 vconn_stream_close, \
374 vconn_stream_connect, \
378 vconn_stream_run_wait, \
382 #define PSTREAM_INIT(NAME) \
385 pvconn_pstream_listen, \
386 pvconn_pstream_close, \
387 pvconn_pstream_accept, \
388 pvconn_pstream_wait \
391 static const struct vconn_class stream_vconn_class
= STREAM_INIT("stream");
392 static const struct pvconn_class pstream_pvconn_class
= PSTREAM_INIT("pstream");
394 const struct vconn_class tcp_vconn_class
= STREAM_INIT("tcp");
395 const struct pvconn_class ptcp_pvconn_class
= PSTREAM_INIT("ptcp");
397 const struct vconn_class unix_vconn_class
= STREAM_INIT("unix");
398 const struct pvconn_class punix_pvconn_class
= PSTREAM_INIT("punix");
401 const struct vconn_class ssl_vconn_class
= STREAM_INIT("ssl");
402 const struct pvconn_class pssl_pvconn_class
= PSTREAM_INIT("pssl");