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