]> git.proxmox.com Git - mirror_ovs.git/blob - lib/vconn-stream.c
lib: Move vlog.h to <openvswitch/vlog.h>
[mirror_ovs.git] / lib / vconn-stream.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <poll.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include "fatal-signal.h"
25 #include "ofpbuf.h"
26 #include "openflow/openflow.h"
27 #include "poll-loop.h"
28 #include "socket-util.h"
29 #include "stream.h"
30 #include "util.h"
31 #include "vconn-provider.h"
32 #include "vconn.h"
33 #include "openvswitch/vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(vconn_stream);
36
37 /* Active stream socket vconn. */
38
39 struct vconn_stream
40 {
41 struct vconn vconn;
42 struct stream *stream;
43 struct ofpbuf *rxbuf;
44 struct ofpbuf *txbuf;
45 int n_packets;
46 };
47
48 static const struct vconn_class stream_vconn_class;
49
50 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
51
52 static void vconn_stream_clear_txbuf(struct vconn_stream *);
53
54 static struct vconn *
55 vconn_stream_new(struct stream *stream, int connect_status,
56 uint32_t allowed_versions)
57 {
58 struct vconn_stream *s;
59
60 s = xmalloc(sizeof *s);
61 vconn_init(&s->vconn, &stream_vconn_class, connect_status,
62 stream_get_name(stream), allowed_versions);
63 s->stream = stream;
64 s->txbuf = NULL;
65 s->rxbuf = NULL;
66 s->n_packets = 0;
67 return &s->vconn;
68 }
69
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'.
72 *
73 * Returns 0 if successful, otherwise a positive errno value. */
74 static int
75 vconn_stream_open(const char *name, uint32_t allowed_versions,
76 char *suffix OVS_UNUSED, struct vconn **vconnp, uint8_t dscp)
77 {
78 struct stream *stream;
79 int error;
80
81 error = stream_open_with_default_port(name, OFP_OLD_PORT, &stream, dscp);
82 if (!error) {
83 error = stream_connect(stream);
84 if (!error || error == EAGAIN) {
85 *vconnp = vconn_stream_new(stream, error, allowed_versions);
86 return 0;
87 }
88 }
89
90 stream_close(stream);
91 return error;
92 }
93
94 static struct vconn_stream *
95 vconn_stream_cast(struct vconn *vconn)
96 {
97 return CONTAINER_OF(vconn, struct vconn_stream, vconn);
98 }
99
100 static void
101 vconn_stream_close(struct vconn *vconn)
102 {
103 struct vconn_stream *s = vconn_stream_cast(vconn);
104
105 if ((vconn->error == EPROTO || s->n_packets < 1) && s->rxbuf) {
106 stream_report_content(ofpbuf_data(s->rxbuf), ofpbuf_size(s->rxbuf), STREAM_OPENFLOW,
107 THIS_MODULE, vconn_get_name(vconn));
108 }
109
110 stream_close(s->stream);
111 vconn_stream_clear_txbuf(s);
112 ofpbuf_delete(s->rxbuf);
113 free(s);
114 }
115
116 static int
117 vconn_stream_connect(struct vconn *vconn)
118 {
119 struct vconn_stream *s = vconn_stream_cast(vconn);
120 return stream_connect(s->stream);
121 }
122
123 static int
124 vconn_stream_recv__(struct vconn_stream *s, int rx_len)
125 {
126 struct ofpbuf *rx = s->rxbuf;
127 int want_bytes, retval;
128
129 want_bytes = rx_len - ofpbuf_size(rx);
130 ofpbuf_prealloc_tailroom(rx, want_bytes);
131 retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
132 if (retval > 0) {
133 ofpbuf_set_size(rx, ofpbuf_size(rx) + retval);
134 return retval == want_bytes ? 0 : EAGAIN;
135 } else if (retval == 0) {
136 if (ofpbuf_size(rx)) {
137 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
138 return EPROTO;
139 }
140 return EOF;
141 } else {
142 return -retval;
143 }
144 }
145
146 static int
147 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
148 {
149 struct vconn_stream *s = vconn_stream_cast(vconn);
150 const struct ofp_header *oh;
151 int rx_len;
152
153 /* Allocate new receive buffer if we don't have one. */
154 if (s->rxbuf == NULL) {
155 s->rxbuf = ofpbuf_new(1564);
156 }
157
158 /* Read ofp_header. */
159 if (ofpbuf_size(s->rxbuf) < sizeof(struct ofp_header)) {
160 int retval = vconn_stream_recv__(s, sizeof(struct ofp_header));
161 if (retval) {
162 return retval;
163 }
164 }
165
166 /* Read payload. */
167 oh = ofpbuf_data(s->rxbuf);
168 rx_len = ntohs(oh->length);
169 if (rx_len < sizeof(struct ofp_header)) {
170 VLOG_ERR_RL(&rl, "received too-short ofp_header (%d bytes)", rx_len);
171 return EPROTO;
172 } else if (ofpbuf_size(s->rxbuf) < rx_len) {
173 int retval = vconn_stream_recv__(s, rx_len);
174 if (retval) {
175 return retval;
176 }
177 }
178
179 s->n_packets++;
180 *bufferp = s->rxbuf;
181 s->rxbuf = NULL;
182 return 0;
183 }
184
185 static void
186 vconn_stream_clear_txbuf(struct vconn_stream *s)
187 {
188 ofpbuf_delete(s->txbuf);
189 s->txbuf = NULL;
190 }
191
192 static int
193 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
194 {
195 struct vconn_stream *s = vconn_stream_cast(vconn);
196 ssize_t retval;
197
198 if (s->txbuf) {
199 return EAGAIN;
200 }
201
202 retval = stream_send(s->stream, ofpbuf_data(buffer), ofpbuf_size(buffer));
203 if (retval == ofpbuf_size(buffer)) {
204 ofpbuf_delete(buffer);
205 return 0;
206 } else if (retval >= 0 || retval == -EAGAIN) {
207 s->txbuf = buffer;
208 if (retval > 0) {
209 ofpbuf_pull(buffer, retval);
210 }
211 return 0;
212 } else {
213 return -retval;
214 }
215 }
216
217 static void
218 vconn_stream_run(struct vconn *vconn)
219 {
220 struct vconn_stream *s = vconn_stream_cast(vconn);
221 ssize_t retval;
222
223 stream_run(s->stream);
224 if (!s->txbuf) {
225 return;
226 }
227
228 retval = stream_send(s->stream, ofpbuf_data(s->txbuf), ofpbuf_size(s->txbuf));
229 if (retval < 0) {
230 if (retval != -EAGAIN) {
231 VLOG_ERR_RL(&rl, "send: %s", ovs_strerror(-retval));
232 vconn_stream_clear_txbuf(s);
233 return;
234 }
235 } else if (retval > 0) {
236 ofpbuf_pull(s->txbuf, retval);
237 if (!ofpbuf_size(s->txbuf)) {
238 vconn_stream_clear_txbuf(s);
239 return;
240 }
241 }
242 }
243
244 static void
245 vconn_stream_run_wait(struct vconn *vconn)
246 {
247 struct vconn_stream *s = vconn_stream_cast(vconn);
248
249 stream_run_wait(s->stream);
250 if (s->txbuf) {
251 stream_send_wait(s->stream);
252 }
253 }
254
255 static void
256 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
257 {
258 struct vconn_stream *s = vconn_stream_cast(vconn);
259 switch (wait) {
260 case WAIT_CONNECT:
261 stream_connect_wait(s->stream);
262 break;
263
264 case WAIT_SEND:
265 if (!s->txbuf) {
266 stream_send_wait(s->stream);
267 } else {
268 /* Nothing to do: need to drain txbuf first.
269 * vconn_stream_run_wait() will arrange to wake up when there room
270 * to send data, so there's no point in calling poll_fd_wait()
271 * redundantly here. */
272 }
273 break;
274
275 case WAIT_RECV:
276 stream_recv_wait(s->stream);
277 break;
278
279 default:
280 OVS_NOT_REACHED();
281 }
282 }
283 \f
284 /* Passive stream socket vconn. */
285
286 struct pvconn_pstream
287 {
288 struct pvconn pvconn;
289 struct pstream *pstream;
290 };
291
292 static const struct pvconn_class pstream_pvconn_class;
293
294 static struct pvconn_pstream *
295 pvconn_pstream_cast(struct pvconn *pvconn)
296 {
297 return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
298 }
299
300 /* Creates a new pvconn named 'name' that will accept new connections using
301 * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
302 *
303 * Returns 0 if successful, otherwise a positive errno value. (The current
304 * implementation never fails.) */
305 static int
306 pvconn_pstream_listen(const char *name, uint32_t allowed_versions,
307 char *suffix OVS_UNUSED, struct pvconn **pvconnp,
308 uint8_t dscp)
309 {
310 struct pvconn_pstream *ps;
311 struct pstream *pstream;
312 int error;
313
314 error = pstream_open_with_default_port(name, OFP_OLD_PORT,
315 &pstream, dscp);
316 if (error) {
317 return error;
318 }
319
320 ps = xmalloc(sizeof *ps);
321 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name, allowed_versions);
322 ps->pstream = pstream;
323 *pvconnp = &ps->pvconn;
324 return 0;
325 }
326
327 static void
328 pvconn_pstream_close(struct pvconn *pvconn)
329 {
330 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
331 pstream_close(ps->pstream);
332 free(ps);
333 }
334
335 static int
336 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
337 {
338 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
339 struct stream *stream;
340 int error;
341
342 error = pstream_accept(ps->pstream, &stream);
343 if (error) {
344 if (error != EAGAIN) {
345 VLOG_DBG_RL(&rl, "%s: accept: %s",
346 pstream_get_name(ps->pstream), ovs_strerror(error));
347 }
348 return error;
349 }
350
351 *new_vconnp = vconn_stream_new(stream, 0, pvconn->allowed_versions);
352 return 0;
353 }
354
355 static void
356 pvconn_pstream_wait(struct pvconn *pvconn)
357 {
358 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
359 pstream_wait(ps->pstream);
360 }
361 \f
362 /* Stream-based vconns and pvconns. */
363
364 #define STREAM_INIT(NAME) \
365 { \
366 NAME, \
367 vconn_stream_open, \
368 vconn_stream_close, \
369 vconn_stream_connect, \
370 vconn_stream_recv, \
371 vconn_stream_send, \
372 vconn_stream_run, \
373 vconn_stream_run_wait, \
374 vconn_stream_wait, \
375 }
376
377 #define PSTREAM_INIT(NAME) \
378 { \
379 NAME, \
380 pvconn_pstream_listen, \
381 pvconn_pstream_close, \
382 pvconn_pstream_accept, \
383 pvconn_pstream_wait \
384 }
385
386 static const struct vconn_class stream_vconn_class = STREAM_INIT("stream");
387 static const struct pvconn_class pstream_pvconn_class = PSTREAM_INIT("pstream");
388
389 const struct vconn_class tcp_vconn_class = STREAM_INIT("tcp");
390 const struct pvconn_class ptcp_pvconn_class = PSTREAM_INIT("ptcp");
391
392 const struct vconn_class unix_vconn_class = STREAM_INIT("unix");
393 const struct pvconn_class punix_pvconn_class = PSTREAM_INIT("punix");
394
395 #ifdef HAVE_OPENSSL
396 const struct vconn_class ssl_vconn_class = STREAM_INIT("ssl");
397 const struct pvconn_class pssl_pvconn_class = PSTREAM_INIT("pssl");
398 #endif