leak-checker: Remove because it cannot be made thread-safe.
[sliver-openvswitch.git] / lib / vconn-stream.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 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 "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     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, uint32_t allowed_versions,
80                   char *suffix OVS_UNUSED, struct vconn **vconnp, uint8_t dscp)
81 {
82     struct stream *stream;
83     int error;
84
85     error = stream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
86                                            &stream, dscp);
87     if (!error) {
88         error = stream_connect(stream);
89         if (!error || error == EAGAIN) {
90             *vconnp = vconn_stream_new(stream, error, allowed_versions);
91             return 0;
92         }
93     }
94
95     stream_close(stream);
96     return error;
97 }
98
99 static struct vconn_stream *
100 vconn_stream_cast(struct vconn *vconn)
101 {
102     return CONTAINER_OF(vconn, struct vconn_stream, vconn);
103 }
104
105 static void
106 vconn_stream_close(struct vconn *vconn)
107 {
108     struct vconn_stream *s = vconn_stream_cast(vconn);
109
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));
113     }
114
115     stream_close(s->stream);
116     vconn_stream_clear_txbuf(s);
117     ofpbuf_delete(s->rxbuf);
118     free(s);
119 }
120
121 static int
122 vconn_stream_connect(struct vconn *vconn)
123 {
124     struct vconn_stream *s = vconn_stream_cast(vconn);
125     return stream_connect(s->stream);
126 }
127
128 static int
129 vconn_stream_recv__(struct vconn_stream *s, int rx_len)
130 {
131     struct ofpbuf *rx = s->rxbuf;
132     int want_bytes, retval;
133
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);
137     if (retval > 0) {
138         rx->size += retval;
139         return retval == want_bytes ? 0 : EAGAIN;
140     } else if (retval == 0) {
141         if (rx->size) {
142             VLOG_ERR_RL(&rl, "connection dropped mid-packet");
143             return EPROTO;
144         }
145         return EOF;
146     } else {
147         return -retval;
148     }
149 }
150
151 static int
152 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
153 {
154     struct vconn_stream *s = vconn_stream_cast(vconn);
155     const struct ofp_header *oh;
156     int rx_len;
157
158     /* Allocate new receive buffer if we don't have one. */
159     if (s->rxbuf == NULL) {
160         s->rxbuf = ofpbuf_new(1564);
161     }
162
163     /* Read ofp_header. */
164     if (s->rxbuf->size < sizeof(struct ofp_header)) {
165         int retval = vconn_stream_recv__(s, sizeof(struct ofp_header));
166         if (retval) {
167             return retval;
168         }
169     }
170
171     /* Read payload. */
172     oh = s->rxbuf->data;
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);
176         return EPROTO;
177     } else if (s->rxbuf->size < rx_len) {
178         int retval = vconn_stream_recv__(s, rx_len);
179         if (retval) {
180             return retval;
181         }
182     }
183
184     s->n_packets++;
185     *bufferp = s->rxbuf;
186     s->rxbuf = NULL;
187     return 0;
188 }
189
190 static void
191 vconn_stream_clear_txbuf(struct vconn_stream *s)
192 {
193     ofpbuf_delete(s->txbuf);
194     s->txbuf = NULL;
195 }
196
197 static int
198 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
199 {
200     struct vconn_stream *s = vconn_stream_cast(vconn);
201     ssize_t retval;
202
203     if (s->txbuf) {
204         return EAGAIN;
205     }
206
207     retval = stream_send(s->stream, buffer->data, buffer->size);
208     if (retval == buffer->size) {
209         ofpbuf_delete(buffer);
210         return 0;
211     } else if (retval >= 0 || retval == -EAGAIN) {
212         s->txbuf = buffer;
213         if (retval > 0) {
214             ofpbuf_pull(buffer, retval);
215         }
216         return 0;
217     } else {
218         return -retval;
219     }
220 }
221
222 static void
223 vconn_stream_run(struct vconn *vconn)
224 {
225     struct vconn_stream *s = vconn_stream_cast(vconn);
226     ssize_t retval;
227
228     stream_run(s->stream);
229     if (!s->txbuf) {
230         return;
231     }
232
233     retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
234     if (retval < 0) {
235         if (retval != -EAGAIN) {
236             VLOG_ERR_RL(&rl, "send: %s", strerror(-retval));
237             vconn_stream_clear_txbuf(s);
238             return;
239         }
240     } else if (retval > 0) {
241         ofpbuf_pull(s->txbuf, retval);
242         if (!s->txbuf->size) {
243             vconn_stream_clear_txbuf(s);
244             return;
245         }
246     }
247 }
248
249 static void
250 vconn_stream_run_wait(struct vconn *vconn)
251 {
252     struct vconn_stream *s = vconn_stream_cast(vconn);
253
254     stream_run_wait(s->stream);
255     if (s->txbuf) {
256         stream_send_wait(s->stream);
257     }
258 }
259
260 static void
261 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
262 {
263     struct vconn_stream *s = vconn_stream_cast(vconn);
264     switch (wait) {
265     case WAIT_CONNECT:
266         stream_connect_wait(s->stream);
267         break;
268
269     case WAIT_SEND:
270         if (!s->txbuf) {
271             stream_send_wait(s->stream);
272         } else {
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. */
277         }
278         break;
279
280     case WAIT_RECV:
281         stream_recv_wait(s->stream);
282         break;
283
284     default:
285         NOT_REACHED();
286     }
287 }
288 \f
289 /* Passive stream socket vconn. */
290
291 struct pvconn_pstream
292 {
293     struct pvconn pvconn;
294     struct pstream *pstream;
295 };
296
297 static const struct pvconn_class pstream_pvconn_class;
298
299 static struct pvconn_pstream *
300 pvconn_pstream_cast(struct pvconn *pvconn)
301 {
302     return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
303 }
304
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'.
307  *
308  * Returns 0 if successful, otherwise a positive errno value.  (The current
309  * implementation never fails.) */
310 static int
311 pvconn_pstream_listen(const char *name, uint32_t allowed_versions,
312                       char *suffix OVS_UNUSED, struct pvconn **pvconnp,
313                       uint8_t dscp)
314 {
315     struct pvconn_pstream *ps;
316     struct pstream *pstream;
317     int error;
318
319     error = pstream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
320                                             &pstream, dscp);
321     if (error) {
322         return error;
323     }
324
325     ps = xmalloc(sizeof *ps);
326     pvconn_init(&ps->pvconn, &pstream_pvconn_class, name, allowed_versions);
327     ps->pstream = pstream;
328     *pvconnp = &ps->pvconn;
329     return 0;
330 }
331
332 static void
333 pvconn_pstream_close(struct pvconn *pvconn)
334 {
335     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
336     pstream_close(ps->pstream);
337     free(ps);
338 }
339
340 static int
341 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
342 {
343     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
344     struct stream *stream;
345     int error;
346
347     error = pstream_accept(ps->pstream, &stream);
348     if (error) {
349         if (error != EAGAIN) {
350             VLOG_DBG_RL(&rl, "%s: accept: %s",
351                         pstream_get_name(ps->pstream), strerror(error));
352         }
353         return error;
354     }
355
356     *new_vconnp = vconn_stream_new(stream, 0, pvconn->allowed_versions);
357     return 0;
358 }
359
360 static void
361 pvconn_pstream_wait(struct pvconn *pvconn)
362 {
363     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
364     pstream_wait(ps->pstream);
365 }
366 \f
367 /* Stream-based vconns and pvconns. */
368
369 #define STREAM_INIT(NAME)                           \
370     {                                               \
371             NAME,                                   \
372             vconn_stream_open,                      \
373             vconn_stream_close,                     \
374             vconn_stream_connect,                   \
375             vconn_stream_recv,                      \
376             vconn_stream_send,                      \
377             vconn_stream_run,                       \
378             vconn_stream_run_wait,                  \
379             vconn_stream_wait,                      \
380     }
381
382 #define PSTREAM_INIT(NAME)                          \
383     {                                               \
384             NAME,                                   \
385             pvconn_pstream_listen,                  \
386             pvconn_pstream_close,                   \
387             pvconn_pstream_accept,                  \
388             pvconn_pstream_wait                     \
389     }
390
391 static const struct vconn_class stream_vconn_class = STREAM_INIT("stream");
392 static const struct pvconn_class pstream_pvconn_class = PSTREAM_INIT("pstream");
393
394 const struct vconn_class tcp_vconn_class = STREAM_INIT("tcp");
395 const struct pvconn_class ptcp_pvconn_class = PSTREAM_INIT("ptcp");
396
397 const struct vconn_class unix_vconn_class = STREAM_INIT("unix");
398 const struct pvconn_class punix_pvconn_class = PSTREAM_INIT("punix");
399
400 #ifdef HAVE_OPENSSL
401 const struct vconn_class ssl_vconn_class = STREAM_INIT("ssl");
402 const struct pvconn_class pssl_pvconn_class = PSTREAM_INIT("pssl");
403 #endif