]> git.proxmox.com Git - mirror_ovs.git/blob - lib/vconn-stream.c
vconn-stream: Copy stream's IP and port info into vconn at creation time.
[mirror_ovs.git] / lib / vconn-stream.c
1 /*
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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 <assert.h>
19 #include <errno.h>
20 #include <poll.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include "fatal-signal.h"
26 #include "leak-checker.h"
27 #include "ofpbuf.h"
28 #include "openflow/openflow.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
31 #include "stream.h"
32 #include "util.h"
33 #include "vconn-provider.h"
34 #include "vconn.h"
35
36 #include "vlog.h"
37 #define THIS_MODULE VLM_vconn_stream
38
39 /* Active stream socket vconn. */
40
41 struct vconn_stream
42 {
43 struct vconn vconn;
44 struct stream *stream;
45 struct ofpbuf *rxbuf;
46 struct ofpbuf *txbuf;
47 };
48
49 static struct vconn_class stream_vconn_class;
50
51 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
52
53 static void vconn_stream_clear_txbuf(struct vconn_stream *);
54 static int count_fields(const char *);
55
56 static struct vconn *
57 vconn_stream_new(struct stream *stream, int connect_status)
58 {
59 struct vconn_stream *s;
60
61 s = xmalloc(sizeof *s);
62 vconn_init(&s->vconn, &stream_vconn_class, connect_status,
63 stream_get_name(stream));
64 s->stream = stream;
65 s->txbuf = NULL;
66 s->rxbuf = NULL;
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);
71 return &s->vconn;
72 }
73
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'.
76 *
77 * Returns 0 if successful, otherwise a positive errno value. */
78 static int
79 vconn_stream_open(const char *name_, char *suffix OVS_UNUSED,
80 struct vconn **vconnp)
81 {
82 struct stream *stream;
83 char *name;
84 int error;
85
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);
90 } else {
91 name = xstrdup(name_);
92 }
93 error = stream_open(name, &stream);
94 free(name);
95
96 if (error && error != EAGAIN) {
97 return error;
98 }
99
100 *vconnp = vconn_stream_new(stream, error);
101 return 0;
102 }
103
104 static struct vconn_stream *
105 vconn_stream_cast(struct vconn *vconn)
106 {
107 return CONTAINER_OF(vconn, struct vconn_stream, vconn);
108 }
109
110 static void
111 vconn_stream_close(struct vconn *vconn)
112 {
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);
117 free(s);
118 }
119
120 static int
121 vconn_stream_connect(struct vconn *vconn)
122 {
123 struct vconn_stream *s = vconn_stream_cast(vconn);
124 return stream_connect(s->stream);
125 }
126
127 static int
128 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
129 {
130 struct vconn_stream *s = vconn_stream_cast(vconn);
131 struct ofpbuf *rx;
132 size_t want_bytes;
133 ssize_t retval;
134
135 if (s->rxbuf == NULL) {
136 s->rxbuf = ofpbuf_new(1564);
137 }
138 rx = s->rxbuf;
139
140 again:
141 if (sizeof(struct ofp_header) > rx->size) {
142 want_bytes = sizeof(struct ofp_header) - rx->size;
143 } else {
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)",
148 length);
149 return EPROTO;
150 }
151 want_bytes = length - rx->size;
152 if (!want_bytes) {
153 *bufferp = rx;
154 s->rxbuf = NULL;
155 return 0;
156 }
157 }
158 ofpbuf_prealloc_tailroom(rx, want_bytes);
159
160 retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
161 if (retval > 0) {
162 rx->size += retval;
163 if (retval == want_bytes) {
164 if (rx->size > sizeof(struct ofp_header)) {
165 *bufferp = rx;
166 s->rxbuf = NULL;
167 return 0;
168 } else {
169 goto again;
170 }
171 }
172 return EAGAIN;
173 } else if (retval == 0) {
174 if (rx->size) {
175 VLOG_ERR_RL(&rl, "connection dropped mid-packet");
176 return EPROTO;
177 } else {
178 return EOF;
179 }
180 } else {
181 return -retval;
182 }
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, buffer->data, buffer->size);
203 if (retval == buffer->size) {
204 ofpbuf_delete(buffer);
205 return 0;
206 } else if (retval >= 0 || retval == -EAGAIN) {
207 leak_checker_claim(buffer);
208 s->txbuf = buffer;
209 if (retval > 0) {
210 ofpbuf_pull(buffer, retval);
211 }
212 return 0;
213 } else {
214 return -retval;
215 }
216 }
217
218 static void
219 vconn_stream_run(struct vconn *vconn)
220 {
221 struct vconn_stream *s = vconn_stream_cast(vconn);
222 ssize_t retval;
223
224 if (!s->txbuf) {
225 return;
226 }
227
228 retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
229 if (retval < 0) {
230 if (retval != -EAGAIN) {
231 VLOG_ERR_RL(&rl, "send: %s", strerror(-retval));
232 vconn_stream_clear_txbuf(s);
233 return;
234 }
235 } else if (retval > 0) {
236 ofpbuf_pull(s->txbuf, retval);
237 if (!s->txbuf->size) {
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 if (s->txbuf) {
250 stream_send_wait(s->stream);
251 }
252 }
253
254 static void
255 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
256 {
257 struct vconn_stream *s = vconn_stream_cast(vconn);
258 switch (wait) {
259 case WAIT_CONNECT:
260 stream_connect_wait(s->stream);
261 break;
262
263 case WAIT_SEND:
264 if (!s->txbuf) {
265 stream_send_wait(s->stream);
266 } else {
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. */
271 }
272 break;
273
274 case WAIT_RECV:
275 stream_recv_wait(s->stream);
276 break;
277
278 default:
279 NOT_REACHED();
280 }
281 }
282 \f
283 /* Passive stream socket vconn. */
284
285 struct pvconn_pstream
286 {
287 struct pvconn pvconn;
288 struct pstream *pstream;
289 };
290
291 static struct pvconn_class pstream_pvconn_class;
292
293 static struct pvconn_pstream *
294 pvconn_pstream_cast(struct pvconn *pvconn)
295 {
296 return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
297 }
298
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'.
301 *
302 * Returns 0 if successful, otherwise a positive errno value. (The current
303 * implementation never fails.) */
304 static int
305 pvconn_pstream_listen(const char *name_, char *suffix OVS_UNUSED,
306 struct pvconn **pvconnp)
307 {
308 struct pvconn_pstream *ps;
309 struct pstream *pstream;
310 char *name;
311 int error;
312
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);
317 } else {
318 name = xstrdup(name_);
319 }
320 error = pstream_open(name, &pstream);
321 free(name);
322 if (error) {
323 return error;
324 }
325
326 ps = xmalloc(sizeof *ps);
327 pvconn_init(&ps->pvconn, &pstream_pvconn_class, name_);
328 ps->pstream = pstream;
329 *pvconnp = &ps->pvconn;
330 return 0;
331 }
332
333 static void
334 pvconn_pstream_close(struct pvconn *pvconn)
335 {
336 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
337 pstream_close(ps->pstream);
338 free(ps);
339 }
340
341 static int
342 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
343 {
344 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
345 struct stream *stream;
346 int error;
347
348 error = pstream_accept(ps->pstream, &stream);
349 if (error) {
350 if (error != EAGAIN) {
351 VLOG_DBG_RL(&rl, "%s: accept: %s",
352 pstream_get_name(ps->pstream), strerror(error));
353 }
354 return error;
355 }
356
357 *new_vconnp = vconn_stream_new(stream, 0);
358 return 0;
359 }
360
361 static void
362 pvconn_pstream_wait(struct pvconn *pvconn)
363 {
364 struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
365 pstream_wait(ps->pstream);
366 }
367 \f
368 static int
369 count_fields(const char *s_)
370 {
371 char *s, *field, *save_ptr;
372 int n = 0;
373
374 save_ptr = NULL;
375 s = xstrdup(s_);
376 for (field = strtok_r(s, ":", &save_ptr); field != NULL;
377 field = strtok_r(NULL, ":", &save_ptr)) {
378 n++;
379 }
380 free(s);
381
382 return n;
383 }
384 \f
385 /* Stream-based vconns and pvconns. */
386
387 #define DEFINE_VCONN_STREAM_CLASS(NAME) \
388 struct vconn_class NAME##_vconn_class = { \
389 #NAME, \
390 vconn_stream_open, \
391 vconn_stream_close, \
392 vconn_stream_connect, \
393 vconn_stream_recv, \
394 vconn_stream_send, \
395 vconn_stream_run, \
396 vconn_stream_run_wait, \
397 vconn_stream_wait, \
398 };
399
400 #define DEFINE_PVCONN_STREAM_CLASS(NAME) \
401 struct pvconn_class NAME##_pvconn_class = { \
402 #NAME, \
403 pvconn_pstream_listen, \
404 pvconn_pstream_close, \
405 pvconn_pstream_accept, \
406 pvconn_pstream_wait \
407 };
408
409 static DEFINE_VCONN_STREAM_CLASS(stream);
410 static DEFINE_PVCONN_STREAM_CLASS(pstream);
411
412 DEFINE_VCONN_STREAM_CLASS(tcp);
413 DEFINE_PVCONN_STREAM_CLASS(ptcp);
414
415 DEFINE_VCONN_STREAM_CLASS(unix);
416 DEFINE_PVCONN_STREAM_CLASS(punix);
417
418 #ifdef HAVE_OPENSSL
419 DEFINE_VCONN_STREAM_CLASS(ssl);
420 DEFINE_PVCONN_STREAM_CLASS(pssl);
421 #endif